eeprom_m95xxx.c 2.8 KB

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