cmd_otp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 to 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. #ifdef CONFIG_CMD_OTP
  18. #include <asm/blackfin.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. int do_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  37. {
  38. bool force = false;
  39. if (!strcmp(argv[1], "--force")) {
  40. force = true;
  41. argv[1] = argv[0];
  42. argv++;
  43. --argc;
  44. }
  45. uint32_t (*otp_func)(uint32_t page, uint32_t flags, uint64_t *page_content);
  46. if (!strcmp(argv[1], "read"))
  47. otp_func = otp_read;
  48. else if (!strcmp(argv[1], "write"))
  49. otp_func = otp_write;
  50. else {
  51. usage:
  52. cmd_usage(cmdtp);
  53. return 1;
  54. }
  55. uint64_t *addr = (uint64_t *)simple_strtoul(argv[2], NULL, 16);
  56. uint32_t page = simple_strtoul(argv[3], NULL, 16);
  57. uint32_t flags, ret;
  58. size_t i, count;
  59. ulong half;
  60. if (argc > 4)
  61. count = simple_strtoul(argv[4], NULL, 16);
  62. else
  63. count = 2;
  64. if (argc > 5) {
  65. half = simple_strtoul(argv[5], NULL, 16);
  66. if (half != 0 && half != 1) {
  67. puts("Error: 'half' can only be '0' or '1'\n");
  68. goto usage;
  69. }
  70. } else
  71. half = 0;
  72. /* do to the nature of OTP, make sure users are sure */
  73. if (!force && otp_func == otp_write) {
  74. printf(
  75. "Writing one time programmable memory\n"
  76. "Make sure your operating voltages and temperature are within spec\n"
  77. " source address: 0x%p\n"
  78. " OTP destination: %s page 0x%03X - %s page 0x%03X\n"
  79. " number to write: %ld halfpages\n"
  80. " type \"YES\" (no quotes) to confirm: ",
  81. addr,
  82. lowup(half), page,
  83. lowup(half + count - 1), page + (half + count - 1) / 2,
  84. half + count
  85. );
  86. i = 0;
  87. while (1) {
  88. if (tstc()) {
  89. const char exp_ans[] = "YES\r";
  90. char c;
  91. putc(c = getc());
  92. if (exp_ans[i++] != c) {
  93. printf(" Aborting\n");
  94. return 1;
  95. } else if (!exp_ans[i]) {
  96. puts("\n");
  97. break;
  98. }
  99. }
  100. }
  101. /* Only supported in newer silicon ... enable writing */
  102. #if (0)
  103. otp_command(OTP_INIT, ...);
  104. #else
  105. *pOTP_TIMING = 0x32149485;
  106. #endif
  107. }
  108. printf("OTP memory %s: addr 0x%08lx page 0x%03X count %ld ... ",
  109. argv[1], addr, page, count);
  110. ret = 0;
  111. for (i = half; i < count + half; ++i) {
  112. flags = (i % 2) ? OTP_UPPER_HALF : OTP_LOWER_HALF;
  113. ret = otp_func(page, flags, addr);
  114. if (ret & 0x1)
  115. break;
  116. else if (ret)
  117. puts("W");
  118. else
  119. puts(".");
  120. ++addr;
  121. if (i % 2)
  122. ++page;
  123. }
  124. if (ret & 0x1)
  125. printf("\nERROR at page 0x%03X (%s-halfpage): 0x%03X: %s\n",
  126. page, lowup(i), ret, otp_strerror(ret));
  127. else
  128. puts(" done\n");
  129. if (otp_func == otp_write)
  130. /* Only supported in newer silicon ... disable writing */
  131. #if (0)
  132. otp_command(OTP_INIT, ...);
  133. #else
  134. *pOTP_TIMING = 0x1485;
  135. #endif
  136. return ret;
  137. }
  138. U_BOOT_CMD(otp, 6, 0, do_otp,
  139. "otp - One-Time-Programmable sub-system\n",
  140. "read <addr> <page> [count] [half]\n"
  141. "otp write [--force] <addr> <page> [count] [half]\n"
  142. " - read/write 'count' half-pages starting at page 'page' (offset 'half')\n");
  143. #endif