summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Blumenstingl <martin.blumenstingl@googlemail.com>2017-10-09 09:26:41 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2017-10-20 09:38:02 -0400
commit8caef1fa9176c4789b74c806434517b3adf7544a (patch)
tree251d8cb6f6a8e96679a29f7e7e1af60ff1110012
parent9593ad32b8be82f3abd6002336a93c405361b9fd (diff)
nvmem: add a driver for the Amlogic Meson6/Meson8/Meson8b SoCs
This adds a driver to access the efuse on Amlogic Meson6, Meson8 and Meson8b SoCs. These SoCs are accessing the efuse IP block directly through the registers in the "secbus" region. This makes it different from the Meson GX efuse driver which uses the "secure monitor" firmware to access the efuse. The efuse on Meson6 can only read one byte at a time, while the efuse on Meson8 and Meson8b always reads 4 bytes at a time. The new driver supports both, but due to lack of hardware Meson6 support was not tested. The hardware also supports writing. However, this is currently not supported by the driver. Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com> Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--drivers/nvmem/Kconfig10
-rw-r--r--drivers/nvmem/Makefile2
-rw-r--r--drivers/nvmem/meson-mx-efuse.c265
3 files changed, 277 insertions, 0 deletions
diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
index 2a07cf169b52..eb09916744bf 100644
--- a/drivers/nvmem/Kconfig
+++ b/drivers/nvmem/Kconfig
@@ -144,6 +144,16 @@ config MESON_EFUSE
144 This driver can also be built as a module. If so, the module 144 This driver can also be built as a module. If so, the module
145 will be called nvmem_meson_efuse. 145 will be called nvmem_meson_efuse.
146 146
147config MESON_MX_EFUSE
148 tristate "Amlogic Meson6/Meson8/Meson8b eFuse Support"
149 depends on ARCH_MESON || COMPILE_TEST
150 help
151 This is a driver to retrieve specific values from the eFuse found on
152 the Amlogic Meson6, Meson8 and Meson8b SoCs.
153
154 This driver can also be built as a module. If so, the module
155 will be called nvmem_meson_mx_efuse.
156
147config NVMEM_SNVS_LPGPR 157config NVMEM_SNVS_LPGPR
148 tristate "Support for Low Power General Purpose Register" 158 tristate "Support for Low Power General Purpose Register"
149 depends on SOC_IMX6 || COMPILE_TEST 159 depends on SOC_IMX6 || COMPILE_TEST
diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile
index 4c589184acee..362f394da65d 100644
--- a/drivers/nvmem/Makefile
+++ b/drivers/nvmem/Makefile
@@ -30,5 +30,7 @@ obj-$(CONFIG_NVMEM_VF610_OCOTP) += nvmem-vf610-ocotp.o
30nvmem-vf610-ocotp-y := vf610-ocotp.o 30nvmem-vf610-ocotp-y := vf610-ocotp.o
31obj-$(CONFIG_MESON_EFUSE) += nvmem_meson_efuse.o 31obj-$(CONFIG_MESON_EFUSE) += nvmem_meson_efuse.o
32nvmem_meson_efuse-y := meson-efuse.o 32nvmem_meson_efuse-y := meson-efuse.o
33obj-$(CONFIG_MESON_MX_EFUSE) += nvmem_meson_mx_efuse.o
34nvmem_meson_mx_efuse-y := meson-mx-efuse.o
33obj-$(CONFIG_NVMEM_SNVS_LPGPR) += nvmem_snvs_lpgpr.o 35obj-$(CONFIG_NVMEM_SNVS_LPGPR) += nvmem_snvs_lpgpr.o
34nvmem_snvs_lpgpr-y := snvs_lpgpr.o 36nvmem_snvs_lpgpr-y := snvs_lpgpr.o
diff --git a/drivers/nvmem/meson-mx-efuse.c b/drivers/nvmem/meson-mx-efuse.c
new file mode 100644
index 000000000000..a346b4923550
--- /dev/null
+++ b/drivers/nvmem/meson-mx-efuse.c
@@ -0,0 +1,265 @@
1/*
2 * Amlogic Meson6, Meson8 and Meson8b eFuse Driver
3 *
4 * Copyright (c) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 */
15
16#include <linux/bitfield.h>
17#include <linux/bitops.h>
18#include <linux/clk.h>
19#include <linux/delay.h>
20#include <linux/io.h>
21#include <linux/iopoll.h>
22#include <linux/module.h>
23#include <linux/nvmem-provider.h>
24#include <linux/of.h>
25#include <linux/of_device.h>
26#include <linux/platform_device.h>
27#include <linux/sizes.h>
28#include <linux/slab.h>
29
30#define MESON_MX_EFUSE_CNTL1 0x04
31#define MESON_MX_EFUSE_CNTL1_PD_ENABLE BIT(27)
32#define MESON_MX_EFUSE_CNTL1_AUTO_RD_BUSY BIT(26)
33#define MESON_MX_EFUSE_CNTL1_AUTO_RD_START BIT(25)
34#define MESON_MX_EFUSE_CNTL1_AUTO_RD_ENABLE BIT(24)
35#define MESON_MX_EFUSE_CNTL1_BYTE_WR_DATA GENMASK(23, 16)
36#define MESON_MX_EFUSE_CNTL1_AUTO_WR_BUSY BIT(14)
37#define MESON_MX_EFUSE_CNTL1_AUTO_WR_START BIT(13)
38#define MESON_MX_EFUSE_CNTL1_AUTO_WR_ENABLE BIT(12)
39#define MESON_MX_EFUSE_CNTL1_BYTE_ADDR_SET BIT(11)
40#define MESON_MX_EFUSE_CNTL1_BYTE_ADDR_MASK GENMASK(10, 0)
41
42#define MESON_MX_EFUSE_CNTL2 0x08
43
44#define MESON_MX_EFUSE_CNTL4 0x10
45#define MESON_MX_EFUSE_CNTL4_ENCRYPT_ENABLE BIT(10)
46
47struct meson_mx_efuse_platform_data {
48 const char *name;
49 unsigned int word_size;
50};
51
52struct meson_mx_efuse {
53 void __iomem *base;
54 struct clk *core_clk;
55 struct nvmem_device *nvmem;
56 struct nvmem_config config;
57};
58
59static void meson_mx_efuse_mask_bits(struct meson_mx_efuse *efuse, u32 reg,
60 u32 mask, u32 set)
61{
62 u32 data;
63
64 data = readl(efuse->base + reg);
65 data &= ~mask;
66 data |= (set & mask);
67
68 writel(data, efuse->base + reg);
69}
70
71static int meson_mx_efuse_hw_enable(struct meson_mx_efuse *efuse)
72{
73 int err;
74
75 err = clk_prepare_enable(efuse->core_clk);
76 if (err)
77 return err;
78
79 /* power up the efuse */
80 meson_mx_efuse_mask_bits(efuse, MESON_MX_EFUSE_CNTL1,
81 MESON_MX_EFUSE_CNTL1_PD_ENABLE, 0);
82
83 meson_mx_efuse_mask_bits(efuse, MESON_MX_EFUSE_CNTL4,
84 MESON_MX_EFUSE_CNTL4_ENCRYPT_ENABLE, 0);
85
86 return 0;
87}
88
89static void meson_mx_efuse_hw_disable(struct meson_mx_efuse *efuse)
90{
91 meson_mx_efuse_mask_bits(efuse, MESON_MX_EFUSE_CNTL1,
92 MESON_MX_EFUSE_CNTL1_PD_ENABLE,
93 MESON_MX_EFUSE_CNTL1_PD_ENABLE);
94
95 clk_disable_unprepare(efuse->core_clk);
96}
97
98static int meson_mx_efuse_read_addr(struct meson_mx_efuse *efuse,
99 unsigned int addr, u32 *value)
100{
101 int err;
102 u32 regval;
103
104 /* write the address to read */
105 regval = FIELD_PREP(MESON_MX_EFUSE_CNTL1_BYTE_ADDR_MASK, addr);
106 meson_mx_efuse_mask_bits(efuse, MESON_MX_EFUSE_CNTL1,
107 MESON_MX_EFUSE_CNTL1_BYTE_ADDR_MASK, regval);
108
109 /* inform the hardware that we changed the address */
110 meson_mx_efuse_mask_bits(efuse, MESON_MX_EFUSE_CNTL1,
111 MESON_MX_EFUSE_CNTL1_BYTE_ADDR_SET,
112 MESON_MX_EFUSE_CNTL1_BYTE_ADDR_SET);
113 meson_mx_efuse_mask_bits(efuse, MESON_MX_EFUSE_CNTL1,
114 MESON_MX_EFUSE_CNTL1_BYTE_ADDR_SET, 0);
115
116 /* start the read process */
117 meson_mx_efuse_mask_bits(efuse, MESON_MX_EFUSE_CNTL1,
118 MESON_MX_EFUSE_CNTL1_AUTO_RD_START,
119 MESON_MX_EFUSE_CNTL1_AUTO_RD_START);
120 meson_mx_efuse_mask_bits(efuse, MESON_MX_EFUSE_CNTL1,
121 MESON_MX_EFUSE_CNTL1_AUTO_RD_START, 0);
122
123 /*
124 * perform a dummy read to ensure that the HW has the RD_BUSY bit set
125 * when polling for the status below.
126 */
127 readl(efuse->base + MESON_MX_EFUSE_CNTL1);
128
129 err = readl_poll_timeout_atomic(efuse->base + MESON_MX_EFUSE_CNTL1,
130 regval,
131 (!(regval & MESON_MX_EFUSE_CNTL1_AUTO_RD_BUSY)),
132 1, 1000);
133 if (err) {
134 dev_err(efuse->config.dev,
135 "Timeout while reading efuse address %u\n", addr);
136 return err;
137 }
138
139 *value = readl(efuse->base + MESON_MX_EFUSE_CNTL2);
140
141 return 0;
142}
143
144static int meson_mx_efuse_read(void *context, unsigned int offset,
145 void *buf, size_t bytes)
146{
147 struct meson_mx_efuse *efuse = context;
148 u32 tmp;
149 int err, i, addr;
150
151 err = meson_mx_efuse_hw_enable(efuse);
152 if (err)
153 return err;
154
155 meson_mx_efuse_mask_bits(efuse, MESON_MX_EFUSE_CNTL1,
156 MESON_MX_EFUSE_CNTL1_AUTO_RD_ENABLE,
157 MESON_MX_EFUSE_CNTL1_AUTO_RD_ENABLE);
158
159 for (i = offset; i < offset + bytes; i += efuse->config.word_size) {
160 addr = i / efuse->config.word_size;
161
162 err = meson_mx_efuse_read_addr(efuse, addr, &tmp);
163 if (err)
164 break;
165
166 memcpy(buf + i, &tmp, efuse->config.word_size);
167 }
168
169 meson_mx_efuse_mask_bits(efuse, MESON_MX_EFUSE_CNTL1,
170 MESON_MX_EFUSE_CNTL1_AUTO_RD_ENABLE, 0);
171
172 meson_mx_efuse_hw_disable(efuse);
173
174 return err;
175}
176
177static const struct meson_mx_efuse_platform_data meson6_efuse_data = {
178 .name = "meson6-efuse",
179 .word_size = 1,
180};
181
182static const struct meson_mx_efuse_platform_data meson8_efuse_data = {
183 .name = "meson8-efuse",
184 .word_size = 4,
185};
186
187static const struct meson_mx_efuse_platform_data meson8b_efuse_data = {
188 .name = "meson8b-efuse",
189 .word_size = 4,
190};
191
192static const struct of_device_id meson_mx_efuse_match[] = {
193 { .compatible = "amlogic,meson6-efuse", .data = &meson6_efuse_data },
194 { .compatible = "amlogic,meson8-efuse", .data = &meson8_efuse_data },
195 { .compatible = "amlogic,meson8b-efuse", .data = &meson8b_efuse_data },
196 { /* sentinel */ },
197};
198MODULE_DEVICE_TABLE(of, meson_mx_efuse_match);
199
200static int meson_mx_efuse_probe(struct platform_device *pdev)
201{
202 const struct meson_mx_efuse_platform_data *drvdata;
203 struct meson_mx_efuse *efuse;
204 struct resource *res;
205
206 drvdata = of_device_get_match_data(&pdev->dev);
207 if (!drvdata)
208 return -EINVAL;
209
210 efuse = devm_kzalloc(&pdev->dev, sizeof(*efuse), GFP_KERNEL);
211 if (!efuse)
212 return -ENOMEM;
213
214 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
215 efuse->base = devm_ioremap_resource(&pdev->dev, res);
216 if (IS_ERR(efuse->base))
217 return PTR_ERR(efuse->base);
218
219 efuse->config.name = devm_kstrdup(&pdev->dev, drvdata->name,
220 GFP_KERNEL);
221 efuse->config.owner = THIS_MODULE;
222 efuse->config.dev = &pdev->dev;
223 efuse->config.priv = efuse;
224 efuse->config.stride = drvdata->word_size;
225 efuse->config.word_size = drvdata->word_size;
226 efuse->config.size = SZ_512;
227 efuse->config.read_only = true;
228 efuse->config.reg_read = meson_mx_efuse_read;
229
230 efuse->core_clk = devm_clk_get(&pdev->dev, "core");
231 if (IS_ERR(efuse->core_clk)) {
232 dev_err(&pdev->dev, "Failed to get core clock\n");
233 return PTR_ERR(efuse->core_clk);
234 }
235
236 efuse->nvmem = nvmem_register(&efuse->config);
237 if (IS_ERR(efuse->nvmem))
238 return PTR_ERR(efuse->nvmem);
239
240 platform_set_drvdata(pdev, efuse);
241
242 return 0;
243}
244
245static int meson_mx_efuse_remove(struct platform_device *pdev)
246{
247 struct meson_mx_efuse *efuse = platform_get_drvdata(pdev);
248
249 return nvmem_unregister(efuse->nvmem);
250}
251
252static struct platform_driver meson_mx_efuse_driver = {
253 .probe = meson_mx_efuse_probe,
254 .remove = meson_mx_efuse_remove,
255 .driver = {
256 .name = "meson-mx-efuse",
257 .of_match_table = meson_mx_efuse_match,
258 },
259};
260
261module_platform_driver(meson_mx_efuse_driver);
262
263MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
264MODULE_DESCRIPTION("Amlogic Meson MX eFuse NVMEM driver");
265MODULE_LICENSE("GPL v2");