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);
126 static int sandbox_swap_case_write_config(struct udevice *emul, uint offset,
127 ulong value, enum pci_size_t size)
129 struct swap_case_platdata *plat = dev_get_platdata(emul);
133 plat->command = value;
135 case PCI_BASE_ADDRESS_0:
136 case PCI_BASE_ADDRESS_1: {
140 barnum = offset_to_barnum(offset);
141 bar = &plat->bar[barnum];
143 debug("w bar %d=%lx\n", barnum, value);
152 static int sandbox_swap_case_find_bar(struct udevice *emul, unsigned int addr,
153 int *barnump, unsigned int *offsetp)
155 struct swap_case_platdata *plat = dev_get_platdata(emul);
158 for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
159 unsigned int size = barinfo[barnum].size;
161 if (addr >= plat->bar[barnum] &&
162 addr < plat->bar[barnum] + size) {
164 *offsetp = addr - plat->bar[barnum];
173 static void sandbox_swap_case_do_op(enum swap_case_op op, char *str, int len)
175 for (; len > 0; len--, str++) {
178 *str = toupper(*str);
181 *str = tolower(*str);
185 *str = tolower(*str);
187 *str = toupper(*str);
193 int sandbox_swap_case_read_io(struct udevice *dev, unsigned int addr,
194 ulong *valuep, enum pci_size_t size)
196 struct swap_case_priv *priv = dev_get_priv(dev);
201 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
205 if (barnum == 0 && offset == 0)
206 *valuep = (*valuep & ~0xff) | priv->op;
211 int sandbox_swap_case_write_io(struct udevice *dev, unsigned int addr,
212 ulong value, enum pci_size_t size)
214 struct swap_case_priv *priv = dev_get_priv(dev);
219 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
222 if (barnum == 0 && offset == 0)
228 static int sandbox_swap_case_map_physmem(struct udevice *dev,
229 phys_addr_t addr, unsigned long *lenp, void **ptrp)
231 struct swap_case_priv *priv = dev_get_priv(dev);
232 unsigned int offset, avail;
236 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
240 *ptrp = priv->mem_text + offset;
241 avail = barinfo[1].size - offset;
242 if (avail > barinfo[1].size)
245 *lenp = min(*lenp, (ulong)avail);
253 static int sandbox_swap_case_unmap_physmem(struct udevice *dev,
254 const void *vaddr, unsigned long len)
256 struct swap_case_priv *priv = dev_get_priv(dev);
258 sandbox_swap_case_do_op(priv->op, (void *)vaddr, len);
263 struct dm_pci_emul_ops sandbox_swap_case_emul_ops = {
264 .get_devfn = sandbox_swap_case_get_devfn,
265 .read_config = sandbox_swap_case_read_config,
266 .write_config = sandbox_swap_case_write_config,
267 .read_io = sandbox_swap_case_read_io,
268 .write_io = sandbox_swap_case_write_io,
269 .map_physmem = sandbox_swap_case_map_physmem,
270 .unmap_physmem = sandbox_swap_case_unmap_physmem,
273 static const struct udevice_id sandbox_swap_case_ids[] = {
274 { .compatible = "sandbox,swap-case" },
278 U_BOOT_DRIVER(sandbox_swap_case_emul) = {
279 .name = "sandbox_swap_case_emul",
280 .id = UCLASS_PCI_EMUL,
281 .of_match = sandbox_swap_case_ids,
282 .ops = &sandbox_swap_case_emul_ops,
283 .priv_auto_alloc_size = sizeof(struct swap_case_priv),
284 .platdata_auto_alloc_size = sizeof(struct swap_case_platdata),