cmd_fuse.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * (C) Copyright 2009-2013 ADVANSEE
  3. * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
  4. *
  5. * Based on the mpc512x iim code:
  6. * Copyright 2008 Silicon Turnkey Express, Inc.
  7. * Martha Marx <mmarx@silicontkx.com>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <command.h>
  29. #include <fuse.h>
  30. #include <asm/errno.h>
  31. static int strtou32(const char *str, unsigned int base, u32 *result)
  32. {
  33. char *ep;
  34. *result = simple_strtoul(str, &ep, base);
  35. if (ep == str || *ep != '\0')
  36. return -EINVAL;
  37. return 0;
  38. }
  39. static int confirm_prog(void)
  40. {
  41. puts("Warning: Programming fuses is an irreversible operation!\n"
  42. " This may brick your system.\n"
  43. " Use this command only if you are sure of "
  44. "what you are doing!\n"
  45. "\nReally perform this fuse programming? <y/N>\n");
  46. if (getc() == 'y') {
  47. int c;
  48. putc('y');
  49. c = getc();
  50. putc('\n');
  51. if (c == '\r')
  52. return 1;
  53. }
  54. puts("Fuse programming aborted\n");
  55. return 0;
  56. }
  57. static int do_fuse(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  58. {
  59. const char *op = argc >= 2 ? argv[1] : NULL;
  60. int confirmed = argc >= 3 && !strcmp(argv[2], "-y");
  61. u32 bank, word, cnt, val;
  62. int ret, i;
  63. argc -= 2 + confirmed;
  64. argv += 2 + confirmed;
  65. if (argc < 2 || strtou32(argv[0], 0, &bank) ||
  66. strtou32(argv[1], 0, &word))
  67. return CMD_RET_USAGE;
  68. if (!strcmp(op, "read")) {
  69. if (argc == 2)
  70. cnt = 1;
  71. else if (argc != 3 || strtou32(argv[2], 0, &cnt))
  72. return CMD_RET_USAGE;
  73. printf("Reading bank %u:\n", bank);
  74. for (i = 0; i < cnt; i++, word++) {
  75. if (!(i % 4))
  76. printf("\nWord 0x%.8x:", word);
  77. ret = fuse_read(bank, word, &val);
  78. if (ret)
  79. goto err;
  80. printf(" %.8x", val);
  81. }
  82. putc('\n');
  83. } else if (!strcmp(op, "sense")) {
  84. if (argc == 2)
  85. cnt = 1;
  86. else if (argc != 3 || strtou32(argv[2], 0, &cnt))
  87. return CMD_RET_USAGE;
  88. printf("Sensing bank %u:\n", bank);
  89. for (i = 0; i < cnt; i++, word++) {
  90. if (!(i % 4))
  91. printf("\nWord 0x%.8x:", word);
  92. ret = fuse_sense(bank, word, &val);
  93. if (ret)
  94. goto err;
  95. printf(" %.8x", val);
  96. }
  97. putc('\n');
  98. } else if (!strcmp(op, "prog")) {
  99. if (argc < 3)
  100. return CMD_RET_USAGE;
  101. for (i = 2; i < argc; i++, word++) {
  102. if (strtou32(argv[i], 16, &val))
  103. return CMD_RET_USAGE;
  104. printf("Programming bank %u word 0x%.8x to 0x%.8x...\n",
  105. bank, word, val);
  106. if (!confirmed && !confirm_prog())
  107. return CMD_RET_FAILURE;
  108. ret = fuse_prog(bank, word, val);
  109. if (ret)
  110. goto err;
  111. }
  112. } else if (!strcmp(op, "override")) {
  113. if (argc < 3)
  114. return CMD_RET_USAGE;
  115. for (i = 2; i < argc; i++, word++) {
  116. if (strtou32(argv[i], 16, &val))
  117. return CMD_RET_USAGE;
  118. printf("Overriding bank %u word 0x%.8x with "
  119. "0x%.8x...\n", bank, word, val);
  120. ret = fuse_override(bank, word, val);
  121. if (ret)
  122. goto err;
  123. }
  124. } else {
  125. return CMD_RET_USAGE;
  126. }
  127. return 0;
  128. err:
  129. puts("ERROR\n");
  130. return ret;
  131. }
  132. U_BOOT_CMD(
  133. fuse, CONFIG_SYS_MAXARGS, 0, do_fuse,
  134. "Fuse sub-system",
  135. "read <bank> <word> [<cnt>] - read 1 or 'cnt' fuse words,\n"
  136. " starting at 'word'\n"
  137. "fuse sense <bank> <word> [<cnt>] - sense 1 or 'cnt' fuse words,\n"
  138. " starting at 'word'\n"
  139. "fuse prog [-y] <bank> <word> <hexval> [<hexval>...] - program 1 or\n"
  140. " several fuse words, starting at 'word' (PERMANENT)\n"
  141. "fuse override <bank> <word> <hexval> [<hexval>...] - override 1 or\n"
  142. " several fuse words, starting at 'word'"
  143. );