blob: 0538348ed843f483479e017fed77fd0ff270de50 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Driver for the Aardvark PCIe controller, used on Marvell Armada
4 * 3700.
5 *
6 * Copyright (C) 2016 Marvell
7 *
8 * Author: Hezi Shahmoon <hezi.shahmoon@marvell.com>
9 */
10
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>
21
22#include "../pci.h"
David Brazdil0f672f62019-12-10 10:32:29 +000023#include "../pci-bridge-emul.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024
25/* PCIe core registers */
David Brazdil0f672f62019-12-10 10:32:29 +000026#define PCIE_CORE_DEV_ID_REG 0x0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027#define PCIE_CORE_CMD_STATUS_REG 0x4
28#define PCIE_CORE_CMD_IO_ACCESS_EN BIT(0)
29#define PCIE_CORE_CMD_MEM_ACCESS_EN BIT(1)
30#define PCIE_CORE_CMD_MEM_IO_REQ_EN BIT(2)
David Brazdil0f672f62019-12-10 10:32:29 +000031#define PCIE_CORE_DEV_REV_REG 0x8
32#define PCIE_CORE_PCIEXP_CAP 0xc0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000033#define PCIE_CORE_DEV_CTRL_STATS_REG 0xc8
34#define PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE (0 << 4)
35#define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT 5
36#define PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE (0 << 11)
37#define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT 12
38#define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ 0x2
39#define PCIE_CORE_LINK_CTRL_STAT_REG 0xd0
40#define PCIE_CORE_LINK_L0S_ENTRY BIT(0)
41#define PCIE_CORE_LINK_TRAINING BIT(5)
42#define PCIE_CORE_LINK_WIDTH_SHIFT 20
43#define PCIE_CORE_ERR_CAPCTL_REG 0x118
44#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
45#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
46#define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7)
47#define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8)
David Brazdil0f672f62019-12-10 10:32:29 +000048#define PCIE_CORE_INT_A_ASSERT_ENABLE 1
49#define PCIE_CORE_INT_B_ASSERT_ENABLE 2
50#define PCIE_CORE_INT_C_ASSERT_ENABLE 3
51#define PCIE_CORE_INT_D_ASSERT_ENABLE 4
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000052/* PIO registers base address and register offsets */
53#define PIO_BASE_ADDR 0x4000
54#define PIO_CTRL (PIO_BASE_ADDR + 0x0)
55#define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
56#define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
57#define PIO_STAT (PIO_BASE_ADDR + 0x4)
58#define PIO_COMPLETION_STATUS_SHIFT 7
59#define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
60#define PIO_COMPLETION_STATUS_OK 0
61#define PIO_COMPLETION_STATUS_UR 1
62#define PIO_COMPLETION_STATUS_CRS 2
63#define PIO_COMPLETION_STATUS_CA 4
Olivier Deprez0e641232021-09-23 10:07:05 +020064#define PIO_NON_POSTED_REQ BIT(10)
65#define PIO_ERR_STATUS BIT(11)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066#define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
67#define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
68#define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
69#define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
70#define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
71#define PIO_START (PIO_BASE_ADDR + 0x1c)
72#define PIO_ISR (PIO_BASE_ADDR + 0x20)
73#define PIO_ISRM (PIO_BASE_ADDR + 0x24)
74
75/* Aardvark Control registers */
76#define CONTROL_BASE_ADDR 0x4800
77#define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
78#define PCIE_GEN_SEL_MSK 0x3
79#define PCIE_GEN_SEL_SHIFT 0x0
80#define SPEED_GEN_1 0
81#define SPEED_GEN_2 1
82#define SPEED_GEN_3 2
83#define IS_RC_MSK 1
84#define IS_RC_SHIFT 2
85#define LANE_CNT_MSK 0x18
86#define LANE_CNT_SHIFT 0x3
87#define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
88#define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
89#define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
90#define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
91#define LINK_TRAINING_EN BIT(6)
92#define LEGACY_INTA BIT(28)
93#define LEGACY_INTB BIT(29)
94#define LEGACY_INTC BIT(30)
95#define LEGACY_INTD BIT(31)
96#define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4)
97#define HOT_RESET_GEN BIT(0)
98#define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
99#define PCIE_CORE_CTRL2_RESERVED 0x7
100#define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
101#define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
102#define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6)
103#define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10)
David Brazdil0f672f62019-12-10 10:32:29 +0000104#define PCIE_MSG_LOG_REG (CONTROL_BASE_ADDR + 0x30)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105#define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
David Brazdil0f672f62019-12-10 10:32:29 +0000106#define PCIE_MSG_PM_PME_MASK BIT(7)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000107#define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44)
108#define PCIE_ISR0_MSI_INT_PENDING BIT(24)
109#define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val))
110#define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val))
111#define PCIE_ISR0_ALL_MASK GENMASK(26, 0)
112#define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48)
113#define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C)
114#define PCIE_ISR1_POWER_STATE_CHANGE BIT(4)
115#define PCIE_ISR1_FLUSH BIT(5)
116#define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val))
117#define PCIE_ISR1_ALL_MASK GENMASK(11, 4)
118#define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50)
119#define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54)
120#define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58)
121#define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C)
122#define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C)
123
124/* LMI registers base address and register offsets */
125#define LMI_BASE_ADDR 0x6000
126#define CFG_REG (LMI_BASE_ADDR + 0x0)
127#define LTSSM_SHIFT 24
128#define LTSSM_MASK 0x3f
129#define LTSSM_L0 0x10
130#define RC_BAR_CONFIG 0x300
Olivier Deprez0e641232021-09-23 10:07:05 +0200131#define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132
133/* PCIe core controller registers */
134#define CTRL_CORE_BASE_ADDR 0x18000
135#define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
136#define CTRL_MODE_SHIFT 0x0
137#define CTRL_MODE_MASK 0x1
138#define PCIE_CORE_MODE_DIRECT 0x0
139#define PCIE_CORE_MODE_COMMAND 0x1
140
141/* PCIe Central Interrupts Registers */
142#define CENTRAL_INT_BASE_ADDR 0x1b000
143#define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
144#define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
145#define PCIE_IRQ_CMDQ_INT BIT(0)
146#define PCIE_IRQ_MSI_STATUS_INT BIT(1)
147#define PCIE_IRQ_CMD_SENT_DONE BIT(3)
148#define PCIE_IRQ_DMA_INT BIT(4)
149#define PCIE_IRQ_IB_DXFERDONE BIT(5)
150#define PCIE_IRQ_OB_DXFERDONE BIT(6)
151#define PCIE_IRQ_OB_RXFERDONE BIT(7)
152#define PCIE_IRQ_COMPQ_INT BIT(12)
153#define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
154#define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
155#define PCIE_IRQ_CORE_INT BIT(16)
156#define PCIE_IRQ_CORE_INT_PIO BIT(17)
157#define PCIE_IRQ_DPMU_INT BIT(18)
158#define PCIE_IRQ_PCIE_MIS_INT BIT(19)
159#define PCIE_IRQ_MSI_INT1_DET BIT(20)
160#define PCIE_IRQ_MSI_INT2_DET BIT(21)
161#define PCIE_IRQ_RC_DBELL_DET BIT(22)
162#define PCIE_IRQ_EP_STATUS BIT(23)
163#define PCIE_IRQ_ALL_MASK 0xfff0fb
164#define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
165
166/* Transaction types */
167#define PCIE_CONFIG_RD_TYPE0 0x8
168#define PCIE_CONFIG_RD_TYPE1 0x9
169#define PCIE_CONFIG_WR_TYPE0 0xa
170#define PCIE_CONFIG_WR_TYPE1 0xb
171
172#define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
173#define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
174#define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
175#define PCIE_CONF_REG(reg) ((reg) & 0xffc)
176#define PCIE_CONF_ADDR(bus, devfn, where) \
177 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
178 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
179
Olivier Deprez0e641232021-09-23 10:07:05 +0200180#define PIO_RETRY_CNT 750000 /* 1.5 s */
181#define PIO_RETRY_DELAY 2 /* 2 us*/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000182
183#define LINK_WAIT_MAX_RETRIES 10
184#define LINK_WAIT_USLEEP_MIN 90000
185#define LINK_WAIT_USLEEP_MAX 100000
Olivier Deprez0e641232021-09-23 10:07:05 +0200186#define RETRAIN_WAIT_MAX_RETRIES 10
187#define RETRAIN_WAIT_USLEEP_US 2000
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000188
189#define MSI_IRQ_NUM 32
190
191struct advk_pcie {
192 struct platform_device *pdev;
193 void __iomem *base;
194 struct list_head resources;
195 struct irq_domain *irq_domain;
196 struct irq_chip irq_chip;
Olivier Deprez0e641232021-09-23 10:07:05 +0200197 raw_spinlock_t irq_lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000198 struct irq_domain *msi_domain;
199 struct irq_domain *msi_inner_domain;
200 struct irq_chip msi_bottom_irq_chip;
201 struct irq_chip msi_irq_chip;
202 struct msi_domain_info msi_domain_info;
203 DECLARE_BITMAP(msi_used, MSI_IRQ_NUM);
204 struct mutex msi_used_lock;
205 u16 msi_msg;
206 int root_bus_nr;
David Brazdil0f672f62019-12-10 10:32:29 +0000207 struct pci_bridge_emul bridge;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000208};
209
210static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
211{
212 writel(val, pcie->base + reg);
213}
214
215static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
216{
217 return readl(pcie->base + reg);
218}
219
220static int advk_pcie_link_up(struct advk_pcie *pcie)
221{
222 u32 val, ltssm_state;
223
224 val = advk_readl(pcie, CFG_REG);
225 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
226 return ltssm_state >= LTSSM_L0;
227}
228
229static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
230{
231 struct device *dev = &pcie->pdev->dev;
232 int retries;
233
234 /* check if the link is up or not */
235 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
236 if (advk_pcie_link_up(pcie)) {
237 dev_info(dev, "link up\n");
238 return 0;
239 }
240
241 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
242 }
243
244 dev_err(dev, "link never came up\n");
245 return -ETIMEDOUT;
246}
247
Olivier Deprez0e641232021-09-23 10:07:05 +0200248static void advk_pcie_wait_for_retrain(struct advk_pcie *pcie)
249{
250 size_t retries;
251
252 for (retries = 0; retries < RETRAIN_WAIT_MAX_RETRIES; ++retries) {
253 if (!advk_pcie_link_up(pcie))
254 break;
255 udelay(RETRAIN_WAIT_USLEEP_US);
256 }
257}
258
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000259static void advk_pcie_setup_hw(struct advk_pcie *pcie)
260{
261 u32 reg;
262
263 /* Set to Direct mode */
264 reg = advk_readl(pcie, CTRL_CONFIG_REG);
265 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
266 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
267 advk_writel(pcie, reg, CTRL_CONFIG_REG);
268
269 /* Set PCI global control register to RC mode */
270 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
271 reg |= (IS_RC_MSK << IS_RC_SHIFT);
272 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
273
Olivier Deprez0e641232021-09-23 10:07:05 +0200274 /*
275 * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab.
276 * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor
277 * id in high 16 bits. Updating this register changes readback value of
278 * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround
279 * for erratum 4.1: "The value of device and vendor ID is incorrect".
280 */
281 reg = (PCI_VENDOR_ID_MARVELL << 16) | PCI_VENDOR_ID_MARVELL;
282 advk_writel(pcie, reg, VENDOR_ID_REG);
283
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000284 /* Set Advanced Error Capabilities and Control PF0 register */
285 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
286 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
287 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
288 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
289 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
290
291 /* Set PCIe Device Control and Status 1 PF0 register */
292 reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
293 (7 << PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT) |
294 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE |
295 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ <<
296 PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT);
297 advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
298
299 /* Program PCIe Control 2 to disable strict ordering */
300 reg = PCIE_CORE_CTRL2_RESERVED |
301 PCIE_CORE_CTRL2_TD_ENABLE;
302 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
303
304 /* Set GEN2 */
305 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
306 reg &= ~PCIE_GEN_SEL_MSK;
307 reg |= SPEED_GEN_2;
308 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
309
310 /* Set lane X1 */
311 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
312 reg &= ~LANE_CNT_MSK;
313 reg |= LANE_COUNT_1;
314 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
315
316 /* Enable link training */
317 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
318 reg |= LINK_TRAINING_EN;
319 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
320
321 /* Enable MSI */
322 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
323 reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
324 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
325
326 /* Clear all interrupts */
327 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
328 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
329 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
330
331 /* Disable All ISR0/1 Sources */
332 reg = PCIE_ISR0_ALL_MASK;
333 reg &= ~PCIE_ISR0_MSI_INT_PENDING;
334 advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
335
336 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
337
David Brazdil0f672f62019-12-10 10:32:29 +0000338 /* Unmask all MSIs */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000339 advk_writel(pcie, 0, PCIE_MSI_MASK_REG);
340
341 /* Enable summary interrupt for GIC SPI source */
342 reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
343 advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
344
345 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
346 reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
347 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
348
349 /* Bypass the address window mapping for PIO */
350 reg = advk_readl(pcie, PIO_CTRL);
351 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
352 advk_writel(pcie, reg, PIO_CTRL);
353
354 /* Start link training */
355 reg = advk_readl(pcie, PCIE_CORE_LINK_CTRL_STAT_REG);
356 reg |= PCIE_CORE_LINK_TRAINING;
357 advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
358
359 advk_pcie_wait_for_link(pcie);
360
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000361 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
362 reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
363 PCIE_CORE_CMD_IO_ACCESS_EN |
364 PCIE_CORE_CMD_MEM_IO_REQ_EN;
365 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
366}
367
Olivier Deprez0e641232021-09-23 10:07:05 +0200368static int advk_pcie_check_pio_status(struct advk_pcie *pcie, u32 *val)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000369{
370 struct device *dev = &pcie->pdev->dev;
371 u32 reg;
372 unsigned int status;
373 char *strcomp_status, *str_posted;
374
375 reg = advk_readl(pcie, PIO_STAT);
376 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
377 PIO_COMPLETION_STATUS_SHIFT;
378
Olivier Deprez0e641232021-09-23 10:07:05 +0200379 /*
380 * According to HW spec, the PIO status check sequence as below:
381 * 1) even if COMPLETION_STATUS(bit9:7) indicates successful,
382 * it still needs to check Error Status(bit11), only when this bit
383 * indicates no error happen, the operation is successful.
384 * 2) value Unsupported Request(1) of COMPLETION_STATUS(bit9:7) only
385 * means a PIO write error, and for PIO read it is successful with
386 * a read value of 0xFFFFFFFF.
387 * 3) value Completion Retry Status(CRS) of COMPLETION_STATUS(bit9:7)
388 * only means a PIO write error, and for PIO read it is successful
389 * with a read value of 0xFFFF0001.
390 * 4) value Completer Abort (CA) of COMPLETION_STATUS(bit9:7) means
391 * error for both PIO read and PIO write operation.
392 * 5) other errors are indicated as 'unknown'.
393 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000394 switch (status) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200395 case PIO_COMPLETION_STATUS_OK:
396 if (reg & PIO_ERR_STATUS) {
397 strcomp_status = "COMP_ERR";
398 break;
399 }
400 /* Get the read result */
401 if (val)
402 *val = advk_readl(pcie, PIO_RD_DATA);
403 /* No error */
404 strcomp_status = NULL;
405 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000406 case PIO_COMPLETION_STATUS_UR:
407 strcomp_status = "UR";
408 break;
409 case PIO_COMPLETION_STATUS_CRS:
Olivier Deprez0e641232021-09-23 10:07:05 +0200410 /* PCIe r4.0, sec 2.3.2, says:
411 * If CRS Software Visibility is not enabled, the Root Complex
412 * must re-issue the Configuration Request as a new Request.
413 * A Root Complex implementation may choose to limit the number
414 * of Configuration Request/CRS Completion Status loops before
415 * determining that something is wrong with the target of the
416 * Request and taking appropriate action, e.g., complete the
417 * Request to the host as a failed transaction.
418 *
419 * To simplify implementation do not re-issue the Configuration
420 * Request and complete the Request as a failed transaction.
421 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000422 strcomp_status = "CRS";
423 break;
424 case PIO_COMPLETION_STATUS_CA:
425 strcomp_status = "CA";
426 break;
427 default:
428 strcomp_status = "Unknown";
429 break;
430 }
431
Olivier Deprez0e641232021-09-23 10:07:05 +0200432 if (!strcomp_status)
433 return 0;
434
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000435 if (reg & PIO_NON_POSTED_REQ)
436 str_posted = "Non-posted";
437 else
438 str_posted = "Posted";
439
440 dev_err(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
441 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
Olivier Deprez0e641232021-09-23 10:07:05 +0200442
443 return -EFAULT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000444}
445
446static int advk_pcie_wait_pio(struct advk_pcie *pcie)
447{
448 struct device *dev = &pcie->pdev->dev;
Olivier Deprez0e641232021-09-23 10:07:05 +0200449 int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000450
Olivier Deprez0e641232021-09-23 10:07:05 +0200451 for (i = 0; i < PIO_RETRY_CNT; i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000452 u32 start, isr;
453
454 start = advk_readl(pcie, PIO_START);
455 isr = advk_readl(pcie, PIO_ISR);
456 if (!start && isr)
457 return 0;
Olivier Deprez0e641232021-09-23 10:07:05 +0200458 udelay(PIO_RETRY_DELAY);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000459 }
460
Olivier Deprez0e641232021-09-23 10:07:05 +0200461 dev_err(dev, "PIO read/write transfer time out\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000462 return -ETIMEDOUT;
463}
464
David Brazdil0f672f62019-12-10 10:32:29 +0000465
466static pci_bridge_emul_read_status_t
467advk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
468 int reg, u32 *value)
469{
470 struct advk_pcie *pcie = bridge->data;
471
472
473 switch (reg) {
474 case PCI_EXP_SLTCTL:
475 *value = PCI_EXP_SLTSTA_PDS << 16;
476 return PCI_BRIDGE_EMUL_HANDLED;
477
478 case PCI_EXP_RTCTL: {
479 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG);
Olivier Deprez0e641232021-09-23 10:07:05 +0200480 *value = (val & PCIE_MSG_PM_PME_MASK) ? 0 : PCI_EXP_RTCTL_PMEIE;
David Brazdil0f672f62019-12-10 10:32:29 +0000481 return PCI_BRIDGE_EMUL_HANDLED;
482 }
483
484 case PCI_EXP_RTSTA: {
485 u32 isr0 = advk_readl(pcie, PCIE_ISR0_REG);
486 u32 msglog = advk_readl(pcie, PCIE_MSG_LOG_REG);
487 *value = (isr0 & PCIE_MSG_PM_PME_MASK) << 16 | (msglog >> 16);
488 return PCI_BRIDGE_EMUL_HANDLED;
489 }
490
Olivier Deprez0e641232021-09-23 10:07:05 +0200491 case PCI_EXP_LNKCTL: {
492 /* u32 contains both PCI_EXP_LNKCTL and PCI_EXP_LNKSTA */
493 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg) &
494 ~(PCI_EXP_LNKSTA_LT << 16);
495 if (!advk_pcie_link_up(pcie))
496 val |= (PCI_EXP_LNKSTA_LT << 16);
497 *value = val;
498 return PCI_BRIDGE_EMUL_HANDLED;
499 }
500
David Brazdil0f672f62019-12-10 10:32:29 +0000501 case PCI_CAP_LIST_ID:
502 case PCI_EXP_DEVCAP:
503 case PCI_EXP_DEVCTL:
504 case PCI_EXP_LNKCAP:
David Brazdil0f672f62019-12-10 10:32:29 +0000505 *value = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg);
506 return PCI_BRIDGE_EMUL_HANDLED;
507 default:
508 return PCI_BRIDGE_EMUL_NOT_HANDLED;
509 }
510
511}
512
513static void
514advk_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge,
515 int reg, u32 old, u32 new, u32 mask)
516{
517 struct advk_pcie *pcie = bridge->data;
518
519 switch (reg) {
520 case PCI_EXP_DEVCTL:
David Brazdil0f672f62019-12-10 10:32:29 +0000521 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
522 break;
523
Olivier Deprez0e641232021-09-23 10:07:05 +0200524 case PCI_EXP_LNKCTL:
525 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
526 if (new & PCI_EXP_LNKCTL_RL)
527 advk_pcie_wait_for_retrain(pcie);
David Brazdil0f672f62019-12-10 10:32:29 +0000528 break;
529
Olivier Deprez0e641232021-09-23 10:07:05 +0200530 case PCI_EXP_RTCTL: {
531 /* Only mask/unmask PME interrupt */
532 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG) &
533 ~PCIE_MSG_PM_PME_MASK;
534 if ((new & PCI_EXP_RTCTL_PMEIE) == 0)
535 val |= PCIE_MSG_PM_PME_MASK;
536 advk_writel(pcie, val, PCIE_ISR0_MASK_REG);
537 break;
538 }
539
David Brazdil0f672f62019-12-10 10:32:29 +0000540 case PCI_EXP_RTSTA:
541 new = (new & PCI_EXP_RTSTA_PME) >> 9;
542 advk_writel(pcie, new, PCIE_ISR0_REG);
543 break;
544
545 default:
546 break;
547 }
548}
549
550static struct pci_bridge_emul_ops advk_pci_bridge_emul_ops = {
551 .read_pcie = advk_pci_bridge_emul_pcie_conf_read,
552 .write_pcie = advk_pci_bridge_emul_pcie_conf_write,
553};
554
555/*
556 * Initialize the configuration space of the PCI-to-PCI bridge
557 * associated with the given PCIe interface.
558 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200559static int advk_sw_pci_bridge_init(struct advk_pcie *pcie)
David Brazdil0f672f62019-12-10 10:32:29 +0000560{
561 struct pci_bridge_emul *bridge = &pcie->bridge;
562
563 bridge->conf.vendor = advk_readl(pcie, PCIE_CORE_DEV_ID_REG) & 0xffff;
564 bridge->conf.device = advk_readl(pcie, PCIE_CORE_DEV_ID_REG) >> 16;
565 bridge->conf.class_revision =
566 advk_readl(pcie, PCIE_CORE_DEV_REV_REG) & 0xff;
567
568 /* Support 32 bits I/O addressing */
569 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32;
570 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32;
571
572 /* Support 64 bits memory pref */
573 bridge->conf.pref_mem_base = PCI_PREF_RANGE_TYPE_64;
574 bridge->conf.pref_mem_limit = PCI_PREF_RANGE_TYPE_64;
575
576 /* Support interrupt A for MSI feature */
577 bridge->conf.intpin = PCIE_CORE_INT_A_ASSERT_ENABLE;
578
579 bridge->has_pcie = true;
580 bridge->data = pcie;
581 bridge->ops = &advk_pci_bridge_emul_ops;
582
Olivier Deprez0e641232021-09-23 10:07:05 +0200583 return pci_bridge_emul_init(bridge, 0);
David Brazdil0f672f62019-12-10 10:32:29 +0000584}
585
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000586static bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus,
587 int devfn)
588{
589 if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0)
590 return false;
591
592 return true;
593}
594
Olivier Deprez0e641232021-09-23 10:07:05 +0200595static bool advk_pcie_pio_is_running(struct advk_pcie *pcie)
596{
597 struct device *dev = &pcie->pdev->dev;
598
599 /*
600 * Trying to start a new PIO transfer when previous has not completed
601 * cause External Abort on CPU which results in kernel panic:
602 *
603 * SError Interrupt on CPU0, code 0xbf000002 -- SError
604 * Kernel panic - not syncing: Asynchronous SError Interrupt
605 *
606 * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected
607 * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent
608 * concurrent calls at the same time. But because PIO transfer may take
609 * about 1.5s when link is down or card is disconnected, it means that
610 * advk_pcie_wait_pio() does not always have to wait for completion.
611 *
612 * Some versions of ARM Trusted Firmware handles this External Abort at
613 * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit:
614 * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50
615 */
616 if (advk_readl(pcie, PIO_START)) {
617 dev_err(dev, "Previous PIO read/write transfer is still running\n");
618 return true;
619 }
620
621 return false;
622}
623
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000624static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
625 int where, int size, u32 *val)
626{
627 struct advk_pcie *pcie = bus->sysdata;
628 u32 reg;
629 int ret;
630
631 if (!advk_pcie_valid_device(pcie, bus, devfn)) {
632 *val = 0xffffffff;
633 return PCIBIOS_DEVICE_NOT_FOUND;
634 }
635
David Brazdil0f672f62019-12-10 10:32:29 +0000636 if (bus->number == pcie->root_bus_nr)
637 return pci_bridge_emul_conf_read(&pcie->bridge, where,
638 size, val);
639
Olivier Deprez0e641232021-09-23 10:07:05 +0200640 if (advk_pcie_pio_is_running(pcie)) {
641 *val = 0xffffffff;
642 return PCIBIOS_SET_FAILED;
643 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000644
645 /* Program the control register */
646 reg = advk_readl(pcie, PIO_CTRL);
647 reg &= ~PIO_CTRL_TYPE_MASK;
David Brazdil0f672f62019-12-10 10:32:29 +0000648 if (bus->primary == pcie->root_bus_nr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000649 reg |= PCIE_CONFIG_RD_TYPE0;
650 else
651 reg |= PCIE_CONFIG_RD_TYPE1;
652 advk_writel(pcie, reg, PIO_CTRL);
653
654 /* Program the address registers */
655 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
656 advk_writel(pcie, reg, PIO_ADDR_LS);
657 advk_writel(pcie, 0, PIO_ADDR_MS);
658
659 /* Program the data strobe */
660 advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
661
Olivier Deprez0e641232021-09-23 10:07:05 +0200662 /* Clear PIO DONE ISR and start the transfer */
663 advk_writel(pcie, 1, PIO_ISR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000664 advk_writel(pcie, 1, PIO_START);
665
666 ret = advk_pcie_wait_pio(pcie);
667 if (ret < 0)
668 return PCIBIOS_SET_FAILED;
669
Olivier Deprez0e641232021-09-23 10:07:05 +0200670 /* Check PIO status and get the read result */
671 ret = advk_pcie_check_pio_status(pcie, val);
672 if (ret < 0) {
673 *val = 0xffffffff;
674 return PCIBIOS_SET_FAILED;
675 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000676
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000677 if (size == 1)
678 *val = (*val >> (8 * (where & 3))) & 0xff;
679 else if (size == 2)
680 *val = (*val >> (8 * (where & 3))) & 0xffff;
681
682 return PCIBIOS_SUCCESSFUL;
683}
684
685static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
686 int where, int size, u32 val)
687{
688 struct advk_pcie *pcie = bus->sysdata;
689 u32 reg;
690 u32 data_strobe = 0x0;
691 int offset;
692 int ret;
693
694 if (!advk_pcie_valid_device(pcie, bus, devfn))
695 return PCIBIOS_DEVICE_NOT_FOUND;
696
David Brazdil0f672f62019-12-10 10:32:29 +0000697 if (bus->number == pcie->root_bus_nr)
698 return pci_bridge_emul_conf_write(&pcie->bridge, where,
699 size, val);
700
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000701 if (where % size)
702 return PCIBIOS_SET_FAILED;
703
Olivier Deprez0e641232021-09-23 10:07:05 +0200704 if (advk_pcie_pio_is_running(pcie))
705 return PCIBIOS_SET_FAILED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000706
707 /* Program the control register */
708 reg = advk_readl(pcie, PIO_CTRL);
709 reg &= ~PIO_CTRL_TYPE_MASK;
David Brazdil0f672f62019-12-10 10:32:29 +0000710 if (bus->primary == pcie->root_bus_nr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000711 reg |= PCIE_CONFIG_WR_TYPE0;
712 else
713 reg |= PCIE_CONFIG_WR_TYPE1;
714 advk_writel(pcie, reg, PIO_CTRL);
715
716 /* Program the address registers */
717 reg = PCIE_CONF_ADDR(bus->number, devfn, where);
718 advk_writel(pcie, reg, PIO_ADDR_LS);
719 advk_writel(pcie, 0, PIO_ADDR_MS);
720
721 /* Calculate the write strobe */
722 offset = where & 0x3;
723 reg = val << (8 * offset);
724 data_strobe = GENMASK(size - 1, 0) << offset;
725
726 /* Program the data register */
727 advk_writel(pcie, reg, PIO_WR_DATA);
728
729 /* Program the data strobe */
730 advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
731
Olivier Deprez0e641232021-09-23 10:07:05 +0200732 /* Clear PIO DONE ISR and start the transfer */
733 advk_writel(pcie, 1, PIO_ISR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000734 advk_writel(pcie, 1, PIO_START);
735
736 ret = advk_pcie_wait_pio(pcie);
737 if (ret < 0)
738 return PCIBIOS_SET_FAILED;
739
Olivier Deprez0e641232021-09-23 10:07:05 +0200740 ret = advk_pcie_check_pio_status(pcie, NULL);
741 if (ret < 0)
742 return PCIBIOS_SET_FAILED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000743
744 return PCIBIOS_SUCCESSFUL;
745}
746
747static struct pci_ops advk_pcie_ops = {
748 .read = advk_pcie_rd_conf,
749 .write = advk_pcie_wr_conf,
750};
751
752static void advk_msi_irq_compose_msi_msg(struct irq_data *data,
753 struct msi_msg *msg)
754{
755 struct advk_pcie *pcie = irq_data_get_irq_chip_data(data);
756 phys_addr_t msi_msg = virt_to_phys(&pcie->msi_msg);
757
758 msg->address_lo = lower_32_bits(msi_msg);
759 msg->address_hi = upper_32_bits(msi_msg);
760 msg->data = data->irq;
761}
762
763static int advk_msi_set_affinity(struct irq_data *irq_data,
764 const struct cpumask *mask, bool force)
765{
766 return -EINVAL;
767}
768
769static int advk_msi_irq_domain_alloc(struct irq_domain *domain,
770 unsigned int virq,
771 unsigned int nr_irqs, void *args)
772{
773 struct advk_pcie *pcie = domain->host_data;
774 int hwirq, i;
775
776 mutex_lock(&pcie->msi_used_lock);
777 hwirq = bitmap_find_next_zero_area(pcie->msi_used, MSI_IRQ_NUM,
778 0, nr_irqs, 0);
779 if (hwirq >= MSI_IRQ_NUM) {
780 mutex_unlock(&pcie->msi_used_lock);
781 return -ENOSPC;
782 }
783
784 bitmap_set(pcie->msi_used, hwirq, nr_irqs);
785 mutex_unlock(&pcie->msi_used_lock);
786
787 for (i = 0; i < nr_irqs; i++)
788 irq_domain_set_info(domain, virq + i, hwirq + i,
789 &pcie->msi_bottom_irq_chip,
790 domain->host_data, handle_simple_irq,
791 NULL, NULL);
792
793 return hwirq;
794}
795
796static void advk_msi_irq_domain_free(struct irq_domain *domain,
797 unsigned int virq, unsigned int nr_irqs)
798{
799 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
800 struct advk_pcie *pcie = domain->host_data;
801
802 mutex_lock(&pcie->msi_used_lock);
803 bitmap_clear(pcie->msi_used, d->hwirq, nr_irqs);
804 mutex_unlock(&pcie->msi_used_lock);
805}
806
807static const struct irq_domain_ops advk_msi_domain_ops = {
808 .alloc = advk_msi_irq_domain_alloc,
809 .free = advk_msi_irq_domain_free,
810};
811
812static void advk_pcie_irq_mask(struct irq_data *d)
813{
814 struct advk_pcie *pcie = d->domain->host_data;
815 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Olivier Deprez0e641232021-09-23 10:07:05 +0200816 unsigned long flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000817 u32 mask;
818
Olivier Deprez0e641232021-09-23 10:07:05 +0200819 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000820 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
821 mask |= PCIE_ISR1_INTX_ASSERT(hwirq);
822 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
Olivier Deprez0e641232021-09-23 10:07:05 +0200823 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000824}
825
826static void advk_pcie_irq_unmask(struct irq_data *d)
827{
828 struct advk_pcie *pcie = d->domain->host_data;
829 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Olivier Deprez0e641232021-09-23 10:07:05 +0200830 unsigned long flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000831 u32 mask;
832
Olivier Deprez0e641232021-09-23 10:07:05 +0200833 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000834 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
835 mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq);
836 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
Olivier Deprez0e641232021-09-23 10:07:05 +0200837 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000838}
839
840static int advk_pcie_irq_map(struct irq_domain *h,
841 unsigned int virq, irq_hw_number_t hwirq)
842{
843 struct advk_pcie *pcie = h->host_data;
844
845 advk_pcie_irq_mask(irq_get_irq_data(virq));
846 irq_set_status_flags(virq, IRQ_LEVEL);
847 irq_set_chip_and_handler(virq, &pcie->irq_chip,
848 handle_level_irq);
849 irq_set_chip_data(virq, pcie);
850
851 return 0;
852}
853
854static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
855 .map = advk_pcie_irq_map,
856 .xlate = irq_domain_xlate_onecell,
857};
858
859static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
860{
861 struct device *dev = &pcie->pdev->dev;
862 struct device_node *node = dev->of_node;
863 struct irq_chip *bottom_ic, *msi_ic;
864 struct msi_domain_info *msi_di;
865 phys_addr_t msi_msg_phys;
866
867 mutex_init(&pcie->msi_used_lock);
868
869 bottom_ic = &pcie->msi_bottom_irq_chip;
870
871 bottom_ic->name = "MSI";
872 bottom_ic->irq_compose_msi_msg = advk_msi_irq_compose_msi_msg;
873 bottom_ic->irq_set_affinity = advk_msi_set_affinity;
874
875 msi_ic = &pcie->msi_irq_chip;
876 msi_ic->name = "advk-MSI";
877
878 msi_di = &pcie->msi_domain_info;
879 msi_di->flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
880 MSI_FLAG_MULTI_PCI_MSI;
881 msi_di->chip = msi_ic;
882
883 msi_msg_phys = virt_to_phys(&pcie->msi_msg);
884
885 advk_writel(pcie, lower_32_bits(msi_msg_phys),
886 PCIE_MSI_ADDR_LOW_REG);
887 advk_writel(pcie, upper_32_bits(msi_msg_phys),
888 PCIE_MSI_ADDR_HIGH_REG);
889
890 pcie->msi_inner_domain =
891 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
892 &advk_msi_domain_ops, pcie);
893 if (!pcie->msi_inner_domain)
894 return -ENOMEM;
895
896 pcie->msi_domain =
897 pci_msi_create_irq_domain(of_node_to_fwnode(node),
898 msi_di, pcie->msi_inner_domain);
899 if (!pcie->msi_domain) {
900 irq_domain_remove(pcie->msi_inner_domain);
901 return -ENOMEM;
902 }
903
904 return 0;
905}
906
907static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
908{
909 irq_domain_remove(pcie->msi_domain);
910 irq_domain_remove(pcie->msi_inner_domain);
911}
912
913static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
914{
915 struct device *dev = &pcie->pdev->dev;
916 struct device_node *node = dev->of_node;
917 struct device_node *pcie_intc_node;
918 struct irq_chip *irq_chip;
David Brazdil0f672f62019-12-10 10:32:29 +0000919 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000920
Olivier Deprez0e641232021-09-23 10:07:05 +0200921 raw_spin_lock_init(&pcie->irq_lock);
922
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000923 pcie_intc_node = of_get_next_child(node, NULL);
924 if (!pcie_intc_node) {
925 dev_err(dev, "No PCIe Intc node found\n");
926 return -ENODEV;
927 }
928
929 irq_chip = &pcie->irq_chip;
930
931 irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
932 dev_name(dev));
933 if (!irq_chip->name) {
David Brazdil0f672f62019-12-10 10:32:29 +0000934 ret = -ENOMEM;
935 goto out_put_node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000936 }
937
938 irq_chip->irq_mask = advk_pcie_irq_mask;
939 irq_chip->irq_mask_ack = advk_pcie_irq_mask;
940 irq_chip->irq_unmask = advk_pcie_irq_unmask;
941
942 pcie->irq_domain =
943 irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
944 &advk_pcie_irq_domain_ops, pcie);
945 if (!pcie->irq_domain) {
946 dev_err(dev, "Failed to get a INTx IRQ domain\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000947 ret = -ENOMEM;
948 goto out_put_node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000949 }
950
David Brazdil0f672f62019-12-10 10:32:29 +0000951out_put_node:
952 of_node_put(pcie_intc_node);
953 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000954}
955
956static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
957{
958 irq_domain_remove(pcie->irq_domain);
959}
960
961static void advk_pcie_handle_msi(struct advk_pcie *pcie)
962{
963 u32 msi_val, msi_mask, msi_status, msi_idx;
964 u16 msi_data;
965
966 msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
967 msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
968 msi_status = msi_val & ~msi_mask;
969
970 for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
971 if (!(BIT(msi_idx) & msi_status))
972 continue;
973
974 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
975 msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & 0xFF;
976 generic_handle_irq(msi_data);
977 }
978
979 advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
980 PCIE_ISR0_REG);
981}
982
983static void advk_pcie_handle_int(struct advk_pcie *pcie)
984{
985 u32 isr0_val, isr0_mask, isr0_status;
986 u32 isr1_val, isr1_mask, isr1_status;
987 int i, virq;
988
989 isr0_val = advk_readl(pcie, PCIE_ISR0_REG);
990 isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
991 isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK);
992
993 isr1_val = advk_readl(pcie, PCIE_ISR1_REG);
994 isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
995 isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK);
996
997 if (!isr0_status && !isr1_status) {
998 advk_writel(pcie, isr0_val, PCIE_ISR0_REG);
999 advk_writel(pcie, isr1_val, PCIE_ISR1_REG);
1000 return;
1001 }
1002
1003 /* Process MSI interrupts */
1004 if (isr0_status & PCIE_ISR0_MSI_INT_PENDING)
1005 advk_pcie_handle_msi(pcie);
1006
1007 /* Process legacy interrupts */
1008 for (i = 0; i < PCI_NUM_INTX; i++) {
1009 if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i)))
1010 continue;
1011
1012 advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i),
1013 PCIE_ISR1_REG);
1014
1015 virq = irq_find_mapping(pcie->irq_domain, i);
1016 generic_handle_irq(virq);
1017 }
1018}
1019
1020static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
1021{
1022 struct advk_pcie *pcie = arg;
1023 u32 status;
1024
1025 status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
1026 if (!(status & PCIE_IRQ_CORE_INT))
1027 return IRQ_NONE;
1028
1029 advk_pcie_handle_int(pcie);
1030
1031 /* Clear interrupt */
1032 advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
1033
1034 return IRQ_HANDLED;
1035}
1036
1037static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie *pcie)
1038{
1039 int err, res_valid = 0;
1040 struct device *dev = &pcie->pdev->dev;
1041 struct resource_entry *win, *tmp;
1042 resource_size_t iobase;
1043
1044 INIT_LIST_HEAD(&pcie->resources);
1045
1046 err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff,
1047 &pcie->resources, &iobase);
1048 if (err)
1049 return err;
1050
1051 err = devm_request_pci_bus_resources(dev, &pcie->resources);
1052 if (err)
1053 goto out_release_res;
1054
1055 resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
1056 struct resource *res = win->res;
1057
1058 switch (resource_type(res)) {
1059 case IORESOURCE_IO:
1060 err = devm_pci_remap_iospace(dev, res, iobase);
1061 if (err) {
1062 dev_warn(dev, "error %d: failed to map resource %pR\n",
1063 err, res);
1064 resource_list_destroy_entry(win);
1065 }
1066 break;
1067 case IORESOURCE_MEM:
1068 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
1069 break;
1070 case IORESOURCE_BUS:
1071 pcie->root_bus_nr = res->start;
1072 break;
1073 }
1074 }
1075
1076 if (!res_valid) {
1077 dev_err(dev, "non-prefetchable memory resource required\n");
1078 err = -EINVAL;
1079 goto out_release_res;
1080 }
1081
1082 return 0;
1083
1084out_release_res:
1085 pci_free_resource_list(&pcie->resources);
1086 return err;
1087}
1088
1089static int advk_pcie_probe(struct platform_device *pdev)
1090{
1091 struct device *dev = &pdev->dev;
1092 struct advk_pcie *pcie;
1093 struct resource *res;
1094 struct pci_host_bridge *bridge;
1095 int ret, irq;
1096
1097 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie));
1098 if (!bridge)
1099 return -ENOMEM;
1100
1101 pcie = pci_host_bridge_priv(bridge);
1102 pcie->pdev = pdev;
1103
1104 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1105 pcie->base = devm_ioremap_resource(dev, res);
1106 if (IS_ERR(pcie->base))
1107 return PTR_ERR(pcie->base);
1108
1109 irq = platform_get_irq(pdev, 0);
1110 ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
1111 IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
1112 pcie);
1113 if (ret) {
1114 dev_err(dev, "Failed to register interrupt\n");
1115 return ret;
1116 }
1117
1118 ret = advk_pcie_parse_request_of_pci_ranges(pcie);
1119 if (ret) {
1120 dev_err(dev, "Failed to parse resources\n");
1121 return ret;
1122 }
1123
1124 advk_pcie_setup_hw(pcie);
1125
Olivier Deprez0e641232021-09-23 10:07:05 +02001126 ret = advk_sw_pci_bridge_init(pcie);
1127 if (ret) {
1128 dev_err(dev, "Failed to register emulated root PCI bridge\n");
1129 return ret;
1130 }
David Brazdil0f672f62019-12-10 10:32:29 +00001131
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001132 ret = advk_pcie_init_irq_domain(pcie);
1133 if (ret) {
1134 dev_err(dev, "Failed to initialize irq\n");
1135 return ret;
1136 }
1137
1138 ret = advk_pcie_init_msi_irq_domain(pcie);
1139 if (ret) {
1140 dev_err(dev, "Failed to initialize irq\n");
1141 advk_pcie_remove_irq_domain(pcie);
1142 return ret;
1143 }
1144
1145 list_splice_init(&pcie->resources, &bridge->windows);
1146 bridge->dev.parent = dev;
1147 bridge->sysdata = pcie;
1148 bridge->busnr = 0;
1149 bridge->ops = &advk_pcie_ops;
1150 bridge->map_irq = of_irq_parse_and_map_pci;
1151 bridge->swizzle_irq = pci_common_swizzle;
1152
1153 ret = pci_host_probe(bridge);
1154 if (ret < 0) {
1155 advk_pcie_remove_msi_irq_domain(pcie);
1156 advk_pcie_remove_irq_domain(pcie);
1157 return ret;
1158 }
1159
1160 return 0;
1161}
1162
1163static const struct of_device_id advk_pcie_of_match_table[] = {
1164 { .compatible = "marvell,armada-3700-pcie", },
1165 {},
1166};
1167
1168static struct platform_driver advk_pcie_driver = {
1169 .driver = {
1170 .name = "advk-pcie",
1171 .of_match_table = advk_pcie_of_match_table,
1172 /* Driver unloading/unbinding currently not supported */
1173 .suppress_bind_attrs = true,
1174 },
1175 .probe = advk_pcie_probe,
1176};
1177builtin_platform_driver(advk_pcie_driver);