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/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/irqdomain.h>
15 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/msi.h>
20 #include <linux/of_address.h>
21 #include <linux/of_pci.h>
24 #include "../pci-bridge-emul.h"
26 /* PCIe core registers */
27 #define PCIE_CORE_DEV_ID_REG 0x0
28 #define PCIE_CORE_CMD_STATUS_REG 0x4
29 #define PCIE_CORE_CMD_IO_ACCESS_EN BIT(0)
30 #define PCIE_CORE_CMD_MEM_ACCESS_EN BIT(1)
31 #define PCIE_CORE_CMD_MEM_IO_REQ_EN BIT(2)
32 #define PCIE_CORE_DEV_REV_REG 0x8
33 #define PCIE_CORE_PCIEXP_CAP 0xc0
34 #define PCIE_CORE_DEV_CTRL_STATS_REG 0xc8
35 #define PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE (0 << 4)
36 #define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT 5
37 #define PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE (0 << 11)
38 #define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT 12
39 #define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ 0x2
40 #define PCIE_CORE_LINK_CTRL_STAT_REG 0xd0
41 #define PCIE_CORE_LINK_L0S_ENTRY BIT(0)
42 #define PCIE_CORE_LINK_TRAINING BIT(5)
43 #define PCIE_CORE_LINK_WIDTH_SHIFT 20
44 #define PCIE_CORE_ERR_CAPCTL_REG 0x118
45 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
46 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
47 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7)
48 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8)
49 #define PCIE_CORE_INT_A_ASSERT_ENABLE 1
50 #define PCIE_CORE_INT_B_ASSERT_ENABLE 2
51 #define PCIE_CORE_INT_C_ASSERT_ENABLE 3
52 #define PCIE_CORE_INT_D_ASSERT_ENABLE 4
53 /* PIO registers base address and register offsets */
54 #define PIO_BASE_ADDR 0x4000
55 #define PIO_CTRL (PIO_BASE_ADDR + 0x0)
56 #define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
57 #define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
58 #define PIO_STAT (PIO_BASE_ADDR + 0x4)
59 #define PIO_COMPLETION_STATUS_SHIFT 7
60 #define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
61 #define PIO_COMPLETION_STATUS_OK 0
62 #define PIO_COMPLETION_STATUS_UR 1
63 #define PIO_COMPLETION_STATUS_CRS 2
64 #define PIO_COMPLETION_STATUS_CA 4
65 #define PIO_NON_POSTED_REQ BIT(0)
66 #define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
67 #define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
68 #define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
69 #define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
70 #define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
71 #define PIO_START (PIO_BASE_ADDR + 0x1c)
72 #define PIO_ISR (PIO_BASE_ADDR + 0x20)
73 #define PIO_ISRM (PIO_BASE_ADDR + 0x24)
75 /* Aardvark Control registers */
76 #define CONTROL_BASE_ADDR 0x4800
77 #define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
78 #define PCIE_GEN_SEL_MSK 0x3
79 #define PCIE_GEN_SEL_SHIFT 0x0
85 #define LANE_CNT_MSK 0x18
86 #define LANE_CNT_SHIFT 0x3
87 #define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
88 #define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
89 #define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
90 #define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
91 #define LINK_TRAINING_EN BIT(6)
92 #define LEGACY_INTA BIT(28)
93 #define LEGACY_INTB BIT(29)
94 #define LEGACY_INTC BIT(30)
95 #define LEGACY_INTD BIT(31)
96 #define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4)
97 #define HOT_RESET_GEN BIT(0)
98 #define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
99 #define PCIE_CORE_CTRL2_RESERVED 0x7
100 #define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
101 #define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
102 #define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6)
103 #define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10)
104 #define PCIE_MSG_LOG_REG (CONTROL_BASE_ADDR + 0x30)
105 #define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
106 #define PCIE_MSG_PM_PME_MASK BIT(7)
107 #define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44)
108 #define PCIE_ISR0_MSI_INT_PENDING BIT(24)
109 #define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val))
110 #define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val))
111 #define PCIE_ISR0_ALL_MASK GENMASK(26, 0)
112 #define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48)
113 #define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C)
114 #define PCIE_ISR1_POWER_STATE_CHANGE BIT(4)
115 #define PCIE_ISR1_FLUSH BIT(5)
116 #define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val))
117 #define PCIE_ISR1_ALL_MASK GENMASK(11, 4)
118 #define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50)
119 #define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54)
120 #define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58)
121 #define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C)
122 #define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C)
124 /* LMI registers base address and register offsets */
125 #define LMI_BASE_ADDR 0x6000
126 #define CFG_REG (LMI_BASE_ADDR + 0x0)
127 #define LTSSM_SHIFT 24
128 #define LTSSM_MASK 0x3f
129 #define LTSSM_L0 0x10
130 #define RC_BAR_CONFIG 0x300
132 /* PCIe core controller registers */
133 #define CTRL_CORE_BASE_ADDR 0x18000
134 #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
135 #define CTRL_MODE_SHIFT 0x0
136 #define CTRL_MODE_MASK 0x1
137 #define PCIE_CORE_MODE_DIRECT 0x0
138 #define PCIE_CORE_MODE_COMMAND 0x1
140 /* PCIe Central Interrupts Registers */
141 #define CENTRAL_INT_BASE_ADDR 0x1b000
142 #define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
143 #define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
144 #define PCIE_IRQ_CMDQ_INT BIT(0)
145 #define PCIE_IRQ_MSI_STATUS_INT BIT(1)
146 #define PCIE_IRQ_CMD_SENT_DONE BIT(3)
147 #define PCIE_IRQ_DMA_INT BIT(4)
148 #define PCIE_IRQ_IB_DXFERDONE BIT(5)
149 #define PCIE_IRQ_OB_DXFERDONE BIT(6)
150 #define PCIE_IRQ_OB_RXFERDONE BIT(7)
151 #define PCIE_IRQ_COMPQ_INT BIT(12)
152 #define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
153 #define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
154 #define PCIE_IRQ_CORE_INT BIT(16)
155 #define PCIE_IRQ_CORE_INT_PIO BIT(17)
156 #define PCIE_IRQ_DPMU_INT BIT(18)
157 #define PCIE_IRQ_PCIE_MIS_INT BIT(19)
158 #define PCIE_IRQ_MSI_INT1_DET BIT(20)
159 #define PCIE_IRQ_MSI_INT2_DET BIT(21)
160 #define PCIE_IRQ_RC_DBELL_DET BIT(22)
161 #define PCIE_IRQ_EP_STATUS BIT(23)
162 #define PCIE_IRQ_ALL_MASK 0xfff0fb
163 #define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
165 /* Transaction types */
166 #define PCIE_CONFIG_RD_TYPE0 0x8
167 #define PCIE_CONFIG_RD_TYPE1 0x9
168 #define PCIE_CONFIG_WR_TYPE0 0xa
169 #define PCIE_CONFIG_WR_TYPE1 0xb
171 #define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
172 #define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
173 #define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
174 #define PCIE_CONF_REG(reg) ((reg) & 0xffc)
175 #define PCIE_CONF_ADDR(bus, devfn, where) \
176 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
177 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
179 #define PIO_RETRY_CNT 500
180 #define PIO_RETRY_DELAY 2 /* 2 us*/
182 #define LINK_WAIT_MAX_RETRIES 10
183 #define LINK_WAIT_USLEEP_MIN 90000
184 #define LINK_WAIT_USLEEP_MAX 100000
185 #define RETRAIN_WAIT_MAX_RETRIES 10
186 #define RETRAIN_WAIT_USLEEP_US 2000
188 #define MSI_IRQ_NUM 32
191 struct platform_device *pdev;
193 struct list_head resources;
194 struct irq_domain *irq_domain;
195 struct irq_chip irq_chip;
196 struct irq_domain *msi_domain;
197 struct irq_domain *msi_inner_domain;
198 struct irq_chip msi_bottom_irq_chip;
199 struct irq_chip msi_irq_chip;
200 struct msi_domain_info msi_domain_info;
201 DECLARE_BITMAP(msi_used, MSI_IRQ_NUM);
202 struct mutex msi_used_lock;
205 struct pci_bridge_emul bridge;
208 static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
210 writel(val, pcie->base + reg);
213 static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
215 return readl(pcie->base + reg);
218 static int advk_pcie_link_up(struct advk_pcie *pcie)
220 u32 val, ltssm_state;
222 val = advk_readl(pcie, CFG_REG);
223 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
224 return ltssm_state >= LTSSM_L0;
227 static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
229 struct device *dev = &pcie->pdev->dev;
232 /* check if the link is up or not */
233 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
234 if (advk_pcie_link_up(pcie)) {
235 dev_info(dev, "link up\n");
239 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
242 dev_err(dev, "link never came up\n");
246 static void advk_pcie_wait_for_retrain(struct advk_pcie *pcie)
250 for (retries = 0; retries < RETRAIN_WAIT_MAX_RETRIES; ++retries) {
251 if (!advk_pcie_link_up(pcie))
253 udelay(RETRAIN_WAIT_USLEEP_US);
257 static void advk_pcie_setup_hw(struct advk_pcie *pcie)
261 /* Set to Direct mode */
262 reg = advk_readl(pcie, CTRL_CONFIG_REG);
263 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
264 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
265 advk_writel(pcie, reg, CTRL_CONFIG_REG);
267 /* Set PCI global control register to RC mode */
268 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
269 reg |= (IS_RC_MSK << IS_RC_SHIFT);
270 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
272 /* Set Advanced Error Capabilities and Control PF0 register */
273 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
274 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
275 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
276 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
277 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
279 /* Set PCIe Device Control and Status 1 PF0 register */
280 reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
281 (7 << PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT) |
282 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE |
283 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ <<
284 PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT);
285 advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
287 /* Program PCIe Control 2 to disable strict ordering */
288 reg = PCIE_CORE_CTRL2_RESERVED |
289 PCIE_CORE_CTRL2_TD_ENABLE;
290 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
293 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
294 reg &= ~PCIE_GEN_SEL_MSK;
296 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
299 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
300 reg &= ~LANE_CNT_MSK;
302 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
304 /* Enable link training */
305 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
306 reg |= LINK_TRAINING_EN;
307 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
310 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
311 reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
312 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
314 /* Clear all interrupts */
315 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
316 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
317 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
319 /* Disable All ISR0/1 Sources */
320 reg = PCIE_ISR0_ALL_MASK;
321 reg &= ~PCIE_ISR0_MSI_INT_PENDING;
322 advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
324 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
326 /* Unmask all MSIs */
327 advk_writel(pcie, 0, PCIE_MSI_MASK_REG);
329 /* Enable summary interrupt for GIC SPI source */
330 reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
331 advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
333 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
334 reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
335 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
337 /* Bypass the address window mapping for PIO */
338 reg = advk_readl(pcie, PIO_CTRL);
339 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
340 advk_writel(pcie, reg, PIO_CTRL);
343 * PERST# signal could have been asserted by pinctrl subsystem before
344 * probe() callback has been called, making the endpoint going into
345 * fundamental reset. As required by PCI Express spec a delay for at
346 * least 100ms after such a reset before link training is needed.
348 msleep(PCI_PM_D3COLD_WAIT);
350 /* Start link training */
351 reg = advk_readl(pcie, PCIE_CORE_LINK_CTRL_STAT_REG);
352 reg |= PCIE_CORE_LINK_TRAINING;
353 advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
355 advk_pcie_wait_for_link(pcie);
357 reg = PCIE_CORE_LINK_L0S_ENTRY |
358 (1 << PCIE_CORE_LINK_WIDTH_SHIFT);
359 advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
361 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
362 reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
363 PCIE_CORE_CMD_IO_ACCESS_EN |
364 PCIE_CORE_CMD_MEM_IO_REQ_EN;
365 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
368 static void advk_pcie_check_pio_status(struct advk_pcie *pcie)
370 struct device *dev = &pcie->pdev->dev;
373 char *strcomp_status, *str_posted;
375 reg = advk_readl(pcie, PIO_STAT);
376 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
377 PIO_COMPLETION_STATUS_SHIFT;
383 case PIO_COMPLETION_STATUS_UR:
384 strcomp_status = "UR";
386 case PIO_COMPLETION_STATUS_CRS:
387 strcomp_status = "CRS";
389 case PIO_COMPLETION_STATUS_CA:
390 strcomp_status = "CA";
393 strcomp_status = "Unknown";
397 if (reg & PIO_NON_POSTED_REQ)
398 str_posted = "Non-posted";
400 str_posted = "Posted";
402 dev_err(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
403 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
406 static int advk_pcie_wait_pio(struct advk_pcie *pcie)
408 struct device *dev = &pcie->pdev->dev;
411 for (i = 0; i < PIO_RETRY_CNT; i++) {
414 start = advk_readl(pcie, PIO_START);
415 isr = advk_readl(pcie, PIO_ISR);
418 udelay(PIO_RETRY_DELAY);
421 dev_err(dev, "config read/write timed out\n");
426 static pci_bridge_emul_read_status_t
427 advk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
430 struct advk_pcie *pcie = bridge->data;
435 *value = PCI_EXP_SLTSTA_PDS << 16;
436 return PCI_BRIDGE_EMUL_HANDLED;
438 case PCI_EXP_RTCTL: {
439 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG);
440 *value = (val & PCIE_MSG_PM_PME_MASK) ? 0 : PCI_EXP_RTCTL_PMEIE;
441 return PCI_BRIDGE_EMUL_HANDLED;
444 case PCI_EXP_RTSTA: {
445 u32 isr0 = advk_readl(pcie, PCIE_ISR0_REG);
446 u32 msglog = advk_readl(pcie, PCIE_MSG_LOG_REG);
447 *value = (isr0 & PCIE_MSG_PM_PME_MASK) << 16 | (msglog >> 16);
448 return PCI_BRIDGE_EMUL_HANDLED;
451 case PCI_EXP_LNKCTL: {
452 /* u32 contains both PCI_EXP_LNKCTL and PCI_EXP_LNKSTA */
453 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg) &
454 ~(PCI_EXP_LNKSTA_LT << 16);
455 if (!advk_pcie_link_up(pcie))
456 val |= (PCI_EXP_LNKSTA_LT << 16);
458 return PCI_BRIDGE_EMUL_HANDLED;
461 case PCI_CAP_LIST_ID:
465 *value = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg);
466 return PCI_BRIDGE_EMUL_HANDLED;
468 return PCI_BRIDGE_EMUL_NOT_HANDLED;
474 advk_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge,
475 int reg, u32 old, u32 new, u32 mask)
477 struct advk_pcie *pcie = bridge->data;
481 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
485 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
486 if (new & PCI_EXP_LNKCTL_RL)
487 advk_pcie_wait_for_retrain(pcie);
490 case PCI_EXP_RTCTL: {
491 /* Only mask/unmask PME interrupt */
492 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG) &
493 ~PCIE_MSG_PM_PME_MASK;
494 if ((new & PCI_EXP_RTCTL_PMEIE) == 0)
495 val |= PCIE_MSG_PM_PME_MASK;
496 advk_writel(pcie, val, PCIE_ISR0_MASK_REG);
501 new = (new & PCI_EXP_RTSTA_PME) >> 9;
502 advk_writel(pcie, new, PCIE_ISR0_REG);
510 static struct pci_bridge_emul_ops advk_pci_bridge_emul_ops = {
511 .read_pcie = advk_pci_bridge_emul_pcie_conf_read,
512 .write_pcie = advk_pci_bridge_emul_pcie_conf_write,
516 * Initialize the configuration space of the PCI-to-PCI bridge
517 * associated with the given PCIe interface.
519 static void advk_sw_pci_bridge_init(struct advk_pcie *pcie)
521 struct pci_bridge_emul *bridge = &pcie->bridge;
523 bridge->conf.vendor =
524 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) & 0xffff);
525 bridge->conf.device =
526 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) >> 16);
527 bridge->conf.class_revision =
528 cpu_to_le32(advk_readl(pcie, PCIE_CORE_DEV_REV_REG) & 0xff);
530 /* Support 32 bits I/O addressing */
531 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32;
532 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32;
534 /* Support 64 bits memory pref */
535 bridge->conf.pref_mem_base = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
536 bridge->conf.pref_mem_limit = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
538 /* Support interrupt A for MSI feature */
539 bridge->conf.intpin = PCIE_CORE_INT_A_ASSERT_ENABLE;
541 bridge->has_pcie = true;
543 bridge->ops = &advk_pci_bridge_emul_ops;
545 pci_bridge_emul_init(bridge, 0);
549 static bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus,
552 if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0)
558 static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
559 int where, int size, u32 *val)
561 struct advk_pcie *pcie = bus->sysdata;
565 if (!advk_pcie_valid_device(pcie, bus, devfn)) {
567 return PCIBIOS_DEVICE_NOT_FOUND;
570 if (bus->number == pcie->root_bus_nr)
571 return pci_bridge_emul_conf_read(&pcie->bridge, where,
575 advk_writel(pcie, 0, PIO_START);
576 advk_writel(pcie, 1, PIO_ISR);
578 /* Program the control register */
579 reg = advk_readl(pcie, PIO_CTRL);
580 reg &= ~PIO_CTRL_TYPE_MASK;
581 if (bus->primary == pcie->root_bus_nr)
582 reg |= PCIE_CONFIG_RD_TYPE0;
584 reg |= PCIE_CONFIG_RD_TYPE1;
585 advk_writel(pcie, reg, PIO_CTRL);
587 /* Program the address registers */
588 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
589 advk_writel(pcie, reg, PIO_ADDR_LS);
590 advk_writel(pcie, 0, PIO_ADDR_MS);
592 /* Program the data strobe */
593 advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
595 /* Start the transfer */
596 advk_writel(pcie, 1, PIO_START);
598 ret = advk_pcie_wait_pio(pcie);
600 return PCIBIOS_SET_FAILED;
602 advk_pcie_check_pio_status(pcie);
604 /* Get the read result */
605 *val = advk_readl(pcie, PIO_RD_DATA);
607 *val = (*val >> (8 * (where & 3))) & 0xff;
609 *val = (*val >> (8 * (where & 3))) & 0xffff;
611 return PCIBIOS_SUCCESSFUL;
614 static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
615 int where, int size, u32 val)
617 struct advk_pcie *pcie = bus->sysdata;
619 u32 data_strobe = 0x0;
623 if (!advk_pcie_valid_device(pcie, bus, devfn))
624 return PCIBIOS_DEVICE_NOT_FOUND;
626 if (bus->number == pcie->root_bus_nr)
627 return pci_bridge_emul_conf_write(&pcie->bridge, where,
631 return PCIBIOS_SET_FAILED;
634 advk_writel(pcie, 0, PIO_START);
635 advk_writel(pcie, 1, PIO_ISR);
637 /* Program the control register */
638 reg = advk_readl(pcie, PIO_CTRL);
639 reg &= ~PIO_CTRL_TYPE_MASK;
640 if (bus->primary == pcie->root_bus_nr)
641 reg |= PCIE_CONFIG_WR_TYPE0;
643 reg |= PCIE_CONFIG_WR_TYPE1;
644 advk_writel(pcie, reg, PIO_CTRL);
646 /* Program the address registers */
647 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
648 advk_writel(pcie, reg, PIO_ADDR_LS);
649 advk_writel(pcie, 0, PIO_ADDR_MS);
651 /* Calculate the write strobe */
652 offset = where & 0x3;
653 reg = val << (8 * offset);
654 data_strobe = GENMASK(size - 1, 0) << offset;
656 /* Program the data register */
657 advk_writel(pcie, reg, PIO_WR_DATA);
659 /* Program the data strobe */
660 advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
662 /* Start the transfer */
663 advk_writel(pcie, 1, PIO_START);
665 ret = advk_pcie_wait_pio(pcie);
667 return PCIBIOS_SET_FAILED;
669 advk_pcie_check_pio_status(pcie);
671 return PCIBIOS_SUCCESSFUL;
674 static struct pci_ops advk_pcie_ops = {
675 .read = advk_pcie_rd_conf,
676 .write = advk_pcie_wr_conf,
679 static void advk_msi_irq_compose_msi_msg(struct irq_data *data,
682 struct advk_pcie *pcie = irq_data_get_irq_chip_data(data);
683 phys_addr_t msi_msg = virt_to_phys(&pcie->msi_msg);
685 msg->address_lo = lower_32_bits(msi_msg);
686 msg->address_hi = upper_32_bits(msi_msg);
687 msg->data = data->irq;
690 static int advk_msi_set_affinity(struct irq_data *irq_data,
691 const struct cpumask *mask, bool force)
696 static int advk_msi_irq_domain_alloc(struct irq_domain *domain,
698 unsigned int nr_irqs, void *args)
700 struct advk_pcie *pcie = domain->host_data;
703 mutex_lock(&pcie->msi_used_lock);
704 hwirq = bitmap_find_next_zero_area(pcie->msi_used, MSI_IRQ_NUM,
706 if (hwirq >= MSI_IRQ_NUM) {
707 mutex_unlock(&pcie->msi_used_lock);
711 bitmap_set(pcie->msi_used, hwirq, nr_irqs);
712 mutex_unlock(&pcie->msi_used_lock);
714 for (i = 0; i < nr_irqs; i++)
715 irq_domain_set_info(domain, virq + i, hwirq + i,
716 &pcie->msi_bottom_irq_chip,
717 domain->host_data, handle_simple_irq,
723 static void advk_msi_irq_domain_free(struct irq_domain *domain,
724 unsigned int virq, unsigned int nr_irqs)
726 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
727 struct advk_pcie *pcie = domain->host_data;
729 mutex_lock(&pcie->msi_used_lock);
730 bitmap_clear(pcie->msi_used, d->hwirq, nr_irqs);
731 mutex_unlock(&pcie->msi_used_lock);
734 static const struct irq_domain_ops advk_msi_domain_ops = {
735 .alloc = advk_msi_irq_domain_alloc,
736 .free = advk_msi_irq_domain_free,
739 static void advk_pcie_irq_mask(struct irq_data *d)
741 struct advk_pcie *pcie = d->domain->host_data;
742 irq_hw_number_t hwirq = irqd_to_hwirq(d);
745 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
746 mask |= PCIE_ISR1_INTX_ASSERT(hwirq);
747 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
750 static void advk_pcie_irq_unmask(struct irq_data *d)
752 struct advk_pcie *pcie = d->domain->host_data;
753 irq_hw_number_t hwirq = irqd_to_hwirq(d);
756 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
757 mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq);
758 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
761 static int advk_pcie_irq_map(struct irq_domain *h,
762 unsigned int virq, irq_hw_number_t hwirq)
764 struct advk_pcie *pcie = h->host_data;
766 advk_pcie_irq_mask(irq_get_irq_data(virq));
767 irq_set_status_flags(virq, IRQ_LEVEL);
768 irq_set_chip_and_handler(virq, &pcie->irq_chip,
770 irq_set_chip_data(virq, pcie);
775 static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
776 .map = advk_pcie_irq_map,
777 .xlate = irq_domain_xlate_onecell,
780 static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
782 struct device *dev = &pcie->pdev->dev;
783 struct device_node *node = dev->of_node;
784 struct irq_chip *bottom_ic, *msi_ic;
785 struct msi_domain_info *msi_di;
786 phys_addr_t msi_msg_phys;
788 mutex_init(&pcie->msi_used_lock);
790 bottom_ic = &pcie->msi_bottom_irq_chip;
792 bottom_ic->name = "MSI";
793 bottom_ic->irq_compose_msi_msg = advk_msi_irq_compose_msi_msg;
794 bottom_ic->irq_set_affinity = advk_msi_set_affinity;
796 msi_ic = &pcie->msi_irq_chip;
797 msi_ic->name = "advk-MSI";
799 msi_di = &pcie->msi_domain_info;
800 msi_di->flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
801 MSI_FLAG_MULTI_PCI_MSI;
802 msi_di->chip = msi_ic;
804 msi_msg_phys = virt_to_phys(&pcie->msi_msg);
806 advk_writel(pcie, lower_32_bits(msi_msg_phys),
807 PCIE_MSI_ADDR_LOW_REG);
808 advk_writel(pcie, upper_32_bits(msi_msg_phys),
809 PCIE_MSI_ADDR_HIGH_REG);
811 pcie->msi_inner_domain =
812 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
813 &advk_msi_domain_ops, pcie);
814 if (!pcie->msi_inner_domain)
818 pci_msi_create_irq_domain(of_node_to_fwnode(node),
819 msi_di, pcie->msi_inner_domain);
820 if (!pcie->msi_domain) {
821 irq_domain_remove(pcie->msi_inner_domain);
828 static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
830 irq_domain_remove(pcie->msi_domain);
831 irq_domain_remove(pcie->msi_inner_domain);
834 static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
836 struct device *dev = &pcie->pdev->dev;
837 struct device_node *node = dev->of_node;
838 struct device_node *pcie_intc_node;
839 struct irq_chip *irq_chip;
842 pcie_intc_node = of_get_next_child(node, NULL);
843 if (!pcie_intc_node) {
844 dev_err(dev, "No PCIe Intc node found\n");
848 irq_chip = &pcie->irq_chip;
850 irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
852 if (!irq_chip->name) {
857 irq_chip->irq_mask = advk_pcie_irq_mask;
858 irq_chip->irq_mask_ack = advk_pcie_irq_mask;
859 irq_chip->irq_unmask = advk_pcie_irq_unmask;
862 irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
863 &advk_pcie_irq_domain_ops, pcie);
864 if (!pcie->irq_domain) {
865 dev_err(dev, "Failed to get a INTx IRQ domain\n");
871 of_node_put(pcie_intc_node);
875 static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
877 irq_domain_remove(pcie->irq_domain);
880 static void advk_pcie_handle_msi(struct advk_pcie *pcie)
882 u32 msi_val, msi_mask, msi_status, msi_idx;
885 msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
886 msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
887 msi_status = msi_val & ~msi_mask;
889 for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
890 if (!(BIT(msi_idx) & msi_status))
893 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
894 msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & 0xFF;
895 generic_handle_irq(msi_data);
898 advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
902 static void advk_pcie_handle_int(struct advk_pcie *pcie)
904 u32 isr0_val, isr0_mask, isr0_status;
905 u32 isr1_val, isr1_mask, isr1_status;
908 isr0_val = advk_readl(pcie, PCIE_ISR0_REG);
909 isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
910 isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK);
912 isr1_val = advk_readl(pcie, PCIE_ISR1_REG);
913 isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
914 isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK);
916 if (!isr0_status && !isr1_status) {
917 advk_writel(pcie, isr0_val, PCIE_ISR0_REG);
918 advk_writel(pcie, isr1_val, PCIE_ISR1_REG);
922 /* Process MSI interrupts */
923 if (isr0_status & PCIE_ISR0_MSI_INT_PENDING)
924 advk_pcie_handle_msi(pcie);
926 /* Process legacy interrupts */
927 for (i = 0; i < PCI_NUM_INTX; i++) {
928 if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i)))
931 advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i),
934 virq = irq_find_mapping(pcie->irq_domain, i);
935 generic_handle_irq(virq);
939 static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
941 struct advk_pcie *pcie = arg;
944 status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
945 if (!(status & PCIE_IRQ_CORE_INT))
948 advk_pcie_handle_int(pcie);
950 /* Clear interrupt */
951 advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
956 static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie *pcie)
958 int err, res_valid = 0;
959 struct device *dev = &pcie->pdev->dev;
960 struct resource_entry *win, *tmp;
961 resource_size_t iobase;
963 INIT_LIST_HEAD(&pcie->resources);
965 err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff,
966 &pcie->resources, &iobase);
970 err = devm_request_pci_bus_resources(dev, &pcie->resources);
972 goto out_release_res;
974 resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
975 struct resource *res = win->res;
977 switch (resource_type(res)) {
979 err = devm_pci_remap_iospace(dev, res, iobase);
981 dev_warn(dev, "error %d: failed to map resource %pR\n",
983 resource_list_destroy_entry(win);
987 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
990 pcie->root_bus_nr = res->start;
996 dev_err(dev, "non-prefetchable memory resource required\n");
998 goto out_release_res;
1004 pci_free_resource_list(&pcie->resources);
1008 static int advk_pcie_probe(struct platform_device *pdev)
1010 struct device *dev = &pdev->dev;
1011 struct advk_pcie *pcie;
1012 struct resource *res;
1013 struct pci_host_bridge *bridge;
1016 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie));
1020 pcie = pci_host_bridge_priv(bridge);
1023 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1024 pcie->base = devm_ioremap_resource(dev, res);
1025 if (IS_ERR(pcie->base))
1026 return PTR_ERR(pcie->base);
1028 irq = platform_get_irq(pdev, 0);
1029 ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
1030 IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
1033 dev_err(dev, "Failed to register interrupt\n");
1037 ret = advk_pcie_parse_request_of_pci_ranges(pcie);
1039 dev_err(dev, "Failed to parse resources\n");
1043 advk_pcie_setup_hw(pcie);
1045 advk_sw_pci_bridge_init(pcie);
1047 ret = advk_pcie_init_irq_domain(pcie);
1049 dev_err(dev, "Failed to initialize irq\n");
1053 ret = advk_pcie_init_msi_irq_domain(pcie);
1055 dev_err(dev, "Failed to initialize irq\n");
1056 advk_pcie_remove_irq_domain(pcie);
1060 list_splice_init(&pcie->resources, &bridge->windows);
1061 bridge->dev.parent = dev;
1062 bridge->sysdata = pcie;
1064 bridge->ops = &advk_pcie_ops;
1065 bridge->map_irq = of_irq_parse_and_map_pci;
1066 bridge->swizzle_irq = pci_common_swizzle;
1068 ret = pci_host_probe(bridge);
1070 advk_pcie_remove_msi_irq_domain(pcie);
1071 advk_pcie_remove_irq_domain(pcie);
1078 static const struct of_device_id advk_pcie_of_match_table[] = {
1079 { .compatible = "marvell,armada-3700-pcie", },
1083 static struct platform_driver advk_pcie_driver = {
1085 .name = "advk-pcie",
1086 .of_match_table = advk_pcie_of_match_table,
1087 /* Driver unloading/unbinding currently not supported */
1088 .suppress_bind_attrs = true,
1090 .probe = advk_pcie_probe,
1092 builtin_platform_driver(advk_pcie_driver);