xref: /openbmc/u-boot/drivers/pci/pci_sandbox.c (revision 6e87ae1c)
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 const struct dm_pci_ops sandbox_pci_ops = {
55 	.read_config = sandbox_pci_read_config,
56 	.write_config = sandbox_pci_write_config,
57 };
58 
59 static const struct udevice_id sandbox_pci_ids[] = {
60 	{ .compatible = "sandbox,pci" },
61 	{ }
62 };
63 
64 U_BOOT_DRIVER(pci_sandbox) = {
65 	.name	= "pci_sandbox",
66 	.id	= UCLASS_PCI,
67 	.of_match = sandbox_pci_ids,
68 	.ops	= &sandbox_pci_ops,
69 
70 	/* Attach an emulator if we can */
71 	.child_post_bind = dm_scan_fdt_dev,
72 	.per_child_platdata_auto_alloc_size =
73 			sizeof(struct pci_child_platdata),
74 };
75