1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Broadcom
13 #include "iproc_i2c.h"
15 DECLARE_GLOBAL_DATA_PTR;
17 struct iproc_i2c_regs {
24 u32 blnks[6]; /* Not to be used */
36 struct iproc_i2c_regs __iomem *base; /* register base */
41 /* Function to read a value from specified register. */
42 static unsigned int iproc_i2c_reg_read(u32 *reg_addr)
46 val = readl((void *)(reg_addr));
47 return cpu_to_le32(val);
50 /* Function to write a value ('val') in to a specified register. */
51 static int iproc_i2c_reg_write(u32 *reg_addr, unsigned int val)
53 val = cpu_to_le32(val);
54 writel(val, (void *)(reg_addr));
59 static int iproc_dump_i2c_regs(struct iproc_i2c *bus_prvdata)
61 struct iproc_i2c_regs *base = bus_prvdata->base;
64 debug("\n----------------------------------------------\n");
65 debug("%s: Dumping SMBus registers...\n", __func__);
67 regval = iproc_i2c_reg_read(&base->cfg_reg);
68 debug("CCB_SMB_CFG_REG=0x%08X\n", regval);
70 regval = iproc_i2c_reg_read(&base->timg_cfg);
71 debug("CCB_SMB_TIMGCFG_REG=0x%08X\n", regval);
73 regval = iproc_i2c_reg_read(&base->addr_reg);
74 debug("CCB_SMB_ADDR_REG=0x%08X\n", regval);
76 regval = iproc_i2c_reg_read(&base->mstr_fifo_ctrl);
77 debug("CCB_SMB_MSTRFIFOCTL_REG=0x%08X\n", regval);
79 regval = iproc_i2c_reg_read(&base->slv_fifo_ctrl);
80 debug("CCB_SMB_SLVFIFOCTL_REG=0x%08X\n", regval);
82 regval = iproc_i2c_reg_read(&base->bitbng_ctrl);
83 debug("CCB_SMB_BITBANGCTL_REG=0x%08X\n", regval);
85 regval = iproc_i2c_reg_read(&base->mstr_cmd);
86 debug("CCB_SMB_MSTRCMD_REG=0x%08X\n", regval);
88 regval = iproc_i2c_reg_read(&base->slv_cmd);
89 debug("CCB_SMB_SLVCMD_REG=0x%08X\n", regval);
91 regval = iproc_i2c_reg_read(&base->evt_en);
92 debug("CCB_SMB_EVTEN_REG=0x%08X\n", regval);
94 regval = iproc_i2c_reg_read(&base->evt_sts);
95 debug("CCB_SMB_EVTSTS_REG=0x%08X\n", regval);
97 regval = iproc_i2c_reg_read(&base->mstr_datawr);
98 debug("CCB_SMB_MSTRDATAWR_REG=0x%08X\n", regval);
100 regval = iproc_i2c_reg_read(&base->mstr_datard);
101 debug("CCB_SMB_MSTRDATARD_REG=0x%08X\n", regval);
103 regval = iproc_i2c_reg_read(&base->slv_datawr);
104 debug("CCB_SMB_SLVDATAWR_REG=0x%08X\n", regval);
106 regval = iproc_i2c_reg_read(&base->slv_datard);
107 debug("CCB_SMB_SLVDATARD_REG=0x%08X\n", regval);
109 debug("----------------------------------------------\n\n");
113 static int iproc_dump_i2c_regs(struct iproc_i2c *bus_prvdata)
120 * Function to ensure that the previous transaction was completed before
121 * initiating a new transaction. It can also be used in polling mode to
122 * check status of completion of a command
124 static int iproc_i2c_startbusy_wait(struct iproc_i2c *bus_prvdata)
126 struct iproc_i2c_regs *base = bus_prvdata->base;
129 regval = iproc_i2c_reg_read(&base->mstr_cmd);
131 /* Check if an operation is in progress. During probe it won't be.
132 * But when shutdown/remove was called we want to make sure that
133 * the transaction in progress completed
135 if (regval & CCB_SMB_MSTRSTARTBUSYCMD_MASK) {
141 regval = iproc_i2c_reg_read(&base->mstr_cmd);
143 /* If start-busy bit cleared, exit the loop */
144 } while ((regval & CCB_SMB_MSTRSTARTBUSYCMD_MASK) &&
145 (i < IPROC_SMB_MAX_RETRIES));
147 if (i >= IPROC_SMB_MAX_RETRIES) {
148 pr_err("%s: START_BUSY bit didn't clear, exiting\n",
157 * This function set clock frequency for SMBus block. As per hardware
158 * engineering, the clock frequency can be changed dynamically.
160 static int iproc_i2c_set_clk_freq(struct iproc_i2c *bus_prvdata)
162 struct iproc_i2c_regs *base = bus_prvdata->base;
165 regval = iproc_i2c_reg_read(&base->timg_cfg);
167 switch (bus_prvdata->bus_speed) {
168 case I2C_SPEED_STANDARD_RATE:
169 regval &= ~CCB_SMB_TIMGCFG_MODE400_MASK;
172 case I2C_SPEED_FAST_RATE:
173 regval |= CCB_SMB_TIMGCFG_MODE400_MASK;
180 iproc_i2c_reg_write(&base->timg_cfg, regval);
184 static int iproc_i2c_init(struct udevice *bus)
186 struct iproc_i2c *bus_prvdata = dev_get_priv(bus);
187 struct iproc_i2c_regs *base = bus_prvdata->base;
190 debug("\nEntering %s\n", __func__);
192 /* Put controller in reset */
193 regval = iproc_i2c_reg_read(&base->cfg_reg);
194 regval |= CCB_SMB_CFG_RST_MASK;
195 regval &= ~CCB_SMB_CFG_SMBEN_MASK;
196 iproc_i2c_reg_write(&base->cfg_reg, regval);
198 /* Wait 100 usec as per spec */
201 /* bring controller out of reset */
202 regval &= ~CCB_SMB_CFG_RST_MASK;
203 iproc_i2c_reg_write(&base->cfg_reg, regval);
205 /* Flush Tx, Rx FIFOs. Note we are setting the Rx FIFO threshold to 0.
206 * May be OK since we are setting RX_EVENT and RX_FIFO_FULL interrupts
208 regval = CCB_SMB_MSTRRXFIFOFLSH_MASK | CCB_SMB_MSTRTXFIFOFLSH_MASK;
209 iproc_i2c_reg_write(&base->mstr_fifo_ctrl, regval);
211 /* Enable SMbus block. Note, we are setting MASTER_RETRY_COUNT to zero
212 * since there will be only one master
214 regval = iproc_i2c_reg_read(&base->cfg_reg);
215 regval |= CCB_SMB_CFG_SMBEN_MASK;
216 iproc_i2c_reg_write(&base->cfg_reg, regval);
218 /* Set default clock frequency */
219 iproc_i2c_set_clk_freq(bus_prvdata);
222 iproc_i2c_reg_write(&base->evt_en, 0);
224 /* Clear intrs (W1TC) */
225 regval = iproc_i2c_reg_read(&base->evt_sts);
226 iproc_i2c_reg_write(&base->evt_sts, regval);
228 bus_prvdata->i2c_init_done = 1;
230 iproc_dump_i2c_regs(bus_prvdata);
231 debug("%s: Init successful\n", __func__);
237 * This function copies data to SMBus's Tx FIFO. Valid for write transactions
240 * base_addr: Mapped address of this SMBus instance
241 * dev_addr: SMBus (I2C) device address. We are assuming 7-bit addresses
243 * info: Data to copy in to Tx FIFO. For read commands, the size should be
244 * set to zero by the caller
247 static void iproc_i2c_write_trans_data(struct iproc_i2c *bus_prvdata,
248 unsigned short dev_addr,
249 struct iproc_xact_info *info)
251 struct iproc_i2c_regs *base = bus_prvdata->base;
254 unsigned int num_data_bytes = 0;
256 debug("%s: dev_addr=0x%X cmd_valid=%d cmd=0x%02x size=%u proto=%d buf[] %x\n",
257 __func__, dev_addr, info->cmd_valid,
258 info->command, info->size, info->smb_proto, info->data[0]);
260 /* Write SMBus device address first */
261 /* Note, we are assuming 7-bit addresses for now. For 10-bit addresses,
262 * we may have one more write to send the upper 3 bits of 10-bit addr
264 iproc_i2c_reg_write(&base->mstr_datawr, dev_addr);
266 /* If the protocol needs command code, copy it */
268 iproc_i2c_reg_write(&base->mstr_datawr, info->command);
270 /* Depending on the SMBus protocol, we need to write additional
271 * transaction data in to Tx FIFO. Refer to section 5.5 of SMBus
272 * spec for sequence for a transaction
274 switch (info->smb_proto) {
275 case SMBUS_PROT_RECV_BYTE:
276 /* No additional data to be written */
280 case SMBUS_PROT_SEND_BYTE:
281 num_data_bytes = info->size;
284 case SMBUS_PROT_RD_BYTE:
285 case SMBUS_PROT_RD_WORD:
286 case SMBUS_PROT_BLK_RD:
287 /* Write slave address with R/W~ set (bit #0) */
288 iproc_i2c_reg_write(&base->mstr_datawr,
293 case SMBUS_PROT_BLK_WR_BLK_RD_PROC_CALL:
294 iproc_i2c_reg_write(&base->mstr_datawr,
296 CCB_SMB_MSTRWRSTS_MASK);
300 case SMBUS_PROT_WR_BYTE:
301 case SMBUS_PROT_WR_WORD:
302 /* No additional bytes to be written.
303 * Data portion is written in the
306 num_data_bytes = info->size;
309 case SMBUS_PROT_BLK_WR:
310 /* 3rd byte is byte count */
311 iproc_i2c_reg_write(&base->mstr_datawr, info->size);
312 num_data_bytes = info->size;
319 /* Copy actual data from caller, next. In general, for reads,
322 for (i = 0; num_data_bytes; --num_data_bytes, i++) {
323 /* For the last byte, set MASTER_WR_STATUS bit */
324 regval = (num_data_bytes == 1) ?
325 info->data[i] | CCB_SMB_MSTRWRSTS_MASK :
328 iproc_i2c_reg_write(&base->mstr_datawr, regval);
332 static int iproc_i2c_data_send(struct iproc_i2c *bus_prvdata,
334 struct iproc_xact_info *info)
336 struct iproc_i2c_regs *base = bus_prvdata->base;
340 /* Make sure the previous transaction completed */
341 rc = iproc_i2c_startbusy_wait(bus_prvdata);
344 pr_err("%s: Send: bus is busy, exiting\n", __func__);
348 /* Write transaction bytes to Tx FIFO */
349 iproc_i2c_write_trans_data(bus_prvdata, addr, info);
351 /* Program master command register (0x30) with protocol type and set
352 * start_busy_command bit to initiate the write transaction
354 regval = (info->smb_proto << CCB_SMB_MSTRSMBUSPROTO_SHIFT) |
355 CCB_SMB_MSTRSTARTBUSYCMD_MASK;
357 iproc_i2c_reg_write(&base->mstr_cmd, regval);
359 /* Check for Master status */
360 regval = iproc_i2c_reg_read(&base->mstr_cmd);
361 while (regval & CCB_SMB_MSTRSTARTBUSYCMD_MASK) {
365 regval = iproc_i2c_reg_read(&base->mstr_cmd);
368 /* If start_busy bit cleared, check if there are any errors */
369 if (!(regval & CCB_SMB_MSTRSTARTBUSYCMD_MASK)) {
370 /* start_busy bit cleared, check master_status field now */
371 regval &= CCB_SMB_MSTRSTS_MASK;
372 regval >>= CCB_SMB_MSTRSTS_SHIFT;
374 if (regval != MSTR_STS_XACT_SUCCESS) {
375 /* Error We can flush Tx FIFO here */
376 pr_err("%s: ERROR: Error in transaction %u, exiting\n",
385 static int iproc_i2c_data_recv(struct iproc_i2c *bus_prvdata,
387 struct iproc_xact_info *info,
388 unsigned int *num_bytes_read)
390 struct iproc_i2c_regs *base = bus_prvdata->base;
394 /* Make sure the previous transaction completed */
395 rc = iproc_i2c_startbusy_wait(bus_prvdata);
398 pr_err("%s: Receive: Bus is busy, exiting\n", __func__);
402 /* Program all transaction bytes into master Tx FIFO */
403 iproc_i2c_write_trans_data(bus_prvdata, addr, info);
405 /* Program master command register (0x30) with protocol type and set
406 * start_busy_command bit to initiate the write transaction
408 regval = (info->smb_proto << CCB_SMB_MSTRSMBUSPROTO_SHIFT) |
409 CCB_SMB_MSTRSTARTBUSYCMD_MASK | info->size;
411 iproc_i2c_reg_write(&base->mstr_cmd, regval);
413 /* Check for Master status */
414 regval = iproc_i2c_reg_read(&base->mstr_cmd);
415 while (regval & CCB_SMB_MSTRSTARTBUSYCMD_MASK) {
419 regval = iproc_i2c_reg_read(&base->mstr_cmd);
422 /* If start_busy bit cleared, check if there are any errors */
423 if (!(regval & CCB_SMB_MSTRSTARTBUSYCMD_MASK)) {
424 /* start_busy bit cleared, check master_status field now */
425 regval &= CCB_SMB_MSTRSTS_MASK;
426 regval >>= CCB_SMB_MSTRSTS_SHIFT;
428 if (regval != MSTR_STS_XACT_SUCCESS) {
429 /* We can flush Tx FIFO here */
430 pr_err("%s: Error in transaction %d, exiting\n",
436 /* Read received byte(s), after TX out address etc */
437 regval = iproc_i2c_reg_read(&base->mstr_datard);
439 /* For block read, protocol (hw) returns byte count,
442 if (info->smb_proto == SMBUS_PROT_BLK_RD) {
445 *num_bytes_read = regval & CCB_SMB_MSTRRDDATA_MASK;
447 /* Limit to reading a max of 32 bytes only; just a safeguard.
448 * If # bytes read is a number > 32, check transaction set up,
449 * and contact hw engg. Assumption: PEC is disabled
452 (i < *num_bytes_read) && (i < I2C_SMBUS_BLOCK_MAX);
454 /* Read Rx FIFO for data bytes */
455 regval = iproc_i2c_reg_read(&base->mstr_datard);
456 info->data[i] = regval & CCB_SMB_MSTRRDDATA_MASK;
460 *info->data = regval & CCB_SMB_MSTRRDDATA_MASK;
467 static int i2c_write_byte(struct iproc_i2c *bus_prvdata,
468 u8 devaddr, u8 regoffset, u8 value)
471 struct iproc_xact_info info;
476 info.command = (unsigned char)regoffset;
480 info.smb_proto = SMBUS_PROT_WR_BYTE;
481 /* Refer to i2c_smbus_write_byte params passed. */
482 rc = iproc_i2c_data_send(bus_prvdata, devaddr, &info);
485 pr_err("%s: %s error accessing device 0x%X\n",
486 __func__, "Write", devaddr);
493 int i2c_write(struct udevice *bus,
494 uchar chip, uint regaddr, int alen, uchar *buffer, int len)
496 struct iproc_i2c *bus_prvdata = dev_get_priv(bus);
501 pr_err("I2C write: address out of range\n");
506 pr_err("I2C write: Need offset addr and value\n");
510 /* buffer contains offset addr followed by value to be written */
515 for (i = 0; i < data_len; i++) {
516 if (i2c_write_byte(bus_prvdata, chip, regaddr + i, data[i])) {
517 pr_err("I2C write (%d): I/O error\n", i);
526 static int i2c_read_byte(struct iproc_i2c *bus_prvdata,
527 u8 devaddr, u8 regoffset, u8 *value)
530 struct iproc_xact_info info;
531 unsigned int num_bytes_read = 0;
536 info.command = (unsigned char)regoffset;
540 info.smb_proto = SMBUS_PROT_RD_BYTE;
541 /* Refer to i2c_smbus_read_byte for params passed. */
542 rc = iproc_i2c_data_recv(bus_prvdata, devaddr, &info, &num_bytes_read);
545 pr_err("%s: %s error accessing device 0x%X\n",
546 __func__, "Read", devaddr);
553 int i2c_read(struct udevice *bus,
554 uchar chip, uint addr, int alen, uchar *buffer, int len)
556 struct iproc_i2c *bus_prvdata = dev_get_priv(bus);
560 pr_err("I2C read: address out of range\n");
564 for (i = 0; i < len; i++) {
565 if (i2c_read_byte(bus_prvdata, chip, addr + i, &buffer[i])) {
566 pr_err("I2C read: I/O error\n");
575 static int iproc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
579 debug("%s: %d messages\n", __func__, nmsgs);
581 for (; nmsgs > 0; nmsgs--, msg++) {
582 if (msg->flags & I2C_M_RD)
583 ret = i2c_read(bus, msg->addr, 0, 0,
586 ret = i2c_write(bus, msg->addr, 0, 0,
593 static int iproc_i2c_probe_chip(struct udevice *bus, uint chip_addr,
596 struct iproc_i2c *bus_prvdata = dev_get_priv(bus);
597 struct iproc_i2c_regs *base = bus_prvdata->base;
600 debug("\n%s: Entering chip probe\n", __func__);
602 /* Init internal regs, disable intrs (and then clear intrs), set fifo
605 if (!bus_prvdata->i2c_init_done)
608 regval = (chip_addr << 1);
609 iproc_i2c_reg_write(&base->mstr_datawr, regval);
610 regval = ((SMBUS_PROT_QUICK_CMD << CCB_SMB_MSTRSMBUSPROTO_SHIFT) |
611 (1 << CCB_SMB_MSTRSTARTBUSYCMD_SHIFT));
612 iproc_i2c_reg_write(&base->mstr_cmd, regval);
616 regval = iproc_i2c_reg_read(&base->mstr_cmd);
617 regval &= CCB_SMB_MSTRSTARTBUSYCMD_MASK;
620 regval = iproc_i2c_reg_read(&base->mstr_cmd);
622 if ((regval & CCB_SMB_MSTRSTS_MASK) != 0)
625 iproc_dump_i2c_regs(bus_prvdata);
626 debug("%s: chip probe successful\n", __func__);
631 static int iproc_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
633 struct iproc_i2c *bus_prvdata = dev_get_priv(bus);
635 bus_prvdata->bus_speed = speed;
636 return iproc_i2c_set_clk_freq(bus_prvdata);
640 * i2c_get_bus_speed - get i2c bus speed
642 * This function returns the speed of operation in Hz
644 int iproc_i2c_get_bus_speed(struct udevice *bus)
646 struct iproc_i2c *bus_prvdata = dev_get_priv(bus);
647 struct iproc_i2c_regs *base = bus_prvdata->base;
651 regval = iproc_i2c_reg_read(&base->timg_cfg);
652 regval = (regval & CCB_SMB_TIMGCFG_MODE400_MASK) >>
653 CCB_SMB_TIMGCFG_MODE400_SHIFT;
657 ret = I2C_SPEED_STANDARD_RATE;
660 ret = I2C_SPEED_FAST_RATE;
670 static int iproc_i2c_probe(struct udevice *bus)
672 return iproc_i2c_init(bus);
675 static int iproc_i2c_ofdata_to_platdata(struct udevice *bus)
677 struct iproc_i2c *bus_prvdata = dev_get_priv(bus);
678 int node = dev_of_offset(bus);
679 const void *blob = gd->fdt_blob;
681 bus_prvdata->base = map_physmem(devfdt_get_addr(bus),
685 bus_prvdata->bus_speed =
686 fdtdec_get_int(blob, node, "bus-frequency",
687 I2C_SPEED_STANDARD_RATE);
692 static const struct dm_i2c_ops iproc_i2c_ops = {
693 .xfer = iproc_i2c_xfer,
694 .probe_chip = iproc_i2c_probe_chip,
695 .set_bus_speed = iproc_i2c_set_bus_speed,
696 .get_bus_speed = iproc_i2c_get_bus_speed,
699 static const struct udevice_id iproc_i2c_ids[] = {
700 { .compatible = "brcm,iproc-i2c" },
704 U_BOOT_DRIVER(iproc_i2c) = {
707 .of_match = iproc_i2c_ids,
708 .ofdata_to_platdata = iproc_i2c_ofdata_to_platdata,
709 .probe = iproc_i2c_probe,
710 .priv_auto_alloc_size = sizeof(struct iproc_i2c),
711 .ops = &iproc_i2c_ops,
712 .flags = DM_FLAG_PRE_RELOC,