pcie_xilinx.c 5.6 KB

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