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(0)
58 #define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
59 #define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
60 #define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
61 #define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
62 #define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
63 #define PIO_START (PIO_BASE_ADDR + 0x1c)
64 #define PIO_ISR (PIO_BASE_ADDR + 0x20)
65 #define PIO_ISRM (PIO_BASE_ADDR + 0x24)
67 /* Aardvark Control registers */
68 #define CONTROL_BASE_ADDR 0x4800
69 #define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
70 #define PCIE_GEN_SEL_MSK 0x3
71 #define PCIE_GEN_SEL_SHIFT 0x0
77 #define LANE_CNT_MSK 0x18
78 #define LANE_CNT_SHIFT 0x3
79 #define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
80 #define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
81 #define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
82 #define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
83 #define LINK_TRAINING_EN BIT(6)
84 #define LEGACY_INTA BIT(28)
85 #define LEGACY_INTB BIT(29)
86 #define LEGACY_INTC BIT(30)
87 #define LEGACY_INTD BIT(31)
88 #define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4)
89 #define HOT_RESET_GEN BIT(0)
90 #define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
91 #define PCIE_CORE_CTRL2_RESERVED 0x7
92 #define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
93 #define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
94 #define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6)
95 #define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10)
96 #define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
97 #define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44)
98 #define PCIE_ISR0_MSI_INT_PENDING BIT(24)
99 #define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val))
100 #define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val))
101 #define PCIE_ISR0_ALL_MASK GENMASK(26, 0)
102 #define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48)
103 #define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C)
104 #define PCIE_ISR1_POWER_STATE_CHANGE BIT(4)
105 #define PCIE_ISR1_FLUSH BIT(5)
106 #define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val))
107 #define PCIE_ISR1_ALL_MASK GENMASK(11, 4)
108 #define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50)
109 #define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54)
110 #define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58)
111 #define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C)
112 #define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C)
114 /* LMI registers base address and register offsets */
115 #define LMI_BASE_ADDR 0x6000
116 #define CFG_REG (LMI_BASE_ADDR + 0x0)
117 #define LTSSM_SHIFT 24
118 #define LTSSM_MASK 0x3f
119 #define LTSSM_L0 0x10
120 #define RC_BAR_CONFIG 0x300
122 /* PCIe core controller registers */
123 #define CTRL_CORE_BASE_ADDR 0x18000
124 #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
125 #define CTRL_MODE_SHIFT 0x0
126 #define CTRL_MODE_MASK 0x1
127 #define PCIE_CORE_MODE_DIRECT 0x0
128 #define PCIE_CORE_MODE_COMMAND 0x1
130 /* PCIe Central Interrupts Registers */
131 #define CENTRAL_INT_BASE_ADDR 0x1b000
132 #define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
133 #define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
134 #define PCIE_IRQ_CMDQ_INT BIT(0)
135 #define PCIE_IRQ_MSI_STATUS_INT BIT(1)
136 #define PCIE_IRQ_CMD_SENT_DONE BIT(3)
137 #define PCIE_IRQ_DMA_INT BIT(4)
138 #define PCIE_IRQ_IB_DXFERDONE BIT(5)
139 #define PCIE_IRQ_OB_DXFERDONE BIT(6)
140 #define PCIE_IRQ_OB_RXFERDONE BIT(7)
141 #define PCIE_IRQ_COMPQ_INT BIT(12)
142 #define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
143 #define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
144 #define PCIE_IRQ_CORE_INT BIT(16)
145 #define PCIE_IRQ_CORE_INT_PIO BIT(17)
146 #define PCIE_IRQ_DPMU_INT BIT(18)
147 #define PCIE_IRQ_PCIE_MIS_INT BIT(19)
148 #define PCIE_IRQ_MSI_INT1_DET BIT(20)
149 #define PCIE_IRQ_MSI_INT2_DET BIT(21)
150 #define PCIE_IRQ_RC_DBELL_DET BIT(22)
151 #define PCIE_IRQ_EP_STATUS BIT(23)
152 #define PCIE_IRQ_ALL_MASK 0xfff0fb
153 #define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
155 /* Transaction types */
156 #define PCIE_CONFIG_RD_TYPE0 0x8
157 #define PCIE_CONFIG_RD_TYPE1 0x9
158 #define PCIE_CONFIG_WR_TYPE0 0xa
159 #define PCIE_CONFIG_WR_TYPE1 0xb
161 #define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
162 #define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
163 #define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
164 #define PCIE_CONF_REG(reg) ((reg) & 0xffc)
165 #define PCIE_CONF_ADDR(bus, devfn, where) \
166 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
167 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
169 #define PIO_TIMEOUT_MS 1
171 #define LINK_WAIT_MAX_RETRIES 10
172 #define LINK_WAIT_USLEEP_MIN 90000
173 #define LINK_WAIT_USLEEP_MAX 100000
175 #define MSI_IRQ_NUM 32
178 struct platform_device
*pdev
;
180 struct list_head resources
;
181 struct irq_domain
*irq_domain
;
182 struct irq_chip irq_chip
;
183 struct irq_domain
*msi_domain
;
184 struct irq_domain
*msi_inner_domain
;
185 struct irq_chip msi_bottom_irq_chip
;
186 struct irq_chip msi_irq_chip
;
187 struct msi_domain_info msi_domain_info
;
188 DECLARE_BITMAP(msi_used
, MSI_IRQ_NUM
);
189 struct mutex msi_used_lock
;
194 static inline void advk_writel(struct advk_pcie
*pcie
, u32 val
, u64 reg
)
196 writel(val
, pcie
->base
+ reg
);
199 static inline u32
advk_readl(struct advk_pcie
*pcie
, u64 reg
)
201 return readl(pcie
->base
+ reg
);
204 static int advk_pcie_link_up(struct advk_pcie
*pcie
)
206 u32 val
, ltssm_state
;
208 val
= advk_readl(pcie
, CFG_REG
);
209 ltssm_state
= (val
>> LTSSM_SHIFT
) & LTSSM_MASK
;
210 return ltssm_state
>= LTSSM_L0
;
213 static int advk_pcie_wait_for_link(struct advk_pcie
*pcie
)
215 struct device
*dev
= &pcie
->pdev
->dev
;
218 /* check if the link is up or not */
219 for (retries
= 0; retries
< LINK_WAIT_MAX_RETRIES
; retries
++) {
220 if (advk_pcie_link_up(pcie
)) {
221 dev_info(dev
, "link up\n");
225 usleep_range(LINK_WAIT_USLEEP_MIN
, LINK_WAIT_USLEEP_MAX
);
228 dev_err(dev
, "link never came up\n");
232 static void advk_pcie_setup_hw(struct advk_pcie
*pcie
)
236 /* Set to Direct mode */
237 reg
= advk_readl(pcie
, CTRL_CONFIG_REG
);
238 reg
&= ~(CTRL_MODE_MASK
<< CTRL_MODE_SHIFT
);
239 reg
|= ((PCIE_CORE_MODE_DIRECT
& CTRL_MODE_MASK
) << CTRL_MODE_SHIFT
);
240 advk_writel(pcie
, reg
, CTRL_CONFIG_REG
);
242 /* Set PCI global control register to RC mode */
243 reg
= advk_readl(pcie
, PCIE_CORE_CTRL0_REG
);
244 reg
|= (IS_RC_MSK
<< IS_RC_SHIFT
);
245 advk_writel(pcie
, reg
, PCIE_CORE_CTRL0_REG
);
247 /* Set Advanced Error Capabilities and Control PF0 register */
248 reg
= PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX
|
249 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN
|
250 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK
|
251 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV
;
252 advk_writel(pcie
, reg
, PCIE_CORE_ERR_CAPCTL_REG
);
254 /* Set PCIe Device Control and Status 1 PF0 register */
255 reg
= PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE
|
256 (7 << PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT
) |
257 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE
|
258 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ
<<
259 PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT
);
260 advk_writel(pcie
, reg
, PCIE_CORE_DEV_CTRL_STATS_REG
);
262 /* Program PCIe Control 2 to disable strict ordering */
263 reg
= PCIE_CORE_CTRL2_RESERVED
|
264 PCIE_CORE_CTRL2_TD_ENABLE
;
265 advk_writel(pcie
, reg
, PCIE_CORE_CTRL2_REG
);
268 reg
= advk_readl(pcie
, PCIE_CORE_CTRL0_REG
);
269 reg
&= ~PCIE_GEN_SEL_MSK
;
271 advk_writel(pcie
, reg
, PCIE_CORE_CTRL0_REG
);
274 reg
= advk_readl(pcie
, PCIE_CORE_CTRL0_REG
);
275 reg
&= ~LANE_CNT_MSK
;
277 advk_writel(pcie
, reg
, PCIE_CORE_CTRL0_REG
);
279 /* Enable link training */
280 reg
= advk_readl(pcie
, PCIE_CORE_CTRL0_REG
);
281 reg
|= LINK_TRAINING_EN
;
282 advk_writel(pcie
, reg
, PCIE_CORE_CTRL0_REG
);
285 reg
= advk_readl(pcie
, PCIE_CORE_CTRL2_REG
);
286 reg
|= PCIE_CORE_CTRL2_MSI_ENABLE
;
287 advk_writel(pcie
, reg
, PCIE_CORE_CTRL2_REG
);
289 /* Clear all interrupts */
290 advk_writel(pcie
, PCIE_ISR0_ALL_MASK
, PCIE_ISR0_REG
);
291 advk_writel(pcie
, PCIE_ISR1_ALL_MASK
, PCIE_ISR1_REG
);
292 advk_writel(pcie
, PCIE_IRQ_ALL_MASK
, HOST_CTRL_INT_STATUS_REG
);
294 /* Disable All ISR0/1 Sources */
295 reg
= PCIE_ISR0_ALL_MASK
;
296 reg
&= ~PCIE_ISR0_MSI_INT_PENDING
;
297 advk_writel(pcie
, reg
, PCIE_ISR0_MASK_REG
);
299 advk_writel(pcie
, PCIE_ISR1_ALL_MASK
, PCIE_ISR1_MASK_REG
);
301 /* Unmask all MSI's */
302 advk_writel(pcie
, 0, PCIE_MSI_MASK_REG
);
304 /* Enable summary interrupt for GIC SPI source */
305 reg
= PCIE_IRQ_ALL_MASK
& (~PCIE_IRQ_ENABLE_INTS_MASK
);
306 advk_writel(pcie
, reg
, HOST_CTRL_INT_MASK_REG
);
308 reg
= advk_readl(pcie
, PCIE_CORE_CTRL2_REG
);
309 reg
|= PCIE_CORE_CTRL2_OB_WIN_ENABLE
;
310 advk_writel(pcie
, reg
, PCIE_CORE_CTRL2_REG
);
312 /* Bypass the address window mapping for PIO */
313 reg
= advk_readl(pcie
, PIO_CTRL
);
314 reg
|= PIO_CTRL_ADDR_WIN_DISABLE
;
315 advk_writel(pcie
, reg
, PIO_CTRL
);
317 /* Start link training */
318 reg
= advk_readl(pcie
, PCIE_CORE_LINK_CTRL_STAT_REG
);
319 reg
|= PCIE_CORE_LINK_TRAINING
;
320 advk_writel(pcie
, reg
, PCIE_CORE_LINK_CTRL_STAT_REG
);
322 advk_pcie_wait_for_link(pcie
);
324 reg
= advk_readl(pcie
, PCIE_CORE_CMD_STATUS_REG
);
325 reg
|= PCIE_CORE_CMD_MEM_ACCESS_EN
|
326 PCIE_CORE_CMD_IO_ACCESS_EN
|
327 PCIE_CORE_CMD_MEM_IO_REQ_EN
;
328 advk_writel(pcie
, reg
, PCIE_CORE_CMD_STATUS_REG
);
331 static void advk_pcie_check_pio_status(struct advk_pcie
*pcie
)
333 struct device
*dev
= &pcie
->pdev
->dev
;
336 char *strcomp_status
, *str_posted
;
338 reg
= advk_readl(pcie
, PIO_STAT
);
339 status
= (reg
& PIO_COMPLETION_STATUS_MASK
) >>
340 PIO_COMPLETION_STATUS_SHIFT
;
346 case PIO_COMPLETION_STATUS_UR
:
347 strcomp_status
= "UR";
349 case PIO_COMPLETION_STATUS_CRS
:
350 strcomp_status
= "CRS";
352 case PIO_COMPLETION_STATUS_CA
:
353 strcomp_status
= "CA";
356 strcomp_status
= "Unknown";
360 if (reg
& PIO_NON_POSTED_REQ
)
361 str_posted
= "Non-posted";
363 str_posted
= "Posted";
365 dev_err(dev
, "%s PIO Response Status: %s, %#x @ %#x\n",
366 str_posted
, strcomp_status
, reg
, advk_readl(pcie
, PIO_ADDR_LS
));
369 static int advk_pcie_wait_pio(struct advk_pcie
*pcie
)
371 struct device
*dev
= &pcie
->pdev
->dev
;
372 unsigned long timeout
;
374 timeout
= jiffies
+ msecs_to_jiffies(PIO_TIMEOUT_MS
);
376 while (time_before(jiffies
, timeout
)) {
379 start
= advk_readl(pcie
, PIO_START
);
380 isr
= advk_readl(pcie
, PIO_ISR
);
385 dev_err(dev
, "config read/write timed out\n");
389 static bool advk_pcie_valid_device(struct advk_pcie
*pcie
, struct pci_bus
*bus
,
392 if ((bus
->number
== pcie
->root_bus_nr
) && PCI_SLOT(devfn
) != 0)
398 static int advk_pcie_rd_conf(struct pci_bus
*bus
, u32 devfn
,
399 int where
, int size
, u32
*val
)
401 struct advk_pcie
*pcie
= bus
->sysdata
;
405 if (!advk_pcie_valid_device(pcie
, bus
, devfn
)) {
407 return PCIBIOS_DEVICE_NOT_FOUND
;
411 advk_writel(pcie
, 0, PIO_START
);
412 advk_writel(pcie
, 1, PIO_ISR
);
414 /* Program the control register */
415 reg
= advk_readl(pcie
, PIO_CTRL
);
416 reg
&= ~PIO_CTRL_TYPE_MASK
;
417 if (bus
->number
== pcie
->root_bus_nr
)
418 reg
|= PCIE_CONFIG_RD_TYPE0
;
420 reg
|= PCIE_CONFIG_RD_TYPE1
;
421 advk_writel(pcie
, reg
, PIO_CTRL
);
423 /* Program the address registers */
424 reg
= PCIE_CONF_ADDR(bus
->number
, devfn
, where
);
425 advk_writel(pcie
, reg
, PIO_ADDR_LS
);
426 advk_writel(pcie
, 0, PIO_ADDR_MS
);
428 /* Program the data strobe */
429 advk_writel(pcie
, 0xf, PIO_WR_DATA_STRB
);
431 /* Start the transfer */
432 advk_writel(pcie
, 1, PIO_START
);
434 ret
= advk_pcie_wait_pio(pcie
);
436 return PCIBIOS_SET_FAILED
;
438 advk_pcie_check_pio_status(pcie
);
440 /* Get the read result */
441 *val
= advk_readl(pcie
, PIO_RD_DATA
);
443 *val
= (*val
>> (8 * (where
& 3))) & 0xff;
445 *val
= (*val
>> (8 * (where
& 3))) & 0xffff;
447 return PCIBIOS_SUCCESSFUL
;
450 static int advk_pcie_wr_conf(struct pci_bus
*bus
, u32 devfn
,
451 int where
, int size
, u32 val
)
453 struct advk_pcie
*pcie
= bus
->sysdata
;
455 u32 data_strobe
= 0x0;
459 if (!advk_pcie_valid_device(pcie
, bus
, devfn
))
460 return PCIBIOS_DEVICE_NOT_FOUND
;
463 return PCIBIOS_SET_FAILED
;
466 advk_writel(pcie
, 0, PIO_START
);
467 advk_writel(pcie
, 1, PIO_ISR
);
469 /* Program the control register */
470 reg
= advk_readl(pcie
, PIO_CTRL
);
471 reg
&= ~PIO_CTRL_TYPE_MASK
;
472 if (bus
->number
== pcie
->root_bus_nr
)
473 reg
|= PCIE_CONFIG_WR_TYPE0
;
475 reg
|= PCIE_CONFIG_WR_TYPE1
;
476 advk_writel(pcie
, reg
, PIO_CTRL
);
478 /* Program the address registers */
479 reg
= PCIE_CONF_ADDR(bus
->number
, devfn
, where
);
480 advk_writel(pcie
, reg
, PIO_ADDR_LS
);
481 advk_writel(pcie
, 0, PIO_ADDR_MS
);
483 /* Calculate the write strobe */
484 offset
= where
& 0x3;
485 reg
= val
<< (8 * offset
);
486 data_strobe
= GENMASK(size
- 1, 0) << offset
;
488 /* Program the data register */
489 advk_writel(pcie
, reg
, PIO_WR_DATA
);
491 /* Program the data strobe */
492 advk_writel(pcie
, data_strobe
, PIO_WR_DATA_STRB
);
494 /* Start the transfer */
495 advk_writel(pcie
, 1, PIO_START
);
497 ret
= advk_pcie_wait_pio(pcie
);
499 return PCIBIOS_SET_FAILED
;
501 advk_pcie_check_pio_status(pcie
);
503 return PCIBIOS_SUCCESSFUL
;
506 static struct pci_ops advk_pcie_ops
= {
507 .read
= advk_pcie_rd_conf
,
508 .write
= advk_pcie_wr_conf
,
511 static void advk_msi_irq_compose_msi_msg(struct irq_data
*data
,
514 struct advk_pcie
*pcie
= irq_data_get_irq_chip_data(data
);
515 phys_addr_t msi_msg
= virt_to_phys(&pcie
->msi_msg
);
517 msg
->address_lo
= lower_32_bits(msi_msg
);
518 msg
->address_hi
= upper_32_bits(msi_msg
);
519 msg
->data
= data
->irq
;
522 static int advk_msi_set_affinity(struct irq_data
*irq_data
,
523 const struct cpumask
*mask
, bool force
)
528 static int advk_msi_irq_domain_alloc(struct irq_domain
*domain
,
530 unsigned int nr_irqs
, void *args
)
532 struct advk_pcie
*pcie
= domain
->host_data
;
535 mutex_lock(&pcie
->msi_used_lock
);
536 hwirq
= bitmap_find_next_zero_area(pcie
->msi_used
, MSI_IRQ_NUM
,
538 if (hwirq
>= MSI_IRQ_NUM
) {
539 mutex_unlock(&pcie
->msi_used_lock
);
543 bitmap_set(pcie
->msi_used
, hwirq
, nr_irqs
);
544 mutex_unlock(&pcie
->msi_used_lock
);
546 for (i
= 0; i
< nr_irqs
; i
++)
547 irq_domain_set_info(domain
, virq
+ i
, hwirq
+ i
,
548 &pcie
->msi_bottom_irq_chip
,
549 domain
->host_data
, handle_simple_irq
,
555 static void advk_msi_irq_domain_free(struct irq_domain
*domain
,
556 unsigned int virq
, unsigned int nr_irqs
)
558 struct irq_data
*d
= irq_domain_get_irq_data(domain
, virq
);
559 struct advk_pcie
*pcie
= domain
->host_data
;
561 mutex_lock(&pcie
->msi_used_lock
);
562 bitmap_clear(pcie
->msi_used
, d
->hwirq
, nr_irqs
);
563 mutex_unlock(&pcie
->msi_used_lock
);
566 static const struct irq_domain_ops advk_msi_domain_ops
= {
567 .alloc
= advk_msi_irq_domain_alloc
,
568 .free
= advk_msi_irq_domain_free
,
571 static void advk_pcie_irq_mask(struct irq_data
*d
)
573 struct advk_pcie
*pcie
= d
->domain
->host_data
;
574 irq_hw_number_t hwirq
= irqd_to_hwirq(d
);
577 mask
= advk_readl(pcie
, PCIE_ISR1_MASK_REG
);
578 mask
|= PCIE_ISR1_INTX_ASSERT(hwirq
);
579 advk_writel(pcie
, mask
, PCIE_ISR1_MASK_REG
);
582 static void advk_pcie_irq_unmask(struct irq_data
*d
)
584 struct advk_pcie
*pcie
= d
->domain
->host_data
;
585 irq_hw_number_t hwirq
= irqd_to_hwirq(d
);
588 mask
= advk_readl(pcie
, PCIE_ISR1_MASK_REG
);
589 mask
&= ~PCIE_ISR1_INTX_ASSERT(hwirq
);
590 advk_writel(pcie
, mask
, PCIE_ISR1_MASK_REG
);
593 static int advk_pcie_irq_map(struct irq_domain
*h
,
594 unsigned int virq
, irq_hw_number_t hwirq
)
596 struct advk_pcie
*pcie
= h
->host_data
;
598 advk_pcie_irq_mask(irq_get_irq_data(virq
));
599 irq_set_status_flags(virq
, IRQ_LEVEL
);
600 irq_set_chip_and_handler(virq
, &pcie
->irq_chip
,
602 irq_set_chip_data(virq
, pcie
);
607 static const struct irq_domain_ops advk_pcie_irq_domain_ops
= {
608 .map
= advk_pcie_irq_map
,
609 .xlate
= irq_domain_xlate_onecell
,
612 static int advk_pcie_init_msi_irq_domain(struct advk_pcie
*pcie
)
614 struct device
*dev
= &pcie
->pdev
->dev
;
615 struct device_node
*node
= dev
->of_node
;
616 struct irq_chip
*bottom_ic
, *msi_ic
;
617 struct msi_domain_info
*msi_di
;
618 phys_addr_t msi_msg_phys
;
620 mutex_init(&pcie
->msi_used_lock
);
622 bottom_ic
= &pcie
->msi_bottom_irq_chip
;
624 bottom_ic
->name
= "MSI";
625 bottom_ic
->irq_compose_msi_msg
= advk_msi_irq_compose_msi_msg
;
626 bottom_ic
->irq_set_affinity
= advk_msi_set_affinity
;
628 msi_ic
= &pcie
->msi_irq_chip
;
629 msi_ic
->name
= "advk-MSI";
631 msi_di
= &pcie
->msi_domain_info
;
632 msi_di
->flags
= MSI_FLAG_USE_DEF_DOM_OPS
| MSI_FLAG_USE_DEF_CHIP_OPS
|
633 MSI_FLAG_MULTI_PCI_MSI
;
634 msi_di
->chip
= msi_ic
;
636 msi_msg_phys
= virt_to_phys(&pcie
->msi_msg
);
638 advk_writel(pcie
, lower_32_bits(msi_msg_phys
),
639 PCIE_MSI_ADDR_LOW_REG
);
640 advk_writel(pcie
, upper_32_bits(msi_msg_phys
),
641 PCIE_MSI_ADDR_HIGH_REG
);
643 pcie
->msi_inner_domain
=
644 irq_domain_add_linear(NULL
, MSI_IRQ_NUM
,
645 &advk_msi_domain_ops
, pcie
);
646 if (!pcie
->msi_inner_domain
)
650 pci_msi_create_irq_domain(of_node_to_fwnode(node
),
651 msi_di
, pcie
->msi_inner_domain
);
652 if (!pcie
->msi_domain
) {
653 irq_domain_remove(pcie
->msi_inner_domain
);
660 static void advk_pcie_remove_msi_irq_domain(struct advk_pcie
*pcie
)
662 irq_domain_remove(pcie
->msi_domain
);
663 irq_domain_remove(pcie
->msi_inner_domain
);
666 static int advk_pcie_init_irq_domain(struct advk_pcie
*pcie
)
668 struct device
*dev
= &pcie
->pdev
->dev
;
669 struct device_node
*node
= dev
->of_node
;
670 struct device_node
*pcie_intc_node
;
671 struct irq_chip
*irq_chip
;
673 pcie_intc_node
= of_get_next_child(node
, NULL
);
674 if (!pcie_intc_node
) {
675 dev_err(dev
, "No PCIe Intc node found\n");
679 irq_chip
= &pcie
->irq_chip
;
681 irq_chip
->name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s-irq",
683 if (!irq_chip
->name
) {
684 of_node_put(pcie_intc_node
);
688 irq_chip
->irq_mask
= advk_pcie_irq_mask
;
689 irq_chip
->irq_mask_ack
= advk_pcie_irq_mask
;
690 irq_chip
->irq_unmask
= advk_pcie_irq_unmask
;
693 irq_domain_add_linear(pcie_intc_node
, PCI_NUM_INTX
,
694 &advk_pcie_irq_domain_ops
, pcie
);
695 if (!pcie
->irq_domain
) {
696 dev_err(dev
, "Failed to get a INTx IRQ domain\n");
697 of_node_put(pcie_intc_node
);
704 static void advk_pcie_remove_irq_domain(struct advk_pcie
*pcie
)
706 irq_domain_remove(pcie
->irq_domain
);
709 static void advk_pcie_handle_msi(struct advk_pcie
*pcie
)
711 u32 msi_val
, msi_mask
, msi_status
, msi_idx
;
714 msi_mask
= advk_readl(pcie
, PCIE_MSI_MASK_REG
);
715 msi_val
= advk_readl(pcie
, PCIE_MSI_STATUS_REG
);
716 msi_status
= msi_val
& ~msi_mask
;
718 for (msi_idx
= 0; msi_idx
< MSI_IRQ_NUM
; msi_idx
++) {
719 if (!(BIT(msi_idx
) & msi_status
))
722 advk_writel(pcie
, BIT(msi_idx
), PCIE_MSI_STATUS_REG
);
723 msi_data
= advk_readl(pcie
, PCIE_MSI_PAYLOAD_REG
) & 0xFF;
724 generic_handle_irq(msi_data
);
727 advk_writel(pcie
, PCIE_ISR0_MSI_INT_PENDING
,
731 static void advk_pcie_handle_int(struct advk_pcie
*pcie
)
733 u32 isr0_val
, isr0_mask
, isr0_status
;
734 u32 isr1_val
, isr1_mask
, isr1_status
;
737 isr0_val
= advk_readl(pcie
, PCIE_ISR0_REG
);
738 isr0_mask
= advk_readl(pcie
, PCIE_ISR0_MASK_REG
);
739 isr0_status
= isr0_val
& ((~isr0_mask
) & PCIE_ISR0_ALL_MASK
);
741 isr1_val
= advk_readl(pcie
, PCIE_ISR1_REG
);
742 isr1_mask
= advk_readl(pcie
, PCIE_ISR1_MASK_REG
);
743 isr1_status
= isr1_val
& ((~isr1_mask
) & PCIE_ISR1_ALL_MASK
);
745 if (!isr0_status
&& !isr1_status
) {
746 advk_writel(pcie
, isr0_val
, PCIE_ISR0_REG
);
747 advk_writel(pcie
, isr1_val
, PCIE_ISR1_REG
);
751 /* Process MSI interrupts */
752 if (isr0_status
& PCIE_ISR0_MSI_INT_PENDING
)
753 advk_pcie_handle_msi(pcie
);
755 /* Process legacy interrupts */
756 for (i
= 0; i
< PCI_NUM_INTX
; i
++) {
757 if (!(isr1_status
& PCIE_ISR1_INTX_ASSERT(i
)))
760 advk_writel(pcie
, PCIE_ISR1_INTX_ASSERT(i
),
763 virq
= irq_find_mapping(pcie
->irq_domain
, i
);
764 generic_handle_irq(virq
);
768 static irqreturn_t
advk_pcie_irq_handler(int irq
, void *arg
)
770 struct advk_pcie
*pcie
= arg
;
773 status
= advk_readl(pcie
, HOST_CTRL_INT_STATUS_REG
);
774 if (!(status
& PCIE_IRQ_CORE_INT
))
777 advk_pcie_handle_int(pcie
);
779 /* Clear interrupt */
780 advk_writel(pcie
, PCIE_IRQ_CORE_INT
, HOST_CTRL_INT_STATUS_REG
);
785 static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie
*pcie
)
787 int err
, res_valid
= 0;
788 struct device
*dev
= &pcie
->pdev
->dev
;
789 struct resource_entry
*win
, *tmp
;
790 resource_size_t iobase
;
792 INIT_LIST_HEAD(&pcie
->resources
);
794 err
= devm_of_pci_get_host_bridge_resources(dev
, 0, 0xff,
795 &pcie
->resources
, &iobase
);
799 err
= devm_request_pci_bus_resources(dev
, &pcie
->resources
);
801 goto out_release_res
;
803 resource_list_for_each_entry_safe(win
, tmp
, &pcie
->resources
) {
804 struct resource
*res
= win
->res
;
806 switch (resource_type(res
)) {
808 err
= devm_pci_remap_iospace(dev
, res
, iobase
);
810 dev_warn(dev
, "error %d: failed to map resource %pR\n",
812 resource_list_destroy_entry(win
);
816 res_valid
|= !(res
->flags
& IORESOURCE_PREFETCH
);
819 pcie
->root_bus_nr
= res
->start
;
825 dev_err(dev
, "non-prefetchable memory resource required\n");
827 goto out_release_res
;
833 pci_free_resource_list(&pcie
->resources
);
837 static int advk_pcie_probe(struct platform_device
*pdev
)
839 struct device
*dev
= &pdev
->dev
;
840 struct advk_pcie
*pcie
;
841 struct resource
*res
;
842 struct pci_host_bridge
*bridge
;
845 bridge
= devm_pci_alloc_host_bridge(dev
, sizeof(struct advk_pcie
));
849 pcie
= pci_host_bridge_priv(bridge
);
852 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
853 pcie
->base
= devm_ioremap_resource(dev
, res
);
854 if (IS_ERR(pcie
->base
))
855 return PTR_ERR(pcie
->base
);
857 irq
= platform_get_irq(pdev
, 0);
858 ret
= devm_request_irq(dev
, irq
, advk_pcie_irq_handler
,
859 IRQF_SHARED
| IRQF_NO_THREAD
, "advk-pcie",
862 dev_err(dev
, "Failed to register interrupt\n");
866 ret
= advk_pcie_parse_request_of_pci_ranges(pcie
);
868 dev_err(dev
, "Failed to parse resources\n");
872 advk_pcie_setup_hw(pcie
);
874 ret
= advk_pcie_init_irq_domain(pcie
);
876 dev_err(dev
, "Failed to initialize irq\n");
880 ret
= advk_pcie_init_msi_irq_domain(pcie
);
882 dev_err(dev
, "Failed to initialize irq\n");
883 advk_pcie_remove_irq_domain(pcie
);
887 list_splice_init(&pcie
->resources
, &bridge
->windows
);
888 bridge
->dev
.parent
= dev
;
889 bridge
->sysdata
= pcie
;
891 bridge
->ops
= &advk_pcie_ops
;
892 bridge
->map_irq
= of_irq_parse_and_map_pci
;
893 bridge
->swizzle_irq
= pci_common_swizzle
;
895 ret
= pci_host_probe(bridge
);
897 advk_pcie_remove_msi_irq_domain(pcie
);
898 advk_pcie_remove_irq_domain(pcie
);
905 static const struct of_device_id advk_pcie_of_match_table
[] = {
906 { .compatible
= "marvell,armada-3700-pcie", },
910 static struct platform_driver advk_pcie_driver
= {
913 .of_match_table
= advk_pcie_of_match_table
,
914 /* Driver unloading/unbinding currently not supported */
915 .suppress_bind_attrs
= true,
917 .probe
= advk_pcie_probe
,
919 builtin_platform_driver(advk_pcie_driver
);