pci_io.c 2.6 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. /*
  7. * IO space access commands.
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <dm.h>
  12. #include <asm/io.h>
  13. int pci_map_physmem(phys_addr_t paddr, unsigned long *lenp,
  14. struct udevice **devp, void **ptrp)
  15. {
  16. struct udevice *dev;
  17. int ret;
  18. *ptrp = 0;
  19. for (uclass_first_device(UCLASS_PCI_EMUL, &dev);
  20. dev;
  21. uclass_next_device(&dev)) {
  22. struct dm_pci_emul_ops *ops = pci_get_emul_ops(dev);
  23. if (!ops || !ops->map_physmem)
  24. continue;
  25. ret = (ops->map_physmem)(dev, paddr, lenp, ptrp);
  26. if (ret)
  27. continue;
  28. *devp = dev;
  29. return 0;
  30. }
  31. debug("%s: failed: addr=%pap\n", __func__, &paddr);
  32. return -ENOSYS;
  33. }
  34. int pci_unmap_physmem(const void *vaddr, unsigned long len,
  35. struct udevice *dev)
  36. {
  37. struct dm_pci_emul_ops *ops = pci_get_emul_ops(dev);
  38. if (!ops || !ops->unmap_physmem)
  39. return -ENOSYS;
  40. return (ops->unmap_physmem)(dev, vaddr, len);
  41. }
  42. static int pci_io_read(unsigned int addr, ulong *valuep, pci_size_t size)
  43. {
  44. struct udevice *dev;
  45. int ret;
  46. *valuep = pci_get_ff(size);
  47. for (uclass_first_device(UCLASS_PCI_EMUL, &dev);
  48. dev;
  49. uclass_next_device(&dev)) {
  50. struct dm_pci_emul_ops *ops = pci_get_emul_ops(dev);
  51. if (ops && ops->read_io) {
  52. ret = (ops->read_io)(dev, addr, valuep, size);
  53. if (!ret)
  54. return 0;
  55. }
  56. }
  57. debug("%s: failed: addr=%x\n", __func__, addr);
  58. return -ENOSYS;
  59. }
  60. static int pci_io_write(unsigned int addr, ulong value, pci_size_t size)
  61. {
  62. struct udevice *dev;
  63. int ret;
  64. for (uclass_first_device(UCLASS_PCI_EMUL, &dev);
  65. dev;
  66. uclass_next_device(&dev)) {
  67. struct dm_pci_emul_ops *ops = pci_get_emul_ops(dev);
  68. if (ops && ops->write_io) {
  69. ret = (ops->write_io)(dev, addr, value, size);
  70. if (!ret)
  71. return 0;
  72. }
  73. }
  74. debug("%s: failed: addr=%x, value=%lx\n", __func__, addr, value);
  75. return -ENOSYS;
  76. }
  77. int _inl(unsigned int addr)
  78. {
  79. unsigned long value;
  80. int ret;
  81. ret = pci_io_read(addr, &value, PCI_SIZE_32);
  82. return ret ? 0 : value;
  83. }
  84. int _inw(unsigned int addr)
  85. {
  86. unsigned long value;
  87. int ret;
  88. ret = pci_io_read(addr, &value, PCI_SIZE_16);
  89. return ret ? 0 : value;
  90. }
  91. int _inb(unsigned int addr)
  92. {
  93. unsigned long value;
  94. int ret;
  95. ret = pci_io_read(addr, &value, PCI_SIZE_8);
  96. return ret ? 0 : value;
  97. }
  98. void _outl(unsigned int value, unsigned int addr)
  99. {
  100. pci_io_write(addr, value, PCI_SIZE_32);
  101. }
  102. void _outw(unsigned int value, unsigned int addr)
  103. {
  104. pci_io_write(addr, value, PCI_SIZE_16);
  105. }
  106. void _outb(unsigned int value, unsigned int addr)
  107. {
  108. pci_io_write(addr, value, PCI_SIZE_8);
  109. }