pcie_ecam_generic.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generic PCIE host provided by e.g. QEMU
  4. *
  5. * Heavily based on drivers/pci/pcie_xilinx.c
  6. *
  7. * Copyright (C) 2016 Imagination Technologies
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <pci.h>
  12. #include <asm/global_data.h>
  13. #include <asm/io.h>
  14. #define TYPE_PCI 0x1
  15. /**
  16. * struct generic_ecam_pcie - generic_ecam PCIe controller state
  17. * @cfg_base: The base address of memory mapped configuration space
  18. */
  19. struct generic_ecam_pcie {
  20. void *cfg_base;
  21. pci_size_t size;
  22. int first_busno;
  23. };
  24. /**
  25. * pci_generic_ecam_conf_address() - Calculate the address of a config access
  26. * @bus: Pointer to the PCI bus
  27. * @bdf: Identifies the PCIe device to access
  28. * @offset: The offset into the device's configuration space
  29. * @paddress: Pointer to the pointer to write the calculates address to
  30. *
  31. * Calculates the address that should be accessed to perform a PCIe
  32. * configuration space access for a given device identified by the PCIe
  33. * controller device @pcie and the bus, device & function numbers in @bdf. If
  34. * access to the device is not valid then the function will return an error
  35. * code. Otherwise the address to access will be written to the pointer pointed
  36. * to by @paddress.
  37. */
  38. static int pci_generic_ecam_conf_address(const struct udevice *bus,
  39. pci_dev_t bdf, uint offset,
  40. void **paddress)
  41. {
  42. struct generic_ecam_pcie *pcie = dev_get_priv(bus);
  43. void *addr;
  44. addr = pcie->cfg_base;
  45. if (dev_get_driver_data(bus) == TYPE_PCI) {
  46. addr += ((PCI_BUS(bdf) - pcie->first_busno) << 16) |
  47. (PCI_DEV(bdf) << 11) | (PCI_FUNC(bdf) << 8) | offset;
  48. } else {
  49. addr += PCIE_ECAM_OFFSET(PCI_BUS(bdf) - pcie->first_busno,
  50. PCI_DEV(bdf), PCI_FUNC(bdf), offset);
  51. }
  52. *paddress = addr;
  53. return 0;
  54. }
  55. static bool pci_generic_ecam_addr_valid(const struct udevice *bus,
  56. pci_dev_t bdf)
  57. {
  58. struct generic_ecam_pcie *pcie = dev_get_priv(bus);
  59. int num_buses = DIV_ROUND_UP(pcie->size, 1 << 16);
  60. return (PCI_BUS(bdf) >= pcie->first_busno &&
  61. PCI_BUS(bdf) < pcie->first_busno + num_buses);
  62. }
  63. /**
  64. * pci_generic_ecam_read_config() - Read from configuration space
  65. * @bus: Pointer to the PCI bus
  66. * @bdf: Identifies the PCIe device to access
  67. * @offset: The offset into the device's configuration space
  68. * @valuep: A pointer at which to store the read value
  69. * @size: Indicates the size of access to perform
  70. *
  71. * Read a value of size @size from offset @offset within the configuration
  72. * space of the device identified by the bus, device & function numbers in @bdf
  73. * on the PCI bus @bus.
  74. */
  75. static int pci_generic_ecam_read_config(const struct udevice *bus,
  76. pci_dev_t bdf, uint offset,
  77. ulong *valuep, enum pci_size_t size)
  78. {
  79. if (!pci_generic_ecam_addr_valid(bus, bdf)) {
  80. *valuep = pci_get_ff(size);
  81. return 0;
  82. }
  83. return pci_generic_mmap_read_config(bus, pci_generic_ecam_conf_address,
  84. bdf, offset, valuep, size);
  85. }
  86. /**
  87. * pci_generic_ecam_write_config() - Write to configuration space
  88. * @bus: Pointer to the PCI bus
  89. * @bdf: Identifies the PCIe device to access
  90. * @offset: The offset into the device's configuration space
  91. * @value: The value to write
  92. * @size: Indicates the size of access to perform
  93. *
  94. * Write the value @value of size @size from offset @offset within the
  95. * configuration space of the device identified by the bus, device & function
  96. * numbers in @bdf on the PCI bus @bus.
  97. */
  98. static int pci_generic_ecam_write_config(struct udevice *bus, pci_dev_t bdf,
  99. uint offset, ulong value,
  100. enum pci_size_t size)
  101. {
  102. if (!pci_generic_ecam_addr_valid(bus, bdf))
  103. return 0;
  104. return pci_generic_mmap_write_config(bus, pci_generic_ecam_conf_address,
  105. bdf, offset, value, size);
  106. }
  107. /**
  108. * pci_generic_ecam_of_to_plat() - Translate from DT to device state
  109. * @dev: A pointer to the device being operated on
  110. *
  111. * Translate relevant data from the device tree pertaining to device @dev into
  112. * state that the driver will later make use of. This state is stored in the
  113. * device's private data structure.
  114. *
  115. * Return: 0 on success, else -EINVAL
  116. */
  117. static int pci_generic_ecam_of_to_plat(struct udevice *dev)
  118. {
  119. struct generic_ecam_pcie *pcie = dev_get_priv(dev);
  120. struct fdt_resource reg_res;
  121. DECLARE_GLOBAL_DATA_PTR;
  122. int err;
  123. err = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), "reg",
  124. 0, &reg_res);
  125. if (err < 0) {
  126. pr_err("\"reg\" resource not found\n");
  127. return err;
  128. }
  129. pcie->size = fdt_resource_size(&reg_res);
  130. pcie->cfg_base = map_physmem(reg_res.start, pcie->size, MAP_NOCACHE);
  131. return 0;
  132. }
  133. static int pci_generic_ecam_probe(struct udevice *dev)
  134. {
  135. struct generic_ecam_pcie *pcie = dev_get_priv(dev);
  136. pcie->first_busno = dev_seq(dev);
  137. return 0;
  138. }
  139. static const struct dm_pci_ops pci_generic_ecam_ops = {
  140. .read_config = pci_generic_ecam_read_config,
  141. .write_config = pci_generic_ecam_write_config,
  142. };
  143. static const struct udevice_id pci_generic_ecam_ids[] = {
  144. { .compatible = "pci-host-ecam-generic" /* PCI-E */ },
  145. { .compatible = "pci-host-cam-generic", .data = TYPE_PCI },
  146. { }
  147. };
  148. U_BOOT_DRIVER(pci_generic_ecam) = {
  149. .name = "pci_generic_ecam",
  150. .id = UCLASS_PCI,
  151. .of_match = pci_generic_ecam_ids,
  152. .ops = &pci_generic_ecam_ops,
  153. .probe = pci_generic_ecam_probe,
  154. .of_to_plat = pci_generic_ecam_of_to_plat,
  155. .priv_auto = sizeof(struct generic_ecam_pcie),
  156. };