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