1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <i2c.h>
26#ifdef CONFIG_SYS_EEPROM_AT24C16
27#undef DEBUG
28
29void eeprom_init(void)
30{
31#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
32 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
33#endif
34}
35
36int 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
56
57
58
59
60
61
62int 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
79#if defined(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS)
80 udelay(CONFIG_SYS_EEPROM_PAGE_WRITE_DELAY_MS * 1000);
81#endif
82
83 return 0;
84}
85
86#ifndef CONFIG_SPI
87int eeprom_probe(unsigned dev_addr, unsigned offset)
88{
89 unsigned char chip;
90
91
92#if CONFIG_SYS_I2C_EEPROM_ADDR_LEN == 1 && !defined(CONFIG_SPI_X)
93 chip = offset >> 8;
94#else
95 chip = offset >> 16;
96#endif
97
98 chip |= dev_addr;
99 return (i2c_probe(chip));
100}
101#endif
102#endif
103