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
21 * struct p2sb_emul_platdata - platform data for this device
23 * @command: Current PCI command value
24 * @bar: Current base address values
26 struct p2sb_emul_platdata {
32 /* This emulator supports 16 different devices */
33 MEMMAP_SIZE = 16 << PCR_PORTID_SHIFT,
36 static struct pci_bar {
40 { PCI_BASE_ADDRESS_MEM_TYPE_32, MEMMAP_SIZE },
48 struct p2sb_emul_priv {
52 static int sandbox_p2sb_emul_read_config(const struct udevice *emul,
53 uint offset, ulong *valuep,
56 struct p2sb_emul_platdata *plat = dev_get_platdata(emul);
60 *valuep = plat->command;
63 *valuep = PCI_HEADER_TYPE_NORMAL;
66 *valuep = SANDBOX_PCI_VENDOR_ID;
69 *valuep = SANDBOX_PCI_P2SB_EMUL_ID;
71 case PCI_CLASS_DEVICE:
72 if (size == PCI_SIZE_8) {
73 *valuep = SANDBOX_PCI_CLASS_SUB_CODE;
75 *valuep = (SANDBOX_PCI_CLASS_CODE << 8) |
76 SANDBOX_PCI_CLASS_SUB_CODE;
80 *valuep = SANDBOX_PCI_CLASS_CODE;
82 case PCI_BASE_ADDRESS_0:
83 case PCI_BASE_ADDRESS_1:
84 case PCI_BASE_ADDRESS_2:
85 case PCI_BASE_ADDRESS_3:
86 case PCI_BASE_ADDRESS_4:
87 case PCI_BASE_ADDRESS_5: {
91 barnum = pci_offset_to_barnum(offset);
92 bar = &plat->bar[barnum];
94 *valuep = sandbox_pci_read_bar(*bar, barinfo[barnum].type,
95 barinfo[barnum].size);
98 case PCI_CAPABILITY_LIST:
99 *valuep = PCI_CAP_ID_PM_OFFSET;
106 static int sandbox_p2sb_emul_write_config(struct udevice *emul, uint offset,
107 ulong value, enum pci_size_t size)
109 struct p2sb_emul_platdata *plat = dev_get_platdata(emul);
113 plat->command = value;
115 case PCI_BASE_ADDRESS_0:
116 case PCI_BASE_ADDRESS_1: {
120 barnum = pci_offset_to_barnum(offset);
121 bar = &plat->bar[barnum];
123 log_debug("w bar %d=%lx\n", barnum, value);
125 /* space indicator (bit#0) is read-only */
126 *bar |= barinfo[barnum].type;
134 static int sandbox_p2sb_emul_find_bar(struct udevice *emul, unsigned int addr,
135 int *barnump, unsigned int *offsetp)
137 struct p2sb_emul_platdata *plat = dev_get_platdata(emul);
140 for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
141 unsigned int size = barinfo[barnum].size;
142 u32 base = plat->bar[barnum] & ~PCI_BASE_ADDRESS_SPACE;
144 if (addr >= base && addr < base + size) {
146 *offsetp = addr - base;
155 static int sandbox_p2sb_emul_read_io(struct udevice *dev, unsigned int addr,
156 ulong *valuep, enum pci_size_t size)
162 ret = sandbox_p2sb_emul_find_bar(dev, addr, &barnum, &offset);
168 else if (barnum == 0)
174 static int sandbox_p2sb_emul_write_io(struct udevice *dev, unsigned int addr,
175 ulong value, enum pci_size_t size)
181 ret = sandbox_p2sb_emul_find_bar(dev, addr, &barnum, &offset);
188 static int find_p2sb_channel(struct udevice *emul, uint offset,
189 struct udevice **devp)
191 uint pid = offset >> PCR_PORTID_SHIFT;
192 struct udevice *p2sb, *dev;
195 ret = sandbox_pci_get_client(emul, &p2sb);
197 return log_msg_ret("No client", ret);
199 device_foreach_child(dev, p2sb) {
200 struct p2sb_child_platdata *pplat =
201 dev_get_parent_platdata(dev);
203 log_debug(" - child %s, pid %d, want %d\n", dev->name,
205 if (pid == pplat->pid) {
214 static int sandbox_p2sb_emul_map_physmem(struct udevice *dev,
215 phys_addr_t addr, unsigned long *lenp,
218 struct p2sb_emul_priv *priv = dev_get_priv(dev);
219 struct udevice *child = NULL; /* Silence compiler warning */
224 log_debug("map %x: ", (uint)addr);
225 ret = sandbox_p2sb_emul_find_bar(dev, addr, &barnum, &offset);
227 return log_msg_ret("Cannot find bar", ret);
228 log_debug("bar %d, offset %x\n", barnum, offset);
231 return log_msg_ret("Unknown BAR", -EINVAL);
233 ret = find_p2sb_channel(dev, offset, &child);
235 return log_msg_ret("Cannot find channel", ret);
237 offset &= ((1 << PCR_PORTID_SHIFT) - 1);
238 ret = axi_read(child, offset, priv->regs, AXI_SIZE_32);
240 return log_msg_ret("Child read failed", ret);
241 *ptrp = priv->regs + (offset & 3);
247 static struct dm_pci_emul_ops sandbox_p2sb_emul_emul_ops = {
248 .read_config = sandbox_p2sb_emul_read_config,
249 .write_config = sandbox_p2sb_emul_write_config,
250 .read_io = sandbox_p2sb_emul_read_io,
251 .write_io = sandbox_p2sb_emul_write_io,
252 .map_physmem = sandbox_p2sb_emul_map_physmem,
255 static const struct udevice_id sandbox_p2sb_emul_ids[] = {
256 { .compatible = "sandbox,p2sb-emul" },
260 U_BOOT_DRIVER(sandbox_p2sb_emul_emul) = {
261 .name = "sandbox_p2sb_emul_emul",
262 .id = UCLASS_PCI_EMUL,
263 .of_match = sandbox_p2sb_emul_ids,
264 .ops = &sandbox_p2sb_emul_emul_ops,
265 .priv_auto_alloc_size = sizeof(struct p2sb_emul_priv),
266 .platdata_auto_alloc_size = sizeof(struct p2sb_emul_platdata),
269 static struct pci_device_id sandbox_p2sb_emul_supported[] = {
270 { PCI_VDEVICE(SANDBOX, SANDBOX_PCI_PMC_EMUL_ID) },
274 U_BOOT_PCI_DEVICE(sandbox_p2sb_emul_emul, sandbox_p2sb_emul_supported);