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 * SPDX-License-Identifier: GPL-2.0+
16 * Back ported to the 8xx platform (from the 8260 platform) by
17 * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
23 #ifdef CONFIG_HARD_I2C
28 #define PRINTD(x) printf x
33 /* All transfers are described by this data structure */
53 static struct mv_i2c
*base
;
54 static void i2c_board_init(struct mv_i2c
*base
)
56 #ifdef CONFIG_SYS_I2C_INIT_BOARD
59 * call board specific i2c bus reset routine before accessing the
60 * environment, which might be in a chip on that bus. For details
61 * about this problem see doc/I2C_Edge_Conditions.
63 * disable I2C controller first, otherwhise it thinks we want to
64 * talk to the slave port...
66 icr
= readl(&base
->icr
);
67 writel(readl(&base
->icr
) & ~(ICR_SCLE
| ICR_IUE
), &base
->icr
);
71 writel(icr
, &base
->icr
);
75 #ifdef CONFIG_I2C_MULTI_BUS
76 static u32 i2c_regs
[CONFIG_MV_I2C_NUM
] = CONFIG_MV_I2C_REG
;
77 static unsigned int bus_initialized
[CONFIG_MV_I2C_NUM
];
78 static unsigned int current_bus
;
80 int i2c_set_bus_num(unsigned int bus
)
82 if ((bus
< 0) || (bus
>= CONFIG_MV_I2C_NUM
)) {
83 printf("Bad bus: %d\n", bus
);
87 base
= (struct mv_i2c
*)i2c_regs
[bus
];
90 if (!bus_initialized
[current_bus
]) {
92 bus_initialized
[current_bus
] = 1;
98 unsigned int i2c_get_bus_num(void)
105 * i2c_reset: - reset the host controller
108 static void i2c_reset(void)
110 writel(readl(&base
->icr
) & ~ICR_IUE
, &base
->icr
); /* disable unit */
111 writel(readl(&base
->icr
) | ICR_UR
, &base
->icr
); /* reset the unit */
113 writel(readl(&base
->icr
) & ~ICR_IUE
, &base
->icr
); /* disable unit */
117 writel(CONFIG_SYS_I2C_SLAVE
, &base
->isar
); /* set our slave address */
118 writel(I2C_ICR_INIT
, &base
->icr
); /* set control reg values */
119 writel(I2C_ISR_INIT
, &base
->isr
); /* set clear interrupt bits */
120 writel(readl(&base
->icr
) | ICR_IUE
, &base
->icr
); /* enable unit */
125 * i2c_isr_set_cleared: - wait until certain bits of the I2C status register
126 * are set and cleared
128 * @return: 1 in case of success, 0 means timeout (no match within 10 ms).
130 static int i2c_isr_set_cleared(unsigned long set_mask
,
131 unsigned long cleared_mask
)
133 int timeout
= 1000, isr
;
136 isr
= readl(&base
->isr
);
140 } while (((isr
& set_mask
) != set_mask
)
141 || ((isr
& cleared_mask
) != 0));
147 * i2c_transfer: - Transfer one byte over the i2c bus
149 * This function can tranfer a byte over the i2c bus in both directions.
150 * It is used by the public API functions.
152 * @return: 0: transfer successful
153 * -1: message is empty
154 * -2: transmit timeout
156 * -4: receive timeout
157 * -5: illegal parameters
158 * -6: bus is busy and couldn't be aquired
160 int i2c_transfer(struct i2c_msg
*msg
)
165 goto transfer_error_msg_empty
;
167 switch (msg
->direction
) {
169 /* check if bus is not busy */
170 if (!i2c_isr_set_cleared(0, ISR_IBB
))
171 goto transfer_error_bus_busy
;
173 /* start transmission */
174 writel(readl(&base
->icr
) & ~ICR_START
, &base
->icr
);
175 writel(readl(&base
->icr
) & ~ICR_STOP
, &base
->icr
);
176 writel(msg
->data
, &base
->idbr
);
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 /* transmit register empty? */
189 if (!i2c_isr_set_cleared(ISR_ITE
, 0))
190 goto transfer_error_transmit_timeout
;
192 /* clear 'transmit empty' state */
193 writel(readl(&base
->isr
) | ISR_ITE
, &base
->isr
);
195 /* wait for ACK from slave */
196 if (msg
->acknack
== I2C_ACKNAK_WAITACK
)
197 if (!i2c_isr_set_cleared(0, ISR_ACKNAK
))
198 goto transfer_error_ack_missing
;
203 /* check if bus is not busy */
204 if (!i2c_isr_set_cleared(0, ISR_IBB
))
205 goto transfer_error_bus_busy
;
208 writel(readl(&base
->icr
) & ~ICR_START
, &base
->icr
);
209 writel(readl(&base
->icr
) & ~ICR_STOP
, &base
->icr
);
210 if (msg
->condition
== I2C_COND_START
)
211 writel(readl(&base
->icr
) | ICR_START
, &base
->icr
);
212 if (msg
->condition
== I2C_COND_STOP
)
213 writel(readl(&base
->icr
) | ICR_STOP
, &base
->icr
);
214 if (msg
->acknack
== I2C_ACKNAK_SENDNAK
)
215 writel(readl(&base
->icr
) | ICR_ACKNAK
, &base
->icr
);
216 if (msg
->acknack
== I2C_ACKNAK_SENDACK
)
217 writel(readl(&base
->icr
) & ~ICR_ACKNAK
, &base
->icr
);
218 writel(readl(&base
->icr
) & ~ICR_ALDIE
, &base
->icr
);
219 writel(readl(&base
->icr
) | ICR_TB
, &base
->icr
);
221 /* receive register full? */
222 if (!i2c_isr_set_cleared(ISR_IRF
, 0))
223 goto transfer_error_receive_timeout
;
225 msg
->data
= readl(&base
->idbr
);
227 /* clear 'receive empty' state */
228 writel(readl(&base
->isr
) | ISR_IRF
, &base
->isr
);
231 goto transfer_error_illegal_param
;
236 transfer_error_msg_empty
:
237 PRINTD(("i2c_transfer: error: 'msg' is empty\n"));
238 ret
= -1; goto i2c_transfer_finish
;
240 transfer_error_transmit_timeout
:
241 PRINTD(("i2c_transfer: error: transmit timeout\n"));
242 ret
= -2; goto i2c_transfer_finish
;
244 transfer_error_ack_missing
:
245 PRINTD(("i2c_transfer: error: ACK missing\n"));
246 ret
= -3; goto i2c_transfer_finish
;
248 transfer_error_receive_timeout
:
249 PRINTD(("i2c_transfer: error: receive timeout\n"));
250 ret
= -4; goto i2c_transfer_finish
;
252 transfer_error_illegal_param
:
253 PRINTD(("i2c_transfer: error: illegal parameters\n"));
254 ret
= -5; goto i2c_transfer_finish
;
256 transfer_error_bus_busy
:
257 PRINTD(("i2c_transfer: error: bus is busy\n"));
258 ret
= -6; goto i2c_transfer_finish
;
261 PRINTD(("i2c_transfer: ISR: 0x%04x\n", readl(&base
->isr
)));
266 /* ------------------------------------------------------------------------ */
268 /* ------------------------------------------------------------------------ */
269 void i2c_init(int speed
, int slaveaddr
)
271 #ifdef CONFIG_I2C_MULTI_BUS
273 base
= (struct mv_i2c
*)i2c_regs
[current_bus
];
275 base
= (struct mv_i2c
*)CONFIG_MV_I2C_REG
;
278 i2c_board_init(base
);
282 * i2c_probe: - Test if a chip answers for a given i2c address
284 * @chip: address of the chip which is searched for
285 * @return: 0 if a chip was found, -1 otherwhise
287 int i2c_probe(uchar chip
)
293 msg
.condition
= I2C_COND_START
;
294 msg
.acknack
= I2C_ACKNAK_WAITACK
;
295 msg
.direction
= I2C_WRITE
;
296 msg
.data
= (chip
<< 1) + 1;
297 if (i2c_transfer(&msg
))
300 msg
.condition
= I2C_COND_STOP
;
301 msg
.acknack
= I2C_ACKNAK_SENDNAK
;
302 msg
.direction
= I2C_READ
;
304 if (i2c_transfer(&msg
))
311 * i2c_read: - Read multiple bytes from an i2c device
313 * The higher level routines take into account that this function is only
314 * called with len < page length of the device (see configuration file)
316 * @chip: address of the chip which is to be read
317 * @addr: i2c data address within the chip
318 * @alen: length of the i2c data address (1..2 bytes)
319 * @buffer: where to write the data
320 * @len: how much byte do we want to read
321 * @return: 0 in case of success
323 int i2c_read(uchar chip
, uint addr
, int alen
, uchar
*buffer
, int len
)
326 u8 addr_bytes
[3]; /* lowest...highest byte of data address */
328 PRINTD(("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
329 "len=0x%02x)\n", chip
, addr
, alen
, len
));
333 /* dummy chip address write */
334 PRINTD(("i2c_read: dummy chip address write\n"));
335 msg
.condition
= I2C_COND_START
;
336 msg
.acknack
= I2C_ACKNAK_WAITACK
;
337 msg
.direction
= I2C_WRITE
;
338 msg
.data
= (chip
<< 1);
340 if (i2c_transfer(&msg
))
344 * send memory address bytes;
345 * alen defines how much bytes we have to send.
347 /*addr &= ((1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)-1); */
348 addr_bytes
[0] = (u8
)((addr
>> 0) & 0x000000FF);
349 addr_bytes
[1] = (u8
)((addr
>> 8) & 0x000000FF);
350 addr_bytes
[2] = (u8
)((addr
>> 16) & 0x000000FF);
352 while (--alen
>= 0) {
353 PRINTD(("i2c_read: send memory word address byte %1d\n", alen
));
354 msg
.condition
= I2C_COND_NORMAL
;
355 msg
.acknack
= I2C_ACKNAK_WAITACK
;
356 msg
.direction
= I2C_WRITE
;
357 msg
.data
= addr_bytes
[alen
];
358 if (i2c_transfer(&msg
))
362 /* start read sequence */
363 PRINTD(("i2c_read: start read sequence\n"));
364 msg
.condition
= I2C_COND_START
;
365 msg
.acknack
= I2C_ACKNAK_WAITACK
;
366 msg
.direction
= I2C_WRITE
;
367 msg
.data
= (chip
<< 1);
369 if (i2c_transfer(&msg
))
372 /* read bytes; send NACK at last byte */
375 msg
.condition
= I2C_COND_STOP
;
376 msg
.acknack
= I2C_ACKNAK_SENDNAK
;
378 msg
.condition
= I2C_COND_NORMAL
;
379 msg
.acknack
= I2C_ACKNAK_SENDACK
;
382 msg
.direction
= I2C_READ
;
384 if (i2c_transfer(&msg
))
388 PRINTD(("i2c_read: reading byte (0x%08x)=0x%02x\n",
389 (unsigned int)buffer
, *buffer
));
399 * i2c_write: - Write multiple bytes to an i2c device
401 * The higher level routines take into account that this function is only
402 * called with len < page length of the device (see configuration file)
404 * @chip: address of the chip which is to be written
405 * @addr: i2c data address within the chip
406 * @alen: length of the i2c data address (1..2 bytes)
407 * @buffer: where to find the data to be written
408 * @len: how much byte do we want to read
409 * @return: 0 in case of success
411 int i2c_write(uchar chip
, uint addr
, int alen
, uchar
*buffer
, int len
)
414 u8 addr_bytes
[3]; /* lowest...highest byte of data address */
416 PRINTD(("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
417 "len=0x%02x)\n", chip
, addr
, alen
, len
));
421 /* chip address write */
422 PRINTD(("i2c_write: chip address write\n"));
423 msg
.condition
= I2C_COND_START
;
424 msg
.acknack
= I2C_ACKNAK_WAITACK
;
425 msg
.direction
= I2C_WRITE
;
426 msg
.data
= (chip
<< 1);
428 if (i2c_transfer(&msg
))
432 * send memory address bytes;
433 * alen defines how much bytes we have to send.
435 addr_bytes
[0] = (u8
)((addr
>> 0) & 0x000000FF);
436 addr_bytes
[1] = (u8
)((addr
>> 8) & 0x000000FF);
437 addr_bytes
[2] = (u8
)((addr
>> 16) & 0x000000FF);
439 while (--alen
>= 0) {
440 PRINTD(("i2c_write: send memory word address\n"));
441 msg
.condition
= I2C_COND_NORMAL
;
442 msg
.acknack
= I2C_ACKNAK_WAITACK
;
443 msg
.direction
= I2C_WRITE
;
444 msg
.data
= addr_bytes
[alen
];
445 if (i2c_transfer(&msg
))
449 /* write bytes; send NACK at last byte */
451 PRINTD(("i2c_write: writing byte (0x%08x)=0x%02x\n",
452 (unsigned int)buffer
, *buffer
));
455 msg
.condition
= I2C_COND_STOP
;
457 msg
.condition
= I2C_COND_NORMAL
;
459 msg
.acknack
= I2C_ACKNAK_WAITACK
;
460 msg
.direction
= I2C_WRITE
;
461 msg
.data
= *(buffer
++);
463 if (i2c_transfer(&msg
))
471 #endif /* CONFIG_HARD_I2C */