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/of_address.h>
20 #include <linux/of_pci.h>
24 /* PCIe core registers */
25 #define PCIE_CORE_CMD_STATUS_REG 0x4
26 #define PCIE_CORE_CMD_IO_ACCESS_EN BIT(0)
27 #define PCIE_CORE_CMD_MEM_ACCESS_EN BIT(1)
28 #define PCIE_CORE_CMD_MEM_IO_REQ_EN BIT(2)
29 #define PCIE_CORE_DEV_CTRL_STATS_REG 0xc8
30 #define PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE (0 << 4)
31 #define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT 5
32 #define PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE (0 << 11)
33 #define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT 12
34 #define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ 0x2
35 #define PCIE_CORE_LINK_CTRL_STAT_REG 0xd0
36 #define PCIE_CORE_LINK_L0S_ENTRY BIT(0)
37 #define PCIE_CORE_LINK_TRAINING BIT(5)
38 #define PCIE_CORE_LINK_WIDTH_SHIFT 20
39 #define PCIE_CORE_ERR_CAPCTL_REG 0x118
40 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
41 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
42 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7)
43 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8)
45 /* PIO registers base address and register offsets */
46 #define PIO_BASE_ADDR 0x4000
47 #define PIO_CTRL (PIO_BASE_ADDR + 0x0)
48 #define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
49 #define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
50 #define PIO_STAT (PIO_BASE_ADDR + 0x4)
51 #define PIO_COMPLETION_STATUS_SHIFT 7
52 #define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
53 #define PIO_COMPLETION_STATUS_OK 0
54 #define PIO_COMPLETION_STATUS_UR 1
55 #define PIO_COMPLETION_STATUS_CRS 2
56 #define PIO_COMPLETION_STATUS_CA 4
57 #define PIO_NON_POSTED_REQ BIT(10)
58 #define PIO_ERR_STATUS BIT(11)
59 #define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
60 #define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
61 #define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
62 #define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
63 #define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
64 #define PIO_START (PIO_BASE_ADDR + 0x1c)
65 #define PIO_ISR (PIO_BASE_ADDR + 0x20)
66 #define PIO_ISRM (PIO_BASE_ADDR + 0x24)
68 /* Aardvark Control registers */
69 #define CONTROL_BASE_ADDR 0x4800
70 #define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
71 #define PCIE_GEN_SEL_MSK 0x3
72 #define PCIE_GEN_SEL_SHIFT 0x0
78 #define LANE_CNT_MSK 0x18
79 #define LANE_CNT_SHIFT 0x3
80 #define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
81 #define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
82 #define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
83 #define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
84 #define LINK_TRAINING_EN BIT(6)
85 #define LEGACY_INTA BIT(28)
86 #define LEGACY_INTB BIT(29)
87 #define LEGACY_INTC BIT(30)
88 #define LEGACY_INTD BIT(31)
89 #define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4)
90 #define HOT_RESET_GEN BIT(0)
91 #define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
92 #define PCIE_CORE_CTRL2_RESERVED 0x7
93 #define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
94 #define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
95 #define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6)
96 #define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10)
97 #define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
98 #define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44)
99 #define PCIE_ISR0_MSI_INT_PENDING BIT(24)
100 #define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val))
101 #define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val))
102 #define PCIE_ISR0_ALL_MASK GENMASK(26, 0)
103 #define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48)
104 #define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C)
105 #define PCIE_ISR1_POWER_STATE_CHANGE BIT(4)
106 #define PCIE_ISR1_FLUSH BIT(5)
107 #define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val))
108 #define PCIE_ISR1_ALL_MASK GENMASK(11, 4)
109 #define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50)
110 #define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54)
111 #define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58)
112 #define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C)
113 #define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C)
115 /* LMI registers base address and register offsets */
116 #define LMI_BASE_ADDR 0x6000
117 #define CFG_REG (LMI_BASE_ADDR + 0x0)
118 #define LTSSM_SHIFT 24
119 #define LTSSM_MASK 0x3f
120 #define LTSSM_L0 0x10
121 #define RC_BAR_CONFIG 0x300
123 /* PCIe core controller registers */
124 #define CTRL_CORE_BASE_ADDR 0x18000
125 #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
126 #define CTRL_MODE_SHIFT 0x0
127 #define CTRL_MODE_MASK 0x1
128 #define PCIE_CORE_MODE_DIRECT 0x0
129 #define PCIE_CORE_MODE_COMMAND 0x1
131 /* PCIe Central Interrupts Registers */
132 #define CENTRAL_INT_BASE_ADDR 0x1b000
133 #define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
134 #define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
135 #define PCIE_IRQ_CMDQ_INT BIT(0)
136 #define PCIE_IRQ_MSI_STATUS_INT BIT(1)
137 #define PCIE_IRQ_CMD_SENT_DONE BIT(3)
138 #define PCIE_IRQ_DMA_INT BIT(4)
139 #define PCIE_IRQ_IB_DXFERDONE BIT(5)
140 #define PCIE_IRQ_OB_DXFERDONE BIT(6)
141 #define PCIE_IRQ_OB_RXFERDONE BIT(7)
142 #define PCIE_IRQ_COMPQ_INT BIT(12)
143 #define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
144 #define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
145 #define PCIE_IRQ_CORE_INT BIT(16)
146 #define PCIE_IRQ_CORE_INT_PIO BIT(17)
147 #define PCIE_IRQ_DPMU_INT BIT(18)
148 #define PCIE_IRQ_PCIE_MIS_INT BIT(19)
149 #define PCIE_IRQ_MSI_INT1_DET BIT(20)
150 #define PCIE_IRQ_MSI_INT2_DET BIT(21)
151 #define PCIE_IRQ_RC_DBELL_DET BIT(22)
152 #define PCIE_IRQ_EP_STATUS BIT(23)
153 #define PCIE_IRQ_ALL_MASK 0xfff0fb
154 #define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
156 /* Transaction types */
157 #define PCIE_CONFIG_RD_TYPE0 0x8
158 #define PCIE_CONFIG_RD_TYPE1 0x9
159 #define PCIE_CONFIG_WR_TYPE0 0xa
160 #define PCIE_CONFIG_WR_TYPE1 0xb
162 #define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
163 #define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
164 #define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
165 #define PCIE_CONF_REG(reg) ((reg) & 0xffc)
166 #define PCIE_CONF_ADDR(bus, devfn, where) \
167 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
168 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
170 #define PIO_RETRY_CNT 750000 /* 1.5 s */
171 #define PIO_RETRY_DELAY 2 /* 2 us*/
173 #define LINK_WAIT_MAX_RETRIES 10
174 #define LINK_WAIT_USLEEP_MIN 90000
175 #define LINK_WAIT_USLEEP_MAX 100000
177 #define MSI_IRQ_NUM 32
180 struct platform_device *pdev;
182 struct list_head resources;
183 struct irq_domain *irq_domain;
184 struct irq_chip irq_chip;
185 raw_spinlock_t irq_lock;
186 struct irq_domain *msi_domain;
187 struct irq_domain *msi_inner_domain;
188 struct irq_chip msi_bottom_irq_chip;
189 struct irq_chip msi_irq_chip;
190 struct msi_domain_info msi_domain_info;
191 DECLARE_BITMAP(msi_used, MSI_IRQ_NUM);
192 struct mutex msi_used_lock;
197 static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
199 writel(val, pcie->base + reg);
202 static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
204 return readl(pcie->base + reg);
207 static int advk_pcie_link_up(struct advk_pcie *pcie)
209 u32 val, ltssm_state;
211 val = advk_readl(pcie, CFG_REG);
212 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
213 return ltssm_state >= LTSSM_L0;
216 static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
218 struct device *dev = &pcie->pdev->dev;
221 /* check if the link is up or not */
222 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
223 if (advk_pcie_link_up(pcie)) {
224 dev_info(dev, "link up\n");
228 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
231 dev_err(dev, "link never came up\n");
235 static void advk_pcie_setup_hw(struct advk_pcie *pcie)
239 /* Set to Direct mode */
240 reg = advk_readl(pcie, CTRL_CONFIG_REG);
241 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
242 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
243 advk_writel(pcie, reg, CTRL_CONFIG_REG);
245 /* Set PCI global control register to RC mode */
246 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
247 reg |= (IS_RC_MSK << IS_RC_SHIFT);
248 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
250 /* Set Advanced Error Capabilities and Control PF0 register */
251 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
252 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
253 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
254 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
255 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
257 /* Set PCIe Device Control and Status 1 PF0 register */
258 reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
259 (7 << PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT) |
260 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE |
261 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ <<
262 PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT);
263 advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
265 /* Program PCIe Control 2 to disable strict ordering */
266 reg = PCIE_CORE_CTRL2_RESERVED |
267 PCIE_CORE_CTRL2_TD_ENABLE;
268 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
271 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
272 reg &= ~PCIE_GEN_SEL_MSK;
274 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
277 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
278 reg &= ~LANE_CNT_MSK;
280 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
282 /* Enable link training */
283 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
284 reg |= LINK_TRAINING_EN;
285 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
288 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
289 reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
290 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
292 /* Clear all interrupts */
293 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
294 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
295 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
297 /* Disable All ISR0/1 Sources */
298 reg = PCIE_ISR0_ALL_MASK;
299 reg &= ~PCIE_ISR0_MSI_INT_PENDING;
300 advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
302 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
304 /* Unmask all MSI's */
305 advk_writel(pcie, 0, PCIE_MSI_MASK_REG);
307 /* Enable summary interrupt for GIC SPI source */
308 reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
309 advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
311 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
312 reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
313 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
315 /* Bypass the address window mapping for PIO */
316 reg = advk_readl(pcie, PIO_CTRL);
317 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
318 advk_writel(pcie, reg, PIO_CTRL);
320 /* Start link training */
321 reg = advk_readl(pcie, PCIE_CORE_LINK_CTRL_STAT_REG);
322 reg |= PCIE_CORE_LINK_TRAINING;
323 advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
325 advk_pcie_wait_for_link(pcie);
327 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
328 reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
329 PCIE_CORE_CMD_IO_ACCESS_EN |
330 PCIE_CORE_CMD_MEM_IO_REQ_EN;
331 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
334 static int advk_pcie_check_pio_status(struct advk_pcie *pcie, u32 *val)
336 struct device *dev = &pcie->pdev->dev;
339 char *strcomp_status, *str_posted;
341 reg = advk_readl(pcie, PIO_STAT);
342 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
343 PIO_COMPLETION_STATUS_SHIFT;
346 * According to HW spec, the PIO status check sequence as below:
347 * 1) even if COMPLETION_STATUS(bit9:7) indicates successful,
348 * it still needs to check Error Status(bit11), only when this bit
349 * indicates no error happen, the operation is successful.
350 * 2) value Unsupported Request(1) of COMPLETION_STATUS(bit9:7) only
351 * means a PIO write error, and for PIO read it is successful with
352 * a read value of 0xFFFFFFFF.
353 * 3) value Completion Retry Status(CRS) of COMPLETION_STATUS(bit9:7)
354 * only means a PIO write error, and for PIO read it is successful
355 * with a read value of 0xFFFF0001.
356 * 4) value Completer Abort (CA) of COMPLETION_STATUS(bit9:7) means
357 * error for both PIO read and PIO write operation.
358 * 5) other errors are indicated as 'unknown'.
361 case PIO_COMPLETION_STATUS_OK:
362 if (reg & PIO_ERR_STATUS) {
363 strcomp_status = "COMP_ERR";
366 /* Get the read result */
368 *val = advk_readl(pcie, PIO_RD_DATA);
370 strcomp_status = NULL;
372 case PIO_COMPLETION_STATUS_UR:
373 strcomp_status = "UR";
375 case PIO_COMPLETION_STATUS_CRS:
376 /* PCIe r4.0, sec 2.3.2, says:
377 * If CRS Software Visibility is not enabled, the Root Complex
378 * must re-issue the Configuration Request as a new Request.
379 * A Root Complex implementation may choose to limit the number
380 * of Configuration Request/CRS Completion Status loops before
381 * determining that something is wrong with the target of the
382 * Request and taking appropriate action, e.g., complete the
383 * Request to the host as a failed transaction.
385 * To simplify implementation do not re-issue the Configuration
386 * Request and complete the Request as a failed transaction.
388 strcomp_status = "CRS";
390 case PIO_COMPLETION_STATUS_CA:
391 strcomp_status = "CA";
394 strcomp_status = "Unknown";
401 if (reg & PIO_NON_POSTED_REQ)
402 str_posted = "Non-posted";
404 str_posted = "Posted";
406 dev_err(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
407 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
412 static int advk_pcie_wait_pio(struct advk_pcie *pcie)
414 struct device *dev = &pcie->pdev->dev;
417 for (i = 0; i < PIO_RETRY_CNT; i++) {
420 start = advk_readl(pcie, PIO_START);
421 isr = advk_readl(pcie, PIO_ISR);
424 udelay(PIO_RETRY_DELAY);
427 dev_err(dev, "PIO read/write transfer time out\n");
431 static bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus,
434 if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0)
440 static bool advk_pcie_pio_is_running(struct advk_pcie *pcie)
442 struct device *dev = &pcie->pdev->dev;
445 * Trying to start a new PIO transfer when previous has not completed
446 * cause External Abort on CPU which results in kernel panic:
448 * SError Interrupt on CPU0, code 0xbf000002 -- SError
449 * Kernel panic - not syncing: Asynchronous SError Interrupt
451 * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected
452 * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent
453 * concurrent calls at the same time. But because PIO transfer may take
454 * about 1.5s when link is down or card is disconnected, it means that
455 * advk_pcie_wait_pio() does not always have to wait for completion.
457 * Some versions of ARM Trusted Firmware handles this External Abort at
458 * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit:
459 * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50
461 if (advk_readl(pcie, PIO_START)) {
462 dev_err(dev, "Previous PIO read/write transfer is still running\n");
469 static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
470 int where, int size, u32 *val)
472 struct advk_pcie *pcie = bus->sysdata;
476 if (!advk_pcie_valid_device(pcie, bus, devfn)) {
478 return PCIBIOS_DEVICE_NOT_FOUND;
481 if (advk_pcie_pio_is_running(pcie)) {
483 return PCIBIOS_SET_FAILED;
486 /* Program the control register */
487 reg = advk_readl(pcie, PIO_CTRL);
488 reg &= ~PIO_CTRL_TYPE_MASK;
489 if (bus->number == pcie->root_bus_nr)
490 reg |= PCIE_CONFIG_RD_TYPE0;
492 reg |= PCIE_CONFIG_RD_TYPE1;
493 advk_writel(pcie, reg, PIO_CTRL);
495 /* Program the address registers */
496 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
497 advk_writel(pcie, reg, PIO_ADDR_LS);
498 advk_writel(pcie, 0, PIO_ADDR_MS);
500 /* Program the data strobe */
501 advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
503 /* Clear PIO DONE ISR and start the transfer */
504 advk_writel(pcie, 1, PIO_ISR);
505 advk_writel(pcie, 1, PIO_START);
507 ret = advk_pcie_wait_pio(pcie);
509 return PCIBIOS_SET_FAILED;
511 /* Check PIO status and get the read result */
512 ret = advk_pcie_check_pio_status(pcie, val);
515 return PCIBIOS_SET_FAILED;
519 *val = (*val >> (8 * (where & 3))) & 0xff;
521 *val = (*val >> (8 * (where & 3))) & 0xffff;
523 return PCIBIOS_SUCCESSFUL;
526 static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
527 int where, int size, u32 val)
529 struct advk_pcie *pcie = bus->sysdata;
531 u32 data_strobe = 0x0;
535 if (!advk_pcie_valid_device(pcie, bus, devfn))
536 return PCIBIOS_DEVICE_NOT_FOUND;
539 return PCIBIOS_SET_FAILED;
541 if (advk_pcie_pio_is_running(pcie))
542 return PCIBIOS_SET_FAILED;
544 /* Program the control register */
545 reg = advk_readl(pcie, PIO_CTRL);
546 reg &= ~PIO_CTRL_TYPE_MASK;
547 if (bus->number == pcie->root_bus_nr)
548 reg |= PCIE_CONFIG_WR_TYPE0;
550 reg |= PCIE_CONFIG_WR_TYPE1;
551 advk_writel(pcie, reg, PIO_CTRL);
553 /* Program the address registers */
554 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
555 advk_writel(pcie, reg, PIO_ADDR_LS);
556 advk_writel(pcie, 0, PIO_ADDR_MS);
558 /* Calculate the write strobe */
559 offset = where & 0x3;
560 reg = val << (8 * offset);
561 data_strobe = GENMASK(size - 1, 0) << offset;
563 /* Program the data register */
564 advk_writel(pcie, reg, PIO_WR_DATA);
566 /* Program the data strobe */
567 advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
569 /* Clear PIO DONE ISR and start the transfer */
570 advk_writel(pcie, 1, PIO_ISR);
571 advk_writel(pcie, 1, PIO_START);
573 ret = advk_pcie_wait_pio(pcie);
575 return PCIBIOS_SET_FAILED;
577 ret = advk_pcie_check_pio_status(pcie, NULL);
579 return PCIBIOS_SET_FAILED;
581 return PCIBIOS_SUCCESSFUL;
584 static struct pci_ops advk_pcie_ops = {
585 .read = advk_pcie_rd_conf,
586 .write = advk_pcie_wr_conf,
589 static void advk_msi_irq_compose_msi_msg(struct irq_data *data,
592 struct advk_pcie *pcie = irq_data_get_irq_chip_data(data);
593 phys_addr_t msi_msg = virt_to_phys(&pcie->msi_msg);
595 msg->address_lo = lower_32_bits(msi_msg);
596 msg->address_hi = upper_32_bits(msi_msg);
597 msg->data = data->irq;
600 static int advk_msi_set_affinity(struct irq_data *irq_data,
601 const struct cpumask *mask, bool force)
606 static int advk_msi_irq_domain_alloc(struct irq_domain *domain,
608 unsigned int nr_irqs, void *args)
610 struct advk_pcie *pcie = domain->host_data;
613 mutex_lock(&pcie->msi_used_lock);
614 hwirq = bitmap_find_next_zero_area(pcie->msi_used, MSI_IRQ_NUM,
616 if (hwirq >= MSI_IRQ_NUM) {
617 mutex_unlock(&pcie->msi_used_lock);
621 bitmap_set(pcie->msi_used, hwirq, nr_irqs);
622 mutex_unlock(&pcie->msi_used_lock);
624 for (i = 0; i < nr_irqs; i++)
625 irq_domain_set_info(domain, virq + i, hwirq + i,
626 &pcie->msi_bottom_irq_chip,
627 domain->host_data, handle_simple_irq,
633 static void advk_msi_irq_domain_free(struct irq_domain *domain,
634 unsigned int virq, unsigned int nr_irqs)
636 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
637 struct advk_pcie *pcie = domain->host_data;
639 mutex_lock(&pcie->msi_used_lock);
640 bitmap_clear(pcie->msi_used, d->hwirq, nr_irqs);
641 mutex_unlock(&pcie->msi_used_lock);
644 static const struct irq_domain_ops advk_msi_domain_ops = {
645 .alloc = advk_msi_irq_domain_alloc,
646 .free = advk_msi_irq_domain_free,
649 static void advk_pcie_irq_mask(struct irq_data *d)
651 struct advk_pcie *pcie = d->domain->host_data;
652 irq_hw_number_t hwirq = irqd_to_hwirq(d);
656 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
657 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
658 mask |= PCIE_ISR1_INTX_ASSERT(hwirq);
659 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
660 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
663 static void advk_pcie_irq_unmask(struct irq_data *d)
665 struct advk_pcie *pcie = d->domain->host_data;
666 irq_hw_number_t hwirq = irqd_to_hwirq(d);
670 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
671 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
672 mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq);
673 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
674 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
677 static int advk_pcie_irq_map(struct irq_domain *h,
678 unsigned int virq, irq_hw_number_t hwirq)
680 struct advk_pcie *pcie = h->host_data;
682 advk_pcie_irq_mask(irq_get_irq_data(virq));
683 irq_set_status_flags(virq, IRQ_LEVEL);
684 irq_set_chip_and_handler(virq, &pcie->irq_chip,
686 irq_set_chip_data(virq, pcie);
691 static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
692 .map = advk_pcie_irq_map,
693 .xlate = irq_domain_xlate_onecell,
696 static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
698 struct device *dev = &pcie->pdev->dev;
699 struct device_node *node = dev->of_node;
700 struct irq_chip *bottom_ic, *msi_ic;
701 struct msi_domain_info *msi_di;
702 phys_addr_t msi_msg_phys;
704 mutex_init(&pcie->msi_used_lock);
706 bottom_ic = &pcie->msi_bottom_irq_chip;
708 bottom_ic->name = "MSI";
709 bottom_ic->irq_compose_msi_msg = advk_msi_irq_compose_msi_msg;
710 bottom_ic->irq_set_affinity = advk_msi_set_affinity;
712 msi_ic = &pcie->msi_irq_chip;
713 msi_ic->name = "advk-MSI";
715 msi_di = &pcie->msi_domain_info;
716 msi_di->flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
717 MSI_FLAG_MULTI_PCI_MSI;
718 msi_di->chip = msi_ic;
720 msi_msg_phys = virt_to_phys(&pcie->msi_msg);
722 advk_writel(pcie, lower_32_bits(msi_msg_phys),
723 PCIE_MSI_ADDR_LOW_REG);
724 advk_writel(pcie, upper_32_bits(msi_msg_phys),
725 PCIE_MSI_ADDR_HIGH_REG);
727 pcie->msi_inner_domain =
728 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
729 &advk_msi_domain_ops, pcie);
730 if (!pcie->msi_inner_domain)
734 pci_msi_create_irq_domain(of_node_to_fwnode(node),
735 msi_di, pcie->msi_inner_domain);
736 if (!pcie->msi_domain) {
737 irq_domain_remove(pcie->msi_inner_domain);
744 static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
746 irq_domain_remove(pcie->msi_domain);
747 irq_domain_remove(pcie->msi_inner_domain);
750 static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
752 struct device *dev = &pcie->pdev->dev;
753 struct device_node *node = dev->of_node;
754 struct device_node *pcie_intc_node;
755 struct irq_chip *irq_chip;
757 raw_spin_lock_init(&pcie->irq_lock);
759 pcie_intc_node = of_get_next_child(node, NULL);
760 if (!pcie_intc_node) {
761 dev_err(dev, "No PCIe Intc node found\n");
765 irq_chip = &pcie->irq_chip;
767 irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
769 if (!irq_chip->name) {
770 of_node_put(pcie_intc_node);
774 irq_chip->irq_mask = advk_pcie_irq_mask;
775 irq_chip->irq_mask_ack = advk_pcie_irq_mask;
776 irq_chip->irq_unmask = advk_pcie_irq_unmask;
779 irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
780 &advk_pcie_irq_domain_ops, pcie);
781 if (!pcie->irq_domain) {
782 dev_err(dev, "Failed to get a INTx IRQ domain\n");
783 of_node_put(pcie_intc_node);
790 static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
792 irq_domain_remove(pcie->irq_domain);
795 static void advk_pcie_handle_msi(struct advk_pcie *pcie)
797 u32 msi_val, msi_mask, msi_status, msi_idx;
800 msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
801 msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
802 msi_status = msi_val & ~msi_mask;
804 for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
805 if (!(BIT(msi_idx) & msi_status))
808 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
809 msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & 0xFF;
810 generic_handle_irq(msi_data);
813 advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
817 static void advk_pcie_handle_int(struct advk_pcie *pcie)
819 u32 isr0_val, isr0_mask, isr0_status;
820 u32 isr1_val, isr1_mask, isr1_status;
823 isr0_val = advk_readl(pcie, PCIE_ISR0_REG);
824 isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
825 isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK);
827 isr1_val = advk_readl(pcie, PCIE_ISR1_REG);
828 isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
829 isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK);
831 if (!isr0_status && !isr1_status) {
832 advk_writel(pcie, isr0_val, PCIE_ISR0_REG);
833 advk_writel(pcie, isr1_val, PCIE_ISR1_REG);
837 /* Process MSI interrupts */
838 if (isr0_status & PCIE_ISR0_MSI_INT_PENDING)
839 advk_pcie_handle_msi(pcie);
841 /* Process legacy interrupts */
842 for (i = 0; i < PCI_NUM_INTX; i++) {
843 if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i)))
846 advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i),
849 virq = irq_find_mapping(pcie->irq_domain, i);
850 generic_handle_irq(virq);
854 static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
856 struct advk_pcie *pcie = arg;
859 status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
860 if (!(status & PCIE_IRQ_CORE_INT))
863 advk_pcie_handle_int(pcie);
865 /* Clear interrupt */
866 advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
871 static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie *pcie)
873 int err, res_valid = 0;
874 struct device *dev = &pcie->pdev->dev;
875 struct resource_entry *win, *tmp;
876 resource_size_t iobase;
878 INIT_LIST_HEAD(&pcie->resources);
880 err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff,
881 &pcie->resources, &iobase);
885 err = devm_request_pci_bus_resources(dev, &pcie->resources);
887 goto out_release_res;
889 resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
890 struct resource *res = win->res;
892 switch (resource_type(res)) {
894 err = devm_pci_remap_iospace(dev, res, iobase);
896 dev_warn(dev, "error %d: failed to map resource %pR\n",
898 resource_list_destroy_entry(win);
902 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
905 pcie->root_bus_nr = res->start;
911 dev_err(dev, "non-prefetchable memory resource required\n");
913 goto out_release_res;
919 pci_free_resource_list(&pcie->resources);
923 static int advk_pcie_probe(struct platform_device *pdev)
925 struct device *dev = &pdev->dev;
926 struct advk_pcie *pcie;
927 struct resource *res;
928 struct pci_host_bridge *bridge;
931 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie));
935 pcie = pci_host_bridge_priv(bridge);
938 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
939 pcie->base = devm_ioremap_resource(dev, res);
940 if (IS_ERR(pcie->base))
941 return PTR_ERR(pcie->base);
943 irq = platform_get_irq(pdev, 0);
944 ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
945 IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
948 dev_err(dev, "Failed to register interrupt\n");
952 ret = advk_pcie_parse_request_of_pci_ranges(pcie);
954 dev_err(dev, "Failed to parse resources\n");
958 advk_pcie_setup_hw(pcie);
960 ret = advk_pcie_init_irq_domain(pcie);
962 dev_err(dev, "Failed to initialize irq\n");
966 ret = advk_pcie_init_msi_irq_domain(pcie);
968 dev_err(dev, "Failed to initialize irq\n");
969 advk_pcie_remove_irq_domain(pcie);
973 list_splice_init(&pcie->resources, &bridge->windows);
974 bridge->dev.parent = dev;
975 bridge->sysdata = pcie;
977 bridge->ops = &advk_pcie_ops;
978 bridge->map_irq = of_irq_parse_and_map_pci;
979 bridge->swizzle_irq = pci_common_swizzle;
981 ret = pci_host_probe(bridge);
983 advk_pcie_remove_msi_irq_domain(pcie);
984 advk_pcie_remove_irq_domain(pcie);
991 static const struct of_device_id advk_pcie_of_match_table[] = {
992 { .compatible = "marvell,armada-3700-pcie", },
996 static struct platform_driver advk_pcie_driver = {
999 .of_match_table = advk_pcie_of_match_table,
1000 /* Driver unloading/unbinding currently not supported */
1001 .suppress_bind_attrs = true,
1003 .probe = advk_pcie_probe,
1005 builtin_platform_driver(advk_pcie_driver);