GNU Linux-libre 4.9.290-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
114 /* PCIe window configuration */
115 #define OB_WIN_BASE_ADDR                        0x4c00
116 #define OB_WIN_BLOCK_SIZE                       0x20
117 #define OB_WIN_REG_ADDR(win, offset)            (OB_WIN_BASE_ADDR + \
118                                                  OB_WIN_BLOCK_SIZE * (win) + \
119                                                  (offset))
120 #define OB_WIN_MATCH_LS(win)                    OB_WIN_REG_ADDR(win, 0x00)
121 #define OB_WIN_MATCH_MS(win)                    OB_WIN_REG_ADDR(win, 0x04)
122 #define OB_WIN_REMAP_LS(win)                    OB_WIN_REG_ADDR(win, 0x08)
123 #define OB_WIN_REMAP_MS(win)                    OB_WIN_REG_ADDR(win, 0x0c)
124 #define OB_WIN_MASK_LS(win)                     OB_WIN_REG_ADDR(win, 0x10)
125 #define OB_WIN_MASK_MS(win)                     OB_WIN_REG_ADDR(win, 0x14)
126 #define OB_WIN_ACTIONS(win)                     OB_WIN_REG_ADDR(win, 0x18)
127
128 /* PCIe window types */
129 #define OB_PCIE_MEM                             0x0
130 #define OB_PCIE_IO                              0x4
131
132 /* LMI registers base address and register offsets */
133 #define LMI_BASE_ADDR                           0x6000
134 #define CFG_REG                                 (LMI_BASE_ADDR + 0x0)
135 #define     LTSSM_SHIFT                         24
136 #define     LTSSM_MASK                          0x3f
137 #define     LTSSM_L0                            0x10
138 #define     RC_BAR_CONFIG                       0x300
139
140 /* PCIe core controller registers */
141 #define CTRL_CORE_BASE_ADDR                     0x18000
142 #define CTRL_CONFIG_REG                         (CTRL_CORE_BASE_ADDR + 0x0)
143 #define     CTRL_MODE_SHIFT                     0x0
144 #define     CTRL_MODE_MASK                      0x1
145 #define     PCIE_CORE_MODE_DIRECT               0x0
146 #define     PCIE_CORE_MODE_COMMAND              0x1
147
148 /* PCIe Central Interrupts Registers */
149 #define CENTRAL_INT_BASE_ADDR                   0x1b000
150 #define HOST_CTRL_INT_STATUS_REG                (CENTRAL_INT_BASE_ADDR + 0x0)
151 #define HOST_CTRL_INT_MASK_REG                  (CENTRAL_INT_BASE_ADDR + 0x4)
152 #define     PCIE_IRQ_CMDQ_INT                   BIT(0)
153 #define     PCIE_IRQ_MSI_STATUS_INT             BIT(1)
154 #define     PCIE_IRQ_CMD_SENT_DONE              BIT(3)
155 #define     PCIE_IRQ_DMA_INT                    BIT(4)
156 #define     PCIE_IRQ_IB_DXFERDONE               BIT(5)
157 #define     PCIE_IRQ_OB_DXFERDONE               BIT(6)
158 #define     PCIE_IRQ_OB_RXFERDONE               BIT(7)
159 #define     PCIE_IRQ_COMPQ_INT                  BIT(12)
160 #define     PCIE_IRQ_DIR_RD_DDR_DET             BIT(13)
161 #define     PCIE_IRQ_DIR_WR_DDR_DET             BIT(14)
162 #define     PCIE_IRQ_CORE_INT                   BIT(16)
163 #define     PCIE_IRQ_CORE_INT_PIO               BIT(17)
164 #define     PCIE_IRQ_DPMU_INT                   BIT(18)
165 #define     PCIE_IRQ_PCIE_MIS_INT               BIT(19)
166 #define     PCIE_IRQ_MSI_INT1_DET               BIT(20)
167 #define     PCIE_IRQ_MSI_INT2_DET               BIT(21)
168 #define     PCIE_IRQ_RC_DBELL_DET               BIT(22)
169 #define     PCIE_IRQ_EP_STATUS                  BIT(23)
170 #define     PCIE_IRQ_ALL_MASK                   0xfff0fb
171 #define     PCIE_IRQ_ENABLE_INTS_MASK           PCIE_IRQ_CORE_INT
172
173 /* Transaction types */
174 #define PCIE_CONFIG_RD_TYPE0                    0x8
175 #define PCIE_CONFIG_RD_TYPE1                    0x9
176 #define PCIE_CONFIG_WR_TYPE0                    0xa
177 #define PCIE_CONFIG_WR_TYPE1                    0xb
178
179 #define PCIE_CONF_BUS(bus)                      (((bus) & 0xff) << 20)
180 #define PCIE_CONF_DEV(dev)                      (((dev) & 0x1f) << 15)
181 #define PCIE_CONF_FUNC(fun)                     (((fun) & 0x7)  << 12)
182 #define PCIE_CONF_REG(reg)                      ((reg) & 0xffc)
183 #define PCIE_CONF_ADDR(bus, devfn, where)       \
184         (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn))    | \
185          PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
186
187 #define PIO_TIMEOUT_MS                  1
188
189 #define LINK_WAIT_MAX_RETRIES           10
190 #define LINK_WAIT_USLEEP_MIN            90000
191 #define LINK_WAIT_USLEEP_MAX            100000
192
193 #define LEGACY_IRQ_NUM                  4
194 #define MSI_IRQ_NUM                     32
195
196 struct advk_pcie {
197         struct platform_device *pdev;
198         void __iomem *base;
199         struct list_head resources;
200         struct irq_domain *irq_domain;
201         struct irq_chip irq_chip;
202         struct msi_controller msi;
203         struct irq_domain *msi_domain;
204         struct irq_chip msi_irq_chip;
205         DECLARE_BITMAP(msi_irq_in_use, MSI_IRQ_NUM);
206         struct mutex msi_used_lock;
207         u16 msi_msg;
208         int root_bus_nr;
209 };
210
211 static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
212 {
213         writel(val, pcie->base + reg);
214 }
215
216 static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
217 {
218         return readl(pcie->base + reg);
219 }
220
221 static int advk_pcie_link_up(struct advk_pcie *pcie)
222 {
223         u32 val, ltssm_state;
224
225         val = advk_readl(pcie, CFG_REG);
226         ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
227         return ltssm_state >= LTSSM_L0;
228 }
229
230 static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
231 {
232         struct device *dev = &pcie->pdev->dev;
233         int retries;
234
235         /* check if the link is up or not */
236         for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
237                 if (advk_pcie_link_up(pcie)) {
238                         dev_info(dev, "link up\n");
239                         return 0;
240                 }
241
242                 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
243         }
244
245         dev_err(dev, "link never came up\n");
246         return -ETIMEDOUT;
247 }
248
249 /*
250  * Set PCIe address window register which could be used for memory
251  * mapping.
252  */
253 static void advk_pcie_set_ob_win(struct advk_pcie *pcie,
254                                  u32 win_num, u32 match_ms,
255                                  u32 match_ls, u32 mask_ms,
256                                  u32 mask_ls, u32 remap_ms,
257                                  u32 remap_ls, u32 action)
258 {
259         advk_writel(pcie, match_ls, OB_WIN_MATCH_LS(win_num));
260         advk_writel(pcie, match_ms, OB_WIN_MATCH_MS(win_num));
261         advk_writel(pcie, mask_ms, OB_WIN_MASK_MS(win_num));
262         advk_writel(pcie, mask_ls, OB_WIN_MASK_LS(win_num));
263         advk_writel(pcie, remap_ms, OB_WIN_REMAP_MS(win_num));
264         advk_writel(pcie, remap_ls, OB_WIN_REMAP_LS(win_num));
265         advk_writel(pcie, action, OB_WIN_ACTIONS(win_num));
266         advk_writel(pcie, match_ls | BIT(0), OB_WIN_MATCH_LS(win_num));
267 }
268
269 static void advk_pcie_setup_hw(struct advk_pcie *pcie)
270 {
271         u32 reg;
272         int i;
273
274         /* Point PCIe unit MBUS decode windows to DRAM space */
275         for (i = 0; i < 8; i++)
276                 advk_pcie_set_ob_win(pcie, i, 0, 0, 0, 0, 0, 0, 0);
277
278         /* Set to Direct mode */
279         reg = advk_readl(pcie, CTRL_CONFIG_REG);
280         reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
281         reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
282         advk_writel(pcie, reg, CTRL_CONFIG_REG);
283
284         /* Set PCI global control register to RC mode */
285         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
286         reg |= (IS_RC_MSK << IS_RC_SHIFT);
287         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
288
289         /* Set Advanced Error Capabilities and Control PF0 register */
290         reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
291                 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
292                 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
293                 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
294         advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
295
296         /* Set PCIe Device Control and Status 1 PF0 register */
297         reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
298                 (7 << PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT) |
299                 PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE |
300                 (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ <<
301                  PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT);
302         advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
303
304         /* Program PCIe Control 2 to disable strict ordering */
305         reg = PCIE_CORE_CTRL2_RESERVED |
306                 PCIE_CORE_CTRL2_TD_ENABLE;
307         advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
308
309         /* Set GEN2 */
310         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
311         reg &= ~PCIE_GEN_SEL_MSK;
312         reg |= SPEED_GEN_2;
313         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
314
315         /* Set lane X1 */
316         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
317         reg &= ~LANE_CNT_MSK;
318         reg |= LANE_COUNT_1;
319         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
320
321         /* Enable link training */
322         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
323         reg |= LINK_TRAINING_EN;
324         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
325
326         /* Enable MSI */
327         reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
328         reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
329         advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
330
331         /* Clear all interrupts */
332         advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
333         advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
334         advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
335
336         /* Disable All ISR0/1 Sources */
337         reg = PCIE_ISR0_ALL_MASK;
338         reg &= ~PCIE_ISR0_MSI_INT_PENDING;
339         advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
340
341         advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
342
343         /* Unmask all MSI's */
344         advk_writel(pcie, 0, PCIE_MSI_MASK_REG);
345
346         /* Enable summary interrupt for GIC SPI source */
347         reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
348         advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
349
350         reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
351         reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
352         advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
353
354         /* Bypass the address window mapping for PIO */
355         reg = advk_readl(pcie, PIO_CTRL);
356         reg |= PIO_CTRL_ADDR_WIN_DISABLE;
357         advk_writel(pcie, reg, PIO_CTRL);
358
359         /* Start link training */
360         reg = advk_readl(pcie, PCIE_CORE_LINK_CTRL_STAT_REG);
361         reg |= PCIE_CORE_LINK_TRAINING;
362         advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
363
364         advk_pcie_wait_for_link(pcie);
365
366         reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
367         reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
368                 PCIE_CORE_CMD_IO_ACCESS_EN |
369                 PCIE_CORE_CMD_MEM_IO_REQ_EN;
370         advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
371 }
372
373 static void advk_pcie_check_pio_status(struct advk_pcie *pcie)
374 {
375         struct device *dev = &pcie->pdev->dev;
376         u32 reg;
377         unsigned int status;
378         char *strcomp_status, *str_posted;
379
380         reg = advk_readl(pcie, PIO_STAT);
381         status = (reg & PIO_COMPLETION_STATUS_MASK) >>
382                 PIO_COMPLETION_STATUS_SHIFT;
383
384         if (!status)
385                 return;
386
387         switch (status) {
388         case PIO_COMPLETION_STATUS_UR:
389                 strcomp_status = "UR";
390                 break;
391         case PIO_COMPLETION_STATUS_CRS:
392                 strcomp_status = "CRS";
393                 break;
394         case PIO_COMPLETION_STATUS_CA:
395                 strcomp_status = "CA";
396                 break;
397         default:
398                 strcomp_status = "Unknown";
399                 break;
400         }
401
402         if (reg & PIO_NON_POSTED_REQ)
403                 str_posted = "Non-posted";
404         else
405                 str_posted = "Posted";
406
407         dev_err(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
408                 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
409 }
410
411 static int advk_pcie_wait_pio(struct advk_pcie *pcie)
412 {
413         struct device *dev = &pcie->pdev->dev;
414         unsigned long timeout;
415
416         timeout = jiffies + msecs_to_jiffies(PIO_TIMEOUT_MS);
417
418         while (time_before(jiffies, timeout)) {
419                 u32 start, isr;
420
421                 start = advk_readl(pcie, PIO_START);
422                 isr = advk_readl(pcie, PIO_ISR);
423                 if (!start && isr)
424                         return 0;
425         }
426
427         dev_err(dev, "config read/write timed out\n");
428         return -ETIMEDOUT;
429 }
430
431 static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
432                              int where, int size, u32 *val)
433 {
434         struct advk_pcie *pcie = bus->sysdata;
435         u32 reg;
436         int ret;
437
438         if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0) {
439                 *val = 0xffffffff;
440                 return PCIBIOS_DEVICE_NOT_FOUND;
441         }
442
443         /* Start PIO */
444         advk_writel(pcie, 0, PIO_START);
445         advk_writel(pcie, 1, PIO_ISR);
446
447         /* Program the control register */
448         reg = advk_readl(pcie, PIO_CTRL);
449         reg &= ~PIO_CTRL_TYPE_MASK;
450         if (bus->number ==  pcie->root_bus_nr)
451                 reg |= PCIE_CONFIG_RD_TYPE0;
452         else
453                 reg |= PCIE_CONFIG_RD_TYPE1;
454         advk_writel(pcie, reg, PIO_CTRL);
455
456         /* Program the address registers */
457         reg = PCIE_CONF_ADDR(bus->number, devfn, where);
458         advk_writel(pcie, reg, PIO_ADDR_LS);
459         advk_writel(pcie, 0, PIO_ADDR_MS);
460
461         /* Program the data strobe */
462         advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
463
464         /* Start the transfer */
465         advk_writel(pcie, 1, PIO_START);
466
467         ret = advk_pcie_wait_pio(pcie);
468         if (ret < 0)
469                 return PCIBIOS_SET_FAILED;
470
471         advk_pcie_check_pio_status(pcie);
472
473         /* Get the read result */
474         *val = advk_readl(pcie, PIO_RD_DATA);
475         if (size == 1)
476                 *val = (*val >> (8 * (where & 3))) & 0xff;
477         else if (size == 2)
478                 *val = (*val >> (8 * (where & 3))) & 0xffff;
479
480         return PCIBIOS_SUCCESSFUL;
481 }
482
483 static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
484                                 int where, int size, u32 val)
485 {
486         struct advk_pcie *pcie = bus->sysdata;
487         u32 reg;
488         u32 data_strobe = 0x0;
489         int offset;
490         int ret;
491
492         if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0)
493                 return PCIBIOS_DEVICE_NOT_FOUND;
494
495         if (where % size)
496                 return PCIBIOS_SET_FAILED;
497
498         /* Start PIO */
499         advk_writel(pcie, 0, PIO_START);
500         advk_writel(pcie, 1, PIO_ISR);
501
502         /* Program the control register */
503         reg = advk_readl(pcie, PIO_CTRL);
504         reg &= ~PIO_CTRL_TYPE_MASK;
505         if (bus->number == pcie->root_bus_nr)
506                 reg |= PCIE_CONFIG_WR_TYPE0;
507         else
508                 reg |= PCIE_CONFIG_WR_TYPE1;
509         advk_writel(pcie, reg, PIO_CTRL);
510
511         /* Program the address registers */
512         reg = PCIE_CONF_ADDR(bus->number, devfn, where);
513         advk_writel(pcie, reg, PIO_ADDR_LS);
514         advk_writel(pcie, 0, PIO_ADDR_MS);
515
516         /* Calculate the write strobe */
517         offset      = where & 0x3;
518         reg         = val << (8 * offset);
519         data_strobe = GENMASK(size - 1, 0) << offset;
520
521         /* Program the data register */
522         advk_writel(pcie, reg, PIO_WR_DATA);
523
524         /* Program the data strobe */
525         advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
526
527         /* Start the transfer */
528         advk_writel(pcie, 1, PIO_START);
529
530         ret = advk_pcie_wait_pio(pcie);
531         if (ret < 0)
532                 return PCIBIOS_SET_FAILED;
533
534         advk_pcie_check_pio_status(pcie);
535
536         return PCIBIOS_SUCCESSFUL;
537 }
538
539 static struct pci_ops advk_pcie_ops = {
540         .read = advk_pcie_rd_conf,
541         .write = advk_pcie_wr_conf,
542 };
543
544 static int advk_pcie_alloc_msi(struct advk_pcie *pcie)
545 {
546         int hwirq;
547
548         mutex_lock(&pcie->msi_used_lock);
549         hwirq = find_first_zero_bit(pcie->msi_irq_in_use, MSI_IRQ_NUM);
550         if (hwirq >= MSI_IRQ_NUM)
551                 hwirq = -ENOSPC;
552         else
553                 set_bit(hwirq, pcie->msi_irq_in_use);
554         mutex_unlock(&pcie->msi_used_lock);
555
556         return hwirq;
557 }
558
559 static void advk_pcie_free_msi(struct advk_pcie *pcie, int hwirq)
560 {
561         struct device *dev = &pcie->pdev->dev;
562
563         mutex_lock(&pcie->msi_used_lock);
564         if (!test_bit(hwirq, pcie->msi_irq_in_use))
565                 dev_err(dev, "trying to free unused MSI#%d\n", hwirq);
566         else
567                 clear_bit(hwirq, pcie->msi_irq_in_use);
568         mutex_unlock(&pcie->msi_used_lock);
569 }
570
571 static int advk_pcie_setup_msi_irq(struct msi_controller *chip,
572                                    struct pci_dev *pdev,
573                                    struct msi_desc *desc)
574 {
575         struct advk_pcie *pcie = pdev->bus->sysdata;
576         struct msi_msg msg;
577         int virq, hwirq;
578         phys_addr_t msi_msg_phys;
579
580         /* We support MSI, but not MSI-X */
581         if (desc->msi_attrib.is_msix)
582                 return -EINVAL;
583
584         hwirq = advk_pcie_alloc_msi(pcie);
585         if (hwirq < 0)
586                 return hwirq;
587
588         virq = irq_create_mapping(pcie->msi_domain, hwirq);
589         if (!virq) {
590                 advk_pcie_free_msi(pcie, hwirq);
591                 return -EINVAL;
592         }
593
594         irq_set_msi_desc(virq, desc);
595
596         msi_msg_phys = virt_to_phys(&pcie->msi_msg);
597
598         msg.address_lo = lower_32_bits(msi_msg_phys);
599         msg.address_hi = upper_32_bits(msi_msg_phys);
600         msg.data = virq;
601
602         pci_write_msi_msg(virq, &msg);
603
604         return 0;
605 }
606
607 static void advk_pcie_teardown_msi_irq(struct msi_controller *chip,
608                                        unsigned int irq)
609 {
610         struct irq_data *d = irq_get_irq_data(irq);
611         struct msi_desc *msi = irq_data_get_msi_desc(d);
612         struct advk_pcie *pcie = msi_desc_to_pci_sysdata(msi);
613         unsigned long hwirq = d->hwirq;
614
615         irq_dispose_mapping(irq);
616         advk_pcie_free_msi(pcie, hwirq);
617 }
618
619 static int advk_pcie_msi_map(struct irq_domain *domain,
620                              unsigned int virq, irq_hw_number_t hw)
621 {
622         struct advk_pcie *pcie = domain->host_data;
623
624         irq_set_chip_and_handler(virq, &pcie->msi_irq_chip,
625                                  handle_simple_irq);
626
627         return 0;
628 }
629
630 static const struct irq_domain_ops advk_pcie_msi_irq_ops = {
631         .map = advk_pcie_msi_map,
632 };
633
634 static void advk_pcie_irq_mask(struct irq_data *d)
635 {
636         struct advk_pcie *pcie = d->domain->host_data;
637         irq_hw_number_t hwirq = irqd_to_hwirq(d);
638         u32 mask;
639
640         mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
641         mask |= PCIE_ISR0_INTX_ASSERT(hwirq);
642         advk_writel(pcie, mask, PCIE_ISR0_MASK_REG);
643 }
644
645 static void advk_pcie_irq_unmask(struct irq_data *d)
646 {
647         struct advk_pcie *pcie = d->domain->host_data;
648         irq_hw_number_t hwirq = irqd_to_hwirq(d);
649         u32 mask;
650
651         mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
652         mask &= ~PCIE_ISR0_INTX_ASSERT(hwirq);
653         advk_writel(pcie, mask, PCIE_ISR0_MASK_REG);
654 }
655
656 static int advk_pcie_irq_map(struct irq_domain *h,
657                              unsigned int virq, irq_hw_number_t hwirq)
658 {
659         struct advk_pcie *pcie = h->host_data;
660
661         advk_pcie_irq_mask(irq_get_irq_data(virq));
662         irq_set_status_flags(virq, IRQ_LEVEL);
663         irq_set_chip_and_handler(virq, &pcie->irq_chip,
664                                  handle_level_irq);
665         irq_set_chip_data(virq, pcie);
666
667         return 0;
668 }
669
670 static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
671         .map = advk_pcie_irq_map,
672         .xlate = irq_domain_xlate_onecell,
673 };
674
675 static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
676 {
677         struct device *dev = &pcie->pdev->dev;
678         struct device_node *node = dev->of_node;
679         struct irq_chip *msi_irq_chip;
680         struct msi_controller *msi;
681         phys_addr_t msi_msg_phys;
682         int ret;
683
684         msi_irq_chip = &pcie->msi_irq_chip;
685
686         msi_irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-msi",
687                                             dev_name(dev));
688         if (!msi_irq_chip->name)
689                 return -ENOMEM;
690
691         msi_irq_chip->irq_enable = pci_msi_unmask_irq;
692         msi_irq_chip->irq_disable = pci_msi_mask_irq;
693         msi_irq_chip->irq_mask = pci_msi_mask_irq;
694         msi_irq_chip->irq_unmask = pci_msi_unmask_irq;
695
696         msi = &pcie->msi;
697
698         msi->setup_irq = advk_pcie_setup_msi_irq;
699         msi->teardown_irq = advk_pcie_teardown_msi_irq;
700         msi->of_node = node;
701
702         mutex_init(&pcie->msi_used_lock);
703
704         msi_msg_phys = virt_to_phys(&pcie->msi_msg);
705
706         advk_writel(pcie, lower_32_bits(msi_msg_phys),
707                     PCIE_MSI_ADDR_LOW_REG);
708         advk_writel(pcie, upper_32_bits(msi_msg_phys),
709                     PCIE_MSI_ADDR_HIGH_REG);
710
711         pcie->msi_domain =
712                 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
713                                       &advk_pcie_msi_irq_ops, pcie);
714         if (!pcie->msi_domain)
715                 return -ENOMEM;
716
717         ret = of_pci_msi_chip_add(msi);
718         if (ret < 0) {
719                 irq_domain_remove(pcie->msi_domain);
720                 return ret;
721         }
722
723         return 0;
724 }
725
726 static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
727 {
728         of_pci_msi_chip_remove(&pcie->msi);
729         irq_domain_remove(pcie->msi_domain);
730 }
731
732 static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
733 {
734         struct device *dev = &pcie->pdev->dev;
735         struct device_node *node = dev->of_node;
736         struct device_node *pcie_intc_node;
737         struct irq_chip *irq_chip;
738
739         pcie_intc_node =  of_get_next_child(node, NULL);
740         if (!pcie_intc_node) {
741                 dev_err(dev, "No PCIe Intc node found\n");
742                 return -ENODEV;
743         }
744
745         irq_chip = &pcie->irq_chip;
746
747         irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
748                                         dev_name(dev));
749         if (!irq_chip->name) {
750                 of_node_put(pcie_intc_node);
751                 return -ENOMEM;
752         }
753
754         irq_chip->irq_mask = advk_pcie_irq_mask;
755         irq_chip->irq_mask_ack = advk_pcie_irq_mask;
756         irq_chip->irq_unmask = advk_pcie_irq_unmask;
757
758         pcie->irq_domain =
759                 irq_domain_add_linear(pcie_intc_node, LEGACY_IRQ_NUM,
760                                       &advk_pcie_irq_domain_ops, pcie);
761         if (!pcie->irq_domain) {
762                 dev_err(dev, "Failed to get a INTx IRQ domain\n");
763                 of_node_put(pcie_intc_node);
764                 return -ENOMEM;
765         }
766
767         return 0;
768 }
769
770 static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
771 {
772         irq_domain_remove(pcie->irq_domain);
773 }
774
775 static void advk_pcie_handle_msi(struct advk_pcie *pcie)
776 {
777         u32 msi_val, msi_mask, msi_status, msi_idx;
778         u16 msi_data;
779
780         msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
781         msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
782         msi_status = msi_val & ~msi_mask;
783
784         for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
785                 if (!(BIT(msi_idx) & msi_status))
786                         continue;
787
788                 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
789                 msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & 0xFF;
790                 generic_handle_irq(msi_data);
791         }
792
793         advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
794                     PCIE_ISR0_REG);
795 }
796
797 static void advk_pcie_handle_int(struct advk_pcie *pcie)
798 {
799         u32 val, mask, status;
800         int i, virq;
801
802         val = advk_readl(pcie, PCIE_ISR0_REG);
803         mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
804         status = val & ((~mask) & PCIE_ISR0_ALL_MASK);
805
806         if (!status) {
807                 advk_writel(pcie, val, PCIE_ISR0_REG);
808                 return;
809         }
810
811         /* Process MSI interrupts */
812         if (status & PCIE_ISR0_MSI_INT_PENDING)
813                 advk_pcie_handle_msi(pcie);
814
815         /* Process legacy interrupts */
816         for (i = 0; i < LEGACY_IRQ_NUM; i++) {
817                 if (!(status & PCIE_ISR0_INTX_ASSERT(i)))
818                         continue;
819
820                 advk_writel(pcie, PCIE_ISR0_INTX_ASSERT(i),
821                             PCIE_ISR0_REG);
822
823                 virq = irq_find_mapping(pcie->irq_domain, i);
824                 generic_handle_irq(virq);
825         }
826 }
827
828 static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
829 {
830         struct advk_pcie *pcie = arg;
831         u32 status;
832
833         status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
834         if (!(status & PCIE_IRQ_CORE_INT))
835                 return IRQ_NONE;
836
837         advk_pcie_handle_int(pcie);
838
839         /* Clear interrupt */
840         advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
841
842         return IRQ_HANDLED;
843 }
844
845 static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie *pcie)
846 {
847         int err, res_valid = 0;
848         struct device *dev = &pcie->pdev->dev;
849         struct device_node *np = dev->of_node;
850         struct resource_entry *win, *tmp;
851         resource_size_t iobase;
852
853         INIT_LIST_HEAD(&pcie->resources);
854
855         err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pcie->resources,
856                                                &iobase);
857         if (err)
858                 return err;
859
860         err = devm_request_pci_bus_resources(dev, &pcie->resources);
861         if (err)
862                 goto out_release_res;
863
864         resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
865                 struct resource *res = win->res;
866
867                 switch (resource_type(res)) {
868                 case IORESOURCE_IO:
869                         advk_pcie_set_ob_win(pcie, 1,
870                                              upper_32_bits(res->start),
871                                              lower_32_bits(res->start),
872                                              0, 0xF8000000, 0,
873                                              lower_32_bits(res->start),
874                                              OB_PCIE_IO);
875                         err = pci_remap_iospace(res, iobase);
876                         if (err) {
877                                 dev_warn(dev, "error %d: failed to map resource %pR\n",
878                                          err, res);
879                                 resource_list_destroy_entry(win);
880                         }
881                         break;
882                 case IORESOURCE_MEM:
883                         advk_pcie_set_ob_win(pcie, 0,
884                                              upper_32_bits(res->start),
885                                              lower_32_bits(res->start),
886                                              0x0, 0xF8000000, 0,
887                                              lower_32_bits(res->start),
888                                              (2 << 20) | OB_PCIE_MEM);
889                         res_valid |= !(res->flags & IORESOURCE_PREFETCH);
890                         break;
891                 case IORESOURCE_BUS:
892                         pcie->root_bus_nr = res->start;
893                         break;
894                 }
895         }
896
897         if (!res_valid) {
898                 dev_err(dev, "non-prefetchable memory resource required\n");
899                 err = -EINVAL;
900                 goto out_release_res;
901         }
902
903         return 0;
904
905 out_release_res:
906         pci_free_resource_list(&pcie->resources);
907         return err;
908 }
909
910 static int advk_pcie_probe(struct platform_device *pdev)
911 {
912         struct device *dev = &pdev->dev;
913         struct advk_pcie *pcie;
914         struct resource *res;
915         struct pci_bus *bus, *child;
916         struct msi_controller *msi;
917         struct device_node *msi_node;
918         int ret, irq;
919
920         pcie = devm_kzalloc(dev, sizeof(struct advk_pcie), GFP_KERNEL);
921         if (!pcie)
922                 return -ENOMEM;
923
924         pcie->pdev = pdev;
925
926         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
927         pcie->base = devm_ioremap_resource(dev, res);
928         if (IS_ERR(pcie->base))
929                 return PTR_ERR(pcie->base);
930
931         irq = platform_get_irq(pdev, 0);
932         ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
933                                IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
934                                pcie);
935         if (ret) {
936                 dev_err(dev, "Failed to register interrupt\n");
937                 return ret;
938         }
939
940         ret = advk_pcie_parse_request_of_pci_ranges(pcie);
941         if (ret) {
942                 dev_err(dev, "Failed to parse resources\n");
943                 return ret;
944         }
945
946         advk_pcie_setup_hw(pcie);
947
948         ret = advk_pcie_init_irq_domain(pcie);
949         if (ret) {
950                 dev_err(dev, "Failed to initialize irq\n");
951                 return ret;
952         }
953
954         ret = advk_pcie_init_msi_irq_domain(pcie);
955         if (ret) {
956                 dev_err(dev, "Failed to initialize irq\n");
957                 advk_pcie_remove_irq_domain(pcie);
958                 return ret;
959         }
960
961         msi_node = of_parse_phandle(dev->of_node, "msi-parent", 0);
962         if (msi_node)
963                 msi = of_pci_find_msi_chip_by_node(msi_node);
964         else
965                 msi = NULL;
966
967         bus = pci_scan_root_bus_msi(dev, 0, &advk_pcie_ops,
968                                     pcie, &pcie->resources, &pcie->msi);
969         if (!bus) {
970                 advk_pcie_remove_msi_irq_domain(pcie);
971                 advk_pcie_remove_irq_domain(pcie);
972                 return -ENOMEM;
973         }
974
975         pci_bus_size_bridges(bus);
976         pci_bus_assign_resources(bus);
977
978         list_for_each_entry(child, &bus->children, node)
979                 pcie_bus_configure_settings(child);
980
981         pci_bus_add_devices(bus);
982         return 0;
983 }
984
985 static const struct of_device_id advk_pcie_of_match_table[] = {
986         { .compatible = "marvell,armada-3700-pcie", },
987         {},
988 };
989
990 static struct platform_driver advk_pcie_driver = {
991         .driver = {
992                 .name = "advk-pcie",
993                 .of_match_table = advk_pcie_of_match_table,
994                 /* Driver unloading/unbinding currently not supported */
995                 .suppress_bind_attrs = true,
996         },
997         .probe = advk_pcie_probe,
998 };
999 builtin_platform_driver(advk_pcie_driver);