1 // SPDX-License-Identifier: GPL-2.0+
4 * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
13 #include <gdsys_fpga.h>
15 #include <asm/unaligned.h>
25 u16 interrupt_enable_control;
26 u16 write_mailbox_ext;
32 #define ihs_i2c_set(map, member, val) \
33 regmap_set(map, struct ihs_i2c_regs, member, val)
35 #define ihs_i2c_get(map, member, valp) \
36 regmap_get(map, struct ihs_i2c_regs, member, valp)
38 #else /* !CONFIG_DM_I2C */
39 DECLARE_GLOBAL_DATA_PTR;
41 #ifdef CONFIG_SYS_I2C_IHS_DUAL
43 #define I2C_SET_REG(fld, val) \
45 if (I2C_ADAP_HWNR & 0x10) \
46 FPGA_SET_REG(I2C_ADAP_HWNR & 0xf, i2c1.fld, val); \
48 FPGA_SET_REG(I2C_ADAP_HWNR, i2c0.fld, val); \
51 #define I2C_SET_REG(fld, val) \
52 FPGA_SET_REG(I2C_ADAP_HWNR, i2c0.fld, val)
55 #ifdef CONFIG_SYS_I2C_IHS_DUAL
56 #define I2C_GET_REG(fld, val) \
58 if (I2C_ADAP_HWNR & 0x10) \
59 FPGA_GET_REG(I2C_ADAP_HWNR & 0xf, i2c1.fld, val); \
61 FPGA_GET_REG(I2C_ADAP_HWNR, i2c0.fld, val); \
64 #define I2C_GET_REG(fld, val) \
65 FPGA_GET_REG(I2C_ADAP_HWNR, i2c0.fld, val)
67 #endif /* CONFIG_DM_I2C */
70 I2CINT_ERROR_EV = BIT(13),
71 I2CINT_TRANSMIT_EV = BIT(14),
72 I2CINT_RECEIVE_EV = BIT(15),
77 I2CMB_WRITE = 1 << 10,
78 I2CMB_1BYTE = 0 << 11,
79 I2CMB_2BYTE = 1 << 11,
80 I2CMB_DONT_HOLD_BUS = 0 << 13,
81 I2CMB_HOLD_BUS = 1 << 13,
82 I2CMB_NATIVE = 2 << 14,
91 static int wait_for_int(struct udevice *dev, int read)
93 static int wait_for_int(bool read)
99 struct ihs_i2c_priv *priv = dev_get_priv(dev);
103 ihs_i2c_get(priv->map, interrupt_status, &val);
105 I2C_GET_REG(interrupt_status, &val);
107 /* Wait until error or receive/transmit interrupt was raised */
108 while (!(val & (I2CINT_ERROR_EV
109 | (read ? I2CINT_RECEIVE_EV : I2CINT_TRANSMIT_EV)))) {
112 debug("%s: timed out\n", __func__);
116 ihs_i2c_get(priv->map, interrupt_status, &val);
118 I2C_GET_REG(interrupt_status, &val);
122 return (val & I2CINT_ERROR_EV) ? -EIO : 0;
126 static int ihs_i2c_transfer(struct udevice *dev, uchar chip,
127 uchar *buffer, int len, int read, bool is_last)
129 static int ihs_i2c_transfer(uchar chip, uchar *buffer, int len, bool read,
137 struct ihs_i2c_priv *priv = dev_get_priv(dev);
140 /* Clear interrupt status */
141 data = I2CINT_ERROR_EV | I2CINT_RECEIVE_EV | I2CINT_TRANSMIT_EV;
143 ihs_i2c_set(priv->map, interrupt_status, data);
144 ihs_i2c_get(priv->map, interrupt_status, &val);
146 I2C_SET_REG(interrupt_status, data);
147 I2C_GET_REG(interrupt_status, &val);
150 /* If we want to write and have data, write the bytes to the mailbox */
155 val |= buffer[1] << 8;
157 ihs_i2c_set(priv->map, write_mailbox_ext, val);
159 I2C_SET_REG(write_mailbox_ext, val);
164 | (read ? 0 : I2CMB_WRITE)
166 | ((len > 1) ? I2CMB_2BYTE : 0)
167 | (is_last ? 0 : I2CMB_HOLD_BUS);
170 ihs_i2c_set(priv->map, write_mailbox, data);
172 I2C_SET_REG(write_mailbox, data);
176 res = wait_for_int(dev, read);
178 res = wait_for_int(read);
181 if (res == -ETIMEDOUT)
182 debug("%s: time out while waiting for event\n", __func__);
187 /* If we want to read, get the bytes from the mailbox */
190 ihs_i2c_get(priv->map, read_mailbox_ext, &val);
192 I2C_GET_REG(read_mailbox_ext, &val);
194 buffer[0] = val & 0xff;
196 buffer[1] = val >> 8;
203 static int ihs_i2c_send_buffer(struct udevice *dev, uchar chip, u8 *data, int len, bool hold_bus, int read)
205 static int ihs_i2c_send_buffer(uchar chip, u8 *data, int len, bool hold_bus,
212 int transfer = min(len, 2);
213 bool is_last = len <= transfer;
216 res = ihs_i2c_transfer(dev, chip, data, transfer, read,
217 hold_bus ? false : is_last);
219 res = ihs_i2c_transfer(chip, data, transfer, read,
220 hold_bus ? false : is_last);
233 static int ihs_i2c_address(struct udevice *dev, uchar chip, u8 *addr, int alen,
236 static int ihs_i2c_address(uchar chip, u8 *addr, int alen, bool hold_bus)
240 return ihs_i2c_send_buffer(dev, chip, addr, alen, hold_bus, I2COP_WRITE);
242 return ihs_i2c_send_buffer(chip, addr, alen, hold_bus, I2COP_WRITE);
247 static int ihs_i2c_access(struct udevice *dev, uchar chip, u8 *addr,
248 int alen, uchar *buffer, int len, int read)
250 static int ihs_i2c_access(struct i2c_adapter *adap, uchar chip, u8 *addr,
251 int alen, uchar *buffer, int len, int read)
256 /* Don't hold the bus if length of data to send/receive is zero */
261 res = ihs_i2c_address(dev, chip, addr, alen, len);
263 res = ihs_i2c_address(chip, addr, alen, len);
269 return ihs_i2c_send_buffer(dev, chip, buffer, len, false, read);
271 return ihs_i2c_send_buffer(chip, buffer, len, false, read);
277 int ihs_i2c_probe(struct udevice *bus)
279 struct ihs_i2c_priv *priv = dev_get_priv(bus);
281 regmap_init_mem(dev_ofnode(bus), &priv->map);
286 static int ihs_i2c_set_bus_speed(struct udevice *bus, uint speed)
288 struct ihs_i2c_priv *priv = dev_get_priv(bus);
290 if (speed != priv->speed && priv->speed != 0)
298 static int ihs_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
300 struct i2c_msg *dmsg, *omsg, dummy;
302 memset(&dummy, 0, sizeof(struct i2c_msg));
304 /* We expect either two messages (one with an offset and one with the
305 * actucal data) or one message (just data)
307 if (nmsgs > 2 || nmsgs == 0) {
308 debug("%s: Only one or two messages are supported\n", __func__);
312 omsg = nmsgs == 1 ? &dummy : msg;
313 dmsg = nmsgs == 1 ? msg : msg + 1;
315 if (dmsg->flags & I2C_M_RD)
316 return ihs_i2c_access(bus, dmsg->addr, omsg->buf,
317 omsg->len, dmsg->buf, dmsg->len,
320 return ihs_i2c_access(bus, dmsg->addr, omsg->buf,
321 omsg->len, dmsg->buf, dmsg->len,
325 static int ihs_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
331 res = ihs_i2c_transfer(bus, chip_addr, buffer, 0, I2COP_READ, true);
338 static const struct dm_i2c_ops ihs_i2c_ops = {
339 .xfer = ihs_i2c_xfer,
340 .probe_chip = ihs_i2c_probe_chip,
341 .set_bus_speed = ihs_i2c_set_bus_speed,
344 static const struct udevice_id ihs_i2c_ids[] = {
345 { .compatible = "gdsys,ihs_i2cmaster", },
349 U_BOOT_DRIVER(i2c_ihs) = {
352 .of_match = ihs_i2c_ids,
353 .probe = ihs_i2c_probe,
354 .priv_auto_alloc_size = sizeof(struct ihs_i2c_priv),
358 #else /* CONFIG_DM_I2C */
360 static void ihs_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
362 #ifdef CONFIG_SYS_I2C_INIT_BOARD
364 * Call board specific i2c bus reset routine before accessing the
365 * environment, which might be in a chip on that bus. For details
366 * about this problem see doc/I2C_Edge_Conditions.
372 static int ihs_i2c_probe(struct i2c_adapter *adap, uchar chip)
377 res = ihs_i2c_transfer(chip, buffer, 0, I2COP_READ, true);
384 static int ihs_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
385 int alen, uchar *buffer, int len)
389 put_unaligned_le32(addr, addr_bytes);
391 return ihs_i2c_access(adap, chip, addr_bytes, alen, buffer, len,
395 static int ihs_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
396 int alen, uchar *buffer, int len)
400 put_unaligned_le32(addr, addr_bytes);
402 return ihs_i2c_access(adap, chip, addr_bytes, alen, buffer, len,
406 static unsigned int ihs_i2c_set_bus_speed(struct i2c_adapter *adap,
409 if (speed != adap->speed)
415 * Register IHS i2c adapters
417 #ifdef CONFIG_SYS_I2C_IHS_CH0
418 U_BOOT_I2C_ADAP_COMPLETE(ihs0, ihs_i2c_init, ihs_i2c_probe,
419 ihs_i2c_read, ihs_i2c_write,
420 ihs_i2c_set_bus_speed,
421 CONFIG_SYS_I2C_IHS_SPEED_0,
422 CONFIG_SYS_I2C_IHS_SLAVE_0, 0)
423 #ifdef CONFIG_SYS_I2C_IHS_DUAL
424 U_BOOT_I2C_ADAP_COMPLETE(ihs0_1, ihs_i2c_init, ihs_i2c_probe,
425 ihs_i2c_read, ihs_i2c_write,
426 ihs_i2c_set_bus_speed,
427 CONFIG_SYS_I2C_IHS_SPEED_0_1,
428 CONFIG_SYS_I2C_IHS_SLAVE_0_1, 16)
431 #ifdef CONFIG_SYS_I2C_IHS_CH1
432 U_BOOT_I2C_ADAP_COMPLETE(ihs1, ihs_i2c_init, ihs_i2c_probe,
433 ihs_i2c_read, ihs_i2c_write,
434 ihs_i2c_set_bus_speed,
435 CONFIG_SYS_I2C_IHS_SPEED_1,
436 CONFIG_SYS_I2C_IHS_SLAVE_1, 1)
437 #ifdef CONFIG_SYS_I2C_IHS_DUAL
438 U_BOOT_I2C_ADAP_COMPLETE(ihs1_1, ihs_i2c_init, ihs_i2c_probe,
439 ihs_i2c_read, ihs_i2c_write,
440 ihs_i2c_set_bus_speed,
441 CONFIG_SYS_I2C_IHS_SPEED_1_1,
442 CONFIG_SYS_I2C_IHS_SLAVE_1_1, 17)
445 #ifdef CONFIG_SYS_I2C_IHS_CH2
446 U_BOOT_I2C_ADAP_COMPLETE(ihs2, ihs_i2c_init, ihs_i2c_probe,
447 ihs_i2c_read, ihs_i2c_write,
448 ihs_i2c_set_bus_speed,
449 CONFIG_SYS_I2C_IHS_SPEED_2,
450 CONFIG_SYS_I2C_IHS_SLAVE_2, 2)
451 #ifdef CONFIG_SYS_I2C_IHS_DUAL
452 U_BOOT_I2C_ADAP_COMPLETE(ihs2_1, ihs_i2c_init, ihs_i2c_probe,
453 ihs_i2c_read, ihs_i2c_write,
454 ihs_i2c_set_bus_speed,
455 CONFIG_SYS_I2C_IHS_SPEED_2_1,
456 CONFIG_SYS_I2C_IHS_SLAVE_2_1, 18)
459 #ifdef CONFIG_SYS_I2C_IHS_CH3
460 U_BOOT_I2C_ADAP_COMPLETE(ihs3, ihs_i2c_init, ihs_i2c_probe,
461 ihs_i2c_read, ihs_i2c_write,
462 ihs_i2c_set_bus_speed,
463 CONFIG_SYS_I2C_IHS_SPEED_3,
464 CONFIG_SYS_I2C_IHS_SLAVE_3, 3)
465 #ifdef CONFIG_SYS_I2C_IHS_DUAL
466 U_BOOT_I2C_ADAP_COMPLETE(ihs3_1, ihs_i2c_init, ihs_i2c_probe,
467 ihs_i2c_read, ihs_i2c_write,
468 ihs_i2c_set_bus_speed,
469 CONFIG_SYS_I2C_IHS_SPEED_3_1,
470 CONFIG_SYS_I2C_IHS_SLAVE_3_1, 19)
473 #endif /* CONFIG_DM_I2C */