mxc_ocotp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2013 ADVANSEE
  4. * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
  5. *
  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,
  10. * which is:
  11. * Copyright (C) 2011 Freescale Semiconductor, Inc.
  12. */
  13. #include <common.h>
  14. #include <fuse.h>
  15. #include <linux/errno.h>
  16. #include <asm/io.h>
  17. #include <asm/arch/clock.h>
  18. #include <asm/arch/imx-regs.h>
  19. #include <asm/mach-imx/sys_proto.h>
  20. #define BO_CTRL_WR_UNLOCK 16
  21. #define BM_CTRL_WR_UNLOCK 0xffff0000
  22. #define BV_CTRL_WR_UNLOCK_KEY 0x3e77
  23. #define BM_CTRL_ERROR 0x00000200
  24. #define BM_CTRL_BUSY 0x00000100
  25. #define BO_CTRL_ADDR 0
  26. #ifdef CONFIG_MX7
  27. #define BM_CTRL_ADDR 0x0000000f
  28. #define BM_CTRL_RELOAD 0x00000400
  29. #elif defined(CONFIG_MX7ULP)
  30. #define BM_CTRL_ADDR 0x000000FF
  31. #define BM_CTRL_RELOAD 0x00000400
  32. #define BM_OUT_STATUS_DED 0x00000400
  33. #define BM_OUT_STATUS_LOCKED 0x00000800
  34. #define BM_OUT_STATUS_PROGFAIL 0x00001000
  35. #elif defined(CONFIG_MX8M)
  36. #define BM_CTRL_ADDR 0x000000ff
  37. #else
  38. #define BM_CTRL_ADDR 0x0000007f
  39. #endif
  40. #ifdef CONFIG_MX7
  41. #define BO_TIMING_FSOURCE 12
  42. #define BM_TIMING_FSOURCE 0x0007f000
  43. #define BV_TIMING_FSOURCE_NS 1001
  44. #define BO_TIMING_PROG 0
  45. #define BM_TIMING_PROG 0x00000fff
  46. #define BV_TIMING_PROG_US 10
  47. #else
  48. #define BO_TIMING_STROBE_READ 16
  49. #define BM_TIMING_STROBE_READ 0x003f0000
  50. #define BV_TIMING_STROBE_READ_NS 37
  51. #define BO_TIMING_RELAX 12
  52. #define BM_TIMING_RELAX 0x0000f000
  53. #define BV_TIMING_RELAX_NS 17
  54. #define BO_TIMING_STROBE_PROG 0
  55. #define BM_TIMING_STROBE_PROG 0x00000fff
  56. #define BV_TIMING_STROBE_PROG_US 10
  57. #endif
  58. #define BM_READ_CTRL_READ_FUSE 0x00000001
  59. #define BF(value, field) (((value) << BO_##field) & BM_##field)
  60. #define WRITE_POSTAMBLE_US 2
  61. #if defined(CONFIG_MX6) || defined(CONFIG_VF610)
  62. #define FUSE_BANK_SIZE 0x80
  63. #ifdef CONFIG_MX6SL
  64. #define FUSE_BANKS 8
  65. #elif defined(CONFIG_MX6ULL) || defined(CONFIG_MX6SLL)
  66. #define FUSE_BANKS 9
  67. #else
  68. #define FUSE_BANKS 16
  69. #endif
  70. #elif defined CONFIG_MX7
  71. #define FUSE_BANK_SIZE 0x40
  72. #define FUSE_BANKS 16
  73. #elif defined(CONFIG_MX7ULP)
  74. #define FUSE_BANK_SIZE 0x80
  75. #define FUSE_BANKS 31
  76. #elif defined(CONFIG_MX8M)
  77. #define FUSE_BANK_SIZE 0x40
  78. #define FUSE_BANKS 64
  79. #else
  80. #error "Unsupported architecture\n"
  81. #endif
  82. #if defined(CONFIG_MX6)
  83. /*
  84. * There is a hole in shadow registers address map of size 0x100
  85. * between bank 5 and bank 6 on iMX6QP, iMX6DQ, iMX6SDL, iMX6SX,
  86. * iMX6UL, i.MX6ULL and i.MX6SLL.
  87. * Bank 5 ends at 0x6F0 and Bank 6 starts at 0x800. When reading the fuses,
  88. * we should account for this hole in address space.
  89. *
  90. * Similar hole exists between bank 14 and bank 15 of size
  91. * 0x80 on iMX6QP, iMX6DQ, iMX6SDL and iMX6SX.
  92. * Note: iMX6SL has only 0-7 banks and there is no hole.
  93. * Note: iMX6UL doesn't have this one.
  94. *
  95. * This function is to covert user input to physical bank index.
  96. * Only needed when read fuse, because we use register offset, so
  97. * need to calculate real register offset.
  98. * When write, no need to consider hole, always use the bank/word
  99. * index from fuse map.
  100. */
  101. u32 fuse_bank_physical(int index)
  102. {
  103. u32 phy_index;
  104. if (is_mx6sl() || is_mx7ulp()) {
  105. phy_index = index;
  106. } else if (is_mx6ul() || is_mx6ull() || is_mx6sll()) {
  107. if ((is_mx6ull() || is_mx6sll()) && index == 8)
  108. index = 7;
  109. if (index >= 6)
  110. phy_index = fuse_bank_physical(5) + (index - 6) + 3;
  111. else
  112. phy_index = index;
  113. } else {
  114. if (index >= 15)
  115. phy_index = fuse_bank_physical(14) + (index - 15) + 2;
  116. else if (index >= 6)
  117. phy_index = fuse_bank_physical(5) + (index - 6) + 3;
  118. else
  119. phy_index = index;
  120. }
  121. return phy_index;
  122. }
  123. u32 fuse_word_physical(u32 bank, u32 word_index)
  124. {
  125. if (is_mx6ull() || is_mx6sll()) {
  126. if (bank == 8)
  127. word_index = word_index + 4;
  128. }
  129. return word_index;
  130. }
  131. #else
  132. u32 fuse_bank_physical(int index)
  133. {
  134. return index;
  135. }
  136. u32 fuse_word_physical(u32 bank, u32 word_index)
  137. {
  138. return word_index;
  139. }
  140. #endif
  141. static void wait_busy(struct ocotp_regs *regs, unsigned int delay_us)
  142. {
  143. while (readl(&regs->ctrl) & BM_CTRL_BUSY)
  144. udelay(delay_us);
  145. }
  146. static void clear_error(struct ocotp_regs *regs)
  147. {
  148. writel(BM_CTRL_ERROR, &regs->ctrl_clr);
  149. }
  150. static int prepare_access(struct ocotp_regs **regs, u32 bank, u32 word,
  151. int assert, const char *caller)
  152. {
  153. *regs = (struct ocotp_regs *)OCOTP_BASE_ADDR;
  154. if (bank >= FUSE_BANKS ||
  155. word >= ARRAY_SIZE((*regs)->bank[0].fuse_regs) >> 2 ||
  156. !assert) {
  157. printf("mxc_ocotp %s(): Invalid argument\n", caller);
  158. return -EINVAL;
  159. }
  160. if (is_mx6ull() || is_mx6sll()) {
  161. if ((bank == 7 || bank == 8) &&
  162. word >= ARRAY_SIZE((*regs)->bank[0].fuse_regs) >> 3) {
  163. printf("mxc_ocotp %s(): Invalid argument\n", caller);
  164. return -EINVAL;
  165. }
  166. }
  167. enable_ocotp_clk(1);
  168. wait_busy(*regs, 1);
  169. clear_error(*regs);
  170. return 0;
  171. }
  172. static int finish_access(struct ocotp_regs *regs, const char *caller)
  173. {
  174. u32 err;
  175. err = !!(readl(&regs->ctrl) & BM_CTRL_ERROR);
  176. clear_error(regs);
  177. #ifdef CONFIG_MX7ULP
  178. /* Need to power down the OTP memory */
  179. writel(1, &regs->pdn);
  180. #endif
  181. if (err) {
  182. printf("mxc_ocotp %s(): Access protect error\n", caller);
  183. return -EIO;
  184. }
  185. return 0;
  186. }
  187. static int prepare_read(struct ocotp_regs **regs, u32 bank, u32 word, u32 *val,
  188. const char *caller)
  189. {
  190. return prepare_access(regs, bank, word, val != NULL, caller);
  191. }
  192. int fuse_read(u32 bank, u32 word, u32 *val)
  193. {
  194. struct ocotp_regs *regs;
  195. int ret;
  196. u32 phy_bank;
  197. u32 phy_word;
  198. ret = prepare_read(&regs, bank, word, val, __func__);
  199. if (ret)
  200. return ret;
  201. phy_bank = fuse_bank_physical(bank);
  202. phy_word = fuse_word_physical(bank, word);
  203. *val = readl(&regs->bank[phy_bank].fuse_regs[phy_word << 2]);
  204. #ifdef CONFIG_MX7ULP
  205. if (readl(&regs->out_status) & BM_OUT_STATUS_DED) {
  206. writel(BM_OUT_STATUS_DED, &regs->out_status_clr);
  207. printf("mxc_ocotp %s(): fuse read wrong\n", __func__);
  208. return -EIO;
  209. }
  210. #endif
  211. return finish_access(regs, __func__);
  212. }
  213. #ifdef CONFIG_MX7
  214. static void set_timing(struct ocotp_regs *regs)
  215. {
  216. u32 ipg_clk;
  217. u32 fsource, prog;
  218. u32 timing;
  219. ipg_clk = mxc_get_clock(MXC_IPG_CLK);
  220. fsource = DIV_ROUND_UP((ipg_clk / 1000) * BV_TIMING_FSOURCE_NS,
  221. + 1000000) + 1;
  222. prog = DIV_ROUND_CLOSEST(ipg_clk * BV_TIMING_PROG_US, 1000000) + 1;
  223. timing = BF(fsource, TIMING_FSOURCE) | BF(prog, TIMING_PROG);
  224. clrsetbits_le32(&regs->timing, BM_TIMING_FSOURCE | BM_TIMING_PROG,
  225. timing);
  226. }
  227. #elif defined(CONFIG_MX7ULP)
  228. static void set_timing(struct ocotp_regs *regs)
  229. {
  230. /* No timing set for MX7ULP */
  231. }
  232. #else
  233. static void set_timing(struct ocotp_regs *regs)
  234. {
  235. u32 ipg_clk;
  236. u32 relax, strobe_read, strobe_prog;
  237. u32 timing;
  238. ipg_clk = mxc_get_clock(MXC_IPG_CLK);
  239. relax = DIV_ROUND_UP(ipg_clk * BV_TIMING_RELAX_NS, 1000000000) - 1;
  240. strobe_read = DIV_ROUND_UP(ipg_clk * BV_TIMING_STROBE_READ_NS,
  241. 1000000000) + 2 * (relax + 1) - 1;
  242. strobe_prog = DIV_ROUND_CLOSEST(ipg_clk * BV_TIMING_STROBE_PROG_US,
  243. 1000000) + 2 * (relax + 1) - 1;
  244. timing = BF(strobe_read, TIMING_STROBE_READ) |
  245. BF(relax, TIMING_RELAX) |
  246. BF(strobe_prog, TIMING_STROBE_PROG);
  247. clrsetbits_le32(&regs->timing, BM_TIMING_STROBE_READ | BM_TIMING_RELAX |
  248. BM_TIMING_STROBE_PROG, timing);
  249. }
  250. #endif
  251. static void setup_direct_access(struct ocotp_regs *regs, u32 bank, u32 word,
  252. int write)
  253. {
  254. u32 wr_unlock = write ? BV_CTRL_WR_UNLOCK_KEY : 0;
  255. #ifdef CONFIG_MX7
  256. u32 addr = bank;
  257. #elif defined CONFIG_MX8M
  258. u32 addr = bank << 2 | word;
  259. #else
  260. u32 addr;
  261. /* Bank 7 and Bank 8 only supports 4 words each for i.MX6ULL */
  262. if ((is_mx6ull() || is_mx6sll()) && (bank > 7)) {
  263. bank = bank - 1;
  264. word += 4;
  265. }
  266. addr = bank << 3 | word;
  267. #endif
  268. set_timing(regs);
  269. clrsetbits_le32(&regs->ctrl, BM_CTRL_WR_UNLOCK | BM_CTRL_ADDR,
  270. BF(wr_unlock, CTRL_WR_UNLOCK) |
  271. BF(addr, CTRL_ADDR));
  272. }
  273. int fuse_sense(u32 bank, u32 word, u32 *val)
  274. {
  275. struct ocotp_regs *regs;
  276. int ret;
  277. ret = prepare_read(&regs, bank, word, val, __func__);
  278. if (ret)
  279. return ret;
  280. setup_direct_access(regs, bank, word, false);
  281. writel(BM_READ_CTRL_READ_FUSE, &regs->read_ctrl);
  282. wait_busy(regs, 1);
  283. #ifdef CONFIG_MX7
  284. *val = readl((&regs->read_fuse_data0) + (word << 2));
  285. #else
  286. *val = readl(&regs->read_fuse_data);
  287. #endif
  288. #ifdef CONFIG_MX7ULP
  289. if (readl(&regs->out_status) & BM_OUT_STATUS_DED) {
  290. writel(BM_OUT_STATUS_DED, &regs->out_status_clr);
  291. printf("mxc_ocotp %s(): fuse read wrong\n", __func__);
  292. return -EIO;
  293. }
  294. #endif
  295. return finish_access(regs, __func__);
  296. }
  297. static int prepare_write(struct ocotp_regs **regs, u32 bank, u32 word,
  298. const char *caller)
  299. {
  300. #ifdef CONFIG_MX7ULP
  301. u32 val;
  302. int ret;
  303. /* Only bank 0 and 1 are redundancy mode, others are ECC mode */
  304. if (bank != 0 && bank != 1) {
  305. ret = fuse_sense(bank, word, &val);
  306. if (ret)
  307. return ret;
  308. if (val != 0) {
  309. printf("mxc_ocotp: The word has been programmed, no more write\n");
  310. return -EPERM;
  311. }
  312. }
  313. #endif
  314. return prepare_access(regs, bank, word, true, caller);
  315. }
  316. int fuse_prog(u32 bank, u32 word, u32 val)
  317. {
  318. struct ocotp_regs *regs;
  319. int ret;
  320. ret = prepare_write(&regs, bank, word, __func__);
  321. if (ret)
  322. return ret;
  323. setup_direct_access(regs, bank, word, true);
  324. #ifdef CONFIG_MX7
  325. switch (word) {
  326. case 0:
  327. writel(0, &regs->data1);
  328. writel(0, &regs->data2);
  329. writel(0, &regs->data3);
  330. writel(val, &regs->data0);
  331. break;
  332. case 1:
  333. writel(val, &regs->data1);
  334. writel(0, &regs->data2);
  335. writel(0, &regs->data3);
  336. writel(0, &regs->data0);
  337. break;
  338. case 2:
  339. writel(0, &regs->data1);
  340. writel(val, &regs->data2);
  341. writel(0, &regs->data3);
  342. writel(0, &regs->data0);
  343. break;
  344. case 3:
  345. writel(0, &regs->data1);
  346. writel(0, &regs->data2);
  347. writel(val, &regs->data3);
  348. writel(0, &regs->data0);
  349. break;
  350. }
  351. wait_busy(regs, BV_TIMING_PROG_US);
  352. #else
  353. writel(val, &regs->data);
  354. wait_busy(regs, BV_TIMING_STROBE_PROG_US);
  355. #endif
  356. udelay(WRITE_POSTAMBLE_US);
  357. #ifdef CONFIG_MX7ULP
  358. if (readl(&regs->out_status) & (BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED)) {
  359. writel((BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED), &regs->out_status_clr);
  360. printf("mxc_ocotp %s(): fuse write is failed\n", __func__);
  361. return -EIO;
  362. }
  363. #endif
  364. return finish_access(regs, __func__);
  365. }
  366. int fuse_override(u32 bank, u32 word, u32 val)
  367. {
  368. struct ocotp_regs *regs;
  369. int ret;
  370. u32 phy_bank;
  371. u32 phy_word;
  372. ret = prepare_write(&regs, bank, word, __func__);
  373. if (ret)
  374. return ret;
  375. phy_bank = fuse_bank_physical(bank);
  376. phy_word = fuse_word_physical(bank, word);
  377. writel(val, &regs->bank[phy_bank].fuse_regs[phy_word << 2]);
  378. #ifdef CONFIG_MX7ULP
  379. if (readl(&regs->out_status) & (BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED)) {
  380. writel((BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED), &regs->out_status_clr);
  381. printf("mxc_ocotp %s(): fuse write is failed\n", __func__);
  382. return -EIO;
  383. }
  384. #endif
  385. return finish_access(regs, __func__);
  386. }