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