3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
5 * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Marius Groeger <mgroeger@sysgo.de>
8 * (C) Copyright 2003 Pengutronix e.K.
9 * Robert Schwebel <r.schwebel@pengutronix.de>
11 * (C) Copyright 2011 Marvell Inc.
12 * Lei Wen <leiwen@marvell.com>
14 * See file CREDITS for list of people who contributed to this
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * Back ported to the 8xx platform (from the 8260 platform) by
33 * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
39 #ifdef CONFIG_HARD_I2C
44 #define PRINTD(x) printf x
49 /* All transfers are described by this data structure */
69 static struct mv_i2c *base;
70 #ifdef CONFIG_I2C_MULTI_BUS
71 static u32 i2c_regs[CONFIG_MV_I2C_NUM] = CONFIG_MV_I2C_REG;
72 static unsigned int bus_initialized[CONFIG_MV_I2C_NUM];
73 static unsigned int current_bus;
75 int i2c_set_bus_num(unsigned int bus)
77 if ((bus < 0) || (bus >= CONFIG_MV_I2C_NUM)) {
78 printf("Bad bus: %d\n", bus);
82 base = (struct mv_i2c *)i2c_regs[bus];
85 if (!bus_initialized[current_bus]) {
86 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
87 bus_initialized[current_bus] = 1;
93 unsigned int i2c_get_bus_num(void)
100 * i2c_reset: - reset the host controller
103 static void i2c_reset(void)
105 writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
106 writel(readl(&base->icr) | ICR_UR, &base->icr); /* reset the unit */
108 writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
112 writel(CONFIG_SYS_I2C_SLAVE, &base->isar); /* set our slave address */
113 writel(I2C_ICR_INIT, &base->icr); /* set control reg values */
114 writel(I2C_ISR_INIT, &base->isr); /* set clear interrupt bits */
115 writel(readl(&base->icr) | ICR_IUE, &base->icr); /* enable unit */
120 * i2c_isr_set_cleared: - wait until certain bits of the I2C status register
121 * are set and cleared
123 * @return: 1 in case of success, 0 means timeout (no match within 10 ms).
125 static int i2c_isr_set_cleared(unsigned long set_mask,
126 unsigned long cleared_mask)
128 int timeout = 1000, isr;
131 isr = readl(&base->isr);
135 } while (((isr & set_mask) != set_mask)
136 || ((isr & cleared_mask) != 0));
142 * i2c_transfer: - Transfer one byte over the i2c bus
144 * This function can tranfer a byte over the i2c bus in both directions.
145 * It is used by the public API functions.
147 * @return: 0: transfer successful
148 * -1: message is empty
149 * -2: transmit timeout
151 * -4: receive timeout
152 * -5: illegal parameters
153 * -6: bus is busy and couldn't be aquired
155 int i2c_transfer(struct i2c_msg *msg)
160 goto transfer_error_msg_empty;
162 switch (msg->direction) {
164 /* check if bus is not busy */
165 if (!i2c_isr_set_cleared(0, ISR_IBB))
166 goto transfer_error_bus_busy;
168 /* start transmission */
169 writel(readl(&base->icr) & ~ICR_START, &base->icr);
170 writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
171 writel(msg->data, &base->idbr);
172 if (msg->condition == I2C_COND_START)
173 writel(readl(&base->icr) | ICR_START, &base->icr);
174 if (msg->condition == I2C_COND_STOP)
175 writel(readl(&base->icr) | ICR_STOP, &base->icr);
176 if (msg->acknack == I2C_ACKNAK_SENDNAK)
177 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
178 if (msg->acknack == I2C_ACKNAK_SENDACK)
179 writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
180 writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
181 writel(readl(&base->icr) | ICR_TB, &base->icr);
183 /* transmit register empty? */
184 if (!i2c_isr_set_cleared(ISR_ITE, 0))
185 goto transfer_error_transmit_timeout;
187 /* clear 'transmit empty' state */
188 writel(readl(&base->isr) | ISR_ITE, &base->isr);
190 /* wait for ACK from slave */
191 if (msg->acknack == I2C_ACKNAK_WAITACK)
192 if (!i2c_isr_set_cleared(0, ISR_ACKNAK))
193 goto transfer_error_ack_missing;
198 /* check if bus is not busy */
199 if (!i2c_isr_set_cleared(0, ISR_IBB))
200 goto transfer_error_bus_busy;
203 writel(readl(&base->icr) & ~ICR_START, &base->icr);
204 writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
205 if (msg->condition == I2C_COND_START)
206 writel(readl(&base->icr) | ICR_START, &base->icr);
207 if (msg->condition == I2C_COND_STOP)
208 writel(readl(&base->icr) | ICR_STOP, &base->icr);
209 if (msg->acknack == I2C_ACKNAK_SENDNAK)
210 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
211 if (msg->acknack == I2C_ACKNAK_SENDACK)
212 writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
213 writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
214 writel(readl(&base->icr) | ICR_TB, &base->icr);
216 /* receive register full? */
217 if (!i2c_isr_set_cleared(ISR_IRF, 0))
218 goto transfer_error_receive_timeout;
220 msg->data = readl(&base->idbr);
222 /* clear 'receive empty' state */
223 writel(readl(&base->isr) | ISR_IRF, &base->isr);
226 goto transfer_error_illegal_param;
231 transfer_error_msg_empty:
232 PRINTD(("i2c_transfer: error: 'msg' is empty\n"));
233 ret = -1; goto i2c_transfer_finish;
235 transfer_error_transmit_timeout:
236 PRINTD(("i2c_transfer: error: transmit timeout\n"));
237 ret = -2; goto i2c_transfer_finish;
239 transfer_error_ack_missing:
240 PRINTD(("i2c_transfer: error: ACK missing\n"));
241 ret = -3; goto i2c_transfer_finish;
243 transfer_error_receive_timeout:
244 PRINTD(("i2c_transfer: error: receive timeout\n"));
245 ret = -4; goto i2c_transfer_finish;
247 transfer_error_illegal_param:
248 PRINTD(("i2c_transfer: error: illegal parameters\n"));
249 ret = -5; goto i2c_transfer_finish;
251 transfer_error_bus_busy:
252 PRINTD(("i2c_transfer: error: bus is busy\n"));
253 ret = -6; goto i2c_transfer_finish;
256 PRINTD(("i2c_transfer: ISR: 0x%04x\n", ISR));
261 /* ------------------------------------------------------------------------ */
263 /* ------------------------------------------------------------------------ */
264 void i2c_init(int speed, int slaveaddr)
266 #ifdef CONFIG_I2C_MULTI_BUS
267 base = (struct mv_i2c *)i2c_regs[current_bus];
269 base = (struct mv_i2c *)CONFIG_MV_I2C_REG;
272 #ifdef CONFIG_SYS_I2C_INIT_BOARD
275 * call board specific i2c bus reset routine before accessing the
276 * environment, which might be in a chip on that bus. For details
277 * about this problem see doc/I2C_Edge_Conditions.
279 * disable I2C controller first, otherwhise it thinks we want to
280 * talk to the slave port...
282 icr = readl(&base->icr);
283 writel(readl(&base->icr) & ~(ICR_SCLE | ICR_IUE), &base->icr);
287 writel(icr, &base->icr);
292 * i2c_probe: - Test if a chip answers for a given i2c address
294 * @chip: address of the chip which is searched for
295 * @return: 0 if a chip was found, -1 otherwhise
297 int i2c_probe(uchar chip)
303 msg.condition = I2C_COND_START;
304 msg.acknack = I2C_ACKNAK_WAITACK;
305 msg.direction = I2C_WRITE;
306 msg.data = (chip << 1) + 1;
307 if (i2c_transfer(&msg))
310 msg.condition = I2C_COND_STOP;
311 msg.acknack = I2C_ACKNAK_SENDNAK;
312 msg.direction = I2C_READ;
314 if (i2c_transfer(&msg))
321 * i2c_read: - Read multiple bytes from an i2c device
323 * The higher level routines take into account that this function is only
324 * called with len < page length of the device (see configuration file)
326 * @chip: address of the chip which is to be read
327 * @addr: i2c data address within the chip
328 * @alen: length of the i2c data address (1..2 bytes)
329 * @buffer: where to write the data
330 * @len: how much byte do we want to read
331 * @return: 0 in case of success
333 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
336 u8 addr_bytes[3]; /* lowest...highest byte of data address */
338 PRINTD(("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
339 "len=0x%02x)\n", chip, addr, alen, len));
343 /* dummy chip address write */
344 PRINTD(("i2c_read: dummy chip address write\n"));
345 msg.condition = I2C_COND_START;
346 msg.acknack = I2C_ACKNAK_WAITACK;
347 msg.direction = I2C_WRITE;
348 msg.data = (chip << 1);
350 if (i2c_transfer(&msg))
354 * send memory address bytes;
355 * alen defines how much bytes we have to send.
357 /*addr &= ((1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)-1); */
358 addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
359 addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
360 addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
362 while (--alen >= 0) {
363 PRINTD(("i2c_read: send memory word address byte %1d\n", alen));
364 msg.condition = I2C_COND_NORMAL;
365 msg.acknack = I2C_ACKNAK_WAITACK;
366 msg.direction = I2C_WRITE;
367 msg.data = addr_bytes[alen];
368 if (i2c_transfer(&msg))
372 /* start read sequence */
373 PRINTD(("i2c_read: start read sequence\n"));
374 msg.condition = I2C_COND_START;
375 msg.acknack = I2C_ACKNAK_WAITACK;
376 msg.direction = I2C_WRITE;
377 msg.data = (chip << 1);
379 if (i2c_transfer(&msg))
382 /* read bytes; send NACK at last byte */
385 msg.condition = I2C_COND_STOP;
386 msg.acknack = I2C_ACKNAK_SENDNAK;
388 msg.condition = I2C_COND_NORMAL;
389 msg.acknack = I2C_ACKNAK_SENDACK;
392 msg.direction = I2C_READ;
394 if (i2c_transfer(&msg))
398 PRINTD(("i2c_read: reading byte (0x%08x)=0x%02x\n",
399 (unsigned int)buffer, *buffer));
409 * i2c_write: - Write multiple bytes to an i2c device
411 * The higher level routines take into account that this function is only
412 * called with len < page length of the device (see configuration file)
414 * @chip: address of the chip which is to be written
415 * @addr: i2c data address within the chip
416 * @alen: length of the i2c data address (1..2 bytes)
417 * @buffer: where to find the data to be written
418 * @len: how much byte do we want to read
419 * @return: 0 in case of success
421 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
424 u8 addr_bytes[3]; /* lowest...highest byte of data address */
426 PRINTD(("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
427 "len=0x%02x)\n", chip, addr, alen, len));
431 /* chip address write */
432 PRINTD(("i2c_write: chip address write\n"));
433 msg.condition = I2C_COND_START;
434 msg.acknack = I2C_ACKNAK_WAITACK;
435 msg.direction = I2C_WRITE;
436 msg.data = (chip << 1);
438 if (i2c_transfer(&msg))
442 * send memory address bytes;
443 * alen defines how much bytes we have to send.
445 addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
446 addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
447 addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
449 while (--alen >= 0) {
450 PRINTD(("i2c_write: send memory word address\n"));
451 msg.condition = I2C_COND_NORMAL;
452 msg.acknack = I2C_ACKNAK_WAITACK;
453 msg.direction = I2C_WRITE;
454 msg.data = addr_bytes[alen];
455 if (i2c_transfer(&msg))
459 /* write bytes; send NACK at last byte */
461 PRINTD(("i2c_write: writing byte (0x%08x)=0x%02x\n",
462 (unsigned int)buffer, *buffer));
465 msg.condition = I2C_COND_STOP;
467 msg.condition = I2C_COND_NORMAL;
469 msg.acknack = I2C_ACKNAK_WAITACK;
470 msg.direction = I2C_WRITE;
471 msg.data = *(buffer++);
473 if (i2c_transfer(&msg))
481 #endif /* CONFIG_HARD_I2C */