NAND Boot: fix an invalid PC error caused by div operation.
[u-boot-openmoko/mini2440.git] / cpu / mpc5xxx / i2c.c
blob0f02e78a3bd3e25d04459b50c0b36c5d0e79cdc1
1 /*
2 * (C) Copyright 2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
6 * project.
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,
21 * MA 02111-1307 USA
24 #include <common.h>
26 DECLARE_GLOBAL_DATA_PTR;
28 #ifdef CONFIG_HARD_I2C
30 #include <mpc5xxx.h>
31 #include <i2c.h>
33 #if (CFG_I2C_MODULE == 2)
34 #define I2C_BASE MPC5XXX_I2C2
35 #elif (CFG_I2C_MODULE == 1)
36 #define I2C_BASE MPC5XXX_I2C1
37 #else
38 #error CFG_I2C_MODULE is not properly configured
39 #endif
41 #define I2C_TIMEOUT 100
42 #define I2C_RETRIES 3
44 struct mpc5xxx_i2c_tap {
45 int scl2tap;
46 int tap2tap;
49 static int mpc_reg_in (volatile u32 *reg);
50 static void mpc_reg_out (volatile u32 *reg, int val, int mask);
51 static int wait_for_bb (void);
52 static int wait_for_pin (int *status);
53 static int do_address (uchar chip, char rdwr_flag);
54 static int send_bytes (uchar chip, char *buf, int len);
55 static int receive_bytes (uchar chip, char *buf, int len);
56 static int mpc_get_fdr (int);
58 static int mpc_reg_in(volatile u32 *reg)
60 int ret = *reg >> 24;
61 __asm__ __volatile__ ("eieio");
62 return ret;
65 static void mpc_reg_out(volatile u32 *reg, int val, int mask)
67 int tmp;
69 if (!mask) {
70 *reg = val << 24;
71 } else {
72 tmp = mpc_reg_in(reg);
73 *reg = ((tmp & ~mask) | (val & mask)) << 24;
75 __asm__ __volatile__ ("eieio");
77 return;
80 static int wait_for_bb(void)
82 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
83 int timeout = I2C_TIMEOUT;
84 int status;
86 status = mpc_reg_in(&regs->msr);
88 while (timeout-- && (status & I2C_BB)) {
89 #if 1
90 volatile int temp;
91 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
92 temp = mpc_reg_in(&regs->mdr);
93 mpc_reg_out(&regs->mcr, 0, I2C_STA);
94 mpc_reg_out(&regs->mcr, 0, 0);
95 mpc_reg_out(&regs->mcr, I2C_EN, 0);
96 #endif
97 udelay(1000);
98 status = mpc_reg_in(&regs->msr);
101 return (status & I2C_BB);
104 static int wait_for_pin(int *status)
106 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
107 int timeout = I2C_TIMEOUT;
109 *status = mpc_reg_in(&regs->msr);
111 while (timeout-- && !(*status & I2C_IF)) {
112 udelay(1000);
113 *status = mpc_reg_in(&regs->msr);
116 if (!(*status & I2C_IF)) {
117 return -1;
120 mpc_reg_out(&regs->msr, 0, I2C_IF);
122 return 0;
125 static int do_address(uchar chip, char rdwr_flag)
127 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
128 int status;
130 chip <<= 1;
132 if (rdwr_flag) {
133 chip |= 1;
136 mpc_reg_out(&regs->mcr, I2C_TX, I2C_TX);
137 mpc_reg_out(&regs->mdr, chip, 0);
139 if (wait_for_pin(&status)) {
140 return -2;
143 if (status & I2C_RXAK) {
144 return -3;
147 return 0;
150 static int send_bytes(uchar chip, char *buf, int len)
152 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
153 int wrcount;
154 int status;
156 for (wrcount = 0; wrcount < len; ++wrcount) {
158 mpc_reg_out(&regs->mdr, buf[wrcount], 0);
160 if (wait_for_pin(&status)) {
161 break;
164 if (status & I2C_RXAK) {
165 break;
170 return !(wrcount == len);
173 static int receive_bytes(uchar chip, char *buf, int len)
175 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
176 int dummy = 1;
177 int rdcount = 0;
178 int status;
179 int i;
181 mpc_reg_out(&regs->mcr, 0, I2C_TX);
183 for (i = 0; i < len; ++i) {
184 buf[rdcount] = mpc_reg_in(&regs->mdr);
186 if (dummy) {
187 dummy = 0;
188 } else {
189 rdcount++;
193 if (wait_for_pin(&status)) {
194 return -4;
198 mpc_reg_out(&regs->mcr, I2C_TXAK, I2C_TXAK);
199 buf[rdcount++] = mpc_reg_in(&regs->mdr);
201 if (wait_for_pin(&status)) {
202 return -5;
205 mpc_reg_out(&regs->mcr, 0, I2C_TXAK);
207 return 0;
210 /**************** I2C API ****************/
212 void i2c_init(int speed, int saddr)
214 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
216 mpc_reg_out(&regs->mcr, 0, 0);
217 mpc_reg_out(&regs->madr, saddr << 1, 0);
219 /* Set clock
221 mpc_reg_out(&regs->mfdr, mpc_get_fdr(speed), 0);
223 /* Enable module
225 mpc_reg_out(&regs->mcr, I2C_EN, I2C_INIT_MASK);
226 mpc_reg_out(&regs->msr, 0, I2C_IF);
228 return;
231 static int mpc_get_fdr(int speed)
233 static int fdr = -1;
235 if (fdr == -1) {
236 ulong best_speed = 0;
237 ulong divider;
238 ulong ipb, scl;
239 ulong bestmatch = 0xffffffffUL;
240 int best_i = 0, best_j = 0, i, j;
241 int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8};
242 struct mpc5xxx_i2c_tap scltap[] = {
243 {4, 1},
244 {4, 2},
245 {6, 4},
246 {6, 8},
247 {14, 16},
248 {30, 32},
249 {62, 64},
250 {126, 128}
253 ipb = gd->ipb_clk;
254 for (i = 7; i >= 0; i--) {
255 for (j = 7; j >= 0; j--) {
256 scl = 2 * (scltap[j].scl2tap +
257 (SCL_Tap[i] - 1) * scltap[j].tap2tap + 2);
258 if (ipb <= speed*scl) {
259 if ((speed*scl - ipb) < bestmatch) {
260 bestmatch = speed*scl - ipb;
261 best_i = i;
262 best_j = j;
263 best_speed = ipb/scl;
268 divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
269 if (gd->flags & GD_FLG_RELOC) {
270 fdr = divider;
271 } else {
272 printf("%ld kHz, ", best_speed / 1000);
273 return divider;
277 return fdr;
280 int i2c_probe(uchar chip)
282 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
283 int i;
285 for (i = 0; i < I2C_RETRIES; i++) {
286 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
288 if (! do_address(chip, 0)) {
289 mpc_reg_out(&regs->mcr, 0, I2C_STA);
290 udelay(500);
291 break;
294 mpc_reg_out(&regs->mcr, 0, I2C_STA);
295 udelay(500);
298 return (i == I2C_RETRIES);
301 int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
303 char xaddr[4];
304 struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
305 int ret = -1;
307 xaddr[0] = (addr >> 24) & 0xFF;
308 xaddr[1] = (addr >> 16) & 0xFF;
309 xaddr[2] = (addr >> 8) & 0xFF;
310 xaddr[3] = addr & 0xFF;
312 if (wait_for_bb()) {
313 printf("i2c_read: bus is busy\n");
314 goto Done;
317 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
318 if (do_address(chip, 0)) {
319 printf("i2c_read: failed to address chip\n");
320 goto Done;
323 if (send_bytes(chip, &xaddr[4-alen], alen)) {
324 printf("i2c_read: send_bytes failed\n");
325 goto Done;
328 mpc_reg_out(&regs->mcr, I2C_RSTA, I2C_RSTA);
329 if (do_address(chip, 1)) {
330 printf("i2c_read: failed to address chip\n");
331 goto Done;
334 if (receive_bytes(chip, (char *)buf, len)) {
335 printf("i2c_read: receive_bytes failed\n");
336 goto Done;
339 ret = 0;
340 Done:
341 mpc_reg_out(&regs->mcr, 0, I2C_STA);
342 return ret;
345 int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
347 char xaddr[4];
348 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
349 int ret = -1;
351 xaddr[0] = (addr >> 24) & 0xFF;
352 xaddr[1] = (addr >> 16) & 0xFF;
353 xaddr[2] = (addr >> 8) & 0xFF;
354 xaddr[3] = addr & 0xFF;
356 if (wait_for_bb()) {
357 printf("i2c_write: bus is busy\n");
358 goto Done;
361 mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
362 if (do_address(chip, 0)) {
363 printf("i2c_write: failed to address chip\n");
364 goto Done;
367 if (send_bytes(chip, &xaddr[4-alen], alen)) {
368 printf("i2c_write: send_bytes failed\n");
369 goto Done;
372 if (send_bytes(chip, (char *)buf, len)) {
373 printf("i2c_write: send_bytes failed\n");
374 goto Done;
377 ret = 0;
378 Done:
379 mpc_reg_out(&regs->mcr, 0, I2C_STA);
380 return ret;
383 uchar i2c_reg_read(uchar chip, uchar reg)
385 uchar buf;
387 i2c_read(chip, reg, 1, &buf, 1);
389 return buf;
392 void i2c_reg_write(uchar chip, uchar reg, uchar val)
394 i2c_write(chip, reg, 1, &val, 1);
396 return;
399 #endif /* CONFIG_HARD_I2C */