blob: cbbbeaa46dad5d025ee4ac0c28cfdea5a98a4a69 [file] [log] [blame]
Ray Jui1fb37a82015-04-08 11:21:35 -07001/*
2 * Copyright (C) 2014 Hauke Mehrtens <hauke@hauke-m.de>
Florian Fainellibe908d22015-10-16 12:04:04 -07003 * Copyright (C) 2015 Broadcom Corporation
Ray Jui1fb37a82015-04-08 11:21:35 -07004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation version 2.
8 *
9 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
10 * kind, whether express or implied; without even the implied warranty
11 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/kernel.h>
16#include <linux/pci.h>
17#include <linux/msi.h>
18#include <linux/clk.h>
19#include <linux/module.h>
20#include <linux/mbus.h>
21#include <linux/slab.h>
22#include <linux/delay.h>
23#include <linux/interrupt.h>
Ray Jui787b3c42016-10-31 17:38:35 -070024#include <linux/irqchip/arm-gic-v3.h>
Ray Jui1fb37a82015-04-08 11:21:35 -070025#include <linux/platform_device.h>
26#include <linux/of_address.h>
27#include <linux/of_pci.h>
28#include <linux/of_irq.h>
29#include <linux/of_platform.h>
30#include <linux/phy/phy.h>
31
32#include "pcie-iproc.h"
33
Ray Jui199ff142015-09-15 17:39:18 -070034#define EP_PERST_SOURCE_SELECT_SHIFT 2
35#define EP_PERST_SOURCE_SELECT BIT(EP_PERST_SOURCE_SELECT_SHIFT)
Ray Jui1fb37a82015-04-08 11:21:35 -070036#define EP_MODE_SURVIVE_PERST_SHIFT 1
37#define EP_MODE_SURVIVE_PERST BIT(EP_MODE_SURVIVE_PERST_SHIFT)
38#define RC_PCIE_RST_OUTPUT_SHIFT 0
39#define RC_PCIE_RST_OUTPUT BIT(RC_PCIE_RST_OUTPUT_SHIFT)
Ray Jui943ebae2015-12-04 09:34:59 -080040#define PAXC_RESET_MASK 0x7f
Ray Jui1fb37a82015-04-08 11:21:35 -070041
Ray Jui787b3c42016-10-31 17:38:35 -070042#define GIC_V3_CFG_SHIFT 0
43#define GIC_V3_CFG BIT(GIC_V3_CFG_SHIFT)
44
45#define MSI_ENABLE_CFG_SHIFT 0
46#define MSI_ENABLE_CFG BIT(MSI_ENABLE_CFG_SHIFT)
47
Ray Jui1fb37a82015-04-08 11:21:35 -070048#define CFG_IND_ADDR_MASK 0x00001ffc
49
Ray Jui1fb37a82015-04-08 11:21:35 -070050#define CFG_ADDR_BUS_NUM_SHIFT 20
51#define CFG_ADDR_BUS_NUM_MASK 0x0ff00000
52#define CFG_ADDR_DEV_NUM_SHIFT 15
53#define CFG_ADDR_DEV_NUM_MASK 0x000f8000
54#define CFG_ADDR_FUNC_NUM_SHIFT 12
55#define CFG_ADDR_FUNC_NUM_MASK 0x00007000
56#define CFG_ADDR_REG_NUM_SHIFT 2
57#define CFG_ADDR_REG_NUM_MASK 0x00000ffc
58#define CFG_ADDR_CFG_TYPE_SHIFT 0
59#define CFG_ADDR_CFG_TYPE_MASK 0x00000003
60
Ray Jui1fb37a82015-04-08 11:21:35 -070061#define SYS_RC_INTX_MASK 0xf
62
Ray Juiaaf22ab2015-09-15 17:39:19 -070063#define PCIE_PHYLINKUP_SHIFT 3
64#define PCIE_PHYLINKUP BIT(PCIE_PHYLINKUP_SHIFT)
65#define PCIE_DL_ACTIVE_SHIFT 2
66#define PCIE_DL_ACTIVE BIT(PCIE_DL_ACTIVE_SHIFT)
67
Ray Jui538928f2016-10-31 17:38:33 -070068#define APB_ERR_EN_SHIFT 0
69#define APB_ERR_EN BIT(APB_ERR_EN_SHIFT)
70
Ray Jui4213e152016-10-31 17:38:37 -070071/* derive the enum index of the outbound/inbound mapping registers */
72#define MAP_REG(base_reg, index) ((base_reg) + (index) * 2)
73
74/*
75 * Maximum number of outbound mapping window sizes that can be supported by any
76 * OARR/OMAP mapping pair
77 */
78#define MAX_NUM_OB_WINDOW_SIZES 4
79
Ray Juie99a1872015-10-16 08:18:24 -050080#define OARR_VALID_SHIFT 0
81#define OARR_VALID BIT(OARR_VALID_SHIFT)
82#define OARR_SIZE_CFG_SHIFT 1
Ray Juie99a1872015-10-16 08:18:24 -050083
Ray Juidd9d4e72016-10-31 17:38:39 -070084/*
85 * Maximum number of inbound mapping region sizes that can be supported by an
86 * IARR
87 */
88#define MAX_NUM_IB_REGION_SIZES 9
89
90#define IMAP_VALID_SHIFT 0
91#define IMAP_VALID BIT(IMAP_VALID_SHIFT)
92
Bjorn Helgaase3a16982016-10-06 13:36:07 -050093#define PCI_EXP_CAP 0xac
94
Ray Jui943ebae2015-12-04 09:34:59 -080095#define IPROC_PCIE_REG_INVALID 0xffff
96
Ray Jui4213e152016-10-31 17:38:37 -070097/**
98 * iProc PCIe outbound mapping controller specific parameters
99 *
100 * @window_sizes: list of supported outbound mapping window sizes in MB
101 * @nr_sizes: number of supported outbound mapping window sizes
102 */
103struct iproc_pcie_ob_map {
104 resource_size_t window_sizes[MAX_NUM_OB_WINDOW_SIZES];
105 unsigned int nr_sizes;
106};
107
108static const struct iproc_pcie_ob_map paxb_ob_map[] = {
109 {
110 /* OARR0/OMAP0 */
111 .window_sizes = { 128, 256 },
112 .nr_sizes = 2,
113 },
114 {
115 /* OARR1/OMAP1 */
116 .window_sizes = { 128, 256 },
117 .nr_sizes = 2,
118 },
119};
120
Ray Juidd9d4e72016-10-31 17:38:39 -0700121/**
122 * iProc PCIe inbound mapping type
123 */
124enum iproc_pcie_ib_map_type {
125 /* for DDR memory */
126 IPROC_PCIE_IB_MAP_MEM = 0,
127
128 /* for device I/O memory */
129 IPROC_PCIE_IB_MAP_IO,
130
131 /* invalid or unused */
132 IPROC_PCIE_IB_MAP_INVALID
133};
134
135/**
136 * iProc PCIe inbound mapping controller specific parameters
137 *
138 * @type: inbound mapping region type
139 * @size_unit: inbound mapping region size unit, could be SZ_1K, SZ_1M, or
140 * SZ_1G
141 * @region_sizes: list of supported inbound mapping region sizes in KB, MB, or
142 * GB, depedning on the size unit
143 * @nr_sizes: number of supported inbound mapping region sizes
144 * @nr_windows: number of supported inbound mapping windows for the region
145 * @imap_addr_offset: register offset between the upper and lower 32-bit
146 * IMAP address registers
147 * @imap_window_offset: register offset between each IMAP window
148 */
149struct iproc_pcie_ib_map {
150 enum iproc_pcie_ib_map_type type;
151 unsigned int size_unit;
152 resource_size_t region_sizes[MAX_NUM_IB_REGION_SIZES];
153 unsigned int nr_sizes;
154 unsigned int nr_windows;
155 u16 imap_addr_offset;
156 u16 imap_window_offset;
157};
158
Ray Jui06324ed2016-10-31 17:38:30 -0700159/*
160 * iProc PCIe host registers
161 */
Ray Jui943ebae2015-12-04 09:34:59 -0800162enum iproc_pcie_reg {
Ray Jui06324ed2016-10-31 17:38:30 -0700163 /* clock/reset signal control */
Ray Jui943ebae2015-12-04 09:34:59 -0800164 IPROC_PCIE_CLK_CTRL = 0,
Ray Jui06324ed2016-10-31 17:38:30 -0700165
Ray Jui787b3c42016-10-31 17:38:35 -0700166 /*
167 * To allow MSI to be steered to an external MSI controller (e.g., ARM
168 * GICv3 ITS)
169 */
170 IPROC_PCIE_MSI_GIC_MODE,
171
172 /*
173 * IPROC_PCIE_MSI_BASE_ADDR and IPROC_PCIE_MSI_WINDOW_SIZE define the
174 * window where the MSI posted writes are written, for the writes to be
175 * interpreted as MSI writes.
176 */
177 IPROC_PCIE_MSI_BASE_ADDR,
178 IPROC_PCIE_MSI_WINDOW_SIZE,
179
180 /*
181 * To hold the address of the register where the MSI writes are
182 * programed. When ARM GICv3 ITS is used, this should be programmed
183 * with the address of the GITS_TRANSLATER register.
184 */
185 IPROC_PCIE_MSI_ADDR_LO,
186 IPROC_PCIE_MSI_ADDR_HI,
187
188 /* enable MSI */
189 IPROC_PCIE_MSI_EN_CFG,
190
Ray Jui06324ed2016-10-31 17:38:30 -0700191 /* allow access to root complex configuration space */
Ray Jui943ebae2015-12-04 09:34:59 -0800192 IPROC_PCIE_CFG_IND_ADDR,
193 IPROC_PCIE_CFG_IND_DATA,
Ray Jui06324ed2016-10-31 17:38:30 -0700194
195 /* allow access to device configuration space */
Ray Jui943ebae2015-12-04 09:34:59 -0800196 IPROC_PCIE_CFG_ADDR,
197 IPROC_PCIE_CFG_DATA,
Ray Jui06324ed2016-10-31 17:38:30 -0700198
199 /* enable INTx */
Ray Jui943ebae2015-12-04 09:34:59 -0800200 IPROC_PCIE_INTX_EN,
Ray Jui06324ed2016-10-31 17:38:30 -0700201
202 /* outbound address mapping */
Ray Jui4213e152016-10-31 17:38:37 -0700203 IPROC_PCIE_OARR0,
204 IPROC_PCIE_OMAP0,
205 IPROC_PCIE_OARR1,
206 IPROC_PCIE_OMAP1,
207 IPROC_PCIE_OARR2,
208 IPROC_PCIE_OMAP2,
209 IPROC_PCIE_OARR3,
210 IPROC_PCIE_OMAP3,
Ray Jui06324ed2016-10-31 17:38:30 -0700211
Ray Juidd9d4e72016-10-31 17:38:39 -0700212 /* inbound address mapping */
213 IPROC_PCIE_IARR0,
214 IPROC_PCIE_IMAP0,
215 IPROC_PCIE_IARR1,
216 IPROC_PCIE_IMAP1,
217 IPROC_PCIE_IARR2,
218 IPROC_PCIE_IMAP2,
219 IPROC_PCIE_IARR3,
220 IPROC_PCIE_IMAP3,
221 IPROC_PCIE_IARR4,
222 IPROC_PCIE_IMAP4,
223
Ray Jui06324ed2016-10-31 17:38:30 -0700224 /* link status */
Ray Jui943ebae2015-12-04 09:34:59 -0800225 IPROC_PCIE_LINK_STATUS,
Ray Jui06324ed2016-10-31 17:38:30 -0700226
Ray Jui538928f2016-10-31 17:38:33 -0700227 /* enable APB error for unsupported requests */
228 IPROC_PCIE_APB_ERR_EN,
229
Ray Jui06324ed2016-10-31 17:38:30 -0700230 /* total number of core registers */
231 IPROC_PCIE_MAX_NUM_REG,
Ray Jui943ebae2015-12-04 09:34:59 -0800232};
233
Ray Jui404349c2016-10-31 17:38:32 -0700234/* iProc PCIe PAXB BCMA registers */
235static const u16 iproc_pcie_reg_paxb_bcma[] = {
236 [IPROC_PCIE_CLK_CTRL] = 0x000,
237 [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
238 [IPROC_PCIE_CFG_IND_DATA] = 0x124,
239 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
240 [IPROC_PCIE_CFG_DATA] = 0x1fc,
241 [IPROC_PCIE_INTX_EN] = 0x330,
242 [IPROC_PCIE_LINK_STATUS] = 0xf0c,
243};
244
Ray Jui943ebae2015-12-04 09:34:59 -0800245/* iProc PCIe PAXB registers */
246static const u16 iproc_pcie_reg_paxb[] = {
Ray Jui4213e152016-10-31 17:38:37 -0700247 [IPROC_PCIE_CLK_CTRL] = 0x000,
248 [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
249 [IPROC_PCIE_CFG_IND_DATA] = 0x124,
250 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
251 [IPROC_PCIE_CFG_DATA] = 0x1fc,
252 [IPROC_PCIE_INTX_EN] = 0x330,
253 [IPROC_PCIE_OARR0] = 0xd20,
254 [IPROC_PCIE_OMAP0] = 0xd40,
255 [IPROC_PCIE_OARR1] = 0xd28,
256 [IPROC_PCIE_OMAP1] = 0xd48,
257 [IPROC_PCIE_LINK_STATUS] = 0xf0c,
258 [IPROC_PCIE_APB_ERR_EN] = 0xf40,
Ray Jui943ebae2015-12-04 09:34:59 -0800259};
260
261/* iProc PCIe PAXC v1 registers */
262static const u16 iproc_pcie_reg_paxc[] = {
Ray Jui4213e152016-10-31 17:38:37 -0700263 [IPROC_PCIE_CLK_CTRL] = 0x000,
264 [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0,
265 [IPROC_PCIE_CFG_IND_DATA] = 0x1f4,
266 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
267 [IPROC_PCIE_CFG_DATA] = 0x1fc,
Ray Jui943ebae2015-12-04 09:34:59 -0800268};
Ray Juie99a1872015-10-16 08:18:24 -0500269
Ray Jui787b3c42016-10-31 17:38:35 -0700270/* iProc PCIe PAXC v2 registers */
271static const u16 iproc_pcie_reg_paxc_v2[] = {
272 [IPROC_PCIE_MSI_GIC_MODE] = 0x050,
273 [IPROC_PCIE_MSI_BASE_ADDR] = 0x074,
274 [IPROC_PCIE_MSI_WINDOW_SIZE] = 0x078,
275 [IPROC_PCIE_MSI_ADDR_LO] = 0x07c,
276 [IPROC_PCIE_MSI_ADDR_HI] = 0x080,
277 [IPROC_PCIE_MSI_EN_CFG] = 0x09c,
278 [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0,
279 [IPROC_PCIE_CFG_IND_DATA] = 0x1f4,
280 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
281 [IPROC_PCIE_CFG_DATA] = 0x1fc,
282};
283
Ray Jui8d9bfe32015-07-21 18:29:40 -0700284static inline struct iproc_pcie *iproc_data(struct pci_bus *bus)
Ray Jui1fb37a82015-04-08 11:21:35 -0700285{
Ray Jui8d9bfe32015-07-21 18:29:40 -0700286 struct iproc_pcie *pcie;
287#ifdef CONFIG_ARM
288 struct pci_sys_data *sys = bus->sysdata;
289
290 pcie = sys->private_data;
291#else
292 pcie = bus->sysdata;
293#endif
294 return pcie;
Ray Jui1fb37a82015-04-08 11:21:35 -0700295}
296
Ray Jui943ebae2015-12-04 09:34:59 -0800297static inline bool iproc_pcie_reg_is_invalid(u16 reg_offset)
298{
299 return !!(reg_offset == IPROC_PCIE_REG_INVALID);
300}
301
302static inline u16 iproc_pcie_reg_offset(struct iproc_pcie *pcie,
303 enum iproc_pcie_reg reg)
304{
305 return pcie->reg_offsets[reg];
306}
307
308static inline u32 iproc_pcie_read_reg(struct iproc_pcie *pcie,
309 enum iproc_pcie_reg reg)
310{
311 u16 offset = iproc_pcie_reg_offset(pcie, reg);
312
313 if (iproc_pcie_reg_is_invalid(offset))
314 return 0;
315
316 return readl(pcie->base + offset);
317}
318
319static inline void iproc_pcie_write_reg(struct iproc_pcie *pcie,
320 enum iproc_pcie_reg reg, u32 val)
321{
322 u16 offset = iproc_pcie_reg_offset(pcie, reg);
323
324 if (iproc_pcie_reg_is_invalid(offset))
325 return;
326
327 writel(val, pcie->base + offset);
328}
329
Ray Jui538928f2016-10-31 17:38:33 -0700330/**
331 * APB error forwarding can be disabled during access of configuration
332 * registers of the endpoint device, to prevent unsupported requests
333 * (typically seen during enumeration with multi-function devices) from
334 * triggering a system exception.
335 */
336static inline void iproc_pcie_apb_err_disable(struct pci_bus *bus,
337 bool disable)
338{
339 struct iproc_pcie *pcie = iproc_data(bus);
340 u32 val;
341
342 if (bus->number && pcie->has_apb_err_disable) {
343 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_APB_ERR_EN);
344 if (disable)
345 val &= ~APB_ERR_EN;
346 else
347 val |= APB_ERR_EN;
348 iproc_pcie_write_reg(pcie, IPROC_PCIE_APB_ERR_EN, val);
349 }
350}
351
Ray Jui1fb37a82015-04-08 11:21:35 -0700352/**
353 * Note access to the configuration registers are protected at the higher layer
354 * by 'pci_lock' in drivers/pci/access.c
355 */
356static void __iomem *iproc_pcie_map_cfg_bus(struct pci_bus *bus,
357 unsigned int devfn,
358 int where)
359{
Ray Jui8d9bfe32015-07-21 18:29:40 -0700360 struct iproc_pcie *pcie = iproc_data(bus);
Ray Jui1fb37a82015-04-08 11:21:35 -0700361 unsigned slot = PCI_SLOT(devfn);
362 unsigned fn = PCI_FUNC(devfn);
363 unsigned busno = bus->number;
364 u32 val;
Ray Jui943ebae2015-12-04 09:34:59 -0800365 u16 offset;
366
Ray Jui1fb37a82015-04-08 11:21:35 -0700367 /* root complex access */
368 if (busno == 0) {
Ray Jui46560382016-01-27 16:52:24 -0600369 if (slot > 0 || fn > 0)
370 return NULL;
371
Ray Jui943ebae2015-12-04 09:34:59 -0800372 iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_IND_ADDR,
373 where & CFG_IND_ADDR_MASK);
374 offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_IND_DATA);
375 if (iproc_pcie_reg_is_invalid(offset))
Ray Jui1fb37a82015-04-08 11:21:35 -0700376 return NULL;
Ray Jui943ebae2015-12-04 09:34:59 -0800377 else
378 return (pcie->base + offset);
Ray Jui1fb37a82015-04-08 11:21:35 -0700379 }
380
Ray Jui46560382016-01-27 16:52:24 -0600381 /*
382 * PAXC is connected to an internally emulated EP within the SoC. It
383 * allows only one device.
384 */
Ray Jui06324ed2016-10-31 17:38:30 -0700385 if (pcie->ep_is_internal)
Ray Jui46560382016-01-27 16:52:24 -0600386 if (slot > 0)
387 return NULL;
388
Ray Jui1fb37a82015-04-08 11:21:35 -0700389 /* EP device access */
390 val = (busno << CFG_ADDR_BUS_NUM_SHIFT) |
391 (slot << CFG_ADDR_DEV_NUM_SHIFT) |
392 (fn << CFG_ADDR_FUNC_NUM_SHIFT) |
393 (where & CFG_ADDR_REG_NUM_MASK) |
394 (1 & CFG_ADDR_CFG_TYPE_MASK);
Ray Jui943ebae2015-12-04 09:34:59 -0800395 iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_ADDR, val);
396 offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_DATA);
397 if (iproc_pcie_reg_is_invalid(offset))
398 return NULL;
399 else
400 return (pcie->base + offset);
Ray Jui1fb37a82015-04-08 11:21:35 -0700401}
402
Ray Jui538928f2016-10-31 17:38:33 -0700403static int iproc_pcie_config_read32(struct pci_bus *bus, unsigned int devfn,
404 int where, int size, u32 *val)
405{
406 int ret;
407
408 iproc_pcie_apb_err_disable(bus, true);
409 ret = pci_generic_config_read32(bus, devfn, where, size, val);
410 iproc_pcie_apb_err_disable(bus, false);
411
412 return ret;
413}
414
415static int iproc_pcie_config_write32(struct pci_bus *bus, unsigned int devfn,
416 int where, int size, u32 val)
417{
418 int ret;
419
420 iproc_pcie_apb_err_disable(bus, true);
421 ret = pci_generic_config_write32(bus, devfn, where, size, val);
422 iproc_pcie_apb_err_disable(bus, false);
423
424 return ret;
425}
426
Ray Jui1fb37a82015-04-08 11:21:35 -0700427static struct pci_ops iproc_pcie_ops = {
428 .map_bus = iproc_pcie_map_cfg_bus,
Ray Jui538928f2016-10-31 17:38:33 -0700429 .read = iproc_pcie_config_read32,
430 .write = iproc_pcie_config_write32,
Ray Jui1fb37a82015-04-08 11:21:35 -0700431};
432
433static void iproc_pcie_reset(struct iproc_pcie *pcie)
434{
435 u32 val;
436
Ray Jui7cbd50d2016-10-31 17:38:31 -0700437 /*
438 * PAXC and the internal emulated endpoint device downstream should not
439 * be reset. If firmware has been loaded on the endpoint device at an
440 * earlier boot stage, reset here causes issues.
441 */
442 if (pcie->ep_is_internal)
Ray Jui943ebae2015-12-04 09:34:59 -0800443 return;
Ray Jui943ebae2015-12-04 09:34:59 -0800444
Ray Jui1fb37a82015-04-08 11:21:35 -0700445 /*
Ray Jui199ff142015-09-15 17:39:18 -0700446 * Select perst_b signal as reset source. Put the device into reset,
447 * and then bring it out of reset
Ray Jui1fb37a82015-04-08 11:21:35 -0700448 */
Ray Jui943ebae2015-12-04 09:34:59 -0800449 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
Ray Jui199ff142015-09-15 17:39:18 -0700450 val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST &
451 ~RC_PCIE_RST_OUTPUT;
Ray Jui943ebae2015-12-04 09:34:59 -0800452 iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
Ray Jui1fb37a82015-04-08 11:21:35 -0700453 udelay(250);
Ray Jui199ff142015-09-15 17:39:18 -0700454
455 val |= RC_PCIE_RST_OUTPUT;
Ray Jui943ebae2015-12-04 09:34:59 -0800456 iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
Ray Jui199ff142015-09-15 17:39:18 -0700457 msleep(100);
Ray Jui1fb37a82015-04-08 11:21:35 -0700458}
459
460static int iproc_pcie_check_link(struct iproc_pcie *pcie, struct pci_bus *bus)
461{
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500462 struct device *dev = pcie->dev;
Ray Jui1fb37a82015-04-08 11:21:35 -0700463 u8 hdr_type;
Ray Juiaaf22ab2015-09-15 17:39:19 -0700464 u32 link_ctrl, class, val;
Bjorn Helgaase3a16982016-10-06 13:36:07 -0500465 u16 pos = PCI_EXP_CAP, link_status;
Ray Juiaaf22ab2015-09-15 17:39:19 -0700466 bool link_is_active = false;
467
Ray Jui943ebae2015-12-04 09:34:59 -0800468 /*
469 * PAXC connects to emulated endpoint devices directly and does not
470 * have a Serdes. Therefore skip the link detection logic here.
471 */
Ray Jui06324ed2016-10-31 17:38:30 -0700472 if (pcie->ep_is_internal)
Ray Jui943ebae2015-12-04 09:34:59 -0800473 return 0;
474
475 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_LINK_STATUS);
Ray Juiaaf22ab2015-09-15 17:39:19 -0700476 if (!(val & PCIE_PHYLINKUP) || !(val & PCIE_DL_ACTIVE)) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500477 dev_err(dev, "PHY or data link is INACTIVE!\n");
Ray Juiaaf22ab2015-09-15 17:39:19 -0700478 return -ENODEV;
479 }
Ray Jui1fb37a82015-04-08 11:21:35 -0700480
481 /* make sure we are not in EP mode */
482 pci_bus_read_config_byte(bus, 0, PCI_HEADER_TYPE, &hdr_type);
483 if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500484 dev_err(dev, "in EP mode, hdr=%#02x\n", hdr_type);
Ray Jui1fb37a82015-04-08 11:21:35 -0700485 return -EFAULT;
486 }
487
488 /* force class to PCI_CLASS_BRIDGE_PCI (0x0604) */
Ray Juiaaf22ab2015-09-15 17:39:19 -0700489#define PCI_BRIDGE_CTRL_REG_OFFSET 0x43c
490#define PCI_CLASS_BRIDGE_MASK 0xffff00
491#define PCI_CLASS_BRIDGE_SHIFT 8
492 pci_bus_read_config_dword(bus, 0, PCI_BRIDGE_CTRL_REG_OFFSET, &class);
493 class &= ~PCI_CLASS_BRIDGE_MASK;
494 class |= (PCI_CLASS_BRIDGE_PCI << PCI_CLASS_BRIDGE_SHIFT);
495 pci_bus_write_config_dword(bus, 0, PCI_BRIDGE_CTRL_REG_OFFSET, class);
Ray Jui1fb37a82015-04-08 11:21:35 -0700496
497 /* check link status to see if link is active */
Ray Jui1fb37a82015-04-08 11:21:35 -0700498 pci_bus_read_config_word(bus, 0, pos + PCI_EXP_LNKSTA, &link_status);
499 if (link_status & PCI_EXP_LNKSTA_NLW)
Ray Juiaaf22ab2015-09-15 17:39:19 -0700500 link_is_active = true;
Ray Jui1fb37a82015-04-08 11:21:35 -0700501
502 if (!link_is_active) {
503 /* try GEN 1 link speed */
Ray Jui1fb37a82015-04-08 11:21:35 -0700504#define PCI_TARGET_LINK_SPEED_MASK 0xf
505#define PCI_TARGET_LINK_SPEED_GEN2 0x2
506#define PCI_TARGET_LINK_SPEED_GEN1 0x1
507 pci_bus_read_config_dword(bus, 0,
Bjorn Helgaase3a16982016-10-06 13:36:07 -0500508 pos + PCI_EXP_LNKCTL2,
Ray Jui1fb37a82015-04-08 11:21:35 -0700509 &link_ctrl);
510 if ((link_ctrl & PCI_TARGET_LINK_SPEED_MASK) ==
511 PCI_TARGET_LINK_SPEED_GEN2) {
512 link_ctrl &= ~PCI_TARGET_LINK_SPEED_MASK;
513 link_ctrl |= PCI_TARGET_LINK_SPEED_GEN1;
514 pci_bus_write_config_dword(bus, 0,
Bjorn Helgaase3a16982016-10-06 13:36:07 -0500515 pos + PCI_EXP_LNKCTL2,
Ray Jui1fb37a82015-04-08 11:21:35 -0700516 link_ctrl);
517 msleep(100);
518
Ray Jui1fb37a82015-04-08 11:21:35 -0700519 pci_bus_read_config_word(bus, 0, pos + PCI_EXP_LNKSTA,
520 &link_status);
521 if (link_status & PCI_EXP_LNKSTA_NLW)
Ray Juiaaf22ab2015-09-15 17:39:19 -0700522 link_is_active = true;
Ray Jui1fb37a82015-04-08 11:21:35 -0700523 }
524 }
525
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500526 dev_info(dev, "link: %s\n", link_is_active ? "UP" : "DOWN");
Ray Jui1fb37a82015-04-08 11:21:35 -0700527
528 return link_is_active ? 0 : -ENODEV;
529}
530
531static void iproc_pcie_enable(struct iproc_pcie *pcie)
532{
Ray Jui943ebae2015-12-04 09:34:59 -0800533 iproc_pcie_write_reg(pcie, IPROC_PCIE_INTX_EN, SYS_RC_INTX_MASK);
Ray Jui1fb37a82015-04-08 11:21:35 -0700534}
535
Ray Jui4213e152016-10-31 17:38:37 -0700536static inline bool iproc_pcie_ob_is_valid(struct iproc_pcie *pcie,
537 int window_idx)
538{
539 u32 val;
540
541 val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_OARR0, window_idx));
542
543 return !!(val & OARR_VALID);
544}
545
546static inline int iproc_pcie_ob_write(struct iproc_pcie *pcie, int window_idx,
547 int size_idx, u64 axi_addr, u64 pci_addr)
548{
549 struct device *dev = pcie->dev;
550 u16 oarr_offset, omap_offset;
551
552 /*
553 * Derive the OARR/OMAP offset from the first pair (OARR0/OMAP0) based
554 * on window index.
555 */
556 oarr_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OARR0,
557 window_idx));
558 omap_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OMAP0,
559 window_idx));
560 if (iproc_pcie_reg_is_invalid(oarr_offset) ||
561 iproc_pcie_reg_is_invalid(omap_offset))
562 return -EINVAL;
563
564 /*
565 * Program the OARR registers. The upper 32-bit OARR register is
566 * always right after the lower 32-bit OARR register.
567 */
568 writel(lower_32_bits(axi_addr) | (size_idx << OARR_SIZE_CFG_SHIFT) |
569 OARR_VALID, pcie->base + oarr_offset);
570 writel(upper_32_bits(axi_addr), pcie->base + oarr_offset + 4);
571
572 /* now program the OMAP registers */
573 writel(lower_32_bits(pci_addr), pcie->base + omap_offset);
574 writel(upper_32_bits(pci_addr), pcie->base + omap_offset + 4);
575
576 dev_info(dev, "ob window [%d]: offset 0x%x axi %pap pci %pap\n",
577 window_idx, oarr_offset, &axi_addr, &pci_addr);
578 dev_info(dev, "oarr lo 0x%x oarr hi 0x%x\n",
579 readl(pcie->base + oarr_offset),
580 readl(pcie->base + oarr_offset + 4));
581 dev_info(dev, "omap lo 0x%x omap hi 0x%x\n",
582 readl(pcie->base + omap_offset),
583 readl(pcie->base + omap_offset + 4));
584
585 return 0;
586}
587
Ray Juie99a1872015-10-16 08:18:24 -0500588/**
589 * Some iProc SoCs require the SW to configure the outbound address mapping
590 *
591 * Outbound address translation:
592 *
593 * iproc_pcie_address = axi_address - axi_offset
594 * OARR = iproc_pcie_address
595 * OMAP = pci_addr
596 *
597 * axi_addr -> iproc_pcie_address -> OARR -> OMAP -> pci_address
598 */
599static int iproc_pcie_setup_ob(struct iproc_pcie *pcie, u64 axi_addr,
600 u64 pci_addr, resource_size_t size)
601{
602 struct iproc_pcie_ob *ob = &pcie->ob;
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500603 struct device *dev = pcie->dev;
Ray Jui4213e152016-10-31 17:38:37 -0700604 int ret = -EINVAL, window_idx, size_idx;
Ray Juie99a1872015-10-16 08:18:24 -0500605
606 if (axi_addr < ob->axi_offset) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500607 dev_err(dev, "axi address %pap less than offset %pap\n",
Ray Juie99a1872015-10-16 08:18:24 -0500608 &axi_addr, &ob->axi_offset);
609 return -EINVAL;
610 }
611
612 /*
613 * Translate the AXI address to the internal address used by the iProc
614 * PCIe core before programming the OARR
615 */
616 axi_addr -= ob->axi_offset;
617
Ray Jui4213e152016-10-31 17:38:37 -0700618 /* iterate through all OARR/OMAP mapping windows */
619 for (window_idx = ob->nr_windows - 1; window_idx >= 0; window_idx--) {
620 const struct iproc_pcie_ob_map *ob_map =
621 &pcie->ob_map[window_idx];
Ray Juie99a1872015-10-16 08:18:24 -0500622
Ray Jui4213e152016-10-31 17:38:37 -0700623 /*
624 * If current outbound window is already in use, move on to the
625 * next one.
626 */
627 if (iproc_pcie_ob_is_valid(pcie, window_idx))
628 continue;
629
630 /*
631 * Iterate through all supported window sizes within the
632 * OARR/OMAP pair to find a match. Go through the window sizes
633 * in a descending order.
634 */
635 for (size_idx = ob_map->nr_sizes - 1; size_idx >= 0;
636 size_idx--) {
637 resource_size_t window_size =
638 ob_map->window_sizes[size_idx] * SZ_1M;
639
640 if (size < window_size)
641 continue;
642
643 if (!IS_ALIGNED(axi_addr, window_size) ||
644 !IS_ALIGNED(pci_addr, window_size)) {
645 dev_err(dev,
646 "axi %pap or pci %pap not aligned\n",
647 &axi_addr, &pci_addr);
648 return -EINVAL;
649 }
650
651 /*
652 * Match found! Program both OARR and OMAP and mark
653 * them as a valid entry.
654 */
655 ret = iproc_pcie_ob_write(pcie, window_idx, size_idx,
656 axi_addr, pci_addr);
657 if (ret)
658 goto err_ob;
659
660 size -= window_size;
661 if (size == 0)
662 return 0;
663
664 /*
665 * If we are here, we are done with the current window,
666 * but not yet finished all mappings. Need to move on
667 * to the next window.
668 */
669 axi_addr += window_size;
670 pci_addr += window_size;
Ray Juie99a1872015-10-16 08:18:24 -0500671 break;
Ray Jui4213e152016-10-31 17:38:37 -0700672 }
Ray Juie99a1872015-10-16 08:18:24 -0500673 }
674
Ray Jui4213e152016-10-31 17:38:37 -0700675err_ob:
676 dev_err(dev, "unable to configure outbound mapping\n");
677 dev_err(dev,
678 "axi %pap, axi offset %pap, pci %pap, res size %pap\n",
679 &axi_addr, &ob->axi_offset, &pci_addr, &size);
680
681 return ret;
Ray Juie99a1872015-10-16 08:18:24 -0500682}
683
684static int iproc_pcie_map_ranges(struct iproc_pcie *pcie,
685 struct list_head *resources)
686{
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500687 struct device *dev = pcie->dev;
Ray Juie99a1872015-10-16 08:18:24 -0500688 struct resource_entry *window;
689 int ret;
690
691 resource_list_for_each_entry(window, resources) {
692 struct resource *res = window->res;
693 u64 res_type = resource_type(res);
694
695 switch (res_type) {
696 case IORESOURCE_IO:
697 case IORESOURCE_BUS:
698 break;
699 case IORESOURCE_MEM:
700 ret = iproc_pcie_setup_ob(pcie, res->start,
701 res->start - window->offset,
702 resource_size(res));
703 if (ret)
704 return ret;
705 break;
706 default:
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500707 dev_err(dev, "invalid resource %pR\n", res);
Ray Juie99a1872015-10-16 08:18:24 -0500708 return -EINVAL;
709 }
710 }
711
712 return 0;
713}
714
Ray Juidd9d4e72016-10-31 17:38:39 -0700715static inline bool iproc_pcie_ib_is_in_use(struct iproc_pcie *pcie,
716 int region_idx)
717{
718 const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
719 u32 val;
720
721 val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_IARR0, region_idx));
722
723 return !!(val & (BIT(ib_map->nr_sizes) - 1));
724}
725
726static inline bool iproc_pcie_ib_check_type(const struct iproc_pcie_ib_map *ib_map,
727 enum iproc_pcie_ib_map_type type)
728{
729 return !!(ib_map->type == type);
730}
731
732static int iproc_pcie_ib_write(struct iproc_pcie *pcie, int region_idx,
733 int size_idx, int nr_windows, u64 axi_addr,
734 u64 pci_addr, resource_size_t size)
735{
736 struct device *dev = pcie->dev;
737 const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
738 u16 iarr_offset, imap_offset;
739 u32 val;
740 int window_idx;
741
742 iarr_offset = iproc_pcie_reg_offset(pcie,
743 MAP_REG(IPROC_PCIE_IARR0, region_idx));
744 imap_offset = iproc_pcie_reg_offset(pcie,
745 MAP_REG(IPROC_PCIE_IMAP0, region_idx));
746 if (iproc_pcie_reg_is_invalid(iarr_offset) ||
747 iproc_pcie_reg_is_invalid(imap_offset))
748 return -EINVAL;
749
750 dev_info(dev, "ib region [%d]: offset 0x%x axi %pap pci %pap\n",
751 region_idx, iarr_offset, &axi_addr, &pci_addr);
752
753 /*
754 * Program the IARR registers. The upper 32-bit IARR register is
755 * always right after the lower 32-bit IARR register.
756 */
757 writel(lower_32_bits(pci_addr) | BIT(size_idx),
758 pcie->base + iarr_offset);
759 writel(upper_32_bits(pci_addr), pcie->base + iarr_offset + 4);
760
761 dev_info(dev, "iarr lo 0x%x iarr hi 0x%x\n",
762 readl(pcie->base + iarr_offset),
763 readl(pcie->base + iarr_offset + 4));
764
765 /*
766 * Now program the IMAP registers. Each IARR region may have one or
767 * more IMAP windows.
768 */
769 size >>= ilog2(nr_windows);
770 for (window_idx = 0; window_idx < nr_windows; window_idx++) {
771 val = readl(pcie->base + imap_offset);
772 val |= lower_32_bits(axi_addr) | IMAP_VALID;
773 writel(val, pcie->base + imap_offset);
774 writel(upper_32_bits(axi_addr),
775 pcie->base + imap_offset + ib_map->imap_addr_offset);
776
777 dev_info(dev, "imap window [%d] lo 0x%x hi 0x%x\n",
778 window_idx, readl(pcie->base + imap_offset),
779 readl(pcie->base + imap_offset +
780 ib_map->imap_addr_offset));
781
782 imap_offset += ib_map->imap_window_offset;
783 axi_addr += size;
784 }
785
786 return 0;
787}
788
789static int iproc_pcie_setup_ib(struct iproc_pcie *pcie,
790 struct of_pci_range *range,
791 enum iproc_pcie_ib_map_type type)
792{
793 struct device *dev = pcie->dev;
794 struct iproc_pcie_ib *ib = &pcie->ib;
795 int ret;
796 unsigned int region_idx, size_idx;
797 u64 axi_addr = range->cpu_addr, pci_addr = range->pci_addr;
798 resource_size_t size = range->size;
799
800 /* iterate through all IARR mapping regions */
801 for (region_idx = 0; region_idx < ib->nr_regions; region_idx++) {
802 const struct iproc_pcie_ib_map *ib_map =
803 &pcie->ib_map[region_idx];
804
805 /*
806 * If current inbound region is already in use or not a
807 * compatible type, move on to the next.
808 */
809 if (iproc_pcie_ib_is_in_use(pcie, region_idx) ||
810 !iproc_pcie_ib_check_type(ib_map, type))
811 continue;
812
813 /* iterate through all supported region sizes to find a match */
814 for (size_idx = 0; size_idx < ib_map->nr_sizes; size_idx++) {
815 resource_size_t region_size =
816 ib_map->region_sizes[size_idx] * ib_map->size_unit;
817
818 if (size != region_size)
819 continue;
820
821 if (!IS_ALIGNED(axi_addr, region_size) ||
822 !IS_ALIGNED(pci_addr, region_size)) {
823 dev_err(dev,
824 "axi %pap or pci %pap not aligned\n",
825 &axi_addr, &pci_addr);
826 return -EINVAL;
827 }
828
829 /* Match found! Program IARR and all IMAP windows. */
830 ret = iproc_pcie_ib_write(pcie, region_idx, size_idx,
831 ib_map->nr_windows, axi_addr,
832 pci_addr, size);
833 if (ret)
834 goto err_ib;
835 else
836 return 0;
837
838 }
839 }
840 ret = -EINVAL;
841
842err_ib:
843 dev_err(dev, "unable to configure inbound mapping\n");
844 dev_err(dev, "axi %pap, pci %pap, res size %pap\n",
845 &axi_addr, &pci_addr, &size);
846
847 return ret;
848}
849
850static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
851 struct device_node *node)
852{
853 const int na = 3, ns = 2;
854 int rlen;
855
856 parser->node = node;
857 parser->pna = of_n_addr_cells(node);
858 parser->np = parser->pna + na + ns;
859
860 parser->range = of_get_property(node, "dma-ranges", &rlen);
861 if (!parser->range)
862 return -ENOENT;
863
864 parser->end = parser->range + rlen / sizeof(__be32);
865 return 0;
866}
867
868static int iproc_pcie_map_dma_ranges(struct iproc_pcie *pcie)
869{
870 struct of_pci_range range;
871 struct of_pci_range_parser parser;
872 int ret;
873
874 /* Get the dma-ranges from DT */
875 ret = pci_dma_range_parser_init(&parser, pcie->dev->of_node);
876 if (ret)
877 return ret;
878
879 for_each_of_pci_range(&parser, &range) {
880 /* Each range entry corresponds to an inbound mapping region */
881 ret = iproc_pcie_setup_ib(pcie, &range, IPROC_PCIE_IB_MAP_MEM);
882 if (ret)
883 return ret;
884 }
885
886 return 0;
887}
888
Ray Jui787b3c42016-10-31 17:38:35 -0700889static int iproce_pcie_get_msi(struct iproc_pcie *pcie,
890 struct device_node *msi_node,
891 u64 *msi_addr)
892{
893 struct device *dev = pcie->dev;
894 int ret;
895 struct resource res;
896
897 /*
898 * Check if 'msi-map' points to ARM GICv3 ITS, which is the only
899 * supported external MSI controller that requires steering.
900 */
901 if (!of_device_is_compatible(msi_node, "arm,gic-v3-its")) {
902 dev_err(dev, "unable to find compatible MSI controller\n");
903 return -ENODEV;
904 }
905
906 /* derive GITS_TRANSLATER address from GICv3 */
907 ret = of_address_to_resource(msi_node, 0, &res);
908 if (ret < 0) {
909 dev_err(dev, "unable to obtain MSI controller resources\n");
910 return ret;
911 }
912
913 *msi_addr = res.start + GITS_TRANSLATER;
914 return 0;
915}
916
917static void iproc_pcie_paxc_v2_msi_steer(struct iproc_pcie *pcie, u64 msi_addr)
918{
919 u32 val;
920
921 /*
922 * Program bits [43:13] of address of GITS_TRANSLATER register into
923 * bits [30:0] of the MSI base address register. In fact, in all iProc
924 * based SoCs, all I/O register bases are well below the 32-bit
925 * boundary, so we can safely assume bits [43:32] are always zeros.
926 */
927 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_BASE_ADDR,
928 (u32)(msi_addr >> 13));
929
930 /* use a default 8K window size */
931 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_WINDOW_SIZE, 0);
932
933 /* steering MSI to GICv3 ITS */
934 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_GIC_MODE);
935 val |= GIC_V3_CFG;
936 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_GIC_MODE, val);
937
938 /*
939 * Program bits [43:2] of address of GITS_TRANSLATER register into the
940 * iProc MSI address registers.
941 */
942 msi_addr >>= 2;
943 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_HI,
944 upper_32_bits(msi_addr));
945 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_LO,
946 lower_32_bits(msi_addr));
947
948 /* enable MSI */
949 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_EN_CFG);
950 val |= MSI_ENABLE_CFG;
951 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_EN_CFG, val);
952}
953
954static int iproc_pcie_msi_steer(struct iproc_pcie *pcie,
955 struct device_node *msi_node)
956{
957 struct device *dev = pcie->dev;
958 int ret;
959 u64 msi_addr;
960
961 ret = iproce_pcie_get_msi(pcie, msi_node, &msi_addr);
962 if (ret < 0) {
963 dev_err(dev, "msi steering failed\n");
964 return ret;
965 }
966
967 switch (pcie->type) {
968 case IPROC_PCIE_PAXC_V2:
969 iproc_pcie_paxc_v2_msi_steer(pcie, msi_addr);
970 break;
971 default:
972 return -EINVAL;
973 }
974
975 return 0;
976}
977
Ray Jui3bc2b232016-01-06 18:04:35 -0600978static int iproc_pcie_msi_enable(struct iproc_pcie *pcie)
979{
980 struct device_node *msi_node;
Ray Jui787b3c42016-10-31 17:38:35 -0700981 int ret;
982
983 /*
984 * Either the "msi-parent" or the "msi-map" phandle needs to exist
985 * for us to obtain the MSI node.
986 */
Ray Jui3bc2b232016-01-06 18:04:35 -0600987
988 msi_node = of_parse_phandle(pcie->dev->of_node, "msi-parent", 0);
Ray Jui787b3c42016-10-31 17:38:35 -0700989 if (!msi_node) {
990 const __be32 *msi_map = NULL;
991 int len;
992 u32 phandle;
993
994 msi_map = of_get_property(pcie->dev->of_node, "msi-map", &len);
995 if (!msi_map)
996 return -ENODEV;
997
998 phandle = be32_to_cpup(msi_map + 1);
999 msi_node = of_find_node_by_phandle(phandle);
1000 if (!msi_node)
1001 return -ENODEV;
1002 }
1003
1004 /*
1005 * Certain revisions of the iProc PCIe controller require additional
1006 * configurations to steer the MSI writes towards an external MSI
1007 * controller.
1008 */
1009 if (pcie->need_msi_steer) {
1010 ret = iproc_pcie_msi_steer(pcie, msi_node);
1011 if (ret)
1012 return ret;
1013 }
Ray Jui3bc2b232016-01-06 18:04:35 -06001014
1015 /*
1016 * If another MSI controller is being used, the call below should fail
1017 * but that is okay
1018 */
1019 return iproc_msi_init(pcie, msi_node);
1020}
1021
1022static void iproc_pcie_msi_disable(struct iproc_pcie *pcie)
1023{
1024 iproc_msi_exit(pcie);
1025}
1026
Ray Jui06324ed2016-10-31 17:38:30 -07001027static int iproc_pcie_rev_init(struct iproc_pcie *pcie)
1028{
1029 struct device *dev = pcie->dev;
1030 unsigned int reg_idx;
1031 const u16 *regs;
1032
1033 switch (pcie->type) {
Ray Jui404349c2016-10-31 17:38:32 -07001034 case IPROC_PCIE_PAXB_BCMA:
1035 regs = iproc_pcie_reg_paxb_bcma;
1036 break;
Ray Jui06324ed2016-10-31 17:38:30 -07001037 case IPROC_PCIE_PAXB:
1038 regs = iproc_pcie_reg_paxb;
Ray Jui538928f2016-10-31 17:38:33 -07001039 pcie->has_apb_err_disable = true;
Ray Jui4213e152016-10-31 17:38:37 -07001040 if (pcie->need_ob_cfg) {
1041 pcie->ob_map = paxb_ob_map;
1042 pcie->ob.nr_windows = ARRAY_SIZE(paxb_ob_map);
1043 }
Ray Jui06324ed2016-10-31 17:38:30 -07001044 break;
1045 case IPROC_PCIE_PAXC:
1046 regs = iproc_pcie_reg_paxc;
1047 pcie->ep_is_internal = true;
1048 break;
Ray Jui787b3c42016-10-31 17:38:35 -07001049 case IPROC_PCIE_PAXC_V2:
1050 regs = iproc_pcie_reg_paxc_v2;
1051 pcie->ep_is_internal = true;
1052 pcie->need_msi_steer = true;
1053 break;
Ray Jui06324ed2016-10-31 17:38:30 -07001054 default:
1055 dev_err(dev, "incompatible iProc PCIe interface\n");
1056 return -EINVAL;
1057 }
1058
1059 pcie->reg_offsets = devm_kcalloc(dev, IPROC_PCIE_MAX_NUM_REG,
1060 sizeof(*pcie->reg_offsets),
1061 GFP_KERNEL);
1062 if (!pcie->reg_offsets)
1063 return -ENOMEM;
1064
1065 /* go through the register table and populate all valid registers */
Ray Jui787b3c42016-10-31 17:38:35 -07001066 pcie->reg_offsets[0] = (pcie->type == IPROC_PCIE_PAXC_V2) ?
1067 IPROC_PCIE_REG_INVALID : regs[0];
Ray Jui06324ed2016-10-31 17:38:30 -07001068 for (reg_idx = 1; reg_idx < IPROC_PCIE_MAX_NUM_REG; reg_idx++)
1069 pcie->reg_offsets[reg_idx] = regs[reg_idx] ?
1070 regs[reg_idx] : IPROC_PCIE_REG_INVALID;
1071
1072 return 0;
1073}
1074
Hauke Mehrtens18c43422015-05-24 22:37:02 +02001075int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res)
Ray Jui1fb37a82015-04-08 11:21:35 -07001076{
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001077 struct device *dev;
Ray Jui1fb37a82015-04-08 11:21:35 -07001078 int ret;
Ray Jui8d9bfe32015-07-21 18:29:40 -07001079 void *sysdata;
Ray Jui1fb37a82015-04-08 11:21:35 -07001080 struct pci_bus *bus;
1081
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001082 dev = pcie->dev;
Ray Jui06324ed2016-10-31 17:38:30 -07001083
1084 ret = iproc_pcie_rev_init(pcie);
1085 if (ret) {
1086 dev_err(dev, "unable to initialize controller parameters\n");
1087 return ret;
1088 }
1089
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001090 ret = devm_request_pci_bus_resources(dev, res);
Bjorn Helgaasc3245a52016-05-28 18:22:24 -05001091 if (ret)
1092 return ret;
1093
Markus Elfring93972d12015-06-28 16:42:04 +02001094 ret = phy_init(pcie->phy);
1095 if (ret) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001096 dev_err(dev, "unable to initialize PCIe PHY\n");
Markus Elfring93972d12015-06-28 16:42:04 +02001097 return ret;
1098 }
Ray Jui1fb37a82015-04-08 11:21:35 -07001099
Markus Elfring93972d12015-06-28 16:42:04 +02001100 ret = phy_power_on(pcie->phy);
1101 if (ret) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001102 dev_err(dev, "unable to power on PCIe PHY\n");
Markus Elfring93972d12015-06-28 16:42:04 +02001103 goto err_exit_phy;
Ray Jui1fb37a82015-04-08 11:21:35 -07001104 }
1105
1106 iproc_pcie_reset(pcie);
1107
Ray Juie99a1872015-10-16 08:18:24 -05001108 if (pcie->need_ob_cfg) {
1109 ret = iproc_pcie_map_ranges(pcie, res);
1110 if (ret) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001111 dev_err(dev, "map failed\n");
Ray Juie99a1872015-10-16 08:18:24 -05001112 goto err_power_off_phy;
1113 }
1114 }
1115
Ray Juidd9d4e72016-10-31 17:38:39 -07001116 ret = iproc_pcie_map_dma_ranges(pcie);
1117 if (ret && ret != -ENOENT)
1118 goto err_power_off_phy;
1119
Ray Jui8d9bfe32015-07-21 18:29:40 -07001120#ifdef CONFIG_ARM
Ray Jui1fb37a82015-04-08 11:21:35 -07001121 pcie->sysdata.private_data = pcie;
Ray Jui8d9bfe32015-07-21 18:29:40 -07001122 sysdata = &pcie->sysdata;
1123#else
1124 sysdata = pcie;
1125#endif
Ray Jui1fb37a82015-04-08 11:21:35 -07001126
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001127 bus = pci_create_root_bus(dev, 0, &iproc_pcie_ops, sysdata, res);
Ray Jui1fb37a82015-04-08 11:21:35 -07001128 if (!bus) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001129 dev_err(dev, "unable to create PCI root bus\n");
Ray Jui1fb37a82015-04-08 11:21:35 -07001130 ret = -ENOMEM;
1131 goto err_power_off_phy;
1132 }
1133 pcie->root_bus = bus;
1134
1135 ret = iproc_pcie_check_link(pcie, bus);
1136 if (ret) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001137 dev_err(dev, "no PCIe EP device detected\n");
Ray Jui1fb37a82015-04-08 11:21:35 -07001138 goto err_rm_root_bus;
1139 }
1140
1141 iproc_pcie_enable(pcie);
1142
Ray Jui3bc2b232016-01-06 18:04:35 -06001143 if (IS_ENABLED(CONFIG_PCI_MSI))
1144 if (iproc_pcie_msi_enable(pcie))
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001145 dev_info(dev, "not using iProc MSI\n");
Ray Jui3bc2b232016-01-06 18:04:35 -06001146
Ray Jui1fb37a82015-04-08 11:21:35 -07001147 pci_scan_child_bus(bus);
1148 pci_assign_unassigned_bus_resources(bus);
Hauke Mehrtensc1e02ce2015-05-12 23:23:00 +02001149 pci_fixup_irqs(pci_common_swizzle, pcie->map_irq);
Ray Jui1fb37a82015-04-08 11:21:35 -07001150 pci_bus_add_devices(bus);
1151
1152 return 0;
1153
1154err_rm_root_bus:
1155 pci_stop_root_bus(bus);
1156 pci_remove_root_bus(bus);
1157
1158err_power_off_phy:
Markus Elfring93972d12015-06-28 16:42:04 +02001159 phy_power_off(pcie->phy);
Ray Jui1fb37a82015-04-08 11:21:35 -07001160err_exit_phy:
Markus Elfring93972d12015-06-28 16:42:04 +02001161 phy_exit(pcie->phy);
Ray Jui1fb37a82015-04-08 11:21:35 -07001162 return ret;
1163}
1164EXPORT_SYMBOL(iproc_pcie_setup);
1165
1166int iproc_pcie_remove(struct iproc_pcie *pcie)
1167{
1168 pci_stop_root_bus(pcie->root_bus);
1169 pci_remove_root_bus(pcie->root_bus);
1170
Ray Jui3bc2b232016-01-06 18:04:35 -06001171 iproc_pcie_msi_disable(pcie);
1172
Markus Elfring93972d12015-06-28 16:42:04 +02001173 phy_power_off(pcie->phy);
1174 phy_exit(pcie->phy);
Ray Jui1fb37a82015-04-08 11:21:35 -07001175
1176 return 0;
1177}
1178EXPORT_SYMBOL(iproc_pcie_remove);
1179
1180MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
1181MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver");
1182MODULE_LICENSE("GPL v2");