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