1 // SPDX-License-Identifier: GPL-2.0+
4 * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
13 #include <gdsys_fpga.h>
16 #include <asm/unaligned.h>
26 u16 interrupt_enable_control;
27 u16 write_mailbox_ext;
33 #define ihs_i2c_set(map, member, val) \
34 regmap_set(map, struct ihs_i2c_regs, member, val)
36 #define ihs_i2c_get(map, member, valp) \
37 regmap_get(map, struct ihs_i2c_regs, member, valp)
39 #else /* !CONFIG_DM_I2C */
40 DECLARE_GLOBAL_DATA_PTR;
42 #ifdef CONFIG_SYS_I2C_IHS_DUAL
44 #define I2C_SET_REG(fld, val) \
46 if (I2C_ADAP_HWNR & 0x10) \
47 FPGA_SET_REG(I2C_ADAP_HWNR & 0xf, i2c1.fld, val); \
49 FPGA_SET_REG(I2C_ADAP_HWNR, i2c0.fld, val); \
52 #define I2C_SET_REG(fld, val) \
53 FPGA_SET_REG(I2C_ADAP_HWNR, i2c0.fld, val)
56 #ifdef CONFIG_SYS_I2C_IHS_DUAL
57 #define I2C_GET_REG(fld, val) \
59 if (I2C_ADAP_HWNR & 0x10) \
60 FPGA_GET_REG(I2C_ADAP_HWNR & 0xf, i2c1.fld, val); \
62 FPGA_GET_REG(I2C_ADAP_HWNR, i2c0.fld, val); \
65 #define I2C_GET_REG(fld, val) \
66 FPGA_GET_REG(I2C_ADAP_HWNR, i2c0.fld, val)
68 #endif /* CONFIG_DM_I2C */
71 I2CINT_ERROR_EV = BIT(13),
72 I2CINT_TRANSMIT_EV = BIT(14),
73 I2CINT_RECEIVE_EV = BIT(15),
78 I2CMB_WRITE = 1 << 10,
79 I2CMB_1BYTE = 0 << 11,
80 I2CMB_2BYTE = 1 << 11,
81 I2CMB_DONT_HOLD_BUS = 0 << 13,
82 I2CMB_HOLD_BUS = 1 << 13,
83 I2CMB_NATIVE = 2 << 14,
92 static int wait_for_int(struct udevice *dev, int read)
94 static int wait_for_int(bool read)
100 struct ihs_i2c_priv *priv = dev_get_priv(dev);
104 ihs_i2c_get(priv->map, interrupt_status, &val);
106 I2C_GET_REG(interrupt_status, &val);
108 /* Wait until error or receive/transmit interrupt was raised */
109 while (!(val & (I2CINT_ERROR_EV
110 | (read ? I2CINT_RECEIVE_EV : I2CINT_TRANSMIT_EV)))) {
113 debug("%s: timed out\n", __func__);
117 ihs_i2c_get(priv->map, interrupt_status, &val);
119 I2C_GET_REG(interrupt_status, &val);
123 return (val & I2CINT_ERROR_EV) ? -EIO : 0;
127 static int ihs_i2c_transfer(struct udevice *dev, uchar chip,
128 uchar *buffer, int len, int read, bool is_last)
130 static int ihs_i2c_transfer(uchar chip, uchar *buffer, int len, bool read,
138 struct ihs_i2c_priv *priv = dev_get_priv(dev);
141 /* Clear interrupt status */
142 data = I2CINT_ERROR_EV | I2CINT_RECEIVE_EV | I2CINT_TRANSMIT_EV;
144 ihs_i2c_set(priv->map, interrupt_status, data);
145 ihs_i2c_get(priv->map, interrupt_status, &val);
147 I2C_SET_REG(interrupt_status, data);
148 I2C_GET_REG(interrupt_status, &val);
151 /* If we want to write and have data, write the bytes to the mailbox */
156 val |= buffer[1] << 8;
158 ihs_i2c_set(priv->map, write_mailbox_ext, val);
160 I2C_SET_REG(write_mailbox_ext, val);
165 | (read ? 0 : I2CMB_WRITE)
167 | ((len > 1) ? I2CMB_2BYTE : 0)
168 | (is_last ? 0 : I2CMB_HOLD_BUS);
171 ihs_i2c_set(priv->map, write_mailbox, data);
173 I2C_SET_REG(write_mailbox, data);
177 res = wait_for_int(dev, read);
179 res = wait_for_int(read);
182 if (res == -ETIMEDOUT)
183 debug("%s: time out while waiting for event\n", __func__);
188 /* If we want to read, get the bytes from the mailbox */
191 ihs_i2c_get(priv->map, read_mailbox_ext, &val);
193 I2C_GET_REG(read_mailbox_ext, &val);
195 buffer[0] = val & 0xff;
197 buffer[1] = val >> 8;
204 static int ihs_i2c_send_buffer(struct udevice *dev, uchar chip, u8 *data, int len, bool hold_bus, int read)
206 static int ihs_i2c_send_buffer(uchar chip, u8 *data, int len, bool hold_bus,
213 int transfer = min(len, 2);
214 bool is_last = len <= transfer;
217 res = ihs_i2c_transfer(dev, chip, data, transfer, read,
218 hold_bus ? false : is_last);
220 res = ihs_i2c_transfer(chip, data, transfer, read,
221 hold_bus ? false : is_last);
234 static int ihs_i2c_address(struct udevice *dev, uchar chip, u8 *addr, int alen,
237 static int ihs_i2c_address(uchar chip, u8 *addr, int alen, bool hold_bus)
241 return ihs_i2c_send_buffer(dev, chip, addr, alen, hold_bus, I2COP_WRITE);
243 return ihs_i2c_send_buffer(chip, addr, alen, hold_bus, I2COP_WRITE);
248 static int ihs_i2c_access(struct udevice *dev, uchar chip, u8 *addr,
249 int alen, uchar *buffer, int len, int read)
251 static int ihs_i2c_access(struct i2c_adapter *adap, uchar chip, u8 *addr,
252 int alen, uchar *buffer, int len, int read)
257 /* Don't hold the bus if length of data to send/receive is zero */
262 res = ihs_i2c_address(dev, chip, addr, alen, len);
264 res = ihs_i2c_address(chip, addr, alen, len);
270 return ihs_i2c_send_buffer(dev, chip, buffer, len, false, read);
272 return ihs_i2c_send_buffer(chip, buffer, len, false, read);
278 int ihs_i2c_probe(struct udevice *bus)
280 struct ihs_i2c_priv *priv = dev_get_priv(bus);
282 regmap_init_mem(dev_ofnode(bus), &priv->map);
287 static int ihs_i2c_set_bus_speed(struct udevice *bus, uint speed)
289 struct ihs_i2c_priv *priv = dev_get_priv(bus);
291 if (speed != priv->speed && priv->speed != 0)
299 static int ihs_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
301 struct i2c_msg *dmsg, *omsg, dummy;
303 memset(&dummy, 0, sizeof(struct i2c_msg));
305 /* We expect either two messages (one with an offset and one with the
306 * actucal data) or one message (just data)
308 if (nmsgs > 2 || nmsgs == 0) {
309 debug("%s: Only one or two messages are supported\n", __func__);
313 omsg = nmsgs == 1 ? &dummy : msg;
314 dmsg = nmsgs == 1 ? msg : msg + 1;
316 if (dmsg->flags & I2C_M_RD)
317 return ihs_i2c_access(bus, dmsg->addr, omsg->buf,
318 omsg->len, dmsg->buf, dmsg->len,
321 return ihs_i2c_access(bus, dmsg->addr, omsg->buf,
322 omsg->len, dmsg->buf, dmsg->len,
326 static int ihs_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
332 res = ihs_i2c_transfer(bus, chip_addr, buffer, 0, I2COP_READ, true);
339 static const struct dm_i2c_ops ihs_i2c_ops = {
340 .xfer = ihs_i2c_xfer,
341 .probe_chip = ihs_i2c_probe_chip,
342 .set_bus_speed = ihs_i2c_set_bus_speed,
345 static const struct udevice_id ihs_i2c_ids[] = {
346 { .compatible = "gdsys,ihs_i2cmaster", },
350 U_BOOT_DRIVER(i2c_ihs) = {
353 .of_match = ihs_i2c_ids,
354 .probe = ihs_i2c_probe,
355 .priv_auto_alloc_size = sizeof(struct ihs_i2c_priv),
359 #else /* CONFIG_DM_I2C */
361 static void ihs_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
363 #ifdef CONFIG_SYS_I2C_INIT_BOARD
365 * Call board specific i2c bus reset routine before accessing the
366 * environment, which might be in a chip on that bus. For details
367 * about this problem see doc/I2C_Edge_Conditions.
373 static int ihs_i2c_probe(struct i2c_adapter *adap, uchar chip)
378 res = ihs_i2c_transfer(chip, buffer, 0, I2COP_READ, true);
385 static int ihs_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
386 int alen, uchar *buffer, int len)
390 put_unaligned_le32(addr, addr_bytes);
392 return ihs_i2c_access(adap, chip, addr_bytes, alen, buffer, len,
396 static int ihs_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
397 int alen, uchar *buffer, int len)
401 put_unaligned_le32(addr, addr_bytes);
403 return ihs_i2c_access(adap, chip, addr_bytes, alen, buffer, len,
407 static unsigned int ihs_i2c_set_bus_speed(struct i2c_adapter *adap,
410 if (speed != adap->speed)
416 * Register IHS i2c adapters
418 #ifdef CONFIG_SYS_I2C_IHS_CH0
419 U_BOOT_I2C_ADAP_COMPLETE(ihs0, ihs_i2c_init, ihs_i2c_probe,
420 ihs_i2c_read, ihs_i2c_write,
421 ihs_i2c_set_bus_speed,
422 CONFIG_SYS_I2C_IHS_SPEED_0,
423 CONFIG_SYS_I2C_IHS_SLAVE_0, 0)
424 #ifdef CONFIG_SYS_I2C_IHS_DUAL
425 U_BOOT_I2C_ADAP_COMPLETE(ihs0_1, ihs_i2c_init, ihs_i2c_probe,
426 ihs_i2c_read, ihs_i2c_write,
427 ihs_i2c_set_bus_speed,
428 CONFIG_SYS_I2C_IHS_SPEED_0_1,
429 CONFIG_SYS_I2C_IHS_SLAVE_0_1, 16)
432 #ifdef CONFIG_SYS_I2C_IHS_CH1
433 U_BOOT_I2C_ADAP_COMPLETE(ihs1, ihs_i2c_init, ihs_i2c_probe,
434 ihs_i2c_read, ihs_i2c_write,
435 ihs_i2c_set_bus_speed,
436 CONFIG_SYS_I2C_IHS_SPEED_1,
437 CONFIG_SYS_I2C_IHS_SLAVE_1, 1)
438 #ifdef CONFIG_SYS_I2C_IHS_DUAL
439 U_BOOT_I2C_ADAP_COMPLETE(ihs1_1, ihs_i2c_init, ihs_i2c_probe,
440 ihs_i2c_read, ihs_i2c_write,
441 ihs_i2c_set_bus_speed,
442 CONFIG_SYS_I2C_IHS_SPEED_1_1,
443 CONFIG_SYS_I2C_IHS_SLAVE_1_1, 17)
446 #ifdef CONFIG_SYS_I2C_IHS_CH2
447 U_BOOT_I2C_ADAP_COMPLETE(ihs2, ihs_i2c_init, ihs_i2c_probe,
448 ihs_i2c_read, ihs_i2c_write,
449 ihs_i2c_set_bus_speed,
450 CONFIG_SYS_I2C_IHS_SPEED_2,
451 CONFIG_SYS_I2C_IHS_SLAVE_2, 2)
452 #ifdef CONFIG_SYS_I2C_IHS_DUAL
453 U_BOOT_I2C_ADAP_COMPLETE(ihs2_1, ihs_i2c_init, ihs_i2c_probe,
454 ihs_i2c_read, ihs_i2c_write,
455 ihs_i2c_set_bus_speed,
456 CONFIG_SYS_I2C_IHS_SPEED_2_1,
457 CONFIG_SYS_I2C_IHS_SLAVE_2_1, 18)
460 #ifdef CONFIG_SYS_I2C_IHS_CH3
461 U_BOOT_I2C_ADAP_COMPLETE(ihs3, ihs_i2c_init, ihs_i2c_probe,
462 ihs_i2c_read, ihs_i2c_write,
463 ihs_i2c_set_bus_speed,
464 CONFIG_SYS_I2C_IHS_SPEED_3,
465 CONFIG_SYS_I2C_IHS_SLAVE_3, 3)
466 #ifdef CONFIG_SYS_I2C_IHS_DUAL
467 U_BOOT_I2C_ADAP_COMPLETE(ihs3_1, ihs_i2c_init, ihs_i2c_probe,
468 ihs_i2c_read, ihs_i2c_write,
469 ihs_i2c_set_bus_speed,
470 CONFIG_SYS_I2C_IHS_SPEED_3_1,
471 CONFIG_SYS_I2C_IHS_SLAVE_3_1, 19)
474 #endif /* CONFIG_DM_I2C */