blob: 7219ca39aa9098f9171cc07df7729803f262c8ad [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Driver for the Aardvark PCIe controller, used on Marvell Armada
4 * 3700.
5 *
6 * Copyright (C) 2016 Marvell
7 *
8 * Author: Hezi Shahmoon <hezi.shahmoon@marvell.com>
9 */
10
11#include <linux/delay.h>
12#include <linux/gpio/consumer.h>
13#include <linux/interrupt.h>
14#include <linux/irq.h>
15#include <linux/irqdomain.h>
16#include <linux/kernel.h>
17#include <linux/pci.h>
18#include <linux/init.h>
19#include <linux/platform_device.h>
20#include <linux/of_address.h>
21#include <linux/of_gpio.h>
22#include <linux/of_pci.h>
23
24#include "../pci.h"
25#include "../pci-bridge-emul.h"
26
27/* PCIe core registers */
28#define PCIE_CORE_DEV_ID_REG 0x0
29#define PCIE_CORE_CMD_STATUS_REG 0x4
30#define PCIE_CORE_DEV_REV_REG 0x8
31#define PCIE_CORE_PCIEXP_CAP 0xc0
32#define PCIE_CORE_ERR_CAPCTL_REG 0x118
33#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
34#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
35#define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7)
36#define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8)
37#define PCIE_CORE_INT_A_ASSERT_ENABLE 1
38#define PCIE_CORE_INT_B_ASSERT_ENABLE 2
39#define PCIE_CORE_INT_C_ASSERT_ENABLE 3
40#define PCIE_CORE_INT_D_ASSERT_ENABLE 4
41/* PIO registers base address and register offsets */
42#define PIO_BASE_ADDR 0x4000
43#define PIO_CTRL (PIO_BASE_ADDR + 0x0)
44#define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
45#define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
46#define PIO_STAT (PIO_BASE_ADDR + 0x4)
47#define PIO_COMPLETION_STATUS_SHIFT 7
48#define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
49#define PIO_COMPLETION_STATUS_OK 0
50#define PIO_COMPLETION_STATUS_UR 1
51#define PIO_COMPLETION_STATUS_CRS 2
52#define PIO_COMPLETION_STATUS_CA 4
53#define PIO_NON_POSTED_REQ BIT(10)
54#define PIO_ERR_STATUS BIT(11)
55#define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
56#define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
57#define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
58#define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
59#define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
60#define PIO_START (PIO_BASE_ADDR + 0x1c)
61#define PIO_ISR (PIO_BASE_ADDR + 0x20)
62#define PIO_ISRM (PIO_BASE_ADDR + 0x24)
63
64/* Aardvark Control registers */
65#define CONTROL_BASE_ADDR 0x4800
66#define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
67#define PCIE_GEN_SEL_MSK 0x3
68#define PCIE_GEN_SEL_SHIFT 0x0
69#define SPEED_GEN_1 0
70#define SPEED_GEN_2 1
71#define SPEED_GEN_3 2
72#define IS_RC_MSK 1
73#define IS_RC_SHIFT 2
74#define LANE_CNT_MSK 0x18
75#define LANE_CNT_SHIFT 0x3
76#define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
77#define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
78#define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
79#define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
80#define LINK_TRAINING_EN BIT(6)
81#define LEGACY_INTA BIT(28)
82#define LEGACY_INTB BIT(29)
83#define LEGACY_INTC BIT(30)
84#define LEGACY_INTD BIT(31)
85#define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4)
86#define HOT_RESET_GEN BIT(0)
87#define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
88#define PCIE_CORE_CTRL2_RESERVED 0x7
89#define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
90#define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
91#define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6)
92#define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10)
93#define PCIE_MSG_LOG_REG (CONTROL_BASE_ADDR + 0x30)
94#define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
95#define PCIE_MSG_PM_PME_MASK BIT(7)
96#define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44)
97#define PCIE_ISR0_MSI_INT_PENDING BIT(24)
98#define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val))
99#define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val))
100#define PCIE_ISR0_ALL_MASK GENMASK(31, 0)
101#define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48)
102#define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C)
103#define PCIE_ISR1_POWER_STATE_CHANGE BIT(4)
104#define PCIE_ISR1_FLUSH BIT(5)
105#define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val))
106#define PCIE_ISR1_ALL_MASK GENMASK(31, 0)
107#define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50)
108#define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54)
109#define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58)
110#define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C)
111#define PCIE_MSI_ALL_MASK GENMASK(31, 0)
112#define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C)
113#define PCIE_MSI_DATA_MASK GENMASK(15, 0)
114
115/* PCIe window configuration */
116#define OB_WIN_BASE_ADDR 0x4c00
117#define OB_WIN_BLOCK_SIZE 0x20
118#define OB_WIN_COUNT 8
119#define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
120 OB_WIN_BLOCK_SIZE * (win) + \
121 (offset))
122#define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
123#define OB_WIN_ENABLE BIT(0)
124#define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
125#define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
126#define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
127#define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
128#define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
129#define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
130#define OB_WIN_DEFAULT_ACTIONS (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4)
131#define OB_WIN_FUNC_NUM_MASK GENMASK(31, 24)
132#define OB_WIN_FUNC_NUM_SHIFT 24
133#define OB_WIN_FUNC_NUM_ENABLE BIT(23)
134#define OB_WIN_BUS_NUM_BITS_MASK GENMASK(22, 20)
135#define OB_WIN_BUS_NUM_BITS_SHIFT 20
136#define OB_WIN_MSG_CODE_ENABLE BIT(22)
137#define OB_WIN_MSG_CODE_MASK GENMASK(21, 14)
138#define OB_WIN_MSG_CODE_SHIFT 14
139#define OB_WIN_MSG_PAYLOAD_LEN BIT(12)
140#define OB_WIN_ATTR_ENABLE BIT(11)
141#define OB_WIN_ATTR_TC_MASK GENMASK(10, 8)
142#define OB_WIN_ATTR_TC_SHIFT 8
143#define OB_WIN_ATTR_RELAXED BIT(7)
144#define OB_WIN_ATTR_NOSNOOP BIT(6)
145#define OB_WIN_ATTR_POISON BIT(5)
146#define OB_WIN_ATTR_IDO BIT(4)
147#define OB_WIN_TYPE_MASK GENMASK(3, 0)
148#define OB_WIN_TYPE_SHIFT 0
149#define OB_WIN_TYPE_MEM 0x0
150#define OB_WIN_TYPE_IO 0x4
151#define OB_WIN_TYPE_CONFIG_TYPE0 0x8
152#define OB_WIN_TYPE_CONFIG_TYPE1 0x9
153#define OB_WIN_TYPE_MSG 0xc
154
155/* LMI registers base address and register offsets */
156#define LMI_BASE_ADDR 0x6000
157#define CFG_REG (LMI_BASE_ADDR + 0x0)
158#define LTSSM_SHIFT 24
159#define LTSSM_MASK 0x3f
160#define RC_BAR_CONFIG 0x300
161
162/* LTSSM values in CFG_REG */
163enum {
164 LTSSM_DETECT_QUIET = 0x0,
165 LTSSM_DETECT_ACTIVE = 0x1,
166 LTSSM_POLLING_ACTIVE = 0x2,
167 LTSSM_POLLING_COMPLIANCE = 0x3,
168 LTSSM_POLLING_CONFIGURATION = 0x4,
169 LTSSM_CONFIG_LINKWIDTH_START = 0x5,
170 LTSSM_CONFIG_LINKWIDTH_ACCEPT = 0x6,
171 LTSSM_CONFIG_LANENUM_ACCEPT = 0x7,
172 LTSSM_CONFIG_LANENUM_WAIT = 0x8,
173 LTSSM_CONFIG_COMPLETE = 0x9,
174 LTSSM_CONFIG_IDLE = 0xa,
175 LTSSM_RECOVERY_RCVR_LOCK = 0xb,
176 LTSSM_RECOVERY_SPEED = 0xc,
177 LTSSM_RECOVERY_RCVR_CFG = 0xd,
178 LTSSM_RECOVERY_IDLE = 0xe,
179 LTSSM_L0 = 0x10,
180 LTSSM_RX_L0S_ENTRY = 0x11,
181 LTSSM_RX_L0S_IDLE = 0x12,
182 LTSSM_RX_L0S_FTS = 0x13,
183 LTSSM_TX_L0S_ENTRY = 0x14,
184 LTSSM_TX_L0S_IDLE = 0x15,
185 LTSSM_TX_L0S_FTS = 0x16,
186 LTSSM_L1_ENTRY = 0x17,
187 LTSSM_L1_IDLE = 0x18,
188 LTSSM_L2_IDLE = 0x19,
189 LTSSM_L2_TRANSMIT_WAKE = 0x1a,
190 LTSSM_DISABLED = 0x20,
191 LTSSM_LOOPBACK_ENTRY_MASTER = 0x21,
192 LTSSM_LOOPBACK_ACTIVE_MASTER = 0x22,
193 LTSSM_LOOPBACK_EXIT_MASTER = 0x23,
194 LTSSM_LOOPBACK_ENTRY_SLAVE = 0x24,
195 LTSSM_LOOPBACK_ACTIVE_SLAVE = 0x25,
196 LTSSM_LOOPBACK_EXIT_SLAVE = 0x26,
197 LTSSM_HOT_RESET = 0x27,
198 LTSSM_RECOVERY_EQUALIZATION_PHASE0 = 0x28,
199 LTSSM_RECOVERY_EQUALIZATION_PHASE1 = 0x29,
200 LTSSM_RECOVERY_EQUALIZATION_PHASE2 = 0x2a,
201 LTSSM_RECOVERY_EQUALIZATION_PHASE3 = 0x2b,
202};
203
204#define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44)
205
206/* PCIe core controller registers */
207#define CTRL_CORE_BASE_ADDR 0x18000
208#define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
209#define CTRL_MODE_SHIFT 0x0
210#define CTRL_MODE_MASK 0x1
211#define PCIE_CORE_MODE_DIRECT 0x0
212#define PCIE_CORE_MODE_COMMAND 0x1
213
214/* PCIe Central Interrupts Registers */
215#define CENTRAL_INT_BASE_ADDR 0x1b000
216#define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
217#define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
218#define PCIE_IRQ_CMDQ_INT BIT(0)
219#define PCIE_IRQ_MSI_STATUS_INT BIT(1)
220#define PCIE_IRQ_CMD_SENT_DONE BIT(3)
221#define PCIE_IRQ_DMA_INT BIT(4)
222#define PCIE_IRQ_IB_DXFERDONE BIT(5)
223#define PCIE_IRQ_OB_DXFERDONE BIT(6)
224#define PCIE_IRQ_OB_RXFERDONE BIT(7)
225#define PCIE_IRQ_COMPQ_INT BIT(12)
226#define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
227#define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
228#define PCIE_IRQ_CORE_INT BIT(16)
229#define PCIE_IRQ_CORE_INT_PIO BIT(17)
230#define PCIE_IRQ_DPMU_INT BIT(18)
231#define PCIE_IRQ_PCIE_MIS_INT BIT(19)
232#define PCIE_IRQ_MSI_INT1_DET BIT(20)
233#define PCIE_IRQ_MSI_INT2_DET BIT(21)
234#define PCIE_IRQ_RC_DBELL_DET BIT(22)
235#define PCIE_IRQ_EP_STATUS BIT(23)
236#define PCIE_IRQ_ALL_MASK GENMASK(31, 0)
237#define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
238
239/* Transaction types */
240#define PCIE_CONFIG_RD_TYPE0 0x8
241#define PCIE_CONFIG_RD_TYPE1 0x9
242#define PCIE_CONFIG_WR_TYPE0 0xa
243#define PCIE_CONFIG_WR_TYPE1 0xb
244
245#define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
246#define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
247#define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
248#define PCIE_CONF_REG(reg) ((reg) & 0xffc)
249#define PCIE_CONF_ADDR(bus, devfn, where) \
250 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
251 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
252
253#define PIO_RETRY_CNT 750000 /* 1.5 s */
254#define PIO_RETRY_DELAY 2 /* 2 us*/
255
256#define LINK_WAIT_MAX_RETRIES 10
257#define LINK_WAIT_USLEEP_MIN 90000
258#define LINK_WAIT_USLEEP_MAX 100000
259#define RETRAIN_WAIT_MAX_RETRIES 10
260#define RETRAIN_WAIT_USLEEP_US 2000
261
262#define MSI_IRQ_NUM 32
263
264#define CFG_RD_CRS_VAL 0xffff0001
265
266struct advk_pcie {
267 struct platform_device *pdev;
268 void __iomem *base;
269 struct list_head resources;
270 struct {
271 phys_addr_t match;
272 phys_addr_t remap;
273 phys_addr_t mask;
274 u32 actions;
275 } wins[OB_WIN_COUNT];
276 u8 wins_count;
277 struct irq_domain *irq_domain;
278 struct irq_chip irq_chip;
279 raw_spinlock_t irq_lock;
280 struct irq_domain *msi_domain;
281 struct irq_domain *msi_inner_domain;
282 struct irq_chip msi_bottom_irq_chip;
283 struct irq_chip msi_irq_chip;
284 struct msi_domain_info msi_domain_info;
285 DECLARE_BITMAP(msi_used, MSI_IRQ_NUM);
286 struct mutex msi_used_lock;
287 u16 msi_msg;
288 int root_bus_nr;
289 int link_gen;
290 struct pci_bridge_emul bridge;
291 struct gpio_desc *reset_gpio;
292};
293
294static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
295{
296 writel(val, pcie->base + reg);
297}
298
299static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
300{
301 return readl(pcie->base + reg);
302}
303
304static u8 advk_pcie_ltssm_state(struct advk_pcie *pcie)
305{
306 u32 val;
307 u8 ltssm_state;
308
309 val = advk_readl(pcie, CFG_REG);
310 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
311 return ltssm_state;
312}
313
314static inline bool advk_pcie_link_up(struct advk_pcie *pcie)
315{
316 /* check if LTSSM is in normal operation - some L* state */
317 u8 ltssm_state = advk_pcie_ltssm_state(pcie);
318 return ltssm_state >= LTSSM_L0 && ltssm_state < LTSSM_DISABLED;
319}
320
321static inline bool advk_pcie_link_active(struct advk_pcie *pcie)
322{
323 /*
324 * According to PCIe Base specification 3.0, Table 4-14: Link
325 * Status Mapped to the LTSSM, and 4.2.6.3.6 Configuration.Idle
326 * is Link Up mapped to LTSSM Configuration.Idle, Recovery, L0,
327 * L0s, L1 and L2 states. And according to 3.2.1. Data Link
328 * Control and Management State Machine Rules is DL Up status
329 * reported in DL Active state.
330 */
331 u8 ltssm_state = advk_pcie_ltssm_state(pcie);
332 return ltssm_state >= LTSSM_CONFIG_IDLE && ltssm_state < LTSSM_DISABLED;
333}
334
335static inline bool advk_pcie_link_training(struct advk_pcie *pcie)
336{
337 /*
338 * According to PCIe Base specification 3.0, Table 4-14: Link
339 * Status Mapped to the LTSSM is Link Training mapped to LTSSM
340 * Configuration and Recovery states.
341 */
342 u8 ltssm_state = advk_pcie_ltssm_state(pcie);
343 return ((ltssm_state >= LTSSM_CONFIG_LINKWIDTH_START &&
344 ltssm_state < LTSSM_L0) ||
345 (ltssm_state >= LTSSM_RECOVERY_EQUALIZATION_PHASE0 &&
346 ltssm_state <= LTSSM_RECOVERY_EQUALIZATION_PHASE3));
347}
348
349static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
350{
351 int retries;
352
353 /* check if the link is up or not */
354 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
355 if (advk_pcie_link_up(pcie))
356 return 0;
357
358 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
359 }
360
361 return -ETIMEDOUT;
362}
363
364static void advk_pcie_wait_for_retrain(struct advk_pcie *pcie)
365{
366 size_t retries;
367
368 for (retries = 0; retries < RETRAIN_WAIT_MAX_RETRIES; ++retries) {
369 if (advk_pcie_link_training(pcie))
370 break;
371 udelay(RETRAIN_WAIT_USLEEP_US);
372 }
373}
374
375static void advk_pcie_issue_perst(struct advk_pcie *pcie)
376{
377 if (!pcie->reset_gpio)
378 return;
379
380 /* 10ms delay is needed for some cards */
381 dev_info(&pcie->pdev->dev, "issuing PERST via reset GPIO for 10ms\n");
382 gpiod_set_value_cansleep(pcie->reset_gpio, 1);
383 usleep_range(10000, 11000);
384 gpiod_set_value_cansleep(pcie->reset_gpio, 0);
385}
386
387static void advk_pcie_train_link(struct advk_pcie *pcie)
388{
389 struct device *dev = &pcie->pdev->dev;
390 u32 reg;
391 int ret;
392
393 /*
394 * Setup PCIe rev / gen compliance based on device tree property
395 * 'max-link-speed' which also forces maximal link speed.
396 */
397 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
398 reg &= ~PCIE_GEN_SEL_MSK;
399 if (pcie->link_gen == 3)
400 reg |= SPEED_GEN_3;
401 else if (pcie->link_gen == 2)
402 reg |= SPEED_GEN_2;
403 else
404 reg |= SPEED_GEN_1;
405 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
406
407 /*
408 * Set maximal link speed value also into PCIe Link Control 2 register.
409 * Armada 3700 Functional Specification says that default value is based
410 * on SPEED_GEN but tests showed that default value is always 8.0 GT/s.
411 */
412 reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2);
413 reg &= ~PCI_EXP_LNKCTL2_TLS;
414 if (pcie->link_gen == 3)
415 reg |= PCI_EXP_LNKCTL2_TLS_8_0GT;
416 else if (pcie->link_gen == 2)
417 reg |= PCI_EXP_LNKCTL2_TLS_5_0GT;
418 else
419 reg |= PCI_EXP_LNKCTL2_TLS_2_5GT;
420 advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2);
421
422 /* Enable link training after selecting PCIe generation */
423 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
424 reg |= LINK_TRAINING_EN;
425 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
426
427 /*
428 * Reset PCIe card via PERST# signal. Some cards are not detected
429 * during link training when they are in some non-initial state.
430 */
431 advk_pcie_issue_perst(pcie);
432
433 /*
434 * PERST# signal could have been asserted by pinctrl subsystem before
435 * probe() callback has been called or issued explicitly by reset gpio
436 * function advk_pcie_issue_perst(), making the endpoint going into
437 * fundamental reset. As required by PCI Express spec (PCI Express
438 * Base Specification, REV. 4.0 PCI Express, February 19 2014, 6.6.1
439 * Conventional Reset) a delay for at least 100ms after such a reset
440 * before sending a Configuration Request to the device is needed.
441 * So wait until PCIe link is up. Function advk_pcie_wait_for_link()
442 * waits for link at least 900ms.
443 */
444 ret = advk_pcie_wait_for_link(pcie);
445 if (ret < 0)
446 dev_err(dev, "link never came up\n");
447 else
448 dev_info(dev, "link up\n");
449}
450
451/*
452 * Set PCIe address window register which could be used for memory
453 * mapping.
454 */
455static void advk_pcie_set_ob_win(struct advk_pcie *pcie, u8 win_num,
456 phys_addr_t match, phys_addr_t remap,
457 phys_addr_t mask, u32 actions)
458{
459 advk_writel(pcie, OB_WIN_ENABLE |
460 lower_32_bits(match), OB_WIN_MATCH_LS(win_num));
461 advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num));
462 advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num));
463 advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num));
464 advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num));
465 advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num));
466 advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num));
467}
468
469static void advk_pcie_disable_ob_win(struct advk_pcie *pcie, u8 win_num)
470{
471 advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num));
472 advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num));
473 advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num));
474 advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num));
475 advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num));
476 advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num));
477 advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num));
478}
479
480static void advk_pcie_setup_hw(struct advk_pcie *pcie)
481{
482 u32 reg;
483 int i;
484
485 /* Set to Direct mode */
486 reg = advk_readl(pcie, CTRL_CONFIG_REG);
487 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
488 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
489 advk_writel(pcie, reg, CTRL_CONFIG_REG);
490
491 /* Set PCI global control register to RC mode */
492 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
493 reg |= (IS_RC_MSK << IS_RC_SHIFT);
494 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
495
496 /*
497 * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab.
498 * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor
499 * id in high 16 bits. Updating this register changes readback value of
500 * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround
501 * for erratum 4.1: "The value of device and vendor ID is incorrect".
502 */
503 reg = (PCI_VENDOR_ID_MARVELL << 16) | PCI_VENDOR_ID_MARVELL;
504 advk_writel(pcie, reg, VENDOR_ID_REG);
505
506 /*
507 * Change Class Code of PCI Bridge device to PCI Bridge (0x600400),
508 * because the default value is Mass storage controller (0x010400).
509 *
510 * Note that this Aardvark PCI Bridge does not have compliant Type 1
511 * Configuration Space and it even cannot be accessed via Aardvark's
512 * PCI config space access method. Something like config space is
513 * available in internal Aardvark registers starting at offset 0x0
514 * and is reported as Type 0. In range 0x10 - 0x34 it has totally
515 * different registers.
516 *
517 * Therefore driver uses emulation of PCI Bridge which emulates
518 * access to configuration space via internal Aardvark registers or
519 * emulated configuration buffer.
520 */
521 reg = advk_readl(pcie, PCIE_CORE_DEV_REV_REG);
522 reg &= ~0xffffff00;
523 reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
524 advk_writel(pcie, reg, PCIE_CORE_DEV_REV_REG);
525
526 /* Disable Root Bridge I/O space, memory space and bus mastering */
527 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
528 reg &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
529 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
530
531 /* Set Advanced Error Capabilities and Control PF0 register */
532 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
533 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
534 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
535 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
536 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
537
538 /* Set PCIe Device Control register */
539 reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL);
540 reg &= ~PCI_EXP_DEVCTL_RELAX_EN;
541 reg &= ~PCI_EXP_DEVCTL_NOSNOOP_EN;
542 reg &= ~PCI_EXP_DEVCTL_PAYLOAD;
543 reg &= ~PCI_EXP_DEVCTL_READRQ;
544 reg |= PCI_EXP_DEVCTL_PAYLOAD_512B;
545 reg |= PCI_EXP_DEVCTL_READRQ_512B;
546 advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL);
547
548 /* Program PCIe Control 2 to disable strict ordering */
549 reg = PCIE_CORE_CTRL2_RESERVED |
550 PCIE_CORE_CTRL2_TD_ENABLE;
551 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
552
553 /* Set lane X1 */
554 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
555 reg &= ~LANE_CNT_MSK;
556 reg |= LANE_COUNT_1;
557 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
558
559 /* Enable MSI */
560 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
561 reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
562 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
563
564 /* Clear all interrupts */
565 advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG);
566 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
567 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
568 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
569
570 /* Disable All ISR0/1 Sources */
571 reg = PCIE_ISR0_ALL_MASK;
572 reg &= ~PCIE_ISR0_MSI_INT_PENDING;
573 advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
574
575 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
576
577 /* Unmask all MSIs */
578 advk_writel(pcie, ~(u32)PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG);
579
580 /* Enable summary interrupt for GIC SPI source */
581 reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
582 advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
583
584 /*
585 * Enable AXI address window location generation:
586 * When it is enabled, the default outbound window
587 * configurations (Default User Field: 0xD0074CFC)
588 * are used to transparent address translation for
589 * the outbound transactions. Thus, PCIe address
590 * windows are not required for transparent memory
591 * access when default outbound window configuration
592 * is set for memory access.
593 */
594 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
595 reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
596 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
597
598 /*
599 * Set memory access in Default User Field so it
600 * is not required to configure PCIe address for
601 * transparent memory access.
602 */
603 advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS);
604
605 /*
606 * Bypass the address window mapping for PIO:
607 * Since PIO access already contains all required
608 * info over AXI interface by PIO registers, the
609 * address window is not required.
610 */
611 reg = advk_readl(pcie, PIO_CTRL);
612 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
613 advk_writel(pcie, reg, PIO_CTRL);
614
615 /*
616 * Configure PCIe address windows for non-memory or
617 * non-transparent access as by default PCIe uses
618 * transparent memory access.
619 */
620 for (i = 0; i < pcie->wins_count; i++)
621 advk_pcie_set_ob_win(pcie, i,
622 pcie->wins[i].match, pcie->wins[i].remap,
623 pcie->wins[i].mask, pcie->wins[i].actions);
624
625 /* Disable remaining PCIe outbound windows */
626 for (i = pcie->wins_count; i < OB_WIN_COUNT; i++)
627 advk_pcie_disable_ob_win(pcie, i);
628
629 advk_pcie_train_link(pcie);
630}
631
632static int advk_pcie_check_pio_status(struct advk_pcie *pcie, bool allow_crs, u32 *val)
633{
634 struct device *dev = &pcie->pdev->dev;
635 u32 reg;
636 unsigned int status;
637 char *strcomp_status, *str_posted;
638 int ret;
639
640 reg = advk_readl(pcie, PIO_STAT);
641 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
642 PIO_COMPLETION_STATUS_SHIFT;
643
644 /*
645 * According to HW spec, the PIO status check sequence as below:
646 * 1) even if COMPLETION_STATUS(bit9:7) indicates successful,
647 * it still needs to check Error Status(bit11), only when this bit
648 * indicates no error happen, the operation is successful.
649 * 2) value Unsupported Request(1) of COMPLETION_STATUS(bit9:7) only
650 * means a PIO write error, and for PIO read it is successful with
651 * a read value of 0xFFFFFFFF.
652 * 3) value Completion Retry Status(CRS) of COMPLETION_STATUS(bit9:7)
653 * only means a PIO write error, and for PIO read it is successful
654 * with a read value of 0xFFFF0001.
655 * 4) value Completer Abort (CA) of COMPLETION_STATUS(bit9:7) means
656 * error for both PIO read and PIO write operation.
657 * 5) other errors are indicated as 'unknown'.
658 */
659 switch (status) {
660 case PIO_COMPLETION_STATUS_OK:
661 if (reg & PIO_ERR_STATUS) {
662 strcomp_status = "COMP_ERR";
663 ret = -EFAULT;
664 break;
665 }
666 /* Get the read result */
667 if (val)
668 *val = advk_readl(pcie, PIO_RD_DATA);
669 /* No error */
670 strcomp_status = NULL;
671 ret = 0;
672 break;
673 case PIO_COMPLETION_STATUS_UR:
674 strcomp_status = "UR";
675 ret = -EOPNOTSUPP;
676 break;
677 case PIO_COMPLETION_STATUS_CRS:
678 if (allow_crs && val) {
679 /* PCIe r4.0, sec 2.3.2, says:
680 * If CRS Software Visibility is enabled:
681 * For a Configuration Read Request that includes both
682 * bytes of the Vendor ID field of a device Function's
683 * Configuration Space Header, the Root Complex must
684 * complete the Request to the host by returning a
685 * read-data value of 0001h for the Vendor ID field and
686 * all '1's for any additional bytes included in the
687 * request.
688 *
689 * So CRS in this case is not an error status.
690 */
691 *val = CFG_RD_CRS_VAL;
692 strcomp_status = NULL;
693 ret = 0;
694 break;
695 }
696 /* PCIe r4.0, sec 2.3.2, says:
697 * If CRS Software Visibility is not enabled, the Root Complex
698 * must re-issue the Configuration Request as a new Request.
699 * If CRS Software Visibility is enabled: For a Configuration
700 * Write Request or for any other Configuration Read Request,
701 * the Root Complex must re-issue the Configuration Request as
702 * a new Request.
703 * A Root Complex implementation may choose to limit the number
704 * of Configuration Request/CRS Completion Status loops before
705 * determining that something is wrong with the target of the
706 * Request and taking appropriate action, e.g., complete the
707 * Request to the host as a failed transaction.
708 *
709 * So return -EAGAIN and caller (pci-aardvark.c driver) will
710 * re-issue request again up to the PIO_RETRY_CNT retries.
711 */
712 strcomp_status = "CRS";
713 ret = -EAGAIN;
714 break;
715 case PIO_COMPLETION_STATUS_CA:
716 strcomp_status = "CA";
717 ret = -ECANCELED;
718 break;
719 default:
720 strcomp_status = "Unknown";
721 ret = -EINVAL;
722 break;
723 }
724
725 if (!strcomp_status)
726 return ret;
727
728 if (reg & PIO_NON_POSTED_REQ)
729 str_posted = "Non-posted";
730 else
731 str_posted = "Posted";
732
733 dev_dbg(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
734 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
735
736 return ret;
737}
738
739static int advk_pcie_wait_pio(struct advk_pcie *pcie)
740{
741 struct device *dev = &pcie->pdev->dev;
742 int i;
743
744 for (i = 1; i <= PIO_RETRY_CNT; i++) {
745 u32 start, isr;
746
747 start = advk_readl(pcie, PIO_START);
748 isr = advk_readl(pcie, PIO_ISR);
749 if (!start && isr)
750 return i;
751 udelay(PIO_RETRY_DELAY);
752 }
753
754 dev_err(dev, "PIO read/write transfer time out\n");
755 return -ETIMEDOUT;
756}
757
758static pci_bridge_emul_read_status_t
759advk_pci_bridge_emul_base_conf_read(struct pci_bridge_emul *bridge,
760 int reg, u32 *value)
761{
762 struct advk_pcie *pcie = bridge->data;
763
764 switch (reg) {
765 case PCI_COMMAND:
766 *value = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
767 return PCI_BRIDGE_EMUL_HANDLED;
768
769 case PCI_INTERRUPT_LINE: {
770 /*
771 * From the whole 32bit register we support reading from HW only
772 * one bit: PCI_BRIDGE_CTL_BUS_RESET.
773 * Other bits are retrieved only from emulated config buffer.
774 */
775 __le32 *cfgspace = (__le32 *)&bridge->conf;
776 u32 val = le32_to_cpu(cfgspace[PCI_INTERRUPT_LINE / 4]);
777 if (advk_readl(pcie, PCIE_CORE_CTRL1_REG) & HOT_RESET_GEN)
778 val |= PCI_BRIDGE_CTL_BUS_RESET << 16;
779 else
780 val &= ~(PCI_BRIDGE_CTL_BUS_RESET << 16);
781 *value = val;
782 return PCI_BRIDGE_EMUL_HANDLED;
783 }
784
785 default:
786 return PCI_BRIDGE_EMUL_NOT_HANDLED;
787 }
788}
789
790static void
791advk_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge,
792 int reg, u32 old, u32 new, u32 mask)
793{
794 struct advk_pcie *pcie = bridge->data;
795
796 switch (reg) {
797 case PCI_COMMAND:
798 advk_writel(pcie, new, PCIE_CORE_CMD_STATUS_REG);
799 break;
800
801 case PCI_INTERRUPT_LINE:
802 if (mask & (PCI_BRIDGE_CTL_BUS_RESET << 16)) {
803 u32 val = advk_readl(pcie, PCIE_CORE_CTRL1_REG);
804 if (new & (PCI_BRIDGE_CTL_BUS_RESET << 16))
805 val |= HOT_RESET_GEN;
806 else
807 val &= ~HOT_RESET_GEN;
808 advk_writel(pcie, val, PCIE_CORE_CTRL1_REG);
809 }
810 break;
811
812 default:
813 break;
814 }
815}
816
817static pci_bridge_emul_read_status_t
818advk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
819 int reg, u32 *value)
820{
821 struct advk_pcie *pcie = bridge->data;
822
823
824 switch (reg) {
825 case PCI_EXP_SLTCTL:
826 *value = PCI_EXP_SLTSTA_PDS << 16;
827 return PCI_BRIDGE_EMUL_HANDLED;
828
829 case PCI_EXP_RTCTL: {
830 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG);
831 *value = (val & PCIE_MSG_PM_PME_MASK) ? 0 : PCI_EXP_RTCTL_PMEIE;
832 *value |= le16_to_cpu(bridge->pcie_conf.rootctl) & PCI_EXP_RTCTL_CRSSVE;
833 *value |= PCI_EXP_RTCAP_CRSVIS << 16;
834 return PCI_BRIDGE_EMUL_HANDLED;
835 }
836
837 case PCI_EXP_RTSTA: {
838 u32 isr0 = advk_readl(pcie, PCIE_ISR0_REG);
839 u32 msglog = advk_readl(pcie, PCIE_MSG_LOG_REG);
840 *value = msglog >> 16;
841 if (isr0 & PCIE_MSG_PM_PME_MASK)
842 *value |= PCI_EXP_RTSTA_PME;
843 return PCI_BRIDGE_EMUL_HANDLED;
844 }
845
846 case PCI_EXP_LNKCAP: {
847 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg);
848 /*
849 * PCI_EXP_LNKCAP_DLLLARC bit is hardwired in aardvark HW to 0.
850 * But support for PCI_EXP_LNKSTA_DLLLA is emulated via ltssm
851 * state so explicitly enable PCI_EXP_LNKCAP_DLLLARC flag.
852 */
853 val |= PCI_EXP_LNKCAP_DLLLARC;
854 *value = val;
855 return PCI_BRIDGE_EMUL_HANDLED;
856 }
857
858 case PCI_EXP_LNKCTL: {
859 /* u32 contains both PCI_EXP_LNKCTL and PCI_EXP_LNKSTA */
860 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg) &
861 ~(PCI_EXP_LNKSTA_LT << 16);
862 if (advk_pcie_link_training(pcie))
863 val |= (PCI_EXP_LNKSTA_LT << 16);
864 if (advk_pcie_link_active(pcie))
865 val |= (PCI_EXP_LNKSTA_DLLLA << 16);
866 *value = val;
867 return PCI_BRIDGE_EMUL_HANDLED;
868 }
869
870 case PCI_EXP_DEVCAP:
871 case PCI_EXP_DEVCTL:
872 *value = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg);
873 return PCI_BRIDGE_EMUL_HANDLED;
874 default:
875 return PCI_BRIDGE_EMUL_NOT_HANDLED;
876 }
877
878}
879
880static void
881advk_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge,
882 int reg, u32 old, u32 new, u32 mask)
883{
884 struct advk_pcie *pcie = bridge->data;
885
886 switch (reg) {
887 case PCI_EXP_DEVCTL:
888 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
889 break;
890
891 case PCI_EXP_LNKCTL:
892 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
893 if (new & PCI_EXP_LNKCTL_RL)
894 advk_pcie_wait_for_retrain(pcie);
895 break;
896
897 case PCI_EXP_RTCTL: {
898 /* Only mask/unmask PME interrupt */
899 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG) &
900 ~PCIE_MSG_PM_PME_MASK;
901 if ((new & PCI_EXP_RTCTL_PMEIE) == 0)
902 val |= PCIE_MSG_PM_PME_MASK;
903 advk_writel(pcie, val, PCIE_ISR0_MASK_REG);
904 break;
905 }
906
907 case PCI_EXP_RTSTA:
908 new = (new & PCI_EXP_RTSTA_PME) >> 9;
909 advk_writel(pcie, new, PCIE_ISR0_REG);
910 break;
911
912 default:
913 break;
914 }
915}
916
917static struct pci_bridge_emul_ops advk_pci_bridge_emul_ops = {
918 .read_base = advk_pci_bridge_emul_base_conf_read,
919 .write_base = advk_pci_bridge_emul_base_conf_write,
920 .read_pcie = advk_pci_bridge_emul_pcie_conf_read,
921 .write_pcie = advk_pci_bridge_emul_pcie_conf_write,
922};
923
924/*
925 * Initialize the configuration space of the PCI-to-PCI bridge
926 * associated with the given PCIe interface.
927 */
928static int advk_sw_pci_bridge_init(struct advk_pcie *pcie)
929{
930 struct pci_bridge_emul *bridge = &pcie->bridge;
931
932 bridge->conf.vendor =
933 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) & 0xffff);
934 bridge->conf.device =
935 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) >> 16);
936 bridge->conf.class_revision =
937 cpu_to_le32(advk_readl(pcie, PCIE_CORE_DEV_REV_REG) & 0xff);
938
939 /* Support 32 bits I/O addressing */
940 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32;
941 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32;
942
943 /* Support 64 bits memory pref */
944 bridge->conf.pref_mem_base = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
945 bridge->conf.pref_mem_limit = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
946
947 /* Support interrupt A for MSI feature */
948 bridge->conf.intpin = PCIE_CORE_INT_A_ASSERT_ENABLE;
949
950 /* Aardvark HW provides PCIe Capability structure in version 2 */
951 bridge->pcie_conf.cap = cpu_to_le16(2);
952
953 /* Indicates supports for Completion Retry Status */
954 bridge->pcie_conf.rootcap = cpu_to_le16(PCI_EXP_RTCAP_CRSVIS);
955
956 bridge->has_pcie = true;
957 bridge->data = pcie;
958 bridge->ops = &advk_pci_bridge_emul_ops;
959
960 return pci_bridge_emul_init(bridge, 0);
961}
962
963static bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus,
964 int devfn)
965{
966 if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0)
967 return false;
968
969 /*
970 * If the link goes down after we check for link-up, nothing bad
971 * happens but the config access times out.
972 */
973 if (bus->number != pcie->root_bus_nr && !advk_pcie_link_up(pcie))
974 return false;
975
976 return true;
977}
978
979static bool advk_pcie_pio_is_running(struct advk_pcie *pcie)
980{
981 struct device *dev = &pcie->pdev->dev;
982
983 /*
984 * Trying to start a new PIO transfer when previous has not completed
985 * cause External Abort on CPU which results in kernel panic:
986 *
987 * SError Interrupt on CPU0, code 0xbf000002 -- SError
988 * Kernel panic - not syncing: Asynchronous SError Interrupt
989 *
990 * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected
991 * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent
992 * concurrent calls at the same time. But because PIO transfer may take
993 * about 1.5s when link is down or card is disconnected, it means that
994 * advk_pcie_wait_pio() does not always have to wait for completion.
995 *
996 * Some versions of ARM Trusted Firmware handles this External Abort at
997 * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit:
998 * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50
999 */
1000 if (advk_readl(pcie, PIO_START)) {
1001 dev_err(dev, "Previous PIO read/write transfer is still running\n");
1002 return true;
1003 }
1004
1005 return false;
1006}
1007
1008static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
1009 int where, int size, u32 *val)
1010{
1011 struct advk_pcie *pcie = bus->sysdata;
1012 int retry_count;
1013 bool allow_crs;
1014 u32 reg;
1015 int ret;
1016
1017 if (!advk_pcie_valid_device(pcie, bus, devfn)) {
1018 *val = 0xffffffff;
1019 return PCIBIOS_DEVICE_NOT_FOUND;
1020 }
1021
1022 if (bus->number == pcie->root_bus_nr)
1023 return pci_bridge_emul_conf_read(&pcie->bridge, where,
1024 size, val);
1025
1026 /*
1027 * Completion Retry Status is possible to return only when reading all
1028 * 4 bytes from PCI_VENDOR_ID and PCI_DEVICE_ID registers at once and
1029 * CRSSVE flag on Root Bridge is enabled.
1030 */
1031 allow_crs = (where == PCI_VENDOR_ID) && (size == 4) &&
1032 (le16_to_cpu(pcie->bridge.pcie_conf.rootctl) &
1033 PCI_EXP_RTCTL_CRSSVE);
1034
1035 if (advk_pcie_pio_is_running(pcie))
1036 goto try_crs;
1037
1038 /* Program the control register */
1039 reg = advk_readl(pcie, PIO_CTRL);
1040 reg &= ~PIO_CTRL_TYPE_MASK;
1041 if (bus->primary == pcie->root_bus_nr)
1042 reg |= PCIE_CONFIG_RD_TYPE0;
1043 else
1044 reg |= PCIE_CONFIG_RD_TYPE1;
1045 advk_writel(pcie, reg, PIO_CTRL);
1046
1047 /* Program the address registers */
1048 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
1049 advk_writel(pcie, reg, PIO_ADDR_LS);
1050 advk_writel(pcie, 0, PIO_ADDR_MS);
1051
1052 /* Program the data strobe */
1053 advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
1054
1055 retry_count = 0;
1056 do {
1057 /* Clear PIO DONE ISR and start the transfer */
1058 advk_writel(pcie, 1, PIO_ISR);
1059 advk_writel(pcie, 1, PIO_START);
1060
1061 ret = advk_pcie_wait_pio(pcie);
1062 if (ret < 0)
1063 goto try_crs;
1064
1065 retry_count += ret;
1066
1067 /* Check PIO status and get the read result */
1068 ret = advk_pcie_check_pio_status(pcie, allow_crs, val);
1069 } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT);
1070
1071 if (ret < 0)
1072 goto fail;
1073
1074 if (size == 1)
1075 *val = (*val >> (8 * (where & 3))) & 0xff;
1076 else if (size == 2)
1077 *val = (*val >> (8 * (where & 3))) & 0xffff;
1078
1079 return PCIBIOS_SUCCESSFUL;
1080
1081try_crs:
1082 /*
1083 * If it is possible, return Completion Retry Status so that caller
1084 * tries to issue the request again instead of failing.
1085 */
1086 if (allow_crs) {
1087 *val = CFG_RD_CRS_VAL;
1088 return PCIBIOS_SUCCESSFUL;
1089 }
1090
1091fail:
1092 *val = 0xffffffff;
1093 return PCIBIOS_SET_FAILED;
1094}
1095
1096static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
1097 int where, int size, u32 val)
1098{
1099 struct advk_pcie *pcie = bus->sysdata;
1100 u32 reg;
1101 u32 data_strobe = 0x0;
1102 int retry_count;
1103 int offset;
1104 int ret;
1105
1106 if (!advk_pcie_valid_device(pcie, bus, devfn))
1107 return PCIBIOS_DEVICE_NOT_FOUND;
1108
1109 if (bus->number == pcie->root_bus_nr)
1110 return pci_bridge_emul_conf_write(&pcie->bridge, where,
1111 size, val);
1112
1113 if (where % size)
1114 return PCIBIOS_SET_FAILED;
1115
1116 if (advk_pcie_pio_is_running(pcie))
1117 return PCIBIOS_SET_FAILED;
1118
1119 /* Program the control register */
1120 reg = advk_readl(pcie, PIO_CTRL);
1121 reg &= ~PIO_CTRL_TYPE_MASK;
1122 if (bus->primary == pcie->root_bus_nr)
1123 reg |= PCIE_CONFIG_WR_TYPE0;
1124 else
1125 reg |= PCIE_CONFIG_WR_TYPE1;
1126 advk_writel(pcie, reg, PIO_CTRL);
1127
1128 /* Program the address registers */
1129 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
1130 advk_writel(pcie, reg, PIO_ADDR_LS);
1131 advk_writel(pcie, 0, PIO_ADDR_MS);
1132
1133 /* Calculate the write strobe */
1134 offset = where & 0x3;
1135 reg = val << (8 * offset);
1136 data_strobe = GENMASK(size - 1, 0) << offset;
1137
1138 /* Program the data register */
1139 advk_writel(pcie, reg, PIO_WR_DATA);
1140
1141 /* Program the data strobe */
1142 advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
1143
1144 retry_count = 0;
1145 do {
1146 /* Clear PIO DONE ISR and start the transfer */
1147 advk_writel(pcie, 1, PIO_ISR);
1148 advk_writel(pcie, 1, PIO_START);
1149
1150 ret = advk_pcie_wait_pio(pcie);
1151 if (ret < 0)
1152 return PCIBIOS_SET_FAILED;
1153
1154 retry_count += ret;
1155
1156 ret = advk_pcie_check_pio_status(pcie, false, NULL);
1157 } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT);
1158
1159 return ret < 0 ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL;
1160}
1161
1162static struct pci_ops advk_pcie_ops = {
1163 .read = advk_pcie_rd_conf,
1164 .write = advk_pcie_wr_conf,
1165};
1166
1167static void advk_msi_irq_compose_msi_msg(struct irq_data *data,
1168 struct msi_msg *msg)
1169{
1170 struct advk_pcie *pcie = irq_data_get_irq_chip_data(data);
1171 phys_addr_t msi_msg = virt_to_phys(&pcie->msi_msg);
1172
1173 msg->address_lo = lower_32_bits(msi_msg);
1174 msg->address_hi = upper_32_bits(msi_msg);
1175 msg->data = data->hwirq;
1176}
1177
1178static int advk_msi_set_affinity(struct irq_data *irq_data,
1179 const struct cpumask *mask, bool force)
1180{
1181 return -EINVAL;
1182}
1183
1184static int advk_msi_irq_domain_alloc(struct irq_domain *domain,
1185 unsigned int virq,
1186 unsigned int nr_irqs, void *args)
1187{
1188 struct advk_pcie *pcie = domain->host_data;
1189 int hwirq, i;
1190
1191 mutex_lock(&pcie->msi_used_lock);
1192 hwirq = bitmap_find_free_region(pcie->msi_used, MSI_IRQ_NUM,
1193 order_base_2(nr_irqs));
1194 mutex_unlock(&pcie->msi_used_lock);
1195 if (hwirq < 0)
1196 return -ENOSPC;
1197
1198 for (i = 0; i < nr_irqs; i++)
1199 irq_domain_set_info(domain, virq + i, hwirq + i,
1200 &pcie->msi_bottom_irq_chip,
1201 domain->host_data, handle_simple_irq,
1202 NULL, NULL);
1203
1204 return 0;
1205}
1206
1207static void advk_msi_irq_domain_free(struct irq_domain *domain,
1208 unsigned int virq, unsigned int nr_irqs)
1209{
1210 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1211 struct advk_pcie *pcie = domain->host_data;
1212
1213 mutex_lock(&pcie->msi_used_lock);
1214 bitmap_release_region(pcie->msi_used, d->hwirq, order_base_2(nr_irqs));
1215 mutex_unlock(&pcie->msi_used_lock);
1216}
1217
1218static const struct irq_domain_ops advk_msi_domain_ops = {
1219 .alloc = advk_msi_irq_domain_alloc,
1220 .free = advk_msi_irq_domain_free,
1221};
1222
1223static void advk_pcie_irq_mask(struct irq_data *d)
1224{
1225 struct advk_pcie *pcie = d->domain->host_data;
1226 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1227 unsigned long flags;
1228 u32 mask;
1229
1230 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
1231 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1232 mask |= PCIE_ISR1_INTX_ASSERT(hwirq);
1233 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
1234 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
1235}
1236
1237static void advk_pcie_irq_unmask(struct irq_data *d)
1238{
1239 struct advk_pcie *pcie = d->domain->host_data;
1240 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1241 unsigned long flags;
1242 u32 mask;
1243
1244 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
1245 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1246 mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq);
1247 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
1248 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
1249}
1250
1251static int advk_pcie_irq_map(struct irq_domain *h,
1252 unsigned int virq, irq_hw_number_t hwirq)
1253{
1254 struct advk_pcie *pcie = h->host_data;
1255
1256 advk_pcie_irq_mask(irq_get_irq_data(virq));
1257 irq_set_status_flags(virq, IRQ_LEVEL);
1258 irq_set_chip_and_handler(virq, &pcie->irq_chip,
1259 handle_level_irq);
1260 irq_set_chip_data(virq, pcie);
1261
1262 return 0;
1263}
1264
1265static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
1266 .map = advk_pcie_irq_map,
1267 .xlate = irq_domain_xlate_onecell,
1268};
1269
1270static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
1271{
1272 struct device *dev = &pcie->pdev->dev;
1273 struct device_node *node = dev->of_node;
1274 struct irq_chip *bottom_ic, *msi_ic;
1275 struct msi_domain_info *msi_di;
1276 phys_addr_t msi_msg_phys;
1277
1278 mutex_init(&pcie->msi_used_lock);
1279
1280 bottom_ic = &pcie->msi_bottom_irq_chip;
1281
1282 bottom_ic->name = "MSI";
1283 bottom_ic->irq_compose_msi_msg = advk_msi_irq_compose_msi_msg;
1284 bottom_ic->irq_set_affinity = advk_msi_set_affinity;
1285
1286 msi_ic = &pcie->msi_irq_chip;
1287 msi_ic->name = "advk-MSI";
1288
1289 msi_di = &pcie->msi_domain_info;
1290 msi_di->flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
1291 MSI_FLAG_MULTI_PCI_MSI;
1292 msi_di->chip = msi_ic;
1293
1294 msi_msg_phys = virt_to_phys(&pcie->msi_msg);
1295
1296 advk_writel(pcie, lower_32_bits(msi_msg_phys),
1297 PCIE_MSI_ADDR_LOW_REG);
1298 advk_writel(pcie, upper_32_bits(msi_msg_phys),
1299 PCIE_MSI_ADDR_HIGH_REG);
1300
1301 pcie->msi_inner_domain =
1302 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
1303 &advk_msi_domain_ops, pcie);
1304 if (!pcie->msi_inner_domain)
1305 return -ENOMEM;
1306
1307 pcie->msi_domain =
1308 pci_msi_create_irq_domain(of_node_to_fwnode(node),
1309 msi_di, pcie->msi_inner_domain);
1310 if (!pcie->msi_domain) {
1311 irq_domain_remove(pcie->msi_inner_domain);
1312 return -ENOMEM;
1313 }
1314
1315 return 0;
1316}
1317
1318static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
1319{
1320 irq_domain_remove(pcie->msi_domain);
1321 irq_domain_remove(pcie->msi_inner_domain);
1322}
1323
1324static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
1325{
1326 struct device *dev = &pcie->pdev->dev;
1327 struct device_node *node = dev->of_node;
1328 struct device_node *pcie_intc_node;
1329 struct irq_chip *irq_chip;
1330 int ret = 0;
1331
1332 raw_spin_lock_init(&pcie->irq_lock);
1333
1334 pcie_intc_node = of_get_next_child(node, NULL);
1335 if (!pcie_intc_node) {
1336 dev_err(dev, "No PCIe Intc node found\n");
1337 return -ENODEV;
1338 }
1339
1340 irq_chip = &pcie->irq_chip;
1341
1342 irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
1343 dev_name(dev));
1344 if (!irq_chip->name) {
1345 ret = -ENOMEM;
1346 goto out_put_node;
1347 }
1348
1349 irq_chip->irq_mask = advk_pcie_irq_mask;
1350 irq_chip->irq_mask_ack = advk_pcie_irq_mask;
1351 irq_chip->irq_unmask = advk_pcie_irq_unmask;
1352
1353 pcie->irq_domain =
1354 irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
1355 &advk_pcie_irq_domain_ops, pcie);
1356 if (!pcie->irq_domain) {
1357 dev_err(dev, "Failed to get a INTx IRQ domain\n");
1358 ret = -ENOMEM;
1359 goto out_put_node;
1360 }
1361
1362out_put_node:
1363 of_node_put(pcie_intc_node);
1364 return ret;
1365}
1366
1367static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
1368{
1369 irq_domain_remove(pcie->irq_domain);
1370}
1371
1372static void advk_pcie_handle_msi(struct advk_pcie *pcie)
1373{
1374 u32 msi_val, msi_mask, msi_status, msi_idx;
1375 int virq;
1376
1377 msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
1378 msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
1379 msi_status = msi_val & ((~msi_mask) & PCIE_MSI_ALL_MASK);
1380
1381 for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
1382 if (!(BIT(msi_idx) & msi_status))
1383 continue;
1384
1385 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
1386 virq = irq_find_mapping(pcie->msi_inner_domain, msi_idx);
1387 generic_handle_irq(virq);
1388 }
1389
1390 advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
1391 PCIE_ISR0_REG);
1392}
1393
1394static void advk_pcie_handle_int(struct advk_pcie *pcie)
1395{
1396 u32 isr0_val, isr0_mask, isr0_status;
1397 u32 isr1_val, isr1_mask, isr1_status;
1398 int i, virq;
1399
1400 isr0_val = advk_readl(pcie, PCIE_ISR0_REG);
1401 isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
1402 isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK);
1403
1404 isr1_val = advk_readl(pcie, PCIE_ISR1_REG);
1405 isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1406 isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK);
1407
1408 /* Process MSI interrupts */
1409 if (isr0_status & PCIE_ISR0_MSI_INT_PENDING)
1410 advk_pcie_handle_msi(pcie);
1411
1412 /* Process legacy interrupts */
1413 for (i = 0; i < PCI_NUM_INTX; i++) {
1414 if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i)))
1415 continue;
1416
1417 advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i),
1418 PCIE_ISR1_REG);
1419
1420 virq = irq_find_mapping(pcie->irq_domain, i);
1421 generic_handle_irq(virq);
1422 }
1423}
1424
1425static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
1426{
1427 struct advk_pcie *pcie = arg;
1428 u32 status;
1429
1430 status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
1431 if (!(status & PCIE_IRQ_CORE_INT))
1432 return IRQ_NONE;
1433
1434 advk_pcie_handle_int(pcie);
1435
1436 /* Clear interrupt */
1437 advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
1438
1439 return IRQ_HANDLED;
1440}
1441
1442static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie *pcie)
1443{
1444 int err, res_valid = 0;
1445 struct device *dev = &pcie->pdev->dev;
1446 struct resource_entry *win, *tmp;
1447 resource_size_t iobase;
1448
1449 INIT_LIST_HEAD(&pcie->resources);
1450
1451 err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff,
1452 &pcie->resources, &iobase);
1453 if (err)
1454 return err;
1455
1456 err = devm_request_pci_bus_resources(dev, &pcie->resources);
1457 if (err)
1458 goto out_release_res;
1459
1460 resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
1461 struct resource *res = win->res;
1462
1463 switch (resource_type(res)) {
1464 case IORESOURCE_IO:
1465 err = devm_pci_remap_iospace(dev, res, iobase);
1466 if (err) {
1467 dev_warn(dev, "error %d: failed to map resource %pR\n",
1468 err, res);
1469 resource_list_destroy_entry(win);
1470 }
1471 break;
1472 case IORESOURCE_MEM:
1473 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
1474 break;
1475 case IORESOURCE_BUS:
1476 pcie->root_bus_nr = res->start;
1477 break;
1478 }
1479 }
1480
1481 if (!res_valid) {
1482 dev_err(dev, "non-prefetchable memory resource required\n");
1483 err = -EINVAL;
1484 goto out_release_res;
1485 }
1486
1487 return 0;
1488
1489out_release_res:
1490 pci_free_resource_list(&pcie->resources);
1491 return err;
1492}
1493
1494static int advk_pcie_probe(struct platform_device *pdev)
1495{
1496 struct device *dev = &pdev->dev;
1497 struct advk_pcie *pcie;
1498 struct resource *res;
1499 struct pci_host_bridge *bridge;
1500 struct resource_entry *entry;
1501 int ret, irq;
1502
1503 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie));
1504 if (!bridge)
1505 return -ENOMEM;
1506
1507 pcie = pci_host_bridge_priv(bridge);
1508 pcie->pdev = pdev;
1509
1510 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1511 pcie->base = devm_ioremap_resource(dev, res);
1512 if (IS_ERR(pcie->base))
1513 return PTR_ERR(pcie->base);
1514
1515 irq = platform_get_irq(pdev, 0);
1516 ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
1517 IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
1518 pcie);
1519 if (ret) {
1520 dev_err(dev, "Failed to register interrupt\n");
1521 return ret;
1522 }
1523
1524 ret = advk_pcie_parse_request_of_pci_ranges(pcie);
1525 if (ret) {
1526 dev_err(dev, "Failed to parse resources\n");
1527 return ret;
1528 }
1529
1530 resource_list_for_each_entry(entry, &pcie->resources) {
1531 resource_size_t start = entry->res->start;
1532 resource_size_t size = resource_size(entry->res);
1533 unsigned long type = resource_type(entry->res);
1534 u64 win_size;
1535
1536 /*
1537 * Aardvark hardware allows to configure also PCIe window
1538 * for config type 0 and type 1 mapping, but driver uses
1539 * only PIO for issuing configuration transfers which does
1540 * not use PCIe window configuration.
1541 */
1542 if (type != IORESOURCE_MEM && type != IORESOURCE_MEM_64 &&
1543 type != IORESOURCE_IO)
1544 continue;
1545
1546 /*
1547 * Skip transparent memory resources. Default outbound access
1548 * configuration is set to transparent memory access so it
1549 * does not need window configuration.
1550 */
1551 if ((type == IORESOURCE_MEM || type == IORESOURCE_MEM_64) &&
1552 entry->offset == 0)
1553 continue;
1554
1555 /*
1556 * The n-th PCIe window is configured by tuple (match, remap, mask)
1557 * and an access to address A uses this window if A matches the
1558 * match with given mask.
1559 * So every PCIe window size must be a power of two and every start
1560 * address must be aligned to window size. Minimal size is 64 KiB
1561 * because lower 16 bits of mask must be zero. Remapped address
1562 * may have set only bits from the mask.
1563 */
1564 while (pcie->wins_count < OB_WIN_COUNT && size > 0) {
1565 /* Calculate the largest aligned window size */
1566 win_size = (1ULL << (fls64(size)-1)) |
1567 (start ? (1ULL << __ffs64(start)) : 0);
1568 win_size = 1ULL << __ffs64(win_size);
1569 if (win_size < 0x10000)
1570 break;
1571
1572 dev_dbg(dev,
1573 "Configuring PCIe window %d: [0x%llx-0x%llx] as %lu\n",
1574 pcie->wins_count, (unsigned long long)start,
1575 (unsigned long long)start + win_size, type);
1576
1577 if (type == IORESOURCE_IO) {
1578 pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_IO;
1579 pcie->wins[pcie->wins_count].match = pci_pio_to_address(start);
1580 } else {
1581 pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_MEM;
1582 pcie->wins[pcie->wins_count].match = start;
1583 }
1584 pcie->wins[pcie->wins_count].remap = start - entry->offset;
1585 pcie->wins[pcie->wins_count].mask = ~(win_size - 1);
1586
1587 if (pcie->wins[pcie->wins_count].remap & (win_size - 1))
1588 break;
1589
1590 start += win_size;
1591 size -= win_size;
1592 pcie->wins_count++;
1593 }
1594
1595 if (size > 0) {
1596 dev_err(&pcie->pdev->dev,
1597 "Invalid PCIe region [0x%llx-0x%llx]\n",
1598 (unsigned long long)entry->res->start,
1599 (unsigned long long)entry->res->end + 1);
1600 return -EINVAL;
1601 }
1602 }
1603
1604 pcie->reset_gpio = devm_gpiod_get_from_of_node(dev, dev->of_node,
1605 "reset-gpios", 0,
1606 GPIOD_OUT_LOW,
1607 "pcie1-reset");
1608 ret = PTR_ERR_OR_ZERO(pcie->reset_gpio);
1609 if (ret) {
1610 if (ret == -ENOENT) {
1611 pcie->reset_gpio = NULL;
1612 } else {
1613 if (ret != -EPROBE_DEFER)
1614 dev_err(dev, "Failed to get reset-gpio: %i\n",
1615 ret);
1616 return ret;
1617 }
1618 }
1619
1620 ret = of_pci_get_max_link_speed(dev->of_node);
1621 if (ret <= 0 || ret > 3)
1622 pcie->link_gen = 3;
1623 else
1624 pcie->link_gen = ret;
1625
1626 advk_pcie_setup_hw(pcie);
1627
1628 ret = advk_sw_pci_bridge_init(pcie);
1629 if (ret) {
1630 dev_err(dev, "Failed to register emulated root PCI bridge\n");
1631 return ret;
1632 }
1633
1634 ret = advk_pcie_init_irq_domain(pcie);
1635 if (ret) {
1636 dev_err(dev, "Failed to initialize irq\n");
1637 return ret;
1638 }
1639
1640 ret = advk_pcie_init_msi_irq_domain(pcie);
1641 if (ret) {
1642 dev_err(dev, "Failed to initialize irq\n");
1643 advk_pcie_remove_irq_domain(pcie);
1644 return ret;
1645 }
1646
1647 list_splice_init(&pcie->resources, &bridge->windows);
1648 bridge->dev.parent = dev;
1649 bridge->sysdata = pcie;
1650 bridge->busnr = 0;
1651 bridge->ops = &advk_pcie_ops;
1652 bridge->map_irq = of_irq_parse_and_map_pci;
1653 bridge->swizzle_irq = pci_common_swizzle;
1654
1655 ret = pci_host_probe(bridge);
1656 if (ret < 0) {
1657 advk_pcie_remove_msi_irq_domain(pcie);
1658 advk_pcie_remove_irq_domain(pcie);
1659 return ret;
1660 }
1661
1662 return 0;
1663}
1664
1665static const struct of_device_id advk_pcie_of_match_table[] = {
1666 { .compatible = "marvell,armada-3700-pcie", },
1667 {},
1668};
1669
1670static struct platform_driver advk_pcie_driver = {
1671 .driver = {
1672 .name = "advk-pcie",
1673 .of_match_table = advk_pcie_of_match_table,
1674 /* Driver unloading/unbinding currently not supported */
1675 .suppress_bind_attrs = true,
1676 },
1677 .probe = advk_pcie_probe,
1678};
1679builtin_platform_driver(advk_pcie_driver);