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
20 * struct p2sb_emul_plat - platform data for this device
22 * @command: Current PCI command value
23 * @bar: Current base address values
25 struct p2sb_emul_plat {
31 /* This emulator supports 16 different devices */
32 MEMMAP_SIZE = 16 << PCR_PORTID_SHIFT,
35 static struct pci_bar {
39 { PCI_BASE_ADDRESS_MEM_TYPE_32, MEMMAP_SIZE },
47 struct p2sb_emul_priv {
51 static int sandbox_p2sb_emul_read_config(const struct udevice *emul,
52 uint offset, ulong *valuep,
55 struct p2sb_emul_plat *plat = dev_get_plat(emul);
59 *valuep = plat->command;
62 *valuep = PCI_HEADER_TYPE_NORMAL;
65 *valuep = SANDBOX_PCI_VENDOR_ID;
68 *valuep = SANDBOX_PCI_P2SB_EMUL_ID;
70 case PCI_CLASS_DEVICE:
71 if (size == PCI_SIZE_8) {
72 *valuep = SANDBOX_PCI_CLASS_SUB_CODE;
74 *valuep = (SANDBOX_PCI_CLASS_CODE << 8) |
75 SANDBOX_PCI_CLASS_SUB_CODE;
79 *valuep = SANDBOX_PCI_CLASS_CODE;
81 case PCI_BASE_ADDRESS_0:
82 case PCI_BASE_ADDRESS_1:
83 case PCI_BASE_ADDRESS_2:
84 case PCI_BASE_ADDRESS_3:
85 case PCI_BASE_ADDRESS_4:
86 case PCI_BASE_ADDRESS_5: {
90 barnum = pci_offset_to_barnum(offset);
91 bar = &plat->bar[barnum];
93 *valuep = sandbox_pci_read_bar(*bar, barinfo[barnum].type,
94 barinfo[barnum].size);
97 case PCI_CAPABILITY_LIST:
98 *valuep = PCI_CAP_ID_PM_OFFSET;
105 static int sandbox_p2sb_emul_write_config(struct udevice *emul, uint offset,
106 ulong value, enum pci_size_t size)
108 struct p2sb_emul_plat *plat = dev_get_plat(emul);
112 plat->command = value;
114 case PCI_BASE_ADDRESS_0:
115 case PCI_BASE_ADDRESS_1: {
119 barnum = pci_offset_to_barnum(offset);
120 bar = &plat->bar[barnum];
122 log_debug("w bar %d=%lx\n", barnum, value);
124 /* space indicator (bit#0) is read-only */
125 *bar |= barinfo[barnum].type;
133 static int sandbox_p2sb_emul_find_bar(struct udevice *emul, unsigned int addr,
134 int *barnump, unsigned int *offsetp)
136 struct p2sb_emul_plat *plat = dev_get_plat(emul);
139 for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
140 unsigned int size = barinfo[barnum].size;
141 u32 base = plat->bar[barnum] & ~PCI_BASE_ADDRESS_SPACE;
143 if (addr >= base && addr < base + size) {
145 *offsetp = addr - base;
154 static int sandbox_p2sb_emul_read_io(struct udevice *dev, unsigned int addr,
155 ulong *valuep, enum pci_size_t size)
161 ret = sandbox_p2sb_emul_find_bar(dev, addr, &barnum, &offset);
167 else if (barnum == 0)
173 static int sandbox_p2sb_emul_write_io(struct udevice *dev, unsigned int addr,
174 ulong value, enum pci_size_t size)
180 ret = sandbox_p2sb_emul_find_bar(dev, addr, &barnum, &offset);
187 static int find_p2sb_channel(struct udevice *emul, uint offset,
188 struct udevice **devp)
190 uint pid = offset >> PCR_PORTID_SHIFT;
191 struct udevice *p2sb, *dev;
194 ret = sandbox_pci_get_client(emul, &p2sb);
196 return log_msg_ret("No client", ret);
198 device_foreach_child(dev, p2sb) {
199 struct p2sb_child_plat *pplat =
200 dev_get_parent_plat(dev);
202 log_debug(" - child %s, pid %d, want %d\n", dev->name,
204 if (pid == pplat->pid) {
213 static int sandbox_p2sb_emul_map_physmem(struct udevice *dev,
214 phys_addr_t addr, unsigned long *lenp,
217 struct p2sb_emul_priv *priv = dev_get_priv(dev);
218 struct udevice *child = NULL; /* Silence compiler warning */
223 log_debug("map %x: ", (uint)addr);
224 ret = sandbox_p2sb_emul_find_bar(dev, addr, &barnum, &offset);
226 return log_msg_ret("Cannot find bar", ret);
227 log_debug("bar %d, offset %x\n", barnum, offset);
230 return log_msg_ret("Unknown BAR", -EINVAL);
232 ret = find_p2sb_channel(dev, offset, &child);
234 return log_msg_ret("Cannot find channel", ret);
236 offset &= ((1 << PCR_PORTID_SHIFT) - 1);
237 ret = axi_read(child, offset, priv->regs, AXI_SIZE_32);
239 return log_msg_ret("Child read failed", ret);
240 *ptrp = priv->regs + (offset & 3);
246 static struct dm_pci_emul_ops sandbox_p2sb_emul_emul_ops = {
247 .read_config = sandbox_p2sb_emul_read_config,
248 .write_config = sandbox_p2sb_emul_write_config,
249 .read_io = sandbox_p2sb_emul_read_io,
250 .write_io = sandbox_p2sb_emul_write_io,
251 .map_physmem = sandbox_p2sb_emul_map_physmem,
254 static const struct udevice_id sandbox_p2sb_emul_ids[] = {
255 { .compatible = "sandbox,p2sb-emul" },
259 U_BOOT_DRIVER(sandbox_p2sb_emul_emul) = {
260 .name = "sandbox_p2sb_emul_emul",
261 .id = UCLASS_PCI_EMUL,
262 .of_match = sandbox_p2sb_emul_ids,
263 .ops = &sandbox_p2sb_emul_emul_ops,
264 .priv_auto = sizeof(struct p2sb_emul_priv),
265 .plat_auto = sizeof(struct p2sb_emul_plat),
268 static struct pci_device_id sandbox_p2sb_emul_supported[] = {
269 { PCI_VDEVICE(SANDBOX, SANDBOX_PCI_PMC_EMUL_ID) },
273 U_BOOT_PCI_DEVICE(sandbox_p2sb_emul_emul, sandbox_p2sb_emul_supported);