blob: 1a9f9aec2ee1d4df5f63231832dc88aa7c59fb79 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Tuomas Tynkkynena765f712017-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 Tynkkynena765f712017-09-19 23:18:06 +03008 */
9
10#include <common.h>
11#include <dm.h>
12#include <pci.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Tuomas Tynkkynena765f712017-09-19 23:18:06 +030014
15#include <asm/io.h>
16
Alistair Delva2f285cd2021-10-20 21:31:34 +000017#define TYPE_PCI 0x1
18
Tuomas Tynkkynena765f712017-09-19 23:18:06 +030019/**
20 * struct generic_ecam_pcie - generic_ecam PCIe controller state
21 * @cfg_base: The base address of memory mapped configuration space
22 */
23struct generic_ecam_pcie {
24 void *cfg_base;
Vladimir Oltean34e46e02020-03-13 16:53:06 +020025 pci_size_t size;
26 int first_busno;
Tuomas Tynkkynena765f712017-09-19 23:18:06 +030027};
28
29/**
30 * pci_generic_ecam_conf_address() - Calculate the address of a config access
31 * @bus: Pointer to the PCI bus
32 * @bdf: Identifies the PCIe device to access
33 * @offset: The offset into the device's configuration space
34 * @paddress: Pointer to the pointer to write the calculates address to
35 *
36 * Calculates the address that should be accessed to perform a PCIe
37 * configuration space access for a given device identified by the PCIe
38 * controller device @pcie and the bus, device & function numbers in @bdf. If
39 * access to the device is not valid then the function will return an error
40 * code. Otherwise the address to access will be written to the pointer pointed
41 * to by @paddress.
42 */
Simon Glass2a311e82020-01-27 08:49:37 -070043static int pci_generic_ecam_conf_address(const struct udevice *bus,
44 pci_dev_t bdf, uint offset,
45 void **paddress)
Tuomas Tynkkynena765f712017-09-19 23:18:06 +030046{
47 struct generic_ecam_pcie *pcie = dev_get_priv(bus);
48 void *addr;
49
50 addr = pcie->cfg_base;
Alistair Delva2f285cd2021-10-20 21:31:34 +000051
52 if (dev_get_driver_data(bus) == TYPE_PCI) {
53 addr += ((PCI_BUS(bdf) - pcie->first_busno) << 16) |
54 (PCI_DEV(bdf) << 11) | (PCI_FUNC(bdf) << 8) | offset;
55 } else {
56 addr += PCIE_ECAM_OFFSET(PCI_BUS(bdf) - pcie->first_busno,
57 PCI_DEV(bdf), PCI_FUNC(bdf), offset);
58 }
Tuomas Tynkkynena765f712017-09-19 23:18:06 +030059 *paddress = addr;
60
61 return 0;
62}
63
Vladimir Oltean34e46e02020-03-13 16:53:06 +020064static bool pci_generic_ecam_addr_valid(const struct udevice *bus,
65 pci_dev_t bdf)
66{
67 struct generic_ecam_pcie *pcie = dev_get_priv(bus);
68 int num_buses = DIV_ROUND_UP(pcie->size, 1 << 16);
69
70 return (PCI_BUS(bdf) >= pcie->first_busno &&
71 PCI_BUS(bdf) < pcie->first_busno + num_buses);
72}
73
Tuomas Tynkkynena765f712017-09-19 23:18:06 +030074/**
75 * pci_generic_ecam_read_config() - Read from configuration space
76 * @bus: Pointer to the PCI bus
77 * @bdf: Identifies the PCIe device to access
78 * @offset: The offset into the device's configuration space
79 * @valuep: A pointer at which to store the read value
80 * @size: Indicates the size of access to perform
81 *
82 * Read a value of size @size from offset @offset within the configuration
83 * space of the device identified by the bus, device & function numbers in @bdf
84 * on the PCI bus @bus.
85 */
Simon Glass2a311e82020-01-27 08:49:37 -070086static int pci_generic_ecam_read_config(const struct udevice *bus,
87 pci_dev_t bdf, uint offset,
88 ulong *valuep, enum pci_size_t size)
Tuomas Tynkkynena765f712017-09-19 23:18:06 +030089{
Vladimir Oltean34e46e02020-03-13 16:53:06 +020090 if (!pci_generic_ecam_addr_valid(bus, bdf)) {
91 *valuep = pci_get_ff(size);
92 return 0;
93 }
94
Tuomas Tynkkynena765f712017-09-19 23:18:06 +030095 return pci_generic_mmap_read_config(bus, pci_generic_ecam_conf_address,
96 bdf, offset, valuep, size);
97}
98
99/**
100 * pci_generic_ecam_write_config() - Write to configuration space
101 * @bus: Pointer to the PCI bus
102 * @bdf: Identifies the PCIe device to access
103 * @offset: The offset into the device's configuration space
104 * @value: The value to write
105 * @size: Indicates the size of access to perform
106 *
107 * Write the value @value of size @size from offset @offset within the
108 * configuration space of the device identified by the bus, device & function
109 * numbers in @bdf on the PCI bus @bus.
110 */
111static int pci_generic_ecam_write_config(struct udevice *bus, pci_dev_t bdf,
112 uint offset, ulong value,
113 enum pci_size_t size)
114{
Vladimir Oltean34e46e02020-03-13 16:53:06 +0200115 if (!pci_generic_ecam_addr_valid(bus, bdf))
116 return 0;
117
Tuomas Tynkkynena765f712017-09-19 23:18:06 +0300118 return pci_generic_mmap_write_config(bus, pci_generic_ecam_conf_address,
119 bdf, offset, value, size);
120}
121
122/**
Simon Glassaad29ae2020-12-03 16:55:21 -0700123 * pci_generic_ecam_of_to_plat() - Translate from DT to device state
Tuomas Tynkkynena765f712017-09-19 23:18:06 +0300124 * @dev: A pointer to the device being operated on
125 *
126 * Translate relevant data from the device tree pertaining to device @dev into
127 * state that the driver will later make use of. This state is stored in the
128 * device's private data structure.
129 *
130 * Return: 0 on success, else -EINVAL
131 */
Simon Glassaad29ae2020-12-03 16:55:21 -0700132static int pci_generic_ecam_of_to_plat(struct udevice *dev)
Tuomas Tynkkynena765f712017-09-19 23:18:06 +0300133{
134 struct generic_ecam_pcie *pcie = dev_get_priv(dev);
135 struct fdt_resource reg_res;
136 DECLARE_GLOBAL_DATA_PTR;
137 int err;
138
139 err = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), "reg",
140 0, &reg_res);
141 if (err < 0) {
142 pr_err("\"reg\" resource not found\n");
143 return err;
144 }
145
Vladimir Oltean34e46e02020-03-13 16:53:06 +0200146 pcie->size = fdt_resource_size(&reg_res);
147 pcie->cfg_base = map_physmem(reg_res.start, pcie->size, MAP_NOCACHE);
Tuomas Tynkkynena765f712017-09-19 23:18:06 +0300148
149 return 0;
150}
151
Vladimir Oltean34e46e02020-03-13 16:53:06 +0200152static int pci_generic_ecam_probe(struct udevice *dev)
153{
154 struct generic_ecam_pcie *pcie = dev_get_priv(dev);
155
Simon Glass75e534b2020-12-16 21:20:07 -0700156 pcie->first_busno = dev_seq(dev);
Vladimir Oltean34e46e02020-03-13 16:53:06 +0200157
158 return 0;
159}
160
Tuomas Tynkkynena765f712017-09-19 23:18:06 +0300161static const struct dm_pci_ops pci_generic_ecam_ops = {
162 .read_config = pci_generic_ecam_read_config,
163 .write_config = pci_generic_ecam_write_config,
164};
165
166static const struct udevice_id pci_generic_ecam_ids[] = {
Alistair Delva2f285cd2021-10-20 21:31:34 +0000167 { .compatible = "pci-host-ecam-generic" /* PCI-E */ },
168 { .compatible = "pci-host-cam-generic", .data = TYPE_PCI },
Tuomas Tynkkynena765f712017-09-19 23:18:06 +0300169 { }
170};
171
172U_BOOT_DRIVER(pci_generic_ecam) = {
173 .name = "pci_generic_ecam",
174 .id = UCLASS_PCI,
175 .of_match = pci_generic_ecam_ids,
176 .ops = &pci_generic_ecam_ops,
Vladimir Oltean34e46e02020-03-13 16:53:06 +0200177 .probe = pci_generic_ecam_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700178 .of_to_plat = pci_generic_ecam_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700179 .priv_auto = sizeof(struct generic_ecam_pcie),
Tuomas Tynkkynena765f712017-09-19 23:18:06 +0300180};