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