pci-emul-uclass.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <linux/libfdt.h>
  10. #include <pci.h>
  11. #include <dm/lists.h>
  12. struct sandbox_pci_emul_priv {
  13. int dev_count;
  14. };
  15. int sandbox_pci_get_emul(const struct udevice *bus, pci_dev_t find_devfn,
  16. struct udevice **containerp, struct udevice **emulp)
  17. {
  18. struct pci_emul_uc_priv *upriv;
  19. struct udevice *dev;
  20. int ret;
  21. *containerp = NULL;
  22. ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(find_devfn), &dev);
  23. if (ret) {
  24. debug("%s: Could not find emulator for dev %x\n", __func__,
  25. find_devfn);
  26. return ret;
  27. }
  28. *containerp = dev;
  29. ret = uclass_get_device_by_phandle(UCLASS_PCI_EMUL, dev, "sandbox,emul",
  30. emulp);
  31. if (!ret) {
  32. upriv = dev_get_uclass_priv(*emulp);
  33. upriv->client = dev;
  34. } else if (device_get_uclass_id(dev) != UCLASS_PCI_GENERIC) {
  35. /*
  36. * See commit 4345998ae9df,
  37. * "pci: sandbox: Support dynamically binding device driver"
  38. */
  39. *emulp = dev;
  40. }
  41. return 0;
  42. }
  43. int sandbox_pci_get_client(struct udevice *emul, struct udevice **devp)
  44. {
  45. struct pci_emul_uc_priv *upriv = dev_get_uclass_priv(emul);
  46. if (!upriv->client)
  47. return -ENOENT;
  48. *devp = upriv->client;
  49. return 0;
  50. }
  51. uint sandbox_pci_read_bar(u32 barval, int type, uint size)
  52. {
  53. u32 result;
  54. result = barval;
  55. if (result == 0xffffffff) {
  56. if (type == PCI_BASE_ADDRESS_SPACE_IO) {
  57. result = (~(size - 1) &
  58. PCI_BASE_ADDRESS_IO_MASK) |
  59. PCI_BASE_ADDRESS_SPACE_IO;
  60. } else {
  61. result = (~(size - 1) &
  62. PCI_BASE_ADDRESS_MEM_MASK) |
  63. PCI_BASE_ADDRESS_MEM_TYPE_32;
  64. }
  65. }
  66. return result;
  67. }
  68. static int sandbox_pci_emul_post_probe(struct udevice *dev)
  69. {
  70. struct sandbox_pci_emul_priv *priv = dev->uclass->priv;
  71. priv->dev_count++;
  72. sandbox_set_enable_pci_map(true);
  73. return 0;
  74. }
  75. static int sandbox_pci_emul_pre_remove(struct udevice *dev)
  76. {
  77. struct sandbox_pci_emul_priv *priv = dev->uclass->priv;
  78. priv->dev_count--;
  79. sandbox_set_enable_pci_map(priv->dev_count > 0);
  80. return 0;
  81. }
  82. UCLASS_DRIVER(pci_emul) = {
  83. .id = UCLASS_PCI_EMUL,
  84. .name = "pci_emul",
  85. .post_probe = sandbox_pci_emul_post_probe,
  86. .pre_remove = sandbox_pci_emul_pre_remove,
  87. .priv_auto_alloc_size = sizeof(struct sandbox_pci_emul_priv),
  88. .per_device_auto_alloc_size = sizeof(struct pci_emul_uc_priv),
  89. };
  90. /*
  91. * This uclass is a child of the pci bus. Its platdata is not defined here so
  92. * is defined by its parent, UCLASS_PCI, which uses struct pci_child_platdata.
  93. * See per_child_platdata_auto_alloc_size in UCLASS_DRIVER(pci).
  94. */
  95. UCLASS_DRIVER(pci_emul_parent) = {
  96. .id = UCLASS_PCI_EMUL_PARENT,
  97. .name = "pci_emul_parent",
  98. .post_bind = dm_scan_fdt_dev,
  99. };
  100. static const struct udevice_id pci_emul_parent_ids[] = {
  101. { .compatible = "sandbox,pci-emul-parent" },
  102. { }
  103. };
  104. U_BOOT_DRIVER(pci_emul_parent_drv) = {
  105. .name = "pci_emul_parent_drv",
  106. .id = UCLASS_PCI_EMUL_PARENT,
  107. .of_match = pci_emul_parent_ids,
  108. };