Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 |
Mario Six | d38826a | 2018-03-06 08:04:58 +0100 | [diff] [blame] | 4 | * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <i2c.h> |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 9 | #include <dm.h> |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 10 | #include <regmap.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 13 | #include <asm/unaligned.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 15 | #include <linux/delay.h> |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 16 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 17 | struct ihs_i2c_priv { |
| 18 | uint speed; |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 19 | struct regmap *map; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 20 | }; |
| 21 | |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 22 | struct ihs_i2c_regs { |
| 23 | u16 interrupt_status; |
| 24 | u16 interrupt_enable_control; |
| 25 | u16 write_mailbox_ext; |
| 26 | u16 write_mailbox; |
| 27 | u16 read_mailbox_ext; |
| 28 | u16 read_mailbox; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 29 | }; |
| 30 | |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 31 | #define ihs_i2c_set(map, member, val) \ |
| 32 | regmap_set(map, struct ihs_i2c_regs, member, val) |
| 33 | |
| 34 | #define ihs_i2c_get(map, member, valp) \ |
| 35 | regmap_get(map, struct ihs_i2c_regs, member, valp) |
| 36 | |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 37 | enum { |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 38 | I2CINT_ERROR_EV = BIT(13), |
| 39 | I2CINT_TRANSMIT_EV = BIT(14), |
| 40 | I2CINT_RECEIVE_EV = BIT(15), |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | enum { |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 44 | I2CMB_READ = 0 << 10, |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 45 | I2CMB_WRITE = 1 << 10, |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 46 | I2CMB_1BYTE = 0 << 11, |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 47 | I2CMB_2BYTE = 1 << 11, |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 48 | I2CMB_DONT_HOLD_BUS = 0 << 13, |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 49 | I2CMB_HOLD_BUS = 1 << 13, |
| 50 | I2CMB_NATIVE = 2 << 14, |
| 51 | }; |
| 52 | |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 53 | enum { |
| 54 | I2COP_WRITE = 0, |
| 55 | I2COP_READ = 1, |
| 56 | }; |
| 57 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 58 | static int wait_for_int(struct udevice *dev, int read) |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 59 | { |
| 60 | u16 val; |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 61 | uint ctr = 0; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 62 | struct ihs_i2c_priv *priv = dev_get_priv(dev); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 63 | |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 64 | ihs_i2c_get(priv->map, interrupt_status, &val); |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 65 | /* Wait until error or receive/transmit interrupt was raised */ |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 66 | while (!(val & (I2CINT_ERROR_EV |
| 67 | | (read ? I2CINT_RECEIVE_EV : I2CINT_TRANSMIT_EV)))) { |
| 68 | udelay(10); |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 69 | if (ctr++ > 5000) { |
| 70 | debug("%s: timed out\n", __func__); |
| 71 | return -ETIMEDOUT; |
| 72 | } |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 73 | ihs_i2c_get(priv->map, interrupt_status, &val); |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 76 | return (val & I2CINT_ERROR_EV) ? -EIO : 0; |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 79 | static int ihs_i2c_transfer(struct udevice *dev, uchar chip, |
| 80 | uchar *buffer, int len, int read, bool is_last) |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 81 | { |
| 82 | u16 val; |
Mario Six | 2df71d6 | 2018-03-28 14:37:42 +0200 | [diff] [blame] | 83 | u16 data; |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 84 | int res; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 85 | struct ihs_i2c_priv *priv = dev_get_priv(dev); |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 86 | |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 87 | /* Clear interrupt status */ |
Mario Six | 2df71d6 | 2018-03-28 14:37:42 +0200 | [diff] [blame] | 88 | data = I2CINT_ERROR_EV | I2CINT_RECEIVE_EV | I2CINT_TRANSMIT_EV; |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 89 | ihs_i2c_set(priv->map, interrupt_status, data); |
| 90 | ihs_i2c_get(priv->map, interrupt_status, &val); |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 91 | |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 92 | /* If we want to write and have data, write the bytes to the mailbox */ |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 93 | if (!read && len) { |
| 94 | val = buffer[0]; |
| 95 | |
| 96 | if (len > 1) |
| 97 | val |= buffer[1] << 8; |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 98 | ihs_i2c_set(priv->map, write_mailbox_ext, val); |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Mario Six | 2df71d6 | 2018-03-28 14:37:42 +0200 | [diff] [blame] | 101 | data = I2CMB_NATIVE |
| 102 | | (read ? 0 : I2CMB_WRITE) |
| 103 | | (chip << 1) |
| 104 | | ((len > 1) ? I2CMB_2BYTE : 0) |
| 105 | | (is_last ? 0 : I2CMB_HOLD_BUS); |
| 106 | |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 107 | ihs_i2c_set(priv->map, write_mailbox, data); |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 108 | |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 109 | res = wait_for_int(dev, read); |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 110 | if (res) { |
| 111 | if (res == -ETIMEDOUT) |
| 112 | debug("%s: time out while waiting for event\n", __func__); |
| 113 | |
| 114 | return res; |
| 115 | } |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 116 | |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 117 | /* If we want to read, get the bytes from the mailbox */ |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 118 | if (read) { |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 119 | ihs_i2c_get(priv->map, read_mailbox_ext, &val); |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 120 | buffer[0] = val & 0xff; |
| 121 | if (len > 1) |
| 122 | buffer[1] = val >> 8; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 128 | static int ihs_i2c_send_buffer(struct udevice *dev, uchar chip, u8 *data, int len, bool hold_bus, int read) |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 129 | { |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 130 | int res; |
| 131 | |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 132 | while (len) { |
| 133 | int transfer = min(len, 2); |
| 134 | bool is_last = len <= transfer; |
| 135 | |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 136 | res = ihs_i2c_transfer(dev, chip, data, transfer, read, |
| 137 | hold_bus ? false : is_last); |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 138 | if (res) |
| 139 | return res; |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 140 | |
| 141 | data += transfer; |
| 142 | len -= transfer; |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 148 | static int ihs_i2c_address(struct udevice *dev, uchar chip, u8 *addr, int alen, |
| 149 | bool hold_bus) |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 150 | { |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 151 | return ihs_i2c_send_buffer(dev, chip, addr, alen, hold_bus, I2COP_WRITE); |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 154 | static int ihs_i2c_access(struct udevice *dev, uchar chip, u8 *addr, |
| 155 | int alen, uchar *buffer, int len, int read) |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 156 | { |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 157 | int res; |
| 158 | |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 159 | /* Don't hold the bus if length of data to send/receive is zero */ |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 160 | if (len <= 0) |
| 161 | return -EINVAL; |
| 162 | |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 163 | res = ihs_i2c_address(dev, chip, addr, alen, len); |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 164 | if (res) |
| 165 | return res; |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 166 | |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 167 | return ihs_i2c_send_buffer(dev, chip, buffer, len, false, read); |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 170 | int ihs_i2c_probe(struct udevice *bus) |
| 171 | { |
| 172 | struct ihs_i2c_priv *priv = dev_get_priv(bus); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 173 | |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 174 | regmap_init_mem(dev_ofnode(bus), &priv->map); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int ihs_i2c_set_bus_speed(struct udevice *bus, uint speed) |
| 180 | { |
| 181 | struct ihs_i2c_priv *priv = dev_get_priv(bus); |
| 182 | |
| 183 | if (speed != priv->speed && priv->speed != 0) |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 184 | return -EINVAL; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 185 | |
| 186 | priv->speed = speed; |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static int ihs_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) |
| 192 | { |
| 193 | struct i2c_msg *dmsg, *omsg, dummy; |
| 194 | |
| 195 | memset(&dummy, 0, sizeof(struct i2c_msg)); |
| 196 | |
| 197 | /* We expect either two messages (one with an offset and one with the |
Michal Simek | a6cc0b0 | 2022-04-19 15:01:31 +0200 | [diff] [blame] | 198 | * actual data) or one message (just data) |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 199 | */ |
| 200 | if (nmsgs > 2 || nmsgs == 0) { |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 201 | debug("%s: Only one or two messages are supported\n", __func__); |
| 202 | return -ENOTSUPP; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | omsg = nmsgs == 1 ? &dummy : msg; |
| 206 | dmsg = nmsgs == 1 ? msg : msg + 1; |
| 207 | |
| 208 | if (dmsg->flags & I2C_M_RD) |
| 209 | return ihs_i2c_access(bus, dmsg->addr, omsg->buf, |
| 210 | omsg->len, dmsg->buf, dmsg->len, |
| 211 | I2COP_READ); |
| 212 | else |
| 213 | return ihs_i2c_access(bus, dmsg->addr, omsg->buf, |
| 214 | omsg->len, dmsg->buf, dmsg->len, |
| 215 | I2COP_WRITE); |
| 216 | } |
| 217 | |
| 218 | static int ihs_i2c_probe_chip(struct udevice *bus, u32 chip_addr, |
| 219 | u32 chip_flags) |
| 220 | { |
| 221 | uchar buffer[2]; |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 222 | int res; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 223 | |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 224 | res = ihs_i2c_transfer(bus, chip_addr, buffer, 0, I2COP_READ, true); |
| 225 | if (res) |
| 226 | return res; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static const struct dm_i2c_ops ihs_i2c_ops = { |
| 232 | .xfer = ihs_i2c_xfer, |
| 233 | .probe_chip = ihs_i2c_probe_chip, |
| 234 | .set_bus_speed = ihs_i2c_set_bus_speed, |
| 235 | }; |
| 236 | |
| 237 | static const struct udevice_id ihs_i2c_ids[] = { |
| 238 | { .compatible = "gdsys,ihs_i2cmaster", }, |
| 239 | { /* sentinel */ } |
| 240 | }; |
| 241 | |
| 242 | U_BOOT_DRIVER(i2c_ihs) = { |
| 243 | .name = "i2c_ihs", |
| 244 | .id = UCLASS_I2C, |
| 245 | .of_match = ihs_i2c_ids, |
| 246 | .probe = ihs_i2c_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 247 | .priv_auto = sizeof(struct ihs_i2c_priv), |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 248 | .ops = &ihs_i2c_ops, |
| 249 | }; |