blob: 3cb2bbbccb4439e18d0f2b69329e9747483ee083 [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
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +030010#include <dm.h>
11#include <pci.h>
Maksim Kiselev67c7f142024-02-14 23:30:01 +030012#include <linux/ioport.h>
Simon Glass1e94b462023-09-14 18:21:46 -060013#include <linux/printk.h>
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +030014
15#include <asm/io.h>
16
Alistair Delva4f2e2282021-10-20 21:31:34 +000017#define TYPE_PCI 0x1
18
Tuomas Tynkkynen3675cb02017-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 Olteanf83567e2020-03-13 16:53:06 +020025 pci_size_t size;
26 int first_busno;
Tuomas Tynkkynen3675cb02017-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 Glassc4e72c42020-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 Tynkkynen3675cb02017-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 Delva4f2e2282021-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 Tynkkynen3675cb02017-09-19 23:18:06 +030059 *paddress = addr;
60
61 return 0;
62}
63
Vladimir Olteanf83567e2020-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 Tynkkynen3675cb02017-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 Glassc4e72c42020-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 Tynkkynen3675cb02017-09-19 23:18:06 +030089{
Vladimir Olteanf83567e2020-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 Tynkkynen3675cb02017-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 Olteanf83567e2020-03-13 16:53:06 +0200115 if (!pci_generic_ecam_addr_valid(bus, bdf))
116 return 0;
117
Tuomas Tynkkynen3675cb02017-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 Glassd1998a92020-12-03 16:55:21 -0700123 * pci_generic_ecam_of_to_plat() - Translate from DT to device state
Tuomas Tynkkynen3675cb02017-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 Glassd1998a92020-12-03 16:55:21 -0700132static int pci_generic_ecam_of_to_plat(struct udevice *dev)
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +0300133{
134 struct generic_ecam_pcie *pcie = dev_get_priv(dev);
Maksim Kiselev67c7f142024-02-14 23:30:01 +0300135 ofnode node = dev_ofnode(dev);
136 struct resource reg_res;
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +0300137 int err;
138
Maksim Kiselev67c7f142024-02-14 23:30:01 +0300139 err = ofnode_read_resource(node, 0, &reg_res);
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +0300140 if (err < 0) {
141 pr_err("\"reg\" resource not found\n");
142 return err;
143 }
144
Maksim Kiselev67c7f142024-02-14 23:30:01 +0300145 pcie->size = resource_size(&reg_res);
Vladimir Olteanf83567e2020-03-13 16:53:06 +0200146 pcie->cfg_base = map_physmem(reg_res.start, pcie->size, MAP_NOCACHE);
147
148 return 0;
149}
150
151static int pci_generic_ecam_probe(struct udevice *dev)
152{
153 struct generic_ecam_pcie *pcie = dev_get_priv(dev);
154
Simon Glass8b85dfc2020-12-16 21:20:07 -0700155 pcie->first_busno = dev_seq(dev);
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +0300156
157 return 0;
158}
159
160static const struct dm_pci_ops pci_generic_ecam_ops = {
161 .read_config = pci_generic_ecam_read_config,
162 .write_config = pci_generic_ecam_write_config,
163};
164
165static const struct udevice_id pci_generic_ecam_ids[] = {
Alistair Delva4f2e2282021-10-20 21:31:34 +0000166 { .compatible = "pci-host-ecam-generic" /* PCI-E */ },
167 { .compatible = "pci-host-cam-generic", .data = TYPE_PCI },
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +0300168 { }
169};
170
171U_BOOT_DRIVER(pci_generic_ecam) = {
172 .name = "pci_generic_ecam",
173 .id = UCLASS_PCI,
174 .of_match = pci_generic_ecam_ids,
175 .ops = &pci_generic_ecam_ops,
Vladimir Olteanf83567e2020-03-13 16:53:06 +0200176 .probe = pci_generic_ecam_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700177 .of_to_plat = pci_generic_ecam_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700178 .priv_auto = sizeof(struct generic_ecam_pcie),
Tuomas Tynkkynen3675cb02017-09-19 23:18:06 +0300179};