]> git.sur5r.net Git - u-boot/blob - cpu/mpc83xx/i2c.c
mpc83xx: Replace CFG_IMMRBAR with CFG_IMMR
[u-boot] / cpu / mpc83xx / i2c.c
1 /*
2  * (C) Copyright 2006 Freescale Semiconductor, Inc.
3  *
4  * (C) Copyright 2003,Motorola Inc.
5  * Xianghua Xiao <x.xiao@motorola.com>
6  * Adapted for Motorola 85xx chip.
7  *
8  * (C) Copyright 2003
9  * Gleb Natapov <gnatapov@mrv.com>
10  * Some bits are taken from linux driver writen by adrian@humboldt.co.uk
11  *
12  * Hardware I2C driver for MPC107 PCI bridge.
13  *
14  * See file CREDITS for list of people who contributed to this
15  * project.
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License as
19  * published by the Free Software Foundation; either version 2 of
20  * the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30  * MA 02111-1307 USA
31  *
32  * Change log:
33  *
34  * 20050101: Eran Liberty (liberty@freescale.com)
35  *           Initial file creating (porting from 85XX & 8260)
36  * 20060601: Dave Liu (daveliu@freescale.com)
37  *           Unified variable names for mpc83xx
38  */
39
40 #include <common.h>
41 #include <command.h>
42 #include <asm/io.h>
43
44 #ifdef CONFIG_HARD_I2C
45 #include <i2c.h>
46 #include <asm/i2c.h>
47
48 DECLARE_GLOBAL_DATA_PTR;
49
50 /* Initialize the bus pointer to whatever one the SPD EEPROM is on.
51  * Default is bus 1.  This is necessary because the DDR initialization
52  * runs from ROM, and we can't switch buses because we can't modify
53  * the i2c_dev variable.  Everything gets straightened out once i2c_init
54  * is called from RAM.  */
55
56 #ifndef CFG_SPD_BUS_NUM
57 #define CFG_SPD_BUS_NUM 1
58 #endif
59
60 static unsigned int i2c_bus_num = CFG_SPD_BUS_NUM;
61
62 #if CFG_SPD_BUS_NUM == 1
63 static volatile i2c_t *i2c_dev = I2C_1;
64 #else
65 static volatile i2c_t *i2c_dev = I2C_2;
66 #endif
67
68 static int i2c_bus_speed[2] = {0, 0};
69
70 /*
71  * Map the frequency divider to the FDR.  This data is taken from table 17-5
72  * of the MPC8349EA reference manual, with duplicates removed.
73  */
74 static struct {
75     unsigned int divider;
76     u8 fdr;
77 } i2c_speed_map[] =
78 {
79         {0, 0x20},
80         {256, 0x20},
81         {288, 0x21},
82         {320, 0x22},
83         {352, 0x23},
84         {384, 0x24},
85         {416, 0x01},
86         {448, 0x25},
87         {480, 0x02},
88         {512, 0x26},
89         {576, 0x27},
90         {640, 0x28},
91         {704, 0x05},
92         {768, 0x29},
93         {832, 0x06},
94         {896, 0x2A},
95         {1024, 0x2B},
96         {1152, 0x08},
97         {1280, 0x2C},
98         {1536, 0x2D},
99         {1792, 0x2E},
100         {1920, 0x0B},
101         {2048, 0x2F},
102         {2304, 0x0C},
103         {2560, 0x30},
104         {3072, 0x31},
105         {3584, 0x32},
106         {3840, 0x0F},
107         {4096, 0x33},
108         {4608, 0x10},
109         {5120, 0x34},
110         {6144, 0x35},
111         {7168, 0x36},
112         {7680, 0x13},
113         {8192, 0x37},
114         {9216, 0x14},
115         {10240, 0x38},
116         {12288, 0x39},
117         {14336, 0x3A},
118         {15360, 0x17},
119         {16384, 0x3B},
120         {18432, 0x18},
121         {20480, 0x3C},
122         {24576, 0x3D},
123         {28672, 0x3E},
124         {30720, 0x1B},
125         {32768, 0x3F},
126         {36864, 0x1C},
127         {40960, 0x1D},
128         {49152, 0x1E},
129         {61440, 0x1F},
130         {-1, 0x1F}
131 };
132
133 #define NUM_I2C_SPEEDS (sizeof(i2c_speed_map) / sizeof(i2c_speed_map[0]))
134
135 static int set_speed(unsigned int speed)
136 {
137         unsigned long i2c_clk;
138         unsigned int divider, i;
139         u8 fdr = 0x3F;
140
141         i2c_clk = (i2c_bus_num == 2) ? gd->i2c2_clk : gd->i2c1_clk;
142
143         divider = i2c_clk / speed;
144
145         /* Scan i2c_speed_map[] for the closest matching divider.*/
146
147         for (i = 0; i < NUM_I2C_SPEEDS-1; i++) {
148                 /* Locate our divider in between two entries in i2c_speed_map[] */
149                 if ((divider >= i2c_speed_map[i].divider) &&
150                     (divider <= i2c_speed_map[i+1].divider)) {
151                         /* Which one is closer? */
152                         if ((divider - i2c_speed_map[i].divider) < (i2c_speed_map[i+1].divider - divider)) {
153                                 fdr = i2c_speed_map[i].fdr;
154                         } else {
155                                 fdr = i2c_speed_map[i+1].fdr;
156                         }
157                         break;
158                 }
159         }
160
161         writeb(fdr, &i2c_dev->fdr);
162         i2c_bus_speed[i2c_bus_num - 1] = speed;
163
164         return 0;
165 }
166
167
168 static void _i2c_init(int speed, int slaveadd)
169 {
170         /* stop I2C controller */
171         writeb(0x00 , &i2c_dev->cr);
172
173         /* set clock */
174         set_speed(speed);
175
176         /* set default filter */
177         writeb(IC2_FDR,&i2c_dev->dfsrr);
178
179         /* write slave address */
180         writeb(slaveadd, &i2c_dev->adr);
181
182         /* clear status register */
183         writeb(I2C_CR_MTX, &i2c_dev->sr);
184
185         /* start I2C controller */
186         writeb(I2C_CR_MEN, &i2c_dev->cr);
187 }
188
189 void i2c_init(int speed, int slaveadd)
190 {
191         /* Set both interfaces to the same speed and slave address */
192         /* Note: This function gets called twice - before and after
193          * relocation to RAM.  The first time it's called, we are unable
194          * to change buses, so whichever one 'i2c_dev' was initialized to
195          * gets set twice.  When run from RAM both buses get set properly */
196
197         i2c_set_bus_num(1);
198         _i2c_init(speed, slaveadd);
199 #ifdef  CFG_I2C2_OFFSET
200         i2c_set_bus_num(2);
201         _i2c_init(speed, slaveadd);
202         i2c_set_bus_num(1);
203 #endif  /* CFG_I2C2_OFFSET */
204 }
205
206 static __inline__ int
207 i2c_wait4bus (void)
208 {
209         ulong timeval = get_timer (0);
210         while (readb(&i2c_dev->sr) & I2C_SR_MBB) {
211                 if (get_timer (timeval) > I2C_TIMEOUT) {
212                         return -1;
213                 }
214         }
215         return 0;
216 }
217
218 static __inline__ int
219 i2c_wait (int write)
220 {
221         u32 csr;
222         ulong timeval = get_timer(0);
223         do {
224                 csr = readb(&i2c_dev->sr);
225
226                 if (!(csr & I2C_SR_MIF))
227                         continue;
228
229                 writeb(0x0, &i2c_dev->sr);
230
231                 if (csr & I2C_SR_MAL) {
232                         debug("i2c_wait: MAL\n");
233                         return -1;
234                 }
235
236                 if (!(csr & I2C_SR_MCF))        {
237                         debug("i2c_wait: unfinished\n");
238                         return -1;
239                 }
240
241                 if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
242                         debug("i2c_wait: No RXACK\n");
243                         return -1;
244                 }
245
246                 return 0;
247         } while (get_timer (timeval) < I2C_TIMEOUT);
248
249         debug("i2c_wait: timed out\n");
250         return -1;
251 }
252
253 static __inline__ int
254 i2c_write_addr (u8 dev, u8 dir, int rsta)
255 {
256         writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
257                (rsta?I2C_CR_RSTA:0),
258                &i2c_dev->cr);
259
260         writeb((dev << 1) | dir, &i2c_dev->dr);
261
262         if (i2c_wait (I2C_WRITE) < 0)
263                 return 0;
264         return 1;
265 }
266
267 static __inline__ int
268 __i2c_write (u8 *data, int length)
269 {
270         int i;
271
272         writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
273                &i2c_dev->cr);
274
275         for (i=0; i < length; i++) {
276                 writeb(data[i], &i2c_dev->dr);
277
278                 if (i2c_wait (I2C_WRITE) < 0)
279                         break;
280         }
281         return i;
282 }
283
284 static __inline__ int
285 __i2c_read (u8 *data, int length)
286 {
287         int i;
288
289         writeb(I2C_CR_MEN | I2C_CR_MSTA |
290                ((length == 1) ? I2C_CR_TXAK : 0),
291                &i2c_dev->cr);
292
293         /* dummy read */
294         readb(&i2c_dev->dr);
295
296         for (i=0; i < length; i++) {
297                 if (i2c_wait (I2C_READ) < 0)
298                         break;
299
300                 /* Generate ack on last next to last byte */
301                 if (i == length - 2)
302                         writeb(I2C_CR_MEN | I2C_CR_MSTA |
303                                I2C_CR_TXAK,
304                                &i2c_dev->cr);
305
306                 /* Generate stop on last byte */
307                 if (i == length - 1)
308                         writeb(I2C_CR_MEN | I2C_CR_TXAK, &i2c_dev->cr);
309
310                 data[i] = readb(&i2c_dev->dr);
311         }
312         return i;
313 }
314
315 int
316 i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
317 {
318         int i = 0;
319         u8 *a = (u8*)&addr;
320
321         if (i2c_wait4bus () < 0)
322                 goto exit;
323
324         if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
325                 goto exit;
326
327         if (__i2c_write (&a[4 - alen], alen) != alen)
328                 goto exit;
329
330         if (i2c_write_addr (dev, I2C_READ, 1) == 0)
331                 goto exit;
332
333         i = __i2c_read (data, length);
334
335  exit:
336         writeb(I2C_CR_MEN, &i2c_dev->cr);
337         return !(i == length);
338 }
339
340 int
341 i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
342 {
343         int i = 0;
344         u8 *a = (u8*)&addr;
345
346         if (i2c_wait4bus () < 0)
347                 goto exit;
348
349         if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
350                 goto exit;
351
352         if (__i2c_write (&a[4 - alen], alen) != alen)
353                 goto exit;
354
355         i = __i2c_write (data, length);
356
357  exit:
358         writeb(I2C_CR_MEN, &i2c_dev->cr);
359         return !(i == length);
360 }
361
362 int i2c_probe (uchar chip)
363 {
364         int tmp;
365
366         /*
367          * Try to read the first location of the chip.  The underlying
368          * driver doesn't appear to support sending just the chip address
369          * and looking for an <ACK> back.
370          */
371         udelay(10000);
372         return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
373 }
374
375 uchar i2c_reg_read (uchar i2c_addr, uchar reg)
376 {
377         uchar buf[1];
378
379         i2c_read (i2c_addr, reg, 1, buf, 1);
380
381         return (buf[0]);
382 }
383
384 void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
385 {
386         i2c_write (i2c_addr, reg, 1, &val, 1);
387 }
388
389 int i2c_set_bus_num(unsigned int bus)
390 {
391         if(bus == 1)
392         {
393                 i2c_dev = I2C_1;
394         }
395 #ifdef  CFG_I2C2_OFFSET
396         else if(bus == 2)
397         {
398                 i2c_dev = I2C_2;
399         }
400 #endif
401         else
402         {
403                 return -1;
404         }
405         i2c_bus_num = bus;
406         return 0;
407 }
408
409 int i2c_set_bus_speed(unsigned int speed)
410 {
411         return set_speed(speed);
412 }
413
414 unsigned int i2c_get_bus_num(void)
415 {
416         return i2c_bus_num;
417 }
418
419 unsigned int i2c_get_bus_speed(void)
420 {
421         return i2c_bus_speed[i2c_bus_num - 1];
422 }
423 #endif /* CONFIG_HARD_I2C */