pci_sandbox.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include <dm/root.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static int sandbox_pci_write_config(struct udevice *bus, pci_dev_t devfn,
  15. uint offset, ulong value,
  16. enum pci_size_t size)
  17. {
  18. struct dm_pci_emul_ops *ops;
  19. struct udevice *emul;
  20. int ret;
  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. return ops->write_config(emul, offset, value, size);
  28. }
  29. static int sandbox_pci_read_config(struct udevice *bus, pci_dev_t devfn,
  30. uint offset, ulong *valuep,
  31. enum pci_size_t size)
  32. {
  33. struct dm_pci_emul_ops *ops;
  34. struct udevice *emul;
  35. int ret;
  36. /* Prepare the default response */
  37. *valuep = pci_get_ff(size);
  38. ret = sandbox_pci_get_emul(bus, devfn, &emul);
  39. if (ret)
  40. return ret == -ENODEV ? 0 : ret;
  41. ops = pci_get_emul_ops(emul);
  42. if (!ops || !ops->read_config)
  43. return -ENOSYS;
  44. return ops->read_config(emul, offset, valuep, size);
  45. }
  46. static int sandbox_pci_child_post_bind(struct udevice *dev)
  47. {
  48. /* Attach an emulator if we can */
  49. return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
  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. static const struct udevice_id sandbox_pci_ids[] = {
  56. { .compatible = "sandbox,pci" },
  57. { }
  58. };
  59. U_BOOT_DRIVER(pci_sandbox) = {
  60. .name = "pci_sandbox",
  61. .id = UCLASS_PCI,
  62. .of_match = sandbox_pci_ids,
  63. .ops = &sandbox_pci_ops,
  64. .child_post_bind = sandbox_pci_child_post_bind,
  65. .per_child_platdata_auto_alloc_size =
  66. sizeof(struct pci_child_platdata),
  67. };