2 * PCI emulation device which swaps the case of text
4 * Copyright (c) 2014 Google, Inc
5 * Written by Simon Glass <sjg@chromium.org>
7 * SPDX-License-Identifier: GPL-2.0+
15 #include <linux/ctype.h>
18 * struct swap_case_platdata - platform data for this device
20 * @command: Current PCI command value
21 * @bar: Current base address values
23 struct swap_case_platdata {
28 #define offset_to_barnum(offset) \
29 (((offset) - PCI_BASE_ADDRESS_0) / sizeof(u32))
32 MEM_TEXT_SIZE = 0x100,
41 static struct pci_bar {
45 { PCI_BASE_ADDRESS_SPACE_IO, 1 },
46 { PCI_BASE_ADDRESS_MEM_TYPE_32, MEM_TEXT_SIZE },
53 struct swap_case_priv {
55 char mem_text[MEM_TEXT_SIZE];
58 static int sandbox_swap_case_get_devfn(struct udevice *dev)
60 struct pci_child_platdata *plat = dev_get_parent_platdata(dev);
65 static int sandbox_swap_case_read_config(struct udevice *emul, uint offset,
66 ulong *valuep, enum pci_size_t size)
68 struct swap_case_platdata *plat = dev_get_platdata(emul);
72 *valuep = plat->command;
78 *valuep = SANDBOX_PCI_VENDOR_ID;
81 *valuep = SANDBOX_PCI_DEVICE_ID;
83 case PCI_CLASS_DEVICE:
84 if (size == PCI_SIZE_8) {
85 *valuep = SANDBOX_PCI_CLASS_SUB_CODE;
87 *valuep = (SANDBOX_PCI_CLASS_CODE << 8) |
88 SANDBOX_PCI_CLASS_SUB_CODE;
92 *valuep = SANDBOX_PCI_CLASS_CODE;
94 case PCI_BASE_ADDRESS_0:
95 case PCI_BASE_ADDRESS_1:
96 case PCI_BASE_ADDRESS_2:
97 case PCI_BASE_ADDRESS_3:
98 case PCI_BASE_ADDRESS_4:
99 case PCI_BASE_ADDRESS_5: {
103 barnum = offset_to_barnum(offset);
104 bar = &plat->bar[barnum];
107 if (*bar == 0xffffffff) {
108 if (barinfo[barnum].type) {
109 result = (~(barinfo[barnum].size - 1) &
110 PCI_BASE_ADDRESS_IO_MASK) |
111 PCI_BASE_ADDRESS_SPACE_IO;
113 result = (~(barinfo[barnum].size - 1) &
114 PCI_BASE_ADDRESS_MEM_MASK) |
115 PCI_BASE_ADDRESS_MEM_TYPE_32;
118 debug("r bar %d=%x\n", barnum, result);
127 static int sandbox_swap_case_write_config(struct udevice *emul, uint offset,
128 ulong value, enum pci_size_t size)
130 struct swap_case_platdata *plat = dev_get_platdata(emul);
134 plat->command = value;
136 case PCI_BASE_ADDRESS_0:
137 case PCI_BASE_ADDRESS_1: {
141 barnum = offset_to_barnum(offset);
142 bar = &plat->bar[barnum];
144 debug("w bar %d=%lx\n", barnum, value);
153 static int sandbox_swap_case_find_bar(struct udevice *emul, unsigned int addr,
154 int *barnump, unsigned int *offsetp)
156 struct swap_case_platdata *plat = dev_get_platdata(emul);
159 for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
160 unsigned int size = barinfo[barnum].size;
162 if (addr >= plat->bar[barnum] &&
163 addr < plat->bar[barnum] + size) {
165 *offsetp = addr - plat->bar[barnum];
174 static void sandbox_swap_case_do_op(enum swap_case_op op, char *str, int len)
176 for (; len > 0; len--, str++) {
179 *str = toupper(*str);
182 *str = tolower(*str);
186 *str = tolower(*str);
188 *str = toupper(*str);
194 int sandbox_swap_case_read_io(struct udevice *dev, unsigned int addr,
195 ulong *valuep, enum pci_size_t size)
197 struct swap_case_priv *priv = dev_get_priv(dev);
202 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
206 if (barnum == 0 && offset == 0)
207 *valuep = (*valuep & ~0xff) | priv->op;
212 int sandbox_swap_case_write_io(struct udevice *dev, unsigned int addr,
213 ulong value, enum pci_size_t size)
215 struct swap_case_priv *priv = dev_get_priv(dev);
220 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
223 if (barnum == 0 && offset == 0)
229 static int sandbox_swap_case_map_physmem(struct udevice *dev,
230 phys_addr_t addr, unsigned long *lenp, void **ptrp)
232 struct swap_case_priv *priv = dev_get_priv(dev);
233 unsigned int offset, avail;
237 ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
241 *ptrp = priv->mem_text + offset;
242 avail = barinfo[1].size - offset;
243 if (avail > barinfo[1].size)
246 *lenp = min(*lenp, (ulong)avail);
254 static int sandbox_swap_case_unmap_physmem(struct udevice *dev,
255 const void *vaddr, unsigned long len)
257 struct swap_case_priv *priv = dev_get_priv(dev);
259 sandbox_swap_case_do_op(priv->op, (void *)vaddr, len);
264 struct dm_pci_emul_ops sandbox_swap_case_emul_ops = {
265 .get_devfn = sandbox_swap_case_get_devfn,
266 .read_config = sandbox_swap_case_read_config,
267 .write_config = sandbox_swap_case_write_config,
268 .read_io = sandbox_swap_case_read_io,
269 .write_io = sandbox_swap_case_write_io,
270 .map_physmem = sandbox_swap_case_map_physmem,
271 .unmap_physmem = sandbox_swap_case_unmap_physmem,
274 static const struct udevice_id sandbox_swap_case_ids[] = {
275 { .compatible = "sandbox,swap-case" },
279 U_BOOT_DRIVER(sandbox_swap_case_emul) = {
280 .name = "sandbox_swap_case_emul",
281 .id = UCLASS_PCI_EMUL,
282 .of_match = sandbox_swap_case_ids,
283 .ops = &sandbox_swap_case_emul_ops,
284 .priv_auto_alloc_size = sizeof(struct swap_case_priv),
285 .platdata_auto_alloc_size = sizeof(struct swap_case_platdata),