blob: 7d1f13d637c0b25706264f00eb38d53b28a75eae [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +03002/*
3 * Generic PCIE host provided by e.g. QEMU
4 *
5 * Heavily based on drivers/pci/pcie_xilinx.c
6 *
7 * Copyright (C) 2016 Imagination Technologies
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +03008 */
9
10#include <common.h>
11#include <dm.h>
12#include <pci.h>
13
14#include <asm/io.h>
15
16/**
17 * struct generic_ecam_pcie - generic_ecam PCIe controller state
18 * @cfg_base: The base address of memory mapped configuration space
19 */
20struct generic_ecam_pcie {
21 void *cfg_base;
Vladimir Olteanf83567e2020-03-13 16:53:06 +020022 pci_size_t size;
23 int first_busno;
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +030024};
25
26/**
27 * pci_generic_ecam_conf_address() - Calculate the address of a config access
28 * @bus: Pointer to the PCI bus
29 * @bdf: Identifies the PCIe device to access
30 * @offset: The offset into the device's configuration space
31 * @paddress: Pointer to the pointer to write the calculates address to
32 *
33 * Calculates the address that should be accessed to perform a PCIe
34 * configuration space access for a given device identified by the PCIe
35 * controller device @pcie and the bus, device & function numbers in @bdf. If
36 * access to the device is not valid then the function will return an error
37 * code. Otherwise the address to access will be written to the pointer pointed
38 * to by @paddress.
39 */
Simon Glassc4e72c42020-01-27 08:49:37 -070040static int pci_generic_ecam_conf_address(const struct udevice *bus,
41 pci_dev_t bdf, uint offset,
42 void **paddress)
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +030043{
44 struct generic_ecam_pcie *pcie = dev_get_priv(bus);
45 void *addr;
46
47 addr = pcie->cfg_base;
Vladimir Olteanf83567e2020-03-13 16:53:06 +020048 addr += (PCI_BUS(bdf) - pcie->first_busno) << 20;
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +030049 addr += PCI_DEV(bdf) << 15;
50 addr += PCI_FUNC(bdf) << 12;
51 addr += offset;
52 *paddress = addr;
53
54 return 0;
55}
56
Vladimir Olteanf83567e2020-03-13 16:53:06 +020057static bool pci_generic_ecam_addr_valid(const struct udevice *bus,
58 pci_dev_t bdf)
59{
60 struct generic_ecam_pcie *pcie = dev_get_priv(bus);
61 int num_buses = DIV_ROUND_UP(pcie->size, 1 << 16);
62
63 return (PCI_BUS(bdf) >= pcie->first_busno &&
64 PCI_BUS(bdf) < pcie->first_busno + num_buses);
65}
66
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +030067/**
68 * pci_generic_ecam_read_config() - Read from configuration space
69 * @bus: Pointer to the PCI bus
70 * @bdf: Identifies the PCIe device to access
71 * @offset: The offset into the device's configuration space
72 * @valuep: A pointer at which to store the read value
73 * @size: Indicates the size of access to perform
74 *
75 * Read a value of size @size from offset @offset within the configuration
76 * space of the device identified by the bus, device & function numbers in @bdf
77 * on the PCI bus @bus.
78 */
Simon Glassc4e72c42020-01-27 08:49:37 -070079static int pci_generic_ecam_read_config(const struct udevice *bus,
80 pci_dev_t bdf, uint offset,
81 ulong *valuep, enum pci_size_t size)
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +030082{
Vladimir Olteanf83567e2020-03-13 16:53:06 +020083 if (!pci_generic_ecam_addr_valid(bus, bdf)) {
84 *valuep = pci_get_ff(size);
85 return 0;
86 }
87
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +030088 return pci_generic_mmap_read_config(bus, pci_generic_ecam_conf_address,
89 bdf, offset, valuep, size);
90}
91
92/**
93 * pci_generic_ecam_write_config() - Write to configuration space
94 * @bus: Pointer to the PCI bus
95 * @bdf: Identifies the PCIe device to access
96 * @offset: The offset into the device's configuration space
97 * @value: The value to write
98 * @size: Indicates the size of access to perform
99 *
100 * Write the value @value of size @size from offset @offset within the
101 * configuration space of the device identified by the bus, device & function
102 * numbers in @bdf on the PCI bus @bus.
103 */
104static int pci_generic_ecam_write_config(struct udevice *bus, pci_dev_t bdf,
105 uint offset, ulong value,
106 enum pci_size_t size)
107{
Vladimir Olteanf83567e2020-03-13 16:53:06 +0200108 if (!pci_generic_ecam_addr_valid(bus, bdf))
109 return 0;
110
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +0300111 return pci_generic_mmap_write_config(bus, pci_generic_ecam_conf_address,
112 bdf, offset, value, size);
113}
114
115/**
Simon Glassd1998a92020-12-03 16:55:21 -0700116 * pci_generic_ecam_of_to_plat() - Translate from DT to device state
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +0300117 * @dev: A pointer to the device being operated on
118 *
119 * Translate relevant data from the device tree pertaining to device @dev into
120 * state that the driver will later make use of. This state is stored in the
121 * device's private data structure.
122 *
123 * Return: 0 on success, else -EINVAL
124 */
Simon Glassd1998a92020-12-03 16:55:21 -0700125static int pci_generic_ecam_of_to_plat(struct udevice *dev)
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +0300126{
127 struct generic_ecam_pcie *pcie = dev_get_priv(dev);
128 struct fdt_resource reg_res;
129 DECLARE_GLOBAL_DATA_PTR;
130 int err;
131
132 err = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), "reg",
133 0, &reg_res);
134 if (err < 0) {
135 pr_err("\"reg\" resource not found\n");
136 return err;
137 }
138
Vladimir Olteanf83567e2020-03-13 16:53:06 +0200139 pcie->size = fdt_resource_size(&reg_res);
140 pcie->cfg_base = map_physmem(reg_res.start, pcie->size, MAP_NOCACHE);
141
142 return 0;
143}
144
145static int pci_generic_ecam_probe(struct udevice *dev)
146{
147 struct generic_ecam_pcie *pcie = dev_get_priv(dev);
148
Simon Glass8b85dfc2020-12-16 21:20:07 -0700149 pcie->first_busno = dev_seq(dev);
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +0300150
151 return 0;
152}
153
154static const struct dm_pci_ops pci_generic_ecam_ops = {
155 .read_config = pci_generic_ecam_read_config,
156 .write_config = pci_generic_ecam_write_config,
157};
158
159static const struct udevice_id pci_generic_ecam_ids[] = {
160 { .compatible = "pci-host-ecam-generic" },
161 { }
162};
163
164U_BOOT_DRIVER(pci_generic_ecam) = {
165 .name = "pci_generic_ecam",
166 .id = UCLASS_PCI,
167 .of_match = pci_generic_ecam_ids,
168 .ops = &pci_generic_ecam_ops,
Vladimir Olteanf83567e2020-03-13 16:53:06 +0200169 .probe = pci_generic_ecam_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700170 .of_to_plat = pci_generic_ecam_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700171 .priv_auto = sizeof(struct generic_ecam_pcie),
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +0300172};