GNU Linux-libre 4.19.211-gnu1
[releases.git] / drivers / pci / controller / pci-aardvark.c
1 // 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"
23
24 /* PCIe core registers */
25 #define PCIE_CORE_CMD_STATUS_REG                                0x4
26 #define     PCIE_CORE_CMD_IO_ACCESS_EN                          BIT(0)
27 #define     PCIE_CORE_CMD_MEM_ACCESS_EN                         BIT(1)
28 #define     PCIE_CORE_CMD_MEM_IO_REQ_EN                         BIT(2)
29 #define PCIE_CORE_DEV_CTRL_STATS_REG                            0xc8
30 #define     PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE        (0 << 4)
31 #define     PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT       5
32 #define     PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE              (0 << 11)
33 #define     PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT      12
34 #define     PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ              0x2
35 #define PCIE_CORE_LINK_CTRL_STAT_REG                            0xd0
36 #define     PCIE_CORE_LINK_L0S_ENTRY                            BIT(0)
37 #define     PCIE_CORE_LINK_TRAINING                             BIT(5)
38 #define     PCIE_CORE_LINK_WIDTH_SHIFT                          20
39 #define PCIE_CORE_ERR_CAPCTL_REG                                0x118
40 #define     PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX                    BIT(5)
41 #define     PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN                 BIT(6)
42 #define     PCIE_CORE_ERR_CAPCTL_ECRC_CHCK                      BIT(7)
43 #define     PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV                  BIT(8)
44
45 /* PIO registers base address and register offsets */
46 #define PIO_BASE_ADDR                           0x4000
47 #define PIO_CTRL                                (PIO_BASE_ADDR + 0x0)
48 #define   PIO_CTRL_TYPE_MASK                    GENMASK(3, 0)
49 #define   PIO_CTRL_ADDR_WIN_DISABLE             BIT(24)
50 #define PIO_STAT                                (PIO_BASE_ADDR + 0x4)
51 #define   PIO_COMPLETION_STATUS_SHIFT           7
52 #define   PIO_COMPLETION_STATUS_MASK            GENMASK(9, 7)
53 #define   PIO_COMPLETION_STATUS_OK              0
54 #define   PIO_COMPLETION_STATUS_UR              1
55 #define   PIO_COMPLETION_STATUS_CRS             2
56 #define   PIO_COMPLETION_STATUS_CA              4
57 #define   PIO_NON_POSTED_REQ                    BIT(10)
58 #define   PIO_ERR_STATUS                        BIT(11)
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)
67
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
73 #define     SPEED_GEN_1                         0
74 #define     SPEED_GEN_2                         1
75 #define     SPEED_GEN_3                         2
76 #define     IS_RC_MSK                           1
77 #define     IS_RC_SHIFT                         2
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_INTX_ASSERT(val)          BIT(8 + (val))
108 #define     PCIE_ISR1_ALL_MASK                  GENMASK(11, 4)
109 #define PCIE_MSI_ADDR_LOW_REG                   (CONTROL_BASE_ADDR + 0x50)
110 #define PCIE_MSI_ADDR_HIGH_REG                  (CONTROL_BASE_ADDR + 0x54)
111 #define PCIE_MSI_STATUS_REG                     (CONTROL_BASE_ADDR + 0x58)
112 #define PCIE_MSI_MASK_REG                       (CONTROL_BASE_ADDR + 0x5C)
113 #define PCIE_MSI_PAYLOAD_REG                    (CONTROL_BASE_ADDR + 0x9C)
114
115 /* LMI registers base address and register offsets */
116 #define LMI_BASE_ADDR                           0x6000
117 #define CFG_REG                                 (LMI_BASE_ADDR + 0x0)
118 #define     LTSSM_SHIFT                         24
119 #define     LTSSM_MASK                          0x3f
120 #define     LTSSM_L0                            0x10
121 #define     RC_BAR_CONFIG                       0x300
122
123 /* PCIe core controller registers */
124 #define CTRL_CORE_BASE_ADDR                     0x18000
125 #define CTRL_CONFIG_REG                         (CTRL_CORE_BASE_ADDR + 0x0)
126 #define     CTRL_MODE_SHIFT                     0x0
127 #define     CTRL_MODE_MASK                      0x1
128 #define     PCIE_CORE_MODE_DIRECT               0x0
129 #define     PCIE_CORE_MODE_COMMAND              0x1
130
131 /* PCIe Central Interrupts Registers */
132 #define CENTRAL_INT_BASE_ADDR                   0x1b000
133 #define HOST_CTRL_INT_STATUS_REG                (CENTRAL_INT_BASE_ADDR + 0x0)
134 #define HOST_CTRL_INT_MASK_REG                  (CENTRAL_INT_BASE_ADDR + 0x4)
135 #define     PCIE_IRQ_CMDQ_INT                   BIT(0)
136 #define     PCIE_IRQ_MSI_STATUS_INT             BIT(1)
137 #define     PCIE_IRQ_CMD_SENT_DONE              BIT(3)
138 #define     PCIE_IRQ_DMA_INT                    BIT(4)
139 #define     PCIE_IRQ_IB_DXFERDONE               BIT(5)
140 #define     PCIE_IRQ_OB_DXFERDONE               BIT(6)
141 #define     PCIE_IRQ_OB_RXFERDONE               BIT(7)
142 #define     PCIE_IRQ_COMPQ_INT                  BIT(12)
143 #define     PCIE_IRQ_DIR_RD_DDR_DET             BIT(13)
144 #define     PCIE_IRQ_DIR_WR_DDR_DET             BIT(14)
145 #define     PCIE_IRQ_CORE_INT                   BIT(16)
146 #define     PCIE_IRQ_CORE_INT_PIO               BIT(17)
147 #define     PCIE_IRQ_DPMU_INT                   BIT(18)
148 #define     PCIE_IRQ_PCIE_MIS_INT               BIT(19)
149 #define     PCIE_IRQ_MSI_INT1_DET               BIT(20)
150 #define     PCIE_IRQ_MSI_INT2_DET               BIT(21)
151 #define     PCIE_IRQ_RC_DBELL_DET               BIT(22)
152 #define     PCIE_IRQ_EP_STATUS                  BIT(23)
153 #define     PCIE_IRQ_ALL_MASK                   0xfff0fb
154 #define     PCIE_IRQ_ENABLE_INTS_MASK           PCIE_IRQ_CORE_INT
155
156 /* Transaction types */
157 #define PCIE_CONFIG_RD_TYPE0                    0x8
158 #define PCIE_CONFIG_RD_TYPE1                    0x9
159 #define PCIE_CONFIG_WR_TYPE0                    0xa
160 #define PCIE_CONFIG_WR_TYPE1                    0xb
161
162 #define PCIE_CONF_BUS(bus)                      (((bus) & 0xff) << 20)
163 #define PCIE_CONF_DEV(dev)                      (((dev) & 0x1f) << 15)
164 #define PCIE_CONF_FUNC(fun)                     (((fun) & 0x7)  << 12)
165 #define PCIE_CONF_REG(reg)                      ((reg) & 0xffc)
166 #define PCIE_CONF_ADDR(bus, devfn, where)       \
167         (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn))    | \
168          PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
169
170 #define PIO_RETRY_CNT                   750000 /* 1.5 s */
171 #define PIO_RETRY_DELAY                 2 /* 2 us*/
172
173 #define LINK_WAIT_MAX_RETRIES           10
174 #define LINK_WAIT_USLEEP_MIN            90000
175 #define LINK_WAIT_USLEEP_MAX            100000
176
177 #define MSI_IRQ_NUM                     32
178
179 struct advk_pcie {
180         struct platform_device *pdev;
181         void __iomem *base;
182         struct list_head resources;
183         struct irq_domain *irq_domain;
184         struct irq_chip irq_chip;
185         raw_spinlock_t irq_lock;
186         struct irq_domain *msi_domain;
187         struct irq_domain *msi_inner_domain;
188         struct irq_chip msi_bottom_irq_chip;
189         struct irq_chip msi_irq_chip;
190         struct msi_domain_info msi_domain_info;
191         DECLARE_BITMAP(msi_used, MSI_IRQ_NUM);
192         struct mutex msi_used_lock;
193         u16 msi_msg;
194         int root_bus_nr;
195 };
196
197 static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
198 {
199         writel(val, pcie->base + reg);
200 }
201
202 static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
203 {
204         return readl(pcie->base + reg);
205 }
206
207 static int advk_pcie_link_up(struct advk_pcie *pcie)
208 {
209         u32 val, ltssm_state;
210
211         val = advk_readl(pcie, CFG_REG);
212         ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
213         return ltssm_state >= LTSSM_L0;
214 }
215
216 static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
217 {
218         struct device *dev = &pcie->pdev->dev;
219         int retries;
220
221         /* check if the link is up or not */
222         for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
223                 if (advk_pcie_link_up(pcie)) {
224                         dev_info(dev, "link up\n");
225                         return 0;
226                 }
227
228                 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
229         }
230
231         dev_err(dev, "link never came up\n");
232         return -ETIMEDOUT;
233 }
234
235 static void advk_pcie_setup_hw(struct advk_pcie *pcie)
236 {
237         u32 reg;
238
239         /* Set to Direct mode */
240         reg = advk_readl(pcie, CTRL_CONFIG_REG);
241         reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
242         reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
243         advk_writel(pcie, reg, CTRL_CONFIG_REG);
244
245         /* Set PCI global control register to RC mode */
246         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
247         reg |= (IS_RC_MSK << IS_RC_SHIFT);
248         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
249
250         /* Set Advanced Error Capabilities and Control PF0 register */
251         reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
252                 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
253                 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
254                 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
255         advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
256
257         /* Set PCIe Device Control and Status 1 PF0 register */
258         reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
259                 (7 << PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT) |
260                 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE |
261                 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ <<
262                  PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT);
263         advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
264
265         /* Program PCIe Control 2 to disable strict ordering */
266         reg = PCIE_CORE_CTRL2_RESERVED |
267                 PCIE_CORE_CTRL2_TD_ENABLE;
268         advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
269
270         /* Set GEN2 */
271         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
272         reg &= ~PCIE_GEN_SEL_MSK;
273         reg |= SPEED_GEN_2;
274         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
275
276         /* Set lane X1 */
277         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
278         reg &= ~LANE_CNT_MSK;
279         reg |= LANE_COUNT_1;
280         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
281
282         /* Enable link training */
283         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
284         reg |= LINK_TRAINING_EN;
285         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
286
287         /* Enable MSI */
288         reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
289         reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
290         advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
291
292         /* Clear all interrupts */
293         advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
294         advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
295         advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
296
297         /* Disable All ISR0/1 Sources */
298         reg = PCIE_ISR0_ALL_MASK;
299         reg &= ~PCIE_ISR0_MSI_INT_PENDING;
300         advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
301
302         advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
303
304         /* Unmask all MSI's */
305         advk_writel(pcie, 0, PCIE_MSI_MASK_REG);
306
307         /* Enable summary interrupt for GIC SPI source */
308         reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
309         advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
310
311         reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
312         reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
313         advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
314
315         /* Bypass the address window mapping for PIO */
316         reg = advk_readl(pcie, PIO_CTRL);
317         reg |= PIO_CTRL_ADDR_WIN_DISABLE;
318         advk_writel(pcie, reg, PIO_CTRL);
319
320         /* Start link training */
321         reg = advk_readl(pcie, PCIE_CORE_LINK_CTRL_STAT_REG);
322         reg |= PCIE_CORE_LINK_TRAINING;
323         advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
324
325         advk_pcie_wait_for_link(pcie);
326
327         reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
328         reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
329                 PCIE_CORE_CMD_IO_ACCESS_EN |
330                 PCIE_CORE_CMD_MEM_IO_REQ_EN;
331         advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
332 }
333
334 static int advk_pcie_check_pio_status(struct advk_pcie *pcie, u32 *val)
335 {
336         struct device *dev = &pcie->pdev->dev;
337         u32 reg;
338         unsigned int status;
339         char *strcomp_status, *str_posted;
340
341         reg = advk_readl(pcie, PIO_STAT);
342         status = (reg & PIO_COMPLETION_STATUS_MASK) >>
343                 PIO_COMPLETION_STATUS_SHIFT;
344
345         /*
346          * According to HW spec, the PIO status check sequence as below:
347          * 1) even if COMPLETION_STATUS(bit9:7) indicates successful,
348          *    it still needs to check Error Status(bit11), only when this bit
349          *    indicates no error happen, the operation is successful.
350          * 2) value Unsupported Request(1) of COMPLETION_STATUS(bit9:7) only
351          *    means a PIO write error, and for PIO read it is successful with
352          *    a read value of 0xFFFFFFFF.
353          * 3) value Completion Retry Status(CRS) of COMPLETION_STATUS(bit9:7)
354          *    only means a PIO write error, and for PIO read it is successful
355          *    with a read value of 0xFFFF0001.
356          * 4) value Completer Abort (CA) of COMPLETION_STATUS(bit9:7) means
357          *    error for both PIO read and PIO write operation.
358          * 5) other errors are indicated as 'unknown'.
359          */
360         switch (status) {
361         case PIO_COMPLETION_STATUS_OK:
362                 if (reg & PIO_ERR_STATUS) {
363                         strcomp_status = "COMP_ERR";
364                         break;
365                 }
366                 /* Get the read result */
367                 if (val)
368                         *val = advk_readl(pcie, PIO_RD_DATA);
369                 /* No error */
370                 strcomp_status = NULL;
371                 break;
372         case PIO_COMPLETION_STATUS_UR:
373                 strcomp_status = "UR";
374                 break;
375         case PIO_COMPLETION_STATUS_CRS:
376                 /* PCIe r4.0, sec 2.3.2, says:
377                  * If CRS Software Visibility is not enabled, the Root Complex
378                  * must re-issue the Configuration Request as a new Request.
379                  * A Root Complex implementation may choose to limit the number
380                  * of Configuration Request/CRS Completion Status loops before
381                  * determining that something is wrong with the target of the
382                  * Request and taking appropriate action, e.g., complete the
383                  * Request to the host as a failed transaction.
384                  *
385                  * To simplify implementation do not re-issue the Configuration
386                  * Request and complete the Request as a failed transaction.
387                  */
388                 strcomp_status = "CRS";
389                 break;
390         case PIO_COMPLETION_STATUS_CA:
391                 strcomp_status = "CA";
392                 break;
393         default:
394                 strcomp_status = "Unknown";
395                 break;
396         }
397
398         if (!strcomp_status)
399                 return 0;
400
401         if (reg & PIO_NON_POSTED_REQ)
402                 str_posted = "Non-posted";
403         else
404                 str_posted = "Posted";
405
406         dev_err(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
407                 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
408
409         return -EFAULT;
410 }
411
412 static int advk_pcie_wait_pio(struct advk_pcie *pcie)
413 {
414         struct device *dev = &pcie->pdev->dev;
415         int i;
416
417         for (i = 0; i < PIO_RETRY_CNT; i++) {
418                 u32 start, isr;
419
420                 start = advk_readl(pcie, PIO_START);
421                 isr = advk_readl(pcie, PIO_ISR);
422                 if (!start && isr)
423                         return 0;
424                 udelay(PIO_RETRY_DELAY);
425         }
426
427         dev_err(dev, "PIO read/write transfer time out\n");
428         return -ETIMEDOUT;
429 }
430
431 static bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus,
432                                   int devfn)
433 {
434         if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0)
435                 return false;
436
437         return true;
438 }
439
440 static bool advk_pcie_pio_is_running(struct advk_pcie *pcie)
441 {
442         struct device *dev = &pcie->pdev->dev;
443
444         /*
445          * Trying to start a new PIO transfer when previous has not completed
446          * cause External Abort on CPU which results in kernel panic:
447          *
448          *     SError Interrupt on CPU0, code 0xbf000002 -- SError
449          *     Kernel panic - not syncing: Asynchronous SError Interrupt
450          *
451          * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected
452          * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent
453          * concurrent calls at the same time. But because PIO transfer may take
454          * about 1.5s when link is down or card is disconnected, it means that
455          * advk_pcie_wait_pio() does not always have to wait for completion.
456          *
457          * Some versions of ARM Trusted Firmware handles this External Abort at
458          * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit:
459          * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50
460          */
461         if (advk_readl(pcie, PIO_START)) {
462                 dev_err(dev, "Previous PIO read/write transfer is still running\n");
463                 return true;
464         }
465
466         return false;
467 }
468
469 static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
470                              int where, int size, u32 *val)
471 {
472         struct advk_pcie *pcie = bus->sysdata;
473         u32 reg;
474         int ret;
475
476         if (!advk_pcie_valid_device(pcie, bus, devfn)) {
477                 *val = 0xffffffff;
478                 return PCIBIOS_DEVICE_NOT_FOUND;
479         }
480
481         if (advk_pcie_pio_is_running(pcie)) {
482                 *val = 0xffffffff;
483                 return PCIBIOS_SET_FAILED;
484         }
485
486         /* Program the control register */
487         reg = advk_readl(pcie, PIO_CTRL);
488         reg &= ~PIO_CTRL_TYPE_MASK;
489         if (bus->number ==  pcie->root_bus_nr)
490                 reg |= PCIE_CONFIG_RD_TYPE0;
491         else
492                 reg |= PCIE_CONFIG_RD_TYPE1;
493         advk_writel(pcie, reg, PIO_CTRL);
494
495         /* Program the address registers */
496         reg = PCIE_CONF_ADDR(bus->number, devfn, where);
497         advk_writel(pcie, reg, PIO_ADDR_LS);
498         advk_writel(pcie, 0, PIO_ADDR_MS);
499
500         /* Program the data strobe */
501         advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
502
503         /* Clear PIO DONE ISR and start the transfer */
504         advk_writel(pcie, 1, PIO_ISR);
505         advk_writel(pcie, 1, PIO_START);
506
507         ret = advk_pcie_wait_pio(pcie);
508         if (ret < 0)
509                 return PCIBIOS_SET_FAILED;
510
511         /* Check PIO status and get the read result */
512         ret = advk_pcie_check_pio_status(pcie, val);
513         if (ret < 0) {
514                 *val = 0xffffffff;
515                 return PCIBIOS_SET_FAILED;
516         }
517
518         if (size == 1)
519                 *val = (*val >> (8 * (where & 3))) & 0xff;
520         else if (size == 2)
521                 *val = (*val >> (8 * (where & 3))) & 0xffff;
522
523         return PCIBIOS_SUCCESSFUL;
524 }
525
526 static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
527                                 int where, int size, u32 val)
528 {
529         struct advk_pcie *pcie = bus->sysdata;
530         u32 reg;
531         u32 data_strobe = 0x0;
532         int offset;
533         int ret;
534
535         if (!advk_pcie_valid_device(pcie, bus, devfn))
536                 return PCIBIOS_DEVICE_NOT_FOUND;
537
538         if (where % size)
539                 return PCIBIOS_SET_FAILED;
540
541         if (advk_pcie_pio_is_running(pcie))
542                 return PCIBIOS_SET_FAILED;
543
544         /* Program the control register */
545         reg = advk_readl(pcie, PIO_CTRL);
546         reg &= ~PIO_CTRL_TYPE_MASK;
547         if (bus->number == pcie->root_bus_nr)
548                 reg |= PCIE_CONFIG_WR_TYPE0;
549         else
550                 reg |= PCIE_CONFIG_WR_TYPE1;
551         advk_writel(pcie, reg, PIO_CTRL);
552
553         /* Program the address registers */
554         reg = PCIE_CONF_ADDR(bus->number, devfn, where);
555         advk_writel(pcie, reg, PIO_ADDR_LS);
556         advk_writel(pcie, 0, PIO_ADDR_MS);
557
558         /* Calculate the write strobe */
559         offset      = where & 0x3;
560         reg         = val << (8 * offset);
561         data_strobe = GENMASK(size - 1, 0) << offset;
562
563         /* Program the data register */
564         advk_writel(pcie, reg, PIO_WR_DATA);
565
566         /* Program the data strobe */
567         advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
568
569         /* Clear PIO DONE ISR and start the transfer */
570         advk_writel(pcie, 1, PIO_ISR);
571         advk_writel(pcie, 1, PIO_START);
572
573         ret = advk_pcie_wait_pio(pcie);
574         if (ret < 0)
575                 return PCIBIOS_SET_FAILED;
576
577         ret = advk_pcie_check_pio_status(pcie, NULL);
578         if (ret < 0)
579                 return PCIBIOS_SET_FAILED;
580
581         return PCIBIOS_SUCCESSFUL;
582 }
583
584 static struct pci_ops advk_pcie_ops = {
585         .read = advk_pcie_rd_conf,
586         .write = advk_pcie_wr_conf,
587 };
588
589 static void advk_msi_irq_compose_msi_msg(struct irq_data *data,
590                                          struct msi_msg *msg)
591 {
592         struct advk_pcie *pcie = irq_data_get_irq_chip_data(data);
593         phys_addr_t msi_msg = virt_to_phys(&pcie->msi_msg);
594
595         msg->address_lo = lower_32_bits(msi_msg);
596         msg->address_hi = upper_32_bits(msi_msg);
597         msg->data = data->irq;
598 }
599
600 static int advk_msi_set_affinity(struct irq_data *irq_data,
601                                  const struct cpumask *mask, bool force)
602 {
603         return -EINVAL;
604 }
605
606 static int advk_msi_irq_domain_alloc(struct irq_domain *domain,
607                                      unsigned int virq,
608                                      unsigned int nr_irqs, void *args)
609 {
610         struct advk_pcie *pcie = domain->host_data;
611         int hwirq, i;
612
613         mutex_lock(&pcie->msi_used_lock);
614         hwirq = bitmap_find_next_zero_area(pcie->msi_used, MSI_IRQ_NUM,
615                                            0, nr_irqs, 0);
616         if (hwirq >= MSI_IRQ_NUM) {
617                 mutex_unlock(&pcie->msi_used_lock);
618                 return -ENOSPC;
619         }
620
621         bitmap_set(pcie->msi_used, hwirq, nr_irqs);
622         mutex_unlock(&pcie->msi_used_lock);
623
624         for (i = 0; i < nr_irqs; i++)
625                 irq_domain_set_info(domain, virq + i, hwirq + i,
626                                     &pcie->msi_bottom_irq_chip,
627                                     domain->host_data, handle_simple_irq,
628                                     NULL, NULL);
629
630         return hwirq;
631 }
632
633 static void advk_msi_irq_domain_free(struct irq_domain *domain,
634                                      unsigned int virq, unsigned int nr_irqs)
635 {
636         struct irq_data *d = irq_domain_get_irq_data(domain, virq);
637         struct advk_pcie *pcie = domain->host_data;
638
639         mutex_lock(&pcie->msi_used_lock);
640         bitmap_clear(pcie->msi_used, d->hwirq, nr_irqs);
641         mutex_unlock(&pcie->msi_used_lock);
642 }
643
644 static const struct irq_domain_ops advk_msi_domain_ops = {
645         .alloc = advk_msi_irq_domain_alloc,
646         .free = advk_msi_irq_domain_free,
647 };
648
649 static void advk_pcie_irq_mask(struct irq_data *d)
650 {
651         struct advk_pcie *pcie = d->domain->host_data;
652         irq_hw_number_t hwirq = irqd_to_hwirq(d);
653         unsigned long flags;
654         u32 mask;
655
656         raw_spin_lock_irqsave(&pcie->irq_lock, flags);
657         mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
658         mask |= PCIE_ISR1_INTX_ASSERT(hwirq);
659         advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
660         raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
661 }
662
663 static void advk_pcie_irq_unmask(struct irq_data *d)
664 {
665         struct advk_pcie *pcie = d->domain->host_data;
666         irq_hw_number_t hwirq = irqd_to_hwirq(d);
667         unsigned long flags;
668         u32 mask;
669
670         raw_spin_lock_irqsave(&pcie->irq_lock, flags);
671         mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
672         mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq);
673         advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
674         raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
675 }
676
677 static int advk_pcie_irq_map(struct irq_domain *h,
678                              unsigned int virq, irq_hw_number_t hwirq)
679 {
680         struct advk_pcie *pcie = h->host_data;
681
682         advk_pcie_irq_mask(irq_get_irq_data(virq));
683         irq_set_status_flags(virq, IRQ_LEVEL);
684         irq_set_chip_and_handler(virq, &pcie->irq_chip,
685                                  handle_level_irq);
686         irq_set_chip_data(virq, pcie);
687
688         return 0;
689 }
690
691 static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
692         .map = advk_pcie_irq_map,
693         .xlate = irq_domain_xlate_onecell,
694 };
695
696 static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
697 {
698         struct device *dev = &pcie->pdev->dev;
699         struct device_node *node = dev->of_node;
700         struct irq_chip *bottom_ic, *msi_ic;
701         struct msi_domain_info *msi_di;
702         phys_addr_t msi_msg_phys;
703
704         mutex_init(&pcie->msi_used_lock);
705
706         bottom_ic = &pcie->msi_bottom_irq_chip;
707
708         bottom_ic->name = "MSI";
709         bottom_ic->irq_compose_msi_msg = advk_msi_irq_compose_msi_msg;
710         bottom_ic->irq_set_affinity = advk_msi_set_affinity;
711
712         msi_ic = &pcie->msi_irq_chip;
713         msi_ic->name = "advk-MSI";
714
715         msi_di = &pcie->msi_domain_info;
716         msi_di->flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
717                 MSI_FLAG_MULTI_PCI_MSI;
718         msi_di->chip = msi_ic;
719
720         msi_msg_phys = virt_to_phys(&pcie->msi_msg);
721
722         advk_writel(pcie, lower_32_bits(msi_msg_phys),
723                     PCIE_MSI_ADDR_LOW_REG);
724         advk_writel(pcie, upper_32_bits(msi_msg_phys),
725                     PCIE_MSI_ADDR_HIGH_REG);
726
727         pcie->msi_inner_domain =
728                 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
729                                       &advk_msi_domain_ops, pcie);
730         if (!pcie->msi_inner_domain)
731                 return -ENOMEM;
732
733         pcie->msi_domain =
734                 pci_msi_create_irq_domain(of_node_to_fwnode(node),
735                                           msi_di, pcie->msi_inner_domain);
736         if (!pcie->msi_domain) {
737                 irq_domain_remove(pcie->msi_inner_domain);
738                 return -ENOMEM;
739         }
740
741         return 0;
742 }
743
744 static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
745 {
746         irq_domain_remove(pcie->msi_domain);
747         irq_domain_remove(pcie->msi_inner_domain);
748 }
749
750 static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
751 {
752         struct device *dev = &pcie->pdev->dev;
753         struct device_node *node = dev->of_node;
754         struct device_node *pcie_intc_node;
755         struct irq_chip *irq_chip;
756
757         raw_spin_lock_init(&pcie->irq_lock);
758
759         pcie_intc_node =  of_get_next_child(node, NULL);
760         if (!pcie_intc_node) {
761                 dev_err(dev, "No PCIe Intc node found\n");
762                 return -ENODEV;
763         }
764
765         irq_chip = &pcie->irq_chip;
766
767         irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
768                                         dev_name(dev));
769         if (!irq_chip->name) {
770                 of_node_put(pcie_intc_node);
771                 return -ENOMEM;
772         }
773
774         irq_chip->irq_mask = advk_pcie_irq_mask;
775         irq_chip->irq_mask_ack = advk_pcie_irq_mask;
776         irq_chip->irq_unmask = advk_pcie_irq_unmask;
777
778         pcie->irq_domain =
779                 irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
780                                       &advk_pcie_irq_domain_ops, pcie);
781         if (!pcie->irq_domain) {
782                 dev_err(dev, "Failed to get a INTx IRQ domain\n");
783                 of_node_put(pcie_intc_node);
784                 return -ENOMEM;
785         }
786
787         return 0;
788 }
789
790 static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
791 {
792         irq_domain_remove(pcie->irq_domain);
793 }
794
795 static void advk_pcie_handle_msi(struct advk_pcie *pcie)
796 {
797         u32 msi_val, msi_mask, msi_status, msi_idx;
798         u16 msi_data;
799
800         msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
801         msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
802         msi_status = msi_val & ~msi_mask;
803
804         for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
805                 if (!(BIT(msi_idx) & msi_status))
806                         continue;
807
808                 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
809                 msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & 0xFF;
810                 generic_handle_irq(msi_data);
811         }
812
813         advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
814                     PCIE_ISR0_REG);
815 }
816
817 static void advk_pcie_handle_int(struct advk_pcie *pcie)
818 {
819         u32 isr0_val, isr0_mask, isr0_status;
820         u32 isr1_val, isr1_mask, isr1_status;
821         int i, virq;
822
823         isr0_val = advk_readl(pcie, PCIE_ISR0_REG);
824         isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
825         isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK);
826
827         isr1_val = advk_readl(pcie, PCIE_ISR1_REG);
828         isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
829         isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK);
830
831         if (!isr0_status && !isr1_status) {
832                 advk_writel(pcie, isr0_val, PCIE_ISR0_REG);
833                 advk_writel(pcie, isr1_val, PCIE_ISR1_REG);
834                 return;
835         }
836
837         /* Process MSI interrupts */
838         if (isr0_status & PCIE_ISR0_MSI_INT_PENDING)
839                 advk_pcie_handle_msi(pcie);
840
841         /* Process legacy interrupts */
842         for (i = 0; i < PCI_NUM_INTX; i++) {
843                 if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i)))
844                         continue;
845
846                 advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i),
847                             PCIE_ISR1_REG);
848
849                 virq = irq_find_mapping(pcie->irq_domain, i);
850                 generic_handle_irq(virq);
851         }
852 }
853
854 static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
855 {
856         struct advk_pcie *pcie = arg;
857         u32 status;
858
859         status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
860         if (!(status & PCIE_IRQ_CORE_INT))
861                 return IRQ_NONE;
862
863         advk_pcie_handle_int(pcie);
864
865         /* Clear interrupt */
866         advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
867
868         return IRQ_HANDLED;
869 }
870
871 static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie *pcie)
872 {
873         int err, res_valid = 0;
874         struct device *dev = &pcie->pdev->dev;
875         struct resource_entry *win, *tmp;
876         resource_size_t iobase;
877
878         INIT_LIST_HEAD(&pcie->resources);
879
880         err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff,
881                                                     &pcie->resources, &iobase);
882         if (err)
883                 return err;
884
885         err = devm_request_pci_bus_resources(dev, &pcie->resources);
886         if (err)
887                 goto out_release_res;
888
889         resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
890                 struct resource *res = win->res;
891
892                 switch (resource_type(res)) {
893                 case IORESOURCE_IO:
894                         err = devm_pci_remap_iospace(dev, res, iobase);
895                         if (err) {
896                                 dev_warn(dev, "error %d: failed to map resource %pR\n",
897                                          err, res);
898                                 resource_list_destroy_entry(win);
899                         }
900                         break;
901                 case IORESOURCE_MEM:
902                         res_valid |= !(res->flags & IORESOURCE_PREFETCH);
903                         break;
904                 case IORESOURCE_BUS:
905                         pcie->root_bus_nr = res->start;
906                         break;
907                 }
908         }
909
910         if (!res_valid) {
911                 dev_err(dev, "non-prefetchable memory resource required\n");
912                 err = -EINVAL;
913                 goto out_release_res;
914         }
915
916         return 0;
917
918 out_release_res:
919         pci_free_resource_list(&pcie->resources);
920         return err;
921 }
922
923 static int advk_pcie_probe(struct platform_device *pdev)
924 {
925         struct device *dev = &pdev->dev;
926         struct advk_pcie *pcie;
927         struct resource *res;
928         struct pci_host_bridge *bridge;
929         int ret, irq;
930
931         bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie));
932         if (!bridge)
933                 return -ENOMEM;
934
935         pcie = pci_host_bridge_priv(bridge);
936         pcie->pdev = pdev;
937
938         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
939         pcie->base = devm_ioremap_resource(dev, res);
940         if (IS_ERR(pcie->base))
941                 return PTR_ERR(pcie->base);
942
943         irq = platform_get_irq(pdev, 0);
944         ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
945                                IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
946                                pcie);
947         if (ret) {
948                 dev_err(dev, "Failed to register interrupt\n");
949                 return ret;
950         }
951
952         ret = advk_pcie_parse_request_of_pci_ranges(pcie);
953         if (ret) {
954                 dev_err(dev, "Failed to parse resources\n");
955                 return ret;
956         }
957
958         advk_pcie_setup_hw(pcie);
959
960         ret = advk_pcie_init_irq_domain(pcie);
961         if (ret) {
962                 dev_err(dev, "Failed to initialize irq\n");
963                 return ret;
964         }
965
966         ret = advk_pcie_init_msi_irq_domain(pcie);
967         if (ret) {
968                 dev_err(dev, "Failed to initialize irq\n");
969                 advk_pcie_remove_irq_domain(pcie);
970                 return ret;
971         }
972
973         list_splice_init(&pcie->resources, &bridge->windows);
974         bridge->dev.parent = dev;
975         bridge->sysdata = pcie;
976         bridge->busnr = 0;
977         bridge->ops = &advk_pcie_ops;
978         bridge->map_irq = of_irq_parse_and_map_pci;
979         bridge->swizzle_irq = pci_common_swizzle;
980
981         ret = pci_host_probe(bridge);
982         if (ret < 0) {
983                 advk_pcie_remove_msi_irq_domain(pcie);
984                 advk_pcie_remove_irq_domain(pcie);
985                 return ret;
986         }
987
988         return 0;
989 }
990
991 static const struct of_device_id advk_pcie_of_match_table[] = {
992         { .compatible = "marvell,armada-3700-pcie", },
993         {},
994 };
995
996 static struct platform_driver advk_pcie_driver = {
997         .driver = {
998                 .name = "advk-pcie",
999                 .of_match_table = advk_pcie_of_match_table,
1000                 /* Driver unloading/unbinding currently not supported */
1001                 .suppress_bind_attrs = true,
1002         },
1003         .probe = advk_pcie_probe,
1004 };
1005 builtin_platform_driver(advk_pcie_driver);