mxc_ocotp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <asm/errno.h>
  17. #include <asm/io.h>
  18. #include <asm/arch/clock.h>
  19. #include <asm/arch/imx-regs.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. #else
  30. #define BM_CTRL_ADDR 0x0000007f
  31. #endif
  32. #ifdef CONFIG_MX7
  33. #define BO_TIMING_FSOURCE 12
  34. #define BM_TIMING_FSOURCE 0x0007f000
  35. #define BV_TIMING_FSOURCE_NS 1001
  36. #define BO_TIMING_PROG 0
  37. #define BM_TIMING_PROG 0x00000fff
  38. #define BV_TIMING_PROG_US 10
  39. #else
  40. #define BO_TIMING_STROBE_READ 16
  41. #define BM_TIMING_STROBE_READ 0x003f0000
  42. #define BV_TIMING_STROBE_READ_NS 37
  43. #define BO_TIMING_RELAX 12
  44. #define BM_TIMING_RELAX 0x0000f000
  45. #define BV_TIMING_RELAX_NS 17
  46. #define BO_TIMING_STROBE_PROG 0
  47. #define BM_TIMING_STROBE_PROG 0x00000fff
  48. #define BV_TIMING_STROBE_PROG_US 10
  49. #endif
  50. #define BM_READ_CTRL_READ_FUSE 0x00000001
  51. #define BF(value, field) (((value) << BO_##field) & BM_##field)
  52. #define WRITE_POSTAMBLE_US 2
  53. #if defined(CONFIG_MX6) || defined(CONFIG_VF610)
  54. #define FUSE_BANK_SIZE 0x80
  55. #ifdef CONFIG_MX6SL
  56. #define FUSE_BANKS 8
  57. #else
  58. #define FUSE_BANKS 16
  59. #endif
  60. #elif defined CONFIG_MX7
  61. #define FUSE_BANK_SIZE 0x40
  62. #define FUSE_BANKS 16
  63. #else
  64. #error "Unsupported architecture\n"
  65. #endif
  66. #if defined(CONFIG_MX6)
  67. #include <asm/arch/sys_proto.h>
  68. /*
  69. * There is a hole in shadow registers address map of size 0x100
  70. * between bank 5 and bank 6 on iMX6QP, iMX6DQ, iMX6SDL, iMX6SX and iMX6UL.
  71. * Bank 5 ends at 0x6F0 and Bank 6 starts at 0x800. When reading the fuses,
  72. * we should account for this hole in address space.
  73. *
  74. * Similar hole exists between bank 14 and bank 15 of size
  75. * 0x80 on iMX6QP, iMX6DQ, iMX6SDL and iMX6SX.
  76. * Note: iMX6SL has only 0-7 banks and there is no hole.
  77. * Note: iMX6UL doesn't have this one.
  78. *
  79. * This function is to covert user input to physical bank index.
  80. * Only needed when read fuse, because we use register offset, so
  81. * need to calculate real register offset.
  82. * When write, no need to consider hole, always use the bank/word
  83. * index from fuse map.
  84. */
  85. u32 fuse_bank_physical(int index)
  86. {
  87. u32 phy_index;
  88. if (is_mx6sl()) {
  89. phy_index = index;
  90. } else if (is_mx6ul()) {
  91. if (index >= 6)
  92. phy_index = fuse_bank_physical(5) + (index - 6) + 3;
  93. else
  94. phy_index = index;
  95. } else {
  96. if (index >= 15)
  97. phy_index = fuse_bank_physical(14) + (index - 15) + 2;
  98. else if (index >= 6)
  99. phy_index = fuse_bank_physical(5) + (index - 6) + 3;
  100. else
  101. phy_index = index;
  102. }
  103. return phy_index;
  104. }
  105. #else
  106. u32 fuse_bank_physical(int index)
  107. {
  108. return index;
  109. }
  110. #endif
  111. static void wait_busy(struct ocotp_regs *regs, unsigned int delay_us)
  112. {
  113. while (readl(&regs->ctrl) & BM_CTRL_BUSY)
  114. udelay(delay_us);
  115. }
  116. static void clear_error(struct ocotp_regs *regs)
  117. {
  118. writel(BM_CTRL_ERROR, &regs->ctrl_clr);
  119. }
  120. static int prepare_access(struct ocotp_regs **regs, u32 bank, u32 word,
  121. int assert, const char *caller)
  122. {
  123. *regs = (struct ocotp_regs *)OCOTP_BASE_ADDR;
  124. if (bank >= FUSE_BANKS ||
  125. word >= ARRAY_SIZE((*regs)->bank[0].fuse_regs) >> 2 ||
  126. !assert) {
  127. printf("mxc_ocotp %s(): Invalid argument\n", caller);
  128. return -EINVAL;
  129. }
  130. enable_ocotp_clk(1);
  131. wait_busy(*regs, 1);
  132. clear_error(*regs);
  133. return 0;
  134. }
  135. static int finish_access(struct ocotp_regs *regs, const char *caller)
  136. {
  137. u32 err;
  138. err = !!(readl(&regs->ctrl) & BM_CTRL_ERROR);
  139. clear_error(regs);
  140. if (err) {
  141. printf("mxc_ocotp %s(): Access protect error\n", caller);
  142. return -EIO;
  143. }
  144. return 0;
  145. }
  146. static int prepare_read(struct ocotp_regs **regs, u32 bank, u32 word, u32 *val,
  147. const char *caller)
  148. {
  149. return prepare_access(regs, bank, word, val != NULL, caller);
  150. }
  151. int fuse_read(u32 bank, u32 word, u32 *val)
  152. {
  153. struct ocotp_regs *regs;
  154. int ret;
  155. u32 phy_bank;
  156. ret = prepare_read(&regs, bank, word, val, __func__);
  157. if (ret)
  158. return ret;
  159. phy_bank = fuse_bank_physical(bank);
  160. *val = readl(&regs->bank[phy_bank].fuse_regs[word << 2]);
  161. return finish_access(regs, __func__);
  162. }
  163. #ifdef CONFIG_MX7
  164. static void set_timing(struct ocotp_regs *regs)
  165. {
  166. u32 ipg_clk;
  167. u32 fsource, prog;
  168. u32 timing;
  169. ipg_clk = mxc_get_clock(MXC_IPG_CLK);
  170. fsource = DIV_ROUND_UP((ipg_clk / 1000) * BV_TIMING_FSOURCE_NS,
  171. + 1000000) + 1;
  172. prog = DIV_ROUND_CLOSEST(ipg_clk * BV_TIMING_PROG_US, 1000000) + 1;
  173. timing = BF(fsource, TIMING_FSOURCE) | BF(prog, TIMING_PROG);
  174. clrsetbits_le32(&regs->timing, BM_TIMING_FSOURCE | BM_TIMING_PROG,
  175. timing);
  176. }
  177. #else
  178. static void set_timing(struct ocotp_regs *regs)
  179. {
  180. u32 ipg_clk;
  181. u32 relax, strobe_read, strobe_prog;
  182. u32 timing;
  183. ipg_clk = mxc_get_clock(MXC_IPG_CLK);
  184. relax = DIV_ROUND_UP(ipg_clk * BV_TIMING_RELAX_NS, 1000000000) - 1;
  185. strobe_read = DIV_ROUND_UP(ipg_clk * BV_TIMING_STROBE_READ_NS,
  186. 1000000000) + 2 * (relax + 1) - 1;
  187. strobe_prog = DIV_ROUND_CLOSEST(ipg_clk * BV_TIMING_STROBE_PROG_US,
  188. 1000000) + 2 * (relax + 1) - 1;
  189. timing = BF(strobe_read, TIMING_STROBE_READ) |
  190. BF(relax, TIMING_RELAX) |
  191. BF(strobe_prog, TIMING_STROBE_PROG);
  192. clrsetbits_le32(&regs->timing, BM_TIMING_STROBE_READ | BM_TIMING_RELAX |
  193. BM_TIMING_STROBE_PROG, timing);
  194. }
  195. #endif
  196. static void setup_direct_access(struct ocotp_regs *regs, u32 bank, u32 word,
  197. int write)
  198. {
  199. u32 wr_unlock = write ? BV_CTRL_WR_UNLOCK_KEY : 0;
  200. #ifdef CONFIG_MX7
  201. u32 addr = bank;
  202. #else
  203. u32 addr = bank << 3 | word;
  204. #endif
  205. set_timing(regs);
  206. clrsetbits_le32(&regs->ctrl, BM_CTRL_WR_UNLOCK | BM_CTRL_ADDR,
  207. BF(wr_unlock, CTRL_WR_UNLOCK) |
  208. BF(addr, CTRL_ADDR));
  209. }
  210. int fuse_sense(u32 bank, u32 word, u32 *val)
  211. {
  212. struct ocotp_regs *regs;
  213. int ret;
  214. ret = prepare_read(&regs, bank, word, val, __func__);
  215. if (ret)
  216. return ret;
  217. setup_direct_access(regs, bank, word, false);
  218. writel(BM_READ_CTRL_READ_FUSE, &regs->read_ctrl);
  219. wait_busy(regs, 1);
  220. #ifdef CONFIG_MX7
  221. *val = readl((&regs->read_fuse_data0) + (word << 2));
  222. #else
  223. *val = readl(&regs->read_fuse_data);
  224. #endif
  225. return finish_access(regs, __func__);
  226. }
  227. static int prepare_write(struct ocotp_regs **regs, u32 bank, u32 word,
  228. const char *caller)
  229. {
  230. return prepare_access(regs, bank, word, true, caller);
  231. }
  232. int fuse_prog(u32 bank, u32 word, u32 val)
  233. {
  234. struct ocotp_regs *regs;
  235. int ret;
  236. ret = prepare_write(&regs, bank, word, __func__);
  237. if (ret)
  238. return ret;
  239. setup_direct_access(regs, bank, word, true);
  240. #ifdef CONFIG_MX7
  241. switch (word) {
  242. case 0:
  243. writel(0, &regs->data1);
  244. writel(0, &regs->data2);
  245. writel(0, &regs->data3);
  246. writel(val, &regs->data0);
  247. break;
  248. case 1:
  249. writel(val, &regs->data1);
  250. writel(0, &regs->data2);
  251. writel(0, &regs->data3);
  252. writel(0, &regs->data0);
  253. break;
  254. case 2:
  255. writel(0, &regs->data1);
  256. writel(val, &regs->data2);
  257. writel(0, &regs->data3);
  258. writel(0, &regs->data0);
  259. break;
  260. case 3:
  261. writel(0, &regs->data1);
  262. writel(0, &regs->data2);
  263. writel(val, &regs->data3);
  264. writel(0, &regs->data0);
  265. break;
  266. }
  267. wait_busy(regs, BV_TIMING_PROG_US);
  268. #else
  269. writel(val, &regs->data);
  270. wait_busy(regs, BV_TIMING_STROBE_PROG_US);
  271. #endif
  272. udelay(WRITE_POSTAMBLE_US);
  273. return finish_access(regs, __func__);
  274. }
  275. int fuse_override(u32 bank, u32 word, u32 val)
  276. {
  277. struct ocotp_regs *regs;
  278. int ret;
  279. u32 phy_bank;
  280. ret = prepare_write(&regs, bank, word, __func__);
  281. if (ret)
  282. return ret;
  283. phy_bank = fuse_bank_physical(bank);
  284. writel(val, &regs->bank[phy_bank].fuse_regs[word << 2]);
  285. return finish_access(regs, __func__);
  286. }