1 // SPDX-License-Identifier: GPL-2.0
3 * MediaTek PCIe host controller driver.
5 * Copyright (c) 2017 MediaTek Inc.
6 * Author: Ryder Lee <ryder.lee@mediatek.com>
7 * Honghui Zhang <honghui.zhang@mediatek.com>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/iopoll.h>
13 #include <linux/irq.h>
14 #include <linux/irqdomain.h>
15 #include <linux/kernel.h>
16 #include <linux/of_address.h>
17 #include <linux/of_pci.h>
18 #include <linux/of_platform.h>
19 #include <linux/pci.h>
20 #include <linux/phy/phy.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/reset.h>
25 /* PCIe shared registers */
26 #define PCIE_SYS_CFG 0x00
27 #define PCIE_INT_ENABLE 0x0c
28 #define PCIE_CFG_ADDR 0x20
29 #define PCIE_CFG_DATA 0x24
31 /* PCIe per port registers */
32 #define PCIE_BAR0_SETUP 0x10
33 #define PCIE_CLASS 0x34
34 #define PCIE_LINK_STATUS 0x50
36 #define PCIE_PORT_INT_EN(x) BIT(20 + (x))
37 #define PCIE_PORT_PERST(x) BIT(1 + (x))
38 #define PCIE_PORT_LINKUP BIT(0)
39 #define PCIE_BAR_MAP_MAX GENMASK(31, 16)
41 #define PCIE_BAR_ENABLE BIT(0)
42 #define PCIE_REVISION_ID BIT(0)
43 #define PCIE_CLASS_CODE (0x60400 << 8)
44 #define PCIE_CONF_REG(regn) (((regn) & GENMASK(7, 2)) | \
45 ((((regn) >> 8) & GENMASK(3, 0)) << 24))
46 #define PCIE_CONF_FUN(fun) (((fun) << 8) & GENMASK(10, 8))
47 #define PCIE_CONF_DEV(dev) (((dev) << 11) & GENMASK(15, 11))
48 #define PCIE_CONF_BUS(bus) (((bus) << 16) & GENMASK(23, 16))
49 #define PCIE_CONF_ADDR(regn, fun, dev, bus) \
50 (PCIE_CONF_REG(regn) | PCIE_CONF_FUN(fun) | \
51 PCIE_CONF_DEV(dev) | PCIE_CONF_BUS(bus))
53 /* MediaTek specific configuration registers */
54 #define PCIE_FTS_NUM 0x70c
55 #define PCIE_FTS_NUM_MASK GENMASK(15, 8)
56 #define PCIE_FTS_NUM_L0(x) ((x) & 0xff << 8)
58 #define PCIE_FC_CREDIT 0x73c
59 #define PCIE_FC_CREDIT_MASK (GENMASK(31, 31) | GENMASK(28, 16))
60 #define PCIE_FC_CREDIT_VAL(x) ((x) << 16)
62 /* PCIe V2 share registers */
63 #define PCIE_SYS_CFG_V2 0x0
64 #define PCIE_CSR_LTSSM_EN(x) BIT(0 + (x) * 8)
65 #define PCIE_CSR_ASPM_L1_EN(x) BIT(1 + (x) * 8)
67 /* PCIe V2 per-port registers */
68 #define PCIE_MSI_VECTOR 0x0c0
69 #define PCIE_INT_MASK 0x420
70 #define INTX_MASK GENMASK(19, 16)
72 #define PCIE_INT_STATUS 0x424
73 #define MSI_STATUS BIT(23)
74 #define PCIE_IMSI_STATUS 0x42c
75 #define PCIE_IMSI_ADDR 0x430
76 #define MSI_MASK BIT(23)
77 #define MTK_MSI_IRQS_NUM 32
79 #define PCIE_AHB_TRANS_BASE0_L 0x438
80 #define PCIE_AHB_TRANS_BASE0_H 0x43c
81 #define AHB2PCIE_SIZE(x) ((x) & GENMASK(4, 0))
82 #define PCIE_AXI_WINDOW0 0x448
83 #define WIN_ENABLE BIT(7)
85 /* PCIe V2 configuration transaction header */
86 #define PCIE_CFG_HEADER0 0x460
87 #define PCIE_CFG_HEADER1 0x464
88 #define PCIE_CFG_HEADER2 0x468
89 #define PCIE_CFG_WDATA 0x470
90 #define PCIE_APP_TLP_REQ 0x488
91 #define PCIE_CFG_RDATA 0x48c
92 #define APP_CFG_REQ BIT(0)
93 #define APP_CPL_STATUS GENMASK(7, 5)
95 #define CFG_WRRD_TYPE_0 4
99 #define CFG_DW0_LENGTH(length) ((length) & GENMASK(9, 0))
100 #define CFG_DW0_TYPE(type) (((type) << 24) & GENMASK(28, 24))
101 #define CFG_DW0_FMT(fmt) (((fmt) << 29) & GENMASK(31, 29))
102 #define CFG_DW2_REGN(regn) ((regn) & GENMASK(11, 2))
103 #define CFG_DW2_FUN(fun) (((fun) << 16) & GENMASK(18, 16))
104 #define CFG_DW2_DEV(dev) (((dev) << 19) & GENMASK(23, 19))
105 #define CFG_DW2_BUS(bus) (((bus) << 24) & GENMASK(31, 24))
106 #define CFG_HEADER_DW0(type, fmt) \
107 (CFG_DW0_LENGTH(1) | CFG_DW0_TYPE(type) | CFG_DW0_FMT(fmt))
108 #define CFG_HEADER_DW1(where, size) \
109 (GENMASK(((size) - 1), 0) << ((where) & 0x3))
110 #define CFG_HEADER_DW2(regn, fun, dev, bus) \
111 (CFG_DW2_REGN(regn) | CFG_DW2_FUN(fun) | \
112 CFG_DW2_DEV(dev) | CFG_DW2_BUS(bus))
114 #define PCIE_RST_CTRL 0x510
115 #define PCIE_PHY_RSTB BIT(0)
116 #define PCIE_PIPE_SRSTB BIT(1)
117 #define PCIE_MAC_SRSTB BIT(2)
118 #define PCIE_CRSTB BIT(3)
119 #define PCIE_PERSTB BIT(8)
120 #define PCIE_LINKDOWN_RST_EN GENMASK(15, 13)
121 #define PCIE_LINK_STATUS_V2 0x804
122 #define PCIE_PORT_LINKUP_V2 BIT(10)
124 struct mtk_pcie_port
;
127 * struct mtk_pcie_soc - differentiate between host generations
128 * @has_msi: whether this host supports MSI interrupts or not
129 * @ops: pointer to configuration access functions
130 * @startup: pointer to controller setting functions
131 * @setup_irq: pointer to initialize IRQ functions
133 struct mtk_pcie_soc
{
136 int (*startup
)(struct mtk_pcie_port
*port
);
137 int (*setup_irq
)(struct mtk_pcie_port
*port
, struct device_node
*node
);
141 * struct mtk_pcie_port - PCIe port information
142 * @base: IO mapped register base
144 * @pcie: pointer to PCIe host info
145 * @reset: pointer to port reset control
146 * @sys_ck: pointer to transaction/data link layer clock
147 * @ahb_ck: pointer to AHB slave interface operating clock for CSR access
148 * and RC initiated MMIO access
149 * @axi_ck: pointer to application layer MMIO channel operating clock
150 * @aux_ck: pointer to pe2_mac_bridge and pe2_mac_core operating clock
151 * when pcie_mac_ck/pcie_pipe_ck is turned off
152 * @obff_ck: pointer to OBFF functional block operating clock
153 * @pipe_ck: pointer to LTSSM and PHY/MAC layer operating clock
154 * @phy: pointer to PHY control block
157 * @irq_domain: legacy INTx IRQ domain
158 * @msi_domain: MSI IRQ domain
159 * @msi_irq_in_use: bit map for assigned MSI IRQ
161 struct mtk_pcie_port
{
163 struct list_head list
;
164 struct mtk_pcie
*pcie
;
165 struct reset_control
*reset
;
175 struct irq_domain
*irq_domain
;
176 struct irq_domain
*msi_domain
;
177 DECLARE_BITMAP(msi_irq_in_use
, MTK_MSI_IRQS_NUM
);
181 * struct mtk_pcie - PCIe host information
182 * @dev: pointer to PCIe device
183 * @base: IO mapped register base
184 * @free_ck: free-run reference clock
187 * @mem: non-prefetchable memory resource
189 * @offset: IO / Memory offset
190 * @ports: pointer to PCIe port information
191 * @soc: pointer to SoC-dependent operations
201 struct resource busn
;
206 struct list_head ports
;
207 const struct mtk_pcie_soc
*soc
;
210 static void mtk_pcie_subsys_powerdown(struct mtk_pcie
*pcie
)
212 struct device
*dev
= pcie
->dev
;
214 clk_disable_unprepare(pcie
->free_ck
);
216 if (dev
->pm_domain
) {
217 pm_runtime_put_sync(dev
);
218 pm_runtime_disable(dev
);
222 static void mtk_pcie_port_free(struct mtk_pcie_port
*port
)
224 struct mtk_pcie
*pcie
= port
->pcie
;
225 struct device
*dev
= pcie
->dev
;
227 devm_iounmap(dev
, port
->base
);
228 list_del(&port
->list
);
229 devm_kfree(dev
, port
);
232 static void mtk_pcie_put_resources(struct mtk_pcie
*pcie
)
234 struct mtk_pcie_port
*port
, *tmp
;
236 list_for_each_entry_safe(port
, tmp
, &pcie
->ports
, list
) {
237 phy_power_off(port
->phy
);
239 clk_disable_unprepare(port
->pipe_ck
);
240 clk_disable_unprepare(port
->obff_ck
);
241 clk_disable_unprepare(port
->axi_ck
);
242 clk_disable_unprepare(port
->aux_ck
);
243 clk_disable_unprepare(port
->ahb_ck
);
244 clk_disable_unprepare(port
->sys_ck
);
245 mtk_pcie_port_free(port
);
248 mtk_pcie_subsys_powerdown(pcie
);
251 static int mtk_pcie_check_cfg_cpld(struct mtk_pcie_port
*port
)
256 err
= readl_poll_timeout_atomic(port
->base
+ PCIE_APP_TLP_REQ
, val
,
257 !(val
& APP_CFG_REQ
), 10,
258 100 * USEC_PER_MSEC
);
260 return PCIBIOS_SET_FAILED
;
262 if (readl(port
->base
+ PCIE_APP_TLP_REQ
) & APP_CPL_STATUS
)
263 return PCIBIOS_SET_FAILED
;
265 return PCIBIOS_SUCCESSFUL
;
268 static int mtk_pcie_hw_rd_cfg(struct mtk_pcie_port
*port
, u32 bus
, u32 devfn
,
269 int where
, int size
, u32
*val
)
273 /* Write PCIe configuration transaction header for Cfgrd */
274 writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0
, CFG_RD_FMT
),
275 port
->base
+ PCIE_CFG_HEADER0
);
276 writel(CFG_HEADER_DW1(where
, size
), port
->base
+ PCIE_CFG_HEADER1
);
277 writel(CFG_HEADER_DW2(where
, PCI_FUNC(devfn
), PCI_SLOT(devfn
), bus
),
278 port
->base
+ PCIE_CFG_HEADER2
);
280 /* Trigger h/w to transmit Cfgrd TLP */
281 tmp
= readl(port
->base
+ PCIE_APP_TLP_REQ
);
283 writel(tmp
, port
->base
+ PCIE_APP_TLP_REQ
);
285 /* Check completion status */
286 if (mtk_pcie_check_cfg_cpld(port
))
287 return PCIBIOS_SET_FAILED
;
289 /* Read cpld payload of Cfgrd */
290 *val
= readl(port
->base
+ PCIE_CFG_RDATA
);
293 *val
= (*val
>> (8 * (where
& 3))) & 0xff;
295 *val
= (*val
>> (8 * (where
& 3))) & 0xffff;
297 return PCIBIOS_SUCCESSFUL
;
300 static int mtk_pcie_hw_wr_cfg(struct mtk_pcie_port
*port
, u32 bus
, u32 devfn
,
301 int where
, int size
, u32 val
)
303 /* Write PCIe configuration transaction header for Cfgwr */
304 writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0
, CFG_WR_FMT
),
305 port
->base
+ PCIE_CFG_HEADER0
);
306 writel(CFG_HEADER_DW1(where
, size
), port
->base
+ PCIE_CFG_HEADER1
);
307 writel(CFG_HEADER_DW2(where
, PCI_FUNC(devfn
), PCI_SLOT(devfn
), bus
),
308 port
->base
+ PCIE_CFG_HEADER2
);
310 /* Write Cfgwr data */
311 val
= val
<< 8 * (where
& 3);
312 writel(val
, port
->base
+ PCIE_CFG_WDATA
);
314 /* Trigger h/w to transmit Cfgwr TLP */
315 val
= readl(port
->base
+ PCIE_APP_TLP_REQ
);
317 writel(val
, port
->base
+ PCIE_APP_TLP_REQ
);
319 /* Check completion status */
320 return mtk_pcie_check_cfg_cpld(port
);
323 static struct mtk_pcie_port
*mtk_pcie_find_port(struct pci_bus
*bus
,
326 struct mtk_pcie
*pcie
= bus
->sysdata
;
327 struct mtk_pcie_port
*port
;
329 list_for_each_entry(port
, &pcie
->ports
, list
)
330 if (port
->slot
== PCI_SLOT(devfn
))
336 static int mtk_pcie_config_read(struct pci_bus
*bus
, unsigned int devfn
,
337 int where
, int size
, u32
*val
)
339 struct mtk_pcie_port
*port
;
340 u32 bn
= bus
->number
;
343 port
= mtk_pcie_find_port(bus
, devfn
);
346 return PCIBIOS_DEVICE_NOT_FOUND
;
349 ret
= mtk_pcie_hw_rd_cfg(port
, bn
, devfn
, where
, size
, val
);
356 static int mtk_pcie_config_write(struct pci_bus
*bus
, unsigned int devfn
,
357 int where
, int size
, u32 val
)
359 struct mtk_pcie_port
*port
;
360 u32 bn
= bus
->number
;
362 port
= mtk_pcie_find_port(bus
, devfn
);
364 return PCIBIOS_DEVICE_NOT_FOUND
;
366 return mtk_pcie_hw_wr_cfg(port
, bn
, devfn
, where
, size
, val
);
369 static struct pci_ops mtk_pcie_ops_v2
= {
370 .read
= mtk_pcie_config_read
,
371 .write
= mtk_pcie_config_write
,
374 static int mtk_pcie_startup_port_v2(struct mtk_pcie_port
*port
)
376 struct mtk_pcie
*pcie
= port
->pcie
;
377 struct resource
*mem
= &pcie
->mem
;
382 /* MT7622 platforms need to enable LTSSM and ASPM from PCIe subsys */
384 val
= readl(pcie
->base
+ PCIE_SYS_CFG_V2
);
385 val
|= PCIE_CSR_LTSSM_EN(port
->slot
) |
386 PCIE_CSR_ASPM_L1_EN(port
->slot
);
387 writel(val
, pcie
->base
+ PCIE_SYS_CFG_V2
);
390 /* Assert all reset signals */
391 writel(0, port
->base
+ PCIE_RST_CTRL
);
394 * Enable PCIe link down reset, if link status changed from link up to
395 * link down, this will reset MAC control registers and configuration
398 writel(PCIE_LINKDOWN_RST_EN
, port
->base
+ PCIE_RST_CTRL
);
400 /* De-assert PHY, PE, PIPE, MAC and configuration reset */
401 val
= readl(port
->base
+ PCIE_RST_CTRL
);
402 val
|= PCIE_PHY_RSTB
| PCIE_PERSTB
| PCIE_PIPE_SRSTB
|
403 PCIE_MAC_SRSTB
| PCIE_CRSTB
;
404 writel(val
, port
->base
+ PCIE_RST_CTRL
);
406 /* 100ms timeout value should be enough for Gen1/2 training */
407 err
= readl_poll_timeout(port
->base
+ PCIE_LINK_STATUS_V2
, val
,
408 !!(val
& PCIE_PORT_LINKUP_V2
), 20,
409 100 * USEC_PER_MSEC
);
414 val
= readl(port
->base
+ PCIE_INT_MASK
);
416 writel(val
, port
->base
+ PCIE_INT_MASK
);
418 /* Set AHB to PCIe translation windows */
419 size
= mem
->end
- mem
->start
;
420 val
= lower_32_bits(mem
->start
) | AHB2PCIE_SIZE(fls(size
));
421 writel(val
, port
->base
+ PCIE_AHB_TRANS_BASE0_L
);
423 val
= upper_32_bits(mem
->start
);
424 writel(val
, port
->base
+ PCIE_AHB_TRANS_BASE0_H
);
426 /* Set PCIe to AXI translation memory space.*/
427 val
= fls(0xffffffff) | WIN_ENABLE
;
428 writel(val
, port
->base
+ PCIE_AXI_WINDOW0
);
433 static int mtk_pcie_msi_alloc(struct mtk_pcie_port
*port
)
437 msi
= find_first_zero_bit(port
->msi_irq_in_use
, MTK_MSI_IRQS_NUM
);
438 if (msi
< MTK_MSI_IRQS_NUM
)
439 set_bit(msi
, port
->msi_irq_in_use
);
446 static void mtk_pcie_msi_free(struct mtk_pcie_port
*port
, unsigned long hwirq
)
448 clear_bit(hwirq
, port
->msi_irq_in_use
);
451 static int mtk_pcie_msi_setup_irq(struct msi_controller
*chip
,
452 struct pci_dev
*pdev
, struct msi_desc
*desc
)
454 struct mtk_pcie_port
*port
;
458 phys_addr_t msg_addr
;
460 port
= mtk_pcie_find_port(pdev
->bus
, pdev
->devfn
);
464 hwirq
= mtk_pcie_msi_alloc(port
);
468 irq
= irq_create_mapping(port
->msi_domain
, hwirq
);
470 mtk_pcie_msi_free(port
, hwirq
);
474 chip
->dev
= &pdev
->dev
;
476 irq_set_msi_desc(irq
, desc
);
478 /* MT2712/MT7622 only support 32-bit MSI addresses */
479 msg_addr
= virt_to_phys(port
->base
+ PCIE_MSI_VECTOR
);
481 msg
.address_lo
= lower_32_bits(msg_addr
);
484 pci_write_msi_msg(irq
, &msg
);
489 static void mtk_msi_teardown_irq(struct msi_controller
*chip
, unsigned int irq
)
491 struct pci_dev
*pdev
= to_pci_dev(chip
->dev
);
492 struct irq_data
*d
= irq_get_irq_data(irq
);
493 irq_hw_number_t hwirq
= irqd_to_hwirq(d
);
494 struct mtk_pcie_port
*port
;
496 port
= mtk_pcie_find_port(pdev
->bus
, pdev
->devfn
);
500 irq_dispose_mapping(irq
);
501 mtk_pcie_msi_free(port
, hwirq
);
504 static struct msi_controller mtk_pcie_msi_chip
= {
505 .setup_irq
= mtk_pcie_msi_setup_irq
,
506 .teardown_irq
= mtk_msi_teardown_irq
,
509 static struct irq_chip mtk_msi_irq_chip
= {
510 .name
= "MTK PCIe MSI",
511 .irq_enable
= pci_msi_unmask_irq
,
512 .irq_disable
= pci_msi_mask_irq
,
513 .irq_mask
= pci_msi_mask_irq
,
514 .irq_unmask
= pci_msi_unmask_irq
,
517 static int mtk_pcie_msi_map(struct irq_domain
*domain
, unsigned int irq
,
518 irq_hw_number_t hwirq
)
520 irq_set_chip_and_handler(irq
, &mtk_msi_irq_chip
, handle_simple_irq
);
521 irq_set_chip_data(irq
, domain
->host_data
);
526 static const struct irq_domain_ops msi_domain_ops
= {
527 .map
= mtk_pcie_msi_map
,
530 static void mtk_pcie_enable_msi(struct mtk_pcie_port
*port
)
533 phys_addr_t msg_addr
;
535 msg_addr
= virt_to_phys(port
->base
+ PCIE_MSI_VECTOR
);
536 val
= lower_32_bits(msg_addr
);
537 writel(val
, port
->base
+ PCIE_IMSI_ADDR
);
539 val
= readl(port
->base
+ PCIE_INT_MASK
);
541 writel(val
, port
->base
+ PCIE_INT_MASK
);
544 static int mtk_pcie_intx_map(struct irq_domain
*domain
, unsigned int irq
,
545 irq_hw_number_t hwirq
)
547 irq_set_chip_and_handler(irq
, &dummy_irq_chip
, handle_simple_irq
);
548 irq_set_chip_data(irq
, domain
->host_data
);
553 static const struct irq_domain_ops intx_domain_ops
= {
554 .map
= mtk_pcie_intx_map
,
557 static int mtk_pcie_init_irq_domain(struct mtk_pcie_port
*port
,
558 struct device_node
*node
)
560 struct device
*dev
= port
->pcie
->dev
;
561 struct device_node
*pcie_intc_node
;
564 pcie_intc_node
= of_get_next_child(node
, NULL
);
565 if (!pcie_intc_node
) {
566 dev_err(dev
, "no PCIe Intc node found\n");
570 port
->irq_domain
= irq_domain_add_linear(pcie_intc_node
, PCI_NUM_INTX
,
571 &intx_domain_ops
, port
);
572 if (!port
->irq_domain
) {
573 dev_err(dev
, "failed to get INTx IRQ domain\n");
577 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
578 port
->msi_domain
= irq_domain_add_linear(node
, MTK_MSI_IRQS_NUM
,
581 if (!port
->msi_domain
) {
582 dev_err(dev
, "failed to create MSI IRQ domain\n");
585 mtk_pcie_enable_msi(port
);
591 static irqreturn_t
mtk_pcie_intr_handler(int irq
, void *data
)
593 struct mtk_pcie_port
*port
= (struct mtk_pcie_port
*)data
;
594 unsigned long status
;
596 u32 bit
= INTX_SHIFT
;
598 while ((status
= readl(port
->base
+ PCIE_INT_STATUS
)) & INTX_MASK
) {
599 for_each_set_bit_from(bit
, &status
, PCI_NUM_INTX
+ INTX_SHIFT
) {
601 writel(1 << bit
, port
->base
+ PCIE_INT_STATUS
);
602 virq
= irq_find_mapping(port
->irq_domain
,
604 generic_handle_irq(virq
);
608 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
609 while ((status
= readl(port
->base
+ PCIE_INT_STATUS
)) & MSI_STATUS
) {
610 unsigned long imsi_status
;
612 while ((imsi_status
= readl(port
->base
+ PCIE_IMSI_STATUS
))) {
613 for_each_set_bit(bit
, &imsi_status
, MTK_MSI_IRQS_NUM
) {
615 writel(1 << bit
, port
->base
+ PCIE_IMSI_STATUS
);
616 virq
= irq_find_mapping(port
->msi_domain
, bit
);
617 generic_handle_irq(virq
);
620 /* Clear MSI interrupt status */
621 writel(MSI_STATUS
, port
->base
+ PCIE_INT_STATUS
);
628 static int mtk_pcie_setup_irq(struct mtk_pcie_port
*port
,
629 struct device_node
*node
)
631 struct mtk_pcie
*pcie
= port
->pcie
;
632 struct device
*dev
= pcie
->dev
;
633 struct platform_device
*pdev
= to_platform_device(dev
);
636 irq
= platform_get_irq(pdev
, port
->slot
);
637 err
= devm_request_irq(dev
, irq
, mtk_pcie_intr_handler
,
638 IRQF_SHARED
, "mtk-pcie", port
);
640 dev_err(dev
, "unable to request IRQ %d\n", irq
);
644 err
= mtk_pcie_init_irq_domain(port
, node
);
646 dev_err(dev
, "failed to init PCIe IRQ domain\n");
653 static void __iomem
*mtk_pcie_map_bus(struct pci_bus
*bus
,
654 unsigned int devfn
, int where
)
656 struct mtk_pcie
*pcie
= bus
->sysdata
;
658 writel(PCIE_CONF_ADDR(where
, PCI_FUNC(devfn
), PCI_SLOT(devfn
),
659 bus
->number
), pcie
->base
+ PCIE_CFG_ADDR
);
661 return pcie
->base
+ PCIE_CFG_DATA
+ (where
& 3);
664 static struct pci_ops mtk_pcie_ops
= {
665 .map_bus
= mtk_pcie_map_bus
,
666 .read
= pci_generic_config_read
,
667 .write
= pci_generic_config_write
,
670 static int mtk_pcie_startup_port(struct mtk_pcie_port
*port
)
672 struct mtk_pcie
*pcie
= port
->pcie
;
673 u32 func
= PCI_FUNC(port
->slot
<< 3);
674 u32 slot
= PCI_SLOT(port
->slot
<< 3);
678 /* assert port PERST_N */
679 val
= readl(pcie
->base
+ PCIE_SYS_CFG
);
680 val
|= PCIE_PORT_PERST(port
->slot
);
681 writel(val
, pcie
->base
+ PCIE_SYS_CFG
);
683 /* de-assert port PERST_N */
684 val
= readl(pcie
->base
+ PCIE_SYS_CFG
);
685 val
&= ~PCIE_PORT_PERST(port
->slot
);
686 writel(val
, pcie
->base
+ PCIE_SYS_CFG
);
688 /* 100ms timeout value should be enough for Gen1/2 training */
689 err
= readl_poll_timeout(port
->base
+ PCIE_LINK_STATUS
, val
,
690 !!(val
& PCIE_PORT_LINKUP
), 20,
691 100 * USEC_PER_MSEC
);
695 /* enable interrupt */
696 val
= readl(pcie
->base
+ PCIE_INT_ENABLE
);
697 val
|= PCIE_PORT_INT_EN(port
->slot
);
698 writel(val
, pcie
->base
+ PCIE_INT_ENABLE
);
700 /* map to all DDR region. We need to set it before cfg operation. */
701 writel(PCIE_BAR_MAP_MAX
| PCIE_BAR_ENABLE
,
702 port
->base
+ PCIE_BAR0_SETUP
);
704 /* configure class code and revision ID */
705 writel(PCIE_CLASS_CODE
| PCIE_REVISION_ID
, port
->base
+ PCIE_CLASS
);
707 /* configure FC credit */
708 writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT
, func
, slot
, 0),
709 pcie
->base
+ PCIE_CFG_ADDR
);
710 val
= readl(pcie
->base
+ PCIE_CFG_DATA
);
711 val
&= ~PCIE_FC_CREDIT_MASK
;
712 val
|= PCIE_FC_CREDIT_VAL(0x806c);
713 writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT
, func
, slot
, 0),
714 pcie
->base
+ PCIE_CFG_ADDR
);
715 writel(val
, pcie
->base
+ PCIE_CFG_DATA
);
717 /* configure RC FTS number to 250 when it leaves L0s */
718 writel(PCIE_CONF_ADDR(PCIE_FTS_NUM
, func
, slot
, 0),
719 pcie
->base
+ PCIE_CFG_ADDR
);
720 val
= readl(pcie
->base
+ PCIE_CFG_DATA
);
721 val
&= ~PCIE_FTS_NUM_MASK
;
722 val
|= PCIE_FTS_NUM_L0(0x50);
723 writel(PCIE_CONF_ADDR(PCIE_FTS_NUM
, func
, slot
, 0),
724 pcie
->base
+ PCIE_CFG_ADDR
);
725 writel(val
, pcie
->base
+ PCIE_CFG_DATA
);
730 static void mtk_pcie_enable_port(struct mtk_pcie_port
*port
)
732 struct mtk_pcie
*pcie
= port
->pcie
;
733 struct device
*dev
= pcie
->dev
;
736 err
= clk_prepare_enable(port
->sys_ck
);
738 dev_err(dev
, "failed to enable sys_ck%d clock\n", port
->slot
);
742 err
= clk_prepare_enable(port
->ahb_ck
);
744 dev_err(dev
, "failed to enable ahb_ck%d\n", port
->slot
);
748 err
= clk_prepare_enable(port
->aux_ck
);
750 dev_err(dev
, "failed to enable aux_ck%d\n", port
->slot
);
754 err
= clk_prepare_enable(port
->axi_ck
);
756 dev_err(dev
, "failed to enable axi_ck%d\n", port
->slot
);
760 err
= clk_prepare_enable(port
->obff_ck
);
762 dev_err(dev
, "failed to enable obff_ck%d\n", port
->slot
);
766 err
= clk_prepare_enable(port
->pipe_ck
);
768 dev_err(dev
, "failed to enable pipe_ck%d\n", port
->slot
);
772 reset_control_assert(port
->reset
);
773 reset_control_deassert(port
->reset
);
775 err
= phy_init(port
->phy
);
777 dev_err(dev
, "failed to initialize port%d phy\n", port
->slot
);
781 err
= phy_power_on(port
->phy
);
783 dev_err(dev
, "failed to power on port%d phy\n", port
->slot
);
787 if (!pcie
->soc
->startup(port
))
790 dev_info(dev
, "Port%d link down\n", port
->slot
);
792 phy_power_off(port
->phy
);
796 clk_disable_unprepare(port
->pipe_ck
);
798 clk_disable_unprepare(port
->obff_ck
);
800 clk_disable_unprepare(port
->axi_ck
);
802 clk_disable_unprepare(port
->aux_ck
);
804 clk_disable_unprepare(port
->ahb_ck
);
806 clk_disable_unprepare(port
->sys_ck
);
808 mtk_pcie_port_free(port
);
811 static int mtk_pcie_parse_port(struct mtk_pcie
*pcie
,
812 struct device_node
*node
,
815 struct mtk_pcie_port
*port
;
816 struct resource
*regs
;
817 struct device
*dev
= pcie
->dev
;
818 struct platform_device
*pdev
= to_platform_device(dev
);
822 port
= devm_kzalloc(dev
, sizeof(*port
), GFP_KERNEL
);
826 err
= of_property_read_u32(node
, "num-lanes", &port
->lane
);
828 dev_err(dev
, "missing num-lanes property\n");
832 snprintf(name
, sizeof(name
), "port%d", slot
);
833 regs
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, name
);
834 port
->base
= devm_ioremap_resource(dev
, regs
);
835 if (IS_ERR(port
->base
)) {
836 dev_err(dev
, "failed to map port%d base\n", slot
);
837 return PTR_ERR(port
->base
);
840 snprintf(name
, sizeof(name
), "sys_ck%d", slot
);
841 port
->sys_ck
= devm_clk_get(dev
, name
);
842 if (IS_ERR(port
->sys_ck
)) {
843 dev_err(dev
, "failed to get sys_ck%d clock\n", slot
);
844 return PTR_ERR(port
->sys_ck
);
847 /* sys_ck might be divided into the following parts in some chips */
848 snprintf(name
, sizeof(name
), "ahb_ck%d", slot
);
849 port
->ahb_ck
= devm_clk_get(dev
, name
);
850 if (IS_ERR(port
->ahb_ck
)) {
851 if (PTR_ERR(port
->ahb_ck
) == -EPROBE_DEFER
)
852 return -EPROBE_DEFER
;
857 snprintf(name
, sizeof(name
), "axi_ck%d", slot
);
858 port
->axi_ck
= devm_clk_get(dev
, name
);
859 if (IS_ERR(port
->axi_ck
)) {
860 if (PTR_ERR(port
->axi_ck
) == -EPROBE_DEFER
)
861 return -EPROBE_DEFER
;
866 snprintf(name
, sizeof(name
), "aux_ck%d", slot
);
867 port
->aux_ck
= devm_clk_get(dev
, name
);
868 if (IS_ERR(port
->aux_ck
)) {
869 if (PTR_ERR(port
->aux_ck
) == -EPROBE_DEFER
)
870 return -EPROBE_DEFER
;
875 snprintf(name
, sizeof(name
), "obff_ck%d", slot
);
876 port
->obff_ck
= devm_clk_get(dev
, name
);
877 if (IS_ERR(port
->obff_ck
)) {
878 if (PTR_ERR(port
->obff_ck
) == -EPROBE_DEFER
)
879 return -EPROBE_DEFER
;
881 port
->obff_ck
= NULL
;
884 snprintf(name
, sizeof(name
), "pipe_ck%d", slot
);
885 port
->pipe_ck
= devm_clk_get(dev
, name
);
886 if (IS_ERR(port
->pipe_ck
)) {
887 if (PTR_ERR(port
->pipe_ck
) == -EPROBE_DEFER
)
888 return -EPROBE_DEFER
;
890 port
->pipe_ck
= NULL
;
893 snprintf(name
, sizeof(name
), "pcie-rst%d", slot
);
894 port
->reset
= devm_reset_control_get_optional_exclusive(dev
, name
);
895 if (PTR_ERR(port
->reset
) == -EPROBE_DEFER
)
896 return PTR_ERR(port
->reset
);
898 /* some platforms may use default PHY setting */
899 snprintf(name
, sizeof(name
), "pcie-phy%d", slot
);
900 port
->phy
= devm_phy_optional_get(dev
, name
);
901 if (IS_ERR(port
->phy
))
902 return PTR_ERR(port
->phy
);
907 if (pcie
->soc
->setup_irq
) {
908 err
= pcie
->soc
->setup_irq(port
, node
);
913 INIT_LIST_HEAD(&port
->list
);
914 list_add_tail(&port
->list
, &pcie
->ports
);
919 static int mtk_pcie_subsys_powerup(struct mtk_pcie
*pcie
)
921 struct device
*dev
= pcie
->dev
;
922 struct platform_device
*pdev
= to_platform_device(dev
);
923 struct resource
*regs
;
926 /* get shared registers, which are optional */
927 regs
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "subsys");
929 pcie
->base
= devm_ioremap_resource(dev
, regs
);
930 if (IS_ERR(pcie
->base
)) {
931 dev_err(dev
, "failed to map shared register\n");
932 return PTR_ERR(pcie
->base
);
936 pcie
->free_ck
= devm_clk_get(dev
, "free_ck");
937 if (IS_ERR(pcie
->free_ck
)) {
938 if (PTR_ERR(pcie
->free_ck
) == -EPROBE_DEFER
)
939 return -EPROBE_DEFER
;
941 pcie
->free_ck
= NULL
;
944 if (dev
->pm_domain
) {
945 pm_runtime_enable(dev
);
946 pm_runtime_get_sync(dev
);
949 /* enable top level clock */
950 err
= clk_prepare_enable(pcie
->free_ck
);
952 dev_err(dev
, "failed to enable free_ck\n");
959 if (dev
->pm_domain
) {
960 pm_runtime_put_sync(dev
);
961 pm_runtime_disable(dev
);
967 static int mtk_pcie_setup(struct mtk_pcie
*pcie
)
969 struct device
*dev
= pcie
->dev
;
970 struct device_node
*node
= dev
->of_node
, *child
;
971 struct of_pci_range_parser parser
;
972 struct of_pci_range range
;
974 struct mtk_pcie_port
*port
, *tmp
;
977 if (of_pci_range_parser_init(&parser
, node
)) {
978 dev_err(dev
, "missing \"ranges\" property\n");
982 for_each_of_pci_range(&parser
, &range
) {
983 err
= of_pci_range_to_resource(&range
, node
, &res
);
987 switch (res
.flags
& IORESOURCE_TYPE_BITS
) {
989 pcie
->offset
.io
= res
.start
- range
.pci_addr
;
991 memcpy(&pcie
->pio
, &res
, sizeof(res
));
992 pcie
->pio
.name
= node
->full_name
;
994 pcie
->io
.start
= range
.cpu_addr
;
995 pcie
->io
.end
= range
.cpu_addr
+ range
.size
- 1;
996 pcie
->io
.flags
= IORESOURCE_MEM
;
997 pcie
->io
.name
= "I/O";
999 memcpy(&res
, &pcie
->io
, sizeof(res
));
1002 case IORESOURCE_MEM
:
1003 pcie
->offset
.mem
= res
.start
- range
.pci_addr
;
1005 memcpy(&pcie
->mem
, &res
, sizeof(res
));
1006 pcie
->mem
.name
= "non-prefetchable";
1011 err
= of_pci_parse_bus_range(node
, &pcie
->busn
);
1013 dev_err(dev
, "failed to parse bus ranges property: %d\n", err
);
1014 pcie
->busn
.name
= node
->name
;
1015 pcie
->busn
.start
= 0;
1016 pcie
->busn
.end
= 0xff;
1017 pcie
->busn
.flags
= IORESOURCE_BUS
;
1020 for_each_available_child_of_node(node
, child
) {
1023 err
= of_pci_get_devfn(child
);
1025 dev_err(dev
, "failed to parse devfn: %d\n", err
);
1029 slot
= PCI_SLOT(err
);
1031 err
= mtk_pcie_parse_port(pcie
, child
, slot
);
1036 err
= mtk_pcie_subsys_powerup(pcie
);
1040 /* enable each port, and then check link status */
1041 list_for_each_entry_safe(port
, tmp
, &pcie
->ports
, list
)
1042 mtk_pcie_enable_port(port
);
1044 /* power down PCIe subsys if slots are all empty (link down) */
1045 if (list_empty(&pcie
->ports
))
1046 mtk_pcie_subsys_powerdown(pcie
);
1051 static int mtk_pcie_request_resources(struct mtk_pcie
*pcie
)
1053 struct pci_host_bridge
*host
= pci_host_bridge_from_priv(pcie
);
1054 struct list_head
*windows
= &host
->windows
;
1055 struct device
*dev
= pcie
->dev
;
1058 pci_add_resource_offset(windows
, &pcie
->pio
, pcie
->offset
.io
);
1059 pci_add_resource_offset(windows
, &pcie
->mem
, pcie
->offset
.mem
);
1060 pci_add_resource(windows
, &pcie
->busn
);
1062 err
= devm_request_pci_bus_resources(dev
, windows
);
1066 pci_remap_iospace(&pcie
->pio
, pcie
->io
.start
);
1071 static int mtk_pcie_register_host(struct pci_host_bridge
*host
)
1073 struct mtk_pcie
*pcie
= pci_host_bridge_priv(host
);
1074 struct pci_bus
*child
;
1077 host
->busnr
= pcie
->busn
.start
;
1078 host
->dev
.parent
= pcie
->dev
;
1079 host
->ops
= pcie
->soc
->ops
;
1080 host
->map_irq
= of_irq_parse_and_map_pci
;
1081 host
->swizzle_irq
= pci_common_swizzle
;
1082 host
->sysdata
= pcie
;
1083 if (IS_ENABLED(CONFIG_PCI_MSI
) && pcie
->soc
->has_msi
)
1084 host
->msi
= &mtk_pcie_msi_chip
;
1086 err
= pci_scan_root_bus_bridge(host
);
1090 pci_bus_size_bridges(host
->bus
);
1091 pci_bus_assign_resources(host
->bus
);
1093 list_for_each_entry(child
, &host
->bus
->children
, node
)
1094 pcie_bus_configure_settings(child
);
1096 pci_bus_add_devices(host
->bus
);
1101 static int mtk_pcie_probe(struct platform_device
*pdev
)
1103 struct device
*dev
= &pdev
->dev
;
1104 struct mtk_pcie
*pcie
;
1105 struct pci_host_bridge
*host
;
1108 host
= devm_pci_alloc_host_bridge(dev
, sizeof(*pcie
));
1112 pcie
= pci_host_bridge_priv(host
);
1115 pcie
->soc
= of_device_get_match_data(dev
);
1116 platform_set_drvdata(pdev
, pcie
);
1117 INIT_LIST_HEAD(&pcie
->ports
);
1119 err
= mtk_pcie_setup(pcie
);
1123 err
= mtk_pcie_request_resources(pcie
);
1127 err
= mtk_pcie_register_host(host
);
1134 if (!list_empty(&pcie
->ports
))
1135 mtk_pcie_put_resources(pcie
);
1140 static const struct mtk_pcie_soc mtk_pcie_soc_v1
= {
1141 .ops
= &mtk_pcie_ops
,
1142 .startup
= mtk_pcie_startup_port
,
1145 static const struct mtk_pcie_soc mtk_pcie_soc_v2
= {
1147 .ops
= &mtk_pcie_ops_v2
,
1148 .startup
= mtk_pcie_startup_port_v2
,
1149 .setup_irq
= mtk_pcie_setup_irq
,
1152 static const struct of_device_id mtk_pcie_ids
[] = {
1153 { .compatible
= "mediatek,mt2701-pcie", .data
= &mtk_pcie_soc_v1
},
1154 { .compatible
= "mediatek,mt7623-pcie", .data
= &mtk_pcie_soc_v1
},
1155 { .compatible
= "mediatek,mt2712-pcie", .data
= &mtk_pcie_soc_v2
},
1156 { .compatible
= "mediatek,mt7622-pcie", .data
= &mtk_pcie_soc_v2
},
1160 static struct platform_driver mtk_pcie_driver
= {
1161 .probe
= mtk_pcie_probe
,
1164 .of_match_table
= mtk_pcie_ids
,
1165 .suppress_bind_attrs
= true,
1168 builtin_platform_driver(mtk_pcie_driver
);