1 // SPDX-License-Identifier: GPL-2.0+
4 * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
6 * (C) Copyright 2017, 2018
7 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
15 #include <linux/delay.h>
18 * struct ihs_axi_regs - Structure for the register map of a IHS AXI device
19 * @interrupt_status: Status register to indicate certain events (e.g.
20 * error during transfer, transfer complete, etc.)
21 * @interrupt_enable_control: Register to both control which statuses will be
22 * indicated in the interrupt_status register, and
23 * to change bus settings
24 * @address_lsb: Least significant 16-bit word of the address of a
25 * device to transfer data from/to
26 * @address_msb: Most significant 16-bit word of the address of a
27 * device to transfer data from/to
28 * @write_data_lsb: Least significant 16-bit word of the data to be
30 * @write_data_msb: Most significant 16-bit word of the data to be
32 * @read_data_lsb: Least significant 16-bit word of the data read
34 * @read_data_msb: Most significant 16-bit word of the data read
39 u16 interrupt_enable_control;
49 * ihs_axi_set() - Convenience macro to set values in register map
50 * @map: The register map to write to
51 * @member: The member of the ihs_axi_regs structure to write
52 * @val: The value to write to the register map
54 #define ihs_axi_set(map, member, val) \
55 regmap_set(map, struct ihs_axi_regs, member, val)
58 * ihs_axi_get() - Convenience macro to read values from register map
59 * @map: The register map to read from
60 * @member: The member of the ihs_axi_regs structure to read
61 * @valp: Pointer to a buffer to receive the value read
63 #define ihs_axi_get(map, member, valp) \
64 regmap_get(map, struct ihs_axi_regs, member, valp)
67 * struct ihs_axi_priv - Private data structure of IHS AXI devices
68 * @map: Register map for the IHS AXI device
75 * enum status_reg - Description of bits in the interrupt_status register
76 * @STATUS_READ_COMPLETE_EVENT: A read transfer was completed
77 * @STATUS_WRITE_COMPLETE_EVENT: A write transfer was completed
78 * @STATUS_TIMEOUT_EVENT: A timeout has occurred during the transfer
79 * @STATUS_ERROR_EVENT: A error has occurred during the transfer
80 * @STATUS_AXI_INT: A AXI interrupt has occurred
81 * @STATUS_READ_DATA_AVAILABLE: Data is available to be read
82 * @STATUS_BUSY: The bus is busy
83 * @STATUS_INIT_DONE: The bus has finished initializing
86 STATUS_READ_COMPLETE_EVENT = BIT(15),
87 STATUS_WRITE_COMPLETE_EVENT = BIT(14),
88 STATUS_TIMEOUT_EVENT = BIT(13),
89 STATUS_ERROR_EVENT = BIT(12),
90 STATUS_AXI_INT = BIT(11),
91 STATUS_READ_DATA_AVAILABLE = BIT(7),
93 STATUS_INIT_DONE = BIT(5),
97 * enum control_reg - Description of bit fields in the interrupt_enable_control
99 * @CONTROL_READ_COMPLETE_EVENT_ENABLE: STATUS_READ_COMPLETE_EVENT will be
100 * raised in the interrupt_status register
101 * @CONTROL_WRITE_COMPLETE_EVENT_ENABLE: STATUS_WRITE_COMPLETE_EVENT will be
102 * raised in the interrupt_status register
103 * @CONTROL_TIMEOUT_EVENT_ENABLE: STATUS_TIMEOUT_EVENT will be raised in
104 * the interrupt_status register
105 * @CONTROL_ERROR_EVENT_ENABLE: STATUS_ERROR_EVENT will be raised in
106 * the interrupt_status register
107 * @CONTROL_AXI_INT_ENABLE: STATUS_AXI_INT will be raised in the
108 * interrupt_status register
109 * @CONTROL_CMD_NOP: Configure bus to send a NOP command
110 * for the next transfer
111 * @CONTROL_CMD_WRITE: Configure bus to do a write transfer
112 * @CONTROL_CMD_WRITE_POST_INC: Auto-increment address after write
114 * @CONTROL_CMD_READ: Configure bus to do a read transfer
115 * @CONTROL_CMD_READ_POST_INC: Auto-increment address after read
119 CONTROL_READ_COMPLETE_EVENT_ENABLE = BIT(15),
120 CONTROL_WRITE_COMPLETE_EVENT_ENABLE = BIT(14),
121 CONTROL_TIMEOUT_EVENT_ENABLE = BIT(13),
122 CONTROL_ERROR_EVENT_ENABLE = BIT(12),
123 CONTROL_AXI_INT_ENABLE = BIT(11),
125 CONTROL_CMD_NOP = 0x0,
126 CONTROL_CMD_WRITE = 0x8,
127 CONTROL_CMD_WRITE_POST_INC = 0x9,
128 CONTROL_CMD_READ = 0xa,
129 CONTROL_CMD_READ_POST_INC = 0xb,
133 * enum axi_cmd - Determine if transfer is read or write transfer
134 * @AXI_CMD_READ: The transfer should be a read transfer
135 * @AXI_CMD_WRITE: The transfer should be a write transfer
143 * ihs_axi_transfer() - Run transfer on the AXI bus
144 * @bus: The AXI bus device on which to run the transfer on
145 * @address: The address to use in the transfer (i.e. which address to
146 * read/write from/to)
147 * @cmd: Should the transfer be a read or write transfer?
149 * Return: 0 if OK, -ve on error
151 static int ihs_axi_transfer(struct udevice *bus, ulong address,
154 struct ihs_axi_priv *priv = dev_get_priv(bus);
155 /* Try waiting for events up to 10 times */
156 const uint WAIT_TRIES = 10;
157 u16 wait_mask = STATUS_TIMEOUT_EVENT |
163 if (cmd == AXI_CMD_READ) {
164 complete_flag = STATUS_READ_COMPLETE_EVENT;
165 cmd = CONTROL_CMD_READ;
167 complete_flag = STATUS_WRITE_COMPLETE_EVENT;
168 cmd = CONTROL_CMD_WRITE;
171 wait_mask |= complete_flag;
174 ihs_axi_set(priv->map, address_lsb, address & 0xffff);
176 ihs_axi_set(priv->map, address_msb, (address >> 16) & 0xffff);
178 ihs_axi_set(priv->map, interrupt_status, wait_mask);
179 ihs_axi_set(priv->map, interrupt_enable_control, cmd);
181 for (k = WAIT_TRIES; k > 0; --k) {
182 ihs_axi_get(priv->map, interrupt_status, &status);
183 if (status & wait_mask)
189 * k == 0 -> Tries ran out with no event we were waiting for actually
193 ihs_axi_get(priv->map, interrupt_status, &status);
195 if (status & complete_flag)
198 if (status & STATUS_ERROR_EVENT) {
199 debug("%s: Error occurred during transfer\n", bus->name);
203 debug("%s: Transfer timed out\n", bus->name);
211 static int ihs_axi_read(struct udevice *dev, ulong address, void *data,
212 enum axi_size_t size)
214 struct ihs_axi_priv *priv = dev_get_priv(dev);
216 u16 data_lsb, data_msb;
219 if (size != AXI_SIZE_32) {
220 debug("%s: transfer size '%d' not supported\n",
225 ret = ihs_axi_transfer(dev, address, AXI_CMD_READ);
227 debug("%s: Error during AXI transfer (err = %d)\n",
232 ihs_axi_get(priv->map, read_data_lsb, &data_lsb);
233 ihs_axi_get(priv->map, read_data_msb, &data_msb);
235 /* Assemble data from two 16-bit words */
236 *p = (data_msb << 16) | data_lsb;
241 static int ihs_axi_write(struct udevice *dev, ulong address, void *data,
242 enum axi_size_t size)
244 struct ihs_axi_priv *priv = dev_get_priv(dev);
248 if (size != AXI_SIZE_32) {
249 debug("%s: transfer size '%d' not supported\n",
255 ihs_axi_set(priv->map, write_data_lsb, *p & 0xffff);
257 ihs_axi_set(priv->map, write_data_msb, (*p >> 16) & 0xffff);
259 ret = ihs_axi_transfer(dev, address, AXI_CMD_WRITE);
261 debug("%s: Error during AXI transfer (err = %d)\n",
269 static const struct udevice_id ihs_axi_ids[] = {
270 { .compatible = "gdsys,ihs_axi" },
274 static const struct axi_ops ihs_axi_ops = {
275 .read = ihs_axi_read,
276 .write = ihs_axi_write,
279 static int ihs_axi_probe(struct udevice *dev)
281 struct ihs_axi_priv *priv = dev_get_priv(dev);
283 regmap_init_mem(dev_ofnode(dev), &priv->map);
288 U_BOOT_DRIVER(ihs_axi_bus) = {
289 .name = "ihs_axi_bus",
291 .of_match = ihs_axi_ids,
293 .priv_auto_alloc_size = sizeof(struct ihs_axi_priv),
294 .probe = ihs_axi_probe,