blob: f76a75bb7ae40803101f44bd5d9dae6e57bc0695 [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Ray Jui1fb37a82015-04-08 11:21:35 -07002/*
3 * Copyright (C) 2014 Hauke Mehrtens <hauke@hauke-m.de>
Florian Fainellibe908d22015-10-16 12:04:04 -07004 * Copyright (C) 2015 Broadcom Corporation
Ray Jui1fb37a82015-04-08 11:21:35 -07005 */
6
7#include <linux/kernel.h>
8#include <linux/pci.h>
9#include <linux/msi.h>
10#include <linux/clk.h>
11#include <linux/module.h>
12#include <linux/mbus.h>
13#include <linux/slab.h>
14#include <linux/delay.h>
15#include <linux/interrupt.h>
Ray Jui787b3c42016-10-31 17:38:35 -070016#include <linux/irqchip/arm-gic-v3.h>
Ray Jui1fb37a82015-04-08 11:21:35 -070017#include <linux/platform_device.h>
18#include <linux/of_address.h>
19#include <linux/of_pci.h>
20#include <linux/of_irq.h>
21#include <linux/of_platform.h>
22#include <linux/phy/phy.h>
23
24#include "pcie-iproc.h"
25
Bjorn Helgaasef685b32017-09-05 12:33:33 -050026#define EP_PERST_SOURCE_SELECT_SHIFT 2
27#define EP_PERST_SOURCE_SELECT BIT(EP_PERST_SOURCE_SELECT_SHIFT)
28#define EP_MODE_SURVIVE_PERST_SHIFT 1
29#define EP_MODE_SURVIVE_PERST BIT(EP_MODE_SURVIVE_PERST_SHIFT)
30#define RC_PCIE_RST_OUTPUT_SHIFT 0
31#define RC_PCIE_RST_OUTPUT BIT(RC_PCIE_RST_OUTPUT_SHIFT)
32#define PAXC_RESET_MASK 0x7f
Ray Jui1fb37a82015-04-08 11:21:35 -070033
Bjorn Helgaasef685b32017-09-05 12:33:33 -050034#define GIC_V3_CFG_SHIFT 0
35#define GIC_V3_CFG BIT(GIC_V3_CFG_SHIFT)
Ray Jui787b3c42016-10-31 17:38:35 -070036
Bjorn Helgaasef685b32017-09-05 12:33:33 -050037#define MSI_ENABLE_CFG_SHIFT 0
38#define MSI_ENABLE_CFG BIT(MSI_ENABLE_CFG_SHIFT)
Ray Jui787b3c42016-10-31 17:38:35 -070039
Bjorn Helgaasef685b32017-09-05 12:33:33 -050040#define CFG_IND_ADDR_MASK 0x00001ffc
Ray Jui1fb37a82015-04-08 11:21:35 -070041
Bjorn Helgaasef685b32017-09-05 12:33:33 -050042#define CFG_ADDR_BUS_NUM_SHIFT 20
43#define CFG_ADDR_BUS_NUM_MASK 0x0ff00000
44#define CFG_ADDR_DEV_NUM_SHIFT 15
45#define CFG_ADDR_DEV_NUM_MASK 0x000f8000
46#define CFG_ADDR_FUNC_NUM_SHIFT 12
47#define CFG_ADDR_FUNC_NUM_MASK 0x00007000
48#define CFG_ADDR_REG_NUM_SHIFT 2
49#define CFG_ADDR_REG_NUM_MASK 0x00000ffc
50#define CFG_ADDR_CFG_TYPE_SHIFT 0
51#define CFG_ADDR_CFG_TYPE_MASK 0x00000003
Ray Jui1fb37a82015-04-08 11:21:35 -070052
Bjorn Helgaasef685b32017-09-05 12:33:33 -050053#define SYS_RC_INTX_MASK 0xf
Ray Jui1fb37a82015-04-08 11:21:35 -070054
Bjorn Helgaasef685b32017-09-05 12:33:33 -050055#define PCIE_PHYLINKUP_SHIFT 3
56#define PCIE_PHYLINKUP BIT(PCIE_PHYLINKUP_SHIFT)
57#define PCIE_DL_ACTIVE_SHIFT 2
58#define PCIE_DL_ACTIVE BIT(PCIE_DL_ACTIVE_SHIFT)
Ray Juiaaf22ab2015-09-15 17:39:19 -070059
Bjorn Helgaasef685b32017-09-05 12:33:33 -050060#define APB_ERR_EN_SHIFT 0
61#define APB_ERR_EN BIT(APB_ERR_EN_SHIFT)
Ray Jui538928f2016-10-31 17:38:33 -070062
Bjorn Helgaasef685b32017-09-05 12:33:33 -050063#define CFG_RETRY_STATUS 0xffff0001
64#define CFG_RETRY_STATUS_TIMEOUT_US 500000 /* 500 milliseconds */
Oza Pawandeep39b7a4f2017-08-28 16:43:30 -050065
Ray Jui4213e152016-10-31 17:38:37 -070066/* derive the enum index of the outbound/inbound mapping registers */
Bjorn Helgaasef685b32017-09-05 12:33:33 -050067#define MAP_REG(base_reg, index) ((base_reg) + (index) * 2)
Ray Jui4213e152016-10-31 17:38:37 -070068
69/*
70 * Maximum number of outbound mapping window sizes that can be supported by any
71 * OARR/OMAP mapping pair
72 */
Bjorn Helgaasef685b32017-09-05 12:33:33 -050073#define MAX_NUM_OB_WINDOW_SIZES 4
Ray Jui4213e152016-10-31 17:38:37 -070074
Bjorn Helgaasef685b32017-09-05 12:33:33 -050075#define OARR_VALID_SHIFT 0
76#define OARR_VALID BIT(OARR_VALID_SHIFT)
77#define OARR_SIZE_CFG_SHIFT 1
Ray Juie99a1872015-10-16 08:18:24 -050078
Ray Juidd9d4e72016-10-31 17:38:39 -070079/*
80 * Maximum number of inbound mapping region sizes that can be supported by an
81 * IARR
82 */
Bjorn Helgaasef685b32017-09-05 12:33:33 -050083#define MAX_NUM_IB_REGION_SIZES 9
Ray Juidd9d4e72016-10-31 17:38:39 -070084
Bjorn Helgaasef685b32017-09-05 12:33:33 -050085#define IMAP_VALID_SHIFT 0
86#define IMAP_VALID BIT(IMAP_VALID_SHIFT)
Ray Juidd9d4e72016-10-31 17:38:39 -070087
Ray Jui3bc70822018-06-11 17:21:04 -070088#define IPROC_PCI_PM_CAP 0x48
89#define IPROC_PCI_PM_CAP_MASK 0xffff
Bjorn Helgaasd8fa9342017-09-05 12:27:11 -050090#define IPROC_PCI_EXP_CAP 0xac
Bjorn Helgaase3a16982016-10-06 13:36:07 -050091
Bjorn Helgaasef685b32017-09-05 12:33:33 -050092#define IPROC_PCIE_REG_INVALID 0xffff
Ray Jui943ebae2015-12-04 09:34:59 -080093
Ray Jui4213e152016-10-31 17:38:37 -070094/**
95 * iProc PCIe outbound mapping controller specific parameters
96 *
97 * @window_sizes: list of supported outbound mapping window sizes in MB
98 * @nr_sizes: number of supported outbound mapping window sizes
99 */
100struct iproc_pcie_ob_map {
101 resource_size_t window_sizes[MAX_NUM_OB_WINDOW_SIZES];
102 unsigned int nr_sizes;
103};
104
105static const struct iproc_pcie_ob_map paxb_ob_map[] = {
106 {
107 /* OARR0/OMAP0 */
108 .window_sizes = { 128, 256 },
109 .nr_sizes = 2,
110 },
111 {
112 /* OARR1/OMAP1 */
113 .window_sizes = { 128, 256 },
114 .nr_sizes = 2,
115 },
116};
117
Ray Juic7c44522016-10-31 17:38:41 -0700118static const struct iproc_pcie_ob_map paxb_v2_ob_map[] = {
119 {
120 /* OARR0/OMAP0 */
121 .window_sizes = { 128, 256 },
122 .nr_sizes = 2,
123 },
124 {
125 /* OARR1/OMAP1 */
126 .window_sizes = { 128, 256 },
127 .nr_sizes = 2,
128 },
129 {
130 /* OARR2/OMAP2 */
131 .window_sizes = { 128, 256, 512, 1024 },
132 .nr_sizes = 4,
133 },
134 {
135 /* OARR3/OMAP3 */
136 .window_sizes = { 128, 256, 512, 1024 },
137 .nr_sizes = 4,
138 },
139};
140
Ray Juidd9d4e72016-10-31 17:38:39 -0700141/**
142 * iProc PCIe inbound mapping type
143 */
144enum iproc_pcie_ib_map_type {
145 /* for DDR memory */
146 IPROC_PCIE_IB_MAP_MEM = 0,
147
148 /* for device I/O memory */
149 IPROC_PCIE_IB_MAP_IO,
150
151 /* invalid or unused */
152 IPROC_PCIE_IB_MAP_INVALID
153};
154
155/**
156 * iProc PCIe inbound mapping controller specific parameters
157 *
158 * @type: inbound mapping region type
159 * @size_unit: inbound mapping region size unit, could be SZ_1K, SZ_1M, or
160 * SZ_1G
161 * @region_sizes: list of supported inbound mapping region sizes in KB, MB, or
162 * GB, depedning on the size unit
163 * @nr_sizes: number of supported inbound mapping region sizes
164 * @nr_windows: number of supported inbound mapping windows for the region
165 * @imap_addr_offset: register offset between the upper and lower 32-bit
166 * IMAP address registers
167 * @imap_window_offset: register offset between each IMAP window
168 */
169struct iproc_pcie_ib_map {
170 enum iproc_pcie_ib_map_type type;
171 unsigned int size_unit;
172 resource_size_t region_sizes[MAX_NUM_IB_REGION_SIZES];
173 unsigned int nr_sizes;
174 unsigned int nr_windows;
175 u16 imap_addr_offset;
176 u16 imap_window_offset;
177};
178
Ray Juic7c44522016-10-31 17:38:41 -0700179static const struct iproc_pcie_ib_map paxb_v2_ib_map[] = {
180 {
181 /* IARR0/IMAP0 */
182 .type = IPROC_PCIE_IB_MAP_IO,
183 .size_unit = SZ_1K,
184 .region_sizes = { 32 },
185 .nr_sizes = 1,
186 .nr_windows = 8,
187 .imap_addr_offset = 0x40,
188 .imap_window_offset = 0x4,
189 },
190 {
191 /* IARR1/IMAP1 (currently unused) */
192 .type = IPROC_PCIE_IB_MAP_INVALID,
193 },
194 {
195 /* IARR2/IMAP2 */
196 .type = IPROC_PCIE_IB_MAP_MEM,
197 .size_unit = SZ_1M,
198 .region_sizes = { 64, 128, 256, 512, 1024, 2048, 4096, 8192,
199 16384 },
200 .nr_sizes = 9,
201 .nr_windows = 1,
202 .imap_addr_offset = 0x4,
203 .imap_window_offset = 0x8,
204 },
205 {
206 /* IARR3/IMAP3 */
207 .type = IPROC_PCIE_IB_MAP_MEM,
208 .size_unit = SZ_1G,
209 .region_sizes = { 1, 2, 4, 8, 16, 32 },
210 .nr_sizes = 6,
211 .nr_windows = 8,
212 .imap_addr_offset = 0x4,
213 .imap_window_offset = 0x8,
214 },
215 {
216 /* IARR4/IMAP4 */
217 .type = IPROC_PCIE_IB_MAP_MEM,
218 .size_unit = SZ_1G,
219 .region_sizes = { 32, 64, 128, 256, 512 },
220 .nr_sizes = 5,
221 .nr_windows = 8,
222 .imap_addr_offset = 0x4,
223 .imap_window_offset = 0x8,
224 },
225};
226
Ray Jui06324ed2016-10-31 17:38:30 -0700227/*
228 * iProc PCIe host registers
229 */
Ray Jui943ebae2015-12-04 09:34:59 -0800230enum iproc_pcie_reg {
Ray Jui06324ed2016-10-31 17:38:30 -0700231 /* clock/reset signal control */
Ray Jui943ebae2015-12-04 09:34:59 -0800232 IPROC_PCIE_CLK_CTRL = 0,
Ray Jui06324ed2016-10-31 17:38:30 -0700233
Ray Jui787b3c42016-10-31 17:38:35 -0700234 /*
235 * To allow MSI to be steered to an external MSI controller (e.g., ARM
236 * GICv3 ITS)
237 */
238 IPROC_PCIE_MSI_GIC_MODE,
239
240 /*
241 * IPROC_PCIE_MSI_BASE_ADDR and IPROC_PCIE_MSI_WINDOW_SIZE define the
242 * window where the MSI posted writes are written, for the writes to be
243 * interpreted as MSI writes.
244 */
245 IPROC_PCIE_MSI_BASE_ADDR,
246 IPROC_PCIE_MSI_WINDOW_SIZE,
247
248 /*
249 * To hold the address of the register where the MSI writes are
250 * programed. When ARM GICv3 ITS is used, this should be programmed
251 * with the address of the GITS_TRANSLATER register.
252 */
253 IPROC_PCIE_MSI_ADDR_LO,
254 IPROC_PCIE_MSI_ADDR_HI,
255
256 /* enable MSI */
257 IPROC_PCIE_MSI_EN_CFG,
258
Ray Jui06324ed2016-10-31 17:38:30 -0700259 /* allow access to root complex configuration space */
Ray Jui943ebae2015-12-04 09:34:59 -0800260 IPROC_PCIE_CFG_IND_ADDR,
261 IPROC_PCIE_CFG_IND_DATA,
Ray Jui06324ed2016-10-31 17:38:30 -0700262
263 /* allow access to device configuration space */
Ray Jui943ebae2015-12-04 09:34:59 -0800264 IPROC_PCIE_CFG_ADDR,
265 IPROC_PCIE_CFG_DATA,
Ray Jui06324ed2016-10-31 17:38:30 -0700266
267 /* enable INTx */
Ray Jui943ebae2015-12-04 09:34:59 -0800268 IPROC_PCIE_INTX_EN,
Ray Jui06324ed2016-10-31 17:38:30 -0700269
270 /* outbound address mapping */
Ray Jui4213e152016-10-31 17:38:37 -0700271 IPROC_PCIE_OARR0,
272 IPROC_PCIE_OMAP0,
273 IPROC_PCIE_OARR1,
274 IPROC_PCIE_OMAP1,
275 IPROC_PCIE_OARR2,
276 IPROC_PCIE_OMAP2,
277 IPROC_PCIE_OARR3,
278 IPROC_PCIE_OMAP3,
Ray Jui06324ed2016-10-31 17:38:30 -0700279
Ray Juidd9d4e72016-10-31 17:38:39 -0700280 /* inbound address mapping */
281 IPROC_PCIE_IARR0,
282 IPROC_PCIE_IMAP0,
283 IPROC_PCIE_IARR1,
284 IPROC_PCIE_IMAP1,
285 IPROC_PCIE_IARR2,
286 IPROC_PCIE_IMAP2,
287 IPROC_PCIE_IARR3,
288 IPROC_PCIE_IMAP3,
289 IPROC_PCIE_IARR4,
290 IPROC_PCIE_IMAP4,
291
Ray Jui06324ed2016-10-31 17:38:30 -0700292 /* link status */
Ray Jui943ebae2015-12-04 09:34:59 -0800293 IPROC_PCIE_LINK_STATUS,
Ray Jui06324ed2016-10-31 17:38:30 -0700294
Ray Jui538928f2016-10-31 17:38:33 -0700295 /* enable APB error for unsupported requests */
296 IPROC_PCIE_APB_ERR_EN,
297
Ray Jui06324ed2016-10-31 17:38:30 -0700298 /* total number of core registers */
299 IPROC_PCIE_MAX_NUM_REG,
Ray Jui943ebae2015-12-04 09:34:59 -0800300};
301
Ray Jui404349c2016-10-31 17:38:32 -0700302/* iProc PCIe PAXB BCMA registers */
303static const u16 iproc_pcie_reg_paxb_bcma[] = {
Bjorn Helgaasef685b32017-09-05 12:33:33 -0500304 [IPROC_PCIE_CLK_CTRL] = 0x000,
305 [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
306 [IPROC_PCIE_CFG_IND_DATA] = 0x124,
307 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
308 [IPROC_PCIE_CFG_DATA] = 0x1fc,
309 [IPROC_PCIE_INTX_EN] = 0x330,
310 [IPROC_PCIE_LINK_STATUS] = 0xf0c,
Ray Jui404349c2016-10-31 17:38:32 -0700311};
312
Ray Jui943ebae2015-12-04 09:34:59 -0800313/* iProc PCIe PAXB registers */
314static const u16 iproc_pcie_reg_paxb[] = {
Bjorn Helgaasef685b32017-09-05 12:33:33 -0500315 [IPROC_PCIE_CLK_CTRL] = 0x000,
316 [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
317 [IPROC_PCIE_CFG_IND_DATA] = 0x124,
318 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
319 [IPROC_PCIE_CFG_DATA] = 0x1fc,
320 [IPROC_PCIE_INTX_EN] = 0x330,
321 [IPROC_PCIE_OARR0] = 0xd20,
322 [IPROC_PCIE_OMAP0] = 0xd40,
323 [IPROC_PCIE_OARR1] = 0xd28,
324 [IPROC_PCIE_OMAP1] = 0xd48,
325 [IPROC_PCIE_LINK_STATUS] = 0xf0c,
326 [IPROC_PCIE_APB_ERR_EN] = 0xf40,
Ray Jui943ebae2015-12-04 09:34:59 -0800327};
328
Ray Juic7c44522016-10-31 17:38:41 -0700329/* iProc PCIe PAXB v2 registers */
330static const u16 iproc_pcie_reg_paxb_v2[] = {
Bjorn Helgaasef685b32017-09-05 12:33:33 -0500331 [IPROC_PCIE_CLK_CTRL] = 0x000,
332 [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
333 [IPROC_PCIE_CFG_IND_DATA] = 0x124,
334 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
335 [IPROC_PCIE_CFG_DATA] = 0x1fc,
336 [IPROC_PCIE_INTX_EN] = 0x330,
337 [IPROC_PCIE_OARR0] = 0xd20,
338 [IPROC_PCIE_OMAP0] = 0xd40,
339 [IPROC_PCIE_OARR1] = 0xd28,
340 [IPROC_PCIE_OMAP1] = 0xd48,
341 [IPROC_PCIE_OARR2] = 0xd60,
342 [IPROC_PCIE_OMAP2] = 0xd68,
343 [IPROC_PCIE_OARR3] = 0xdf0,
344 [IPROC_PCIE_OMAP3] = 0xdf8,
345 [IPROC_PCIE_IARR0] = 0xd00,
346 [IPROC_PCIE_IMAP0] = 0xc00,
347 [IPROC_PCIE_IARR2] = 0xd10,
348 [IPROC_PCIE_IMAP2] = 0xcc0,
349 [IPROC_PCIE_IARR3] = 0xe00,
350 [IPROC_PCIE_IMAP3] = 0xe08,
351 [IPROC_PCIE_IARR4] = 0xe68,
352 [IPROC_PCIE_IMAP4] = 0xe70,
353 [IPROC_PCIE_LINK_STATUS] = 0xf0c,
354 [IPROC_PCIE_APB_ERR_EN] = 0xf40,
Ray Juic7c44522016-10-31 17:38:41 -0700355};
356
Ray Jui943ebae2015-12-04 09:34:59 -0800357/* iProc PCIe PAXC v1 registers */
358static const u16 iproc_pcie_reg_paxc[] = {
Bjorn Helgaasef685b32017-09-05 12:33:33 -0500359 [IPROC_PCIE_CLK_CTRL] = 0x000,
360 [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0,
361 [IPROC_PCIE_CFG_IND_DATA] = 0x1f4,
362 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
363 [IPROC_PCIE_CFG_DATA] = 0x1fc,
Ray Jui943ebae2015-12-04 09:34:59 -0800364};
Ray Juie99a1872015-10-16 08:18:24 -0500365
Ray Jui787b3c42016-10-31 17:38:35 -0700366/* iProc PCIe PAXC v2 registers */
367static const u16 iproc_pcie_reg_paxc_v2[] = {
Bjorn Helgaasef685b32017-09-05 12:33:33 -0500368 [IPROC_PCIE_MSI_GIC_MODE] = 0x050,
369 [IPROC_PCIE_MSI_BASE_ADDR] = 0x074,
370 [IPROC_PCIE_MSI_WINDOW_SIZE] = 0x078,
371 [IPROC_PCIE_MSI_ADDR_LO] = 0x07c,
372 [IPROC_PCIE_MSI_ADDR_HI] = 0x080,
373 [IPROC_PCIE_MSI_EN_CFG] = 0x09c,
374 [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0,
375 [IPROC_PCIE_CFG_IND_DATA] = 0x1f4,
376 [IPROC_PCIE_CFG_ADDR] = 0x1f8,
377 [IPROC_PCIE_CFG_DATA] = 0x1fc,
Ray Jui787b3c42016-10-31 17:38:35 -0700378};
379
Ray Jui3bc70822018-06-11 17:21:04 -0700380/*
381 * List of device IDs of controllers that have corrupted capability list that
382 * require SW fixup
383 */
384static const u16 iproc_pcie_corrupt_cap_did[] = {
385 0x16cd,
386 0x16f0,
387 0xd802,
388 0xd804
389};
390
Ray Jui8d9bfe32015-07-21 18:29:40 -0700391static inline struct iproc_pcie *iproc_data(struct pci_bus *bus)
Ray Jui1fb37a82015-04-08 11:21:35 -0700392{
Rob Herringa1b363a2018-03-07 09:42:36 -0600393 struct iproc_pcie *pcie = bus->sysdata;
Ray Jui8d9bfe32015-07-21 18:29:40 -0700394 return pcie;
Ray Jui1fb37a82015-04-08 11:21:35 -0700395}
396
Ray Jui943ebae2015-12-04 09:34:59 -0800397static inline bool iproc_pcie_reg_is_invalid(u16 reg_offset)
398{
399 return !!(reg_offset == IPROC_PCIE_REG_INVALID);
400}
401
402static inline u16 iproc_pcie_reg_offset(struct iproc_pcie *pcie,
403 enum iproc_pcie_reg reg)
404{
405 return pcie->reg_offsets[reg];
406}
407
408static inline u32 iproc_pcie_read_reg(struct iproc_pcie *pcie,
409 enum iproc_pcie_reg reg)
410{
411 u16 offset = iproc_pcie_reg_offset(pcie, reg);
412
413 if (iproc_pcie_reg_is_invalid(offset))
414 return 0;
415
416 return readl(pcie->base + offset);
417}
418
419static inline void iproc_pcie_write_reg(struct iproc_pcie *pcie,
420 enum iproc_pcie_reg reg, u32 val)
421{
422 u16 offset = iproc_pcie_reg_offset(pcie, reg);
423
424 if (iproc_pcie_reg_is_invalid(offset))
425 return;
426
427 writel(val, pcie->base + offset);
428}
429
Ray Jui538928f2016-10-31 17:38:33 -0700430/**
431 * APB error forwarding can be disabled during access of configuration
432 * registers of the endpoint device, to prevent unsupported requests
433 * (typically seen during enumeration with multi-function devices) from
434 * triggering a system exception.
435 */
436static inline void iproc_pcie_apb_err_disable(struct pci_bus *bus,
437 bool disable)
438{
439 struct iproc_pcie *pcie = iproc_data(bus);
440 u32 val;
441
442 if (bus->number && pcie->has_apb_err_disable) {
443 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_APB_ERR_EN);
444 if (disable)
445 val &= ~APB_ERR_EN;
446 else
447 val |= APB_ERR_EN;
448 iproc_pcie_write_reg(pcie, IPROC_PCIE_APB_ERR_EN, val);
449 }
450}
451
Oza Pawandeepd0050452017-08-28 16:43:24 -0500452static void __iomem *iproc_pcie_map_ep_cfg_reg(struct iproc_pcie *pcie,
453 unsigned int busno,
454 unsigned int slot,
455 unsigned int fn,
456 int where)
457{
458 u16 offset;
459 u32 val;
460
461 /* EP device access */
462 val = (busno << CFG_ADDR_BUS_NUM_SHIFT) |
463 (slot << CFG_ADDR_DEV_NUM_SHIFT) |
464 (fn << CFG_ADDR_FUNC_NUM_SHIFT) |
465 (where & CFG_ADDR_REG_NUM_MASK) |
466 (1 & CFG_ADDR_CFG_TYPE_MASK);
467
468 iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_ADDR, val);
469 offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_DATA);
470
471 if (iproc_pcie_reg_is_invalid(offset))
472 return NULL;
473
474 return (pcie->base + offset);
475}
476
Oza Pawandeep39b7a4f2017-08-28 16:43:30 -0500477static unsigned int iproc_pcie_cfg_retry(void __iomem *cfg_data_p)
478{
479 int timeout = CFG_RETRY_STATUS_TIMEOUT_US;
480 unsigned int data;
481
482 /*
483 * As per PCIe spec r3.1, sec 2.3.2, CRS Software Visibility only
484 * affects config reads of the Vendor ID. For config writes or any
485 * other config reads, the Root may automatically reissue the
486 * configuration request again as a new request.
487 *
488 * For config reads, this hardware returns CFG_RETRY_STATUS data
489 * when it receives a CRS completion, regardless of the address of
490 * the read or the CRS Software Visibility Enable bit. As a
491 * partial workaround for this, we retry in software any read that
492 * returns CFG_RETRY_STATUS.
493 *
494 * Note that a non-Vendor ID config register may have a value of
495 * CFG_RETRY_STATUS. If we read that, we can't distinguish it from
496 * a CRS completion, so we will incorrectly retry the read and
497 * eventually return the wrong data (0xffffffff).
498 */
499 data = readl(cfg_data_p);
500 while (data == CFG_RETRY_STATUS && timeout--) {
501 udelay(1);
502 data = readl(cfg_data_p);
503 }
504
505 if (data == CFG_RETRY_STATUS)
506 data = 0xffffffff;
507
508 return data;
509}
510
Ray Jui3bc70822018-06-11 17:21:04 -0700511static void iproc_pcie_fix_cap(struct iproc_pcie *pcie, int where, u32 *val)
512{
513 u32 i, dev_id;
514
515 switch (where & ~0x3) {
516 case PCI_VENDOR_ID:
517 dev_id = *val >> 16;
518
519 /*
520 * Activate fixup for those controllers that have corrupted
521 * capability list registers
522 */
523 for (i = 0; i < ARRAY_SIZE(iproc_pcie_corrupt_cap_did); i++)
524 if (dev_id == iproc_pcie_corrupt_cap_did[i])
525 pcie->fix_paxc_cap = true;
526 break;
527
528 case IPROC_PCI_PM_CAP:
529 if (pcie->fix_paxc_cap) {
530 /* advertise PM, force next capability to PCIe */
531 *val &= ~IPROC_PCI_PM_CAP_MASK;
532 *val |= IPROC_PCI_EXP_CAP << 8 | PCI_CAP_ID_PM;
533 }
534 break;
535
536 case IPROC_PCI_EXP_CAP:
537 if (pcie->fix_paxc_cap) {
538 /* advertise root port, version 2, terminate here */
539 *val = (PCI_EXP_TYPE_ROOT_PORT << 4 | 2) << 16 |
540 PCI_CAP_ID_EXP;
541 }
542 break;
543
544 case IPROC_PCI_EXP_CAP + PCI_EXP_RTCTL:
545 /* Don't advertise CRS SV support */
546 *val &= ~(PCI_EXP_RTCAP_CRSVIS << 16);
547 break;
548
549 default:
550 break;
551 }
552}
553
Oza Pawandeep39b7a4f2017-08-28 16:43:30 -0500554static int iproc_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
Bjorn Helgaasef685b32017-09-05 12:33:33 -0500555 int where, int size, u32 *val)
Oza Pawandeep39b7a4f2017-08-28 16:43:30 -0500556{
557 struct iproc_pcie *pcie = iproc_data(bus);
558 unsigned int slot = PCI_SLOT(devfn);
559 unsigned int fn = PCI_FUNC(devfn);
560 unsigned int busno = bus->number;
561 void __iomem *cfg_data_p;
562 unsigned int data;
563 int ret;
564
565 /* root complex access */
566 if (busno == 0) {
567 ret = pci_generic_config_read32(bus, devfn, where, size, val);
Ray Jui3bc70822018-06-11 17:21:04 -0700568 if (ret == PCIBIOS_SUCCESSFUL)
569 iproc_pcie_fix_cap(pcie, where, val);
Oza Pawandeep39b7a4f2017-08-28 16:43:30 -0500570
Ray Jui3bc70822018-06-11 17:21:04 -0700571 return ret;
Oza Pawandeep39b7a4f2017-08-28 16:43:30 -0500572 }
573
574 cfg_data_p = iproc_pcie_map_ep_cfg_reg(pcie, busno, slot, fn, where);
575
576 if (!cfg_data_p)
577 return PCIBIOS_DEVICE_NOT_FOUND;
578
579 data = iproc_pcie_cfg_retry(cfg_data_p);
580
581 *val = data;
582 if (size <= 2)
583 *val = (data >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
584
Ray Juif78e60a2018-06-11 17:21:06 -0700585 /*
586 * For PAXC and PAXCv2, the total number of PFs that one can enumerate
587 * depends on the firmware configuration. Unfortunately, due to an ASIC
588 * bug, unconfigured PFs cannot be properly hidden from the root
589 * complex. As a result, write access to these PFs will cause bus lock
590 * up on the embedded processor
591 *
592 * Since all unconfigured PFs are left with an incorrect, staled device
593 * ID of 0x168e (PCI_DEVICE_ID_NX2_57810), we try to catch those access
594 * early here and reject them all
595 */
596#define DEVICE_ID_MASK 0xffff0000
597#define DEVICE_ID_SHIFT 16
598 if (pcie->rej_unconfig_pf &&
599 (where & CFG_ADDR_REG_NUM_MASK) == PCI_VENDOR_ID)
600 if ((*val & DEVICE_ID_MASK) ==
601 (PCI_DEVICE_ID_NX2_57810 << DEVICE_ID_SHIFT))
602 return PCIBIOS_FUNC_NOT_SUPPORTED;
603
Oza Pawandeep39b7a4f2017-08-28 16:43:30 -0500604 return PCIBIOS_SUCCESSFUL;
605}
606
Ray Jui1fb37a82015-04-08 11:21:35 -0700607/**
608 * Note access to the configuration registers are protected at the higher layer
609 * by 'pci_lock' in drivers/pci/access.c
610 */
Lorenzo Pieralisi022adcf2017-06-28 15:13:50 -0500611static void __iomem *iproc_pcie_map_cfg_bus(struct iproc_pcie *pcie,
Bjorn Helgaasef685b32017-09-05 12:33:33 -0500612 int busno, unsigned int devfn,
Ray Jui1fb37a82015-04-08 11:21:35 -0700613 int where)
614{
Ray Jui1fb37a82015-04-08 11:21:35 -0700615 unsigned slot = PCI_SLOT(devfn);
616 unsigned fn = PCI_FUNC(devfn);
Ray Jui943ebae2015-12-04 09:34:59 -0800617 u16 offset;
618
Ray Jui1fb37a82015-04-08 11:21:35 -0700619 /* root complex access */
620 if (busno == 0) {
Ray Jui46560382016-01-27 16:52:24 -0600621 if (slot > 0 || fn > 0)
622 return NULL;
623
Ray Jui943ebae2015-12-04 09:34:59 -0800624 iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_IND_ADDR,
625 where & CFG_IND_ADDR_MASK);
626 offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_IND_DATA);
627 if (iproc_pcie_reg_is_invalid(offset))
Ray Jui1fb37a82015-04-08 11:21:35 -0700628 return NULL;
Ray Jui943ebae2015-12-04 09:34:59 -0800629 else
630 return (pcie->base + offset);
Ray Jui1fb37a82015-04-08 11:21:35 -0700631 }
632
Oza Pawandeepd0050452017-08-28 16:43:24 -0500633 return iproc_pcie_map_ep_cfg_reg(pcie, busno, slot, fn, where);
Ray Jui1fb37a82015-04-08 11:21:35 -0700634}
635
Lorenzo Pieralisi022adcf2017-06-28 15:13:50 -0500636static void __iomem *iproc_pcie_bus_map_cfg_bus(struct pci_bus *bus,
637 unsigned int devfn,
638 int where)
639{
640 return iproc_pcie_map_cfg_bus(iproc_data(bus), bus->number, devfn,
641 where);
642}
643
644static int iproc_pci_raw_config_read32(struct iproc_pcie *pcie,
645 unsigned int devfn, int where,
646 int size, u32 *val)
647{
648 void __iomem *addr;
649
650 addr = iproc_pcie_map_cfg_bus(pcie, 0, devfn, where & ~0x3);
651 if (!addr) {
652 *val = ~0;
653 return PCIBIOS_DEVICE_NOT_FOUND;
654 }
655
656 *val = readl(addr);
657
658 if (size <= 2)
659 *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
660
661 return PCIBIOS_SUCCESSFUL;
662}
663
664static int iproc_pci_raw_config_write32(struct iproc_pcie *pcie,
665 unsigned int devfn, int where,
666 int size, u32 val)
667{
668 void __iomem *addr;
669 u32 mask, tmp;
670
671 addr = iproc_pcie_map_cfg_bus(pcie, 0, devfn, where & ~0x3);
672 if (!addr)
673 return PCIBIOS_DEVICE_NOT_FOUND;
674
675 if (size == 4) {
676 writel(val, addr);
677 return PCIBIOS_SUCCESSFUL;
678 }
679
680 mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
681 tmp = readl(addr) & mask;
682 tmp |= val << ((where & 0x3) * 8);
683 writel(tmp, addr);
684
685 return PCIBIOS_SUCCESSFUL;
686}
687
Ray Jui538928f2016-10-31 17:38:33 -0700688static int iproc_pcie_config_read32(struct pci_bus *bus, unsigned int devfn,
689 int where, int size, u32 *val)
690{
691 int ret;
Oza Pawandeep39b7a4f2017-08-28 16:43:30 -0500692 struct iproc_pcie *pcie = iproc_data(bus);
Ray Jui538928f2016-10-31 17:38:33 -0700693
694 iproc_pcie_apb_err_disable(bus, true);
Ray Juif78e60a2018-06-11 17:21:06 -0700695 if (pcie->iproc_cfg_read)
Oza Pawandeep39b7a4f2017-08-28 16:43:30 -0500696 ret = iproc_pcie_config_read(bus, devfn, where, size, val);
697 else
698 ret = pci_generic_config_read32(bus, devfn, where, size, val);
Ray Jui538928f2016-10-31 17:38:33 -0700699 iproc_pcie_apb_err_disable(bus, false);
700
701 return ret;
702}
703
704static int iproc_pcie_config_write32(struct pci_bus *bus, unsigned int devfn,
705 int where, int size, u32 val)
706{
707 int ret;
708
709 iproc_pcie_apb_err_disable(bus, true);
710 ret = pci_generic_config_write32(bus, devfn, where, size, val);
711 iproc_pcie_apb_err_disable(bus, false);
712
713 return ret;
714}
715
Ray Jui1fb37a82015-04-08 11:21:35 -0700716static struct pci_ops iproc_pcie_ops = {
Lorenzo Pieralisi022adcf2017-06-28 15:13:50 -0500717 .map_bus = iproc_pcie_bus_map_cfg_bus,
Ray Jui538928f2016-10-31 17:38:33 -0700718 .read = iproc_pcie_config_read32,
719 .write = iproc_pcie_config_write32,
Ray Jui1fb37a82015-04-08 11:21:35 -0700720};
721
Oza Pawandeepb91c26c2017-08-28 16:43:35 -0500722static void iproc_pcie_perst_ctrl(struct iproc_pcie *pcie, bool assert)
Ray Jui1fb37a82015-04-08 11:21:35 -0700723{
724 u32 val;
725
Ray Jui7cbd50d2016-10-31 17:38:31 -0700726 /*
727 * PAXC and the internal emulated endpoint device downstream should not
728 * be reset. If firmware has been loaded on the endpoint device at an
729 * earlier boot stage, reset here causes issues.
730 */
731 if (pcie->ep_is_internal)
Ray Jui943ebae2015-12-04 09:34:59 -0800732 return;
Ray Jui943ebae2015-12-04 09:34:59 -0800733
Oza Pawandeepb91c26c2017-08-28 16:43:35 -0500734 if (assert) {
735 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
736 val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST &
737 ~RC_PCIE_RST_OUTPUT;
738 iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
739 udelay(250);
740 } else {
741 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
742 val |= RC_PCIE_RST_OUTPUT;
743 iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
744 msleep(100);
745 }
Ray Jui1fb37a82015-04-08 11:21:35 -0700746}
747
Oza Pawandeepb91c26c2017-08-28 16:43:35 -0500748int iproc_pcie_shutdown(struct iproc_pcie *pcie)
749{
750 iproc_pcie_perst_ctrl(pcie, true);
751 msleep(500);
752
753 return 0;
754}
755EXPORT_SYMBOL_GPL(iproc_pcie_shutdown);
756
Lorenzo Pieralisi022adcf2017-06-28 15:13:50 -0500757static int iproc_pcie_check_link(struct iproc_pcie *pcie)
Ray Jui1fb37a82015-04-08 11:21:35 -0700758{
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500759 struct device *dev = pcie->dev;
Lorenzo Pieralisi022adcf2017-06-28 15:13:50 -0500760 u32 hdr_type, link_ctrl, link_status, class, val;
Ray Juiaaf22ab2015-09-15 17:39:19 -0700761 bool link_is_active = false;
762
Ray Jui943ebae2015-12-04 09:34:59 -0800763 /*
764 * PAXC connects to emulated endpoint devices directly and does not
765 * have a Serdes. Therefore skip the link detection logic here.
766 */
Ray Jui06324ed2016-10-31 17:38:30 -0700767 if (pcie->ep_is_internal)
Ray Jui943ebae2015-12-04 09:34:59 -0800768 return 0;
769
770 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_LINK_STATUS);
Ray Juiaaf22ab2015-09-15 17:39:19 -0700771 if (!(val & PCIE_PHYLINKUP) || !(val & PCIE_DL_ACTIVE)) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500772 dev_err(dev, "PHY or data link is INACTIVE!\n");
Ray Juiaaf22ab2015-09-15 17:39:19 -0700773 return -ENODEV;
774 }
Ray Jui1fb37a82015-04-08 11:21:35 -0700775
776 /* make sure we are not in EP mode */
Bjorn Helgaasef685b32017-09-05 12:33:33 -0500777 iproc_pci_raw_config_read32(pcie, 0, PCI_HEADER_TYPE, 1, &hdr_type);
Ray Jui1fb37a82015-04-08 11:21:35 -0700778 if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500779 dev_err(dev, "in EP mode, hdr=%#02x\n", hdr_type);
Ray Jui1fb37a82015-04-08 11:21:35 -0700780 return -EFAULT;
781 }
782
783 /* force class to PCI_CLASS_BRIDGE_PCI (0x0604) */
Bjorn Helgaasef685b32017-09-05 12:33:33 -0500784#define PCI_BRIDGE_CTRL_REG_OFFSET 0x43c
785#define PCI_CLASS_BRIDGE_MASK 0xffff00
786#define PCI_CLASS_BRIDGE_SHIFT 8
Lorenzo Pieralisi022adcf2017-06-28 15:13:50 -0500787 iproc_pci_raw_config_read32(pcie, 0, PCI_BRIDGE_CTRL_REG_OFFSET,
788 4, &class);
Ray Juiaaf22ab2015-09-15 17:39:19 -0700789 class &= ~PCI_CLASS_BRIDGE_MASK;
790 class |= (PCI_CLASS_BRIDGE_PCI << PCI_CLASS_BRIDGE_SHIFT);
Lorenzo Pieralisi022adcf2017-06-28 15:13:50 -0500791 iproc_pci_raw_config_write32(pcie, 0, PCI_BRIDGE_CTRL_REG_OFFSET,
792 4, class);
Ray Jui1fb37a82015-04-08 11:21:35 -0700793
794 /* check link status to see if link is active */
Bjorn Helgaasd8fa9342017-09-05 12:27:11 -0500795 iproc_pci_raw_config_read32(pcie, 0, IPROC_PCI_EXP_CAP + PCI_EXP_LNKSTA,
Lorenzo Pieralisi022adcf2017-06-28 15:13:50 -0500796 2, &link_status);
Ray Jui1fb37a82015-04-08 11:21:35 -0700797 if (link_status & PCI_EXP_LNKSTA_NLW)
Ray Juiaaf22ab2015-09-15 17:39:19 -0700798 link_is_active = true;
Ray Jui1fb37a82015-04-08 11:21:35 -0700799
800 if (!link_is_active) {
801 /* try GEN 1 link speed */
Bjorn Helgaasef685b32017-09-05 12:33:33 -0500802#define PCI_TARGET_LINK_SPEED_MASK 0xf
803#define PCI_TARGET_LINK_SPEED_GEN2 0x2
804#define PCI_TARGET_LINK_SPEED_GEN1 0x1
Lorenzo Pieralisi022adcf2017-06-28 15:13:50 -0500805 iproc_pci_raw_config_read32(pcie, 0,
Bjorn Helgaasd8fa9342017-09-05 12:27:11 -0500806 IPROC_PCI_EXP_CAP + PCI_EXP_LNKCTL2,
807 4, &link_ctrl);
Ray Jui1fb37a82015-04-08 11:21:35 -0700808 if ((link_ctrl & PCI_TARGET_LINK_SPEED_MASK) ==
809 PCI_TARGET_LINK_SPEED_GEN2) {
810 link_ctrl &= ~PCI_TARGET_LINK_SPEED_MASK;
811 link_ctrl |= PCI_TARGET_LINK_SPEED_GEN1;
Lorenzo Pieralisi022adcf2017-06-28 15:13:50 -0500812 iproc_pci_raw_config_write32(pcie, 0,
Bjorn Helgaasd8fa9342017-09-05 12:27:11 -0500813 IPROC_PCI_EXP_CAP + PCI_EXP_LNKCTL2,
814 4, link_ctrl);
Ray Jui1fb37a82015-04-08 11:21:35 -0700815 msleep(100);
816
Lorenzo Pieralisi022adcf2017-06-28 15:13:50 -0500817 iproc_pci_raw_config_read32(pcie, 0,
Bjorn Helgaasd8fa9342017-09-05 12:27:11 -0500818 IPROC_PCI_EXP_CAP + PCI_EXP_LNKSTA,
819 2, &link_status);
Ray Jui1fb37a82015-04-08 11:21:35 -0700820 if (link_status & PCI_EXP_LNKSTA_NLW)
Ray Juiaaf22ab2015-09-15 17:39:19 -0700821 link_is_active = true;
Ray Jui1fb37a82015-04-08 11:21:35 -0700822 }
823 }
824
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500825 dev_info(dev, "link: %s\n", link_is_active ? "UP" : "DOWN");
Ray Jui1fb37a82015-04-08 11:21:35 -0700826
827 return link_is_active ? 0 : -ENODEV;
828}
829
830static void iproc_pcie_enable(struct iproc_pcie *pcie)
831{
Ray Jui943ebae2015-12-04 09:34:59 -0800832 iproc_pcie_write_reg(pcie, IPROC_PCIE_INTX_EN, SYS_RC_INTX_MASK);
Ray Jui1fb37a82015-04-08 11:21:35 -0700833}
834
Ray Jui4213e152016-10-31 17:38:37 -0700835static inline bool iproc_pcie_ob_is_valid(struct iproc_pcie *pcie,
836 int window_idx)
837{
838 u32 val;
839
840 val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_OARR0, window_idx));
841
842 return !!(val & OARR_VALID);
843}
844
845static inline int iproc_pcie_ob_write(struct iproc_pcie *pcie, int window_idx,
846 int size_idx, u64 axi_addr, u64 pci_addr)
847{
848 struct device *dev = pcie->dev;
849 u16 oarr_offset, omap_offset;
850
851 /*
852 * Derive the OARR/OMAP offset from the first pair (OARR0/OMAP0) based
853 * on window index.
854 */
855 oarr_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OARR0,
856 window_idx));
857 omap_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OMAP0,
858 window_idx));
859 if (iproc_pcie_reg_is_invalid(oarr_offset) ||
860 iproc_pcie_reg_is_invalid(omap_offset))
861 return -EINVAL;
862
863 /*
864 * Program the OARR registers. The upper 32-bit OARR register is
865 * always right after the lower 32-bit OARR register.
866 */
867 writel(lower_32_bits(axi_addr) | (size_idx << OARR_SIZE_CFG_SHIFT) |
868 OARR_VALID, pcie->base + oarr_offset);
869 writel(upper_32_bits(axi_addr), pcie->base + oarr_offset + 4);
870
871 /* now program the OMAP registers */
872 writel(lower_32_bits(pci_addr), pcie->base + omap_offset);
873 writel(upper_32_bits(pci_addr), pcie->base + omap_offset + 4);
874
Ray Jui0043d4a2018-06-11 17:21:07 -0700875 dev_dbg(dev, "ob window [%d]: offset 0x%x axi %pap pci %pap\n",
876 window_idx, oarr_offset, &axi_addr, &pci_addr);
877 dev_dbg(dev, "oarr lo 0x%x oarr hi 0x%x\n",
878 readl(pcie->base + oarr_offset),
879 readl(pcie->base + oarr_offset + 4));
880 dev_dbg(dev, "omap lo 0x%x omap hi 0x%x\n",
881 readl(pcie->base + omap_offset),
882 readl(pcie->base + omap_offset + 4));
Ray Jui4213e152016-10-31 17:38:37 -0700883
884 return 0;
885}
886
Ray Juie99a1872015-10-16 08:18:24 -0500887/**
888 * Some iProc SoCs require the SW to configure the outbound address mapping
889 *
890 * Outbound address translation:
891 *
892 * iproc_pcie_address = axi_address - axi_offset
893 * OARR = iproc_pcie_address
894 * OMAP = pci_addr
895 *
896 * axi_addr -> iproc_pcie_address -> OARR -> OMAP -> pci_address
897 */
898static int iproc_pcie_setup_ob(struct iproc_pcie *pcie, u64 axi_addr,
899 u64 pci_addr, resource_size_t size)
900{
901 struct iproc_pcie_ob *ob = &pcie->ob;
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500902 struct device *dev = pcie->dev;
Ray Jui4213e152016-10-31 17:38:37 -0700903 int ret = -EINVAL, window_idx, size_idx;
Ray Juie99a1872015-10-16 08:18:24 -0500904
905 if (axi_addr < ob->axi_offset) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500906 dev_err(dev, "axi address %pap less than offset %pap\n",
Ray Juie99a1872015-10-16 08:18:24 -0500907 &axi_addr, &ob->axi_offset);
908 return -EINVAL;
909 }
910
911 /*
912 * Translate the AXI address to the internal address used by the iProc
913 * PCIe core before programming the OARR
914 */
915 axi_addr -= ob->axi_offset;
916
Ray Jui4213e152016-10-31 17:38:37 -0700917 /* iterate through all OARR/OMAP mapping windows */
918 for (window_idx = ob->nr_windows - 1; window_idx >= 0; window_idx--) {
919 const struct iproc_pcie_ob_map *ob_map =
920 &pcie->ob_map[window_idx];
Ray Juie99a1872015-10-16 08:18:24 -0500921
Ray Jui4213e152016-10-31 17:38:37 -0700922 /*
923 * If current outbound window is already in use, move on to the
924 * next one.
925 */
926 if (iproc_pcie_ob_is_valid(pcie, window_idx))
927 continue;
928
929 /*
930 * Iterate through all supported window sizes within the
931 * OARR/OMAP pair to find a match. Go through the window sizes
932 * in a descending order.
933 */
934 for (size_idx = ob_map->nr_sizes - 1; size_idx >= 0;
935 size_idx--) {
936 resource_size_t window_size =
937 ob_map->window_sizes[size_idx] * SZ_1M;
938
939 if (size < window_size)
940 continue;
941
942 if (!IS_ALIGNED(axi_addr, window_size) ||
943 !IS_ALIGNED(pci_addr, window_size)) {
944 dev_err(dev,
945 "axi %pap or pci %pap not aligned\n",
946 &axi_addr, &pci_addr);
947 return -EINVAL;
948 }
949
950 /*
951 * Match found! Program both OARR and OMAP and mark
952 * them as a valid entry.
953 */
954 ret = iproc_pcie_ob_write(pcie, window_idx, size_idx,
955 axi_addr, pci_addr);
956 if (ret)
957 goto err_ob;
958
959 size -= window_size;
960 if (size == 0)
961 return 0;
962
963 /*
964 * If we are here, we are done with the current window,
965 * but not yet finished all mappings. Need to move on
966 * to the next window.
967 */
968 axi_addr += window_size;
969 pci_addr += window_size;
Ray Juie99a1872015-10-16 08:18:24 -0500970 break;
Ray Jui4213e152016-10-31 17:38:37 -0700971 }
Ray Juie99a1872015-10-16 08:18:24 -0500972 }
973
Ray Jui4213e152016-10-31 17:38:37 -0700974err_ob:
975 dev_err(dev, "unable to configure outbound mapping\n");
976 dev_err(dev,
977 "axi %pap, axi offset %pap, pci %pap, res size %pap\n",
978 &axi_addr, &ob->axi_offset, &pci_addr, &size);
979
980 return ret;
Ray Juie99a1872015-10-16 08:18:24 -0500981}
982
983static int iproc_pcie_map_ranges(struct iproc_pcie *pcie,
984 struct list_head *resources)
985{
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500986 struct device *dev = pcie->dev;
Ray Juie99a1872015-10-16 08:18:24 -0500987 struct resource_entry *window;
988 int ret;
989
990 resource_list_for_each_entry(window, resources) {
991 struct resource *res = window->res;
992 u64 res_type = resource_type(res);
993
994 switch (res_type) {
995 case IORESOURCE_IO:
996 case IORESOURCE_BUS:
997 break;
998 case IORESOURCE_MEM:
999 ret = iproc_pcie_setup_ob(pcie, res->start,
1000 res->start - window->offset,
1001 resource_size(res));
1002 if (ret)
1003 return ret;
1004 break;
1005 default:
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001006 dev_err(dev, "invalid resource %pR\n", res);
Ray Juie99a1872015-10-16 08:18:24 -05001007 return -EINVAL;
1008 }
1009 }
1010
1011 return 0;
1012}
1013
Ray Juidd9d4e72016-10-31 17:38:39 -07001014static inline bool iproc_pcie_ib_is_in_use(struct iproc_pcie *pcie,
1015 int region_idx)
1016{
1017 const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
1018 u32 val;
1019
1020 val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_IARR0, region_idx));
1021
1022 return !!(val & (BIT(ib_map->nr_sizes) - 1));
1023}
1024
1025static inline bool iproc_pcie_ib_check_type(const struct iproc_pcie_ib_map *ib_map,
1026 enum iproc_pcie_ib_map_type type)
1027{
1028 return !!(ib_map->type == type);
1029}
1030
1031static int iproc_pcie_ib_write(struct iproc_pcie *pcie, int region_idx,
1032 int size_idx, int nr_windows, u64 axi_addr,
1033 u64 pci_addr, resource_size_t size)
1034{
1035 struct device *dev = pcie->dev;
1036 const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
1037 u16 iarr_offset, imap_offset;
1038 u32 val;
1039 int window_idx;
1040
1041 iarr_offset = iproc_pcie_reg_offset(pcie,
1042 MAP_REG(IPROC_PCIE_IARR0, region_idx));
1043 imap_offset = iproc_pcie_reg_offset(pcie,
1044 MAP_REG(IPROC_PCIE_IMAP0, region_idx));
1045 if (iproc_pcie_reg_is_invalid(iarr_offset) ||
1046 iproc_pcie_reg_is_invalid(imap_offset))
1047 return -EINVAL;
1048
Ray Jui0043d4a2018-06-11 17:21:07 -07001049 dev_dbg(dev, "ib region [%d]: offset 0x%x axi %pap pci %pap\n",
1050 region_idx, iarr_offset, &axi_addr, &pci_addr);
Ray Juidd9d4e72016-10-31 17:38:39 -07001051
1052 /*
1053 * Program the IARR registers. The upper 32-bit IARR register is
1054 * always right after the lower 32-bit IARR register.
1055 */
1056 writel(lower_32_bits(pci_addr) | BIT(size_idx),
1057 pcie->base + iarr_offset);
1058 writel(upper_32_bits(pci_addr), pcie->base + iarr_offset + 4);
1059
Ray Jui0043d4a2018-06-11 17:21:07 -07001060 dev_dbg(dev, "iarr lo 0x%x iarr hi 0x%x\n",
1061 readl(pcie->base + iarr_offset),
1062 readl(pcie->base + iarr_offset + 4));
Ray Juidd9d4e72016-10-31 17:38:39 -07001063
1064 /*
1065 * Now program the IMAP registers. Each IARR region may have one or
1066 * more IMAP windows.
1067 */
1068 size >>= ilog2(nr_windows);
1069 for (window_idx = 0; window_idx < nr_windows; window_idx++) {
1070 val = readl(pcie->base + imap_offset);
1071 val |= lower_32_bits(axi_addr) | IMAP_VALID;
1072 writel(val, pcie->base + imap_offset);
1073 writel(upper_32_bits(axi_addr),
1074 pcie->base + imap_offset + ib_map->imap_addr_offset);
1075
Ray Jui0043d4a2018-06-11 17:21:07 -07001076 dev_dbg(dev, "imap window [%d] lo 0x%x hi 0x%x\n",
1077 window_idx, readl(pcie->base + imap_offset),
1078 readl(pcie->base + imap_offset +
1079 ib_map->imap_addr_offset));
Ray Juidd9d4e72016-10-31 17:38:39 -07001080
1081 imap_offset += ib_map->imap_window_offset;
1082 axi_addr += size;
1083 }
1084
1085 return 0;
1086}
1087
1088static int iproc_pcie_setup_ib(struct iproc_pcie *pcie,
1089 struct of_pci_range *range,
1090 enum iproc_pcie_ib_map_type type)
1091{
1092 struct device *dev = pcie->dev;
1093 struct iproc_pcie_ib *ib = &pcie->ib;
1094 int ret;
1095 unsigned int region_idx, size_idx;
1096 u64 axi_addr = range->cpu_addr, pci_addr = range->pci_addr;
1097 resource_size_t size = range->size;
1098
1099 /* iterate through all IARR mapping regions */
1100 for (region_idx = 0; region_idx < ib->nr_regions; region_idx++) {
1101 const struct iproc_pcie_ib_map *ib_map =
1102 &pcie->ib_map[region_idx];
1103
1104 /*
1105 * If current inbound region is already in use or not a
1106 * compatible type, move on to the next.
1107 */
1108 if (iproc_pcie_ib_is_in_use(pcie, region_idx) ||
1109 !iproc_pcie_ib_check_type(ib_map, type))
1110 continue;
1111
1112 /* iterate through all supported region sizes to find a match */
1113 for (size_idx = 0; size_idx < ib_map->nr_sizes; size_idx++) {
1114 resource_size_t region_size =
1115 ib_map->region_sizes[size_idx] * ib_map->size_unit;
1116
1117 if (size != region_size)
1118 continue;
1119
1120 if (!IS_ALIGNED(axi_addr, region_size) ||
1121 !IS_ALIGNED(pci_addr, region_size)) {
1122 dev_err(dev,
1123 "axi %pap or pci %pap not aligned\n",
1124 &axi_addr, &pci_addr);
1125 return -EINVAL;
1126 }
1127
1128 /* Match found! Program IARR and all IMAP windows. */
1129 ret = iproc_pcie_ib_write(pcie, region_idx, size_idx,
1130 ib_map->nr_windows, axi_addr,
1131 pci_addr, size);
1132 if (ret)
1133 goto err_ib;
1134 else
1135 return 0;
1136
1137 }
1138 }
1139 ret = -EINVAL;
1140
1141err_ib:
1142 dev_err(dev, "unable to configure inbound mapping\n");
1143 dev_err(dev, "axi %pap, pci %pap, res size %pap\n",
1144 &axi_addr, &pci_addr, &size);
1145
1146 return ret;
1147}
1148
Srinath Mannam90199c92019-05-03 19:35:34 +05301149static int iproc_pcie_add_dma_range(struct device *dev,
1150 struct list_head *resources,
1151 struct of_pci_range *range)
1152{
1153 struct resource *res;
1154 struct resource_entry *entry, *tmp;
1155 struct list_head *head = resources;
1156
1157 res = devm_kzalloc(dev, sizeof(struct resource), GFP_KERNEL);
1158 if (!res)
1159 return -ENOMEM;
1160
1161 resource_list_for_each_entry(tmp, resources) {
1162 if (tmp->res->start < range->cpu_addr)
1163 head = &tmp->node;
1164 }
1165
1166 res->start = range->cpu_addr;
1167 res->end = res->start + range->size - 1;
1168
1169 entry = resource_list_create_entry(res, 0);
1170 if (!entry)
1171 return -ENOMEM;
1172
1173 entry->offset = res->start - range->cpu_addr;
1174 resource_list_add(entry, head);
1175
1176 return 0;
1177}
1178
Ray Juidd9d4e72016-10-31 17:38:39 -07001179static int iproc_pcie_map_dma_ranges(struct iproc_pcie *pcie)
1180{
Srinath Mannam90199c92019-05-03 19:35:34 +05301181 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
Ray Juidd9d4e72016-10-31 17:38:39 -07001182 struct of_pci_range range;
1183 struct of_pci_range_parser parser;
1184 int ret;
Srinath Mannam90199c92019-05-03 19:35:34 +05301185 LIST_HEAD(resources);
Ray Juidd9d4e72016-10-31 17:38:39 -07001186
1187 /* Get the dma-ranges from DT */
Marc Gonzalez1e61a572017-09-26 12:26:55 +02001188 ret = of_pci_dma_range_parser_init(&parser, pcie->dev->of_node);
Ray Juidd9d4e72016-10-31 17:38:39 -07001189 if (ret)
1190 return ret;
1191
1192 for_each_of_pci_range(&parser, &range) {
Srinath Mannam90199c92019-05-03 19:35:34 +05301193 ret = iproc_pcie_add_dma_range(pcie->dev,
1194 &resources,
1195 &range);
1196 if (ret)
1197 goto out;
Ray Juidd9d4e72016-10-31 17:38:39 -07001198 /* Each range entry corresponds to an inbound mapping region */
1199 ret = iproc_pcie_setup_ib(pcie, &range, IPROC_PCIE_IB_MAP_MEM);
1200 if (ret)
Srinath Mannam90199c92019-05-03 19:35:34 +05301201 goto out;
Ray Juidd9d4e72016-10-31 17:38:39 -07001202 }
1203
Srinath Mannam90199c92019-05-03 19:35:34 +05301204 list_splice_init(&resources, &host->dma_ranges);
1205
Ray Juidd9d4e72016-10-31 17:38:39 -07001206 return 0;
Srinath Mannam90199c92019-05-03 19:35:34 +05301207out:
1208 pci_free_resource_list(&resources);
1209 return ret;
Ray Juidd9d4e72016-10-31 17:38:39 -07001210}
1211
Ray Jui787b3c42016-10-31 17:38:35 -07001212static int iproce_pcie_get_msi(struct iproc_pcie *pcie,
1213 struct device_node *msi_node,
1214 u64 *msi_addr)
1215{
1216 struct device *dev = pcie->dev;
1217 int ret;
1218 struct resource res;
1219
1220 /*
1221 * Check if 'msi-map' points to ARM GICv3 ITS, which is the only
1222 * supported external MSI controller that requires steering.
1223 */
1224 if (!of_device_is_compatible(msi_node, "arm,gic-v3-its")) {
1225 dev_err(dev, "unable to find compatible MSI controller\n");
1226 return -ENODEV;
1227 }
1228
1229 /* derive GITS_TRANSLATER address from GICv3 */
1230 ret = of_address_to_resource(msi_node, 0, &res);
1231 if (ret < 0) {
1232 dev_err(dev, "unable to obtain MSI controller resources\n");
1233 return ret;
1234 }
1235
1236 *msi_addr = res.start + GITS_TRANSLATER;
1237 return 0;
1238}
1239
Ray Juic7c44522016-10-31 17:38:41 -07001240static int iproc_pcie_paxb_v2_msi_steer(struct iproc_pcie *pcie, u64 msi_addr)
1241{
1242 int ret;
1243 struct of_pci_range range;
1244
1245 memset(&range, 0, sizeof(range));
1246 range.size = SZ_32K;
Ray Juifeacdb42016-11-21 17:48:30 -08001247 range.pci_addr = range.cpu_addr = msi_addr & ~(range.size - 1);
Ray Juic7c44522016-10-31 17:38:41 -07001248
1249 ret = iproc_pcie_setup_ib(pcie, &range, IPROC_PCIE_IB_MAP_IO);
1250 return ret;
1251}
1252
Ray Jui1e5748c2018-06-11 17:21:05 -07001253static void iproc_pcie_paxc_v2_msi_steer(struct iproc_pcie *pcie, u64 msi_addr,
1254 bool enable)
Ray Jui787b3c42016-10-31 17:38:35 -07001255{
1256 u32 val;
1257
Ray Jui1e5748c2018-06-11 17:21:05 -07001258 if (!enable) {
1259 /*
1260 * Disable PAXC MSI steering. All write transfers will be
1261 * treated as non-MSI transfers
1262 */
1263 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_EN_CFG);
1264 val &= ~MSI_ENABLE_CFG;
1265 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_EN_CFG, val);
1266 return;
1267 }
1268
Ray Jui787b3c42016-10-31 17:38:35 -07001269 /*
1270 * Program bits [43:13] of address of GITS_TRANSLATER register into
1271 * bits [30:0] of the MSI base address register. In fact, in all iProc
1272 * based SoCs, all I/O register bases are well below the 32-bit
1273 * boundary, so we can safely assume bits [43:32] are always zeros.
1274 */
1275 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_BASE_ADDR,
1276 (u32)(msi_addr >> 13));
1277
1278 /* use a default 8K window size */
1279 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_WINDOW_SIZE, 0);
1280
1281 /* steering MSI to GICv3 ITS */
1282 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_GIC_MODE);
1283 val |= GIC_V3_CFG;
1284 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_GIC_MODE, val);
1285
1286 /*
1287 * Program bits [43:2] of address of GITS_TRANSLATER register into the
1288 * iProc MSI address registers.
1289 */
1290 msi_addr >>= 2;
1291 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_HI,
1292 upper_32_bits(msi_addr));
1293 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_LO,
1294 lower_32_bits(msi_addr));
1295
1296 /* enable MSI */
1297 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_EN_CFG);
1298 val |= MSI_ENABLE_CFG;
1299 iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_EN_CFG, val);
1300}
1301
1302static int iproc_pcie_msi_steer(struct iproc_pcie *pcie,
1303 struct device_node *msi_node)
1304{
1305 struct device *dev = pcie->dev;
1306 int ret;
1307 u64 msi_addr;
1308
1309 ret = iproce_pcie_get_msi(pcie, msi_node, &msi_addr);
1310 if (ret < 0) {
1311 dev_err(dev, "msi steering failed\n");
1312 return ret;
1313 }
1314
1315 switch (pcie->type) {
Ray Juic7c44522016-10-31 17:38:41 -07001316 case IPROC_PCIE_PAXB_V2:
1317 ret = iproc_pcie_paxb_v2_msi_steer(pcie, msi_addr);
1318 if (ret)
1319 return ret;
1320 break;
Ray Jui787b3c42016-10-31 17:38:35 -07001321 case IPROC_PCIE_PAXC_V2:
Ray Jui1e5748c2018-06-11 17:21:05 -07001322 iproc_pcie_paxc_v2_msi_steer(pcie, msi_addr, true);
Ray Jui787b3c42016-10-31 17:38:35 -07001323 break;
1324 default:
1325 return -EINVAL;
1326 }
1327
1328 return 0;
1329}
1330
Ray Jui3bc2b232016-01-06 18:04:35 -06001331static int iproc_pcie_msi_enable(struct iproc_pcie *pcie)
1332{
1333 struct device_node *msi_node;
Ray Jui787b3c42016-10-31 17:38:35 -07001334 int ret;
1335
1336 /*
1337 * Either the "msi-parent" or the "msi-map" phandle needs to exist
1338 * for us to obtain the MSI node.
1339 */
Ray Jui3bc2b232016-01-06 18:04:35 -06001340
1341 msi_node = of_parse_phandle(pcie->dev->of_node, "msi-parent", 0);
Ray Jui787b3c42016-10-31 17:38:35 -07001342 if (!msi_node) {
1343 const __be32 *msi_map = NULL;
1344 int len;
1345 u32 phandle;
1346
1347 msi_map = of_get_property(pcie->dev->of_node, "msi-map", &len);
1348 if (!msi_map)
1349 return -ENODEV;
1350
1351 phandle = be32_to_cpup(msi_map + 1);
1352 msi_node = of_find_node_by_phandle(phandle);
1353 if (!msi_node)
1354 return -ENODEV;
1355 }
1356
1357 /*
1358 * Certain revisions of the iProc PCIe controller require additional
1359 * configurations to steer the MSI writes towards an external MSI
1360 * controller.
1361 */
1362 if (pcie->need_msi_steer) {
1363 ret = iproc_pcie_msi_steer(pcie, msi_node);
1364 if (ret)
1365 return ret;
1366 }
Ray Jui3bc2b232016-01-06 18:04:35 -06001367
1368 /*
1369 * If another MSI controller is being used, the call below should fail
1370 * but that is okay
1371 */
1372 return iproc_msi_init(pcie, msi_node);
1373}
1374
1375static void iproc_pcie_msi_disable(struct iproc_pcie *pcie)
1376{
1377 iproc_msi_exit(pcie);
1378}
1379
Ray Jui06324ed2016-10-31 17:38:30 -07001380static int iproc_pcie_rev_init(struct iproc_pcie *pcie)
1381{
1382 struct device *dev = pcie->dev;
1383 unsigned int reg_idx;
1384 const u16 *regs;
1385
1386 switch (pcie->type) {
Ray Jui404349c2016-10-31 17:38:32 -07001387 case IPROC_PCIE_PAXB_BCMA:
1388 regs = iproc_pcie_reg_paxb_bcma;
1389 break;
Ray Jui06324ed2016-10-31 17:38:30 -07001390 case IPROC_PCIE_PAXB:
1391 regs = iproc_pcie_reg_paxb;
Ray Juif78e60a2018-06-11 17:21:06 -07001392 pcie->iproc_cfg_read = true;
Ray Jui538928f2016-10-31 17:38:33 -07001393 pcie->has_apb_err_disable = true;
Ray Jui4213e152016-10-31 17:38:37 -07001394 if (pcie->need_ob_cfg) {
1395 pcie->ob_map = paxb_ob_map;
1396 pcie->ob.nr_windows = ARRAY_SIZE(paxb_ob_map);
1397 }
Ray Jui06324ed2016-10-31 17:38:30 -07001398 break;
Ray Juic7c44522016-10-31 17:38:41 -07001399 case IPROC_PCIE_PAXB_V2:
1400 regs = iproc_pcie_reg_paxb_v2;
1401 pcie->has_apb_err_disable = true;
1402 if (pcie->need_ob_cfg) {
1403 pcie->ob_map = paxb_v2_ob_map;
1404 pcie->ob.nr_windows = ARRAY_SIZE(paxb_v2_ob_map);
1405 }
1406 pcie->ib.nr_regions = ARRAY_SIZE(paxb_v2_ib_map);
1407 pcie->ib_map = paxb_v2_ib_map;
1408 pcie->need_msi_steer = true;
Oza Pawandeep39b7a4f2017-08-28 16:43:30 -05001409 dev_warn(dev, "reads of config registers that contain %#x return incorrect data\n",
1410 CFG_RETRY_STATUS);
Ray Juic7c44522016-10-31 17:38:41 -07001411 break;
Ray Jui06324ed2016-10-31 17:38:30 -07001412 case IPROC_PCIE_PAXC:
1413 regs = iproc_pcie_reg_paxc;
1414 pcie->ep_is_internal = true;
Ray Juif78e60a2018-06-11 17:21:06 -07001415 pcie->iproc_cfg_read = true;
1416 pcie->rej_unconfig_pf = true;
Ray Jui06324ed2016-10-31 17:38:30 -07001417 break;
Ray Jui787b3c42016-10-31 17:38:35 -07001418 case IPROC_PCIE_PAXC_V2:
1419 regs = iproc_pcie_reg_paxc_v2;
1420 pcie->ep_is_internal = true;
Ray Juif78e60a2018-06-11 17:21:06 -07001421 pcie->iproc_cfg_read = true;
1422 pcie->rej_unconfig_pf = true;
Ray Jui787b3c42016-10-31 17:38:35 -07001423 pcie->need_msi_steer = true;
1424 break;
Ray Jui06324ed2016-10-31 17:38:30 -07001425 default:
1426 dev_err(dev, "incompatible iProc PCIe interface\n");
1427 return -EINVAL;
1428 }
1429
1430 pcie->reg_offsets = devm_kcalloc(dev, IPROC_PCIE_MAX_NUM_REG,
1431 sizeof(*pcie->reg_offsets),
1432 GFP_KERNEL);
1433 if (!pcie->reg_offsets)
1434 return -ENOMEM;
1435
1436 /* go through the register table and populate all valid registers */
Ray Jui787b3c42016-10-31 17:38:35 -07001437 pcie->reg_offsets[0] = (pcie->type == IPROC_PCIE_PAXC_V2) ?
1438 IPROC_PCIE_REG_INVALID : regs[0];
Ray Jui06324ed2016-10-31 17:38:30 -07001439 for (reg_idx = 1; reg_idx < IPROC_PCIE_MAX_NUM_REG; reg_idx++)
1440 pcie->reg_offsets[reg_idx] = regs[reg_idx] ?
1441 regs[reg_idx] : IPROC_PCIE_REG_INVALID;
1442
1443 return 0;
1444}
1445
Hauke Mehrtens18c43422015-05-24 22:37:02 +02001446int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res)
Ray Jui1fb37a82015-04-08 11:21:35 -07001447{
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001448 struct device *dev;
Ray Jui1fb37a82015-04-08 11:21:35 -07001449 int ret;
Lorenzo Pieralisi52774072017-06-28 15:13:57 -05001450 struct pci_bus *child;
1451 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
Ray Jui1fb37a82015-04-08 11:21:35 -07001452
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001453 dev = pcie->dev;
Ray Jui06324ed2016-10-31 17:38:30 -07001454
1455 ret = iproc_pcie_rev_init(pcie);
1456 if (ret) {
1457 dev_err(dev, "unable to initialize controller parameters\n");
1458 return ret;
1459 }
1460
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001461 ret = devm_request_pci_bus_resources(dev, res);
Bjorn Helgaasc3245a52016-05-28 18:22:24 -05001462 if (ret)
1463 return ret;
1464
Markus Elfring93972d12015-06-28 16:42:04 +02001465 ret = phy_init(pcie->phy);
1466 if (ret) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001467 dev_err(dev, "unable to initialize PCIe PHY\n");
Markus Elfring93972d12015-06-28 16:42:04 +02001468 return ret;
1469 }
Ray Jui1fb37a82015-04-08 11:21:35 -07001470
Markus Elfring93972d12015-06-28 16:42:04 +02001471 ret = phy_power_on(pcie->phy);
1472 if (ret) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001473 dev_err(dev, "unable to power on PCIe PHY\n");
Markus Elfring93972d12015-06-28 16:42:04 +02001474 goto err_exit_phy;
Ray Jui1fb37a82015-04-08 11:21:35 -07001475 }
1476
Oza Pawandeepb91c26c2017-08-28 16:43:35 -05001477 iproc_pcie_perst_ctrl(pcie, true);
1478 iproc_pcie_perst_ctrl(pcie, false);
Ray Jui1fb37a82015-04-08 11:21:35 -07001479
Ray Juie99a1872015-10-16 08:18:24 -05001480 if (pcie->need_ob_cfg) {
1481 ret = iproc_pcie_map_ranges(pcie, res);
1482 if (ret) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001483 dev_err(dev, "map failed\n");
Ray Juie99a1872015-10-16 08:18:24 -05001484 goto err_power_off_phy;
1485 }
1486 }
1487
Ray Jui3b65ca52018-01-11 12:36:16 -08001488 if (pcie->need_ib_cfg) {
1489 ret = iproc_pcie_map_dma_ranges(pcie);
1490 if (ret && ret != -ENOENT)
1491 goto err_power_off_phy;
1492 }
Ray Juidd9d4e72016-10-31 17:38:39 -07001493
Lorenzo Pieralisi022adcf2017-06-28 15:13:50 -05001494 ret = iproc_pcie_check_link(pcie);
Ray Jui1fb37a82015-04-08 11:21:35 -07001495 if (ret) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001496 dev_err(dev, "no PCIe EP device detected\n");
Lorenzo Pieralisi52774072017-06-28 15:13:57 -05001497 goto err_power_off_phy;
Ray Jui1fb37a82015-04-08 11:21:35 -07001498 }
1499
1500 iproc_pcie_enable(pcie);
1501
Ray Jui3bc2b232016-01-06 18:04:35 -06001502 if (IS_ENABLED(CONFIG_PCI_MSI))
1503 if (iproc_pcie_msi_enable(pcie))
Bjorn Helgaas786aecc2016-10-06 13:36:08 -05001504 dev_info(dev, "not using iProc MSI\n");
Ray Jui3bc2b232016-01-06 18:04:35 -06001505
Lorenzo Pieralisi52774072017-06-28 15:13:57 -05001506 list_splice_init(res, &host->windows);
1507 host->busnr = 0;
1508 host->dev.parent = dev;
1509 host->ops = &iproc_pcie_ops;
Rob Herringa1b363a2018-03-07 09:42:36 -06001510 host->sysdata = pcie;
Lorenzo Pieralisi64bcd002017-06-28 15:14:07 -05001511 host->map_irq = pcie->map_irq;
1512 host->swizzle_irq = pci_common_swizzle;
Lorenzo Pieralisi52774072017-06-28 15:13:57 -05001513
1514 ret = pci_scan_root_bus_bridge(host);
1515 if (ret < 0) {
1516 dev_err(dev, "failed to scan host: %d\n", ret);
1517 goto err_power_off_phy;
1518 }
Andy Gospodarekffbd7962016-12-01 15:34:52 -05001519
Lorenzo Pieralisi52774072017-06-28 15:13:57 -05001520 pci_assign_unassigned_bus_resources(host->bus);
1521
1522 pcie->root_bus = host->bus;
1523
1524 list_for_each_entry(child, &host->bus->children, node)
Jon Mason4d4836a2017-01-27 16:44:08 -05001525 pcie_bus_configure_settings(child);
1526
Lorenzo Pieralisi52774072017-06-28 15:13:57 -05001527 pci_bus_add_devices(host->bus);
Ray Jui1fb37a82015-04-08 11:21:35 -07001528
1529 return 0;
1530
Ray Jui1fb37a82015-04-08 11:21:35 -07001531err_power_off_phy:
Markus Elfring93972d12015-06-28 16:42:04 +02001532 phy_power_off(pcie->phy);
Ray Jui1fb37a82015-04-08 11:21:35 -07001533err_exit_phy:
Markus Elfring93972d12015-06-28 16:42:04 +02001534 phy_exit(pcie->phy);
Ray Jui1fb37a82015-04-08 11:21:35 -07001535 return ret;
1536}
1537EXPORT_SYMBOL(iproc_pcie_setup);
1538
1539int iproc_pcie_remove(struct iproc_pcie *pcie)
1540{
1541 pci_stop_root_bus(pcie->root_bus);
1542 pci_remove_root_bus(pcie->root_bus);
1543
Ray Jui3bc2b232016-01-06 18:04:35 -06001544 iproc_pcie_msi_disable(pcie);
1545
Markus Elfring93972d12015-06-28 16:42:04 +02001546 phy_power_off(pcie->phy);
1547 phy_exit(pcie->phy);
Ray Jui1fb37a82015-04-08 11:21:35 -07001548
1549 return 0;
1550}
1551EXPORT_SYMBOL(iproc_pcie_remove);
1552
Ray Jui1e5748c2018-06-11 17:21:05 -07001553/*
1554 * The MSI parsing logic in certain revisions of Broadcom PAXC based root
1555 * complex does not work and needs to be disabled
1556 */
1557static void quirk_paxc_disable_msi_parsing(struct pci_dev *pdev)
1558{
1559 struct iproc_pcie *pcie = iproc_data(pdev->bus);
1560
1561 if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
1562 iproc_pcie_paxc_v2_msi_steer(pcie, 0, false);
1563}
1564DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0x16f0,
1565 quirk_paxc_disable_msi_parsing);
1566DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd802,
1567 quirk_paxc_disable_msi_parsing);
1568DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd804,
1569 quirk_paxc_disable_msi_parsing);
1570
Ray Jui1fb37a82015-04-08 11:21:35 -07001571MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
1572MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver");
1573MODULE_LICENSE("GPL v2");