spi_flash.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * SPI flash interface
  3. *
  4. * Copyright (C) 2008 Atmel Corporation
  5. */
  6. #include <common.h>
  7. #include <malloc.h>
  8. #include <spi.h>
  9. #include <spi_flash.h>
  10. #include "spi_flash_internal.h"
  11. int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len)
  12. {
  13. unsigned long flags = SPI_XFER_BEGIN;
  14. int ret;
  15. if (len == 0)
  16. flags |= SPI_XFER_END;
  17. ret = spi_xfer(spi, 8, &cmd, NULL, flags);
  18. if (ret) {
  19. debug("SF: Failed to send command %02x: %d\n", cmd, ret);
  20. return ret;
  21. }
  22. if (len) {
  23. ret = spi_xfer(spi, len * 8, NULL, response, SPI_XFER_END);
  24. if (ret)
  25. debug("SF: Failed to read response (%zu bytes): %d\n",
  26. len, ret);
  27. }
  28. return ret;
  29. }
  30. int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd,
  31. size_t cmd_len, void *data, size_t data_len)
  32. {
  33. unsigned long flags = SPI_XFER_BEGIN;
  34. int ret;
  35. if (data_len == 0)
  36. flags |= SPI_XFER_END;
  37. ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
  38. if (ret) {
  39. debug("SF: Failed to send read command (%zu bytes): %d\n",
  40. cmd_len, ret);
  41. } else if (data_len != 0) {
  42. ret = spi_xfer(spi, data_len * 8, NULL, data, SPI_XFER_END);
  43. if (ret)
  44. debug("SF: Failed to read %zu bytes of data: %d\n",
  45. data_len, ret);
  46. }
  47. return ret;
  48. }
  49. int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len,
  50. const void *data, size_t data_len)
  51. {
  52. unsigned long flags = SPI_XFER_BEGIN;
  53. int ret;
  54. if (data_len == 0)
  55. flags |= SPI_XFER_END;
  56. ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
  57. if (ret) {
  58. debug("SF: Failed to send read command (%zu bytes): %d\n",
  59. cmd_len, ret);
  60. } else if (data_len != 0) {
  61. ret = spi_xfer(spi, data_len * 8, data, NULL, SPI_XFER_END);
  62. if (ret)
  63. debug("SF: Failed to read %zu bytes of data: %d\n",
  64. data_len, ret);
  65. }
  66. return ret;
  67. }
  68. int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
  69. size_t cmd_len, void *data, size_t data_len)
  70. {
  71. struct spi_slave *spi = flash->spi;
  72. int ret;
  73. spi_claim_bus(spi);
  74. ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
  75. spi_release_bus(spi);
  76. return ret;
  77. }
  78. struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
  79. unsigned int max_hz, unsigned int spi_mode)
  80. {
  81. struct spi_slave *spi;
  82. struct spi_flash *flash;
  83. int ret;
  84. u8 idcode[5];
  85. spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
  86. if (!spi) {
  87. debug("SF: Failed to set up slave\n");
  88. return NULL;
  89. }
  90. ret = spi_claim_bus(spi);
  91. if (ret) {
  92. debug("SF: Failed to claim SPI bus: %d\n", ret);
  93. goto err_claim_bus;
  94. }
  95. /* Read the ID codes */
  96. ret = spi_flash_cmd(spi, CMD_READ_ID, &idcode, sizeof(idcode));
  97. if (ret)
  98. goto err_read_id;
  99. debug("SF: Got idcode %02x %02x %02x %02x %02x\n", idcode[0],
  100. idcode[1], idcode[2], idcode[3], idcode[4]);
  101. switch (idcode[0]) {
  102. #ifdef CONFIG_SPI_FLASH_SPANSION
  103. case 0x01:
  104. flash = spi_flash_probe_spansion(spi, idcode);
  105. break;
  106. #endif
  107. #ifdef CONFIG_SPI_FLASH_ATMEL
  108. case 0x1F:
  109. flash = spi_flash_probe_atmel(spi, idcode);
  110. break;
  111. #endif
  112. #ifdef CONFIG_SPI_FLASH_STMICRO
  113. case 0x20:
  114. flash = spi_flash_probe_stmicro(spi, idcode);
  115. break;
  116. #endif
  117. #ifdef CONFIG_SPI_FLASH_SST
  118. case 0xBF:
  119. flash = spi_flash_probe_sst(spi, idcode);
  120. break;
  121. #endif
  122. default:
  123. debug("SF: Unsupported manufacturer %02X\n", idcode[0]);
  124. flash = NULL;
  125. break;
  126. }
  127. if (!flash)
  128. goto err_manufacturer_probe;
  129. spi_release_bus(spi);
  130. return flash;
  131. err_manufacturer_probe:
  132. err_read_id:
  133. spi_release_bus(spi);
  134. err_claim_bus:
  135. spi_free_slave(spi);
  136. return NULL;
  137. }
  138. void spi_flash_free(struct spi_flash *flash)
  139. {
  140. spi_free_slave(flash->spi);
  141. free(flash);
  142. }