Oliver Hübers | 7d6bc56 | 2018-07-13 15:41:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sergio Paracuellos | 335bbd9 | 2018-11-04 11:49:32 +0100 | [diff] [blame] | 2 | /* |
| 3 | * BRIEF MODULE DESCRIPTION |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 4 | * PCI init for Ralink RT2880 solution |
| 5 | * |
Sergio Paracuellos | 335bbd9 | 2018-11-04 11:49:32 +0100 | [diff] [blame] | 6 | * Copyright 2007 Ralink Inc. (bruce_chang@ralinktech.com.tw) |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 7 | * |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 8 | * May 2007 Bruce Chang |
| 9 | * Initial Release |
| 10 | * |
| 11 | * May 2009 Bruce Chang |
| 12 | * support RT2880/RT3883 PCIe |
| 13 | * |
| 14 | * May 2011 Bruce Chang |
| 15 | * support RT6855/MT7620 PCIe |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 16 | */ |
| 17 | |
Sergio Paracuellos | cd7d07d | 2018-08-03 10:27:02 +0200 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Sergio Paracuellos | cc4e864 | 2021-05-05 14:17:27 +0200 | [diff] [blame] | 19 | #include <linux/clk.h> |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 20 | #include <linux/delay.h> |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 21 | #include <linux/gpio/consumer.h> |
Sergio Paracuellos | cd7d07d | 2018-08-03 10:27:02 +0200 | [diff] [blame] | 22 | #include <linux/module.h> |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 23 | #include <linux/of.h> |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 24 | #include <linux/of_address.h> |
Sergio Paracuellos | cd7d07d | 2018-08-03 10:27:02 +0200 | [diff] [blame] | 25 | #include <linux/of_pci.h> |
| 26 | #include <linux/of_platform.h> |
| 27 | #include <linux/pci.h> |
Sergio Paracuellos | 61f9bde | 2019-01-04 08:08:22 +0100 | [diff] [blame] | 28 | #include <linux/phy/phy.h> |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 29 | #include <linux/platform_device.h> |
Sergio Paracuellos | cd7d07d | 2018-08-03 10:27:02 +0200 | [diff] [blame] | 30 | #include <linux/reset.h> |
Sergio Paracuellos | b483b4e | 2019-10-06 20:10:32 +0200 | [diff] [blame] | 31 | #include <linux/sys_soc.h> |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 32 | |
Sergio Paracuellos | 2bdd523 | 2021-09-22 07:00:34 +0200 | [diff] [blame] | 33 | /* MediaTek-specific configuration registers */ |
Sergio Paracuellos | a4b2eb9 | 2018-11-04 11:49:51 +0100 | [diff] [blame] | 34 | #define PCIE_FTS_NUM 0x70c |
| 35 | #define PCIE_FTS_NUM_MASK GENMASK(15, 8) |
Sergio Paracuellos | 0ae0cf5 | 2019-06-26 14:43:18 +0200 | [diff] [blame] | 36 | #define PCIE_FTS_NUM_L0(x) (((x) & 0xff) << 8) |
Sergio Paracuellos | a4b2eb9 | 2018-11-04 11:49:51 +0100 | [diff] [blame] | 37 | |
Sergio Paracuellos | a934d90 | 2018-11-04 11:49:48 +0100 | [diff] [blame] | 38 | /* Host-PCI bridge registers */ |
Sergio Paracuellos | 152f389 | 2018-08-03 10:27:03 +0200 | [diff] [blame] | 39 | #define RALINK_PCI_PCICFG_ADDR 0x0000 |
Sergio Paracuellos | 2bdd523 | 2021-09-22 07:00:34 +0200 | [diff] [blame] | 40 | #define RALINK_PCI_PCIMSK_ADDR 0x000c |
Sergio Paracuellos | a934d90 | 2018-11-04 11:49:48 +0100 | [diff] [blame] | 41 | #define RALINK_PCI_CONFIG_ADDR 0x0020 |
| 42 | #define RALINK_PCI_CONFIG_DATA 0x0024 |
| 43 | #define RALINK_PCI_MEMBASE 0x0028 |
Sergio Paracuellos | 2bdd523 | 2021-09-22 07:00:34 +0200 | [diff] [blame] | 44 | #define RALINK_PCI_IOBASE 0x002c |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 45 | |
Sergio Paracuellos | a934d90 | 2018-11-04 11:49:48 +0100 | [diff] [blame] | 46 | /* PCIe RC control registers */ |
Sergio Paracuellos | e38bb17 | 2018-08-03 10:27:01 +0200 | [diff] [blame] | 47 | #define RALINK_PCI_ID 0x0030 |
| 48 | #define RALINK_PCI_CLASS 0x0034 |
| 49 | #define RALINK_PCI_SUBID 0x0038 |
| 50 | #define RALINK_PCI_STATUS 0x0050 |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 51 | |
Sergio Paracuellos | a934d90 | 2018-11-04 11:49:48 +0100 | [diff] [blame] | 52 | /* Some definition values */ |
Sergio Paracuellos | 301c15b | 2018-11-04 11:49:52 +0100 | [diff] [blame] | 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)) |
Sergio Paracuellos | bd1a05b | 2018-11-04 11:49:54 +0100 | [diff] [blame] | 58 | #define PCIE_PORT_LINKUP BIT(0) |
Sergio Paracuellos | 7d761b0 | 2021-08-23 19:08:03 +0200 | [diff] [blame] | 59 | #define PCIE_PORT_CNT 3 |
Sergio Paracuellos | 301c15b | 2018-11-04 11:49:52 +0100 | [diff] [blame] | 60 | |
Sergio Paracuellos | 475fe23 | 2020-03-13 21:09:09 +0100 | [diff] [blame] | 61 | #define PERST_DELAY_MS 100 |
Sergio Paracuellos | 6b76790 | 2018-11-04 11:49:36 +0100 | [diff] [blame] | 62 | |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 63 | /** |
| 64 | * struct mt7621_pcie_port - PCIe port information |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 65 | * @base: I/O mapped register base |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 66 | * @list: port list |
| 67 | * @pcie: pointer to PCIe host info |
Sergio Paracuellos | cc4e864 | 2021-05-05 14:17:27 +0200 | [diff] [blame] | 68 | * @clk: pointer to the port clock gate |
Sergio Paracuellos | 61f9bde | 2019-01-04 08:08:22 +0100 | [diff] [blame] | 69 | * @phy: pointer to PHY control block |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 70 | * @pcie_rst: pointer to port reset control |
Sergio Paracuellos | b27e35f | 2020-03-13 21:09:08 +0100 | [diff] [blame] | 71 | * @gpio_rst: gpio reset |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 72 | * @slot: port slot |
Sergio Paracuellos | a41a1f8 | 2018-11-04 11:49:44 +0100 | [diff] [blame] | 73 | * @enabled: indicates if port is enabled |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 74 | */ |
| 75 | struct mt7621_pcie_port { |
| 76 | void __iomem *base; |
| 77 | struct list_head list; |
| 78 | struct mt7621_pcie *pcie; |
Sergio Paracuellos | cc4e864 | 2021-05-05 14:17:27 +0200 | [diff] [blame] | 79 | struct clk *clk; |
Sergio Paracuellos | 61f9bde | 2019-01-04 08:08:22 +0100 | [diff] [blame] | 80 | struct phy *phy; |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 81 | struct reset_control *pcie_rst; |
Sergio Paracuellos | b27e35f | 2020-03-13 21:09:08 +0100 | [diff] [blame] | 82 | struct gpio_desc *gpio_rst; |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 83 | u32 slot; |
Sergio Paracuellos | a41a1f8 | 2018-11-04 11:49:44 +0100 | [diff] [blame] | 84 | bool enabled; |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /** |
| 88 | * struct mt7621_pcie - PCIe host information |
| 89 | * @base: IO Mapped Register Base |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 90 | * @dev: Pointer to PCIe device |
| 91 | * @ports: pointer to PCIe port information |
Sergio Paracuellos | b483b4e | 2019-10-06 20:10:32 +0200 | [diff] [blame] | 92 | * @resets_inverted: depends on chip revision |
| 93 | * reset lines are inverted. |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 94 | */ |
| 95 | struct mt7621_pcie { |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 96 | struct device *dev; |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 97 | void __iomem *base; |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 98 | struct list_head ports; |
Sergio Paracuellos | b483b4e | 2019-10-06 20:10:32 +0200 | [diff] [blame] | 99 | bool resets_inverted; |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
Sergio Paracuellos | 52ed727 | 2018-08-03 10:26:56 +0200 | [diff] [blame] | 102 | static inline u32 pcie_read(struct mt7621_pcie *pcie, u32 reg) |
| 103 | { |
Sergio Paracuellos | 860bce4 | 2021-06-07 14:01:50 +0200 | [diff] [blame] | 104 | return readl_relaxed(pcie->base + reg); |
Sergio Paracuellos | 52ed727 | 2018-08-03 10:26:56 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static inline void pcie_write(struct mt7621_pcie *pcie, u32 val, u32 reg) |
| 108 | { |
Sergio Paracuellos | 860bce4 | 2021-06-07 14:01:50 +0200 | [diff] [blame] | 109 | writel_relaxed(val, pcie->base + reg); |
Sergio Paracuellos | 52ed727 | 2018-08-03 10:26:56 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Sergio Paracuellos | c8242be | 2018-11-04 11:49:29 +0100 | [diff] [blame] | 112 | static inline u32 pcie_port_read(struct mt7621_pcie_port *port, u32 reg) |
| 113 | { |
Sergio Paracuellos | 860bce4 | 2021-06-07 14:01:50 +0200 | [diff] [blame] | 114 | return readl_relaxed(port->base + reg); |
Sergio Paracuellos | c8242be | 2018-11-04 11:49:29 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static inline void pcie_port_write(struct mt7621_pcie_port *port, |
| 118 | u32 val, u32 reg) |
| 119 | { |
Sergio Paracuellos | 860bce4 | 2021-06-07 14:01:50 +0200 | [diff] [blame] | 120 | writel_relaxed(val, port->base + reg); |
Sergio Paracuellos | c8242be | 2018-11-04 11:49:29 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 123 | static inline u32 mt7621_pcie_get_cfgaddr(unsigned int bus, unsigned int slot, |
Sergio Paracuellos | 2427d17 | 2018-07-09 22:21:04 +0200 | [diff] [blame] | 124 | unsigned int func, unsigned int where) |
| 125 | { |
Sergio Paracuellos | 2bdd523 | 2021-09-22 07:00:34 +0200 | [diff] [blame] | 126 | return (((where & 0xf00) >> 8) << 24) | (bus << 16) | (slot << 11) | |
Sergio Paracuellos | 2427d17 | 2018-07-09 22:21:04 +0200 | [diff] [blame] | 127 | (func << 8) | (where & 0xfc) | 0x80000000; |
| 128 | } |
| 129 | |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 130 | static void __iomem *mt7621_pcie_map_bus(struct pci_bus *bus, |
| 131 | unsigned int devfn, int where) |
| 132 | { |
| 133 | struct mt7621_pcie *pcie = bus->sysdata; |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 134 | u32 address = mt7621_pcie_get_cfgaddr(bus->number, PCI_SLOT(devfn), |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 135 | PCI_FUNC(devfn), where); |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 136 | |
Sergio Paracuellos | 860bce4 | 2021-06-07 14:01:50 +0200 | [diff] [blame] | 137 | writel_relaxed(address, pcie->base + RALINK_PCI_CONFIG_ADDR); |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 138 | |
Sergio Paracuellos | 8594351 | 2018-08-03 10:27:06 +0200 | [diff] [blame] | 139 | return pcie->base + RALINK_PCI_CONFIG_DATA + (where & 3); |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Bjorn Helgaas | 87c7193 | 2022-01-13 09:57:53 -0600 | [diff] [blame] | 142 | static struct pci_ops mt7621_pcie_ops = { |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 143 | .map_bus = mt7621_pcie_map_bus, |
| 144 | .read = pci_generic_config_read, |
| 145 | .write = pci_generic_config_write, |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 146 | }; |
| 147 | |
Sergio Paracuellos | ce3368d | 2018-11-04 11:49:50 +0100 | [diff] [blame] | 148 | static u32 read_config(struct mt7621_pcie *pcie, unsigned int dev, u32 reg) |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 149 | { |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 150 | u32 address = mt7621_pcie_get_cfgaddr(0, dev, 0, reg); |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 151 | |
Sergio Paracuellos | 9f999b4 | 2018-08-03 10:26:57 +0200 | [diff] [blame] | 152 | pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR); |
Sergio Paracuellos | 8594351 | 2018-08-03 10:27:06 +0200 | [diff] [blame] | 153 | return pcie_read(pcie, RALINK_PCI_CONFIG_DATA); |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 154 | } |
| 155 | |
Sergio Paracuellos | ce3368d | 2018-11-04 11:49:50 +0100 | [diff] [blame] | 156 | static void write_config(struct mt7621_pcie *pcie, unsigned int dev, |
| 157 | u32 reg, u32 val) |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 158 | { |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 159 | u32 address = mt7621_pcie_get_cfgaddr(0, dev, 0, reg); |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 160 | |
Sergio Paracuellos | 9f999b4 | 2018-08-03 10:26:57 +0200 | [diff] [blame] | 161 | pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR); |
Sergio Paracuellos | 8594351 | 2018-08-03 10:27:06 +0200 | [diff] [blame] | 162 | pcie_write(pcie, val, RALINK_PCI_CONFIG_DATA); |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 163 | } |
| 164 | |
Sergio Paracuellos | b27e35f | 2020-03-13 21:09:08 +0100 | [diff] [blame] | 165 | static inline void mt7621_rst_gpio_pcie_assert(struct mt7621_pcie_port *port) |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 166 | { |
Sergio Paracuellos | b27e35f | 2020-03-13 21:09:08 +0100 | [diff] [blame] | 167 | if (port->gpio_rst) |
| 168 | gpiod_set_value(port->gpio_rst, 1); |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Sergio Paracuellos | b27e35f | 2020-03-13 21:09:08 +0100 | [diff] [blame] | 171 | static inline void mt7621_rst_gpio_pcie_deassert(struct mt7621_pcie_port *port) |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 172 | { |
Sergio Paracuellos | b27e35f | 2020-03-13 21:09:08 +0100 | [diff] [blame] | 173 | if (port->gpio_rst) |
| 174 | gpiod_set_value(port->gpio_rst, 0); |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static inline bool mt7621_pcie_port_is_linkup(struct mt7621_pcie_port *port) |
| 178 | { |
| 179 | return (pcie_port_read(port, RALINK_PCI_STATUS) & PCIE_PORT_LINKUP) != 0; |
| 180 | } |
| 181 | |
Sergio Paracuellos | e51844b | 2018-11-24 18:54:54 +0100 | [diff] [blame] | 182 | static inline void mt7621_control_assert(struct mt7621_pcie_port *port) |
| 183 | { |
Sergio Paracuellos | b483b4e | 2019-10-06 20:10:32 +0200 | [diff] [blame] | 184 | struct mt7621_pcie *pcie = port->pcie; |
Sergio Paracuellos | e51844b | 2018-11-24 18:54:54 +0100 | [diff] [blame] | 185 | |
Sergio Paracuellos | b483b4e | 2019-10-06 20:10:32 +0200 | [diff] [blame] | 186 | if (pcie->resets_inverted) |
Sergio Paracuellos | e51844b | 2018-11-24 18:54:54 +0100 | [diff] [blame] | 187 | reset_control_assert(port->pcie_rst); |
| 188 | else |
| 189 | reset_control_deassert(port->pcie_rst); |
| 190 | } |
| 191 | |
| 192 | static inline void mt7621_control_deassert(struct mt7621_pcie_port *port) |
| 193 | { |
Sergio Paracuellos | b483b4e | 2019-10-06 20:10:32 +0200 | [diff] [blame] | 194 | struct mt7621_pcie *pcie = port->pcie; |
Sergio Paracuellos | e51844b | 2018-11-24 18:54:54 +0100 | [diff] [blame] | 195 | |
Sergio Paracuellos | b483b4e | 2019-10-06 20:10:32 +0200 | [diff] [blame] | 196 | if (pcie->resets_inverted) |
Sergio Paracuellos | e51844b | 2018-11-24 18:54:54 +0100 | [diff] [blame] | 197 | reset_control_deassert(port->pcie_rst); |
| 198 | else |
| 199 | reset_control_assert(port->pcie_rst); |
| 200 | } |
| 201 | |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 202 | static int mt7621_pcie_parse_port(struct mt7621_pcie *pcie, |
Sergio Paracuellos | 2d3d288 | 2021-06-07 14:01:52 +0200 | [diff] [blame] | 203 | struct device_node *node, |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 204 | int slot) |
| 205 | { |
| 206 | struct mt7621_pcie_port *port; |
| 207 | struct device *dev = pcie->dev; |
Sergio Paracuellos | fab6710 | 2020-04-13 07:59:42 +0200 | [diff] [blame] | 208 | struct platform_device *pdev = to_platform_device(dev); |
Sergio Paracuellos | 61f9bde | 2019-01-04 08:08:22 +0100 | [diff] [blame] | 209 | char name[10]; |
Sergio Paracuellos | 2d3d288 | 2021-06-07 14:01:52 +0200 | [diff] [blame] | 210 | int err; |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 211 | |
| 212 | port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL); |
| 213 | if (!port) |
| 214 | return -ENOMEM; |
| 215 | |
Sergio Paracuellos | 108b2f2 | 2020-11-23 10:36:36 +0100 | [diff] [blame] | 216 | port->base = devm_platform_ioremap_resource(pdev, slot + 1); |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 217 | if (IS_ERR(port->base)) |
| 218 | return PTR_ERR(port->base); |
| 219 | |
Sergio Paracuellos | 2d3d288 | 2021-06-07 14:01:52 +0200 | [diff] [blame] | 220 | port->clk = devm_get_clk_from_child(dev, node, NULL); |
Sergio Paracuellos | cc4e864 | 2021-05-05 14:17:27 +0200 | [diff] [blame] | 221 | if (IS_ERR(port->clk)) { |
| 222 | dev_err(dev, "failed to get pcie%d clock\n", slot); |
| 223 | return PTR_ERR(port->clk); |
| 224 | } |
| 225 | |
Sergio Paracuellos | 2d3d288 | 2021-06-07 14:01:52 +0200 | [diff] [blame] | 226 | port->pcie_rst = of_reset_control_get_exclusive(node, NULL); |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 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); |
| 230 | } |
| 231 | |
Sergio Paracuellos | 61f9bde | 2019-01-04 08:08:22 +0100 | [diff] [blame] | 232 | snprintf(name, sizeof(name), "pcie-phy%d", slot); |
Sergio Paracuellos | 2d3d288 | 2021-06-07 14:01:52 +0200 | [diff] [blame] | 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); |
| 237 | goto remove_reset; |
| 238 | } |
Sergio Paracuellos | 61f9bde | 2019-01-04 08:08:22 +0100 | [diff] [blame] | 239 | |
Sergio Paracuellos | b27e35f | 2020-03-13 21:09:08 +0100 | [diff] [blame] | 240 | port->gpio_rst = devm_gpiod_get_index_optional(dev, "reset", slot, |
| 241 | GPIOD_OUT_LOW); |
Sergio Paracuellos | 825c6f4 | 2020-03-20 12:01:23 +0100 | [diff] [blame] | 242 | if (IS_ERR(port->gpio_rst)) { |
Sergio Paracuellos | 2bdd523 | 2021-09-22 07:00:34 +0200 | [diff] [blame] | 243 | dev_err(dev, "failed to get GPIO for PCIe%d\n", slot); |
Sergio Paracuellos | 2d3d288 | 2021-06-07 14:01:52 +0200 | [diff] [blame] | 244 | err = PTR_ERR(port->gpio_rst); |
| 245 | goto remove_reset; |
Sergio Paracuellos | 825c6f4 | 2020-03-20 12:01:23 +0100 | [diff] [blame] | 246 | } |
Sergio Paracuellos | b27e35f | 2020-03-13 21:09:08 +0100 | [diff] [blame] | 247 | |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 248 | port->slot = slot; |
| 249 | port->pcie = pcie; |
| 250 | |
| 251 | INIT_LIST_HEAD(&port->list); |
| 252 | list_add_tail(&port->list, &pcie->ports); |
| 253 | |
| 254 | return 0; |
Sergio Paracuellos | 2d3d288 | 2021-06-07 14:01:52 +0200 | [diff] [blame] | 255 | |
| 256 | remove_reset: |
| 257 | reset_control_put(port->pcie_rst); |
| 258 | return err; |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 259 | } |
| 260 | |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 261 | static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie) |
| 262 | { |
| 263 | struct device *dev = pcie->dev; |
Sergio Paracuellos | 108b2f2 | 2020-11-23 10:36:36 +0100 | [diff] [blame] | 264 | struct platform_device *pdev = to_platform_device(dev); |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 265 | struct device_node *node = dev->of_node, *child; |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 266 | int err; |
| 267 | |
Sergio Paracuellos | 108b2f2 | 2020-11-23 10:36:36 +0100 | [diff] [blame] | 268 | pcie->base = devm_platform_ioremap_resource(pdev, 0); |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 269 | if (IS_ERR(pcie->base)) |
| 270 | return PTR_ERR(pcie->base); |
| 271 | |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 272 | for_each_available_child_of_node(node, child) { |
| 273 | int slot; |
| 274 | |
| 275 | err = of_pci_get_devfn(child); |
| 276 | if (err < 0) { |
Nishka Dasgupta | 2125a44 | 2019-07-16 11:29:44 +0530 | [diff] [blame] | 277 | of_node_put(child); |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 278 | dev_err(dev, "failed to parse devfn: %d\n", err); |
| 279 | return err; |
| 280 | } |
| 281 | |
| 282 | slot = PCI_SLOT(err); |
| 283 | |
Sergio Paracuellos | 2d3d288 | 2021-06-07 14:01:52 +0200 | [diff] [blame] | 284 | err = mt7621_pcie_parse_port(pcie, child, slot); |
Nishka Dasgupta | 2125a44 | 2019-07-16 11:29:44 +0530 | [diff] [blame] | 285 | if (err) { |
| 286 | of_node_put(child); |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 287 | return err; |
Nishka Dasgupta | 2125a44 | 2019-07-16 11:29:44 +0530 | [diff] [blame] | 288 | } |
Sergio Paracuellos | ad9c87e | 2018-11-04 11:49:27 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
Sergio Paracuellos | 4fdf3ab | 2018-11-04 11:49:39 +0100 | [diff] [blame] | 294 | static int mt7621_pcie_init_port(struct mt7621_pcie_port *port) |
Sergio Paracuellos | 745eeea | 2018-11-04 11:49:30 +0100 | [diff] [blame] | 295 | { |
| 296 | struct mt7621_pcie *pcie = port->pcie; |
| 297 | struct device *dev = pcie->dev; |
| 298 | u32 slot = port->slot; |
Sergio Paracuellos | 61f9bde | 2019-01-04 08:08:22 +0100 | [diff] [blame] | 299 | int err; |
Sergio Paracuellos | 745eeea | 2018-11-04 11:49:30 +0100 | [diff] [blame] | 300 | |
Sergio Paracuellos | 61f9bde | 2019-01-04 08:08:22 +0100 | [diff] [blame] | 301 | err = phy_init(port->phy); |
| 302 | if (err) { |
| 303 | dev_err(dev, "failed to initialize port%d phy\n", slot); |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 304 | return err; |
Sergio Paracuellos | 61f9bde | 2019-01-04 08:08:22 +0100 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | err = phy_power_on(port->phy); |
| 308 | if (err) { |
| 309 | dev_err(dev, "failed to power on port%d phy\n", slot); |
Sergio Paracuellos | cdf6f83 | 2019-06-21 08:15:15 +0200 | [diff] [blame] | 310 | phy_exit(port->phy); |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 311 | return err; |
Sergio Paracuellos | 745eeea | 2018-11-04 11:49:30 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Sergio Paracuellos | 61f9bde | 2019-01-04 08:08:22 +0100 | [diff] [blame] | 314 | port->enabled = true; |
Sergio Paracuellos | 3beb6da | 2018-11-04 11:49:37 +0100 | [diff] [blame] | 315 | |
Sergio Paracuellos | 745eeea | 2018-11-04 11:49:30 +0100 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
Sergio Paracuellos | b27e35f | 2020-03-13 21:09:08 +0100 | [diff] [blame] | 319 | static void mt7621_pcie_reset_assert(struct mt7621_pcie *pcie) |
| 320 | { |
| 321 | struct mt7621_pcie_port *port; |
| 322 | |
| 323 | list_for_each_entry(port, &pcie->ports, list) { |
| 324 | /* PCIe RC reset assert */ |
| 325 | mt7621_control_assert(port); |
| 326 | |
| 327 | /* PCIe EP reset assert */ |
| 328 | mt7621_rst_gpio_pcie_assert(port); |
| 329 | } |
| 330 | |
Sergio Paracuellos | 4ab4ca2 | 2021-05-05 14:17:31 +0200 | [diff] [blame] | 331 | msleep(PERST_DELAY_MS); |
Sergio Paracuellos | b27e35f | 2020-03-13 21:09:08 +0100 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static void mt7621_pcie_reset_rc_deassert(struct mt7621_pcie *pcie) |
| 335 | { |
| 336 | struct mt7621_pcie_port *port; |
| 337 | |
| 338 | list_for_each_entry(port, &pcie->ports, list) |
| 339 | mt7621_control_deassert(port); |
| 340 | } |
| 341 | |
| 342 | static void mt7621_pcie_reset_ep_deassert(struct mt7621_pcie *pcie) |
| 343 | { |
| 344 | struct mt7621_pcie_port *port; |
| 345 | |
| 346 | list_for_each_entry(port, &pcie->ports, list) |
| 347 | mt7621_rst_gpio_pcie_deassert(port); |
| 348 | |
Sergio Paracuellos | 4ab4ca2 | 2021-05-05 14:17:31 +0200 | [diff] [blame] | 349 | msleep(PERST_DELAY_MS); |
Sergio Paracuellos | b27e35f | 2020-03-13 21:09:08 +0100 | [diff] [blame] | 350 | } |
| 351 | |
Sergio Paracuellos | 7d761b0 | 2021-08-23 19:08:03 +0200 | [diff] [blame] | 352 | static int mt7621_pcie_init_ports(struct mt7621_pcie *pcie) |
Sergio Paracuellos | cd910f1 | 2018-11-04 11:49:46 +0100 | [diff] [blame] | 353 | { |
| 354 | struct device *dev = pcie->dev; |
| 355 | struct mt7621_pcie_port *port, *tmp; |
Sergio Paracuellos | 7d761b0 | 2021-08-23 19:08:03 +0200 | [diff] [blame] | 356 | u8 num_disabled = 0; |
Sergio Paracuellos | cd910f1 | 2018-11-04 11:49:46 +0100 | [diff] [blame] | 357 | int err; |
| 358 | |
Sergio Paracuellos | b27e35f | 2020-03-13 21:09:08 +0100 | [diff] [blame] | 359 | mt7621_pcie_reset_assert(pcie); |
| 360 | mt7621_pcie_reset_rc_deassert(pcie); |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 361 | |
Sergio Paracuellos | cd910f1 | 2018-11-04 11:49:46 +0100 | [diff] [blame] | 362 | list_for_each_entry_safe(port, tmp, &pcie->ports, list) { |
| 363 | u32 slot = port->slot; |
| 364 | |
Sergio Paracuellos | bf516f4 | 2020-03-20 12:01:21 +0100 | [diff] [blame] | 365 | if (slot == 1) { |
| 366 | port->enabled = true; |
| 367 | continue; |
| 368 | } |
| 369 | |
Sergio Paracuellos | cd910f1 | 2018-11-04 11:49:46 +0100 | [diff] [blame] | 370 | err = mt7621_pcie_init_port(port); |
| 371 | if (err) { |
Sergio Paracuellos | 2bdd523 | 2021-09-22 07:00:34 +0200 | [diff] [blame] | 372 | dev_err(dev, "initializing port %d failed\n", slot); |
Sergio Paracuellos | cd910f1 | 2018-11-04 11:49:46 +0100 | [diff] [blame] | 373 | list_del(&port->list); |
| 374 | } |
| 375 | } |
Sergio Paracuellos | b8d97d43 | 2018-11-04 11:49:58 +0100 | [diff] [blame] | 376 | |
Sergio Paracuellos | b27e35f | 2020-03-13 21:09:08 +0100 | [diff] [blame] | 377 | mt7621_pcie_reset_ep_deassert(pcie); |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 378 | |
Sergio Paracuellos | 5fcded5 | 2020-04-09 13:16:52 +0200 | [diff] [blame] | 379 | tmp = NULL; |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 380 | list_for_each_entry(port, &pcie->ports, list) { |
| 381 | u32 slot = port->slot; |
| 382 | |
| 383 | if (!mt7621_pcie_port_is_linkup(port)) { |
| 384 | dev_err(dev, "pcie%d no card, disable it (RST & CLK)\n", |
| 385 | slot); |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 386 | mt7621_control_assert(port); |
| 387 | port->enabled = false; |
Sergio Paracuellos | 7d761b0 | 2021-08-23 19:08:03 +0200 | [diff] [blame] | 388 | num_disabled++; |
Sergio Paracuellos | 5fcded5 | 2020-04-09 13:16:52 +0200 | [diff] [blame] | 389 | |
| 390 | if (slot == 0) { |
| 391 | tmp = port; |
| 392 | continue; |
| 393 | } |
| 394 | |
| 395 | if (slot == 1 && tmp && !tmp->enabled) |
| 396 | phy_power_off(tmp->phy); |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 397 | } |
| 398 | } |
Sergio Paracuellos | 7d761b0 | 2021-08-23 19:08:03 +0200 | [diff] [blame] | 399 | |
| 400 | return (num_disabled != PCIE_PORT_CNT) ? 0 : -ENODEV; |
Sergio Paracuellos | cd910f1 | 2018-11-04 11:49:46 +0100 | [diff] [blame] | 401 | } |
| 402 | |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 403 | static void mt7621_pcie_enable_port(struct mt7621_pcie_port *port) |
Sergio Paracuellos | 802a2f7 | 2018-11-04 11:49:57 +0100 | [diff] [blame] | 404 | { |
| 405 | struct mt7621_pcie *pcie = port->pcie; |
| 406 | u32 slot = port->slot; |
Sergio Paracuellos | 802a2f7 | 2018-11-04 11:49:57 +0100 | [diff] [blame] | 407 | u32 val; |
Sergio Paracuellos | 802a2f7 | 2018-11-04 11:49:57 +0100 | [diff] [blame] | 408 | |
| 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); |
| 413 | |
| 414 | /* map 2G DDR region */ |
Sergio Paracuellos | 25203e3 | 2021-06-07 14:01:53 +0200 | [diff] [blame] | 415 | pcie_port_write(port, PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE, |
| 416 | PCI_BASE_ADDRESS_0); |
Sergio Paracuellos | 802a2f7 | 2018-11-04 11:49:57 +0100 | [diff] [blame] | 417 | |
| 418 | /* configure class code and revision ID */ |
Sergio Paracuellos | 25203e3 | 2021-06-07 14:01:53 +0200 | [diff] [blame] | 419 | pcie_port_write(port, PCIE_CLASS_CODE | PCIE_REVISION_ID, |
| 420 | RALINK_PCI_CLASS); |
Sergio Paracuellos | a3bb1d0 | 2021-06-07 14:01:48 +0200 | [diff] [blame] | 421 | |
| 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); |
Sergio Paracuellos | 802a2f7 | 2018-11-04 11:49:57 +0100 | [diff] [blame] | 427 | } |
| 428 | |
Sergio Paracuellos | b15606e | 2021-06-14 12:06:16 +0200 | [diff] [blame] | 429 | static int mt7621_pcie_enable_ports(struct pci_host_bridge *host) |
Sergio Paracuellos | a41a1f8 | 2018-11-04 11:49:44 +0100 | [diff] [blame] | 430 | { |
Sergio Paracuellos | b15606e | 2021-06-14 12:06:16 +0200 | [diff] [blame] | 431 | struct mt7621_pcie *pcie = pci_host_bridge_priv(host); |
Sergio Paracuellos | a41a1f8 | 2018-11-04 11:49:44 +0100 | [diff] [blame] | 432 | struct device *dev = pcie->dev; |
| 433 | struct mt7621_pcie_port *port; |
Sergio Paracuellos | b15606e | 2021-06-14 12:06:16 +0200 | [diff] [blame] | 434 | struct resource_entry *entry; |
Sergio Paracuellos | cc4e864 | 2021-05-05 14:17:27 +0200 | [diff] [blame] | 435 | int err; |
Sergio Paracuellos | a41a1f8 | 2018-11-04 11:49:44 +0100 | [diff] [blame] | 436 | |
Sergio Paracuellos | b15606e | 2021-06-14 12:06:16 +0200 | [diff] [blame] | 437 | entry = resource_list_first_type(&host->windows, IORESOURCE_IO); |
| 438 | if (!entry) { |
Sergio Paracuellos | 2bdd523 | 2021-09-22 07:00:34 +0200 | [diff] [blame] | 439 | dev_err(dev, "cannot get io resource\n"); |
Sergio Paracuellos | b15606e | 2021-06-14 12:06:16 +0200 | [diff] [blame] | 440 | return -EINVAL; |
| 441 | } |
| 442 | |
Sergio Paracuellos | 09dd629 | 2020-03-18 10:44:45 +0100 | [diff] [blame] | 443 | /* Setup MEMWIN and IOWIN */ |
| 444 | pcie_write(pcie, 0xffffffff, RALINK_PCI_MEMBASE); |
Sergio Paracuellos | e0b9138 | 2021-09-25 22:32:24 +0200 | [diff] [blame] | 445 | pcie_write(pcie, entry->res->start - entry->offset, RALINK_PCI_IOBASE); |
Sergio Paracuellos | 09dd629 | 2020-03-18 10:44:45 +0100 | [diff] [blame] | 446 | |
Sergio Paracuellos | a41a1f8 | 2018-11-04 11:49:44 +0100 | [diff] [blame] | 447 | list_for_each_entry(port, &pcie->ports, list) { |
Sergio Paracuellos | a41a1f8 | 2018-11-04 11:49:44 +0100 | [diff] [blame] | 448 | if (port->enabled) { |
Sergio Paracuellos | cc4e864 | 2021-05-05 14:17:27 +0200 | [diff] [blame] | 449 | err = clk_prepare_enable(port->clk); |
| 450 | if (err) { |
Sergio Paracuellos | a3bb1d0 | 2021-06-07 14:01:48 +0200 | [diff] [blame] | 451 | dev_err(dev, "enabling clk pcie%d\n", |
| 452 | port->slot); |
Sergio Paracuellos | cc4e864 | 2021-05-05 14:17:27 +0200 | [diff] [blame] | 453 | return err; |
| 454 | } |
| 455 | |
Sergio Paracuellos | 07420a0 | 2019-06-19 09:44:56 +0200 | [diff] [blame] | 456 | mt7621_pcie_enable_port(port); |
Sergio Paracuellos | 7846a98 | 2020-03-20 12:01:22 +0100 | [diff] [blame] | 457 | dev_info(dev, "PCIE%d enabled\n", port->slot); |
Sergio Paracuellos | a41a1f8 | 2018-11-04 11:49:44 +0100 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Sergio Paracuellos | cc4e864 | 2021-05-05 14:17:27 +0200 | [diff] [blame] | 461 | return 0; |
Sergio Paracuellos | a41a1f8 | 2018-11-04 11:49:44 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Sergio Paracuellos | 2fc0898 | 2020-11-23 10:36:35 +0100 | [diff] [blame] | 464 | static int mt7621_pcie_register_host(struct pci_host_bridge *host) |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 465 | { |
| 466 | struct mt7621_pcie *pcie = pci_host_bridge_priv(host); |
| 467 | |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 468 | host->ops = &mt7621_pcie_ops; |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 469 | host->sysdata = pcie; |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 470 | return pci_host_probe(host); |
| 471 | } |
| 472 | |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 473 | static const struct soc_device_attribute mt7621_pcie_quirks_match[] = { |
Sergio Paracuellos | b483b4e | 2019-10-06 20:10:32 +0200 | [diff] [blame] | 474 | { .soc_id = "mt7621", .revision = "E2" } |
| 475 | }; |
| 476 | |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 477 | static int mt7621_pcie_probe(struct platform_device *pdev) |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 478 | { |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 479 | struct device *dev = &pdev->dev; |
Sergio Paracuellos | b483b4e | 2019-10-06 20:10:32 +0200 | [diff] [blame] | 480 | const struct soc_device_attribute *attr; |
Sergio Paracuellos | 2d3d288 | 2021-06-07 14:01:52 +0200 | [diff] [blame] | 481 | struct mt7621_pcie_port *port; |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 482 | struct mt7621_pcie *pcie; |
| 483 | struct pci_host_bridge *bridge; |
| 484 | int err; |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 485 | |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 486 | if (!dev->of_node) |
| 487 | return -ENODEV; |
| 488 | |
| 489 | bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie)); |
| 490 | if (!bridge) |
Sergio Paracuellos | a80775d | 2018-11-04 11:49:28 +0100 | [diff] [blame] | 491 | return -ENOMEM; |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 492 | |
| 493 | pcie = pci_host_bridge_priv(bridge); |
| 494 | pcie->dev = dev; |
| 495 | platform_set_drvdata(pdev, pcie); |
| 496 | INIT_LIST_HEAD(&pcie->ports); |
| 497 | |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 498 | attr = soc_device_match(mt7621_pcie_quirks_match); |
Sergio Paracuellos | b483b4e | 2019-10-06 20:10:32 +0200 | [diff] [blame] | 499 | if (attr) |
| 500 | pcie->resets_inverted = true; |
| 501 | |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 502 | err = mt7621_pcie_parse_dt(pcie); |
| 503 | if (err) { |
Sergio Paracuellos | 2bdd523 | 2021-09-22 07:00:34 +0200 | [diff] [blame] | 504 | dev_err(dev, "parsing DT failed\n"); |
Sergio Paracuellos | 8571c62 | 2018-08-03 10:26:54 +0200 | [diff] [blame] | 505 | return err; |
| 506 | } |
| 507 | |
Sergio Paracuellos | 7d761b0 | 2021-08-23 19:08:03 +0200 | [diff] [blame] | 508 | err = mt7621_pcie_init_ports(pcie); |
| 509 | if (err) { |
Sergio Paracuellos | 2bdd523 | 2021-09-22 07:00:34 +0200 | [diff] [blame] | 510 | dev_err(dev, "nothing connected in virtual bridges\n"); |
Sergio Paracuellos | 7d761b0 | 2021-08-23 19:08:03 +0200 | [diff] [blame] | 511 | return 0; |
| 512 | } |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 513 | |
Sergio Paracuellos | b15606e | 2021-06-14 12:06:16 +0200 | [diff] [blame] | 514 | err = mt7621_pcie_enable_ports(bridge); |
Sergio Paracuellos | cc4e864 | 2021-05-05 14:17:27 +0200 | [diff] [blame] | 515 | if (err) { |
Sergio Paracuellos | 2bdd523 | 2021-09-22 07:00:34 +0200 | [diff] [blame] | 516 | dev_err(dev, "error enabling pcie ports\n"); |
Sergio Paracuellos | 2d3d288 | 2021-06-07 14:01:52 +0200 | [diff] [blame] | 517 | goto remove_resets; |
Sergio Paracuellos | cc4e864 | 2021-05-05 14:17:27 +0200 | [diff] [blame] | 518 | } |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 519 | |
Sergio Paracuellos | 35d96b8 | 2021-05-05 14:17:32 +0200 | [diff] [blame] | 520 | return mt7621_pcie_register_host(bridge); |
Sergio Paracuellos | 2d3d288 | 2021-06-07 14:01:52 +0200 | [diff] [blame] | 521 | |
| 522 | remove_resets: |
| 523 | list_for_each_entry(port, &pcie->ports, list) |
| 524 | reset_control_put(port->pcie_rst); |
| 525 | |
| 526 | return err; |
| 527 | } |
| 528 | |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 529 | static int mt7621_pcie_remove(struct platform_device *pdev) |
Sergio Paracuellos | 2d3d288 | 2021-06-07 14:01:52 +0200 | [diff] [blame] | 530 | { |
| 531 | struct mt7621_pcie *pcie = platform_get_drvdata(pdev); |
| 532 | struct mt7621_pcie_port *port; |
| 533 | |
| 534 | list_for_each_entry(port, &pcie->ports, list) |
| 535 | reset_control_put(port->pcie_rst); |
| 536 | |
| 537 | return 0; |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 538 | } |
| 539 | |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 540 | static const struct of_device_id mt7621_pcie_ids[] = { |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 541 | { .compatible = "mediatek,mt7621-pci" }, |
| 542 | {}, |
| 543 | }; |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 544 | MODULE_DEVICE_TABLE(of, mt7621_pcie_ids); |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 545 | |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 546 | static struct platform_driver mt7621_pcie_driver = { |
| 547 | .probe = mt7621_pcie_probe, |
| 548 | .remove = mt7621_pcie_remove, |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 549 | .driver = { |
| 550 | .name = "mt7621-pci", |
Sergio Paracuellos | 4b77e4a | 2022-01-24 12:30:02 +0100 | [diff] [blame] | 551 | .of_match_table = mt7621_pcie_ids, |
John Crispin | 03f152e | 2018-03-15 07:22:35 +1100 | [diff] [blame] | 552 | }, |
| 553 | }; |
Bjorn Helgaas | 4793895 | 2021-12-22 19:10:48 -0600 | [diff] [blame] | 554 | builtin_platform_driver(mt7621_pcie_driver); |
Sergio Paracuellos | e4b1cd0 | 2021-12-07 11:49:23 +0100 | [diff] [blame] | 555 | |
| 556 | MODULE_LICENSE("GPL v2"); |