Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Xilinx AXI Bridge for PCI Express Driver |
| 4 | * |
| 5 | * Copyright (C) 2016 Imagination Technologies |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <pci.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 10 | #include <linux/bitops.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 11 | #include <linux/printk.h> |
Mayuresh Chitale | e45286a | 2023-11-16 22:21:02 +0530 | [diff] [blame] | 12 | #include <linux/io.h> |
| 13 | #include <linux/err.h> |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * struct xilinx_pcie - Xilinx PCIe controller state |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 17 | * @cfg_base: The base address of memory mapped configuration space |
| 18 | */ |
| 19 | struct xilinx_pcie { |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 20 | void *cfg_base; |
Jiaxun Yang | 490c186 | 2024-05-17 19:14:49 +0100 | [diff] [blame] | 21 | pci_size_t size; |
| 22 | int first_busno; |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | /* Register definitions */ |
Jiaxun Yang | 490c186 | 2024-05-17 19:14:49 +0100 | [diff] [blame] | 26 | #define XILINX_PCIE_REG_BRIDGE_INFO 0x130 |
| 27 | #define XILINX_PCIE_REG_BRIDGE_INFO_ECAMSZ_SHIFT 16 |
| 28 | #define XILINX_PCIE_REG_BRIDGE_INFO_ECAMSZ_MASK (0x7 << 16) |
| 29 | #define XILINX_PCIE_REG_INT_MASK 0x13c |
| 30 | #define XILINX_PCIE_REG_PSCR 0x144 |
| 31 | #define XILINX_PCIE_REG_PSCR_LNKUP BIT(11) |
| 32 | #define XILINX_PCIE_REG_RPSC 0x148 |
| 33 | #define XILINX_PCIE_REG_RPSC_BEN BIT(0) |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 34 | /** |
| 35 | * pcie_xilinx_link_up() - Check whether the PCIe link is up |
| 36 | * @pcie: Pointer to the PCI controller state |
| 37 | * |
| 38 | * Checks whether the PCIe link for the given device is up or down. |
| 39 | * |
| 40 | * Return: true if the link is up, else false |
| 41 | */ |
| 42 | static bool pcie_xilinx_link_up(struct xilinx_pcie *pcie) |
| 43 | { |
| 44 | uint32_t pscr = __raw_readl(pcie->cfg_base + XILINX_PCIE_REG_PSCR); |
| 45 | |
| 46 | return pscr & XILINX_PCIE_REG_PSCR_LNKUP; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * pcie_xilinx_config_address() - Calculate the address of a config access |
Tuomas Tynkkynen | 6b18f2a | 2017-09-19 23:18:04 +0300 | [diff] [blame] | 51 | * @udev: Pointer to the PCI bus |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 52 | * @bdf: Identifies the PCIe device to access |
| 53 | * @offset: The offset into the device's configuration space |
| 54 | * @paddress: Pointer to the pointer to write the calculates address to |
| 55 | * |
| 56 | * Calculates the address that should be accessed to perform a PCIe |
| 57 | * configuration space access for a given device identified by the PCIe |
| 58 | * controller device @pcie and the bus, device & function numbers in @bdf. If |
| 59 | * access to the device is not valid then the function will return an error |
| 60 | * code. Otherwise the address to access will be written to the pointer pointed |
| 61 | * to by @paddress. |
| 62 | * |
| 63 | * Return: 0 on success, else -ENODEV |
| 64 | */ |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 65 | static int pcie_xilinx_config_address(const struct udevice *udev, pci_dev_t bdf, |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 66 | uint offset, void **paddress) |
| 67 | { |
Tuomas Tynkkynen | 6b18f2a | 2017-09-19 23:18:04 +0300 | [diff] [blame] | 68 | struct xilinx_pcie *pcie = dev_get_priv(udev); |
Jiaxun Yang | 490c186 | 2024-05-17 19:14:49 +0100 | [diff] [blame] | 69 | unsigned int bus = PCI_BUS(bdf) - pcie->first_busno; |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 70 | unsigned int dev = PCI_DEV(bdf); |
| 71 | unsigned int func = PCI_FUNC(bdf); |
Jiaxun Yang | 490c186 | 2024-05-17 19:14:49 +0100 | [diff] [blame] | 72 | int num_buses = DIV_ROUND_UP(pcie->size, 1 << 16); |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 73 | void *addr; |
| 74 | |
| 75 | if ((bus > 0) && !pcie_xilinx_link_up(pcie)) |
| 76 | return -ENODEV; |
| 77 | |
Jiaxun Yang | 490c186 | 2024-05-17 19:14:49 +0100 | [diff] [blame] | 78 | if (bus > num_buses) |
| 79 | return -ENODEV; |
| 80 | |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 81 | /* |
| 82 | * Busses 0 (host-PCIe bridge) & 1 (its immediate child) are |
| 83 | * limited to a single device each. |
| 84 | */ |
| 85 | if ((bus < 2) && (dev > 0)) |
| 86 | return -ENODEV; |
| 87 | |
| 88 | addr = pcie->cfg_base; |
Pali Rohár | 2376935 | 2021-11-03 01:01:05 +0100 | [diff] [blame] | 89 | addr += PCIE_ECAM_OFFSET(bus, dev, func, offset); |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 90 | *paddress = addr; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * pcie_xilinx_read_config() - Read from configuration space |
Tuomas Tynkkynen | f0e8d23 | 2017-09-01 17:25:58 +0300 | [diff] [blame] | 97 | * @bus: Pointer to the PCI bus |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 98 | * @bdf: Identifies the PCIe device to access |
| 99 | * @offset: The offset into the device's configuration space |
| 100 | * @valuep: A pointer at which to store the read value |
| 101 | * @size: Indicates the size of access to perform |
| 102 | * |
| 103 | * Read a value of size @size from offset @offset within the configuration |
| 104 | * space of the device identified by the bus, device & function numbers in @bdf |
| 105 | * on the PCI bus @bus. |
| 106 | * |
| 107 | * Return: 0 on success, else -ENODEV or -EINVAL |
| 108 | */ |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 109 | static int pcie_xilinx_read_config(const struct udevice *bus, pci_dev_t bdf, |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 110 | uint offset, ulong *valuep, |
| 111 | enum pci_size_t size) |
| 112 | { |
Tuomas Tynkkynen | 6b18f2a | 2017-09-19 23:18:04 +0300 | [diff] [blame] | 113 | return pci_generic_mmap_read_config(bus, pcie_xilinx_config_address, |
| 114 | bdf, offset, valuep, size); |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * pcie_xilinx_write_config() - Write to configuration space |
Tuomas Tynkkynen | f0e8d23 | 2017-09-01 17:25:58 +0300 | [diff] [blame] | 119 | * @bus: Pointer to the PCI bus |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 120 | * @bdf: Identifies the PCIe device to access |
| 121 | * @offset: The offset into the device's configuration space |
| 122 | * @value: The value to write |
| 123 | * @size: Indicates the size of access to perform |
| 124 | * |
| 125 | * Write the value @value of size @size from offset @offset within the |
| 126 | * configuration space of the device identified by the bus, device & function |
| 127 | * numbers in @bdf on the PCI bus @bus. |
| 128 | * |
| 129 | * Return: 0 on success, else -ENODEV or -EINVAL |
| 130 | */ |
| 131 | static int pcie_xilinx_write_config(struct udevice *bus, pci_dev_t bdf, |
| 132 | uint offset, ulong value, |
| 133 | enum pci_size_t size) |
| 134 | { |
Tuomas Tynkkynen | 6b18f2a | 2017-09-19 23:18:04 +0300 | [diff] [blame] | 135 | return pci_generic_mmap_write_config(bus, pcie_xilinx_config_address, |
| 136 | bdf, offset, value, size); |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 140 | * pcie_xilinx_of_to_plat() - Translate from DT to device state |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 141 | * @dev: A pointer to the device being operated on |
| 142 | * |
| 143 | * Translate relevant data from the device tree pertaining to device @dev into |
| 144 | * state that the driver will later make use of. This state is stored in the |
| 145 | * device's private data structure. |
| 146 | * |
| 147 | * Return: 0 on success, else -EINVAL |
| 148 | */ |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 149 | static int pcie_xilinx_of_to_plat(struct udevice *dev) |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 150 | { |
| 151 | struct xilinx_pcie *pcie = dev_get_priv(dev); |
Mayuresh Chitale | e45286a | 2023-11-16 22:21:02 +0530 | [diff] [blame] | 152 | fdt_addr_t addr; |
| 153 | fdt_size_t size; |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 154 | |
Mayuresh Chitale | e45286a | 2023-11-16 22:21:02 +0530 | [diff] [blame] | 155 | addr = dev_read_addr_size(dev, &size); |
| 156 | if (addr == FDT_ADDR_T_NONE) |
| 157 | return -EINVAL; |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 158 | |
Jiaxun Yang | 490c186 | 2024-05-17 19:14:49 +0100 | [diff] [blame] | 159 | pcie->cfg_base = map_physmem(addr, size, MAP_NOCACHE); |
| 160 | if (!pcie->cfg_base) |
| 161 | return -ENOMEM; |
| 162 | pcie->size = size; |
| 163 | return 0; |
| 164 | } |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 165 | |
Jiaxun Yang | 490c186 | 2024-05-17 19:14:49 +0100 | [diff] [blame] | 166 | static int pci_xilinx_probe(struct udevice *dev) |
| 167 | { |
| 168 | struct xilinx_pcie *pcie = dev_get_priv(dev); |
| 169 | u32 rpsc; |
| 170 | int num_buses = DIV_ROUND_UP(pcie->size, 1 << 16); |
| 171 | |
| 172 | pcie->first_busno = dev_seq(dev); |
| 173 | |
| 174 | /* Disable all interrupts */ |
| 175 | writel(0, pcie->cfg_base + XILINX_PCIE_REG_INT_MASK); |
| 176 | |
| 177 | /* Enable the bridge */ |
| 178 | rpsc = readl(pcie->cfg_base + XILINX_PCIE_REG_RPSC); |
Mayuresh Chitale | 46944e1 | 2023-11-16 22:21:03 +0530 | [diff] [blame] | 179 | rpsc |= XILINX_PCIE_REG_RPSC_BEN; |
Jiaxun Yang | 490c186 | 2024-05-17 19:14:49 +0100 | [diff] [blame] | 180 | writel(rpsc, pcie->cfg_base + XILINX_PCIE_REG_RPSC); |
| 181 | |
| 182 | /* Enable access to all possible subordinate buses */ |
| 183 | writel((0 << 0) | (1 << 8) | (num_buses << 16), |
| 184 | pcie->cfg_base + PCI_PRIMARY_BUS); |
Mayuresh Chitale | 46944e1 | 2023-11-16 22:21:03 +0530 | [diff] [blame] | 185 | |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static const struct dm_pci_ops pcie_xilinx_ops = { |
| 190 | .read_config = pcie_xilinx_read_config, |
| 191 | .write_config = pcie_xilinx_write_config, |
| 192 | }; |
| 193 | |
| 194 | static const struct udevice_id pcie_xilinx_ids[] = { |
| 195 | { .compatible = "xlnx,axi-pcie-host-1.00.a" }, |
| 196 | { } |
| 197 | }; |
| 198 | |
| 199 | U_BOOT_DRIVER(pcie_xilinx) = { |
| 200 | .name = "pcie_xilinx", |
| 201 | .id = UCLASS_PCI, |
| 202 | .of_match = pcie_xilinx_ids, |
| 203 | .ops = &pcie_xilinx_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 204 | .of_to_plat = pcie_xilinx_of_to_plat, |
Jiaxun Yang | 490c186 | 2024-05-17 19:14:49 +0100 | [diff] [blame] | 205 | .probe = pci_xilinx_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 206 | .priv_auto = sizeof(struct xilinx_pcie), |
Paul Burton | c893f21 | 2016-09-08 07:47:31 +0100 | [diff] [blame] | 207 | }; |