pci_sandbox.c 3.2 KB

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