1 // SPDX-License-Identifier: GPL-2.0+
3 * PCI emulation device for an x86 Primary-to-Sideband bus
5 * Copyright 2019 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
9 #define LOG_CATEGORY UCLASS_MISC
19 * struct p2sb_emul_plat - platform data for this device
21 * @command: Current PCI command value
22 * @bar: Current base address values
24 struct p2sb_emul_plat {
30 /* This emulator supports 16 different devices */
31 MEMMAP_SIZE = 16 << PCR_PORTID_SHIFT,
34 static struct pci_bar {
38 { PCI_BASE_ADDRESS_MEM_TYPE_32, MEMMAP_SIZE },
46 struct p2sb_emul_priv {
50 static int sandbox_p2sb_emul_read_config(const struct udevice *emul,
51 uint offset, ulong *valuep,
54 struct p2sb_emul_plat *plat = dev_get_plat(emul);
58 *valuep = plat->command;
61 *valuep = PCI_HEADER_TYPE_NORMAL;
64 *valuep = SANDBOX_PCI_VENDOR_ID;
67 *valuep = SANDBOX_PCI_P2SB_EMUL_ID;
69 case PCI_CLASS_DEVICE:
70 if (size == PCI_SIZE_8) {
71 *valuep = SANDBOX_PCI_CLASS_SUB_CODE;
73 *valuep = (SANDBOX_PCI_CLASS_CODE << 8) |
74 SANDBOX_PCI_CLASS_SUB_CODE;
78 *valuep = SANDBOX_PCI_CLASS_CODE;
80 case PCI_BASE_ADDRESS_0:
81 case PCI_BASE_ADDRESS_1:
82 case PCI_BASE_ADDRESS_2:
83 case PCI_BASE_ADDRESS_3:
84 case PCI_BASE_ADDRESS_4:
85 case PCI_BASE_ADDRESS_5: {
89 barnum = pci_offset_to_barnum(offset);
90 bar = &plat->bar[barnum];
92 *valuep = sandbox_pci_read_bar(*bar, barinfo[barnum].type,
93 barinfo[barnum].size);
96 case PCI_CAPABILITY_LIST:
97 *valuep = PCI_CAP_ID_PM_OFFSET;
104 static int sandbox_p2sb_emul_write_config(struct udevice *emul, uint offset,
105 ulong value, enum pci_size_t size)
107 struct p2sb_emul_plat *plat = dev_get_plat(emul);
111 plat->command = value;
113 case PCI_BASE_ADDRESS_0:
114 case PCI_BASE_ADDRESS_1: {
118 barnum = pci_offset_to_barnum(offset);
119 bar = &plat->bar[barnum];
121 log_debug("w bar %d=%lx\n", barnum, value);
123 /* space indicator (bit#0) is read-only */
124 *bar |= barinfo[barnum].type;
132 static int sandbox_p2sb_emul_find_bar(struct udevice *emul, unsigned int addr,
133 int *barnump, unsigned int *offsetp)
135 struct p2sb_emul_plat *plat = dev_get_plat(emul);
138 for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
139 unsigned int size = barinfo[barnum].size;
140 u32 base = plat->bar[barnum] & ~PCI_BASE_ADDRESS_SPACE;
142 if (addr >= base && addr < base + size) {
144 *offsetp = addr - base;
153 static int sandbox_p2sb_emul_read_io(struct udevice *dev, unsigned int addr,
154 ulong *valuep, enum pci_size_t size)
160 ret = sandbox_p2sb_emul_find_bar(dev, addr, &barnum, &offset);
166 else if (barnum == 0)
172 static int sandbox_p2sb_emul_write_io(struct udevice *dev, unsigned int addr,
173 ulong value, enum pci_size_t size)
179 ret = sandbox_p2sb_emul_find_bar(dev, addr, &barnum, &offset);
186 static int find_p2sb_channel(struct udevice *emul, uint offset,
187 struct udevice **devp)
189 uint pid = offset >> PCR_PORTID_SHIFT;
190 struct udevice *p2sb, *dev;
193 ret = sandbox_pci_get_client(emul, &p2sb);
195 return log_msg_ret("No client", ret);
197 device_foreach_child(dev, p2sb) {
198 struct p2sb_child_plat *pplat =
199 dev_get_parent_plat(dev);
201 log_debug(" - child %s, pid %d, want %d\n", dev->name,
203 if (pid == pplat->pid) {
212 static int sandbox_p2sb_emul_map_physmem(struct udevice *dev,
213 phys_addr_t addr, unsigned long *lenp,
216 struct p2sb_emul_priv *priv = dev_get_priv(dev);
217 struct udevice *child = NULL; /* Silence compiler warning */
222 log_debug("map %x: ", (uint)addr);
223 ret = sandbox_p2sb_emul_find_bar(dev, addr, &barnum, &offset);
225 return log_msg_ret("Cannot find bar", ret);
226 log_debug("bar %d, offset %x\n", barnum, offset);
229 return log_msg_ret("Unknown BAR", -EINVAL);
231 ret = find_p2sb_channel(dev, offset, &child);
233 return log_msg_ret("Cannot find channel", ret);
235 offset &= ((1 << PCR_PORTID_SHIFT) - 1);
236 ret = axi_read(child, offset, priv->regs, AXI_SIZE_32);
238 return log_msg_ret("Child read failed", ret);
239 *ptrp = priv->regs + (offset & 3);
245 static struct dm_pci_emul_ops sandbox_p2sb_emul_emul_ops = {
246 .read_config = sandbox_p2sb_emul_read_config,
247 .write_config = sandbox_p2sb_emul_write_config,
248 .read_io = sandbox_p2sb_emul_read_io,
249 .write_io = sandbox_p2sb_emul_write_io,
250 .map_physmem = sandbox_p2sb_emul_map_physmem,
253 static const struct udevice_id sandbox_p2sb_emul_ids[] = {
254 { .compatible = "sandbox,p2sb-emul" },
258 U_BOOT_DRIVER(sandbox_p2sb_emul_emul) = {
259 .name = "sandbox_p2sb_emul_emul",
260 .id = UCLASS_PCI_EMUL,
261 .of_match = sandbox_p2sb_emul_ids,
262 .ops = &sandbox_p2sb_emul_emul_ops,
263 .priv_auto = sizeof(struct p2sb_emul_priv),
264 .plat_auto = sizeof(struct p2sb_emul_plat),
267 static struct pci_device_id sandbox_p2sb_emul_supported[] = {
268 { PCI_VDEVICE(SANDBOX, SANDBOX_PCI_PMC_EMUL_ID) },
272 U_BOOT_PCI_DEVICE(sandbox_p2sb_emul_emul, sandbox_p2sb_emul_supported);