eeprom.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Add by Alan Lu, 07-29-2005
  3. * For ATMEL AT24C16 EEPROM
  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 <i2c.h>
  25. #ifdef CONFIG_SYS_EEPROM_AT24C16
  26. #undef DEBUG
  27. void eeprom_init(void)
  28. {
  29. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
  30. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  31. #endif
  32. }
  33. int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer,
  34. unsigned cnt)
  35. {
  36. int page, count = 0, i = 0;
  37. page = offset / 0x100;
  38. i = offset % 0x100;
  39. while (count < cnt) {
  40. if (i2c_read(dev_addr|page, i++, 1, buffer+count++, 1) != 0)
  41. return 1;
  42. if (i > 0xff) {
  43. page++;
  44. i = 0;
  45. }
  46. }
  47. return 0;
  48. }
  49. /*
  50. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is
  51. * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM.
  52. *
  53. * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is
  54. * 0x00000nxx for EEPROM address selectors and page number at n.
  55. */
  56. int eeprom_write(unsigned dev_addr, unsigned offset, uchar *buffer,
  57. unsigned cnt)
  58. {
  59. int page, i = 0, count = 0;
  60. page = offset / 0x100;
  61. i = offset % 0x100;
  62. while (count < cnt) {
  63. if (i2c_write(dev_addr|page, i++, 1, buffer+count++, 1) != 0)
  64. return 1;
  65. if (i > 0xff) {
  66. page++;
  67. i = 0;
  68. }
  69. }
  70. #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
  71. udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
  72. #endif
  73. return 0;
  74. }
  75. #ifndef CONFIG_SPI
  76. int eeprom_probe(unsigned dev_addr, unsigned offset)
  77. {
  78. unsigned char chip;
  79. /* Probe the chip address */
  80. #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
  81. chip = offset >> 8; /* block number */
  82. #else
  83. chip = offset >> 16; /* block number */
  84. #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */
  85. chip |= dev_addr; /* insert device address */
  86. return (i2c_probe(chip));
  87. }
  88. #endif
  89. #endif