blob: 08f41328cc71199fd1220343ce64e62f02ffe0e0 [file] [log] [blame]
Thomas Gleixneracee2e82019-06-04 10:10:53 +02001// SPDX-License-Identifier: GPL-2.0-only
Philipp Zabel3edba6b2015-09-30 13:55:47 +01002/*
3 * i.MX6 OCOTP fusebox driver
4 *
5 * Copyright (c) 2015 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
6 *
Peng Fan3311bf12020-11-27 10:28:37 +00007 * Copyright 2019 NXP
8 *
Philipp Zabel3edba6b2015-09-30 13:55:47 +01009 * Based on the barebox ocotp driver,
10 * Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>,
11 * Orex Computed Radiography
12 *
Richard Leitner0642bac2017-03-31 13:44:55 +010013 * Write support based on the fsl_otp driver,
14 * Copyright (C) 2010-2013 Freescale Semiconductor, Inc
Philipp Zabel3edba6b2015-09-30 13:55:47 +010015 */
16
Peng Fandeb31972016-06-02 12:05:11 +010017#include <linux/clk.h>
Philipp Zabel3edba6b2015-09-30 13:55:47 +010018#include <linux/device.h>
19#include <linux/io.h>
20#include <linux/module.h>
21#include <linux/nvmem-provider.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/platform_device.h>
Philipp Zabel3edba6b2015-09-30 13:55:47 +010025#include <linux/slab.h>
Richard Leitner0642bac2017-03-31 13:44:55 +010026#include <linux/delay.h>
Philipp Zabel3edba6b2015-09-30 13:55:47 +010027
Richard Leitner9b665872017-03-31 13:44:54 +010028#define IMX_OCOTP_OFFSET_B0W0 0x400 /* Offset from base address of the
29 * OTP Bank0 Word0
30 */
31#define IMX_OCOTP_OFFSET_PER_WORD 0x10 /* Offset between the start addr
32 * of two consecutive OTP words.
33 */
Richard Leitner0642bac2017-03-31 13:44:55 +010034
Richard Leitner9b665872017-03-31 13:44:54 +010035#define IMX_OCOTP_ADDR_CTRL 0x0000
Richard Leitner0642bac2017-03-31 13:44:55 +010036#define IMX_OCOTP_ADDR_CTRL_SET 0x0004
Richard Leitner9b665872017-03-31 13:44:54 +010037#define IMX_OCOTP_ADDR_CTRL_CLR 0x0008
Richard Leitner0642bac2017-03-31 13:44:55 +010038#define IMX_OCOTP_ADDR_TIMING 0x0010
Bryan O'Donoghueffd91152017-10-24 10:54:29 +010039#define IMX_OCOTP_ADDR_DATA0 0x0020
40#define IMX_OCOTP_ADDR_DATA1 0x0030
41#define IMX_OCOTP_ADDR_DATA2 0x0040
42#define IMX_OCOTP_ADDR_DATA3 0x0050
Richard Leitner9b665872017-03-31 13:44:54 +010043
Bryan O'Donoghuec03bb442019-06-26 11:27:28 +010044#define IMX_OCOTP_BM_CTRL_ADDR 0x000000FF
Richard Leitner0642bac2017-03-31 13:44:55 +010045#define IMX_OCOTP_BM_CTRL_BUSY 0x00000100
Richard Leitner9b665872017-03-31 13:44:54 +010046#define IMX_OCOTP_BM_CTRL_ERROR 0x00000200
Richard Leitner0642bac2017-03-31 13:44:55 +010047#define IMX_OCOTP_BM_CTRL_REL_SHADOWS 0x00000400
Richard Leitner9b665872017-03-31 13:44:54 +010048
Peng Fanc3f4af82020-03-10 13:22:44 +000049#define IMX_OCOTP_BM_CTRL_ADDR_8MP 0x000001FF
50#define IMX_OCOTP_BM_CTRL_BUSY_8MP 0x00000200
51#define IMX_OCOTP_BM_CTRL_ERROR_8MP 0x00000400
52#define IMX_OCOTP_BM_CTRL_REL_SHADOWS_8MP 0x00000800
53
Peng Fan226c5122020-01-09 10:40:16 +000054#define IMX_OCOTP_BM_CTRL_DEFAULT \
55 { \
56 .bm_addr = IMX_OCOTP_BM_CTRL_ADDR, \
57 .bm_busy = IMX_OCOTP_BM_CTRL_BUSY, \
58 .bm_error = IMX_OCOTP_BM_CTRL_ERROR, \
59 .bm_rel_shadows = IMX_OCOTP_BM_CTRL_REL_SHADOWS,\
60 }
61
Peng Fanc3f4af82020-03-10 13:22:44 +000062#define IMX_OCOTP_BM_CTRL_8MP \
63 { \
64 .bm_addr = IMX_OCOTP_BM_CTRL_ADDR_8MP, \
65 .bm_busy = IMX_OCOTP_BM_CTRL_BUSY_8MP, \
66 .bm_error = IMX_OCOTP_BM_CTRL_ERROR_8MP, \
67 .bm_rel_shadows = IMX_OCOTP_BM_CTRL_REL_SHADOWS_8MP,\
68 }
69
Bryan O'Donoghue159dbaf2019-06-26 11:27:30 +010070#define TIMING_STROBE_PROG_US 10 /* Min time to blow a fuse */
71#define TIMING_STROBE_READ_NS 37 /* Min time before read */
72#define TIMING_RELAX_NS 17
Bryan O'Donoghue828ae7a42017-10-24 10:54:31 +010073#define DEF_FSOURCE 1001 /* > 1000 ns */
74#define DEF_STROBE_PROG 10000 /* IPG clocks */
Richard Leitner0642bac2017-03-31 13:44:55 +010075#define IMX_OCOTP_WR_UNLOCK 0x3E770000
Richard Leitner9b665872017-03-31 13:44:54 +010076#define IMX_OCOTP_READ_LOCKED_VAL 0xBADABADA
77
Richard Leitner0642bac2017-03-31 13:44:55 +010078static DEFINE_MUTEX(ocotp_mutex);
79
Philipp Zabel3edba6b2015-09-30 13:55:47 +010080struct ocotp_priv {
81 struct device *dev;
Peng Fandeb31972016-06-02 12:05:11 +010082 struct clk *clk;
Philipp Zabel3edba6b2015-09-30 13:55:47 +010083 void __iomem *base;
Bryan O'Donoghuee20d2b22017-10-24 10:54:28 +010084 const struct ocotp_params *params;
Richard Leitner0642bac2017-03-31 13:44:55 +010085 struct nvmem_config *config;
Philipp Zabel3edba6b2015-09-30 13:55:47 +010086};
87
Peng Fan226c5122020-01-09 10:40:16 +000088struct ocotp_ctrl_reg {
89 u32 bm_addr;
90 u32 bm_busy;
91 u32 bm_error;
92 u32 bm_rel_shadows;
93};
94
Bryan O'Donoghue828ae7a42017-10-24 10:54:31 +010095struct ocotp_params {
96 unsigned int nregs;
97 unsigned int bank_address_words;
98 void (*set_timing)(struct ocotp_priv *priv);
Peng Fan226c5122020-01-09 10:40:16 +000099 struct ocotp_ctrl_reg ctrl;
Bryan O'Donoghue828ae7a42017-10-24 10:54:31 +0100100};
101
Peng Fan226c5122020-01-09 10:40:16 +0000102static int imx_ocotp_wait_for_busy(struct ocotp_priv *priv, u32 flags)
Richard Leitner0642bac2017-03-31 13:44:55 +0100103{
104 int count;
105 u32 c, mask;
Peng Fan226c5122020-01-09 10:40:16 +0000106 u32 bm_ctrl_busy, bm_ctrl_error;
107 void __iomem *base = priv->base;
Richard Leitner0642bac2017-03-31 13:44:55 +0100108
Peng Fan226c5122020-01-09 10:40:16 +0000109 bm_ctrl_busy = priv->params->ctrl.bm_busy;
110 bm_ctrl_error = priv->params->ctrl.bm_error;
111
112 mask = bm_ctrl_busy | bm_ctrl_error | flags;
Richard Leitner0642bac2017-03-31 13:44:55 +0100113
114 for (count = 10000; count >= 0; count--) {
115 c = readl(base + IMX_OCOTP_ADDR_CTRL);
116 if (!(c & mask))
117 break;
118 cpu_relax();
119 }
120
121 if (count < 0) {
122 /* HW_OCOTP_CTRL[ERROR] will be set under the following
123 * conditions:
124 * - A write is performed to a shadow register during a shadow
125 * reload (essentially, while HW_OCOTP_CTRL[RELOAD_SHADOWS] is
126 * set. In addition, the contents of the shadow register shall
127 * not be updated.
128 * - A write is performed to a shadow register which has been
129 * locked.
130 * - A read is performed to from a shadow register which has
131 * been read locked.
132 * - A program is performed to a fuse word which has been locked
133 * - A read is performed to from a fuse word which has been read
134 * locked.
135 */
Peng Fan226c5122020-01-09 10:40:16 +0000136 if (c & bm_ctrl_error)
Richard Leitner0642bac2017-03-31 13:44:55 +0100137 return -EPERM;
138 return -ETIMEDOUT;
139 }
140
141 return 0;
142}
143
Peng Fan226c5122020-01-09 10:40:16 +0000144static void imx_ocotp_clr_err_if_set(struct ocotp_priv *priv)
Richard Leitner9b665872017-03-31 13:44:54 +0100145{
Peng Fan226c5122020-01-09 10:40:16 +0000146 u32 c, bm_ctrl_error;
147 void __iomem *base = priv->base;
148
149 bm_ctrl_error = priv->params->ctrl.bm_error;
Richard Leitner9b665872017-03-31 13:44:54 +0100150
151 c = readl(base + IMX_OCOTP_ADDR_CTRL);
Peng Fan226c5122020-01-09 10:40:16 +0000152 if (!(c & bm_ctrl_error))
Richard Leitner9b665872017-03-31 13:44:54 +0100153 return;
154
Peng Fan226c5122020-01-09 10:40:16 +0000155 writel(bm_ctrl_error, base + IMX_OCOTP_ADDR_CTRL_CLR);
Richard Leitner9b665872017-03-31 13:44:54 +0100156}
157
Srinivas Kandagatla33e5e292016-04-24 20:28:13 +0100158static int imx_ocotp_read(void *context, unsigned int offset,
159 void *val, size_t bytes)
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100160{
161 struct ocotp_priv *priv = context;
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100162 unsigned int count;
Peng Fan3311bf12020-11-27 10:28:37 +0000163 u8 *buf, *p;
Peng Fandeb31972016-06-02 12:05:11 +0100164 int i, ret;
Peng Fan3311bf12020-11-27 10:28:37 +0000165 u32 index, num_bytes;
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100166
167 index = offset >> 2;
Peng Fan3311bf12020-11-27 10:28:37 +0000168 num_bytes = round_up((offset % 4) + bytes, 4);
169 count = num_bytes >> 2;
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100170
Bryan O'Donoghuee20d2b22017-10-24 10:54:28 +0100171 if (count > (priv->params->nregs - index))
172 count = priv->params->nregs - index;
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100173
Peng Fan3311bf12020-11-27 10:28:37 +0000174 p = kzalloc(num_bytes, GFP_KERNEL);
175 if (!p)
176 return -ENOMEM;
177
Richard Leitner0642bac2017-03-31 13:44:55 +0100178 mutex_lock(&ocotp_mutex);
179
Peng Fan3311bf12020-11-27 10:28:37 +0000180 buf = p;
181
Peng Fandeb31972016-06-02 12:05:11 +0100182 ret = clk_prepare_enable(priv->clk);
183 if (ret < 0) {
Richard Leitner0642bac2017-03-31 13:44:55 +0100184 mutex_unlock(&ocotp_mutex);
Peng Fandeb31972016-06-02 12:05:11 +0100185 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
Peng Fan3311bf12020-11-27 10:28:37 +0000186 kfree(p);
Peng Fandeb31972016-06-02 12:05:11 +0100187 return ret;
188 }
Richard Leitner9b665872017-03-31 13:44:54 +0100189
Peng Fan226c5122020-01-09 10:40:16 +0000190 ret = imx_ocotp_wait_for_busy(priv, 0);
Richard Leitner0642bac2017-03-31 13:44:55 +0100191 if (ret < 0) {
192 dev_err(priv->dev, "timeout during read setup\n");
193 goto read_end;
194 }
195
Richard Leitner9b665872017-03-31 13:44:54 +0100196 for (i = index; i < (index + count); i++) {
Peng Fan3311bf12020-11-27 10:28:37 +0000197 *(u32 *)buf = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 +
Richard Leitner9b665872017-03-31 13:44:54 +0100198 i * IMX_OCOTP_OFFSET_PER_WORD);
199
200 /* 47.3.1.2
201 * For "read locked" registers 0xBADABADA will be returned and
202 * HW_OCOTP_CTRL[ERROR] will be set. It must be cleared by
203 * software before any new write, read or reload access can be
204 * issued
205 */
Peng Fan3311bf12020-11-27 10:28:37 +0000206 if (*((u32 *)buf) == IMX_OCOTP_READ_LOCKED_VAL)
Peng Fan226c5122020-01-09 10:40:16 +0000207 imx_ocotp_clr_err_if_set(priv);
Peng Fan3311bf12020-11-27 10:28:37 +0000208
209 buf += 4;
Richard Leitner9b665872017-03-31 13:44:54 +0100210 }
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100211
Peng Fan3311bf12020-11-27 10:28:37 +0000212 index = offset % 4;
213 memcpy(val, &p[index], bytes);
214
Richard Leitner0642bac2017-03-31 13:44:55 +0100215read_end:
Peng Fandeb31972016-06-02 12:05:11 +0100216 clk_disable_unprepare(priv->clk);
Richard Leitner0642bac2017-03-31 13:44:55 +0100217 mutex_unlock(&ocotp_mutex);
Peng Fan3311bf12020-11-27 10:28:37 +0000218
219 kfree(p);
220
Richard Leitner0642bac2017-03-31 13:44:55 +0100221 return ret;
222}
223
Bryan O'Donoghueb50cb682017-10-24 10:54:30 +0100224static void imx_ocotp_set_imx6_timing(struct ocotp_priv *priv)
Richard Leitner0642bac2017-03-31 13:44:55 +0100225{
Anson Huang13d588b2020-03-10 13:22:53 +0000226 unsigned long clk_rate;
Richard Leitner0642bac2017-03-31 13:44:55 +0100227 unsigned long strobe_read, relax, strobe_prog;
Anson Huang13d588b2020-03-10 13:22:53 +0000228 u32 timing;
Richard Leitner0642bac2017-03-31 13:44:55 +0100229
230 /* 47.3.1.3.1
231 * Program HW_OCOTP_TIMING[STROBE_PROG] and HW_OCOTP_TIMING[RELAX]
232 * fields with timing values to match the current frequency of the
233 * ipg_clk. OTP writes will work at maximum bus frequencies as long
234 * as the HW_OCOTP_TIMING parameters are set correctly.
Bryan O'Donoghue159dbaf2019-06-26 11:27:30 +0100235 *
236 * Note: there are minimum timings required to ensure an OTP fuse burns
237 * correctly that are independent of the ipg_clk. Those values are not
238 * formally documented anywhere however, working from the minimum
239 * timings given in u-boot we can say:
240 *
241 * - Minimum STROBE_PROG time is 10 microseconds. Intuitively 10
242 * microseconds feels about right as representative of a minimum time
243 * to physically burn out a fuse.
244 *
245 * - Minimum STROBE_READ i.e. the time to wait post OTP fuse burn before
246 * performing another read is 37 nanoseconds
247 *
248 * - Minimum RELAX timing is 17 nanoseconds. This final RELAX minimum
249 * timing is not entirely clear the documentation says "This
250 * count value specifies the time to add to all default timing
251 * parameters other than the Tpgm and Trd. It is given in number
252 * of ipg_clk periods." where Tpgm and Trd refer to STROBE_PROG
253 * and STROBE_READ respectively. What the other timing parameters
254 * are though, is not specified. Experience shows a zero RELAX
255 * value will mess up a re-load of the shadow registers post OTP
256 * burn.
Richard Leitner0642bac2017-03-31 13:44:55 +0100257 */
258 clk_rate = clk_get_rate(priv->clk);
259
Bryan O'Donoghue159dbaf2019-06-26 11:27:30 +0100260 relax = DIV_ROUND_UP(clk_rate * TIMING_RELAX_NS, 1000000000) - 1;
261 strobe_read = DIV_ROUND_UP(clk_rate * TIMING_STROBE_READ_NS,
262 1000000000);
263 strobe_read += 2 * (relax + 1) - 1;
264 strobe_prog = DIV_ROUND_CLOSEST(clk_rate * TIMING_STROBE_PROG_US,
265 1000000);
266 strobe_prog += 2 * (relax + 1) - 1;
Richard Leitner0642bac2017-03-31 13:44:55 +0100267
Bryan O'Donoghue0493c472019-06-26 11:27:29 +0100268 timing = readl(priv->base + IMX_OCOTP_ADDR_TIMING) & 0x0FC00000;
269 timing |= strobe_prog & 0x00000FFF;
Richard Leitner0642bac2017-03-31 13:44:55 +0100270 timing |= (relax << 12) & 0x0000F000;
271 timing |= (strobe_read << 16) & 0x003F0000;
272
273 writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
Bryan O'Donoghueb50cb682017-10-24 10:54:30 +0100274}
275
Bryan O'Donoghue828ae7a42017-10-24 10:54:31 +0100276static void imx_ocotp_set_imx7_timing(struct ocotp_priv *priv)
277{
Anson Huang13d588b2020-03-10 13:22:53 +0000278 unsigned long clk_rate;
Bryan O'Donoghue828ae7a42017-10-24 10:54:31 +0100279 u64 fsource, strobe_prog;
Anson Huang13d588b2020-03-10 13:22:53 +0000280 u32 timing;
Bryan O'Donoghue828ae7a42017-10-24 10:54:31 +0100281
282 /* i.MX 7Solo Applications Processor Reference Manual, Rev. 0.1
283 * 6.4.3.3
284 */
285 clk_rate = clk_get_rate(priv->clk);
286 fsource = DIV_ROUND_UP_ULL((u64)clk_rate * DEF_FSOURCE,
287 NSEC_PER_SEC) + 1;
288 strobe_prog = DIV_ROUND_CLOSEST_ULL((u64)clk_rate * DEF_STROBE_PROG,
289 NSEC_PER_SEC) + 1;
290
291 timing = strobe_prog & 0x00000FFF;
292 timing |= (fsource << 12) & 0x000FF000;
293
294 writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
295}
296
Bryan O'Donoghueb50cb682017-10-24 10:54:30 +0100297static int imx_ocotp_write(void *context, unsigned int offset, void *val,
298 size_t bytes)
299{
300 struct ocotp_priv *priv = context;
301 u32 *buf = val;
302 int ret;
303
304 u32 ctrl;
305 u8 waddr;
306 u8 word = 0;
307
308 /* allow only writing one complete OTP word at a time */
309 if ((bytes != priv->config->word_size) ||
310 (offset % priv->config->word_size))
311 return -EINVAL;
312
313 mutex_lock(&ocotp_mutex);
314
315 ret = clk_prepare_enable(priv->clk);
316 if (ret < 0) {
317 mutex_unlock(&ocotp_mutex);
318 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
319 return ret;
320 }
321
322 /* Setup the write timing values */
Bryan O'Donoghue828ae7a42017-10-24 10:54:31 +0100323 priv->params->set_timing(priv);
Richard Leitner0642bac2017-03-31 13:44:55 +0100324
325 /* 47.3.1.3.2
326 * Check that HW_OCOTP_CTRL[BUSY] and HW_OCOTP_CTRL[ERROR] are clear.
327 * Overlapped accesses are not supported by the controller. Any pending
328 * write or reload must be completed before a write access can be
329 * requested.
330 */
Peng Fan226c5122020-01-09 10:40:16 +0000331 ret = imx_ocotp_wait_for_busy(priv, 0);
Richard Leitner0642bac2017-03-31 13:44:55 +0100332 if (ret < 0) {
333 dev_err(priv->dev, "timeout during timing setup\n");
334 goto write_end;
335 }
336
337 /* 47.3.1.3.3
338 * Write the requested address to HW_OCOTP_CTRL[ADDR] and program the
339 * unlock code into HW_OCOTP_CTRL[WR_UNLOCK]. This must be programmed
340 * for each write access. The lock code is documented in the register
341 * description. Both the unlock code and address can be written in the
342 * same operation.
343 */
Bryan O'Donoghueffd91152017-10-24 10:54:29 +0100344 if (priv->params->bank_address_words != 0) {
345 /*
346 * In banked/i.MX7 mode the OTP register bank goes into waddr
347 * see i.MX 7Solo Applications Processor Reference Manual, Rev.
348 * 0.1 section 6.4.3.1
349 */
350 offset = offset / priv->config->word_size;
351 waddr = offset / priv->params->bank_address_words;
352 word = offset & (priv->params->bank_address_words - 1);
353 } else {
354 /*
355 * Non-banked i.MX6 mode.
356 * OTP write/read address specifies one of 128 word address
357 * locations
358 */
359 waddr = offset / 4;
360 }
Richard Leitner0642bac2017-03-31 13:44:55 +0100361
362 ctrl = readl(priv->base + IMX_OCOTP_ADDR_CTRL);
Peng Fan226c5122020-01-09 10:40:16 +0000363 ctrl &= ~priv->params->ctrl.bm_addr;
364 ctrl |= waddr & priv->params->ctrl.bm_addr;
Richard Leitner0642bac2017-03-31 13:44:55 +0100365 ctrl |= IMX_OCOTP_WR_UNLOCK;
366
367 writel(ctrl, priv->base + IMX_OCOTP_ADDR_CTRL);
368
369 /* 47.3.1.3.4
370 * Write the data to the HW_OCOTP_DATA register. This will automatically
371 * set HW_OCOTP_CTRL[BUSY] and clear HW_OCOTP_CTRL[WR_UNLOCK]. To
372 * protect programming same OTP bit twice, before program OCOTP will
373 * automatically read fuse value in OTP and use read value to mask
374 * program data. The controller will use masked program data to program
375 * a 32-bit word in the OTP per the address in HW_OCOTP_CTRL[ADDR]. Bit
376 * fields with 1's will result in that OTP bit being programmed. Bit
377 * fields with 0's will be ignored. At the same time that the write is
378 * accepted, the controller makes an internal copy of
379 * HW_OCOTP_CTRL[ADDR] which cannot be updated until the next write
380 * sequence is initiated. This copy guarantees that erroneous writes to
381 * HW_OCOTP_CTRL[ADDR] will not affect an active write operation. It
382 * should also be noted that during the programming HW_OCOTP_DATA will
383 * shift right (with zero fill). This shifting is required to program
384 * the OTP serially. During the write operation, HW_OCOTP_DATA cannot be
385 * modified.
Bryan O'Donoghueffd91152017-10-24 10:54:29 +0100386 * Note: on i.MX7 there are four data fields to write for banked write
387 * with the fuse blowing operation only taking place after data0
388 * has been written. This is why data0 must always be the last
389 * register written.
Richard Leitner0642bac2017-03-31 13:44:55 +0100390 */
Bryan O'Donoghueffd91152017-10-24 10:54:29 +0100391 if (priv->params->bank_address_words != 0) {
392 /* Banked/i.MX7 mode */
393 switch (word) {
394 case 0:
395 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
396 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
397 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
398 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
399 break;
400 case 1:
401 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA1);
402 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
403 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
404 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
405 break;
406 case 2:
407 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
408 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA2);
409 writel(0, priv->base + IMX_OCOTP_ADDR_DATA3);
410 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
411 break;
412 case 3:
413 writel(0, priv->base + IMX_OCOTP_ADDR_DATA1);
414 writel(0, priv->base + IMX_OCOTP_ADDR_DATA2);
415 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA3);
416 writel(0, priv->base + IMX_OCOTP_ADDR_DATA0);
417 break;
418 }
419 } else {
420 /* Non-banked i.MX6 mode */
421 writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0);
422 }
Richard Leitner0642bac2017-03-31 13:44:55 +0100423
424 /* 47.4.1.4.5
425 * Once complete, the controller will clear BUSY. A write request to a
426 * protected or locked region will result in no OTP access and no
427 * setting of HW_OCOTP_CTRL[BUSY]. In addition HW_OCOTP_CTRL[ERROR] will
428 * be set. It must be cleared by software before any new write access
429 * can be issued.
430 */
Peng Fan226c5122020-01-09 10:40:16 +0000431 ret = imx_ocotp_wait_for_busy(priv, 0);
Richard Leitner0642bac2017-03-31 13:44:55 +0100432 if (ret < 0) {
433 if (ret == -EPERM) {
434 dev_err(priv->dev, "failed write to locked region");
Peng Fan226c5122020-01-09 10:40:16 +0000435 imx_ocotp_clr_err_if_set(priv);
Richard Leitner0642bac2017-03-31 13:44:55 +0100436 } else {
437 dev_err(priv->dev, "timeout during data write\n");
438 }
439 goto write_end;
440 }
441
442 /* 47.3.1.4
443 * Write Postamble: Due to internal electrical characteristics of the
444 * OTP during writes, all OTP operations following a write must be
445 * separated by 2 us after the clearing of HW_OCOTP_CTRL_BUSY following
446 * the write.
447 */
448 udelay(2);
449
450 /* reload all shadow registers */
Peng Fan226c5122020-01-09 10:40:16 +0000451 writel(priv->params->ctrl.bm_rel_shadows,
Richard Leitner0642bac2017-03-31 13:44:55 +0100452 priv->base + IMX_OCOTP_ADDR_CTRL_SET);
Peng Fan226c5122020-01-09 10:40:16 +0000453 ret = imx_ocotp_wait_for_busy(priv,
454 priv->params->ctrl.bm_rel_shadows);
Anson Huang0e2abff2020-05-11 15:50:40 +0100455 if (ret < 0)
Richard Leitner0642bac2017-03-31 13:44:55 +0100456 dev_err(priv->dev, "timeout during shadow register reload\n");
Richard Leitner0642bac2017-03-31 13:44:55 +0100457
458write_end:
459 clk_disable_unprepare(priv->clk);
460 mutex_unlock(&ocotp_mutex);
Anson Huang0e2abff2020-05-11 15:50:40 +0100461 return ret < 0 ? ret : bytes;
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100462}
463
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100464static struct nvmem_config imx_ocotp_nvmem_config = {
465 .name = "imx-ocotp",
Richard Leitner0642bac2017-03-31 13:44:55 +0100466 .read_only = false,
Srinivas Kandagatla33e5e292016-04-24 20:28:13 +0100467 .word_size = 4,
Peng Fan3311bf12020-11-27 10:28:37 +0000468 .stride = 1,
Srinivas Kandagatla33e5e292016-04-24 20:28:13 +0100469 .reg_read = imx_ocotp_read,
Richard Leitner0642bac2017-03-31 13:44:55 +0100470 .reg_write = imx_ocotp_write,
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100471};
472
Bryan O'Donoghuee20d2b22017-10-24 10:54:28 +0100473static const struct ocotp_params imx6q_params = {
474 .nregs = 128,
Bryan O'Donoghueffd91152017-10-24 10:54:29 +0100475 .bank_address_words = 0,
Bryan O'Donoghue828ae7a42017-10-24 10:54:31 +0100476 .set_timing = imx_ocotp_set_imx6_timing,
Peng Fan226c5122020-01-09 10:40:16 +0000477 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
Bryan O'Donoghuee20d2b22017-10-24 10:54:28 +0100478};
479
480static const struct ocotp_params imx6sl_params = {
481 .nregs = 64,
Bryan O'Donoghueffd91152017-10-24 10:54:29 +0100482 .bank_address_words = 0,
Bryan O'Donoghue828ae7a42017-10-24 10:54:31 +0100483 .set_timing = imx_ocotp_set_imx6_timing,
Peng Fan226c5122020-01-09 10:40:16 +0000484 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
Bryan O'Donoghuee20d2b22017-10-24 10:54:28 +0100485};
486
Anson Huang6da27822018-07-11 11:20:43 +0100487static const struct ocotp_params imx6sll_params = {
488 .nregs = 128,
489 .bank_address_words = 0,
490 .set_timing = imx_ocotp_set_imx6_timing,
Peng Fan226c5122020-01-09 10:40:16 +0000491 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
Anson Huang6da27822018-07-11 11:20:43 +0100492};
493
Bryan O'Donoghuee20d2b22017-10-24 10:54:28 +0100494static const struct ocotp_params imx6sx_params = {
495 .nregs = 128,
Bryan O'Donoghueffd91152017-10-24 10:54:29 +0100496 .bank_address_words = 0,
Bryan O'Donoghue828ae7a42017-10-24 10:54:31 +0100497 .set_timing = imx_ocotp_set_imx6_timing,
Peng Fan226c5122020-01-09 10:40:16 +0000498 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
Bryan O'Donoghuee20d2b22017-10-24 10:54:28 +0100499};
500
501static const struct ocotp_params imx6ul_params = {
502 .nregs = 128,
Bryan O'Donoghueffd91152017-10-24 10:54:29 +0100503 .bank_address_words = 0,
Bryan O'Donoghue828ae7a42017-10-24 10:54:31 +0100504 .set_timing = imx_ocotp_set_imx6_timing,
Peng Fan226c5122020-01-09 10:40:16 +0000505 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
Bryan O'Donoghuee20d2b22017-10-24 10:54:28 +0100506};
507
Stefan Wahrenffbc34b2019-01-28 15:54:59 +0000508static const struct ocotp_params imx6ull_params = {
509 .nregs = 64,
510 .bank_address_words = 0,
511 .set_timing = imx_ocotp_set_imx6_timing,
Peng Fan226c5122020-01-09 10:40:16 +0000512 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
Stefan Wahrenffbc34b2019-01-28 15:54:59 +0000513};
514
Bryan O'Donoghuee20d2b22017-10-24 10:54:28 +0100515static const struct ocotp_params imx7d_params = {
516 .nregs = 64,
Bryan O'Donoghueffd91152017-10-24 10:54:29 +0100517 .bank_address_words = 4,
Bryan O'Donoghue828ae7a42017-10-24 10:54:31 +0100518 .set_timing = imx_ocotp_set_imx7_timing,
Peng Fan226c5122020-01-09 10:40:16 +0000519 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
Bryan O'Donoghuee20d2b22017-10-24 10:54:28 +0100520};
521
Anson Huangc8b63dd2019-01-28 15:54:57 +0000522static const struct ocotp_params imx7ulp_params = {
523 .nregs = 256,
524 .bank_address_words = 0,
Peng Fan226c5122020-01-09 10:40:16 +0000525 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
Anson Huangc8b63dd2019-01-28 15:54:57 +0000526};
527
Lucas Stach38e7b6e2019-04-13 11:32:47 +0100528static const struct ocotp_params imx8mq_params = {
529 .nregs = 256,
Leonard Crestez5a1c1722019-06-26 11:27:33 +0100530 .bank_address_words = 0,
531 .set_timing = imx_ocotp_set_imx6_timing,
Peng Fan226c5122020-01-09 10:40:16 +0000532 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
Lucas Stach38e7b6e2019-04-13 11:32:47 +0100533};
534
Bryan O'Donoghue4112c8532019-06-26 11:27:31 +0100535static const struct ocotp_params imx8mm_params = {
536 .nregs = 256,
537 .bank_address_words = 0,
538 .set_timing = imx_ocotp_set_imx6_timing,
Peng Fan226c5122020-01-09 10:40:16 +0000539 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
Bryan O'Donoghue4112c8532019-06-26 11:27:31 +0100540};
541
Anson Huangd93b5d42019-08-18 10:33:40 +0100542static const struct ocotp_params imx8mn_params = {
543 .nregs = 256,
544 .bank_address_words = 0,
545 .set_timing = imx_ocotp_set_imx6_timing,
Peng Fan226c5122020-01-09 10:40:16 +0000546 .ctrl = IMX_OCOTP_BM_CTRL_DEFAULT,
Anson Huangd93b5d42019-08-18 10:33:40 +0100547};
548
Peng Fanc3f4af82020-03-10 13:22:44 +0000549static const struct ocotp_params imx8mp_params = {
550 .nregs = 384,
551 .bank_address_words = 0,
552 .set_timing = imx_ocotp_set_imx6_timing,
553 .ctrl = IMX_OCOTP_BM_CTRL_8MP,
554};
555
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100556static const struct of_device_id imx_ocotp_dt_ids[] = {
Bryan O'Donoghuee20d2b22017-10-24 10:54:28 +0100557 { .compatible = "fsl,imx6q-ocotp", .data = &imx6q_params },
558 { .compatible = "fsl,imx6sl-ocotp", .data = &imx6sl_params },
559 { .compatible = "fsl,imx6sx-ocotp", .data = &imx6sx_params },
560 { .compatible = "fsl,imx6ul-ocotp", .data = &imx6ul_params },
Stefan Wahrenffbc34b2019-01-28 15:54:59 +0000561 { .compatible = "fsl,imx6ull-ocotp", .data = &imx6ull_params },
Bryan O'Donoghuee20d2b22017-10-24 10:54:28 +0100562 { .compatible = "fsl,imx7d-ocotp", .data = &imx7d_params },
Anson Huang6da27822018-07-11 11:20:43 +0100563 { .compatible = "fsl,imx6sll-ocotp", .data = &imx6sll_params },
Anson Huangc8b63dd2019-01-28 15:54:57 +0000564 { .compatible = "fsl,imx7ulp-ocotp", .data = &imx7ulp_params },
Lucas Stach38e7b6e2019-04-13 11:32:47 +0100565 { .compatible = "fsl,imx8mq-ocotp", .data = &imx8mq_params },
Bryan O'Donoghue4112c8532019-06-26 11:27:31 +0100566 { .compatible = "fsl,imx8mm-ocotp", .data = &imx8mm_params },
Anson Huangd93b5d42019-08-18 10:33:40 +0100567 { .compatible = "fsl,imx8mn-ocotp", .data = &imx8mn_params },
Peng Fanc3f4af82020-03-10 13:22:44 +0000568 { .compatible = "fsl,imx8mp-ocotp", .data = &imx8mp_params },
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100569 { },
570};
571MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
572
573static int imx_ocotp_probe(struct platform_device *pdev)
574{
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100575 struct device *dev = &pdev->dev;
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100576 struct ocotp_priv *priv;
577 struct nvmem_device *nvmem;
578
579 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
580 if (!priv)
581 return -ENOMEM;
582
Richard Leitner4cefb742017-03-31 13:44:49 +0100583 priv->dev = dev;
584
Anson Huang3b26cd82019-04-13 11:32:59 +0100585 priv->base = devm_platform_ioremap_resource(pdev, 0);
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100586 if (IS_ERR(priv->base))
587 return PTR_ERR(priv->base);
588
Richard Leitner4cefb742017-03-31 13:44:49 +0100589 priv->clk = devm_clk_get(dev, NULL);
Peng Fandeb31972016-06-02 12:05:11 +0100590 if (IS_ERR(priv->clk))
591 return PTR_ERR(priv->clk);
592
Bryan O'Donoghuee20d2b22017-10-24 10:54:28 +0100593 priv->params = of_device_get_match_data(&pdev->dev);
594 imx_ocotp_nvmem_config.size = 4 * priv->params->nregs;
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100595 imx_ocotp_nvmem_config.dev = dev;
Srinivas Kandagatla33e5e292016-04-24 20:28:13 +0100596 imx_ocotp_nvmem_config.priv = priv;
Richard Leitner0642bac2017-03-31 13:44:55 +0100597 priv->config = &imx_ocotp_nvmem_config;
Richard Leitner0642bac2017-03-31 13:44:55 +0100598
Peng Fan226c5122020-01-09 10:40:16 +0000599 clk_prepare_enable(priv->clk);
600 imx_ocotp_clr_err_if_set(priv);
601 clk_disable_unprepare(priv->clk);
602
603 nvmem = devm_nvmem_register(dev, &imx_ocotp_nvmem_config);
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100604
Andrey Smirnova8302742018-03-09 14:46:59 +0000605 return PTR_ERR_OR_ZERO(nvmem);
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100606}
607
608static struct platform_driver imx_ocotp_driver = {
609 .probe = imx_ocotp_probe,
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100610 .driver = {
611 .name = "imx_ocotp",
612 .of_match_table = imx_ocotp_dt_ids,
613 },
614};
615module_platform_driver(imx_ocotp_driver);
616
617MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
Bryan O'Donoghueaef9a4d2017-10-24 10:54:33 +0100618MODULE_DESCRIPTION("i.MX6/i.MX7 OCOTP fuse box driver");
Philipp Zabel3edba6b2015-09-30 13:55:47 +0100619MODULE_LICENSE("GPL v2");