blob: 53fd121e905cd8a8436768d13b71006ca5f04285 [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 Glass401d1c42020-10-30 21:38:53 -060011#include <asm/global_data.h>
Simon Glasscd93d622020-05-10 11:40:13 -060012#include <linux/bitops.h>
Simon Glass1e94b462023-09-14 18:21:46 -060013#include <linux/printk.h>
Paul Burtona29e45a2016-09-08 07:47:31 +010014
15#include <asm/io.h>
16
17/**
18 * struct xilinx_pcie - Xilinx PCIe controller state
Paul Burtona29e45a2016-09-08 07:47:31 +010019 * @cfg_base: The base address of memory mapped configuration space
20 */
21struct xilinx_pcie {
Paul Burtona29e45a2016-09-08 07:47:31 +010022 void *cfg_base;
23};
24
25/* Register definitions */
26#define XILINX_PCIE_REG_PSCR 0x144
27#define XILINX_PCIE_REG_PSCR_LNKUP BIT(11)
28
29/**
30 * pcie_xilinx_link_up() - Check whether the PCIe link is up
31 * @pcie: Pointer to the PCI controller state
32 *
33 * Checks whether the PCIe link for the given device is up or down.
34 *
35 * Return: true if the link is up, else false
36 */
37static bool pcie_xilinx_link_up(struct xilinx_pcie *pcie)
38{
39 uint32_t pscr = __raw_readl(pcie->cfg_base + XILINX_PCIE_REG_PSCR);
40
41 return pscr & XILINX_PCIE_REG_PSCR_LNKUP;
42}
43
44/**
45 * pcie_xilinx_config_address() - Calculate the address of a config access
Tuomas Tynkkynen75e3fea2017-09-19 23:18:04 +030046 * @udev: Pointer to the PCI bus
Paul Burtona29e45a2016-09-08 07:47:31 +010047 * @bdf: Identifies the PCIe device to access
48 * @offset: The offset into the device's configuration space
49 * @paddress: Pointer to the pointer to write the calculates address to
50 *
51 * Calculates the address that should be accessed to perform a PCIe
52 * configuration space access for a given device identified by the PCIe
53 * controller device @pcie and the bus, device & function numbers in @bdf. If
54 * access to the device is not valid then the function will return an error
55 * code. Otherwise the address to access will be written to the pointer pointed
56 * to by @paddress.
57 *
58 * Return: 0 on success, else -ENODEV
59 */
Simon Glassc4e72c42020-01-27 08:49:37 -070060static int pcie_xilinx_config_address(const struct udevice *udev, pci_dev_t bdf,
Paul Burtona29e45a2016-09-08 07:47:31 +010061 uint offset, void **paddress)
62{
Tuomas Tynkkynen75e3fea2017-09-19 23:18:04 +030063 struct xilinx_pcie *pcie = dev_get_priv(udev);
Paul Burtona29e45a2016-09-08 07:47:31 +010064 unsigned int bus = PCI_BUS(bdf);
65 unsigned int dev = PCI_DEV(bdf);
66 unsigned int func = PCI_FUNC(bdf);
67 void *addr;
68
69 if ((bus > 0) && !pcie_xilinx_link_up(pcie))
70 return -ENODEV;
71
72 /*
73 * Busses 0 (host-PCIe bridge) & 1 (its immediate child) are
74 * limited to a single device each.
75 */
76 if ((bus < 2) && (dev > 0))
77 return -ENODEV;
78
79 addr = pcie->cfg_base;
Pali Rohára4bc38d2021-11-03 01:01:05 +010080 addr += PCIE_ECAM_OFFSET(bus, dev, func, offset);
Paul Burtona29e45a2016-09-08 07:47:31 +010081 *paddress = addr;
82
83 return 0;
84}
85
86/**
87 * pcie_xilinx_read_config() - Read from configuration space
Tuomas Tynkkynenadfc3e42017-09-01 17:25:58 +030088 * @bus: Pointer to the PCI bus
Paul Burtona29e45a2016-09-08 07:47:31 +010089 * @bdf: Identifies the PCIe device to access
90 * @offset: The offset into the device's configuration space
91 * @valuep: A pointer at which to store the read value
92 * @size: Indicates the size of access to perform
93 *
94 * Read a value of size @size from offset @offset within the configuration
95 * space of the device identified by the bus, device & function numbers in @bdf
96 * on the PCI bus @bus.
97 *
98 * Return: 0 on success, else -ENODEV or -EINVAL
99 */
Simon Glassc4e72c42020-01-27 08:49:37 -0700100static int pcie_xilinx_read_config(const struct udevice *bus, pci_dev_t bdf,
Paul Burtona29e45a2016-09-08 07:47:31 +0100101 uint offset, ulong *valuep,
102 enum pci_size_t size)
103{
Tuomas Tynkkynen75e3fea2017-09-19 23:18:04 +0300104 return pci_generic_mmap_read_config(bus, pcie_xilinx_config_address,
105 bdf, offset, valuep, size);
Paul Burtona29e45a2016-09-08 07:47:31 +0100106}
107
108/**
109 * pcie_xilinx_write_config() - Write to configuration space
Tuomas Tynkkynenadfc3e42017-09-01 17:25:58 +0300110 * @bus: Pointer to the PCI bus
Paul Burtona29e45a2016-09-08 07:47:31 +0100111 * @bdf: Identifies the PCIe device to access
112 * @offset: The offset into the device's configuration space
113 * @value: The value to write
114 * @size: Indicates the size of access to perform
115 *
116 * Write the value @value of size @size from offset @offset within the
117 * configuration space of the device identified by the bus, device & function
118 * numbers in @bdf on the PCI bus @bus.
119 *
120 * Return: 0 on success, else -ENODEV or -EINVAL
121 */
122static int pcie_xilinx_write_config(struct udevice *bus, pci_dev_t bdf,
123 uint offset, ulong value,
124 enum pci_size_t size)
125{
Tuomas Tynkkynen75e3fea2017-09-19 23:18:04 +0300126 return pci_generic_mmap_write_config(bus, pcie_xilinx_config_address,
127 bdf, offset, value, size);
Paul Burtona29e45a2016-09-08 07:47:31 +0100128}
129
130/**
Simon Glassd1998a92020-12-03 16:55:21 -0700131 * pcie_xilinx_of_to_plat() - Translate from DT to device state
Paul Burtona29e45a2016-09-08 07:47:31 +0100132 * @dev: A pointer to the device being operated on
133 *
134 * Translate relevant data from the device tree pertaining to device @dev into
135 * state that the driver will later make use of. This state is stored in the
136 * device's private data structure.
137 *
138 * Return: 0 on success, else -EINVAL
139 */
Simon Glassd1998a92020-12-03 16:55:21 -0700140static int pcie_xilinx_of_to_plat(struct udevice *dev)
Paul Burtona29e45a2016-09-08 07:47:31 +0100141{
142 struct xilinx_pcie *pcie = dev_get_priv(dev);
143 struct fdt_resource reg_res;
144 DECLARE_GLOBAL_DATA_PTR;
145 int err;
146
Simon Glasse160f7d2017-01-17 16:52:55 -0700147 err = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), "reg",
Paul Burtona29e45a2016-09-08 07:47:31 +0100148 0, &reg_res);
149 if (err < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900150 pr_err("\"reg\" resource not found\n");
Paul Burtona29e45a2016-09-08 07:47:31 +0100151 return err;
152 }
153
154 pcie->cfg_base = map_physmem(reg_res.start,
155 fdt_resource_size(&reg_res),
156 MAP_NOCACHE);
157
158 return 0;
159}
160
161static const struct dm_pci_ops pcie_xilinx_ops = {
162 .read_config = pcie_xilinx_read_config,
163 .write_config = pcie_xilinx_write_config,
164};
165
166static const struct udevice_id pcie_xilinx_ids[] = {
167 { .compatible = "xlnx,axi-pcie-host-1.00.a" },
168 { }
169};
170
171U_BOOT_DRIVER(pcie_xilinx) = {
172 .name = "pcie_xilinx",
173 .id = UCLASS_PCI,
174 .of_match = pcie_xilinx_ids,
175 .ops = &pcie_xilinx_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700176 .of_to_plat = pcie_xilinx_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700177 .priv_auto = sizeof(struct xilinx_pcie),
Paul Burtona29e45a2016-09-08 07:47:31 +0100178};