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/gpio/consumer.h>
21 #include <linux/iopoll.h>
22 #include <linux/module.h>
24 #include <linux/of_address.h>
25 #include <linux/of_irq.h>
26 #include <linux/of_pci.h>
27 #include <linux/of_platform.h>
28 #include <linux/pci.h>
29 #include <linux/phy/phy.h>
30 #include <linux/platform_device.h>
31 #include <linux/reset.h>
32 #include <linux/sys_soc.h>
34 #include <ralink_regs.h>
36 #include "../../pci/pci.h"
39 #define MT7621_GPIO_MODE 0x60
41 /* MediaTek specific configuration registers */
42 #define PCIE_FTS_NUM 0x70c
43 #define PCIE_FTS_NUM_MASK GENMASK(15, 8)
44 #define PCIE_FTS_NUM_L0(x) (((x) & 0xff) << 8)
46 /* rt_sysc_membase relative registers */
47 #define RALINK_CLKCFG1 0x30
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 PCIE_P2P_CNT 3
59 #define PCIE_P2P_BR_DEVNUM_SHIFT(p) (16 + (p) * 4)
60 #define PCIE_P2P_BR_DEVNUM0_SHIFT PCIE_P2P_BR_DEVNUM_SHIFT(0)
61 #define PCIE_P2P_BR_DEVNUM1_SHIFT PCIE_P2P_BR_DEVNUM_SHIFT(1)
62 #define PCIE_P2P_BR_DEVNUM2_SHIFT PCIE_P2P_BR_DEVNUM_SHIFT(2)
63 #define PCIE_P2P_BR_DEVNUM_MASK 0xf
64 #define PCIE_P2P_BR_DEVNUM_MASK_FULL (0xfff << PCIE_P2P_BR_DEVNUM0_SHIFT)
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_ID 0x0030
72 #define RALINK_PCI_CLASS 0x0034
73 #define RALINK_PCI_SUBID 0x0038
74 #define RALINK_PCI_STATUS 0x0050
76 /* Some definition values */
77 #define PCIE_REVISION_ID BIT(0)
78 #define PCIE_CLASS_CODE (0x60400 << 8)
79 #define PCIE_BAR_MAP_MAX GENMASK(30, 16)
80 #define PCIE_BAR_ENABLE BIT(0)
81 #define PCIE_PORT_INT_EN(x) BIT(20 + (x))
82 #define PCIE_PORT_CLK_EN(x) BIT(24 + (x))
83 #define PCIE_PORT_LINKUP BIT(0)
85 #define PERST_MODE_MASK GENMASK(11, 10)
86 #define PERST_MODE_GPIO BIT(10)
87 #define PERST_DELAY_MS 100
90 * struct mt7621_pcie_port - PCIe port information
91 * @base: I/O mapped register base
93 * @pcie: pointer to PCIe host info
94 * @phy: pointer to PHY control block
95 * @pcie_rst: pointer to port reset control
96 * @gpio_rst: gpio reset
99 * @enabled: indicates if port is enabled
101 struct mt7621_pcie_port
{
103 struct list_head list
;
104 struct mt7621_pcie
*pcie
;
106 struct reset_control
*pcie_rst
;
107 struct gpio_desc
*gpio_rst
;
114 * struct mt7621_pcie - PCIe host information
115 * @base: IO Mapped Register Base
117 * @mem: pointer to non-prefetchable memory resource
118 * @dev: Pointer to PCIe device
119 * @io_map_base: virtual memory base address for io
120 * @ports: pointer to PCIe port information
121 * @irq_map: irq mapping info according pcie link status
122 * @resets_inverted: depends on chip revision
123 * reset lines are inverted.
129 struct resource
*mem
;
130 unsigned long io_map_base
;
131 struct list_head ports
;
132 int irq_map
[PCIE_P2P_CNT
];
133 bool resets_inverted
;
136 static inline u32
pcie_read(struct mt7621_pcie
*pcie
, u32 reg
)
138 return readl(pcie
->base
+ reg
);
141 static inline void pcie_write(struct mt7621_pcie
*pcie
, u32 val
, u32 reg
)
143 writel(val
, pcie
->base
+ reg
);
146 static inline void pcie_rmw(struct mt7621_pcie
*pcie
, u32 reg
, u32 clr
, u32 set
)
148 u32 val
= readl(pcie
->base
+ reg
);
152 writel(val
, pcie
->base
+ reg
);
155 static inline u32
pcie_port_read(struct mt7621_pcie_port
*port
, u32 reg
)
157 return readl(port
->base
+ reg
);
160 static inline void pcie_port_write(struct mt7621_pcie_port
*port
,
163 writel(val
, port
->base
+ reg
);
166 static inline u32
mt7621_pci_get_cfgaddr(unsigned int bus
, unsigned int slot
,
167 unsigned int func
, unsigned int where
)
169 return (((where
& 0xF00) >> 8) << 24) | (bus
<< 16) | (slot
<< 11) |
170 (func
<< 8) | (where
& 0xfc) | 0x80000000;
173 static void __iomem
*mt7621_pcie_map_bus(struct pci_bus
*bus
,
174 unsigned int devfn
, int where
)
176 struct mt7621_pcie
*pcie
= bus
->sysdata
;
177 u32 address
= mt7621_pci_get_cfgaddr(bus
->number
, PCI_SLOT(devfn
),
178 PCI_FUNC(devfn
), where
);
180 writel(address
, pcie
->base
+ RALINK_PCI_CONFIG_ADDR
);
182 return pcie
->base
+ RALINK_PCI_CONFIG_DATA
+ (where
& 3);
185 struct pci_ops mt7621_pci_ops
= {
186 .map_bus
= mt7621_pcie_map_bus
,
187 .read
= pci_generic_config_read
,
188 .write
= pci_generic_config_write
,
191 static u32
read_config(struct mt7621_pcie
*pcie
, unsigned int dev
, u32 reg
)
193 u32 address
= mt7621_pci_get_cfgaddr(0, dev
, 0, reg
);
195 pcie_write(pcie
, address
, RALINK_PCI_CONFIG_ADDR
);
196 return pcie_read(pcie
, RALINK_PCI_CONFIG_DATA
);
199 static void write_config(struct mt7621_pcie
*pcie
, unsigned int dev
,
202 u32 address
= mt7621_pci_get_cfgaddr(0, dev
, 0, reg
);
204 pcie_write(pcie
, address
, RALINK_PCI_CONFIG_ADDR
);
205 pcie_write(pcie
, val
, RALINK_PCI_CONFIG_DATA
);
208 static inline void mt7621_rst_gpio_pcie_assert(struct mt7621_pcie_port
*port
)
211 gpiod_set_value(port
->gpio_rst
, 1);
214 static inline void mt7621_rst_gpio_pcie_deassert(struct mt7621_pcie_port
*port
)
217 gpiod_set_value(port
->gpio_rst
, 0);
220 static inline bool mt7621_pcie_port_is_linkup(struct mt7621_pcie_port
*port
)
222 return (pcie_port_read(port
, RALINK_PCI_STATUS
) & PCIE_PORT_LINKUP
) != 0;
225 static inline void mt7621_pcie_port_clk_enable(struct mt7621_pcie_port
*port
)
227 rt_sysc_m32(0, PCIE_PORT_CLK_EN(port
->slot
), RALINK_CLKCFG1
);
230 static inline void mt7621_pcie_port_clk_disable(struct mt7621_pcie_port
*port
)
232 rt_sysc_m32(PCIE_PORT_CLK_EN(port
->slot
), 0, RALINK_CLKCFG1
);
235 static inline void mt7621_control_assert(struct mt7621_pcie_port
*port
)
237 struct mt7621_pcie
*pcie
= port
->pcie
;
239 if (pcie
->resets_inverted
)
240 reset_control_assert(port
->pcie_rst
);
242 reset_control_deassert(port
->pcie_rst
);
245 static inline void mt7621_control_deassert(struct mt7621_pcie_port
*port
)
247 struct mt7621_pcie
*pcie
= port
->pcie
;
249 if (pcie
->resets_inverted
)
250 reset_control_deassert(port
->pcie_rst
);
252 reset_control_assert(port
->pcie_rst
);
255 static void setup_cm_memory_region(struct mt7621_pcie
*pcie
)
257 struct resource
*mem_resource
= pcie
->mem
;
258 struct device
*dev
= pcie
->dev
;
259 resource_size_t mask
;
261 if (mips_cps_numiocu(0)) {
263 * FIXME: hardware doesn't accept mask values with 1s after
264 * 0s (e.g. 0xffef), so it would be great to warn if that's
267 mask
= ~(mem_resource
->end
- mem_resource
->start
);
269 write_gcr_reg1_base(mem_resource
->start
);
270 write_gcr_reg1_mask(mask
| CM_GCR_REGn_MASK_CMTGT_IOCU0
);
271 dev_info(dev
, "PCI coherence region base: 0x%08llx, mask/settings: 0x%08llx\n",
272 (unsigned long long)read_gcr_reg1_base(),
273 (unsigned long long)read_gcr_reg1_mask());
277 static int mt7621_map_irq(const struct pci_dev
*pdev
, u8 slot
, u8 pin
)
279 struct mt7621_pcie
*pcie
= pdev
->bus
->sysdata
;
280 struct device
*dev
= pcie
->dev
;
281 int irq
= pcie
->irq_map
[slot
];
283 dev_info(dev
, "bus=%d slot=%d irq=%d\n", pdev
->bus
->number
, slot
, irq
);
287 static int mt7621_pci_parse_request_of_pci_ranges(struct pci_host_bridge
*host
)
289 struct mt7621_pcie
*pcie
= pci_host_bridge_priv(host
);
290 struct device
*dev
= pcie
->dev
;
291 struct device_node
*node
= dev
->of_node
;
292 struct of_pci_range_parser parser
;
293 struct resource_entry
*entry
;
294 struct of_pci_range range
;
297 if (of_pci_range_parser_init(&parser
, node
)) {
298 dev_err(dev
, "missing \"ranges\" property\n");
303 * IO_SPACE_LIMIT for MIPS is 0xffff but this platform uses IO at
304 * upper address 0x001e160000 so we have to get the resource from
305 * the DT because when it has been requested it failed and has been
306 * removed from bridge->dma_ranges and bridge->windows. So parse it
307 * and remap it manually to make things work.
309 for_each_of_pci_range(&parser
, &range
) {
310 switch (range
.flags
& IORESOURCE_TYPE_BITS
) {
313 (unsigned long)ioremap(range
.cpu_addr
,
315 of_pci_range_to_resource(&range
, node
, &pcie
->io
);
316 pcie
->io
.start
= range
.cpu_addr
;
317 pcie
->io
.end
= range
.cpu_addr
+ range
.size
- 1;
318 set_io_port_base(pcie
->io_map_base
);
323 entry
= resource_list_first_type(&host
->windows
, IORESOURCE_MEM
);
325 dev_err(dev
, "Cannot get memory resource");
329 pcie
->mem
= entry
->res
;
330 pci_add_resource(&res
, &pcie
->io
);
331 pci_add_resource(&res
, entry
->res
);
332 list_splice_init(&res
, &host
->windows
);
337 static int mt7621_pcie_parse_port(struct mt7621_pcie
*pcie
,
340 struct mt7621_pcie_port
*port
;
341 struct device
*dev
= pcie
->dev
;
342 struct platform_device
*pdev
= to_platform_device(dev
);
345 port
= devm_kzalloc(dev
, sizeof(*port
), GFP_KERNEL
);
349 port
->base
= devm_platform_ioremap_resource(pdev
, slot
+ 1);
350 if (IS_ERR(port
->base
))
351 return PTR_ERR(port
->base
);
353 snprintf(name
, sizeof(name
), "pcie%d", slot
);
354 port
->pcie_rst
= devm_reset_control_get_exclusive(dev
, name
);
355 if (PTR_ERR(port
->pcie_rst
) == -EPROBE_DEFER
) {
356 dev_err(dev
, "failed to get pcie%d reset control\n", slot
);
357 return PTR_ERR(port
->pcie_rst
);
360 snprintf(name
, sizeof(name
), "pcie-phy%d", slot
);
361 port
->phy
= devm_phy_get(dev
, name
);
362 if (IS_ERR(port
->phy
) && slot
!= 1)
363 return PTR_ERR(port
->phy
);
365 port
->gpio_rst
= devm_gpiod_get_index_optional(dev
, "reset", slot
,
367 if (IS_ERR(port
->gpio_rst
)) {
368 dev_err(dev
, "Failed to get GPIO for PCIe%d\n", slot
);
369 return PTR_ERR(port
->gpio_rst
);
375 port
->irq
= platform_get_irq(pdev
, slot
);
377 dev_err(dev
, "Failed to get IRQ for PCIe%d\n", slot
);
381 INIT_LIST_HEAD(&port
->list
);
382 list_add_tail(&port
->list
, &pcie
->ports
);
387 static int mt7621_pcie_parse_dt(struct mt7621_pcie
*pcie
)
389 struct device
*dev
= pcie
->dev
;
390 struct platform_device
*pdev
= to_platform_device(dev
);
391 struct device_node
*node
= dev
->of_node
, *child
;
394 pcie
->base
= devm_platform_ioremap_resource(pdev
, 0);
395 if (IS_ERR(pcie
->base
))
396 return PTR_ERR(pcie
->base
);
398 for_each_available_child_of_node(node
, child
) {
401 err
= of_pci_get_devfn(child
);
404 dev_err(dev
, "failed to parse devfn: %d\n", err
);
408 slot
= PCI_SLOT(err
);
410 err
= mt7621_pcie_parse_port(pcie
, slot
);
420 static int mt7621_pcie_init_port(struct mt7621_pcie_port
*port
)
422 struct mt7621_pcie
*pcie
= port
->pcie
;
423 struct device
*dev
= pcie
->dev
;
424 u32 slot
= port
->slot
;
427 err
= phy_init(port
->phy
);
429 dev_err(dev
, "failed to initialize port%d phy\n", slot
);
433 err
= phy_power_on(port
->phy
);
435 dev_err(dev
, "failed to power on port%d phy\n", slot
);
440 port
->enabled
= true;
445 static void mt7621_pcie_reset_assert(struct mt7621_pcie
*pcie
)
447 struct mt7621_pcie_port
*port
;
449 list_for_each_entry(port
, &pcie
->ports
, list
) {
450 /* PCIe RC reset assert */
451 mt7621_control_assert(port
);
453 /* PCIe EP reset assert */
454 mt7621_rst_gpio_pcie_assert(port
);
457 mdelay(PERST_DELAY_MS
);
460 static void mt7621_pcie_reset_rc_deassert(struct mt7621_pcie
*pcie
)
462 struct mt7621_pcie_port
*port
;
464 list_for_each_entry(port
, &pcie
->ports
, list
)
465 mt7621_control_deassert(port
);
468 static void mt7621_pcie_reset_ep_deassert(struct mt7621_pcie
*pcie
)
470 struct mt7621_pcie_port
*port
;
472 list_for_each_entry(port
, &pcie
->ports
, list
)
473 mt7621_rst_gpio_pcie_deassert(port
);
475 mdelay(PERST_DELAY_MS
);
478 static void mt7621_pcie_init_ports(struct mt7621_pcie
*pcie
)
480 struct device
*dev
= pcie
->dev
;
481 struct mt7621_pcie_port
*port
, *tmp
;
484 rt_sysc_m32(PERST_MODE_MASK
, PERST_MODE_GPIO
, MT7621_GPIO_MODE
);
486 mt7621_pcie_reset_assert(pcie
);
487 mt7621_pcie_reset_rc_deassert(pcie
);
489 list_for_each_entry_safe(port
, tmp
, &pcie
->ports
, list
) {
490 u32 slot
= port
->slot
;
493 port
->enabled
= true;
497 err
= mt7621_pcie_init_port(port
);
499 dev_err(dev
, "Initiating port %d failed\n", slot
);
500 list_del(&port
->list
);
504 mt7621_pcie_reset_ep_deassert(pcie
);
507 list_for_each_entry(port
, &pcie
->ports
, list
) {
508 u32 slot
= port
->slot
;
510 if (!mt7621_pcie_port_is_linkup(port
)) {
511 dev_err(dev
, "pcie%d no card, disable it (RST & CLK)\n",
513 mt7621_control_assert(port
);
514 mt7621_pcie_port_clk_disable(port
);
515 port
->enabled
= false;
522 if (slot
== 1 && tmp
&& !tmp
->enabled
)
523 phy_power_off(tmp
->phy
);
529 static void mt7621_pcie_enable_port(struct mt7621_pcie_port
*port
)
531 struct mt7621_pcie
*pcie
= port
->pcie
;
532 u32 slot
= port
->slot
;
533 u32 offset
= MT7621_PCIE_OFFSET
+ (slot
* MT7621_NEXT_PORT
);
536 /* enable pcie interrupt */
537 val
= pcie_read(pcie
, RALINK_PCI_PCIMSK_ADDR
);
538 val
|= PCIE_PORT_INT_EN(slot
);
539 pcie_write(pcie
, val
, RALINK_PCI_PCIMSK_ADDR
);
541 /* map 2G DDR region */
542 pcie_write(pcie
, PCIE_BAR_MAP_MAX
| PCIE_BAR_ENABLE
,
543 offset
+ RALINK_PCI_BAR0SETUP_ADDR
);
545 /* configure class code and revision ID */
546 pcie_write(pcie
, PCIE_CLASS_CODE
| PCIE_REVISION_ID
,
547 offset
+ RALINK_PCI_CLASS
);
550 static void mt7621_pcie_enable_ports(struct mt7621_pcie
*pcie
)
552 struct device
*dev
= pcie
->dev
;
553 struct mt7621_pcie_port
*port
;
554 u8 num_slots_enabled
= 0;
558 /* Setup MEMWIN and IOWIN */
559 pcie_write(pcie
, 0xffffffff, RALINK_PCI_MEMBASE
);
560 pcie_write(pcie
, pcie
->io
.start
, RALINK_PCI_IOBASE
);
562 list_for_each_entry(port
, &pcie
->ports
, list
) {
564 mt7621_pcie_port_clk_enable(port
);
565 mt7621_pcie_enable_port(port
);
566 dev_info(dev
, "PCIE%d enabled\n", port
->slot
);
571 for (slot
= 0; slot
< num_slots_enabled
; slot
++) {
572 val
= read_config(pcie
, slot
, PCI_COMMAND
);
573 val
|= PCI_COMMAND_MASTER
;
574 write_config(pcie
, slot
, PCI_COMMAND
, val
);
575 /* configure RC FTS number to 250 when it leaves L0s */
576 val
= read_config(pcie
, slot
, PCIE_FTS_NUM
);
577 val
&= ~PCIE_FTS_NUM_MASK
;
578 val
|= PCIE_FTS_NUM_L0(0x50);
579 write_config(pcie
, slot
, PCIE_FTS_NUM
, val
);
583 static int mt7621_pcie_init_virtual_bridges(struct mt7621_pcie
*pcie
)
585 u32 pcie_link_status
= 0;
588 u32 p2p_br_devnum
[PCIE_P2P_CNT
];
589 int irqs
[PCIE_P2P_CNT
];
590 struct mt7621_pcie_port
*port
;
592 list_for_each_entry(port
, &pcie
->ports
, list
) {
593 u32 slot
= port
->slot
;
595 irqs
[i
++] = port
->irq
;
597 pcie_link_status
|= BIT(slot
);
600 if (pcie_link_status
== 0)
604 * Assign device numbers from zero to the enabled ports,
605 * then assigning remaining device numbers to any disabled
608 for (i
= 0; i
< PCIE_P2P_CNT
; i
++)
609 if (pcie_link_status
& BIT(i
))
610 p2p_br_devnum
[i
] = n
++;
612 for (i
= 0; i
< PCIE_P2P_CNT
; i
++)
613 if ((pcie_link_status
& BIT(i
)) == 0)
614 p2p_br_devnum
[i
] = n
++;
616 pcie_rmw(pcie
, RALINK_PCI_PCICFG_ADDR
,
617 PCIE_P2P_BR_DEVNUM_MASK_FULL
,
618 (p2p_br_devnum
[0] << PCIE_P2P_BR_DEVNUM0_SHIFT
) |
619 (p2p_br_devnum
[1] << PCIE_P2P_BR_DEVNUM1_SHIFT
) |
620 (p2p_br_devnum
[2] << PCIE_P2P_BR_DEVNUM2_SHIFT
));
624 for (i
= 0; i
< PCIE_P2P_CNT
; i
++)
625 if (pcie_link_status
& BIT(i
))
626 pcie
->irq_map
[n
++] = irqs
[i
];
628 for (i
= n
; i
< PCIE_P2P_CNT
; i
++)
629 pcie
->irq_map
[i
] = -1;
634 static int mt7621_pcie_register_host(struct pci_host_bridge
*host
)
636 struct mt7621_pcie
*pcie
= pci_host_bridge_priv(host
);
638 host
->ops
= &mt7621_pci_ops
;
639 host
->map_irq
= mt7621_map_irq
;
640 host
->sysdata
= pcie
;
642 return pci_host_probe(host
);
645 static const struct soc_device_attribute mt7621_pci_quirks_match
[] = {
646 { .soc_id
= "mt7621", .revision
= "E2" }
649 static int mt7621_pci_probe(struct platform_device
*pdev
)
651 struct device
*dev
= &pdev
->dev
;
652 const struct soc_device_attribute
*attr
;
653 struct mt7621_pcie
*pcie
;
654 struct pci_host_bridge
*bridge
;
660 bridge
= devm_pci_alloc_host_bridge(dev
, sizeof(*pcie
));
664 pcie
= pci_host_bridge_priv(bridge
);
666 platform_set_drvdata(pdev
, pcie
);
667 INIT_LIST_HEAD(&pcie
->ports
);
669 attr
= soc_device_match(mt7621_pci_quirks_match
);
671 pcie
->resets_inverted
= true;
673 err
= mt7621_pcie_parse_dt(pcie
);
675 dev_err(dev
, "Parsing DT failed\n");
679 err
= mt7621_pci_parse_request_of_pci_ranges(bridge
);
681 dev_err(dev
, "Error requesting pci resources from ranges");
685 /* set resources limits */
686 ioport_resource
.start
= pcie
->io
.start
;
687 ioport_resource
.end
= pcie
->io
.end
;
689 mt7621_pcie_init_ports(pcie
);
691 err
= mt7621_pcie_init_virtual_bridges(pcie
);
693 dev_err(dev
, "Nothing is connected in virtual bridges. Exiting...");
697 mt7621_pcie_enable_ports(pcie
);
699 setup_cm_memory_region(pcie
);
701 err
= mt7621_pcie_register_host(bridge
);
703 dev_err(dev
, "Error registering host\n");
710 static const struct of_device_id mt7621_pci_ids
[] = {
711 { .compatible
= "mediatek,mt7621-pci" },
714 MODULE_DEVICE_TABLE(of
, mt7621_pci_ids
);
716 static struct platform_driver mt7621_pci_driver
= {
717 .probe
= mt7621_pci_probe
,
719 .name
= "mt7621-pci",
720 .of_match_table
= of_match_ptr(mt7621_pci_ids
),
724 builtin_platform_driver(mt7621_pci_driver
);