eeprom_m95xxx.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. ssize_t spi_read(uchar *addr, int alen, uchar *buffer, int len)
  36. {
  37. struct spi_slave *slave;
  38. u8 cmd = SPI_EEPROM_READ;
  39. slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, 1, 1000000,
  40. CONFIG_DEFAULT_SPI_MODE);
  41. if (!slave)
  42. return 0;
  43. spi_claim_bus(slave);
  44. /* command */
  45. if (spi_xfer(slave, 8, &cmd, NULL, SPI_XFER_BEGIN))
  46. return -1;
  47. /*
  48. * if alen == 3, addr[0] is the block number, we never use it here.
  49. * All we need are the lower 16 bits.
  50. */
  51. if (alen == 3)
  52. addr++;
  53. /* address, and data */
  54. if (spi_xfer(slave, 16, addr, NULL, 0))
  55. return -1;
  56. if (spi_xfer(slave, 8 * len, NULL, buffer, SPI_XFER_END))
  57. return -1;
  58. spi_release_bus(slave);
  59. spi_free_slave(slave);
  60. return len;
  61. }
  62. ssize_t spi_write(uchar *addr, int alen, uchar *buffer, int len)
  63. {
  64. struct spi_slave *slave;
  65. char buf[3];
  66. ulong start;
  67. slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, 1, 1000000,
  68. CONFIG_DEFAULT_SPI_MODE);
  69. if (!slave)
  70. return 0;
  71. spi_claim_bus(slave);
  72. buf[0] = SPI_EEPROM_WREN;
  73. if (spi_xfer(slave, 8, buf, NULL, SPI_XFER_BEGIN | SPI_XFER_END))
  74. return -1;
  75. buf[0] = SPI_EEPROM_WRITE;
  76. /* As for reading, drop addr[0] if alen is 3 */
  77. if (alen == 3) {
  78. alen--;
  79. addr++;
  80. }
  81. memcpy(buf + 1, addr, alen);
  82. /* command + addr, then data */
  83. if (spi_xfer(slave, 24, buf, NULL, SPI_XFER_BEGIN))
  84. return -1;
  85. if (spi_xfer(slave, len * 8, buffer, NULL, SPI_XFER_END))
  86. return -1;
  87. start = get_timer(0);
  88. do {
  89. buf[0] = SPI_EEPROM_RDSR;
  90. buf[1] = 0;
  91. spi_xfer(slave, 16, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END);
  92. if (!(buf[1] & 1))
  93. break;
  94. } while (get_timer(start) < CONFIG_SYS_SPI_WRITE_TOUT);
  95. if (buf[1] & 1)
  96. printf("*** spi_write: Timeout while writing!\n");
  97. spi_release_bus(slave);
  98. spi_free_slave(slave);
  99. return len;
  100. }