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 | |
| 10 | #include <common.h> |
| 11 | #include <dm.h> |
| 12 | #include <pci.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 14 | #include <linux/printk.h> |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 15 | |
| 16 | #include <asm/io.h> |
| 17 | |
Alistair Delva | 2f285cd | 2021-10-20 21:31:34 +0000 | [diff] [blame] | 18 | #define TYPE_PCI 0x1 |
| 19 | |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 20 | /** |
| 21 | * struct generic_ecam_pcie - generic_ecam PCIe controller state |
| 22 | * @cfg_base: The base address of memory mapped configuration space |
| 23 | */ |
| 24 | struct generic_ecam_pcie { |
| 25 | void *cfg_base; |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 26 | pci_size_t size; |
| 27 | int first_busno; |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | /** |
| 31 | * pci_generic_ecam_conf_address() - Calculate the address of a config access |
| 32 | * @bus: Pointer to the PCI bus |
| 33 | * @bdf: Identifies the PCIe device to access |
| 34 | * @offset: The offset into the device's configuration space |
| 35 | * @paddress: Pointer to the pointer to write the calculates address to |
| 36 | * |
| 37 | * Calculates the address that should be accessed to perform a PCIe |
| 38 | * configuration space access for a given device identified by the PCIe |
| 39 | * controller device @pcie and the bus, device & function numbers in @bdf. If |
| 40 | * access to the device is not valid then the function will return an error |
| 41 | * code. Otherwise the address to access will be written to the pointer pointed |
| 42 | * to by @paddress. |
| 43 | */ |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 44 | static int pci_generic_ecam_conf_address(const struct udevice *bus, |
| 45 | pci_dev_t bdf, uint offset, |
| 46 | void **paddress) |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 47 | { |
| 48 | struct generic_ecam_pcie *pcie = dev_get_priv(bus); |
| 49 | void *addr; |
| 50 | |
| 51 | addr = pcie->cfg_base; |
Alistair Delva | 2f285cd | 2021-10-20 21:31:34 +0000 | [diff] [blame] | 52 | |
| 53 | if (dev_get_driver_data(bus) == TYPE_PCI) { |
| 54 | addr += ((PCI_BUS(bdf) - pcie->first_busno) << 16) | |
| 55 | (PCI_DEV(bdf) << 11) | (PCI_FUNC(bdf) << 8) | offset; |
| 56 | } else { |
| 57 | addr += PCIE_ECAM_OFFSET(PCI_BUS(bdf) - pcie->first_busno, |
| 58 | PCI_DEV(bdf), PCI_FUNC(bdf), offset); |
| 59 | } |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 60 | *paddress = addr; |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 65 | static bool pci_generic_ecam_addr_valid(const struct udevice *bus, |
| 66 | pci_dev_t bdf) |
| 67 | { |
| 68 | struct generic_ecam_pcie *pcie = dev_get_priv(bus); |
| 69 | int num_buses = DIV_ROUND_UP(pcie->size, 1 << 16); |
| 70 | |
| 71 | return (PCI_BUS(bdf) >= pcie->first_busno && |
| 72 | PCI_BUS(bdf) < pcie->first_busno + num_buses); |
| 73 | } |
| 74 | |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 75 | /** |
| 76 | * pci_generic_ecam_read_config() - Read from configuration space |
| 77 | * @bus: Pointer to the PCI bus |
| 78 | * @bdf: Identifies the PCIe device to access |
| 79 | * @offset: The offset into the device's configuration space |
| 80 | * @valuep: A pointer at which to store the read value |
| 81 | * @size: Indicates the size of access to perform |
| 82 | * |
| 83 | * Read a value of size @size from offset @offset within the configuration |
| 84 | * space of the device identified by the bus, device & function numbers in @bdf |
| 85 | * on the PCI bus @bus. |
| 86 | */ |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 87 | static int pci_generic_ecam_read_config(const struct udevice *bus, |
| 88 | pci_dev_t bdf, uint offset, |
| 89 | ulong *valuep, enum pci_size_t size) |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 90 | { |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 91 | if (!pci_generic_ecam_addr_valid(bus, bdf)) { |
| 92 | *valuep = pci_get_ff(size); |
| 93 | return 0; |
| 94 | } |
| 95 | |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 96 | return pci_generic_mmap_read_config(bus, pci_generic_ecam_conf_address, |
| 97 | bdf, offset, valuep, size); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * pci_generic_ecam_write_config() - Write to configuration space |
| 102 | * @bus: Pointer to the PCI bus |
| 103 | * @bdf: Identifies the PCIe device to access |
| 104 | * @offset: The offset into the device's configuration space |
| 105 | * @value: The value to write |
| 106 | * @size: Indicates the size of access to perform |
| 107 | * |
| 108 | * Write the value @value of size @size from offset @offset within the |
| 109 | * configuration space of the device identified by the bus, device & function |
| 110 | * numbers in @bdf on the PCI bus @bus. |
| 111 | */ |
| 112 | static int pci_generic_ecam_write_config(struct udevice *bus, pci_dev_t bdf, |
| 113 | uint offset, ulong value, |
| 114 | enum pci_size_t size) |
| 115 | { |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 116 | if (!pci_generic_ecam_addr_valid(bus, bdf)) |
| 117 | return 0; |
| 118 | |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 119 | return pci_generic_mmap_write_config(bus, pci_generic_ecam_conf_address, |
| 120 | bdf, offset, value, size); |
| 121 | } |
| 122 | |
| 123 | /** |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 124 | * pci_generic_ecam_of_to_plat() - Translate from DT to device state |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 125 | * @dev: A pointer to the device being operated on |
| 126 | * |
| 127 | * Translate relevant data from the device tree pertaining to device @dev into |
| 128 | * state that the driver will later make use of. This state is stored in the |
| 129 | * device's private data structure. |
| 130 | * |
| 131 | * Return: 0 on success, else -EINVAL |
| 132 | */ |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 133 | static int pci_generic_ecam_of_to_plat(struct udevice *dev) |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 134 | { |
| 135 | struct generic_ecam_pcie *pcie = dev_get_priv(dev); |
| 136 | struct fdt_resource reg_res; |
| 137 | DECLARE_GLOBAL_DATA_PTR; |
| 138 | int err; |
| 139 | |
| 140 | err = fdt_get_resource(gd->fdt_blob, dev_of_offset(dev), "reg", |
| 141 | 0, ®_res); |
| 142 | if (err < 0) { |
| 143 | pr_err("\"reg\" resource not found\n"); |
| 144 | return err; |
| 145 | } |
| 146 | |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 147 | pcie->size = fdt_resource_size(®_res); |
| 148 | pcie->cfg_base = map_physmem(reg_res.start, pcie->size, MAP_NOCACHE); |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 153 | static int pci_generic_ecam_probe(struct udevice *dev) |
| 154 | { |
| 155 | struct generic_ecam_pcie *pcie = dev_get_priv(dev); |
| 156 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 157 | pcie->first_busno = dev_seq(dev); |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 162 | static const struct dm_pci_ops pci_generic_ecam_ops = { |
| 163 | .read_config = pci_generic_ecam_read_config, |
| 164 | .write_config = pci_generic_ecam_write_config, |
| 165 | }; |
| 166 | |
| 167 | static const struct udevice_id pci_generic_ecam_ids[] = { |
Alistair Delva | 2f285cd | 2021-10-20 21:31:34 +0000 | [diff] [blame] | 168 | { .compatible = "pci-host-ecam-generic" /* PCI-E */ }, |
| 169 | { .compatible = "pci-host-cam-generic", .data = TYPE_PCI }, |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 170 | { } |
| 171 | }; |
| 172 | |
| 173 | U_BOOT_DRIVER(pci_generic_ecam) = { |
| 174 | .name = "pci_generic_ecam", |
| 175 | .id = UCLASS_PCI, |
| 176 | .of_match = pci_generic_ecam_ids, |
| 177 | .ops = &pci_generic_ecam_ops, |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 178 | .probe = pci_generic_ecam_probe, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 179 | .of_to_plat = pci_generic_ecam_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 180 | .priv_auto = sizeof(struct generic_ecam_pcie), |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 181 | }; |