Mario Six | 9fc8706 | 2018-08-09 14:51:17 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2016 |
| 4 | * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc |
| 5 | * |
| 6 | * (C) Copyright 2017, 2018 |
| 7 | * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <axi.h> |
| 12 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 13 | #include <log.h> |
Mario Six | 9fc8706 | 2018-08-09 14:51:17 +0200 | [diff] [blame] | 14 | #include <regmap.h> |
| 15 | |
| 16 | /** |
| 17 | * struct ihs_axi_regs - Structure for the register map of a IHS AXI device |
| 18 | * @interrupt_status: Status register to indicate certain events (e.g. |
| 19 | * error during transfer, transfer complete, etc.) |
| 20 | * @interrupt_enable_control: Register to both control which statuses will be |
| 21 | * indicated in the interrupt_status register, and |
| 22 | * to change bus settings |
| 23 | * @address_lsb: Least significant 16-bit word of the address of a |
| 24 | * device to transfer data from/to |
| 25 | * @address_msb: Most significant 16-bit word of the address of a |
| 26 | * device to transfer data from/to |
| 27 | * @write_data_lsb: Least significant 16-bit word of the data to be |
| 28 | * written to a device |
| 29 | * @write_data_msb: Most significant 16-bit word of the data to be |
| 30 | * written to a device |
| 31 | * @read_data_lsb: Least significant 16-bit word of the data read |
| 32 | * from a device |
| 33 | * @read_data_msb: Most significant 16-bit word of the data read |
| 34 | * from a device |
| 35 | */ |
| 36 | struct ihs_axi_regs { |
| 37 | u16 interrupt_status; |
| 38 | u16 interrupt_enable_control; |
| 39 | u16 address_lsb; |
| 40 | u16 address_msb; |
| 41 | u16 write_data_lsb; |
| 42 | u16 write_data_msb; |
| 43 | u16 read_data_lsb; |
| 44 | u16 read_data_msb; |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * ihs_axi_set() - Convenience macro to set values in register map |
| 49 | * @map: The register map to write to |
| 50 | * @member: The member of the ihs_axi_regs structure to write |
| 51 | * @val: The value to write to the register map |
| 52 | */ |
| 53 | #define ihs_axi_set(map, member, val) \ |
| 54 | regmap_set(map, struct ihs_axi_regs, member, val) |
| 55 | |
| 56 | /** |
| 57 | * ihs_axi_get() - Convenience macro to read values from register map |
| 58 | * @map: The register map to read from |
| 59 | * @member: The member of the ihs_axi_regs structure to read |
| 60 | * @valp: Pointer to a buffer to receive the value read |
| 61 | */ |
| 62 | #define ihs_axi_get(map, member, valp) \ |
| 63 | regmap_get(map, struct ihs_axi_regs, member, valp) |
| 64 | |
| 65 | /** |
| 66 | * struct ihs_axi_priv - Private data structure of IHS AXI devices |
| 67 | * @map: Register map for the IHS AXI device |
| 68 | */ |
| 69 | struct ihs_axi_priv { |
| 70 | struct regmap *map; |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * enum status_reg - Description of bits in the interrupt_status register |
| 75 | * @STATUS_READ_COMPLETE_EVENT: A read transfer was completed |
| 76 | * @STATUS_WRITE_COMPLETE_EVENT: A write transfer was completed |
| 77 | * @STATUS_TIMEOUT_EVENT: A timeout has occurred during the transfer |
| 78 | * @STATUS_ERROR_EVENT: A error has occurred during the transfer |
| 79 | * @STATUS_AXI_INT: A AXI interrupt has occurred |
| 80 | * @STATUS_READ_DATA_AVAILABLE: Data is available to be read |
| 81 | * @STATUS_BUSY: The bus is busy |
| 82 | * @STATUS_INIT_DONE: The bus has finished initializing |
| 83 | */ |
| 84 | enum status_reg { |
| 85 | STATUS_READ_COMPLETE_EVENT = BIT(15), |
| 86 | STATUS_WRITE_COMPLETE_EVENT = BIT(14), |
| 87 | STATUS_TIMEOUT_EVENT = BIT(13), |
| 88 | STATUS_ERROR_EVENT = BIT(12), |
| 89 | STATUS_AXI_INT = BIT(11), |
| 90 | STATUS_READ_DATA_AVAILABLE = BIT(7), |
| 91 | STATUS_BUSY = BIT(6), |
| 92 | STATUS_INIT_DONE = BIT(5), |
| 93 | }; |
| 94 | |
| 95 | /** |
| 96 | * enum control_reg - Description of bit fields in the interrupt_enable_control |
| 97 | * register |
| 98 | * @CONTROL_READ_COMPLETE_EVENT_ENABLE: STATUS_READ_COMPLETE_EVENT will be |
| 99 | * raised in the interrupt_status register |
| 100 | * @CONTROL_WRITE_COMPLETE_EVENT_ENABLE: STATUS_WRITE_COMPLETE_EVENT will be |
| 101 | * raised in the interrupt_status register |
| 102 | * @CONTROL_TIMEOUT_EVENT_ENABLE: STATUS_TIMEOUT_EVENT will be raised in |
| 103 | * the interrupt_status register |
| 104 | * @CONTROL_ERROR_EVENT_ENABLE: STATUS_ERROR_EVENT will be raised in |
| 105 | * the interrupt_status register |
| 106 | * @CONTROL_AXI_INT_ENABLE: STATUS_AXI_INT will be raised in the |
| 107 | * interrupt_status register |
| 108 | * @CONTROL_CMD_NOP: Configure bus to send a NOP command |
| 109 | * for the next transfer |
| 110 | * @CONTROL_CMD_WRITE: Configure bus to do a write transfer |
| 111 | * @CONTROL_CMD_WRITE_POST_INC: Auto-increment address after write |
| 112 | * transfer |
| 113 | * @CONTROL_CMD_READ: Configure bus to do a read transfer |
| 114 | * @CONTROL_CMD_READ_POST_INC: Auto-increment address after read |
| 115 | * transfer |
| 116 | */ |
| 117 | enum control_reg { |
| 118 | CONTROL_READ_COMPLETE_EVENT_ENABLE = BIT(15), |
| 119 | CONTROL_WRITE_COMPLETE_EVENT_ENABLE = BIT(14), |
| 120 | CONTROL_TIMEOUT_EVENT_ENABLE = BIT(13), |
| 121 | CONTROL_ERROR_EVENT_ENABLE = BIT(12), |
| 122 | CONTROL_AXI_INT_ENABLE = BIT(11), |
| 123 | |
| 124 | CONTROL_CMD_NOP = 0x0, |
| 125 | CONTROL_CMD_WRITE = 0x8, |
| 126 | CONTROL_CMD_WRITE_POST_INC = 0x9, |
| 127 | CONTROL_CMD_READ = 0xa, |
| 128 | CONTROL_CMD_READ_POST_INC = 0xb, |
| 129 | }; |
| 130 | |
| 131 | /** |
| 132 | * enum axi_cmd - Determine if transfer is read or write transfer |
| 133 | * @AXI_CMD_READ: The transfer should be a read transfer |
| 134 | * @AXI_CMD_WRITE: The transfer should be a write transfer |
| 135 | */ |
| 136 | enum axi_cmd { |
| 137 | AXI_CMD_READ, |
| 138 | AXI_CMD_WRITE, |
| 139 | }; |
| 140 | |
| 141 | /** |
| 142 | * ihs_axi_transfer() - Run transfer on the AXI bus |
| 143 | * @bus: The AXI bus device on which to run the transfer on |
| 144 | * @address: The address to use in the transfer (i.e. which address to |
| 145 | * read/write from/to) |
| 146 | * @cmd: Should the transfer be a read or write transfer? |
| 147 | * |
| 148 | * Return: 0 if OK, -ve on error |
| 149 | */ |
| 150 | static int ihs_axi_transfer(struct udevice *bus, ulong address, |
| 151 | enum axi_cmd cmd) |
| 152 | { |
| 153 | struct ihs_axi_priv *priv = dev_get_priv(bus); |
| 154 | /* Try waiting for events up to 10 times */ |
| 155 | const uint WAIT_TRIES = 10; |
| 156 | u16 wait_mask = STATUS_TIMEOUT_EVENT | |
| 157 | STATUS_ERROR_EVENT; |
| 158 | u16 complete_flag; |
| 159 | u16 status; |
| 160 | uint k; |
| 161 | |
| 162 | if (cmd == AXI_CMD_READ) { |
| 163 | complete_flag = STATUS_READ_COMPLETE_EVENT; |
| 164 | cmd = CONTROL_CMD_READ; |
| 165 | } else { |
| 166 | complete_flag = STATUS_WRITE_COMPLETE_EVENT; |
| 167 | cmd = CONTROL_CMD_WRITE; |
| 168 | } |
| 169 | |
| 170 | wait_mask |= complete_flag; |
| 171 | |
| 172 | /* Lower 16 bit */ |
| 173 | ihs_axi_set(priv->map, address_lsb, address & 0xffff); |
| 174 | /* Upper 16 bit */ |
| 175 | ihs_axi_set(priv->map, address_msb, (address >> 16) & 0xffff); |
| 176 | |
| 177 | ihs_axi_set(priv->map, interrupt_status, wait_mask); |
| 178 | ihs_axi_set(priv->map, interrupt_enable_control, cmd); |
| 179 | |
| 180 | for (k = WAIT_TRIES; k > 0; --k) { |
| 181 | ihs_axi_get(priv->map, interrupt_status, &status); |
| 182 | if (status & wait_mask) |
| 183 | break; |
| 184 | udelay(1); |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * k == 0 -> Tries ran out with no event we were waiting for actually |
| 189 | * occurring. |
| 190 | */ |
| 191 | if (!k) |
| 192 | ihs_axi_get(priv->map, interrupt_status, &status); |
| 193 | |
| 194 | if (status & complete_flag) |
| 195 | return 0; |
| 196 | |
| 197 | if (status & STATUS_ERROR_EVENT) { |
| 198 | debug("%s: Error occurred during transfer\n", bus->name); |
| 199 | return -EIO; |
| 200 | } |
| 201 | |
| 202 | debug("%s: Transfer timed out\n", bus->name); |
| 203 | return -ETIMEDOUT; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * API |
| 208 | */ |
| 209 | |
| 210 | static int ihs_axi_read(struct udevice *dev, ulong address, void *data, |
| 211 | enum axi_size_t size) |
| 212 | { |
| 213 | struct ihs_axi_priv *priv = dev_get_priv(dev); |
| 214 | int ret; |
| 215 | u16 data_lsb, data_msb; |
| 216 | u32 *p = data; |
| 217 | |
| 218 | if (size != AXI_SIZE_32) { |
| 219 | debug("%s: transfer size '%d' not supported\n", |
| 220 | dev->name, size); |
| 221 | return -ENOSYS; |
| 222 | } |
| 223 | |
| 224 | ret = ihs_axi_transfer(dev, address, AXI_CMD_READ); |
| 225 | if (ret < 0) { |
| 226 | debug("%s: Error during AXI transfer (err = %d)\n", |
| 227 | dev->name, ret); |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | ihs_axi_get(priv->map, read_data_lsb, &data_lsb); |
| 232 | ihs_axi_get(priv->map, read_data_msb, &data_msb); |
| 233 | |
| 234 | /* Assemble data from two 16-bit words */ |
| 235 | *p = (data_msb << 16) | data_lsb; |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static int ihs_axi_write(struct udevice *dev, ulong address, void *data, |
| 241 | enum axi_size_t size) |
| 242 | { |
| 243 | struct ihs_axi_priv *priv = dev_get_priv(dev); |
| 244 | int ret; |
| 245 | u32 *p = data; |
| 246 | |
| 247 | if (size != AXI_SIZE_32) { |
| 248 | debug("%s: transfer size '%d' not supported\n", |
| 249 | dev->name, size); |
| 250 | return -ENOSYS; |
| 251 | } |
| 252 | |
| 253 | /* Lower 16 bit */ |
| 254 | ihs_axi_set(priv->map, write_data_lsb, *p & 0xffff); |
| 255 | /* Upper 16 bit */ |
| 256 | ihs_axi_set(priv->map, write_data_msb, (*p >> 16) & 0xffff); |
| 257 | |
| 258 | ret = ihs_axi_transfer(dev, address, AXI_CMD_WRITE); |
| 259 | if (ret < 0) { |
| 260 | debug("%s: Error during AXI transfer (err = %d)\n", |
| 261 | dev->name, ret); |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static const struct udevice_id ihs_axi_ids[] = { |
| 269 | { .compatible = "gdsys,ihs_axi" }, |
| 270 | { /* sentinel */ } |
| 271 | }; |
| 272 | |
| 273 | static const struct axi_ops ihs_axi_ops = { |
| 274 | .read = ihs_axi_read, |
| 275 | .write = ihs_axi_write, |
| 276 | }; |
| 277 | |
| 278 | static int ihs_axi_probe(struct udevice *dev) |
| 279 | { |
| 280 | struct ihs_axi_priv *priv = dev_get_priv(dev); |
| 281 | |
| 282 | regmap_init_mem(dev_ofnode(dev), &priv->map); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | U_BOOT_DRIVER(ihs_axi_bus) = { |
| 288 | .name = "ihs_axi_bus", |
| 289 | .id = UCLASS_AXI, |
| 290 | .of_match = ihs_axi_ids, |
| 291 | .ops = &ihs_axi_ops, |
| 292 | .priv_auto_alloc_size = sizeof(struct ihs_axi_priv), |
| 293 | .probe = ihs_axi_probe, |
| 294 | }; |