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 static void i2c_board_init(struct mv_i2c
*base
)
72 #ifdef CONFIG_SYS_I2C_INIT_BOARD
75 * call board specific i2c bus reset routine before accessing the
76 * environment, which might be in a chip on that bus. For details
77 * about this problem see doc/I2C_Edge_Conditions.
79 * disable I2C controller first, otherwhise it thinks we want to
80 * talk to the slave port...
82 icr
= readl(&base
->icr
);
83 writel(readl(&base
->icr
) & ~(ICR_SCLE
| ICR_IUE
), &base
->icr
);
87 writel(icr
, &base
->icr
);
91 #ifdef CONFIG_I2C_MULTI_BUS
92 static u32 i2c_regs
[CONFIG_MV_I2C_NUM
] = CONFIG_MV_I2C_REG
;
93 static unsigned int bus_initialized
[CONFIG_MV_I2C_NUM
];
94 static unsigned int current_bus
;
96 int i2c_set_bus_num(unsigned int bus
)
98 if ((bus
< 0) || (bus
>= CONFIG_MV_I2C_NUM
)) {
99 printf("Bad bus: %d\n", bus
);
103 base
= (struct mv_i2c
*)i2c_regs
[bus
];
106 if (!bus_initialized
[current_bus
]) {
107 i2c_board_init(base
);
108 bus_initialized
[current_bus
] = 1;
114 unsigned int i2c_get_bus_num(void)
121 * i2c_reset: - reset the host controller
124 static void i2c_reset(void)
126 writel(readl(&base
->icr
) & ~ICR_IUE
, &base
->icr
); /* disable unit */
127 writel(readl(&base
->icr
) | ICR_UR
, &base
->icr
); /* reset the unit */
129 writel(readl(&base
->icr
) & ~ICR_IUE
, &base
->icr
); /* disable unit */
133 writel(CONFIG_SYS_I2C_SLAVE
, &base
->isar
); /* set our slave address */
134 writel(I2C_ICR_INIT
, &base
->icr
); /* set control reg values */
135 writel(I2C_ISR_INIT
, &base
->isr
); /* set clear interrupt bits */
136 writel(readl(&base
->icr
) | ICR_IUE
, &base
->icr
); /* enable unit */
141 * i2c_isr_set_cleared: - wait until certain bits of the I2C status register
142 * are set and cleared
144 * @return: 1 in case of success, 0 means timeout (no match within 10 ms).
146 static int i2c_isr_set_cleared(unsigned long set_mask
,
147 unsigned long cleared_mask
)
149 int timeout
= 1000, isr
;
152 isr
= readl(&base
->isr
);
156 } while (((isr
& set_mask
) != set_mask
)
157 || ((isr
& cleared_mask
) != 0));
163 * i2c_transfer: - Transfer one byte over the i2c bus
165 * This function can tranfer a byte over the i2c bus in both directions.
166 * It is used by the public API functions.
168 * @return: 0: transfer successful
169 * -1: message is empty
170 * -2: transmit timeout
172 * -4: receive timeout
173 * -5: illegal parameters
174 * -6: bus is busy and couldn't be aquired
176 int i2c_transfer(struct i2c_msg
*msg
)
181 goto transfer_error_msg_empty
;
183 switch (msg
->direction
) {
185 /* check if bus is not busy */
186 if (!i2c_isr_set_cleared(0, ISR_IBB
))
187 goto transfer_error_bus_busy
;
189 /* start transmission */
190 writel(readl(&base
->icr
) & ~ICR_START
, &base
->icr
);
191 writel(readl(&base
->icr
) & ~ICR_STOP
, &base
->icr
);
192 writel(msg
->data
, &base
->idbr
);
193 if (msg
->condition
== I2C_COND_START
)
194 writel(readl(&base
->icr
) | ICR_START
, &base
->icr
);
195 if (msg
->condition
== I2C_COND_STOP
)
196 writel(readl(&base
->icr
) | ICR_STOP
, &base
->icr
);
197 if (msg
->acknack
== I2C_ACKNAK_SENDNAK
)
198 writel(readl(&base
->icr
) | ICR_ACKNAK
, &base
->icr
);
199 if (msg
->acknack
== I2C_ACKNAK_SENDACK
)
200 writel(readl(&base
->icr
) & ~ICR_ACKNAK
, &base
->icr
);
201 writel(readl(&base
->icr
) & ~ICR_ALDIE
, &base
->icr
);
202 writel(readl(&base
->icr
) | ICR_TB
, &base
->icr
);
204 /* transmit register empty? */
205 if (!i2c_isr_set_cleared(ISR_ITE
, 0))
206 goto transfer_error_transmit_timeout
;
208 /* clear 'transmit empty' state */
209 writel(readl(&base
->isr
) | ISR_ITE
, &base
->isr
);
211 /* wait for ACK from slave */
212 if (msg
->acknack
== I2C_ACKNAK_WAITACK
)
213 if (!i2c_isr_set_cleared(0, ISR_ACKNAK
))
214 goto transfer_error_ack_missing
;
219 /* check if bus is not busy */
220 if (!i2c_isr_set_cleared(0, ISR_IBB
))
221 goto transfer_error_bus_busy
;
224 writel(readl(&base
->icr
) & ~ICR_START
, &base
->icr
);
225 writel(readl(&base
->icr
) & ~ICR_STOP
, &base
->icr
);
226 if (msg
->condition
== I2C_COND_START
)
227 writel(readl(&base
->icr
) | ICR_START
, &base
->icr
);
228 if (msg
->condition
== I2C_COND_STOP
)
229 writel(readl(&base
->icr
) | ICR_STOP
, &base
->icr
);
230 if (msg
->acknack
== I2C_ACKNAK_SENDNAK
)
231 writel(readl(&base
->icr
) | ICR_ACKNAK
, &base
->icr
);
232 if (msg
->acknack
== I2C_ACKNAK_SENDACK
)
233 writel(readl(&base
->icr
) & ~ICR_ACKNAK
, &base
->icr
);
234 writel(readl(&base
->icr
) & ~ICR_ALDIE
, &base
->icr
);
235 writel(readl(&base
->icr
) | ICR_TB
, &base
->icr
);
237 /* receive register full? */
238 if (!i2c_isr_set_cleared(ISR_IRF
, 0))
239 goto transfer_error_receive_timeout
;
241 msg
->data
= readl(&base
->idbr
);
243 /* clear 'receive empty' state */
244 writel(readl(&base
->isr
) | ISR_IRF
, &base
->isr
);
247 goto transfer_error_illegal_param
;
252 transfer_error_msg_empty
:
253 PRINTD(("i2c_transfer: error: 'msg' is empty\n"));
254 ret
= -1; goto i2c_transfer_finish
;
256 transfer_error_transmit_timeout
:
257 PRINTD(("i2c_transfer: error: transmit timeout\n"));
258 ret
= -2; goto i2c_transfer_finish
;
260 transfer_error_ack_missing
:
261 PRINTD(("i2c_transfer: error: ACK missing\n"));
262 ret
= -3; goto i2c_transfer_finish
;
264 transfer_error_receive_timeout
:
265 PRINTD(("i2c_transfer: error: receive timeout\n"));
266 ret
= -4; goto i2c_transfer_finish
;
268 transfer_error_illegal_param
:
269 PRINTD(("i2c_transfer: error: illegal parameters\n"));
270 ret
= -5; goto i2c_transfer_finish
;
272 transfer_error_bus_busy
:
273 PRINTD(("i2c_transfer: error: bus is busy\n"));
274 ret
= -6; goto i2c_transfer_finish
;
277 PRINTD(("i2c_transfer: ISR: 0x%04x\n", readl(&base
->isr
)));
282 /* ------------------------------------------------------------------------ */
284 /* ------------------------------------------------------------------------ */
285 void i2c_init(int speed
, int slaveaddr
)
287 #ifdef CONFIG_I2C_MULTI_BUS
289 base
= (struct mv_i2c
*)i2c_regs
[current_bus
];
291 base
= (struct mv_i2c
*)CONFIG_MV_I2C_REG
;
294 i2c_board_init(base
);
298 * i2c_probe: - Test if a chip answers for a given i2c address
300 * @chip: address of the chip which is searched for
301 * @return: 0 if a chip was found, -1 otherwhise
303 int i2c_probe(uchar chip
)
309 msg
.condition
= I2C_COND_START
;
310 msg
.acknack
= I2C_ACKNAK_WAITACK
;
311 msg
.direction
= I2C_WRITE
;
312 msg
.data
= (chip
<< 1) + 1;
313 if (i2c_transfer(&msg
))
316 msg
.condition
= I2C_COND_STOP
;
317 msg
.acknack
= I2C_ACKNAK_SENDNAK
;
318 msg
.direction
= I2C_READ
;
320 if (i2c_transfer(&msg
))
327 * i2c_read: - Read multiple bytes from an i2c device
329 * The higher level routines take into account that this function is only
330 * called with len < page length of the device (see configuration file)
332 * @chip: address of the chip which is to be read
333 * @addr: i2c data address within the chip
334 * @alen: length of the i2c data address (1..2 bytes)
335 * @buffer: where to write the data
336 * @len: how much byte do we want to read
337 * @return: 0 in case of success
339 int i2c_read(uchar chip
, uint addr
, int alen
, uchar
*buffer
, int len
)
342 u8 addr_bytes
[3]; /* lowest...highest byte of data address */
344 PRINTD(("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
345 "len=0x%02x)\n", chip
, addr
, alen
, len
));
349 /* dummy chip address write */
350 PRINTD(("i2c_read: dummy chip address write\n"));
351 msg
.condition
= I2C_COND_START
;
352 msg
.acknack
= I2C_ACKNAK_WAITACK
;
353 msg
.direction
= I2C_WRITE
;
354 msg
.data
= (chip
<< 1);
356 if (i2c_transfer(&msg
))
360 * send memory address bytes;
361 * alen defines how much bytes we have to send.
363 /*addr &= ((1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)-1); */
364 addr_bytes
[0] = (u8
)((addr
>> 0) & 0x000000FF);
365 addr_bytes
[1] = (u8
)((addr
>> 8) & 0x000000FF);
366 addr_bytes
[2] = (u8
)((addr
>> 16) & 0x000000FF);
368 while (--alen
>= 0) {
369 PRINTD(("i2c_read: send memory word address byte %1d\n", alen
));
370 msg
.condition
= I2C_COND_NORMAL
;
371 msg
.acknack
= I2C_ACKNAK_WAITACK
;
372 msg
.direction
= I2C_WRITE
;
373 msg
.data
= addr_bytes
[alen
];
374 if (i2c_transfer(&msg
))
378 /* start read sequence */
379 PRINTD(("i2c_read: start read sequence\n"));
380 msg
.condition
= I2C_COND_START
;
381 msg
.acknack
= I2C_ACKNAK_WAITACK
;
382 msg
.direction
= I2C_WRITE
;
383 msg
.data
= (chip
<< 1);
385 if (i2c_transfer(&msg
))
388 /* read bytes; send NACK at last byte */
391 msg
.condition
= I2C_COND_STOP
;
392 msg
.acknack
= I2C_ACKNAK_SENDNAK
;
394 msg
.condition
= I2C_COND_NORMAL
;
395 msg
.acknack
= I2C_ACKNAK_SENDACK
;
398 msg
.direction
= I2C_READ
;
400 if (i2c_transfer(&msg
))
404 PRINTD(("i2c_read: reading byte (0x%08x)=0x%02x\n",
405 (unsigned int)buffer
, *buffer
));
415 * i2c_write: - Write multiple bytes to an i2c device
417 * The higher level routines take into account that this function is only
418 * called with len < page length of the device (see configuration file)
420 * @chip: address of the chip which is to be written
421 * @addr: i2c data address within the chip
422 * @alen: length of the i2c data address (1..2 bytes)
423 * @buffer: where to find the data to be written
424 * @len: how much byte do we want to read
425 * @return: 0 in case of success
427 int i2c_write(uchar chip
, uint addr
, int alen
, uchar
*buffer
, int len
)
430 u8 addr_bytes
[3]; /* lowest...highest byte of data address */
432 PRINTD(("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
433 "len=0x%02x)\n", chip
, addr
, alen
, len
));
437 /* chip address write */
438 PRINTD(("i2c_write: chip address write\n"));
439 msg
.condition
= I2C_COND_START
;
440 msg
.acknack
= I2C_ACKNAK_WAITACK
;
441 msg
.direction
= I2C_WRITE
;
442 msg
.data
= (chip
<< 1);
444 if (i2c_transfer(&msg
))
448 * send memory address bytes;
449 * alen defines how much bytes we have to send.
451 addr_bytes
[0] = (u8
)((addr
>> 0) & 0x000000FF);
452 addr_bytes
[1] = (u8
)((addr
>> 8) & 0x000000FF);
453 addr_bytes
[2] = (u8
)((addr
>> 16) & 0x000000FF);
455 while (--alen
>= 0) {
456 PRINTD(("i2c_write: send memory word address\n"));
457 msg
.condition
= I2C_COND_NORMAL
;
458 msg
.acknack
= I2C_ACKNAK_WAITACK
;
459 msg
.direction
= I2C_WRITE
;
460 msg
.data
= addr_bytes
[alen
];
461 if (i2c_transfer(&msg
))
465 /* write bytes; send NACK at last byte */
467 PRINTD(("i2c_write: writing byte (0x%08x)=0x%02x\n",
468 (unsigned int)buffer
, *buffer
));
471 msg
.condition
= I2C_COND_STOP
;
473 msg
.condition
= I2C_COND_NORMAL
;
475 msg
.acknack
= I2C_ACKNAK_WAITACK
;
476 msg
.direction
= I2C_WRITE
;
477 msg
.data
= *(buffer
++);
479 if (i2c_transfer(&msg
))
487 #endif /* CONFIG_HARD_I2C */