pcie_phytium.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Phytium PCIE host driver
  4. *
  5. * Heavily based on drivers/pci/pcie_xilinx.c
  6. *
  7. * Copyright (C) 2019
  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. /**
  15. * struct phytium_pcie - phytium PCIe controller state
  16. * @cfg_base: The base address of memory mapped configuration space
  17. */
  18. struct phytium_pcie {
  19. void *cfg_base;
  20. };
  21. /*
  22. * phytium_pci_skip_dev()
  23. * @parent: Identifies the PCIe device to access
  24. *
  25. * Checks whether the parent of the PCIe device is bridge
  26. *
  27. * Return: true if it is bridge, else false.
  28. */
  29. static int phytium_pci_skip_dev(pci_dev_t parent)
  30. {
  31. unsigned char pos, id;
  32. unsigned long addr = 0x40000000;
  33. unsigned short capreg;
  34. unsigned char port_type;
  35. addr += PCIE_ECAM_OFFSET(PCI_BUS(parent), PCI_DEV(parent), PCI_FUNC(parent), 0);
  36. pos = 0x34;
  37. while (1) {
  38. pos = readb(addr + pos);
  39. if (pos < 0x40)
  40. break;
  41. pos &= ~3;
  42. id = readb(addr + pos);
  43. if (id == 0xff)
  44. break;
  45. if (id == 0x10) {
  46. capreg = readw(addr + pos + 2);
  47. port_type = (capreg >> 4) & 0xf;
  48. if (port_type == 0x6 || port_type == 0x4)
  49. return 1;
  50. else
  51. return 0;
  52. }
  53. pos += 1;
  54. }
  55. return 0;
  56. }
  57. /**
  58. * pci_phytium_conf_address() - Calculate the address of a config access
  59. * @bus: Pointer to the PCI bus
  60. * @bdf: Identifies the PCIe device to access
  61. * @offset: The offset into the device's configuration space
  62. * @paddress: Pointer to the pointer to write the calculates address to
  63. *
  64. * Calculates the address that should be accessed to perform a PCIe
  65. * configuration space access for a given device identified by the PCIe
  66. * controller device @pcie and the bus, device & function numbers in @bdf. If
  67. * access to the device is not valid then the function will return an error
  68. * code. Otherwise the address to access will be written to the pointer pointed
  69. * to by @paddress.
  70. */
  71. static int pci_phytium_conf_address(const struct udevice *bus, pci_dev_t bdf,
  72. uint offset, void **paddress)
  73. {
  74. struct phytium_pcie *pcie = dev_get_priv(bus);
  75. void *addr;
  76. pci_dev_t bdf_parent;
  77. unsigned int bus_no = PCI_BUS(bdf);
  78. unsigned int dev_no = PCI_DEV(bdf);
  79. bdf_parent = PCI_BDF((bus_no - 1), 0, 0);
  80. addr = pcie->cfg_base;
  81. addr += PCIE_ECAM_OFFSET(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), 0);
  82. if (bus_no > 0 && dev_no > 0) {
  83. if ((readb(addr + PCI_HEADER_TYPE) & 0x7f) !=
  84. PCI_HEADER_TYPE_BRIDGE)
  85. return -ENODEV;
  86. if (phytium_pci_skip_dev(bdf_parent))
  87. return -ENODEV;
  88. }
  89. addr += offset;
  90. *paddress = addr;
  91. return 0;
  92. }
  93. /**
  94. * pci_phytium_read_config() - Read from configuration space
  95. * @bus: Pointer to the PCI bus
  96. * @bdf: Identifies the PCIe device to access
  97. * @offset: The offset into the device's configuration space
  98. * @valuep: A pointer at which to store the read value
  99. * @size: Indicates the size of access to perform
  100. *
  101. * Read a value of size @size from offset @offset within the configuration
  102. * space of the device identified by the bus, device & function numbers in @bdf
  103. * on the PCI bus @bus.
  104. */
  105. static int pci_phytium_read_config(const struct udevice *bus, pci_dev_t bdf,
  106. uint offset, ulong *valuep,
  107. enum pci_size_t size)
  108. {
  109. return pci_generic_mmap_read_config(bus, pci_phytium_conf_address,
  110. bdf, offset, valuep, size);
  111. }
  112. /**
  113. * pci_phytium_write_config() - Write to configuration space
  114. * @bus: Pointer to the PCI bus
  115. * @bdf: Identifies the PCIe device to access
  116. * @offset: The offset into the device's configuration space
  117. * @value: The value to write
  118. * @size: Indicates the size of access to perform
  119. *
  120. * Write the value @value of size @size from offset @offset within the
  121. * configuration space of the device identified by the bus, device & function
  122. * numbers in @bdf on the PCI bus @bus.
  123. */
  124. static int pci_phytium_write_config(struct udevice *bus, pci_dev_t bdf,
  125. uint offset, ulong value,
  126. enum pci_size_t size)
  127. {
  128. return pci_generic_mmap_write_config(bus, pci_phytium_conf_address,
  129. bdf, offset, value, size);
  130. }
  131. /**
  132. * pci_phytium_of_to_plat() - Translate from DT to device state
  133. * @dev: A pointer to the device being operated on
  134. *
  135. * Translate relevant data from the device tree pertaining to device @dev into
  136. * state that the driver will later make use of. This state is stored in the
  137. * device's private data structure.
  138. *
  139. * Return: 0 on success, else -EINVAL
  140. */
  141. static int pci_phytium_of_to_plat(struct udevice *dev)
  142. {
  143. struct phytium_pcie *pcie = dev_get_priv(dev);
  144. struct fdt_resource reg_res;
  145. DECLARE_GLOBAL_DATA_PTR;
  146. int err;
  147. err = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), "reg",
  148. 0, &reg_res);
  149. if (err < 0) {
  150. pr_err("\"reg\" resource not found\n");
  151. return err;
  152. }
  153. pcie->cfg_base = map_physmem(reg_res.start,
  154. fdt_resource_size(&reg_res),
  155. MAP_NOCACHE);
  156. return 0;
  157. }
  158. static const struct dm_pci_ops pci_phytium_ops = {
  159. .read_config = pci_phytium_read_config,
  160. .write_config = pci_phytium_write_config,
  161. };
  162. static const struct udevice_id pci_phytium_ids[] = {
  163. { .compatible = "phytium,pcie-host-1.0" },
  164. { }
  165. };
  166. U_BOOT_DRIVER(pci_phytium) = {
  167. .name = "pci_phytium",
  168. .id = UCLASS_PCI,
  169. .of_match = pci_phytium_ids,
  170. .ops = &pci_phytium_ops,
  171. .of_to_plat = pci_phytium_of_to_plat,
  172. .priv_auto = sizeof(struct phytium_pcie),
  173. };