pcie_xilinx.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. * @udev: Pointer to the PCI bus
  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 udevice *udev, pci_dev_t bdf,
  52. uint offset, void **paddress)
  53. {
  54. struct xilinx_pcie *pcie = dev_get_priv(udev);
  55. unsigned int bus = PCI_BUS(bdf);
  56. unsigned int dev = PCI_DEV(bdf);
  57. unsigned int func = PCI_FUNC(bdf);
  58. void *addr;
  59. if ((bus > 0) && !pcie_xilinx_link_up(pcie))
  60. return -ENODEV;
  61. /*
  62. * Busses 0 (host-PCIe bridge) & 1 (its immediate child) are
  63. * limited to a single device each.
  64. */
  65. if ((bus < 2) && (dev > 0))
  66. return -ENODEV;
  67. addr = pcie->cfg_base;
  68. addr += bus << 20;
  69. addr += dev << 15;
  70. addr += func << 12;
  71. addr += offset;
  72. *paddress = addr;
  73. return 0;
  74. }
  75. /**
  76. * pcie_xilinx_read_config() - Read from configuration space
  77. * @bus: Pointer to the PCI bus
  78. * @bdf: Identifies the PCIe device to access
  79. * @offset: The offset into the device's configuration space
  80. * @valuep: A pointer at which to store the read value
  81. * @size: Indicates the size of access to perform
  82. *
  83. * Read a value of size @size from offset @offset within the configuration
  84. * space of the device identified by the bus, device & function numbers in @bdf
  85. * on the PCI bus @bus.
  86. *
  87. * Return: 0 on success, else -ENODEV or -EINVAL
  88. */
  89. static int pcie_xilinx_read_config(struct udevice *bus, pci_dev_t bdf,
  90. uint offset, ulong *valuep,
  91. enum pci_size_t size)
  92. {
  93. return pci_generic_mmap_read_config(bus, pcie_xilinx_config_address,
  94. bdf, offset, valuep, size);
  95. }
  96. /**
  97. * pcie_xilinx_write_config() - Write to configuration space
  98. * @bus: Pointer to the PCI bus
  99. * @bdf: Identifies the PCIe device to access
  100. * @offset: The offset into the device's configuration space
  101. * @value: The value to write
  102. * @size: Indicates the size of access to perform
  103. *
  104. * Write the value @value of size @size from offset @offset within the
  105. * configuration space of the device identified by the bus, device & function
  106. * numbers in @bdf on the PCI bus @bus.
  107. *
  108. * Return: 0 on success, else -ENODEV or -EINVAL
  109. */
  110. static int pcie_xilinx_write_config(struct udevice *bus, pci_dev_t bdf,
  111. uint offset, ulong value,
  112. enum pci_size_t size)
  113. {
  114. return pci_generic_mmap_write_config(bus, pcie_xilinx_config_address,
  115. bdf, offset, value, size);
  116. }
  117. /**
  118. * pcie_xilinx_ofdata_to_platdata() - Translate from DT to device state
  119. * @dev: A pointer to the device being operated on
  120. *
  121. * Translate relevant data from the device tree pertaining to device @dev into
  122. * state that the driver will later make use of. This state is stored in the
  123. * device's private data structure.
  124. *
  125. * Return: 0 on success, else -EINVAL
  126. */
  127. static int pcie_xilinx_ofdata_to_platdata(struct udevice *dev)
  128. {
  129. struct xilinx_pcie *pcie = dev_get_priv(dev);
  130. struct fdt_resource reg_res;
  131. DECLARE_GLOBAL_DATA_PTR;
  132. int err;
  133. err = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), "reg",
  134. 0, &reg_res);
  135. if (err < 0) {
  136. pr_err("\"reg\" resource not found\n");
  137. return err;
  138. }
  139. pcie->cfg_base = map_physmem(reg_res.start,
  140. fdt_resource_size(&reg_res),
  141. MAP_NOCACHE);
  142. return 0;
  143. }
  144. static const struct dm_pci_ops pcie_xilinx_ops = {
  145. .read_config = pcie_xilinx_read_config,
  146. .write_config = pcie_xilinx_write_config,
  147. };
  148. static const struct udevice_id pcie_xilinx_ids[] = {
  149. { .compatible = "xlnx,axi-pcie-host-1.00.a" },
  150. { }
  151. };
  152. U_BOOT_DRIVER(pcie_xilinx) = {
  153. .name = "pcie_xilinx",
  154. .id = UCLASS_PCI,
  155. .of_match = pcie_xilinx_ids,
  156. .ops = &pcie_xilinx_ops,
  157. .ofdata_to_platdata = pcie_xilinx_ofdata_to_platdata,
  158. .priv_auto_alloc_size = sizeof(struct xilinx_pcie),
  159. };