ramips: add support for Beeline SmartBox GIGA
[openwrt/openwrt.git] / target / linux / ramips / patches-5.15 / 100-PCI-mt7621-Add-MediaTek-MT7621-PCIe-host-controller-.patch
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
4
5 Add driver for the PCIe controller of the MT7621 SoC.
6
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>
14 ---
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
20
21 --- a/arch/mips/ralink/Kconfig
22 +++ b/arch/mips/ralink/Kconfig
23 @@ -51,7 +51,8 @@ choice
24 select SYS_SUPPORTS_HIGHMEM
25 select MIPS_GIC
26 select CLKSRC_MIPS_GIC
27 - select HAVE_PCI if PCI_MT7621
28 + select HAVE_PCI
29 + select PCI_DRIVERS_GENERIC
30 select SOC_BUS
31 endchoice
32
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
38
39 +config PCIE_MT7621
40 + tristate "MediaTek MT7621 PCIe Controller"
41 + depends on (RALINK && SOC_MT7621) || (MIPS && COMPILE_TEST)
42 + select PHY_MT7621_PCI
43 + default SOC_MT7621
44 + help
45 + This selects a driver for the MediaTek MT7621 PCIe Controller.
46 +
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
57 +
58 # pcie-hisi.o quirks are needed even without CONFIG_PCIE_DW
59 obj-y += dwc/
60 obj-y += mobiveil/
61 --- a/drivers/staging/Kconfig
62 +++ b/drivers/staging/Kconfig
63 @@ -86,8 +86,6 @@ source "drivers/staging/vc04_services/Kc
64
65 source "drivers/staging/pi433/Kconfig"
66
67 -source "drivers/staging/mt7621-pci/Kconfig"
68 -
69 source "drivers/staging/mt7621-dma/Kconfig"
70
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
83 +++ /dev/null
84 @@ -1,8 +0,0 @@
85 -# SPDX-License-Identifier: GPL-2.0
86 -config PCI_MT7621
87 - tristate "MediaTek MT7621 PCI Controller"
88 - depends on RALINK
89 - select PCI_DRIVERS_GENERIC
90 - help
91 - This selects a driver for the MediaTek MT7621 PCI Controller.
92 -
93 --- a/drivers/staging/mt7621-pci/Makefile
94 +++ /dev/null
95 @@ -1,2 +0,0 @@
96 -# SPDX-License-Identifier: GPL-2.0
97 -obj-$(CONFIG_PCI_MT7621) += pci-mt7621.o
98 --- a/drivers/staging/mt7621-pci/TODO
99 +++ /dev/null
100 @@ -1,4 +0,0 @@
101 -
102 -- general code review and cleanup
103 -
104 -Cc: NeilBrown <neil@brown.name>
105 --- a/drivers/staging/mt7621-pci/mediatek,mt7621-pci.txt
106 +++ /dev/null
107 @@ -1,104 +0,0 @@
108 -MediaTek MT7621 PCIe controller
109 -
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
123 - explanation.
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
128 - root ports.
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
132 - root ports.
133 -- reset-gpios: GPIO specs for the reset pins.
134 -
135 -In addition, the device tree node must have sub-nodes describing each PCIe port
136 -interface, having the following mandatory properties:
137 -
138 -Required properties:
139 -- reg: Only the first four bytes are used to refer to the correct bus number
140 - and device 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.
146 -
147 -Example for MT7621:
148 -
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 */
155 -
156 - #address-cells = <3>;
157 - #size-cells = <2>;
158 -
159 - pinctrl-names = "default";
160 - pinctrl-0 = <&pcie_pins>;
161 -
162 - device_type = "pci";
163 -
164 - bus-range = <0 255>;
165 - ranges = <
166 - 0x02000000 0 0x00000000 0x60000000 0 0x10000000 /* pci memory */
167 - 0x01000000 0 0x00000000 0x1e160000 0 0x00010000 /* io space */
168 - >;
169 -
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>;
175 -
176 - status = "disabled";
177 -
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";
182 -
183 - reset-gpios = <&gpio 19 GPIO_ACTIVE_LOW>,
184 - <&gpio 8 GPIO_ACTIVE_LOW>,
185 - <&gpio 7 GPIO_ACTIVE_LOW>;
186 -
187 - pcie@0,0 {
188 - reg = <0x0000 0 0 0 0>;
189 - #address-cells = <3>;
190 - #size-cells = <2>;
191 - ranges;
192 - bus-range = <0x00 0xff>;
193 - };
194 -
195 - pcie@1,0 {
196 - reg = <0x0800 0 0 0 0>;
197 - #address-cells = <3>;
198 - #size-cells = <2>;
199 - ranges;
200 - bus-range = <0x00 0xff>;
201 - };
202 -
203 - pcie@2,0 {
204 - reg = <0x1000 0 0 0 0>;
205 - #address-cells = <3>;
206 - #size-cells = <2>;
207 - ranges;
208 - bus-range = <0x00 0xff>;
209 - };
210 - };
211 -
212 --- a/drivers/staging/mt7621-pci/pci-mt7621.c
213 +++ /dev/null
214 @@ -1,600 +0,0 @@
215 -// SPDX-License-Identifier: GPL-2.0+
216 -/*
217 - * BRIEF MODULE DESCRIPTION
218 - * PCI init for Ralink RT2880 solution
219 - *
220 - * Copyright 2007 Ralink Inc. (bruce_chang@ralinktech.com.tw)
221 - *
222 - * May 2007 Bruce Chang
223 - * Initial Release
224 - *
225 - * May 2009 Bruce Chang
226 - * support RT2880/RT3883 PCIe
227 - *
228 - * May 2011 Bruce Chang
229 - * support RT6855/MT7620 PCIe
230 - */
231 -
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>
246 -
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)
251 -
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
259 -
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
265 -
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
274 -
275 -#define PERST_DELAY_MS 100
276 -
277 -/**
278 - * struct mt7621_pcie_port - PCIe port information
279 - * @base: I/O mapped register base
280 - * @list: port list
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
286 - * @slot: port slot
287 - * @enabled: indicates if port is enabled
288 - */
289 -struct mt7621_pcie_port {
290 - void __iomem *base;
291 - struct list_head list;
292 - struct mt7621_pcie *pcie;
293 - struct clk *clk;
294 - struct phy *phy;
295 - struct reset_control *pcie_rst;
296 - struct gpio_desc *gpio_rst;
297 - u32 slot;
298 - bool enabled;
299 -};
300 -
301 -/**
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.
308 - */
309 -struct mt7621_pcie {
310 - void __iomem *base;
311 - struct device *dev;
312 - struct list_head ports;
313 - bool resets_inverted;
314 -};
315 -
316 -static inline u32 pcie_read(struct mt7621_pcie *pcie, u32 reg)
317 -{
318 - return readl_relaxed(pcie->base + reg);
319 -}
320 -
321 -static inline void pcie_write(struct mt7621_pcie *pcie, u32 val, u32 reg)
322 -{
323 - writel_relaxed(val, pcie->base + reg);
324 -}
325 -
326 -static inline void pcie_rmw(struct mt7621_pcie *pcie, u32 reg, u32 clr, u32 set)
327 -{
328 - u32 val = readl_relaxed(pcie->base + reg);
329 -
330 - val &= ~clr;
331 - val |= set;
332 - writel_relaxed(val, pcie->base + reg);
333 -}
334 -
335 -static inline u32 pcie_port_read(struct mt7621_pcie_port *port, u32 reg)
336 -{
337 - return readl_relaxed(port->base + reg);
338 -}
339 -
340 -static inline void pcie_port_write(struct mt7621_pcie_port *port,
341 - u32 val, u32 reg)
342 -{
343 - writel_relaxed(val, port->base + reg);
344 -}
345 -
346 -static inline u32 mt7621_pci_get_cfgaddr(unsigned int bus, unsigned int slot,
347 - unsigned int func, unsigned int where)
348 -{
349 - return (((where & 0xF00) >> 8) << 24) | (bus << 16) | (slot << 11) |
350 - (func << 8) | (where & 0xfc) | 0x80000000;
351 -}
352 -
353 -static void __iomem *mt7621_pcie_map_bus(struct pci_bus *bus,
354 - unsigned int devfn, int where)
355 -{
356 - struct mt7621_pcie *pcie = bus->sysdata;
357 - u32 address = mt7621_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
358 - PCI_FUNC(devfn), where);
359 -
360 - writel_relaxed(address, pcie->base + RALINK_PCI_CONFIG_ADDR);
361 -
362 - return pcie->base + RALINK_PCI_CONFIG_DATA + (where & 3);
363 -}
364 -
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,
369 -};
370 -
371 -static u32 read_config(struct mt7621_pcie *pcie, unsigned int dev, u32 reg)
372 -{
373 - u32 address = mt7621_pci_get_cfgaddr(0, dev, 0, reg);
374 -
375 - pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
376 - return pcie_read(pcie, RALINK_PCI_CONFIG_DATA);
377 -}
378 -
379 -static void write_config(struct mt7621_pcie *pcie, unsigned int dev,
380 - u32 reg, u32 val)
381 -{
382 - u32 address = mt7621_pci_get_cfgaddr(0, dev, 0, reg);
383 -
384 - pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
385 - pcie_write(pcie, val, RALINK_PCI_CONFIG_DATA);
386 -}
387 -
388 -static inline void mt7621_rst_gpio_pcie_assert(struct mt7621_pcie_port *port)
389 -{
390 - if (port->gpio_rst)
391 - gpiod_set_value(port->gpio_rst, 1);
392 -}
393 -
394 -static inline void mt7621_rst_gpio_pcie_deassert(struct mt7621_pcie_port *port)
395 -{
396 - if (port->gpio_rst)
397 - gpiod_set_value(port->gpio_rst, 0);
398 -}
399 -
400 -static inline bool mt7621_pcie_port_is_linkup(struct mt7621_pcie_port *port)
401 -{
402 - return (pcie_port_read(port, RALINK_PCI_STATUS) & PCIE_PORT_LINKUP) != 0;
403 -}
404 -
405 -static inline void mt7621_control_assert(struct mt7621_pcie_port *port)
406 -{
407 - struct mt7621_pcie *pcie = port->pcie;
408 -
409 - if (pcie->resets_inverted)
410 - reset_control_assert(port->pcie_rst);
411 - else
412 - reset_control_deassert(port->pcie_rst);
413 -}
414 -
415 -static inline void mt7621_control_deassert(struct mt7621_pcie_port *port)
416 -{
417 - struct mt7621_pcie *pcie = port->pcie;
418 -
419 - if (pcie->resets_inverted)
420 - reset_control_deassert(port->pcie_rst);
421 - else
422 - reset_control_assert(port->pcie_rst);
423 -}
424 -
425 -static int setup_cm_memory_region(struct pci_host_bridge *host)
426 -{
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;
431 -
432 - entry = resource_list_first_type(&host->windows, IORESOURCE_MEM);
433 - if (!entry) {
434 - dev_err(dev, "Cannot get memory resource\n");
435 - return -EINVAL;
436 - }
437 -
438 - if (mips_cps_numiocu(0)) {
439 - /*
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
442 - * about to happen
443 - */
444 - mask = ~(entry->res->end - entry->res->start);
445 -
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());
451 - }
452 -
453 - return 0;
454 -}
455 -
456 -static int mt7621_pcie_parse_port(struct mt7621_pcie *pcie,
457 - struct device_node *node,
458 - int slot)
459 -{
460 - struct mt7621_pcie_port *port;
461 - struct device *dev = pcie->dev;
462 - struct platform_device *pdev = to_platform_device(dev);
463 - char name[10];
464 - int err;
465 -
466 - port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
467 - if (!port)
468 - return -ENOMEM;
469 -
470 - port->base = devm_platform_ioremap_resource(pdev, slot + 1);
471 - if (IS_ERR(port->base))
472 - return PTR_ERR(port->base);
473 -
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);
478 - }
479 -
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);
484 - }
485 -
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);
491 - goto remove_reset;
492 - }
493 -
494 - port->gpio_rst = devm_gpiod_get_index_optional(dev, "reset", slot,
495 - GPIOD_OUT_LOW);
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);
499 - goto remove_reset;
500 - }
501 -
502 - port->slot = slot;
503 - port->pcie = pcie;
504 -
505 - INIT_LIST_HEAD(&port->list);
506 - list_add_tail(&port->list, &pcie->ports);
507 -
508 - return 0;
509 -
510 -remove_reset:
511 - reset_control_put(port->pcie_rst);
512 - return err;
513 -}
514 -
515 -static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
516 -{
517 - struct device *dev = pcie->dev;
518 - struct platform_device *pdev = to_platform_device(dev);
519 - struct device_node *node = dev->of_node, *child;
520 - int err;
521 -
522 - pcie->base = devm_platform_ioremap_resource(pdev, 0);
523 - if (IS_ERR(pcie->base))
524 - return PTR_ERR(pcie->base);
525 -
526 - for_each_available_child_of_node(node, child) {
527 - int slot;
528 -
529 - err = of_pci_get_devfn(child);
530 - if (err < 0) {
531 - of_node_put(child);
532 - dev_err(dev, "failed to parse devfn: %d\n", err);
533 - return err;
534 - }
535 -
536 - slot = PCI_SLOT(err);
537 -
538 - err = mt7621_pcie_parse_port(pcie, child, slot);
539 - if (err) {
540 - of_node_put(child);
541 - return err;
542 - }
543 - }
544 -
545 - return 0;
546 -}
547 -
548 -static int mt7621_pcie_init_port(struct mt7621_pcie_port *port)
549 -{
550 - struct mt7621_pcie *pcie = port->pcie;
551 - struct device *dev = pcie->dev;
552 - u32 slot = port->slot;
553 - int err;
554 -
555 - err = phy_init(port->phy);
556 - if (err) {
557 - dev_err(dev, "failed to initialize port%d phy\n", slot);
558 - return err;
559 - }
560 -
561 - err = phy_power_on(port->phy);
562 - if (err) {
563 - dev_err(dev, "failed to power on port%d phy\n", slot);
564 - phy_exit(port->phy);
565 - return err;
566 - }
567 -
568 - port->enabled = true;
569 -
570 - return 0;
571 -}
572 -
573 -static void mt7621_pcie_reset_assert(struct mt7621_pcie *pcie)
574 -{
575 - struct mt7621_pcie_port *port;
576 -
577 - list_for_each_entry(port, &pcie->ports, list) {
578 - /* PCIe RC reset assert */
579 - mt7621_control_assert(port);
580 -
581 - /* PCIe EP reset assert */
582 - mt7621_rst_gpio_pcie_assert(port);
583 - }
584 -
585 - msleep(PERST_DELAY_MS);
586 -}
587 -
588 -static void mt7621_pcie_reset_rc_deassert(struct mt7621_pcie *pcie)
589 -{
590 - struct mt7621_pcie_port *port;
591 -
592 - list_for_each_entry(port, &pcie->ports, list)
593 - mt7621_control_deassert(port);
594 -}
595 -
596 -static void mt7621_pcie_reset_ep_deassert(struct mt7621_pcie *pcie)
597 -{
598 - struct mt7621_pcie_port *port;
599 -
600 - list_for_each_entry(port, &pcie->ports, list)
601 - mt7621_rst_gpio_pcie_deassert(port);
602 -
603 - msleep(PERST_DELAY_MS);
604 -}
605 -
606 -static int mt7621_pcie_init_ports(struct mt7621_pcie *pcie)
607 -{
608 - struct device *dev = pcie->dev;
609 - struct mt7621_pcie_port *port, *tmp;
610 - u8 num_disabled = 0;
611 - int err;
612 -
613 - mt7621_pcie_reset_assert(pcie);
614 - mt7621_pcie_reset_rc_deassert(pcie);
615 -
616 - list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
617 - u32 slot = port->slot;
618 -
619 - if (slot == 1) {
620 - port->enabled = true;
621 - continue;
622 - }
623 -
624 - err = mt7621_pcie_init_port(port);
625 - if (err) {
626 - dev_err(dev, "Initiating port %d failed\n", slot);
627 - list_del(&port->list);
628 - }
629 - }
630 -
631 - mt7621_pcie_reset_ep_deassert(pcie);
632 -
633 - tmp = NULL;
634 - list_for_each_entry(port, &pcie->ports, list) {
635 - u32 slot = port->slot;
636 -
637 - if (!mt7621_pcie_port_is_linkup(port)) {
638 - dev_err(dev, "pcie%d no card, disable it (RST & CLK)\n",
639 - slot);
640 - mt7621_control_assert(port);
641 - port->enabled = false;
642 - num_disabled++;
643 -
644 - if (slot == 0) {
645 - tmp = port;
646 - continue;
647 - }
648 -
649 - if (slot == 1 && tmp && !tmp->enabled)
650 - phy_power_off(tmp->phy);
651 - }
652 - }
653 -
654 - return (num_disabled != PCIE_PORT_CNT) ? 0 : -ENODEV;
655 -}
656 -
657 -static void mt7621_pcie_enable_port(struct mt7621_pcie_port *port)
658 -{
659 - struct mt7621_pcie *pcie = port->pcie;
660 - u32 slot = port->slot;
661 - u32 val;
662 -
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);
667 -
668 - /* map 2G DDR region */
669 - pcie_port_write(port, PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
670 - PCI_BASE_ADDRESS_0);
671 -
672 - /* configure class code and revision ID */
673 - pcie_port_write(port, PCIE_CLASS_CODE | PCIE_REVISION_ID,
674 - RALINK_PCI_CLASS);
675 -
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);
681 -}
682 -
683 -static int mt7621_pcie_enable_ports(struct pci_host_bridge *host)
684 -{
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;
689 - int err;
690 -
691 - entry = resource_list_first_type(&host->windows, IORESOURCE_IO);
692 - if (!entry) {
693 - dev_err(dev, "Cannot get io resource\n");
694 - return -EINVAL;
695 - }
696 -
697 - /* Setup MEMWIN and IOWIN */
698 - pcie_write(pcie, 0xffffffff, RALINK_PCI_MEMBASE);
699 - pcie_write(pcie, entry->res->start, RALINK_PCI_IOBASE);
700 -
701 - list_for_each_entry(port, &pcie->ports, list) {
702 - if (port->enabled) {
703 - err = clk_prepare_enable(port->clk);
704 - if (err) {
705 - dev_err(dev, "enabling clk pcie%d\n",
706 - port->slot);
707 - return err;
708 - }
709 -
710 - mt7621_pcie_enable_port(port);
711 - dev_info(dev, "PCIE%d enabled\n", port->slot);
712 - }
713 - }
714 -
715 - return 0;
716 -}
717 -
718 -static int mt7621_pcie_register_host(struct pci_host_bridge *host)
719 -{
720 - struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
721 -
722 - host->ops = &mt7621_pci_ops;
723 - host->sysdata = pcie;
724 - return pci_host_probe(host);
725 -}
726 -
727 -static const struct soc_device_attribute mt7621_pci_quirks_match[] = {
728 - { .soc_id = "mt7621", .revision = "E2" }
729 -};
730 -
731 -static int mt7621_pci_probe(struct platform_device *pdev)
732 -{
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;
738 - int err;
739 -
740 - if (!dev->of_node)
741 - return -ENODEV;
742 -
743 - bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
744 - if (!bridge)
745 - return -ENOMEM;
746 -
747 - pcie = pci_host_bridge_priv(bridge);
748 - pcie->dev = dev;
749 - platform_set_drvdata(pdev, pcie);
750 - INIT_LIST_HEAD(&pcie->ports);
751 -
752 - attr = soc_device_match(mt7621_pci_quirks_match);
753 - if (attr)
754 - pcie->resets_inverted = true;
755 -
756 - err = mt7621_pcie_parse_dt(pcie);
757 - if (err) {
758 - dev_err(dev, "Parsing DT failed\n");
759 - return err;
760 - }
761 -
762 - err = mt7621_pcie_init_ports(pcie);
763 - if (err) {
764 - dev_err(dev, "Nothing connected in virtual bridges\n");
765 - return 0;
766 - }
767 -
768 - err = mt7621_pcie_enable_ports(bridge);
769 - if (err) {
770 - dev_err(dev, "Error enabling pcie ports\n");
771 - goto remove_resets;
772 - }
773 -
774 - err = setup_cm_memory_region(bridge);
775 - if (err) {
776 - dev_err(dev, "Error setting up iocu mem regions\n");
777 - goto remove_resets;
778 - }
779 -
780 - return mt7621_pcie_register_host(bridge);
781 -
782 -remove_resets:
783 - list_for_each_entry(port, &pcie->ports, list)
784 - reset_control_put(port->pcie_rst);
785 -
786 - return err;
787 -}
788 -
789 -static int mt7621_pci_remove(struct platform_device *pdev)
790 -{
791 - struct mt7621_pcie *pcie = platform_get_drvdata(pdev);
792 - struct mt7621_pcie_port *port;
793 -
794 - list_for_each_entry(port, &pcie->ports, list)
795 - reset_control_put(port->pcie_rst);
796 -
797 - return 0;
798 -}
799 -
800 -static const struct of_device_id mt7621_pci_ids[] = {
801 - { .compatible = "mediatek,mt7621-pci" },
802 - {},
803 -};
804 -MODULE_DEVICE_TABLE(of, mt7621_pci_ids);
805 -
806 -static struct platform_driver mt7621_pci_driver = {
807 - .probe = mt7621_pci_probe,
808 - .remove = mt7621_pci_remove,
809 - .driver = {
810 - .name = "mt7621-pci",
811 - .of_match_table = of_match_ptr(mt7621_pci_ids),
812 - },
813 -};
814 -builtin_platform_driver(mt7621_pci_driver);
815 --- /dev/null
816 +++ b/drivers/pci/controller/pcie-mt7621.c
817 @@ -0,0 +1,600 @@
818 +// SPDX-License-Identifier: GPL-2.0+
819 +/*
820 + * BRIEF MODULE DESCRIPTION
821 + * PCI init for Ralink RT2880 solution
822 + *
823 + * Copyright 2007 Ralink Inc. (bruce_chang@ralinktech.com.tw)
824 + *
825 + * May 2007 Bruce Chang
826 + * Initial Release
827 + *
828 + * May 2009 Bruce Chang
829 + * support RT2880/RT3883 PCIe
830 + *
831 + * May 2011 Bruce Chang
832 + * support RT6855/MT7620 PCIe
833 + */
834 +
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>
849 +
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)
854 +
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
862 +
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
868 +
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
877 +
878 +#define PERST_DELAY_MS 100
879 +
880 +/**
881 + * struct mt7621_pcie_port - PCIe port information
882 + * @base: I/O mapped register base
883 + * @list: port list
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
889 + * @slot: port slot
890 + * @enabled: indicates if port is enabled
891 + */
892 +struct mt7621_pcie_port {
893 + void __iomem *base;
894 + struct list_head list;
895 + struct mt7621_pcie *pcie;
896 + struct clk *clk;
897 + struct phy *phy;
898 + struct reset_control *pcie_rst;
899 + struct gpio_desc *gpio_rst;
900 + u32 slot;
901 + bool enabled;
902 +};
903 +
904 +/**
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.
911 + */
912 +struct mt7621_pcie {
913 + void __iomem *base;
914 + struct device *dev;
915 + struct list_head ports;
916 + bool resets_inverted;
917 +};
918 +
919 +static inline u32 pcie_read(struct mt7621_pcie *pcie, u32 reg)
920 +{
921 + return readl_relaxed(pcie->base + reg);
922 +}
923 +
924 +static inline void pcie_write(struct mt7621_pcie *pcie, u32 val, u32 reg)
925 +{
926 + writel_relaxed(val, pcie->base + reg);
927 +}
928 +
929 +static inline void pcie_rmw(struct mt7621_pcie *pcie, u32 reg, u32 clr, u32 set)
930 +{
931 + u32 val = readl_relaxed(pcie->base + reg);
932 +
933 + val &= ~clr;
934 + val |= set;
935 + writel_relaxed(val, pcie->base + reg);
936 +}
937 +
938 +static inline u32 pcie_port_read(struct mt7621_pcie_port *port, u32 reg)
939 +{
940 + return readl_relaxed(port->base + reg);
941 +}
942 +
943 +static inline void pcie_port_write(struct mt7621_pcie_port *port,
944 + u32 val, u32 reg)
945 +{
946 + writel_relaxed(val, port->base + reg);
947 +}
948 +
949 +static inline u32 mt7621_pci_get_cfgaddr(unsigned int bus, unsigned int slot,
950 + unsigned int func, unsigned int where)
951 +{
952 + return (((where & 0xf00) >> 8) << 24) | (bus << 16) | (slot << 11) |
953 + (func << 8) | (where & 0xfc) | 0x80000000;
954 +}
955 +
956 +static void __iomem *mt7621_pcie_map_bus(struct pci_bus *bus,
957 + unsigned int devfn, int where)
958 +{
959 + struct mt7621_pcie *pcie = bus->sysdata;
960 + u32 address = mt7621_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
961 + PCI_FUNC(devfn), where);
962 +
963 + writel_relaxed(address, pcie->base + RALINK_PCI_CONFIG_ADDR);
964 +
965 + return pcie->base + RALINK_PCI_CONFIG_DATA + (where & 3);
966 +}
967 +
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,
972 +};
973 +
974 +static u32 read_config(struct mt7621_pcie *pcie, unsigned int dev, u32 reg)
975 +{
976 + u32 address = mt7621_pci_get_cfgaddr(0, dev, 0, reg);
977 +
978 + pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
979 + return pcie_read(pcie, RALINK_PCI_CONFIG_DATA);
980 +}
981 +
982 +static void write_config(struct mt7621_pcie *pcie, unsigned int dev,
983 + u32 reg, u32 val)
984 +{
985 + u32 address = mt7621_pci_get_cfgaddr(0, dev, 0, reg);
986 +
987 + pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
988 + pcie_write(pcie, val, RALINK_PCI_CONFIG_DATA);
989 +}
990 +
991 +static inline void mt7621_rst_gpio_pcie_assert(struct mt7621_pcie_port *port)
992 +{
993 + if (port->gpio_rst)
994 + gpiod_set_value(port->gpio_rst, 1);
995 +}
996 +
997 +static inline void mt7621_rst_gpio_pcie_deassert(struct mt7621_pcie_port *port)
998 +{
999 + if (port->gpio_rst)
1000 + gpiod_set_value(port->gpio_rst, 0);
1001 +}
1002 +
1003 +static inline bool mt7621_pcie_port_is_linkup(struct mt7621_pcie_port *port)
1004 +{
1005 + return (pcie_port_read(port, RALINK_PCI_STATUS) & PCIE_PORT_LINKUP) != 0;
1006 +}
1007 +
1008 +static inline void mt7621_control_assert(struct mt7621_pcie_port *port)
1009 +{
1010 + struct mt7621_pcie *pcie = port->pcie;
1011 +
1012 + if (pcie->resets_inverted)
1013 + reset_control_assert(port->pcie_rst);
1014 + else
1015 + reset_control_deassert(port->pcie_rst);
1016 +}
1017 +
1018 +static inline void mt7621_control_deassert(struct mt7621_pcie_port *port)
1019 +{
1020 + struct mt7621_pcie *pcie = port->pcie;
1021 +
1022 + if (pcie->resets_inverted)
1023 + reset_control_deassert(port->pcie_rst);
1024 + else
1025 + reset_control_assert(port->pcie_rst);
1026 +}
1027 +
1028 +static int setup_cm_memory_region(struct pci_host_bridge *host)
1029 +{
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;
1034 +
1035 + entry = resource_list_first_type(&host->windows, IORESOURCE_MEM);
1036 + if (!entry) {
1037 + dev_err(dev, "cannot get memory resource\n");
1038 + return -EINVAL;
1039 + }
1040 +
1041 + if (mips_cps_numiocu(0)) {
1042 + /*
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
1045 + * about to happen
1046 + */
1047 + mask = ~(entry->res->end - entry->res->start);
1048 +
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());
1054 + }
1055 +
1056 + return 0;
1057 +}
1058 +
1059 +static int mt7621_pcie_parse_port(struct mt7621_pcie *pcie,
1060 + struct device_node *node,
1061 + int slot)
1062 +{
1063 + struct mt7621_pcie_port *port;
1064 + struct device *dev = pcie->dev;
1065 + struct platform_device *pdev = to_platform_device(dev);
1066 + char name[10];
1067 + int err;
1068 +
1069 + port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1070 + if (!port)
1071 + return -ENOMEM;
1072 +
1073 + port->base = devm_platform_ioremap_resource(pdev, slot + 1);
1074 + if (IS_ERR(port->base))
1075 + return PTR_ERR(port->base);
1076 +
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);
1081 + }
1082 +
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);
1087 + }
1088 +
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;
1095 + }
1096 +
1097 + port->gpio_rst = devm_gpiod_get_index_optional(dev, "reset", slot,
1098 + GPIOD_OUT_LOW);
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;
1103 + }
1104 +
1105 + port->slot = slot;
1106 + port->pcie = pcie;
1107 +
1108 + INIT_LIST_HEAD(&port->list);
1109 + list_add_tail(&port->list, &pcie->ports);
1110 +
1111 + return 0;
1112 +
1113 +remove_reset:
1114 + reset_control_put(port->pcie_rst);
1115 + return err;
1116 +}
1117 +
1118 +static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
1119 +{
1120 + struct device *dev = pcie->dev;
1121 + struct platform_device *pdev = to_platform_device(dev);
1122 + struct device_node *node = dev->of_node, *child;
1123 + int err;
1124 +
1125 + pcie->base = devm_platform_ioremap_resource(pdev, 0);
1126 + if (IS_ERR(pcie->base))
1127 + return PTR_ERR(pcie->base);
1128 +
1129 + for_each_available_child_of_node(node, child) {
1130 + int slot;
1131 +
1132 + err = of_pci_get_devfn(child);
1133 + if (err < 0) {
1134 + of_node_put(child);
1135 + dev_err(dev, "failed to parse devfn: %d\n", err);
1136 + return err;
1137 + }
1138 +
1139 + slot = PCI_SLOT(err);
1140 +
1141 + err = mt7621_pcie_parse_port(pcie, child, slot);
1142 + if (err) {
1143 + of_node_put(child);
1144 + return err;
1145 + }
1146 + }
1147 +
1148 + return 0;
1149 +}
1150 +
1151 +static int mt7621_pcie_init_port(struct mt7621_pcie_port *port)
1152 +{
1153 + struct mt7621_pcie *pcie = port->pcie;
1154 + struct device *dev = pcie->dev;
1155 + u32 slot = port->slot;
1156 + int err;
1157 +
1158 + err = phy_init(port->phy);
1159 + if (err) {
1160 + dev_err(dev, "failed to initialize port%d phy\n", slot);
1161 + return err;
1162 + }
1163 +
1164 + err = phy_power_on(port->phy);
1165 + if (err) {
1166 + dev_err(dev, "failed to power on port%d phy\n", slot);
1167 + phy_exit(port->phy);
1168 + return err;
1169 + }
1170 +
1171 + port->enabled = true;
1172 +
1173 + return 0;
1174 +}
1175 +
1176 +static void mt7621_pcie_reset_assert(struct mt7621_pcie *pcie)
1177 +{
1178 + struct mt7621_pcie_port *port;
1179 +
1180 + list_for_each_entry(port, &pcie->ports, list) {
1181 + /* PCIe RC reset assert */
1182 + mt7621_control_assert(port);
1183 +
1184 + /* PCIe EP reset assert */
1185 + mt7621_rst_gpio_pcie_assert(port);
1186 + }
1187 +
1188 + msleep(PERST_DELAY_MS);
1189 +}
1190 +
1191 +static void mt7621_pcie_reset_rc_deassert(struct mt7621_pcie *pcie)
1192 +{
1193 + struct mt7621_pcie_port *port;
1194 +
1195 + list_for_each_entry(port, &pcie->ports, list)
1196 + mt7621_control_deassert(port);
1197 +}
1198 +
1199 +static void mt7621_pcie_reset_ep_deassert(struct mt7621_pcie *pcie)
1200 +{
1201 + struct mt7621_pcie_port *port;
1202 +
1203 + list_for_each_entry(port, &pcie->ports, list)
1204 + mt7621_rst_gpio_pcie_deassert(port);
1205 +
1206 + msleep(PERST_DELAY_MS);
1207 +}
1208 +
1209 +static int mt7621_pcie_init_ports(struct mt7621_pcie *pcie)
1210 +{
1211 + struct device *dev = pcie->dev;
1212 + struct mt7621_pcie_port *port, *tmp;
1213 + u8 num_disabled = 0;
1214 + int err;
1215 +
1216 + mt7621_pcie_reset_assert(pcie);
1217 + mt7621_pcie_reset_rc_deassert(pcie);
1218 +
1219 + list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
1220 + u32 slot = port->slot;
1221 +
1222 + if (slot == 1) {
1223 + port->enabled = true;
1224 + continue;
1225 + }
1226 +
1227 + err = mt7621_pcie_init_port(port);
1228 + if (err) {
1229 + dev_err(dev, "initializing port %d failed\n", slot);
1230 + list_del(&port->list);
1231 + }
1232 + }
1233 +
1234 + mt7621_pcie_reset_ep_deassert(pcie);
1235 +
1236 + tmp = NULL;
1237 + list_for_each_entry(port, &pcie->ports, list) {
1238 + u32 slot = port->slot;
1239 +
1240 + if (!mt7621_pcie_port_is_linkup(port)) {
1241 + dev_err(dev, "pcie%d no card, disable it (RST & CLK)\n",
1242 + slot);
1243 + mt7621_control_assert(port);
1244 + port->enabled = false;
1245 + num_disabled++;
1246 +
1247 + if (slot == 0) {
1248 + tmp = port;
1249 + continue;
1250 + }
1251 +
1252 + if (slot == 1 && tmp && !tmp->enabled)
1253 + phy_power_off(tmp->phy);
1254 + }
1255 + }
1256 +
1257 + return (num_disabled != PCIE_PORT_CNT) ? 0 : -ENODEV;
1258 +}
1259 +
1260 +static void mt7621_pcie_enable_port(struct mt7621_pcie_port *port)
1261 +{
1262 + struct mt7621_pcie *pcie = port->pcie;
1263 + u32 slot = port->slot;
1264 + u32 val;
1265 +
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);
1270 +
1271 + /* map 2G DDR region */
1272 + pcie_port_write(port, PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
1273 + PCI_BASE_ADDRESS_0);
1274 +
1275 + /* configure class code and revision ID */
1276 + pcie_port_write(port, PCIE_CLASS_CODE | PCIE_REVISION_ID,
1277 + RALINK_PCI_CLASS);
1278 +
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);
1284 +}
1285 +
1286 +static int mt7621_pcie_enable_ports(struct pci_host_bridge *host)
1287 +{
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;
1292 + int err;
1293 +
1294 + entry = resource_list_first_type(&host->windows, IORESOURCE_IO);
1295 + if (!entry) {
1296 + dev_err(dev, "cannot get io resource\n");
1297 + return -EINVAL;
1298 + }
1299 +
1300 + /* Setup MEMWIN and IOWIN */
1301 + pcie_write(pcie, 0xffffffff, RALINK_PCI_MEMBASE);
1302 + pcie_write(pcie, entry->res->start, RALINK_PCI_IOBASE);
1303 +
1304 + list_for_each_entry(port, &pcie->ports, list) {
1305 + if (port->enabled) {
1306 + err = clk_prepare_enable(port->clk);
1307 + if (err) {
1308 + dev_err(dev, "enabling clk pcie%d\n",
1309 + port->slot);
1310 + return err;
1311 + }
1312 +
1313 + mt7621_pcie_enable_port(port);
1314 + dev_info(dev, "PCIE%d enabled\n", port->slot);
1315 + }
1316 + }
1317 +
1318 + return 0;
1319 +}
1320 +
1321 +static int mt7621_pcie_register_host(struct pci_host_bridge *host)
1322 +{
1323 + struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
1324 +
1325 + host->ops = &mt7621_pci_ops;
1326 + host->sysdata = pcie;
1327 + return pci_host_probe(host);
1328 +}
1329 +
1330 +static const struct soc_device_attribute mt7621_pci_quirks_match[] = {
1331 + { .soc_id = "mt7621", .revision = "E2" }
1332 +};
1333 +
1334 +static int mt7621_pci_probe(struct platform_device *pdev)
1335 +{
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;
1341 + int err;
1342 +
1343 + if (!dev->of_node)
1344 + return -ENODEV;
1345 +
1346 + bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
1347 + if (!bridge)
1348 + return -ENOMEM;
1349 +
1350 + pcie = pci_host_bridge_priv(bridge);
1351 + pcie->dev = dev;
1352 + platform_set_drvdata(pdev, pcie);
1353 + INIT_LIST_HEAD(&pcie->ports);
1354 +
1355 + attr = soc_device_match(mt7621_pci_quirks_match);
1356 + if (attr)
1357 + pcie->resets_inverted = true;
1358 +
1359 + err = mt7621_pcie_parse_dt(pcie);
1360 + if (err) {
1361 + dev_err(dev, "parsing DT failed\n");
1362 + return err;
1363 + }
1364 +
1365 + err = mt7621_pcie_init_ports(pcie);
1366 + if (err) {
1367 + dev_err(dev, "nothing connected in virtual bridges\n");
1368 + return 0;
1369 + }
1370 +
1371 + err = mt7621_pcie_enable_ports(bridge);
1372 + if (err) {
1373 + dev_err(dev, "error enabling pcie ports\n");
1374 + goto remove_resets;
1375 + }
1376 +
1377 + err = setup_cm_memory_region(bridge);
1378 + if (err) {
1379 + dev_err(dev, "error setting up iocu mem regions\n");
1380 + goto remove_resets;
1381 + }
1382 +
1383 + return mt7621_pcie_register_host(bridge);
1384 +
1385 +remove_resets:
1386 + list_for_each_entry(port, &pcie->ports, list)
1387 + reset_control_put(port->pcie_rst);
1388 +
1389 + return err;
1390 +}
1391 +
1392 +static int mt7621_pci_remove(struct platform_device *pdev)
1393 +{
1394 + struct mt7621_pcie *pcie = platform_get_drvdata(pdev);
1395 + struct mt7621_pcie_port *port;
1396 +
1397 + list_for_each_entry(port, &pcie->ports, list)
1398 + reset_control_put(port->pcie_rst);
1399 +
1400 + return 0;
1401 +}
1402 +
1403 +static const struct of_device_id mt7621_pci_ids[] = {
1404 + { .compatible = "mediatek,mt7621-pci" },
1405 + {},
1406 +};
1407 +MODULE_DEVICE_TABLE(of, mt7621_pci_ids);
1408 +
1409 +static struct platform_driver mt7621_pci_driver = {
1410 + .probe = mt7621_pci_probe,
1411 + .remove = mt7621_pci_remove,
1412 + .driver = {
1413 + .name = "mt7621-pci",
1414 + .of_match_table = of_match_ptr(mt7621_pci_ids),
1415 + },
1416 +};
1417 +builtin_platform_driver(mt7621_pci_driver);