blob: ae9a65b0a9e10f34779c07e0e18e245841f5d925 [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
8#include <common.h>
9#include <dm.h>
10#include <pci.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060012#include <linux/bitops.h>
Paul Burtonc893f212016-09-08 07:47:31 +010013
14#include <asm/io.h>
15
16/**
17 * struct xilinx_pcie - Xilinx PCIe controller state
Paul Burtonc893f212016-09-08 07:47:31 +010018 * @cfg_base: The base address of memory mapped configuration space
19 */
20struct xilinx_pcie {
Paul Burtonc893f212016-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 Tynkkynen6b18f2a2017-09-19 23:18:04 +030045 * @udev: Pointer to the PCI bus
Paul Burtonc893f212016-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 Glass2a311e82020-01-27 08:49:37 -070059static int pcie_xilinx_config_address(const struct udevice *udev, pci_dev_t bdf,
Paul Burtonc893f212016-09-08 07:47:31 +010060 uint offset, void **paddress)
61{
Tuomas Tynkkynen6b18f2a2017-09-19 23:18:04 +030062 struct xilinx_pcie *pcie = dev_get_priv(udev);
Paul Burtonc893f212016-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;
79 addr += bus << 20;
80 addr += dev << 15;
81 addr += func << 12;
82 addr += offset;
83 *paddress = addr;
84
85 return 0;
86}
87
88/**
89 * pcie_xilinx_read_config() - Read from configuration space
Tuomas Tynkkynenf0e8d232017-09-01 17:25:58 +030090 * @bus: Pointer to the PCI bus
Paul Burtonc893f212016-09-08 07:47:31 +010091 * @bdf: Identifies the PCIe device to access
92 * @offset: The offset into the device's configuration space
93 * @valuep: A pointer at which to store the read value
94 * @size: Indicates the size of access to perform
95 *
96 * Read a value of size @size from offset @offset within the configuration
97 * space of the device identified by the bus, device & function numbers in @bdf
98 * on the PCI bus @bus.
99 *
100 * Return: 0 on success, else -ENODEV or -EINVAL
101 */
Simon Glass2a311e82020-01-27 08:49:37 -0700102static int pcie_xilinx_read_config(const struct udevice *bus, pci_dev_t bdf,
Paul Burtonc893f212016-09-08 07:47:31 +0100103 uint offset, ulong *valuep,
104 enum pci_size_t size)
105{
Tuomas Tynkkynen6b18f2a2017-09-19 23:18:04 +0300106 return pci_generic_mmap_read_config(bus, pcie_xilinx_config_address,
107 bdf, offset, valuep, size);
Paul Burtonc893f212016-09-08 07:47:31 +0100108}
109
110/**
111 * pcie_xilinx_write_config() - Write to configuration space
Tuomas Tynkkynenf0e8d232017-09-01 17:25:58 +0300112 * @bus: Pointer to the PCI bus
Paul Burtonc893f212016-09-08 07:47:31 +0100113 * @bdf: Identifies the PCIe device to access
114 * @offset: The offset into the device's configuration space
115 * @value: The value to write
116 * @size: Indicates the size of access to perform
117 *
118 * Write the value @value of size @size from offset @offset within the
119 * configuration space of the device identified by the bus, device & function
120 * numbers in @bdf on the PCI bus @bus.
121 *
122 * Return: 0 on success, else -ENODEV or -EINVAL
123 */
124static int pcie_xilinx_write_config(struct udevice *bus, pci_dev_t bdf,
125 uint offset, ulong value,
126 enum pci_size_t size)
127{
Tuomas Tynkkynen6b18f2a2017-09-19 23:18:04 +0300128 return pci_generic_mmap_write_config(bus, pcie_xilinx_config_address,
129 bdf, offset, value, size);
Paul Burtonc893f212016-09-08 07:47:31 +0100130}
131
132/**
Simon Glassaad29ae2020-12-03 16:55:21 -0700133 * pcie_xilinx_of_to_plat() - Translate from DT to device state
Paul Burtonc893f212016-09-08 07:47:31 +0100134 * @dev: A pointer to the device being operated on
135 *
136 * Translate relevant data from the device tree pertaining to device @dev into
137 * state that the driver will later make use of. This state is stored in the
138 * device's private data structure.
139 *
140 * Return: 0 on success, else -EINVAL
141 */
Simon Glassaad29ae2020-12-03 16:55:21 -0700142static int pcie_xilinx_of_to_plat(struct udevice *dev)
Paul Burtonc893f212016-09-08 07:47:31 +0100143{
144 struct xilinx_pcie *pcie = dev_get_priv(dev);
145 struct fdt_resource reg_res;
146 DECLARE_GLOBAL_DATA_PTR;
147 int err;
148
Simon Glassdd79d6e2017-01-17 16:52:55 -0700149 err = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), "reg",
Paul Burtonc893f212016-09-08 07:47:31 +0100150 0, &reg_res);
151 if (err < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900152 pr_err("\"reg\" resource not found\n");
Paul Burtonc893f212016-09-08 07:47:31 +0100153 return err;
154 }
155
156 pcie->cfg_base = map_physmem(reg_res.start,
157 fdt_resource_size(&reg_res),
158 MAP_NOCACHE);
159
160 return 0;
161}
162
163static const struct dm_pci_ops pcie_xilinx_ops = {
164 .read_config = pcie_xilinx_read_config,
165 .write_config = pcie_xilinx_write_config,
166};
167
168static const struct udevice_id pcie_xilinx_ids[] = {
169 { .compatible = "xlnx,axi-pcie-host-1.00.a" },
170 { }
171};
172
173U_BOOT_DRIVER(pcie_xilinx) = {
174 .name = "pcie_xilinx",
175 .id = UCLASS_PCI,
176 .of_match = pcie_xilinx_ids,
177 .ops = &pcie_xilinx_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700178 .of_to_plat = pcie_xilinx_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700179 .priv_auto = sizeof(struct xilinx_pcie),
Paul Burtonc893f212016-09-08 07:47:31 +0100180};