mxc_ocotp.c 10 KB

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