2 * Driver for the Aardvark PCIe controller, used on Marvell Armada
5 * Copyright (C) 2016 Marvell
7 * Author: Hezi Shahmoon <hezi.shahmoon@marvell.com>
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/irqdomain.h>
18 #include <linux/kernel.h>
19 #include <linux/pci.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/of_address.h>
23 #include <linux/of_pci.h>
25 /* PCIe core registers */
26 #define PCIE_CORE_CMD_STATUS_REG 0x4
27 #define PCIE_CORE_CMD_IO_ACCESS_EN BIT(0)
28 #define PCIE_CORE_CMD_MEM_ACCESS_EN BIT(1)
29 #define PCIE_CORE_CMD_MEM_IO_REQ_EN BIT(2)
30 #define PCIE_CORE_DEV_CTRL_STATS_REG 0xc8
31 #define PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE (0 << 4)
32 #define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT 5
33 #define PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE (0 << 11)
34 #define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT 12
35 #define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ 0x2
36 #define PCIE_CORE_LINK_CTRL_STAT_REG 0xd0
37 #define PCIE_CORE_LINK_L0S_ENTRY BIT(0)
38 #define PCIE_CORE_LINK_TRAINING BIT(5)
39 #define PCIE_CORE_LINK_WIDTH_SHIFT 20
40 #define PCIE_CORE_ERR_CAPCTL_REG 0x118
41 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
42 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
43 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7)
44 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8)
46 /* PIO registers base address and register offsets */
47 #define PIO_BASE_ADDR 0x4000
48 #define PIO_CTRL (PIO_BASE_ADDR + 0x0)
49 #define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
50 #define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
51 #define PIO_STAT (PIO_BASE_ADDR + 0x4)
52 #define PIO_COMPLETION_STATUS_SHIFT 7
53 #define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
54 #define PIO_COMPLETION_STATUS_OK 0
55 #define PIO_COMPLETION_STATUS_UR 1
56 #define PIO_COMPLETION_STATUS_CRS 2
57 #define PIO_COMPLETION_STATUS_CA 4
58 #define PIO_NON_POSTED_REQ BIT(10)
59 #define PIO_ERR_STATUS BIT(11)
60 #define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
61 #define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
62 #define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
63 #define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
64 #define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
65 #define PIO_START (PIO_BASE_ADDR + 0x1c)
66 #define PIO_ISR (PIO_BASE_ADDR + 0x20)
67 #define PIO_ISRM (PIO_BASE_ADDR + 0x24)
69 /* Aardvark Control registers */
70 #define CONTROL_BASE_ADDR 0x4800
71 #define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
72 #define PCIE_GEN_SEL_MSK 0x3
73 #define PCIE_GEN_SEL_SHIFT 0x0
79 #define LANE_CNT_MSK 0x18
80 #define LANE_CNT_SHIFT 0x3
81 #define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
82 #define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
83 #define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
84 #define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
85 #define LINK_TRAINING_EN BIT(6)
86 #define LEGACY_INTA BIT(28)
87 #define LEGACY_INTB BIT(29)
88 #define LEGACY_INTC BIT(30)
89 #define LEGACY_INTD BIT(31)
90 #define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4)
91 #define HOT_RESET_GEN BIT(0)
92 #define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
93 #define PCIE_CORE_CTRL2_RESERVED 0x7
94 #define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
95 #define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
96 #define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6)
97 #define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10)
98 #define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
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(26, 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(11, 4)
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_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C)
116 /* PCIe window configuration */
117 #define OB_WIN_BASE_ADDR 0x4c00
118 #define OB_WIN_BLOCK_SIZE 0x20
119 #define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
120 OB_WIN_BLOCK_SIZE * (win) + \
122 #define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
123 #define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
124 #define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
125 #define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
126 #define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
127 #define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
128 #define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
130 /* PCIe window types */
131 #define OB_PCIE_MEM 0x0
132 #define OB_PCIE_IO 0x4
134 /* LMI registers base address and register offsets */
135 #define LMI_BASE_ADDR 0x6000
136 #define CFG_REG (LMI_BASE_ADDR + 0x0)
137 #define LTSSM_SHIFT 24
138 #define LTSSM_MASK 0x3f
139 #define LTSSM_L0 0x10
140 #define RC_BAR_CONFIG 0x300
142 /* PCIe core controller registers */
143 #define CTRL_CORE_BASE_ADDR 0x18000
144 #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
145 #define CTRL_MODE_SHIFT 0x0
146 #define CTRL_MODE_MASK 0x1
147 #define PCIE_CORE_MODE_DIRECT 0x0
148 #define PCIE_CORE_MODE_COMMAND 0x1
150 /* PCIe Central Interrupts Registers */
151 #define CENTRAL_INT_BASE_ADDR 0x1b000
152 #define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
153 #define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
154 #define PCIE_IRQ_CMDQ_INT BIT(0)
155 #define PCIE_IRQ_MSI_STATUS_INT BIT(1)
156 #define PCIE_IRQ_CMD_SENT_DONE BIT(3)
157 #define PCIE_IRQ_DMA_INT BIT(4)
158 #define PCIE_IRQ_IB_DXFERDONE BIT(5)
159 #define PCIE_IRQ_OB_DXFERDONE BIT(6)
160 #define PCIE_IRQ_OB_RXFERDONE BIT(7)
161 #define PCIE_IRQ_COMPQ_INT BIT(12)
162 #define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
163 #define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
164 #define PCIE_IRQ_CORE_INT BIT(16)
165 #define PCIE_IRQ_CORE_INT_PIO BIT(17)
166 #define PCIE_IRQ_DPMU_INT BIT(18)
167 #define PCIE_IRQ_PCIE_MIS_INT BIT(19)
168 #define PCIE_IRQ_MSI_INT1_DET BIT(20)
169 #define PCIE_IRQ_MSI_INT2_DET BIT(21)
170 #define PCIE_IRQ_RC_DBELL_DET BIT(22)
171 #define PCIE_IRQ_EP_STATUS BIT(23)
172 #define PCIE_IRQ_ALL_MASK 0xfff0fb
173 #define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
175 /* Transaction types */
176 #define PCIE_CONFIG_RD_TYPE0 0x8
177 #define PCIE_CONFIG_RD_TYPE1 0x9
178 #define PCIE_CONFIG_WR_TYPE0 0xa
179 #define PCIE_CONFIG_WR_TYPE1 0xb
181 #define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
182 #define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
183 #define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
184 #define PCIE_CONF_REG(reg) ((reg) & 0xffc)
185 #define PCIE_CONF_ADDR(bus, devfn, where) \
186 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
187 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
189 #define PIO_RETRY_CNT 750000 /* 1.5 s */
190 #define PIO_RETRY_DELAY 2 /* 2 us*/
192 #define LINK_WAIT_MAX_RETRIES 10
193 #define LINK_WAIT_USLEEP_MIN 90000
194 #define LINK_WAIT_USLEEP_MAX 100000
196 #define MSI_IRQ_NUM 32
199 struct platform_device *pdev;
201 struct list_head resources;
202 struct irq_domain *irq_domain;
203 struct irq_chip irq_chip;
204 raw_spinlock_t irq_lock;
205 struct irq_domain *msi_domain;
206 struct irq_domain *msi_inner_domain;
207 struct irq_chip msi_bottom_irq_chip;
208 struct irq_chip msi_irq_chip;
209 struct msi_domain_info msi_domain_info;
210 DECLARE_BITMAP(msi_used, MSI_IRQ_NUM);
211 struct mutex msi_used_lock;
216 static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
218 writel(val, pcie->base + reg);
221 static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
223 return readl(pcie->base + reg);
226 static int advk_pcie_link_up(struct advk_pcie *pcie)
228 u32 val, ltssm_state;
230 val = advk_readl(pcie, CFG_REG);
231 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
232 return ltssm_state >= LTSSM_L0;
235 static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
237 struct device *dev = &pcie->pdev->dev;
240 /* check if the link is up or not */
241 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
242 if (advk_pcie_link_up(pcie)) {
243 dev_info(dev, "link up\n");
247 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
250 dev_err(dev, "link never came up\n");
255 * Set PCIe address window register which could be used for memory
258 static void advk_pcie_set_ob_win(struct advk_pcie *pcie,
259 u32 win_num, u32 match_ms,
260 u32 match_ls, u32 mask_ms,
261 u32 mask_ls, u32 remap_ms,
262 u32 remap_ls, u32 action)
264 advk_writel(pcie, match_ls, OB_WIN_MATCH_LS(win_num));
265 advk_writel(pcie, match_ms, OB_WIN_MATCH_MS(win_num));
266 advk_writel(pcie, mask_ms, OB_WIN_MASK_MS(win_num));
267 advk_writel(pcie, mask_ls, OB_WIN_MASK_LS(win_num));
268 advk_writel(pcie, remap_ms, OB_WIN_REMAP_MS(win_num));
269 advk_writel(pcie, remap_ls, OB_WIN_REMAP_LS(win_num));
270 advk_writel(pcie, action, OB_WIN_ACTIONS(win_num));
271 advk_writel(pcie, match_ls | BIT(0), OB_WIN_MATCH_LS(win_num));
274 static void advk_pcie_setup_hw(struct advk_pcie *pcie)
279 /* Point PCIe unit MBUS decode windows to DRAM space */
280 for (i = 0; i < 8; i++)
281 advk_pcie_set_ob_win(pcie, i, 0, 0, 0, 0, 0, 0, 0);
283 /* Set to Direct mode */
284 reg = advk_readl(pcie, CTRL_CONFIG_REG);
285 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
286 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
287 advk_writel(pcie, reg, CTRL_CONFIG_REG);
289 /* Set PCI global control register to RC mode */
290 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
291 reg |= (IS_RC_MSK << IS_RC_SHIFT);
292 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
294 /* Set Advanced Error Capabilities and Control PF0 register */
295 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
296 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
297 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
298 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
299 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
301 /* Set PCIe Device Control and Status 1 PF0 register */
302 reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
303 (7 << PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT) |
304 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE |
305 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ <<
306 PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT);
307 advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
309 /* Program PCIe Control 2 to disable strict ordering */
310 reg = PCIE_CORE_CTRL2_RESERVED |
311 PCIE_CORE_CTRL2_TD_ENABLE;
312 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
315 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
316 reg &= ~PCIE_GEN_SEL_MSK;
318 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
321 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
322 reg &= ~LANE_CNT_MSK;
324 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
326 /* Enable link training */
327 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
328 reg |= LINK_TRAINING_EN;
329 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
332 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
333 reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
334 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
336 /* Clear all interrupts */
337 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
338 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
339 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
341 /* Disable All ISR0/1 Sources */
342 reg = PCIE_ISR0_ALL_MASK;
343 reg &= ~PCIE_ISR0_MSI_INT_PENDING;
344 advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
346 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
348 /* Unmask all MSI's */
349 advk_writel(pcie, 0, PCIE_MSI_MASK_REG);
351 /* Enable summary interrupt for GIC SPI source */
352 reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
353 advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
355 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
356 reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
357 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
359 /* Bypass the address window mapping for PIO */
360 reg = advk_readl(pcie, PIO_CTRL);
361 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
362 advk_writel(pcie, reg, PIO_CTRL);
364 /* Start link training */
365 reg = advk_readl(pcie, PCIE_CORE_LINK_CTRL_STAT_REG);
366 reg |= PCIE_CORE_LINK_TRAINING;
367 advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
369 advk_pcie_wait_for_link(pcie);
371 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
372 reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
373 PCIE_CORE_CMD_IO_ACCESS_EN |
374 PCIE_CORE_CMD_MEM_IO_REQ_EN;
375 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
378 static int advk_pcie_check_pio_status(struct advk_pcie *pcie, u32 *val)
380 struct device *dev = &pcie->pdev->dev;
383 char *strcomp_status, *str_posted;
385 reg = advk_readl(pcie, PIO_STAT);
386 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
387 PIO_COMPLETION_STATUS_SHIFT;
390 * According to HW spec, the PIO status check sequence as below:
391 * 1) even if COMPLETION_STATUS(bit9:7) indicates successful,
392 * it still needs to check Error Status(bit11), only when this bit
393 * indicates no error happen, the operation is successful.
394 * 2) value Unsupported Request(1) of COMPLETION_STATUS(bit9:7) only
395 * means a PIO write error, and for PIO read it is successful with
396 * a read value of 0xFFFFFFFF.
397 * 3) value Completion Retry Status(CRS) of COMPLETION_STATUS(bit9:7)
398 * only means a PIO write error, and for PIO read it is successful
399 * with a read value of 0xFFFF0001.
400 * 4) value Completer Abort (CA) of COMPLETION_STATUS(bit9:7) means
401 * error for both PIO read and PIO write operation.
402 * 5) other errors are indicated as 'unknown'.
405 case PIO_COMPLETION_STATUS_OK:
406 if (reg & PIO_ERR_STATUS) {
407 strcomp_status = "COMP_ERR";
410 /* Get the read result */
412 *val = advk_readl(pcie, PIO_RD_DATA);
414 strcomp_status = NULL;
416 case PIO_COMPLETION_STATUS_UR:
417 strcomp_status = "UR";
419 case PIO_COMPLETION_STATUS_CRS:
420 /* PCIe r4.0, sec 2.3.2, says:
421 * If CRS Software Visibility is not enabled, the Root Complex
422 * must re-issue the Configuration Request as a new Request.
423 * A Root Complex implementation may choose to limit the number
424 * of Configuration Request/CRS Completion Status loops before
425 * determining that something is wrong with the target of the
426 * Request and taking appropriate action, e.g., complete the
427 * Request to the host as a failed transaction.
429 * To simplify implementation do not re-issue the Configuration
430 * Request and complete the Request as a failed transaction.
432 strcomp_status = "CRS";
434 case PIO_COMPLETION_STATUS_CA:
435 strcomp_status = "CA";
438 strcomp_status = "Unknown";
445 if (reg & PIO_NON_POSTED_REQ)
446 str_posted = "Non-posted";
448 str_posted = "Posted";
450 dev_err(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
451 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
456 static int advk_pcie_wait_pio(struct advk_pcie *pcie)
458 struct device *dev = &pcie->pdev->dev;
461 for (i = 0; i < PIO_RETRY_CNT; i++) {
464 start = advk_readl(pcie, PIO_START);
465 isr = advk_readl(pcie, PIO_ISR);
468 udelay(PIO_RETRY_DELAY);
471 dev_err(dev, "PIO read/write transfer time out\n");
475 static bool advk_pcie_pio_is_running(struct advk_pcie *pcie)
477 struct device *dev = &pcie->pdev->dev;
480 * Trying to start a new PIO transfer when previous has not completed
481 * cause External Abort on CPU which results in kernel panic:
483 * SError Interrupt on CPU0, code 0xbf000002 -- SError
484 * Kernel panic - not syncing: Asynchronous SError Interrupt
486 * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected
487 * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent
488 * concurrent calls at the same time. But because PIO transfer may take
489 * about 1.5s when link is down or card is disconnected, it means that
490 * advk_pcie_wait_pio() does not always have to wait for completion.
492 * Some versions of ARM Trusted Firmware handles this External Abort at
493 * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit:
494 * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50
496 if (advk_readl(pcie, PIO_START)) {
497 dev_err(dev, "Previous PIO read/write transfer is still running\n");
504 static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
505 int where, int size, u32 *val)
507 struct advk_pcie *pcie = bus->sysdata;
511 if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0) {
513 return PCIBIOS_DEVICE_NOT_FOUND;
516 if (advk_pcie_pio_is_running(pcie)) {
518 return PCIBIOS_SET_FAILED;
521 /* Program the control register */
522 reg = advk_readl(pcie, PIO_CTRL);
523 reg &= ~PIO_CTRL_TYPE_MASK;
524 if (bus->number == pcie->root_bus_nr)
525 reg |= PCIE_CONFIG_RD_TYPE0;
527 reg |= PCIE_CONFIG_RD_TYPE1;
528 advk_writel(pcie, reg, PIO_CTRL);
530 /* Program the address registers */
531 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
532 advk_writel(pcie, reg, PIO_ADDR_LS);
533 advk_writel(pcie, 0, PIO_ADDR_MS);
535 /* Program the data strobe */
536 advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
538 /* Clear PIO DONE ISR and start the transfer */
539 advk_writel(pcie, 1, PIO_ISR);
540 advk_writel(pcie, 1, PIO_START);
542 ret = advk_pcie_wait_pio(pcie);
544 return PCIBIOS_SET_FAILED;
546 /* Check PIO status and get the read result */
547 ret = advk_pcie_check_pio_status(pcie, val);
550 return PCIBIOS_SET_FAILED;
554 *val = (*val >> (8 * (where & 3))) & 0xff;
556 *val = (*val >> (8 * (where & 3))) & 0xffff;
558 return PCIBIOS_SUCCESSFUL;
561 static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
562 int where, int size, u32 val)
564 struct advk_pcie *pcie = bus->sysdata;
566 u32 data_strobe = 0x0;
570 if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0)
571 return PCIBIOS_DEVICE_NOT_FOUND;
574 return PCIBIOS_SET_FAILED;
576 if (advk_pcie_pio_is_running(pcie))
577 return PCIBIOS_SET_FAILED;
579 /* Program the control register */
580 reg = advk_readl(pcie, PIO_CTRL);
581 reg &= ~PIO_CTRL_TYPE_MASK;
582 if (bus->number == pcie->root_bus_nr)
583 reg |= PCIE_CONFIG_WR_TYPE0;
585 reg |= PCIE_CONFIG_WR_TYPE1;
586 advk_writel(pcie, reg, PIO_CTRL);
588 /* Program the address registers */
589 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
590 advk_writel(pcie, reg, PIO_ADDR_LS);
591 advk_writel(pcie, 0, PIO_ADDR_MS);
593 /* Calculate the write strobe */
594 offset = where & 0x3;
595 reg = val << (8 * offset);
596 data_strobe = GENMASK(size - 1, 0) << offset;
598 /* Program the data register */
599 advk_writel(pcie, reg, PIO_WR_DATA);
601 /* Program the data strobe */
602 advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
604 /* Clear PIO DONE ISR and start the transfer */
605 advk_writel(pcie, 1, PIO_ISR);
606 advk_writel(pcie, 1, PIO_START);
608 ret = advk_pcie_wait_pio(pcie);
610 return PCIBIOS_SET_FAILED;
612 ret = advk_pcie_check_pio_status(pcie, NULL);
614 return PCIBIOS_SET_FAILED;
616 return PCIBIOS_SUCCESSFUL;
619 static struct pci_ops advk_pcie_ops = {
620 .read = advk_pcie_rd_conf,
621 .write = advk_pcie_wr_conf,
624 static void advk_msi_irq_compose_msi_msg(struct irq_data *data,
627 struct advk_pcie *pcie = irq_data_get_irq_chip_data(data);
628 phys_addr_t msi_msg = virt_to_phys(&pcie->msi_msg);
630 msg->address_lo = lower_32_bits(msi_msg);
631 msg->address_hi = upper_32_bits(msi_msg);
632 msg->data = data->irq;
635 static int advk_msi_set_affinity(struct irq_data *irq_data,
636 const struct cpumask *mask, bool force)
641 static int advk_msi_irq_domain_alloc(struct irq_domain *domain,
643 unsigned int nr_irqs, void *args)
645 struct advk_pcie *pcie = domain->host_data;
648 mutex_lock(&pcie->msi_used_lock);
649 hwirq = bitmap_find_next_zero_area(pcie->msi_used, MSI_IRQ_NUM,
651 if (hwirq >= MSI_IRQ_NUM) {
652 mutex_unlock(&pcie->msi_used_lock);
656 bitmap_set(pcie->msi_used, hwirq, nr_irqs);
657 mutex_unlock(&pcie->msi_used_lock);
659 for (i = 0; i < nr_irqs; i++)
660 irq_domain_set_info(domain, virq + i, hwirq + i,
661 &pcie->msi_bottom_irq_chip,
662 domain->host_data, handle_simple_irq,
668 static void advk_msi_irq_domain_free(struct irq_domain *domain,
669 unsigned int virq, unsigned int nr_irqs)
671 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
672 struct advk_pcie *pcie = domain->host_data;
674 mutex_lock(&pcie->msi_used_lock);
675 bitmap_clear(pcie->msi_used, d->hwirq, nr_irqs);
676 mutex_unlock(&pcie->msi_used_lock);
679 static const struct irq_domain_ops advk_msi_domain_ops = {
680 .alloc = advk_msi_irq_domain_alloc,
681 .free = advk_msi_irq_domain_free,
684 static void advk_pcie_irq_mask(struct irq_data *d)
686 struct advk_pcie *pcie = d->domain->host_data;
687 irq_hw_number_t hwirq = irqd_to_hwirq(d);
691 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
692 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
693 mask |= PCIE_ISR1_INTX_ASSERT(hwirq);
694 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
695 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
698 static void advk_pcie_irq_unmask(struct irq_data *d)
700 struct advk_pcie *pcie = d->domain->host_data;
701 irq_hw_number_t hwirq = irqd_to_hwirq(d);
705 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
706 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
707 mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq);
708 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
709 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
712 static int advk_pcie_irq_map(struct irq_domain *h,
713 unsigned int virq, irq_hw_number_t hwirq)
715 struct advk_pcie *pcie = h->host_data;
717 advk_pcie_irq_mask(irq_get_irq_data(virq));
718 irq_set_status_flags(virq, IRQ_LEVEL);
719 irq_set_chip_and_handler(virq, &pcie->irq_chip,
721 irq_set_chip_data(virq, pcie);
726 static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
727 .map = advk_pcie_irq_map,
728 .xlate = irq_domain_xlate_onecell,
731 static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
733 struct device *dev = &pcie->pdev->dev;
734 struct device_node *node = dev->of_node;
735 struct irq_chip *bottom_ic, *msi_ic;
736 struct msi_domain_info *msi_di;
737 phys_addr_t msi_msg_phys;
739 mutex_init(&pcie->msi_used_lock);
741 bottom_ic = &pcie->msi_bottom_irq_chip;
743 bottom_ic->name = "MSI";
744 bottom_ic->irq_compose_msi_msg = advk_msi_irq_compose_msi_msg;
745 bottom_ic->irq_set_affinity = advk_msi_set_affinity;
747 msi_ic = &pcie->msi_irq_chip;
748 msi_ic->name = "advk-MSI";
750 msi_di = &pcie->msi_domain_info;
751 msi_di->flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
752 MSI_FLAG_MULTI_PCI_MSI;
753 msi_di->chip = msi_ic;
755 msi_msg_phys = virt_to_phys(&pcie->msi_msg);
757 advk_writel(pcie, lower_32_bits(msi_msg_phys),
758 PCIE_MSI_ADDR_LOW_REG);
759 advk_writel(pcie, upper_32_bits(msi_msg_phys),
760 PCIE_MSI_ADDR_HIGH_REG);
762 pcie->msi_inner_domain =
763 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
764 &advk_msi_domain_ops, pcie);
765 if (!pcie->msi_inner_domain)
769 pci_msi_create_irq_domain(of_node_to_fwnode(node),
770 msi_di, pcie->msi_inner_domain);
771 if (!pcie->msi_domain) {
772 irq_domain_remove(pcie->msi_inner_domain);
779 static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
781 irq_domain_remove(pcie->msi_domain);
782 irq_domain_remove(pcie->msi_inner_domain);
785 static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
787 struct device *dev = &pcie->pdev->dev;
788 struct device_node *node = dev->of_node;
789 struct device_node *pcie_intc_node;
790 struct irq_chip *irq_chip;
792 raw_spin_lock_init(&pcie->irq_lock);
794 pcie_intc_node = of_get_next_child(node, NULL);
795 if (!pcie_intc_node) {
796 dev_err(dev, "No PCIe Intc node found\n");
800 irq_chip = &pcie->irq_chip;
802 irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
804 if (!irq_chip->name) {
805 of_node_put(pcie_intc_node);
809 irq_chip->irq_mask = advk_pcie_irq_mask;
810 irq_chip->irq_mask_ack = advk_pcie_irq_mask;
811 irq_chip->irq_unmask = advk_pcie_irq_unmask;
814 irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
815 &advk_pcie_irq_domain_ops, pcie);
816 if (!pcie->irq_domain) {
817 dev_err(dev, "Failed to get a INTx IRQ domain\n");
818 of_node_put(pcie_intc_node);
825 static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
827 irq_domain_remove(pcie->irq_domain);
830 static void advk_pcie_handle_msi(struct advk_pcie *pcie)
832 u32 msi_val, msi_mask, msi_status, msi_idx;
835 msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
836 msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
837 msi_status = msi_val & ~msi_mask;
839 for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
840 if (!(BIT(msi_idx) & msi_status))
843 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
844 msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & 0xFF;
845 generic_handle_irq(msi_data);
848 advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
852 static void advk_pcie_handle_int(struct advk_pcie *pcie)
854 u32 isr0_val, isr0_mask, isr0_status;
855 u32 isr1_val, isr1_mask, isr1_status;
858 isr0_val = advk_readl(pcie, PCIE_ISR0_REG);
859 isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
860 isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK);
862 isr1_val = advk_readl(pcie, PCIE_ISR1_REG);
863 isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
864 isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK);
866 if (!isr0_status && !isr1_status) {
867 advk_writel(pcie, isr0_val, PCIE_ISR0_REG);
868 advk_writel(pcie, isr1_val, PCIE_ISR1_REG);
872 /* Process MSI interrupts */
873 if (isr0_status & PCIE_ISR0_MSI_INT_PENDING)
874 advk_pcie_handle_msi(pcie);
876 /* Process legacy interrupts */
877 for (i = 0; i < PCI_NUM_INTX; i++) {
878 if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i)))
881 advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i),
884 virq = irq_find_mapping(pcie->irq_domain, i);
885 generic_handle_irq(virq);
889 static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
891 struct advk_pcie *pcie = arg;
894 status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
895 if (!(status & PCIE_IRQ_CORE_INT))
898 advk_pcie_handle_int(pcie);
900 /* Clear interrupt */
901 advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
906 static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie *pcie)
908 int err, res_valid = 0;
909 struct device *dev = &pcie->pdev->dev;
910 struct device_node *np = dev->of_node;
911 struct resource_entry *win, *tmp;
912 resource_size_t iobase;
914 INIT_LIST_HEAD(&pcie->resources);
916 err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pcie->resources,
921 err = devm_request_pci_bus_resources(dev, &pcie->resources);
923 goto out_release_res;
925 resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
926 struct resource *res = win->res;
928 switch (resource_type(res)) {
930 advk_pcie_set_ob_win(pcie, 1,
931 upper_32_bits(res->start),
932 lower_32_bits(res->start),
934 lower_32_bits(res->start),
936 err = pci_remap_iospace(res, iobase);
938 dev_warn(dev, "error %d: failed to map resource %pR\n",
940 resource_list_destroy_entry(win);
944 advk_pcie_set_ob_win(pcie, 0,
945 upper_32_bits(res->start),
946 lower_32_bits(res->start),
948 lower_32_bits(res->start),
949 (2 << 20) | OB_PCIE_MEM);
950 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
953 pcie->root_bus_nr = res->start;
959 dev_err(dev, "non-prefetchable memory resource required\n");
961 goto out_release_res;
967 pci_free_resource_list(&pcie->resources);
971 static int advk_pcie_probe(struct platform_device *pdev)
973 struct device *dev = &pdev->dev;
974 struct advk_pcie *pcie;
975 struct resource *res;
976 struct pci_bus *bus, *child;
977 struct pci_host_bridge *bridge;
980 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie));
984 pcie = pci_host_bridge_priv(bridge);
987 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
988 pcie->base = devm_ioremap_resource(dev, res);
989 if (IS_ERR(pcie->base))
990 return PTR_ERR(pcie->base);
992 irq = platform_get_irq(pdev, 0);
993 ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
994 IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
997 dev_err(dev, "Failed to register interrupt\n");
1001 ret = advk_pcie_parse_request_of_pci_ranges(pcie);
1003 dev_err(dev, "Failed to parse resources\n");
1007 advk_pcie_setup_hw(pcie);
1009 ret = advk_pcie_init_irq_domain(pcie);
1011 dev_err(dev, "Failed to initialize irq\n");
1015 ret = advk_pcie_init_msi_irq_domain(pcie);
1017 dev_err(dev, "Failed to initialize irq\n");
1018 advk_pcie_remove_irq_domain(pcie);
1022 list_splice_init(&pcie->resources, &bridge->windows);
1023 bridge->dev.parent = dev;
1024 bridge->sysdata = pcie;
1026 bridge->ops = &advk_pcie_ops;
1027 bridge->map_irq = of_irq_parse_and_map_pci;
1028 bridge->swizzle_irq = pci_common_swizzle;
1030 ret = pci_scan_root_bus_bridge(bridge);
1032 advk_pcie_remove_msi_irq_domain(pcie);
1033 advk_pcie_remove_irq_domain(pcie);
1039 pci_bus_size_bridges(bus);
1040 pci_bus_assign_resources(bus);
1042 list_for_each_entry(child, &bus->children, node)
1043 pcie_bus_configure_settings(child);
1045 pci_bus_add_devices(bus);
1049 static const struct of_device_id advk_pcie_of_match_table[] = {
1050 { .compatible = "marvell,armada-3700-pcie", },
1054 static struct platform_driver advk_pcie_driver = {
1056 .name = "advk-pcie",
1057 .of_match_table = advk_pcie_of_match_table,
1058 /* Driver unloading/unbinding currently not supported */
1059 .suppress_bind_attrs = true,
1061 .probe = advk_pcie_probe,
1063 builtin_platform_driver(advk_pcie_driver);