blob: e9d077178b10f4adcbf5ee0fa6fe9064bd0b6889 [file] [log] [blame]
wdenk983fda82004-10-28 00:09:35 +00001/*
2 * (C) Copyright 2004, Freescale, Inc
3 * TsiChung Liew, Tsi-Chung.Liew@freescale.com.
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#include <common.h>
25
26#ifdef CONFIG_HARD_I2C
27
28#include <mpc8220.h>
29#include <i2c.h>
30
31typedef struct mpc8220_i2c {
32 volatile u32 adr; /* I2Cn + 0x00 */
33 volatile u32 fdr; /* I2Cn + 0x04 */
34 volatile u32 cr; /* I2Cn + 0x08 */
35 volatile u32 sr; /* I2Cn + 0x0C */
36 volatile u32 dr; /* I2Cn + 0x10 */
37} i2c_t;
38
39/* I2Cn control register bits */
40#define I2C_EN 0x80
41#define I2C_IEN 0x40
42#define I2C_STA 0x20
43#define I2C_TX 0x10
44#define I2C_TXAK 0x08
45#define I2C_RSTA 0x04
46#define I2C_INIT_MASK (I2C_EN | I2C_STA | I2C_TX | I2C_RSTA)
47
48/* I2Cn status register bits */
49#define I2C_CF 0x80
50#define I2C_AAS 0x40
51#define I2C_BB 0x20
52#define I2C_AL 0x10
53#define I2C_SRW 0x04
54#define I2C_IF 0x02
55#define I2C_RXAK 0x01
56
57#define I2C_TIMEOUT 100
58#define I2C_RETRIES 1
59
60struct mpc8220_i2c_tap {
61 int scl2tap;
62 int tap2tap;
63};
64
65static int mpc_reg_in (volatile u32 * reg);
66static void mpc_reg_out (volatile u32 * reg, int val, int mask);
67static int wait_for_bb (void);
68static int wait_for_pin (int *status);
69static int do_address (uchar chip, char rdwr_flag);
70static int send_bytes (uchar chip, char *buf, int len);
71static int receive_bytes (uchar chip, char *buf, int len);
72static int mpc_get_fdr (int);
73
74static int mpc_reg_in (volatile u32 * reg)
75{
76 return *reg >> 24;
77 __asm__ __volatile__ ("eieio");
78}
79
80static void mpc_reg_out (volatile u32 * reg, int val, int mask)
81{
82 int tmp;
83
84 if (!mask) {
85 *reg = val << 24;
86 } else {
87 tmp = mpc_reg_in (reg);
88 *reg = ((tmp & ~mask) | (val & mask)) << 24;
89 }
90 __asm__ __volatile__ ("eieio");
91
92 return;
93}
94
95static int wait_for_bb (void)
96{
97 i2c_t *regs = (i2c_t *) MMAP_I2C;
98 int timeout = I2C_TIMEOUT;
99 int status;
100
101 status = mpc_reg_in (&regs->sr);
102
103 while (timeout-- && (status & I2C_BB)) {
104#if 1
105 volatile int temp;
106
107 mpc_reg_out (&regs->cr, I2C_STA, I2C_STA);
108 temp = mpc_reg_in (&regs->dr);
109 mpc_reg_out (&regs->cr, 0, I2C_STA);
110 mpc_reg_out (&regs->cr, 0, 0);
111 mpc_reg_out (&regs->cr, I2C_EN, 0);
112#endif
113 udelay (1000);
114 status = mpc_reg_in (&regs->sr);
115 }
116
117 return (status & I2C_BB);
118}
119
120static int wait_for_pin (int *status)
121{
122 i2c_t *regs = (i2c_t *) MMAP_I2C;
123 int timeout = I2C_TIMEOUT;
124
125 *status = mpc_reg_in (&regs->sr);
126
127 while (timeout-- && !(*status & I2C_IF)) {
128 udelay (1000);
129 *status = mpc_reg_in (&regs->sr);
130 }
131
132 if (!(*status & I2C_IF)) {
133 return -1;
134 }
135
136 mpc_reg_out (&regs->sr, 0, I2C_IF);
137 return 0;
138}
139
140static int do_address (uchar chip, char rdwr_flag)
141{
142 i2c_t *regs = (i2c_t *) MMAP_I2C;
143 int status;
144
145 chip <<= 1;
146
147 if (rdwr_flag)
148 chip |= 1;
149
150 mpc_reg_out (&regs->cr, I2C_TX, I2C_TX);
151 mpc_reg_out (&regs->dr, chip, 0);
152
153 if (wait_for_pin (&status))
154 return -2;
155 if (status & I2C_RXAK)
156 return -3;
157 return 0;
158}
159
160static int send_bytes (uchar chip, char *buf, int len)
161{
162 i2c_t *regs = (i2c_t *) MMAP_I2C;
163 int wrcount;
164 int status;
165
166 for (wrcount = 0; wrcount < len; ++wrcount) {
167
168 mpc_reg_out (&regs->dr, buf[wrcount], 0);
169
170 if (wait_for_pin (&status))
171 break;
172
173 if (status & I2C_RXAK)
174 break;
175
176 }
177
178 return !(wrcount == len);
179 return 0;
180}
181
182static int receive_bytes (uchar chip, char *buf, int len)
183{
184 i2c_t *regs = (i2c_t *) MMAP_I2C;
185 int dummy = 1;
186 int rdcount = 0;
187 int status;
188 int i;
189
190 mpc_reg_out (&regs->cr, 0, I2C_TX);
191
192 for (i = 0; i < len; ++i) {
193 buf[rdcount] = mpc_reg_in (&regs->dr);
194
195 if (dummy)
196 dummy = 0;
197 else
198 rdcount++;
199
200 if (wait_for_pin (&status))
201 return -4;
202 }
203
204 mpc_reg_out (&regs->cr, I2C_TXAK, I2C_TXAK);
205 buf[rdcount++] = mpc_reg_in (&regs->dr);
206
207 if (wait_for_pin (&status))
208 return -5;
209
210 mpc_reg_out (&regs->cr, 0, I2C_TXAK);
211 return 0;
212}
213
214/**************** I2C API ****************/
215
216void i2c_init (int speed, int saddr)
217{
218 i2c_t *regs = (i2c_t *) MMAP_I2C;
219
220 mpc_reg_out (&regs->cr, 0, 0);
221 mpc_reg_out (&regs->adr, saddr << 1, 0);
222
223 /* Set clock
224 */
225 mpc_reg_out (&regs->fdr, mpc_get_fdr (speed), 0);
226
227 /* Enable module
228 */
229 mpc_reg_out (&regs->cr, I2C_EN, I2C_INIT_MASK);
230 mpc_reg_out (&regs->sr, 0, I2C_IF);
231 return;
232}
233
234static int mpc_get_fdr (int speed)
235{
236 DECLARE_GLOBAL_DATA_PTR;
237 static int fdr = -1;
238
239 if (fdr == -1) {
240 ulong best_speed = 0;
241 ulong divider;
242 ulong ipb, scl;
243 ulong bestmatch = 0xffffffffUL;
244 int best_i = 0, best_j = 0, i, j;
245 int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8 };
246 struct mpc8220_i2c_tap scltap[] = {
247 {4, 1},
248 {4, 2},
249 {6, 4},
250 {6, 8},
251 {14, 16},
252 {30, 32},
253 {62, 64},
254 {126, 128}
255 };
256
257 ipb = gd->bus_clk;
258 for (i = 7; i >= 0; i--) {
259 for (j = 7; j >= 0; j--) {
260 scl = 2 * (scltap[j].scl2tap +
261 (SCL_Tap[i] -
262 1) * scltap[j].tap2tap + 2);
263 if (ipb <= speed * scl) {
264 if ((speed * scl - ipb) < bestmatch) {
265 bestmatch = speed * scl - ipb;
266 best_i = i;
267 best_j = j;
268 best_speed = ipb / scl;
269 }
270 }
271 }
272 }
273 divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
274 if (gd->flags & GD_FLG_RELOC) {
275 fdr = divider;
276 } else {
277 printf ("%ld kHz, ", best_speed / 1000);
278 return divider;
279 }
280 }
281
282 return fdr;
283}
284
285int i2c_probe (uchar chip)
286{
287 i2c_t *regs = (i2c_t *) MMAP_I2C;
288 int i;
289
290 for (i = 0; i < I2C_RETRIES; i++) {
291 mpc_reg_out (&regs->cr, I2C_STA, I2C_STA);
292
293 if (!do_address (chip, 0)) {
294 mpc_reg_out (&regs->cr, 0, I2C_STA);
295 break;
296 }
297
298 mpc_reg_out (&regs->cr, 0, I2C_STA);
299 udelay (50);
300 }
301
302 return (i == I2C_RETRIES);
303}
304
305int i2c_read (uchar chip, uint addr, int alen, uchar * buf, int len)
306{
307 uchar xaddr[4];
308 i2c_t *regs = (i2c_t *) MMAP_I2C;
309 int ret = -1;
310
311 xaddr[0] = (addr >> 24) & 0xFF;
312 xaddr[1] = (addr >> 16) & 0xFF;
313 xaddr[2] = (addr >> 8) & 0xFF;
314 xaddr[3] = addr & 0xFF;
315
316 if (wait_for_bb ()) {
317 printf ("i2c_read: bus is busy\n");
318 goto Done;
319 }
320
321 mpc_reg_out (&regs->cr, I2C_STA, I2C_STA);
322 if (do_address (chip, 0)) {
323 printf ("i2c_read: failed to address chip\n");
324 goto Done;
325 }
326
327 if (send_bytes (chip, &xaddr[4 - alen], alen)) {
328 printf ("i2c_read: send_bytes failed\n");
329 goto Done;
330 }
331
332 mpc_reg_out (&regs->cr, I2C_RSTA, I2C_RSTA);
333 if (do_address (chip, 1)) {
334 printf ("i2c_read: failed to address chip\n");
335 goto Done;
336 }
337
338 if (receive_bytes (chip, buf, len)) {
339 printf ("i2c_read: receive_bytes failed\n");
340 goto Done;
341 }
342
343 ret = 0;
344 Done:
345 mpc_reg_out (&regs->cr, 0, I2C_STA);
346 return ret;
347}
348
349int i2c_write (uchar chip, uint addr, int alen, uchar * buf, int len)
350{
351 uchar xaddr[4];
352 i2c_t *regs = (i2c_t *) MMAP_I2C;
353 int ret = -1;
354
355 xaddr[0] = (addr >> 24) & 0xFF;
356 xaddr[1] = (addr >> 16) & 0xFF;
357 xaddr[2] = (addr >> 8) & 0xFF;
358 xaddr[3] = addr & 0xFF;
359
360 if (wait_for_bb ()) {
361 printf ("i2c_write: bus is busy\n");
362 goto Done;
363 }
364
365 mpc_reg_out (&regs->cr, I2C_STA, I2C_STA);
366 if (do_address (chip, 0)) {
367 printf ("i2c_write: failed to address chip\n");
368 goto Done;
369 }
370
371 if (send_bytes (chip, &xaddr[4 - alen], alen)) {
372 printf ("i2c_write: send_bytes failed\n");
373 goto Done;
374 }
375
376 if (send_bytes (chip, buf, len)) {
377 printf ("i2c_write: send_bytes failed\n");
378 goto Done;
379 }
380
381 ret = 0;
382 Done:
383 mpc_reg_out (&regs->cr, 0, I2C_STA);
384 return ret;
385}
386
387uchar i2c_reg_read (uchar chip, uchar reg)
388{
389 char buf;
390
391 i2c_read (chip, reg, 1, &buf, 1);
392
393 return buf;
394}
395
396void i2c_reg_write (uchar chip, uchar reg, uchar val)
397{
398 i2c_write (chip, reg, 1, &val, 1);
399
400 return;
401}
402
403#endif /* CONFIG_HARD_I2C */