2 * MediaTek PCIe host controller driver.
4 * Copyright (c) 2017 MediaTek Inc.
5 * Author: Ryder Lee <ryder.lee@mediatek.com>
6 * Honghui Zhang <honghui.zhang@mediatek.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/clk.h>
19 #include <linux/delay.h>
20 #include <linux/iopoll.h>
21 #include <linux/irq.h>
22 #include <linux/irqdomain.h>
23 #include <linux/kernel.h>
24 #include <linux/of_address.h>
25 #include <linux/of_pci.h>
26 #include <linux/of_platform.h>
27 #include <linux/pci.h>
28 #include <linux/phy/phy.h>
29 #include <linux/platform_device.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/reset.h>
33 /* PCIe shared registers */
34 #define PCIE_SYS_CFG 0x00
35 #define PCIE_INT_ENABLE 0x0c
36 #define PCIE_CFG_ADDR 0x20
37 #define PCIE_CFG_DATA 0x24
39 /* PCIe per port registers */
40 #define PCIE_BAR0_SETUP 0x10
41 #define PCIE_CLASS 0x34
42 #define PCIE_LINK_STATUS 0x50
44 #define PCIE_PORT_INT_EN(x) BIT(20 + (x))
45 #define PCIE_PORT_PERST(x) BIT(1 + (x))
46 #define PCIE_PORT_LINKUP BIT(0)
47 #define PCIE_BAR_MAP_MAX GENMASK(31, 16)
49 #define PCIE_BAR_ENABLE BIT(0)
50 #define PCIE_REVISION_ID BIT(0)
51 #define PCIE_CLASS_CODE (0x60400 << 8)
52 #define PCIE_CONF_REG(regn) (((regn) & GENMASK(7, 2)) | \
53 ((((regn) >> 8) & GENMASK(3, 0)) << 24))
54 #define PCIE_CONF_FUN(fun) (((fun) << 8) & GENMASK(10, 8))
55 #define PCIE_CONF_DEV(dev) (((dev) << 11) & GENMASK(15, 11))
56 #define PCIE_CONF_BUS(bus) (((bus) << 16) & GENMASK(23, 16))
57 #define PCIE_CONF_ADDR(regn, fun, dev, bus) \
58 (PCIE_CONF_REG(regn) | PCIE_CONF_FUN(fun) | \
59 PCIE_CONF_DEV(dev) | PCIE_CONF_BUS(bus))
61 /* MediaTek specific configuration registers */
62 #define PCIE_FTS_NUM 0x70c
63 #define PCIE_FTS_NUM_MASK GENMASK(15, 8)
64 #define PCIE_FTS_NUM_L0(x) ((x) & 0xff << 8)
66 #define PCIE_FC_CREDIT 0x73c
67 #define PCIE_FC_CREDIT_MASK (GENMASK(31, 31) | GENMASK(28, 16))
68 #define PCIE_FC_CREDIT_VAL(x) ((x) << 16)
70 /* PCIe V2 share registers */
71 #define PCIE_SYS_CFG_V2 0x0
72 #define PCIE_CSR_LTSSM_EN(x) BIT(0 + (x) * 8)
73 #define PCIE_CSR_ASPM_L1_EN(x) BIT(1 + (x) * 8)
75 /* PCIe V2 per-port registers */
76 #define PCIE_MSI_VECTOR 0x0c0
77 #define PCIE_INT_MASK 0x420
78 #define INTX_MASK GENMASK(19, 16)
80 #define PCIE_INT_STATUS 0x424
81 #define MSI_STATUS BIT(23)
82 #define PCIE_IMSI_STATUS 0x42c
83 #define PCIE_IMSI_ADDR 0x430
84 #define MSI_MASK BIT(23)
85 #define MTK_MSI_IRQS_NUM 32
87 #define PCIE_AHB_TRANS_BASE0_L 0x438
88 #define PCIE_AHB_TRANS_BASE0_H 0x43c
89 #define AHB2PCIE_SIZE(x) ((x) & GENMASK(4, 0))
90 #define PCIE_AXI_WINDOW0 0x448
91 #define WIN_ENABLE BIT(7)
93 /* PCIe V2 configuration transaction header */
94 #define PCIE_CFG_HEADER0 0x460
95 #define PCIE_CFG_HEADER1 0x464
96 #define PCIE_CFG_HEADER2 0x468
97 #define PCIE_CFG_WDATA 0x470
98 #define PCIE_APP_TLP_REQ 0x488
99 #define PCIE_CFG_RDATA 0x48c
100 #define APP_CFG_REQ BIT(0)
101 #define APP_CPL_STATUS GENMASK(7, 5)
103 #define CFG_WRRD_TYPE_0 4
107 #define CFG_DW0_LENGTH(length) ((length) & GENMASK(9, 0))
108 #define CFG_DW0_TYPE(type) (((type) << 24) & GENMASK(28, 24))
109 #define CFG_DW0_FMT(fmt) (((fmt) << 29) & GENMASK(31, 29))
110 #define CFG_DW2_REGN(regn) ((regn) & GENMASK(11, 2))
111 #define CFG_DW2_FUN(fun) (((fun) << 16) & GENMASK(18, 16))
112 #define CFG_DW2_DEV(dev) (((dev) << 19) & GENMASK(23, 19))
113 #define CFG_DW2_BUS(bus) (((bus) << 24) & GENMASK(31, 24))
114 #define CFG_HEADER_DW0(type, fmt) \
115 (CFG_DW0_LENGTH(1) | CFG_DW0_TYPE(type) | CFG_DW0_FMT(fmt))
116 #define CFG_HEADER_DW1(where, size) \
117 (GENMASK(((size) - 1), 0) << ((where) & 0x3))
118 #define CFG_HEADER_DW2(regn, fun, dev, bus) \
119 (CFG_DW2_REGN(regn) | CFG_DW2_FUN(fun) | \
120 CFG_DW2_DEV(dev) | CFG_DW2_BUS(bus))
122 #define PCIE_RST_CTRL 0x510
123 #define PCIE_PHY_RSTB BIT(0)
124 #define PCIE_PIPE_SRSTB BIT(1)
125 #define PCIE_MAC_SRSTB BIT(2)
126 #define PCIE_CRSTB BIT(3)
127 #define PCIE_PERSTB BIT(8)
128 #define PCIE_LINKDOWN_RST_EN GENMASK(15, 13)
129 #define PCIE_LINK_STATUS_V2 0x804
130 #define PCIE_PORT_LINKUP_V2 BIT(10)
132 struct mtk_pcie_port
;
135 * struct mtk_pcie_soc - differentiate between host generations
136 * @has_msi: whether this host supports MSI interrupts or not
137 * @ops: pointer to configuration access functions
138 * @startup: pointer to controller setting functions
139 * @setup_irq: pointer to initialize IRQ functions
141 struct mtk_pcie_soc
{
144 int (*startup
)(struct mtk_pcie_port
*port
);
145 int (*setup_irq
)(struct mtk_pcie_port
*port
, struct device_node
*node
);
149 * struct mtk_pcie_port - PCIe port information
150 * @base: IO mapped register base
152 * @pcie: pointer to PCIe host info
153 * @reset: pointer to port reset control
154 * @sys_ck: pointer to transaction/data link layer clock
155 * @ahb_ck: pointer to AHB slave interface operating clock for CSR access
156 * and RC initiated MMIO access
157 * @axi_ck: pointer to application layer MMIO channel operating clock
158 * @aux_ck: pointer to pe2_mac_bridge and pe2_mac_core operating clock
159 * when pcie_mac_ck/pcie_pipe_ck is turned off
160 * @obff_ck: pointer to OBFF functional block operating clock
161 * @pipe_ck: pointer to LTSSM and PHY/MAC layer operating clock
162 * @phy: pointer to PHY control block
165 * @irq_domain: legacy INTx IRQ domain
166 * @msi_domain: MSI IRQ domain
167 * @msi_irq_in_use: bit map for assigned MSI IRQ
169 struct mtk_pcie_port
{
171 struct list_head list
;
172 struct mtk_pcie
*pcie
;
173 struct reset_control
*reset
;
183 struct irq_domain
*irq_domain
;
184 struct irq_domain
*msi_domain
;
185 DECLARE_BITMAP(msi_irq_in_use
, MTK_MSI_IRQS_NUM
);
189 * struct mtk_pcie - PCIe host information
190 * @dev: pointer to PCIe device
191 * @base: IO mapped register base
192 * @free_ck: free-run reference clock
195 * @mem: non-prefetchable memory resource
197 * @offset: IO / Memory offset
198 * @ports: pointer to PCIe port information
199 * @soc: pointer to SoC-dependent operations
209 struct resource busn
;
214 struct list_head ports
;
215 const struct mtk_pcie_soc
*soc
;
218 static void mtk_pcie_subsys_powerdown(struct mtk_pcie
*pcie
)
220 struct device
*dev
= pcie
->dev
;
222 clk_disable_unprepare(pcie
->free_ck
);
224 if (dev
->pm_domain
) {
225 pm_runtime_put_sync(dev
);
226 pm_runtime_disable(dev
);
230 static void mtk_pcie_port_free(struct mtk_pcie_port
*port
)
232 struct mtk_pcie
*pcie
= port
->pcie
;
233 struct device
*dev
= pcie
->dev
;
235 devm_iounmap(dev
, port
->base
);
236 list_del(&port
->list
);
237 devm_kfree(dev
, port
);
240 static void mtk_pcie_put_resources(struct mtk_pcie
*pcie
)
242 struct mtk_pcie_port
*port
, *tmp
;
244 list_for_each_entry_safe(port
, tmp
, &pcie
->ports
, list
) {
245 phy_power_off(port
->phy
);
247 clk_disable_unprepare(port
->pipe_ck
);
248 clk_disable_unprepare(port
->obff_ck
);
249 clk_disable_unprepare(port
->axi_ck
);
250 clk_disable_unprepare(port
->aux_ck
);
251 clk_disable_unprepare(port
->ahb_ck
);
252 clk_disable_unprepare(port
->sys_ck
);
253 mtk_pcie_port_free(port
);
256 mtk_pcie_subsys_powerdown(pcie
);
259 static int mtk_pcie_check_cfg_cpld(struct mtk_pcie_port
*port
)
264 err
= readl_poll_timeout_atomic(port
->base
+ PCIE_APP_TLP_REQ
, val
,
265 !(val
& APP_CFG_REQ
), 10,
266 100 * USEC_PER_MSEC
);
268 return PCIBIOS_SET_FAILED
;
270 if (readl(port
->base
+ PCIE_APP_TLP_REQ
) & APP_CPL_STATUS
)
271 return PCIBIOS_SET_FAILED
;
273 return PCIBIOS_SUCCESSFUL
;
276 static int mtk_pcie_hw_rd_cfg(struct mtk_pcie_port
*port
, u32 bus
, u32 devfn
,
277 int where
, int size
, u32
*val
)
281 /* Write PCIe configuration transaction header for Cfgrd */
282 writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0
, CFG_RD_FMT
),
283 port
->base
+ PCIE_CFG_HEADER0
);
284 writel(CFG_HEADER_DW1(where
, size
), port
->base
+ PCIE_CFG_HEADER1
);
285 writel(CFG_HEADER_DW2(where
, PCI_FUNC(devfn
), PCI_SLOT(devfn
), bus
),
286 port
->base
+ PCIE_CFG_HEADER2
);
288 /* Trigger h/w to transmit Cfgrd TLP */
289 tmp
= readl(port
->base
+ PCIE_APP_TLP_REQ
);
291 writel(tmp
, port
->base
+ PCIE_APP_TLP_REQ
);
293 /* Check completion status */
294 if (mtk_pcie_check_cfg_cpld(port
))
295 return PCIBIOS_SET_FAILED
;
297 /* Read cpld payload of Cfgrd */
298 *val
= readl(port
->base
+ PCIE_CFG_RDATA
);
301 *val
= (*val
>> (8 * (where
& 3))) & 0xff;
303 *val
= (*val
>> (8 * (where
& 3))) & 0xffff;
305 return PCIBIOS_SUCCESSFUL
;
308 static int mtk_pcie_hw_wr_cfg(struct mtk_pcie_port
*port
, u32 bus
, u32 devfn
,
309 int where
, int size
, u32 val
)
311 /* Write PCIe configuration transaction header for Cfgwr */
312 writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0
, CFG_WR_FMT
),
313 port
->base
+ PCIE_CFG_HEADER0
);
314 writel(CFG_HEADER_DW1(where
, size
), port
->base
+ PCIE_CFG_HEADER1
);
315 writel(CFG_HEADER_DW2(where
, PCI_FUNC(devfn
), PCI_SLOT(devfn
), bus
),
316 port
->base
+ PCIE_CFG_HEADER2
);
318 /* Write Cfgwr data */
319 val
= val
<< 8 * (where
& 3);
320 writel(val
, port
->base
+ PCIE_CFG_WDATA
);
322 /* Trigger h/w to transmit Cfgwr TLP */
323 val
= readl(port
->base
+ PCIE_APP_TLP_REQ
);
325 writel(val
, port
->base
+ PCIE_APP_TLP_REQ
);
327 /* Check completion status */
328 return mtk_pcie_check_cfg_cpld(port
);
331 static struct mtk_pcie_port
*mtk_pcie_find_port(struct pci_bus
*bus
,
334 struct mtk_pcie
*pcie
= bus
->sysdata
;
335 struct mtk_pcie_port
*port
;
337 list_for_each_entry(port
, &pcie
->ports
, list
)
338 if (port
->slot
== PCI_SLOT(devfn
))
344 static int mtk_pcie_config_read(struct pci_bus
*bus
, unsigned int devfn
,
345 int where
, int size
, u32
*val
)
347 struct mtk_pcie_port
*port
;
348 u32 bn
= bus
->number
;
351 port
= mtk_pcie_find_port(bus
, devfn
);
354 return PCIBIOS_DEVICE_NOT_FOUND
;
357 ret
= mtk_pcie_hw_rd_cfg(port
, bn
, devfn
, where
, size
, val
);
364 static int mtk_pcie_config_write(struct pci_bus
*bus
, unsigned int devfn
,
365 int where
, int size
, u32 val
)
367 struct mtk_pcie_port
*port
;
368 u32 bn
= bus
->number
;
370 port
= mtk_pcie_find_port(bus
, devfn
);
372 return PCIBIOS_DEVICE_NOT_FOUND
;
374 return mtk_pcie_hw_wr_cfg(port
, bn
, devfn
, where
, size
, val
);
377 static struct pci_ops mtk_pcie_ops_v2
= {
378 .read
= mtk_pcie_config_read
,
379 .write
= mtk_pcie_config_write
,
382 static int mtk_pcie_startup_port_v2(struct mtk_pcie_port
*port
)
384 struct mtk_pcie
*pcie
= port
->pcie
;
385 struct resource
*mem
= &pcie
->mem
;
390 /* MT7622 platforms need to enable LTSSM and ASPM from PCIe subsys */
392 val
= readl(pcie
->base
+ PCIE_SYS_CFG_V2
);
393 val
|= PCIE_CSR_LTSSM_EN(port
->slot
) |
394 PCIE_CSR_ASPM_L1_EN(port
->slot
);
395 writel(val
, pcie
->base
+ PCIE_SYS_CFG_V2
);
398 /* Assert all reset signals */
399 writel(0, port
->base
+ PCIE_RST_CTRL
);
402 * Enable PCIe link down reset, if link status changed from link up to
403 * link down, this will reset MAC control registers and configuration
406 writel(PCIE_LINKDOWN_RST_EN
, port
->base
+ PCIE_RST_CTRL
);
408 /* De-assert PHY, PE, PIPE, MAC and configuration reset */
409 val
= readl(port
->base
+ PCIE_RST_CTRL
);
410 val
|= PCIE_PHY_RSTB
| PCIE_PERSTB
| PCIE_PIPE_SRSTB
|
411 PCIE_MAC_SRSTB
| PCIE_CRSTB
;
412 writel(val
, port
->base
+ PCIE_RST_CTRL
);
414 /* 100ms timeout value should be enough for Gen1/2 training */
415 err
= readl_poll_timeout(port
->base
+ PCIE_LINK_STATUS_V2
, val
,
416 !!(val
& PCIE_PORT_LINKUP_V2
), 20,
417 100 * USEC_PER_MSEC
);
422 val
= readl(port
->base
+ PCIE_INT_MASK
);
424 writel(val
, port
->base
+ PCIE_INT_MASK
);
426 /* Set AHB to PCIe translation windows */
427 size
= mem
->end
- mem
->start
;
428 val
= lower_32_bits(mem
->start
) | AHB2PCIE_SIZE(fls(size
));
429 writel(val
, port
->base
+ PCIE_AHB_TRANS_BASE0_L
);
431 val
= upper_32_bits(mem
->start
);
432 writel(val
, port
->base
+ PCIE_AHB_TRANS_BASE0_H
);
434 /* Set PCIe to AXI translation memory space.*/
435 val
= fls(0xffffffff) | WIN_ENABLE
;
436 writel(val
, port
->base
+ PCIE_AXI_WINDOW0
);
441 static int mtk_pcie_msi_alloc(struct mtk_pcie_port
*port
)
445 msi
= find_first_zero_bit(port
->msi_irq_in_use
, MTK_MSI_IRQS_NUM
);
446 if (msi
< MTK_MSI_IRQS_NUM
)
447 set_bit(msi
, port
->msi_irq_in_use
);
454 static void mtk_pcie_msi_free(struct mtk_pcie_port
*port
, unsigned long hwirq
)
456 clear_bit(hwirq
, port
->msi_irq_in_use
);
459 static int mtk_pcie_msi_setup_irq(struct msi_controller
*chip
,
460 struct pci_dev
*pdev
, struct msi_desc
*desc
)
462 struct mtk_pcie_port
*port
;
466 phys_addr_t msg_addr
;
468 port
= mtk_pcie_find_port(pdev
->bus
, pdev
->devfn
);
472 hwirq
= mtk_pcie_msi_alloc(port
);
476 irq
= irq_create_mapping(port
->msi_domain
, hwirq
);
478 mtk_pcie_msi_free(port
, hwirq
);
482 chip
->dev
= &pdev
->dev
;
484 irq_set_msi_desc(irq
, desc
);
486 /* MT2712/MT7622 only support 32-bit MSI addresses */
487 msg_addr
= virt_to_phys(port
->base
+ PCIE_MSI_VECTOR
);
489 msg
.address_lo
= lower_32_bits(msg_addr
);
492 pci_write_msi_msg(irq
, &msg
);
497 static void mtk_msi_teardown_irq(struct msi_controller
*chip
, unsigned int irq
)
499 struct pci_dev
*pdev
= to_pci_dev(chip
->dev
);
500 struct irq_data
*d
= irq_get_irq_data(irq
);
501 irq_hw_number_t hwirq
= irqd_to_hwirq(d
);
502 struct mtk_pcie_port
*port
;
504 port
= mtk_pcie_find_port(pdev
->bus
, pdev
->devfn
);
508 irq_dispose_mapping(irq
);
509 mtk_pcie_msi_free(port
, hwirq
);
512 static struct msi_controller mtk_pcie_msi_chip
= {
513 .setup_irq
= mtk_pcie_msi_setup_irq
,
514 .teardown_irq
= mtk_msi_teardown_irq
,
517 static struct irq_chip mtk_msi_irq_chip
= {
518 .name
= "MTK PCIe MSI",
519 .irq_enable
= pci_msi_unmask_irq
,
520 .irq_disable
= pci_msi_mask_irq
,
521 .irq_mask
= pci_msi_mask_irq
,
522 .irq_unmask
= pci_msi_unmask_irq
,
525 static int mtk_pcie_msi_map(struct irq_domain
*domain
, unsigned int irq
,
526 irq_hw_number_t hwirq
)
528 irq_set_chip_and_handler(irq
, &mtk_msi_irq_chip
, handle_simple_irq
);
529 irq_set_chip_data(irq
, domain
->host_data
);
534 static const struct irq_domain_ops msi_domain_ops
= {
535 .map
= mtk_pcie_msi_map
,
538 static void mtk_pcie_enable_msi(struct mtk_pcie_port
*port
)
541 phys_addr_t msg_addr
;
543 msg_addr
= virt_to_phys(port
->base
+ PCIE_MSI_VECTOR
);
544 val
= lower_32_bits(msg_addr
);
545 writel(val
, port
->base
+ PCIE_IMSI_ADDR
);
547 val
= readl(port
->base
+ PCIE_INT_MASK
);
549 writel(val
, port
->base
+ PCIE_INT_MASK
);
552 static int mtk_pcie_intx_map(struct irq_domain
*domain
, unsigned int irq
,
553 irq_hw_number_t hwirq
)
555 irq_set_chip_and_handler(irq
, &dummy_irq_chip
, handle_simple_irq
);
556 irq_set_chip_data(irq
, domain
->host_data
);
561 static const struct irq_domain_ops intx_domain_ops
= {
562 .map
= mtk_pcie_intx_map
,
565 static int mtk_pcie_init_irq_domain(struct mtk_pcie_port
*port
,
566 struct device_node
*node
)
568 struct device
*dev
= port
->pcie
->dev
;
569 struct device_node
*pcie_intc_node
;
572 pcie_intc_node
= of_get_next_child(node
, NULL
);
573 if (!pcie_intc_node
) {
574 dev_err(dev
, "no PCIe Intc node found\n");
578 port
->irq_domain
= irq_domain_add_linear(pcie_intc_node
, PCI_NUM_INTX
,
579 &intx_domain_ops
, port
);
580 if (!port
->irq_domain
) {
581 dev_err(dev
, "failed to get INTx IRQ domain\n");
585 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
586 port
->msi_domain
= irq_domain_add_linear(node
, MTK_MSI_IRQS_NUM
,
589 if (!port
->msi_domain
) {
590 dev_err(dev
, "failed to create MSI IRQ domain\n");
593 mtk_pcie_enable_msi(port
);
599 static irqreturn_t
mtk_pcie_intr_handler(int irq
, void *data
)
601 struct mtk_pcie_port
*port
= (struct mtk_pcie_port
*)data
;
602 unsigned long status
;
604 u32 bit
= INTX_SHIFT
;
606 while ((status
= readl(port
->base
+ PCIE_INT_STATUS
)) & INTX_MASK
) {
607 for_each_set_bit_from(bit
, &status
, PCI_NUM_INTX
+ INTX_SHIFT
) {
609 writel(1 << bit
, port
->base
+ PCIE_INT_STATUS
);
610 virq
= irq_find_mapping(port
->irq_domain
,
612 generic_handle_irq(virq
);
616 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
617 while ((status
= readl(port
->base
+ PCIE_INT_STATUS
)) & MSI_STATUS
) {
618 unsigned long imsi_status
;
620 while ((imsi_status
= readl(port
->base
+ PCIE_IMSI_STATUS
))) {
621 for_each_set_bit(bit
, &imsi_status
, MTK_MSI_IRQS_NUM
) {
623 writel(1 << bit
, port
->base
+ PCIE_IMSI_STATUS
);
624 virq
= irq_find_mapping(port
->msi_domain
, bit
);
625 generic_handle_irq(virq
);
628 /* Clear MSI interrupt status */
629 writel(MSI_STATUS
, port
->base
+ PCIE_INT_STATUS
);
636 static int mtk_pcie_setup_irq(struct mtk_pcie_port
*port
,
637 struct device_node
*node
)
639 struct mtk_pcie
*pcie
= port
->pcie
;
640 struct device
*dev
= pcie
->dev
;
641 struct platform_device
*pdev
= to_platform_device(dev
);
644 irq
= platform_get_irq(pdev
, port
->slot
);
645 err
= devm_request_irq(dev
, irq
, mtk_pcie_intr_handler
,
646 IRQF_SHARED
, "mtk-pcie", port
);
648 dev_err(dev
, "unable to request IRQ %d\n", irq
);
652 err
= mtk_pcie_init_irq_domain(port
, node
);
654 dev_err(dev
, "failed to init PCIe IRQ domain\n");
661 static void __iomem
*mtk_pcie_map_bus(struct pci_bus
*bus
,
662 unsigned int devfn
, int where
)
664 struct mtk_pcie
*pcie
= bus
->sysdata
;
666 writel(PCIE_CONF_ADDR(where
, PCI_FUNC(devfn
), PCI_SLOT(devfn
),
667 bus
->number
), pcie
->base
+ PCIE_CFG_ADDR
);
669 return pcie
->base
+ PCIE_CFG_DATA
+ (where
& 3);
672 static struct pci_ops mtk_pcie_ops
= {
673 .map_bus
= mtk_pcie_map_bus
,
674 .read
= pci_generic_config_read
,
675 .write
= pci_generic_config_write
,
678 static int mtk_pcie_startup_port(struct mtk_pcie_port
*port
)
680 struct mtk_pcie
*pcie
= port
->pcie
;
681 u32 func
= PCI_FUNC(port
->slot
<< 3);
682 u32 slot
= PCI_SLOT(port
->slot
<< 3);
686 /* assert port PERST_N */
687 val
= readl(pcie
->base
+ PCIE_SYS_CFG
);
688 val
|= PCIE_PORT_PERST(port
->slot
);
689 writel(val
, pcie
->base
+ PCIE_SYS_CFG
);
691 /* de-assert port PERST_N */
692 val
= readl(pcie
->base
+ PCIE_SYS_CFG
);
693 val
&= ~PCIE_PORT_PERST(port
->slot
);
694 writel(val
, pcie
->base
+ PCIE_SYS_CFG
);
696 /* 100ms timeout value should be enough for Gen1/2 training */
697 err
= readl_poll_timeout(port
->base
+ PCIE_LINK_STATUS
, val
,
698 !!(val
& PCIE_PORT_LINKUP
), 20,
699 100 * USEC_PER_MSEC
);
703 /* enable interrupt */
704 val
= readl(pcie
->base
+ PCIE_INT_ENABLE
);
705 val
|= PCIE_PORT_INT_EN(port
->slot
);
706 writel(val
, pcie
->base
+ PCIE_INT_ENABLE
);
708 /* map to all DDR region. We need to set it before cfg operation. */
709 writel(PCIE_BAR_MAP_MAX
| PCIE_BAR_ENABLE
,
710 port
->base
+ PCIE_BAR0_SETUP
);
712 /* configure class code and revision ID */
713 writel(PCIE_CLASS_CODE
| PCIE_REVISION_ID
, port
->base
+ PCIE_CLASS
);
715 /* configure FC credit */
716 writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT
, func
, slot
, 0),
717 pcie
->base
+ PCIE_CFG_ADDR
);
718 val
= readl(pcie
->base
+ PCIE_CFG_DATA
);
719 val
&= ~PCIE_FC_CREDIT_MASK
;
720 val
|= PCIE_FC_CREDIT_VAL(0x806c);
721 writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT
, func
, slot
, 0),
722 pcie
->base
+ PCIE_CFG_ADDR
);
723 writel(val
, pcie
->base
+ PCIE_CFG_DATA
);
725 /* configure RC FTS number to 250 when it leaves L0s */
726 writel(PCIE_CONF_ADDR(PCIE_FTS_NUM
, func
, slot
, 0),
727 pcie
->base
+ PCIE_CFG_ADDR
);
728 val
= readl(pcie
->base
+ PCIE_CFG_DATA
);
729 val
&= ~PCIE_FTS_NUM_MASK
;
730 val
|= PCIE_FTS_NUM_L0(0x50);
731 writel(PCIE_CONF_ADDR(PCIE_FTS_NUM
, func
, slot
, 0),
732 pcie
->base
+ PCIE_CFG_ADDR
);
733 writel(val
, pcie
->base
+ PCIE_CFG_DATA
);
738 static void mtk_pcie_enable_port(struct mtk_pcie_port
*port
)
740 struct mtk_pcie
*pcie
= port
->pcie
;
741 struct device
*dev
= pcie
->dev
;
744 err
= clk_prepare_enable(port
->sys_ck
);
746 dev_err(dev
, "failed to enable sys_ck%d clock\n", port
->slot
);
750 err
= clk_prepare_enable(port
->ahb_ck
);
752 dev_err(dev
, "failed to enable ahb_ck%d\n", port
->slot
);
756 err
= clk_prepare_enable(port
->aux_ck
);
758 dev_err(dev
, "failed to enable aux_ck%d\n", port
->slot
);
762 err
= clk_prepare_enable(port
->axi_ck
);
764 dev_err(dev
, "failed to enable axi_ck%d\n", port
->slot
);
768 err
= clk_prepare_enable(port
->obff_ck
);
770 dev_err(dev
, "failed to enable obff_ck%d\n", port
->slot
);
774 err
= clk_prepare_enable(port
->pipe_ck
);
776 dev_err(dev
, "failed to enable pipe_ck%d\n", port
->slot
);
780 reset_control_assert(port
->reset
);
781 reset_control_deassert(port
->reset
);
783 err
= phy_init(port
->phy
);
785 dev_err(dev
, "failed to initialize port%d phy\n", port
->slot
);
789 err
= phy_power_on(port
->phy
);
791 dev_err(dev
, "failed to power on port%d phy\n", port
->slot
);
795 if (!pcie
->soc
->startup(port
))
798 dev_info(dev
, "Port%d link down\n", port
->slot
);
800 phy_power_off(port
->phy
);
804 clk_disable_unprepare(port
->pipe_ck
);
806 clk_disable_unprepare(port
->obff_ck
);
808 clk_disable_unprepare(port
->axi_ck
);
810 clk_disable_unprepare(port
->aux_ck
);
812 clk_disable_unprepare(port
->ahb_ck
);
814 clk_disable_unprepare(port
->sys_ck
);
816 mtk_pcie_port_free(port
);
819 static int mtk_pcie_parse_port(struct mtk_pcie
*pcie
,
820 struct device_node
*node
,
823 struct mtk_pcie_port
*port
;
824 struct resource
*regs
;
825 struct device
*dev
= pcie
->dev
;
826 struct platform_device
*pdev
= to_platform_device(dev
);
830 port
= devm_kzalloc(dev
, sizeof(*port
), GFP_KERNEL
);
834 err
= of_property_read_u32(node
, "num-lanes", &port
->lane
);
836 dev_err(dev
, "missing num-lanes property\n");
840 snprintf(name
, sizeof(name
), "port%d", slot
);
841 regs
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, name
);
842 port
->base
= devm_ioremap_resource(dev
, regs
);
843 if (IS_ERR(port
->base
)) {
844 dev_err(dev
, "failed to map port%d base\n", slot
);
845 return PTR_ERR(port
->base
);
848 snprintf(name
, sizeof(name
), "sys_ck%d", slot
);
849 port
->sys_ck
= devm_clk_get(dev
, name
);
850 if (IS_ERR(port
->sys_ck
)) {
851 dev_err(dev
, "failed to get sys_ck%d clock\n", slot
);
852 return PTR_ERR(port
->sys_ck
);
855 /* sys_ck might be divided into the following parts in some chips */
856 snprintf(name
, sizeof(name
), "ahb_ck%d", slot
);
857 port
->ahb_ck
= devm_clk_get(dev
, name
);
858 if (IS_ERR(port
->ahb_ck
)) {
859 if (PTR_ERR(port
->ahb_ck
) == -EPROBE_DEFER
)
860 return -EPROBE_DEFER
;
865 snprintf(name
, sizeof(name
), "axi_ck%d", slot
);
866 port
->axi_ck
= devm_clk_get(dev
, name
);
867 if (IS_ERR(port
->axi_ck
)) {
868 if (PTR_ERR(port
->axi_ck
) == -EPROBE_DEFER
)
869 return -EPROBE_DEFER
;
874 snprintf(name
, sizeof(name
), "aux_ck%d", slot
);
875 port
->aux_ck
= devm_clk_get(dev
, name
);
876 if (IS_ERR(port
->aux_ck
)) {
877 if (PTR_ERR(port
->aux_ck
) == -EPROBE_DEFER
)
878 return -EPROBE_DEFER
;
883 snprintf(name
, sizeof(name
), "obff_ck%d", slot
);
884 port
->obff_ck
= devm_clk_get(dev
, name
);
885 if (IS_ERR(port
->obff_ck
)) {
886 if (PTR_ERR(port
->obff_ck
) == -EPROBE_DEFER
)
887 return -EPROBE_DEFER
;
889 port
->obff_ck
= NULL
;
892 snprintf(name
, sizeof(name
), "pipe_ck%d", slot
);
893 port
->pipe_ck
= devm_clk_get(dev
, name
);
894 if (IS_ERR(port
->pipe_ck
)) {
895 if (PTR_ERR(port
->pipe_ck
) == -EPROBE_DEFER
)
896 return -EPROBE_DEFER
;
898 port
->pipe_ck
= NULL
;
901 snprintf(name
, sizeof(name
), "pcie-rst%d", slot
);
902 port
->reset
= devm_reset_control_get_optional_exclusive(dev
, name
);
903 if (PTR_ERR(port
->reset
) == -EPROBE_DEFER
)
904 return PTR_ERR(port
->reset
);
906 /* some platforms may use default PHY setting */
907 snprintf(name
, sizeof(name
), "pcie-phy%d", slot
);
908 port
->phy
= devm_phy_optional_get(dev
, name
);
909 if (IS_ERR(port
->phy
))
910 return PTR_ERR(port
->phy
);
915 if (pcie
->soc
->setup_irq
) {
916 err
= pcie
->soc
->setup_irq(port
, node
);
921 INIT_LIST_HEAD(&port
->list
);
922 list_add_tail(&port
->list
, &pcie
->ports
);
927 static int mtk_pcie_subsys_powerup(struct mtk_pcie
*pcie
)
929 struct device
*dev
= pcie
->dev
;
930 struct platform_device
*pdev
= to_platform_device(dev
);
931 struct resource
*regs
;
934 /* get shared registers, which are optional */
935 regs
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "subsys");
937 pcie
->base
= devm_ioremap_resource(dev
, regs
);
938 if (IS_ERR(pcie
->base
)) {
939 dev_err(dev
, "failed to map shared register\n");
940 return PTR_ERR(pcie
->base
);
944 pcie
->free_ck
= devm_clk_get(dev
, "free_ck");
945 if (IS_ERR(pcie
->free_ck
)) {
946 if (PTR_ERR(pcie
->free_ck
) == -EPROBE_DEFER
)
947 return -EPROBE_DEFER
;
949 pcie
->free_ck
= NULL
;
952 if (dev
->pm_domain
) {
953 pm_runtime_enable(dev
);
954 pm_runtime_get_sync(dev
);
957 /* enable top level clock */
958 err
= clk_prepare_enable(pcie
->free_ck
);
960 dev_err(dev
, "failed to enable free_ck\n");
967 if (dev
->pm_domain
) {
968 pm_runtime_put_sync(dev
);
969 pm_runtime_disable(dev
);
975 static int mtk_pcie_setup(struct mtk_pcie
*pcie
)
977 struct device
*dev
= pcie
->dev
;
978 struct device_node
*node
= dev
->of_node
, *child
;
979 struct of_pci_range_parser parser
;
980 struct of_pci_range range
;
982 struct mtk_pcie_port
*port
, *tmp
;
985 if (of_pci_range_parser_init(&parser
, node
)) {
986 dev_err(dev
, "missing \"ranges\" property\n");
990 for_each_of_pci_range(&parser
, &range
) {
991 err
= of_pci_range_to_resource(&range
, node
, &res
);
995 switch (res
.flags
& IORESOURCE_TYPE_BITS
) {
997 pcie
->offset
.io
= res
.start
- range
.pci_addr
;
999 memcpy(&pcie
->pio
, &res
, sizeof(res
));
1000 pcie
->pio
.name
= node
->full_name
;
1002 pcie
->io
.start
= range
.cpu_addr
;
1003 pcie
->io
.end
= range
.cpu_addr
+ range
.size
- 1;
1004 pcie
->io
.flags
= IORESOURCE_MEM
;
1005 pcie
->io
.name
= "I/O";
1007 memcpy(&res
, &pcie
->io
, sizeof(res
));
1010 case IORESOURCE_MEM
:
1011 pcie
->offset
.mem
= res
.start
- range
.pci_addr
;
1013 memcpy(&pcie
->mem
, &res
, sizeof(res
));
1014 pcie
->mem
.name
= "non-prefetchable";
1019 err
= of_pci_parse_bus_range(node
, &pcie
->busn
);
1021 dev_err(dev
, "failed to parse bus ranges property: %d\n", err
);
1022 pcie
->busn
.name
= node
->name
;
1023 pcie
->busn
.start
= 0;
1024 pcie
->busn
.end
= 0xff;
1025 pcie
->busn
.flags
= IORESOURCE_BUS
;
1028 for_each_available_child_of_node(node
, child
) {
1031 err
= of_pci_get_devfn(child
);
1033 dev_err(dev
, "failed to parse devfn: %d\n", err
);
1037 slot
= PCI_SLOT(err
);
1039 err
= mtk_pcie_parse_port(pcie
, child
, slot
);
1044 err
= mtk_pcie_subsys_powerup(pcie
);
1048 /* enable each port, and then check link status */
1049 list_for_each_entry_safe(port
, tmp
, &pcie
->ports
, list
)
1050 mtk_pcie_enable_port(port
);
1052 /* power down PCIe subsys if slots are all empty (link down) */
1053 if (list_empty(&pcie
->ports
))
1054 mtk_pcie_subsys_powerdown(pcie
);
1059 static int mtk_pcie_request_resources(struct mtk_pcie
*pcie
)
1061 struct pci_host_bridge
*host
= pci_host_bridge_from_priv(pcie
);
1062 struct list_head
*windows
= &host
->windows
;
1063 struct device
*dev
= pcie
->dev
;
1066 pci_add_resource_offset(windows
, &pcie
->pio
, pcie
->offset
.io
);
1067 pci_add_resource_offset(windows
, &pcie
->mem
, pcie
->offset
.mem
);
1068 pci_add_resource(windows
, &pcie
->busn
);
1070 err
= devm_request_pci_bus_resources(dev
, windows
);
1074 pci_remap_iospace(&pcie
->pio
, pcie
->io
.start
);
1079 static int mtk_pcie_register_host(struct pci_host_bridge
*host
)
1081 struct mtk_pcie
*pcie
= pci_host_bridge_priv(host
);
1082 struct pci_bus
*child
;
1085 host
->busnr
= pcie
->busn
.start
;
1086 host
->dev
.parent
= pcie
->dev
;
1087 host
->ops
= pcie
->soc
->ops
;
1088 host
->map_irq
= of_irq_parse_and_map_pci
;
1089 host
->swizzle_irq
= pci_common_swizzle
;
1090 host
->sysdata
= pcie
;
1091 if (IS_ENABLED(CONFIG_PCI_MSI
) && pcie
->soc
->has_msi
)
1092 host
->msi
= &mtk_pcie_msi_chip
;
1094 err
= pci_scan_root_bus_bridge(host
);
1098 pci_bus_size_bridges(host
->bus
);
1099 pci_bus_assign_resources(host
->bus
);
1101 list_for_each_entry(child
, &host
->bus
->children
, node
)
1102 pcie_bus_configure_settings(child
);
1104 pci_bus_add_devices(host
->bus
);
1109 static int mtk_pcie_probe(struct platform_device
*pdev
)
1111 struct device
*dev
= &pdev
->dev
;
1112 struct mtk_pcie
*pcie
;
1113 struct pci_host_bridge
*host
;
1116 host
= devm_pci_alloc_host_bridge(dev
, sizeof(*pcie
));
1120 pcie
= pci_host_bridge_priv(host
);
1123 pcie
->soc
= of_device_get_match_data(dev
);
1124 platform_set_drvdata(pdev
, pcie
);
1125 INIT_LIST_HEAD(&pcie
->ports
);
1127 err
= mtk_pcie_setup(pcie
);
1131 err
= mtk_pcie_request_resources(pcie
);
1135 err
= mtk_pcie_register_host(host
);
1142 if (!list_empty(&pcie
->ports
))
1143 mtk_pcie_put_resources(pcie
);
1148 static const struct mtk_pcie_soc mtk_pcie_soc_v1
= {
1149 .ops
= &mtk_pcie_ops
,
1150 .startup
= mtk_pcie_startup_port
,
1153 static const struct mtk_pcie_soc mtk_pcie_soc_v2
= {
1155 .ops
= &mtk_pcie_ops_v2
,
1156 .startup
= mtk_pcie_startup_port_v2
,
1157 .setup_irq
= mtk_pcie_setup_irq
,
1160 static const struct of_device_id mtk_pcie_ids
[] = {
1161 { .compatible
= "mediatek,mt2701-pcie", .data
= &mtk_pcie_soc_v1
},
1162 { .compatible
= "mediatek,mt7623-pcie", .data
= &mtk_pcie_soc_v1
},
1163 { .compatible
= "mediatek,mt2712-pcie", .data
= &mtk_pcie_soc_v2
},
1164 { .compatible
= "mediatek,mt7622-pcie", .data
= &mtk_pcie_soc_v2
},
1168 static struct platform_driver mtk_pcie_driver
= {
1169 .probe
= mtk_pcie_probe
,
1172 .of_match_table
= mtk_pcie_ids
,
1173 .suppress_bind_attrs
= true,
1176 builtin_platform_driver(mtk_pcie_driver
);