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>
14 #include <linux/ctype.h>
17 * struct swap_case_platdata - platform data for this device
19 * @command: Current PCI command value
20 * @bar: Current base address values
22 struct swap_case_platdata {
27 #define offset_to_barnum(offset) \
28 (((offset) - PCI_BASE_ADDRESS_0) / sizeof(u32))
31 MEM_TEXT_SIZE = 0x100,
40 static struct pci_bar {
44 { PCI_BASE_ADDRESS_SPACE_IO, 1 },
45 { PCI_BASE_ADDRESS_MEM_TYPE_32, MEM_TEXT_SIZE },
52 struct swap_case_priv {
54 char mem_text[MEM_TEXT_SIZE];
57 static int sandbox_swap_case_get_devfn(struct udevice *dev)
59 struct pci_child_platdata *plat = dev_get_parent_platdata(dev);
64 static int sandbox_swap_case_read_config(struct udevice *emul, uint offset,
65 ulong *valuep, enum pci_size_t size)
67 struct swap_case_platdata *plat = dev_get_platdata(emul);
71 *valuep = plat->command;
77 *valuep = SANDBOX_PCI_VENDOR_ID;
80 *valuep = SANDBOX_PCI_DEVICE_ID;
82 case PCI_CLASS_DEVICE:
83 if (size == PCI_SIZE_8) {
84 *valuep = SANDBOX_PCI_CLASS_SUB_CODE;
86 *valuep = (SANDBOX_PCI_CLASS_CODE << 8) |
87 SANDBOX_PCI_CLASS_SUB_CODE;
91 *valuep = SANDBOX_PCI_CLASS_CODE;
93 case PCI_BASE_ADDRESS_0:
94 case PCI_BASE_ADDRESS_1:
95 case PCI_BASE_ADDRESS_2:
96 case PCI_BASE_ADDRESS_3:
97 case PCI_BASE_ADDRESS_4:
98 case PCI_BASE_ADDRESS_5: {
102 barnum = offset_to_barnum(offset);
103 bar = &plat->bar[barnum];
106 if (*bar == 0xffffffff) {
107 if (barinfo[barnum].type) {
108 result = (~(barinfo[barnum].size - 1) &
109 PCI_BASE_ADDRESS_IO_MASK) |
110 PCI_BASE_ADDRESS_SPACE_IO;
112 result = (~(barinfo[barnum].size - 1) &
113 PCI_BASE_ADDRESS_MEM_MASK) |
114 PCI_BASE_ADDRESS_MEM_TYPE_32;
117 debug("r bar %d=%x\n", barnum, result);
121 case PCI_CAPABILITY_LIST:
122 *valuep = PCI_CAP_ID_PM_OFFSET;
124 case PCI_CAP_ID_PM_OFFSET:
125 *valuep = (PCI_CAP_ID_EXP_OFFSET << 8) | PCI_CAP_ID_PM;
127 case PCI_CAP_ID_EXP_OFFSET:
128 *valuep = (PCI_CAP_ID_MSIX_OFFSET << 8) | PCI_CAP_ID_EXP;
130 case PCI_CAP_ID_MSIX_OFFSET:
131 *valuep = PCI_CAP_ID_MSIX;
133 case PCI_EXT_CAP_ID_ERR_OFFSET:
134 *valuep = (PCI_EXT_CAP_ID_VC_OFFSET << 20) | PCI_EXT_CAP_ID_ERR;
136 case PCI_EXT_CAP_ID_VC_OFFSET:
137 *valuep = (PCI_EXT_CAP_ID_DSN_OFFSET << 20) | PCI_EXT_CAP_ID_VC;
139 case PCI_EXT_CAP_ID_DSN_OFFSET:
140 *valuep = PCI_EXT_CAP_ID_DSN;
147 static int sandbox_swap_case_write_config(struct udevice *emul, uint offset,
148 ulong value, enum pci_size_t size)
150 struct swap_case_platdata *plat = dev_get_platdata(emul);
154 plat->command = value;
156 case PCI_BASE_ADDRESS_0:
157 case PCI_BASE_ADDRESS_1: {
161 barnum = offset_to_barnum(offset);
162 bar = &plat->bar[barnum];
164 debug("w bar %d=%lx\n", barnum, value);
166 /* space indicator (bit#0) is read-only */
167 *bar |= barinfo[barnum].type;
175 static int sandbox_swap_case_find_bar(struct udevice *emul, unsigned int addr,
176 int *barnump, unsigned int *offsetp)
178 struct swap_case_platdata *plat = dev_get_platdata(emul);
181 for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
182 unsigned int size = barinfo[barnum].size;
183 u32 base = plat->bar[barnum] & ~PCI_BASE_ADDRESS_SPACE;
185 if (addr >= base && addr < base + size) {
187 *offsetp = addr - base;
196 static void sandbox_swap_case_do_op(enum swap_case_op op, char *str, int len)
198 for (; len > 0; len--, str++) {
201 *str = toupper(*str);
204 *str = tolower(*str);
208 *str = tolower(*str);
210 *str = toupper(*str);
216 int sandbox_swap_case_read_io(struct udevice *dev, unsigned int addr,
217 ulong *valuep, enum pci_size_t size)
219 struct swap_case_priv *priv = dev_get_priv(dev);
224 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
228 if (barnum == 0 && offset == 0)
229 *valuep = (*valuep & ~0xff) | priv->op;
234 int sandbox_swap_case_write_io(struct udevice *dev, unsigned int addr,
235 ulong value, enum pci_size_t size)
237 struct swap_case_priv *priv = dev_get_priv(dev);
242 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
245 if (barnum == 0 && offset == 0)
251 static int sandbox_swap_case_map_physmem(struct udevice *dev,
252 phys_addr_t addr, unsigned long *lenp, void **ptrp)
254 struct swap_case_priv *priv = dev_get_priv(dev);
255 unsigned int offset, avail;
259 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
263 *ptrp = priv->mem_text + offset;
264 avail = barinfo[1].size - offset;
265 if (avail > barinfo[1].size)
268 *lenp = min(*lenp, (ulong)avail);
276 static int sandbox_swap_case_unmap_physmem(struct udevice *dev,
277 const void *vaddr, unsigned long len)
279 struct swap_case_priv *priv = dev_get_priv(dev);
281 sandbox_swap_case_do_op(priv->op, (void *)vaddr, len);
286 struct dm_pci_emul_ops sandbox_swap_case_emul_ops = {
287 .get_devfn = sandbox_swap_case_get_devfn,
288 .read_config = sandbox_swap_case_read_config,
289 .write_config = sandbox_swap_case_write_config,
290 .read_io = sandbox_swap_case_read_io,
291 .write_io = sandbox_swap_case_write_io,
292 .map_physmem = sandbox_swap_case_map_physmem,
293 .unmap_physmem = sandbox_swap_case_unmap_physmem,
296 static const struct udevice_id sandbox_swap_case_ids[] = {
297 { .compatible = "sandbox,swap-case" },
301 U_BOOT_DRIVER(sandbox_swap_case_emul) = {
302 .name = "sandbox_swap_case_emul",
303 .id = UCLASS_PCI_EMUL,
304 .of_match = sandbox_swap_case_ids,
305 .ops = &sandbox_swap_case_emul_ops,
306 .priv_auto_alloc_size = sizeof(struct swap_case_priv),
307 .platdata_auto_alloc_size = sizeof(struct swap_case_platdata),
310 static struct pci_device_id sandbox_swap_case_supported[] = {
311 { PCI_VDEVICE(SANDBOX, SANDBOX_PCI_DEVICE_ID), SWAP_CASE_DRV_DATA },
315 U_BOOT_PCI_DEVICE(sandbox_swap_case_emul, sandbox_swap_case_supported);