1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (c) 2017 Cadence 3 // Cadence PCIe host controller driver. 4 // Author: Cyrille Pitchen <cyrille.pitchen@free-electrons.com> 5 6 #include <linux/kernel.h> 7 #include <linux/of_address.h> 8 #include <linux/of_pci.h> 9 #include <linux/platform_device.h> 10 #include <linux/pm_runtime.h> 11 12 #include "pcie-cadence.h" 13 14 static void __iomem *cdns_pci_map_bus(struct pci_bus *bus, unsigned int devfn, 15 int where) 16 { 17 struct pci_host_bridge *bridge = pci_find_host_bridge(bus); 18 struct cdns_pcie_rc *rc = pci_host_bridge_priv(bridge); 19 struct cdns_pcie *pcie = &rc->pcie; 20 unsigned int busn = bus->number; 21 u32 addr0, desc0; 22 23 if (busn == rc->bus_range->start) { 24 /* 25 * Only the root port (devfn == 0) is connected to this bus. 26 * All other PCI devices are behind some bridge hence on another 27 * bus. 28 */ 29 if (devfn) 30 return NULL; 31 32 return pcie->reg_base + (where & 0xfff); 33 } 34 /* Check that the link is up */ 35 if (!(cdns_pcie_readl(pcie, CDNS_PCIE_LM_BASE) & 0x1)) 36 return NULL; 37 /* Clear AXI link-down status */ 38 cdns_pcie_writel(pcie, CDNS_PCIE_AT_LINKDOWN, 0x0); 39 40 /* Update Output registers for AXI region 0. */ 41 addr0 = CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(12) | 42 CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_DEVFN(devfn) | 43 CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_BUS(busn); 44 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(0), addr0); 45 46 /* Configuration Type 0 or Type 1 access. */ 47 desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID | 48 CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0); 49 /* 50 * The bus number was already set once for all in desc1 by 51 * cdns_pcie_host_init_address_translation(). 52 */ 53 if (busn == rc->bus_range->start + 1) 54 desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE0; 55 else 56 desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_CONF_TYPE1; 57 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(0), desc0); 58 59 return rc->cfg_base + (where & 0xfff); 60 } 61 62 static struct pci_ops cdns_pcie_host_ops = { 63 .map_bus = cdns_pci_map_bus, 64 .read = pci_generic_config_read, 65 .write = pci_generic_config_write, 66 }; 67 68 69 static int cdns_pcie_host_init_root_port(struct cdns_pcie_rc *rc) 70 { 71 struct cdns_pcie *pcie = &rc->pcie; 72 u32 value, ctrl; 73 74 /* 75 * Set the root complex BAR configuration register: 76 * - disable both BAR0 and BAR1. 77 * - enable Prefetchable Memory Base and Limit registers in type 1 78 * config space (64 bits). 79 * - enable IO Base and Limit registers in type 1 config 80 * space (32 bits). 81 */ 82 ctrl = CDNS_PCIE_LM_BAR_CFG_CTRL_DISABLED; 83 value = CDNS_PCIE_LM_RC_BAR_CFG_BAR0_CTRL(ctrl) | 84 CDNS_PCIE_LM_RC_BAR_CFG_BAR1_CTRL(ctrl) | 85 CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_ENABLE | 86 CDNS_PCIE_LM_RC_BAR_CFG_PREFETCH_MEM_64BITS | 87 CDNS_PCIE_LM_RC_BAR_CFG_IO_ENABLE | 88 CDNS_PCIE_LM_RC_BAR_CFG_IO_32BITS; 89 cdns_pcie_writel(pcie, CDNS_PCIE_LM_RC_BAR_CFG, value); 90 91 /* Set root port configuration space */ 92 if (rc->vendor_id != 0xffff) 93 cdns_pcie_rp_writew(pcie, PCI_VENDOR_ID, rc->vendor_id); 94 if (rc->device_id != 0xffff) 95 cdns_pcie_rp_writew(pcie, PCI_DEVICE_ID, rc->device_id); 96 97 cdns_pcie_rp_writeb(pcie, PCI_CLASS_REVISION, 0); 98 cdns_pcie_rp_writeb(pcie, PCI_CLASS_PROG, 0); 99 cdns_pcie_rp_writew(pcie, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI); 100 101 return 0; 102 } 103 104 static int cdns_pcie_host_init_address_translation(struct cdns_pcie_rc *rc) 105 { 106 struct cdns_pcie *pcie = &rc->pcie; 107 struct resource *mem_res = pcie->mem_res; 108 struct resource *bus_range = rc->bus_range; 109 struct resource *cfg_res = rc->cfg_res; 110 struct device *dev = pcie->dev; 111 struct device_node *np = dev->of_node; 112 struct of_pci_range_parser parser; 113 struct of_pci_range range; 114 u32 addr0, addr1, desc1; 115 u64 cpu_addr; 116 int r, err; 117 118 /* 119 * Reserve region 0 for PCI configure space accesses: 120 * OB_REGION_PCI_ADDR0 and OB_REGION_DESC0 are updated dynamically by 121 * cdns_pci_map_bus(), other region registers are set here once for all. 122 */ 123 addr1 = 0; /* Should be programmed to zero. */ 124 desc1 = CDNS_PCIE_AT_OB_REGION_DESC1_BUS(bus_range->start); 125 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(0), addr1); 126 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(0), desc1); 127 128 cpu_addr = cfg_res->start - mem_res->start; 129 addr0 = CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(12) | 130 (lower_32_bits(cpu_addr) & GENMASK(31, 8)); 131 addr1 = upper_32_bits(cpu_addr); 132 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(0), addr0); 133 cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(0), addr1); 134 135 err = of_pci_range_parser_init(&parser, np); 136 if (err) 137 return err; 138 139 r = 1; 140 for_each_of_pci_range(&parser, &range) { 141 bool is_io; 142 143 if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM) 144 is_io = false; 145 else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO) 146 is_io = true; 147 else 148 continue; 149 150 cdns_pcie_set_outbound_region(pcie, 0, r, is_io, 151 range.cpu_addr, 152 range.pci_addr, 153 range.size); 154 r++; 155 } 156 157 /* 158 * Set Root Port no BAR match Inbound Translation registers: 159 * needed for MSI and DMA. 160 * Root Port BAR0 and BAR1 are disabled, hence no need to set their 161 * inbound translation registers. 162 */ 163 addr0 = CDNS_PCIE_AT_IB_RP_BAR_ADDR0_NBITS(rc->no_bar_nbits); 164 addr1 = 0; 165 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_RP_BAR_ADDR0(RP_NO_BAR), addr0); 166 cdns_pcie_writel(pcie, CDNS_PCIE_AT_IB_RP_BAR_ADDR1(RP_NO_BAR), addr1); 167 168 return 0; 169 } 170 171 static int cdns_pcie_host_init(struct device *dev, 172 struct list_head *resources, 173 struct cdns_pcie_rc *rc) 174 { 175 struct resource *bus_range = NULL; 176 int err; 177 178 /* Parse our PCI ranges and request their resources */ 179 err = pci_parse_request_of_pci_ranges(dev, resources, NULL, &bus_range); 180 if (err) 181 return err; 182 183 rc->bus_range = bus_range; 184 rc->pcie.bus = bus_range->start; 185 186 err = cdns_pcie_host_init_root_port(rc); 187 if (err) 188 goto err_out; 189 190 err = cdns_pcie_host_init_address_translation(rc); 191 if (err) 192 goto err_out; 193 194 return 0; 195 196 err_out: 197 pci_free_resource_list(resources); 198 return err; 199 } 200 201 int cdns_pcie_host_setup(struct cdns_pcie_rc *rc) 202 { 203 struct device *dev = rc->pcie.dev; 204 struct platform_device *pdev = to_platform_device(dev); 205 struct device_node *np = dev->of_node; 206 struct pci_host_bridge *bridge; 207 struct list_head resources; 208 struct cdns_pcie *pcie; 209 struct resource *res; 210 int ret; 211 212 bridge = pci_host_bridge_from_priv(rc); 213 if (!bridge) 214 return -ENOMEM; 215 216 pcie = &rc->pcie; 217 pcie->is_rc = true; 218 219 rc->no_bar_nbits = 32; 220 of_property_read_u32(np, "cdns,no-bar-match-nbits", &rc->no_bar_nbits); 221 222 rc->vendor_id = 0xffff; 223 of_property_read_u32(np, "vendor-id", &rc->vendor_id); 224 225 rc->device_id = 0xffff; 226 of_property_read_u32(np, "device-id", &rc->device_id); 227 228 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "reg"); 229 pcie->reg_base = devm_ioremap_resource(dev, res); 230 if (IS_ERR(pcie->reg_base)) { 231 dev_err(dev, "missing \"reg\"\n"); 232 return PTR_ERR(pcie->reg_base); 233 } 234 235 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg"); 236 rc->cfg_base = devm_pci_remap_cfg_resource(dev, res); 237 if (IS_ERR(rc->cfg_base)) { 238 dev_err(dev, "missing \"cfg\"\n"); 239 return PTR_ERR(rc->cfg_base); 240 } 241 rc->cfg_res = res; 242 243 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem"); 244 if (!res) { 245 dev_err(dev, "missing \"mem\"\n"); 246 return -EINVAL; 247 } 248 249 pcie->mem_res = res; 250 251 ret = cdns_pcie_host_init(dev, &resources, rc); 252 if (ret) 253 goto err_init; 254 255 list_splice_init(&resources, &bridge->windows); 256 bridge->dev.parent = dev; 257 bridge->busnr = pcie->bus; 258 bridge->ops = &cdns_pcie_host_ops; 259 bridge->map_irq = of_irq_parse_and_map_pci; 260 bridge->swizzle_irq = pci_common_swizzle; 261 262 ret = pci_host_probe(bridge); 263 if (ret < 0) 264 goto err_host_probe; 265 266 return 0; 267 268 err_host_probe: 269 pci_free_resource_list(&resources); 270 271 err_init: 272 pm_runtime_put_sync(dev); 273 274 return ret; 275 } 276