xref: /openbmc/u-boot/drivers/pci/pci_sandbox.c (revision 63e22517)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2014 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <fdtdec.h>
10 #include <inttypes.h>
11 #include <pci.h>
12 
13 static int sandbox_pci_write_config(struct udevice *bus, pci_dev_t devfn,
14 				    uint offset, ulong value,
15 				    enum pci_size_t size)
16 {
17 	struct dm_pci_emul_ops *ops;
18 	struct udevice *emul;
19 	int ret;
20 
21 	ret = sandbox_pci_get_emul(bus, devfn, &emul);
22 	if (ret)
23 		return ret == -ENODEV ? 0 : ret;
24 	ops = pci_get_emul_ops(emul);
25 	if (!ops || !ops->write_config)
26 		return -ENOSYS;
27 
28 	return ops->write_config(emul, offset, value, size);
29 }
30 
31 static int sandbox_pci_read_config(struct udevice *bus, pci_dev_t devfn,
32 				   uint offset, ulong *valuep,
33 				   enum pci_size_t size)
34 {
35 	struct dm_pci_emul_ops *ops;
36 	struct udevice *emul;
37 	int ret;
38 
39 	/* Prepare the default response */
40 	*valuep = pci_get_ff(size);
41 	ret = sandbox_pci_get_emul(bus, devfn, &emul);
42 	if (ret)
43 		return ret == -ENODEV ? 0 : ret;
44 	ops = pci_get_emul_ops(emul);
45 	if (!ops || !ops->read_config)
46 		return -ENOSYS;
47 
48 	return ops->read_config(emul, offset, valuep, size);
49 }
50 
51 static const struct dm_pci_ops sandbox_pci_ops = {
52 	.read_config = sandbox_pci_read_config,
53 	.write_config = sandbox_pci_write_config,
54 };
55 
56 static const struct udevice_id sandbox_pci_ids[] = {
57 	{ .compatible = "sandbox,pci" },
58 	{ }
59 };
60 
61 U_BOOT_DRIVER(pci_sandbox) = {
62 	.name	= "pci_sandbox",
63 	.id	= UCLASS_PCI,
64 	.of_match = sandbox_pci_ids,
65 	.ops	= &sandbox_pci_ops,
66 
67 	/* Attach an emulator if we can */
68 	.child_post_bind = dm_scan_fdt_dev,
69 	.per_child_platdata_auto_alloc_size =
70 			sizeof(struct pci_child_platdata),
71 };
72