1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for the Aardvark PCIe controller, used on Marvell Armada
6 * Copyright (C) 2016 Marvell
8 * Author: Hezi Shahmoon <hezi.shahmoon@marvell.com>
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/module.h>
18 #include <linux/pci.h>
19 #include <linux/pci-ecam.h>
20 #include <linux/init.h>
21 #include <linux/phy/phy.h>
22 #include <linux/platform_device.h>
23 #include <linux/msi.h>
24 #include <linux/of_address.h>
25 #include <linux/of_gpio.h>
26 #include <linux/of_pci.h>
29 #include "../pci-bridge-emul.h"
31 /* PCIe core registers */
32 #define PCIE_CORE_DEV_ID_REG 0x0
33 #define PCIE_CORE_CMD_STATUS_REG 0x4
34 #define PCIE_CORE_DEV_REV_REG 0x8
35 #define PCIE_CORE_PCIEXP_CAP 0xc0
36 #define PCIE_CORE_ERR_CAPCTL_REG 0x118
37 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
38 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
39 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7)
40 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8)
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)
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
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_CORE_REF_CLK_REG (CONTROL_BASE_ADDR + 0x14)
94 #define PCIE_CORE_REF_CLK_TX_ENABLE BIT(1)
95 #define PCIE_CORE_REF_CLK_RX_ENABLE BIT(2)
96 #define PCIE_MSG_LOG_REG (CONTROL_BASE_ADDR + 0x30)
97 #define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
98 #define PCIE_MSG_PM_PME_MASK BIT(7)
99 #define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44)
100 #define PCIE_ISR0_MSI_INT_PENDING BIT(24)
101 #define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val))
102 #define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val))
103 #define PCIE_ISR0_ALL_MASK GENMASK(31, 0)
104 #define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48)
105 #define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C)
106 #define PCIE_ISR1_POWER_STATE_CHANGE BIT(4)
107 #define PCIE_ISR1_FLUSH BIT(5)
108 #define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val))
109 #define PCIE_ISR1_ALL_MASK GENMASK(31, 0)
110 #define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50)
111 #define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54)
112 #define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58)
113 #define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C)
114 #define PCIE_MSI_ALL_MASK GENMASK(31, 0)
115 #define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C)
116 #define PCIE_MSI_DATA_MASK GENMASK(15, 0)
118 /* PCIe window configuration */
119 #define OB_WIN_BASE_ADDR 0x4c00
120 #define OB_WIN_BLOCK_SIZE 0x20
121 #define OB_WIN_COUNT 8
122 #define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
123 OB_WIN_BLOCK_SIZE * (win) + \
125 #define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
126 #define OB_WIN_ENABLE BIT(0)
127 #define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
128 #define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
129 #define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
130 #define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
131 #define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
132 #define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
133 #define OB_WIN_DEFAULT_ACTIONS (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4)
134 #define OB_WIN_FUNC_NUM_MASK GENMASK(31, 24)
135 #define OB_WIN_FUNC_NUM_SHIFT 24
136 #define OB_WIN_FUNC_NUM_ENABLE BIT(23)
137 #define OB_WIN_BUS_NUM_BITS_MASK GENMASK(22, 20)
138 #define OB_WIN_BUS_NUM_BITS_SHIFT 20
139 #define OB_WIN_MSG_CODE_ENABLE BIT(22)
140 #define OB_WIN_MSG_CODE_MASK GENMASK(21, 14)
141 #define OB_WIN_MSG_CODE_SHIFT 14
142 #define OB_WIN_MSG_PAYLOAD_LEN BIT(12)
143 #define OB_WIN_ATTR_ENABLE BIT(11)
144 #define OB_WIN_ATTR_TC_MASK GENMASK(10, 8)
145 #define OB_WIN_ATTR_TC_SHIFT 8
146 #define OB_WIN_ATTR_RELAXED BIT(7)
147 #define OB_WIN_ATTR_NOSNOOP BIT(6)
148 #define OB_WIN_ATTR_POISON BIT(5)
149 #define OB_WIN_ATTR_IDO BIT(4)
150 #define OB_WIN_TYPE_MASK GENMASK(3, 0)
151 #define OB_WIN_TYPE_SHIFT 0
152 #define OB_WIN_TYPE_MEM 0x0
153 #define OB_WIN_TYPE_IO 0x4
154 #define OB_WIN_TYPE_CONFIG_TYPE0 0x8
155 #define OB_WIN_TYPE_CONFIG_TYPE1 0x9
156 #define OB_WIN_TYPE_MSG 0xc
158 /* LMI registers base address and register offsets */
159 #define LMI_BASE_ADDR 0x6000
160 #define CFG_REG (LMI_BASE_ADDR + 0x0)
161 #define LTSSM_SHIFT 24
162 #define LTSSM_MASK 0x3f
163 #define RC_BAR_CONFIG 0x300
165 /* LTSSM values in CFG_REG */
167 LTSSM_DETECT_QUIET = 0x0,
168 LTSSM_DETECT_ACTIVE = 0x1,
169 LTSSM_POLLING_ACTIVE = 0x2,
170 LTSSM_POLLING_COMPLIANCE = 0x3,
171 LTSSM_POLLING_CONFIGURATION = 0x4,
172 LTSSM_CONFIG_LINKWIDTH_START = 0x5,
173 LTSSM_CONFIG_LINKWIDTH_ACCEPT = 0x6,
174 LTSSM_CONFIG_LANENUM_ACCEPT = 0x7,
175 LTSSM_CONFIG_LANENUM_WAIT = 0x8,
176 LTSSM_CONFIG_COMPLETE = 0x9,
177 LTSSM_CONFIG_IDLE = 0xa,
178 LTSSM_RECOVERY_RCVR_LOCK = 0xb,
179 LTSSM_RECOVERY_SPEED = 0xc,
180 LTSSM_RECOVERY_RCVR_CFG = 0xd,
181 LTSSM_RECOVERY_IDLE = 0xe,
183 LTSSM_RX_L0S_ENTRY = 0x11,
184 LTSSM_RX_L0S_IDLE = 0x12,
185 LTSSM_RX_L0S_FTS = 0x13,
186 LTSSM_TX_L0S_ENTRY = 0x14,
187 LTSSM_TX_L0S_IDLE = 0x15,
188 LTSSM_TX_L0S_FTS = 0x16,
189 LTSSM_L1_ENTRY = 0x17,
190 LTSSM_L1_IDLE = 0x18,
191 LTSSM_L2_IDLE = 0x19,
192 LTSSM_L2_TRANSMIT_WAKE = 0x1a,
193 LTSSM_DISABLED = 0x20,
194 LTSSM_LOOPBACK_ENTRY_MASTER = 0x21,
195 LTSSM_LOOPBACK_ACTIVE_MASTER = 0x22,
196 LTSSM_LOOPBACK_EXIT_MASTER = 0x23,
197 LTSSM_LOOPBACK_ENTRY_SLAVE = 0x24,
198 LTSSM_LOOPBACK_ACTIVE_SLAVE = 0x25,
199 LTSSM_LOOPBACK_EXIT_SLAVE = 0x26,
200 LTSSM_HOT_RESET = 0x27,
201 LTSSM_RECOVERY_EQUALIZATION_PHASE0 = 0x28,
202 LTSSM_RECOVERY_EQUALIZATION_PHASE1 = 0x29,
203 LTSSM_RECOVERY_EQUALIZATION_PHASE2 = 0x2a,
204 LTSSM_RECOVERY_EQUALIZATION_PHASE3 = 0x2b,
207 #define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44)
209 /* PCIe core controller registers */
210 #define CTRL_CORE_BASE_ADDR 0x18000
211 #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
212 #define CTRL_MODE_SHIFT 0x0
213 #define CTRL_MODE_MASK 0x1
214 #define PCIE_CORE_MODE_DIRECT 0x0
215 #define PCIE_CORE_MODE_COMMAND 0x1
217 /* PCIe Central Interrupts Registers */
218 #define CENTRAL_INT_BASE_ADDR 0x1b000
219 #define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
220 #define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
221 #define PCIE_IRQ_CMDQ_INT BIT(0)
222 #define PCIE_IRQ_MSI_STATUS_INT BIT(1)
223 #define PCIE_IRQ_CMD_SENT_DONE BIT(3)
224 #define PCIE_IRQ_DMA_INT BIT(4)
225 #define PCIE_IRQ_IB_DXFERDONE BIT(5)
226 #define PCIE_IRQ_OB_DXFERDONE BIT(6)
227 #define PCIE_IRQ_OB_RXFERDONE BIT(7)
228 #define PCIE_IRQ_COMPQ_INT BIT(12)
229 #define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
230 #define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
231 #define PCIE_IRQ_CORE_INT BIT(16)
232 #define PCIE_IRQ_CORE_INT_PIO BIT(17)
233 #define PCIE_IRQ_DPMU_INT BIT(18)
234 #define PCIE_IRQ_PCIE_MIS_INT BIT(19)
235 #define PCIE_IRQ_MSI_INT1_DET BIT(20)
236 #define PCIE_IRQ_MSI_INT2_DET BIT(21)
237 #define PCIE_IRQ_RC_DBELL_DET BIT(22)
238 #define PCIE_IRQ_EP_STATUS BIT(23)
239 #define PCIE_IRQ_ALL_MASK GENMASK(31, 0)
240 #define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
242 /* Transaction types */
243 #define PCIE_CONFIG_RD_TYPE0 0x8
244 #define PCIE_CONFIG_RD_TYPE1 0x9
245 #define PCIE_CONFIG_WR_TYPE0 0xa
246 #define PCIE_CONFIG_WR_TYPE1 0xb
248 #define PIO_RETRY_CNT 750000 /* 1.5 s */
249 #define PIO_RETRY_DELAY 2 /* 2 us*/
251 #define LINK_WAIT_MAX_RETRIES 10
252 #define LINK_WAIT_USLEEP_MIN 90000
253 #define LINK_WAIT_USLEEP_MAX 100000
254 #define RETRAIN_WAIT_MAX_RETRIES 10
255 #define RETRAIN_WAIT_USLEEP_US 2000
257 #define MSI_IRQ_NUM 32
259 #define CFG_RD_CRS_VAL 0xffff0001
262 struct platform_device *pdev;
269 } wins[OB_WIN_COUNT];
272 struct irq_domain *irq_domain;
273 struct irq_chip irq_chip;
274 raw_spinlock_t irq_lock;
275 struct irq_domain *msi_domain;
276 struct irq_domain *msi_inner_domain;
277 struct irq_chip msi_bottom_irq_chip;
278 struct irq_chip msi_irq_chip;
279 struct msi_domain_info msi_domain_info;
280 DECLARE_BITMAP(msi_used, MSI_IRQ_NUM);
281 struct mutex msi_used_lock;
284 struct pci_bridge_emul bridge;
285 struct gpio_desc *reset_gpio;
289 static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
291 writel(val, pcie->base + reg);
294 static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
296 return readl(pcie->base + reg);
299 static u8 advk_pcie_ltssm_state(struct advk_pcie *pcie)
304 val = advk_readl(pcie, CFG_REG);
305 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
309 static inline bool advk_pcie_link_up(struct advk_pcie *pcie)
311 /* check if LTSSM is in normal operation - some L* state */
312 u8 ltssm_state = advk_pcie_ltssm_state(pcie);
313 return ltssm_state >= LTSSM_L0 && ltssm_state < LTSSM_DISABLED;
316 static inline bool advk_pcie_link_active(struct advk_pcie *pcie)
319 * According to PCIe Base specification 3.0, Table 4-14: Link
320 * Status Mapped to the LTSSM, and 4.2.6.3.6 Configuration.Idle
321 * is Link Up mapped to LTSSM Configuration.Idle, Recovery, L0,
322 * L0s, L1 and L2 states. And according to 3.2.1. Data Link
323 * Control and Management State Machine Rules is DL Up status
324 * reported in DL Active state.
326 u8 ltssm_state = advk_pcie_ltssm_state(pcie);
327 return ltssm_state >= LTSSM_CONFIG_IDLE && ltssm_state < LTSSM_DISABLED;
330 static inline bool advk_pcie_link_training(struct advk_pcie *pcie)
333 * According to PCIe Base specification 3.0, Table 4-14: Link
334 * Status Mapped to the LTSSM is Link Training mapped to LTSSM
335 * Configuration and Recovery states.
337 u8 ltssm_state = advk_pcie_ltssm_state(pcie);
338 return ((ltssm_state >= LTSSM_CONFIG_LINKWIDTH_START &&
339 ltssm_state < LTSSM_L0) ||
340 (ltssm_state >= LTSSM_RECOVERY_EQUALIZATION_PHASE0 &&
341 ltssm_state <= LTSSM_RECOVERY_EQUALIZATION_PHASE3));
344 static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
348 /* check if the link is up or not */
349 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
350 if (advk_pcie_link_up(pcie))
353 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
359 static void advk_pcie_wait_for_retrain(struct advk_pcie *pcie)
363 for (retries = 0; retries < RETRAIN_WAIT_MAX_RETRIES; ++retries) {
364 if (advk_pcie_link_training(pcie))
366 udelay(RETRAIN_WAIT_USLEEP_US);
370 static void advk_pcie_issue_perst(struct advk_pcie *pcie)
372 if (!pcie->reset_gpio)
375 /* 10ms delay is needed for some cards */
376 dev_info(&pcie->pdev->dev, "issuing PERST via reset GPIO for 10ms\n");
377 gpiod_set_value_cansleep(pcie->reset_gpio, 1);
378 usleep_range(10000, 11000);
379 gpiod_set_value_cansleep(pcie->reset_gpio, 0);
382 static void advk_pcie_train_link(struct advk_pcie *pcie)
384 struct device *dev = &pcie->pdev->dev;
389 * Setup PCIe rev / gen compliance based on device tree property
390 * 'max-link-speed' which also forces maximal link speed.
392 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
393 reg &= ~PCIE_GEN_SEL_MSK;
394 if (pcie->link_gen == 3)
396 else if (pcie->link_gen == 2)
400 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
403 * Set maximal link speed value also into PCIe Link Control 2 register.
404 * Armada 3700 Functional Specification says that default value is based
405 * on SPEED_GEN but tests showed that default value is always 8.0 GT/s.
407 reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2);
408 reg &= ~PCI_EXP_LNKCTL2_TLS;
409 if (pcie->link_gen == 3)
410 reg |= PCI_EXP_LNKCTL2_TLS_8_0GT;
411 else if (pcie->link_gen == 2)
412 reg |= PCI_EXP_LNKCTL2_TLS_5_0GT;
414 reg |= PCI_EXP_LNKCTL2_TLS_2_5GT;
415 advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2);
417 /* Enable link training after selecting PCIe generation */
418 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
419 reg |= LINK_TRAINING_EN;
420 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
423 * Reset PCIe card via PERST# signal. Some cards are not detected
424 * during link training when they are in some non-initial state.
426 advk_pcie_issue_perst(pcie);
429 * PERST# signal could have been asserted by pinctrl subsystem before
430 * probe() callback has been called or issued explicitly by reset gpio
431 * function advk_pcie_issue_perst(), making the endpoint going into
432 * fundamental reset. As required by PCI Express spec (PCI Express
433 * Base Specification, REV. 4.0 PCI Express, February 19 2014, 6.6.1
434 * Conventional Reset) a delay for at least 100ms after such a reset
435 * before sending a Configuration Request to the device is needed.
436 * So wait until PCIe link is up. Function advk_pcie_wait_for_link()
437 * waits for link at least 900ms.
439 ret = advk_pcie_wait_for_link(pcie);
441 dev_err(dev, "link never came up\n");
443 dev_info(dev, "link up\n");
447 * Set PCIe address window register which could be used for memory
450 static void advk_pcie_set_ob_win(struct advk_pcie *pcie, u8 win_num,
451 phys_addr_t match, phys_addr_t remap,
452 phys_addr_t mask, u32 actions)
454 advk_writel(pcie, OB_WIN_ENABLE |
455 lower_32_bits(match), OB_WIN_MATCH_LS(win_num));
456 advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num));
457 advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num));
458 advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num));
459 advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num));
460 advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num));
461 advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num));
464 static void advk_pcie_disable_ob_win(struct advk_pcie *pcie, u8 win_num)
466 advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num));
467 advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num));
468 advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num));
469 advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num));
470 advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num));
471 advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num));
472 advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num));
475 static void advk_pcie_setup_hw(struct advk_pcie *pcie)
481 * Configure PCIe Reference clock. Direction is from the PCIe
482 * controller to the endpoint card, so enable transmitting of
483 * Reference clock differential signal off-chip and disable
484 * receiving off-chip differential signal.
486 reg = advk_readl(pcie, PCIE_CORE_REF_CLK_REG);
487 reg |= PCIE_CORE_REF_CLK_TX_ENABLE;
488 reg &= ~PCIE_CORE_REF_CLK_RX_ENABLE;
489 advk_writel(pcie, reg, PCIE_CORE_REF_CLK_REG);
491 /* Set to Direct mode */
492 reg = advk_readl(pcie, CTRL_CONFIG_REG);
493 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
494 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
495 advk_writel(pcie, reg, CTRL_CONFIG_REG);
497 /* Set PCI global control register to RC mode */
498 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
499 reg |= (IS_RC_MSK << IS_RC_SHIFT);
500 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
503 * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab.
504 * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor
505 * id in high 16 bits. Updating this register changes readback value of
506 * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround
507 * for erratum 4.1: "The value of device and vendor ID is incorrect".
509 reg = (PCI_VENDOR_ID_MARVELL << 16) | PCI_VENDOR_ID_MARVELL;
510 advk_writel(pcie, reg, VENDOR_ID_REG);
513 * Change Class Code of PCI Bridge device to PCI Bridge (0x600400),
514 * because the default value is Mass storage controller (0x010400).
516 * Note that this Aardvark PCI Bridge does not have compliant Type 1
517 * Configuration Space and it even cannot be accessed via Aardvark's
518 * PCI config space access method. Something like config space is
519 * available in internal Aardvark registers starting at offset 0x0
520 * and is reported as Type 0. In range 0x10 - 0x34 it has totally
521 * different registers.
523 * Therefore driver uses emulation of PCI Bridge which emulates
524 * access to configuration space via internal Aardvark registers or
525 * emulated configuration buffer.
527 reg = advk_readl(pcie, PCIE_CORE_DEV_REV_REG);
529 reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
530 advk_writel(pcie, reg, PCIE_CORE_DEV_REV_REG);
532 /* Disable Root Bridge I/O space, memory space and bus mastering */
533 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
534 reg &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
535 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
537 /* Set Advanced Error Capabilities and Control PF0 register */
538 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
539 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
540 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
541 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
542 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
544 /* Set PCIe Device Control register */
545 reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL);
546 reg &= ~PCI_EXP_DEVCTL_RELAX_EN;
547 reg &= ~PCI_EXP_DEVCTL_NOSNOOP_EN;
548 reg &= ~PCI_EXP_DEVCTL_PAYLOAD;
549 reg &= ~PCI_EXP_DEVCTL_READRQ;
550 reg |= PCI_EXP_DEVCTL_PAYLOAD_512B;
551 reg |= PCI_EXP_DEVCTL_READRQ_512B;
552 advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL);
554 /* Program PCIe Control 2 to disable strict ordering */
555 reg = PCIE_CORE_CTRL2_RESERVED |
556 PCIE_CORE_CTRL2_TD_ENABLE;
557 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
560 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
561 reg &= ~LANE_CNT_MSK;
563 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
566 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
567 reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
568 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
570 /* Clear all interrupts */
571 advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG);
572 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
573 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
574 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
576 /* Disable All ISR0/1 Sources */
577 reg = PCIE_ISR0_ALL_MASK;
578 reg &= ~PCIE_ISR0_MSI_INT_PENDING;
579 advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
581 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
583 /* Unmask all MSIs */
584 advk_writel(pcie, ~(u32)PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG);
586 /* Enable summary interrupt for GIC SPI source */
587 reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
588 advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
591 * Enable AXI address window location generation:
592 * When it is enabled, the default outbound window
593 * configurations (Default User Field: 0xD0074CFC)
594 * are used to transparent address translation for
595 * the outbound transactions. Thus, PCIe address
596 * windows are not required for transparent memory
597 * access when default outbound window configuration
598 * is set for memory access.
600 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
601 reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
602 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
605 * Set memory access in Default User Field so it
606 * is not required to configure PCIe address for
607 * transparent memory access.
609 advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS);
612 * Bypass the address window mapping for PIO:
613 * Since PIO access already contains all required
614 * info over AXI interface by PIO registers, the
615 * address window is not required.
617 reg = advk_readl(pcie, PIO_CTRL);
618 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
619 advk_writel(pcie, reg, PIO_CTRL);
622 * Configure PCIe address windows for non-memory or
623 * non-transparent access as by default PCIe uses
624 * transparent memory access.
626 for (i = 0; i < pcie->wins_count; i++)
627 advk_pcie_set_ob_win(pcie, i,
628 pcie->wins[i].match, pcie->wins[i].remap,
629 pcie->wins[i].mask, pcie->wins[i].actions);
631 /* Disable remaining PCIe outbound windows */
632 for (i = pcie->wins_count; i < OB_WIN_COUNT; i++)
633 advk_pcie_disable_ob_win(pcie, i);
635 advk_pcie_train_link(pcie);
638 static int advk_pcie_check_pio_status(struct advk_pcie *pcie, bool allow_crs, u32 *val)
640 struct device *dev = &pcie->pdev->dev;
643 char *strcomp_status, *str_posted;
646 reg = advk_readl(pcie, PIO_STAT);
647 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
648 PIO_COMPLETION_STATUS_SHIFT;
651 * According to HW spec, the PIO status check sequence as below:
652 * 1) even if COMPLETION_STATUS(bit9:7) indicates successful,
653 * it still needs to check Error Status(bit11), only when this bit
654 * indicates no error happen, the operation is successful.
655 * 2) value Unsupported Request(1) of COMPLETION_STATUS(bit9:7) only
656 * means a PIO write error, and for PIO read it is successful with
657 * a read value of 0xFFFFFFFF.
658 * 3) value Completion Retry Status(CRS) of COMPLETION_STATUS(bit9:7)
659 * only means a PIO write error, and for PIO read it is successful
660 * with a read value of 0xFFFF0001.
661 * 4) value Completer Abort (CA) of COMPLETION_STATUS(bit9:7) means
662 * error for both PIO read and PIO write operation.
663 * 5) other errors are indicated as 'unknown'.
666 case PIO_COMPLETION_STATUS_OK:
667 if (reg & PIO_ERR_STATUS) {
668 strcomp_status = "COMP_ERR";
672 /* Get the read result */
674 *val = advk_readl(pcie, PIO_RD_DATA);
676 strcomp_status = NULL;
679 case PIO_COMPLETION_STATUS_UR:
680 strcomp_status = "UR";
683 case PIO_COMPLETION_STATUS_CRS:
684 if (allow_crs && val) {
685 /* PCIe r4.0, sec 2.3.2, says:
686 * If CRS Software Visibility is enabled:
687 * For a Configuration Read Request that includes both
688 * bytes of the Vendor ID field of a device Function's
689 * Configuration Space Header, the Root Complex must
690 * complete the Request to the host by returning a
691 * read-data value of 0001h for the Vendor ID field and
692 * all '1's for any additional bytes included in the
695 * So CRS in this case is not an error status.
697 *val = CFG_RD_CRS_VAL;
698 strcomp_status = NULL;
702 /* PCIe r4.0, sec 2.3.2, says:
703 * If CRS Software Visibility is not enabled, the Root Complex
704 * must re-issue the Configuration Request as a new Request.
705 * If CRS Software Visibility is enabled: For a Configuration
706 * Write Request or for any other Configuration Read Request,
707 * the Root Complex must re-issue the Configuration Request as
709 * A Root Complex implementation may choose to limit the number
710 * of Configuration Request/CRS Completion Status loops before
711 * determining that something is wrong with the target of the
712 * Request and taking appropriate action, e.g., complete the
713 * Request to the host as a failed transaction.
715 * So return -EAGAIN and caller (pci-aardvark.c driver) will
716 * re-issue request again up to the PIO_RETRY_CNT retries.
718 strcomp_status = "CRS";
721 case PIO_COMPLETION_STATUS_CA:
722 strcomp_status = "CA";
726 strcomp_status = "Unknown";
734 if (reg & PIO_NON_POSTED_REQ)
735 str_posted = "Non-posted";
737 str_posted = "Posted";
739 dev_dbg(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
740 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
745 static int advk_pcie_wait_pio(struct advk_pcie *pcie)
747 struct device *dev = &pcie->pdev->dev;
750 for (i = 1; i <= PIO_RETRY_CNT; i++) {
753 start = advk_readl(pcie, PIO_START);
754 isr = advk_readl(pcie, PIO_ISR);
757 udelay(PIO_RETRY_DELAY);
760 dev_err(dev, "PIO read/write transfer time out\n");
764 static pci_bridge_emul_read_status_t
765 advk_pci_bridge_emul_base_conf_read(struct pci_bridge_emul *bridge,
768 struct advk_pcie *pcie = bridge->data;
772 *value = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
773 return PCI_BRIDGE_EMUL_HANDLED;
775 case PCI_INTERRUPT_LINE: {
777 * From the whole 32bit register we support reading from HW only
778 * one bit: PCI_BRIDGE_CTL_BUS_RESET.
779 * Other bits are retrieved only from emulated config buffer.
781 __le32 *cfgspace = (__le32 *)&bridge->conf;
782 u32 val = le32_to_cpu(cfgspace[PCI_INTERRUPT_LINE / 4]);
783 if (advk_readl(pcie, PCIE_CORE_CTRL1_REG) & HOT_RESET_GEN)
784 val |= PCI_BRIDGE_CTL_BUS_RESET << 16;
786 val &= ~(PCI_BRIDGE_CTL_BUS_RESET << 16);
788 return PCI_BRIDGE_EMUL_HANDLED;
792 return PCI_BRIDGE_EMUL_NOT_HANDLED;
797 advk_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge,
798 int reg, u32 old, u32 new, u32 mask)
800 struct advk_pcie *pcie = bridge->data;
804 advk_writel(pcie, new, PCIE_CORE_CMD_STATUS_REG);
807 case PCI_INTERRUPT_LINE:
808 if (mask & (PCI_BRIDGE_CTL_BUS_RESET << 16)) {
809 u32 val = advk_readl(pcie, PCIE_CORE_CTRL1_REG);
810 if (new & (PCI_BRIDGE_CTL_BUS_RESET << 16))
811 val |= HOT_RESET_GEN;
813 val &= ~HOT_RESET_GEN;
814 advk_writel(pcie, val, PCIE_CORE_CTRL1_REG);
823 static pci_bridge_emul_read_status_t
824 advk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
827 struct advk_pcie *pcie = bridge->data;
832 *value = PCI_EXP_SLTSTA_PDS << 16;
833 return PCI_BRIDGE_EMUL_HANDLED;
835 case PCI_EXP_RTCTL: {
836 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG);
837 *value = (val & PCIE_MSG_PM_PME_MASK) ? 0 : PCI_EXP_RTCTL_PMEIE;
838 *value |= le16_to_cpu(bridge->pcie_conf.rootctl) & PCI_EXP_RTCTL_CRSSVE;
839 *value |= PCI_EXP_RTCAP_CRSVIS << 16;
840 return PCI_BRIDGE_EMUL_HANDLED;
843 case PCI_EXP_RTSTA: {
844 u32 isr0 = advk_readl(pcie, PCIE_ISR0_REG);
845 u32 msglog = advk_readl(pcie, PCIE_MSG_LOG_REG);
846 *value = msglog >> 16;
847 if (isr0 & PCIE_MSG_PM_PME_MASK)
848 *value |= PCI_EXP_RTSTA_PME;
849 return PCI_BRIDGE_EMUL_HANDLED;
852 case PCI_EXP_LNKCAP: {
853 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg);
855 * PCI_EXP_LNKCAP_DLLLARC bit is hardwired in aardvark HW to 0.
856 * But support for PCI_EXP_LNKSTA_DLLLA is emulated via ltssm
857 * state so explicitly enable PCI_EXP_LNKCAP_DLLLARC flag.
859 val |= PCI_EXP_LNKCAP_DLLLARC;
861 return PCI_BRIDGE_EMUL_HANDLED;
864 case PCI_EXP_LNKCTL: {
865 /* u32 contains both PCI_EXP_LNKCTL and PCI_EXP_LNKSTA */
866 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg) &
867 ~(PCI_EXP_LNKSTA_LT << 16);
868 if (advk_pcie_link_training(pcie))
869 val |= (PCI_EXP_LNKSTA_LT << 16);
870 if (advk_pcie_link_active(pcie))
871 val |= (PCI_EXP_LNKSTA_DLLLA << 16);
873 return PCI_BRIDGE_EMUL_HANDLED;
878 case PCI_EXP_DEVCAP2:
879 case PCI_EXP_DEVCTL2:
880 case PCI_EXP_LNKCAP2:
881 case PCI_EXP_LNKCTL2:
882 *value = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg);
883 return PCI_BRIDGE_EMUL_HANDLED;
886 return PCI_BRIDGE_EMUL_NOT_HANDLED;
892 advk_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge,
893 int reg, u32 old, u32 new, u32 mask)
895 struct advk_pcie *pcie = bridge->data;
899 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
900 if (new & PCI_EXP_LNKCTL_RL)
901 advk_pcie_wait_for_retrain(pcie);
904 case PCI_EXP_RTCTL: {
905 /* Only mask/unmask PME interrupt */
906 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG) &
907 ~PCIE_MSG_PM_PME_MASK;
908 if ((new & PCI_EXP_RTCTL_PMEIE) == 0)
909 val |= PCIE_MSG_PM_PME_MASK;
910 advk_writel(pcie, val, PCIE_ISR0_MASK_REG);
915 new = (new & PCI_EXP_RTSTA_PME) >> 9;
916 advk_writel(pcie, new, PCIE_ISR0_REG);
920 case PCI_EXP_DEVCTL2:
921 case PCI_EXP_LNKCTL2:
922 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
930 static struct pci_bridge_emul_ops advk_pci_bridge_emul_ops = {
931 .read_base = advk_pci_bridge_emul_base_conf_read,
932 .write_base = advk_pci_bridge_emul_base_conf_write,
933 .read_pcie = advk_pci_bridge_emul_pcie_conf_read,
934 .write_pcie = advk_pci_bridge_emul_pcie_conf_write,
938 * Initialize the configuration space of the PCI-to-PCI bridge
939 * associated with the given PCIe interface.
941 static int advk_sw_pci_bridge_init(struct advk_pcie *pcie)
943 struct pci_bridge_emul *bridge = &pcie->bridge;
945 bridge->conf.vendor =
946 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) & 0xffff);
947 bridge->conf.device =
948 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) >> 16);
949 bridge->conf.class_revision =
950 cpu_to_le32(advk_readl(pcie, PCIE_CORE_DEV_REV_REG) & 0xff);
952 /* Support 32 bits I/O addressing */
953 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32;
954 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32;
956 /* Support 64 bits memory pref */
957 bridge->conf.pref_mem_base = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
958 bridge->conf.pref_mem_limit = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
960 /* Support interrupt A for MSI feature */
961 bridge->conf.intpin = PCI_INTERRUPT_INTA;
963 /* Aardvark HW provides PCIe Capability structure in version 2 */
964 bridge->pcie_conf.cap = cpu_to_le16(2);
966 /* Indicates supports for Completion Retry Status */
967 bridge->pcie_conf.rootcap = cpu_to_le16(PCI_EXP_RTCAP_CRSVIS);
969 bridge->has_pcie = true;
971 bridge->ops = &advk_pci_bridge_emul_ops;
973 return pci_bridge_emul_init(bridge, 0);
976 static bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus,
979 if (pci_is_root_bus(bus) && PCI_SLOT(devfn) != 0)
983 * If the link goes down after we check for link-up, nothing bad
984 * happens but the config access times out.
986 if (!pci_is_root_bus(bus) && !advk_pcie_link_up(pcie))
992 static bool advk_pcie_pio_is_running(struct advk_pcie *pcie)
994 struct device *dev = &pcie->pdev->dev;
997 * Trying to start a new PIO transfer when previous has not completed
998 * cause External Abort on CPU which results in kernel panic:
1000 * SError Interrupt on CPU0, code 0xbf000002 -- SError
1001 * Kernel panic - not syncing: Asynchronous SError Interrupt
1003 * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected
1004 * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent
1005 * concurrent calls at the same time. But because PIO transfer may take
1006 * about 1.5s when link is down or card is disconnected, it means that
1007 * advk_pcie_wait_pio() does not always have to wait for completion.
1009 * Some versions of ARM Trusted Firmware handles this External Abort at
1010 * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit:
1011 * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50
1013 if (advk_readl(pcie, PIO_START)) {
1014 dev_err(dev, "Previous PIO read/write transfer is still running\n");
1021 static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
1022 int where, int size, u32 *val)
1024 struct advk_pcie *pcie = bus->sysdata;
1030 if (!advk_pcie_valid_device(pcie, bus, devfn)) {
1032 return PCIBIOS_DEVICE_NOT_FOUND;
1035 if (pci_is_root_bus(bus))
1036 return pci_bridge_emul_conf_read(&pcie->bridge, where,
1040 * Completion Retry Status is possible to return only when reading all
1041 * 4 bytes from PCI_VENDOR_ID and PCI_DEVICE_ID registers at once and
1042 * CRSSVE flag on Root Bridge is enabled.
1044 allow_crs = (where == PCI_VENDOR_ID) && (size == 4) &&
1045 (le16_to_cpu(pcie->bridge.pcie_conf.rootctl) &
1046 PCI_EXP_RTCTL_CRSSVE);
1048 if (advk_pcie_pio_is_running(pcie))
1051 /* Program the control register */
1052 reg = advk_readl(pcie, PIO_CTRL);
1053 reg &= ~PIO_CTRL_TYPE_MASK;
1054 if (pci_is_root_bus(bus->parent))
1055 reg |= PCIE_CONFIG_RD_TYPE0;
1057 reg |= PCIE_CONFIG_RD_TYPE1;
1058 advk_writel(pcie, reg, PIO_CTRL);
1060 /* Program the address registers */
1061 reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4);
1062 advk_writel(pcie, reg, PIO_ADDR_LS);
1063 advk_writel(pcie, 0, PIO_ADDR_MS);
1065 /* Program the data strobe */
1066 advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
1070 /* Clear PIO DONE ISR and start the transfer */
1071 advk_writel(pcie, 1, PIO_ISR);
1072 advk_writel(pcie, 1, PIO_START);
1074 ret = advk_pcie_wait_pio(pcie);
1080 /* Check PIO status and get the read result */
1081 ret = advk_pcie_check_pio_status(pcie, allow_crs, val);
1082 } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT);
1088 *val = (*val >> (8 * (where & 3))) & 0xff;
1090 *val = (*val >> (8 * (where & 3))) & 0xffff;
1092 return PCIBIOS_SUCCESSFUL;
1096 * If it is possible, return Completion Retry Status so that caller
1097 * tries to issue the request again instead of failing.
1100 *val = CFG_RD_CRS_VAL;
1101 return PCIBIOS_SUCCESSFUL;
1106 return PCIBIOS_SET_FAILED;
1109 static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
1110 int where, int size, u32 val)
1112 struct advk_pcie *pcie = bus->sysdata;
1114 u32 data_strobe = 0x0;
1119 if (!advk_pcie_valid_device(pcie, bus, devfn))
1120 return PCIBIOS_DEVICE_NOT_FOUND;
1122 if (pci_is_root_bus(bus))
1123 return pci_bridge_emul_conf_write(&pcie->bridge, where,
1127 return PCIBIOS_SET_FAILED;
1129 if (advk_pcie_pio_is_running(pcie))
1130 return PCIBIOS_SET_FAILED;
1132 /* Program the control register */
1133 reg = advk_readl(pcie, PIO_CTRL);
1134 reg &= ~PIO_CTRL_TYPE_MASK;
1135 if (pci_is_root_bus(bus->parent))
1136 reg |= PCIE_CONFIG_WR_TYPE0;
1138 reg |= PCIE_CONFIG_WR_TYPE1;
1139 advk_writel(pcie, reg, PIO_CTRL);
1141 /* Program the address registers */
1142 reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4);
1143 advk_writel(pcie, reg, PIO_ADDR_LS);
1144 advk_writel(pcie, 0, PIO_ADDR_MS);
1146 /* Calculate the write strobe */
1147 offset = where & 0x3;
1148 reg = val << (8 * offset);
1149 data_strobe = GENMASK(size - 1, 0) << offset;
1151 /* Program the data register */
1152 advk_writel(pcie, reg, PIO_WR_DATA);
1154 /* Program the data strobe */
1155 advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
1159 /* Clear PIO DONE ISR and start the transfer */
1160 advk_writel(pcie, 1, PIO_ISR);
1161 advk_writel(pcie, 1, PIO_START);
1163 ret = advk_pcie_wait_pio(pcie);
1165 return PCIBIOS_SET_FAILED;
1169 ret = advk_pcie_check_pio_status(pcie, false, NULL);
1170 } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT);
1172 return ret < 0 ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL;
1175 static struct pci_ops advk_pcie_ops = {
1176 .read = advk_pcie_rd_conf,
1177 .write = advk_pcie_wr_conf,
1180 static void advk_msi_irq_compose_msi_msg(struct irq_data *data,
1181 struct msi_msg *msg)
1183 struct advk_pcie *pcie = irq_data_get_irq_chip_data(data);
1184 phys_addr_t msi_msg = virt_to_phys(&pcie->msi_msg);
1186 msg->address_lo = lower_32_bits(msi_msg);
1187 msg->address_hi = upper_32_bits(msi_msg);
1188 msg->data = data->hwirq;
1191 static int advk_msi_set_affinity(struct irq_data *irq_data,
1192 const struct cpumask *mask, bool force)
1197 static int advk_msi_irq_domain_alloc(struct irq_domain *domain,
1199 unsigned int nr_irqs, void *args)
1201 struct advk_pcie *pcie = domain->host_data;
1204 mutex_lock(&pcie->msi_used_lock);
1205 hwirq = bitmap_find_free_region(pcie->msi_used, MSI_IRQ_NUM,
1206 order_base_2(nr_irqs));
1207 mutex_unlock(&pcie->msi_used_lock);
1211 for (i = 0; i < nr_irqs; i++)
1212 irq_domain_set_info(domain, virq + i, hwirq + i,
1213 &pcie->msi_bottom_irq_chip,
1214 domain->host_data, handle_simple_irq,
1220 static void advk_msi_irq_domain_free(struct irq_domain *domain,
1221 unsigned int virq, unsigned int nr_irqs)
1223 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1224 struct advk_pcie *pcie = domain->host_data;
1226 mutex_lock(&pcie->msi_used_lock);
1227 bitmap_release_region(pcie->msi_used, d->hwirq, order_base_2(nr_irqs));
1228 mutex_unlock(&pcie->msi_used_lock);
1231 static const struct irq_domain_ops advk_msi_domain_ops = {
1232 .alloc = advk_msi_irq_domain_alloc,
1233 .free = advk_msi_irq_domain_free,
1236 static void advk_pcie_irq_mask(struct irq_data *d)
1238 struct advk_pcie *pcie = d->domain->host_data;
1239 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1240 unsigned long flags;
1243 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
1244 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1245 mask |= PCIE_ISR1_INTX_ASSERT(hwirq);
1246 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
1247 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
1250 static void advk_pcie_irq_unmask(struct irq_data *d)
1252 struct advk_pcie *pcie = d->domain->host_data;
1253 irq_hw_number_t hwirq = irqd_to_hwirq(d);
1254 unsigned long flags;
1257 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
1258 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1259 mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq);
1260 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
1261 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
1264 static int advk_pcie_irq_map(struct irq_domain *h,
1265 unsigned int virq, irq_hw_number_t hwirq)
1267 struct advk_pcie *pcie = h->host_data;
1269 advk_pcie_irq_mask(irq_get_irq_data(virq));
1270 irq_set_status_flags(virq, IRQ_LEVEL);
1271 irq_set_chip_and_handler(virq, &pcie->irq_chip,
1273 irq_set_chip_data(virq, pcie);
1278 static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
1279 .map = advk_pcie_irq_map,
1280 .xlate = irq_domain_xlate_onecell,
1283 static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
1285 struct device *dev = &pcie->pdev->dev;
1286 struct device_node *node = dev->of_node;
1287 struct irq_chip *bottom_ic, *msi_ic;
1288 struct msi_domain_info *msi_di;
1289 phys_addr_t msi_msg_phys;
1291 mutex_init(&pcie->msi_used_lock);
1293 bottom_ic = &pcie->msi_bottom_irq_chip;
1295 bottom_ic->name = "MSI";
1296 bottom_ic->irq_compose_msi_msg = advk_msi_irq_compose_msi_msg;
1297 bottom_ic->irq_set_affinity = advk_msi_set_affinity;
1299 msi_ic = &pcie->msi_irq_chip;
1300 msi_ic->name = "advk-MSI";
1302 msi_di = &pcie->msi_domain_info;
1303 msi_di->flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
1304 MSI_FLAG_MULTI_PCI_MSI;
1305 msi_di->chip = msi_ic;
1307 msi_msg_phys = virt_to_phys(&pcie->msi_msg);
1309 advk_writel(pcie, lower_32_bits(msi_msg_phys),
1310 PCIE_MSI_ADDR_LOW_REG);
1311 advk_writel(pcie, upper_32_bits(msi_msg_phys),
1312 PCIE_MSI_ADDR_HIGH_REG);
1314 pcie->msi_inner_domain =
1315 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
1316 &advk_msi_domain_ops, pcie);
1317 if (!pcie->msi_inner_domain)
1321 pci_msi_create_irq_domain(of_node_to_fwnode(node),
1322 msi_di, pcie->msi_inner_domain);
1323 if (!pcie->msi_domain) {
1324 irq_domain_remove(pcie->msi_inner_domain);
1331 static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
1333 irq_domain_remove(pcie->msi_domain);
1334 irq_domain_remove(pcie->msi_inner_domain);
1337 static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
1339 struct device *dev = &pcie->pdev->dev;
1340 struct device_node *node = dev->of_node;
1341 struct device_node *pcie_intc_node;
1342 struct irq_chip *irq_chip;
1345 raw_spin_lock_init(&pcie->irq_lock);
1347 pcie_intc_node = of_get_next_child(node, NULL);
1348 if (!pcie_intc_node) {
1349 dev_err(dev, "No PCIe Intc node found\n");
1353 irq_chip = &pcie->irq_chip;
1355 irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
1357 if (!irq_chip->name) {
1362 irq_chip->irq_mask = advk_pcie_irq_mask;
1363 irq_chip->irq_mask_ack = advk_pcie_irq_mask;
1364 irq_chip->irq_unmask = advk_pcie_irq_unmask;
1367 irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
1368 &advk_pcie_irq_domain_ops, pcie);
1369 if (!pcie->irq_domain) {
1370 dev_err(dev, "Failed to get a INTx IRQ domain\n");
1376 of_node_put(pcie_intc_node);
1380 static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
1382 irq_domain_remove(pcie->irq_domain);
1385 static void advk_pcie_handle_msi(struct advk_pcie *pcie)
1387 u32 msi_val, msi_mask, msi_status, msi_idx;
1389 msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
1390 msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
1391 msi_status = msi_val & ((~msi_mask) & PCIE_MSI_ALL_MASK);
1393 for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
1394 if (!(BIT(msi_idx) & msi_status))
1397 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
1398 if (generic_handle_domain_irq(pcie->msi_inner_domain, msi_idx) == -EINVAL)
1399 dev_err_ratelimited(&pcie->pdev->dev, "unexpected MSI 0x%02x\n", msi_idx);
1402 advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
1406 static void advk_pcie_handle_int(struct advk_pcie *pcie)
1408 u32 isr0_val, isr0_mask, isr0_status;
1409 u32 isr1_val, isr1_mask, isr1_status;
1412 isr0_val = advk_readl(pcie, PCIE_ISR0_REG);
1413 isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
1414 isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK);
1416 isr1_val = advk_readl(pcie, PCIE_ISR1_REG);
1417 isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1418 isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK);
1420 /* Process MSI interrupts */
1421 if (isr0_status & PCIE_ISR0_MSI_INT_PENDING)
1422 advk_pcie_handle_msi(pcie);
1424 /* Process legacy interrupts */
1425 for (i = 0; i < PCI_NUM_INTX; i++) {
1426 if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i)))
1429 advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i),
1432 generic_handle_domain_irq(pcie->irq_domain, i);
1436 static void advk_pcie_irq_handler(struct irq_desc *desc)
1438 struct advk_pcie *pcie = irq_desc_get_handler_data(desc);
1439 struct irq_chip *chip = irq_desc_get_chip(desc);
1440 u32 val, mask, status;
1442 chained_irq_enter(chip, desc);
1444 val = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
1445 mask = advk_readl(pcie, HOST_CTRL_INT_MASK_REG);
1446 status = val & ((~mask) & PCIE_IRQ_ALL_MASK);
1448 if (status & PCIE_IRQ_CORE_INT) {
1449 advk_pcie_handle_int(pcie);
1451 /* Clear interrupt */
1452 advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
1455 chained_irq_exit(chip, desc);
1458 static void __maybe_unused advk_pcie_disable_phy(struct advk_pcie *pcie)
1460 phy_power_off(pcie->phy);
1461 phy_exit(pcie->phy);
1464 static int advk_pcie_enable_phy(struct advk_pcie *pcie)
1471 ret = phy_init(pcie->phy);
1475 ret = phy_set_mode(pcie->phy, PHY_MODE_PCIE);
1477 phy_exit(pcie->phy);
1481 ret = phy_power_on(pcie->phy);
1482 if (ret == -EOPNOTSUPP) {
1483 dev_warn(&pcie->pdev->dev, "PHY unsupported by firmware\n");
1485 phy_exit(pcie->phy);
1492 static int advk_pcie_setup_phy(struct advk_pcie *pcie)
1494 struct device *dev = &pcie->pdev->dev;
1495 struct device_node *node = dev->of_node;
1498 pcie->phy = devm_of_phy_get(dev, node, NULL);
1499 if (IS_ERR(pcie->phy) && (PTR_ERR(pcie->phy) == -EPROBE_DEFER))
1500 return PTR_ERR(pcie->phy);
1502 /* Old bindings miss the PHY handle */
1503 if (IS_ERR(pcie->phy)) {
1504 dev_warn(dev, "PHY unavailable (%ld)\n", PTR_ERR(pcie->phy));
1509 ret = advk_pcie_enable_phy(pcie);
1511 dev_err(dev, "Failed to initialize PHY (%d)\n", ret);
1516 static int advk_pcie_probe(struct platform_device *pdev)
1518 struct device *dev = &pdev->dev;
1519 struct advk_pcie *pcie;
1520 struct pci_host_bridge *bridge;
1521 struct resource_entry *entry;
1524 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie));
1528 pcie = pci_host_bridge_priv(bridge);
1530 platform_set_drvdata(pdev, pcie);
1532 resource_list_for_each_entry(entry, &bridge->windows) {
1533 resource_size_t start = entry->res->start;
1534 resource_size_t size = resource_size(entry->res);
1535 unsigned long type = resource_type(entry->res);
1539 * Aardvark hardware allows to configure also PCIe window
1540 * for config type 0 and type 1 mapping, but driver uses
1541 * only PIO for issuing configuration transfers which does
1542 * not use PCIe window configuration.
1544 if (type != IORESOURCE_MEM && type != IORESOURCE_IO)
1548 * Skip transparent memory resources. Default outbound access
1549 * configuration is set to transparent memory access so it
1550 * does not need window configuration.
1552 if (type == IORESOURCE_MEM && entry->offset == 0)
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.
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)
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);
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);
1581 pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_MEM;
1582 pcie->wins[pcie->wins_count].match = start;
1584 pcie->wins[pcie->wins_count].remap = start - entry->offset;
1585 pcie->wins[pcie->wins_count].mask = ~(win_size - 1);
1587 if (pcie->wins[pcie->wins_count].remap & (win_size - 1))
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);
1604 pcie->base = devm_platform_ioremap_resource(pdev, 0);
1605 if (IS_ERR(pcie->base))
1606 return PTR_ERR(pcie->base);
1608 pcie->irq = platform_get_irq(pdev, 0);
1612 pcie->reset_gpio = devm_gpiod_get_from_of_node(dev, dev->of_node,
1616 ret = PTR_ERR_OR_ZERO(pcie->reset_gpio);
1618 if (ret == -ENOENT) {
1619 pcie->reset_gpio = NULL;
1621 if (ret != -EPROBE_DEFER)
1622 dev_err(dev, "Failed to get reset-gpio: %i\n",
1628 ret = of_pci_get_max_link_speed(dev->of_node);
1629 if (ret <= 0 || ret > 3)
1632 pcie->link_gen = ret;
1634 ret = advk_pcie_setup_phy(pcie);
1638 advk_pcie_setup_hw(pcie);
1640 ret = advk_sw_pci_bridge_init(pcie);
1642 dev_err(dev, "Failed to register emulated root PCI bridge\n");
1646 ret = advk_pcie_init_irq_domain(pcie);
1648 dev_err(dev, "Failed to initialize irq\n");
1652 ret = advk_pcie_init_msi_irq_domain(pcie);
1654 dev_err(dev, "Failed to initialize irq\n");
1655 advk_pcie_remove_irq_domain(pcie);
1659 irq_set_chained_handler_and_data(pcie->irq, advk_pcie_irq_handler, pcie);
1661 bridge->sysdata = pcie;
1662 bridge->ops = &advk_pcie_ops;
1664 ret = pci_host_probe(bridge);
1666 irq_set_chained_handler_and_data(pcie->irq, NULL, NULL);
1667 advk_pcie_remove_msi_irq_domain(pcie);
1668 advk_pcie_remove_irq_domain(pcie);
1675 static int advk_pcie_remove(struct platform_device *pdev)
1677 struct advk_pcie *pcie = platform_get_drvdata(pdev);
1678 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1682 /* Remove PCI bus with all devices */
1683 pci_lock_rescan_remove();
1684 pci_stop_root_bus(bridge->bus);
1685 pci_remove_root_bus(bridge->bus);
1686 pci_unlock_rescan_remove();
1688 /* Disable Root Bridge I/O space, memory space and bus mastering */
1689 val = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
1690 val &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1691 advk_writel(pcie, val, PCIE_CORE_CMD_STATUS_REG);
1694 val = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
1695 val &= ~PCIE_CORE_CTRL2_MSI_ENABLE;
1696 advk_writel(pcie, val, PCIE_CORE_CTRL2_REG);
1698 /* Clear MSI address */
1699 advk_writel(pcie, 0, PCIE_MSI_ADDR_LOW_REG);
1700 advk_writel(pcie, 0, PCIE_MSI_ADDR_HIGH_REG);
1702 /* Mask all interrupts */
1703 advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG);
1704 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_MASK_REG);
1705 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
1706 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_MASK_REG);
1708 /* Clear all interrupts */
1709 advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG);
1710 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
1711 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
1712 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
1714 /* Remove IRQ handler */
1715 irq_set_chained_handler_and_data(pcie->irq, NULL, NULL);
1717 /* Remove IRQ domains */
1718 advk_pcie_remove_msi_irq_domain(pcie);
1719 advk_pcie_remove_irq_domain(pcie);
1721 /* Free config space for emulated root bridge */
1722 pci_bridge_emul_cleanup(&pcie->bridge);
1724 /* Assert PERST# signal which prepares PCIe card for power down */
1725 if (pcie->reset_gpio)
1726 gpiod_set_value_cansleep(pcie->reset_gpio, 1);
1728 /* Disable link training */
1729 val = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
1730 val &= ~LINK_TRAINING_EN;
1731 advk_writel(pcie, val, PCIE_CORE_CTRL0_REG);
1733 /* Disable outbound address windows mapping */
1734 for (i = 0; i < OB_WIN_COUNT; i++)
1735 advk_pcie_disable_ob_win(pcie, i);
1738 advk_pcie_disable_phy(pcie);
1743 static const struct of_device_id advk_pcie_of_match_table[] = {
1744 { .compatible = "marvell,armada-3700-pcie", },
1747 MODULE_DEVICE_TABLE(of, advk_pcie_of_match_table);
1749 static struct platform_driver advk_pcie_driver = {
1751 .name = "advk-pcie",
1752 .of_match_table = advk_pcie_of_match_table,
1754 .probe = advk_pcie_probe,
1755 .remove = advk_pcie_remove,
1757 module_platform_driver(advk_pcie_driver);
1759 MODULE_DESCRIPTION("Aardvark PCIe controller");
1760 MODULE_LICENSE("GPL v2");