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)
113 #define PCIE_MSI_DATA_MASK GENMASK(15, 0)
115 /* PCIe window configuration */
116 #define OB_WIN_BASE_ADDR 0x4c00
117 #define OB_WIN_BLOCK_SIZE 0x20
118 #define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
119 OB_WIN_BLOCK_SIZE * (win) + \
121 #define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
122 #define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
123 #define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
124 #define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
125 #define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
126 #define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
127 #define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
129 /* PCIe window types */
130 #define OB_PCIE_MEM 0x0
131 #define OB_PCIE_IO 0x4
133 /* LMI registers base address and register offsets */
134 #define LMI_BASE_ADDR 0x6000
135 #define CFG_REG (LMI_BASE_ADDR + 0x0)
136 #define LTSSM_SHIFT 24
137 #define LTSSM_MASK 0x3f
138 #define LTSSM_L0 0x10
139 #define RC_BAR_CONFIG 0x300
141 /* PCIe core controller registers */
142 #define CTRL_CORE_BASE_ADDR 0x18000
143 #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
144 #define CTRL_MODE_SHIFT 0x0
145 #define CTRL_MODE_MASK 0x1
146 #define PCIE_CORE_MODE_DIRECT 0x0
147 #define PCIE_CORE_MODE_COMMAND 0x1
149 /* PCIe Central Interrupts Registers */
150 #define CENTRAL_INT_BASE_ADDR 0x1b000
151 #define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
152 #define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
153 #define PCIE_IRQ_CMDQ_INT BIT(0)
154 #define PCIE_IRQ_MSI_STATUS_INT BIT(1)
155 #define PCIE_IRQ_CMD_SENT_DONE BIT(3)
156 #define PCIE_IRQ_DMA_INT BIT(4)
157 #define PCIE_IRQ_IB_DXFERDONE BIT(5)
158 #define PCIE_IRQ_OB_DXFERDONE BIT(6)
159 #define PCIE_IRQ_OB_RXFERDONE BIT(7)
160 #define PCIE_IRQ_COMPQ_INT BIT(12)
161 #define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
162 #define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
163 #define PCIE_IRQ_CORE_INT BIT(16)
164 #define PCIE_IRQ_CORE_INT_PIO BIT(17)
165 #define PCIE_IRQ_DPMU_INT BIT(18)
166 #define PCIE_IRQ_PCIE_MIS_INT BIT(19)
167 #define PCIE_IRQ_MSI_INT1_DET BIT(20)
168 #define PCIE_IRQ_MSI_INT2_DET BIT(21)
169 #define PCIE_IRQ_RC_DBELL_DET BIT(22)
170 #define PCIE_IRQ_EP_STATUS BIT(23)
171 #define PCIE_IRQ_ALL_MASK 0xfff0fb
172 #define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
174 /* Transaction types */
175 #define PCIE_CONFIG_RD_TYPE0 0x8
176 #define PCIE_CONFIG_RD_TYPE1 0x9
177 #define PCIE_CONFIG_WR_TYPE0 0xa
178 #define PCIE_CONFIG_WR_TYPE1 0xb
180 #define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
181 #define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
182 #define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
183 #define PCIE_CONF_REG(reg) ((reg) & 0xffc)
184 #define PCIE_CONF_ADDR(bus, devfn, where) \
185 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
186 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
188 #define PIO_TIMEOUT_MS 1
190 #define LINK_WAIT_MAX_RETRIES 10
191 #define LINK_WAIT_USLEEP_MIN 90000
192 #define LINK_WAIT_USLEEP_MAX 100000
194 #define LEGACY_IRQ_NUM 4
195 #define MSI_IRQ_NUM 32
198 struct platform_device *pdev;
200 struct list_head resources;
201 struct irq_domain *irq_domain;
202 struct irq_chip irq_chip;
203 struct msi_controller msi;
204 struct irq_domain *msi_domain;
205 struct irq_chip msi_irq_chip;
206 DECLARE_BITMAP(msi_irq_in_use, MSI_IRQ_NUM);
207 struct mutex msi_used_lock;
212 static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
214 writel(val, pcie->base + reg);
217 static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
219 return readl(pcie->base + reg);
222 static int advk_pcie_link_up(struct advk_pcie *pcie)
224 u32 val, ltssm_state;
226 val = advk_readl(pcie, CFG_REG);
227 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
228 return ltssm_state >= LTSSM_L0;
231 static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
233 struct device *dev = &pcie->pdev->dev;
236 /* check if the link is up or not */
237 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
238 if (advk_pcie_link_up(pcie)) {
239 dev_info(dev, "link up\n");
243 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
246 dev_err(dev, "link never came up\n");
251 * Set PCIe address window register which could be used for memory
254 static void advk_pcie_set_ob_win(struct advk_pcie *pcie,
255 u32 win_num, u32 match_ms,
256 u32 match_ls, u32 mask_ms,
257 u32 mask_ls, u32 remap_ms,
258 u32 remap_ls, u32 action)
260 advk_writel(pcie, match_ls, OB_WIN_MATCH_LS(win_num));
261 advk_writel(pcie, match_ms, OB_WIN_MATCH_MS(win_num));
262 advk_writel(pcie, mask_ms, OB_WIN_MASK_MS(win_num));
263 advk_writel(pcie, mask_ls, OB_WIN_MASK_LS(win_num));
264 advk_writel(pcie, remap_ms, OB_WIN_REMAP_MS(win_num));
265 advk_writel(pcie, remap_ls, OB_WIN_REMAP_LS(win_num));
266 advk_writel(pcie, action, OB_WIN_ACTIONS(win_num));
267 advk_writel(pcie, match_ls | BIT(0), OB_WIN_MATCH_LS(win_num));
270 static void advk_pcie_setup_hw(struct advk_pcie *pcie)
275 /* Point PCIe unit MBUS decode windows to DRAM space */
276 for (i = 0; i < 8; i++)
277 advk_pcie_set_ob_win(pcie, i, 0, 0, 0, 0, 0, 0, 0);
279 /* Set to Direct mode */
280 reg = advk_readl(pcie, CTRL_CONFIG_REG);
281 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
282 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
283 advk_writel(pcie, reg, CTRL_CONFIG_REG);
285 /* Set PCI global control register to RC mode */
286 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
287 reg |= (IS_RC_MSK << IS_RC_SHIFT);
288 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
290 /* Set Advanced Error Capabilities and Control PF0 register */
291 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
292 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
293 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
294 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
295 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
297 /* Set PCIe Device Control and Status 1 PF0 register */
298 reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
299 (7 << PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT) |
300 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE |
301 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ <<
302 PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT);
303 advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
305 /* Program PCIe Control 2 to disable strict ordering */
306 reg = PCIE_CORE_CTRL2_RESERVED |
307 PCIE_CORE_CTRL2_TD_ENABLE;
308 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
311 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
312 reg &= ~PCIE_GEN_SEL_MSK;
314 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
317 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
318 reg &= ~LANE_CNT_MSK;
320 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
322 /* Enable link training */
323 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
324 reg |= LINK_TRAINING_EN;
325 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
328 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
329 reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
330 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
332 /* Clear all interrupts */
333 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
334 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
335 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
337 /* Disable All ISR0/1 Sources */
338 reg = PCIE_ISR0_ALL_MASK;
339 reg &= ~PCIE_ISR0_MSI_INT_PENDING;
340 advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
342 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
344 /* Unmask all MSI's */
345 advk_writel(pcie, 0, PCIE_MSI_MASK_REG);
347 /* Enable summary interrupt for GIC SPI source */
348 reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
349 advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
351 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
352 reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
353 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
355 /* Bypass the address window mapping for PIO */
356 reg = advk_readl(pcie, PIO_CTRL);
357 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
358 advk_writel(pcie, reg, PIO_CTRL);
360 /* Start link training */
361 reg = advk_readl(pcie, PCIE_CORE_LINK_CTRL_STAT_REG);
362 reg |= PCIE_CORE_LINK_TRAINING;
363 advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
365 advk_pcie_wait_for_link(pcie);
367 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
368 reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
369 PCIE_CORE_CMD_IO_ACCESS_EN |
370 PCIE_CORE_CMD_MEM_IO_REQ_EN;
371 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
374 static void advk_pcie_check_pio_status(struct advk_pcie *pcie)
376 struct device *dev = &pcie->pdev->dev;
379 char *strcomp_status, *str_posted;
381 reg = advk_readl(pcie, PIO_STAT);
382 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
383 PIO_COMPLETION_STATUS_SHIFT;
389 case PIO_COMPLETION_STATUS_UR:
390 strcomp_status = "UR";
392 case PIO_COMPLETION_STATUS_CRS:
393 strcomp_status = "CRS";
395 case PIO_COMPLETION_STATUS_CA:
396 strcomp_status = "CA";
399 strcomp_status = "Unknown";
403 if (reg & PIO_NON_POSTED_REQ)
404 str_posted = "Non-posted";
406 str_posted = "Posted";
408 dev_dbg(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
409 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;
415 unsigned long timeout;
417 timeout = jiffies + msecs_to_jiffies(PIO_TIMEOUT_MS);
419 while (time_before(jiffies, timeout)) {
422 start = advk_readl(pcie, PIO_START);
423 isr = advk_readl(pcie, PIO_ISR);
428 dev_err(dev, "config read/write timed out\n");
432 static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
433 int where, int size, u32 *val)
435 struct advk_pcie *pcie = bus->sysdata;
439 if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0) {
441 return PCIBIOS_DEVICE_NOT_FOUND;
445 advk_writel(pcie, 0, PIO_START);
446 advk_writel(pcie, 1, PIO_ISR);
448 /* Program the control register */
449 reg = advk_readl(pcie, PIO_CTRL);
450 reg &= ~PIO_CTRL_TYPE_MASK;
451 if (bus->number == pcie->root_bus_nr)
452 reg |= PCIE_CONFIG_RD_TYPE0;
454 reg |= PCIE_CONFIG_RD_TYPE1;
455 advk_writel(pcie, reg, PIO_CTRL);
457 /* Program the address registers */
458 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
459 advk_writel(pcie, reg, PIO_ADDR_LS);
460 advk_writel(pcie, 0, PIO_ADDR_MS);
462 /* Program the data strobe */
463 advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
465 /* Start the transfer */
466 advk_writel(pcie, 1, PIO_START);
468 ret = advk_pcie_wait_pio(pcie);
470 return PCIBIOS_SET_FAILED;
472 advk_pcie_check_pio_status(pcie);
474 /* Get the read result */
475 *val = advk_readl(pcie, PIO_RD_DATA);
477 *val = (*val >> (8 * (where & 3))) & 0xff;
479 *val = (*val >> (8 * (where & 3))) & 0xffff;
481 return PCIBIOS_SUCCESSFUL;
484 static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
485 int where, int size, u32 val)
487 struct advk_pcie *pcie = bus->sysdata;
489 u32 data_strobe = 0x0;
493 if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0)
494 return PCIBIOS_DEVICE_NOT_FOUND;
497 return PCIBIOS_SET_FAILED;
500 advk_writel(pcie, 0, PIO_START);
501 advk_writel(pcie, 1, PIO_ISR);
503 /* Program the control register */
504 reg = advk_readl(pcie, PIO_CTRL);
505 reg &= ~PIO_CTRL_TYPE_MASK;
506 if (bus->number == pcie->root_bus_nr)
507 reg |= PCIE_CONFIG_WR_TYPE0;
509 reg |= PCIE_CONFIG_WR_TYPE1;
510 advk_writel(pcie, reg, PIO_CTRL);
512 /* Program the address registers */
513 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
514 advk_writel(pcie, reg, PIO_ADDR_LS);
515 advk_writel(pcie, 0, PIO_ADDR_MS);
517 /* Calculate the write strobe */
518 offset = where & 0x3;
519 reg = val << (8 * offset);
520 data_strobe = GENMASK(size - 1, 0) << offset;
522 /* Program the data register */
523 advk_writel(pcie, reg, PIO_WR_DATA);
525 /* Program the data strobe */
526 advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
528 /* Start the transfer */
529 advk_writel(pcie, 1, PIO_START);
531 ret = advk_pcie_wait_pio(pcie);
533 return PCIBIOS_SET_FAILED;
535 advk_pcie_check_pio_status(pcie);
537 return PCIBIOS_SUCCESSFUL;
540 static struct pci_ops advk_pcie_ops = {
541 .read = advk_pcie_rd_conf,
542 .write = advk_pcie_wr_conf,
545 static int advk_pcie_alloc_msi(struct advk_pcie *pcie)
549 mutex_lock(&pcie->msi_used_lock);
550 hwirq = find_first_zero_bit(pcie->msi_irq_in_use, MSI_IRQ_NUM);
551 if (hwirq >= MSI_IRQ_NUM)
554 set_bit(hwirq, pcie->msi_irq_in_use);
555 mutex_unlock(&pcie->msi_used_lock);
560 static void advk_pcie_free_msi(struct advk_pcie *pcie, int hwirq)
562 struct device *dev = &pcie->pdev->dev;
564 mutex_lock(&pcie->msi_used_lock);
565 if (!test_bit(hwirq, pcie->msi_irq_in_use))
566 dev_err(dev, "trying to free unused MSI#%d\n", hwirq);
568 clear_bit(hwirq, pcie->msi_irq_in_use);
569 mutex_unlock(&pcie->msi_used_lock);
572 static int advk_pcie_setup_msi_irq(struct msi_controller *chip,
573 struct pci_dev *pdev,
574 struct msi_desc *desc)
576 struct advk_pcie *pcie = pdev->bus->sysdata;
579 phys_addr_t msi_msg_phys;
581 /* We support MSI, but not MSI-X */
582 if (desc->msi_attrib.is_msix)
585 hwirq = advk_pcie_alloc_msi(pcie);
589 virq = irq_create_mapping(pcie->msi_domain, hwirq);
591 advk_pcie_free_msi(pcie, hwirq);
595 irq_set_msi_desc(virq, desc);
597 msi_msg_phys = virt_to_phys(&pcie->msi_msg);
599 msg.address_lo = lower_32_bits(msi_msg_phys);
600 msg.address_hi = upper_32_bits(msi_msg_phys);
603 pci_write_msi_msg(virq, &msg);
608 static void advk_pcie_teardown_msi_irq(struct msi_controller *chip,
611 struct irq_data *d = irq_get_irq_data(irq);
612 struct msi_desc *msi = irq_data_get_msi_desc(d);
613 struct advk_pcie *pcie = msi_desc_to_pci_sysdata(msi);
614 unsigned long hwirq = d->hwirq;
616 irq_dispose_mapping(irq);
617 advk_pcie_free_msi(pcie, hwirq);
620 static int advk_pcie_msi_map(struct irq_domain *domain,
621 unsigned int virq, irq_hw_number_t hw)
623 struct advk_pcie *pcie = domain->host_data;
625 irq_set_chip_and_handler(virq, &pcie->msi_irq_chip,
631 static const struct irq_domain_ops advk_pcie_msi_irq_ops = {
632 .map = advk_pcie_msi_map,
635 static void advk_pcie_irq_mask(struct irq_data *d)
637 struct advk_pcie *pcie = d->domain->host_data;
638 irq_hw_number_t hwirq = irqd_to_hwirq(d);
641 mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
642 mask |= PCIE_ISR0_INTX_ASSERT(hwirq);
643 advk_writel(pcie, mask, PCIE_ISR0_MASK_REG);
646 static void advk_pcie_irq_unmask(struct irq_data *d)
648 struct advk_pcie *pcie = d->domain->host_data;
649 irq_hw_number_t hwirq = irqd_to_hwirq(d);
652 mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
653 mask &= ~PCIE_ISR0_INTX_ASSERT(hwirq);
654 advk_writel(pcie, mask, PCIE_ISR0_MASK_REG);
657 static int advk_pcie_irq_map(struct irq_domain *h,
658 unsigned int virq, irq_hw_number_t hwirq)
660 struct advk_pcie *pcie = h->host_data;
662 advk_pcie_irq_mask(irq_get_irq_data(virq));
663 irq_set_status_flags(virq, IRQ_LEVEL);
664 irq_set_chip_and_handler(virq, &pcie->irq_chip,
666 irq_set_chip_data(virq, pcie);
671 static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
672 .map = advk_pcie_irq_map,
673 .xlate = irq_domain_xlate_onecell,
676 static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
678 struct device *dev = &pcie->pdev->dev;
679 struct device_node *node = dev->of_node;
680 struct irq_chip *msi_irq_chip;
681 struct msi_controller *msi;
682 phys_addr_t msi_msg_phys;
685 msi_irq_chip = &pcie->msi_irq_chip;
687 msi_irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-msi",
689 if (!msi_irq_chip->name)
692 msi_irq_chip->irq_enable = pci_msi_unmask_irq;
693 msi_irq_chip->irq_disable = pci_msi_mask_irq;
694 msi_irq_chip->irq_mask = pci_msi_mask_irq;
695 msi_irq_chip->irq_unmask = pci_msi_unmask_irq;
699 msi->setup_irq = advk_pcie_setup_msi_irq;
700 msi->teardown_irq = advk_pcie_teardown_msi_irq;
703 mutex_init(&pcie->msi_used_lock);
705 msi_msg_phys = virt_to_phys(&pcie->msi_msg);
707 advk_writel(pcie, lower_32_bits(msi_msg_phys),
708 PCIE_MSI_ADDR_LOW_REG);
709 advk_writel(pcie, upper_32_bits(msi_msg_phys),
710 PCIE_MSI_ADDR_HIGH_REG);
713 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
714 &advk_pcie_msi_irq_ops, pcie);
715 if (!pcie->msi_domain)
718 ret = of_pci_msi_chip_add(msi);
720 irq_domain_remove(pcie->msi_domain);
727 static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
729 of_pci_msi_chip_remove(&pcie->msi);
730 irq_domain_remove(pcie->msi_domain);
733 static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
735 struct device *dev = &pcie->pdev->dev;
736 struct device_node *node = dev->of_node;
737 struct device_node *pcie_intc_node;
738 struct irq_chip *irq_chip;
740 pcie_intc_node = of_get_next_child(node, NULL);
741 if (!pcie_intc_node) {
742 dev_err(dev, "No PCIe Intc node found\n");
746 irq_chip = &pcie->irq_chip;
748 irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
750 if (!irq_chip->name) {
751 of_node_put(pcie_intc_node);
755 irq_chip->irq_mask = advk_pcie_irq_mask;
756 irq_chip->irq_mask_ack = advk_pcie_irq_mask;
757 irq_chip->irq_unmask = advk_pcie_irq_unmask;
760 irq_domain_add_linear(pcie_intc_node, LEGACY_IRQ_NUM,
761 &advk_pcie_irq_domain_ops, pcie);
762 if (!pcie->irq_domain) {
763 dev_err(dev, "Failed to get a INTx IRQ domain\n");
764 of_node_put(pcie_intc_node);
771 static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
773 irq_domain_remove(pcie->irq_domain);
776 static void advk_pcie_handle_msi(struct advk_pcie *pcie)
778 u32 msi_val, msi_mask, msi_status, msi_idx;
781 msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
782 msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
783 msi_status = msi_val & ~msi_mask;
785 for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
786 if (!(BIT(msi_idx) & msi_status))
790 * msi_idx contains bits [4:0] of the msi_data and msi_data
791 * contains 16bit MSI interrupt number
793 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
794 msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & PCIE_MSI_DATA_MASK;
795 generic_handle_irq(msi_data);
798 advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
802 static void advk_pcie_handle_int(struct advk_pcie *pcie)
804 u32 val, mask, status;
807 val = advk_readl(pcie, PCIE_ISR0_REG);
808 mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
809 status = val & ((~mask) & PCIE_ISR0_ALL_MASK);
812 advk_writel(pcie, val, PCIE_ISR0_REG);
816 /* Process MSI interrupts */
817 if (status & PCIE_ISR0_MSI_INT_PENDING)
818 advk_pcie_handle_msi(pcie);
820 /* Process legacy interrupts */
821 for (i = 0; i < LEGACY_IRQ_NUM; i++) {
822 if (!(status & PCIE_ISR0_INTX_ASSERT(i)))
825 advk_writel(pcie, PCIE_ISR0_INTX_ASSERT(i),
828 virq = irq_find_mapping(pcie->irq_domain, i);
829 generic_handle_irq(virq);
833 static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
835 struct advk_pcie *pcie = arg;
838 status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
839 if (!(status & PCIE_IRQ_CORE_INT))
842 advk_pcie_handle_int(pcie);
844 /* Clear interrupt */
845 advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
850 static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie *pcie)
852 int err, res_valid = 0;
853 struct device *dev = &pcie->pdev->dev;
854 struct device_node *np = dev->of_node;
855 struct resource_entry *win, *tmp;
856 resource_size_t iobase;
858 INIT_LIST_HEAD(&pcie->resources);
860 err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pcie->resources,
865 err = devm_request_pci_bus_resources(dev, &pcie->resources);
867 goto out_release_res;
869 resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
870 struct resource *res = win->res;
872 switch (resource_type(res)) {
874 advk_pcie_set_ob_win(pcie, 1,
875 upper_32_bits(res->start),
876 lower_32_bits(res->start),
878 lower_32_bits(res->start),
880 err = pci_remap_iospace(res, iobase);
882 dev_warn(dev, "error %d: failed to map resource %pR\n",
884 resource_list_destroy_entry(win);
888 advk_pcie_set_ob_win(pcie, 0,
889 upper_32_bits(res->start),
890 lower_32_bits(res->start),
892 lower_32_bits(res->start),
893 (2 << 20) | OB_PCIE_MEM);
894 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
897 pcie->root_bus_nr = res->start;
903 dev_err(dev, "non-prefetchable memory resource required\n");
905 goto out_release_res;
911 pci_free_resource_list(&pcie->resources);
915 static int advk_pcie_probe(struct platform_device *pdev)
917 struct device *dev = &pdev->dev;
918 struct advk_pcie *pcie;
919 struct resource *res;
920 struct pci_bus *bus, *child;
921 struct msi_controller *msi;
922 struct device_node *msi_node;
925 pcie = devm_kzalloc(dev, sizeof(struct advk_pcie), GFP_KERNEL);
931 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
932 pcie->base = devm_ioremap_resource(dev, res);
933 if (IS_ERR(pcie->base))
934 return PTR_ERR(pcie->base);
936 irq = platform_get_irq(pdev, 0);
937 ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
938 IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
941 dev_err(dev, "Failed to register interrupt\n");
945 ret = advk_pcie_parse_request_of_pci_ranges(pcie);
947 dev_err(dev, "Failed to parse resources\n");
951 advk_pcie_setup_hw(pcie);
953 ret = advk_pcie_init_irq_domain(pcie);
955 dev_err(dev, "Failed to initialize irq\n");
959 ret = advk_pcie_init_msi_irq_domain(pcie);
961 dev_err(dev, "Failed to initialize irq\n");
962 advk_pcie_remove_irq_domain(pcie);
966 msi_node = of_parse_phandle(dev->of_node, "msi-parent", 0);
968 msi = of_pci_find_msi_chip_by_node(msi_node);
972 bus = pci_scan_root_bus_msi(dev, 0, &advk_pcie_ops,
973 pcie, &pcie->resources, &pcie->msi);
975 advk_pcie_remove_msi_irq_domain(pcie);
976 advk_pcie_remove_irq_domain(pcie);
980 pci_bus_size_bridges(bus);
981 pci_bus_assign_resources(bus);
983 list_for_each_entry(child, &bus->children, node)
984 pcie_bus_configure_settings(child);
986 pci_bus_add_devices(bus);
990 static const struct of_device_id advk_pcie_of_match_table[] = {
991 { .compatible = "marvell,armada-3700-pcie", },
995 static struct platform_driver advk_pcie_driver = {
998 .of_match_table = advk_pcie_of_match_table,
999 /* Driver unloading/unbinding currently not supported */
1000 .suppress_bind_attrs = true,
1002 .probe = advk_pcie_probe,
1004 builtin_platform_driver(advk_pcie_driver);