1 // SPDX-License-Identifier: GPL-2.0+
13 #include <gdsys_fpga.h>
16 #include <asm/unaligned.h>
17 #include <linux/bitops.h>
18 #include <linux/delay.h>
28 u16 interrupt_enable_control;
29 u16 write_mailbox_ext;
35 #define ihs_i2c_set(map, member, val) \
36 regmap_set(map, struct ihs_i2c_regs, member, val)
38 #define ihs_i2c_get(map, member, valp) \
39 regmap_get(map, struct ihs_i2c_regs, member, valp)
41 #else /* !CONFIG_DM_I2C */
42 DECLARE_GLOBAL_DATA_PTR;
44 #ifdef CONFIG_SYS_I2C_IHS_DUAL
46 #define I2C_SET_REG(fld, val) \
48 if (I2C_ADAP_HWNR & 0x10) \
49 FPGA_SET_REG(I2C_ADAP_HWNR & 0xf, i2c1.fld, val); \
51 FPGA_SET_REG(I2C_ADAP_HWNR, i2c0.fld, val); \
54 #define I2C_SET_REG(fld, val) \
55 FPGA_SET_REG(I2C_ADAP_HWNR, i2c0.fld, val)
58 #ifdef CONFIG_SYS_I2C_IHS_DUAL
59 #define I2C_GET_REG(fld, val) \
61 if (I2C_ADAP_HWNR & 0x10) \
62 FPGA_GET_REG(I2C_ADAP_HWNR & 0xf, i2c1.fld, val); \
64 FPGA_GET_REG(I2C_ADAP_HWNR, i2c0.fld, val); \
67 #define I2C_GET_REG(fld, val) \
68 FPGA_GET_REG(I2C_ADAP_HWNR, i2c0.fld, val)
70 #endif /* CONFIG_DM_I2C */
73 I2CINT_ERROR_EV = BIT(13),
74 I2CINT_TRANSMIT_EV = BIT(14),
75 I2CINT_RECEIVE_EV = BIT(15),
80 I2CMB_WRITE = 1 << 10,
81 I2CMB_1BYTE = 0 << 11,
82 I2CMB_2BYTE = 1 << 11,
83 I2CMB_DONT_HOLD_BUS = 0 << 13,
84 I2CMB_HOLD_BUS = 1 << 13,
85 I2CMB_NATIVE = 2 << 14,
94 static int wait_for_int(struct udevice *dev, int read)
96 static int wait_for_int(bool read)
102 struct ihs_i2c_priv *priv = dev_get_priv(dev);
106 ihs_i2c_get(priv->map, interrupt_status, &val);
108 I2C_GET_REG(interrupt_status, &val);
110 /* Wait until error or receive/transmit interrupt was raised */
111 while (!(val & (I2CINT_ERROR_EV
112 | (read ? I2CINT_RECEIVE_EV : I2CINT_TRANSMIT_EV)))) {
115 debug("%s: timed out\n", __func__);
119 ihs_i2c_get(priv->map, interrupt_status, &val);
121 I2C_GET_REG(interrupt_status, &val);
125 return (val & I2CINT_ERROR_EV) ? -EIO : 0;
129 static int ihs_i2c_transfer(struct udevice *dev, uchar chip,
130 uchar *buffer, int len, int read, bool is_last)
132 static int ihs_i2c_transfer(uchar chip, uchar *buffer, int len, bool read,
140 struct ihs_i2c_priv *priv = dev_get_priv(dev);
143 /* Clear interrupt status */
144 data = I2CINT_ERROR_EV | I2CINT_RECEIVE_EV | I2CINT_TRANSMIT_EV;
146 ihs_i2c_set(priv->map, interrupt_status, data);
147 ihs_i2c_get(priv->map, interrupt_status, &val);
149 I2C_SET_REG(interrupt_status, data);
150 I2C_GET_REG(interrupt_status, &val);
153 /* If we want to write and have data, write the bytes to the mailbox */
158 val |= buffer[1] << 8;
160 ihs_i2c_set(priv->map, write_mailbox_ext, val);
162 I2C_SET_REG(write_mailbox_ext, val);
167 | (read ? 0 : I2CMB_WRITE)
169 | ((len > 1) ? I2CMB_2BYTE : 0)
170 | (is_last ? 0 : I2CMB_HOLD_BUS);
173 ihs_i2c_set(priv->map, write_mailbox, data);
175 I2C_SET_REG(write_mailbox, data);
179 res = wait_for_int(dev, read);
181 res = wait_for_int(read);
184 if (res == -ETIMEDOUT)
185 debug("%s: time out while waiting for event\n", __func__);
190 /* If we want to read, get the bytes from the mailbox */
193 ihs_i2c_get(priv->map, read_mailbox_ext, &val);
195 I2C_GET_REG(read_mailbox_ext, &val);
197 buffer[0] = val & 0xff;
199 buffer[1] = val >> 8;
206 static int ihs_i2c_send_buffer(struct udevice *dev, uchar chip, u8 *data, int len, bool hold_bus, int read)
208 static int ihs_i2c_send_buffer(uchar chip, u8 *data, int len, bool hold_bus,
215 int transfer = min(len, 2);
216 bool is_last = len <= transfer;
219 res = ihs_i2c_transfer(dev, chip, data, transfer, read,
220 hold_bus ? false : is_last);
222 res = ihs_i2c_transfer(chip, data, transfer, read,
223 hold_bus ? false : is_last);
236 static int ihs_i2c_address(struct udevice *dev, uchar chip, u8 *addr, int alen,
239 static int ihs_i2c_address(uchar chip, u8 *addr, int alen, bool hold_bus)
243 return ihs_i2c_send_buffer(dev, chip, addr, alen, hold_bus, I2COP_WRITE);
245 return ihs_i2c_send_buffer(chip, addr, alen, hold_bus, I2COP_WRITE);
250 static int ihs_i2c_access(struct udevice *dev, uchar chip, u8 *addr,
251 int alen, uchar *buffer, int len, int read)
253 static int ihs_i2c_access(struct i2c_adapter *adap, uchar chip, u8 *addr,
254 int alen, uchar *buffer, int len, int read)
259 /* Don't hold the bus if length of data to send/receive is zero */
264 res = ihs_i2c_address(dev, chip, addr, alen, len);
266 res = ihs_i2c_address(chip, addr, alen, len);
272 return ihs_i2c_send_buffer(dev, chip, buffer, len, false, read);
274 return ihs_i2c_send_buffer(chip, buffer, len, false, read);
280 int ihs_i2c_probe(struct udevice *bus)
282 struct ihs_i2c_priv *priv = dev_get_priv(bus);
284 regmap_init_mem(dev_ofnode(bus), &priv->map);
289 static int ihs_i2c_set_bus_speed(struct udevice *bus, uint speed)
291 struct ihs_i2c_priv *priv = dev_get_priv(bus);
293 if (speed != priv->speed && priv->speed != 0)
301 static int ihs_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
303 struct i2c_msg *dmsg, *omsg, dummy;
305 memset(&dummy, 0, sizeof(struct i2c_msg));
307 /* We expect either two messages (one with an offset and one with the
308 * actucal data) or one message (just data)
310 if (nmsgs > 2 || nmsgs == 0) {
311 debug("%s: Only one or two messages are supported\n", __func__);
315 omsg = nmsgs == 1 ? &dummy : msg;
316 dmsg = nmsgs == 1 ? msg : msg + 1;
318 if (dmsg->flags & I2C_M_RD)
319 return ihs_i2c_access(bus, dmsg->addr, omsg->buf,
320 omsg->len, dmsg->buf, dmsg->len,
323 return ihs_i2c_access(bus, dmsg->addr, omsg->buf,
324 omsg->len, dmsg->buf, dmsg->len,
328 static int ihs_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
334 res = ihs_i2c_transfer(bus, chip_addr, buffer, 0, I2COP_READ, true);
341 static const struct dm_i2c_ops ihs_i2c_ops = {
342 .xfer = ihs_i2c_xfer,
343 .probe_chip = ihs_i2c_probe_chip,
344 .set_bus_speed = ihs_i2c_set_bus_speed,
347 static const struct udevice_id ihs_i2c_ids[] = {
348 { .compatible = "gdsys,ihs_i2cmaster", },
352 U_BOOT_DRIVER(i2c_ihs) = {
355 .of_match = ihs_i2c_ids,
356 .probe = ihs_i2c_probe,
357 .priv_auto = sizeof(struct ihs_i2c_priv),
361 #else /* CONFIG_DM_I2C */
363 static void ihs_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
365 #ifdef CONFIG_SYS_I2C_INIT_BOARD
367 * Call board specific i2c bus reset routine before accessing the
368 * environment, which might be in a chip on that bus. For details
369 * about this problem see doc/I2C_Edge_Conditions.
375 static int ihs_i2c_probe(struct i2c_adapter *adap, uchar chip)
380 res = ihs_i2c_transfer(chip, buffer, 0, I2COP_READ, true);
387 static int ihs_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
388 int alen, uchar *buffer, int len)
392 put_unaligned_le32(addr, addr_bytes);
394 return ihs_i2c_access(adap, chip, addr_bytes, alen, buffer, len,
398 static int ihs_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
399 int alen, uchar *buffer, int len)
403 put_unaligned_le32(addr, addr_bytes);
405 return ihs_i2c_access(adap, chip, addr_bytes, alen, buffer, len,
409 static unsigned int ihs_i2c_set_bus_speed(struct i2c_adapter *adap,
412 if (speed != adap->speed)
418 * Register IHS i2c adapters
420 #ifdef CONFIG_SYS_I2C_IHS_CH0
421 U_BOOT_I2C_ADAP_COMPLETE(ihs0, ihs_i2c_init, ihs_i2c_probe,
422 ihs_i2c_read, ihs_i2c_write,
423 ihs_i2c_set_bus_speed,
424 CONFIG_SYS_I2C_IHS_SPEED_0,
425 CONFIG_SYS_I2C_IHS_SLAVE_0, 0)
426 #ifdef CONFIG_SYS_I2C_IHS_DUAL
427 U_BOOT_I2C_ADAP_COMPLETE(ihs0_1, ihs_i2c_init, ihs_i2c_probe,
428 ihs_i2c_read, ihs_i2c_write,
429 ihs_i2c_set_bus_speed,
430 CONFIG_SYS_I2C_IHS_SPEED_0_1,
431 CONFIG_SYS_I2C_IHS_SLAVE_0_1, 16)
434 #ifdef CONFIG_SYS_I2C_IHS_CH1
435 U_BOOT_I2C_ADAP_COMPLETE(ihs1, ihs_i2c_init, ihs_i2c_probe,
436 ihs_i2c_read, ihs_i2c_write,
437 ihs_i2c_set_bus_speed,
438 CONFIG_SYS_I2C_IHS_SPEED_1,
439 CONFIG_SYS_I2C_IHS_SLAVE_1, 1)
440 #ifdef CONFIG_SYS_I2C_IHS_DUAL
441 U_BOOT_I2C_ADAP_COMPLETE(ihs1_1, ihs_i2c_init, ihs_i2c_probe,
442 ihs_i2c_read, ihs_i2c_write,
443 ihs_i2c_set_bus_speed,
444 CONFIG_SYS_I2C_IHS_SPEED_1_1,
445 CONFIG_SYS_I2C_IHS_SLAVE_1_1, 17)
448 #ifdef CONFIG_SYS_I2C_IHS_CH2
449 U_BOOT_I2C_ADAP_COMPLETE(ihs2, ihs_i2c_init, ihs_i2c_probe,
450 ihs_i2c_read, ihs_i2c_write,
451 ihs_i2c_set_bus_speed,
452 CONFIG_SYS_I2C_IHS_SPEED_2,
453 CONFIG_SYS_I2C_IHS_SLAVE_2, 2)
454 #ifdef CONFIG_SYS_I2C_IHS_DUAL
455 U_BOOT_I2C_ADAP_COMPLETE(ihs2_1, ihs_i2c_init, ihs_i2c_probe,
456 ihs_i2c_read, ihs_i2c_write,
457 ihs_i2c_set_bus_speed,
458 CONFIG_SYS_I2C_IHS_SPEED_2_1,
459 CONFIG_SYS_I2C_IHS_SLAVE_2_1, 18)
462 #ifdef CONFIG_SYS_I2C_IHS_CH3
463 U_BOOT_I2C_ADAP_COMPLETE(ihs3, ihs_i2c_init, ihs_i2c_probe,
464 ihs_i2c_read, ihs_i2c_write,
465 ihs_i2c_set_bus_speed,
466 CONFIG_SYS_I2C_IHS_SPEED_3,
467 CONFIG_SYS_I2C_IHS_SLAVE_3, 3)
468 #ifdef CONFIG_SYS_I2C_IHS_DUAL
469 U_BOOT_I2C_ADAP_COMPLETE(ihs3_1, ihs_i2c_init, ihs_i2c_probe,
470 ihs_i2c_read, ihs_i2c_write,
471 ihs_i2c_set_bus_speed,
472 CONFIG_SYS_I2C_IHS_SPEED_3_1,
473 CONFIG_SYS_I2C_IHS_SLAVE_3_1, 19)
476 #endif /* CONFIG_DM_I2C */