mxc_ocotp.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. * See file CREDITS for list of people who contributed to this
  13. * project.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  28. * MA 02111-1307 USA
  29. */
  30. #include <common.h>
  31. #include <fuse.h>
  32. #include <asm/errno.h>
  33. #include <asm/io.h>
  34. #include <asm/arch/clock.h>
  35. #include <asm/arch/imx-regs.h>
  36. #define BO_CTRL_WR_UNLOCK 16
  37. #define BM_CTRL_WR_UNLOCK 0xffff0000
  38. #define BV_CTRL_WR_UNLOCK_KEY 0x3e77
  39. #define BM_CTRL_ERROR 0x00000200
  40. #define BM_CTRL_BUSY 0x00000100
  41. #define BO_CTRL_ADDR 0
  42. #define BM_CTRL_ADDR 0x0000007f
  43. #define BO_TIMING_STROBE_READ 16
  44. #define BM_TIMING_STROBE_READ 0x003f0000
  45. #define BV_TIMING_STROBE_READ_NS 37
  46. #define BO_TIMING_RELAX 12
  47. #define BM_TIMING_RELAX 0x0000f000
  48. #define BV_TIMING_RELAX_NS 17
  49. #define BO_TIMING_STROBE_PROG 0
  50. #define BM_TIMING_STROBE_PROG 0x00000fff
  51. #define BV_TIMING_STROBE_PROG_US 10
  52. #define BM_READ_CTRL_READ_FUSE 0x00000001
  53. #define BF(value, field) (((value) << BO_##field) & BM_##field)
  54. #define WRITE_POSTAMBLE_US 2
  55. static void wait_busy(struct ocotp_regs *regs, unsigned int delay_us)
  56. {
  57. while (readl(&regs->ctrl) & BM_CTRL_BUSY)
  58. udelay(delay_us);
  59. }
  60. static void clear_error(struct ocotp_regs *regs)
  61. {
  62. writel(BM_CTRL_ERROR, &regs->ctrl_clr);
  63. }
  64. static int prepare_access(struct ocotp_regs **regs, u32 bank, u32 word,
  65. int assert, const char *caller)
  66. {
  67. *regs = (struct ocotp_regs *)OCOTP_BASE_ADDR;
  68. if (bank >= ARRAY_SIZE((*regs)->bank) ||
  69. word >= ARRAY_SIZE((*regs)->bank[0].fuse_regs) >> 2 ||
  70. !assert) {
  71. printf("mxc_ocotp %s(): Invalid argument\n", caller);
  72. return -EINVAL;
  73. }
  74. enable_ocotp_clk(1);
  75. wait_busy(*regs, 1);
  76. clear_error(*regs);
  77. return 0;
  78. }
  79. static int finish_access(struct ocotp_regs *regs, const char *caller)
  80. {
  81. u32 err;
  82. err = !!(readl(&regs->ctrl) & BM_CTRL_ERROR);
  83. clear_error(regs);
  84. enable_ocotp_clk(0);
  85. if (err) {
  86. printf("mxc_ocotp %s(): Access protect error\n", caller);
  87. return -EIO;
  88. }
  89. return 0;
  90. }
  91. static int prepare_read(struct ocotp_regs **regs, u32 bank, u32 word, u32 *val,
  92. const char *caller)
  93. {
  94. return prepare_access(regs, bank, word, val != NULL, caller);
  95. }
  96. int fuse_read(u32 bank, u32 word, u32 *val)
  97. {
  98. struct ocotp_regs *regs;
  99. int ret;
  100. ret = prepare_read(&regs, bank, word, val, __func__);
  101. if (ret)
  102. return ret;
  103. *val = readl(&regs->bank[bank].fuse_regs[word << 2]);
  104. return finish_access(regs, __func__);
  105. }
  106. static void set_timing(struct ocotp_regs *regs)
  107. {
  108. u32 ipg_clk;
  109. u32 relax, strobe_read, strobe_prog;
  110. u32 timing;
  111. ipg_clk = mxc_get_clock(MXC_IPG_CLK);
  112. relax = DIV_ROUND_UP(ipg_clk * BV_TIMING_RELAX_NS, 1000000000) - 1;
  113. strobe_read = DIV_ROUND_UP(ipg_clk * BV_TIMING_STROBE_READ_NS,
  114. 1000000000) + 2 * (relax + 1) - 1;
  115. strobe_prog = DIV_ROUND(ipg_clk * BV_TIMING_STROBE_PROG_US, 1000000) +
  116. 2 * (relax + 1) - 1;
  117. timing = BF(strobe_read, TIMING_STROBE_READ) |
  118. BF(relax, TIMING_RELAX) |
  119. BF(strobe_prog, TIMING_STROBE_PROG);
  120. clrsetbits_le32(&regs->timing, BM_TIMING_STROBE_READ | BM_TIMING_RELAX |
  121. BM_TIMING_STROBE_PROG, timing);
  122. }
  123. static void setup_direct_access(struct ocotp_regs *regs, u32 bank, u32 word,
  124. int write)
  125. {
  126. u32 wr_unlock = write ? BV_CTRL_WR_UNLOCK_KEY : 0;
  127. u32 addr = bank << 3 | word;
  128. set_timing(regs);
  129. clrsetbits_le32(&regs->ctrl, BM_CTRL_WR_UNLOCK | BM_CTRL_ADDR,
  130. BF(wr_unlock, CTRL_WR_UNLOCK) |
  131. BF(addr, CTRL_ADDR));
  132. }
  133. int fuse_sense(u32 bank, u32 word, u32 *val)
  134. {
  135. struct ocotp_regs *regs;
  136. int ret;
  137. ret = prepare_read(&regs, bank, word, val, __func__);
  138. if (ret)
  139. return ret;
  140. setup_direct_access(regs, bank, word, false);
  141. writel(BM_READ_CTRL_READ_FUSE, &regs->read_ctrl);
  142. wait_busy(regs, 1);
  143. *val = readl(&regs->read_fuse_data);
  144. return finish_access(regs, __func__);
  145. }
  146. static int prepare_write(struct ocotp_regs **regs, u32 bank, u32 word,
  147. const char *caller)
  148. {
  149. return prepare_access(regs, bank, word, true, caller);
  150. }
  151. int fuse_prog(u32 bank, u32 word, u32 val)
  152. {
  153. struct ocotp_regs *regs;
  154. int ret;
  155. ret = prepare_write(&regs, bank, word, __func__);
  156. if (ret)
  157. return ret;
  158. setup_direct_access(regs, bank, word, true);
  159. writel(val, &regs->data);
  160. wait_busy(regs, BV_TIMING_STROBE_PROG_US);
  161. udelay(WRITE_POSTAMBLE_US);
  162. return finish_access(regs, __func__);
  163. }
  164. int fuse_override(u32 bank, u32 word, u32 val)
  165. {
  166. struct ocotp_regs *regs;
  167. int ret;
  168. ret = prepare_write(&regs, bank, word, __func__);
  169. if (ret)
  170. return ret;
  171. writel(val, &regs->bank[bank].fuse_regs[word << 2]);
  172. return finish_access(regs, __func__);
  173. }