Merge branch 'master' of rsync://rsync.denx.de/git/u-boot
[platform/kernel/u-boot.git] / common / soft_i2c.c
1 /*
2  * (C) Copyright 2001, 2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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  * This has been changed substantially by Gerald Van Baren, Custom IDEAS,
24  * vanbaren@cideas.com.  It was heavily influenced by LiMon, written by
25  * Neil Russell.
26  */
27
28 #include <common.h>
29 #ifdef  CONFIG_MPC8260                  /* only valid for MPC8260 */
30 #include <ioports.h>
31 #endif
32 #ifdef CONFIG_AT91RM9200DK              /* need this for the at91rm9200dk */
33 #include <asm/io.h>
34 #include <asm/arch/hardware.h>
35 #endif
36 #include <i2c.h>
37
38 #if defined(CONFIG_SOFT_I2C)
39
40 /* #define      DEBUG_I2C       */
41
42 #ifdef DEBUG_I2C
43 DECLARE_GLOBAL_DATA_PTR;
44 #endif
45
46
47 /*-----------------------------------------------------------------------
48  * Definitions
49  */
50
51 #define RETRIES         0
52
53
54 #define I2C_ACK         0               /* PD_SDA level to ack a byte */
55 #define I2C_NOACK       1               /* PD_SDA level to noack a byte */
56
57
58 #ifdef DEBUG_I2C
59 #define PRINTD(fmt,args...)     do {    \
60         if (gd->have_console)           \
61                 printf (fmt ,##args);   \
62         } while (0)
63 #else
64 #define PRINTD(fmt,args...)
65 #endif
66
67 /*-----------------------------------------------------------------------
68  * Local functions
69  */
70 static void  send_reset (void);
71 static void  send_start (void);
72 static void  send_stop  (void);
73 static void  send_ack   (int);
74 static int   write_byte (uchar byte);
75 static uchar read_byte  (int);
76
77
78 /*-----------------------------------------------------------------------
79  * Send a reset sequence consisting of 9 clocks with the data signal high
80  * to clock any confused device back into an idle state.  Also send a
81  * <stop> at the end of the sequence for belts & suspenders.
82  */
83 static void send_reset(void)
84 {
85 #ifdef  CONFIG_MPC8260
86         volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, I2C_PORT);
87 #endif
88 #ifdef  CONFIG_8xx
89         volatile immap_t *immr = (immap_t *)CFG_IMMR;
90 #endif
91         int j;
92
93         I2C_SCL(1);
94         I2C_SDA(1);
95 #ifdef  I2C_INIT
96         I2C_INIT;
97 #endif
98         I2C_TRISTATE;
99         for(j = 0; j < 9; j++) {
100                 I2C_SCL(0);
101                 I2C_DELAY;
102                 I2C_DELAY;
103                 I2C_SCL(1);
104                 I2C_DELAY;
105                 I2C_DELAY;
106         }
107         send_stop();
108         I2C_TRISTATE;
109 }
110
111 /*-----------------------------------------------------------------------
112  * START: High -> Low on SDA while SCL is High
113  */
114 static void send_start(void)
115 {
116 #ifdef  CONFIG_MPC8260
117         volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, I2C_PORT);
118 #endif
119 #ifdef  CONFIG_8xx
120         volatile immap_t *immr = (immap_t *)CFG_IMMR;
121 #endif
122
123         I2C_DELAY;
124         I2C_SDA(1);
125         I2C_ACTIVE;
126         I2C_DELAY;
127         I2C_SCL(1);
128         I2C_DELAY;
129         I2C_SDA(0);
130         I2C_DELAY;
131 }
132
133 /*-----------------------------------------------------------------------
134  * STOP: Low -> High on SDA while SCL is High
135  */
136 static void send_stop(void)
137 {
138 #ifdef  CONFIG_MPC8260
139         volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, I2C_PORT);
140 #endif
141 #ifdef  CONFIG_8xx
142         volatile immap_t *immr = (immap_t *)CFG_IMMR;
143 #endif
144
145         I2C_SCL(0);
146         I2C_DELAY;
147         I2C_SDA(0);
148         I2C_ACTIVE;
149         I2C_DELAY;
150         I2C_SCL(1);
151         I2C_DELAY;
152         I2C_SDA(1);
153         I2C_DELAY;
154         I2C_TRISTATE;
155 }
156
157
158 /*-----------------------------------------------------------------------
159  * ack should be I2C_ACK or I2C_NOACK
160  */
161 static void send_ack(int ack)
162 {
163 #ifdef  CONFIG_MPC8260
164         volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, I2C_PORT);
165 #endif
166 #ifdef  CONFIG_8xx
167         volatile immap_t *immr = (immap_t *)CFG_IMMR;
168 #endif
169
170         I2C_SCL(0);
171         I2C_DELAY;
172         I2C_ACTIVE;
173         I2C_SDA(ack);
174         I2C_DELAY;
175         I2C_SCL(1);
176         I2C_DELAY;
177         I2C_DELAY;
178         I2C_SCL(0);
179         I2C_DELAY;
180 }
181
182
183 /*-----------------------------------------------------------------------
184  * Send 8 bits and look for an acknowledgement.
185  */
186 static int write_byte(uchar data)
187 {
188 #ifdef  CONFIG_MPC8260
189         volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, I2C_PORT);
190 #endif
191 #ifdef  CONFIG_8xx
192         volatile immap_t *immr = (immap_t *)CFG_IMMR;
193 #endif
194         int j;
195         int nack;
196
197         I2C_ACTIVE;
198         for(j = 0; j < 8; j++) {
199                 I2C_SCL(0);
200                 I2C_DELAY;
201                 I2C_SDA(data & 0x80);
202                 I2C_DELAY;
203                 I2C_SCL(1);
204                 I2C_DELAY;
205                 I2C_DELAY;
206
207                 data <<= 1;
208         }
209
210         /*
211          * Look for an <ACK>(negative logic) and return it.
212          */
213         I2C_SCL(0);
214         I2C_DELAY;
215         I2C_SDA(1);
216         I2C_TRISTATE;
217         I2C_DELAY;
218         I2C_SCL(1);
219         I2C_DELAY;
220         I2C_DELAY;
221         nack = I2C_READ;
222         I2C_SCL(0);
223         I2C_DELAY;
224         I2C_ACTIVE;
225
226         return(nack);   /* not a nack is an ack */
227 }
228
229
230 /*-----------------------------------------------------------------------
231  * if ack == I2C_ACK, ACK the byte so can continue reading, else
232  * send I2C_NOACK to end the read.
233  */
234 static uchar read_byte(int ack)
235 {
236 #ifdef  CONFIG_MPC8260
237         volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, I2C_PORT);
238 #endif
239 #ifdef  CONFIG_8xx
240         volatile immap_t *immr = (immap_t *)CFG_IMMR;
241 #endif
242         int  data;
243         int  j;
244
245         /*
246          * Read 8 bits, MSB first.
247          */
248         I2C_TRISTATE;
249         data = 0;
250         for(j = 0; j < 8; j++) {
251                 I2C_SCL(0);
252                 I2C_DELAY;
253                 I2C_SCL(1);
254                 I2C_DELAY;
255                 data <<= 1;
256                 data |= I2C_READ;
257                 I2C_DELAY;
258         }
259         send_ack(ack);
260
261         return(data);
262 }
263
264 /*=====================================================================*/
265 /*                         Public Functions                            */
266 /*=====================================================================*/
267
268 /*-----------------------------------------------------------------------
269  * Initialization
270  */
271 void i2c_init (int speed, int slaveaddr)
272 {
273         /*
274          * WARNING: Do NOT save speed in a static variable: if the
275          * I2C routines are called before RAM is initialized (to read
276          * the DIMM SPD, for instance), RAM won't be usable and your
277          * system will crash.
278          */
279         send_reset ();
280 }
281
282 /*-----------------------------------------------------------------------
283  * Probe to see if a chip is present.  Also good for checking for the
284  * completion of EEPROM writes since the chip stops responding until
285  * the write completes (typically 10mSec).
286  */
287 int i2c_probe(uchar addr)
288 {
289         int rc;
290
291         /*
292          * perform 1 byte write transaction with just address byte
293          * (fake write)
294          */
295         send_start();
296         rc = write_byte ((addr << 1) | 0);
297         send_stop();
298
299         return (rc ? 1 : 0);
300 }
301
302 /*-----------------------------------------------------------------------
303  * Read bytes
304  */
305 int  i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
306 {
307         int shift;
308         PRINTD("i2c_read: chip %02X addr %02X alen %d buffer %p len %d\n",
309                 chip, addr, alen, buffer, len);
310
311 #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
312         /*
313          * EEPROM chips that implement "address overflow" are ones
314          * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
315          * address and the extra bits end up in the "chip address"
316          * bit slots. This makes a 24WC08 (1Kbyte) chip look like
317          * four 256 byte chips.
318          *
319          * Note that we consider the length of the address field to
320          * still be one byte because the extra address bits are
321          * hidden in the chip address.
322          */
323         chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
324
325         PRINTD("i2c_read: fix addr_overflow: chip %02X addr %02X\n",
326                 chip, addr);
327 #endif
328
329         /*
330          * Do the addressing portion of a write cycle to set the
331          * chip's address pointer.  If the address length is zero,
332          * don't do the normal write cycle to set the address pointer,
333          * there is no address pointer in this chip.
334          */
335         send_start();
336         if(alen > 0) {
337                 if(write_byte(chip << 1)) {     /* write cycle */
338                         send_stop();
339                         PRINTD("i2c_read, no chip responded %02X\n", chip);
340                         return(1);
341                 }
342                 shift = (alen-1) * 8;
343                 while(alen-- > 0) {
344                         if(write_byte(addr >> shift)) {
345                                 PRINTD("i2c_read, address not <ACK>ed\n");
346                                 return(1);
347                         }
348                         shift -= 8;
349                 }
350                 send_stop();    /* reportedly some chips need a full stop */
351                 send_start();
352         }
353         /*
354          * Send the chip address again, this time for a read cycle.
355          * Then read the data.  On the last byte, we do a NACK instead
356          * of an ACK(len == 0) to terminate the read.
357          */
358         write_byte((chip << 1) | 1);    /* read cycle */
359         while(len-- > 0) {
360                 *buffer++ = read_byte(len == 0);
361         }
362         send_stop();
363         return(0);
364 }
365
366 /*-----------------------------------------------------------------------
367  * Write bytes
368  */
369 int  i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
370 {
371         int shift, failures = 0;
372
373         PRINTD("i2c_write: chip %02X addr %02X alen %d buffer %p len %d\n",
374                 chip, addr, alen, buffer, len);
375
376         send_start();
377         if(write_byte(chip << 1)) {     /* write cycle */
378                 send_stop();
379                 PRINTD("i2c_write, no chip responded %02X\n", chip);
380                 return(1);
381         }
382         shift = (alen-1) * 8;
383         while(alen-- > 0) {
384                 if(write_byte(addr >> shift)) {
385                         PRINTD("i2c_write, address not <ACK>ed\n");
386                         return(1);
387                 }
388                 shift -= 8;
389         }
390
391         while(len-- > 0) {
392                 if(write_byte(*buffer++)) {
393                         failures++;
394                 }
395         }
396         send_stop();
397         return(failures);
398 }
399
400 /*-----------------------------------------------------------------------
401  * Read a register
402  */
403 uchar i2c_reg_read(uchar i2c_addr, uchar reg)
404 {
405         uchar buf;
406
407         i2c_read(i2c_addr, reg, 1, &buf, 1);
408
409         return(buf);
410 }
411
412 /*-----------------------------------------------------------------------
413  * Write a register
414  */
415 void i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
416 {
417         i2c_write(i2c_addr, reg, 1, &val, 1);
418 }
419
420
421 #endif  /* CONFIG_SOFT_I2C */