2 * Copyright (C) ST-Ericsson SA 2010
4 * Basic U-Boot I2C interface for STn8500/DB8500
5 * Author: Michael Brandt <Michael.Brandt@stericsson.com> for ST-Ericsson
7 * SPDX-License-Identifier: GPL-2.0+
11 * Only 7-bit I2C device addresses are supported.
17 #include "u8500_i2c.h"
19 #include <asm/arch/clock.h>
21 #define U8500_I2C_ENDAD_COUNTER (CONFIG_SYS_HZ/100) /* I2C bus timeout */
22 #define U8500_I2C_FIFO_FLUSH_COUNTER 500000 /* flush "timeout" */
23 #define U8500_I2C_SCL_FREQ 100000 /* I2C bus clock freq */
24 #define U8500_I2C_INPUT_FREQ 48000000 /* Input clock freq */
25 #define TX_FIFO_THRESHOLD 0x4
26 #define RX_FIFO_THRESHOLD 0x4
27 #define SLAVE_SETUP_TIME 14 /* Slave data setup time, 250ns for 48MHz i2c_clk */
29 #define WRITE_FIELD(var, mask, shift, value) \
30 (var = ((var & ~(mask)) | ((value) << (shift))))
32 static unsigned int bus_initialized[CONFIG_SYS_U8500_I2C_BUS_MAX];
33 static unsigned int i2c_bus_num;
34 static unsigned int i2c_bus_speed[] = {
35 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SPEED,
36 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SPEED
38 static struct u8500_i2c_regs *i2c_dev[] = {
39 (struct u8500_i2c_regs *)CONFIG_SYS_U8500_I2C0_BASE,
40 (struct u8500_i2c_regs *)CONFIG_SYS_U8500_I2C1_BASE,
41 (struct u8500_i2c_regs *)CONFIG_SYS_U8500_I2C2_BASE,
42 (struct u8500_i2c_regs *)CONFIG_SYS_U8500_I2C3_BASE,
49 } i2c_clock_bits[] = {
56 static void i2c_set_bit(void *reg, u32 mask)
58 writel(readl(reg) | mask, reg);
61 static void i2c_clr_bit(void *reg, u32 mask)
63 writel(readl(reg) & ~mask, reg);
66 static void i2c_write_field(void *reg, u32 mask, uint shift, u32 value)
68 writel((readl(reg) & ~mask) | (value << shift), reg);
71 static int __i2c_set_bus_speed(unsigned int speed)
74 struct u8500_i2c_regs *i2c_regs;
76 i2c_regs = i2c_dev[i2c_bus_num];
78 /* Select standard (100 kbps) speed mode */
79 i2c_write_field(&i2c_regs->cr, U8500_I2C_CR_SM,
80 U8500_I2C_CR_SHIFT_SM, 0x0);
83 * Set the Baud Rate Counter 2 value
84 * Baud rate (standard) = fi2cclk / ( (BRCNT2 x 2) + Foncycle )
85 * Foncycle = 0 (no digital filtering)
87 value = (u32) (U8500_I2C_INPUT_FREQ / (speed * 2));
88 i2c_write_field(&i2c_regs->brcr, U8500_I2C_BRCR_BRCNT2,
89 U8500_I2C_BRCR_SHIFT_BRCNT2, value);
91 /* ensure that BRCNT value is zero */
92 i2c_write_field(&i2c_regs->brcr, U8500_I2C_BRCR_BRCNT1,
93 U8500_I2C_BRCR_SHIFT_BRCNT1, 0);
95 return U8500_I2C_INPUT_FREQ/(value * 2);
99 * i2c_init - initialize the i2c bus
101 * speed: bus speed (in HZ)
102 * slaveaddr: address of device in slave mode
104 * Slave mode is not implemented.
106 void i2c_init(int speed, int slaveaddr)
108 struct u8500_i2c_regs *i2c_regs;
110 debug("i2c_init bus %d, speed %d\n", i2c_bus_num, speed);
112 u8500_clock_enable(i2c_clock_bits[i2c_bus_num].periph,
113 i2c_clock_bits[i2c_bus_num].pcken,
114 i2c_clock_bits[i2c_bus_num].kcken);
116 i2c_regs = i2c_dev[i2c_bus_num];
118 /* Disable the controller */
119 i2c_clr_bit(&i2c_regs->cr, U8500_I2C_CR_PE);
121 /* Clear registers */
122 writel(0, &i2c_regs->cr);
123 writel(0, &i2c_regs->scr);
124 writel(0, &i2c_regs->hsmcr);
125 writel(0, &i2c_regs->tftr);
126 writel(0, &i2c_regs->rftr);
127 writel(0, &i2c_regs->dmar);
129 i2c_bus_speed[i2c_bus_num] = __i2c_set_bus_speed(speed);
132 * Set our own address.
133 * Set slave address mode to 7 bit addressing mode
135 i2c_clr_bit(&i2c_regs->cr, U8500_I2C_CR_SAM);
136 i2c_write_field(&i2c_regs->scr, U8500_I2C_SCR_ADDR,
137 U8500_I2C_SCR_SHIFT_ADDR, slaveaddr);
138 /* Slave Data Set up Time */
139 i2c_write_field(&i2c_regs->scr, U8500_I2C_SCR_DATA_SETUP_TIME,
140 U8500_I2C_SCR_SHIFT_DATA_SETUP_TIME, SLAVE_SETUP_TIME);
142 /* Disable the DMA sync logic */
143 i2c_write_field(&i2c_regs->cr, U8500_I2C_CR_DMA_SLE,
144 U8500_I2C_CR_SHIFT_DMA_SLE, 0);
146 /* Disable interrupts */
147 writel(0, &i2c_regs->imscr);
149 /* Configure bus master mode */
150 i2c_write_field(&i2c_regs->cr, U8500_I2C_CR_OM, U8500_I2C_CR_SHIFT_OM,
151 U8500_I2C_BUS_MASTER_MODE);
152 /* Set FIFO threshold values */
153 writel(TX_FIFO_THRESHOLD, &i2c_regs->tftr);
154 writel(RX_FIFO_THRESHOLD, &i2c_regs->rftr);
156 /* Enable the I2C Controller */
157 i2c_set_bit(&i2c_regs->cr, U8500_I2C_CR_PE);
159 bus_initialized[i2c_bus_num] = 1;
164 * loop_till_bit_clear - polls on a bit till it clears
165 * ioreg: register where you want to check status
166 * mask: bit mask for the bit you wish to check
167 * timeout: timeout in ticks/s
169 static int loop_till_bit_clear(void *io_reg, u32 mask, unsigned long timeout)
171 unsigned long timebase = get_timer(0);
174 if ((readl(io_reg) & mask) == 0x0UL)
176 } while (get_timer(timebase) < timeout);
178 debug("loop_till_bit_clear timed out\n");
183 * loop_till_bit_set - polls on a bit till it is set.
184 * ioreg: register where you want to check status
185 * mask: bit mask for the bit you wish to check
186 * timeout: timeout in ticks/s
188 static int loop_till_bit_set(void *io_reg, u32 mask, unsigned long timeout)
190 unsigned long timebase = get_timer(0);
193 if ((readl(io_reg) & mask) != 0x0UL)
195 } while (get_timer(timebase) < timeout);
197 debug("loop_till_bit_set timed out\n");
202 * flush_fifo - flush the I2C TX and RX FIFOs
204 static void flush_fifo(struct u8500_i2c_regs *i2c_regs)
206 int counter = U8500_I2C_FIFO_FLUSH_COUNTER;
209 i2c_set_bit(&i2c_regs->cr, U8500_I2C_CR_FTX);
211 i2c_set_bit(&i2c_regs->cr, U8500_I2C_CR_FRX);
213 if (!(readl(&i2c_regs->cr) &
214 (U8500_I2C_CR_FTX | U8500_I2C_CR_FRX)))
221 static void print_abort_reason(struct u8500_i2c_regs *i2c_regs)
225 printf("abort: risr %08x, sr %08x\n", i2c_regs->risr, i2c_regs->sr);
226 cause = (readl(&i2c_regs->sr) & U8500_I2C_SR_CAUSE) >>
227 U8500_I2C_SR_SHIFT_CAUSE;
229 case U8500_I2C_NACK_ADDR:
230 printf("No Ack received after Slave Address xmission\n");
232 case U8500_I2C_NACK_DATA:
233 printf("Valid for MASTER_WRITE: No Ack received "
234 "during data phase\n");
236 case U8500_I2C_ACK_MCODE:
237 printf("Master recv ack after xmission of master code"
240 case U8500_I2C_ARB_LOST:
241 printf("Master Lost arbitration\n");
243 case U8500_I2C_BERR_START:
244 printf("Slave restarts\n");
246 case U8500_I2C_BERR_STOP:
247 printf("Slave reset\n");
250 printf("Overflow\n");
253 printf("Unknown error type\n");
259 * i2c_abort - called when a I2C transaction failed
261 static void i2c_abort(struct u8500_i2c_regs *i2c_regs)
264 print_abort_reason(i2c_regs);
266 /* flush RX and TX fifos */
267 flush_fifo(i2c_regs);
269 /* Acknowledge the Master Transaction Done */
270 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTD);
272 /* Acknowledge the Master Transaction Done Without Stop */
273 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTDWS);
275 i2c_init(i2c_bus_speed[i2c_bus_num], CONFIG_SYS_I2C_SLAVE);
279 * write addr, alias index, to I2C bus.
281 static int i2c_write_addr(struct u8500_i2c_regs *i2c_regs, uint addr, int alen)
284 /* Wait until the Tx Fifo is not full */
285 if (loop_till_bit_clear((void *)&i2c_regs->risr,
287 U8500_I2C_ENDAD_COUNTER)) {
293 writeb((addr >> (alen * 8)) & 0xff, &i2c_regs->tfr);
300 * Internal simplified read function:
301 * i2c_regs: Pointer to I2C registers for current bus
302 * chip: I2C chip address, range 0..127
303 * addr: Memory (register) address within the chip
304 * alen: Number of bytes to use for addr (typically 1, 2 for larger
305 * memories, 0 for register type devices with only one register)
306 * value: Where to put the data
308 * Returns: 0 on success, not 0 on failure
310 static int i2c_read_byte(struct u8500_i2c_regs *i2c_regs, uchar chip,
311 uint addr, int alen, uchar *value)
315 /* Set the address mode to 7 bit */
316 WRITE_FIELD(mcr, U8500_I2C_MCR_AM, U8500_I2C_MCR_SHIFT_AM, 1);
318 /* Store the slave address in the master control register */
319 WRITE_FIELD(mcr, U8500_I2C_MCR_A7, U8500_I2C_MCR_SHIFT_A7, chip);
322 /* Master write operation */
323 mcr &= ~(U8500_I2C_MCR_OP);
325 /* Configure the Frame length to one byte */
326 WRITE_FIELD(mcr, U8500_I2C_MCR_LENGTH,
327 U8500_I2C_MCR_SHIFT_LENGTH, 1);
329 /* Repeated start, no stop */
330 mcr &= ~(U8500_I2C_MCR_STOP);
332 /* Write Master Control Register */
333 writel(mcr, &i2c_regs->mcr);
335 /* send addr/index */
336 if (i2c_write_addr(i2c_regs, addr, alen) != 0)
339 /* Check for the Master Transaction Done Without Stop */
340 if (loop_till_bit_set((void *)&i2c_regs->risr,
342 U8500_I2C_ENDAD_COUNTER)) {
346 /* Acknowledge the Master Transaction Done Without Stop */
347 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTDWS);
350 /* Master control configuration for read operation */
351 mcr |= U8500_I2C_MCR_OP;
353 /* Configure the STOP condition, we read only one byte */
354 mcr |= U8500_I2C_MCR_STOP;
356 /* Set the frame length to one byte, we support only 1 byte reads */
357 WRITE_FIELD(mcr, U8500_I2C_MCR_LENGTH, U8500_I2C_MCR_SHIFT_LENGTH, 1);
359 i2c_write_field(&i2c_regs->mcr, U8500_I2C_MCR_LENGTH_STOP_OP,
360 U8500_I2C_MCR_SHIFT_LENGTH_STOP_OP, mcr);
363 * receive_data_polling
366 /* Wait until the Rx FIFO is not empty */
367 if (loop_till_bit_clear((void *)&i2c_regs->risr,
369 U8500_I2C_ENDAD_COUNTER))
372 /* Read the data byte from Rx FIFO */
373 *value = readb(&i2c_regs->rfr);
375 /* Wait until the work is done */
376 if (loop_till_bit_set((void *)&i2c_regs->risr, U8500_I2C_INT_MTD,
377 U8500_I2C_ENDAD_COUNTER))
380 /* Acknowledge the Master Transaction Done */
381 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTD);
383 /* If MTD is set, Master Transaction Done Without Stop is set too */
384 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTDWS);
390 * Internal simplified write function:
391 * i2c_regs: Pointer to I2C registers for current bus
392 * chip: I2C chip address, range 0..127
393 * addr: Memory (register) address within the chip
394 * alen: Number of bytes to use for addr (typically 1, 2 for larger
395 * memories, 0 for register type devices with only one register)
396 * data: Where to read the data
397 * len: How many bytes to write
399 * Returns: 0 on success, not 0 on failure
401 static int __i2c_write(struct u8500_i2c_regs *i2c_regs, u8 chip, uint addr,
402 int alen, u8 *data, int len)
407 /* Set the address mode to 7 bit */
408 WRITE_FIELD(mcr, U8500_I2C_MCR_AM, U8500_I2C_MCR_SHIFT_AM, 1);
410 /* Store the slave address in the master control register */
411 WRITE_FIELD(mcr, U8500_I2C_MCR_A7, U8500_I2C_MCR_SHIFT_A7, chip);
413 /* Write operation */
414 mcr &= ~(U8500_I2C_MCR_OP);
416 /* Current transaction is terminated by STOP condition */
417 mcr |= U8500_I2C_MCR_STOP;
419 /* Frame length: addr byte + len */
420 WRITE_FIELD(mcr, U8500_I2C_MCR_LENGTH, U8500_I2C_MCR_SHIFT_LENGTH,
423 /* Write MCR register */
424 writel(mcr, &i2c_regs->mcr);
426 if (i2c_write_addr(i2c_regs, addr, alen) != 0)
429 for (i = 0; i < len; i++) {
430 /* Wait until the Tx FIFO is not full */
431 if (loop_till_bit_clear((void *)&i2c_regs->risr,
433 U8500_I2C_ENDAD_COUNTER))
436 /* it is a 32 bit register with upper 24 reserved R/O */
437 writeb(data[i], &i2c_regs->tfr);
440 /* Check for Master Transaction Done */
441 if (loop_till_bit_set((void *)&i2c_regs->risr,
443 U8500_I2C_ENDAD_COUNTER)) {
444 printf("i2c_write_byte error2: risr %08x\n",
449 /* Acknowledge Master Transaction Done */
450 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTD);
452 /* Acknowledge Master Transaction Done Without Stop */
453 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTDWS);
459 * Probe the given I2C chip address. Returns 0 if a chip responded,
462 int i2c_probe(uchar chip)
465 struct u8500_i2c_regs *i2c_regs;
467 if (chip == CONFIG_SYS_I2C_SLAVE)
470 i2c_regs = i2c_dev[i2c_bus_num];
472 /* Set the address mode to 7 bit */
473 WRITE_FIELD(mcr, U8500_I2C_MCR_AM, U8500_I2C_MCR_SHIFT_AM, 1);
475 /* Store the slave address in the master control register */
476 WRITE_FIELD(mcr, U8500_I2C_MCR_A10, U8500_I2C_MCR_SHIFT_A7, chip);
479 mcr |= U8500_I2C_MCR_OP;
481 /* Set the frame length to one byte */
482 WRITE_FIELD(mcr, U8500_I2C_MCR_LENGTH, U8500_I2C_MCR_SHIFT_LENGTH, 1);
484 /* Current transaction is terminated by STOP condition */
485 mcr |= U8500_I2C_MCR_STOP;
487 /* Write MCR register */
488 writel(mcr, &i2c_regs->mcr);
490 /* Wait until the Rx Fifo is not empty */
491 if (loop_till_bit_clear((void *)&i2c_regs->risr,
493 U8500_I2C_ENDAD_COUNTER)) {
498 flush_fifo(i2c_regs);
500 /* Acknowledge the Master Transaction Done */
501 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTD);
503 /* Acknowledge the Master Transaction Done Without Stop */
504 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTDWS);
510 * Read/Write interface:
511 * chip: I2C chip address, range 0..127
512 * addr: Memory (register) address within the chip
513 * alen: Number of bytes to use for addr (typically 1, 2 for larger
514 * memories, 0 for register type devices with only one
516 * buffer: Where to read/write the data
517 * len: How many bytes to read/write
519 * Returns: 0 on success, not 0 on failure
521 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
525 struct u8500_i2c_regs *i2c_regs;
528 debug("I2C read: addr len %d not supported\n", alen);
532 i2c_regs = i2c_dev[i2c_bus_num];
534 for (i = 0; i < len; i++) {
535 rc = i2c_read_byte(i2c_regs, chip, addr + i, alen, &buffer[i]);
537 debug("I2C read: I/O error: %d\n", rc);
546 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
549 struct u8500_i2c_regs *i2c_regs;
550 i2c_regs = i2c_dev[i2c_bus_num];
552 rc = __i2c_write(i2c_regs, chip, addr, alen, buffer,
555 debug("I2C write: I/O error\n");
562 int i2c_set_bus_num(unsigned int bus)
564 if (bus > ARRAY_SIZE(i2c_dev) - 1) {
565 debug("i2c_set_bus_num: only up to bus %d supported\n",
566 ARRAY_SIZE(i2c_dev)-1);
572 if (!bus_initialized[i2c_bus_num])
573 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
578 int i2c_set_bus_speed(unsigned int speed)
581 if (speed > U8500_I2C_MAX_STANDARD_SCL) {
582 debug("i2c_set_bus_speed: only up to %d supported\n",
583 U8500_I2C_MAX_STANDARD_SCL);
587 /* sets as side effect i2c_bus_speed[i2c_bus_num] */
588 i2c_init(speed, CONFIG_SYS_I2C_SLAVE);
593 unsigned int i2c_get_bus_num(void)
598 unsigned int i2c_get_bus_speed(void)
600 return i2c_bus_speed[i2c_bus_num];