blob: 63058e8e7c5dfa044091e4aa66edeb1a42eaf894 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Paul Burtonc893f212016-09-08 07:47:31 +01002/*
3 * Xilinx AXI Bridge for PCI Express Driver
4 *
5 * Copyright (C) 2016 Imagination Technologies
Paul Burtonc893f212016-09-08 07:47:31 +01006 */
7
Paul Burtonc893f212016-09-08 07:47:31 +01008#include <dm.h>
9#include <pci.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060010#include <linux/bitops.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060011#include <linux/printk.h>
Mayuresh Chitalee45286a2023-11-16 22:21:02 +053012#include <linux/io.h>
13#include <linux/err.h>
Paul Burtonc893f212016-09-08 07:47:31 +010014
15/**
16 * struct xilinx_pcie - Xilinx PCIe controller state
Paul Burtonc893f212016-09-08 07:47:31 +010017 * @cfg_base: The base address of memory mapped configuration space
18 */
19struct xilinx_pcie {
Paul Burtonc893f212016-09-08 07:47:31 +010020 void *cfg_base;
Jiaxun Yang490c1862024-05-17 19:14:49 +010021 pci_size_t size;
22 int first_busno;
Paul Burtonc893f212016-09-08 07:47:31 +010023};
24
25/* Register definitions */
Jiaxun Yang490c1862024-05-17 19:14:49 +010026#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 Burtonc893f212016-09-08 07:47:31 +010034/**
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 */
42static 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 Tynkkynen6b18f2a2017-09-19 23:18:04 +030051 * @udev: Pointer to the PCI bus
Paul Burtonc893f212016-09-08 07:47:31 +010052 * @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 Glass2a311e82020-01-27 08:49:37 -070065static int pcie_xilinx_config_address(const struct udevice *udev, pci_dev_t bdf,
Paul Burtonc893f212016-09-08 07:47:31 +010066 uint offset, void **paddress)
67{
Tuomas Tynkkynen6b18f2a2017-09-19 23:18:04 +030068 struct xilinx_pcie *pcie = dev_get_priv(udev);
Jiaxun Yang490c1862024-05-17 19:14:49 +010069 unsigned int bus = PCI_BUS(bdf) - pcie->first_busno;
Paul Burtonc893f212016-09-08 07:47:31 +010070 unsigned int dev = PCI_DEV(bdf);
71 unsigned int func = PCI_FUNC(bdf);
Jiaxun Yang490c1862024-05-17 19:14:49 +010072 int num_buses = DIV_ROUND_UP(pcie->size, 1 << 16);
Paul Burtonc893f212016-09-08 07:47:31 +010073 void *addr;
74
75 if ((bus > 0) && !pcie_xilinx_link_up(pcie))
76 return -ENODEV;
77
Jiaxun Yang490c1862024-05-17 19:14:49 +010078 if (bus > num_buses)
79 return -ENODEV;
80
Paul Burtonc893f212016-09-08 07:47:31 +010081 /*
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ár23769352021-11-03 01:01:05 +010089 addr += PCIE_ECAM_OFFSET(bus, dev, func, offset);
Paul Burtonc893f212016-09-08 07:47:31 +010090 *paddress = addr;
91
92 return 0;
93}
94
95/**
96 * pcie_xilinx_read_config() - Read from configuration space
Tuomas Tynkkynenf0e8d232017-09-01 17:25:58 +030097 * @bus: Pointer to the PCI bus
Paul Burtonc893f212016-09-08 07:47:31 +010098 * @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 Glass2a311e82020-01-27 08:49:37 -0700109static int pcie_xilinx_read_config(const struct udevice *bus, pci_dev_t bdf,
Paul Burtonc893f212016-09-08 07:47:31 +0100110 uint offset, ulong *valuep,
111 enum pci_size_t size)
112{
Tuomas Tynkkynen6b18f2a2017-09-19 23:18:04 +0300113 return pci_generic_mmap_read_config(bus, pcie_xilinx_config_address,
114 bdf, offset, valuep, size);
Paul Burtonc893f212016-09-08 07:47:31 +0100115}
116
117/**
118 * pcie_xilinx_write_config() - Write to configuration space
Tuomas Tynkkynenf0e8d232017-09-01 17:25:58 +0300119 * @bus: Pointer to the PCI bus
Paul Burtonc893f212016-09-08 07:47:31 +0100120 * @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 */
131static 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 Tynkkynen6b18f2a2017-09-19 23:18:04 +0300135 return pci_generic_mmap_write_config(bus, pcie_xilinx_config_address,
136 bdf, offset, value, size);
Paul Burtonc893f212016-09-08 07:47:31 +0100137}
138
139/**
Simon Glassaad29ae2020-12-03 16:55:21 -0700140 * pcie_xilinx_of_to_plat() - Translate from DT to device state
Paul Burtonc893f212016-09-08 07:47:31 +0100141 * @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 Glassaad29ae2020-12-03 16:55:21 -0700149static int pcie_xilinx_of_to_plat(struct udevice *dev)
Paul Burtonc893f212016-09-08 07:47:31 +0100150{
151 struct xilinx_pcie *pcie = dev_get_priv(dev);
Mayuresh Chitalee45286a2023-11-16 22:21:02 +0530152 fdt_addr_t addr;
153 fdt_size_t size;
Paul Burtonc893f212016-09-08 07:47:31 +0100154
Mayuresh Chitalee45286a2023-11-16 22:21:02 +0530155 addr = dev_read_addr_size(dev, &size);
156 if (addr == FDT_ADDR_T_NONE)
157 return -EINVAL;
Paul Burtonc893f212016-09-08 07:47:31 +0100158
Jiaxun Yang490c1862024-05-17 19:14:49 +0100159 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 Burtonc893f212016-09-08 07:47:31 +0100165
Jiaxun Yang490c1862024-05-17 19:14:49 +0100166static 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 Chitale46944e12023-11-16 22:21:03 +0530179 rpsc |= XILINX_PCIE_REG_RPSC_BEN;
Jiaxun Yang490c1862024-05-17 19:14:49 +0100180 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 Chitale46944e12023-11-16 22:21:03 +0530185
Paul Burtonc893f212016-09-08 07:47:31 +0100186 return 0;
187}
188
189static const struct dm_pci_ops pcie_xilinx_ops = {
190 .read_config = pcie_xilinx_read_config,
191 .write_config = pcie_xilinx_write_config,
192};
193
194static const struct udevice_id pcie_xilinx_ids[] = {
195 { .compatible = "xlnx,axi-pcie-host-1.00.a" },
196 { }
197};
198
199U_BOOT_DRIVER(pcie_xilinx) = {
200 .name = "pcie_xilinx",
201 .id = UCLASS_PCI,
202 .of_match = pcie_xilinx_ids,
203 .ops = &pcie_xilinx_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700204 .of_to_plat = pcie_xilinx_of_to_plat,
Jiaxun Yang490c1862024-05-17 19:14:49 +0100205 .probe = pci_xilinx_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700206 .priv_auto = sizeof(struct xilinx_pcie),
Paul Burtonc893f212016-09-08 07:47:31 +0100207};