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
32 #define I2C_BASE MPC5XXX_I2C2
34 #define I2C_BASE MPC5XXX_I2C1
37 #define I2C_TIMEOUT 100
40 static int mpc_reg_in (volatile u32 *reg);
41 static void mpc_reg_out (volatile u32 *reg, int val, int mask);
42 static int wait_for_bb (void);
43 static int wait_for_pin (int *status);
44 static int do_address (uchar chip, char rdwr_flag);
45 static int send_bytes (uchar chip, char *buf, int len);
46 static int receive_bytes (uchar chip, char *buf, int len);
48 static int mpc_reg_in(volatile u32 *reg)
51 __asm__ __volatile__ ("eieio");
54 static void mpc_reg_out(volatile u32 *reg, int val, int mask)
61 tmp = mpc_reg_in(reg);
62 *reg = ((tmp & ~mask) | (val & mask)) << 24;
64 __asm__ __volatile__ ("eieio");
69 static int wait_for_bb(void)
71 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
72 int timeout = I2C_TIMEOUT;
75 status = mpc_reg_in(®s->msr);
77 while (timeout-- && (status & I2C_BB)) {
80 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
81 temp = mpc_reg_in(®s->mdr);
82 mpc_reg_out(®s->mcr, 0, I2C_STA);
83 mpc_reg_out(®s->mcr, 0, 0);
84 mpc_reg_out(®s->mcr, I2C_EN, 0);
87 status = mpc_reg_in(®s->msr);
90 return (status & I2C_BB);
93 static int wait_for_pin(int *status)
95 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
96 int timeout = I2C_TIMEOUT;
98 *status = mpc_reg_in(®s->msr);
100 while (timeout-- && !(*status & I2C_IF)) {
102 *status = mpc_reg_in(®s->msr);
105 if (!(*status & I2C_IF)) {
109 mpc_reg_out(®s->msr, 0, I2C_IF);
114 static int do_address(uchar chip, char rdwr_flag)
116 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
125 mpc_reg_out(®s->mcr, I2C_TX, I2C_TX);
126 mpc_reg_out(®s->mdr, chip, 0);
128 if (wait_for_pin(&status)) {
132 if (status & I2C_RXAK) {
139 static int send_bytes(uchar chip, char *buf, int len)
141 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
145 for (wrcount = 0; wrcount < len; ++wrcount) {
147 mpc_reg_out(®s->mdr, buf[wrcount], 0);
149 if (wait_for_pin(&status)) {
153 if (status & I2C_RXAK) {
159 return !(wrcount == len);
162 static int receive_bytes(uchar chip, char *buf, int len)
164 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
170 mpc_reg_out(®s->mcr, 0, I2C_TX);
172 for (i = 0; i < len; ++i) {
173 buf[rdcount] = mpc_reg_in(®s->mdr);
182 if (wait_for_pin(&status)) {
187 mpc_reg_out(®s->mcr, I2C_TXAK, I2C_TXAK);
188 buf[rdcount++] = mpc_reg_in(®s->mdr);
190 if (wait_for_pin(&status)) {
194 mpc_reg_out(®s->mcr, 0, I2C_TXAK);
199 /**************** I2C API ****************/
201 void i2c_init(int speed, int saddr)
203 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
205 mpc_reg_out(®s->mcr, 0, 0);
206 mpc_reg_out(®s->madr, saddr << 1, 0);
210 mpc_reg_out(®s->mfdr, speed, 0);
214 mpc_reg_out(®s->mcr, I2C_EN, I2C_INIT_MASK);
215 mpc_reg_out(®s->msr, 0, I2C_IF);
220 int i2c_probe(uchar chip)
222 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
225 for (i = 0; i < I2C_RETRIES; i++) {
226 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
228 if (! do_address(chip, 0)) {
229 mpc_reg_out(®s->mcr, 0, I2C_STA);
233 mpc_reg_out(®s->mcr, 0, I2C_STA);
237 return (i == I2C_RETRIES);
240 int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
243 struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
246 xaddr[0] = (addr >> 24) & 0xFF;
247 xaddr[1] = (addr >> 16) & 0xFF;
248 xaddr[2] = (addr >> 8) & 0xFF;
249 xaddr[3] = addr & 0xFF;
252 printf("i2c_read: bus is busy\n");
256 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
257 if (do_address(chip, 0)) {
258 printf("i2c_read: failed to address chip\n");
262 if (send_bytes(chip, &xaddr[4-alen], alen)) {
263 printf("i2c_read: send_bytes failed\n");
267 mpc_reg_out(®s->mcr, I2C_RSTA, I2C_RSTA);
268 if (do_address(chip, 1)) {
269 printf("i2c_read: failed to address chip\n");
273 if (receive_bytes(chip, buf, len)) {
274 printf("i2c_read: receive_bytes failed\n");
280 mpc_reg_out(®s->mcr, 0, I2C_STA);
284 int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
287 struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
290 xaddr[0] = (addr >> 24) & 0xFF;
291 xaddr[1] = (addr >> 16) & 0xFF;
292 xaddr[2] = (addr >> 8) & 0xFF;
293 xaddr[3] = addr & 0xFF;
296 printf("i2c_write: bus is busy\n");
300 mpc_reg_out(®s->mcr, I2C_STA, I2C_STA);
301 if (do_address(chip, 0)) {
302 printf("i2c_write: failed to address chip\n");
306 if (send_bytes(chip, &xaddr[4-alen], alen)) {
307 printf("i2c_write: send_bytes failed\n");
311 if (send_bytes(chip, buf, len)) {
312 printf("i2c_write: send_bytes failed\n");
318 mpc_reg_out(®s->mcr, 0, I2C_STA);
322 uchar i2c_reg_read(uchar chip, uchar reg)
326 i2c_read(chip, reg, 1, &buf, 1);
331 void i2c_reg_write(uchar chip, uchar reg, uchar val)
333 i2c_write(chip, reg, 1, &val, 1);
338 #endif /* CONFIG_HARD_I2C */