spi_flash.c 3.3 KB

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