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