blob: de195fd430dc02ad8242bf237ddd2b55703de5c6 [file] [log] [blame]
Bjorn Helgaas736759e2018-01-26 14:22:04 -06001// SPDX-License-Identifier: GPL-2.0+
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -06002/**
3 * APM X-Gene PCIe Driver
4 *
5 * Copyright (c) 2014 Applied Micro Circuits Corporation.
6 *
7 * Author: Tanmay Inamdar <tinamdar@apm.com>.
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -06008 */
Stephen Boyd29ef7092015-01-22 11:25:54 -08009#include <linux/clk.h>
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -060010#include <linux/delay.h>
11#include <linux/io.h>
12#include <linux/jiffies.h>
13#include <linux/memblock.h>
Paul Gortmaker50dcd292016-07-02 19:13:34 -040014#include <linux/init.h>
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -060015#include <linux/of.h>
16#include <linux/of_address.h>
17#include <linux/of_irq.h>
18#include <linux/of_pci.h>
19#include <linux/pci.h>
Duc Dangc5d46032016-12-01 18:27:07 -080020#include <linux/pci-acpi.h>
21#include <linux/pci-ecam.h>
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -060022#include <linux/platform_device.h>
23#include <linux/slab.h>
24
Rob Herring9e2aee82018-05-11 12:15:30 -050025#include "../pci.h"
26
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -060027#define PCIECORE_CTLANDSTATUS 0x50
28#define PIM1_1L 0x80
29#define IBAR2 0x98
30#define IR2MSK 0x9c
31#define PIM2_1L 0xa0
32#define IBAR3L 0xb4
33#define IR3MSKL 0xbc
34#define PIM3_1L 0xc4
35#define OMR1BARL 0x100
36#define OMR2BARL 0x118
37#define OMR3BARL 0x130
38#define CFGBARL 0x154
39#define CFGBARH 0x158
40#define CFGCTL 0x15c
41#define RTDID 0x160
42#define BRIDGE_CFG_0 0x2000
43#define BRIDGE_CFG_4 0x2010
44#define BRIDGE_STATUS_0 0x2600
45
46#define LINK_UP_MASK 0x00000100
47#define AXI_EP_CFG_ACCESS 0x10000
48#define EN_COHERENCY 0xF0000000
49#define EN_REG 0x00000001
50#define OB_LO_IO 0x00000002
51#define XGENE_PCIE_VENDORID 0x10E8
52#define XGENE_PCIE_DEVICEID 0xE004
53#define SZ_1T (SZ_1G*1024ULL)
54#define PIPE_PHY_RATE_RD(src) ((0xc000 & (u32)(src)) >> 0xe)
55
Bjorn Helgaas582ffae2017-09-05 12:58:03 -050056#define XGENE_V1_PCI_EXP_CAP 0x40
Duc Dangf09f8732015-06-12 17:35:57 -070057
58/* PCIe IP version */
59#define XGENE_PCIE_IP_VER_UNKN 0
60#define XGENE_PCIE_IP_VER_1 1
Duc Dangc5d46032016-12-01 18:27:07 -080061#define XGENE_PCIE_IP_VER_2 2
Duc Dangf09f8732015-06-12 17:35:57 -070062
Duc Dangc5d46032016-12-01 18:27:07 -080063#if defined(CONFIG_PCI_XGENE) || (defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS))
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -060064struct xgene_pcie_port {
65 struct device_node *node;
66 struct device *dev;
67 struct clk *clk;
68 void __iomem *csr_base;
69 void __iomem *cfg_base;
70 unsigned long cfg_addr;
71 bool link_up;
Duc Dangf09f8732015-06-12 17:35:57 -070072 u32 version;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -060073};
74
Bjorn Helgaas8e93c512016-10-06 13:43:42 -050075static u32 xgene_pcie_readl(struct xgene_pcie_port *port, u32 reg)
76{
77 return readl(port->csr_base + reg);
78}
79
80static void xgene_pcie_writel(struct xgene_pcie_port *port, u32 reg, u32 val)
81{
82 writel(val, port->csr_base + reg);
83}
84
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -060085static inline u32 pcie_bar_low_val(u32 addr, u32 flags)
86{
87 return (addr & PCI_BASE_ADDRESS_MEM_MASK) | flags;
88}
89
Duc Dangc5d46032016-12-01 18:27:07 -080090static inline struct xgene_pcie_port *pcie_bus_to_port(struct pci_bus *bus)
91{
92 struct pci_config_window *cfg;
93
94 if (acpi_disabled)
95 return (struct xgene_pcie_port *)(bus->sysdata);
96
97 cfg = bus->sysdata;
98 return (struct xgene_pcie_port *)(cfg->priv);
99}
100
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600101/*
102 * When the address bit [17:16] is 2'b01, the Configuration access will be
103 * treated as Type 1 and it will be forwarded to external PCIe device.
104 */
105static void __iomem *xgene_pcie_get_cfg_base(struct pci_bus *bus)
106{
Duc Dangc5d46032016-12-01 18:27:07 -0800107 struct xgene_pcie_port *port = pcie_bus_to_port(bus);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600108
109 if (bus->number >= (bus->primary + 1))
110 return port->cfg_base + AXI_EP_CFG_ACCESS;
111
112 return port->cfg_base;
113}
114
115/*
116 * For Configuration request, RTDID register is used as Bus Number,
117 * Device Number and Function number of the header fields.
118 */
119static void xgene_pcie_set_rtdid_reg(struct pci_bus *bus, uint devfn)
120{
Duc Dangc5d46032016-12-01 18:27:07 -0800121 struct xgene_pcie_port *port = pcie_bus_to_port(bus);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600122 unsigned int b, d, f;
123 u32 rtdid_val = 0;
124
125 b = bus->number;
126 d = PCI_SLOT(devfn);
127 f = PCI_FUNC(devfn);
128
129 if (!pci_is_root_bus(bus))
130 rtdid_val = (b << 8) | (d << 3) | f;
131
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500132 xgene_pcie_writel(port, RTDID, rtdid_val);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600133 /* read the register back to ensure flush */
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500134 xgene_pcie_readl(port, RTDID);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600135}
136
137/*
138 * X-Gene PCIe port uses BAR0-BAR1 of RC's configuration space as
139 * the translation from PCI bus to native BUS. Entire DDR region
140 * is mapped into PCIe space using these registers, so it can be
141 * reached by DMA from EP devices. The BAR0/1 of bridge should be
142 * hidden during enumeration to avoid the sizing and resource allocation
143 * by PCIe core.
144 */
145static bool xgene_pcie_hide_rc_bars(struct pci_bus *bus, int offset)
146{
147 if (pci_is_root_bus(bus) && ((offset == PCI_BASE_ADDRESS_0) ||
148 (offset == PCI_BASE_ADDRESS_1)))
149 return true;
150
151 return false;
152}
153
Feng Kan085a68d2015-02-17 15:14:00 -0800154static void __iomem *xgene_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
Bjorn Helgaasfca48482017-09-05 13:09:05 -0500155 int offset)
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600156{
Duc Dangae4fa5f2015-06-18 11:45:39 -0700157 if ((pci_is_root_bus(bus) && devfn != 0) ||
Rob Herring350f8be2015-01-09 20:34:49 -0600158 xgene_pcie_hide_rc_bars(bus, offset))
159 return NULL;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600160
161 xgene_pcie_set_rtdid_reg(bus, devfn);
Feng Kan085a68d2015-02-17 15:14:00 -0800162 return xgene_pcie_get_cfg_base(bus) + offset;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600163}
164
Duc Dangf09f8732015-06-12 17:35:57 -0700165static int xgene_pcie_config_read32(struct pci_bus *bus, unsigned int devfn,
166 int where, int size, u32 *val)
167{
Duc Dangc5d46032016-12-01 18:27:07 -0800168 struct xgene_pcie_port *port = pcie_bus_to_port(bus);
Duc Dangf09f8732015-06-12 17:35:57 -0700169
170 if (pci_generic_config_read32(bus, devfn, where & ~0x3, 4, val) !=
171 PCIBIOS_SUCCESSFUL)
172 return PCIBIOS_DEVICE_NOT_FOUND;
173
174 /*
175 * The v1 controller has a bug in its Configuration Request
176 * Retry Status (CRS) logic: when CRS is enabled and we read the
177 * Vendor and Device ID of a non-existent device, the controller
178 * fabricates return data of 0xFFFF0001 ("device exists but is not
179 * ready") instead of 0xFFFFFFFF ("device does not exist"). This
180 * causes the PCI core to retry the read until it times out.
181 * Avoid this by not claiming to support CRS.
182 */
183 if (pci_is_root_bus(bus) && (port->version == XGENE_PCIE_IP_VER_1) &&
Bjorn Helgaas582ffae2017-09-05 12:58:03 -0500184 ((where & ~0x3) == XGENE_V1_PCI_EXP_CAP + PCI_EXP_RTCTL))
Duc Dangf09f8732015-06-12 17:35:57 -0700185 *val &= ~(PCI_EXP_RTCAP_CRSVIS << 16);
186
187 if (size <= 2)
188 *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
189
190 return PCIBIOS_SUCCESSFUL;
191}
Duc Dangc5d46032016-12-01 18:27:07 -0800192#endif
Duc Dangf09f8732015-06-12 17:35:57 -0700193
Duc Dangc5d46032016-12-01 18:27:07 -0800194#if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)
195static int xgene_get_csr_resource(struct acpi_device *adev,
196 struct resource *res)
197{
198 struct device *dev = &adev->dev;
199 struct resource_entry *entry;
200 struct list_head list;
201 unsigned long flags;
202 int ret;
203
204 INIT_LIST_HEAD(&list);
205 flags = IORESOURCE_MEM;
206 ret = acpi_dev_get_resources(adev, &list,
207 acpi_dev_filter_resource_type_cb,
208 (void *) flags);
209 if (ret < 0) {
210 dev_err(dev, "failed to parse _CRS method, error code %d\n",
211 ret);
212 return ret;
213 }
214
215 if (ret == 0) {
216 dev_err(dev, "no IO and memory resources present in _CRS\n");
217 return -EINVAL;
218 }
219
220 entry = list_first_entry(&list, struct resource_entry, node);
221 *res = *entry->res;
222 acpi_dev_free_resource_list(&list);
223 return 0;
224}
225
226static int xgene_pcie_ecam_init(struct pci_config_window *cfg, u32 ipversion)
227{
228 struct device *dev = cfg->parent;
229 struct acpi_device *adev = to_acpi_device(dev);
230 struct xgene_pcie_port *port;
231 struct resource csr;
232 int ret;
233
234 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
235 if (!port)
236 return -ENOMEM;
237
238 ret = xgene_get_csr_resource(adev, &csr);
239 if (ret) {
240 dev_err(dev, "can't get CSR resource\n");
Duc Dangc5d46032016-12-01 18:27:07 -0800241 return ret;
242 }
Lorenzo Pieralisi26b758f2017-04-19 17:49:05 +0100243 port->csr_base = devm_pci_remap_cfg_resource(dev, &csr);
Dan Carpenter1ded56d2017-01-21 07:49:49 +0300244 if (IS_ERR(port->csr_base))
245 return PTR_ERR(port->csr_base);
Duc Dangc5d46032016-12-01 18:27:07 -0800246
247 port->cfg_base = cfg->win;
248 port->version = ipversion;
249
250 cfg->priv = port;
251 return 0;
252}
253
254static int xgene_v1_pcie_ecam_init(struct pci_config_window *cfg)
255{
256 return xgene_pcie_ecam_init(cfg, XGENE_PCIE_IP_VER_1);
257}
258
259struct pci_ecam_ops xgene_v1_pcie_ecam_ops = {
Bjorn Helgaasfca48482017-09-05 13:09:05 -0500260 .bus_shift = 16,
261 .init = xgene_v1_pcie_ecam_init,
262 .pci_ops = {
263 .map_bus = xgene_pcie_map_bus,
264 .read = xgene_pcie_config_read32,
265 .write = pci_generic_config_write,
Duc Dangc5d46032016-12-01 18:27:07 -0800266 }
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600267};
268
Duc Dangc5d46032016-12-01 18:27:07 -0800269static int xgene_v2_pcie_ecam_init(struct pci_config_window *cfg)
270{
271 return xgene_pcie_ecam_init(cfg, XGENE_PCIE_IP_VER_2);
272}
273
274struct pci_ecam_ops xgene_v2_pcie_ecam_ops = {
Bjorn Helgaasfca48482017-09-05 13:09:05 -0500275 .bus_shift = 16,
276 .init = xgene_v2_pcie_ecam_init,
277 .pci_ops = {
278 .map_bus = xgene_pcie_map_bus,
279 .read = xgene_pcie_config_read32,
280 .write = pci_generic_config_write,
Duc Dangc5d46032016-12-01 18:27:07 -0800281 }
282};
283#endif
284
285#if defined(CONFIG_PCI_XGENE)
Bjorn Helgaas4ecf6b02016-10-06 13:43:41 -0500286static u64 xgene_pcie_set_ib_mask(struct xgene_pcie_port *port, u32 addr,
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600287 u32 flags, u64 size)
288{
289 u64 mask = (~(size - 1) & PCI_BASE_ADDRESS_MEM_MASK) | flags;
290 u32 val32 = 0;
291 u32 val;
292
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500293 val32 = xgene_pcie_readl(port, addr);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600294 val = (val32 & 0x0000ffff) | (lower_32_bits(mask) << 16);
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500295 xgene_pcie_writel(port, addr, val);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600296
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500297 val32 = xgene_pcie_readl(port, addr + 0x04);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600298 val = (val32 & 0xffff0000) | (lower_32_bits(mask) >> 16);
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500299 xgene_pcie_writel(port, addr + 0x04, val);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600300
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500301 val32 = xgene_pcie_readl(port, addr + 0x04);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600302 val = (val32 & 0x0000ffff) | (upper_32_bits(mask) << 16);
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500303 xgene_pcie_writel(port, addr + 0x04, val);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600304
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500305 val32 = xgene_pcie_readl(port, addr + 0x08);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600306 val = (val32 & 0xffff0000) | (upper_32_bits(mask) >> 16);
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500307 xgene_pcie_writel(port, addr + 0x08, val);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600308
309 return mask;
310}
311
312static void xgene_pcie_linkup(struct xgene_pcie_port *port,
Bjorn Helgaasfca48482017-09-05 13:09:05 -0500313 u32 *lanes, u32 *speed)
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600314{
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600315 u32 val32;
316
317 port->link_up = false;
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500318 val32 = xgene_pcie_readl(port, PCIECORE_CTLANDSTATUS);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600319 if (val32 & LINK_UP_MASK) {
320 port->link_up = true;
321 *speed = PIPE_PHY_RATE_RD(val32);
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500322 val32 = xgene_pcie_readl(port, BRIDGE_STATUS_0);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600323 *lanes = val32 >> 26;
324 }
325}
326
327static int xgene_pcie_init_port(struct xgene_pcie_port *port)
328{
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500329 struct device *dev = port->dev;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600330 int rc;
331
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500332 port->clk = clk_get(dev, NULL);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600333 if (IS_ERR(port->clk)) {
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500334 dev_err(dev, "clock not available\n");
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600335 return -ENODEV;
336 }
337
338 rc = clk_prepare_enable(port->clk);
339 if (rc) {
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500340 dev_err(dev, "clock enable failed\n");
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600341 return rc;
342 }
343
344 return 0;
345}
346
347static int xgene_pcie_map_reg(struct xgene_pcie_port *port,
348 struct platform_device *pdev)
349{
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500350 struct device *dev = port->dev;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600351 struct resource *res;
352
353 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csr");
Lorenzo Pieralisi26b758f2017-04-19 17:49:05 +0100354 port->csr_base = devm_pci_remap_cfg_resource(dev, res);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600355 if (IS_ERR(port->csr_base))
356 return PTR_ERR(port->csr_base);
357
358 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500359 port->cfg_base = devm_ioremap_resource(dev, res);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600360 if (IS_ERR(port->cfg_base))
361 return PTR_ERR(port->cfg_base);
362 port->cfg_addr = res->start;
363
364 return 0;
365}
366
367static void xgene_pcie_setup_ob_reg(struct xgene_pcie_port *port,
368 struct resource *res, u32 offset,
369 u64 cpu_addr, u64 pci_addr)
370{
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500371 struct device *dev = port->dev;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600372 resource_size_t size = resource_size(res);
373 u64 restype = resource_type(res);
374 u64 mask = 0;
375 u32 min_size;
376 u32 flag = EN_REG;
377
378 if (restype == IORESOURCE_MEM) {
379 min_size = SZ_128M;
380 } else {
381 min_size = 128;
382 flag |= OB_LO_IO;
383 }
384
385 if (size >= min_size)
386 mask = ~(size - 1) | flag;
387 else
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500388 dev_warn(dev, "res size 0x%llx less than minimum 0x%x\n",
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600389 (u64)size, min_size);
390
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500391 xgene_pcie_writel(port, offset, lower_32_bits(cpu_addr));
392 xgene_pcie_writel(port, offset + 0x04, upper_32_bits(cpu_addr));
393 xgene_pcie_writel(port, offset + 0x08, lower_32_bits(mask));
394 xgene_pcie_writel(port, offset + 0x0c, upper_32_bits(mask));
395 xgene_pcie_writel(port, offset + 0x10, lower_32_bits(pci_addr));
396 xgene_pcie_writel(port, offset + 0x14, upper_32_bits(pci_addr));
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600397}
398
Bjorn Helgaas4ecf6b02016-10-06 13:43:41 -0500399static void xgene_pcie_setup_cfg_reg(struct xgene_pcie_port *port)
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600400{
Bjorn Helgaas4ecf6b02016-10-06 13:43:41 -0500401 u64 addr = port->cfg_addr;
402
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500403 xgene_pcie_writel(port, CFGBARL, lower_32_bits(addr));
404 xgene_pcie_writel(port, CFGBARH, upper_32_bits(addr));
405 xgene_pcie_writel(port, CFGCTL, EN_REG);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600406}
407
Rob Herring83083e22019-10-28 11:32:44 -0500408static int xgene_pcie_map_ranges(struct xgene_pcie_port *port)
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600409{
Rob Herring83083e22019-10-28 11:32:44 -0500410 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(port);
Jiang Liu14d76b62015-02-05 13:44:44 +0800411 struct resource_entry *window;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600412 struct device *dev = port->dev;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600413
Rob Herring83083e22019-10-28 11:32:44 -0500414 resource_list_for_each_entry(window, &bridge->windows) {
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600415 struct resource *res = window->res;
416 u64 restype = resource_type(res);
417
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500418 dev_dbg(dev, "%pR\n", res);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600419
420 switch (restype) {
421 case IORESOURCE_IO:
Rob Herring83083e22019-10-28 11:32:44 -0500422 xgene_pcie_setup_ob_reg(port, res, OMR3BARL,
423 pci_pio_to_address(res->start),
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600424 res->start - window->offset);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600425 break;
426 case IORESOURCE_MEM:
Duc Dang8ef54f22015-07-09 14:20:12 -0700427 if (res->flags & IORESOURCE_PREFETCH)
428 xgene_pcie_setup_ob_reg(port, res, OMR2BARL,
429 res->start,
430 res->start -
431 window->offset);
432 else
433 xgene_pcie_setup_ob_reg(port, res, OMR1BARL,
434 res->start,
435 res->start -
436 window->offset);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600437 break;
438 case IORESOURCE_BUS:
439 break;
440 default:
441 dev_err(dev, "invalid resource %pR\n", res);
442 return -EINVAL;
443 }
444 }
Bjorn Helgaas4ecf6b02016-10-06 13:43:41 -0500445 xgene_pcie_setup_cfg_reg(port);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600446 return 0;
447}
448
Bjorn Helgaas4ecf6b02016-10-06 13:43:41 -0500449static void xgene_pcie_setup_pims(struct xgene_pcie_port *port, u32 pim_reg,
450 u64 pim, u64 size)
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600451{
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500452 xgene_pcie_writel(port, pim_reg, lower_32_bits(pim));
453 xgene_pcie_writel(port, pim_reg + 0x04,
454 upper_32_bits(pim) | EN_COHERENCY);
455 xgene_pcie_writel(port, pim_reg + 0x10, lower_32_bits(size));
456 xgene_pcie_writel(port, pim_reg + 0x14, upper_32_bits(size));
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600457}
458
459/*
460 * X-Gene PCIe support maximum 3 inbound memory regions
461 * This function helps to select a region based on size of region
462 */
463static int xgene_pcie_select_ib_reg(u8 *ib_reg_mask, u64 size)
464{
465 if ((size > 4) && (size < SZ_16M) && !(*ib_reg_mask & (1 << 1))) {
466 *ib_reg_mask |= (1 << 1);
467 return 1;
468 }
469
470 if ((size > SZ_1K) && (size < SZ_1T) && !(*ib_reg_mask & (1 << 0))) {
471 *ib_reg_mask |= (1 << 0);
472 return 0;
473 }
474
475 if ((size > SZ_1M) && (size < SZ_1T) && !(*ib_reg_mask & (1 << 2))) {
476 *ib_reg_mask |= (1 << 2);
477 return 2;
478 }
479
480 return -EINVAL;
481}
482
483static void xgene_pcie_setup_ib_reg(struct xgene_pcie_port *port,
Rob Herring6dce5aa52019-10-28 11:32:53 -0500484 struct resource_entry *entry,
485 u8 *ib_reg_mask)
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600486{
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600487 void __iomem *cfg_base = port->cfg_base;
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500488 struct device *dev = port->dev;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600489 void *bar_addr;
Bjorn Helgaas4ecf6b02016-10-06 13:43:41 -0500490 u32 pim_reg;
Rob Herring6dce5aa52019-10-28 11:32:53 -0500491 u64 cpu_addr = entry->res->start;
492 u64 pci_addr = cpu_addr - entry->offset;
493 u64 size = resource_size(entry->res);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600494 u64 mask = ~(size - 1) | EN_REG;
495 u32 flags = PCI_BASE_ADDRESS_MEM_TYPE_64;
496 u32 bar_low;
497 int region;
498
Rob Herring6dce5aa52019-10-28 11:32:53 -0500499 region = xgene_pcie_select_ib_reg(ib_reg_mask, size);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600500 if (region < 0) {
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500501 dev_warn(dev, "invalid pcie dma-range config\n");
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600502 return;
503 }
504
Rob Herring6dce5aa52019-10-28 11:32:53 -0500505 if (entry->res->flags & IORESOURCE_PREFETCH)
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600506 flags |= PCI_BASE_ADDRESS_MEM_PREFETCH;
507
508 bar_low = pcie_bar_low_val((u32)cpu_addr, flags);
509 switch (region) {
510 case 0:
Bjorn Helgaas4ecf6b02016-10-06 13:43:41 -0500511 xgene_pcie_set_ib_mask(port, BRIDGE_CFG_4, flags, size);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600512 bar_addr = cfg_base + PCI_BASE_ADDRESS_0;
513 writel(bar_low, bar_addr);
514 writel(upper_32_bits(cpu_addr), bar_addr + 0x4);
Bjorn Helgaas4ecf6b02016-10-06 13:43:41 -0500515 pim_reg = PIM1_1L;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600516 break;
517 case 1:
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500518 xgene_pcie_writel(port, IBAR2, bar_low);
519 xgene_pcie_writel(port, IR2MSK, lower_32_bits(mask));
Bjorn Helgaas4ecf6b02016-10-06 13:43:41 -0500520 pim_reg = PIM2_1L;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600521 break;
522 case 2:
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500523 xgene_pcie_writel(port, IBAR3L, bar_low);
524 xgene_pcie_writel(port, IBAR3L + 0x4, upper_32_bits(cpu_addr));
525 xgene_pcie_writel(port, IR3MSKL, lower_32_bits(mask));
526 xgene_pcie_writel(port, IR3MSKL + 0x4, upper_32_bits(mask));
Bjorn Helgaas4ecf6b02016-10-06 13:43:41 -0500527 pim_reg = PIM3_1L;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600528 break;
529 }
530
Bjorn Helgaas4ecf6b02016-10-06 13:43:41 -0500531 xgene_pcie_setup_pims(port, pim_reg, pci_addr, ~(size - 1));
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600532}
533
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600534static int xgene_pcie_parse_map_dma_ranges(struct xgene_pcie_port *port)
535{
Rob Herring6dce5aa52019-10-28 11:32:53 -0500536 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(port);
537 struct resource_entry *entry;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600538 u8 ib_reg_mask = 0;
539
Rob Herring6dce5aa52019-10-28 11:32:53 -0500540 resource_list_for_each_entry(entry, &bridge->dma_ranges)
541 xgene_pcie_setup_ib_reg(port, entry, &ib_reg_mask);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600542
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600543 return 0;
544}
545
546/* clear BAR configuration which was done by firmware */
547static void xgene_pcie_clear_config(struct xgene_pcie_port *port)
548{
549 int i;
550
551 for (i = PIM1_1L; i <= CFGCTL; i += 4)
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500552 xgene_pcie_writel(port, i, 0);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600553}
554
Rob Herring83083e22019-10-28 11:32:44 -0500555static int xgene_pcie_setup(struct xgene_pcie_port *port)
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600556{
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500557 struct device *dev = port->dev;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600558 u32 val, lanes = 0, speed = 0;
559 int ret;
560
561 xgene_pcie_clear_config(port);
562
563 /* setup the vendor and device IDs correctly */
564 val = (XGENE_PCIE_DEVICEID << 16) | XGENE_PCIE_VENDORID;
Bjorn Helgaas8e93c512016-10-06 13:43:42 -0500565 xgene_pcie_writel(port, BRIDGE_CFG_0, val);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600566
Rob Herring83083e22019-10-28 11:32:44 -0500567 ret = xgene_pcie_map_ranges(port);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600568 if (ret)
569 return ret;
570
571 ret = xgene_pcie_parse_map_dma_ranges(port);
572 if (ret)
573 return ret;
574
575 xgene_pcie_linkup(port, &lanes, &speed);
576 if (!port->link_up)
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500577 dev_info(dev, "(rc) link down\n");
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600578 else
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500579 dev_info(dev, "(rc) x%d gen-%d link up\n", lanes, speed + 1);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600580 return 0;
581}
582
Duc Dangc5d46032016-12-01 18:27:07 -0800583static struct pci_ops xgene_pcie_ops = {
584 .map_bus = xgene_pcie_map_bus,
585 .read = xgene_pcie_config_read32,
586 .write = pci_generic_config_write32,
587};
588
Bjorn Helgaas92e31452017-11-09 18:12:01 -0600589static int xgene_pcie_probe(struct platform_device *pdev)
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600590{
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500591 struct device *dev = &pdev->dev;
592 struct device_node *dn = dev->of_node;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600593 struct xgene_pcie_port *port;
Bjorn Helgaas7da7a1a2017-02-08 15:43:45 -0600594 struct pci_bus *bus, *child;
Lorenzo Pieralisi9af275b2017-06-28 15:13:59 -0500595 struct pci_host_bridge *bridge;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600596 int ret;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600597
Lorenzo Pieralisi9af275b2017-06-28 15:13:59 -0500598 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*port));
599 if (!bridge)
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600600 return -ENOMEM;
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500601
Lorenzo Pieralisi9af275b2017-06-28 15:13:59 -0500602 port = pci_host_bridge_priv(bridge);
603
Bjorn Helgaasd963ab22016-10-06 13:43:42 -0500604 port->node = of_node_get(dn);
605 port->dev = dev;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600606
Duc Dangf09f8732015-06-12 17:35:57 -0700607 port->version = XGENE_PCIE_IP_VER_UNKN;
608 if (of_device_is_compatible(port->node, "apm,xgene-pcie"))
609 port->version = XGENE_PCIE_IP_VER_1;
610
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600611 ret = xgene_pcie_map_reg(port, pdev);
612 if (ret)
613 return ret;
614
615 ret = xgene_pcie_init_port(port);
616 if (ret)
617 return ret;
618
Rob Herring331f6342019-10-30 17:30:57 -0500619 ret = pci_parse_request_of_pci_ranges(dev, &bridge->windows,
620 &bridge->dma_ranges, NULL);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600621 if (ret)
622 return ret;
623
Rob Herring83083e22019-10-28 11:32:44 -0500624 ret = xgene_pcie_setup(port);
Bjorn Helgaas0ccb7eef2016-05-28 18:14:24 -0500625 if (ret)
Rob Herring83083e22019-10-28 11:32:44 -0500626 return ret;
Bjorn Helgaas0ccb7eef2016-05-28 18:14:24 -0500627
Lorenzo Pieralisi9af275b2017-06-28 15:13:59 -0500628 bridge->dev.parent = dev;
629 bridge->sysdata = port;
630 bridge->busnr = 0;
631 bridge->ops = &xgene_pcie_ops;
Lorenzo Pieralisic62e98b2017-06-28 15:14:10 -0500632 bridge->map_irq = of_irq_parse_and_map_pci;
633 bridge->swizzle_irq = pci_common_swizzle;
Lorenzo Pieralisi9af275b2017-06-28 15:13:59 -0500634
635 ret = pci_scan_root_bus_bridge(bridge);
636 if (ret < 0)
Rob Herring83083e22019-10-28 11:32:44 -0500637 return ret;
Lorenzo Pieralisi9af275b2017-06-28 15:13:59 -0500638
639 bus = bridge->bus;
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600640
Duc Dang336b5be2014-11-06 17:14:18 -0800641 pci_assign_unassigned_bus_resources(bus);
Bjorn Helgaas7da7a1a2017-02-08 15:43:45 -0600642 list_for_each_entry(child, &bus->children, node)
643 pcie_bus_configure_settings(child);
Duc Dang336b5be2014-11-06 17:14:18 -0800644 pci_bus_add_devices(bus);
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600645 return 0;
646}
647
648static const struct of_device_id xgene_pcie_match_table[] = {
649 {.compatible = "apm,xgene-pcie",},
650 {},
651};
652
653static struct platform_driver xgene_pcie_driver = {
654 .driver = {
Bjorn Helgaasfca48482017-09-05 13:09:05 -0500655 .name = "xgene-pcie",
656 .of_match_table = of_match_ptr(xgene_pcie_match_table),
657 .suppress_bind_attrs = true,
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600658 },
Bjorn Helgaas92e31452017-11-09 18:12:01 -0600659 .probe = xgene_pcie_probe,
Tanmay Inamdar5f6b6cc2014-10-01 13:01:35 -0600660};
Paul Gortmaker50dcd292016-07-02 19:13:34 -0400661builtin_platform_driver(xgene_pcie_driver);
Duc Dangc5d46032016-12-01 18:27:07 -0800662#endif