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> |
| 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 | */ |
| 20 | struct generic_ecam_pcie { |
| 21 | void *cfg_base; |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 22 | pci_size_t size; |
| 23 | int first_busno; |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 24 | }; |
| 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 Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 40 | static int pci_generic_ecam_conf_address(const struct udevice *bus, |
| 41 | pci_dev_t bdf, uint offset, |
| 42 | void **paddress) |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 43 | { |
| 44 | struct generic_ecam_pcie *pcie = dev_get_priv(bus); |
| 45 | void *addr; |
| 46 | |
| 47 | addr = pcie->cfg_base; |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 48 | addr += (PCI_BUS(bdf) - pcie->first_busno) << 20; |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 49 | addr += PCI_DEV(bdf) << 15; |
| 50 | addr += PCI_FUNC(bdf) << 12; |
| 51 | addr += offset; |
| 52 | *paddress = addr; |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 57 | static 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 Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 67 | /** |
| 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 Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 79 | static 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 Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 82 | { |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 83 | if (!pci_generic_ecam_addr_valid(bus, bdf)) { |
| 84 | *valuep = pci_get_ff(size); |
| 85 | return 0; |
| 86 | } |
| 87 | |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 88 | 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 | */ |
| 104 | static 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 Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 108 | if (!pci_generic_ecam_addr_valid(bus, bdf)) |
| 109 | return 0; |
| 110 | |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 111 | return pci_generic_mmap_write_config(bus, pci_generic_ecam_conf_address, |
| 112 | bdf, offset, value, size); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * pci_generic_ecam_ofdata_to_platdata() - Translate from DT to device state |
| 117 | * @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 | */ |
| 125 | static int pci_generic_ecam_ofdata_to_platdata(struct udevice *dev) |
| 126 | { |
| 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, ®_res); |
| 134 | if (err < 0) { |
| 135 | pr_err("\"reg\" resource not found\n"); |
| 136 | return err; |
| 137 | } |
| 138 | |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 139 | pcie->size = fdt_resource_size(®_res); |
| 140 | pcie->cfg_base = map_physmem(reg_res.start, pcie->size, MAP_NOCACHE); |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Vladimir Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 145 | static int pci_generic_ecam_probe(struct udevice *dev) |
| 146 | { |
| 147 | struct generic_ecam_pcie *pcie = dev_get_priv(dev); |
| 148 | |
| 149 | pcie->first_busno = dev->seq; |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 154 | static 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 | |
| 159 | static const struct udevice_id pci_generic_ecam_ids[] = { |
| 160 | { .compatible = "pci-host-ecam-generic" }, |
| 161 | { } |
| 162 | }; |
| 163 | |
| 164 | U_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 Oltean | 34e46e0 | 2020-03-13 16:53:06 +0200 | [diff] [blame] | 169 | .probe = pci_generic_ecam_probe, |
Tuomas Tynkkynen | a765f71 | 2017-09-19 23:18:06 +0300 | [diff] [blame] | 170 | .ofdata_to_platdata = pci_generic_ecam_ofdata_to_platdata, |
| 171 | .priv_auto_alloc_size = sizeof(struct generic_ecam_pcie), |
| 172 | }; |