blob: fdc9b08c10b1f74e545e123f6c09ab3617ef86d4 [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)
27
28/**
29 * pcie_xilinx_link_up() - Check whether the PCIe link is up
30 * @pcie: Pointer to the PCI controller state
31 *
32 * Checks whether the PCIe link for the given device is up or down.
33 *
34 * Return: true if the link is up, else false
35 */
36static bool pcie_xilinx_link_up(struct xilinx_pcie *pcie)
37{
38 uint32_t pscr = __raw_readl(pcie->cfg_base + XILINX_PCIE_REG_PSCR);
39
40 return pscr & XILINX_PCIE_REG_PSCR_LNKUP;
41}
42
43/**
44 * pcie_xilinx_config_address() - Calculate the address of a config access
Tuomas Tynkkynen75e3fea2017-09-19 23:18:04 +030045 * @udev: Pointer to the PCI bus
Paul Burtona29e45a2016-09-08 07:47:31 +010046 * @bdf: Identifies the PCIe device to access
47 * @offset: The offset into the device's configuration space
48 * @paddress: Pointer to the pointer to write the calculates address to
49 *
50 * Calculates the address that should be accessed to perform a PCIe
51 * configuration space access for a given device identified by the PCIe
52 * controller device @pcie and the bus, device & function numbers in @bdf. If
53 * access to the device is not valid then the function will return an error
54 * code. Otherwise the address to access will be written to the pointer pointed
55 * to by @paddress.
56 *
57 * Return: 0 on success, else -ENODEV
58 */
Simon Glassc4e72c42020-01-27 08:49:37 -070059static int pcie_xilinx_config_address(const struct udevice *udev, pci_dev_t bdf,
Paul Burtona29e45a2016-09-08 07:47:31 +010060 uint offset, void **paddress)
61{
Tuomas Tynkkynen75e3fea2017-09-19 23:18:04 +030062 struct xilinx_pcie *pcie = dev_get_priv(udev);
Paul Burtona29e45a2016-09-08 07:47:31 +010063 unsigned int bus = PCI_BUS(bdf);
64 unsigned int dev = PCI_DEV(bdf);
65 unsigned int func = PCI_FUNC(bdf);
66 void *addr;
67
68 if ((bus > 0) && !pcie_xilinx_link_up(pcie))
69 return -ENODEV;
70
71 /*
72 * Busses 0 (host-PCIe bridge) & 1 (its immediate child) are
73 * limited to a single device each.
74 */
75 if ((bus < 2) && (dev > 0))
76 return -ENODEV;
77
78 addr = pcie->cfg_base;
Pali Rohára4bc38d2021-11-03 01:01:05 +010079 addr += PCIE_ECAM_OFFSET(bus, dev, func, offset);
Paul Burtona29e45a2016-09-08 07:47:31 +010080 *paddress = addr;
81
82 return 0;
83}
84
85/**
86 * pcie_xilinx_read_config() - Read from configuration space
Tuomas Tynkkynenadfc3e42017-09-01 17:25:58 +030087 * @bus: Pointer to the PCI bus
Paul Burtona29e45a2016-09-08 07:47:31 +010088 * @bdf: Identifies the PCIe device to access
89 * @offset: The offset into the device's configuration space
90 * @valuep: A pointer at which to store the read value
91 * @size: Indicates the size of access to perform
92 *
93 * Read a value of size @size from offset @offset within the configuration
94 * space of the device identified by the bus, device & function numbers in @bdf
95 * on the PCI bus @bus.
96 *
97 * Return: 0 on success, else -ENODEV or -EINVAL
98 */
Simon Glassc4e72c42020-01-27 08:49:37 -070099static int pcie_xilinx_read_config(const struct udevice *bus, pci_dev_t bdf,
Paul Burtona29e45a2016-09-08 07:47:31 +0100100 uint offset, ulong *valuep,
101 enum pci_size_t size)
102{
Tuomas Tynkkynen75e3fea2017-09-19 23:18:04 +0300103 return pci_generic_mmap_read_config(bus, pcie_xilinx_config_address,
104 bdf, offset, valuep, size);
Paul Burtona29e45a2016-09-08 07:47:31 +0100105}
106
107/**
108 * pcie_xilinx_write_config() - Write to configuration space
Tuomas Tynkkynenadfc3e42017-09-01 17:25:58 +0300109 * @bus: Pointer to the PCI bus
Paul Burtona29e45a2016-09-08 07:47:31 +0100110 * @bdf: Identifies the PCIe device to access
111 * @offset: The offset into the device's configuration space
112 * @value: The value to write
113 * @size: Indicates the size of access to perform
114 *
115 * Write the value @value of size @size from offset @offset within the
116 * configuration space of the device identified by the bus, device & function
117 * numbers in @bdf on the PCI bus @bus.
118 *
119 * Return: 0 on success, else -ENODEV or -EINVAL
120 */
121static int pcie_xilinx_write_config(struct udevice *bus, pci_dev_t bdf,
122 uint offset, ulong value,
123 enum pci_size_t size)
124{
Tuomas Tynkkynen75e3fea2017-09-19 23:18:04 +0300125 return pci_generic_mmap_write_config(bus, pcie_xilinx_config_address,
126 bdf, offset, value, size);
Paul Burtona29e45a2016-09-08 07:47:31 +0100127}
128
129/**
Simon Glassd1998a92020-12-03 16:55:21 -0700130 * pcie_xilinx_of_to_plat() - Translate from DT to device state
Paul Burtona29e45a2016-09-08 07:47:31 +0100131 * @dev: A pointer to the device being operated on
132 *
133 * Translate relevant data from the device tree pertaining to device @dev into
134 * state that the driver will later make use of. This state is stored in the
135 * device's private data structure.
136 *
137 * Return: 0 on success, else -EINVAL
138 */
Simon Glassd1998a92020-12-03 16:55:21 -0700139static int pcie_xilinx_of_to_plat(struct udevice *dev)
Paul Burtona29e45a2016-09-08 07:47:31 +0100140{
141 struct xilinx_pcie *pcie = dev_get_priv(dev);
Mayuresh Chitale891b4812023-11-16 22:21:02 +0530142 fdt_addr_t addr;
143 fdt_size_t size;
Paul Burtona29e45a2016-09-08 07:47:31 +0100144
Mayuresh Chitale891b4812023-11-16 22:21:02 +0530145 addr = dev_read_addr_size(dev, &size);
146 if (addr == FDT_ADDR_T_NONE)
147 return -EINVAL;
Paul Burtona29e45a2016-09-08 07:47:31 +0100148
Mayuresh Chitale891b4812023-11-16 22:21:02 +0530149 pcie->cfg_base = devm_ioremap(dev, addr, size);
150 if (IS_ERR(pcie->cfg_base))
151 return PTR_ERR(pcie->cfg_base);
Paul Burtona29e45a2016-09-08 07:47:31 +0100152
153 return 0;
154}
155
156static const struct dm_pci_ops pcie_xilinx_ops = {
157 .read_config = pcie_xilinx_read_config,
158 .write_config = pcie_xilinx_write_config,
159};
160
161static const struct udevice_id pcie_xilinx_ids[] = {
162 { .compatible = "xlnx,axi-pcie-host-1.00.a" },
163 { }
164};
165
166U_BOOT_DRIVER(pcie_xilinx) = {
167 .name = "pcie_xilinx",
168 .id = UCLASS_PCI,
169 .of_match = pcie_xilinx_ids,
170 .ops = &pcie_xilinx_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700171 .of_to_plat = pcie_xilinx_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700172 .priv_auto = sizeof(struct xilinx_pcie),
Paul Burtona29e45a2016-09-08 07:47:31 +0100173};