eeprom_m95xxx.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C) 2009
  3. * Albin Tonnerre, Free Electrons <albin.tonnerre@free-electrons.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <spi.h>
  25. #define SPI_EEPROM_WREN 0x06
  26. #define SPI_EEPROM_RDSR 0x05
  27. #define SPI_EEPROM_READ 0x03
  28. #define SPI_EEPROM_WRITE 0x02
  29. #ifndef CONFIG_DEFAULT_SPI_BUS
  30. #define CONFIG_DEFAULT_SPI_BUS 0
  31. #endif
  32. #ifndef CONFIG_DEFAULT_SPI_MODE
  33. #define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0
  34. #endif
  35. #ifndef CONFIG_SYS_SPI_WRITE_TOUT
  36. #define CONFIG_SYS_SPI_WRITE_TOUT (5 * CONFIG_SYS_HZ)
  37. #endif
  38. ssize_t spi_read(uchar *addr, int alen, uchar *buffer, int len)
  39. {
  40. struct spi_slave *slave;
  41. u8 cmd = SPI_EEPROM_READ;
  42. slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, 1, 1000000,
  43. CONFIG_DEFAULT_SPI_MODE);
  44. if (!slave)
  45. return 0;
  46. spi_claim_bus(slave);
  47. /* command */
  48. if (spi_xfer(slave, 8, &cmd, NULL, SPI_XFER_BEGIN))
  49. return -1;
  50. /*
  51. * if alen == 3, addr[0] is the block number, we never use it here.
  52. * All we need are the lower 16 bits.
  53. */
  54. if (alen == 3)
  55. addr++;
  56. /* address, and data */
  57. if (spi_xfer(slave, 16, addr, NULL, 0))
  58. return -1;
  59. if (spi_xfer(slave, 8 * len, NULL, buffer, SPI_XFER_END))
  60. return -1;
  61. spi_release_bus(slave);
  62. spi_free_slave(slave);
  63. return len;
  64. }
  65. ssize_t spi_write(uchar *addr, int alen, uchar *buffer, int len)
  66. {
  67. struct spi_slave *slave;
  68. char buf[3];
  69. ulong start;
  70. slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, 1, 1000000,
  71. CONFIG_DEFAULT_SPI_MODE);
  72. if (!slave)
  73. return 0;
  74. spi_claim_bus(slave);
  75. buf[0] = SPI_EEPROM_WREN;
  76. if (spi_xfer(slave, 8, buf, NULL, SPI_XFER_BEGIN | SPI_XFER_END))
  77. return -1;
  78. buf[0] = SPI_EEPROM_WRITE;
  79. /* As for reading, drop addr[0] if alen is 3 */
  80. if (alen == 3) {
  81. alen--;
  82. addr++;
  83. }
  84. memcpy(buf + 1, addr, alen);
  85. /* command + addr, then data */
  86. if (spi_xfer(slave, 24, buf, NULL, SPI_XFER_BEGIN))
  87. return -1;
  88. if (spi_xfer(slave, len * 8, buffer, NULL, SPI_XFER_END))
  89. return -1;
  90. start = get_timer(0);
  91. do {
  92. buf[0] = SPI_EEPROM_RDSR;
  93. buf[1] = 0;
  94. spi_xfer(slave, 16, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END);
  95. if (!(buf[1] & 1))
  96. break;
  97. } while (get_timer(start) < CONFIG_SYS_SPI_WRITE_TOUT);
  98. if (buf[1] & 1)
  99. printf("*** spi_write: Timeout while writing!\n");
  100. spi_release_bus(slave);
  101. spi_free_slave(slave);
  102. return len;
  103. }