3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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 #if (CFG_I2C_MODULE == 2)
32 #define I2C_BASE MPC5XXX_I2C2
33 #elif (CFG_I2C_MODULE == 1)
34 #define I2C_BASE MPC5XXX_I2C1
36 #error CFG_I2C_MODULE is not properly configured
39 #define I2C_TIMEOUT 100
42 struct mpc5xxx_i2c_tap {
47 static int mpc_reg_in (volatile u32 *reg);
48 static void mpc_reg_out (volatile u32 *reg, int val, int mask);
49 static int wait_for_bb (void);
50 static int wait_for_pin (int *status);
51 static int do_address (uchar chip, char rdwr_flag);
52 static int send_bytes (uchar chip, char *buf, int len);
53 static int receive_bytes (uchar chip, char *buf, int len);
54 static int mpc_get_fdr (int);
56 static int mpc_reg_in(volatile u32 *reg)
59 __asm__ __volatile__ ("eieio");
63 static void mpc_reg_out(volatile u32 *reg, int val, int mask)
70 tmp = mpc_reg_in(reg);
71 *reg = ((tmp & ~mask) | (val & mask)) << 24;
73 __asm__ __volatile__ ("eieio");
78 static int wait_for_bb(void)
80 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
81 int timeout = I2C_TIMEOUT;
84 status = mpc_reg_in(®s->msr);
86 while (timeout-- && (status & I2C_BB)) {
89 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
90 temp = mpc_reg_in(®s->mdr);
91 mpc_reg_out(®s->mcr, 0, I2C_STA);
92 mpc_reg_out(®s->mcr, 0, 0);
93 mpc_reg_out(®s->mcr, I2C_EN, 0);
96 status = mpc_reg_in(®s->msr);
99 return (status & I2C_BB);
102 static int wait_for_pin(int *status)
104 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
105 int timeout = I2C_TIMEOUT;
107 *status = mpc_reg_in(®s->msr);
109 while (timeout-- && !(*status & I2C_IF)) {
111 *status = mpc_reg_in(®s->msr);
114 if (!(*status & I2C_IF)) {
118 mpc_reg_out(®s->msr, 0, I2C_IF);
123 static int do_address(uchar chip, char rdwr_flag)
125 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
134 mpc_reg_out(®s->mcr, I2C_TX, I2C_TX);
135 mpc_reg_out(®s->mdr, chip, 0);
137 if (wait_for_pin(&status)) {
141 if (status & I2C_RXAK) {
148 static int send_bytes(uchar chip, char *buf, int len)
150 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
154 for (wrcount = 0; wrcount < len; ++wrcount) {
156 mpc_reg_out(®s->mdr, buf[wrcount], 0);
158 if (wait_for_pin(&status)) {
162 if (status & I2C_RXAK) {
168 return !(wrcount == len);
171 static int receive_bytes(uchar chip, char *buf, int len)
173 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
179 mpc_reg_out(®s->mcr, 0, I2C_TX);
181 for (i = 0; i < len; ++i) {
182 buf[rdcount] = mpc_reg_in(®s->mdr);
191 if (wait_for_pin(&status)) {
196 mpc_reg_out(®s->mcr, I2C_TXAK, I2C_TXAK);
197 buf[rdcount++] = mpc_reg_in(®s->mdr);
199 if (wait_for_pin(&status)) {
203 mpc_reg_out(®s->mcr, 0, I2C_TXAK);
208 /**************** I2C API ****************/
210 void i2c_init(int speed, int saddr)
212 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
214 mpc_reg_out(®s->mcr, 0, 0);
215 mpc_reg_out(®s->madr, saddr << 1, 0);
219 mpc_reg_out(®s->mfdr, mpc_get_fdr(speed), 0);
223 mpc_reg_out(®s->mcr, I2C_EN, I2C_INIT_MASK);
224 mpc_reg_out(®s->msr, 0, I2C_IF);
229 static int mpc_get_fdr(int speed)
231 DECLARE_GLOBAL_DATA_PTR;
235 ulong best_speed = 0;
238 ulong bestmatch = 0xffffffffUL;
239 int best_i = 0, best_j = 0, i, j;
240 int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8};
241 struct mpc5xxx_i2c_tap scltap[] = {
253 for (i = 7; i >= 0; i--) {
254 for (j = 7; j >= 0; j--) {
255 scl = 2 * (scltap[j].scl2tap +
256 (SCL_Tap[i] - 1) * scltap[j].tap2tap + 2);
257 if (ipb <= speed*scl) {
258 if ((speed*scl - ipb) < bestmatch) {
259 bestmatch = speed*scl - ipb;
262 best_speed = ipb/scl;
267 divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
268 if (gd->flags & GD_FLG_RELOC) {
271 printf("%ld kHz, ", best_speed / 1000);
279 int i2c_probe(uchar chip)
281 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
284 for (i = 0; i < I2C_RETRIES; i++) {
285 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
287 if (! do_address(chip, 0)) {
288 mpc_reg_out(®s->mcr, 0, I2C_STA);
293 mpc_reg_out(®s->mcr, 0, I2C_STA);
297 return (i == I2C_RETRIES);
300 int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
303 struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
306 xaddr[0] = (addr >> 24) & 0xFF;
307 xaddr[1] = (addr >> 16) & 0xFF;
308 xaddr[2] = (addr >> 8) & 0xFF;
309 xaddr[3] = addr & 0xFF;
312 printf("i2c_read: bus is busy\n");
316 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
317 if (do_address(chip, 0)) {
318 printf("i2c_read: failed to address chip\n");
322 if (send_bytes(chip, &xaddr[4-alen], alen)) {
323 printf("i2c_read: send_bytes failed\n");
327 mpc_reg_out(®s->mcr, I2C_RSTA, I2C_RSTA);
328 if (do_address(chip, 1)) {
329 printf("i2c_read: failed to address chip\n");
333 if (receive_bytes(chip, (char *)buf, len)) {
334 printf("i2c_read: receive_bytes failed\n");
340 mpc_reg_out(®s->mcr, 0, I2C_STA);
344 int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
347 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
350 xaddr[0] = (addr >> 24) & 0xFF;
351 xaddr[1] = (addr >> 16) & 0xFF;
352 xaddr[2] = (addr >> 8) & 0xFF;
353 xaddr[3] = addr & 0xFF;
356 printf("i2c_write: bus is busy\n");
360 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
361 if (do_address(chip, 0)) {
362 printf("i2c_write: failed to address chip\n");
366 if (send_bytes(chip, &xaddr[4-alen], alen)) {
367 printf("i2c_write: send_bytes failed\n");
371 if (send_bytes(chip, (char *)buf, len)) {
372 printf("i2c_write: send_bytes failed\n");
378 mpc_reg_out(®s->mcr, 0, I2C_STA);
382 uchar i2c_reg_read(uchar chip, uchar reg)
386 i2c_read(chip, reg, 1, &buf, 1);
391 void i2c_reg_write(uchar chip, uchar reg, uchar val)
393 i2c_write(chip, reg, 1, &val, 1);
398 #endif /* CONFIG_HARD_I2C */