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 = (struct mv_i2c *)CONFIG_MV_I2C_REG;
72 * i2c_reset: - reset the host controller
75 static void i2c_reset(void)
77 writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
78 writel(readl(&base->icr) | ICR_UR, &base->icr); /* reset the unit */
80 writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
84 writel(CONFIG_SYS_I2C_SLAVE, &base->isar); /* set our slave address */
85 writel(I2C_ICR_INIT, &base->icr); /* set control reg values */
86 writel(I2C_ISR_INIT, &base->isr); /* set clear interrupt bits */
87 writel(readl(&base->icr) | ICR_IUE, &base->icr); /* enable unit */
92 * i2c_isr_set_cleared: - wait until certain bits of the I2C status register
95 * @return: 1 in case of success, 0 means timeout (no match within 10 ms).
97 static int i2c_isr_set_cleared(unsigned long set_mask,
98 unsigned long cleared_mask)
100 int timeout = 1000, isr;
103 isr = readl(&base->isr);
107 } while (((isr & set_mask) != set_mask)
108 || ((isr & cleared_mask) != 0));
114 * i2c_transfer: - Transfer one byte over the i2c bus
116 * This function can tranfer a byte over the i2c bus in both directions.
117 * It is used by the public API functions.
119 * @return: 0: transfer successful
120 * -1: message is empty
121 * -2: transmit timeout
123 * -4: receive timeout
124 * -5: illegal parameters
125 * -6: bus is busy and couldn't be aquired
127 int i2c_transfer(struct i2c_msg *msg)
132 goto transfer_error_msg_empty;
134 switch (msg->direction) {
136 /* check if bus is not busy */
137 if (!i2c_isr_set_cleared(0, ISR_IBB))
138 goto transfer_error_bus_busy;
140 /* start transmission */
141 writel(readl(&base->icr) & ~ICR_START, &base->icr);
142 writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
143 writel(msg->data, &base->idbr);
144 if (msg->condition == I2C_COND_START)
145 writel(readl(&base->icr) | ICR_START, &base->icr);
146 if (msg->condition == I2C_COND_STOP)
147 writel(readl(&base->icr) | ICR_STOP, &base->icr);
148 if (msg->acknack == I2C_ACKNAK_SENDNAK)
149 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
150 if (msg->acknack == I2C_ACKNAK_SENDACK)
151 writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
152 writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
153 writel(readl(&base->icr) | ICR_TB, &base->icr);
155 /* transmit register empty? */
156 if (!i2c_isr_set_cleared(ISR_ITE, 0))
157 goto transfer_error_transmit_timeout;
159 /* clear 'transmit empty' state */
160 writel(readl(&base->isr) | ISR_ITE, &base->isr);
162 /* wait for ACK from slave */
163 if (msg->acknack == I2C_ACKNAK_WAITACK)
164 if (!i2c_isr_set_cleared(0, ISR_ACKNAK))
165 goto transfer_error_ack_missing;
170 /* check if bus is not busy */
171 if (!i2c_isr_set_cleared(0, ISR_IBB))
172 goto transfer_error_bus_busy;
175 writel(readl(&base->icr) & ~ICR_START, &base->icr);
176 writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
177 if (msg->condition == I2C_COND_START)
178 writel(readl(&base->icr) | ICR_START, &base->icr);
179 if (msg->condition == I2C_COND_STOP)
180 writel(readl(&base->icr) | ICR_STOP, &base->icr);
181 if (msg->acknack == I2C_ACKNAK_SENDNAK)
182 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
183 if (msg->acknack == I2C_ACKNAK_SENDACK)
184 writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
185 writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
186 writel(readl(&base->icr) | ICR_TB, &base->icr);
188 /* receive register full? */
189 if (!i2c_isr_set_cleared(ISR_IRF, 0))
190 goto transfer_error_receive_timeout;
192 msg->data = readl(&base->idbr);
194 /* clear 'receive empty' state */
195 writel(readl(&base->isr) | ISR_IRF, &base->isr);
198 goto transfer_error_illegal_param;
203 transfer_error_msg_empty:
204 PRINTD(("i2c_transfer: error: 'msg' is empty\n"));
205 ret = -1; goto i2c_transfer_finish;
207 transfer_error_transmit_timeout:
208 PRINTD(("i2c_transfer: error: transmit timeout\n"));
209 ret = -2; goto i2c_transfer_finish;
211 transfer_error_ack_missing:
212 PRINTD(("i2c_transfer: error: ACK missing\n"));
213 ret = -3; goto i2c_transfer_finish;
215 transfer_error_receive_timeout:
216 PRINTD(("i2c_transfer: error: receive timeout\n"));
217 ret = -4; goto i2c_transfer_finish;
219 transfer_error_illegal_param:
220 PRINTD(("i2c_transfer: error: illegal parameters\n"));
221 ret = -5; goto i2c_transfer_finish;
223 transfer_error_bus_busy:
224 PRINTD(("i2c_transfer: error: bus is busy\n"));
225 ret = -6; goto i2c_transfer_finish;
228 PRINTD(("i2c_transfer: ISR: 0x%04x\n", ISR));
233 /* ------------------------------------------------------------------------ */
235 /* ------------------------------------------------------------------------ */
236 void i2c_init(int speed, int slaveaddr)
238 #ifdef CONFIG_SYS_I2C_INIT_BOARD
241 * call board specific i2c bus reset routine before accessing the
242 * environment, which might be in a chip on that bus. For details
243 * about this problem see doc/I2C_Edge_Conditions.
245 * disable I2C controller first, otherwhise it thinks we want to
246 * talk to the slave port...
248 icr = readl(&base->icr);
249 writel(readl(&base->icr) & ~(ICR_SCLE | ICR_IUE), &base->icr);
253 writel(icr, &base->icr);
258 * i2c_probe: - Test if a chip answers for a given i2c address
260 * @chip: address of the chip which is searched for
261 * @return: 0 if a chip was found, -1 otherwhise
263 int i2c_probe(uchar chip)
269 msg.condition = I2C_COND_START;
270 msg.acknack = I2C_ACKNAK_WAITACK;
271 msg.direction = I2C_WRITE;
272 msg.data = (chip << 1) + 1;
273 if (i2c_transfer(&msg))
276 msg.condition = I2C_COND_STOP;
277 msg.acknack = I2C_ACKNAK_SENDNAK;
278 msg.direction = I2C_READ;
280 if (i2c_transfer(&msg))
287 * i2c_read: - Read multiple bytes from an i2c device
289 * The higher level routines take into account that this function is only
290 * called with len < page length of the device (see configuration file)
292 * @chip: address of the chip which is to be read
293 * @addr: i2c data address within the chip
294 * @alen: length of the i2c data address (1..2 bytes)
295 * @buffer: where to write the data
296 * @len: how much byte do we want to read
297 * @return: 0 in case of success
299 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
302 u8 addr_bytes[3]; /* lowest...highest byte of data address */
304 PRINTD(("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
305 "len=0x%02x)\n", chip, addr, alen, len));
309 /* dummy chip address write */
310 PRINTD(("i2c_read: dummy chip address write\n"));
311 msg.condition = I2C_COND_START;
312 msg.acknack = I2C_ACKNAK_WAITACK;
313 msg.direction = I2C_WRITE;
314 msg.data = (chip << 1);
316 if (i2c_transfer(&msg))
320 * send memory address bytes;
321 * alen defines how much bytes we have to send.
323 /*addr &= ((1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)-1); */
324 addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
325 addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
326 addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
328 while (--alen >= 0) {
329 PRINTD(("i2c_read: send memory word address byte %1d\n", alen));
330 msg.condition = I2C_COND_NORMAL;
331 msg.acknack = I2C_ACKNAK_WAITACK;
332 msg.direction = I2C_WRITE;
333 msg.data = addr_bytes[alen];
334 if (i2c_transfer(&msg))
338 /* start read sequence */
339 PRINTD(("i2c_read: start read sequence\n"));
340 msg.condition = I2C_COND_START;
341 msg.acknack = I2C_ACKNAK_WAITACK;
342 msg.direction = I2C_WRITE;
343 msg.data = (chip << 1);
345 if (i2c_transfer(&msg))
348 /* read bytes; send NACK at last byte */
351 msg.condition = I2C_COND_STOP;
352 msg.acknack = I2C_ACKNAK_SENDNAK;
354 msg.condition = I2C_COND_NORMAL;
355 msg.acknack = I2C_ACKNAK_SENDACK;
358 msg.direction = I2C_READ;
360 if (i2c_transfer(&msg))
364 PRINTD(("i2c_read: reading byte (0x%08x)=0x%02x\n",
365 (unsigned int)buffer, *buffer));
375 * i2c_write: - Write multiple bytes to an i2c device
377 * The higher level routines take into account that this function is only
378 * called with len < page length of the device (see configuration file)
380 * @chip: address of the chip which is to be written
381 * @addr: i2c data address within the chip
382 * @alen: length of the i2c data address (1..2 bytes)
383 * @buffer: where to find the data to be written
384 * @len: how much byte do we want to read
385 * @return: 0 in case of success
387 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
390 u8 addr_bytes[3]; /* lowest...highest byte of data address */
392 PRINTD(("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
393 "len=0x%02x)\n", chip, addr, alen, len));
397 /* chip address write */
398 PRINTD(("i2c_write: chip address write\n"));
399 msg.condition = I2C_COND_START;
400 msg.acknack = I2C_ACKNAK_WAITACK;
401 msg.direction = I2C_WRITE;
402 msg.data = (chip << 1);
404 if (i2c_transfer(&msg))
408 * send memory address bytes;
409 * alen defines how much bytes we have to send.
411 addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF);
412 addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF);
413 addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF);
415 while (--alen >= 0) {
416 PRINTD(("i2c_write: send memory word address\n"));
417 msg.condition = I2C_COND_NORMAL;
418 msg.acknack = I2C_ACKNAK_WAITACK;
419 msg.direction = I2C_WRITE;
420 msg.data = addr_bytes[alen];
421 if (i2c_transfer(&msg))
425 /* write bytes; send NACK at last byte */
427 PRINTD(("i2c_write: writing byte (0x%08x)=0x%02x\n",
428 (unsigned int)buffer, *buffer));
431 msg.condition = I2C_COND_STOP;
433 msg.condition = I2C_COND_NORMAL;
435 msg.acknack = I2C_ACKNAK_WAITACK;
436 msg.direction = I2C_WRITE;
437 msg.data = *(buffer++);
439 if (i2c_transfer(&msg))
447 #endif /* CONFIG_HARD_I2C */