1 // SPDX-License-Identifier: GPL-2.0+
6 * (C) Copyright 2017, 2018
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
29 * @write_data_msb: Most significant 16-bit word of the data to be
31 * @read_data_lsb: Least significant 16-bit word of the data read
33 * @read_data_msb: Most significant 16-bit word of the data read
38 u16 interrupt_enable_control;
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
53 #define ihs_axi_set(map, member, val) \
54 regmap_set(map, struct ihs_axi_regs, member, val)
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
62 #define ihs_axi_get(map, member, valp) \
63 regmap_get(map, struct ihs_axi_regs, member, valp)
66 * struct ihs_axi_priv - Private data structure of IHS AXI devices
67 * @map: Register map for the IHS AXI device
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
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),
92 STATUS_INIT_DONE = BIT(5),
96 * enum control_reg - Description of bit fields in the interrupt_enable_control
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
113 * @CONTROL_CMD_READ: Configure bus to do a read transfer
114 * @CONTROL_CMD_READ_POST_INC: Auto-increment address after read
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),
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,
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
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?
148 * Return: 0 if OK, -ve on error
150 static int ihs_axi_transfer(struct udevice *bus, ulong address,
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 |
162 if (cmd == AXI_CMD_READ) {
163 complete_flag = STATUS_READ_COMPLETE_EVENT;
164 cmd = CONTROL_CMD_READ;
166 complete_flag = STATUS_WRITE_COMPLETE_EVENT;
167 cmd = CONTROL_CMD_WRITE;
170 wait_mask |= complete_flag;
173 ihs_axi_set(priv->map, address_lsb, address & 0xffff);
175 ihs_axi_set(priv->map, address_msb, (address >> 16) & 0xffff);
177 ihs_axi_set(priv->map, interrupt_status, wait_mask);
178 ihs_axi_set(priv->map, interrupt_enable_control, cmd);
180 for (k = WAIT_TRIES; k > 0; --k) {
181 ihs_axi_get(priv->map, interrupt_status, &status);
182 if (status & wait_mask)
188 * k == 0 -> Tries ran out with no event we were waiting for actually
192 ihs_axi_get(priv->map, interrupt_status, &status);
194 if (status & complete_flag)
197 if (status & STATUS_ERROR_EVENT) {
198 debug("%s: Error occurred during transfer\n", bus->name);
202 debug("%s: Transfer timed out\n", bus->name);
210 static int ihs_axi_read(struct udevice *dev, ulong address, void *data,
211 enum axi_size_t size)
213 struct ihs_axi_priv *priv = dev_get_priv(dev);
215 u16 data_lsb, data_msb;
218 if (size != AXI_SIZE_32) {
219 debug("%s: transfer size '%d' not supported\n",
224 ret = ihs_axi_transfer(dev, address, AXI_CMD_READ);
226 debug("%s: Error during AXI transfer (err = %d)\n",
231 ihs_axi_get(priv->map, read_data_lsb, &data_lsb);
232 ihs_axi_get(priv->map, read_data_msb, &data_msb);
234 /* Assemble data from two 16-bit words */
235 *p = (data_msb << 16) | data_lsb;
240 static int ihs_axi_write(struct udevice *dev, ulong address, void *data,
241 enum axi_size_t size)
243 struct ihs_axi_priv *priv = dev_get_priv(dev);
247 if (size != AXI_SIZE_32) {
248 debug("%s: transfer size '%d' not supported\n",
254 ihs_axi_set(priv->map, write_data_lsb, *p & 0xffff);
256 ihs_axi_set(priv->map, write_data_msb, (*p >> 16) & 0xffff);
258 ret = ihs_axi_transfer(dev, address, AXI_CMD_WRITE);
260 debug("%s: Error during AXI transfer (err = %d)\n",
268 static const struct udevice_id ihs_axi_ids[] = {
269 { .compatible = "gdsys,ihs_axi" },
273 static const struct axi_ops ihs_axi_ops = {
274 .read = ihs_axi_read,
275 .write = ihs_axi_write,
278 static int ihs_axi_probe(struct udevice *dev)
280 struct ihs_axi_priv *priv = dev_get_priv(dev);
282 regmap_init_mem(dev_ofnode(dev), &priv->map);
287 U_BOOT_DRIVER(ihs_axi_bus) = {
288 .name = "ihs_axi_bus",
290 .of_match = ihs_axi_ids,
292 .priv_auto_alloc_size = sizeof(struct ihs_axi_priv),
293 .probe = ihs_axi_probe,