atmel_df_pow2.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * atmel_df_pow2.c - convert Atmel Dataflashes to Power of 2 mode
  3. *
  4. * Copyright 2009 Analog Devices Inc.
  5. *
  6. * Licensed under the 2-clause BSD.
  7. */
  8. #include <common.h>
  9. #include <exports.h>
  10. #include <spi.h>
  11. #define CMD_ID 0x9f
  12. #define CMD_STAT 0xd7
  13. #define CMD_CFG 0x3d
  14. static int flash_cmd(struct spi_slave *slave, uchar cmd, uchar *buf, int len)
  15. {
  16. buf[0] = cmd;
  17. return spi_xfer(slave, 8 * len, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END);
  18. }
  19. static int flash_status(struct spi_slave *slave)
  20. {
  21. uchar buf[2];
  22. if (flash_cmd(slave, CMD_STAT, buf, sizeof(buf)))
  23. return -1;
  24. return buf[1];
  25. }
  26. static int flash_set_pow2(struct spi_slave *slave)
  27. {
  28. int ret;
  29. uchar buf[4];
  30. buf[1] = 0x2a;
  31. buf[2] = 0x80;
  32. buf[3] = 0xa6;
  33. ret = flash_cmd(slave, CMD_CFG, buf, sizeof(buf));
  34. if (ret)
  35. return ret;
  36. /* wait Tp, or 6 msec */
  37. udelay(6000);
  38. ret = flash_status(slave);
  39. if (ret == -1)
  40. return 1;
  41. return ret & 0x1 ? 0 : 1;
  42. }
  43. static int flash_check(struct spi_slave *slave)
  44. {
  45. int ret;
  46. uchar buf[4];
  47. ret = flash_cmd(slave, CMD_ID, buf, sizeof(buf));
  48. if (ret)
  49. return ret;
  50. if (buf[1] != 0x1F) {
  51. printf("atmel flash not found (id[0] = %#x)\n", buf[1]);
  52. return 1;
  53. }
  54. if ((buf[2] >> 5) != 0x1) {
  55. printf("AT45 flash not found (id[0] = %#x)\n", buf[2]);
  56. return 2;
  57. }
  58. return 0;
  59. }
  60. static char *getline(void)
  61. {
  62. static char buffer[100];
  63. char c;
  64. size_t i;
  65. i = 0;
  66. while (1) {
  67. buffer[i] = '\0';
  68. c = getc();
  69. switch (c) {
  70. case '\r': /* Enter/Return key */
  71. case '\n':
  72. puts("\n");
  73. return buffer;
  74. case 0x03: /* ^C - break */
  75. return NULL;
  76. case 0x5F:
  77. case 0x08: /* ^H - backspace */
  78. case 0x7F: /* DEL - backspace */
  79. if (i) {
  80. puts("\b \b");
  81. i--;
  82. }
  83. break;
  84. default:
  85. /* Ignore control characters */
  86. if (c < 0x20)
  87. break;
  88. /* Queue up all other characters */
  89. buffer[i++] = c;
  90. printf("%c", c);
  91. break;
  92. }
  93. }
  94. }
  95. int atmel_df_pow2(int argc, char * const argv[])
  96. {
  97. /* Print the ABI version */
  98. app_startup(argv);
  99. if (XF_VERSION != get_version()) {
  100. printf("Expects ABI version %d\n", XF_VERSION);
  101. printf("Actual U-Boot ABI version %lu\n", get_version());
  102. printf("Can't run\n\n");
  103. return 1;
  104. }
  105. while (1) {
  106. struct spi_slave *slave;
  107. char *line, *p;
  108. int bus, cs, status;
  109. puts("\nenter the [BUS:]CS of the SPI flash: ");
  110. line = getline();
  111. /* CTRL+C */
  112. if (!line)
  113. return 0;
  114. if (line[0] == '\0')
  115. continue;
  116. bus = cs = simple_strtoul(line, &p, 10);
  117. if (*p) {
  118. if (*p == ':') {
  119. ++p;
  120. cs = simple_strtoul(p, &p, 10);
  121. }
  122. if (*p) {
  123. puts("invalid format, please try again\n");
  124. continue;
  125. }
  126. } else
  127. bus = 0;
  128. printf("\ngoing to work with dataflash at %i:%i\n", bus, cs);
  129. /* use a low speed -- it'll work with all devices, and
  130. * speed here doesn't really matter.
  131. */
  132. slave = spi_setup_slave(bus, cs, 1000, SPI_MODE_3);
  133. if (!slave) {
  134. puts("unable to setup slave\n");
  135. continue;
  136. }
  137. if (spi_claim_bus(slave)) {
  138. spi_free_slave(slave);
  139. continue;
  140. }
  141. if (flash_check(slave)) {
  142. puts("no flash found\n");
  143. goto done;
  144. }
  145. status = flash_status(slave);
  146. if (status == -1) {
  147. puts("unable to read status register\n");
  148. goto done;
  149. }
  150. if (status & 0x1) {
  151. puts("flash is already in power-of-2 mode!\n");
  152. goto done;
  153. }
  154. puts("are you sure you wish to set power-of-2 mode?\n");
  155. puts("this operation is permanent and irreversible\n");
  156. printf("enter YES to continue: ");
  157. line = getline();
  158. if (!line || strcmp(line, "YES"))
  159. goto done;
  160. if (flash_set_pow2(slave)) {
  161. puts("setting pow2 mode failed\n");
  162. goto done;
  163. }
  164. puts(
  165. "Configuration should be updated now. You will have to\n"
  166. "power cycle the part in order to finish the conversion.\n"
  167. );
  168. done:
  169. spi_release_bus(slave);
  170. spi_free_slave(slave);
  171. }
  172. }