1 // SPDX-License-Identifier: GPL-2.0+
3 * BRIEF MODULE DESCRIPTION
4 * PCI init for Ralink RT2880 solution
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/clk.h>
20 #include <linux/delay.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/module.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/reset.h>
31 #include <linux/sys_soc.h>
33 /* MediaTek-specific configuration registers */
34 #define PCIE_FTS_NUM 0x70c
35 #define PCIE_FTS_NUM_MASK GENMASK(15, 8)
36 #define PCIE_FTS_NUM_L0(x) (((x) & 0xff) << 8)
38 /* Host-PCI bridge registers */
39 #define RALINK_PCI_PCICFG_ADDR 0x0000
40 #define RALINK_PCI_PCIMSK_ADDR 0x000c
41 #define RALINK_PCI_CONFIG_ADDR 0x0020
42 #define RALINK_PCI_CONFIG_DATA 0x0024
43 #define RALINK_PCI_MEMBASE 0x0028
44 #define RALINK_PCI_IOBASE 0x002c
46 /* PCIe RC control registers */
47 #define RALINK_PCI_ID 0x0030
48 #define RALINK_PCI_CLASS 0x0034
49 #define RALINK_PCI_SUBID 0x0038
50 #define RALINK_PCI_STATUS 0x0050
52 /* Some definition values */
53 #define PCIE_REVISION_ID BIT(0)
54 #define PCIE_CLASS_CODE (0x60400 << 8)
55 #define PCIE_BAR_MAP_MAX GENMASK(30, 16)
56 #define PCIE_BAR_ENABLE BIT(0)
57 #define PCIE_PORT_INT_EN(x) BIT(20 + (x))
58 #define PCIE_PORT_LINKUP BIT(0)
59 #define PCIE_PORT_CNT 3
61 #define PERST_DELAY_MS 100
64 * struct mt7621_pcie_port - PCIe port information
65 * @base: I/O mapped register base
67 * @pcie: pointer to PCIe host info
68 * @clk: pointer to the port clock gate
69 * @phy: pointer to PHY control block
70 * @pcie_rst: pointer to port reset control
71 * @gpio_rst: gpio reset
73 * @enabled: indicates if port is enabled
75 struct mt7621_pcie_port {
77 struct list_head list;
78 struct mt7621_pcie *pcie;
81 struct reset_control *pcie_rst;
82 struct gpio_desc *gpio_rst;
88 * struct mt7621_pcie - PCIe host information
89 * @base: IO Mapped Register Base
90 * @dev: Pointer to PCIe device
91 * @ports: pointer to PCIe port information
92 * @resets_inverted: depends on chip revision
93 * reset lines are inverted.
98 struct list_head ports;
102 static inline u32 pcie_read(struct mt7621_pcie *pcie, u32 reg)
104 return readl_relaxed(pcie->base + reg);
107 static inline void pcie_write(struct mt7621_pcie *pcie, u32 val, u32 reg)
109 writel_relaxed(val, pcie->base + reg);
112 static inline u32 pcie_port_read(struct mt7621_pcie_port *port, u32 reg)
114 return readl_relaxed(port->base + reg);
117 static inline void pcie_port_write(struct mt7621_pcie_port *port,
120 writel_relaxed(val, port->base + reg);
123 static inline u32 mt7621_pcie_get_cfgaddr(unsigned int bus, unsigned int slot,
124 unsigned int func, unsigned int where)
126 return (((where & 0xf00) >> 8) << 24) | (bus << 16) | (slot << 11) |
127 (func << 8) | (where & 0xfc) | 0x80000000;
130 static void __iomem *mt7621_pcie_map_bus(struct pci_bus *bus,
131 unsigned int devfn, int where)
133 struct mt7621_pcie *pcie = bus->sysdata;
134 u32 address = mt7621_pcie_get_cfgaddr(bus->number, PCI_SLOT(devfn),
135 PCI_FUNC(devfn), where);
137 writel_relaxed(address, pcie->base + RALINK_PCI_CONFIG_ADDR);
139 return pcie->base + RALINK_PCI_CONFIG_DATA + (where & 3);
142 static struct pci_ops mt7621_pcie_ops = {
143 .map_bus = mt7621_pcie_map_bus,
144 .read = pci_generic_config_read,
145 .write = pci_generic_config_write,
148 static u32 read_config(struct mt7621_pcie *pcie, unsigned int dev, u32 reg)
150 u32 address = mt7621_pcie_get_cfgaddr(0, dev, 0, reg);
152 pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
153 return pcie_read(pcie, RALINK_PCI_CONFIG_DATA);
156 static void write_config(struct mt7621_pcie *pcie, unsigned int dev,
159 u32 address = mt7621_pcie_get_cfgaddr(0, dev, 0, reg);
161 pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
162 pcie_write(pcie, val, RALINK_PCI_CONFIG_DATA);
165 static inline void mt7621_rst_gpio_pcie_assert(struct mt7621_pcie_port *port)
168 gpiod_set_value(port->gpio_rst, 1);
171 static inline void mt7621_rst_gpio_pcie_deassert(struct mt7621_pcie_port *port)
174 gpiod_set_value(port->gpio_rst, 0);
177 static inline bool mt7621_pcie_port_is_linkup(struct mt7621_pcie_port *port)
179 return (pcie_port_read(port, RALINK_PCI_STATUS) & PCIE_PORT_LINKUP) != 0;
182 static inline void mt7621_control_assert(struct mt7621_pcie_port *port)
184 struct mt7621_pcie *pcie = port->pcie;
186 if (pcie->resets_inverted)
187 reset_control_assert(port->pcie_rst);
189 reset_control_deassert(port->pcie_rst);
192 static inline void mt7621_control_deassert(struct mt7621_pcie_port *port)
194 struct mt7621_pcie *pcie = port->pcie;
196 if (pcie->resets_inverted)
197 reset_control_deassert(port->pcie_rst);
199 reset_control_assert(port->pcie_rst);
202 static int mt7621_pcie_parse_port(struct mt7621_pcie *pcie,
203 struct device_node *node,
206 struct mt7621_pcie_port *port;
207 struct device *dev = pcie->dev;
208 struct platform_device *pdev = to_platform_device(dev);
212 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
216 port->base = devm_platform_ioremap_resource(pdev, slot + 1);
217 if (IS_ERR(port->base))
218 return PTR_ERR(port->base);
220 port->clk = devm_get_clk_from_child(dev, node, NULL);
221 if (IS_ERR(port->clk)) {
222 dev_err(dev, "failed to get pcie%d clock\n", slot);
223 return PTR_ERR(port->clk);
226 port->pcie_rst = of_reset_control_get_exclusive(node, NULL);
227 if (PTR_ERR(port->pcie_rst) == -EPROBE_DEFER) {
228 dev_err(dev, "failed to get pcie%d reset control\n", slot);
229 return PTR_ERR(port->pcie_rst);
232 snprintf(name, sizeof(name), "pcie-phy%d", slot);
233 port->phy = devm_of_phy_get(dev, node, name);
234 if (IS_ERR(port->phy)) {
235 dev_err(dev, "failed to get pcie-phy%d\n", slot);
236 err = PTR_ERR(port->phy);
240 port->gpio_rst = devm_gpiod_get_index_optional(dev, "reset", slot,
242 if (IS_ERR(port->gpio_rst)) {
243 dev_err(dev, "failed to get GPIO for PCIe%d\n", slot);
244 err = PTR_ERR(port->gpio_rst);
251 INIT_LIST_HEAD(&port->list);
252 list_add_tail(&port->list, &pcie->ports);
257 reset_control_put(port->pcie_rst);
261 static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
263 struct device *dev = pcie->dev;
264 struct platform_device *pdev = to_platform_device(dev);
265 struct device_node *node = dev->of_node, *child;
268 pcie->base = devm_platform_ioremap_resource(pdev, 0);
269 if (IS_ERR(pcie->base))
270 return PTR_ERR(pcie->base);
272 for_each_available_child_of_node(node, child) {
275 err = of_pci_get_devfn(child);
278 dev_err(dev, "failed to parse devfn: %d\n", err);
282 slot = PCI_SLOT(err);
284 err = mt7621_pcie_parse_port(pcie, child, slot);
294 static int mt7621_pcie_init_port(struct mt7621_pcie_port *port)
296 struct mt7621_pcie *pcie = port->pcie;
297 struct device *dev = pcie->dev;
298 u32 slot = port->slot;
301 err = phy_init(port->phy);
303 dev_err(dev, "failed to initialize port%d phy\n", slot);
307 err = phy_power_on(port->phy);
309 dev_err(dev, "failed to power on port%d phy\n", slot);
314 port->enabled = true;
319 static void mt7621_pcie_reset_assert(struct mt7621_pcie *pcie)
321 struct mt7621_pcie_port *port;
323 list_for_each_entry(port, &pcie->ports, list) {
324 /* PCIe RC reset assert */
325 mt7621_control_assert(port);
327 /* PCIe EP reset assert */
328 mt7621_rst_gpio_pcie_assert(port);
331 msleep(PERST_DELAY_MS);
334 static void mt7621_pcie_reset_rc_deassert(struct mt7621_pcie *pcie)
336 struct mt7621_pcie_port *port;
338 list_for_each_entry(port, &pcie->ports, list)
339 mt7621_control_deassert(port);
342 static void mt7621_pcie_reset_ep_deassert(struct mt7621_pcie *pcie)
344 struct mt7621_pcie_port *port;
346 list_for_each_entry(port, &pcie->ports, list)
347 mt7621_rst_gpio_pcie_deassert(port);
349 msleep(PERST_DELAY_MS);
352 static int mt7621_pcie_init_ports(struct mt7621_pcie *pcie)
354 struct device *dev = pcie->dev;
355 struct mt7621_pcie_port *port, *tmp;
359 mt7621_pcie_reset_assert(pcie);
360 mt7621_pcie_reset_rc_deassert(pcie);
362 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
363 u32 slot = port->slot;
366 port->enabled = true;
370 err = mt7621_pcie_init_port(port);
372 dev_err(dev, "initializing port %d failed\n", slot);
373 list_del(&port->list);
377 mt7621_pcie_reset_ep_deassert(pcie);
380 list_for_each_entry(port, &pcie->ports, list) {
381 u32 slot = port->slot;
383 if (!mt7621_pcie_port_is_linkup(port)) {
384 dev_err(dev, "pcie%d no card, disable it (RST & CLK)\n",
386 mt7621_control_assert(port);
387 port->enabled = false;
395 if (slot == 1 && tmp && !tmp->enabled)
396 phy_power_off(tmp->phy);
400 return (num_disabled != PCIE_PORT_CNT) ? 0 : -ENODEV;
403 static void mt7621_pcie_enable_port(struct mt7621_pcie_port *port)
405 struct mt7621_pcie *pcie = port->pcie;
406 u32 slot = port->slot;
409 /* enable pcie interrupt */
410 val = pcie_read(pcie, RALINK_PCI_PCIMSK_ADDR);
411 val |= PCIE_PORT_INT_EN(slot);
412 pcie_write(pcie, val, RALINK_PCI_PCIMSK_ADDR);
414 /* map 2G DDR region */
415 pcie_port_write(port, PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
418 /* configure class code and revision ID */
419 pcie_port_write(port, PCIE_CLASS_CODE | PCIE_REVISION_ID,
422 /* configure RC FTS number to 250 when it leaves L0s */
423 val = read_config(pcie, slot, PCIE_FTS_NUM);
424 val &= ~PCIE_FTS_NUM_MASK;
425 val |= PCIE_FTS_NUM_L0(0x50);
426 write_config(pcie, slot, PCIE_FTS_NUM, val);
429 static int mt7621_pcie_enable_ports(struct pci_host_bridge *host)
431 struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
432 struct device *dev = pcie->dev;
433 struct mt7621_pcie_port *port;
434 struct resource_entry *entry;
437 entry = resource_list_first_type(&host->windows, IORESOURCE_IO);
439 dev_err(dev, "cannot get io resource\n");
443 /* Setup MEMWIN and IOWIN */
444 pcie_write(pcie, 0xffffffff, RALINK_PCI_MEMBASE);
445 pcie_write(pcie, entry->res->start - entry->offset, RALINK_PCI_IOBASE);
447 list_for_each_entry(port, &pcie->ports, list) {
449 err = clk_prepare_enable(port->clk);
451 dev_err(dev, "enabling clk pcie%d\n",
456 mt7621_pcie_enable_port(port);
457 dev_info(dev, "PCIE%d enabled\n", port->slot);
464 static int mt7621_pcie_register_host(struct pci_host_bridge *host)
466 struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
468 host->ops = &mt7621_pcie_ops;
469 host->sysdata = pcie;
470 return pci_host_probe(host);
473 static const struct soc_device_attribute mt7621_pcie_quirks_match[] = {
474 { .soc_id = "mt7621", .revision = "E2" }
477 static int mt7621_pcie_probe(struct platform_device *pdev)
479 struct device *dev = &pdev->dev;
480 const struct soc_device_attribute *attr;
481 struct mt7621_pcie_port *port;
482 struct mt7621_pcie *pcie;
483 struct pci_host_bridge *bridge;
489 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
493 pcie = pci_host_bridge_priv(bridge);
495 platform_set_drvdata(pdev, pcie);
496 INIT_LIST_HEAD(&pcie->ports);
498 attr = soc_device_match(mt7621_pcie_quirks_match);
500 pcie->resets_inverted = true;
502 err = mt7621_pcie_parse_dt(pcie);
504 dev_err(dev, "parsing DT failed\n");
508 err = mt7621_pcie_init_ports(pcie);
510 dev_err(dev, "nothing connected in virtual bridges\n");
514 err = mt7621_pcie_enable_ports(bridge);
516 dev_err(dev, "error enabling pcie ports\n");
520 return mt7621_pcie_register_host(bridge);
523 list_for_each_entry(port, &pcie->ports, list)
524 reset_control_put(port->pcie_rst);
529 static int mt7621_pcie_remove(struct platform_device *pdev)
531 struct mt7621_pcie *pcie = platform_get_drvdata(pdev);
532 struct mt7621_pcie_port *port;
534 list_for_each_entry(port, &pcie->ports, list)
535 reset_control_put(port->pcie_rst);
540 static const struct of_device_id mt7621_pcie_ids[] = {
541 { .compatible = "mediatek,mt7621-pci" },
544 MODULE_DEVICE_TABLE(of, mt7621_pcie_ids);
546 static struct platform_driver mt7621_pcie_driver = {
547 .probe = mt7621_pcie_probe,
548 .remove = mt7621_pcie_remove,
550 .name = "mt7621-pci",
551 .of_match_table = mt7621_pcie_ids,
554 builtin_platform_driver(mt7621_pcie_driver);
556 MODULE_LICENSE("GPL v2");