1
2
3
4
5
6
7
8
9
10#include <common.h>
11#include <axi.h>
12#include <dm.h>
13#include <log.h>
14#include <regmap.h>
15#include <linux/bitops.h>
16#include <linux/delay.h>
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38struct ihs_axi_regs {
39 u16 interrupt_status;
40 u16 interrupt_enable_control;
41 u16 address_lsb;
42 u16 address_msb;
43 u16 write_data_lsb;
44 u16 write_data_msb;
45 u16 read_data_lsb;
46 u16 read_data_msb;
47};
48
49
50
51
52
53
54
55#define ihs_axi_set(map, member, val) \
56 regmap_set(map, struct ihs_axi_regs, member, val)
57
58
59
60
61
62
63
64#define ihs_axi_get(map, member, valp) \
65 regmap_get(map, struct ihs_axi_regs, member, valp)
66
67
68
69
70
71struct ihs_axi_priv {
72 struct regmap *map;
73};
74
75
76
77
78
79
80
81
82
83
84
85
86enum status_reg {
87 STATUS_READ_COMPLETE_EVENT = BIT(15),
88 STATUS_WRITE_COMPLETE_EVENT = BIT(14),
89 STATUS_TIMEOUT_EVENT = BIT(13),
90 STATUS_ERROR_EVENT = BIT(12),
91 STATUS_AXI_INT = BIT(11),
92 STATUS_READ_DATA_AVAILABLE = BIT(7),
93 STATUS_BUSY = BIT(6),
94 STATUS_INIT_DONE = BIT(5),
95};
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119enum control_reg {
120 CONTROL_READ_COMPLETE_EVENT_ENABLE = BIT(15),
121 CONTROL_WRITE_COMPLETE_EVENT_ENABLE = BIT(14),
122 CONTROL_TIMEOUT_EVENT_ENABLE = BIT(13),
123 CONTROL_ERROR_EVENT_ENABLE = BIT(12),
124 CONTROL_AXI_INT_ENABLE = BIT(11),
125
126 CONTROL_CMD_NOP = 0x0,
127 CONTROL_CMD_WRITE = 0x8,
128 CONTROL_CMD_WRITE_POST_INC = 0x9,
129 CONTROL_CMD_READ = 0xa,
130 CONTROL_CMD_READ_POST_INC = 0xb,
131};
132
133
134
135
136
137
138enum axi_cmd {
139 AXI_CMD_READ,
140 AXI_CMD_WRITE,
141};
142
143
144
145
146
147
148
149
150
151
152static int ihs_axi_transfer(struct udevice *bus, ulong address,
153 enum axi_cmd cmd)
154{
155 struct ihs_axi_priv *priv = dev_get_priv(bus);
156
157 const uint WAIT_TRIES = 10;
158 u16 wait_mask = STATUS_TIMEOUT_EVENT |
159 STATUS_ERROR_EVENT;
160 u16 complete_flag;
161 u16 status;
162 uint k;
163
164 if (cmd == AXI_CMD_READ) {
165 complete_flag = STATUS_READ_COMPLETE_EVENT;
166 cmd = CONTROL_CMD_READ;
167 } else {
168 complete_flag = STATUS_WRITE_COMPLETE_EVENT;
169 cmd = CONTROL_CMD_WRITE;
170 }
171
172 wait_mask |= complete_flag;
173
174
175 ihs_axi_set(priv->map, address_lsb, address & 0xffff);
176
177 ihs_axi_set(priv->map, address_msb, (address >> 16) & 0xffff);
178
179 ihs_axi_set(priv->map, interrupt_status, wait_mask);
180 ihs_axi_set(priv->map, interrupt_enable_control, cmd);
181
182 for (k = WAIT_TRIES; k > 0; --k) {
183 ihs_axi_get(priv->map, interrupt_status, &status);
184 if (status & wait_mask)
185 break;
186 udelay(1);
187 }
188
189
190
191
192
193 if (!k)
194 ihs_axi_get(priv->map, interrupt_status, &status);
195
196 if (status & complete_flag)
197 return 0;
198
199 if (status & STATUS_ERROR_EVENT) {
200 debug("%s: Error occurred during transfer\n", bus->name);
201 return -EIO;
202 }
203
204 debug("%s: Transfer timed out\n", bus->name);
205 return -ETIMEDOUT;
206}
207
208
209
210
211
212static int ihs_axi_read(struct udevice *dev, ulong address, void *data,
213 enum axi_size_t size)
214{
215 struct ihs_axi_priv *priv = dev_get_priv(dev);
216 int ret;
217 u16 data_lsb, data_msb;
218 u32 *p = data;
219
220 if (size != AXI_SIZE_32) {
221 debug("%s: transfer size '%d' not supported\n",
222 dev->name, size);
223 return -ENOSYS;
224 }
225
226 ret = ihs_axi_transfer(dev, address, AXI_CMD_READ);
227 if (ret < 0) {
228 debug("%s: Error during AXI transfer (err = %d)\n",
229 dev->name, ret);
230 return ret;
231 }
232
233 ihs_axi_get(priv->map, read_data_lsb, &data_lsb);
234 ihs_axi_get(priv->map, read_data_msb, &data_msb);
235
236
237 *p = (data_msb << 16) | data_lsb;
238
239 return 0;
240}
241
242static int ihs_axi_write(struct udevice *dev, ulong address, void *data,
243 enum axi_size_t size)
244{
245 struct ihs_axi_priv *priv = dev_get_priv(dev);
246 int ret;
247 u32 *p = data;
248
249 if (size != AXI_SIZE_32) {
250 debug("%s: transfer size '%d' not supported\n",
251 dev->name, size);
252 return -ENOSYS;
253 }
254
255
256 ihs_axi_set(priv->map, write_data_lsb, *p & 0xffff);
257
258 ihs_axi_set(priv->map, write_data_msb, (*p >> 16) & 0xffff);
259
260 ret = ihs_axi_transfer(dev, address, AXI_CMD_WRITE);
261 if (ret < 0) {
262 debug("%s: Error during AXI transfer (err = %d)\n",
263 dev->name, ret);
264 return ret;
265 }
266
267 return 0;
268}
269
270static const struct udevice_id ihs_axi_ids[] = {
271 { .compatible = "gdsys,ihs_axi" },
272 { }
273};
274
275static const struct axi_ops ihs_axi_ops = {
276 .read = ihs_axi_read,
277 .write = ihs_axi_write,
278};
279
280static int ihs_axi_probe(struct udevice *dev)
281{
282 struct ihs_axi_priv *priv = dev_get_priv(dev);
283
284 regmap_init_mem(dev_ofnode(dev), &priv->map);
285
286 return 0;
287}
288
289U_BOOT_DRIVER(ihs_axi_bus) = {
290 .name = "ihs_axi_bus",
291 .id = UCLASS_AXI,
292 .of_match = ihs_axi_ids,
293 .ops = &ihs_axi_ops,
294 .priv_auto = sizeof(struct ihs_axi_priv),
295 .probe = ihs_axi_probe,
296};
297