]> git.sur5r.net Git - u-boot/blob - drivers/s3c24x0_i2c.c
Code reworked for PPC405EP support.
[u-boot] / drivers / s3c24x0_i2c.c
1 /*
2  * (C) Copyright 2002
3  * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 /* This code should work for both the S3C2400 and the S3C2410
25  * as they seem to have the same I2C controller inside.
26  * The different address mapping is handled by the s3c24xx.h files below.
27  */
28
29 #include <common.h>
30
31 #ifdef CONFIG_DRIVER_S3C24X0_I2C
32
33 #if defined(CONFIG_S3C2400)
34 #include <s3c2400.h>
35 #elif defined(CONFIG_S3C2410)
36 #include <s3c2410.h>
37 #endif
38 #include <i2c.h>
39
40 #ifdef CONFIG_HARD_I2C
41
42 #define IIC_WRITE       0
43 #define IIC_READ        1
44
45 #define IIC_OK          0
46 #define IIC_NOK         1
47 #define IIC_NACK        2
48 #define IIC_NOK_LA      3               /* Lost arbitration */
49 #define IIC_NOK_TOUT    4               /* time out */
50
51 #define IICSTAT_BSY     0x20            /* Busy bit */
52 #define IICSTAT_NACK    0x01            /* Nack bit */
53 #define IICCON_IRPND    0x10            /* Interrupt pending bit */
54 #define IIC_MODE_MT     0xC0            /* Master Transmit Mode */
55 #define IIC_MODE_MR     0x80            /* Master Receive Mode */
56 #define IIC_START_STOP  0x20            /* START / STOP */
57 #define IIC_TXRX_ENA    0x10            /* I2C Tx/Rx enable */
58
59 #define IIC_TIMEOUT 1                   /* 1 seconde */
60
61
62 static int GetIICSDA(void)
63 {
64         return (rGPEDAT & 0x8000) >> 15;
65 }
66
67 #if 0
68 static void SetIICSDA(int x)
69 {
70         rGPEDAT = (rGPEDAT & ~0x8000) | (x&1) << 15;
71 }
72 #endif
73
74 static void SetIICSCL(int x)
75 {
76         rGPEDAT = (rGPEDAT & ~0x4000) | (x&1) << 14;
77 }
78
79
80 static int WaitForXfer(void)
81 {
82     int i, status;
83
84     i = IIC_TIMEOUT * 1000;
85     status = rIICCON;
86     while ((i > 0) && !(status & IICCON_IRPND)) {
87         udelay(1000);
88         status = rIICCON;
89         i--;
90     }
91
92     return(status & IICCON_IRPND) ? IIC_OK : IIC_NOK_TOUT;
93 }
94
95 static int IsACK(void)
96 {
97     return(!(rIICSTAT & IICSTAT_NACK));
98 }
99
100 static void ReadWriteByte(void)
101 {
102     rIICCON &= ~IICCON_IRPND;
103 }
104
105 void i2c_init (int speed, int slaveadd)
106 {
107     ulong freq, pres = 16, div;
108     int i, status;
109
110     /* wait for some time to give previous transfer a chance to finish */
111
112     i = IIC_TIMEOUT * 1000;
113     status = rIICSTAT;
114     while ((i > 0) && (status & IICSTAT_BSY)) {
115         udelay(1000);
116         status = rIICSTAT;
117         i--;
118     }
119
120     if ((status & IICSTAT_BSY) || GetIICSDA() == 0) {
121         ulong old_gpecon = rGPECON;
122         /* bus still busy probably by (most) previously interrupted transfer */
123
124         /* set IICSDA and IICSCL (GPE15, GPE14) to GPIO */
125         rGPECON = (rGPECON & ~0xF0000000) | 0x10000000;
126
127         /* toggle IICSCL until bus idle */
128         SetIICSCL(0); udelay(1000);
129         i = 10;
130         while ((i > 0) && (GetIICSDA() != 1)) {
131                 SetIICSCL(1); udelay(1000);
132                 SetIICSCL(0); udelay(1000);
133                 i--;
134         }
135         SetIICSCL(1); udelay(1000);
136
137         /* restore pin functions */
138         rGPECON = old_gpecon;
139     }
140
141     /* calculate prescaler and divisor values */
142     freq = get_PCLK();
143     if ((freq / pres / (16+1)) > speed)
144         /* set prescaler to 512 */
145         pres = 512;
146
147     div = 0;
148     while ((freq / pres / (div+1)) > speed)
149         div++;
150
151     /* set prescaler, divisor according to freq, also set
152        ACKGEN, IRQ */
153     rIICCON = (div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0);
154
155     /* init to SLAVE REVEIVE and set slaveaddr */
156     rIICSTAT = 0;
157     rIICADD = slaveadd;
158     /* program Master Transmit (and implicit STOP) */
159     rIICSTAT = IIC_MODE_MT | IIC_TXRX_ENA;
160
161 }
162
163 /*
164   cmd_type is 0 for write 1 for read.
165
166   addr_len can take any value from 0-255, it is only limited
167   by the char, we could make it larger if needed. If it is
168   0 we skip the address write cycle.
169
170 */
171 static
172 int i2c_transfer(unsigned char cmd_type,
173                  unsigned char chip,
174                  unsigned char addr[],
175                  unsigned char addr_len,
176                  unsigned char data[],
177                  unsigned short data_len)
178 {
179     int i, status, result;
180
181     if (data == 0 || data_len == 0) {
182         /*Don't support data transfer of no length or to address 0*/
183         printf( "i2c_transfer: bad call\n" );
184         return IIC_NOK;
185     }
186
187     //CheckDelay();
188
189     /* Check I2C bus idle */
190     i = IIC_TIMEOUT * 1000;
191     status = rIICSTAT;
192     while ((i > 0) && (status & IICSTAT_BSY)) {
193         udelay(1000);
194         status = rIICSTAT;
195         i--;
196     }
197
198
199     if (status & IICSTAT_BSY) {
200         result = IIC_NOK_TOUT;
201         return(result);
202     }
203
204     rIICCON |= 0x80;
205
206     result = IIC_OK;
207
208     switch (cmd_type) {
209         case IIC_WRITE:
210             if (addr && addr_len) {
211                 rIICDS = chip;
212                 /* send START */
213                 rIICSTAT = IIC_MODE_MT | IIC_TXRX_ENA | IIC_START_STOP;
214                 i = 0;
215                 while ((i < addr_len) && (result == IIC_OK)) {
216                     result = WaitForXfer();
217                     rIICDS = addr[i];
218                     ReadWriteByte();
219                     i++;
220                 }
221                 i = 0;
222                 while ((i < data_len) && (result == IIC_OK)) {
223                     result = WaitForXfer();
224                     rIICDS = data[i];
225                     ReadWriteByte();
226                     i++;
227                 }
228             } else {
229                 rIICDS = chip;
230                 /* send START */
231                 rIICSTAT = IIC_MODE_MT | IIC_TXRX_ENA | IIC_START_STOP;
232                 i = 0;
233                 while ((i < data_len) && (result = IIC_OK)) {
234                     result = WaitForXfer();
235                     rIICDS = data[i];
236                     ReadWriteByte();
237                     i++;
238                 }
239             }
240
241             if (result == IIC_OK)
242                 result = WaitForXfer();
243
244             /* send STOP */
245             rIICSTAT = IIC_MODE_MR | IIC_TXRX_ENA;
246             ReadWriteByte();
247             break;
248
249         case IIC_READ:
250             if (addr && addr_len) {
251                 rIICSTAT = IIC_MODE_MT | IIC_TXRX_ENA;
252                 rIICDS = chip;
253                 /* send START */
254                 rIICSTAT |= IIC_START_STOP;
255                 result = WaitForXfer();
256                 if (IsACK()) {
257                     i = 0;
258                     while ((i < addr_len) && (result == IIC_OK)) {
259                         rIICDS = addr[i];
260                         ReadWriteByte();
261                         result = WaitForXfer();
262                         i++;
263                     }
264
265                     rIICDS = chip;
266                     /* resend START */
267                     rIICSTAT = IIC_MODE_MR | IIC_TXRX_ENA | IIC_START_STOP;
268                     ReadWriteByte();
269                     result = WaitForXfer();
270                     i = 0;
271                     while ((i < data_len) && (result == IIC_OK)) {
272                         /* disable ACK for final READ */
273                         if (i == data_len - 1)
274                             rIICCON &= ~0x80;
275                         ReadWriteByte();
276                         result = WaitForXfer();
277                         data[i] = rIICDS;
278                         i++;
279                     }
280                 } else {
281                     result = IIC_NACK;
282                 }
283
284             } else {
285                 rIICSTAT = IIC_MODE_MR | IIC_TXRX_ENA;
286                 rIICDS = chip;
287                 /* send START */
288                 rIICSTAT |= IIC_START_STOP;
289                 result = WaitForXfer();
290
291                 if (IsACK()) {
292                     i = 0;
293                     while ((i < data_len) && (result == IIC_OK)) {
294                         /* disable ACK for final READ */
295                         if (i == data_len - 1)
296                             rIICCON &= ~0x80;
297                         ReadWriteByte();
298                         result = WaitForXfer();
299                         data[i] = rIICDS;
300                         i++;
301                     }
302                 } else {
303                     result = IIC_NACK;
304                 }
305             }
306
307             /* send STOP */
308             rIICSTAT = IIC_MODE_MR | IIC_TXRX_ENA;
309             ReadWriteByte();
310             break;
311
312         default:
313             printf( "i2c_transfer: bad call\n" );
314             result = IIC_NOK;
315             break;
316     }
317
318     return (result);
319 }
320
321 int i2c_probe (uchar chip)
322 {
323     uchar buf[1];
324
325     buf[0] = 0;
326
327     /*
328      * What is needed is to send the chip address and verify that the
329      * address was <ACK>ed (i.e. there was a chip at that address which
330      * drove the data line low).
331      */
332     return(i2c_transfer (IIC_READ, chip << 1, 0, 0, buf, 1) != IIC_OK);
333 }
334
335 int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
336 {
337     uchar xaddr[4];
338     int ret;
339
340     if ( alen > 4 ) {
341         printf ("I2C read: addr len %d not supported\n", alen);
342         return 1;
343     }
344
345     if ( alen > 0 ) {
346         xaddr[0] = (addr >> 24) & 0xFF;
347         xaddr[1] = (addr >> 16) & 0xFF;
348         xaddr[2] = (addr >> 8) & 0xFF;
349         xaddr[3] = addr & 0xFF;
350     }
351
352
353 #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
354     /*
355      * EEPROM chips that implement "address overflow" are ones
356      * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
357      * address and the extra bits end up in the "chip address"
358      * bit slots. This makes a 24WC08 (1Kbyte) chip look like
359      * four 256 byte chips.
360      *
361      * Note that we consider the length of the address field to
362      * still be one byte because the extra address bits are
363      * hidden in the chip address.
364      */
365     if( alen > 0 )
366         chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
367 #endif
368     if( (ret = i2c_transfer(IIC_READ, chip<<1, &xaddr[4-alen], alen, buffer, len )) != 0) {
369         printf( "I2c read: failed %d\n", ret);
370         return 1;
371     }
372     return 0;
373 }
374
375 int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
376 {
377     uchar xaddr[4];
378
379     if ( alen > 4 ) {
380         printf ("I2C write: addr len %d not supported\n", alen);
381         return 1;
382     }
383
384     if ( alen > 0 ) {
385         xaddr[0] = (addr >> 24) & 0xFF;
386         xaddr[1] = (addr >> 16) & 0xFF;
387         xaddr[2] = (addr >> 8) & 0xFF;
388         xaddr[3] = addr & 0xFF;
389     }
390
391 #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
392     /*
393      * EEPROM chips that implement "address overflow" are ones
394      * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
395      * address and the extra bits end up in the "chip address"
396      * bit slots. This makes a 24WC08 (1Kbyte) chip look like
397      * four 256 byte chips.
398      *
399      * Note that we consider the length of the address field to
400      * still be one byte because the extra address bits are
401      * hidden in the chip address.
402      */
403     if( alen > 0 )
404         chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
405 #endif
406     return (i2c_transfer(IIC_WRITE, chip<<1, &xaddr[4-alen], alen, buffer, len ) != 0);
407 }
408
409 #endif  /* CONFIG_HARD_I2C */
410
411 #endif /* CONFIG_DRIVER_S3C24X0_I2C */