atmel_df_pow2.c 3.7 KB

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