1 // SPDX-License-Identifier: GPL-2.0+
11 #include <asm/global_data.h>
12 #include <asm/unaligned.h>
13 #include <linux/bitops.h>
14 #include <linux/delay.h>
23 u16 interrupt_enable_control;
24 u16 write_mailbox_ext;
30 #define ihs_i2c_set(map, member, val) \
31 regmap_set(map, struct ihs_i2c_regs, member, val)
33 #define ihs_i2c_get(map, member, valp) \
34 regmap_get(map, struct ihs_i2c_regs, member, valp)
37 I2CINT_ERROR_EV = BIT(13),
38 I2CINT_TRANSMIT_EV = BIT(14),
39 I2CINT_RECEIVE_EV = BIT(15),
44 I2CMB_WRITE = 1 << 10,
45 I2CMB_1BYTE = 0 << 11,
46 I2CMB_2BYTE = 1 << 11,
47 I2CMB_DONT_HOLD_BUS = 0 << 13,
48 I2CMB_HOLD_BUS = 1 << 13,
49 I2CMB_NATIVE = 2 << 14,
57 static int wait_for_int(struct udevice *dev, int read)
61 struct ihs_i2c_priv *priv = dev_get_priv(dev);
63 ihs_i2c_get(priv->map, interrupt_status, &val);
64 /* Wait until error or receive/transmit interrupt was raised */
65 while (!(val & (I2CINT_ERROR_EV
66 | (read ? I2CINT_RECEIVE_EV : I2CINT_TRANSMIT_EV)))) {
69 debug("%s: timed out\n", __func__);
72 ihs_i2c_get(priv->map, interrupt_status, &val);
75 return (val & I2CINT_ERROR_EV) ? -EIO : 0;
78 static int ihs_i2c_transfer(struct udevice *dev, uchar chip,
79 uchar *buffer, int len, int read, bool is_last)
84 struct ihs_i2c_priv *priv = dev_get_priv(dev);
86 /* Clear interrupt status */
87 data = I2CINT_ERROR_EV | I2CINT_RECEIVE_EV | I2CINT_TRANSMIT_EV;
88 ihs_i2c_set(priv->map, interrupt_status, data);
89 ihs_i2c_get(priv->map, interrupt_status, &val);
91 /* If we want to write and have data, write the bytes to the mailbox */
96 val |= buffer[1] << 8;
97 ihs_i2c_set(priv->map, write_mailbox_ext, val);
101 | (read ? 0 : I2CMB_WRITE)
103 | ((len > 1) ? I2CMB_2BYTE : 0)
104 | (is_last ? 0 : I2CMB_HOLD_BUS);
106 ihs_i2c_set(priv->map, write_mailbox, data);
108 res = wait_for_int(dev, read);
110 if (res == -ETIMEDOUT)
111 debug("%s: time out while waiting for event\n", __func__);
116 /* If we want to read, get the bytes from the mailbox */
118 ihs_i2c_get(priv->map, read_mailbox_ext, &val);
119 buffer[0] = val & 0xff;
121 buffer[1] = val >> 8;
127 static int ihs_i2c_send_buffer(struct udevice *dev, uchar chip, u8 *data, int len, bool hold_bus, int read)
132 int transfer = min(len, 2);
133 bool is_last = len <= transfer;
135 res = ihs_i2c_transfer(dev, chip, data, transfer, read,
136 hold_bus ? false : is_last);
147 static int ihs_i2c_address(struct udevice *dev, uchar chip, u8 *addr, int alen,
150 return ihs_i2c_send_buffer(dev, chip, addr, alen, hold_bus, I2COP_WRITE);
153 static int ihs_i2c_access(struct udevice *dev, uchar chip, u8 *addr,
154 int alen, uchar *buffer, int len, int read)
158 /* Don't hold the bus if length of data to send/receive is zero */
162 res = ihs_i2c_address(dev, chip, addr, alen, len);
166 return ihs_i2c_send_buffer(dev, chip, buffer, len, false, read);
169 int ihs_i2c_probe(struct udevice *bus)
171 struct ihs_i2c_priv *priv = dev_get_priv(bus);
173 regmap_init_mem(dev_ofnode(bus), &priv->map);
178 static int ihs_i2c_set_bus_speed(struct udevice *bus, uint speed)
180 struct ihs_i2c_priv *priv = dev_get_priv(bus);
182 if (speed != priv->speed && priv->speed != 0)
190 static int ihs_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
192 struct i2c_msg *dmsg, *omsg, dummy;
194 memset(&dummy, 0, sizeof(struct i2c_msg));
196 /* We expect either two messages (one with an offset and one with the
197 * actual data) or one message (just data)
199 if (nmsgs > 2 || nmsgs == 0) {
200 debug("%s: Only one or two messages are supported\n", __func__);
204 omsg = nmsgs == 1 ? &dummy : msg;
205 dmsg = nmsgs == 1 ? msg : msg + 1;
207 if (dmsg->flags & I2C_M_RD)
208 return ihs_i2c_access(bus, dmsg->addr, omsg->buf,
209 omsg->len, dmsg->buf, dmsg->len,
212 return ihs_i2c_access(bus, dmsg->addr, omsg->buf,
213 omsg->len, dmsg->buf, dmsg->len,
217 static int ihs_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
223 res = ihs_i2c_transfer(bus, chip_addr, buffer, 0, I2COP_READ, true);
230 static const struct dm_i2c_ops ihs_i2c_ops = {
231 .xfer = ihs_i2c_xfer,
232 .probe_chip = ihs_i2c_probe_chip,
233 .set_bus_speed = ihs_i2c_set_bus_speed,
236 static const struct udevice_id ihs_i2c_ids[] = {
237 { .compatible = "gdsys,ihs_i2cmaster", },
241 U_BOOT_DRIVER(i2c_ihs) = {
244 .of_match = ihs_i2c_ids,
245 .probe = ihs_i2c_probe,
246 .priv_auto = sizeof(struct ihs_i2c_priv),