GNU Linux-libre 4.9.297-gnu1
[releases.git] / drivers / pci / host / pci-aardvark.c
1 /*
2  * Driver for the Aardvark PCIe controller, used on Marvell Armada
3  * 3700.
4  *
5  * Copyright (C) 2016 Marvell
6  *
7  * Author: Hezi Shahmoon <hezi.shahmoon@marvell.com>
8  *
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.
12  */
13
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>
24
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)
45
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)
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_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)
114
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) + \
120                                                  (offset))
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)
128
129 /* PCIe window types */
130 #define OB_PCIE_MEM                             0x0
131 #define OB_PCIE_IO                              0x4
132
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
140
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
148
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
173
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
179
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))
187
188 #define PIO_TIMEOUT_MS                  1
189
190 #define LINK_WAIT_MAX_RETRIES           10
191 #define LINK_WAIT_USLEEP_MIN            90000
192 #define LINK_WAIT_USLEEP_MAX            100000
193
194 #define LEGACY_IRQ_NUM                  4
195 #define MSI_IRQ_NUM                     32
196
197 struct advk_pcie {
198         struct platform_device *pdev;
199         void __iomem *base;
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;
208         u16 msi_msg;
209         int root_bus_nr;
210 };
211
212 static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
213 {
214         writel(val, pcie->base + reg);
215 }
216
217 static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
218 {
219         return readl(pcie->base + reg);
220 }
221
222 static int advk_pcie_link_up(struct advk_pcie *pcie)
223 {
224         u32 val, ltssm_state;
225
226         val = advk_readl(pcie, CFG_REG);
227         ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
228         return ltssm_state >= LTSSM_L0;
229 }
230
231 static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
232 {
233         struct device *dev = &pcie->pdev->dev;
234         int retries;
235
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");
240                         return 0;
241                 }
242
243                 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
244         }
245
246         dev_err(dev, "link never came up\n");
247         return -ETIMEDOUT;
248 }
249
250 /*
251  * Set PCIe address window register which could be used for memory
252  * mapping.
253  */
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)
259 {
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));
268 }
269
270 static void advk_pcie_setup_hw(struct advk_pcie *pcie)
271 {
272         u32 reg;
273         int i;
274
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);
278
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);
284
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);
289
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);
296
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);
304
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);
309
310         /* Set GEN2 */
311         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
312         reg &= ~PCIE_GEN_SEL_MSK;
313         reg |= SPEED_GEN_2;
314         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
315
316         /* Set lane X1 */
317         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
318         reg &= ~LANE_CNT_MSK;
319         reg |= LANE_COUNT_1;
320         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
321
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);
326
327         /* Enable MSI */
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);
331
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);
336
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);
341
342         advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
343
344         /* Unmask all MSI's */
345         advk_writel(pcie, 0, PCIE_MSI_MASK_REG);
346
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);
350
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);
354
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);
359
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);
364
365         advk_pcie_wait_for_link(pcie);
366
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);
372 }
373
374 static void advk_pcie_check_pio_status(struct advk_pcie *pcie)
375 {
376         struct device *dev = &pcie->pdev->dev;
377         u32 reg;
378         unsigned int status;
379         char *strcomp_status, *str_posted;
380
381         reg = advk_readl(pcie, PIO_STAT);
382         status = (reg & PIO_COMPLETION_STATUS_MASK) >>
383                 PIO_COMPLETION_STATUS_SHIFT;
384
385         if (!status)
386                 return;
387
388         switch (status) {
389         case PIO_COMPLETION_STATUS_UR:
390                 strcomp_status = "UR";
391                 break;
392         case PIO_COMPLETION_STATUS_CRS:
393                 strcomp_status = "CRS";
394                 break;
395         case PIO_COMPLETION_STATUS_CA:
396                 strcomp_status = "CA";
397                 break;
398         default:
399                 strcomp_status = "Unknown";
400                 break;
401         }
402
403         if (reg & PIO_NON_POSTED_REQ)
404                 str_posted = "Non-posted";
405         else
406                 str_posted = "Posted";
407
408         dev_dbg(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
409                 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
410 }
411
412 static int advk_pcie_wait_pio(struct advk_pcie *pcie)
413 {
414         struct device *dev = &pcie->pdev->dev;
415         unsigned long timeout;
416
417         timeout = jiffies + msecs_to_jiffies(PIO_TIMEOUT_MS);
418
419         while (time_before(jiffies, timeout)) {
420                 u32 start, isr;
421
422                 start = advk_readl(pcie, PIO_START);
423                 isr = advk_readl(pcie, PIO_ISR);
424                 if (!start && isr)
425                         return 0;
426         }
427
428         dev_err(dev, "config read/write timed out\n");
429         return -ETIMEDOUT;
430 }
431
432 static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
433                              int where, int size, u32 *val)
434 {
435         struct advk_pcie *pcie = bus->sysdata;
436         u32 reg;
437         int ret;
438
439         if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0) {
440                 *val = 0xffffffff;
441                 return PCIBIOS_DEVICE_NOT_FOUND;
442         }
443
444         /* Start PIO */
445         advk_writel(pcie, 0, PIO_START);
446         advk_writel(pcie, 1, PIO_ISR);
447
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;
453         else
454                 reg |= PCIE_CONFIG_RD_TYPE1;
455         advk_writel(pcie, reg, PIO_CTRL);
456
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);
461
462         /* Program the data strobe */
463         advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
464
465         /* Start the transfer */
466         advk_writel(pcie, 1, PIO_START);
467
468         ret = advk_pcie_wait_pio(pcie);
469         if (ret < 0)
470                 return PCIBIOS_SET_FAILED;
471
472         advk_pcie_check_pio_status(pcie);
473
474         /* Get the read result */
475         *val = advk_readl(pcie, PIO_RD_DATA);
476         if (size == 1)
477                 *val = (*val >> (8 * (where & 3))) & 0xff;
478         else if (size == 2)
479                 *val = (*val >> (8 * (where & 3))) & 0xffff;
480
481         return PCIBIOS_SUCCESSFUL;
482 }
483
484 static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
485                                 int where, int size, u32 val)
486 {
487         struct advk_pcie *pcie = bus->sysdata;
488         u32 reg;
489         u32 data_strobe = 0x0;
490         int offset;
491         int ret;
492
493         if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0)
494                 return PCIBIOS_DEVICE_NOT_FOUND;
495
496         if (where % size)
497                 return PCIBIOS_SET_FAILED;
498
499         /* Start PIO */
500         advk_writel(pcie, 0, PIO_START);
501         advk_writel(pcie, 1, PIO_ISR);
502
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;
508         else
509                 reg |= PCIE_CONFIG_WR_TYPE1;
510         advk_writel(pcie, reg, PIO_CTRL);
511
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);
516
517         /* Calculate the write strobe */
518         offset      = where & 0x3;
519         reg         = val << (8 * offset);
520         data_strobe = GENMASK(size - 1, 0) << offset;
521
522         /* Program the data register */
523         advk_writel(pcie, reg, PIO_WR_DATA);
524
525         /* Program the data strobe */
526         advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
527
528         /* Start the transfer */
529         advk_writel(pcie, 1, PIO_START);
530
531         ret = advk_pcie_wait_pio(pcie);
532         if (ret < 0)
533                 return PCIBIOS_SET_FAILED;
534
535         advk_pcie_check_pio_status(pcie);
536
537         return PCIBIOS_SUCCESSFUL;
538 }
539
540 static struct pci_ops advk_pcie_ops = {
541         .read = advk_pcie_rd_conf,
542         .write = advk_pcie_wr_conf,
543 };
544
545 static int advk_pcie_alloc_msi(struct advk_pcie *pcie)
546 {
547         int hwirq;
548
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)
552                 hwirq = -ENOSPC;
553         else
554                 set_bit(hwirq, pcie->msi_irq_in_use);
555         mutex_unlock(&pcie->msi_used_lock);
556
557         return hwirq;
558 }
559
560 static void advk_pcie_free_msi(struct advk_pcie *pcie, int hwirq)
561 {
562         struct device *dev = &pcie->pdev->dev;
563
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);
567         else
568                 clear_bit(hwirq, pcie->msi_irq_in_use);
569         mutex_unlock(&pcie->msi_used_lock);
570 }
571
572 static int advk_pcie_setup_msi_irq(struct msi_controller *chip,
573                                    struct pci_dev *pdev,
574                                    struct msi_desc *desc)
575 {
576         struct advk_pcie *pcie = pdev->bus->sysdata;
577         struct msi_msg msg;
578         int virq, hwirq;
579         phys_addr_t msi_msg_phys;
580
581         /* We support MSI, but not MSI-X */
582         if (desc->msi_attrib.is_msix)
583                 return -EINVAL;
584
585         hwirq = advk_pcie_alloc_msi(pcie);
586         if (hwirq < 0)
587                 return hwirq;
588
589         virq = irq_create_mapping(pcie->msi_domain, hwirq);
590         if (!virq) {
591                 advk_pcie_free_msi(pcie, hwirq);
592                 return -EINVAL;
593         }
594
595         irq_set_msi_desc(virq, desc);
596
597         msi_msg_phys = virt_to_phys(&pcie->msi_msg);
598
599         msg.address_lo = lower_32_bits(msi_msg_phys);
600         msg.address_hi = upper_32_bits(msi_msg_phys);
601         msg.data = virq;
602
603         pci_write_msi_msg(virq, &msg);
604
605         return 0;
606 }
607
608 static void advk_pcie_teardown_msi_irq(struct msi_controller *chip,
609                                        unsigned int irq)
610 {
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;
615
616         irq_dispose_mapping(irq);
617         advk_pcie_free_msi(pcie, hwirq);
618 }
619
620 static int advk_pcie_msi_map(struct irq_domain *domain,
621                              unsigned int virq, irq_hw_number_t hw)
622 {
623         struct advk_pcie *pcie = domain->host_data;
624
625         irq_set_chip_and_handler(virq, &pcie->msi_irq_chip,
626                                  handle_simple_irq);
627
628         return 0;
629 }
630
631 static const struct irq_domain_ops advk_pcie_msi_irq_ops = {
632         .map = advk_pcie_msi_map,
633 };
634
635 static void advk_pcie_irq_mask(struct irq_data *d)
636 {
637         struct advk_pcie *pcie = d->domain->host_data;
638         irq_hw_number_t hwirq = irqd_to_hwirq(d);
639         u32 mask;
640
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);
644 }
645
646 static void advk_pcie_irq_unmask(struct irq_data *d)
647 {
648         struct advk_pcie *pcie = d->domain->host_data;
649         irq_hw_number_t hwirq = irqd_to_hwirq(d);
650         u32 mask;
651
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);
655 }
656
657 static int advk_pcie_irq_map(struct irq_domain *h,
658                              unsigned int virq, irq_hw_number_t hwirq)
659 {
660         struct advk_pcie *pcie = h->host_data;
661
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,
665                                  handle_level_irq);
666         irq_set_chip_data(virq, pcie);
667
668         return 0;
669 }
670
671 static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
672         .map = advk_pcie_irq_map,
673         .xlate = irq_domain_xlate_onecell,
674 };
675
676 static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
677 {
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;
683         int ret;
684
685         msi_irq_chip = &pcie->msi_irq_chip;
686
687         msi_irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-msi",
688                                             dev_name(dev));
689         if (!msi_irq_chip->name)
690                 return -ENOMEM;
691
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;
696
697         msi = &pcie->msi;
698
699         msi->setup_irq = advk_pcie_setup_msi_irq;
700         msi->teardown_irq = advk_pcie_teardown_msi_irq;
701         msi->of_node = node;
702
703         mutex_init(&pcie->msi_used_lock);
704
705         msi_msg_phys = virt_to_phys(&pcie->msi_msg);
706
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);
711
712         pcie->msi_domain =
713                 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
714                                       &advk_pcie_msi_irq_ops, pcie);
715         if (!pcie->msi_domain)
716                 return -ENOMEM;
717
718         ret = of_pci_msi_chip_add(msi);
719         if (ret < 0) {
720                 irq_domain_remove(pcie->msi_domain);
721                 return ret;
722         }
723
724         return 0;
725 }
726
727 static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
728 {
729         of_pci_msi_chip_remove(&pcie->msi);
730         irq_domain_remove(pcie->msi_domain);
731 }
732
733 static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
734 {
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;
739
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");
743                 return -ENODEV;
744         }
745
746         irq_chip = &pcie->irq_chip;
747
748         irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
749                                         dev_name(dev));
750         if (!irq_chip->name) {
751                 of_node_put(pcie_intc_node);
752                 return -ENOMEM;
753         }
754
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;
758
759         pcie->irq_domain =
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);
765                 return -ENOMEM;
766         }
767
768         return 0;
769 }
770
771 static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
772 {
773         irq_domain_remove(pcie->irq_domain);
774 }
775
776 static void advk_pcie_handle_msi(struct advk_pcie *pcie)
777 {
778         u32 msi_val, msi_mask, msi_status, msi_idx;
779         u16 msi_data;
780
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;
784
785         for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
786                 if (!(BIT(msi_idx) & msi_status))
787                         continue;
788
789                 /*
790                  * msi_idx contains bits [4:0] of the msi_data and msi_data
791                  * contains 16bit MSI interrupt number
792                  */
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);
796         }
797
798         advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
799                     PCIE_ISR0_REG);
800 }
801
802 static void advk_pcie_handle_int(struct advk_pcie *pcie)
803 {
804         u32 val, mask, status;
805         int i, virq;
806
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);
810
811         if (!status) {
812                 advk_writel(pcie, val, PCIE_ISR0_REG);
813                 return;
814         }
815
816         /* Process MSI interrupts */
817         if (status & PCIE_ISR0_MSI_INT_PENDING)
818                 advk_pcie_handle_msi(pcie);
819
820         /* Process legacy interrupts */
821         for (i = 0; i < LEGACY_IRQ_NUM; i++) {
822                 if (!(status & PCIE_ISR0_INTX_ASSERT(i)))
823                         continue;
824
825                 advk_writel(pcie, PCIE_ISR0_INTX_ASSERT(i),
826                             PCIE_ISR0_REG);
827
828                 virq = irq_find_mapping(pcie->irq_domain, i);
829                 generic_handle_irq(virq);
830         }
831 }
832
833 static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
834 {
835         struct advk_pcie *pcie = arg;
836         u32 status;
837
838         status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
839         if (!(status & PCIE_IRQ_CORE_INT))
840                 return IRQ_NONE;
841
842         advk_pcie_handle_int(pcie);
843
844         /* Clear interrupt */
845         advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
846
847         return IRQ_HANDLED;
848 }
849
850 static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie *pcie)
851 {
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;
857
858         INIT_LIST_HEAD(&pcie->resources);
859
860         err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pcie->resources,
861                                                &iobase);
862         if (err)
863                 return err;
864
865         err = devm_request_pci_bus_resources(dev, &pcie->resources);
866         if (err)
867                 goto out_release_res;
868
869         resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
870                 struct resource *res = win->res;
871
872                 switch (resource_type(res)) {
873                 case IORESOURCE_IO:
874                         advk_pcie_set_ob_win(pcie, 1,
875                                              upper_32_bits(res->start),
876                                              lower_32_bits(res->start),
877                                              0, 0xF8000000, 0,
878                                              lower_32_bits(res->start),
879                                              OB_PCIE_IO);
880                         err = pci_remap_iospace(res, iobase);
881                         if (err) {
882                                 dev_warn(dev, "error %d: failed to map resource %pR\n",
883                                          err, res);
884                                 resource_list_destroy_entry(win);
885                         }
886                         break;
887                 case IORESOURCE_MEM:
888                         advk_pcie_set_ob_win(pcie, 0,
889                                              upper_32_bits(res->start),
890                                              lower_32_bits(res->start),
891                                              0x0, 0xF8000000, 0,
892                                              lower_32_bits(res->start),
893                                              (2 << 20) | OB_PCIE_MEM);
894                         res_valid |= !(res->flags & IORESOURCE_PREFETCH);
895                         break;
896                 case IORESOURCE_BUS:
897                         pcie->root_bus_nr = res->start;
898                         break;
899                 }
900         }
901
902         if (!res_valid) {
903                 dev_err(dev, "non-prefetchable memory resource required\n");
904                 err = -EINVAL;
905                 goto out_release_res;
906         }
907
908         return 0;
909
910 out_release_res:
911         pci_free_resource_list(&pcie->resources);
912         return err;
913 }
914
915 static int advk_pcie_probe(struct platform_device *pdev)
916 {
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;
923         int ret, irq;
924
925         pcie = devm_kzalloc(dev, sizeof(struct advk_pcie), GFP_KERNEL);
926         if (!pcie)
927                 return -ENOMEM;
928
929         pcie->pdev = pdev;
930
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);
935
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",
939                                pcie);
940         if (ret) {
941                 dev_err(dev, "Failed to register interrupt\n");
942                 return ret;
943         }
944
945         ret = advk_pcie_parse_request_of_pci_ranges(pcie);
946         if (ret) {
947                 dev_err(dev, "Failed to parse resources\n");
948                 return ret;
949         }
950
951         advk_pcie_setup_hw(pcie);
952
953         ret = advk_pcie_init_irq_domain(pcie);
954         if (ret) {
955                 dev_err(dev, "Failed to initialize irq\n");
956                 return ret;
957         }
958
959         ret = advk_pcie_init_msi_irq_domain(pcie);
960         if (ret) {
961                 dev_err(dev, "Failed to initialize irq\n");
962                 advk_pcie_remove_irq_domain(pcie);
963                 return ret;
964         }
965
966         msi_node = of_parse_phandle(dev->of_node, "msi-parent", 0);
967         if (msi_node)
968                 msi = of_pci_find_msi_chip_by_node(msi_node);
969         else
970                 msi = NULL;
971
972         bus = pci_scan_root_bus_msi(dev, 0, &advk_pcie_ops,
973                                     pcie, &pcie->resources, &pcie->msi);
974         if (!bus) {
975                 advk_pcie_remove_msi_irq_domain(pcie);
976                 advk_pcie_remove_irq_domain(pcie);
977                 return -ENOMEM;
978         }
979
980         pci_bus_size_bridges(bus);
981         pci_bus_assign_resources(bus);
982
983         list_for_each_entry(child, &bus->children, node)
984                 pcie_bus_configure_settings(child);
985
986         pci_bus_add_devices(bus);
987         return 0;
988 }
989
990 static const struct of_device_id advk_pcie_of_match_table[] = {
991         { .compatible = "marvell,armada-3700-pcie", },
992         {},
993 };
994
995 static struct platform_driver advk_pcie_driver = {
996         .driver = {
997                 .name = "advk-pcie",
998                 .of_match_table = advk_pcie_of_match_table,
999                 /* Driver unloading/unbinding currently not supported */
1000                 .suppress_bind_attrs = true,
1001         },
1002         .probe = advk_pcie_probe,
1003 };
1004 builtin_platform_driver(advk_pcie_driver);