blob: 88610d6af2cd60be0f40ba00ed8174cf09680a48 [file] [log] [blame]
Benoît Thébaudeau20db6312013-04-23 10:17:44 +00001/*
2 * (C) Copyright 2013 ADVANSEE
3 * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
4 *
5 * Based on Dirk Behme's
6 * https://github.com/dirkbehme/u-boot-imx6/blob/28b17e9/drivers/misc/imx_otp.c,
7 * which is based on Freescale's
8 * 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,
9 * which is:
10 * Copyright (C) 2011 Freescale Semiconductor, Inc.
11 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020012 * SPDX-License-Identifier: GPL-2.0+
Benoît Thébaudeau20db6312013-04-23 10:17:44 +000013 */
14
15#include <common.h>
16#include <fuse.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090017#include <linux/errno.h>
Benoît Thébaudeau20db6312013-04-23 10:17:44 +000018#include <asm/io.h>
19#include <asm/arch/clock.h>
20#include <asm/arch/imx-regs.h>
Peng Fancb356f92016-08-11 14:02:41 +080021#include <asm/imx-common/sys_proto.h>
Benoît Thébaudeau20db6312013-04-23 10:17:44 +000022
23#define BO_CTRL_WR_UNLOCK 16
24#define BM_CTRL_WR_UNLOCK 0xffff0000
25#define BV_CTRL_WR_UNLOCK_KEY 0x3e77
26#define BM_CTRL_ERROR 0x00000200
27#define BM_CTRL_BUSY 0x00000100
28#define BO_CTRL_ADDR 0
Adrian Alonso57b00132015-08-11 11:19:52 -050029#ifdef CONFIG_MX7
30#define BM_CTRL_ADDR 0x0000000f
31#define BM_CTRL_RELOAD 0x00000400
Peng Fan98c57822017-02-22 16:21:46 +080032#elif defined(CONFIG_MX7ULP)
33#define BM_CTRL_ADDR 0x000000FF
34#define BM_CTRL_RELOAD 0x00000400
35#define BM_OUT_STATUS_DED 0x00000400
36#define BM_OUT_STATUS_LOCKED 0x00000800
37#define BM_OUT_STATUS_PROGFAIL 0x00001000
Adrian Alonso57b00132015-08-11 11:19:52 -050038#else
Benoît Thébaudeau20db6312013-04-23 10:17:44 +000039#define BM_CTRL_ADDR 0x0000007f
Adrian Alonso57b00132015-08-11 11:19:52 -050040#endif
Benoît Thébaudeau20db6312013-04-23 10:17:44 +000041
Adrian Alonso57b00132015-08-11 11:19:52 -050042#ifdef CONFIG_MX7
43#define BO_TIMING_FSOURCE 12
44#define BM_TIMING_FSOURCE 0x0007f000
45#define BV_TIMING_FSOURCE_NS 1001
46#define BO_TIMING_PROG 0
47#define BM_TIMING_PROG 0x00000fff
48#define BV_TIMING_PROG_US 10
49#else
Benoît Thébaudeau20db6312013-04-23 10:17:44 +000050#define BO_TIMING_STROBE_READ 16
51#define BM_TIMING_STROBE_READ 0x003f0000
52#define BV_TIMING_STROBE_READ_NS 37
53#define BO_TIMING_RELAX 12
54#define BM_TIMING_RELAX 0x0000f000
55#define BV_TIMING_RELAX_NS 17
56#define BO_TIMING_STROBE_PROG 0
57#define BM_TIMING_STROBE_PROG 0x00000fff
58#define BV_TIMING_STROBE_PROG_US 10
Adrian Alonso57b00132015-08-11 11:19:52 -050059#endif
Benoît Thébaudeau20db6312013-04-23 10:17:44 +000060
61#define BM_READ_CTRL_READ_FUSE 0x00000001
62
63#define BF(value, field) (((value) << BO_##field) & BM_##field)
64
65#define WRITE_POSTAMBLE_US 2
66
Peng Fan52bae462015-08-26 15:40:47 +080067#if defined(CONFIG_MX6) || defined(CONFIG_VF610)
68#define FUSE_BANK_SIZE 0x80
69#ifdef CONFIG_MX6SL
70#define FUSE_BANKS 8
Peng Fana2c2f6c2016-12-11 19:24:33 +080071#elif defined(CONFIG_MX6ULL) || defined(CONFIG_MX6SLL)
Peng Fancb356f92016-08-11 14:02:41 +080072#define FUSE_BANKS 9
Peng Fan52bae462015-08-26 15:40:47 +080073#else
74#define FUSE_BANKS 16
75#endif
76#elif defined CONFIG_MX7
77#define FUSE_BANK_SIZE 0x40
78#define FUSE_BANKS 16
Peng Fan98c57822017-02-22 16:21:46 +080079#elif defined(CONFIG_MX7ULP)
80#define FUSE_BANK_SIZE 0x80
81#define FUSE_BANKS 31
Peng Fan52bae462015-08-26 15:40:47 +080082#else
83#error "Unsupported architecture\n"
84#endif
85
86#if defined(CONFIG_MX6)
Peng Fan52bae462015-08-26 15:40:47 +080087
88/*
89 * There is a hole in shadow registers address map of size 0x100
Peng Fancb356f92016-08-11 14:02:41 +080090 * between bank 5 and bank 6 on iMX6QP, iMX6DQ, iMX6SDL, iMX6SX,
Peng Fana2c2f6c2016-12-11 19:24:33 +080091 * iMX6UL, i.MX6ULL and i.MX6SLL.
Peng Fan52bae462015-08-26 15:40:47 +080092 * Bank 5 ends at 0x6F0 and Bank 6 starts at 0x800. When reading the fuses,
93 * we should account for this hole in address space.
94 *
95 * Similar hole exists between bank 14 and bank 15 of size
96 * 0x80 on iMX6QP, iMX6DQ, iMX6SDL and iMX6SX.
97 * Note: iMX6SL has only 0-7 banks and there is no hole.
98 * Note: iMX6UL doesn't have this one.
99 *
100 * This function is to covert user input to physical bank index.
101 * Only needed when read fuse, because we use register offset, so
102 * need to calculate real register offset.
103 * When write, no need to consider hole, always use the bank/word
104 * index from fuse map.
105 */
106u32 fuse_bank_physical(int index)
107{
108 u32 phy_index;
109
Peng Fan98c57822017-02-22 16:21:46 +0800110 if (is_mx6sl() || is_mx7ulp()) {
Peng Fan52bae462015-08-26 15:40:47 +0800111 phy_index = index;
Peng Fana2c2f6c2016-12-11 19:24:33 +0800112 } else if (is_mx6ul() || is_mx6ull() || is_mx6sll()) {
113 if ((is_mx6ull() || is_mx6sll()) && index == 8)
Peng Fancb356f92016-08-11 14:02:41 +0800114 index = 7;
115
Peng Fan52bae462015-08-26 15:40:47 +0800116 if (index >= 6)
117 phy_index = fuse_bank_physical(5) + (index - 6) + 3;
118 else
119 phy_index = index;
120 } else {
121 if (index >= 15)
122 phy_index = fuse_bank_physical(14) + (index - 15) + 2;
123 else if (index >= 6)
124 phy_index = fuse_bank_physical(5) + (index - 6) + 3;
125 else
126 phy_index = index;
127 }
128 return phy_index;
129}
Peng Fancb356f92016-08-11 14:02:41 +0800130
131u32 fuse_word_physical(u32 bank, u32 word_index)
132{
Peng Fana2c2f6c2016-12-11 19:24:33 +0800133 if (is_mx6ull() || is_mx6sll()) {
Peng Fancb356f92016-08-11 14:02:41 +0800134 if (bank == 8)
135 word_index = word_index + 4;
136 }
137
138 return word_index;
139}
Peng Fan52bae462015-08-26 15:40:47 +0800140#else
141u32 fuse_bank_physical(int index)
142{
143 return index;
144}
Peng Fancb356f92016-08-11 14:02:41 +0800145
146u32 fuse_word_physical(u32 bank, u32 word_index)
147{
148 return word_index;
149}
150
Peng Fan52bae462015-08-26 15:40:47 +0800151#endif
152
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000153static void wait_busy(struct ocotp_regs *regs, unsigned int delay_us)
154{
155 while (readl(&regs->ctrl) & BM_CTRL_BUSY)
156 udelay(delay_us);
157}
158
159static void clear_error(struct ocotp_regs *regs)
160{
161 writel(BM_CTRL_ERROR, &regs->ctrl_clr);
162}
163
164static int prepare_access(struct ocotp_regs **regs, u32 bank, u32 word,
165 int assert, const char *caller)
166{
167 *regs = (struct ocotp_regs *)OCOTP_BASE_ADDR;
168
Peng Fan52bae462015-08-26 15:40:47 +0800169 if (bank >= FUSE_BANKS ||
170 word >= ARRAY_SIZE((*regs)->bank[0].fuse_regs) >> 2 ||
171 !assert) {
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000172 printf("mxc_ocotp %s(): Invalid argument\n", caller);
173 return -EINVAL;
174 }
175
Peng Fana2c2f6c2016-12-11 19:24:33 +0800176 if (is_mx6ull() || is_mx6sll()) {
Peng Fancb356f92016-08-11 14:02:41 +0800177 if ((bank == 7 || bank == 8) &&
178 word >= ARRAY_SIZE((*regs)->bank[0].fuse_regs) >> 3) {
Peng Fana2c2f6c2016-12-11 19:24:33 +0800179 printf("mxc_ocotp %s(): Invalid argument\n", caller);
Peng Fancb356f92016-08-11 14:02:41 +0800180 return -EINVAL;
181 }
182 }
183
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000184 enable_ocotp_clk(1);
185
186 wait_busy(*regs, 1);
187 clear_error(*regs);
188
189 return 0;
190}
191
192static int finish_access(struct ocotp_regs *regs, const char *caller)
193{
194 u32 err;
195
196 err = !!(readl(&regs->ctrl) & BM_CTRL_ERROR);
197 clear_error(regs);
198
Peng Fan98c57822017-02-22 16:21:46 +0800199#ifdef CONFIG_MX7ULP
200 /* Need to power down the OTP memory */
201 writel(1, &regs->pdn);
202#endif
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000203 if (err) {
204 printf("mxc_ocotp %s(): Access protect error\n", caller);
205 return -EIO;
206 }
207
208 return 0;
209}
210
211static int prepare_read(struct ocotp_regs **regs, u32 bank, u32 word, u32 *val,
212 const char *caller)
213{
214 return prepare_access(regs, bank, word, val != NULL, caller);
215}
216
217int fuse_read(u32 bank, u32 word, u32 *val)
218{
219 struct ocotp_regs *regs;
220 int ret;
Peng Fan52bae462015-08-26 15:40:47 +0800221 u32 phy_bank;
Peng Fancb356f92016-08-11 14:02:41 +0800222 u32 phy_word;
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000223
224 ret = prepare_read(&regs, bank, word, val, __func__);
225 if (ret)
226 return ret;
227
Peng Fan52bae462015-08-26 15:40:47 +0800228 phy_bank = fuse_bank_physical(bank);
Peng Fancb356f92016-08-11 14:02:41 +0800229 phy_word = fuse_word_physical(bank, word);
Peng Fan52bae462015-08-26 15:40:47 +0800230
Peng Fancb356f92016-08-11 14:02:41 +0800231 *val = readl(&regs->bank[phy_bank].fuse_regs[phy_word << 2]);
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000232
Peng Fan98c57822017-02-22 16:21:46 +0800233#ifdef CONFIG_MX7ULP
234 if (readl(&regs->out_status) & BM_OUT_STATUS_DED) {
235 writel(BM_OUT_STATUS_DED, &regs->out_status_clr);
236 printf("mxc_ocotp %s(): fuse read wrong\n", __func__);
237 return -EIO;
238 }
239#endif
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000240 return finish_access(regs, __func__);
241}
242
Adrian Alonso57b00132015-08-11 11:19:52 -0500243#ifdef CONFIG_MX7
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000244static void set_timing(struct ocotp_regs *regs)
245{
246 u32 ipg_clk;
Adrian Alonso57b00132015-08-11 11:19:52 -0500247 u32 fsource, prog;
248 u32 timing;
249
250 ipg_clk = mxc_get_clock(MXC_IPG_CLK);
251
252 fsource = DIV_ROUND_UP((ipg_clk / 1000) * BV_TIMING_FSOURCE_NS,
253 + 1000000) + 1;
254 prog = DIV_ROUND_CLOSEST(ipg_clk * BV_TIMING_PROG_US, 1000000) + 1;
255
256 timing = BF(fsource, TIMING_FSOURCE) | BF(prog, TIMING_PROG);
257
258 clrsetbits_le32(&regs->timing, BM_TIMING_FSOURCE | BM_TIMING_PROG,
259 timing);
260}
Peng Fan98c57822017-02-22 16:21:46 +0800261#elif defined(CONFIG_MX7ULP)
262static void set_timing(struct ocotp_regs *regs)
263{
264 /* No timing set for MX7ULP */
265}
266
Adrian Alonso57b00132015-08-11 11:19:52 -0500267#else
268static void set_timing(struct ocotp_regs *regs)
269{
270 u32 ipg_clk;
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000271 u32 relax, strobe_read, strobe_prog;
272 u32 timing;
273
274 ipg_clk = mxc_get_clock(MXC_IPG_CLK);
275
276 relax = DIV_ROUND_UP(ipg_clk * BV_TIMING_RELAX_NS, 1000000000) - 1;
277 strobe_read = DIV_ROUND_UP(ipg_clk * BV_TIMING_STROBE_READ_NS,
278 1000000000) + 2 * (relax + 1) - 1;
Masahiro Yamadaeff8e682014-11-07 03:03:26 +0900279 strobe_prog = DIV_ROUND_CLOSEST(ipg_clk * BV_TIMING_STROBE_PROG_US,
280 1000000) + 2 * (relax + 1) - 1;
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000281
282 timing = BF(strobe_read, TIMING_STROBE_READ) |
283 BF(relax, TIMING_RELAX) |
284 BF(strobe_prog, TIMING_STROBE_PROG);
285
286 clrsetbits_le32(&regs->timing, BM_TIMING_STROBE_READ | BM_TIMING_RELAX |
287 BM_TIMING_STROBE_PROG, timing);
288}
Adrian Alonso57b00132015-08-11 11:19:52 -0500289#endif
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000290
291static void setup_direct_access(struct ocotp_regs *regs, u32 bank, u32 word,
292 int write)
293{
294 u32 wr_unlock = write ? BV_CTRL_WR_UNLOCK_KEY : 0;
Adrian Alonso57b00132015-08-11 11:19:52 -0500295#ifdef CONFIG_MX7
296 u32 addr = bank;
297#else
Peng Fancb356f92016-08-11 14:02:41 +0800298 u32 addr;
299 /* Bank 7 and Bank 8 only supports 4 words each for i.MX6ULL */
Peng Fana2c2f6c2016-12-11 19:24:33 +0800300 if ((is_mx6ull() || is_mx6sll()) && (bank > 7)) {
Peng Fancb356f92016-08-11 14:02:41 +0800301 bank = bank - 1;
302 word += 4;
303 }
304 addr = bank << 3 | word;
Adrian Alonso57b00132015-08-11 11:19:52 -0500305#endif
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000306
307 set_timing(regs);
308 clrsetbits_le32(&regs->ctrl, BM_CTRL_WR_UNLOCK | BM_CTRL_ADDR,
309 BF(wr_unlock, CTRL_WR_UNLOCK) |
310 BF(addr, CTRL_ADDR));
311}
312
313int fuse_sense(u32 bank, u32 word, u32 *val)
314{
315 struct ocotp_regs *regs;
316 int ret;
317
318 ret = prepare_read(&regs, bank, word, val, __func__);
319 if (ret)
320 return ret;
321
322 setup_direct_access(regs, bank, word, false);
323 writel(BM_READ_CTRL_READ_FUSE, &regs->read_ctrl);
324 wait_busy(regs, 1);
Adrian Alonso57b00132015-08-11 11:19:52 -0500325#ifdef CONFIG_MX7
326 *val = readl((&regs->read_fuse_data0) + (word << 2));
327#else
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000328 *val = readl(&regs->read_fuse_data);
Adrian Alonso57b00132015-08-11 11:19:52 -0500329#endif
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000330
Peng Fan98c57822017-02-22 16:21:46 +0800331#ifdef CONFIG_MX7ULP
332 if (readl(&regs->out_status) & BM_OUT_STATUS_DED) {
333 writel(BM_OUT_STATUS_DED, &regs->out_status_clr);
334 printf("mxc_ocotp %s(): fuse read wrong\n", __func__);
335 return -EIO;
336 }
337#endif
338
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000339 return finish_access(regs, __func__);
340}
341
342static int prepare_write(struct ocotp_regs **regs, u32 bank, u32 word,
343 const char *caller)
344{
345 return prepare_access(regs, bank, word, true, caller);
346}
347
348int fuse_prog(u32 bank, u32 word, u32 val)
349{
350 struct ocotp_regs *regs;
351 int ret;
352
353 ret = prepare_write(&regs, bank, word, __func__);
354 if (ret)
355 return ret;
356
357 setup_direct_access(regs, bank, word, true);
Adrian Alonso57b00132015-08-11 11:19:52 -0500358#ifdef CONFIG_MX7
359 switch (word) {
360 case 0:
361 writel(0, &regs->data1);
362 writel(0, &regs->data2);
363 writel(0, &regs->data3);
364 writel(val, &regs->data0);
365 break;
366 case 1:
367 writel(val, &regs->data1);
368 writel(0, &regs->data2);
369 writel(0, &regs->data3);
370 writel(0, &regs->data0);
371 break;
372 case 2:
373 writel(0, &regs->data1);
374 writel(val, &regs->data2);
375 writel(0, &regs->data3);
376 writel(0, &regs->data0);
377 break;
378 case 3:
379 writel(0, &regs->data1);
380 writel(0, &regs->data2);
381 writel(val, &regs->data3);
382 writel(0, &regs->data0);
383 break;
384 }
385 wait_busy(regs, BV_TIMING_PROG_US);
386#else
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000387 writel(val, &regs->data);
388 wait_busy(regs, BV_TIMING_STROBE_PROG_US);
Adrian Alonso57b00132015-08-11 11:19:52 -0500389#endif
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000390 udelay(WRITE_POSTAMBLE_US);
391
Peng Fan98c57822017-02-22 16:21:46 +0800392#ifdef CONFIG_MX7ULP
393 if (readl(&regs->out_status) & (BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED)) {
394 writel((BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED), &regs->out_status_clr);
395 printf("mxc_ocotp %s(): fuse write is failed\n", __func__);
396 return -EIO;
397 }
398#endif
399
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000400 return finish_access(regs, __func__);
401}
402
403int fuse_override(u32 bank, u32 word, u32 val)
404{
405 struct ocotp_regs *regs;
406 int ret;
Peng Fan52bae462015-08-26 15:40:47 +0800407 u32 phy_bank;
Peng Fancb356f92016-08-11 14:02:41 +0800408 u32 phy_word;
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000409
410 ret = prepare_write(&regs, bank, word, __func__);
411 if (ret)
412 return ret;
413
Peng Fan52bae462015-08-26 15:40:47 +0800414 phy_bank = fuse_bank_physical(bank);
Peng Fancb356f92016-08-11 14:02:41 +0800415 phy_word = fuse_word_physical(bank, word);
Peng Fan52bae462015-08-26 15:40:47 +0800416
Peng Fancb356f92016-08-11 14:02:41 +0800417 writel(val, &regs->bank[phy_bank].fuse_regs[phy_word << 2]);
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000418
Peng Fan98c57822017-02-22 16:21:46 +0800419#ifdef CONFIG_MX7ULP
420 if (readl(&regs->out_status) & (BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED)) {
421 writel((BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED), &regs->out_status_clr);
422 printf("mxc_ocotp %s(): fuse write is failed\n", __func__);
423 return -EIO;
424 }
425#endif
426
Benoît Thébaudeau20db6312013-04-23 10:17:44 +0000427 return finish_access(regs, __func__);
428}