pci-emul-uclass.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <libfdt.h>
  11. #include <pci.h>
  12. #include <dm/lists.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. struct sandbox_pci_priv {
  15. int dev_count;
  16. };
  17. int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn,
  18. struct udevice **emulp)
  19. {
  20. struct udevice *dev;
  21. int ret;
  22. ret = pci_bus_find_devfn(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. ret = device_find_first_child(dev, emulp);
  29. if (ret)
  30. return ret;
  31. return *emulp ? 0 : -ENODEV;
  32. }
  33. static int sandbox_pci_emul_post_probe(struct udevice *dev)
  34. {
  35. struct sandbox_pci_priv *priv = dev->uclass->priv;
  36. priv->dev_count++;
  37. sandbox_set_enable_pci_map(true);
  38. return 0;
  39. }
  40. static int sandbox_pci_emul_pre_remove(struct udevice *dev)
  41. {
  42. struct sandbox_pci_priv *priv = dev->uclass->priv;
  43. priv->dev_count--;
  44. sandbox_set_enable_pci_map(priv->dev_count > 0);
  45. return 0;
  46. }
  47. UCLASS_DRIVER(pci_emul) = {
  48. .id = UCLASS_PCI_EMUL,
  49. .name = "pci_emul",
  50. .post_probe = sandbox_pci_emul_post_probe,
  51. .pre_remove = sandbox_pci_emul_pre_remove,
  52. .priv_auto_alloc_size = sizeof(struct sandbox_pci_priv),
  53. };