Timo Tuunainen | ea8d989 | 2008-02-01 10:09:03 +0000 | [diff] [blame] | 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 | |
| 24 | #include <common.h> |
| 25 | #include <i2c.h> |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 26 | #ifdef CONFIG_SYS_EEPROM_AT24C16 |
Timo Tuunainen | ea8d989 | 2008-02-01 10:09:03 +0000 | [diff] [blame] | 27 | #undef DEBUG |
| 28 | |
| 29 | void eeprom_init(void) |
| 30 | { |
| 31 | #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C) |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 32 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
Timo Tuunainen | ea8d989 | 2008-02-01 10:09:03 +0000 | [diff] [blame] | 33 | #endif |
| 34 | } |
| 35 | |
| 36 | int eeprom_read(unsigned dev_addr, unsigned offset, uchar *buffer, |
| 37 | unsigned cnt) |
| 38 | { |
| 39 | int page, count = 0, i = 0; |
| 40 | page = offset / 0x100; |
| 41 | i = offset % 0x100; |
| 42 | |
| 43 | while (count < cnt) { |
| 44 | if (i2c_read(dev_addr|page, i++, 1, buffer+count++, 1) != 0) |
| 45 | return 1; |
| 46 | if (i > 0xff) { |
| 47 | page++; |
| 48 | i = 0; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | /* |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 56 | * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 2 (16-bit EEPROM address) offset is |
Timo Tuunainen | ea8d989 | 2008-02-01 10:09:03 +0000 | [diff] [blame] | 57 | * 0x000nxxxx for EEPROM address selectors at n, offset xxxx in EEPROM. |
| 58 | * |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 59 | * for CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 (8-bit EEPROM page address) offset is |
Timo Tuunainen | ea8d989 | 2008-02-01 10:09:03 +0000 | [diff] [blame] | 60 | * 0x00000nxx for EEPROM address selectors and page number at n. |
| 61 | */ |
| 62 | int eeprom_write(unsigned dev_addr, unsigned offset, uchar *buffer, |
| 63 | unsigned cnt) |
| 64 | { |
| 65 | int page, i = 0, count = 0; |
| 66 | |
| 67 | page = offset / 0x100; |
| 68 | i = offset % 0x100; |
| 69 | |
| 70 | while (count < cnt) { |
| 71 | if (i2c_write(dev_addr|page, i++, 1, buffer+count++, 1) != 0) |
| 72 | return 1; |
| 73 | if (i > 0xff) { |
| 74 | page++; |
| 75 | i = 0; |
| 76 | } |
| 77 | } |
| 78 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 79 | #if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS) |
| 80 | udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000); |
Timo Tuunainen | ea8d989 | 2008-02-01 10:09:03 +0000 | [diff] [blame] | 81 | #endif |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | #ifndef CONFIG_SPI |
| 87 | int eeprom_probe(unsigned dev_addr, unsigned offset) |
| 88 | { |
| 89 | unsigned char chip; |
| 90 | |
| 91 | /* Probe the chip address */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 92 | #if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X) |
Timo Tuunainen | ea8d989 | 2008-02-01 10:09:03 +0000 | [diff] [blame] | 93 | chip = offset >> 8; /* block number */ |
| 94 | #else |
| 95 | chip = offset >> 16; /* block number */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 96 | #endif /* CONFIG_SYS_I2C_EEPROM_ADDR_LEN, CONFIG_SPI_X */ |
Timo Tuunainen | ea8d989 | 2008-02-01 10:09:03 +0000 | [diff] [blame] | 97 | |
| 98 | chip |= dev_addr; /* insert device address */ |
| 99 | return (i2c_probe(chip)); |
| 100 | } |
| 101 | #endif |
| 102 | #endif |