Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 2 | /* |
| 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 Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 8 | */ |
| 9 | |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <pci.h> |
Maksim Kiselev | ba92e89 | 2024-02-14 23:30:01 +0300 | [diff] [blame] | 12 | #include <linux/ioport.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 13 | #include <linux/printk.h> |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 14 | |
| 15 | #include <asm/io.h> |
| 16 | |
Alistair Delva | 2f285cd | 2021-10-20 21:31:34 +0000 | [diff] [blame] | 17 | #define TYPE_PCI 0x1 |
| 18 | |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 19 | /** |
| 20 | * struct generic_ecam_pcie - generic_ecam PCIe controller state |
| 21 | * @cfg_base: The base address of memory mapped configuration space |
| 22 | */ |
| 23 | struct generic_ecam_pcie { |
| 24 | void *cfg_base; |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 25 | pci_size_t size; |
| 26 | int first_busno; |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 27 | }; |
| 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 Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 43 | static int pci_generic_ecam_conf_address(const struct udevice *bus, |
| 44 | pci_dev_t bdf, uint offset, |
| 45 | void **paddress) |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 46 | { |
| 47 | struct generic_ecam_pcie *pcie = dev_get_priv(bus); |
| 48 | void *addr; |
| 49 | |
| 50 | addr = pcie->cfg_base; |
Alistair Delva | 2f285cd | 2021-10-20 21:31:34 +0000 | [diff] [blame] | 51 | |
| 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 Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 59 | *paddress = addr; |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 64 | static 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 Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 74 | /** |
| 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 Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 86 | static 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 Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 89 | { |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 90 | if (!pci_generic_ecam_addr_valid(bus, bdf)) { |
| 91 | *valuep = pci_get_ff(size); |
| 92 | return 0; |
| 93 | } |
| 94 | |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 95 | 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 | */ |
| 111 | static 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 Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 115 | if (!pci_generic_ecam_addr_valid(bus, bdf)) |
| 116 | return 0; |
| 117 | |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 118 | return pci_generic_mmap_write_config(bus, pci_generic_ecam_conf_address, |
| 119 | bdf, offset, value, size); |
| 120 | } |
| 121 | |
| 122 | /** |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 123 | * pci_generic_ecam_of_to_plat() - Translate from DT to device state |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 124 | * @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 Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 132 | static int pci_generic_ecam_of_to_plat(struct udevice *dev) |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 133 | { |
| 134 | struct generic_ecam_pcie *pcie = dev_get_priv(dev); |
Maksim Kiselev | ba92e89 | 2024-02-14 23:30:01 +0300 | [diff] [blame] | 135 | ofnode node = dev_ofnode(dev); |
| 136 | struct resource reg_res; |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 137 | int err; |
| 138 | |
Maksim Kiselev | ba92e89 | 2024-02-14 23:30:01 +0300 | [diff] [blame] | 139 | err = ofnode_read_resource(node, 0, ®_res); |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 140 | if (err < 0) { |
| 141 | pr_err("\"reg\" resource not found\n"); |
| 142 | return err; |
| 143 | } |
| 144 | |
Maksim Kiselev | ba92e89 | 2024-02-14 23:30:01 +0300 | [diff] [blame] | 145 | pcie->size = resource_size(®_res); |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 146 | pcie->cfg_base = map_physmem(reg_res.start, pcie->size, MAP_NOCACHE); |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 151 | static int pci_generic_ecam_probe(struct udevice *dev) |
| 152 | { |
| 153 | struct generic_ecam_pcie *pcie = dev_get_priv(dev); |
| 154 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 155 | pcie->first_busno = dev_seq(dev); |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 160 | static 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 | |
| 165 | static const struct udevice_id pci_generic_ecam_ids[] = { |
Alistair Delva | 2f285cd | 2021-10-20 21:31:34 +0000 | [diff] [blame] | 166 | { .compatible = "pci-host-ecam-generic" /* PCI-E */ }, |
| 167 | { .compatible = "pci-host-cam-generic", .data = TYPE_PCI }, |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 168 | { } |
| 169 | }; |
| 170 | |
| 171 | U_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 Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 176 | .probe = pci_generic_ecam_probe, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 177 | .of_to_plat = pci_generic_ecam_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 178 | .priv_auto = sizeof(struct generic_ecam_pcie), |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 179 | }; |