atmel_df_pow2.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #define CMD_ID 0x9f
  11. #define CMD_STAT 0xd7
  12. #define CMD_CFG 0x3d
  13. static int flash_cmd(struct spi_slave *slave, uchar cmd, uchar *buf, int len)
  14. {
  15. buf[0] = cmd;
  16. return spi_xfer(slave, 8 * len, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END);
  17. }
  18. static int flash_status(struct spi_slave *slave)
  19. {
  20. uchar buf[2];
  21. if (flash_cmd(slave, CMD_STAT, buf, sizeof(buf)))
  22. return -1;
  23. return buf[1];
  24. }
  25. static int flash_set_pow2(struct spi_slave *slave)
  26. {
  27. int ret;
  28. uchar buf[4];
  29. buf[1] = 0x2a;
  30. buf[2] = 0x80;
  31. buf[3] = 0xa6;
  32. ret = flash_cmd(slave, CMD_CFG, buf, sizeof(buf));
  33. if (ret)
  34. return ret;
  35. /* wait Tp, or 6 msec */
  36. udelay(6000);
  37. ret = flash_status(slave);
  38. if (ret == -1)
  39. return 1;
  40. return ret & 0x1 ? 0 : 1;
  41. }
  42. static int flash_check(struct spi_slave *slave)
  43. {
  44. int ret;
  45. uchar buf[4];
  46. ret = flash_cmd(slave, CMD_ID, buf, sizeof(buf));
  47. if (ret)
  48. return ret;
  49. if (buf[1] != 0x1F) {
  50. printf("atmel flash not found (id[0] = %#x)\n", buf[1]);
  51. return 1;
  52. }
  53. if ((buf[2] >> 5) != 0x1) {
  54. printf("AT45 flash not found (id[0] = %#x)\n", buf[2]);
  55. return 2;
  56. }
  57. return 0;
  58. }
  59. static char *getline(void)
  60. {
  61. static char buffer[100];
  62. char c;
  63. size_t i;
  64. i = 0;
  65. while (1) {
  66. buffer[i] = '\0';
  67. c = getc();
  68. switch (c) {
  69. case '\r': /* Enter/Return key */
  70. case '\n':
  71. puts("\n");
  72. return buffer;
  73. case 0x03: /* ^C - break */
  74. return NULL;
  75. case 0x5F:
  76. case 0x08: /* ^H - backspace */
  77. case 0x7F: /* DEL - backspace */
  78. if (i) {
  79. puts("\b \b");
  80. i--;
  81. }
  82. break;
  83. default:
  84. /* Ignore control characters */
  85. if (c < 0x20)
  86. break;
  87. /* Queue up all other characters */
  88. buffer[i++] = c;
  89. printf("%c", c);
  90. break;
  91. }
  92. }
  93. }
  94. int atmel_df_pow2(int argc, char * const argv[])
  95. {
  96. /* Print the ABI version */
  97. app_startup(argv);
  98. if (XF_VERSION != get_version()) {
  99. printf("Expects ABI version %d\n", XF_VERSION);
  100. printf("Actual U-Boot ABI version %lu\n", get_version());
  101. printf("Can't run\n\n");
  102. return 1;
  103. }
  104. spi_init();
  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. }