1 // SPDX-License-Identifier: GPL-2.0-only
3 * i.MX6 OCOTP fusebox driver
5 * Copyright (c) 2015 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
7 * Based on the barebox ocotp driver,
8 * Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>,
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 0x0000007F
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 DEF_RELAX 20 /* > 16.5ns */
48 #define DEF_FSOURCE 1001 /* > 1000 ns */
49 #define DEF_STROBE_PROG 10000 /* IPG clocks */
50 #define IMX_OCOTP_WR_UNLOCK 0x3E770000
51 #define IMX_OCOTP_READ_LOCKED_VAL 0xBADABADA
53 static DEFINE_MUTEX(ocotp_mutex);
59 const struct ocotp_params *params;
60 struct nvmem_config *config;
65 unsigned int bank_address_words;
66 void (*set_timing)(struct ocotp_priv *priv);
69 static int imx_ocotp_wait_for_busy(void __iomem *base, u32 flags)
74 mask = IMX_OCOTP_BM_CTRL_BUSY | IMX_OCOTP_BM_CTRL_ERROR | flags;
76 for (count = 10000; count >= 0; count--) {
77 c = readl(base + IMX_OCOTP_ADDR_CTRL);
84 /* HW_OCOTP_CTRL[ERROR] will be set under the following
86 * - A write is performed to a shadow register during a shadow
87 * reload (essentially, while HW_OCOTP_CTRL[RELOAD_SHADOWS] is
88 * set. In addition, the contents of the shadow register shall
90 * - A write is performed to a shadow register which has been
92 * - A read is performed to from a shadow register which has
94 * - A program is performed to a fuse word which has been locked
95 * - A read is performed to from a fuse word which has been read
98 if (c & IMX_OCOTP_BM_CTRL_ERROR)
106 static void imx_ocotp_clr_err_if_set(void __iomem *base)
110 c = readl(base + IMX_OCOTP_ADDR_CTRL);
111 if (!(c & IMX_OCOTP_BM_CTRL_ERROR))
114 writel(IMX_OCOTP_BM_CTRL_ERROR, base + IMX_OCOTP_ADDR_CTRL_CLR);
117 static int imx_ocotp_read(void *context, unsigned int offset,
118 void *val, size_t bytes)
120 struct ocotp_priv *priv = context;
129 if (count > (priv->params->nregs - index))
130 count = priv->params->nregs - index;
132 mutex_lock(&ocotp_mutex);
134 ret = clk_prepare_enable(priv->clk);
136 mutex_unlock(&ocotp_mutex);
137 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
141 ret = imx_ocotp_wait_for_busy(priv->base, 0);
143 dev_err(priv->dev, "timeout during read setup\n");
147 for (i = index; i < (index + count); i++) {
148 *buf++ = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 +
149 i * IMX_OCOTP_OFFSET_PER_WORD);
152 * For "read locked" registers 0xBADABADA will be returned and
153 * HW_OCOTP_CTRL[ERROR] will be set. It must be cleared by
154 * software before any new write, read or reload access can be
157 if (*(buf - 1) == IMX_OCOTP_READ_LOCKED_VAL)
158 imx_ocotp_clr_err_if_set(priv->base);
163 clk_disable_unprepare(priv->clk);
164 mutex_unlock(&ocotp_mutex);
168 static void imx_ocotp_set_imx6_timing(struct ocotp_priv *priv)
170 unsigned long clk_rate = 0;
171 unsigned long strobe_read, relax, strobe_prog;
175 * Program HW_OCOTP_TIMING[STROBE_PROG] and HW_OCOTP_TIMING[RELAX]
176 * fields with timing values to match the current frequency of the
177 * ipg_clk. OTP writes will work at maximum bus frequencies as long
178 * as the HW_OCOTP_TIMING parameters are set correctly.
180 clk_rate = clk_get_rate(priv->clk);
182 relax = clk_rate / (1000000000 / DEF_RELAX) - 1;
183 strobe_prog = clk_rate / (1000000000 / 10000) + 2 * (DEF_RELAX + 1) - 1;
184 strobe_read = clk_rate / (1000000000 / 40) + 2 * (DEF_RELAX + 1) - 1;
186 timing = strobe_prog & 0x00000FFF;
187 timing |= (relax << 12) & 0x0000F000;
188 timing |= (strobe_read << 16) & 0x003F0000;
190 writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
193 static void imx_ocotp_set_imx7_timing(struct ocotp_priv *priv)
195 unsigned long clk_rate = 0;
196 u64 fsource, strobe_prog;
199 /* i.MX 7Solo Applications Processor Reference Manual, Rev. 0.1
202 clk_rate = clk_get_rate(priv->clk);
203 fsource = DIV_ROUND_UP_ULL((u64)clk_rate * DEF_FSOURCE,
205 strobe_prog = DIV_ROUND_CLOSEST_ULL((u64)clk_rate * DEF_STROBE_PROG,
208 timing = strobe_prog & 0x00000FFF;
209 timing |= (fsource << 12) & 0x000FF000;
211 writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
214 static int imx_ocotp_write(void *context, unsigned int offset, void *val,
217 struct ocotp_priv *priv = context;
225 /* allow only writing one complete OTP word at a time */
226 if ((bytes != priv->config->word_size) ||
227 (offset % priv->config->word_size))
230 mutex_lock(&ocotp_mutex);
232 ret = clk_prepare_enable(priv->clk);
234 mutex_unlock(&ocotp_mutex);
235 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
239 /* Setup the write timing values */
240 priv->params->set_timing(priv);
243 * Check that HW_OCOTP_CTRL[BUSY] and HW_OCOTP_CTRL[ERROR] are clear.
244 * Overlapped accesses are not supported by the controller. Any pending
245 * write or reload must be completed before a write access can be
248 ret = imx_ocotp_wait_for_busy(priv->base, 0);
250 dev_err(priv->dev, "timeout during timing setup\n");
255 * Write the requested address to HW_OCOTP_CTRL[ADDR] and program the
256 * unlock code into HW_OCOTP_CTRL[WR_UNLOCK]. This must be programmed
257 * for each write access. The lock code is documented in the register
258 * description. Both the unlock code and address can be written in the
261 if (priv->params->bank_address_words != 0) {
263 * In banked/i.MX7 mode the OTP register bank goes into waddr
264 * see i.MX 7Solo Applications Processor Reference Manual, Rev.
265 * 0.1 section 6.4.3.1
267 offset = offset / priv->config->word_size;
268 waddr = offset / priv->params->bank_address_words;
269 word = offset & (priv->params->bank_address_words - 1);
272 * Non-banked i.MX6 mode.
273 * OTP write/read address specifies one of 128 word address
279 ctrl = readl(priv->base + IMX_OCOTP_ADDR_CTRL);
280 ctrl &= ~IMX_OCOTP_BM_CTRL_ADDR;
281 ctrl |= waddr & IMX_OCOTP_BM_CTRL_ADDR;
282 ctrl |= IMX_OCOTP_WR_UNLOCK;
284 writel(ctrl, priv->base + IMX_OCOTP_ADDR_CTRL);
287 * Write the data to the HW_OCOTP_DATA register. This will automatically
288 * set HW_OCOTP_CTRL[BUSY] and clear HW_OCOTP_CTRL[WR_UNLOCK]. To
289 * protect programming same OTP bit twice, before program OCOTP will
290 * automatically read fuse value in OTP and use read value to mask
291 * program data. The controller will use masked program data to program
292 * a 32-bit word in the OTP per the address in HW_OCOTP_CTRL[ADDR]. Bit
293 * fields with 1's will result in that OTP bit being programmed. Bit
294 * fields with 0's will be ignored. At the same time that the write is
295 * accepted, the controller makes an internal copy of
296 * HW_OCOTP_CTRL[ADDR] which cannot be updated until the next write
297 * sequence is initiated. This copy guarantees that erroneous writes to
298 * HW_OCOTP_CTRL[ADDR] will not affect an active write operation. It
299 * should also be noted that during the programming HW_OCOTP_DATA will
300 * shift right (with zero fill). This shifting is required to program
301 * the OTP serially. During the write operation, HW_OCOTP_DATA cannot be
303 * Note: on i.MX7 there are four data fields to write for banked write
304 * with the fuse blowing operation only taking place after data0
305 * has been written. This is why data0 must always be the last
308 if (priv->params->bank_address_words != 0) {
309 /* Banked/i.MX7 mode */
312 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
313 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
314 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
315 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
318 writel(*buf, 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(0, priv->base + IMX_OCOTP_ADDR_DATA0);
324 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
325 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA2);
326 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
327 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
330 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
331 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
332 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA3);
333 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
337 /* Non-banked i.MX6 mode */
338 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
342 * Once complete, the controller will clear BUSY. A write request to a
343 * protected or locked region will result in no OTP access and no
344 * setting of HW_OCOTP_CTRL[BUSY]. In addition HW_OCOTP_CTRL[ERROR] will
345 * be set. It must be cleared by software before any new write access
348 ret = imx_ocotp_wait_for_busy(priv->base, 0);
351 dev_err(priv->dev, "failed write to locked region");
352 imx_ocotp_clr_err_if_set(priv->base);
354 dev_err(priv->dev, "timeout during data write\n");
360 * Write Postamble: Due to internal electrical characteristics of the
361 * OTP during writes, all OTP operations following a write must be
362 * separated by 2 us after the clearing of HW_OCOTP_CTRL_BUSY following
367 /* reload all shadow registers */
368 writel(IMX_OCOTP_BM_CTRL_REL_SHADOWS,
369 priv->base + IMX_OCOTP_ADDR_CTRL_SET);
370 ret = imx_ocotp_wait_for_busy(priv->base,
371 IMX_OCOTP_BM_CTRL_REL_SHADOWS);
373 dev_err(priv->dev, "timeout during shadow register reload\n");
378 clk_disable_unprepare(priv->clk);
379 mutex_unlock(&ocotp_mutex);
385 static struct nvmem_config imx_ocotp_nvmem_config = {
390 .reg_read = imx_ocotp_read,
391 .reg_write = imx_ocotp_write,
394 static const struct ocotp_params imx6q_params = {
396 .bank_address_words = 0,
397 .set_timing = imx_ocotp_set_imx6_timing,
400 static const struct ocotp_params imx6sl_params = {
402 .bank_address_words = 0,
403 .set_timing = imx_ocotp_set_imx6_timing,
406 static const struct ocotp_params imx6sll_params = {
408 .bank_address_words = 0,
409 .set_timing = imx_ocotp_set_imx6_timing,
412 static const struct ocotp_params imx6sx_params = {
414 .bank_address_words = 0,
415 .set_timing = imx_ocotp_set_imx6_timing,
418 static const struct ocotp_params imx6ul_params = {
420 .bank_address_words = 0,
421 .set_timing = imx_ocotp_set_imx6_timing,
424 static const struct ocotp_params imx6ull_params = {
426 .bank_address_words = 0,
427 .set_timing = imx_ocotp_set_imx6_timing,
430 static const struct ocotp_params imx7d_params = {
432 .bank_address_words = 4,
433 .set_timing = imx_ocotp_set_imx7_timing,
436 static const struct ocotp_params imx7ulp_params = {
438 .bank_address_words = 0,
441 static const struct ocotp_params imx8mq_params = {
443 .bank_address_words = 4,
444 .set_timing = imx_ocotp_set_imx7_timing,
447 static const struct of_device_id imx_ocotp_dt_ids[] = {
448 { .compatible = "fsl,imx6q-ocotp", .data = &imx6q_params },
449 { .compatible = "fsl,imx6sl-ocotp", .data = &imx6sl_params },
450 { .compatible = "fsl,imx6sx-ocotp", .data = &imx6sx_params },
451 { .compatible = "fsl,imx6ul-ocotp", .data = &imx6ul_params },
452 { .compatible = "fsl,imx6ull-ocotp", .data = &imx6ull_params },
453 { .compatible = "fsl,imx7d-ocotp", .data = &imx7d_params },
454 { .compatible = "fsl,imx6sll-ocotp", .data = &imx6sll_params },
455 { .compatible = "fsl,imx7ulp-ocotp", .data = &imx7ulp_params },
456 { .compatible = "fsl,imx8mq-ocotp", .data = &imx8mq_params },
459 MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
461 static int imx_ocotp_probe(struct platform_device *pdev)
463 struct device *dev = &pdev->dev;
464 struct ocotp_priv *priv;
465 struct nvmem_device *nvmem;
467 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
473 priv->base = devm_platform_ioremap_resource(pdev, 0);
474 if (IS_ERR(priv->base))
475 return PTR_ERR(priv->base);
477 priv->clk = devm_clk_get(dev, NULL);
478 if (IS_ERR(priv->clk))
479 return PTR_ERR(priv->clk);
481 priv->params = of_device_get_match_data(&pdev->dev);
482 imx_ocotp_nvmem_config.size = 4 * priv->params->nregs;
483 imx_ocotp_nvmem_config.dev = dev;
484 imx_ocotp_nvmem_config.priv = priv;
485 priv->config = &imx_ocotp_nvmem_config;
486 nvmem = devm_nvmem_register(dev, &imx_ocotp_nvmem_config);
489 return PTR_ERR_OR_ZERO(nvmem);
492 static struct platform_driver imx_ocotp_driver = {
493 .probe = imx_ocotp_probe,
496 .of_match_table = imx_ocotp_dt_ids,
499 module_platform_driver(imx_ocotp_driver);
501 MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
502 MODULE_DESCRIPTION("i.MX6/i.MX7 OCOTP fuse box driver");
503 MODULE_LICENSE("GPL v2");