1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2013 Broadcom Corporation.
5 * NOTE: This driver should be converted to driver model before June 2017.
6 * Please see doc/driver-model/i2c-howto.rst for instructions.
11 #include <linux/errno.h>
12 #include <asm/arch/sysmap.h>
13 #include <asm/kona-common/clk.h>
16 /* Hardware register offsets and field defintions */
17 #define CS_OFFSET 0x00000020
18 #define CS_ACK_SHIFT 3
19 #define CS_ACK_MASK 0x00000008
20 #define CS_ACK_CMD_GEN_START 0x00000000
21 #define CS_ACK_CMD_GEN_RESTART 0x00000001
22 #define CS_CMD_SHIFT 1
23 #define CS_CMD_CMD_NO_ACTION 0x00000000
24 #define CS_CMD_CMD_START_RESTART 0x00000001
25 #define CS_CMD_CMD_STOP 0x00000002
27 #define CS_EN_CMD_ENABLE_BSC 0x00000001
29 #define TIM_OFFSET 0x00000024
30 #define TIM_PRESCALE_SHIFT 6
32 #define TIM_NO_DIV_SHIFT 2
33 #define TIM_DIV_SHIFT 0
35 #define DAT_OFFSET 0x00000028
37 #define TOUT_OFFSET 0x0000002c
39 #define TXFCR_OFFSET 0x0000003c
40 #define TXFCR_FIFO_FLUSH_MASK 0x00000080
41 #define TXFCR_FIFO_EN_MASK 0x00000040
43 #define IER_OFFSET 0x00000044
44 #define IER_READ_COMPLETE_INT_MASK 0x00000010
45 #define IER_I2C_INT_EN_MASK 0x00000008
46 #define IER_FIFO_INT_EN_MASK 0x00000002
47 #define IER_NOACK_EN_MASK 0x00000001
49 #define ISR_OFFSET 0x00000048
50 #define ISR_RESERVED_MASK 0xffffff60
51 #define ISR_CMDBUSY_MASK 0x00000080
52 #define ISR_READ_COMPLETE_MASK 0x00000010
53 #define ISR_SES_DONE_MASK 0x00000008
54 #define ISR_ERR_MASK 0x00000004
55 #define ISR_TXFIFOEMPTY_MASK 0x00000002
56 #define ISR_NOACK_MASK 0x00000001
58 #define CLKEN_OFFSET 0x0000004c
59 #define CLKEN_AUTOSENSE_OFF_MASK 0x00000080
60 #define CLKEN_M_SHIFT 4
61 #define CLKEN_N_SHIFT 1
62 #define CLKEN_CLKEN_MASK 0x00000001
64 #define FIFO_STATUS_OFFSET 0x00000054
65 #define FIFO_STATUS_RXFIFO_EMPTY_MASK 0x00000004
66 #define FIFO_STATUS_TXFIFO_EMPTY_MASK 0x00000010
68 #define HSTIM_OFFSET 0x00000058
69 #define HSTIM_HS_MODE_MASK 0x00008000
70 #define HSTIM_HS_HOLD_SHIFT 10
71 #define HSTIM_HS_HIGH_PHASE_SHIFT 5
72 #define HSTIM_HS_SETUP_SHIFT 0
74 #define PADCTL_OFFSET 0x0000005c
75 #define PADCTL_PAD_OUT_EN_MASK 0x00000004
77 #define RXFCR_OFFSET 0x00000068
78 #define RXFCR_NACK_EN_SHIFT 7
79 #define RXFCR_READ_COUNT_SHIFT 0
80 #define RXFIFORDOUT_OFFSET 0x0000006c
82 /* Locally used constants */
83 #define MAX_RX_FIFO_SIZE 64U /* bytes */
84 #define MAX_TX_FIFO_SIZE 64U /* bytes */
86 #define I2C_TIMEOUT 100000 /* usecs */
88 #define WAIT_INT_CHK 100 /* usecs */
89 #if I2C_TIMEOUT % WAIT_INT_CHK
90 #error I2C_TIMEOUT must be a multiple of WAIT_INT_CHK
93 /* Operations that can be commanded to the controller */
101 /* Internal divider settings for standard mode, fast mode and fast mode plus */
102 struct bus_speed_cfg {
103 uint8_t time_m; /* Number of cycles for setup time */
104 uint8_t time_n; /* Number of cycles for hold time */
105 uint8_t prescale; /* Prescale divider */
106 uint8_t time_p; /* Timing coefficient */
107 uint8_t no_div; /* Disable clock divider */
108 uint8_t time_div; /* Post-prescale divider */
111 static const struct bus_speed_cfg std_cfg_table[] = {
112 [IC_SPEED_MODE_STANDARD] = {0x01, 0x01, 0x03, 0x06, 0x00, 0x02},
113 [IC_SPEED_MODE_FAST] = {0x05, 0x01, 0x03, 0x05, 0x01, 0x02},
114 [IC_SPEED_MODE_FAST_PLUS] = {0x01, 0x01, 0x03, 0x01, 0x01, 0x03},
117 struct bcm_kona_i2c_dev {
120 const struct bus_speed_cfg *std_cfg;
123 /* Keep these two defines in sync */
124 #define DEF_SPD I2C_SPEED_STANDARD_RATE
125 #define DEF_SPD_ENUM IC_SPEED_MODE_STANDARD
127 #define DEF_DEVICE(num) \
128 {(void *)CONFIG_SYS_I2C_BASE##num, DEF_SPD, &std_cfg_table[DEF_SPD_ENUM]}
130 static struct bcm_kona_i2c_dev g_i2c_devs[CONFIG_SYS_MAX_I2C_BUS] = {
131 #ifdef CONFIG_SYS_I2C_BASE0
134 #ifdef CONFIG_SYS_I2C_BASE1
137 #ifdef CONFIG_SYS_I2C_BASE2
140 #ifdef CONFIG_SYS_I2C_BASE3
143 #ifdef CONFIG_SYS_I2C_BASE4
146 #ifdef CONFIG_SYS_I2C_BASE5
151 #define I2C_M_TEN 0x0010 /* ten bit address */
152 #define I2C_M_RD 0x0001 /* read data */
153 #define I2C_M_NOSTART 0x4000 /* no restart between msgs */
155 struct kona_i2c_msg {
162 static void bcm_kona_i2c_send_cmd_to_ctrl(struct bcm_kona_i2c_dev *dev,
163 enum bcm_kona_cmd_t cmd)
165 debug("%s, %d\n", __func__, cmd);
168 case BCM_CMD_NOACTION:
169 writel((CS_CMD_CMD_NO_ACTION << CS_CMD_SHIFT) |
170 (CS_EN_CMD_ENABLE_BSC << CS_EN_SHIFT),
171 dev->base + CS_OFFSET);
175 writel((CS_ACK_CMD_GEN_START << CS_ACK_SHIFT) |
176 (CS_CMD_CMD_START_RESTART << CS_CMD_SHIFT) |
177 (CS_EN_CMD_ENABLE_BSC << CS_EN_SHIFT),
178 dev->base + CS_OFFSET);
181 case BCM_CMD_RESTART:
182 writel((CS_ACK_CMD_GEN_RESTART << CS_ACK_SHIFT) |
183 (CS_CMD_CMD_START_RESTART << CS_CMD_SHIFT) |
184 (CS_EN_CMD_ENABLE_BSC << CS_EN_SHIFT),
185 dev->base + CS_OFFSET);
189 writel((CS_CMD_CMD_STOP << CS_CMD_SHIFT) |
190 (CS_EN_CMD_ENABLE_BSC << CS_EN_SHIFT),
191 dev->base + CS_OFFSET);
195 printf("Unknown command %d\n", cmd);
199 static void bcm_kona_i2c_enable_clock(struct bcm_kona_i2c_dev *dev)
201 writel(readl(dev->base + CLKEN_OFFSET) | CLKEN_CLKEN_MASK,
202 dev->base + CLKEN_OFFSET);
205 static void bcm_kona_i2c_disable_clock(struct bcm_kona_i2c_dev *dev)
207 writel(readl(dev->base + CLKEN_OFFSET) & ~CLKEN_CLKEN_MASK,
208 dev->base + CLKEN_OFFSET);
211 /* Wait until at least one of the mask bit(s) are set */
212 static unsigned long wait_for_int_timeout(struct bcm_kona_i2c_dev *dev,
213 unsigned long time_left,
219 status = readl(dev->base + ISR_OFFSET);
221 if ((status & ~ISR_RESERVED_MASK) == 0) {
222 debug("Bogus I2C interrupt 0x%x\n", status);
226 /* Must flush the TX FIFO when NAK detected */
227 if (status & ISR_NOACK_MASK)
228 writel(TXFCR_FIFO_FLUSH_MASK | TXFCR_FIFO_EN_MASK,
229 dev->base + TXFCR_OFFSET);
231 writel(status & ~ISR_RESERVED_MASK, dev->base + ISR_OFFSET);
234 /* We are done since one of the mask bits are set */
237 udelay(WAIT_INT_CHK);
238 time_left -= WAIT_INT_CHK;
243 /* Send command to I2C bus */
244 static int bcm_kona_send_i2c_cmd(struct bcm_kona_i2c_dev *dev,
245 enum bcm_kona_cmd_t cmd)
248 unsigned long time_left = I2C_TIMEOUT;
250 /* Send the command */
251 bcm_kona_i2c_send_cmd_to_ctrl(dev, cmd);
253 /* Wait for transaction to finish or timeout */
254 time_left = wait_for_int_timeout(dev, time_left, IER_I2C_INT_EN_MASK);
257 printf("controller timed out\n");
262 bcm_kona_i2c_send_cmd_to_ctrl(dev, BCM_CMD_NOACTION);
267 /* Read a single RX FIFO worth of data from the i2c bus */
268 static int bcm_kona_i2c_read_fifo_single(struct bcm_kona_i2c_dev *dev,
269 uint8_t *buf, unsigned int len,
270 unsigned int last_byte_nak)
272 unsigned long time_left = I2C_TIMEOUT;
274 /* Start the RX FIFO */
275 writel((last_byte_nak << RXFCR_NACK_EN_SHIFT) |
276 (len << RXFCR_READ_COUNT_SHIFT), dev->base + RXFCR_OFFSET);
278 /* Wait for FIFO read to complete */
280 wait_for_int_timeout(dev, time_left, IER_READ_COMPLETE_INT_MASK);
283 printf("RX FIFO time out\n");
287 /* Read data from FIFO */
288 for (; len > 0; len--, buf++)
289 *buf = readl(dev->base + RXFIFORDOUT_OFFSET);
294 /* Read any amount of data using the RX FIFO from the i2c bus */
295 static int bcm_kona_i2c_read_fifo(struct bcm_kona_i2c_dev *dev,
296 struct kona_i2c_msg *msg)
298 unsigned int bytes_to_read = MAX_RX_FIFO_SIZE;
299 unsigned int last_byte_nak = 0;
300 unsigned int bytes_read = 0;
303 uint8_t *tmp_buf = msg->buf;
305 while (bytes_read < msg->len) {
306 if (msg->len - bytes_read <= MAX_RX_FIFO_SIZE) {
307 last_byte_nak = 1; /* NAK last byte of transfer */
308 bytes_to_read = msg->len - bytes_read;
311 rc = bcm_kona_i2c_read_fifo_single(dev, tmp_buf, bytes_to_read,
316 bytes_read += bytes_to_read;
317 tmp_buf += bytes_to_read;
323 /* Write a single byte of data to the i2c bus */
324 static int bcm_kona_i2c_write_byte(struct bcm_kona_i2c_dev *dev, uint8_t data,
325 unsigned int nak_expected)
327 unsigned long time_left = I2C_TIMEOUT;
328 unsigned int nak_received;
330 /* Clear pending session done interrupt */
331 writel(ISR_SES_DONE_MASK, dev->base + ISR_OFFSET);
333 /* Send one byte of data */
334 writel(data, dev->base + DAT_OFFSET);
336 time_left = wait_for_int_timeout(dev, time_left, IER_I2C_INT_EN_MASK);
339 debug("controller timed out\n");
343 nak_received = readl(dev->base + CS_OFFSET) & CS_ACK_MASK ? 1 : 0;
345 if (nak_received ^ nak_expected) {
346 debug("unexpected NAK/ACK\n");
353 /* Write a single TX FIFO worth of data to the i2c bus */
354 static int bcm_kona_i2c_write_fifo_single(struct bcm_kona_i2c_dev *dev,
355 uint8_t *buf, unsigned int len)
358 unsigned long time_left = I2C_TIMEOUT;
359 unsigned int fifo_status;
361 /* Write data into FIFO */
362 for (k = 0; k < len; k++)
363 writel(buf[k], (dev->base + DAT_OFFSET));
365 /* Wait for FIFO to empty */
368 wait_for_int_timeout(dev, time_left,
369 (IER_FIFO_INT_EN_MASK |
371 fifo_status = readl(dev->base + FIFO_STATUS_OFFSET);
372 } while (time_left && !(fifo_status & FIFO_STATUS_TXFIFO_EMPTY_MASK));
374 /* Check if there was a NAK */
375 if (readl(dev->base + CS_OFFSET) & CS_ACK_MASK) {
376 printf("unexpected NAK\n");
380 /* Check if a timeout occurred */
382 printf("completion timed out\n");
389 /* Write any amount of data using TX FIFO to the i2c bus */
390 static int bcm_kona_i2c_write_fifo(struct bcm_kona_i2c_dev *dev,
391 struct kona_i2c_msg *msg)
393 unsigned int bytes_to_write = MAX_TX_FIFO_SIZE;
394 unsigned int bytes_written = 0;
397 uint8_t *tmp_buf = msg->buf;
399 while (bytes_written < msg->len) {
400 if (msg->len - bytes_written <= MAX_TX_FIFO_SIZE)
401 bytes_to_write = msg->len - bytes_written;
403 rc = bcm_kona_i2c_write_fifo_single(dev, tmp_buf,
408 bytes_written += bytes_to_write;
409 tmp_buf += bytes_to_write;
415 /* Send i2c address */
416 static int bcm_kona_i2c_do_addr(struct bcm_kona_i2c_dev *dev,
417 struct kona_i2c_msg *msg)
421 if (msg->flags & I2C_M_TEN) {
422 /* First byte is 11110XX0 where XX is upper 2 bits */
423 addr = 0xf0 | ((msg->addr & 0x300) >> 7);
424 if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0)
427 /* Second byte is the remaining 8 bits */
428 addr = msg->addr & 0xff;
429 if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0)
432 if (msg->flags & I2C_M_RD) {
433 /* For read, send restart command */
434 if (bcm_kona_send_i2c_cmd(dev, BCM_CMD_RESTART) < 0)
437 /* Then re-send the first byte with the read bit set */
438 addr = 0xf0 | ((msg->addr & 0x300) >> 7) | 0x01;
439 if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0)
443 addr = msg->addr << 1;
445 if (msg->flags & I2C_M_RD)
448 if (bcm_kona_i2c_write_byte(dev, addr, 0) < 0)
455 static void bcm_kona_i2c_enable_autosense(struct bcm_kona_i2c_dev *dev)
457 writel(readl(dev->base + CLKEN_OFFSET) & ~CLKEN_AUTOSENSE_OFF_MASK,
458 dev->base + CLKEN_OFFSET);
461 static void bcm_kona_i2c_config_timing(struct bcm_kona_i2c_dev *dev)
463 writel(readl(dev->base + HSTIM_OFFSET) & ~HSTIM_HS_MODE_MASK,
464 dev->base + HSTIM_OFFSET);
466 writel((dev->std_cfg->prescale << TIM_PRESCALE_SHIFT) |
467 (dev->std_cfg->time_p << TIM_P_SHIFT) |
468 (dev->std_cfg->no_div << TIM_NO_DIV_SHIFT) |
469 (dev->std_cfg->time_div << TIM_DIV_SHIFT),
470 dev->base + TIM_OFFSET);
472 writel((dev->std_cfg->time_m << CLKEN_M_SHIFT) |
473 (dev->std_cfg->time_n << CLKEN_N_SHIFT) |
474 CLKEN_CLKEN_MASK, dev->base + CLKEN_OFFSET);
477 /* Master transfer function */
478 static int bcm_kona_i2c_xfer(struct bcm_kona_i2c_dev *dev,
479 struct kona_i2c_msg msgs[], int num)
481 struct kona_i2c_msg *pmsg;
485 /* Enable pad output */
486 writel(0, dev->base + PADCTL_OFFSET);
488 /* Enable internal clocks */
489 bcm_kona_i2c_enable_clock(dev);
491 /* Send start command */
492 rc = bcm_kona_send_i2c_cmd(dev, BCM_CMD_START);
494 printf("Start command failed rc = %d\n", rc);
495 goto xfer_disable_pad;
498 /* Loop through all messages */
499 for (i = 0; i < num; i++) {
502 /* Send restart for subsequent messages */
503 if ((i != 0) && ((pmsg->flags & I2C_M_NOSTART) == 0)) {
504 rc = bcm_kona_send_i2c_cmd(dev, BCM_CMD_RESTART);
506 printf("restart cmd failed rc = %d\n", rc);
511 /* Send slave address */
512 if (!(pmsg->flags & I2C_M_NOSTART)) {
513 rc = bcm_kona_i2c_do_addr(dev, pmsg);
515 debug("NAK from addr %2.2x msg#%d rc = %d\n",
521 /* Perform data transfer */
522 if (pmsg->flags & I2C_M_RD) {
523 rc = bcm_kona_i2c_read_fifo(dev, pmsg);
525 printf("read failure\n");
529 rc = bcm_kona_i2c_write_fifo(dev, pmsg);
531 printf("write failure");
540 /* Send a STOP command */
541 bcm_kona_send_i2c_cmd(dev, BCM_CMD_STOP);
544 /* Disable pad output */
545 writel(PADCTL_PAD_OUT_EN_MASK, dev->base + PADCTL_OFFSET);
547 /* Stop internal clock */
548 bcm_kona_i2c_disable_clock(dev);
553 static uint bcm_kona_i2c_assign_bus_speed(struct bcm_kona_i2c_dev *dev,
557 case I2C_SPEED_STANDARD_RATE:
558 dev->std_cfg = &std_cfg_table[IC_SPEED_MODE_STANDARD];
560 case I2C_SPEED_FAST_RATE:
561 dev->std_cfg = &std_cfg_table[IC_SPEED_MODE_FAST];
563 case I2C_SPEED_FAST_PLUS_RATE:
564 dev->std_cfg = &std_cfg_table[IC_SPEED_MODE_FAST_PLUS];
567 printf("%d hz bus speed not supported\n", speed);
574 static void bcm_kona_i2c_init(struct bcm_kona_i2c_dev *dev)
576 /* Parse bus speed */
577 bcm_kona_i2c_assign_bus_speed(dev, dev->speed);
579 /* Enable internal clocks */
580 bcm_kona_i2c_enable_clock(dev);
582 /* Configure internal dividers */
583 bcm_kona_i2c_config_timing(dev);
585 /* Disable timeout */
586 writel(0, dev->base + TOUT_OFFSET);
588 /* Enable autosense */
589 bcm_kona_i2c_enable_autosense(dev);
592 writel(TXFCR_FIFO_FLUSH_MASK | TXFCR_FIFO_EN_MASK,
593 dev->base + TXFCR_OFFSET);
595 /* Mask all interrupts */
596 writel(0, dev->base + IER_OFFSET);
598 /* Clear all pending interrupts */
599 writel(ISR_CMDBUSY_MASK |
600 ISR_READ_COMPLETE_MASK |
603 ISR_TXFIFOEMPTY_MASK | ISR_NOACK_MASK, dev->base + ISR_OFFSET);
605 /* Enable the controller but leave it idle */
606 bcm_kona_i2c_send_cmd_to_ctrl(dev, BCM_CMD_NOACTION);
608 /* Disable pad output */
609 writel(PADCTL_PAD_OUT_EN_MASK, dev->base + PADCTL_OFFSET);
615 struct bcm_kona_i2c_dev *kona_get_dev(struct i2c_adapter *adap)
617 return &g_i2c_devs[adap->hwadapnr];
620 static void kona_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
622 struct bcm_kona_i2c_dev *dev = kona_get_dev(adap);
624 if (clk_bsc_enable(dev->base))
627 bcm_kona_i2c_init(dev);
630 static int kona_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
631 int alen, uchar *buffer, int len)
633 /* msg[0] writes the addr, msg[1] reads the data */
634 struct kona_i2c_msg msg[2];
635 unsigned char msgbuf0[64];
636 struct bcm_kona_i2c_dev *dev = kona_get_dev(adap);
641 msg[0].buf = msgbuf0; /* msgbuf0 contains incrementing reg addr */
644 msg[1].flags = I2C_M_RD;
645 /* msg[1].buf dest ptr increments each read */
647 msgbuf0[0] = (unsigned char)addr;
650 if (bcm_kona_i2c_xfer(dev, msg, 2) < 0) {
651 /* Sending 2 i2c messages */
652 kona_i2c_init(adap, adap->speed, adap->slaveaddr);
653 debug("I2C read: I/O error\n");
659 static int kona_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
660 int alen, uchar *buffer, int len)
662 struct kona_i2c_msg msg[1];
663 unsigned char msgbuf0[64];
665 struct bcm_kona_i2c_dev *dev = kona_get_dev(adap);
669 msg[0].len = 2; /* addr byte plus data */
670 msg[0].buf = msgbuf0;
672 for (i = 0; i < len; i++) {
674 msgbuf0[1] = buffer[i];
675 if (bcm_kona_i2c_xfer(dev, msg, 1) < 0) {
676 kona_i2c_init(adap, adap->speed, adap->slaveaddr);
677 debug("I2C write: I/O error\n");
684 static int kona_i2c_probe(struct i2c_adapter *adap, uchar chip)
689 * read addr 0x0 of the given chip.
691 return kona_i2c_read(adap, chip, 0x0, 1, &tmp, 1);
694 static uint kona_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
696 struct bcm_kona_i2c_dev *dev = kona_get_dev(adap);
697 return bcm_kona_i2c_assign_bus_speed(dev, speed);
701 * Register kona i2c adapters. Keep the order below so
702 * that the bus number matches the adapter number.
704 #define DEF_ADAPTER(num) \
705 U_BOOT_I2C_ADAP_COMPLETE(kona##num, kona_i2c_init, kona_i2c_probe, \
706 kona_i2c_read, kona_i2c_write, \
707 kona_i2c_set_bus_speed, DEF_SPD, 0x00, num)
709 #ifdef CONFIG_SYS_I2C_BASE0
712 #ifdef CONFIG_SYS_I2C_BASE1
715 #ifdef CONFIG_SYS_I2C_BASE2
718 #ifdef CONFIG_SYS_I2C_BASE3
721 #ifdef CONFIG_SYS_I2C_BASE4
724 #ifdef CONFIG_SYS_I2C_BASE5