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