otp.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * cmd_otp.c - interface to Blackfin on-chip One-Time-Programmable memory
  3. *
  4. * Copyright (c) 2007-2008 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. /* There are 512 128-bit "pages" (0x000 through 0x1FF).
  9. * The pages are accessable as 64-bit "halfpages" (an upper and lower half).
  10. * The pages are not part of the memory map. There is an OTP controller which
  11. * handles scanning in/out of bits. While access is done through OTP MMRs,
  12. * the bootrom provides C-callable helper functions to handle the interaction.
  13. */
  14. #include <config.h>
  15. #include <common.h>
  16. #include <command.h>
  17. #include <console.h>
  18. #include <asm/blackfin.h>
  19. #include <asm/clock.h>
  20. #include <asm/mach-common/bits/otp.h>
  21. static const char *otp_strerror(uint32_t err)
  22. {
  23. switch (err) {
  24. case 0: return "no error";
  25. case OTP_WRITE_ERROR: return "OTP fuse write error";
  26. case OTP_READ_ERROR: return "OTP fuse read error";
  27. case OTP_ACC_VIO_ERROR: return "invalid OTP address";
  28. case OTP_DATA_MULT_ERROR: return "multiple bad bits detected";
  29. case OTP_ECC_MULT_ERROR: return "error in ECC bits";
  30. case OTP_PREV_WR_ERROR: return "space already written";
  31. case OTP_DATA_SB_WARN: return "single bad bit in half page";
  32. case OTP_ECC_SB_WARN: return "single bad bit in ECC";
  33. default: return "unknown error";
  34. }
  35. }
  36. #define lowup(x) ((x) % 2 ? "upper" : "lower")
  37. static int check_voltage(void)
  38. {
  39. /* Make sure voltage limits are within datasheet spec */
  40. uint16_t vr_ctl = bfin_read_VR_CTL();
  41. #ifdef __ADSPBF54x__
  42. /* 0.9V <= VDDINT <= 1.1V */
  43. if ((vr_ctl & 0xc) && (vr_ctl & 0xc0) == 0xc0)
  44. return 1;
  45. #else
  46. /* for the parts w/out qualification yet */
  47. (void)vr_ctl;
  48. #endif
  49. return 0;
  50. }
  51. static void set_otp_timing(bool write)
  52. {
  53. static uint32_t timing;
  54. if (!timing) {
  55. uint32_t tp1, tp2, tp3;
  56. /* OTP_TP1 = 1000 / sclk_period (in nanoseconds)
  57. * OTP_TP1 = 1000 / (1 / get_sclk() * 10^9)
  58. * OTP_TP1 = (1000 * get_sclk()) / 10^9
  59. * OTP_TP1 = get_sclk() / 10^6
  60. */
  61. tp1 = get_sclk() / 1000000;
  62. /* OTP_TP2 = 400 / (2 * sclk_period)
  63. * OTP_TP2 = 400 / (2 * 1 / get_sclk() * 10^9)
  64. * OTP_TP2 = (400 * get_sclk()) / (2 * 10^9)
  65. * OTP_TP2 = (2 * get_sclk()) / 10^7
  66. */
  67. tp2 = (2 * get_sclk() / 10000000) << 8;
  68. /* OTP_TP3 = magic constant */
  69. tp3 = (0x1401) << 15;
  70. timing = tp1 | tp2 | tp3;
  71. }
  72. bfrom_OtpCommand(OTP_INIT, write ? timing : timing & ~(-1 << 15));
  73. }
  74. int do_otp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  75. {
  76. char *cmd;
  77. uint32_t ret, base_flags;
  78. bool prompt_user, force_read;
  79. uint32_t (*otp_func)(uint32_t page, uint32_t flags, uint64_t *page_content);
  80. if (argc < 4) {
  81. usage:
  82. return CMD_RET_USAGE;
  83. }
  84. prompt_user = false;
  85. base_flags = 0;
  86. cmd = argv[1];
  87. if (!strcmp(cmd, "read"))
  88. otp_func = bfrom_OtpRead;
  89. else if (!strcmp(cmd, "dump")) {
  90. otp_func = bfrom_OtpRead;
  91. force_read = true;
  92. } else if (!strcmp(cmd, "write")) {
  93. otp_func = bfrom_OtpWrite;
  94. base_flags = OTP_CHECK_FOR_PREV_WRITE;
  95. if (!strcmp(argv[2], "--force")) {
  96. argv++;
  97. --argc;
  98. } else
  99. prompt_user = false;
  100. } else if (!strcmp(cmd, "lock")) {
  101. if (argc != 4)
  102. goto usage;
  103. otp_func = bfrom_OtpWrite;
  104. base_flags = OTP_LOCK;
  105. } else
  106. goto usage;
  107. uint64_t *addr = (uint64_t *)simple_strtoul(argv[2], NULL, 16);
  108. uint32_t page = simple_strtoul(argv[3], NULL, 16);
  109. uint32_t flags;
  110. size_t i, count;
  111. ulong half;
  112. if (argc > 4)
  113. count = simple_strtoul(argv[4], NULL, 16);
  114. else
  115. count = 2;
  116. if (argc > 5) {
  117. half = simple_strtoul(argv[5], NULL, 16);
  118. if (half != 0 && half != 1) {
  119. puts("Error: 'half' can only be '0' or '1'\n");
  120. goto usage;
  121. }
  122. } else
  123. half = 0;
  124. /* "otp lock" has slightly different semantics */
  125. if (base_flags & OTP_LOCK) {
  126. count = page;
  127. page = (uint32_t)addr;
  128. addr = NULL;
  129. }
  130. /* do to the nature of OTP, make sure users are sure */
  131. if (prompt_user) {
  132. printf(
  133. "Writing one time programmable memory\n"
  134. "Make sure your operating voltages and temperature are within spec\n"
  135. " source address: 0x%p\n"
  136. " OTP destination: %s page 0x%03X - %s page 0x%03lX\n"
  137. " number to write: %lu halfpages\n"
  138. " type \"YES\" (no quotes) to confirm: ",
  139. addr,
  140. lowup(half), page,
  141. lowup(half + count - 1), page + (half + count - 1) / 2,
  142. half + count
  143. );
  144. if (!confirm_yesno()) {
  145. printf(" Aborting\n");
  146. return 1;
  147. }
  148. }
  149. printf("OTP memory %s: addr 0x%p page 0x%03X count %zu ... ",
  150. cmd, addr, page, count);
  151. set_otp_timing(otp_func == bfrom_OtpWrite);
  152. if (otp_func == bfrom_OtpWrite && check_voltage()) {
  153. puts("ERROR: VDDINT voltage is out of spec for writing\n");
  154. return -1;
  155. }
  156. /* Do the actual reading/writing stuff */
  157. ret = 0;
  158. for (i = half; i < count + half; ++i) {
  159. flags = base_flags | (i % 2 ? OTP_UPPER_HALF : OTP_LOWER_HALF);
  160. try_again:
  161. ret = otp_func(page, flags, addr);
  162. if (ret & OTP_MASTER_ERROR) {
  163. if (force_read) {
  164. if (flags & OTP_NO_ECC)
  165. break;
  166. else
  167. flags |= OTP_NO_ECC;
  168. puts("E");
  169. goto try_again;
  170. } else
  171. break;
  172. } else if (ret)
  173. puts("W");
  174. else
  175. puts(".");
  176. if (!(base_flags & OTP_LOCK)) {
  177. ++addr;
  178. if (i % 2)
  179. ++page;
  180. } else
  181. ++page;
  182. }
  183. if (ret & 0x1)
  184. printf("\nERROR at page 0x%03X (%s-halfpage): 0x%03X: %s\n",
  185. page, lowup(i), ret, otp_strerror(ret));
  186. else
  187. puts(" done\n");
  188. /* Make sure we disable writing */
  189. set_otp_timing(false);
  190. bfrom_OtpCommand(OTP_CLOSE, 0);
  191. return ret;
  192. }
  193. U_BOOT_CMD(
  194. otp, 7, 0, do_otp,
  195. "One-Time-Programmable sub-system",
  196. "read <addr> <page> [count] [half]\n"
  197. " - read 'count' half-pages starting at 'page' (offset 'half') to 'addr'\n"
  198. "otp dump <addr> <page> [count] [half]\n"
  199. " - like 'otp read', but skip read errors\n"
  200. "otp write [--force] <addr> <page> [count] [half]\n"
  201. " - write 'count' half-pages starting at 'page' (offset 'half') from 'addr'\n"
  202. "otp lock <page> <count>\n"
  203. " - lock 'count' pages starting at 'page'"
  204. );