pci_sandbox.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2014 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <fdtdec.h>
  9. #include <log.h>
  10. #include <pci.h>
  11. #define FDT_DEV_INFO_CELLS 4
  12. #define FDT_DEV_INFO_SIZE (FDT_DEV_INFO_CELLS * sizeof(u32))
  13. #define SANDBOX_PCI_DEVFN(d, f) ((d << 3) | f)
  14. struct sandbox_pci_priv {
  15. struct {
  16. u16 vendor;
  17. u16 device;
  18. } vendev[256];
  19. };
  20. static int sandbox_pci_write_config(struct udevice *bus, pci_dev_t devfn,
  21. uint offset, ulong value,
  22. enum pci_size_t size)
  23. {
  24. struct dm_pci_emul_ops *ops;
  25. struct udevice *container, *emul;
  26. int ret;
  27. ret = sandbox_pci_get_emul(bus, devfn, &container, &emul);
  28. if (ret)
  29. return ret == -ENODEV ? 0 : ret;
  30. ops = pci_get_emul_ops(emul);
  31. if (!ops || !ops->write_config)
  32. return -ENOSYS;
  33. return ops->write_config(emul, offset, value, size);
  34. }
  35. static int sandbox_pci_read_config(const struct udevice *bus, pci_dev_t devfn,
  36. uint offset, ulong *valuep,
  37. enum pci_size_t size)
  38. {
  39. struct dm_pci_emul_ops *ops;
  40. struct udevice *container, *emul;
  41. struct sandbox_pci_priv *priv = dev_get_priv(bus);
  42. int ret;
  43. /* Prepare the default response */
  44. *valuep = pci_get_ff(size);
  45. ret = sandbox_pci_get_emul(bus, devfn, &container, &emul);
  46. if (ret) {
  47. if (!container) {
  48. u16 vendor, device;
  49. devfn = SANDBOX_PCI_DEVFN(PCI_DEV(devfn),
  50. PCI_FUNC(devfn));
  51. vendor = priv->vendev[devfn].vendor;
  52. device = priv->vendev[devfn].device;
  53. if (offset == PCI_VENDOR_ID && vendor)
  54. *valuep = vendor;
  55. else if (offset == PCI_DEVICE_ID && device)
  56. *valuep = device;
  57. return 0;
  58. } else {
  59. return ret == -ENODEV ? 0 : ret;
  60. }
  61. }
  62. ops = pci_get_emul_ops(emul);
  63. if (!ops || !ops->read_config)
  64. return -ENOSYS;
  65. return ops->read_config(emul, offset, valuep, size);
  66. }
  67. static int sandbox_pci_probe(struct udevice *dev)
  68. {
  69. struct sandbox_pci_priv *priv = dev_get_priv(dev);
  70. const fdt32_t *cell;
  71. u8 pdev, pfn, devfn;
  72. int len;
  73. cell = ofnode_get_property(dev_ofnode(dev), "sandbox,dev-info", &len);
  74. if (!cell)
  75. return 0;
  76. if ((len % FDT_DEV_INFO_SIZE) == 0) {
  77. int num = len / FDT_DEV_INFO_SIZE;
  78. int i;
  79. for (i = 0; i < num; i++) {
  80. debug("dev info #%d: %02x %02x %04x %04x\n", i,
  81. fdt32_to_cpu(cell[0]), fdt32_to_cpu(cell[1]),
  82. fdt32_to_cpu(cell[2]), fdt32_to_cpu(cell[3]));
  83. pdev = fdt32_to_cpu(cell[0]);
  84. pfn = fdt32_to_cpu(cell[1]);
  85. if (pdev > 31 || pfn > 7)
  86. continue;
  87. devfn = SANDBOX_PCI_DEVFN(pdev, pfn);
  88. priv->vendev[devfn].vendor = fdt32_to_cpu(cell[2]);
  89. priv->vendev[devfn].device = fdt32_to_cpu(cell[3]);
  90. cell += FDT_DEV_INFO_CELLS;
  91. }
  92. }
  93. return 0;
  94. }
  95. static const struct dm_pci_ops sandbox_pci_ops = {
  96. .read_config = sandbox_pci_read_config,
  97. .write_config = sandbox_pci_write_config,
  98. };
  99. static const struct udevice_id sandbox_pci_ids[] = {
  100. { .compatible = "sandbox,pci" },
  101. { }
  102. };
  103. U_BOOT_DRIVER(pci_sandbox) = {
  104. .name = "pci_sandbox",
  105. .id = UCLASS_PCI,
  106. .of_match = sandbox_pci_ids,
  107. .ops = &sandbox_pci_ops,
  108. .probe = sandbox_pci_probe,
  109. .priv_auto_alloc_size = sizeof(struct sandbox_pci_priv),
  110. /* Attach an emulator if we can */
  111. .child_post_bind = dm_scan_fdt_dev,
  112. .per_child_platdata_auto_alloc_size =
  113. sizeof(struct pci_child_platdata),
  114. };