1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/clk.h>
22#include <linux/device.h>
23#include <linux/io.h>
24#include <linux/module.h>
25#include <linux/nvmem-provider.h>
26#include <linux/of.h>
27#include <linux/of_device.h>
28#include <linux/platform_device.h>
29#include <linux/slab.h>
30#include <linux/delay.h>
31
32#define IMX_OCOTP_OFFSET_B0W0 0x400
33
34
35#define IMX_OCOTP_OFFSET_PER_WORD 0x10
36
37
38
39#define IMX_OCOTP_ADDR_CTRL 0x0000
40#define IMX_OCOTP_ADDR_CTRL_SET 0x0004
41#define IMX_OCOTP_ADDR_CTRL_CLR 0x0008
42#define IMX_OCOTP_ADDR_TIMING 0x0010
43#define IMX_OCOTP_ADDR_DATA0 0x0020
44#define IMX_OCOTP_ADDR_DATA1 0x0030
45#define IMX_OCOTP_ADDR_DATA2 0x0040
46#define IMX_OCOTP_ADDR_DATA3 0x0050
47
48#define IMX_OCOTP_BM_CTRL_ADDR 0x0000007F
49#define IMX_OCOTP_BM_CTRL_BUSY 0x00000100
50#define IMX_OCOTP_BM_CTRL_ERROR 0x00000200
51#define IMX_OCOTP_BM_CTRL_REL_SHADOWS 0x00000400
52
53#define DEF_RELAX 20
54#define DEF_FSOURCE 1001
55#define DEF_STROBE_PROG 10000
56#define IMX_OCOTP_WR_UNLOCK 0x3E770000
57#define IMX_OCOTP_READ_LOCKED_VAL 0xBADABADA
58
59static DEFINE_MUTEX(ocotp_mutex);
60
61struct ocotp_priv {
62 struct device *dev;
63 struct clk *clk;
64 void __iomem *base;
65 const struct ocotp_params *params;
66 struct nvmem_config *config;
67};
68
69struct ocotp_params {
70 unsigned int nregs;
71 unsigned int bank_address_words;
72 void (*set_timing)(struct ocotp_priv *priv);
73};
74
75static int imx_ocotp_wait_for_busy(void __iomem *base, u32 flags)
76{
77 int count;
78 u32 c, mask;
79
80 mask = IMX_OCOTP_BM_CTRL_BUSY | IMX_OCOTP_BM_CTRL_ERROR | flags;
81
82 for (count = 10000; count >= 0; count--) {
83 c = readl(base + IMX_OCOTP_ADDR_CTRL);
84 if (!(c & mask))
85 break;
86 cpu_relax();
87 }
88
89 if (count < 0) {
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 if (c & IMX_OCOTP_BM_CTRL_ERROR)
105 return -EPERM;
106 return -ETIMEDOUT;
107 }
108
109 return 0;
110}
111
112static void imx_ocotp_clr_err_if_set(void __iomem *base)
113{
114 u32 c;
115
116 c = readl(base + IMX_OCOTP_ADDR_CTRL);
117 if (!(c & IMX_OCOTP_BM_CTRL_ERROR))
118 return;
119
120 writel(IMX_OCOTP_BM_CTRL_ERROR, base + IMX_OCOTP_ADDR_CTRL_CLR);
121}
122
123static int imx_ocotp_read(void *context, unsigned int offset,
124 void *val, size_t bytes)
125{
126 struct ocotp_priv *priv = context;
127 unsigned int count;
128 u32 *buf = val;
129 int i, ret;
130 u32 index;
131
132 index = offset >> 2;
133 count = bytes >> 2;
134
135 if (count > (priv->params->nregs - index))
136 count = priv->params->nregs - index;
137
138 mutex_lock(&ocotp_mutex);
139
140 ret = clk_prepare_enable(priv->clk);
141 if (ret < 0) {
142 mutex_unlock(&ocotp_mutex);
143 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
144 return ret;
145 }
146
147 ret = imx_ocotp_wait_for_busy(priv->base, 0);
148 if (ret < 0) {
149 dev_err(priv->dev, "timeout during read setup\n");
150 goto read_end;
151 }
152
153 for (i = index; i < (index + count); i++) {
154 *buf++ = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 +
155 i * IMX_OCOTP_OFFSET_PER_WORD);
156
157
158
159
160
161
162
163 if (*(buf - 1) == IMX_OCOTP_READ_LOCKED_VAL)
164 imx_ocotp_clr_err_if_set(priv->base);
165 }
166 ret = 0;
167
168read_end:
169 clk_disable_unprepare(priv->clk);
170 mutex_unlock(&ocotp_mutex);
171 return ret;
172}
173
174static void imx_ocotp_set_imx6_timing(struct ocotp_priv *priv)
175{
176 unsigned long clk_rate = 0;
177 unsigned long strobe_read, relax, strobe_prog;
178 u32 timing = 0;
179
180
181
182
183
184
185
186 clk_rate = clk_get_rate(priv->clk);
187
188 relax = clk_rate / (1000000000 / DEF_RELAX) - 1;
189 strobe_prog = clk_rate / (1000000000 / 10000) + 2 * (DEF_RELAX + 1) - 1;
190 strobe_read = clk_rate / (1000000000 / 40) + 2 * (DEF_RELAX + 1) - 1;
191
192 timing = strobe_prog & 0x00000FFF;
193 timing |= (relax << 12) & 0x0000F000;
194 timing |= (strobe_read << 16) & 0x003F0000;
195
196 writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
197}
198
199static void imx_ocotp_set_imx7_timing(struct ocotp_priv *priv)
200{
201 unsigned long clk_rate = 0;
202 u64 fsource, strobe_prog;
203 u32 timing = 0;
204
205
206
207
208 clk_rate = clk_get_rate(priv->clk);
209 fsource = DIV_ROUND_UP_ULL((u64)clk_rate * DEF_FSOURCE,
210 NSEC_PER_SEC) + 1;
211 strobe_prog = DIV_ROUND_CLOSEST_ULL((u64)clk_rate * DEF_STROBE_PROG,
212 NSEC_PER_SEC) + 1;
213
214 timing = strobe_prog & 0x00000FFF;
215 timing |= (fsource << 12) & 0x000FF000;
216
217 writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
218}
219
220static int imx_ocotp_write(void *context, unsigned int offset, void *val,
221 size_t bytes)
222{
223 struct ocotp_priv *priv = context;
224 u32 *buf = val;
225 int ret;
226
227 u32 ctrl;
228 u8 waddr;
229 u8 word = 0;
230
231
232 if ((bytes != priv->config->word_size) ||
233 (offset % priv->config->word_size))
234 return -EINVAL;
235
236 mutex_lock(&ocotp_mutex);
237
238 ret = clk_prepare_enable(priv->clk);
239 if (ret < 0) {
240 mutex_unlock(&ocotp_mutex);
241 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
242 return ret;
243 }
244
245
246 priv->params->set_timing(priv);
247
248
249
250
251
252
253
254 ret = imx_ocotp_wait_for_busy(priv->base, 0);
255 if (ret < 0) {
256 dev_err(priv->dev, "timeout during timing setup\n");
257 goto write_end;
258 }
259
260
261
262
263
264
265
266
267 if (priv->params->bank_address_words != 0) {
268
269
270
271
272
273 offset = offset / priv->config->word_size;
274 waddr = offset / priv->params->bank_address_words;
275 word = offset & (priv->params->bank_address_words - 1);
276 } else {
277
278
279
280
281
282 waddr = offset / 4;
283 }
284
285 ctrl = readl(priv->base + IMX_OCOTP_ADDR_CTRL);
286 ctrl &= ~IMX_OCOTP_BM_CTRL_ADDR;
287 ctrl |= waddr & IMX_OCOTP_BM_CTRL_ADDR;
288 ctrl |= IMX_OCOTP_WR_UNLOCK;
289
290 writel(ctrl, priv->base + IMX_OCOTP_ADDR_CTRL);
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314 if (priv->params->bank_address_words != 0) {
315
316 switch (word) {
317 case 0:
318 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
319 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
320 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
321 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
322 break;
323 case 1:
324 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA1);
325 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
326 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
327 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
328 break;
329 case 2:
330 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
331 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA2);
332 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
333 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
334 break;
335 case 3:
336 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
337 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
338 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA3);
339 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
340 break;
341 }
342 } else {
343
344 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
345 }
346
347
348
349
350
351
352
353
354 ret = imx_ocotp_wait_for_busy(priv->base, 0);
355 if (ret < 0) {
356 if (ret == -EPERM) {
357 dev_err(priv->dev, "failed write to locked region");
358 imx_ocotp_clr_err_if_set(priv->base);
359 } else {
360 dev_err(priv->dev, "timeout during data write\n");
361 }
362 goto write_end;
363 }
364
365
366
367
368
369
370
371 udelay(2);
372
373
374 writel(IMX_OCOTP_BM_CTRL_REL_SHADOWS,
375 priv->base + IMX_OCOTP_ADDR_CTRL_SET);
376 ret = imx_ocotp_wait_for_busy(priv->base,
377 IMX_OCOTP_BM_CTRL_REL_SHADOWS);
378 if (ret < 0) {
379 dev_err(priv->dev, "timeout during shadow register reload\n");
380 goto write_end;
381 }
382
383write_end:
384 clk_disable_unprepare(priv->clk);
385 mutex_unlock(&ocotp_mutex);
386 if (ret < 0)
387 return ret;
388 return bytes;
389}
390
391static struct nvmem_config imx_ocotp_nvmem_config = {
392 .name = "imx-ocotp",
393 .read_only = false,
394 .word_size = 4,
395 .stride = 4,
396 .reg_read = imx_ocotp_read,
397 .reg_write = imx_ocotp_write,
398};
399
400static const struct ocotp_params imx6q_params = {
401 .nregs = 128,
402 .bank_address_words = 0,
403 .set_timing = imx_ocotp_set_imx6_timing,
404};
405
406static const struct ocotp_params imx6sl_params = {
407 .nregs = 64,
408 .bank_address_words = 0,
409 .set_timing = imx_ocotp_set_imx6_timing,
410};
411
412static const struct ocotp_params imx6sx_params = {
413 .nregs = 128,
414 .bank_address_words = 0,
415 .set_timing = imx_ocotp_set_imx6_timing,
416};
417
418static const struct ocotp_params imx6ul_params = {
419 .nregs = 128,
420 .bank_address_words = 0,
421 .set_timing = imx_ocotp_set_imx6_timing,
422};
423
424static const struct ocotp_params imx7d_params = {
425 .nregs = 64,
426 .bank_address_words = 4,
427 .set_timing = imx_ocotp_set_imx7_timing,
428};
429
430static const struct of_device_id imx_ocotp_dt_ids[] = {
431 { .compatible = "fsl,imx6q-ocotp", .data = &imx6q_params },
432 { .compatible = "fsl,imx6sl-ocotp", .data = &imx6sl_params },
433 { .compatible = "fsl,imx6sx-ocotp", .data = &imx6sx_params },
434 { .compatible = "fsl,imx6ul-ocotp", .data = &imx6ul_params },
435 { .compatible = "fsl,imx7d-ocotp", .data = &imx7d_params },
436 { },
437};
438MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
439
440static int imx_ocotp_probe(struct platform_device *pdev)
441{
442 struct device *dev = &pdev->dev;
443 struct resource *res;
444 struct ocotp_priv *priv;
445 struct nvmem_device *nvmem;
446
447 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
448 if (!priv)
449 return -ENOMEM;
450
451 priv->dev = dev;
452
453 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
454 priv->base = devm_ioremap_resource(dev, res);
455 if (IS_ERR(priv->base))
456 return PTR_ERR(priv->base);
457
458 priv->clk = devm_clk_get(dev, NULL);
459 if (IS_ERR(priv->clk))
460 return PTR_ERR(priv->clk);
461
462 priv->params = of_device_get_match_data(&pdev->dev);
463 imx_ocotp_nvmem_config.size = 4 * priv->params->nregs;
464 imx_ocotp_nvmem_config.dev = dev;
465 imx_ocotp_nvmem_config.priv = priv;
466 priv->config = &imx_ocotp_nvmem_config;
467 nvmem = devm_nvmem_register(dev, &imx_ocotp_nvmem_config);
468
469
470 return PTR_ERR_OR_ZERO(nvmem);
471}
472
473static struct platform_driver imx_ocotp_driver = {
474 .probe = imx_ocotp_probe,
475 .driver = {
476 .name = "imx_ocotp",
477 .of_match_table = imx_ocotp_dt_ids,
478 },
479};
480module_platform_driver(imx_ocotp_driver);
481
482MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
483MODULE_DESCRIPTION("i.MX6/i.MX7 OCOTP fuse box driver");
484MODULE_LICENSE("GPL v2");
485