cmd_otp.c 5.9 KB

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