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