2 * (C) Copyright 2004, Freescale, Inc
3 * TsiChung Liew, Tsi-Chung.Liew@freescale.com.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 #ifdef CONFIG_HARD_I2C
31 typedef 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 */
39 /* I2Cn control register bits */
46 #define I2C_INIT_MASK (I2C_EN | I2C_STA | I2C_TX | I2C_RSTA)
48 /* I2Cn status register bits */
57 #define I2C_TIMEOUT 100
60 struct mpc8220_i2c_tap {
65 static int mpc_reg_in (volatile u32 * reg);
66 static void mpc_reg_out (volatile u32 * reg, int val, int mask);
67 static int wait_for_bb (void);
68 static int wait_for_pin (int *status);
69 static int do_address (uchar chip, char rdwr_flag);
70 static int send_bytes (uchar chip, char *buf, int len);
71 static int receive_bytes (uchar chip, char *buf, int len);
72 static int mpc_get_fdr (int);
74 static int mpc_reg_in (volatile u32 * reg)
77 __asm__ __volatile__ ("eieio");
80 static void mpc_reg_out (volatile u32 * reg, int val, int mask)
87 tmp = mpc_reg_in (reg);
88 *reg = ((tmp & ~mask) | (val & mask)) << 24;
90 __asm__ __volatile__ ("eieio");
95 static int wait_for_bb (void)
97 i2c_t *regs = (i2c_t *) MMAP_I2C;
98 int timeout = I2C_TIMEOUT;
101 status = mpc_reg_in (®s->sr);
103 while (timeout-- && (status & I2C_BB)) {
107 mpc_reg_out (®s->cr, I2C_STA, I2C_STA);
108 temp = mpc_reg_in (®s->dr);
109 mpc_reg_out (®s->cr, 0, I2C_STA);
110 mpc_reg_out (®s->cr, 0, 0);
111 mpc_reg_out (®s->cr, I2C_EN, 0);
114 status = mpc_reg_in (®s->sr);
117 return (status & I2C_BB);
120 static int wait_for_pin (int *status)
122 i2c_t *regs = (i2c_t *) MMAP_I2C;
123 int timeout = I2C_TIMEOUT;
125 *status = mpc_reg_in (®s->sr);
127 while (timeout-- && !(*status & I2C_IF)) {
129 *status = mpc_reg_in (®s->sr);
132 if (!(*status & I2C_IF)) {
136 mpc_reg_out (®s->sr, 0, I2C_IF);
140 static int do_address (uchar chip, char rdwr_flag)
142 i2c_t *regs = (i2c_t *) MMAP_I2C;
150 mpc_reg_out (®s->cr, I2C_TX, I2C_TX);
151 mpc_reg_out (®s->dr, chip, 0);
153 if (wait_for_pin (&status))
155 if (status & I2C_RXAK)
160 static int send_bytes (uchar chip, char *buf, int len)
162 i2c_t *regs = (i2c_t *) MMAP_I2C;
166 for (wrcount = 0; wrcount < len; ++wrcount) {
168 mpc_reg_out (®s->dr, buf[wrcount], 0);
170 if (wait_for_pin (&status))
173 if (status & I2C_RXAK)
178 return !(wrcount == len);
182 static int receive_bytes (uchar chip, char *buf, int len)
184 i2c_t *regs = (i2c_t *) MMAP_I2C;
190 mpc_reg_out (®s->cr, 0, I2C_TX);
192 for (i = 0; i < len; ++i) {
193 buf[rdcount] = mpc_reg_in (®s->dr);
200 if (wait_for_pin (&status))
204 mpc_reg_out (®s->cr, I2C_TXAK, I2C_TXAK);
205 buf[rdcount++] = mpc_reg_in (®s->dr);
207 if (wait_for_pin (&status))
210 mpc_reg_out (®s->cr, 0, I2C_TXAK);
214 /**************** I2C API ****************/
216 void i2c_init (int speed, int saddr)
218 i2c_t *regs = (i2c_t *) MMAP_I2C;
220 mpc_reg_out (®s->cr, 0, 0);
221 mpc_reg_out (®s->adr, saddr << 1, 0);
225 mpc_reg_out (®s->fdr, mpc_get_fdr (speed), 0);
229 mpc_reg_out (®s->cr, I2C_EN, I2C_INIT_MASK);
230 mpc_reg_out (®s->sr, 0, I2C_IF);
234 static int mpc_get_fdr (int speed)
236 DECLARE_GLOBAL_DATA_PTR;
240 ulong best_speed = 0;
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[] = {
258 for (i = 7; i >= 0; i--) {
259 for (j = 7; j >= 0; j--) {
260 scl = 2 * (scltap[j].scl2tap +
262 1) * scltap[j].tap2tap + 2);
263 if (ipb <= speed * scl) {
264 if ((speed * scl - ipb) < bestmatch) {
265 bestmatch = speed * scl - ipb;
268 best_speed = ipb / scl;
273 divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
274 if (gd->flags & GD_FLG_RELOC) {
277 printf ("%ld kHz, ", best_speed / 1000);
285 int i2c_probe (uchar chip)
287 i2c_t *regs = (i2c_t *) MMAP_I2C;
290 for (i = 0; i < I2C_RETRIES; i++) {
291 mpc_reg_out (®s->cr, I2C_STA, I2C_STA);
293 if (!do_address (chip, 0)) {
294 mpc_reg_out (®s->cr, 0, I2C_STA);
298 mpc_reg_out (®s->cr, 0, I2C_STA);
302 return (i == I2C_RETRIES);
305 int i2c_read (uchar chip, uint addr, int alen, uchar * buf, int len)
308 i2c_t *regs = (i2c_t *) MMAP_I2C;
311 xaddr[0] = (addr >> 24) & 0xFF;
312 xaddr[1] = (addr >> 16) & 0xFF;
313 xaddr[2] = (addr >> 8) & 0xFF;
314 xaddr[3] = addr & 0xFF;
316 if (wait_for_bb ()) {
317 printf ("i2c_read: bus is busy\n");
321 mpc_reg_out (®s->cr, I2C_STA, I2C_STA);
322 if (do_address (chip, 0)) {
323 printf ("i2c_read: failed to address chip\n");
327 if (send_bytes (chip, &xaddr[4 - alen], alen)) {
328 printf ("i2c_read: send_bytes failed\n");
332 mpc_reg_out (®s->cr, I2C_RSTA, I2C_RSTA);
333 if (do_address (chip, 1)) {
334 printf ("i2c_read: failed to address chip\n");
338 if (receive_bytes (chip, buf, len)) {
339 printf ("i2c_read: receive_bytes failed\n");
345 mpc_reg_out (®s->cr, 0, I2C_STA);
349 int i2c_write (uchar chip, uint addr, int alen, uchar * buf, int len)
352 i2c_t *regs = (i2c_t *) MMAP_I2C;
355 xaddr[0] = (addr >> 24) & 0xFF;
356 xaddr[1] = (addr >> 16) & 0xFF;
357 xaddr[2] = (addr >> 8) & 0xFF;
358 xaddr[3] = addr & 0xFF;
360 if (wait_for_bb ()) {
361 printf ("i2c_write: bus is busy\n");
365 mpc_reg_out (®s->cr, I2C_STA, I2C_STA);
366 if (do_address (chip, 0)) {
367 printf ("i2c_write: failed to address chip\n");
371 if (send_bytes (chip, &xaddr[4 - alen], alen)) {
372 printf ("i2c_write: send_bytes failed\n");
376 if (send_bytes (chip, buf, len)) {
377 printf ("i2c_write: send_bytes failed\n");
383 mpc_reg_out (®s->cr, 0, I2C_STA);
387 uchar i2c_reg_read (uchar chip, uchar reg)
391 i2c_read (chip, reg, 1, &buf, 1);
396 void i2c_reg_write (uchar chip, uchar reg, uchar val)
398 i2c_write (chip, reg, 1, &val, 1);
403 #endif /* CONFIG_HARD_I2C */