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(0)
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_ALL_MASK GENMASK(5, 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 /* PCIe window configuration */
115 #define OB_WIN_BASE_ADDR 0x4c00
116 #define OB_WIN_BLOCK_SIZE 0x20
117 #define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
118 OB_WIN_BLOCK_SIZE * (win) + \
120 #define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
121 #define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
122 #define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
123 #define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
124 #define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
125 #define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
126 #define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
128 /* PCIe window types */
129 #define OB_PCIE_MEM 0x0
130 #define OB_PCIE_IO 0x4
132 /* LMI registers base address and register offsets */
133 #define LMI_BASE_ADDR 0x6000
134 #define CFG_REG (LMI_BASE_ADDR + 0x0)
135 #define LTSSM_SHIFT 24
136 #define LTSSM_MASK 0x3f
137 #define LTSSM_L0 0x10
138 #define RC_BAR_CONFIG 0x300
140 /* PCIe core controller registers */
141 #define CTRL_CORE_BASE_ADDR 0x18000
142 #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
143 #define CTRL_MODE_SHIFT 0x0
144 #define CTRL_MODE_MASK 0x1
145 #define PCIE_CORE_MODE_DIRECT 0x0
146 #define PCIE_CORE_MODE_COMMAND 0x1
148 /* PCIe Central Interrupts Registers */
149 #define CENTRAL_INT_BASE_ADDR 0x1b000
150 #define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
151 #define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
152 #define PCIE_IRQ_CMDQ_INT BIT(0)
153 #define PCIE_IRQ_MSI_STATUS_INT BIT(1)
154 #define PCIE_IRQ_CMD_SENT_DONE BIT(3)
155 #define PCIE_IRQ_DMA_INT BIT(4)
156 #define PCIE_IRQ_IB_DXFERDONE BIT(5)
157 #define PCIE_IRQ_OB_DXFERDONE BIT(6)
158 #define PCIE_IRQ_OB_RXFERDONE BIT(7)
159 #define PCIE_IRQ_COMPQ_INT BIT(12)
160 #define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
161 #define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
162 #define PCIE_IRQ_CORE_INT BIT(16)
163 #define PCIE_IRQ_CORE_INT_PIO BIT(17)
164 #define PCIE_IRQ_DPMU_INT BIT(18)
165 #define PCIE_IRQ_PCIE_MIS_INT BIT(19)
166 #define PCIE_IRQ_MSI_INT1_DET BIT(20)
167 #define PCIE_IRQ_MSI_INT2_DET BIT(21)
168 #define PCIE_IRQ_RC_DBELL_DET BIT(22)
169 #define PCIE_IRQ_EP_STATUS BIT(23)
170 #define PCIE_IRQ_ALL_MASK 0xfff0fb
171 #define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
173 /* Transaction types */
174 #define PCIE_CONFIG_RD_TYPE0 0x8
175 #define PCIE_CONFIG_RD_TYPE1 0x9
176 #define PCIE_CONFIG_WR_TYPE0 0xa
177 #define PCIE_CONFIG_WR_TYPE1 0xb
179 #define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
180 #define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
181 #define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
182 #define PCIE_CONF_REG(reg) ((reg) & 0xffc)
183 #define PCIE_CONF_ADDR(bus, devfn, where) \
184 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
185 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
187 #define PIO_TIMEOUT_MS 1
189 #define LINK_WAIT_MAX_RETRIES 10
190 #define LINK_WAIT_USLEEP_MIN 90000
191 #define LINK_WAIT_USLEEP_MAX 100000
193 #define LEGACY_IRQ_NUM 4
194 #define MSI_IRQ_NUM 32
197 struct platform_device *pdev;
199 struct list_head resources;
200 struct irq_domain *irq_domain;
201 struct irq_chip irq_chip;
202 struct msi_controller msi;
203 struct irq_domain *msi_domain;
204 struct irq_chip msi_irq_chip;
205 DECLARE_BITMAP(msi_irq_in_use, MSI_IRQ_NUM);
206 struct mutex msi_used_lock;
211 static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
213 writel(val, pcie->base + reg);
216 static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
218 return readl(pcie->base + reg);
221 static int advk_pcie_link_up(struct advk_pcie *pcie)
223 u32 val, ltssm_state;
225 val = advk_readl(pcie, CFG_REG);
226 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
227 return ltssm_state >= LTSSM_L0;
230 static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
232 struct device *dev = &pcie->pdev->dev;
235 /* check if the link is up or not */
236 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
237 if (advk_pcie_link_up(pcie)) {
238 dev_info(dev, "link up\n");
242 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
245 dev_err(dev, "link never came up\n");
250 * Set PCIe address window register which could be used for memory
253 static void advk_pcie_set_ob_win(struct advk_pcie *pcie,
254 u32 win_num, u32 match_ms,
255 u32 match_ls, u32 mask_ms,
256 u32 mask_ls, u32 remap_ms,
257 u32 remap_ls, u32 action)
259 advk_writel(pcie, match_ls, OB_WIN_MATCH_LS(win_num));
260 advk_writel(pcie, match_ms, OB_WIN_MATCH_MS(win_num));
261 advk_writel(pcie, mask_ms, OB_WIN_MASK_MS(win_num));
262 advk_writel(pcie, mask_ls, OB_WIN_MASK_LS(win_num));
263 advk_writel(pcie, remap_ms, OB_WIN_REMAP_MS(win_num));
264 advk_writel(pcie, remap_ls, OB_WIN_REMAP_LS(win_num));
265 advk_writel(pcie, action, OB_WIN_ACTIONS(win_num));
266 advk_writel(pcie, match_ls | BIT(0), OB_WIN_MATCH_LS(win_num));
269 static void advk_pcie_setup_hw(struct advk_pcie *pcie)
274 /* Point PCIe unit MBUS decode windows to DRAM space */
275 for (i = 0; i < 8; i++)
276 advk_pcie_set_ob_win(pcie, i, 0, 0, 0, 0, 0, 0, 0);
278 /* Set to Direct mode */
279 reg = advk_readl(pcie, CTRL_CONFIG_REG);
280 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
281 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
282 advk_writel(pcie, reg, CTRL_CONFIG_REG);
284 /* Set PCI global control register to RC mode */
285 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
286 reg |= (IS_RC_MSK << IS_RC_SHIFT);
287 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
289 /* Set Advanced Error Capabilities and Control PF0 register */
290 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
291 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
292 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
293 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
294 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
296 /* Set PCIe Device Control and Status 1 PF0 register */
297 reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
298 (7 << PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT) |
299 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE |
300 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ <<
301 PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT);
302 advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
304 /* Program PCIe Control 2 to disable strict ordering */
305 reg = PCIE_CORE_CTRL2_RESERVED |
306 PCIE_CORE_CTRL2_TD_ENABLE;
307 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
310 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
311 reg &= ~PCIE_GEN_SEL_MSK;
313 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
316 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
317 reg &= ~LANE_CNT_MSK;
319 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
321 /* Enable link training */
322 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
323 reg |= LINK_TRAINING_EN;
324 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
327 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
328 reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
329 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
331 /* Clear all interrupts */
332 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
333 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
334 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
336 /* Disable All ISR0/1 Sources */
337 reg = PCIE_ISR0_ALL_MASK;
338 reg &= ~PCIE_ISR0_MSI_INT_PENDING;
339 advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
341 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
343 /* Unmask all MSI's */
344 advk_writel(pcie, 0, PCIE_MSI_MASK_REG);
346 /* Enable summary interrupt for GIC SPI source */
347 reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
348 advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
350 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
351 reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
352 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
354 /* Bypass the address window mapping for PIO */
355 reg = advk_readl(pcie, PIO_CTRL);
356 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
357 advk_writel(pcie, reg, PIO_CTRL);
359 /* Start link training */
360 reg = advk_readl(pcie, PCIE_CORE_LINK_CTRL_STAT_REG);
361 reg |= PCIE_CORE_LINK_TRAINING;
362 advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
364 advk_pcie_wait_for_link(pcie);
366 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
367 reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
368 PCIE_CORE_CMD_IO_ACCESS_EN |
369 PCIE_CORE_CMD_MEM_IO_REQ_EN;
370 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
373 static void advk_pcie_check_pio_status(struct advk_pcie *pcie)
375 struct device *dev = &pcie->pdev->dev;
378 char *strcomp_status, *str_posted;
380 reg = advk_readl(pcie, PIO_STAT);
381 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
382 PIO_COMPLETION_STATUS_SHIFT;
388 case PIO_COMPLETION_STATUS_UR:
389 strcomp_status = "UR";
391 case PIO_COMPLETION_STATUS_CRS:
392 strcomp_status = "CRS";
394 case PIO_COMPLETION_STATUS_CA:
395 strcomp_status = "CA";
398 strcomp_status = "Unknown";
402 if (reg & PIO_NON_POSTED_REQ)
403 str_posted = "Non-posted";
405 str_posted = "Posted";
407 dev_err(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
408 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
411 static int advk_pcie_wait_pio(struct advk_pcie *pcie)
413 struct device *dev = &pcie->pdev->dev;
414 unsigned long timeout;
416 timeout = jiffies + msecs_to_jiffies(PIO_TIMEOUT_MS);
418 while (time_before(jiffies, timeout)) {
421 start = advk_readl(pcie, PIO_START);
422 isr = advk_readl(pcie, PIO_ISR);
427 dev_err(dev, "config read/write timed out\n");
431 static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
432 int where, int size, u32 *val)
434 struct advk_pcie *pcie = bus->sysdata;
438 if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0) {
440 return PCIBIOS_DEVICE_NOT_FOUND;
444 advk_writel(pcie, 0, PIO_START);
445 advk_writel(pcie, 1, PIO_ISR);
447 /* Program the control register */
448 reg = advk_readl(pcie, PIO_CTRL);
449 reg &= ~PIO_CTRL_TYPE_MASK;
450 if (bus->number == pcie->root_bus_nr)
451 reg |= PCIE_CONFIG_RD_TYPE0;
453 reg |= PCIE_CONFIG_RD_TYPE1;
454 advk_writel(pcie, reg, PIO_CTRL);
456 /* Program the address registers */
457 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
458 advk_writel(pcie, reg, PIO_ADDR_LS);
459 advk_writel(pcie, 0, PIO_ADDR_MS);
461 /* Program the data strobe */
462 advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
464 /* Start the transfer */
465 advk_writel(pcie, 1, PIO_START);
467 ret = advk_pcie_wait_pio(pcie);
469 return PCIBIOS_SET_FAILED;
471 advk_pcie_check_pio_status(pcie);
473 /* Get the read result */
474 *val = advk_readl(pcie, PIO_RD_DATA);
476 *val = (*val >> (8 * (where & 3))) & 0xff;
478 *val = (*val >> (8 * (where & 3))) & 0xffff;
480 return PCIBIOS_SUCCESSFUL;
483 static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
484 int where, int size, u32 val)
486 struct advk_pcie *pcie = bus->sysdata;
488 u32 data_strobe = 0x0;
492 if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0)
493 return PCIBIOS_DEVICE_NOT_FOUND;
496 return PCIBIOS_SET_FAILED;
499 advk_writel(pcie, 0, PIO_START);
500 advk_writel(pcie, 1, PIO_ISR);
502 /* Program the control register */
503 reg = advk_readl(pcie, PIO_CTRL);
504 reg &= ~PIO_CTRL_TYPE_MASK;
505 if (bus->number == pcie->root_bus_nr)
506 reg |= PCIE_CONFIG_WR_TYPE0;
508 reg |= PCIE_CONFIG_WR_TYPE1;
509 advk_writel(pcie, reg, PIO_CTRL);
511 /* Program the address registers */
512 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
513 advk_writel(pcie, reg, PIO_ADDR_LS);
514 advk_writel(pcie, 0, PIO_ADDR_MS);
516 /* Calculate the write strobe */
517 offset = where & 0x3;
518 reg = val << (8 * offset);
519 data_strobe = GENMASK(size - 1, 0) << offset;
521 /* Program the data register */
522 advk_writel(pcie, reg, PIO_WR_DATA);
524 /* Program the data strobe */
525 advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
527 /* Start the transfer */
528 advk_writel(pcie, 1, PIO_START);
530 ret = advk_pcie_wait_pio(pcie);
532 return PCIBIOS_SET_FAILED;
534 advk_pcie_check_pio_status(pcie);
536 return PCIBIOS_SUCCESSFUL;
539 static struct pci_ops advk_pcie_ops = {
540 .read = advk_pcie_rd_conf,
541 .write = advk_pcie_wr_conf,
544 static int advk_pcie_alloc_msi(struct advk_pcie *pcie)
548 mutex_lock(&pcie->msi_used_lock);
549 hwirq = find_first_zero_bit(pcie->msi_irq_in_use, MSI_IRQ_NUM);
550 if (hwirq >= MSI_IRQ_NUM)
553 set_bit(hwirq, pcie->msi_irq_in_use);
554 mutex_unlock(&pcie->msi_used_lock);
559 static void advk_pcie_free_msi(struct advk_pcie *pcie, int hwirq)
561 struct device *dev = &pcie->pdev->dev;
563 mutex_lock(&pcie->msi_used_lock);
564 if (!test_bit(hwirq, pcie->msi_irq_in_use))
565 dev_err(dev, "trying to free unused MSI#%d\n", hwirq);
567 clear_bit(hwirq, pcie->msi_irq_in_use);
568 mutex_unlock(&pcie->msi_used_lock);
571 static int advk_pcie_setup_msi_irq(struct msi_controller *chip,
572 struct pci_dev *pdev,
573 struct msi_desc *desc)
575 struct advk_pcie *pcie = pdev->bus->sysdata;
578 phys_addr_t msi_msg_phys;
580 /* We support MSI, but not MSI-X */
581 if (desc->msi_attrib.is_msix)
584 hwirq = advk_pcie_alloc_msi(pcie);
588 virq = irq_create_mapping(pcie->msi_domain, hwirq);
590 advk_pcie_free_msi(pcie, hwirq);
594 irq_set_msi_desc(virq, desc);
596 msi_msg_phys = virt_to_phys(&pcie->msi_msg);
598 msg.address_lo = lower_32_bits(msi_msg_phys);
599 msg.address_hi = upper_32_bits(msi_msg_phys);
602 pci_write_msi_msg(virq, &msg);
607 static void advk_pcie_teardown_msi_irq(struct msi_controller *chip,
610 struct irq_data *d = irq_get_irq_data(irq);
611 struct msi_desc *msi = irq_data_get_msi_desc(d);
612 struct advk_pcie *pcie = msi_desc_to_pci_sysdata(msi);
613 unsigned long hwirq = d->hwirq;
615 irq_dispose_mapping(irq);
616 advk_pcie_free_msi(pcie, hwirq);
619 static int advk_pcie_msi_map(struct irq_domain *domain,
620 unsigned int virq, irq_hw_number_t hw)
622 struct advk_pcie *pcie = domain->host_data;
624 irq_set_chip_and_handler(virq, &pcie->msi_irq_chip,
630 static const struct irq_domain_ops advk_pcie_msi_irq_ops = {
631 .map = advk_pcie_msi_map,
634 static void advk_pcie_irq_mask(struct irq_data *d)
636 struct advk_pcie *pcie = d->domain->host_data;
637 irq_hw_number_t hwirq = irqd_to_hwirq(d);
640 mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
641 mask |= PCIE_ISR0_INTX_ASSERT(hwirq);
642 advk_writel(pcie, mask, PCIE_ISR0_MASK_REG);
645 static void advk_pcie_irq_unmask(struct irq_data *d)
647 struct advk_pcie *pcie = d->domain->host_data;
648 irq_hw_number_t hwirq = irqd_to_hwirq(d);
651 mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
652 mask &= ~PCIE_ISR0_INTX_ASSERT(hwirq);
653 advk_writel(pcie, mask, PCIE_ISR0_MASK_REG);
656 static int advk_pcie_irq_map(struct irq_domain *h,
657 unsigned int virq, irq_hw_number_t hwirq)
659 struct advk_pcie *pcie = h->host_data;
661 advk_pcie_irq_mask(irq_get_irq_data(virq));
662 irq_set_status_flags(virq, IRQ_LEVEL);
663 irq_set_chip_and_handler(virq, &pcie->irq_chip,
665 irq_set_chip_data(virq, pcie);
670 static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
671 .map = advk_pcie_irq_map,
672 .xlate = irq_domain_xlate_onecell,
675 static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
677 struct device *dev = &pcie->pdev->dev;
678 struct device_node *node = dev->of_node;
679 struct irq_chip *msi_irq_chip;
680 struct msi_controller *msi;
681 phys_addr_t msi_msg_phys;
684 msi_irq_chip = &pcie->msi_irq_chip;
686 msi_irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-msi",
688 if (!msi_irq_chip->name)
691 msi_irq_chip->irq_enable = pci_msi_unmask_irq;
692 msi_irq_chip->irq_disable = pci_msi_mask_irq;
693 msi_irq_chip->irq_mask = pci_msi_mask_irq;
694 msi_irq_chip->irq_unmask = pci_msi_unmask_irq;
698 msi->setup_irq = advk_pcie_setup_msi_irq;
699 msi->teardown_irq = advk_pcie_teardown_msi_irq;
702 mutex_init(&pcie->msi_used_lock);
704 msi_msg_phys = virt_to_phys(&pcie->msi_msg);
706 advk_writel(pcie, lower_32_bits(msi_msg_phys),
707 PCIE_MSI_ADDR_LOW_REG);
708 advk_writel(pcie, upper_32_bits(msi_msg_phys),
709 PCIE_MSI_ADDR_HIGH_REG);
712 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
713 &advk_pcie_msi_irq_ops, pcie);
714 if (!pcie->msi_domain)
717 ret = of_pci_msi_chip_add(msi);
719 irq_domain_remove(pcie->msi_domain);
726 static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
728 of_pci_msi_chip_remove(&pcie->msi);
729 irq_domain_remove(pcie->msi_domain);
732 static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
734 struct device *dev = &pcie->pdev->dev;
735 struct device_node *node = dev->of_node;
736 struct device_node *pcie_intc_node;
737 struct irq_chip *irq_chip;
739 pcie_intc_node = of_get_next_child(node, NULL);
740 if (!pcie_intc_node) {
741 dev_err(dev, "No PCIe Intc node found\n");
745 irq_chip = &pcie->irq_chip;
747 irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
749 if (!irq_chip->name) {
750 of_node_put(pcie_intc_node);
754 irq_chip->irq_mask = advk_pcie_irq_mask;
755 irq_chip->irq_mask_ack = advk_pcie_irq_mask;
756 irq_chip->irq_unmask = advk_pcie_irq_unmask;
759 irq_domain_add_linear(pcie_intc_node, LEGACY_IRQ_NUM,
760 &advk_pcie_irq_domain_ops, pcie);
761 if (!pcie->irq_domain) {
762 dev_err(dev, "Failed to get a INTx IRQ domain\n");
763 of_node_put(pcie_intc_node);
770 static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
772 irq_domain_remove(pcie->irq_domain);
775 static void advk_pcie_handle_msi(struct advk_pcie *pcie)
777 u32 msi_val, msi_mask, msi_status, msi_idx;
780 msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
781 msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
782 msi_status = msi_val & ~msi_mask;
784 for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
785 if (!(BIT(msi_idx) & msi_status))
788 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
789 msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & 0xFF;
790 generic_handle_irq(msi_data);
793 advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
797 static void advk_pcie_handle_int(struct advk_pcie *pcie)
799 u32 val, mask, status;
802 val = advk_readl(pcie, PCIE_ISR0_REG);
803 mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
804 status = val & ((~mask) & PCIE_ISR0_ALL_MASK);
807 advk_writel(pcie, val, PCIE_ISR0_REG);
811 /* Process MSI interrupts */
812 if (status & PCIE_ISR0_MSI_INT_PENDING)
813 advk_pcie_handle_msi(pcie);
815 /* Process legacy interrupts */
816 for (i = 0; i < LEGACY_IRQ_NUM; i++) {
817 if (!(status & PCIE_ISR0_INTX_ASSERT(i)))
820 advk_writel(pcie, PCIE_ISR0_INTX_ASSERT(i),
823 virq = irq_find_mapping(pcie->irq_domain, i);
824 generic_handle_irq(virq);
828 static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
830 struct advk_pcie *pcie = arg;
833 status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
834 if (!(status & PCIE_IRQ_CORE_INT))
837 advk_pcie_handle_int(pcie);
839 /* Clear interrupt */
840 advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
845 static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie *pcie)
847 int err, res_valid = 0;
848 struct device *dev = &pcie->pdev->dev;
849 struct device_node *np = dev->of_node;
850 struct resource_entry *win, *tmp;
851 resource_size_t iobase;
853 INIT_LIST_HEAD(&pcie->resources);
855 err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pcie->resources,
860 err = devm_request_pci_bus_resources(dev, &pcie->resources);
862 goto out_release_res;
864 resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
865 struct resource *res = win->res;
867 switch (resource_type(res)) {
869 advk_pcie_set_ob_win(pcie, 1,
870 upper_32_bits(res->start),
871 lower_32_bits(res->start),
873 lower_32_bits(res->start),
875 err = pci_remap_iospace(res, iobase);
877 dev_warn(dev, "error %d: failed to map resource %pR\n",
879 resource_list_destroy_entry(win);
883 advk_pcie_set_ob_win(pcie, 0,
884 upper_32_bits(res->start),
885 lower_32_bits(res->start),
887 lower_32_bits(res->start),
888 (2 << 20) | OB_PCIE_MEM);
889 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
892 pcie->root_bus_nr = res->start;
898 dev_err(dev, "non-prefetchable memory resource required\n");
900 goto out_release_res;
906 pci_free_resource_list(&pcie->resources);
910 static int advk_pcie_probe(struct platform_device *pdev)
912 struct device *dev = &pdev->dev;
913 struct advk_pcie *pcie;
914 struct resource *res;
915 struct pci_bus *bus, *child;
916 struct msi_controller *msi;
917 struct device_node *msi_node;
920 pcie = devm_kzalloc(dev, sizeof(struct advk_pcie), GFP_KERNEL);
926 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
927 pcie->base = devm_ioremap_resource(dev, res);
928 if (IS_ERR(pcie->base))
929 return PTR_ERR(pcie->base);
931 irq = platform_get_irq(pdev, 0);
932 ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
933 IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
936 dev_err(dev, "Failed to register interrupt\n");
940 ret = advk_pcie_parse_request_of_pci_ranges(pcie);
942 dev_err(dev, "Failed to parse resources\n");
946 advk_pcie_setup_hw(pcie);
948 ret = advk_pcie_init_irq_domain(pcie);
950 dev_err(dev, "Failed to initialize irq\n");
954 ret = advk_pcie_init_msi_irq_domain(pcie);
956 dev_err(dev, "Failed to initialize irq\n");
957 advk_pcie_remove_irq_domain(pcie);
961 msi_node = of_parse_phandle(dev->of_node, "msi-parent", 0);
963 msi = of_pci_find_msi_chip_by_node(msi_node);
967 bus = pci_scan_root_bus_msi(dev, 0, &advk_pcie_ops,
968 pcie, &pcie->resources, &pcie->msi);
970 advk_pcie_remove_msi_irq_domain(pcie);
971 advk_pcie_remove_irq_domain(pcie);
975 pci_bus_size_bridges(bus);
976 pci_bus_assign_resources(bus);
978 list_for_each_entry(child, &bus->children, node)
979 pcie_bus_configure_settings(child);
981 pci_bus_add_devices(bus);
985 static const struct of_device_id advk_pcie_of_match_table[] = {
986 { .compatible = "marvell,armada-3700-pcie", },
990 static struct platform_driver advk_pcie_driver = {
993 .of_match_table = advk_pcie_of_match_table,
994 /* Driver unloading/unbinding currently not supported */
995 .suppress_bind_attrs = true,
997 .probe = advk_pcie_probe,
999 builtin_platform_driver(advk_pcie_driver);