1 // SPDX-License-Identifier: GPL-2.0+
3 * BRIEF MODULE DESCRIPTION
4 * PCI init for Ralink RT2880 solution
6 * Copyright 2007 Ralink Inc. (bruce_chang@ralinktech.com.tw)
11 * May 2009 Bruce Chang
12 * support RT2880/RT3883 PCIe
14 * May 2011 Bruce Chang
15 * support RT6855/MT7620 PCIe
18 #include <linux/bitops.h>
19 #include <linux/delay.h>
20 #include <linux/iopoll.h>
21 #include <linux/module.h>
23 #include <linux/of_address.h>
24 #include <linux/of_irq.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/reset.h>
32 #include <ralink_regs.h>
34 #include "../../pci/pci.h"
37 #define MT7621_CHIP_REV_ID 0x0c
38 #define CHIP_REV_MT7621_E2 0x0101
40 /* MediaTek specific configuration registers */
41 #define PCIE_FTS_NUM 0x70c
42 #define PCIE_FTS_NUM_MASK GENMASK(15, 8)
43 #define PCIE_FTS_NUM_L0(x) ((x) & 0xff << 8)
45 /* rt_sysc_membase relative registers */
46 #define RALINK_PCIE_CLK_GEN 0x7c
47 #define RALINK_PCIE_CLK_GEN1 0x80
49 /* Host-PCI bridge registers */
50 #define RALINK_PCI_PCICFG_ADDR 0x0000
51 #define RALINK_PCI_PCIMSK_ADDR 0x000C
52 #define RALINK_PCI_CONFIG_ADDR 0x0020
53 #define RALINK_PCI_CONFIG_DATA 0x0024
54 #define RALINK_PCI_MEMBASE 0x0028
55 #define RALINK_PCI_IOBASE 0x002C
57 /* PCICFG virtual bridges */
58 #define MT7621_BR0_MASK GENMASK(19, 16)
59 #define MT7621_BR1_MASK GENMASK(23, 20)
60 #define MT7621_BR2_MASK GENMASK(27, 24)
61 #define MT7621_BR_ALL_MASK GENMASK(27, 16)
62 #define MT7621_BR0_SHIFT 16
63 #define MT7621_BR1_SHIFT 20
64 #define MT7621_BR2_SHIFT 24
66 /* PCIe RC control registers */
67 #define MT7621_PCIE_OFFSET 0x2000
68 #define MT7621_NEXT_PORT 0x1000
70 #define RALINK_PCI_BAR0SETUP_ADDR 0x0010
71 #define RALINK_PCI_IMBASEBAR0_ADDR 0x0018
72 #define RALINK_PCI_ID 0x0030
73 #define RALINK_PCI_CLASS 0x0034
74 #define RALINK_PCI_SUBID 0x0038
75 #define RALINK_PCI_STATUS 0x0050
77 /* Some definition values */
78 #define PCIE_REVISION_ID BIT(0)
79 #define PCIE_CLASS_CODE (0x60400 << 8)
80 #define PCIE_BAR_MAP_MAX GENMASK(30, 16)
81 #define PCIE_BAR_ENABLE BIT(0)
82 #define PCIE_PORT_INT_EN(x) BIT(20 + (x))
83 #define PCIE_PORT_CLK_EN(x) BIT(24 + (x))
84 #define PCIE_PORT_PERST(x) BIT(1 + (x))
85 #define PCIE_PORT_LINKUP BIT(0)
87 #define PCIE_CLK_GEN_EN BIT(31)
88 #define PCIE_CLK_GEN_DIS 0
89 #define PCIE_CLK_GEN1_DIS GENMASK(30, 24)
90 #define PCIE_CLK_GEN1_EN (BIT(27) | BIT(25))
91 #define MEMORY_BASE 0x0
94 * struct mt7621_pcie_port - PCIe port information
95 * @base: I/O mapped register base
97 * @pcie: pointer to PCIe host info
98 * @phy: pointer to PHY control block
99 * @pcie_rst: pointer to port reset control
101 * @enabled: indicates if port is enabled
103 struct mt7621_pcie_port
{
105 struct list_head list
;
106 struct mt7621_pcie
*pcie
;
108 struct reset_control
*pcie_rst
;
114 * struct mt7621_pcie - PCIe host information
115 * @base: IO Mapped Register Base
117 * @mem: non-prefetchable memory resource
119 * @offset: IO / Memory offset
120 * @dev: Pointer to PCIe device
121 * @ports: pointer to PCIe port information
122 * @rst: pointer to pcie reset
129 struct resource busn
;
134 struct list_head ports
;
135 struct reset_control
*rst
;
138 static inline u32
pcie_read(struct mt7621_pcie
*pcie
, u32 reg
)
140 return readl(pcie
->base
+ reg
);
143 static inline void pcie_write(struct mt7621_pcie
*pcie
, u32 val
, u32 reg
)
145 writel(val
, pcie
->base
+ reg
);
148 static inline u32
pcie_port_read(struct mt7621_pcie_port
*port
, u32 reg
)
150 return readl(port
->base
+ reg
);
153 static inline void pcie_port_write(struct mt7621_pcie_port
*port
,
156 writel(val
, port
->base
+ reg
);
159 static inline u32
mt7621_pci_get_cfgaddr(unsigned int bus
, unsigned int slot
,
160 unsigned int func
, unsigned int where
)
162 return (((where
& 0xF00) >> 8) << 24) | (bus
<< 16) | (slot
<< 11) |
163 (func
<< 8) | (where
& 0xfc) | 0x80000000;
166 static void __iomem
*mt7621_pcie_map_bus(struct pci_bus
*bus
,
167 unsigned int devfn
, int where
)
169 struct mt7621_pcie
*pcie
= bus
->sysdata
;
170 u32 address
= mt7621_pci_get_cfgaddr(bus
->number
, PCI_SLOT(devfn
),
171 PCI_FUNC(devfn
), where
);
173 writel(address
, pcie
->base
+ RALINK_PCI_CONFIG_ADDR
);
175 return pcie
->base
+ RALINK_PCI_CONFIG_DATA
+ (where
& 3);
178 struct pci_ops mt7621_pci_ops
= {
179 .map_bus
= mt7621_pcie_map_bus
,
180 .read
= pci_generic_config_read
,
181 .write
= pci_generic_config_write
,
184 static u32
read_config(struct mt7621_pcie
*pcie
, unsigned int dev
, u32 reg
)
186 u32 address
= mt7621_pci_get_cfgaddr(0, dev
, 0, reg
);
188 pcie_write(pcie
, address
, RALINK_PCI_CONFIG_ADDR
);
189 return pcie_read(pcie
, RALINK_PCI_CONFIG_DATA
);
192 static void write_config(struct mt7621_pcie
*pcie
, unsigned int dev
,
195 u32 address
= mt7621_pci_get_cfgaddr(0, dev
, 0, reg
);
197 pcie_write(pcie
, address
, RALINK_PCI_CONFIG_ADDR
);
198 pcie_write(pcie
, val
, RALINK_PCI_CONFIG_DATA
);
201 static inline void mt7621_control_assert(struct mt7621_pcie_port
*port
)
203 u32 chip_rev_id
= rt_sysc_r32(MT7621_CHIP_REV_ID
);
205 if ((chip_rev_id
& 0xFFFF) == CHIP_REV_MT7621_E2
)
206 reset_control_assert(port
->pcie_rst
);
208 reset_control_deassert(port
->pcie_rst
);
211 static inline void mt7621_control_deassert(struct mt7621_pcie_port
*port
)
213 u32 chip_rev_id
= rt_sysc_r32(MT7621_CHIP_REV_ID
);
215 if ((chip_rev_id
& 0xFFFF) == CHIP_REV_MT7621_E2
)
216 reset_control_deassert(port
->pcie_rst
);
218 reset_control_assert(port
->pcie_rst
);
221 static void mt7621_reset_port(struct mt7621_pcie_port
*port
)
223 mt7621_control_assert(port
);
225 mt7621_control_deassert(port
);
228 static void setup_cm_memory_region(struct mt7621_pcie
*pcie
)
230 struct resource
*mem_resource
= &pcie
->mem
;
231 struct device
*dev
= pcie
->dev
;
232 resource_size_t mask
;
234 if (mips_cps_numiocu(0)) {
236 * FIXME: hardware doesn't accept mask values with 1s after
237 * 0s (e.g. 0xffef), so it would be great to warn if that's
240 mask
= ~(mem_resource
->end
- mem_resource
->start
);
242 write_gcr_reg1_base(mem_resource
->start
);
243 write_gcr_reg1_mask(mask
| CM_GCR_REGn_MASK_CMTGT_IOCU0
);
244 dev_info(dev
, "PCI coherence region base: 0x%08llx, mask/settings: 0x%08llx\n",
245 (unsigned long long)read_gcr_reg1_base(),
246 (unsigned long long)read_gcr_reg1_mask());
250 static int mt7621_pci_parse_request_of_pci_ranges(struct mt7621_pcie
*pcie
)
252 struct device
*dev
= pcie
->dev
;
253 struct device_node
*node
= dev
->of_node
;
254 struct of_pci_range_parser parser
;
255 struct of_pci_range range
;
258 if (of_pci_range_parser_init(&parser
, node
)) {
259 dev_err(dev
, "missing \"ranges\" property\n");
263 for_each_of_pci_range(&parser
, &range
) {
264 struct resource
*res
= NULL
;
266 switch (range
.flags
& IORESOURCE_TYPE_BITS
) {
268 ioremap(range
.cpu_addr
, range
.size
);
270 pcie
->offset
.io
= 0x00000000UL
;
274 pcie
->offset
.mem
= 0x00000000UL
;
279 of_pci_range_to_resource(&range
, node
, res
);
282 err
= of_pci_parse_bus_range(node
, &pcie
->busn
);
284 dev_err(dev
, "failed to parse bus ranges property: %d\n", err
);
285 pcie
->busn
.name
= node
->name
;
286 pcie
->busn
.start
= 0;
287 pcie
->busn
.end
= 0xff;
288 pcie
->busn
.flags
= IORESOURCE_BUS
;
294 static int mt7621_pcie_parse_port(struct mt7621_pcie
*pcie
,
295 struct device_node
*node
,
298 struct mt7621_pcie_port
*port
;
299 struct device
*dev
= pcie
->dev
;
300 struct device_node
*pnode
= dev
->of_node
;
301 struct resource regs
;
305 port
= devm_kzalloc(dev
, sizeof(*port
), GFP_KERNEL
);
309 err
= of_address_to_resource(pnode
, slot
+ 1, ®s
);
311 dev_err(dev
, "missing \"reg\" property\n");
315 port
->base
= devm_ioremap_resource(dev
, ®s
);
316 if (IS_ERR(port
->base
))
317 return PTR_ERR(port
->base
);
319 snprintf(name
, sizeof(name
), "pcie%d", slot
);
320 port
->pcie_rst
= devm_reset_control_get_exclusive(dev
, name
);
321 if (PTR_ERR(port
->pcie_rst
) == -EPROBE_DEFER
) {
322 dev_err(dev
, "failed to get pcie%d reset control\n", slot
);
323 return PTR_ERR(port
->pcie_rst
);
326 snprintf(name
, sizeof(name
), "pcie-phy%d", slot
);
327 port
->phy
= devm_phy_get(dev
, name
);
328 if (IS_ERR(port
->phy
))
329 return PTR_ERR(port
->phy
);
334 INIT_LIST_HEAD(&port
->list
);
335 list_add_tail(&port
->list
, &pcie
->ports
);
340 static int mt7621_pcie_parse_dt(struct mt7621_pcie
*pcie
)
342 struct device
*dev
= pcie
->dev
;
343 struct device_node
*node
= dev
->of_node
, *child
;
344 struct resource regs
;
347 err
= of_address_to_resource(node
, 0, ®s
);
349 dev_err(dev
, "missing \"reg\" property\n");
353 pcie
->base
= devm_ioremap_resource(dev
, ®s
);
354 if (IS_ERR(pcie
->base
))
355 return PTR_ERR(pcie
->base
);
357 pcie
->rst
= devm_reset_control_get_exclusive(dev
, "pcie");
358 if (PTR_ERR(pcie
->rst
) == -EPROBE_DEFER
) {
359 dev_err(dev
, "failed to get pcie reset control\n");
360 return PTR_ERR(pcie
->rst
);
363 for_each_available_child_of_node(node
, child
) {
366 err
= of_pci_get_devfn(child
);
368 dev_err(dev
, "failed to parse devfn: %d\n", err
);
372 slot
= PCI_SLOT(err
);
374 err
= mt7621_pcie_parse_port(pcie
, child
, slot
);
382 static int mt7621_pcie_init_port(struct mt7621_pcie_port
*port
)
384 struct mt7621_pcie
*pcie
= port
->pcie
;
385 struct device
*dev
= pcie
->dev
;
386 u32 slot
= port
->slot
;
391 * Any MT7621 Ralink pcie controller that doesn't have 0x0101 at
392 * the end of the chip_id has inverted PCI resets.
394 mt7621_reset_port(port
);
396 val
= read_config(pcie
, slot
, PCIE_FTS_NUM
);
397 dev_info(dev
, "Port %d N_FTS = %x\n", (unsigned int)val
, slot
);
399 err
= phy_init(port
->phy
);
401 dev_err(dev
, "failed to initialize port%d phy\n", slot
);
405 err
= phy_power_on(port
->phy
);
407 dev_err(dev
, "failed to power on port%d phy\n", slot
);
411 if ((pcie_port_read(port
, RALINK_PCI_STATUS
) & PCIE_PORT_LINKUP
) == 0) {
412 dev_err(dev
, "pcie%d no card, disable it (RST & CLK)\n", slot
);
413 mt7621_control_assert(port
);
414 port
->enabled
= false;
419 port
->enabled
= true;
424 phy_power_off(port
->phy
);
431 static void mt7621_pcie_init_ports(struct mt7621_pcie
*pcie
)
433 struct device
*dev
= pcie
->dev
;
434 struct mt7621_pcie_port
*port
, *tmp
;
437 list_for_each_entry_safe(port
, tmp
, &pcie
->ports
, list
) {
438 u32 slot
= port
->slot
;
440 err
= mt7621_pcie_init_port(port
);
442 dev_err(dev
, "Initiating port %d failed\n", slot
);
443 list_del(&port
->list
);
447 reset_control_assert(pcie
->rst
);
448 rt_sysc_m32(0x30, 2 << 4, SYSC_REG_SYSTEM_CONFIG1
);
449 rt_sysc_m32(PCIE_CLK_GEN_EN
, PCIE_CLK_GEN_DIS
, RALINK_PCIE_CLK_GEN
);
450 rt_sysc_m32(PCIE_CLK_GEN1_DIS
, PCIE_CLK_GEN1_EN
, RALINK_PCIE_CLK_GEN1
);
451 rt_sysc_m32(PCIE_CLK_GEN_DIS
, PCIE_CLK_GEN_EN
, RALINK_PCIE_CLK_GEN
);
453 reset_control_deassert(pcie
->rst
);
456 static int mt7621_pcie_enable_port(struct mt7621_pcie_port
*port
)
458 struct mt7621_pcie
*pcie
= port
->pcie
;
459 u32 slot
= port
->slot
;
460 u32 offset
= MT7621_PCIE_OFFSET
+ (slot
* MT7621_NEXT_PORT
);
464 /* assert port PERST_N */
465 val
= pcie_read(pcie
, RALINK_PCI_PCICFG_ADDR
);
466 val
|= PCIE_PORT_PERST(slot
);
467 pcie_write(pcie
, val
, RALINK_PCI_PCICFG_ADDR
);
469 /* de-assert port PERST_N */
470 val
= pcie_read(pcie
, RALINK_PCI_PCICFG_ADDR
);
471 val
&= ~PCIE_PORT_PERST(slot
);
472 pcie_write(pcie
, val
, RALINK_PCI_PCICFG_ADDR
);
474 /* 100ms timeout value should be enough for Gen1 training */
475 err
= readl_poll_timeout(port
->base
+ RALINK_PCI_STATUS
,
476 val
, !!(val
& PCIE_PORT_LINKUP
),
477 20, 100 * USEC_PER_MSEC
);
481 /* enable pcie interrupt */
482 val
= pcie_read(pcie
, RALINK_PCI_PCIMSK_ADDR
);
483 val
|= PCIE_PORT_INT_EN(slot
);
484 pcie_write(pcie
, val
, RALINK_PCI_PCIMSK_ADDR
);
486 /* map 2G DDR region */
487 pcie_write(pcie
, PCIE_BAR_MAP_MAX
| PCIE_BAR_ENABLE
,
488 offset
+ RALINK_PCI_BAR0SETUP_ADDR
);
489 pcie_write(pcie
, MEMORY_BASE
,
490 offset
+ RALINK_PCI_IMBASEBAR0_ADDR
);
492 /* configure class code and revision ID */
493 pcie_write(pcie
, PCIE_CLASS_CODE
| PCIE_REVISION_ID
,
494 offset
+ RALINK_PCI_CLASS
);
499 static void mt7621_pcie_enable_ports(struct mt7621_pcie
*pcie
)
501 struct device
*dev
= pcie
->dev
;
502 struct mt7621_pcie_port
*port
;
503 u8 num_slots_enabled
= 0;
507 list_for_each_entry(port
, &pcie
->ports
, list
) {
509 if (mt7621_pcie_enable_port(port
)) {
510 dev_err(dev
, "de-assert port %d PERST_N\n",
514 dev_info(dev
, "PCIE%d enabled\n", slot
);
519 for (slot
= 0; slot
< num_slots_enabled
; slot
++) {
520 val
= read_config(pcie
, slot
, PCI_COMMAND
);
521 val
|= PCI_COMMAND_MASTER
;
522 write_config(pcie
, slot
, PCI_COMMAND
, val
);
523 /* configure RC FTS number to 250 when it leaves L0s */
524 val
= read_config(pcie
, slot
, PCIE_FTS_NUM
);
525 val
&= ~PCIE_FTS_NUM_MASK
;
526 val
|= PCIE_FTS_NUM_L0(0x50);
527 write_config(pcie
, slot
, PCIE_FTS_NUM
, val
);
531 static int mt7621_pcie_init_virtual_bridges(struct mt7621_pcie
*pcie
)
533 u32 pcie_link_status
= 0;
535 struct mt7621_pcie_port
*port
;
537 list_for_each_entry(port
, &pcie
->ports
, list
) {
538 u32 slot
= port
->slot
;
541 pcie_link_status
|= BIT(slot
);
544 if (pcie_link_status
== 0)
548 * pcie(2/1/0) link status pcie2_num pcie1_num pcie0_num
558 switch (pcie_link_status
) {
560 val
= pcie_read(pcie
, RALINK_PCI_PCICFG_ADDR
);
561 val
&= ~(MT7621_BR0_MASK
| MT7621_BR1_MASK
);
562 val
|= 0x1 << MT7621_BR0_SHIFT
;
563 val
|= 0x0 << MT7621_BR1_SHIFT
;
564 pcie_write(pcie
, val
, RALINK_PCI_PCICFG_ADDR
);
567 val
= pcie_read(pcie
, RALINK_PCI_PCICFG_ADDR
);
568 val
&= ~MT7621_BR_ALL_MASK
;
569 val
|= 0x1 << MT7621_BR0_SHIFT
;
570 val
|= 0x2 << MT7621_BR1_SHIFT
;
571 val
|= 0x0 << MT7621_BR2_SHIFT
;
572 pcie_write(pcie
, val
, RALINK_PCI_PCICFG_ADDR
);
575 val
= pcie_read(pcie
, RALINK_PCI_PCICFG_ADDR
);
576 val
&= ~MT7621_BR_ALL_MASK
;
577 val
|= 0x0 << MT7621_BR0_SHIFT
;
578 val
|= 0x2 << MT7621_BR1_SHIFT
;
579 val
|= 0x1 << MT7621_BR2_SHIFT
;
580 pcie_write(pcie
, val
, RALINK_PCI_PCICFG_ADDR
);
583 val
= pcie_read(pcie
, RALINK_PCI_PCICFG_ADDR
);
584 val
&= ~MT7621_BR_ALL_MASK
;
585 val
|= 0x2 << MT7621_BR0_SHIFT
;
586 val
|= 0x0 << MT7621_BR1_SHIFT
;
587 val
|= 0x1 << MT7621_BR2_SHIFT
;
588 pcie_write(pcie
, val
, RALINK_PCI_PCICFG_ADDR
);
595 static int mt7621_pcie_request_resources(struct mt7621_pcie
*pcie
,
596 struct list_head
*res
)
598 struct device
*dev
= pcie
->dev
;
601 pci_add_resource_offset(res
, &pcie
->io
, pcie
->offset
.io
);
602 pci_add_resource_offset(res
, &pcie
->mem
, pcie
->offset
.mem
);
603 pci_add_resource(res
, &pcie
->busn
);
605 err
= devm_request_pci_bus_resources(dev
, res
);
612 static int mt7621_pcie_register_host(struct pci_host_bridge
*host
,
613 struct list_head
*res
)
615 struct mt7621_pcie
*pcie
= pci_host_bridge_priv(host
);
617 list_splice_init(res
, &host
->windows
);
618 host
->busnr
= pcie
->busn
.start
;
619 host
->dev
.parent
= pcie
->dev
;
620 host
->ops
= &mt7621_pci_ops
;
621 host
->map_irq
= of_irq_parse_and_map_pci
;
622 host
->swizzle_irq
= pci_common_swizzle
;
623 host
->sysdata
= pcie
;
625 return pci_host_probe(host
);
628 static int mt7621_pci_probe(struct platform_device
*pdev
)
630 struct device
*dev
= &pdev
->dev
;
631 struct mt7621_pcie
*pcie
;
632 struct pci_host_bridge
*bridge
;
639 bridge
= devm_pci_alloc_host_bridge(dev
, sizeof(*pcie
));
643 pcie
= pci_host_bridge_priv(bridge
);
645 platform_set_drvdata(pdev
, pcie
);
646 INIT_LIST_HEAD(&pcie
->ports
);
648 err
= mt7621_pcie_parse_dt(pcie
);
650 dev_err(dev
, "Parsing DT failed\n");
654 /* set resources limits */
655 iomem_resource
.start
= 0;
656 iomem_resource
.end
= ~0UL; /* no limit */
657 ioport_resource
.start
= 0;
658 ioport_resource
.end
= ~0UL; /* no limit */
660 mt7621_pcie_init_ports(pcie
);
662 err
= mt7621_pcie_init_virtual_bridges(pcie
);
664 dev_err(dev
, "Nothing is connected in virtual bridges. Exiting...");
668 mt7621_pcie_enable_ports(pcie
);
670 err
= mt7621_pci_parse_request_of_pci_ranges(pcie
);
672 dev_err(dev
, "Error requesting pci resources from ranges");
676 setup_cm_memory_region(pcie
);
678 err
= mt7621_pcie_request_resources(pcie
, &res
);
680 dev_err(dev
, "Error requesting resources\n");
684 err
= mt7621_pcie_register_host(bridge
, &res
);
686 dev_err(dev
, "Error registering host\n");
693 static const struct of_device_id mt7621_pci_ids
[] = {
694 { .compatible
= "mediatek,mt7621-pci" },
697 MODULE_DEVICE_TABLE(of
, mt7621_pci_ids
);
699 static struct platform_driver mt7621_pci_driver
= {
700 .probe
= mt7621_pci_probe
,
702 .name
= "mt7621-pci",
703 .of_match_table
= of_match_ptr(mt7621_pci_ids
),
707 static int __init
mt7621_pci_init(void)
709 return platform_driver_register(&mt7621_pci_driver
);
712 arch_initcall(mt7621_pci_init
);