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 DECLARE_GLOBAL_DATA_PTR;
28 #ifdef CONFIG_HARD_I2C
33 #if (CONFIG_SYS_I2C_MODULE == 2)
34 #define I2C_BASE MPC5XXX_I2C2
35 #elif (CONFIG_SYS_I2C_MODULE == 1)
36 #define I2C_BASE MPC5XXX_I2C1
38 #error CONFIG_SYS_I2C_MODULE is not properly configured
41 #define I2C_TIMEOUT 100
44 struct mpc5xxx_i2c_tap {
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)
61 __asm__ __volatile__ ("eieio");
65 static void mpc_reg_out(volatile u32 *reg, int val, int mask)
72 tmp = mpc_reg_in(reg);
73 *reg = ((tmp & ~mask) | (val & mask)) << 24;
75 __asm__ __volatile__ ("eieio");
80 static int wait_for_bb(void)
82 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
83 int timeout = I2C_TIMEOUT;
86 status = mpc_reg_in(®s->msr);
88 while (timeout-- && (status & I2C_BB)) {
91 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
92 temp = mpc_reg_in(®s->mdr);
93 mpc_reg_out(®s->mcr, 0, I2C_STA);
94 mpc_reg_out(®s->mcr, 0, 0);
95 mpc_reg_out(®s->mcr, I2C_EN, 0);
98 status = mpc_reg_in(®s->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(®s->msr);
111 while (timeout-- && !(*status & I2C_IF)) {
113 *status = mpc_reg_in(®s->msr);
116 if (!(*status & I2C_IF)) {
120 mpc_reg_out(®s->msr, 0, I2C_IF);
125 static int do_address(uchar chip, char rdwr_flag)
127 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
136 mpc_reg_out(®s->mcr, I2C_TX, I2C_TX);
137 mpc_reg_out(®s->mdr, chip, 0);
139 if (wait_for_pin(&status)) {
143 if (status & I2C_RXAK) {
150 static int send_bytes(uchar chip, char *buf, int len)
152 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
156 for (wrcount = 0; wrcount < len; ++wrcount) {
158 mpc_reg_out(®s->mdr, buf[wrcount], 0);
160 if (wait_for_pin(&status)) {
164 if (status & I2C_RXAK) {
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;
181 mpc_reg_out(®s->mcr, 0, I2C_TX);
183 for (i = 0; i < len; ++i) {
184 buf[rdcount] = mpc_reg_in(®s->mdr);
193 if (wait_for_pin(&status)) {
198 mpc_reg_out(®s->mcr, I2C_TXAK, I2C_TXAK);
199 buf[rdcount++] = mpc_reg_in(®s->mdr);
201 if (wait_for_pin(&status)) {
205 mpc_reg_out(®s->mcr, 0, I2C_TXAK);
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(®s->mcr, 0, 0);
217 mpc_reg_out(®s->madr, saddr << 1, 0);
221 mpc_reg_out(®s->mfdr, mpc_get_fdr(speed), 0);
225 mpc_reg_out(®s->mcr, I2C_EN, I2C_INIT_MASK);
226 mpc_reg_out(®s->msr, 0, I2C_IF);
231 static int mpc_get_fdr(int speed)
236 ulong best_speed = 0;
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[] = {
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;
263 best_speed = ipb/scl;
268 divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
269 if (gd->flags & GD_FLG_RELOC) {
272 printf("%ld kHz, ", best_speed / 1000);
280 int i2c_probe(uchar chip)
282 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
285 for (i = 0; i < I2C_RETRIES; i++) {
286 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
288 if (! do_address(chip, 0)) {
289 mpc_reg_out(®s->mcr, 0, I2C_STA);
294 mpc_reg_out(®s->mcr, 0, I2C_STA);
298 return (i == I2C_RETRIES);
301 int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
304 struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
307 xaddr[0] = (addr >> 24) & 0xFF;
308 xaddr[1] = (addr >> 16) & 0xFF;
309 xaddr[2] = (addr >> 8) & 0xFF;
310 xaddr[3] = addr & 0xFF;
313 printf("i2c_read: bus is busy\n");
317 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
318 if (do_address(chip, 0)) {
319 printf("i2c_read: failed to address chip\n");
323 if (send_bytes(chip, &xaddr[4-alen], alen)) {
324 printf("i2c_read: send_bytes failed\n");
328 mpc_reg_out(®s->mcr, I2C_RSTA, I2C_RSTA);
329 if (do_address(chip, 1)) {
330 printf("i2c_read: failed to address chip\n");
334 if (receive_bytes(chip, (char *)buf, len)) {
335 printf("i2c_read: receive_bytes failed\n");
341 mpc_reg_out(®s->mcr, 0, I2C_STA);
345 int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
348 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
351 xaddr[0] = (addr >> 24) & 0xFF;
352 xaddr[1] = (addr >> 16) & 0xFF;
353 xaddr[2] = (addr >> 8) & 0xFF;
354 xaddr[3] = addr & 0xFF;
357 printf("i2c_write: bus is busy\n");
361 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
362 if (do_address(chip, 0)) {
363 printf("i2c_write: failed to address chip\n");
367 if (send_bytes(chip, &xaddr[4-alen], alen)) {
368 printf("i2c_write: send_bytes failed\n");
372 if (send_bytes(chip, (char *)buf, len)) {
373 printf("i2c_write: send_bytes failed\n");
379 mpc_reg_out(®s->mcr, 0, I2C_STA);
383 #endif /* CONFIG_HARD_I2C */