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