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