2 * i.MX6 OCOTP fusebox driver
4 * Copyright (c) 2015 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
6 * Based on the barebox ocotp driver,
7 * Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>,
8 * Orex Computed Radiography
10 * Write support based on the fsl_otp driver,
11 * Copyright (C) 2010-2013 Freescale Semiconductor, Inc
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation.
17 * http://www.opensource.org/licenses/gpl-license.html
18 * http://www.gnu.org/copyleft/gpl.html
21 #include <linux/clk.h>
22 #include <linux/device.h>
24 #include <linux/module.h>
25 #include <linux/nvmem-provider.h>
27 #include <linux/of_device.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
32 #define IMX_OCOTP_OFFSET_B0W0 0x400 /* Offset from base address of the
35 #define IMX_OCOTP_OFFSET_PER_WORD 0x10 /* Offset between the start addr
36 * of two consecutive OTP words.
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_DATA 0x0020
45 #define IMX_OCOTP_BM_CTRL_ADDR 0x0000007F
46 #define IMX_OCOTP_BM_CTRL_BUSY 0x00000100
47 #define IMX_OCOTP_BM_CTRL_ERROR 0x00000200
48 #define IMX_OCOTP_BM_CTRL_REL_SHADOWS 0x00000400
50 #define DEF_RELAX 20 /* > 16.5ns */
51 #define IMX_OCOTP_WR_UNLOCK 0x3E770000
52 #define IMX_OCOTP_READ_LOCKED_VAL 0xBADABADA
54 static DEFINE_MUTEX(ocotp_mutex
);
61 struct nvmem_config
*config
;
64 static int imx_ocotp_wait_for_busy(void __iomem
*base
, u32 flags
)
69 mask
= IMX_OCOTP_BM_CTRL_BUSY
| IMX_OCOTP_BM_CTRL_ERROR
| flags
;
71 for (count
= 10000; count
>= 0; count
--) {
72 c
= readl(base
+ IMX_OCOTP_ADDR_CTRL
);
79 /* HW_OCOTP_CTRL[ERROR] will be set under the following
81 * - A write is performed to a shadow register during a shadow
82 * reload (essentially, while HW_OCOTP_CTRL[RELOAD_SHADOWS] is
83 * set. In addition, the contents of the shadow register shall
85 * - A write is performed to a shadow register which has been
87 * - A read is performed to from a shadow register which has
89 * - A program is performed to a fuse word which has been locked
90 * - A read is performed to from a fuse word which has been read
93 if (c
& IMX_OCOTP_BM_CTRL_ERROR
)
101 static void imx_ocotp_clr_err_if_set(void __iomem
*base
)
105 c
= readl(base
+ IMX_OCOTP_ADDR_CTRL
);
106 if (!(c
& IMX_OCOTP_BM_CTRL_ERROR
))
109 writel(IMX_OCOTP_BM_CTRL_ERROR
, base
+ IMX_OCOTP_ADDR_CTRL_CLR
);
112 static int imx_ocotp_read(void *context
, unsigned int offset
,
113 void *val
, size_t bytes
)
115 struct ocotp_priv
*priv
= context
;
124 if (count
> (priv
->nregs
- index
))
125 count
= priv
->nregs
- index
;
127 mutex_lock(&ocotp_mutex
);
129 ret
= clk_prepare_enable(priv
->clk
);
131 mutex_unlock(&ocotp_mutex
);
132 dev_err(priv
->dev
, "failed to prepare/enable ocotp clk\n");
136 ret
= imx_ocotp_wait_for_busy(priv
->base
, 0);
138 dev_err(priv
->dev
, "timeout during read setup\n");
142 for (i
= index
; i
< (index
+ count
); i
++) {
143 *buf
++ = readl(priv
->base
+ IMX_OCOTP_OFFSET_B0W0
+
144 i
* IMX_OCOTP_OFFSET_PER_WORD
);
147 * For "read locked" registers 0xBADABADA will be returned and
148 * HW_OCOTP_CTRL[ERROR] will be set. It must be cleared by
149 * software before any new write, read or reload access can be
152 if (*(buf
- 1) == IMX_OCOTP_READ_LOCKED_VAL
)
153 imx_ocotp_clr_err_if_set(priv
->base
);
158 clk_disable_unprepare(priv
->clk
);
159 mutex_unlock(&ocotp_mutex
);
163 static int imx_ocotp_write(void *context
, unsigned int offset
, void *val
,
166 struct ocotp_priv
*priv
= context
;
170 unsigned long clk_rate
= 0;
171 unsigned long strobe_read
, relax
, strobe_prog
;
176 /* allow only writing one complete OTP word at a time */
177 if ((bytes
!= priv
->config
->word_size
) ||
178 (offset
% priv
->config
->word_size
))
181 mutex_lock(&ocotp_mutex
);
183 ret
= clk_prepare_enable(priv
->clk
);
185 mutex_unlock(&ocotp_mutex
);
186 dev_err(priv
->dev
, "failed to prepare/enable ocotp clk\n");
191 * Program HW_OCOTP_TIMING[STROBE_PROG] and HW_OCOTP_TIMING[RELAX]
192 * fields with timing values to match the current frequency of the
193 * ipg_clk. OTP writes will work at maximum bus frequencies as long
194 * as the HW_OCOTP_TIMING parameters are set correctly.
196 clk_rate
= clk_get_rate(priv
->clk
);
198 relax
= clk_rate
/ (1000000000 / DEF_RELAX
) - 1;
199 strobe_prog
= clk_rate
/ (1000000000 / 10000) + 2 * (DEF_RELAX
+ 1) - 1;
200 strobe_read
= clk_rate
/ (1000000000 / 40) + 2 * (DEF_RELAX
+ 1) - 1;
202 timing
= strobe_prog
& 0x00000FFF;
203 timing
|= (relax
<< 12) & 0x0000F000;
204 timing
|= (strobe_read
<< 16) & 0x003F0000;
206 writel(timing
, priv
->base
+ IMX_OCOTP_ADDR_TIMING
);
209 * Check that HW_OCOTP_CTRL[BUSY] and HW_OCOTP_CTRL[ERROR] are clear.
210 * Overlapped accesses are not supported by the controller. Any pending
211 * write or reload must be completed before a write access can be
214 ret
= imx_ocotp_wait_for_busy(priv
->base
, 0);
216 dev_err(priv
->dev
, "timeout during timing setup\n");
221 * Write the requested address to HW_OCOTP_CTRL[ADDR] and program the
222 * unlock code into HW_OCOTP_CTRL[WR_UNLOCK]. This must be programmed
223 * for each write access. The lock code is documented in the register
224 * description. Both the unlock code and address can be written in the
227 /* OTP write/read address specifies one of 128 word address locations */
230 ctrl
= readl(priv
->base
+ IMX_OCOTP_ADDR_CTRL
);
231 ctrl
&= ~IMX_OCOTP_BM_CTRL_ADDR
;
232 ctrl
|= waddr
& IMX_OCOTP_BM_CTRL_ADDR
;
233 ctrl
|= IMX_OCOTP_WR_UNLOCK
;
235 writel(ctrl
, priv
->base
+ IMX_OCOTP_ADDR_CTRL
);
238 * Write the data to the HW_OCOTP_DATA register. This will automatically
239 * set HW_OCOTP_CTRL[BUSY] and clear HW_OCOTP_CTRL[WR_UNLOCK]. To
240 * protect programming same OTP bit twice, before program OCOTP will
241 * automatically read fuse value in OTP and use read value to mask
242 * program data. The controller will use masked program data to program
243 * a 32-bit word in the OTP per the address in HW_OCOTP_CTRL[ADDR]. Bit
244 * fields with 1's will result in that OTP bit being programmed. Bit
245 * fields with 0's will be ignored. At the same time that the write is
246 * accepted, the controller makes an internal copy of
247 * HW_OCOTP_CTRL[ADDR] which cannot be updated until the next write
248 * sequence is initiated. This copy guarantees that erroneous writes to
249 * HW_OCOTP_CTRL[ADDR] will not affect an active write operation. It
250 * should also be noted that during the programming HW_OCOTP_DATA will
251 * shift right (with zero fill). This shifting is required to program
252 * the OTP serially. During the write operation, HW_OCOTP_DATA cannot be
255 writel(*buf
, priv
->base
+ IMX_OCOTP_ADDR_DATA
);
258 * Once complete, the controller will clear BUSY. A write request to a
259 * protected or locked region will result in no OTP access and no
260 * setting of HW_OCOTP_CTRL[BUSY]. In addition HW_OCOTP_CTRL[ERROR] will
261 * be set. It must be cleared by software before any new write access
264 ret
= imx_ocotp_wait_for_busy(priv
->base
, 0);
267 dev_err(priv
->dev
, "failed write to locked region");
268 imx_ocotp_clr_err_if_set(priv
->base
);
270 dev_err(priv
->dev
, "timeout during data write\n");
276 * Write Postamble: Due to internal electrical characteristics of the
277 * OTP during writes, all OTP operations following a write must be
278 * separated by 2 us after the clearing of HW_OCOTP_CTRL_BUSY following
283 /* reload all shadow registers */
284 writel(IMX_OCOTP_BM_CTRL_REL_SHADOWS
,
285 priv
->base
+ IMX_OCOTP_ADDR_CTRL_SET
);
286 ret
= imx_ocotp_wait_for_busy(priv
->base
,
287 IMX_OCOTP_BM_CTRL_REL_SHADOWS
);
289 dev_err(priv
->dev
, "timeout during shadow register reload\n");
294 clk_disable_unprepare(priv
->clk
);
295 mutex_unlock(&ocotp_mutex
);
301 static struct nvmem_config imx_ocotp_nvmem_config
= {
306 .owner
= THIS_MODULE
,
307 .reg_read
= imx_ocotp_read
,
308 .reg_write
= imx_ocotp_write
,
311 static const struct of_device_id imx_ocotp_dt_ids
[] = {
312 { .compatible
= "fsl,imx6q-ocotp", (void *)128 },
313 { .compatible
= "fsl,imx6sl-ocotp", (void *)64 },
314 { .compatible
= "fsl,imx6sx-ocotp", (void *)128 },
315 { .compatible
= "fsl,imx6ul-ocotp", (void *)128 },
316 { .compatible
= "fsl,imx7d-ocotp", (void *)64 },
319 MODULE_DEVICE_TABLE(of
, imx_ocotp_dt_ids
);
321 static int imx_ocotp_probe(struct platform_device
*pdev
)
323 const struct of_device_id
*of_id
;
324 struct device
*dev
= &pdev
->dev
;
325 struct resource
*res
;
326 struct ocotp_priv
*priv
;
327 struct nvmem_device
*nvmem
;
329 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
335 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
336 priv
->base
= devm_ioremap_resource(dev
, res
);
337 if (IS_ERR(priv
->base
))
338 return PTR_ERR(priv
->base
);
340 priv
->clk
= devm_clk_get(dev
, NULL
);
341 if (IS_ERR(priv
->clk
))
342 return PTR_ERR(priv
->clk
);
344 of_id
= of_match_device(imx_ocotp_dt_ids
, dev
);
345 priv
->nregs
= (unsigned long)of_id
->data
;
346 imx_ocotp_nvmem_config
.size
= 4 * priv
->nregs
;
347 imx_ocotp_nvmem_config
.dev
= dev
;
348 imx_ocotp_nvmem_config
.priv
= priv
;
349 priv
->config
= &imx_ocotp_nvmem_config
;
350 nvmem
= nvmem_register(&imx_ocotp_nvmem_config
);
353 return PTR_ERR(nvmem
);
355 platform_set_drvdata(pdev
, nvmem
);
360 static int imx_ocotp_remove(struct platform_device
*pdev
)
362 struct nvmem_device
*nvmem
= platform_get_drvdata(pdev
);
364 return nvmem_unregister(nvmem
);
367 static struct platform_driver imx_ocotp_driver
= {
368 .probe
= imx_ocotp_probe
,
369 .remove
= imx_ocotp_remove
,
372 .of_match_table
= imx_ocotp_dt_ids
,
375 module_platform_driver(imx_ocotp_driver
);
377 MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
378 MODULE_DESCRIPTION("i.MX6 OCOTP fuse box driver");
379 MODULE_LICENSE("GPL v2");