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");
62 static void mpc_reg_out(volatile u32 *reg, int val, int mask)
69 tmp = mpc_reg_in(reg);
70 *reg = ((tmp & ~mask) | (val & mask)) << 24;
72 __asm__ __volatile__ ("eieio");
77 static int wait_for_bb(void)
79 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
80 int timeout = I2C_TIMEOUT;
83 status = mpc_reg_in(®s->msr);
85 while (timeout-- && (status & I2C_BB)) {
88 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
89 temp = mpc_reg_in(®s->mdr);
90 mpc_reg_out(®s->mcr, 0, I2C_STA);
91 mpc_reg_out(®s->mcr, 0, 0);
92 mpc_reg_out(®s->mcr, I2C_EN, 0);
95 status = mpc_reg_in(®s->msr);
98 return (status & I2C_BB);
101 static int wait_for_pin(int *status)
103 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
104 int timeout = I2C_TIMEOUT;
106 *status = mpc_reg_in(®s->msr);
108 while (timeout-- && !(*status & I2C_IF)) {
110 *status = mpc_reg_in(®s->msr);
113 if (!(*status & I2C_IF)) {
117 mpc_reg_out(®s->msr, 0, I2C_IF);
122 static int do_address(uchar chip, char rdwr_flag)
124 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
133 mpc_reg_out(®s->mcr, I2C_TX, I2C_TX);
134 mpc_reg_out(®s->mdr, chip, 0);
136 if (wait_for_pin(&status)) {
140 if (status & I2C_RXAK) {
147 static int send_bytes(uchar chip, char *buf, int len)
149 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
153 for (wrcount = 0; wrcount < len; ++wrcount) {
155 mpc_reg_out(®s->mdr, buf[wrcount], 0);
157 if (wait_for_pin(&status)) {
161 if (status & I2C_RXAK) {
167 return !(wrcount == len);
170 static int receive_bytes(uchar chip, char *buf, int len)
172 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
178 mpc_reg_out(®s->mcr, 0, I2C_TX);
180 for (i = 0; i < len; ++i) {
181 buf[rdcount] = mpc_reg_in(®s->mdr);
190 if (wait_for_pin(&status)) {
195 mpc_reg_out(®s->mcr, I2C_TXAK, I2C_TXAK);
196 buf[rdcount++] = mpc_reg_in(®s->mdr);
198 if (wait_for_pin(&status)) {
202 mpc_reg_out(®s->mcr, 0, I2C_TXAK);
207 /**************** I2C API ****************/
209 void i2c_init(int speed, int saddr)
211 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
213 mpc_reg_out(®s->mcr, 0, 0);
214 mpc_reg_out(®s->madr, saddr << 1, 0);
218 mpc_reg_out(®s->mfdr, mpc_get_fdr(speed), 0);
222 mpc_reg_out(®s->mcr, I2C_EN, I2C_INIT_MASK);
223 mpc_reg_out(®s->msr, 0, I2C_IF);
228 static int mpc_get_fdr(int speed)
230 DECLARE_GLOBAL_DATA_PTR;
234 ulong best_speed = 0;
237 ulong bestmatch = 0xffffffffUL;
238 int best_i = 0, best_j = 0, i, j;
239 int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8};
240 struct mpc5xxx_i2c_tap scltap[] = {
252 for (i = 7; i >= 0; i--) {
253 for (j = 7; j >= 0; j--) {
254 scl = 2 * (scltap[j].scl2tap +
255 (SCL_Tap[i] - 1) * scltap[j].tap2tap + 2);
256 if (ipb <= speed*scl) {
257 if ((speed*scl - ipb) < bestmatch) {
258 bestmatch = speed*scl - ipb;
261 best_speed = ipb/scl;
266 divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
267 if (gd->flags & GD_FLG_RELOC) {
270 printf("%ld kHz, ", best_speed / 1000);
278 int i2c_probe(uchar chip)
280 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
283 for (i = 0; i < I2C_RETRIES; i++) {
284 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
286 if (! do_address(chip, 0)) {
287 mpc_reg_out(®s->mcr, 0, I2C_STA);
292 mpc_reg_out(®s->mcr, 0, I2C_STA);
296 return (i == I2C_RETRIES);
299 int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
302 struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
305 xaddr[0] = (addr >> 24) & 0xFF;
306 xaddr[1] = (addr >> 16) & 0xFF;
307 xaddr[2] = (addr >> 8) & 0xFF;
308 xaddr[3] = addr & 0xFF;
311 printf("i2c_read: bus is busy\n");
315 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
316 if (do_address(chip, 0)) {
317 printf("i2c_read: failed to address chip\n");
321 if (send_bytes(chip, &xaddr[4-alen], alen)) {
322 printf("i2c_read: send_bytes failed\n");
326 mpc_reg_out(®s->mcr, I2C_RSTA, I2C_RSTA);
327 if (do_address(chip, 1)) {
328 printf("i2c_read: failed to address chip\n");
332 if (receive_bytes(chip, buf, len)) {
333 printf("i2c_read: receive_bytes failed\n");
339 mpc_reg_out(®s->mcr, 0, I2C_STA);
343 int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
346 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
349 xaddr[0] = (addr >> 24) & 0xFF;
350 xaddr[1] = (addr >> 16) & 0xFF;
351 xaddr[2] = (addr >> 8) & 0xFF;
352 xaddr[3] = addr & 0xFF;
355 printf("i2c_write: bus is busy\n");
359 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
360 if (do_address(chip, 0)) {
361 printf("i2c_write: failed to address chip\n");
365 if (send_bytes(chip, &xaddr[4-alen], alen)) {
366 printf("i2c_write: send_bytes failed\n");
370 if (send_bytes(chip, buf, len)) {
371 printf("i2c_write: send_bytes failed\n");
377 mpc_reg_out(®s->mcr, 0, I2C_STA);
381 uchar i2c_reg_read(uchar chip, uchar reg)
385 i2c_read(chip, reg, 1, &buf, 1);
390 void i2c_reg_write(uchar chip, uchar reg, uchar val)
392 i2c_write(chip, reg, 1, &val, 1);
397 #endif /* CONFIG_HARD_I2C */