]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - cpu/xscale/i2c.c
* Patch by Robert Schwebel, 21 Jan 2003:
[karo-tx-uboot.git] / cpu / xscale / i2c.c
1 /*
2  * (C) Copyright 2000
3  * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
4  *
5  * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6  * Marius Groeger <mgroeger@sysgo.de>
7  *
8  * (C) Copyright 2003 Pengutronix e.K.
9  * Robert Schwebel <r.schwebel@pengutronix.de>
10  *
11  * See file CREDITS for list of people who contributed to this
12  * project.
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License as
16  * published by the Free Software Foundation; either version 2 of
17  * the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27  * MA 02111-1307 USA
28  *
29  * Back ported to the 8xx platform (from the 8260 platform) by
30  * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
31  */
32
33 /* FIXME: this file is PXA255 specific! What about other XScales? */
34
35 #include <common.h>
36
37 #ifdef CONFIG_HARD_I2C
38
39 /* 
40  *      - CFG_I2C_SPEED 
41  *      - I2C_PXA_SLAVE_ADDR 
42  */
43
44 #include <asm/arch/pxa-regs.h>
45 #include <i2c.h>
46
47 //#define       DEBUG_I2C       1       /* activate local debugging output  */
48 #define I2C_PXA_SLAVE_ADDR      0x1     /* slave pxa unit address           */
49 #define I2C_ICR_INIT            (ICR_BEIE | ICR_IRFIE | ICR_ITEIE | ICR_GCD | ICR_SCLE)
50 #define I2C_ISR_INIT            0x7FF
51
52 #ifdef DEBUG_I2C
53 #define PRINTD(x) printf x
54 #else
55 #define PRINTD(x)
56 #endif
57
58
59 /* Shall the current transfer have a start/stop condition? */
60 #define I2C_COND_NORMAL         0
61 #define I2C_COND_START          1
62 #define I2C_COND_STOP           2
63
64 /* Shall the current transfer be ack/nacked or being waited for it? */
65 #define I2C_ACKNAK_WAITACK      1       
66 #define I2C_ACKNAK_SENDACK      2
67 #define I2C_ACKNAK_SENDNAK      4
68
69 /* Specify who shall transfer the data (master or slave) */
70 #define I2C_READ                0
71 #define I2C_WRITE               1
72
73 /* All transfers are described by this data structure */
74 struct i2c_msg {
75         u8 condition;
76         u8 acknack; 
77         u8 direction; 
78         u8 data;
79 };
80
81
82 /**
83  * i2c_pxa_reset: - reset the host controller 
84  *
85  */
86
87 static void i2c_reset( void )
88 {
89         ICR &= ~ICR_IUE;                /* disable unit */
90         ICR |= ICR_UR;                  /* reset the unit */
91         udelay(100);
92         ICR &= ~ICR_IUE;                /* disable unit */
93         CKEN |= CKEN14_I2C;             /* set the global I2C clock on */
94         ISAR = I2C_PXA_SLAVE_ADDR;      /* set our slave address */
95         ICR = I2C_ICR_INIT;             /* set control register values */
96         ISR = I2C_ISR_INIT;             /* set clear interrupt bits */
97         ICR |= ICR_IUE;                 /* enable unit */
98         udelay(100);
99 }
100
101
102 /**
103  * i2c_isr_set_cleared: - wait until certain bits of the I2C status register 
104  *                        are set and cleared
105  *
106  * @return: 0 in case of success, 1 means timeout (no match within 10 ms). 
107  */
108
109 static int i2c_isr_set_cleared( unsigned long set_mask, unsigned long cleared_mask )
110 {
111         int timeout = 10000;
112
113         while( ((ISR & set_mask)!=set_mask) || ((ISR & cleared_mask)!=0) ){
114                 udelay( 10 );
115                 if( timeout-- < 0 ) return 0;
116         }
117
118         return 1;
119 }
120
121
122 /**
123  * i2c_transfer: - Transfer one byte over the i2c bus
124  *
125  * This function can tranfer a byte over the i2c bus in both directions. 
126  * It is used by the public API functions. 
127  *
128  * @return:  0: transfer successful
129  *          -1: message is empty
130  *          -2: transmit timeout
131  *          -3: ACK missing
132  *          -4: receive timeout
133  *          -5: illegal parameters
134  *          -6: bus is busy and couldn't be aquired
135  */ 
136 int i2c_transfer(struct i2c_msg *msg)
137 {
138         int ret;
139
140         if (!msg) 
141                 goto transfer_error_msg_empty;
142
143         switch(msg->direction) {
144
145         case I2C_WRITE:
146
147                 /* check if bus is not busy */
148                 if (!i2c_isr_set_cleared(0,ISR_IBB))
149                         goto transfer_error_bus_busy;
150
151                 /* start transmission */
152                 ICR &= ~ICR_START;
153                 ICR &= ~ICR_STOP;
154                 IDBR = msg->data;
155                 if (msg->condition == I2C_COND_START)     ICR |=  ICR_START;
156                 if (msg->condition == I2C_COND_STOP)      ICR |=  ICR_STOP; 
157                 if (msg->acknack   == I2C_ACKNAK_SENDNAK) ICR |=  ICR_ACKNAK;
158                 if (msg->acknack   == I2C_ACKNAK_SENDACK) ICR &= ~ICR_ACKNAK;
159                 ICR &= ~ICR_ALDIE;
160                 ICR |= ICR_TB; 
161
162                 /* transmit register empty? */
163                 if (!i2c_isr_set_cleared(ISR_ITE,0)) 
164                         goto transfer_error_transmit_timeout;
165
166                 /* clear 'transmit empty' state */
167                 ISR |= ISR_ITE;
168
169                 /* wait for ACK from slave */
170                 if (msg->acknack == I2C_ACKNAK_WAITACK)
171                         if (!i2c_isr_set_cleared(0,ISR_ACKNAK)) 
172                                 goto transfer_error_ack_missing;
173                 break;
174
175         case I2C_READ:
176
177                 /* check if bus is not busy */
178                 if (!i2c_isr_set_cleared(0,ISR_IBB))
179                         goto transfer_error_bus_busy;
180
181                 /* start receive */
182                 ICR &= ~ICR_START;
183                 ICR &= ~ICR_STOP;
184                 if (msg->condition == I2C_COND_START)     ICR |= ICR_START;
185                 if (msg->condition == I2C_COND_STOP)      ICR |= ICR_STOP;
186                 if (msg->acknack   == I2C_ACKNAK_SENDNAK) ICR |=  ICR_ACKNAK;
187                 if (msg->acknack   == I2C_ACKNAK_SENDACK) ICR &= ~ICR_ACKNAK;
188                 ICR &= ~ICR_ALDIE;
189                 ICR |= ICR_TB;
190
191                 /* receive register full? */
192                 if (!i2c_isr_set_cleared(ISR_IRF,0)) 
193                         goto transfer_error_receive_timeout; 
194
195                 msg->data = IDBR;
196
197                 /* clear 'receive empty' state */
198                 ISR |= ISR_IRF;
199
200                 break;
201
202         default:
203
204                 goto transfer_error_illegal_param;
205
206         }
207
208         return 0; 
209
210 transfer_error_msg_empty: 
211                 PRINTD(("i2c_transfer: error: 'msg' is empty\n"));
212                 ret = -1; goto i2c_transfer_finish;
213
214 transfer_error_transmit_timeout:
215                 PRINTD(("i2c_transfer: error: transmit timeout\n"));
216                 ret = -2; goto i2c_transfer_finish;
217
218 transfer_error_ack_missing:
219                 PRINTD(("i2c_transfer: error: ACK missing\n"));
220                 ret = -3; goto i2c_transfer_finish;
221
222 transfer_error_receive_timeout:
223                 PRINTD(("i2c_transfer: error: receive timeout\n"));
224                 ret = -4; goto i2c_transfer_finish;
225
226 transfer_error_illegal_param:
227                 PRINTD(("i2c_transfer: error: illegal parameters\n"));
228                 ret = -5; goto i2c_transfer_finish;
229
230 transfer_error_bus_busy:
231                 PRINTD(("i2c_transfer: error: bus is busy\n"));
232                 ret = -6; goto i2c_transfer_finish;
233
234 i2c_transfer_finish:
235                 PRINTD(("i2c_transfer: ISR: 0x%04x\n",ISR));
236                 i2c_reset();
237                 return ret;
238
239 }
240
241 /* ------------------------------------------------------------------------ */
242 /* API Functions                                                            */
243 /* ------------------------------------------------------------------------ */
244
245 void i2c_init(int speed, int slaveaddr)
246 {
247 }
248
249
250 /**
251  * i2c_probe: - Test if a chip answers for a given i2c address
252  *
253  * @chip:       address of the chip which is searched for 
254  * @return:     0 if a chip was found, -1 otherwhise
255  */
256
257 int i2c_probe(uchar chip)
258 {
259         struct i2c_msg msg;
260
261         i2c_reset();
262
263         msg.condition = I2C_COND_START;
264         msg.acknack   = I2C_ACKNAK_WAITACK;
265         msg.direction = I2C_WRITE;
266         msg.data      = (chip << 1) + 1;
267         if (i2c_transfer(&msg)) return -1;
268
269         msg.condition = I2C_COND_STOP;
270         msg.acknack   = I2C_ACKNAK_SENDNAK;
271         msg.direction = I2C_READ;
272         msg.data      = 0x00;
273         if (i2c_transfer(&msg)) return -1;
274
275         return 0;
276 }
277
278
279 /**
280  * i2c_read: - Read multiple bytes from an i2c device
281  *
282  * The higher level routines take into account that this function is only
283  * called with len < page length of the device (see configuration file) 
284  *
285  * @chip:       address of the chip which is to be read
286  * @addr:       i2c data address within the chip
287  * @alen:       length of the i2c data address (1..2 bytes)
288  * @buffer:     where to write the data
289  * @len:        how much byte do we want to read
290  * @return:     0 in case of success
291  */
292
293 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
294 {
295         struct i2c_msg msg;
296         u8 addr_bytes[3]; /* lowest...highest byte of data address */
297         int ret;
298
299         PRINTD(("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, len=0x%02x)\n",chip,addr,alen,len));
300
301         i2c_reset();
302
303         /* dummy chip address write */
304         PRINTD(("i2c_read: dummy chip address write\n"));
305         msg.condition = I2C_COND_START;
306         msg.acknack   = I2C_ACKNAK_WAITACK;
307         msg.direction = I2C_WRITE;
308         msg.data      = (chip << 1);
309         msg.data     &= 0xFE;
310         if ((ret=i2c_transfer(&msg))) return -1;
311         
312         /*
313          * send memory address bytes; 
314          * alen defines how much bytes we have to send. 
315          */
316         //addr &= ((1 << CFG_EEPROM_PAGE_WRITE_BITS)-1);
317         addr_bytes[0] = (u8)((addr >>  0) & 0x000000FF);
318         addr_bytes[1] = (u8)((addr >>  8) & 0x000000FF);
319         addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
320
321         while (--alen >= 0) {
322
323                 PRINTD(("i2c_read: send memory word address byte %1d\n",alen));
324                 msg.condition = I2C_COND_NORMAL;
325                 msg.acknack   = I2C_ACKNAK_WAITACK;
326                 msg.direction = I2C_WRITE;
327                 msg.data      = addr_bytes[alen];
328                 if ((ret=i2c_transfer(&msg))) return -1;
329         }
330         
331
332         /* start read sequence */
333         PRINTD(("i2c_read: start read sequence\n"));
334         msg.condition = I2C_COND_START;
335         msg.acknack   = I2C_ACKNAK_WAITACK;
336         msg.direction = I2C_WRITE;
337         msg.data      = (chip << 1);
338         msg.data     |= 0x01;
339         if ((ret=i2c_transfer(&msg))) return -1;
340
341         /* read bytes; send NACK at last byte */
342         while (len--) {
343
344                 if (len==0) { 
345                         msg.condition = I2C_COND_STOP;
346                         msg.acknack   = I2C_ACKNAK_SENDNAK;
347                 } else {
348                         msg.condition = I2C_COND_NORMAL;
349                         msg.acknack   = I2C_ACKNAK_SENDACK;
350                 }
351
352                 msg.direction = I2C_READ;
353                 msg.data      = 0x00;
354                 if ((ret=i2c_transfer(&msg))) return -1;
355
356                 *(buffer++) = msg.data;
357
358                 PRINTD(("i2c_read: reading byte (0x%08x)=0x%02x\n",(unsigned int)buffer,*buffer));
359
360         }
361
362         i2c_reset();
363
364         return 0;
365 }
366
367
368 /**
369  * i2c_write: -  Write multiple bytes to an i2c device
370  *
371  * The higher level routines take into account that this function is only
372  * called with len < page length of the device (see configuration file) 
373  *
374  * @chip:       address of the chip which is to be written
375  * @addr:       i2c data address within the chip
376  * @alen:       length of the i2c data address (1..2 bytes)
377  * @buffer:     where to find the data to be written 
378  * @len:        how much byte do we want to read
379  * @return:     0 in case of success
380  */
381
382 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
383 {
384         struct i2c_msg msg;
385         u8 addr_bytes[3]; /* lowest...highest byte of data address */
386
387         PRINTD(("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, len=0x%02x)\n",chip,addr,alen,len));
388
389         i2c_reset();
390
391         /* chip address write */
392         PRINTD(("i2c_write: chip address write\n"));
393         msg.condition = I2C_COND_START;
394         msg.acknack   = I2C_ACKNAK_WAITACK;
395         msg.direction = I2C_WRITE;
396         msg.data      = (chip << 1);
397         msg.data     &= 0xFE;
398         if (i2c_transfer(&msg)) return -1;
399         
400         /*
401          * send memory address bytes; 
402          * alen defines how much bytes we have to send. 
403          */
404         addr_bytes[0] = (u8)((addr >>  0) & 0x000000FF);
405         addr_bytes[1] = (u8)((addr >>  8) & 0x000000FF);
406         addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
407
408         while (--alen >= 0) {
409
410                 PRINTD(("i2c_write: send memory word address\n"));
411                 msg.condition = I2C_COND_NORMAL;
412                 msg.acknack   = I2C_ACKNAK_WAITACK;
413                 msg.direction = I2C_WRITE;
414                 msg.data      = addr_bytes[alen];
415                 if (i2c_transfer(&msg)) return -1;
416         }
417                 
418         /* write bytes; send NACK at last byte */
419         while (len--) {
420
421                 PRINTD(("i2c_write: writing byte (0x%08x)=0x%02x\n",(unsigned int)buffer,*buffer));
422
423                 if (len==0) 
424                         msg.condition = I2C_COND_STOP;
425                 else
426                         msg.condition = I2C_COND_NORMAL;
427
428                 msg.acknack   = I2C_ACKNAK_WAITACK;
429                 msg.direction = I2C_WRITE;
430                 msg.data      = *(buffer++);
431                 
432                 if (i2c_transfer(&msg)) return -1;
433
434         }
435
436         i2c_reset();
437
438         return 0;
439
440 }
441
442 uchar i2c_reg_read (uchar chip, uchar reg)
443 {
444         PRINTD(("i2c_reg_read(chip=0x%02x, reg=0x%02x)\n",chip,reg));
445         return 0;
446 }
447
448 void  i2c_reg_write(uchar chip, uchar reg, uchar val)
449 {
450         PRINTD(("i2c_reg_write(chip=0x%02x, reg=0x%02x, val=0x%02x)\n",chip,reg,val));
451 }
452
453 #endif  /* CONFIG_HARD_I2C */