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