1 From: Sergio Paracuellos <sergio.paracuellos@gmail.com>
2 Date: Wed, 22 Sep 2021 07:00:34 +0200
3 Subject: [PATCH] PCI: mt7621: Add MediaTek MT7621 PCIe host controller driver
5 Add driver for the PCIe controller of the MT7621 SoC.
7 [bhelgaas: rename from pci-mt7621.c to pcie-mt7621.c; also rename Kconfig
8 symbol from PCI_MT7621 to PCIE_MT7621]
9 Link: https://lore.kernel.org/r/20210922050035.18162-3-sergio.paracuellos@gmail.com
10 Signed-off-by: Sergio Paracuellos <sergio.paracuellos@gmail.com>
11 Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
12 Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
13 Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
15 rename drivers/{staging/mt7621-pci/pci-mt7621.c => pci/controller/pcie-mt7621.c} (95%)
16 delete mode 100644 drivers/staging/mt7621-pci/Kconfig
17 delete mode 100644 drivers/staging/mt7621-pci/Makefile
18 delete mode 100644 drivers/staging/mt7621-pci/TODO
19 delete mode 100644 drivers/staging/mt7621-pci/mediatek,mt7621-pci.txt
21 --- a/arch/mips/ralink/Kconfig
22 +++ b/arch/mips/ralink/Kconfig
23 @@ -51,7 +51,8 @@ choice
24 select SYS_SUPPORTS_HIGHMEM
26 select CLKSRC_MIPS_GIC
27 - select HAVE_PCI if PCI_MT7621
29 + select PCI_DRIVERS_GENERIC
33 --- a/drivers/pci/controller/Kconfig
34 +++ b/drivers/pci/controller/Kconfig
35 @@ -312,6 +312,14 @@ config PCIE_HISI_ERR
36 Say Y here if you want error handling support
37 for the PCIe controller's errors on HiSilicon HIP SoCs
40 + tristate "MediaTek MT7621 PCIe Controller"
41 + depends on (RALINK && SOC_MT7621) || (MIPS && COMPILE_TEST)
42 + select PHY_MT7621_PCI
45 + This selects a driver for the MediaTek MT7621 PCIe Controller.
47 source "drivers/pci/controller/dwc/Kconfig"
48 source "drivers/pci/controller/mobiveil/Kconfig"
49 source "drivers/pci/controller/cadence/Kconfig"
50 --- a/drivers/pci/controller/Makefile
51 +++ b/drivers/pci/controller/Makefile
52 @@ -37,6 +37,8 @@ obj-$(CONFIG_VMD) += vmd.o
53 obj-$(CONFIG_PCIE_BRCMSTB) += pcie-brcmstb.o
54 obj-$(CONFIG_PCI_LOONGSON) += pci-loongson.o
55 obj-$(CONFIG_PCIE_HISI_ERR) += pcie-hisi-error.o
56 +obj-$(CONFIG_PCIE_MT7621) += pcie-mt7621.o
58 # pcie-hisi.o quirks are needed even without CONFIG_PCIE_DW
61 --- a/drivers/staging/Kconfig
62 +++ b/drivers/staging/Kconfig
63 @@ -86,8 +86,6 @@ source "drivers/staging/vc04_services/Kc
65 source "drivers/staging/pi433/Kconfig"
67 -source "drivers/staging/mt7621-pci/Kconfig"
69 source "drivers/staging/mt7621-dma/Kconfig"
71 source "drivers/staging/ralink-gdma/Kconfig"
72 --- a/drivers/staging/Makefile
73 +++ b/drivers/staging/Makefile
74 @@ -33,7 +33,6 @@ obj-$(CONFIG_KS7010) += ks7010/
75 obj-$(CONFIG_GREYBUS) += greybus/
76 obj-$(CONFIG_BCM2835_VCHIQ) += vc04_services/
77 obj-$(CONFIG_PI433) += pi433/
78 -obj-$(CONFIG_PCI_MT7621) += mt7621-pci/
79 obj-$(CONFIG_SOC_MT7621) += mt7621-dma/
80 obj-$(CONFIG_DMA_RALINK) += ralink-gdma/
81 obj-$(CONFIG_SOC_MT7621) += mt7621-dts/
82 --- a/drivers/staging/mt7621-pci/Kconfig
85 -# SPDX-License-Identifier: GPL-2.0
87 - tristate "MediaTek MT7621 PCI Controller"
89 - select PCI_DRIVERS_GENERIC
91 - This selects a driver for the MediaTek MT7621 PCI Controller.
93 --- a/drivers/staging/mt7621-pci/Makefile
96 -# SPDX-License-Identifier: GPL-2.0
97 -obj-$(CONFIG_PCI_MT7621) += pci-mt7621.o
98 --- a/drivers/staging/mt7621-pci/TODO
102 -- general code review and cleanup
104 -Cc: NeilBrown <neil@brown.name>
105 --- a/drivers/staging/mt7621-pci/mediatek,mt7621-pci.txt
108 -MediaTek MT7621 PCIe controller
110 -Required properties:
111 -- compatible: "mediatek,mt7621-pci"
112 -- device_type: Must be "pci"
113 -- reg: Base addresses and lengths of the PCIe subsys and root ports.
114 -- bus-range: Range of bus numbers associated with this controller.
115 -- #address-cells: Address representation for root ports (must be 3)
116 -- pinctrl-names : The pin control state names.
117 -- pinctrl-0: The "default" pinctrl state.
118 -- #size-cells: Size representation for root ports (must be 2)
119 -- ranges: Ranges for the PCI memory and I/O regions.
120 -- #interrupt-cells: Must be 1
121 -- interrupt-map-mask and interrupt-map: Standard PCI IRQ mapping properties.
122 - Please refer to the standard PCI bus binding document for a more detailed
124 -- status: either "disabled" or "okay".
125 -- resets: Must contain an entry for each entry in reset-names.
126 - See ../reset/reset.txt for details.
127 -- reset-names: Must be "pcie0", "pcie1", "pcieN"... based on the number of
129 -- clocks: Must contain an entry for each entry in clock-names.
130 - See ../clocks/clock-bindings.txt for details.
131 -- clock-names: Must be "pcie0", "pcie1", "pcieN"... based on the number of
133 -- reset-gpios: GPIO specs for the reset pins.
135 -In addition, the device tree node must have sub-nodes describing each PCIe port
136 -interface, having the following mandatory properties:
138 -Required properties:
139 -- reg: Only the first four bytes are used to refer to the correct bus number
141 -- #address-cells: Must be 3
142 -- #size-cells: Must be 2
143 -- ranges: Sub-ranges distributed from the PCIe controller node. An empty
144 - property is sufficient.
145 -- bus-range: Range of bus numbers associated with this port.
149 - pcie: pcie@1e140000 {
150 - compatible = "mediatek,mt7621-pci";
151 - reg = <0x1e140000 0x100 /* host-pci bridge registers */
152 - 0x1e142000 0x100 /* pcie port 0 RC control registers */
153 - 0x1e143000 0x100 /* pcie port 1 RC control registers */
154 - 0x1e144000 0x100>; /* pcie port 2 RC control registers */
156 - #address-cells = <3>;
159 - pinctrl-names = "default";
160 - pinctrl-0 = <&pcie_pins>;
162 - device_type = "pci";
164 - bus-range = <0 255>;
166 - 0x02000000 0 0x00000000 0x60000000 0 0x10000000 /* pci memory */
167 - 0x01000000 0 0x00000000 0x1e160000 0 0x00010000 /* io space */
170 - #interrupt-cells = <1>;
171 - interrupt-map-mask = <0xF0000 0 0 1>;
172 - interrupt-map = <0x10000 0 0 1 &gic GIC_SHARED 4 IRQ_TYPE_LEVEL_HIGH>,
173 - <0x20000 0 0 1 &gic GIC_SHARED 24 IRQ_TYPE_LEVEL_HIGH>,
174 - <0x30000 0 0 1 &gic GIC_SHARED 25 IRQ_TYPE_LEVEL_HIGH>;
176 - status = "disabled";
178 - resets = <&rstctrl 24 &rstctrl 25 &rstctrl 26>;
179 - reset-names = "pcie0", "pcie1", "pcie2";
180 - clocks = <&clkctrl 24 &clkctrl 25 &clkctrl 26>;
181 - clock-names = "pcie0", "pcie1", "pcie2";
183 - reset-gpios = <&gpio 19 GPIO_ACTIVE_LOW>,
184 - <&gpio 8 GPIO_ACTIVE_LOW>,
185 - <&gpio 7 GPIO_ACTIVE_LOW>;
188 - reg = <0x0000 0 0 0 0>;
189 - #address-cells = <3>;
192 - bus-range = <0x00 0xff>;
196 - reg = <0x0800 0 0 0 0>;
197 - #address-cells = <3>;
200 - bus-range = <0x00 0xff>;
204 - reg = <0x1000 0 0 0 0>;
205 - #address-cells = <3>;
208 - bus-range = <0x00 0xff>;
212 --- a/drivers/staging/mt7621-pci/pci-mt7621.c
215 -// SPDX-License-Identifier: GPL-2.0+
217 - * BRIEF MODULE DESCRIPTION
218 - * PCI init for Ralink RT2880 solution
220 - * Copyright 2007 Ralink Inc. (bruce_chang@ralinktech.com.tw)
222 - * May 2007 Bruce Chang
225 - * May 2009 Bruce Chang
226 - * support RT2880/RT3883 PCIe
228 - * May 2011 Bruce Chang
229 - * support RT6855/MT7620 PCIe
232 -#include <linux/bitops.h>
233 -#include <linux/clk.h>
234 -#include <linux/delay.h>
235 -#include <linux/gpio/consumer.h>
236 -#include <linux/module.h>
237 -#include <linux/of.h>
238 -#include <linux/of_address.h>
239 -#include <linux/of_pci.h>
240 -#include <linux/of_platform.h>
241 -#include <linux/pci.h>
242 -#include <linux/phy/phy.h>
243 -#include <linux/platform_device.h>
244 -#include <linux/reset.h>
245 -#include <linux/sys_soc.h>
247 -/* MediaTek specific configuration registers */
248 -#define PCIE_FTS_NUM 0x70c
249 -#define PCIE_FTS_NUM_MASK GENMASK(15, 8)
250 -#define PCIE_FTS_NUM_L0(x) (((x) & 0xff) << 8)
252 -/* Host-PCI bridge registers */
253 -#define RALINK_PCI_PCICFG_ADDR 0x0000
254 -#define RALINK_PCI_PCIMSK_ADDR 0x000C
255 -#define RALINK_PCI_CONFIG_ADDR 0x0020
256 -#define RALINK_PCI_CONFIG_DATA 0x0024
257 -#define RALINK_PCI_MEMBASE 0x0028
258 -#define RALINK_PCI_IOBASE 0x002C
260 -/* PCIe RC control registers */
261 -#define RALINK_PCI_ID 0x0030
262 -#define RALINK_PCI_CLASS 0x0034
263 -#define RALINK_PCI_SUBID 0x0038
264 -#define RALINK_PCI_STATUS 0x0050
266 -/* Some definition values */
267 -#define PCIE_REVISION_ID BIT(0)
268 -#define PCIE_CLASS_CODE (0x60400 << 8)
269 -#define PCIE_BAR_MAP_MAX GENMASK(30, 16)
270 -#define PCIE_BAR_ENABLE BIT(0)
271 -#define PCIE_PORT_INT_EN(x) BIT(20 + (x))
272 -#define PCIE_PORT_LINKUP BIT(0)
273 -#define PCIE_PORT_CNT 3
275 -#define PERST_DELAY_MS 100
278 - * struct mt7621_pcie_port - PCIe port information
279 - * @base: I/O mapped register base
281 - * @pcie: pointer to PCIe host info
282 - * @clk: pointer to the port clock gate
283 - * @phy: pointer to PHY control block
284 - * @pcie_rst: pointer to port reset control
285 - * @gpio_rst: gpio reset
287 - * @enabled: indicates if port is enabled
289 -struct mt7621_pcie_port {
290 - void __iomem *base;
291 - struct list_head list;
292 - struct mt7621_pcie *pcie;
295 - struct reset_control *pcie_rst;
296 - struct gpio_desc *gpio_rst;
302 - * struct mt7621_pcie - PCIe host information
303 - * @base: IO Mapped Register Base
304 - * @dev: Pointer to PCIe device
305 - * @ports: pointer to PCIe port information
306 - * @resets_inverted: depends on chip revision
307 - * reset lines are inverted.
309 -struct mt7621_pcie {
310 - void __iomem *base;
311 - struct device *dev;
312 - struct list_head ports;
313 - bool resets_inverted;
316 -static inline u32 pcie_read(struct mt7621_pcie *pcie, u32 reg)
318 - return readl_relaxed(pcie->base + reg);
321 -static inline void pcie_write(struct mt7621_pcie *pcie, u32 val, u32 reg)
323 - writel_relaxed(val, pcie->base + reg);
326 -static inline void pcie_rmw(struct mt7621_pcie *pcie, u32 reg, u32 clr, u32 set)
328 - u32 val = readl_relaxed(pcie->base + reg);
332 - writel_relaxed(val, pcie->base + reg);
335 -static inline u32 pcie_port_read(struct mt7621_pcie_port *port, u32 reg)
337 - return readl_relaxed(port->base + reg);
340 -static inline void pcie_port_write(struct mt7621_pcie_port *port,
343 - writel_relaxed(val, port->base + reg);
346 -static inline u32 mt7621_pci_get_cfgaddr(unsigned int bus, unsigned int slot,
347 - unsigned int func, unsigned int where)
349 - return (((where & 0xF00) >> 8) << 24) | (bus << 16) | (slot << 11) |
350 - (func << 8) | (where & 0xfc) | 0x80000000;
353 -static void __iomem *mt7621_pcie_map_bus(struct pci_bus *bus,
354 - unsigned int devfn, int where)
356 - struct mt7621_pcie *pcie = bus->sysdata;
357 - u32 address = mt7621_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
358 - PCI_FUNC(devfn), where);
360 - writel_relaxed(address, pcie->base + RALINK_PCI_CONFIG_ADDR);
362 - return pcie->base + RALINK_PCI_CONFIG_DATA + (where & 3);
365 -struct pci_ops mt7621_pci_ops = {
366 - .map_bus = mt7621_pcie_map_bus,
367 - .read = pci_generic_config_read,
368 - .write = pci_generic_config_write,
371 -static u32 read_config(struct mt7621_pcie *pcie, unsigned int dev, u32 reg)
373 - u32 address = mt7621_pci_get_cfgaddr(0, dev, 0, reg);
375 - pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
376 - return pcie_read(pcie, RALINK_PCI_CONFIG_DATA);
379 -static void write_config(struct mt7621_pcie *pcie, unsigned int dev,
382 - u32 address = mt7621_pci_get_cfgaddr(0, dev, 0, reg);
384 - pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
385 - pcie_write(pcie, val, RALINK_PCI_CONFIG_DATA);
388 -static inline void mt7621_rst_gpio_pcie_assert(struct mt7621_pcie_port *port)
390 - if (port->gpio_rst)
391 - gpiod_set_value(port->gpio_rst, 1);
394 -static inline void mt7621_rst_gpio_pcie_deassert(struct mt7621_pcie_port *port)
396 - if (port->gpio_rst)
397 - gpiod_set_value(port->gpio_rst, 0);
400 -static inline bool mt7621_pcie_port_is_linkup(struct mt7621_pcie_port *port)
402 - return (pcie_port_read(port, RALINK_PCI_STATUS) & PCIE_PORT_LINKUP) != 0;
405 -static inline void mt7621_control_assert(struct mt7621_pcie_port *port)
407 - struct mt7621_pcie *pcie = port->pcie;
409 - if (pcie->resets_inverted)
410 - reset_control_assert(port->pcie_rst);
412 - reset_control_deassert(port->pcie_rst);
415 -static inline void mt7621_control_deassert(struct mt7621_pcie_port *port)
417 - struct mt7621_pcie *pcie = port->pcie;
419 - if (pcie->resets_inverted)
420 - reset_control_deassert(port->pcie_rst);
422 - reset_control_assert(port->pcie_rst);
425 -static int setup_cm_memory_region(struct pci_host_bridge *host)
427 - struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
428 - struct device *dev = pcie->dev;
429 - struct resource_entry *entry;
430 - resource_size_t mask;
432 - entry = resource_list_first_type(&host->windows, IORESOURCE_MEM);
434 - dev_err(dev, "Cannot get memory resource\n");
438 - if (mips_cps_numiocu(0)) {
440 - * FIXME: hardware doesn't accept mask values with 1s after
441 - * 0s (e.g. 0xffef), so it would be great to warn if that's
444 - mask = ~(entry->res->end - entry->res->start);
446 - write_gcr_reg1_base(entry->res->start);
447 - write_gcr_reg1_mask(mask | CM_GCR_REGn_MASK_CMTGT_IOCU0);
448 - dev_info(dev, "PCI coherence region base: 0x%08llx, mask/settings: 0x%08llx\n",
449 - (unsigned long long)read_gcr_reg1_base(),
450 - (unsigned long long)read_gcr_reg1_mask());
456 -static int mt7621_pcie_parse_port(struct mt7621_pcie *pcie,
457 - struct device_node *node,
460 - struct mt7621_pcie_port *port;
461 - struct device *dev = pcie->dev;
462 - struct platform_device *pdev = to_platform_device(dev);
466 - port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
470 - port->base = devm_platform_ioremap_resource(pdev, slot + 1);
471 - if (IS_ERR(port->base))
472 - return PTR_ERR(port->base);
474 - port->clk = devm_get_clk_from_child(dev, node, NULL);
475 - if (IS_ERR(port->clk)) {
476 - dev_err(dev, "failed to get pcie%d clock\n", slot);
477 - return PTR_ERR(port->clk);
480 - port->pcie_rst = of_reset_control_get_exclusive(node, NULL);
481 - if (PTR_ERR(port->pcie_rst) == -EPROBE_DEFER) {
482 - dev_err(dev, "failed to get pcie%d reset control\n", slot);
483 - return PTR_ERR(port->pcie_rst);
486 - snprintf(name, sizeof(name), "pcie-phy%d", slot);
487 - port->phy = devm_of_phy_get(dev, node, name);
488 - if (IS_ERR(port->phy)) {
489 - dev_err(dev, "failed to get pcie-phy%d\n", slot);
490 - err = PTR_ERR(port->phy);
494 - port->gpio_rst = devm_gpiod_get_index_optional(dev, "reset", slot,
496 - if (IS_ERR(port->gpio_rst)) {
497 - dev_err(dev, "Failed to get GPIO for PCIe%d\n", slot);
498 - err = PTR_ERR(port->gpio_rst);
505 - INIT_LIST_HEAD(&port->list);
506 - list_add_tail(&port->list, &pcie->ports);
511 - reset_control_put(port->pcie_rst);
515 -static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
517 - struct device *dev = pcie->dev;
518 - struct platform_device *pdev = to_platform_device(dev);
519 - struct device_node *node = dev->of_node, *child;
522 - pcie->base = devm_platform_ioremap_resource(pdev, 0);
523 - if (IS_ERR(pcie->base))
524 - return PTR_ERR(pcie->base);
526 - for_each_available_child_of_node(node, child) {
529 - err = of_pci_get_devfn(child);
531 - of_node_put(child);
532 - dev_err(dev, "failed to parse devfn: %d\n", err);
536 - slot = PCI_SLOT(err);
538 - err = mt7621_pcie_parse_port(pcie, child, slot);
540 - of_node_put(child);
548 -static int mt7621_pcie_init_port(struct mt7621_pcie_port *port)
550 - struct mt7621_pcie *pcie = port->pcie;
551 - struct device *dev = pcie->dev;
552 - u32 slot = port->slot;
555 - err = phy_init(port->phy);
557 - dev_err(dev, "failed to initialize port%d phy\n", slot);
561 - err = phy_power_on(port->phy);
563 - dev_err(dev, "failed to power on port%d phy\n", slot);
564 - phy_exit(port->phy);
568 - port->enabled = true;
573 -static void mt7621_pcie_reset_assert(struct mt7621_pcie *pcie)
575 - struct mt7621_pcie_port *port;
577 - list_for_each_entry(port, &pcie->ports, list) {
578 - /* PCIe RC reset assert */
579 - mt7621_control_assert(port);
581 - /* PCIe EP reset assert */
582 - mt7621_rst_gpio_pcie_assert(port);
585 - msleep(PERST_DELAY_MS);
588 -static void mt7621_pcie_reset_rc_deassert(struct mt7621_pcie *pcie)
590 - struct mt7621_pcie_port *port;
592 - list_for_each_entry(port, &pcie->ports, list)
593 - mt7621_control_deassert(port);
596 -static void mt7621_pcie_reset_ep_deassert(struct mt7621_pcie *pcie)
598 - struct mt7621_pcie_port *port;
600 - list_for_each_entry(port, &pcie->ports, list)
601 - mt7621_rst_gpio_pcie_deassert(port);
603 - msleep(PERST_DELAY_MS);
606 -static int mt7621_pcie_init_ports(struct mt7621_pcie *pcie)
608 - struct device *dev = pcie->dev;
609 - struct mt7621_pcie_port *port, *tmp;
610 - u8 num_disabled = 0;
613 - mt7621_pcie_reset_assert(pcie);
614 - mt7621_pcie_reset_rc_deassert(pcie);
616 - list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
617 - u32 slot = port->slot;
620 - port->enabled = true;
624 - err = mt7621_pcie_init_port(port);
626 - dev_err(dev, "Initiating port %d failed\n", slot);
627 - list_del(&port->list);
631 - mt7621_pcie_reset_ep_deassert(pcie);
634 - list_for_each_entry(port, &pcie->ports, list) {
635 - u32 slot = port->slot;
637 - if (!mt7621_pcie_port_is_linkup(port)) {
638 - dev_err(dev, "pcie%d no card, disable it (RST & CLK)\n",
640 - mt7621_control_assert(port);
641 - port->enabled = false;
649 - if (slot == 1 && tmp && !tmp->enabled)
650 - phy_power_off(tmp->phy);
654 - return (num_disabled != PCIE_PORT_CNT) ? 0 : -ENODEV;
657 -static void mt7621_pcie_enable_port(struct mt7621_pcie_port *port)
659 - struct mt7621_pcie *pcie = port->pcie;
660 - u32 slot = port->slot;
663 - /* enable pcie interrupt */
664 - val = pcie_read(pcie, RALINK_PCI_PCIMSK_ADDR);
665 - val |= PCIE_PORT_INT_EN(slot);
666 - pcie_write(pcie, val, RALINK_PCI_PCIMSK_ADDR);
668 - /* map 2G DDR region */
669 - pcie_port_write(port, PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
670 - PCI_BASE_ADDRESS_0);
672 - /* configure class code and revision ID */
673 - pcie_port_write(port, PCIE_CLASS_CODE | PCIE_REVISION_ID,
676 - /* configure RC FTS number to 250 when it leaves L0s */
677 - val = read_config(pcie, slot, PCIE_FTS_NUM);
678 - val &= ~PCIE_FTS_NUM_MASK;
679 - val |= PCIE_FTS_NUM_L0(0x50);
680 - write_config(pcie, slot, PCIE_FTS_NUM, val);
683 -static int mt7621_pcie_enable_ports(struct pci_host_bridge *host)
685 - struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
686 - struct device *dev = pcie->dev;
687 - struct mt7621_pcie_port *port;
688 - struct resource_entry *entry;
691 - entry = resource_list_first_type(&host->windows, IORESOURCE_IO);
693 - dev_err(dev, "Cannot get io resource\n");
697 - /* Setup MEMWIN and IOWIN */
698 - pcie_write(pcie, 0xffffffff, RALINK_PCI_MEMBASE);
699 - pcie_write(pcie, entry->res->start, RALINK_PCI_IOBASE);
701 - list_for_each_entry(port, &pcie->ports, list) {
702 - if (port->enabled) {
703 - err = clk_prepare_enable(port->clk);
705 - dev_err(dev, "enabling clk pcie%d\n",
710 - mt7621_pcie_enable_port(port);
711 - dev_info(dev, "PCIE%d enabled\n", port->slot);
718 -static int mt7621_pcie_register_host(struct pci_host_bridge *host)
720 - struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
722 - host->ops = &mt7621_pci_ops;
723 - host->sysdata = pcie;
724 - return pci_host_probe(host);
727 -static const struct soc_device_attribute mt7621_pci_quirks_match[] = {
728 - { .soc_id = "mt7621", .revision = "E2" }
731 -static int mt7621_pci_probe(struct platform_device *pdev)
733 - struct device *dev = &pdev->dev;
734 - const struct soc_device_attribute *attr;
735 - struct mt7621_pcie_port *port;
736 - struct mt7621_pcie *pcie;
737 - struct pci_host_bridge *bridge;
743 - bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
747 - pcie = pci_host_bridge_priv(bridge);
749 - platform_set_drvdata(pdev, pcie);
750 - INIT_LIST_HEAD(&pcie->ports);
752 - attr = soc_device_match(mt7621_pci_quirks_match);
754 - pcie->resets_inverted = true;
756 - err = mt7621_pcie_parse_dt(pcie);
758 - dev_err(dev, "Parsing DT failed\n");
762 - err = mt7621_pcie_init_ports(pcie);
764 - dev_err(dev, "Nothing connected in virtual bridges\n");
768 - err = mt7621_pcie_enable_ports(bridge);
770 - dev_err(dev, "Error enabling pcie ports\n");
771 - goto remove_resets;
774 - err = setup_cm_memory_region(bridge);
776 - dev_err(dev, "Error setting up iocu mem regions\n");
777 - goto remove_resets;
780 - return mt7621_pcie_register_host(bridge);
783 - list_for_each_entry(port, &pcie->ports, list)
784 - reset_control_put(port->pcie_rst);
789 -static int mt7621_pci_remove(struct platform_device *pdev)
791 - struct mt7621_pcie *pcie = platform_get_drvdata(pdev);
792 - struct mt7621_pcie_port *port;
794 - list_for_each_entry(port, &pcie->ports, list)
795 - reset_control_put(port->pcie_rst);
800 -static const struct of_device_id mt7621_pci_ids[] = {
801 - { .compatible = "mediatek,mt7621-pci" },
804 -MODULE_DEVICE_TABLE(of, mt7621_pci_ids);
806 -static struct platform_driver mt7621_pci_driver = {
807 - .probe = mt7621_pci_probe,
808 - .remove = mt7621_pci_remove,
810 - .name = "mt7621-pci",
811 - .of_match_table = of_match_ptr(mt7621_pci_ids),
814 -builtin_platform_driver(mt7621_pci_driver);
816 +++ b/drivers/pci/controller/pcie-mt7621.c
818 +// SPDX-License-Identifier: GPL-2.0+
820 + * BRIEF MODULE DESCRIPTION
821 + * PCI init for Ralink RT2880 solution
823 + * Copyright 2007 Ralink Inc. (bruce_chang@ralinktech.com.tw)
825 + * May 2007 Bruce Chang
828 + * May 2009 Bruce Chang
829 + * support RT2880/RT3883 PCIe
831 + * May 2011 Bruce Chang
832 + * support RT6855/MT7620 PCIe
835 +#include <linux/bitops.h>
836 +#include <linux/clk.h>
837 +#include <linux/delay.h>
838 +#include <linux/gpio/consumer.h>
839 +#include <linux/module.h>
840 +#include <linux/of.h>
841 +#include <linux/of_address.h>
842 +#include <linux/of_pci.h>
843 +#include <linux/of_platform.h>
844 +#include <linux/pci.h>
845 +#include <linux/phy/phy.h>
846 +#include <linux/platform_device.h>
847 +#include <linux/reset.h>
848 +#include <linux/sys_soc.h>
850 +/* MediaTek-specific configuration registers */
851 +#define PCIE_FTS_NUM 0x70c
852 +#define PCIE_FTS_NUM_MASK GENMASK(15, 8)
853 +#define PCIE_FTS_NUM_L0(x) (((x) & 0xff) << 8)
855 +/* Host-PCI bridge registers */
856 +#define RALINK_PCI_PCICFG_ADDR 0x0000
857 +#define RALINK_PCI_PCIMSK_ADDR 0x000c
858 +#define RALINK_PCI_CONFIG_ADDR 0x0020
859 +#define RALINK_PCI_CONFIG_DATA 0x0024
860 +#define RALINK_PCI_MEMBASE 0x0028
861 +#define RALINK_PCI_IOBASE 0x002c
863 +/* PCIe RC control registers */
864 +#define RALINK_PCI_ID 0x0030
865 +#define RALINK_PCI_CLASS 0x0034
866 +#define RALINK_PCI_SUBID 0x0038
867 +#define RALINK_PCI_STATUS 0x0050
869 +/* Some definition values */
870 +#define PCIE_REVISION_ID BIT(0)
871 +#define PCIE_CLASS_CODE (0x60400 << 8)
872 +#define PCIE_BAR_MAP_MAX GENMASK(30, 16)
873 +#define PCIE_BAR_ENABLE BIT(0)
874 +#define PCIE_PORT_INT_EN(x) BIT(20 + (x))
875 +#define PCIE_PORT_LINKUP BIT(0)
876 +#define PCIE_PORT_CNT 3
878 +#define PERST_DELAY_MS 100
881 + * struct mt7621_pcie_port - PCIe port information
882 + * @base: I/O mapped register base
884 + * @pcie: pointer to PCIe host info
885 + * @clk: pointer to the port clock gate
886 + * @phy: pointer to PHY control block
887 + * @pcie_rst: pointer to port reset control
888 + * @gpio_rst: gpio reset
890 + * @enabled: indicates if port is enabled
892 +struct mt7621_pcie_port {
893 + void __iomem *base;
894 + struct list_head list;
895 + struct mt7621_pcie *pcie;
898 + struct reset_control *pcie_rst;
899 + struct gpio_desc *gpio_rst;
905 + * struct mt7621_pcie - PCIe host information
906 + * @base: IO Mapped Register Base
907 + * @dev: Pointer to PCIe device
908 + * @ports: pointer to PCIe port information
909 + * @resets_inverted: depends on chip revision
910 + * reset lines are inverted.
912 +struct mt7621_pcie {
913 + void __iomem *base;
914 + struct device *dev;
915 + struct list_head ports;
916 + bool resets_inverted;
919 +static inline u32 pcie_read(struct mt7621_pcie *pcie, u32 reg)
921 + return readl_relaxed(pcie->base + reg);
924 +static inline void pcie_write(struct mt7621_pcie *pcie, u32 val, u32 reg)
926 + writel_relaxed(val, pcie->base + reg);
929 +static inline void pcie_rmw(struct mt7621_pcie *pcie, u32 reg, u32 clr, u32 set)
931 + u32 val = readl_relaxed(pcie->base + reg);
935 + writel_relaxed(val, pcie->base + reg);
938 +static inline u32 pcie_port_read(struct mt7621_pcie_port *port, u32 reg)
940 + return readl_relaxed(port->base + reg);
943 +static inline void pcie_port_write(struct mt7621_pcie_port *port,
946 + writel_relaxed(val, port->base + reg);
949 +static inline u32 mt7621_pci_get_cfgaddr(unsigned int bus, unsigned int slot,
950 + unsigned int func, unsigned int where)
952 + return (((where & 0xf00) >> 8) << 24) | (bus << 16) | (slot << 11) |
953 + (func << 8) | (where & 0xfc) | 0x80000000;
956 +static void __iomem *mt7621_pcie_map_bus(struct pci_bus *bus,
957 + unsigned int devfn, int where)
959 + struct mt7621_pcie *pcie = bus->sysdata;
960 + u32 address = mt7621_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
961 + PCI_FUNC(devfn), where);
963 + writel_relaxed(address, pcie->base + RALINK_PCI_CONFIG_ADDR);
965 + return pcie->base + RALINK_PCI_CONFIG_DATA + (where & 3);
968 +struct pci_ops mt7621_pci_ops = {
969 + .map_bus = mt7621_pcie_map_bus,
970 + .read = pci_generic_config_read,
971 + .write = pci_generic_config_write,
974 +static u32 read_config(struct mt7621_pcie *pcie, unsigned int dev, u32 reg)
976 + u32 address = mt7621_pci_get_cfgaddr(0, dev, 0, reg);
978 + pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
979 + return pcie_read(pcie, RALINK_PCI_CONFIG_DATA);
982 +static void write_config(struct mt7621_pcie *pcie, unsigned int dev,
985 + u32 address = mt7621_pci_get_cfgaddr(0, dev, 0, reg);
987 + pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
988 + pcie_write(pcie, val, RALINK_PCI_CONFIG_DATA);
991 +static inline void mt7621_rst_gpio_pcie_assert(struct mt7621_pcie_port *port)
993 + if (port->gpio_rst)
994 + gpiod_set_value(port->gpio_rst, 1);
997 +static inline void mt7621_rst_gpio_pcie_deassert(struct mt7621_pcie_port *port)
999 + if (port->gpio_rst)
1000 + gpiod_set_value(port->gpio_rst, 0);
1003 +static inline bool mt7621_pcie_port_is_linkup(struct mt7621_pcie_port *port)
1005 + return (pcie_port_read(port, RALINK_PCI_STATUS) & PCIE_PORT_LINKUP) != 0;
1008 +static inline void mt7621_control_assert(struct mt7621_pcie_port *port)
1010 + struct mt7621_pcie *pcie = port->pcie;
1012 + if (pcie->resets_inverted)
1013 + reset_control_assert(port->pcie_rst);
1015 + reset_control_deassert(port->pcie_rst);
1018 +static inline void mt7621_control_deassert(struct mt7621_pcie_port *port)
1020 + struct mt7621_pcie *pcie = port->pcie;
1022 + if (pcie->resets_inverted)
1023 + reset_control_deassert(port->pcie_rst);
1025 + reset_control_assert(port->pcie_rst);
1028 +static int setup_cm_memory_region(struct pci_host_bridge *host)
1030 + struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
1031 + struct device *dev = pcie->dev;
1032 + struct resource_entry *entry;
1033 + resource_size_t mask;
1035 + entry = resource_list_first_type(&host->windows, IORESOURCE_MEM);
1037 + dev_err(dev, "cannot get memory resource\n");
1041 + if (mips_cps_numiocu(0)) {
1043 + * FIXME: hardware doesn't accept mask values with 1s after
1044 + * 0s (e.g. 0xffef), so it would be great to warn if that's
1047 + mask = ~(entry->res->end - entry->res->start);
1049 + write_gcr_reg1_base(entry->res->start);
1050 + write_gcr_reg1_mask(mask | CM_GCR_REGn_MASK_CMTGT_IOCU0);
1051 + dev_info(dev, "PCI coherence region base: 0x%08llx, mask/settings: 0x%08llx\n",
1052 + (unsigned long long)read_gcr_reg1_base(),
1053 + (unsigned long long)read_gcr_reg1_mask());
1059 +static int mt7621_pcie_parse_port(struct mt7621_pcie *pcie,
1060 + struct device_node *node,
1063 + struct mt7621_pcie_port *port;
1064 + struct device *dev = pcie->dev;
1065 + struct platform_device *pdev = to_platform_device(dev);
1069 + port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1073 + port->base = devm_platform_ioremap_resource(pdev, slot + 1);
1074 + if (IS_ERR(port->base))
1075 + return PTR_ERR(port->base);
1077 + port->clk = devm_get_clk_from_child(dev, node, NULL);
1078 + if (IS_ERR(port->clk)) {
1079 + dev_err(dev, "failed to get pcie%d clock\n", slot);
1080 + return PTR_ERR(port->clk);
1083 + port->pcie_rst = of_reset_control_get_exclusive(node, NULL);
1084 + if (PTR_ERR(port->pcie_rst) == -EPROBE_DEFER) {
1085 + dev_err(dev, "failed to get pcie%d reset control\n", slot);
1086 + return PTR_ERR(port->pcie_rst);
1089 + snprintf(name, sizeof(name), "pcie-phy%d", slot);
1090 + port->phy = devm_of_phy_get(dev, node, name);
1091 + if (IS_ERR(port->phy)) {
1092 + dev_err(dev, "failed to get pcie-phy%d\n", slot);
1093 + err = PTR_ERR(port->phy);
1094 + goto remove_reset;
1097 + port->gpio_rst = devm_gpiod_get_index_optional(dev, "reset", slot,
1099 + if (IS_ERR(port->gpio_rst)) {
1100 + dev_err(dev, "failed to get GPIO for PCIe%d\n", slot);
1101 + err = PTR_ERR(port->gpio_rst);
1102 + goto remove_reset;
1105 + port->slot = slot;
1106 + port->pcie = pcie;
1108 + INIT_LIST_HEAD(&port->list);
1109 + list_add_tail(&port->list, &pcie->ports);
1114 + reset_control_put(port->pcie_rst);
1118 +static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
1120 + struct device *dev = pcie->dev;
1121 + struct platform_device *pdev = to_platform_device(dev);
1122 + struct device_node *node = dev->of_node, *child;
1125 + pcie->base = devm_platform_ioremap_resource(pdev, 0);
1126 + if (IS_ERR(pcie->base))
1127 + return PTR_ERR(pcie->base);
1129 + for_each_available_child_of_node(node, child) {
1132 + err = of_pci_get_devfn(child);
1134 + of_node_put(child);
1135 + dev_err(dev, "failed to parse devfn: %d\n", err);
1139 + slot = PCI_SLOT(err);
1141 + err = mt7621_pcie_parse_port(pcie, child, slot);
1143 + of_node_put(child);
1151 +static int mt7621_pcie_init_port(struct mt7621_pcie_port *port)
1153 + struct mt7621_pcie *pcie = port->pcie;
1154 + struct device *dev = pcie->dev;
1155 + u32 slot = port->slot;
1158 + err = phy_init(port->phy);
1160 + dev_err(dev, "failed to initialize port%d phy\n", slot);
1164 + err = phy_power_on(port->phy);
1166 + dev_err(dev, "failed to power on port%d phy\n", slot);
1167 + phy_exit(port->phy);
1171 + port->enabled = true;
1176 +static void mt7621_pcie_reset_assert(struct mt7621_pcie *pcie)
1178 + struct mt7621_pcie_port *port;
1180 + list_for_each_entry(port, &pcie->ports, list) {
1181 + /* PCIe RC reset assert */
1182 + mt7621_control_assert(port);
1184 + /* PCIe EP reset assert */
1185 + mt7621_rst_gpio_pcie_assert(port);
1188 + msleep(PERST_DELAY_MS);
1191 +static void mt7621_pcie_reset_rc_deassert(struct mt7621_pcie *pcie)
1193 + struct mt7621_pcie_port *port;
1195 + list_for_each_entry(port, &pcie->ports, list)
1196 + mt7621_control_deassert(port);
1199 +static void mt7621_pcie_reset_ep_deassert(struct mt7621_pcie *pcie)
1201 + struct mt7621_pcie_port *port;
1203 + list_for_each_entry(port, &pcie->ports, list)
1204 + mt7621_rst_gpio_pcie_deassert(port);
1206 + msleep(PERST_DELAY_MS);
1209 +static int mt7621_pcie_init_ports(struct mt7621_pcie *pcie)
1211 + struct device *dev = pcie->dev;
1212 + struct mt7621_pcie_port *port, *tmp;
1213 + u8 num_disabled = 0;
1216 + mt7621_pcie_reset_assert(pcie);
1217 + mt7621_pcie_reset_rc_deassert(pcie);
1219 + list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
1220 + u32 slot = port->slot;
1223 + port->enabled = true;
1227 + err = mt7621_pcie_init_port(port);
1229 + dev_err(dev, "initializing port %d failed\n", slot);
1230 + list_del(&port->list);
1234 + mt7621_pcie_reset_ep_deassert(pcie);
1237 + list_for_each_entry(port, &pcie->ports, list) {
1238 + u32 slot = port->slot;
1240 + if (!mt7621_pcie_port_is_linkup(port)) {
1241 + dev_err(dev, "pcie%d no card, disable it (RST & CLK)\n",
1243 + mt7621_control_assert(port);
1244 + port->enabled = false;
1252 + if (slot == 1 && tmp && !tmp->enabled)
1253 + phy_power_off(tmp->phy);
1257 + return (num_disabled != PCIE_PORT_CNT) ? 0 : -ENODEV;
1260 +static void mt7621_pcie_enable_port(struct mt7621_pcie_port *port)
1262 + struct mt7621_pcie *pcie = port->pcie;
1263 + u32 slot = port->slot;
1266 + /* enable pcie interrupt */
1267 + val = pcie_read(pcie, RALINK_PCI_PCIMSK_ADDR);
1268 + val |= PCIE_PORT_INT_EN(slot);
1269 + pcie_write(pcie, val, RALINK_PCI_PCIMSK_ADDR);
1271 + /* map 2G DDR region */
1272 + pcie_port_write(port, PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
1273 + PCI_BASE_ADDRESS_0);
1275 + /* configure class code and revision ID */
1276 + pcie_port_write(port, PCIE_CLASS_CODE | PCIE_REVISION_ID,
1277 + RALINK_PCI_CLASS);
1279 + /* configure RC FTS number to 250 when it leaves L0s */
1280 + val = read_config(pcie, slot, PCIE_FTS_NUM);
1281 + val &= ~PCIE_FTS_NUM_MASK;
1282 + val |= PCIE_FTS_NUM_L0(0x50);
1283 + write_config(pcie, slot, PCIE_FTS_NUM, val);
1286 +static int mt7621_pcie_enable_ports(struct pci_host_bridge *host)
1288 + struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
1289 + struct device *dev = pcie->dev;
1290 + struct mt7621_pcie_port *port;
1291 + struct resource_entry *entry;
1294 + entry = resource_list_first_type(&host->windows, IORESOURCE_IO);
1296 + dev_err(dev, "cannot get io resource\n");
1300 + /* Setup MEMWIN and IOWIN */
1301 + pcie_write(pcie, 0xffffffff, RALINK_PCI_MEMBASE);
1302 + pcie_write(pcie, entry->res->start, RALINK_PCI_IOBASE);
1304 + list_for_each_entry(port, &pcie->ports, list) {
1305 + if (port->enabled) {
1306 + err = clk_prepare_enable(port->clk);
1308 + dev_err(dev, "enabling clk pcie%d\n",
1313 + mt7621_pcie_enable_port(port);
1314 + dev_info(dev, "PCIE%d enabled\n", port->slot);
1321 +static int mt7621_pcie_register_host(struct pci_host_bridge *host)
1323 + struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
1325 + host->ops = &mt7621_pci_ops;
1326 + host->sysdata = pcie;
1327 + return pci_host_probe(host);
1330 +static const struct soc_device_attribute mt7621_pci_quirks_match[] = {
1331 + { .soc_id = "mt7621", .revision = "E2" }
1334 +static int mt7621_pci_probe(struct platform_device *pdev)
1336 + struct device *dev = &pdev->dev;
1337 + const struct soc_device_attribute *attr;
1338 + struct mt7621_pcie_port *port;
1339 + struct mt7621_pcie *pcie;
1340 + struct pci_host_bridge *bridge;
1343 + if (!dev->of_node)
1346 + bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
1350 + pcie = pci_host_bridge_priv(bridge);
1352 + platform_set_drvdata(pdev, pcie);
1353 + INIT_LIST_HEAD(&pcie->ports);
1355 + attr = soc_device_match(mt7621_pci_quirks_match);
1357 + pcie->resets_inverted = true;
1359 + err = mt7621_pcie_parse_dt(pcie);
1361 + dev_err(dev, "parsing DT failed\n");
1365 + err = mt7621_pcie_init_ports(pcie);
1367 + dev_err(dev, "nothing connected in virtual bridges\n");
1371 + err = mt7621_pcie_enable_ports(bridge);
1373 + dev_err(dev, "error enabling pcie ports\n");
1374 + goto remove_resets;
1377 + err = setup_cm_memory_region(bridge);
1379 + dev_err(dev, "error setting up iocu mem regions\n");
1380 + goto remove_resets;
1383 + return mt7621_pcie_register_host(bridge);
1386 + list_for_each_entry(port, &pcie->ports, list)
1387 + reset_control_put(port->pcie_rst);
1392 +static int mt7621_pci_remove(struct platform_device *pdev)
1394 + struct mt7621_pcie *pcie = platform_get_drvdata(pdev);
1395 + struct mt7621_pcie_port *port;
1397 + list_for_each_entry(port, &pcie->ports, list)
1398 + reset_control_put(port->pcie_rst);
1403 +static const struct of_device_id mt7621_pci_ids[] = {
1404 + { .compatible = "mediatek,mt7621-pci" },
1407 +MODULE_DEVICE_TABLE(of, mt7621_pci_ids);
1409 +static struct platform_driver mt7621_pci_driver = {
1410 + .probe = mt7621_pci_probe,
1411 + .remove = mt7621_pci_remove,
1413 + .name = "mt7621-pci",
1414 + .of_match_table = of_match_ptr(mt7621_pci_ids),
1417 +builtin_platform_driver(mt7621_pci_driver);