pcie_xilinx.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Xilinx AXI Bridge for PCI Express Driver
  3. *
  4. * Copyright (C) 2016 Imagination Technologies
  5. *
  6. * SPDX-License-Identifier: GPL-2.0
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <pci.h>
  11. #include <asm/io.h>
  12. /**
  13. * struct xilinx_pcie - Xilinx PCIe controller state
  14. * @hose: The parent classes PCI controller state
  15. * @cfg_base: The base address of memory mapped configuration space
  16. */
  17. struct xilinx_pcie {
  18. struct pci_controller hose;
  19. void *cfg_base;
  20. };
  21. /* Register definitions */
  22. #define XILINX_PCIE_REG_PSCR 0x144
  23. #define XILINX_PCIE_REG_PSCR_LNKUP BIT(11)
  24. /**
  25. * pcie_xilinx_link_up() - Check whether the PCIe link is up
  26. * @pcie: Pointer to the PCI controller state
  27. *
  28. * Checks whether the PCIe link for the given device is up or down.
  29. *
  30. * Return: true if the link is up, else false
  31. */
  32. static bool pcie_xilinx_link_up(struct xilinx_pcie *pcie)
  33. {
  34. uint32_t pscr = __raw_readl(pcie->cfg_base + XILINX_PCIE_REG_PSCR);
  35. return pscr & XILINX_PCIE_REG_PSCR_LNKUP;
  36. }
  37. /**
  38. * pcie_xilinx_config_address() - Calculate the address of a config access
  39. * @pcie: Pointer to the PCI controller state
  40. * @bdf: Identifies the PCIe device to access
  41. * @offset: The offset into the device's configuration space
  42. * @paddress: Pointer to the pointer to write the calculates address to
  43. *
  44. * Calculates the address that should be accessed to perform a PCIe
  45. * configuration space access for a given device identified by the PCIe
  46. * controller device @pcie and the bus, device & function numbers in @bdf. If
  47. * access to the device is not valid then the function will return an error
  48. * code. Otherwise the address to access will be written to the pointer pointed
  49. * to by @paddress.
  50. *
  51. * Return: 0 on success, else -ENODEV
  52. */
  53. static int pcie_xilinx_config_address(struct xilinx_pcie *pcie, pci_dev_t bdf,
  54. uint offset, void **paddress)
  55. {
  56. unsigned int bus = PCI_BUS(bdf);
  57. unsigned int dev = PCI_DEV(bdf);
  58. unsigned int func = PCI_FUNC(bdf);
  59. void *addr;
  60. if ((bus > 0) && !pcie_xilinx_link_up(pcie))
  61. return -ENODEV;
  62. /*
  63. * Busses 0 (host-PCIe bridge) & 1 (its immediate child) are
  64. * limited to a single device each.
  65. */
  66. if ((bus < 2) && (dev > 0))
  67. return -ENODEV;
  68. addr = pcie->cfg_base;
  69. addr += bus << 20;
  70. addr += dev << 15;
  71. addr += func << 12;
  72. addr += offset;
  73. *paddress = addr;
  74. return 0;
  75. }
  76. /**
  77. * pcie_xilinx_read_config() - Read from configuration space
  78. * @pcie: Pointer to the PCI controller state
  79. * @bdf: Identifies the PCIe device to access
  80. * @offset: The offset into the device's configuration space
  81. * @valuep: A pointer at which to store the read value
  82. * @size: Indicates the size of access to perform
  83. *
  84. * Read a value of size @size from offset @offset within the configuration
  85. * space of the device identified by the bus, device & function numbers in @bdf
  86. * on the PCI bus @bus.
  87. *
  88. * Return: 0 on success, else -ENODEV or -EINVAL
  89. */
  90. static int pcie_xilinx_read_config(struct udevice *bus, pci_dev_t bdf,
  91. uint offset, ulong *valuep,
  92. enum pci_size_t size)
  93. {
  94. struct xilinx_pcie *pcie = dev_get_priv(bus);
  95. void *address;
  96. int err;
  97. err = pcie_xilinx_config_address(pcie, bdf, offset, &address);
  98. if (err < 0) {
  99. *valuep = pci_get_ff(size);
  100. return 0;
  101. }
  102. switch (size) {
  103. case PCI_SIZE_8:
  104. *valuep = __raw_readb(address);
  105. return 0;
  106. case PCI_SIZE_16:
  107. *valuep = __raw_readw(address);
  108. return 0;
  109. case PCI_SIZE_32:
  110. *valuep = __raw_readl(address);
  111. return 0;
  112. default:
  113. return -EINVAL;
  114. }
  115. }
  116. /**
  117. * pcie_xilinx_write_config() - Write to configuration space
  118. * @pcie: Pointer to the PCI controller state
  119. * @bdf: Identifies the PCIe device to access
  120. * @offset: The offset into the device's configuration space
  121. * @value: The value to write
  122. * @size: Indicates the size of access to perform
  123. *
  124. * Write the value @value of size @size from offset @offset within the
  125. * configuration space of the device identified by the bus, device & function
  126. * numbers in @bdf on the PCI bus @bus.
  127. *
  128. * Return: 0 on success, else -ENODEV or -EINVAL
  129. */
  130. static int pcie_xilinx_write_config(struct udevice *bus, pci_dev_t bdf,
  131. uint offset, ulong value,
  132. enum pci_size_t size)
  133. {
  134. struct xilinx_pcie *pcie = dev_get_priv(bus);
  135. void *address;
  136. int err;
  137. err = pcie_xilinx_config_address(pcie, bdf, offset, &address);
  138. if (err < 0)
  139. return 0;
  140. switch (size) {
  141. case PCI_SIZE_8:
  142. __raw_writeb(value, address);
  143. return 0;
  144. case PCI_SIZE_16:
  145. __raw_writew(value, address);
  146. return 0;
  147. case PCI_SIZE_32:
  148. __raw_writel(value, address);
  149. return 0;
  150. default:
  151. return -EINVAL;
  152. }
  153. }
  154. /**
  155. * pcie_xilinx_ofdata_to_platdata() - Translate from DT to device state
  156. * @dev: A pointer to the device being operated on
  157. *
  158. * Translate relevant data from the device tree pertaining to device @dev into
  159. * state that the driver will later make use of. This state is stored in the
  160. * device's private data structure.
  161. *
  162. * Return: 0 on success, else -EINVAL
  163. */
  164. static int pcie_xilinx_ofdata_to_platdata(struct udevice *dev)
  165. {
  166. struct xilinx_pcie *pcie = dev_get_priv(dev);
  167. struct fdt_resource reg_res;
  168. DECLARE_GLOBAL_DATA_PTR;
  169. int err;
  170. err = fdt_get_resource(gd->fdt_blob, dev->of_offset, "reg",
  171. 0, &reg_res);
  172. if (err < 0) {
  173. error("\"reg\" resource not found\n");
  174. return err;
  175. }
  176. pcie->cfg_base = map_physmem(reg_res.start,
  177. fdt_resource_size(&reg_res),
  178. MAP_NOCACHE);
  179. return 0;
  180. }
  181. static const struct dm_pci_ops pcie_xilinx_ops = {
  182. .read_config = pcie_xilinx_read_config,
  183. .write_config = pcie_xilinx_write_config,
  184. };
  185. static const struct udevice_id pcie_xilinx_ids[] = {
  186. { .compatible = "xlnx,axi-pcie-host-1.00.a" },
  187. { }
  188. };
  189. U_BOOT_DRIVER(pcie_xilinx) = {
  190. .name = "pcie_xilinx",
  191. .id = UCLASS_PCI,
  192. .of_match = pcie_xilinx_ids,
  193. .ops = &pcie_xilinx_ops,
  194. .ofdata_to_platdata = pcie_xilinx_ofdata_to_platdata,
  195. .priv_auto_alloc_size = sizeof(struct xilinx_pcie),
  196. };