1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for Mobiveil PCIe Host controller
5 * Copyright (c) 2018 Mobiveil Inc.
6 * Author: Subrahmanya Lingappa <l.subrahmanya@mobiveil.co.in>
9 #include <linux/delay.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/irqchip/chained_irq.h>
14 #include <linux/irqdomain.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/msi.h>
18 #include <linux/of_address.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_platform.h>
21 #include <linux/of_pci.h>
22 #include <linux/pci.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
28 /* register offsets and bit positions */
31 * translation tables are grouped into windows, each window registers are
32 * grouped into blocks of 4 or 16 registers each
34 #define PAB_REG_BLOCK_SIZE 16
35 #define PAB_EXT_REG_BLOCK_SIZE 4
37 #define PAB_REG_ADDR(offset, win) (offset + (win * PAB_REG_BLOCK_SIZE))
38 #define PAB_EXT_REG_ADDR(offset, win) (offset + (win * PAB_EXT_REG_BLOCK_SIZE))
40 #define LTSSM_STATUS 0x0404
41 #define LTSSM_STATUS_L0_MASK 0x3f
42 #define LTSSM_STATUS_L0 0x2d
44 #define PAB_CTRL 0x0808
45 #define AMBA_PIO_ENABLE_SHIFT 0
46 #define PEX_PIO_ENABLE_SHIFT 1
47 #define PAGE_SEL_SHIFT 13
48 #define PAGE_SEL_MASK 0x3f
49 #define PAGE_LO_MASK 0x3ff
50 #define PAGE_SEL_EN 0xc00
51 #define PAGE_SEL_OFFSET_SHIFT 10
53 #define PAB_AXI_PIO_CTRL 0x0840
54 #define APIO_EN_MASK 0xf
56 #define PAB_PEX_PIO_CTRL 0x08c0
57 #define PIO_ENABLE_SHIFT 0
59 #define PAB_INTP_AMBA_MISC_ENB 0x0b0c
60 #define PAB_INTP_AMBA_MISC_STAT 0x0b1c
61 #define PAB_INTP_INTX_MASK 0x01e0
62 #define PAB_INTP_MSI_MASK 0x8
64 #define PAB_AXI_AMAP_CTRL(win) PAB_REG_ADDR(0x0ba0, win)
65 #define WIN_ENABLE_SHIFT 0
66 #define WIN_TYPE_SHIFT 1
68 #define PAB_EXT_AXI_AMAP_SIZE(win) PAB_EXT_REG_ADDR(0xbaf0, win)
70 #define PAB_AXI_AMAP_AXI_WIN(win) PAB_REG_ADDR(0x0ba4, win)
71 #define AXI_WINDOW_ALIGN_MASK 3
73 #define PAB_AXI_AMAP_PEX_WIN_L(win) PAB_REG_ADDR(0x0ba8, win)
74 #define PAB_BUS_SHIFT 24
75 #define PAB_DEVICE_SHIFT 19
76 #define PAB_FUNCTION_SHIFT 16
78 #define PAB_AXI_AMAP_PEX_WIN_H(win) PAB_REG_ADDR(0x0bac, win)
79 #define PAB_INTP_AXI_PIO_CLASS 0x474
81 #define PAB_PEX_AMAP_CTRL(win) PAB_REG_ADDR(0x4ba0, win)
82 #define AMAP_CTRL_EN_SHIFT 0
83 #define AMAP_CTRL_TYPE_SHIFT 1
85 #define PAB_EXT_PEX_AMAP_SIZEN(win) PAB_EXT_REG_ADDR(0xbef0, win)
86 #define PAB_PEX_AMAP_AXI_WIN(win) PAB_REG_ADDR(0x4ba4, win)
87 #define PAB_PEX_AMAP_PEX_WIN_L(win) PAB_REG_ADDR(0x4ba8, win)
88 #define PAB_PEX_AMAP_PEX_WIN_H(win) PAB_REG_ADDR(0x4bac, win)
90 /* starting offset of INTX bits in status register */
91 #define PAB_INTX_START 5
93 /* supported number of MSI interrupts */
94 #define PCI_NUM_MSI 16
97 #define MSI_BASE_LO_OFFSET 0x04
98 #define MSI_BASE_HI_OFFSET 0x08
99 #define MSI_SIZE_OFFSET 0x0c
100 #define MSI_ENABLE_OFFSET 0x14
101 #define MSI_STATUS_OFFSET 0x18
102 #define MSI_DATA_OFFSET 0x20
103 #define MSI_ADDR_L_OFFSET 0x24
104 #define MSI_ADDR_H_OFFSET 0x28
106 /* outbound and inbound window definitions */
109 #define CFG_WINDOW_TYPE 0
110 #define IO_WINDOW_TYPE 1
111 #define MEM_WINDOW_TYPE 2
112 #define IB_WIN_SIZE ((u64)256 * 1024 * 1024 * 1024)
113 #define MAX_PIO_WINDOWS 8
115 /* Parameters for the waiting for link up routine */
116 #define LINK_WAIT_MAX_RETRIES 10
117 #define LINK_WAIT_MIN 90000
118 #define LINK_WAIT_MAX 100000
120 struct mobiveil_msi
{ /* MSI information */
121 struct mutex lock
; /* protect bitmap variable */
122 struct irq_domain
*msi_domain
;
123 struct irq_domain
*dev_domain
;
124 phys_addr_t msi_pages_phys
;
126 DECLARE_BITMAP(msi_irq_in_use
, PCI_NUM_MSI
);
129 struct mobiveil_pcie
{
130 struct platform_device
*pdev
;
131 struct list_head resources
;
132 void __iomem
*config_axi_slave_base
; /* endpoint config base */
133 void __iomem
*csr_axi_slave_base
; /* root port config base */
134 void __iomem
*apb_csr_base
; /* MSI register base */
135 phys_addr_t pcie_reg_base
; /* Physical PCIe Controller Base */
136 struct irq_domain
*intx_domain
;
137 raw_spinlock_t intx_mask_lock
;
141 int ob_wins_configured
; /* configured outbound windows */
142 int ib_wins_configured
; /* configured inbound windows */
143 struct resource
*ob_io_res
;
145 struct mobiveil_msi msi
;
148 static inline void csr_writel(struct mobiveil_pcie
*pcie
, const u32 value
,
151 writel_relaxed(value
, pcie
->csr_axi_slave_base
+ reg
);
154 static inline u32
csr_readl(struct mobiveil_pcie
*pcie
, const u32 reg
)
156 return readl_relaxed(pcie
->csr_axi_slave_base
+ reg
);
159 static bool mobiveil_pcie_link_up(struct mobiveil_pcie
*pcie
)
161 return (csr_readl(pcie
, LTSSM_STATUS
) &
162 LTSSM_STATUS_L0_MASK
) == LTSSM_STATUS_L0
;
165 static bool mobiveil_pcie_valid_device(struct pci_bus
*bus
, unsigned int devfn
)
167 struct mobiveil_pcie
*pcie
= bus
->sysdata
;
169 /* Only one device down on each root port */
170 if ((bus
->number
== pcie
->root_bus_nr
) && (devfn
> 0))
174 * Do not read more than one device on the bus directly
177 if ((bus
->primary
== pcie
->root_bus_nr
) && (PCI_SLOT(devfn
) > 0))
184 * mobiveil_pcie_map_bus - routine to get the configuration base of either
185 * root port or endpoint
187 static void __iomem
*mobiveil_pcie_map_bus(struct pci_bus
*bus
,
188 unsigned int devfn
, int where
)
190 struct mobiveil_pcie
*pcie
= bus
->sysdata
;
192 if (!mobiveil_pcie_valid_device(bus
, devfn
))
195 if (bus
->number
== pcie
->root_bus_nr
) {
196 /* RC config access */
197 return pcie
->csr_axi_slave_base
+ where
;
201 * EP config access (in Config/APIO space)
202 * Program PEX Address base (31..16 bits) with appropriate value
203 * (BDF) in PAB_AXI_AMAP_PEX_WIN_L0 Register.
204 * Relies on pci_lock serialization
206 csr_writel(pcie
, bus
->number
<< PAB_BUS_SHIFT
|
207 PCI_SLOT(devfn
) << PAB_DEVICE_SHIFT
|
208 PCI_FUNC(devfn
) << PAB_FUNCTION_SHIFT
,
209 PAB_AXI_AMAP_PEX_WIN_L(WIN_NUM_0
));
210 return pcie
->config_axi_slave_base
+ where
;
213 static struct pci_ops mobiveil_pcie_ops
= {
214 .map_bus
= mobiveil_pcie_map_bus
,
215 .read
= pci_generic_config_read
,
216 .write
= pci_generic_config_write
,
219 static void mobiveil_pcie_isr(struct irq_desc
*desc
)
221 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
222 struct mobiveil_pcie
*pcie
= irq_desc_get_handler_data(desc
);
223 struct device
*dev
= &pcie
->pdev
->dev
;
224 struct mobiveil_msi
*msi
= &pcie
->msi
;
225 u32 msi_data
, msi_addr_lo
, msi_addr_hi
;
226 u32 intr_status
, msi_status
;
227 unsigned long shifted_status
;
228 u32 bit
, virq
, val
, mask
;
231 * The core provides a single interrupt for both INTx/MSI messages.
232 * So we'll read both INTx and MSI status
235 chained_irq_enter(chip
, desc
);
237 /* read INTx status */
238 val
= csr_readl(pcie
, PAB_INTP_AMBA_MISC_STAT
);
239 mask
= csr_readl(pcie
, PAB_INTP_AMBA_MISC_ENB
);
240 intr_status
= val
& mask
;
243 if (intr_status
& PAB_INTP_INTX_MASK
) {
244 shifted_status
= csr_readl(pcie
, PAB_INTP_AMBA_MISC_STAT
) >>
247 for_each_set_bit(bit
, &shifted_status
, PCI_NUM_INTX
) {
248 virq
= irq_find_mapping(pcie
->intx_domain
,
251 generic_handle_irq(virq
);
253 dev_err_ratelimited(dev
,
254 "unexpected IRQ, INT%d\n", bit
);
256 /* clear interrupt */
258 shifted_status
<< PAB_INTX_START
,
259 PAB_INTP_AMBA_MISC_STAT
);
261 } while ((shifted_status
>> PAB_INTX_START
) != 0);
264 /* read extra MSI status register */
265 msi_status
= readl_relaxed(pcie
->apb_csr_base
+ MSI_STATUS_OFFSET
);
267 /* handle MSI interrupts */
268 while (msi_status
& 1) {
269 msi_data
= readl_relaxed(pcie
->apb_csr_base
273 * MSI_STATUS_OFFSET register gets updated to zero
274 * once we pop not only the MSI data but also address
275 * from MSI hardware FIFO. So keeping these following
278 msi_addr_lo
= readl_relaxed(pcie
->apb_csr_base
+
280 msi_addr_hi
= readl_relaxed(pcie
->apb_csr_base
+
282 dev_dbg(dev
, "MSI registers, data: %08x, addr: %08x:%08x\n",
283 msi_data
, msi_addr_hi
, msi_addr_lo
);
285 virq
= irq_find_mapping(msi
->dev_domain
, msi_data
);
287 generic_handle_irq(virq
);
289 msi_status
= readl_relaxed(pcie
->apb_csr_base
+
293 /* Clear the interrupt status */
294 csr_writel(pcie
, intr_status
, PAB_INTP_AMBA_MISC_STAT
);
295 chained_irq_exit(chip
, desc
);
298 static int mobiveil_pcie_parse_dt(struct mobiveil_pcie
*pcie
)
300 struct device
*dev
= &pcie
->pdev
->dev
;
301 struct platform_device
*pdev
= pcie
->pdev
;
302 struct device_node
*node
= dev
->of_node
;
303 struct resource
*res
;
306 type
= of_get_property(node
, "device_type", NULL
);
307 if (!type
|| strcmp(type
, "pci")) {
308 dev_err(dev
, "invalid \"device_type\" %s\n", type
);
312 /* map config resource */
313 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
315 pcie
->config_axi_slave_base
= devm_pci_remap_cfg_resource(dev
, res
);
316 if (IS_ERR(pcie
->config_axi_slave_base
))
317 return PTR_ERR(pcie
->config_axi_slave_base
);
318 pcie
->ob_io_res
= res
;
320 /* map csr resource */
321 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
323 pcie
->csr_axi_slave_base
= devm_pci_remap_cfg_resource(dev
, res
);
324 if (IS_ERR(pcie
->csr_axi_slave_base
))
325 return PTR_ERR(pcie
->csr_axi_slave_base
);
326 pcie
->pcie_reg_base
= res
->start
;
328 /* map MSI config resource */
329 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "apb_csr");
330 pcie
->apb_csr_base
= devm_pci_remap_cfg_resource(dev
, res
);
331 if (IS_ERR(pcie
->apb_csr_base
))
332 return PTR_ERR(pcie
->apb_csr_base
);
334 /* read the number of windows requested */
335 if (of_property_read_u32(node
, "apio-wins", &pcie
->apio_wins
))
336 pcie
->apio_wins
= MAX_PIO_WINDOWS
;
338 if (of_property_read_u32(node
, "ppio-wins", &pcie
->ppio_wins
))
339 pcie
->ppio_wins
= MAX_PIO_WINDOWS
;
341 pcie
->irq
= platform_get_irq(pdev
, 0);
342 if (pcie
->irq
<= 0) {
343 dev_err(dev
, "failed to map IRQ: %d\n", pcie
->irq
);
347 irq_set_chained_handler_and_data(pcie
->irq
, mobiveil_pcie_isr
, pcie
);
353 * select_paged_register - routine to access paged register of root complex
355 * registers of RC are paged, for this scheme to work
356 * extracted higher 6 bits of the offset will be written to pg_sel
357 * field of PAB_CTRL register and rest of the lower 10 bits enabled with
358 * PAGE_SEL_EN are used as offset of the register.
360 static void select_paged_register(struct mobiveil_pcie
*pcie
, u32 offset
)
362 int pab_ctrl_dw
, pg_sel
;
364 /* clear pg_sel field */
365 pab_ctrl_dw
= csr_readl(pcie
, PAB_CTRL
);
366 pab_ctrl_dw
= (pab_ctrl_dw
& ~(PAGE_SEL_MASK
<< PAGE_SEL_SHIFT
));
368 /* set pg_sel field */
369 pg_sel
= (offset
>> PAGE_SEL_OFFSET_SHIFT
) & PAGE_SEL_MASK
;
370 pab_ctrl_dw
|= ((pg_sel
<< PAGE_SEL_SHIFT
));
371 csr_writel(pcie
, pab_ctrl_dw
, PAB_CTRL
);
374 static void write_paged_register(struct mobiveil_pcie
*pcie
,
377 u32 off
= (offset
& PAGE_LO_MASK
) | PAGE_SEL_EN
;
379 select_paged_register(pcie
, offset
);
380 csr_writel(pcie
, val
, off
);
383 static u32
read_paged_register(struct mobiveil_pcie
*pcie
, u32 offset
)
385 u32 off
= (offset
& PAGE_LO_MASK
) | PAGE_SEL_EN
;
387 select_paged_register(pcie
, offset
);
388 return csr_readl(pcie
, off
);
391 static void program_ib_windows(struct mobiveil_pcie
*pcie
, int win_num
,
392 int pci_addr
, u32 type
, u64 size
)
396 u64 size64
= ~(size
- 1);
398 if (win_num
>= pcie
->ppio_wins
) {
399 dev_err(&pcie
->pdev
->dev
,
400 "ERROR: max inbound windows reached !\n");
404 pio_ctrl_val
= csr_readl(pcie
, PAB_PEX_PIO_CTRL
);
406 pio_ctrl_val
| (1 << PIO_ENABLE_SHIFT
), PAB_PEX_PIO_CTRL
);
407 amap_ctrl_dw
= read_paged_register(pcie
, PAB_PEX_AMAP_CTRL(win_num
));
408 amap_ctrl_dw
= (amap_ctrl_dw
| (type
<< AMAP_CTRL_TYPE_SHIFT
));
409 amap_ctrl_dw
= (amap_ctrl_dw
| (1 << AMAP_CTRL_EN_SHIFT
));
411 write_paged_register(pcie
, amap_ctrl_dw
| lower_32_bits(size64
),
412 PAB_PEX_AMAP_CTRL(win_num
));
414 write_paged_register(pcie
, upper_32_bits(size64
),
415 PAB_EXT_PEX_AMAP_SIZEN(win_num
));
417 write_paged_register(pcie
, pci_addr
, PAB_PEX_AMAP_AXI_WIN(win_num
));
418 write_paged_register(pcie
, pci_addr
, PAB_PEX_AMAP_PEX_WIN_L(win_num
));
419 write_paged_register(pcie
, 0, PAB_PEX_AMAP_PEX_WIN_H(win_num
));
423 * routine to program the outbound windows
425 static void program_ob_windows(struct mobiveil_pcie
*pcie
, int win_num
,
426 u64 cpu_addr
, u64 pci_addr
, u32 config_io_bit
, u64 size
)
430 u64 size64
= ~(size
- 1);
432 if (win_num
>= pcie
->apio_wins
) {
433 dev_err(&pcie
->pdev
->dev
,
434 "ERROR: max outbound windows reached !\n");
439 * program Enable Bit to 1, Type Bit to (00) base 2, AXI Window Size Bit
440 * to 4 KB in PAB_AXI_AMAP_CTRL register
442 type
= config_io_bit
;
443 value
= csr_readl(pcie
, PAB_AXI_AMAP_CTRL(win_num
));
444 csr_writel(pcie
, 1 << WIN_ENABLE_SHIFT
| type
<< WIN_TYPE_SHIFT
|
445 lower_32_bits(size64
), PAB_AXI_AMAP_CTRL(win_num
));
447 write_paged_register(pcie
, upper_32_bits(size64
),
448 PAB_EXT_AXI_AMAP_SIZE(win_num
));
451 * program AXI window base with appropriate value in
452 * PAB_AXI_AMAP_AXI_WIN0 register
454 value
= csr_readl(pcie
, PAB_AXI_AMAP_AXI_WIN(win_num
));
455 csr_writel(pcie
, cpu_addr
& (~AXI_WINDOW_ALIGN_MASK
),
456 PAB_AXI_AMAP_AXI_WIN(win_num
));
458 value
= csr_readl(pcie
, PAB_AXI_AMAP_PEX_WIN_H(win_num
));
460 csr_writel(pcie
, lower_32_bits(pci_addr
),
461 PAB_AXI_AMAP_PEX_WIN_L(win_num
));
462 csr_writel(pcie
, upper_32_bits(pci_addr
),
463 PAB_AXI_AMAP_PEX_WIN_H(win_num
));
465 pcie
->ob_wins_configured
++;
468 static int mobiveil_bringup_link(struct mobiveil_pcie
*pcie
)
472 /* check if the link is up or not */
473 for (retries
= 0; retries
< LINK_WAIT_MAX_RETRIES
; retries
++) {
474 if (mobiveil_pcie_link_up(pcie
))
477 usleep_range(LINK_WAIT_MIN
, LINK_WAIT_MAX
);
479 dev_err(&pcie
->pdev
->dev
, "link never came up\n");
483 static void mobiveil_pcie_enable_msi(struct mobiveil_pcie
*pcie
)
485 phys_addr_t msg_addr
= pcie
->pcie_reg_base
;
486 struct mobiveil_msi
*msi
= &pcie
->msi
;
488 pcie
->msi
.num_of_vectors
= PCI_NUM_MSI
;
489 msi
->msi_pages_phys
= (phys_addr_t
)msg_addr
;
491 writel_relaxed(lower_32_bits(msg_addr
),
492 pcie
->apb_csr_base
+ MSI_BASE_LO_OFFSET
);
493 writel_relaxed(upper_32_bits(msg_addr
),
494 pcie
->apb_csr_base
+ MSI_BASE_HI_OFFSET
);
495 writel_relaxed(4096, pcie
->apb_csr_base
+ MSI_SIZE_OFFSET
);
496 writel_relaxed(1, pcie
->apb_csr_base
+ MSI_ENABLE_OFFSET
);
499 static int mobiveil_host_init(struct mobiveil_pcie
*pcie
)
501 u32 value
, pab_ctrl
, type
= 0;
503 struct resource_entry
*win
, *tmp
;
505 err
= mobiveil_bringup_link(pcie
);
507 dev_info(&pcie
->pdev
->dev
, "link bring-up failed\n");
511 /* setup bus numbers */
512 value
= csr_readl(pcie
, PCI_PRIMARY_BUS
);
515 csr_writel(pcie
, value
, PCI_PRIMARY_BUS
);
518 * program Bus Master Enable Bit in Command Register in PAB Config
521 value
= csr_readl(pcie
, PCI_COMMAND
);
522 csr_writel(pcie
, value
| PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
|
523 PCI_COMMAND_MASTER
, PCI_COMMAND
);
526 * program PIO Enable Bit to 1 (and PEX PIO Enable to 1) in PAB_CTRL
529 pab_ctrl
= csr_readl(pcie
, PAB_CTRL
);
530 csr_writel(pcie
, pab_ctrl
| (1 << AMBA_PIO_ENABLE_SHIFT
) |
531 (1 << PEX_PIO_ENABLE_SHIFT
), PAB_CTRL
);
533 csr_writel(pcie
, (PAB_INTP_INTX_MASK
| PAB_INTP_MSI_MASK
),
534 PAB_INTP_AMBA_MISC_ENB
);
537 * program PIO Enable Bit to 1 and Config Window Enable Bit to 1 in
538 * PAB_AXI_PIO_CTRL Register
540 value
= csr_readl(pcie
, PAB_AXI_PIO_CTRL
);
541 csr_writel(pcie
, value
| APIO_EN_MASK
, PAB_AXI_PIO_CTRL
);
544 * we'll program one outbound window for config reads and
545 * another default inbound window for all the upstream traffic
546 * rest of the outbound windows will be configured according to
547 * the "ranges" field defined in device tree
550 /* config outbound translation window */
551 program_ob_windows(pcie
, pcie
->ob_wins_configured
,
552 pcie
->ob_io_res
->start
, 0, CFG_WINDOW_TYPE
,
553 resource_size(pcie
->ob_io_res
));
555 /* memory inbound translation window */
556 program_ib_windows(pcie
, WIN_NUM_0
, 0, MEM_WINDOW_TYPE
, IB_WIN_SIZE
);
558 /* Get the I/O and memory ranges from DT */
559 resource_list_for_each_entry_safe(win
, tmp
, &pcie
->resources
) {
561 if (resource_type(win
->res
) == IORESOURCE_MEM
)
562 type
= MEM_WINDOW_TYPE
;
563 if (resource_type(win
->res
) == IORESOURCE_IO
)
564 type
= IO_WINDOW_TYPE
;
566 /* configure outbound translation window */
567 program_ob_windows(pcie
, pcie
->ob_wins_configured
,
569 win
->res
->start
- win
->offset
,
570 type
, resource_size(win
->res
));
574 /* fixup for PCIe class register */
575 value
= csr_readl(pcie
, PAB_INTP_AXI_PIO_CLASS
);
577 value
|= (PCI_CLASS_BRIDGE_PCI
<< 16);
578 csr_writel(pcie
, value
, PAB_INTP_AXI_PIO_CLASS
);
580 /* setup MSI hardware registers */
581 mobiveil_pcie_enable_msi(pcie
);
586 static void mobiveil_mask_intx_irq(struct irq_data
*data
)
588 struct irq_desc
*desc
= irq_to_desc(data
->irq
);
589 struct mobiveil_pcie
*pcie
;
591 u32 mask
, shifted_val
;
593 pcie
= irq_desc_get_chip_data(desc
);
594 mask
= 1 << ((data
->hwirq
+ PAB_INTX_START
) - 1);
595 raw_spin_lock_irqsave(&pcie
->intx_mask_lock
, flags
);
596 shifted_val
= csr_readl(pcie
, PAB_INTP_AMBA_MISC_ENB
);
597 csr_writel(pcie
, (shifted_val
& (~mask
)), PAB_INTP_AMBA_MISC_ENB
);
598 raw_spin_unlock_irqrestore(&pcie
->intx_mask_lock
, flags
);
601 static void mobiveil_unmask_intx_irq(struct irq_data
*data
)
603 struct irq_desc
*desc
= irq_to_desc(data
->irq
);
604 struct mobiveil_pcie
*pcie
;
606 u32 shifted_val
, mask
;
608 pcie
= irq_desc_get_chip_data(desc
);
609 mask
= 1 << ((data
->hwirq
+ PAB_INTX_START
) - 1);
610 raw_spin_lock_irqsave(&pcie
->intx_mask_lock
, flags
);
611 shifted_val
= csr_readl(pcie
, PAB_INTP_AMBA_MISC_ENB
);
612 csr_writel(pcie
, (shifted_val
| mask
), PAB_INTP_AMBA_MISC_ENB
);
613 raw_spin_unlock_irqrestore(&pcie
->intx_mask_lock
, flags
);
616 static struct irq_chip intx_irq_chip
= {
617 .name
= "mobiveil_pcie:intx",
618 .irq_enable
= mobiveil_unmask_intx_irq
,
619 .irq_disable
= mobiveil_mask_intx_irq
,
620 .irq_mask
= mobiveil_mask_intx_irq
,
621 .irq_unmask
= mobiveil_unmask_intx_irq
,
624 /* routine to setup the INTx related data */
625 static int mobiveil_pcie_intx_map(struct irq_domain
*domain
, unsigned int irq
,
626 irq_hw_number_t hwirq
)
628 irq_set_chip_and_handler(irq
, &intx_irq_chip
, handle_level_irq
);
629 irq_set_chip_data(irq
, domain
->host_data
);
633 /* INTx domain operations structure */
634 static const struct irq_domain_ops intx_domain_ops
= {
635 .map
= mobiveil_pcie_intx_map
,
638 static struct irq_chip mobiveil_msi_irq_chip
= {
639 .name
= "Mobiveil PCIe MSI",
640 .irq_mask
= pci_msi_mask_irq
,
641 .irq_unmask
= pci_msi_unmask_irq
,
644 static struct msi_domain_info mobiveil_msi_domain_info
= {
645 .flags
= (MSI_FLAG_USE_DEF_DOM_OPS
| MSI_FLAG_USE_DEF_CHIP_OPS
|
647 .chip
= &mobiveil_msi_irq_chip
,
650 static void mobiveil_compose_msi_msg(struct irq_data
*data
, struct msi_msg
*msg
)
652 struct mobiveil_pcie
*pcie
= irq_data_get_irq_chip_data(data
);
653 phys_addr_t addr
= pcie
->pcie_reg_base
+ (data
->hwirq
* sizeof(int));
655 msg
->address_lo
= lower_32_bits(addr
);
656 msg
->address_hi
= upper_32_bits(addr
);
657 msg
->data
= data
->hwirq
;
659 dev_dbg(&pcie
->pdev
->dev
, "msi#%d address_hi %#x address_lo %#x\n",
660 (int)data
->hwirq
, msg
->address_hi
, msg
->address_lo
);
663 static int mobiveil_msi_set_affinity(struct irq_data
*irq_data
,
664 const struct cpumask
*mask
, bool force
)
669 static struct irq_chip mobiveil_msi_bottom_irq_chip
= {
670 .name
= "Mobiveil MSI",
671 .irq_compose_msi_msg
= mobiveil_compose_msi_msg
,
672 .irq_set_affinity
= mobiveil_msi_set_affinity
,
675 static int mobiveil_irq_msi_domain_alloc(struct irq_domain
*domain
,
676 unsigned int virq
, unsigned int nr_irqs
, void *args
)
678 struct mobiveil_pcie
*pcie
= domain
->host_data
;
679 struct mobiveil_msi
*msi
= &pcie
->msi
;
682 WARN_ON(nr_irqs
!= 1);
683 mutex_lock(&msi
->lock
);
685 bit
= find_first_zero_bit(msi
->msi_irq_in_use
, msi
->num_of_vectors
);
686 if (bit
>= msi
->num_of_vectors
) {
687 mutex_unlock(&msi
->lock
);
691 set_bit(bit
, msi
->msi_irq_in_use
);
693 mutex_unlock(&msi
->lock
);
695 irq_domain_set_info(domain
, virq
, bit
, &mobiveil_msi_bottom_irq_chip
,
696 domain
->host_data
, handle_level_irq
,
701 static void mobiveil_irq_msi_domain_free(struct irq_domain
*domain
,
702 unsigned int virq
, unsigned int nr_irqs
)
704 struct irq_data
*d
= irq_domain_get_irq_data(domain
, virq
);
705 struct mobiveil_pcie
*pcie
= irq_data_get_irq_chip_data(d
);
706 struct mobiveil_msi
*msi
= &pcie
->msi
;
708 mutex_lock(&msi
->lock
);
710 if (!test_bit(d
->hwirq
, msi
->msi_irq_in_use
)) {
711 dev_err(&pcie
->pdev
->dev
, "trying to free unused MSI#%lu\n",
714 __clear_bit(d
->hwirq
, msi
->msi_irq_in_use
);
717 mutex_unlock(&msi
->lock
);
719 static const struct irq_domain_ops msi_domain_ops
= {
720 .alloc
= mobiveil_irq_msi_domain_alloc
,
721 .free
= mobiveil_irq_msi_domain_free
,
724 static int mobiveil_allocate_msi_domains(struct mobiveil_pcie
*pcie
)
726 struct device
*dev
= &pcie
->pdev
->dev
;
727 struct fwnode_handle
*fwnode
= of_node_to_fwnode(dev
->of_node
);
728 struct mobiveil_msi
*msi
= &pcie
->msi
;
730 mutex_init(&pcie
->msi
.lock
);
731 msi
->dev_domain
= irq_domain_add_linear(NULL
, msi
->num_of_vectors
,
732 &msi_domain_ops
, pcie
);
733 if (!msi
->dev_domain
) {
734 dev_err(dev
, "failed to create IRQ domain\n");
738 msi
->msi_domain
= pci_msi_create_irq_domain(fwnode
,
739 &mobiveil_msi_domain_info
, msi
->dev_domain
);
740 if (!msi
->msi_domain
) {
741 dev_err(dev
, "failed to create MSI domain\n");
742 irq_domain_remove(msi
->dev_domain
);
748 static int mobiveil_pcie_init_irq_domain(struct mobiveil_pcie
*pcie
)
750 struct device
*dev
= &pcie
->pdev
->dev
;
751 struct device_node
*node
= dev
->of_node
;
755 pcie
->intx_domain
= irq_domain_add_linear(node
,
756 PCI_NUM_INTX
, &intx_domain_ops
, pcie
);
758 if (!pcie
->intx_domain
) {
759 dev_err(dev
, "Failed to get a INTx IRQ domain\n");
763 raw_spin_lock_init(&pcie
->intx_mask_lock
);
766 ret
= mobiveil_allocate_msi_domains(pcie
);
773 static int mobiveil_pcie_probe(struct platform_device
*pdev
)
775 struct mobiveil_pcie
*pcie
;
777 struct pci_bus
*child
;
778 struct pci_host_bridge
*bridge
;
779 struct device
*dev
= &pdev
->dev
;
780 resource_size_t iobase
;
783 /* allocate the PCIe port */
784 bridge
= devm_pci_alloc_host_bridge(dev
, sizeof(*pcie
));
788 pcie
= pci_host_bridge_priv(bridge
);
794 ret
= mobiveil_pcie_parse_dt(pcie
);
796 dev_err(dev
, "Parsing DT failed, ret: %x\n", ret
);
800 INIT_LIST_HEAD(&pcie
->resources
);
802 /* parse the host bridge base addresses from the device tree file */
803 ret
= devm_of_pci_get_host_bridge_resources(dev
, 0, 0xff,
804 &pcie
->resources
, &iobase
);
806 dev_err(dev
, "Getting bridge resources failed\n");
811 * configure all inbound and outbound windows and prepare the RC for
814 ret
= mobiveil_host_init(pcie
);
816 dev_err(dev
, "Failed to initialize host\n");
820 /* initialize the IRQ domains */
821 ret
= mobiveil_pcie_init_irq_domain(pcie
);
823 dev_err(dev
, "Failed creating IRQ Domain\n");
827 ret
= devm_request_pci_bus_resources(dev
, &pcie
->resources
);
831 /* Initialize bridge */
832 list_splice_init(&pcie
->resources
, &bridge
->windows
);
833 bridge
->dev
.parent
= dev
;
834 bridge
->sysdata
= pcie
;
835 bridge
->busnr
= pcie
->root_bus_nr
;
836 bridge
->ops
= &mobiveil_pcie_ops
;
837 bridge
->map_irq
= of_irq_parse_and_map_pci
;
838 bridge
->swizzle_irq
= pci_common_swizzle
;
840 /* setup the kernel resources for the newly added PCIe root bus */
841 ret
= pci_scan_root_bus_bridge(bridge
);
847 pci_assign_unassigned_bus_resources(bus
);
848 list_for_each_entry(child
, &bus
->children
, node
)
849 pcie_bus_configure_settings(child
);
850 pci_bus_add_devices(bus
);
854 pci_free_resource_list(&pcie
->resources
);
858 static const struct of_device_id mobiveil_pcie_of_match
[] = {
859 {.compatible
= "mbvl,gpex40-pcie",},
863 MODULE_DEVICE_TABLE(of
, mobiveil_pcie_of_match
);
865 static struct platform_driver mobiveil_pcie_driver
= {
866 .probe
= mobiveil_pcie_probe
,
868 .name
= "mobiveil-pcie",
869 .of_match_table
= mobiveil_pcie_of_match
,
870 .suppress_bind_attrs
= true,
874 builtin_platform_driver(mobiveil_pcie_driver
);
876 MODULE_LICENSE("GPL v2");
877 MODULE_DESCRIPTION("Mobiveil PCIe host controller driver");
878 MODULE_AUTHOR("Subrahmanya Lingappa <l.subrahmanya@mobiveil.co.in>");