1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <common.h>
26#include <command.h>
27#include <asm/io.h>
28
29#define EEPROM_CAP 0x50000358
30#define EEPROM_DATA 0x5000035c
31
32
33unsigned int eepromReadLong(int offs)
34{
35 unsigned int value;
36 unsigned short ret;
37 int count;
38
39 out_be16((void *)EEPROM_CAP, offs);
40
41 count = 0;
42
43 for (;;)
44 {
45 count++;
46 ret = in_be16((void *)EEPROM_CAP);
47
48 if ((ret & 0x8000) != 0)
49 break;
50 }
51
52 value = in_be32((void *)EEPROM_DATA);
53
54 return value;
55}
56
57
58unsigned char eepromReadByte(int offs)
59{
60 unsigned int valueLong;
61 unsigned char *ptr;
62
63 valueLong = eepromReadLong(offs & ~3);
64 ptr = (unsigned char *)&valueLong;
65
66 return ptr[offs & 3];
67}
68
69
70void eepromWriteLong(int offs, unsigned int value)
71{
72 unsigned short ret;
73 int count;
74
75 count = 0;
76
77 out_be32((void *)EEPROM_DATA, value);
78 out_be16((void *)EEPROM_CAP, 0x8000 + offs);
79
80 for (;;)
81 {
82 count++;
83 ret = in_be16((void *)EEPROM_CAP);
84
85 if ((ret & 0x8000) == 0)
86 break;
87 }
88}
89
90
91void eepromWriteByte(int offs, unsigned char valueByte)
92{
93 unsigned int valueLong;
94 unsigned char *ptr;
95
96 valueLong = eepromReadLong(offs & ~3);
97 ptr = (unsigned char *)&valueLong;
98
99 ptr[offs & 3] = valueByte;
100
101 eepromWriteLong(offs & ~3, valueLong);
102}
103
104
105void i2c_read (uchar *addr, int alen, uchar *buffer, int len)
106{
107 int i;
108 int len2, ptr;
109
110
111
112 ptr = *(short *)addr;
113
114
115
116
117 len2 = 4 - (*(short *)addr & 0x0003);
118 for (i=0; i<len2; i++)
119 {
120 *buffer++ = eepromReadByte(ptr++);
121 }
122
123
124
125
126 len2 = (len - len2) >> 2;
127 for (i=0; i<len2; i++)
128 {
129 *(unsigned int *)buffer = eepromReadLong(ptr);
130 buffer += 4;
131 ptr += 4;
132 }
133
134
135
136
137 len2 = (*(short *)addr + len) & 0x0003;
138 for (i=0; i<len2; i++)
139 {
140 *buffer++ = eepromReadByte(ptr++);
141 }
142}
143
144void i2c_write (uchar *addr, int alen, uchar *buffer, int len)
145{
146 int i;
147 int len2, ptr;
148
149
150
151 ptr = *(short *)addr;
152
153
154
155
156 len2 = 4 - (*(short *)addr & 0x0003);
157 for (i=0; i<len2; i++)
158 {
159 eepromWriteByte(ptr++, *buffer++);
160 }
161
162
163
164
165 len2 = (len - len2) >> 2;
166 for (i=0; i<len2; i++)
167 {
168 eepromWriteLong(ptr, *(unsigned int *)buffer);
169 buffer += 4;
170 ptr += 4;
171 }
172
173
174
175
176 len2 = (*(short *)addr + len) & 0x0003;
177 for (i=0; i<len2; i++)
178 {
179 eepromWriteByte(ptr++, *buffer++);
180 }
181}
182