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 | #ifdef CONFIG_DM_I2C |
| 10 | #include <dm.h> |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 11 | #include <regmap.h> |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 12 | #else |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 13 | #include <gdsys_fpga.h> |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 14 | #endif |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 16 | #include <asm/unaligned.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 18 | #include <linux/delay.h> |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 19 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 20 | #ifdef CONFIG_DM_I2C |
| 21 | struct ihs_i2c_priv { |
| 22 | uint speed; |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 23 | struct regmap *map; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 24 | }; |
| 25 | |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 26 | struct ihs_i2c_regs { |
| 27 | u16 interrupt_status; |
| 28 | u16 interrupt_enable_control; |
| 29 | u16 write_mailbox_ext; |
| 30 | u16 write_mailbox; |
| 31 | u16 read_mailbox_ext; |
| 32 | u16 read_mailbox; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 33 | }; |
| 34 | |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 35 | #define ihs_i2c_set(map, member, val) \ |
| 36 | regmap_set(map, struct ihs_i2c_regs, member, val) |
| 37 | |
| 38 | #define ihs_i2c_get(map, member, valp) \ |
| 39 | regmap_get(map, struct ihs_i2c_regs, member, valp) |
| 40 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 41 | #else /* !CONFIG_DM_I2C */ |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 42 | DECLARE_GLOBAL_DATA_PTR; |
| 43 | |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 44 | #ifdef CONFIG_SYS_I2C_IHS_DUAL |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 45 | |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 46 | #define I2C_SET_REG(fld, val) \ |
Dirk Eibach | 3af0cdb | 2015-10-28 11:46:23 +0100 | [diff] [blame] | 47 | do { \ |
| 48 | if (I2C_ADAP_HWNR & 0x10) \ |
| 49 | FPGA_SET_REG(I2C_ADAP_HWNR & 0xf, i2c1.fld, val); \ |
| 50 | else \ |
| 51 | FPGA_SET_REG(I2C_ADAP_HWNR, i2c0.fld, val); \ |
| 52 | } while (0) |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 53 | #else |
| 54 | #define I2C_SET_REG(fld, val) \ |
Dirk Eibach | 3af0cdb | 2015-10-28 11:46:23 +0100 | [diff] [blame] | 55 | FPGA_SET_REG(I2C_ADAP_HWNR, i2c0.fld, val) |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 56 | #endif |
| 57 | |
| 58 | #ifdef CONFIG_SYS_I2C_IHS_DUAL |
| 59 | #define I2C_GET_REG(fld, val) \ |
Dirk Eibach | 3af0cdb | 2015-10-28 11:46:23 +0100 | [diff] [blame] | 60 | do { \ |
| 61 | if (I2C_ADAP_HWNR & 0x10) \ |
| 62 | FPGA_GET_REG(I2C_ADAP_HWNR & 0xf, i2c1.fld, val); \ |
| 63 | else \ |
| 64 | FPGA_GET_REG(I2C_ADAP_HWNR, i2c0.fld, val); \ |
| 65 | } while (0) |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 66 | #else |
| 67 | #define I2C_GET_REG(fld, val) \ |
Dirk Eibach | 3af0cdb | 2015-10-28 11:46:23 +0100 | [diff] [blame] | 68 | FPGA_GET_REG(I2C_ADAP_HWNR, i2c0.fld, val) |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 69 | #endif |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 70 | #endif /* CONFIG_DM_I2C */ |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 71 | |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 72 | enum { |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 73 | I2CINT_ERROR_EV = BIT(13), |
| 74 | I2CINT_TRANSMIT_EV = BIT(14), |
| 75 | I2CINT_RECEIVE_EV = BIT(15), |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | enum { |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 79 | I2CMB_READ = 0 << 10, |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 80 | I2CMB_WRITE = 1 << 10, |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 81 | I2CMB_1BYTE = 0 << 11, |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 82 | I2CMB_2BYTE = 1 << 11, |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 83 | I2CMB_DONT_HOLD_BUS = 0 << 13, |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 84 | I2CMB_HOLD_BUS = 1 << 13, |
| 85 | I2CMB_NATIVE = 2 << 14, |
| 86 | }; |
| 87 | |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 88 | enum { |
| 89 | I2COP_WRITE = 0, |
| 90 | I2COP_READ = 1, |
| 91 | }; |
| 92 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 93 | #ifdef CONFIG_DM_I2C |
| 94 | static int wait_for_int(struct udevice *dev, int read) |
| 95 | #else |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 96 | static int wait_for_int(bool read) |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 97 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 98 | { |
| 99 | u16 val; |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 100 | uint ctr = 0; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 101 | #ifdef CONFIG_DM_I2C |
| 102 | struct ihs_i2c_priv *priv = dev_get_priv(dev); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 103 | #endif |
| 104 | |
| 105 | #ifdef CONFIG_DM_I2C |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 106 | ihs_i2c_get(priv->map, interrupt_status, &val); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 107 | #else |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 108 | I2C_GET_REG(interrupt_status, &val); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 109 | #endif |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 110 | /* Wait until error or receive/transmit interrupt was raised */ |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 111 | while (!(val & (I2CINT_ERROR_EV |
| 112 | | (read ? I2CINT_RECEIVE_EV : I2CINT_TRANSMIT_EV)))) { |
| 113 | udelay(10); |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 114 | if (ctr++ > 5000) { |
| 115 | debug("%s: timed out\n", __func__); |
| 116 | return -ETIMEDOUT; |
| 117 | } |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 118 | #ifdef CONFIG_DM_I2C |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 119 | ihs_i2c_get(priv->map, interrupt_status, &val); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 120 | #else |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 121 | I2C_GET_REG(interrupt_status, &val); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 122 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 125 | return (val & I2CINT_ERROR_EV) ? -EIO : 0; |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 128 | #ifdef CONFIG_DM_I2C |
| 129 | static int ihs_i2c_transfer(struct udevice *dev, uchar chip, |
| 130 | uchar *buffer, int len, int read, bool is_last) |
| 131 | #else |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 132 | static int ihs_i2c_transfer(uchar chip, uchar *buffer, int len, bool read, |
| 133 | bool is_last) |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 134 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 135 | { |
| 136 | u16 val; |
Mario Six | 2df71d6 | 2018-03-28 14:37:42 +0200 | [diff] [blame] | 137 | u16 data; |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 138 | int res; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 139 | #ifdef CONFIG_DM_I2C |
| 140 | struct ihs_i2c_priv *priv = dev_get_priv(dev); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 141 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 142 | |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 143 | /* Clear interrupt status */ |
Mario Six | 2df71d6 | 2018-03-28 14:37:42 +0200 | [diff] [blame] | 144 | data = I2CINT_ERROR_EV | I2CINT_RECEIVE_EV | I2CINT_TRANSMIT_EV; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 145 | #ifdef CONFIG_DM_I2C |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 146 | ihs_i2c_set(priv->map, interrupt_status, data); |
| 147 | ihs_i2c_get(priv->map, interrupt_status, &val); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 148 | #else |
Mario Six | 2df71d6 | 2018-03-28 14:37:42 +0200 | [diff] [blame] | 149 | I2C_SET_REG(interrupt_status, data); |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 150 | I2C_GET_REG(interrupt_status, &val); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 151 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 152 | |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 153 | /* 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] | 154 | if (!read && len) { |
| 155 | val = buffer[0]; |
| 156 | |
| 157 | if (len > 1) |
| 158 | val |= buffer[1] << 8; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 159 | #ifdef CONFIG_DM_I2C |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 160 | ihs_i2c_set(priv->map, write_mailbox_ext, val); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 161 | #else |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 162 | I2C_SET_REG(write_mailbox_ext, val); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 163 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Mario Six | 2df71d6 | 2018-03-28 14:37:42 +0200 | [diff] [blame] | 166 | data = I2CMB_NATIVE |
| 167 | | (read ? 0 : I2CMB_WRITE) |
| 168 | | (chip << 1) |
| 169 | | ((len > 1) ? I2CMB_2BYTE : 0) |
| 170 | | (is_last ? 0 : I2CMB_HOLD_BUS); |
| 171 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 172 | #ifdef CONFIG_DM_I2C |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 173 | ihs_i2c_set(priv->map, write_mailbox, data); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 174 | #else |
Mario Six | 2df71d6 | 2018-03-28 14:37:42 +0200 | [diff] [blame] | 175 | I2C_SET_REG(write_mailbox, data); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 176 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 177 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 178 | #ifdef CONFIG_DM_I2C |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 179 | res = wait_for_int(dev, read); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 180 | #else |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 181 | res = wait_for_int(read); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 182 | #endif |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 183 | if (res) { |
| 184 | if (res == -ETIMEDOUT) |
| 185 | debug("%s: time out while waiting for event\n", __func__); |
| 186 | |
| 187 | return res; |
| 188 | } |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 189 | |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 190 | /* If we want to read, get the bytes from the mailbox */ |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 191 | if (read) { |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 192 | #ifdef CONFIG_DM_I2C |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 193 | ihs_i2c_get(priv->map, read_mailbox_ext, &val); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 194 | #else |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 195 | I2C_GET_REG(read_mailbox_ext, &val); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 196 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 197 | buffer[0] = val & 0xff; |
| 198 | if (len > 1) |
| 199 | buffer[1] = val >> 8; |
| 200 | } |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 205 | #ifdef CONFIG_DM_I2C |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 206 | static int ihs_i2c_send_buffer(struct udevice *dev, uchar chip, u8 *data, int len, bool hold_bus, int read) |
| 207 | #else |
| 208 | static int ihs_i2c_send_buffer(uchar chip, u8 *data, int len, bool hold_bus, |
| 209 | int read) |
| 210 | #endif |
| 211 | { |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 212 | int res; |
| 213 | |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 214 | while (len) { |
| 215 | int transfer = min(len, 2); |
| 216 | bool is_last = len <= transfer; |
| 217 | |
| 218 | #ifdef CONFIG_DM_I2C |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 219 | res = ihs_i2c_transfer(dev, chip, data, transfer, read, |
| 220 | hold_bus ? false : is_last); |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 221 | #else |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 222 | res = ihs_i2c_transfer(chip, data, transfer, read, |
| 223 | hold_bus ? false : is_last); |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 224 | #endif |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 225 | if (res) |
| 226 | return res; |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 227 | |
| 228 | data += transfer; |
| 229 | len -= transfer; |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | #ifdef CONFIG_DM_I2C |
| 236 | static int ihs_i2c_address(struct udevice *dev, uchar chip, u8 *addr, int alen, |
| 237 | bool hold_bus) |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 238 | #else |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 239 | static int ihs_i2c_address(uchar chip, u8 *addr, int alen, bool hold_bus) |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 240 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 241 | { |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 242 | #ifdef CONFIG_DM_I2C |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 243 | return ihs_i2c_send_buffer(dev, chip, addr, alen, hold_bus, I2COP_WRITE); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 244 | #else |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 245 | return ihs_i2c_send_buffer(chip, addr, alen, hold_bus, I2COP_WRITE); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 246 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 249 | #ifdef CONFIG_DM_I2C |
| 250 | static int ihs_i2c_access(struct udevice *dev, uchar chip, u8 *addr, |
| 251 | int alen, uchar *buffer, int len, int read) |
| 252 | #else |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 253 | static int ihs_i2c_access(struct i2c_adapter *adap, uchar chip, u8 *addr, |
| 254 | int alen, uchar *buffer, int len, int read) |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 255 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 256 | { |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 257 | int res; |
| 258 | |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 259 | /* 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] | 260 | if (len <= 0) |
| 261 | return -EINVAL; |
| 262 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 263 | #ifdef CONFIG_DM_I2C |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 264 | res = ihs_i2c_address(dev, chip, addr, alen, len); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 265 | #else |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 266 | res = ihs_i2c_address(chip, addr, alen, len); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 267 | #endif |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 268 | if (res) |
| 269 | return res; |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 270 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 271 | #ifdef CONFIG_DM_I2C |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 272 | return ihs_i2c_send_buffer(dev, chip, buffer, len, false, read); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 273 | #else |
Mario Six | 9cef983 | 2018-01-15 11:08:12 +0100 | [diff] [blame] | 274 | return ihs_i2c_send_buffer(chip, buffer, len, false, read); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 275 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 278 | #ifdef CONFIG_DM_I2C |
| 279 | |
| 280 | int ihs_i2c_probe(struct udevice *bus) |
| 281 | { |
| 282 | struct ihs_i2c_priv *priv = dev_get_priv(bus); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 283 | |
Mario Six | 98e4249 | 2019-01-28 09:45:57 +0100 | [diff] [blame] | 284 | regmap_init_mem(dev_ofnode(bus), &priv->map); |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static int ihs_i2c_set_bus_speed(struct udevice *bus, uint speed) |
| 290 | { |
| 291 | struct ihs_i2c_priv *priv = dev_get_priv(bus); |
| 292 | |
| 293 | if (speed != priv->speed && priv->speed != 0) |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 294 | return -EINVAL; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 295 | |
| 296 | priv->speed = speed; |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int ihs_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) |
| 302 | { |
| 303 | struct i2c_msg *dmsg, *omsg, dummy; |
| 304 | |
| 305 | memset(&dummy, 0, sizeof(struct i2c_msg)); |
| 306 | |
| 307 | /* We expect either two messages (one with an offset and one with the |
| 308 | * actucal data) or one message (just data) |
| 309 | */ |
| 310 | if (nmsgs > 2 || nmsgs == 0) { |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 311 | debug("%s: Only one or two messages are supported\n", __func__); |
| 312 | return -ENOTSUPP; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | omsg = nmsgs == 1 ? &dummy : msg; |
| 316 | dmsg = nmsgs == 1 ? msg : msg + 1; |
| 317 | |
| 318 | if (dmsg->flags & I2C_M_RD) |
| 319 | return ihs_i2c_access(bus, dmsg->addr, omsg->buf, |
| 320 | omsg->len, dmsg->buf, dmsg->len, |
| 321 | I2COP_READ); |
| 322 | else |
| 323 | return ihs_i2c_access(bus, dmsg->addr, omsg->buf, |
| 324 | omsg->len, dmsg->buf, dmsg->len, |
| 325 | I2COP_WRITE); |
| 326 | } |
| 327 | |
| 328 | static int ihs_i2c_probe_chip(struct udevice *bus, u32 chip_addr, |
| 329 | u32 chip_flags) |
| 330 | { |
| 331 | uchar buffer[2]; |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 332 | int res; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 333 | |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 334 | res = ihs_i2c_transfer(bus, chip_addr, buffer, 0, I2COP_READ, true); |
| 335 | if (res) |
| 336 | return res; |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 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, |
| 345 | }; |
| 346 | |
| 347 | static const struct udevice_id ihs_i2c_ids[] = { |
| 348 | { .compatible = "gdsys,ihs_i2cmaster", }, |
| 349 | { /* sentinel */ } |
| 350 | }; |
| 351 | |
| 352 | U_BOOT_DRIVER(i2c_ihs) = { |
| 353 | .name = "i2c_ihs", |
| 354 | .id = UCLASS_I2C, |
| 355 | .of_match = ihs_i2c_ids, |
| 356 | .probe = ihs_i2c_probe, |
| 357 | .priv_auto_alloc_size = sizeof(struct ihs_i2c_priv), |
| 358 | .ops = &ihs_i2c_ops, |
| 359 | }; |
| 360 | |
| 361 | #else /* CONFIG_DM_I2C */ |
| 362 | |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 363 | static void ihs_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr) |
| 364 | { |
| 365 | #ifdef CONFIG_SYS_I2C_INIT_BOARD |
| 366 | /* |
| 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. |
| 370 | */ |
| 371 | i2c_init_board(); |
| 372 | #endif |
| 373 | } |
| 374 | |
| 375 | static int ihs_i2c_probe(struct i2c_adapter *adap, uchar chip) |
| 376 | { |
| 377 | uchar buffer[2]; |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 378 | int res; |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 379 | |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 380 | res = ihs_i2c_transfer(chip, buffer, 0, I2COP_READ, true); |
| 381 | if (res) |
| 382 | return res; |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int ihs_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr, |
| 388 | int alen, uchar *buffer, int len) |
| 389 | { |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 390 | u8 addr_bytes[4]; |
| 391 | |
| 392 | put_unaligned_le32(addr, addr_bytes); |
| 393 | |
| 394 | return ihs_i2c_access(adap, chip, addr_bytes, alen, buffer, len, |
| 395 | I2COP_READ); |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static int ihs_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr, |
| 399 | int alen, uchar *buffer, int len) |
| 400 | { |
Mario Six | 64ef094 | 2018-01-15 11:08:10 +0100 | [diff] [blame] | 401 | u8 addr_bytes[4]; |
| 402 | |
| 403 | put_unaligned_le32(addr, addr_bytes); |
| 404 | |
| 405 | return ihs_i2c_access(adap, chip, addr_bytes, alen, buffer, len, |
| 406 | I2COP_WRITE); |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static unsigned int ihs_i2c_set_bus_speed(struct i2c_adapter *adap, |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 410 | unsigned int speed) |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 411 | { |
| 412 | if (speed != adap->speed) |
Mario Six | 482c76e | 2019-01-28 09:45:58 +0100 | [diff] [blame] | 413 | return -EINVAL; |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 414 | return speed; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Register IHS i2c adapters |
| 419 | */ |
| 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) |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 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) |
| 432 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 433 | #endif |
| 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) |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 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) |
| 446 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 447 | #endif |
| 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) |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 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) |
| 460 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 461 | #endif |
| 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) |
Dirk Eibach | 071be89 | 2015-10-28 11:46:22 +0100 | [diff] [blame] | 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) |
| 474 | #endif |
Dirk Eibach | b46226b | 2014-07-03 09:28:18 +0200 | [diff] [blame] | 475 | #endif |
Mario Six | 9216421 | 2018-01-15 11:08:11 +0100 | [diff] [blame] | 476 | #endif /* CONFIG_DM_I2C */ |