pcie_xilinx.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xilinx AXI Bridge for PCI Express Driver
  4. *
  5. * Copyright (C) 2016 Imagination Technologies
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <pci.h>
  10. #include <asm/global_data.h>
  11. #include <linux/bitops.h>
  12. #include <asm/io.h>
  13. /**
  14. * struct xilinx_pcie - Xilinx PCIe controller state
  15. * @cfg_base: The base address of memory mapped configuration space
  16. */
  17. struct xilinx_pcie {
  18. void *cfg_base;
  19. };
  20. /* Register definitions */
  21. #define XILINX_PCIE_REG_PSCR 0x144
  22. #define XILINX_PCIE_REG_PSCR_LNKUP BIT(11)
  23. /**
  24. * pcie_xilinx_link_up() - Check whether the PCIe link is up
  25. * @pcie: Pointer to the PCI controller state
  26. *
  27. * Checks whether the PCIe link for the given device is up or down.
  28. *
  29. * Return: true if the link is up, else false
  30. */
  31. static bool pcie_xilinx_link_up(struct xilinx_pcie *pcie)
  32. {
  33. uint32_t pscr = __raw_readl(pcie->cfg_base + XILINX_PCIE_REG_PSCR);
  34. return pscr & XILINX_PCIE_REG_PSCR_LNKUP;
  35. }
  36. /**
  37. * pcie_xilinx_config_address() - Calculate the address of a config access
  38. * @udev: Pointer to the PCI bus
  39. * @bdf: Identifies the PCIe device to access
  40. * @offset: The offset into the device's configuration space
  41. * @paddress: Pointer to the pointer to write the calculates address to
  42. *
  43. * Calculates the address that should be accessed to perform a PCIe
  44. * configuration space access for a given device identified by the PCIe
  45. * controller device @pcie and the bus, device & function numbers in @bdf. If
  46. * access to the device is not valid then the function will return an error
  47. * code. Otherwise the address to access will be written to the pointer pointed
  48. * to by @paddress.
  49. *
  50. * Return: 0 on success, else -ENODEV
  51. */
  52. static int pcie_xilinx_config_address(const struct udevice *udev, pci_dev_t bdf,
  53. uint offset, void **paddress)
  54. {
  55. struct xilinx_pcie *pcie = dev_get_priv(udev);
  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 += PCIE_ECAM_OFFSET(bus, dev, func, offset);
  70. *paddress = addr;
  71. return 0;
  72. }
  73. /**
  74. * pcie_xilinx_read_config() - Read from configuration space
  75. * @bus: Pointer to the PCI bus
  76. * @bdf: Identifies the PCIe device to access
  77. * @offset: The offset into the device's configuration space
  78. * @valuep: A pointer at which to store the read value
  79. * @size: Indicates the size of access to perform
  80. *
  81. * Read a value of size @size from offset @offset within the configuration
  82. * space of the device identified by the bus, device & function numbers in @bdf
  83. * on the PCI bus @bus.
  84. *
  85. * Return: 0 on success, else -ENODEV or -EINVAL
  86. */
  87. static int pcie_xilinx_read_config(const struct udevice *bus, pci_dev_t bdf,
  88. uint offset, ulong *valuep,
  89. enum pci_size_t size)
  90. {
  91. return pci_generic_mmap_read_config(bus, pcie_xilinx_config_address,
  92. bdf, offset, valuep, size);
  93. }
  94. /**
  95. * pcie_xilinx_write_config() - Write to configuration space
  96. * @bus: Pointer to the PCI bus
  97. * @bdf: Identifies the PCIe device to access
  98. * @offset: The offset into the device's configuration space
  99. * @value: The value to write
  100. * @size: Indicates the size of access to perform
  101. *
  102. * Write the value @value of size @size from offset @offset within the
  103. * configuration space of the device identified by the bus, device & function
  104. * numbers in @bdf on the PCI bus @bus.
  105. *
  106. * Return: 0 on success, else -ENODEV or -EINVAL
  107. */
  108. static int pcie_xilinx_write_config(struct udevice *bus, pci_dev_t bdf,
  109. uint offset, ulong value,
  110. enum pci_size_t size)
  111. {
  112. return pci_generic_mmap_write_config(bus, pcie_xilinx_config_address,
  113. bdf, offset, value, size);
  114. }
  115. /**
  116. * pcie_xilinx_of_to_plat() - Translate from DT to device state
  117. * @dev: A pointer to the device being operated on
  118. *
  119. * Translate relevant data from the device tree pertaining to device @dev into
  120. * state that the driver will later make use of. This state is stored in the
  121. * device's private data structure.
  122. *
  123. * Return: 0 on success, else -EINVAL
  124. */
  125. static int pcie_xilinx_of_to_plat(struct udevice *dev)
  126. {
  127. struct xilinx_pcie *pcie = dev_get_priv(dev);
  128. struct fdt_resource reg_res;
  129. DECLARE_GLOBAL_DATA_PTR;
  130. int err;
  131. err = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), "reg",
  132. 0, &reg_res);
  133. if (err < 0) {
  134. pr_err("\"reg\" resource not found\n");
  135. return err;
  136. }
  137. pcie->cfg_base = map_physmem(reg_res.start,
  138. fdt_resource_size(&reg_res),
  139. MAP_NOCACHE);
  140. return 0;
  141. }
  142. static const struct dm_pci_ops pcie_xilinx_ops = {
  143. .read_config = pcie_xilinx_read_config,
  144. .write_config = pcie_xilinx_write_config,
  145. };
  146. static const struct udevice_id pcie_xilinx_ids[] = {
  147. { .compatible = "xlnx,axi-pcie-host-1.00.a" },
  148. { }
  149. };
  150. U_BOOT_DRIVER(pcie_xilinx) = {
  151. .name = "pcie_xilinx",
  152. .id = UCLASS_PCI,
  153. .of_match = pcie_xilinx_ids,
  154. .ops = &pcie_xilinx_ops,
  155. .of_to_plat = pcie_xilinx_of_to_plat,
  156. .priv_auto = sizeof(struct xilinx_pcie),
  157. };