1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2013 ADVANSEE
4 * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
6 * Based on Dirk Behme's
7 * https://github.com/dirkbehme/u-boot-imx6/blob/28b17e9/drivers/misc/imx_otp.c,
8 * which is based on Freescale's
9 * http://git.freescale.com/git/cgit.cgi/imx/uboot-imx.git/tree/drivers/misc/imx_otp.c?h=imx_v2009.08_1.1.0&id=9aa74e6,
11 * Copyright (C) 2011 Freescale Semiconductor, Inc.
16 #include <linux/errno.h>
18 #include <asm/arch/clock.h>
19 #include <asm/arch/imx-regs.h>
20 #include <asm/mach-imx/sys_proto.h>
22 #define BO_CTRL_WR_UNLOCK 16
23 #define BM_CTRL_WR_UNLOCK 0xffff0000
24 #define BV_CTRL_WR_UNLOCK_KEY 0x3e77
25 #define BM_CTRL_ERROR 0x00000200
26 #define BM_CTRL_BUSY 0x00000100
27 #define BO_CTRL_ADDR 0
29 #define BM_CTRL_ADDR 0x0000000f
30 #define BM_CTRL_RELOAD 0x00000400
31 #elif defined(CONFIG_MX7ULP)
32 #define BM_CTRL_ADDR 0x000000FF
33 #define BM_CTRL_RELOAD 0x00000400
34 #define BM_OUT_STATUS_DED 0x00000400
35 #define BM_OUT_STATUS_LOCKED 0x00000800
36 #define BM_OUT_STATUS_PROGFAIL 0x00001000
37 #elif defined(CONFIG_MX8M)
38 #define BM_CTRL_ADDR 0x000000ff
40 #define BM_CTRL_ADDR 0x0000007f
44 #define BO_TIMING_FSOURCE 12
45 #define BM_TIMING_FSOURCE 0x0007f000
46 #define BV_TIMING_FSOURCE_NS 1001
47 #define BO_TIMING_PROG 0
48 #define BM_TIMING_PROG 0x00000fff
49 #define BV_TIMING_PROG_US 10
51 #define BO_TIMING_STROBE_READ 16
52 #define BM_TIMING_STROBE_READ 0x003f0000
53 #define BV_TIMING_STROBE_READ_NS 37
54 #define BO_TIMING_RELAX 12
55 #define BM_TIMING_RELAX 0x0000f000
56 #define BV_TIMING_RELAX_NS 17
57 #define BO_TIMING_STROBE_PROG 0
58 #define BM_TIMING_STROBE_PROG 0x00000fff
59 #define BV_TIMING_STROBE_PROG_US 10
62 #define BM_READ_CTRL_READ_FUSE 0x00000001
64 #define BF(value, field) (((value) << BO_##field) & BM_##field)
66 #define WRITE_POSTAMBLE_US 2
68 #if defined(CONFIG_MX6) || defined(CONFIG_VF610)
69 #define FUSE_BANK_SIZE 0x80
72 #elif defined(CONFIG_MX6ULL) || defined(CONFIG_MX6SLL)
77 #elif defined CONFIG_MX7
78 #define FUSE_BANK_SIZE 0x40
80 #elif defined(CONFIG_MX7ULP)
81 #define FUSE_BANK_SIZE 0x80
83 #elif defined(CONFIG_MX8M)
84 #define FUSE_BANK_SIZE 0x40
87 #error "Unsupported architecture\n"
90 #if defined(CONFIG_MX6)
93 * There is a hole in shadow registers address map of size 0x100
94 * between bank 5 and bank 6 on iMX6QP, iMX6DQ, iMX6SDL, iMX6SX,
95 * iMX6UL, i.MX6ULL and i.MX6SLL.
96 * Bank 5 ends at 0x6F0 and Bank 6 starts at 0x800. When reading the fuses,
97 * we should account for this hole in address space.
99 * Similar hole exists between bank 14 and bank 15 of size
100 * 0x80 on iMX6QP, iMX6DQ, iMX6SDL and iMX6SX.
101 * Note: iMX6SL has only 0-7 banks and there is no hole.
102 * Note: iMX6UL doesn't have this one.
104 * This function is to covert user input to physical bank index.
105 * Only needed when read fuse, because we use register offset, so
106 * need to calculate real register offset.
107 * When write, no need to consider hole, always use the bank/word
108 * index from fuse map.
110 u32 fuse_bank_physical(int index)
114 if (is_mx6sl() || is_mx7ulp()) {
116 } else if (is_mx6ul() || is_mx6ull() || is_mx6sll()) {
117 if ((is_mx6ull() || is_mx6sll()) && index == 8)
121 phy_index = fuse_bank_physical(5) + (index - 6) + 3;
126 phy_index = fuse_bank_physical(14) + (index - 15) + 2;
128 phy_index = fuse_bank_physical(5) + (index - 6) + 3;
135 u32 fuse_word_physical(u32 bank, u32 word_index)
137 if (is_mx6ull() || is_mx6sll()) {
139 word_index = word_index + 4;
145 u32 fuse_bank_physical(int index)
150 u32 fuse_word_physical(u32 bank, u32 word_index)
157 static void wait_busy(struct ocotp_regs *regs, unsigned int delay_us)
159 while (readl(®s->ctrl) & BM_CTRL_BUSY)
163 static void clear_error(struct ocotp_regs *regs)
165 writel(BM_CTRL_ERROR, ®s->ctrl_clr);
168 static int prepare_access(struct ocotp_regs **regs, u32 bank, u32 word,
169 int assert, const char *caller)
171 *regs = (struct ocotp_regs *)OCOTP_BASE_ADDR;
173 if (bank >= FUSE_BANKS ||
174 word >= ARRAY_SIZE((*regs)->bank[0].fuse_regs) >> 2 ||
176 printf("mxc_ocotp %s(): Invalid argument\n", caller);
180 if (is_mx6ull() || is_mx6sll()) {
181 if ((bank == 7 || bank == 8) &&
182 word >= ARRAY_SIZE((*regs)->bank[0].fuse_regs) >> 3) {
183 printf("mxc_ocotp %s(): Invalid argument\n", caller);
196 static int finish_access(struct ocotp_regs *regs, const char *caller)
200 err = !!(readl(®s->ctrl) & BM_CTRL_ERROR);
204 /* Need to power down the OTP memory */
205 writel(1, ®s->pdn);
208 printf("mxc_ocotp %s(): Access protect error\n", caller);
215 static int prepare_read(struct ocotp_regs **regs, u32 bank, u32 word, u32 *val,
218 return prepare_access(regs, bank, word, val != NULL, caller);
221 int fuse_read(u32 bank, u32 word, u32 *val)
223 struct ocotp_regs *regs;
228 ret = prepare_read(®s, bank, word, val, __func__);
232 phy_bank = fuse_bank_physical(bank);
233 phy_word = fuse_word_physical(bank, word);
235 *val = readl(®s->bank[phy_bank].fuse_regs[phy_word << 2]);
238 if (readl(®s->out_status) & BM_OUT_STATUS_DED) {
239 writel(BM_OUT_STATUS_DED, ®s->out_status_clr);
240 printf("mxc_ocotp %s(): fuse read wrong\n", __func__);
244 return finish_access(regs, __func__);
248 static void set_timing(struct ocotp_regs *regs)
254 ipg_clk = mxc_get_clock(MXC_IPG_CLK);
256 fsource = DIV_ROUND_UP((ipg_clk / 1000) * BV_TIMING_FSOURCE_NS,
258 prog = DIV_ROUND_CLOSEST(ipg_clk * BV_TIMING_PROG_US, 1000000) + 1;
260 timing = BF(fsource, TIMING_FSOURCE) | BF(prog, TIMING_PROG);
262 clrsetbits_le32(®s->timing, BM_TIMING_FSOURCE | BM_TIMING_PROG,
265 #elif defined(CONFIG_MX7ULP)
266 static void set_timing(struct ocotp_regs *regs)
268 /* No timing set for MX7ULP */
272 static void set_timing(struct ocotp_regs *regs)
275 u32 relax, strobe_read, strobe_prog;
278 ipg_clk = mxc_get_clock(MXC_IPG_CLK);
280 relax = DIV_ROUND_UP(ipg_clk * BV_TIMING_RELAX_NS, 1000000000) - 1;
281 strobe_read = DIV_ROUND_UP(ipg_clk * BV_TIMING_STROBE_READ_NS,
282 1000000000) + 2 * (relax + 1) - 1;
283 strobe_prog = DIV_ROUND_CLOSEST(ipg_clk * BV_TIMING_STROBE_PROG_US,
284 1000000) + 2 * (relax + 1) - 1;
286 timing = BF(strobe_read, TIMING_STROBE_READ) |
287 BF(relax, TIMING_RELAX) |
288 BF(strobe_prog, TIMING_STROBE_PROG);
290 clrsetbits_le32(®s->timing, BM_TIMING_STROBE_READ | BM_TIMING_RELAX |
291 BM_TIMING_STROBE_PROG, timing);
295 static void setup_direct_access(struct ocotp_regs *regs, u32 bank, u32 word,
298 u32 wr_unlock = write ? BV_CTRL_WR_UNLOCK_KEY : 0;
301 #elif defined CONFIG_MX8M
302 u32 addr = bank << 2 | word;
305 /* Bank 7 and Bank 8 only supports 4 words each for i.MX6ULL */
306 if ((is_mx6ull() || is_mx6sll()) && (bank > 7)) {
310 addr = bank << 3 | word;
314 clrsetbits_le32(®s->ctrl, BM_CTRL_WR_UNLOCK | BM_CTRL_ADDR,
315 BF(wr_unlock, CTRL_WR_UNLOCK) |
316 BF(addr, CTRL_ADDR));
319 int fuse_sense(u32 bank, u32 word, u32 *val)
321 struct ocotp_regs *regs;
324 ret = prepare_read(®s, bank, word, val, __func__);
328 setup_direct_access(regs, bank, word, false);
329 writel(BM_READ_CTRL_READ_FUSE, ®s->read_ctrl);
332 *val = readl((®s->read_fuse_data0) + (word << 2));
334 *val = readl(®s->read_fuse_data);
338 if (readl(®s->out_status) & BM_OUT_STATUS_DED) {
339 writel(BM_OUT_STATUS_DED, ®s->out_status_clr);
340 printf("mxc_ocotp %s(): fuse read wrong\n", __func__);
345 return finish_access(regs, __func__);
348 static int prepare_write(struct ocotp_regs **regs, u32 bank, u32 word,
355 /* Only bank 0 and 1 are redundancy mode, others are ECC mode */
356 if (bank != 0 && bank != 1) {
357 ret = fuse_sense(bank, word, &val);
362 printf("mxc_ocotp: The word has been programmed, no more write\n");
368 return prepare_access(regs, bank, word, true, caller);
371 int fuse_prog(u32 bank, u32 word, u32 val)
373 struct ocotp_regs *regs;
376 ret = prepare_write(®s, bank, word, __func__);
380 setup_direct_access(regs, bank, word, true);
384 writel(0, ®s->data1);
385 writel(0, ®s->data2);
386 writel(0, ®s->data3);
387 writel(val, ®s->data0);
390 writel(val, ®s->data1);
391 writel(0, ®s->data2);
392 writel(0, ®s->data3);
393 writel(0, ®s->data0);
396 writel(0, ®s->data1);
397 writel(val, ®s->data2);
398 writel(0, ®s->data3);
399 writel(0, ®s->data0);
402 writel(0, ®s->data1);
403 writel(0, ®s->data2);
404 writel(val, ®s->data3);
405 writel(0, ®s->data0);
408 wait_busy(regs, BV_TIMING_PROG_US);
410 writel(val, ®s->data);
411 wait_busy(regs, BV_TIMING_STROBE_PROG_US);
413 udelay(WRITE_POSTAMBLE_US);
416 if (readl(®s->out_status) & (BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED)) {
417 writel((BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED), ®s->out_status_clr);
418 printf("mxc_ocotp %s(): fuse write is failed\n", __func__);
423 return finish_access(regs, __func__);
426 int fuse_override(u32 bank, u32 word, u32 val)
428 struct ocotp_regs *regs;
433 ret = prepare_write(®s, bank, word, __func__);
437 phy_bank = fuse_bank_physical(bank);
438 phy_word = fuse_word_physical(bank, word);
440 writel(val, ®s->bank[phy_bank].fuse_regs[phy_word << 2]);
443 if (readl(®s->out_status) & (BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED)) {
444 writel((BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED), ®s->out_status_clr);
445 printf("mxc_ocotp %s(): fuse write is failed\n", __func__);
450 return finish_access(regs, __func__);