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