cmd_otp.c 5.8 KB

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