1 // SPDX-License-Identifier: GPL-2.0+
3 * PCI emulation device which swaps the case of text
5 * Copyright (c) 2014 Google, Inc
6 * Written by Simon Glass <sjg@chromium.org>
15 #include <linux/ctype.h>
18 * struct swap_case_plat - platform data for this device
20 * @command: Current PCI command value
21 * @bar: Current base address values
23 struct swap_case_plat {
29 MEM_TEXT_SIZE = 0x100,
38 static struct pci_bar {
42 { PCI_BASE_ADDRESS_SPACE_IO, 1 },
43 { PCI_BASE_ADDRESS_MEM_TYPE_32, MEM_TEXT_SIZE },
50 struct swap_case_priv {
52 char mem_text[MEM_TEXT_SIZE];
55 static int sandbox_swap_case_use_ea(const struct udevice *dev)
57 return !!ofnode_get_property(dev_ofnode(dev), "use-ea", NULL);
60 /* Please keep these macros in sync with ea_regs below */
61 #define PCI_CAP_ID_EA_SIZE (sizeof(ea_regs) + 4)
62 #define PCI_CAP_ID_EA_ENTRY_CNT 4
63 /* Hardcoded EA structure, excluding 1st DW. */
64 static const u32 ea_regs[] = {
65 /* BEI=0, ES=2, BAR0 32b Base + 32b MaxOffset, I/O space */
69 /* BEI=1, ES=2, BAR1 32b Base + 32b MaxOffset */
73 /* BEI=2, ES=3, BAR2 64b Base + 32b MaxOffset */
75 PCI_CAP_EA_BASE_LO2 | PCI_EA_IS_64,
78 /* BEI=4, ES=4, BAR4 64b Base + 64b MaxOffset */
80 PCI_CAP_EA_BASE_LO4 | PCI_EA_IS_64,
81 PCI_CAP_EA_SIZE_LO | PCI_EA_IS_64,
86 static int sandbox_swap_case_read_ea(const struct udevice *emul, uint offset,
87 ulong *valuep, enum pci_size_t size)
91 offset = offset - PCI_CAP_ID_EA_OFFSET - 4;
92 reg = ea_regs[offset >> 2];
93 reg >>= (offset % 4) * 8;
99 static int sandbox_swap_case_read_config(const struct udevice *emul,
100 uint offset, ulong *valuep,
101 enum pci_size_t size)
103 struct swap_case_plat *plat = dev_get_plat(emul);
106 * The content of the EA capability structure is handled elsewhere to
107 * keep the switch/case below sane
109 if (offset > PCI_CAP_ID_EA_OFFSET + PCI_CAP_LIST_NEXT &&
110 offset < PCI_CAP_ID_EA_OFFSET + PCI_CAP_ID_EA_SIZE)
111 return sandbox_swap_case_read_ea(emul, offset, valuep, size);
115 *valuep = plat->command;
117 case PCI_HEADER_TYPE:
121 *valuep = SANDBOX_PCI_VENDOR_ID;
124 *valuep = SANDBOX_PCI_SWAP_CASE_EMUL_ID;
126 case PCI_CLASS_DEVICE:
127 if (size == PCI_SIZE_8) {
128 *valuep = SANDBOX_PCI_CLASS_SUB_CODE;
130 *valuep = (SANDBOX_PCI_CLASS_CODE << 8) |
131 SANDBOX_PCI_CLASS_SUB_CODE;
135 *valuep = SANDBOX_PCI_CLASS_CODE;
137 case PCI_BASE_ADDRESS_0:
138 case PCI_BASE_ADDRESS_1:
139 case PCI_BASE_ADDRESS_2:
140 case PCI_BASE_ADDRESS_3:
141 case PCI_BASE_ADDRESS_4:
142 case PCI_BASE_ADDRESS_5: {
146 barnum = pci_offset_to_barnum(offset);
147 bar = &plat->bar[barnum];
149 *valuep = sandbox_pci_read_bar(*bar, barinfo[barnum].type,
150 barinfo[barnum].size);
153 case PCI_CAPABILITY_LIST:
154 *valuep = PCI_CAP_ID_PM_OFFSET;
156 case PCI_CAP_ID_PM_OFFSET:
157 *valuep = (PCI_CAP_ID_EXP_OFFSET << 8) | PCI_CAP_ID_PM;
159 case PCI_CAP_ID_PM_OFFSET + PCI_CAP_LIST_NEXT:
160 *valuep = PCI_CAP_ID_EXP_OFFSET;
162 case PCI_CAP_ID_EXP_OFFSET:
163 *valuep = (PCI_CAP_ID_MSIX_OFFSET << 8) | PCI_CAP_ID_EXP;
165 case PCI_CAP_ID_EXP_OFFSET + PCI_CAP_LIST_NEXT:
166 *valuep = PCI_CAP_ID_MSIX_OFFSET;
168 case PCI_CAP_ID_MSIX_OFFSET:
169 if (sandbox_swap_case_use_ea(emul))
170 *valuep = (PCI_CAP_ID_EA_OFFSET << 8) | PCI_CAP_ID_MSIX;
172 *valuep = PCI_CAP_ID_MSIX;
174 case PCI_CAP_ID_MSIX_OFFSET + PCI_CAP_LIST_NEXT:
175 if (sandbox_swap_case_use_ea(emul))
176 *valuep = PCI_CAP_ID_EA_OFFSET;
180 case PCI_CAP_ID_EA_OFFSET:
181 *valuep = (PCI_CAP_ID_EA_ENTRY_CNT << 16) | PCI_CAP_ID_EA;
183 case PCI_CAP_ID_EA_OFFSET + PCI_CAP_LIST_NEXT:
186 case PCI_EXT_CAP_ID_ERR_OFFSET:
187 *valuep = (PCI_EXT_CAP_ID_VC_OFFSET << 20) | PCI_EXT_CAP_ID_ERR;
189 case PCI_EXT_CAP_ID_VC_OFFSET:
190 *valuep = (PCI_EXT_CAP_ID_DSN_OFFSET << 20) | PCI_EXT_CAP_ID_VC;
192 case PCI_EXT_CAP_ID_DSN_OFFSET:
193 *valuep = PCI_EXT_CAP_ID_DSN;
200 static int sandbox_swap_case_write_config(struct udevice *emul, uint offset,
201 ulong value, enum pci_size_t size)
203 struct swap_case_plat *plat = dev_get_plat(emul);
207 plat->command = value;
209 case PCI_BASE_ADDRESS_0:
210 case PCI_BASE_ADDRESS_1: {
214 barnum = pci_offset_to_barnum(offset);
215 bar = &plat->bar[barnum];
217 debug("w bar %d=%lx\n", barnum, value);
219 /* space indicator (bit#0) is read-only */
220 *bar |= barinfo[barnum].type;
228 static int sandbox_swap_case_find_bar(struct udevice *emul, unsigned int addr,
229 int *barnump, unsigned int *offsetp)
231 struct swap_case_plat *plat = dev_get_plat(emul);
234 for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
235 unsigned int size = barinfo[barnum].size;
236 u32 base = plat->bar[barnum] & ~PCI_BASE_ADDRESS_SPACE;
238 if (addr >= base && addr < base + size) {
240 *offsetp = addr - base;
249 static void sandbox_swap_case_do_op(enum swap_case_op op, char *str, int len)
251 for (; len > 0; len--, str++) {
254 *str = toupper(*str);
257 *str = tolower(*str);
261 *str = tolower(*str);
263 *str = toupper(*str);
269 static int sandbox_swap_case_read_io(struct udevice *dev, unsigned int addr,
270 ulong *valuep, enum pci_size_t size)
272 struct swap_case_priv *priv = dev_get_priv(dev);
277 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
281 if (barnum == 0 && offset == 0)
282 *valuep = (*valuep & ~0xff) | priv->op;
287 static int sandbox_swap_case_write_io(struct udevice *dev, unsigned int addr,
288 ulong value, enum pci_size_t size)
290 struct swap_case_priv *priv = dev_get_priv(dev);
295 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
298 if (barnum == 0 && offset == 0)
304 static int pci_ea_bar2_magic = PCI_EA_BAR2_MAGIC;
305 static int pci_ea_bar4_magic = PCI_EA_BAR4_MAGIC;
307 static int sandbox_swap_case_map_physmem(struct udevice *dev,
308 phys_addr_t addr, unsigned long *lenp, void **ptrp)
310 struct swap_case_priv *priv = dev_get_priv(dev);
311 unsigned int offset, avail;
315 if (sandbox_swap_case_use_ea(dev)) {
317 * only support mapping base address in EA test for now, we
318 * don't handle mapping an offset inside a BAR. Seems good
319 * enough for the current test.
322 case (phys_addr_t)PCI_CAP_EA_BASE_LO0:
326 case (phys_addr_t)PCI_CAP_EA_BASE_LO1:
327 *ptrp = priv->mem_text;
328 *lenp = barinfo[1].size - 1;
330 case (phys_addr_t)((PCI_CAP_EA_BASE_HI2 << 32) |
331 PCI_CAP_EA_BASE_LO2):
332 *ptrp = &pci_ea_bar2_magic;
333 *lenp = PCI_CAP_EA_SIZE_LO;
335 case (phys_addr_t)((PCI_CAP_EA_BASE_HI4 << 32) |
336 PCI_CAP_EA_BASE_LO4):
337 *ptrp = &pci_ea_bar4_magic;
338 *lenp = (PCI_CAP_EA_SIZE_HI << 32) |
347 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
352 *ptrp = priv->mem_text + offset;
353 avail = barinfo[1].size - offset;
354 if (avail > barinfo[1].size)
357 *lenp = min(*lenp, (ulong)avail);
365 static int sandbox_swap_case_unmap_physmem(struct udevice *dev,
366 const void *vaddr, unsigned long len)
368 struct swap_case_priv *priv = dev_get_priv(dev);
370 sandbox_swap_case_do_op(priv->op, (void *)vaddr, len);
375 static struct dm_pci_emul_ops sandbox_swap_case_emul_ops = {
376 .read_config = sandbox_swap_case_read_config,
377 .write_config = sandbox_swap_case_write_config,
378 .read_io = sandbox_swap_case_read_io,
379 .write_io = sandbox_swap_case_write_io,
380 .map_physmem = sandbox_swap_case_map_physmem,
381 .unmap_physmem = sandbox_swap_case_unmap_physmem,
384 static const struct udevice_id sandbox_swap_case_ids[] = {
385 { .compatible = "sandbox,swap-case" },
389 U_BOOT_DRIVER(sandbox_swap_case_emul) = {
390 .name = "sandbox_swap_case_emul",
391 .id = UCLASS_PCI_EMUL,
392 .of_match = sandbox_swap_case_ids,
393 .ops = &sandbox_swap_case_emul_ops,
394 .priv_auto = sizeof(struct swap_case_priv),
395 .plat_auto = sizeof(struct swap_case_plat),
398 static struct pci_device_id sandbox_swap_case_supported[] = {
399 { PCI_VDEVICE(SANDBOX, SANDBOX_PCI_SWAP_CASE_EMUL_ID),
400 SWAP_CASE_DRV_DATA },
404 U_BOOT_PCI_DEVICE(sandbox_swap_case_emul, sandbox_swap_case_supported);