blob: 3db460b5f93627304a3eeec82b2cd14b1568ea24 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Paul Burtona29e45a2016-09-08 07:47:31 +01002/*
3 * Xilinx AXI Bridge for PCI Express Driver
4 *
5 * Copyright (C) 2016 Imagination Technologies
Paul Burtona29e45a2016-09-08 07:47:31 +01006 */
7
8#include <common.h>
9#include <dm.h>
10#include <pci.h>
Simon Glasscd93d622020-05-10 11:40:13 -060011#include <linux/bitops.h>
Simon Glass1e94b462023-09-14 18:21:46 -060012#include <linux/printk.h>
Mayuresh Chitale891b4812023-11-16 22:21:02 +053013#include <linux/io.h>
14#include <linux/err.h>
Paul Burtona29e45a2016-09-08 07:47:31 +010015
16/**
17 * struct xilinx_pcie - Xilinx PCIe controller state
Paul Burtona29e45a2016-09-08 07:47:31 +010018 * @cfg_base: The base address of memory mapped configuration space
19 */
20struct xilinx_pcie {
Paul Burtona29e45a2016-09-08 07:47:31 +010021 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 Chitalea62b01d2023-11-16 22:21:03 +053027#define XILINX_PCIE_REG_RPSC 0x148
28#define XILINX_PCIE_REG_RPSC_BEN BIT(0)
Paul Burtona29e45a2016-09-08 07:47:31 +010029
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 */
38static 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 Tynkkynen75e3fea2017-09-19 23:18:04 +030047 * @udev: Pointer to the PCI bus
Paul Burtona29e45a2016-09-08 07:47:31 +010048 * @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 Glassc4e72c42020-01-27 08:49:37 -070061static int pcie_xilinx_config_address(const struct udevice *udev, pci_dev_t bdf,
Paul Burtona29e45a2016-09-08 07:47:31 +010062 uint offset, void **paddress)
63{
Tuomas Tynkkynen75e3fea2017-09-19 23:18:04 +030064 struct xilinx_pcie *pcie = dev_get_priv(udev);
Paul Burtona29e45a2016-09-08 07:47:31 +010065 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ára4bc38d2021-11-03 01:01:05 +010081 addr += PCIE_ECAM_OFFSET(bus, dev, func, offset);
Paul Burtona29e45a2016-09-08 07:47:31 +010082 *paddress = addr;
83
84 return 0;
85}
86
87/**
88 * pcie_xilinx_read_config() - Read from configuration space
Tuomas Tynkkynenadfc3e42017-09-01 17:25:58 +030089 * @bus: Pointer to the PCI bus
Paul Burtona29e45a2016-09-08 07:47:31 +010090 * @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 Glassc4e72c42020-01-27 08:49:37 -0700101static int pcie_xilinx_read_config(const struct udevice *bus, pci_dev_t bdf,
Paul Burtona29e45a2016-09-08 07:47:31 +0100102 uint offset, ulong *valuep,
103 enum pci_size_t size)
104{
Tuomas Tynkkynen75e3fea2017-09-19 23:18:04 +0300105 return pci_generic_mmap_read_config(bus, pcie_xilinx_config_address,
106 bdf, offset, valuep, size);
Paul Burtona29e45a2016-09-08 07:47:31 +0100107}
108
109/**
110 * pcie_xilinx_write_config() - Write to configuration space
Tuomas Tynkkynenadfc3e42017-09-01 17:25:58 +0300111 * @bus: Pointer to the PCI bus
Paul Burtona29e45a2016-09-08 07:47:31 +0100112 * @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 */
123static 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 Tynkkynen75e3fea2017-09-19 23:18:04 +0300127 return pci_generic_mmap_write_config(bus, pcie_xilinx_config_address,
128 bdf, offset, value, size);
Paul Burtona29e45a2016-09-08 07:47:31 +0100129}
130
131/**
Simon Glassd1998a92020-12-03 16:55:21 -0700132 * pcie_xilinx_of_to_plat() - Translate from DT to device state
Paul Burtona29e45a2016-09-08 07:47:31 +0100133 * @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 Glassd1998a92020-12-03 16:55:21 -0700141static int pcie_xilinx_of_to_plat(struct udevice *dev)
Paul Burtona29e45a2016-09-08 07:47:31 +0100142{
143 struct xilinx_pcie *pcie = dev_get_priv(dev);
Mayuresh Chitale891b4812023-11-16 22:21:02 +0530144 fdt_addr_t addr;
145 fdt_size_t size;
Mayuresh Chitalea62b01d2023-11-16 22:21:03 +0530146 u32 rpsc;
Paul Burtona29e45a2016-09-08 07:47:31 +0100147
Mayuresh Chitale891b4812023-11-16 22:21:02 +0530148 addr = dev_read_addr_size(dev, &size);
149 if (addr == FDT_ADDR_T_NONE)
150 return -EINVAL;
Paul Burtona29e45a2016-09-08 07:47:31 +0100151
Mayuresh Chitale891b4812023-11-16 22:21:02 +0530152 pcie->cfg_base = devm_ioremap(dev, addr, size);
153 if (IS_ERR(pcie->cfg_base))
154 return PTR_ERR(pcie->cfg_base);
Paul Burtona29e45a2016-09-08 07:47:31 +0100155
Mayuresh Chitalea62b01d2023-11-16 22:21:03 +0530156 /* 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 Burtona29e45a2016-09-08 07:47:31 +0100161 return 0;
162}
163
164static const struct dm_pci_ops pcie_xilinx_ops = {
165 .read_config = pcie_xilinx_read_config,
166 .write_config = pcie_xilinx_write_config,
167};
168
169static const struct udevice_id pcie_xilinx_ids[] = {
170 { .compatible = "xlnx,axi-pcie-host-1.00.a" },
171 { }
172};
173
174U_BOOT_DRIVER(pcie_xilinx) = {
175 .name = "pcie_xilinx",
176 .id = UCLASS_PCI,
177 .of_match = pcie_xilinx_ids,
178 .ops = &pcie_xilinx_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700179 .of_to_plat = pcie_xilinx_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700180 .priv_auto = sizeof(struct xilinx_pcie),
Paul Burtona29e45a2016-09-08 07:47:31 +0100181};