1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <common.h>
21#include <asm/io.h>
22
23#ifdef CONFIG_HARD_I2C
24#include <i2c.h>
25#include "mv_i2c.h"
26
27#ifdef DEBUG_I2C
28#define PRINTD(x) printf x
29#else
30#define PRINTD(x)
31#endif
32
33
34struct i2c_msg {
35 u8 condition;
36 u8 acknack;
37 u8 direction;
38 u8 data;
39};
40
41struct mv_i2c {
42 u32 ibmr;
43 u32 pad0;
44 u32 idbr;
45 u32 pad1;
46 u32 icr;
47 u32 pad2;
48 u32 isr;
49 u32 pad3;
50 u32 isar;
51};
52
53static struct mv_i2c *base;
54static void i2c_board_init(struct mv_i2c *base)
55{
56#ifdef CONFIG_SYS_I2C_INIT_BOARD
57 u32 icr;
58
59
60
61
62
63
64
65
66 icr = readl(&base->icr);
67 writel(readl(&base->icr) & ~(ICR_SCLE | ICR_IUE), &base->icr);
68
69 i2c_init_board();
70
71 writel(icr, &base->icr);
72#endif
73}
74
75#ifdef CONFIG_I2C_MULTI_BUS
76static u32 i2c_regs[CONFIG_MV_I2C_NUM] = CONFIG_MV_I2C_REG;
77static unsigned int bus_initialized[CONFIG_MV_I2C_NUM];
78static unsigned int current_bus;
79
80int i2c_set_bus_num(unsigned int bus)
81{
82 if ((bus < 0) || (bus >= CONFIG_MV_I2C_NUM)) {
83 printf("Bad bus: %d\n", bus);
84 return -1;
85 }
86
87 base = (struct mv_i2c *)i2c_regs[bus];
88 current_bus = bus;
89
90 if (!bus_initialized[current_bus]) {
91 i2c_board_init(base);
92 bus_initialized[current_bus] = 1;
93 }
94
95 return 0;
96}
97
98unsigned int i2c_get_bus_num(void)
99{
100 return current_bus;
101}
102#endif
103
104
105
106
107
108static void i2c_reset(void)
109{
110 writel(readl(&base->icr) & ~ICR_IUE, &base->icr);
111 writel(readl(&base->icr) | ICR_UR, &base->icr);
112 udelay(100);
113 writel(readl(&base->icr) & ~ICR_IUE, &base->icr);
114
115 i2c_clk_enable();
116
117 writel(CONFIG_SYS_I2C_SLAVE, &base->isar);
118 writel(I2C_ICR_INIT, &base->icr);
119 writel(I2C_ISR_INIT, &base->isr);
120 writel(readl(&base->icr) | ICR_IUE, &base->icr);
121 udelay(100);
122}
123
124
125
126
127
128
129
130static int i2c_isr_set_cleared(unsigned long set_mask,
131 unsigned long cleared_mask)
132{
133 int timeout = 1000, isr;
134
135 do {
136 isr = readl(&base->isr);
137 udelay(10);
138 if (timeout-- < 0)
139 return 0;
140 } while (((isr & set_mask) != set_mask)
141 || ((isr & cleared_mask) != 0));
142
143 return 1;
144}
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160int i2c_transfer(struct i2c_msg *msg)
161{
162 int ret;
163
164 if (!msg)
165 goto transfer_error_msg_empty;
166
167 switch (msg->direction) {
168 case I2C_WRITE:
169
170 if (!i2c_isr_set_cleared(0, ISR_IBB))
171 goto transfer_error_bus_busy;
172
173
174 writel(readl(&base->icr) & ~ICR_START, &base->icr);
175 writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
176 writel(msg->data, &base->idbr);
177 if (msg->condition == I2C_COND_START)
178 writel(readl(&base->icr) | ICR_START, &base->icr);
179 if (msg->condition == I2C_COND_STOP)
180 writel(readl(&base->icr) | ICR_STOP, &base->icr);
181 if (msg->acknack == I2C_ACKNAK_SENDNAK)
182 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
183 if (msg->acknack == I2C_ACKNAK_SENDACK)
184 writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
185 writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
186 writel(readl(&base->icr) | ICR_TB, &base->icr);
187
188
189 if (!i2c_isr_set_cleared(ISR_ITE, 0))
190 goto transfer_error_transmit_timeout;
191
192
193 writel(readl(&base->isr) | ISR_ITE, &base->isr);
194
195
196 if (msg->acknack == I2C_ACKNAK_WAITACK)
197 if (!i2c_isr_set_cleared(0, ISR_ACKNAK))
198 goto transfer_error_ack_missing;
199 break;
200
201 case I2C_READ:
202
203
204 if (!i2c_isr_set_cleared(0, ISR_IBB))
205 goto transfer_error_bus_busy;
206
207
208 writel(readl(&base->icr) & ~ICR_START, &base->icr);
209 writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
210 if (msg->condition == I2C_COND_START)
211 writel(readl(&base->icr) | ICR_START, &base->icr);
212 if (msg->condition == I2C_COND_STOP)
213 writel(readl(&base->icr) | ICR_STOP, &base->icr);
214 if (msg->acknack == I2C_ACKNAK_SENDNAK)
215 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
216 if (msg->acknack == I2C_ACKNAK_SENDACK)
217 writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
218 writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
219 writel(readl(&base->icr) | ICR_TB, &base->icr);
220
221
222 if (!i2c_isr_set_cleared(ISR_IRF, 0))
223 goto transfer_error_receive_timeout;
224
225 msg->data = readl(&base->idbr);
226
227
228 writel(readl(&base->isr) | ISR_IRF, &base->isr);
229 break;
230 default:
231 goto transfer_error_illegal_param;
232 }
233
234 return 0;
235
236transfer_error_msg_empty:
237 PRINTD(("i2c_transfer: error: 'msg' is empty\n"));
238 ret = -1; goto i2c_transfer_finish;
239
240transfer_error_transmit_timeout:
241 PRINTD(("i2c_transfer: error: transmit timeout\n"));
242 ret = -2; goto i2c_transfer_finish;
243
244transfer_error_ack_missing:
245 PRINTD(("i2c_transfer: error: ACK missing\n"));
246 ret = -3; goto i2c_transfer_finish;
247
248transfer_error_receive_timeout:
249 PRINTD(("i2c_transfer: error: receive timeout\n"));
250 ret = -4; goto i2c_transfer_finish;
251
252transfer_error_illegal_param:
253 PRINTD(("i2c_transfer: error: illegal parameters\n"));
254 ret = -5; goto i2c_transfer_finish;
255
256transfer_error_bus_busy:
257 PRINTD(("i2c_transfer: error: bus is busy\n"));
258 ret = -6; goto i2c_transfer_finish;
259
260i2c_transfer_finish:
261 PRINTD(("i2c_transfer: ISR: 0x%04x\n", readl(&base->isr)));
262 i2c_reset();
263 return ret;
264}
265
266
267
268
269void i2c_init(int speed, int slaveaddr)
270{
271#ifdef CONFIG_I2C_MULTI_BUS
272 current_bus = 0;
273 base = (struct mv_i2c *)i2c_regs[current_bus];
274#else
275 base = (struct mv_i2c *)CONFIG_MV_I2C_REG;
276#endif
277
278 i2c_board_init(base);
279}
280
281
282
283
284
285
286
287int i2c_probe(uchar chip)
288{
289 struct i2c_msg msg;
290
291 i2c_reset();
292
293 msg.condition = I2C_COND_START;
294 msg.acknack = I2C_ACKNAK_WAITACK;
295 msg.direction = I2C_WRITE;
296 msg.data = (chip << 1) + 1;
297 if (i2c_transfer(&msg))
298 return -1;
299
300 msg.condition = I2C_COND_STOP;
301 msg.acknack = I2C_ACKNAK_SENDNAK;
302 msg.direction = I2C_READ;
303 msg.data = 0x00;
304 if (i2c_transfer(&msg))
305 return -1;
306
307 return 0;
308}
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
324{
325 struct i2c_msg msg;
326 u8 addr_bytes[3];
327
328 PRINTD(("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
329 "len=0x%02x)\n", chip, addr, alen, len));
330
331 i2c_reset();
332
333
334 PRINTD(("i2c_read: dummy chip address write\n"));
335 msg.condition = I2C_COND_START;
336 msg.acknack = I2C_ACKNAK_WAITACK;
337 msg.direction = I2C_WRITE;
338 msg.data = (chip << 1);
339 msg.data &= 0xFE;
340 if (i2c_transfer(&msg))
341 return -1;
342
343
344
345
346
347
348 addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
349 addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
350 addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
351
352 while (--alen >= 0) {
353 PRINTD(("i2c_read: send memory word address byte %1d\n", alen));
354 msg.condition = I2C_COND_NORMAL;
355 msg.acknack = I2C_ACKNAK_WAITACK;
356 msg.direction = I2C_WRITE;
357 msg.data = addr_bytes[alen];
358 if (i2c_transfer(&msg))
359 return -1;
360 }
361
362
363 PRINTD(("i2c_read: start read sequence\n"));
364 msg.condition = I2C_COND_START;
365 msg.acknack = I2C_ACKNAK_WAITACK;
366 msg.direction = I2C_WRITE;
367 msg.data = (chip << 1);
368 msg.data |= 0x01;
369 if (i2c_transfer(&msg))
370 return -1;
371
372
373 while (len--) {
374 if (len == 0) {
375 msg.condition = I2C_COND_STOP;
376 msg.acknack = I2C_ACKNAK_SENDNAK;
377 } else {
378 msg.condition = I2C_COND_NORMAL;
379 msg.acknack = I2C_ACKNAK_SENDACK;
380 }
381
382 msg.direction = I2C_READ;
383 msg.data = 0x00;
384 if (i2c_transfer(&msg))
385 return -1;
386
387 *buffer = msg.data;
388 PRINTD(("i2c_read: reading byte (0x%08x)=0x%02x\n",
389 (unsigned int)buffer, *buffer));
390 buffer++;
391 }
392
393 i2c_reset();
394
395 return 0;
396}
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
412{
413 struct i2c_msg msg;
414 u8 addr_bytes[3];
415
416 PRINTD(("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
417 "len=0x%02x)\n", chip, addr, alen, len));
418
419 i2c_reset();
420
421
422 PRINTD(("i2c_write: chip address write\n"));
423 msg.condition = I2C_COND_START;
424 msg.acknack = I2C_ACKNAK_WAITACK;
425 msg.direction = I2C_WRITE;
426 msg.data = (chip << 1);
427 msg.data &= 0xFE;
428 if (i2c_transfer(&msg))
429 return -1;
430
431
432
433
434
435 addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
436 addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
437 addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
438
439 while (--alen >= 0) {
440 PRINTD(("i2c_write: send memory word address\n"));
441 msg.condition = I2C_COND_NORMAL;
442 msg.acknack = I2C_ACKNAK_WAITACK;
443 msg.direction = I2C_WRITE;
444 msg.data = addr_bytes[alen];
445 if (i2c_transfer(&msg))
446 return -1;
447 }
448
449
450 while (len--) {
451 PRINTD(("i2c_write: writing byte (0x%08x)=0x%02x\n",
452 (unsigned int)buffer, *buffer));
453
454 if (len == 0)
455 msg.condition = I2C_COND_STOP;
456 else
457 msg.condition = I2C_COND_NORMAL;
458
459 msg.acknack = I2C_ACKNAK_WAITACK;
460 msg.direction = I2C_WRITE;
461 msg.data = *(buffer++);
462
463 if (i2c_transfer(&msg))
464 return -1;
465 }
466
467 i2c_reset();
468
469 return 0;
470}
471#endif
472