pci_sandbox.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include <common.h>
  8. #include <dm.h>
  9. #include <fdtdec.h>
  10. #include <inttypes.h>
  11. #include <pci.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  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. ret = sandbox_pci_get_emul(bus, devfn, &emul);
  21. if (ret)
  22. return ret == -ENODEV ? 0 : ret;
  23. ops = pci_get_emul_ops(emul);
  24. if (!ops || !ops->write_config)
  25. return -ENOSYS;
  26. return ops->write_config(emul, offset, value, size);
  27. }
  28. static int sandbox_pci_read_config(struct udevice *bus, pci_dev_t devfn,
  29. uint offset, ulong *valuep,
  30. enum pci_size_t size)
  31. {
  32. struct dm_pci_emul_ops *ops;
  33. struct udevice *emul;
  34. int ret;
  35. /* Prepare the default response */
  36. *valuep = pci_get_ff(size);
  37. ret = sandbox_pci_get_emul(bus, devfn, &emul);
  38. if (ret)
  39. return ret == -ENODEV ? 0 : ret;
  40. ops = pci_get_emul_ops(emul);
  41. if (!ops || !ops->read_config)
  42. return -ENOSYS;
  43. return ops->read_config(emul, offset, valuep, size);
  44. }
  45. static int sandbox_pci_child_post_bind(struct udevice *dev)
  46. {
  47. /* Attach an emulator if we can */
  48. return dm_scan_fdt_dev(dev);
  49. }
  50. static const struct dm_pci_ops sandbox_pci_ops = {
  51. .read_config = sandbox_pci_read_config,
  52. .write_config = sandbox_pci_write_config,
  53. };
  54. static const struct udevice_id sandbox_pci_ids[] = {
  55. { .compatible = "sandbox,pci" },
  56. { }
  57. };
  58. U_BOOT_DRIVER(pci_sandbox) = {
  59. .name = "pci_sandbox",
  60. .id = UCLASS_PCI,
  61. .of_match = sandbox_pci_ids,
  62. .ops = &sandbox_pci_ops,
  63. .child_post_bind = sandbox_pci_child_post_bind,
  64. .per_child_platdata_auto_alloc_size =
  65. sizeof(struct pci_child_platdata),
  66. };