1 // SPDX-License-Identifier: GPL-2.0-only
3 * i.MX6 OCOTP fusebox driver
7 * Based on the barebox ocotp driver,
9 * Orex Computed Radiography
11 * Write support based on the fsl_otp driver,
12 * Copyright (C) 2010-2013 Freescale Semiconductor, Inc
15 #include <linux/clk.h>
16 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/nvmem-provider.h>
21 #include <linux/of_device.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
26 #define IMX_OCOTP_OFFSET_B0W0 0x400 /* Offset from base address of the
29 #define IMX_OCOTP_OFFSET_PER_WORD 0x10 /* Offset between the start addr
30 * of two consecutive OTP words.
33 #define IMX_OCOTP_ADDR_CTRL 0x0000
34 #define IMX_OCOTP_ADDR_CTRL_SET 0x0004
35 #define IMX_OCOTP_ADDR_CTRL_CLR 0x0008
36 #define IMX_OCOTP_ADDR_TIMING 0x0010
37 #define IMX_OCOTP_ADDR_DATA0 0x0020
38 #define IMX_OCOTP_ADDR_DATA1 0x0030
39 #define IMX_OCOTP_ADDR_DATA2 0x0040
40 #define IMX_OCOTP_ADDR_DATA3 0x0050
42 #define IMX_OCOTP_BM_CTRL_ADDR 0x000000FF
43 #define IMX_OCOTP_BM_CTRL_BUSY 0x00000100
44 #define IMX_OCOTP_BM_CTRL_ERROR 0x00000200
45 #define IMX_OCOTP_BM_CTRL_REL_SHADOWS 0x00000400
47 #define IMX_OCOTP_BM_CTRL_ADDR_8MP 0x000001FF
48 #define IMX_OCOTP_BM_CTRL_BUSY_8MP 0x00000200
49 #define IMX_OCOTP_BM_CTRL_ERROR_8MP 0x00000400
50 #define IMX_OCOTP_BM_CTRL_REL_SHADOWS_8MP 0x00000800
52 #define IMX_OCOTP_BM_CTRL_DEFAULT \
54 .bm_addr = IMX_OCOTP_BM_CTRL_ADDR, \
55 .bm_busy = IMX_OCOTP_BM_CTRL_BUSY, \
56 .bm_error = IMX_OCOTP_BM_CTRL_ERROR, \
57 .bm_rel_shadows = IMX_OCOTP_BM_CTRL_REL_SHADOWS,\
60 #define IMX_OCOTP_BM_CTRL_8MP \
62 .bm_addr = IMX_OCOTP_BM_CTRL_ADDR_8MP, \
63 .bm_busy = IMX_OCOTP_BM_CTRL_BUSY_8MP, \
64 .bm_error = IMX_OCOTP_BM_CTRL_ERROR_8MP, \
65 .bm_rel_shadows = IMX_OCOTP_BM_CTRL_REL_SHADOWS_8MP,\
68 #define TIMING_STROBE_PROG_US 10 /* Min time to blow a fuse */
69 #define TIMING_STROBE_READ_NS 37 /* Min time before read */
70 #define TIMING_RELAX_NS 17
71 #define DEF_FSOURCE 1001 /* > 1000 ns */
72 #define DEF_STROBE_PROG 10000 /* IPG clocks */
73 #define IMX_OCOTP_WR_UNLOCK 0x3E770000
74 #define IMX_OCOTP_READ_LOCKED_VAL 0xBADABADA
76 static DEFINE_MUTEX(ocotp_mutex);
82 const struct ocotp_params *params;
83 struct nvmem_config *config;
86 struct ocotp_ctrl_reg {
95 unsigned int bank_address_words;
96 void (*set_timing)(struct ocotp_priv *priv);
97 struct ocotp_ctrl_reg ctrl;
100 static int imx_ocotp_wait_for_busy(struct ocotp_priv *priv, u32 flags)
104 u32 bm_ctrl_busy, bm_ctrl_error;
105 void __iomem *base = priv->base;
107 bm_ctrl_busy = priv->params->ctrl.bm_busy;
108 bm_ctrl_error = priv->params->ctrl.bm_error;
110 mask = bm_ctrl_busy | bm_ctrl_error | flags;
112 for (count = 10000; count >= 0; count--) {
113 c = readl(base + IMX_OCOTP_ADDR_CTRL);
120 /* HW_OCOTP_CTRL[ERROR] will be set under the following
122 * - A write is performed to a shadow register during a shadow
123 * reload (essentially, while HW_OCOTP_CTRL[RELOAD_SHADOWS] is
124 * set. In addition, the contents of the shadow register shall
126 * - A write is performed to a shadow register which has been
128 * - A read is performed to from a shadow register which has
130 * - A program is performed to a fuse word which has been locked
131 * - A read is performed to from a fuse word which has been read
134 if (c & bm_ctrl_error)
142 static void imx_ocotp_clr_err_if_set(struct ocotp_priv *priv)
144 u32 c, bm_ctrl_error;
145 void __iomem *base = priv->base;
147 bm_ctrl_error = priv->params->ctrl.bm_error;
149 c = readl(base + IMX_OCOTP_ADDR_CTRL);
150 if (!(c & bm_ctrl_error))
153 writel(bm_ctrl_error, base + IMX_OCOTP_ADDR_CTRL_CLR);
156 static int imx_ocotp_read(void *context, unsigned int offset,
157 void *val, size_t bytes)
159 struct ocotp_priv *priv = context;
168 if (count > (priv->params->nregs - index))
169 count = priv->params->nregs - index;
171 mutex_lock(&ocotp_mutex);
173 ret = clk_prepare_enable(priv->clk);
175 mutex_unlock(&ocotp_mutex);
176 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
180 ret = imx_ocotp_wait_for_busy(priv, 0);
182 dev_err(priv->dev, "timeout during read setup\n");
186 for (i = index; i < (index + count); i++) {
187 *buf++ = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 +
188 i * IMX_OCOTP_OFFSET_PER_WORD);
191 * For "read locked" registers 0xBADABADA will be returned and
192 * HW_OCOTP_CTRL[ERROR] will be set. It must be cleared by
193 * software before any new write, read or reload access can be
196 if (*(buf - 1) == IMX_OCOTP_READ_LOCKED_VAL)
197 imx_ocotp_clr_err_if_set(priv);
201 clk_disable_unprepare(priv->clk);
202 mutex_unlock(&ocotp_mutex);
206 static void imx_ocotp_set_imx6_timing(struct ocotp_priv *priv)
208 unsigned long clk_rate;
209 unsigned long strobe_read, relax, strobe_prog;
213 * Program HW_OCOTP_TIMING[STROBE_PROG] and HW_OCOTP_TIMING[RELAX]
214 * fields with timing values to match the current frequency of the
215 * ipg_clk. OTP writes will work at maximum bus frequencies as long
216 * as the HW_OCOTP_TIMING parameters are set correctly.
218 * Note: there are minimum timings required to ensure an OTP fuse burns
219 * correctly that are independent of the ipg_clk. Those values are not
220 * formally documented anywhere however, working from the minimum
221 * timings given in u-boot we can say:
223 * - Minimum STROBE_PROG time is 10 microseconds. Intuitively 10
224 * microseconds feels about right as representative of a minimum time
225 * to physically burn out a fuse.
227 * - Minimum STROBE_READ i.e. the time to wait post OTP fuse burn before
228 * performing another read is 37 nanoseconds
230 * - Minimum RELAX timing is 17 nanoseconds. This final RELAX minimum
231 * timing is not entirely clear the documentation says "This
232 * count value specifies the time to add to all default timing
233 * parameters other than the Tpgm and Trd. It is given in number
234 * of ipg_clk periods." where Tpgm and Trd refer to STROBE_PROG
235 * and STROBE_READ respectively. What the other timing parameters
236 * are though, is not specified. Experience shows a zero RELAX
237 * value will mess up a re-load of the shadow registers post OTP
240 clk_rate = clk_get_rate(priv->clk);
242 relax = DIV_ROUND_UP(clk_rate * TIMING_RELAX_NS, 1000000000) - 1;
243 strobe_read = DIV_ROUND_UP(clk_rate * TIMING_STROBE_READ_NS,
245 strobe_read += 2 * (relax + 1) - 1;
246 strobe_prog = DIV_ROUND_CLOSEST(clk_rate * TIMING_STROBE_PROG_US,
248 strobe_prog += 2 * (relax + 1) - 1;
250 timing = readl(priv->base + IMX_OCOTP_ADDR_TIMING) & 0x0FC00000;
251 timing |= strobe_prog & 0x00000FFF;
252 timing |= (relax << 12) & 0x0000F000;
253 timing |= (strobe_read << 16) & 0x003F0000;
255 writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
258 static void imx_ocotp_set_imx7_timing(struct ocotp_priv *priv)
260 unsigned long clk_rate;
261 u64 fsource, strobe_prog;
264 /* i.MX 7Solo Applications Processor Reference Manual, Rev. 0.1
267 clk_rate = clk_get_rate(priv->clk);
268 fsource = DIV_ROUND_UP_ULL((u64)clk_rate * DEF_FSOURCE,
270 strobe_prog = DIV_ROUND_CLOSEST_ULL((u64)clk_rate * DEF_STROBE_PROG,
273 timing = strobe_prog & 0x00000FFF;
274 timing |= (fsource << 12) & 0x000FF000;
276 writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
279 static int imx_ocotp_write(void *context, unsigned int offset, void *val,
282 struct ocotp_priv *priv = context;
290 /* allow only writing one complete OTP word at a time */
291 if ((bytes != priv->config->word_size) ||
292 (offset % priv->config->word_size))
295 mutex_lock(&ocotp_mutex);
297 ret = clk_prepare_enable(priv->clk);
299 mutex_unlock(&ocotp_mutex);
300 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
304 /* Setup the write timing values */
305 priv->params->set_timing(priv);
308 * Check that HW_OCOTP_CTRL[BUSY] and HW_OCOTP_CTRL[ERROR] are clear.
309 * Overlapped accesses are not supported by the controller. Any pending
310 * write or reload must be completed before a write access can be
313 ret = imx_ocotp_wait_for_busy(priv, 0);
315 dev_err(priv->dev, "timeout during timing setup\n");
320 * Write the requested address to HW_OCOTP_CTRL[ADDR] and program the
321 * unlock code into HW_OCOTP_CTRL[WR_UNLOCK]. This must be programmed
322 * for each write access. The lock code is documented in the register
323 * description. Both the unlock code and address can be written in the
326 if (priv->params->bank_address_words != 0) {
328 * In banked/i.MX7 mode the OTP register bank goes into waddr
329 * see i.MX 7Solo Applications Processor Reference Manual, Rev.
330 * 0.1 section 6.4.3.1
332 offset = offset / priv->config->word_size;
333 waddr = offset / priv->params->bank_address_words;
334 word = offset & (priv->params->bank_address_words - 1);
337 * Non-banked i.MX6 mode.
338 * OTP write/read address specifies one of 128 word address
344 ctrl = readl(priv->base + IMX_OCOTP_ADDR_CTRL);
345 ctrl &= ~priv->params->ctrl.bm_addr;
346 ctrl |= waddr & priv->params->ctrl.bm_addr;
347 ctrl |= IMX_OCOTP_WR_UNLOCK;
349 writel(ctrl, priv->base + IMX_OCOTP_ADDR_CTRL);
352 * Write the data to the HW_OCOTP_DATA register. This will automatically
353 * set HW_OCOTP_CTRL[BUSY] and clear HW_OCOTP_CTRL[WR_UNLOCK]. To
354 * protect programming same OTP bit twice, before program OCOTP will
355 * automatically read fuse value in OTP and use read value to mask
356 * program data. The controller will use masked program data to program
357 * a 32-bit word in the OTP per the address in HW_OCOTP_CTRL[ADDR]. Bit
358 * fields with 1's will result in that OTP bit being programmed. Bit
359 * fields with 0's will be ignored. At the same time that the write is
360 * accepted, the controller makes an internal copy of
361 * HW_OCOTP_CTRL[ADDR] which cannot be updated until the next write
362 * sequence is initiated. This copy guarantees that erroneous writes to
363 * HW_OCOTP_CTRL[ADDR] will not affect an active write operation. It
364 * should also be noted that during the programming HW_OCOTP_DATA will
365 * shift right (with zero fill). This shifting is required to program
366 * the OTP serially. During the write operation, HW_OCOTP_DATA cannot be
368 * Note: on i.MX7 there are four data fields to write for banked write
369 * with the fuse blowing operation only taking place after data0
370 * has been written. This is why data0 must always be the last
373 if (priv->params->bank_address_words != 0) {
374 /* Banked/i.MX7 mode */
377 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
378 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
379 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
380 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
383 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA1);
384 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
385 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
386 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
389 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
390 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA2);
391 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
392 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
395 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
396 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
397 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA3);
398 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
402 /* Non-banked i.MX6 mode */
403 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
407 * Once complete, the controller will clear BUSY. A write request to a
408 * protected or locked region will result in no OTP access and no
409 * setting of HW_OCOTP_CTRL[BUSY]. In addition HW_OCOTP_CTRL[ERROR] will
410 * be set. It must be cleared by software before any new write access
413 ret = imx_ocotp_wait_for_busy(priv, 0);
416 dev_err(priv->dev, "failed write to locked region");
417 imx_ocotp_clr_err_if_set(priv);
419 dev_err(priv->dev, "timeout during data write\n");
425 * Write Postamble: Due to internal electrical characteristics of the
426 * OTP during writes, all OTP operations following a write must be
427 * separated by 2 us after the clearing of HW_OCOTP_CTRL_BUSY following
432 /* reload all shadow registers */
433 writel(priv->params->ctrl.bm_rel_shadows,
434 priv->base + IMX_OCOTP_ADDR_CTRL_SET);
435 ret = imx_ocotp_wait_for_busy(priv,
436 priv->params->ctrl.bm_rel_shadows);
438 dev_err(priv->dev, "timeout during shadow register reload\n");
441 clk_disable_unprepare(priv->clk);
442 mutex_unlock(&ocotp_mutex);
443 return ret < 0 ? ret : bytes;
446 static struct nvmem_config imx_ocotp_nvmem_config = {
451 .reg_read = imx_ocotp_read,
452 .reg_write = imx_ocotp_write,
455 static const struct ocotp_params imx6q_params = {
457 .bank_address_words = 0,
458 .set_timing = imx_ocotp_set_imx6_timing,
459 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
462 static const struct ocotp_params imx6sl_params = {
464 .bank_address_words = 0,
465 .set_timing = imx_ocotp_set_imx6_timing,
466 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
469 static const struct ocotp_params imx6sll_params = {
471 .bank_address_words = 0,
472 .set_timing = imx_ocotp_set_imx6_timing,
473 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
476 static const struct ocotp_params imx6sx_params = {
478 .bank_address_words = 0,
479 .set_timing = imx_ocotp_set_imx6_timing,
480 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
483 static const struct ocotp_params imx6ul_params = {
485 .bank_address_words = 0,
486 .set_timing = imx_ocotp_set_imx6_timing,
487 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
490 static const struct ocotp_params imx6ull_params = {
492 .bank_address_words = 0,
493 .set_timing = imx_ocotp_set_imx6_timing,
494 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
497 static const struct ocotp_params imx7d_params = {
499 .bank_address_words = 4,
500 .set_timing = imx_ocotp_set_imx7_timing,
501 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
504 static const struct ocotp_params imx7ulp_params = {
506 .bank_address_words = 0,
507 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
510 static const struct ocotp_params imx8mq_params = {
512 .bank_address_words = 0,
513 .set_timing = imx_ocotp_set_imx6_timing,
514 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
517 static const struct ocotp_params imx8mm_params = {
519 .bank_address_words = 0,
520 .set_timing = imx_ocotp_set_imx6_timing,
521 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
524 static const struct ocotp_params imx8mn_params = {
526 .bank_address_words = 0,
527 .set_timing = imx_ocotp_set_imx6_timing,
528 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
531 static const struct ocotp_params imx8mp_params = {
533 .bank_address_words = 0,
534 .set_timing = imx_ocotp_set_imx6_timing,
535 .ctrl = IMX_OCOTP_BM_CTRL_8MP,
538 static const struct of_device_id imx_ocotp_dt_ids[] = {
539 { .compatible = "fsl,imx6q-ocotp", .data = &imx6q_params },
540 { .compatible = "fsl,imx6sl-ocotp", .data = &imx6sl_params },
541 { .compatible = "fsl,imx6sx-ocotp", .data = &imx6sx_params },
542 { .compatible = "fsl,imx6ul-ocotp", .data = &imx6ul_params },
543 { .compatible = "fsl,imx6ull-ocotp", .data = &imx6ull_params },
544 { .compatible = "fsl,imx7d-ocotp", .data = &imx7d_params },
545 { .compatible = "fsl,imx6sll-ocotp", .data = &imx6sll_params },
546 { .compatible = "fsl,imx7ulp-ocotp", .data = &imx7ulp_params },
547 { .compatible = "fsl,imx8mq-ocotp", .data = &imx8mq_params },
548 { .compatible = "fsl,imx8mm-ocotp", .data = &imx8mm_params },
549 { .compatible = "fsl,imx8mn-ocotp", .data = &imx8mn_params },
550 { .compatible = "fsl,imx8mp-ocotp", .data = &imx8mp_params },
553 MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
555 static int imx_ocotp_probe(struct platform_device *pdev)
557 struct device *dev = &pdev->dev;
558 struct ocotp_priv *priv;
559 struct nvmem_device *nvmem;
561 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
567 priv->base = devm_platform_ioremap_resource(pdev, 0);
568 if (IS_ERR(priv->base))
569 return PTR_ERR(priv->base);
571 priv->clk = devm_clk_get(dev, NULL);
572 if (IS_ERR(priv->clk))
573 return PTR_ERR(priv->clk);
575 priv->params = of_device_get_match_data(&pdev->dev);
576 imx_ocotp_nvmem_config.size = 4 * priv->params->nregs;
577 imx_ocotp_nvmem_config.dev = dev;
578 imx_ocotp_nvmem_config.priv = priv;
579 priv->config = &imx_ocotp_nvmem_config;
581 clk_prepare_enable(priv->clk);
582 imx_ocotp_clr_err_if_set(priv);
583 clk_disable_unprepare(priv->clk);
585 nvmem = devm_nvmem_register(dev, &imx_ocotp_nvmem_config);
587 return PTR_ERR_OR_ZERO(nvmem);
590 static struct platform_driver imx_ocotp_driver = {
591 .probe = imx_ocotp_probe,
594 .of_match_table = imx_ocotp_dt_ids,
597 module_platform_driver(imx_ocotp_driver);
600 MODULE_DESCRIPTION("i.MX6/i.MX7 OCOTP fuse box driver");
601 MODULE_LICENSE("GPL v2");