blob: 18d756e9cd5d80d98e7f7c5b49f7e61a01b92b7b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass5a822e12015-03-05 12:25:29 -07002/*
3 * PCI emulation device which swaps the case of text
4 *
5 * Copyright (c) 2014 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
Simon Glass5a822e12015-03-05 12:25:29 -07007 */
8
9#include <common.h>
10#include <dm.h>
Simon Glass07a3b232015-05-04 11:31:08 -060011#include <errno.h>
Simon Glass5a822e12015-03-05 12:25:29 -070012#include <pci.h>
13#include <asm/test.h>
14#include <linux/ctype.h>
15
16/**
17 * struct swap_case_platdata - platform data for this device
18 *
19 * @command: Current PCI command value
20 * @bar: Current base address values
21 */
22struct swap_case_platdata {
23 u16 command;
Simon Glassff7dd582018-06-12 00:05:02 -060024 u32 bar[6];
Simon Glass5a822e12015-03-05 12:25:29 -070025};
26
27#define offset_to_barnum(offset) \
28 (((offset) - PCI_BASE_ADDRESS_0) / sizeof(u32))
29
30enum {
31 MEM_TEXT_SIZE = 0x100,
32};
33
34enum swap_case_op {
35 OP_TO_LOWER,
36 OP_TO_UPPER,
37 OP_SWAP,
38};
39
40static struct pci_bar {
41 int type;
42 u32 size;
43} barinfo[] = {
44 { PCI_BASE_ADDRESS_SPACE_IO, 1 },
45 { PCI_BASE_ADDRESS_MEM_TYPE_32, MEM_TEXT_SIZE },
46 { 0, 0 },
47 { 0, 0 },
48 { 0, 0 },
49 { 0, 0 },
50};
51
52struct swap_case_priv {
53 enum swap_case_op op;
54 char mem_text[MEM_TEXT_SIZE];
55};
56
Alex Margineanf1274432019-06-07 11:24:24 +030057static int sandbox_swap_case_use_ea(struct udevice *dev)
58{
59 return !!ofnode_get_property(dev->node, "use-ea", NULL);
60}
61
62/* Please keep these macros in sync with ea_regs below */
63#define PCI_CAP_ID_EA_SIZE (sizeof(ea_regs) + 4)
64#define PCI_CAP_ID_EA_ENTRY_CNT 4
65/* Hardcoded EA structure, excluding 1st DW. */
66static const u32 ea_regs[] = {
67 /* BEI=0, ES=2, BAR0 32b Base + 32b MaxOffset, I/O space */
68 (2 << 8) | 2,
69 PCI_CAP_EA_BASE_LO0,
70 0,
71 /* BEI=1, ES=2, BAR1 32b Base + 32b MaxOffset */
72 (1 << 4) | 2,
73 PCI_CAP_EA_BASE_LO1,
74 MEM_TEXT_SIZE - 1,
75 /* BEI=2, ES=3, BAR2 64b Base + 32b MaxOffset */
76 (2 << 4) | 3,
77 PCI_CAP_EA_BASE_LO2 | PCI_EA_IS_64,
78 PCI_CAP_EA_SIZE_LO,
79 PCI_CAP_EA_BASE_HI2,
80 /* BEI=4, ES=4, BAR4 64b Base + 64b MaxOffset */
81 (4 << 4) | 4,
82 PCI_CAP_EA_BASE_LO4 | PCI_EA_IS_64,
83 PCI_CAP_EA_SIZE_LO | PCI_EA_IS_64,
84 PCI_CAP_EA_BASE_HI4,
85 PCI_CAP_EA_SIZE_HI,
86};
87
88static int sandbox_swap_case_read_ea(struct udevice *emul, uint offset,
89 ulong *valuep, enum pci_size_t size)
90{
91 u32 reg;
92
93 offset = offset - PCI_CAP_ID_EA_OFFSET - 4;
94 reg = ea_regs[offset >> 2];
95 reg >>= (offset % 4) * 8;
96
97 *valuep = reg;
98 return 0;
99}
100
Simon Glass5a822e12015-03-05 12:25:29 -0700101static int sandbox_swap_case_read_config(struct udevice *emul, uint offset,
102 ulong *valuep, enum pci_size_t size)
103{
104 struct swap_case_platdata *plat = dev_get_platdata(emul);
105
Alex Margineanf1274432019-06-07 11:24:24 +0300106 /*
107 * The content of the EA capability structure is handled elsewhere to
108 * keep the switch/case below sane
109 */
110 if (offset > PCI_CAP_ID_EA_OFFSET + PCI_CAP_LIST_NEXT &&
111 offset < PCI_CAP_ID_EA_OFFSET + PCI_CAP_ID_EA_SIZE)
112 return sandbox_swap_case_read_ea(emul, offset, valuep, size);
113
Simon Glass5a822e12015-03-05 12:25:29 -0700114 switch (offset) {
115 case PCI_COMMAND:
116 *valuep = plat->command;
117 break;
118 case PCI_HEADER_TYPE:
119 *valuep = 0;
120 break;
121 case PCI_VENDOR_ID:
122 *valuep = SANDBOX_PCI_VENDOR_ID;
123 break;
124 case PCI_DEVICE_ID:
Simon Glass21c8f1a2019-09-25 08:56:01 -0600125 *valuep = SANDBOX_PCI_SWAP_CASE_EMUL_ID;
Simon Glass5a822e12015-03-05 12:25:29 -0700126 break;
127 case PCI_CLASS_DEVICE:
128 if (size == PCI_SIZE_8) {
129 *valuep = SANDBOX_PCI_CLASS_SUB_CODE;
130 } else {
131 *valuep = (SANDBOX_PCI_CLASS_CODE << 8) |
132 SANDBOX_PCI_CLASS_SUB_CODE;
133 }
134 break;
135 case PCI_CLASS_CODE:
136 *valuep = SANDBOX_PCI_CLASS_CODE;
137 break;
138 case PCI_BASE_ADDRESS_0:
139 case PCI_BASE_ADDRESS_1:
140 case PCI_BASE_ADDRESS_2:
141 case PCI_BASE_ADDRESS_3:
142 case PCI_BASE_ADDRESS_4:
143 case PCI_BASE_ADDRESS_5: {
144 int barnum;
145 u32 *bar, result;
146
147 barnum = offset_to_barnum(offset);
148 bar = &plat->bar[barnum];
149
150 result = *bar;
151 if (*bar == 0xffffffff) {
152 if (barinfo[barnum].type) {
153 result = (~(barinfo[barnum].size - 1) &
154 PCI_BASE_ADDRESS_IO_MASK) |
155 PCI_BASE_ADDRESS_SPACE_IO;
156 } else {
157 result = (~(barinfo[barnum].size - 1) &
158 PCI_BASE_ADDRESS_MEM_MASK) |
159 PCI_BASE_ADDRESS_MEM_TYPE_32;
160 }
161 }
162 debug("r bar %d=%x\n", barnum, result);
163 *valuep = result;
164 break;
165 }
Bin Mengd74d3122018-08-03 01:14:53 -0700166 case PCI_CAPABILITY_LIST:
167 *valuep = PCI_CAP_ID_PM_OFFSET;
168 break;
169 case PCI_CAP_ID_PM_OFFSET:
170 *valuep = (PCI_CAP_ID_EXP_OFFSET << 8) | PCI_CAP_ID_PM;
171 break;
Bin Mengb59b3692018-10-15 02:21:22 -0700172 case PCI_CAP_ID_PM_OFFSET + PCI_CAP_LIST_NEXT:
173 *valuep = PCI_CAP_ID_EXP_OFFSET;
174 break;
Bin Mengd74d3122018-08-03 01:14:53 -0700175 case PCI_CAP_ID_EXP_OFFSET:
176 *valuep = (PCI_CAP_ID_MSIX_OFFSET << 8) | PCI_CAP_ID_EXP;
177 break;
Bin Mengb59b3692018-10-15 02:21:22 -0700178 case PCI_CAP_ID_EXP_OFFSET + PCI_CAP_LIST_NEXT:
179 *valuep = PCI_CAP_ID_MSIX_OFFSET;
180 break;
Bin Mengd74d3122018-08-03 01:14:53 -0700181 case PCI_CAP_ID_MSIX_OFFSET:
Alex Margineanf1274432019-06-07 11:24:24 +0300182 if (sandbox_swap_case_use_ea(emul))
183 *valuep = (PCI_CAP_ID_EA_OFFSET << 8) | PCI_CAP_ID_MSIX;
184 else
185 *valuep = PCI_CAP_ID_MSIX;
Bin Mengd74d3122018-08-03 01:14:53 -0700186 break;
Bin Mengb59b3692018-10-15 02:21:22 -0700187 case PCI_CAP_ID_MSIX_OFFSET + PCI_CAP_LIST_NEXT:
Alex Margineanf1274432019-06-07 11:24:24 +0300188 if (sandbox_swap_case_use_ea(emul))
189 *valuep = PCI_CAP_ID_EA_OFFSET;
190 else
191 *valuep = 0;
192 break;
193 case PCI_CAP_ID_EA_OFFSET:
194 *valuep = (PCI_CAP_ID_EA_ENTRY_CNT << 16) | PCI_CAP_ID_EA;
195 break;
196 case PCI_CAP_ID_EA_OFFSET + PCI_CAP_LIST_NEXT:
Bin Mengb59b3692018-10-15 02:21:22 -0700197 *valuep = 0;
198 break;
Bin Mengd74d3122018-08-03 01:14:53 -0700199 case PCI_EXT_CAP_ID_ERR_OFFSET:
200 *valuep = (PCI_EXT_CAP_ID_VC_OFFSET << 20) | PCI_EXT_CAP_ID_ERR;
201 break;
202 case PCI_EXT_CAP_ID_VC_OFFSET:
203 *valuep = (PCI_EXT_CAP_ID_DSN_OFFSET << 20) | PCI_EXT_CAP_ID_VC;
204 break;
205 case PCI_EXT_CAP_ID_DSN_OFFSET:
206 *valuep = PCI_EXT_CAP_ID_DSN;
207 break;
Simon Glass5a822e12015-03-05 12:25:29 -0700208 }
209
210 return 0;
211}
212
213static int sandbox_swap_case_write_config(struct udevice *emul, uint offset,
214 ulong value, enum pci_size_t size)
215{
216 struct swap_case_platdata *plat = dev_get_platdata(emul);
217
218 switch (offset) {
219 case PCI_COMMAND:
220 plat->command = value;
221 break;
222 case PCI_BASE_ADDRESS_0:
223 case PCI_BASE_ADDRESS_1: {
224 int barnum;
225 u32 *bar;
226
227 barnum = offset_to_barnum(offset);
228 bar = &plat->bar[barnum];
229
230 debug("w bar %d=%lx\n", barnum, value);
231 *bar = value;
Bin Meng5b87baf2018-08-03 01:14:40 -0700232 /* space indicator (bit#0) is read-only */
233 *bar |= barinfo[barnum].type;
Simon Glass5a822e12015-03-05 12:25:29 -0700234 break;
235 }
236 }
237
238 return 0;
239}
240
241static int sandbox_swap_case_find_bar(struct udevice *emul, unsigned int addr,
242 int *barnump, unsigned int *offsetp)
243{
244 struct swap_case_platdata *plat = dev_get_platdata(emul);
245 int barnum;
246
247 for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
248 unsigned int size = barinfo[barnum].size;
Bin Meng5b87baf2018-08-03 01:14:40 -0700249 u32 base = plat->bar[barnum] & ~PCI_BASE_ADDRESS_SPACE;
Simon Glass5a822e12015-03-05 12:25:29 -0700250
Bin Meng5b87baf2018-08-03 01:14:40 -0700251 if (addr >= base && addr < base + size) {
Simon Glass5a822e12015-03-05 12:25:29 -0700252 *barnump = barnum;
Bin Meng5b87baf2018-08-03 01:14:40 -0700253 *offsetp = addr - base;
Simon Glass5a822e12015-03-05 12:25:29 -0700254 return 0;
255 }
256 }
257 *barnump = -1;
258
259 return -ENOENT;
260}
261
262static void sandbox_swap_case_do_op(enum swap_case_op op, char *str, int len)
263{
264 for (; len > 0; len--, str++) {
265 switch (op) {
266 case OP_TO_UPPER:
267 *str = toupper(*str);
268 break;
269 case OP_TO_LOWER:
270 *str = tolower(*str);
271 break;
272 case OP_SWAP:
273 if (isupper(*str))
274 *str = tolower(*str);
275 else
276 *str = toupper(*str);
277 break;
278 }
279 }
280}
281
Simon Glass81ae7782019-09-25 08:56:03 -0600282static int sandbox_swap_case_read_io(struct udevice *dev, unsigned int addr,
283 ulong *valuep, enum pci_size_t size)
Simon Glass5a822e12015-03-05 12:25:29 -0700284{
285 struct swap_case_priv *priv = dev_get_priv(dev);
286 unsigned int offset;
287 int barnum;
288 int ret;
289
290 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
291 if (ret)
292 return ret;
293
294 if (barnum == 0 && offset == 0)
295 *valuep = (*valuep & ~0xff) | priv->op;
296
297 return 0;
298}
299
Simon Glass81ae7782019-09-25 08:56:03 -0600300static int sandbox_swap_case_write_io(struct udevice *dev, unsigned int addr,
301 ulong value, enum pci_size_t size)
Simon Glass5a822e12015-03-05 12:25:29 -0700302{
303 struct swap_case_priv *priv = dev_get_priv(dev);
304 unsigned int offset;
305 int barnum;
306 int ret;
307
308 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
309 if (ret)
310 return ret;
311 if (barnum == 0 && offset == 0)
312 priv->op = value;
313
314 return 0;
315}
316
Alex Margineanf1274432019-06-07 11:24:24 +0300317static int pci_ea_bar2_magic = PCI_EA_BAR2_MAGIC;
318static int pci_ea_bar4_magic = PCI_EA_BAR4_MAGIC;
319
Simon Glass5a822e12015-03-05 12:25:29 -0700320static int sandbox_swap_case_map_physmem(struct udevice *dev,
321 phys_addr_t addr, unsigned long *lenp, void **ptrp)
322{
323 struct swap_case_priv *priv = dev_get_priv(dev);
324 unsigned int offset, avail;
325 int barnum;
326 int ret;
327
Alex Margineanf1274432019-06-07 11:24:24 +0300328 if (sandbox_swap_case_use_ea(dev)) {
329 /*
330 * only support mapping base address in EA test for now, we
331 * don't handle mapping an offset inside a BAR. Seems good
332 * enough for the current test.
333 */
334 switch (addr) {
335 case (phys_addr_t)PCI_CAP_EA_BASE_LO0:
336 *ptrp = &priv->op;
337 *lenp = 4;
338 break;
339 case (phys_addr_t)PCI_CAP_EA_BASE_LO1:
340 *ptrp = priv->mem_text;
341 *lenp = barinfo[1].size - 1;
342 break;
343 case (phys_addr_t)((PCI_CAP_EA_BASE_HI2 << 32) |
344 PCI_CAP_EA_BASE_LO2):
345 *ptrp = &pci_ea_bar2_magic;
346 *lenp = PCI_CAP_EA_SIZE_LO;
347 break;
348 case (phys_addr_t)((PCI_CAP_EA_BASE_HI4 << 32) |
349 PCI_CAP_EA_BASE_LO4):
350 *ptrp = &pci_ea_bar4_magic;
351 *lenp = (PCI_CAP_EA_SIZE_HI << 32) |
352 PCI_CAP_EA_SIZE_LO;
353 break;
354 default:
355 return -ENOENT;
356 }
357 return 0;
358 }
359
Simon Glass5a822e12015-03-05 12:25:29 -0700360 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
361 if (ret)
362 return ret;
Alex Margineanf1274432019-06-07 11:24:24 +0300363
Simon Glass5a822e12015-03-05 12:25:29 -0700364 if (barnum == 1) {
365 *ptrp = priv->mem_text + offset;
366 avail = barinfo[1].size - offset;
367 if (avail > barinfo[1].size)
368 *lenp = 0;
369 else
370 *lenp = min(*lenp, (ulong)avail);
371
372 return 0;
373 }
374
375 return -ENOENT;
376}
377
378static int sandbox_swap_case_unmap_physmem(struct udevice *dev,
379 const void *vaddr, unsigned long len)
380{
381 struct swap_case_priv *priv = dev_get_priv(dev);
382
383 sandbox_swap_case_do_op(priv->op, (void *)vaddr, len);
384
385 return 0;
386}
387
Simon Glass81ae7782019-09-25 08:56:03 -0600388static struct dm_pci_emul_ops sandbox_swap_case_emul_ops = {
Simon Glass5a822e12015-03-05 12:25:29 -0700389 .read_config = sandbox_swap_case_read_config,
390 .write_config = sandbox_swap_case_write_config,
391 .read_io = sandbox_swap_case_read_io,
392 .write_io = sandbox_swap_case_write_io,
393 .map_physmem = sandbox_swap_case_map_physmem,
394 .unmap_physmem = sandbox_swap_case_unmap_physmem,
395};
396
397static const struct udevice_id sandbox_swap_case_ids[] = {
398 { .compatible = "sandbox,swap-case" },
399 { }
400};
401
402U_BOOT_DRIVER(sandbox_swap_case_emul) = {
403 .name = "sandbox_swap_case_emul",
404 .id = UCLASS_PCI_EMUL,
405 .of_match = sandbox_swap_case_ids,
406 .ops = &sandbox_swap_case_emul_ops,
407 .priv_auto_alloc_size = sizeof(struct swap_case_priv),
408 .platdata_auto_alloc_size = sizeof(struct swap_case_platdata),
409};
Bin Mengc69ae412018-08-03 01:14:46 -0700410
411static struct pci_device_id sandbox_swap_case_supported[] = {
Simon Glass21c8f1a2019-09-25 08:56:01 -0600412 { PCI_VDEVICE(SANDBOX, SANDBOX_PCI_SWAP_CASE_EMUL_ID),
413 SWAP_CASE_DRV_DATA },
Bin Mengc69ae412018-08-03 01:14:46 -0700414 {},
415};
416
417U_BOOT_PCI_DEVICE(sandbox_swap_case_emul, sandbox_swap_case_supported);