1
2
3
4
5
6
7
8#include <config.h>
9#include <common.h>
10
11#include <tsi108.h>
12
13#if defined(CONFIG_CMD_I2C)
14
15#define I2C_DELAY 100000
16#undef DEBUG_I2C
17
18#ifdef DEBUG_I2C
19#define DPRINT(x) printf (x)
20#else
21#define DPRINT(x)
22#endif
23
24
25
26
27void i2c_init(int speed, int slaveaddr)
28{
29
30
31
32
33
34}
35
36static int i2c_read_byte (
37 uint i2c_chan,
38 uchar chip_addr,
39 uint byte_addr,
40 uchar * buffer
41 )
42{
43 u32 temp;
44 u32 to_count = I2C_DELAY;
45 u32 op_status = TSI108_I2C_TIMEOUT_ERR;
46 u32 chan_offset = TSI108_I2C_OFFSET;
47
48 DPRINT (("I2C read_byte() %d 0x%02x 0x%02x\n",
49 i2c_chan, chip_addr, byte_addr));
50
51 if (0 != i2c_chan)
52 chan_offset = TSI108_I2C_SDRAM_OFFSET;
53
54
55 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
56
57 if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS |
58 I2C_CNTRL2_START))) {
59
60 temp = (byte_addr << 16) | ((chip_addr & 0x07) << 8) |
61 ((chip_addr >> 3) & 0x0F);
62 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL1) =
63 temp;
64
65
66
67
68
69
70 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2) =
71 (I2C_CNTRL2_START);
72
73
74 do {
75
76 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + chan_offset + I2C_CNTRL2);
77
78 if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_START))) {
79 if (0 == (temp &
80 (I2C_CNTRL2_I2C_CFGERR |
81 I2C_CNTRL2_I2C_TO_ERR))
82 ) {
83 op_status = TSI108_I2C_SUCCESS;
84
85 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE +
86 chan_offset +
87 I2C_RD_DATA);
88
89 *buffer = (u8) (temp & 0xFF);
90 } else {
91
92 op_status = TSI108_I2C_IF_ERROR;
93
94 DPRINT (("I2C HW error reported: 0x%02x\n", temp));
95 }
96
97 break;
98 }
99 } while (to_count--);
100 } else {
101 op_status = TSI108_I2C_IF_BUSY;
102
103 DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
104 }
105
106 DPRINT (("I2C read_byte() status: 0x%02x\n", op_status));
107 return op_status;
108}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127int i2c_read (uchar chip_addr, uint byte_addr, int alen,
128 uchar * buffer, int len)
129{
130 u32 op_status = TSI108_I2C_PARAM_ERR;
131 u32 i2c_if = 0;
132
133
134 if (0xD0 == (chip_addr & ~0x07)) {
135 i2c_if = 1;
136 chip_addr &= 0x7F;
137 }
138
139 if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
140 while (len--) {
141 op_status = i2c_read_byte(i2c_if, chip_addr, byte_addr++, buffer++);
142
143 if (TSI108_I2C_SUCCESS != op_status) {
144 DPRINT (("I2C read_byte() failed: 0x%02x (%d left)\n", op_status, len));
145
146 break;
147 }
148 }
149 }
150
151 DPRINT (("I2C read() status: 0x%02x\n", op_status));
152 return op_status;
153}
154
155
156
157static int i2c_write_byte (uchar chip_addr,
158 uint byte_addr,
159 uchar * buffer
160 )
161{
162 u32 temp;
163 u32 to_count = I2C_DELAY;
164 u32 op_status = TSI108_I2C_TIMEOUT_ERR;
165
166
167 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
168
169 if (0 == (temp & (I2C_CNTRL2_RD_STATUS | I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
170
171 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
172 I2C_TX_DATA) = (u32) * buffer;
173
174
175 temp =
176 I2C_CNTRL1_I2CWRITE | (byte_addr << 16) |
177 ((chip_addr & 0x07) << 8) | ((chip_addr >> 3) & 0x0F);
178 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
179 I2C_CNTRL1) = temp;
180
181
182
183
184
185 *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET +
186 I2C_CNTRL2) = (I2C_CNTRL2_START);
187
188 op_status = TSI108_I2C_TIMEOUT_ERR;
189
190
191 do {
192
193 temp = *(u32 *) (CONFIG_SYS_TSI108_CSR_BASE + TSI108_I2C_OFFSET + I2C_CNTRL2);
194
195 if (0 == (temp & (I2C_CNTRL2_WR_STATUS | I2C_CNTRL2_START))) {
196 if (0 == (temp &
197 (I2C_CNTRL2_I2C_CFGERR |
198 I2C_CNTRL2_I2C_TO_ERR))) {
199 op_status = TSI108_I2C_SUCCESS;
200 } else {
201
202 op_status = TSI108_I2C_IF_ERROR;
203
204 DPRINT (("I2C HW error reported: 0x%02x\n", temp));
205 }
206
207 break;
208 }
209
210 } while (to_count--);
211 } else {
212 op_status = TSI108_I2C_IF_BUSY;
213
214 DPRINT (("I2C Transaction start failed: 0x%02x\n", temp));
215 }
216
217 return op_status;
218}
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233int i2c_write (uchar chip_addr, uint byte_addr, int alen, uchar * buffer,
234 int len)
235{
236 u32 op_status = TSI108_I2C_PARAM_ERR;
237
238
239 if (chip_addr <= 0x7F && (byte_addr + len) <= (0x01 << (alen * 8))) {
240 while (len--) {
241 op_status =
242 i2c_write_byte (chip_addr, byte_addr++, buffer++);
243
244 if (TSI108_I2C_SUCCESS != op_status) {
245 DPRINT (("I2C write_byte() failed: 0x%02x (%d left)\n", op_status, len));
246
247 break;
248 }
249 }
250 }
251
252 return op_status;
253}
254
255
256
257
258
259
260
261int i2c_probe (uchar chip)
262{
263 u32 tmp;
264
265
266
267
268
269
270 return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
271}
272
273#endif
274