mxc_ocotp.c 8.8 KB

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