blob: 33eb37a2225c14c7e9be5f53c45b9ca7cb96bef1 [file] [log] [blame]
Oliver Hübers7d6bc562018-07-13 15:41:06 +02001// SPDX-License-Identifier: GPL-2.0+
Sergio Paracuellos335bbd92018-11-04 11:49:32 +01002/*
3 * BRIEF MODULE DESCRIPTION
John Crispin03f152e2018-03-15 07:22:35 +11004 * PCI init for Ralink RT2880 solution
5 *
Sergio Paracuellos335bbd92018-11-04 11:49:32 +01006 * Copyright 2007 Ralink Inc. (bruce_chang@ralinktech.com.tw)
John Crispin03f152e2018-03-15 07:22:35 +11007 *
John Crispin03f152e2018-03-15 07:22:35 +11008 * 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 Crispin03f152e2018-03-15 07:22:35 +110016 */
17
Sergio Paracuelloscd7d07d2018-08-03 10:27:02 +020018#include <linux/bitops.h>
Sergio Paracuelloscc4e8642021-05-05 14:17:27 +020019#include <linux/clk.h>
John Crispin03f152e2018-03-15 07:22:35 +110020#include <linux/delay.h>
Sergio Paracuellos07420a02019-06-19 09:44:56 +020021#include <linux/gpio/consumer.h>
Sergio Paracuelloscd7d07d2018-08-03 10:27:02 +020022#include <linux/module.h>
John Crispin03f152e2018-03-15 07:22:35 +110023#include <linux/of.h>
Sergio Paracuellos8571c622018-08-03 10:26:54 +020024#include <linux/of_address.h>
Sergio Paracuelloscd7d07d2018-08-03 10:27:02 +020025#include <linux/of_pci.h>
26#include <linux/of_platform.h>
27#include <linux/pci.h>
Sergio Paracuellos61f9bde2019-01-04 08:08:22 +010028#include <linux/phy/phy.h>
John Crispin03f152e2018-03-15 07:22:35 +110029#include <linux/platform_device.h>
Sergio Paracuelloscd7d07d2018-08-03 10:27:02 +020030#include <linux/reset.h>
Sergio Paracuellosb483b4e2019-10-06 20:10:32 +020031#include <linux/sys_soc.h>
Sergio Paracuellos8571c622018-08-03 10:26:54 +020032
Sergio Paracuellos2bdd5232021-09-22 07:00:34 +020033/* MediaTek-specific configuration registers */
Sergio Paracuellosa4b2eb92018-11-04 11:49:51 +010034#define PCIE_FTS_NUM 0x70c
35#define PCIE_FTS_NUM_MASK GENMASK(15, 8)
Sergio Paracuellos0ae0cf52019-06-26 14:43:18 +020036#define PCIE_FTS_NUM_L0(x) (((x) & 0xff) << 8)
Sergio Paracuellosa4b2eb92018-11-04 11:49:51 +010037
Sergio Paracuellosa934d902018-11-04 11:49:48 +010038/* Host-PCI bridge registers */
Sergio Paracuellos152f3892018-08-03 10:27:03 +020039#define RALINK_PCI_PCICFG_ADDR 0x0000
Sergio Paracuellos2bdd5232021-09-22 07:00:34 +020040#define RALINK_PCI_PCIMSK_ADDR 0x000c
Sergio Paracuellosa934d902018-11-04 11:49:48 +010041#define RALINK_PCI_CONFIG_ADDR 0x0020
42#define RALINK_PCI_CONFIG_DATA 0x0024
43#define RALINK_PCI_MEMBASE 0x0028
Sergio Paracuellos2bdd5232021-09-22 07:00:34 +020044#define RALINK_PCI_IOBASE 0x002c
John Crispin03f152e2018-03-15 07:22:35 +110045
Sergio Paracuellosa934d902018-11-04 11:49:48 +010046/* PCIe RC control registers */
Sergio Paracuellose38bb172018-08-03 10:27:01 +020047#define RALINK_PCI_ID 0x0030
48#define RALINK_PCI_CLASS 0x0034
49#define RALINK_PCI_SUBID 0x0038
50#define RALINK_PCI_STATUS 0x0050
John Crispin03f152e2018-03-15 07:22:35 +110051
Sergio Paracuellosa934d902018-11-04 11:49:48 +010052/* Some definition values */
Sergio Paracuellos301c15b2018-11-04 11:49:52 +010053#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 Paracuellosbd1a05b2018-11-04 11:49:54 +010058#define PCIE_PORT_LINKUP BIT(0)
Sergio Paracuellos7d761b02021-08-23 19:08:03 +020059#define PCIE_PORT_CNT 3
Sergio Paracuellos301c15b2018-11-04 11:49:52 +010060
Sergio Paracuellos475fe232020-03-13 21:09:09 +010061#define PERST_DELAY_MS 100
Sergio Paracuellos6b767902018-11-04 11:49:36 +010062
Sergio Paracuellos8571c622018-08-03 10:26:54 +020063/**
64 * struct mt7621_pcie_port - PCIe port information
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +010065 * @base: I/O mapped register base
Sergio Paracuellos8571c622018-08-03 10:26:54 +020066 * @list: port list
67 * @pcie: pointer to PCIe host info
Sergio Paracuelloscc4e8642021-05-05 14:17:27 +020068 * @clk: pointer to the port clock gate
Sergio Paracuellos61f9bde2019-01-04 08:08:22 +010069 * @phy: pointer to PHY control block
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +010070 * @pcie_rst: pointer to port reset control
Sergio Paracuellosb27e35f2020-03-13 21:09:08 +010071 * @gpio_rst: gpio reset
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +010072 * @slot: port slot
Sergio Paracuellosa41a1f82018-11-04 11:49:44 +010073 * @enabled: indicates if port is enabled
Sergio Paracuellos8571c622018-08-03 10:26:54 +020074 */
75struct mt7621_pcie_port {
76 void __iomem *base;
77 struct list_head list;
78 struct mt7621_pcie *pcie;
Sergio Paracuelloscc4e8642021-05-05 14:17:27 +020079 struct clk *clk;
Sergio Paracuellos61f9bde2019-01-04 08:08:22 +010080 struct phy *phy;
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +010081 struct reset_control *pcie_rst;
Sergio Paracuellosb27e35f2020-03-13 21:09:08 +010082 struct gpio_desc *gpio_rst;
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +010083 u32 slot;
Sergio Paracuellosa41a1f82018-11-04 11:49:44 +010084 bool enabled;
Sergio Paracuellos8571c622018-08-03 10:26:54 +020085};
86
87/**
88 * struct mt7621_pcie - PCIe host information
89 * @base: IO Mapped Register Base
Sergio Paracuellos8571c622018-08-03 10:26:54 +020090 * @dev: Pointer to PCIe device
91 * @ports: pointer to PCIe port information
Sergio Paracuellosb483b4e2019-10-06 20:10:32 +020092 * @resets_inverted: depends on chip revision
93 * reset lines are inverted.
Sergio Paracuellos8571c622018-08-03 10:26:54 +020094 */
95struct mt7621_pcie {
Sergio Paracuellos8571c622018-08-03 10:26:54 +020096 struct device *dev;
Bjorn Helgaas47938952021-12-22 19:10:48 -060097 void __iomem *base;
Sergio Paracuellos8571c622018-08-03 10:26:54 +020098 struct list_head ports;
Sergio Paracuellosb483b4e2019-10-06 20:10:32 +020099 bool resets_inverted;
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200100};
101
Sergio Paracuellos52ed7272018-08-03 10:26:56 +0200102static inline u32 pcie_read(struct mt7621_pcie *pcie, u32 reg)
103{
Sergio Paracuellos860bce42021-06-07 14:01:50 +0200104 return readl_relaxed(pcie->base + reg);
Sergio Paracuellos52ed7272018-08-03 10:26:56 +0200105}
106
107static inline void pcie_write(struct mt7621_pcie *pcie, u32 val, u32 reg)
108{
Sergio Paracuellos860bce42021-06-07 14:01:50 +0200109 writel_relaxed(val, pcie->base + reg);
Sergio Paracuellos52ed7272018-08-03 10:26:56 +0200110}
111
Sergio Paracuellosc8242be2018-11-04 11:49:29 +0100112static inline u32 pcie_port_read(struct mt7621_pcie_port *port, u32 reg)
113{
Sergio Paracuellos860bce42021-06-07 14:01:50 +0200114 return readl_relaxed(port->base + reg);
Sergio Paracuellosc8242be2018-11-04 11:49:29 +0100115}
116
117static inline void pcie_port_write(struct mt7621_pcie_port *port,
118 u32 val, u32 reg)
119{
Sergio Paracuellos860bce42021-06-07 14:01:50 +0200120 writel_relaxed(val, port->base + reg);
Sergio Paracuellosc8242be2018-11-04 11:49:29 +0100121}
122
Bjorn Helgaas47938952021-12-22 19:10:48 -0600123static inline u32 mt7621_pcie_get_cfgaddr(unsigned int bus, unsigned int slot,
Sergio Paracuellos2427d172018-07-09 22:21:04 +0200124 unsigned int func, unsigned int where)
125{
Sergio Paracuellos2bdd5232021-09-22 07:00:34 +0200126 return (((where & 0xf00) >> 8) << 24) | (bus << 16) | (slot << 11) |
Sergio Paracuellos2427d172018-07-09 22:21:04 +0200127 (func << 8) | (where & 0xfc) | 0x80000000;
128}
129
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200130static 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 Helgaas47938952021-12-22 19:10:48 -0600134 u32 address = mt7621_pcie_get_cfgaddr(bus->number, PCI_SLOT(devfn),
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200135 PCI_FUNC(devfn), where);
John Crispin03f152e2018-03-15 07:22:35 +1100136
Sergio Paracuellos860bce42021-06-07 14:01:50 +0200137 writel_relaxed(address, pcie->base + RALINK_PCI_CONFIG_ADDR);
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200138
Sergio Paracuellos85943512018-08-03 10:27:06 +0200139 return pcie->base + RALINK_PCI_CONFIG_DATA + (where & 3);
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200140}
141
Bjorn Helgaas87c71932022-01-13 09:57:53 -0600142static struct pci_ops mt7621_pcie_ops = {
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200143 .map_bus = mt7621_pcie_map_bus,
144 .read = pci_generic_config_read,
145 .write = pci_generic_config_write,
John Crispin03f152e2018-03-15 07:22:35 +1100146};
147
Sergio Paracuellosce3368d2018-11-04 11:49:50 +0100148static u32 read_config(struct mt7621_pcie *pcie, unsigned int dev, u32 reg)
John Crispin03f152e2018-03-15 07:22:35 +1100149{
Bjorn Helgaas47938952021-12-22 19:10:48 -0600150 u32 address = mt7621_pcie_get_cfgaddr(0, dev, 0, reg);
John Crispin03f152e2018-03-15 07:22:35 +1100151
Sergio Paracuellos9f999b42018-08-03 10:26:57 +0200152 pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
Sergio Paracuellos85943512018-08-03 10:27:06 +0200153 return pcie_read(pcie, RALINK_PCI_CONFIG_DATA);
John Crispin03f152e2018-03-15 07:22:35 +1100154}
155
Sergio Paracuellosce3368d2018-11-04 11:49:50 +0100156static void write_config(struct mt7621_pcie *pcie, unsigned int dev,
157 u32 reg, u32 val)
John Crispin03f152e2018-03-15 07:22:35 +1100158{
Bjorn Helgaas47938952021-12-22 19:10:48 -0600159 u32 address = mt7621_pcie_get_cfgaddr(0, dev, 0, reg);
John Crispin03f152e2018-03-15 07:22:35 +1100160
Sergio Paracuellos9f999b42018-08-03 10:26:57 +0200161 pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
Sergio Paracuellos85943512018-08-03 10:27:06 +0200162 pcie_write(pcie, val, RALINK_PCI_CONFIG_DATA);
John Crispin03f152e2018-03-15 07:22:35 +1100163}
164
Sergio Paracuellosb27e35f2020-03-13 21:09:08 +0100165static inline void mt7621_rst_gpio_pcie_assert(struct mt7621_pcie_port *port)
Sergio Paracuellos07420a02019-06-19 09:44:56 +0200166{
Sergio Paracuellosb27e35f2020-03-13 21:09:08 +0100167 if (port->gpio_rst)
168 gpiod_set_value(port->gpio_rst, 1);
Sergio Paracuellos07420a02019-06-19 09:44:56 +0200169}
170
Sergio Paracuellosb27e35f2020-03-13 21:09:08 +0100171static inline void mt7621_rst_gpio_pcie_deassert(struct mt7621_pcie_port *port)
Sergio Paracuellos07420a02019-06-19 09:44:56 +0200172{
Sergio Paracuellosb27e35f2020-03-13 21:09:08 +0100173 if (port->gpio_rst)
174 gpiod_set_value(port->gpio_rst, 0);
Sergio Paracuellos07420a02019-06-19 09:44:56 +0200175}
176
177static 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 Paracuellose51844b2018-11-24 18:54:54 +0100182static inline void mt7621_control_assert(struct mt7621_pcie_port *port)
183{
Sergio Paracuellosb483b4e2019-10-06 20:10:32 +0200184 struct mt7621_pcie *pcie = port->pcie;
Sergio Paracuellose51844b2018-11-24 18:54:54 +0100185
Sergio Paracuellosb483b4e2019-10-06 20:10:32 +0200186 if (pcie->resets_inverted)
Sergio Paracuellose51844b2018-11-24 18:54:54 +0100187 reset_control_assert(port->pcie_rst);
188 else
189 reset_control_deassert(port->pcie_rst);
190}
191
192static inline void mt7621_control_deassert(struct mt7621_pcie_port *port)
193{
Sergio Paracuellosb483b4e2019-10-06 20:10:32 +0200194 struct mt7621_pcie *pcie = port->pcie;
Sergio Paracuellose51844b2018-11-24 18:54:54 +0100195
Sergio Paracuellosb483b4e2019-10-06 20:10:32 +0200196 if (pcie->resets_inverted)
Sergio Paracuellose51844b2018-11-24 18:54:54 +0100197 reset_control_deassert(port->pcie_rst);
198 else
199 reset_control_assert(port->pcie_rst);
200}
201
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +0100202static int mt7621_pcie_parse_port(struct mt7621_pcie *pcie,
Sergio Paracuellos2d3d2882021-06-07 14:01:52 +0200203 struct device_node *node,
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +0100204 int slot)
205{
206 struct mt7621_pcie_port *port;
207 struct device *dev = pcie->dev;
Sergio Paracuellosfab67102020-04-13 07:59:42 +0200208 struct platform_device *pdev = to_platform_device(dev);
Sergio Paracuellos61f9bde2019-01-04 08:08:22 +0100209 char name[10];
Sergio Paracuellos2d3d2882021-06-07 14:01:52 +0200210 int err;
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +0100211
212 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
213 if (!port)
214 return -ENOMEM;
215
Sergio Paracuellos108b2f22020-11-23 10:36:36 +0100216 port->base = devm_platform_ioremap_resource(pdev, slot + 1);
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +0100217 if (IS_ERR(port->base))
218 return PTR_ERR(port->base);
219
Sergio Paracuellos2d3d2882021-06-07 14:01:52 +0200220 port->clk = devm_get_clk_from_child(dev, node, NULL);
Sergio Paracuelloscc4e8642021-05-05 14:17:27 +0200221 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 Paracuellos2d3d2882021-06-07 14:01:52 +0200226 port->pcie_rst = of_reset_control_get_exclusive(node, NULL);
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +0100227 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 Paracuellos61f9bde2019-01-04 08:08:22 +0100232 snprintf(name, sizeof(name), "pcie-phy%d", slot);
Sergio Paracuellos2d3d2882021-06-07 14:01:52 +0200233 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 Paracuellos61f9bde2019-01-04 08:08:22 +0100239
Sergio Paracuellosb27e35f2020-03-13 21:09:08 +0100240 port->gpio_rst = devm_gpiod_get_index_optional(dev, "reset", slot,
241 GPIOD_OUT_LOW);
Sergio Paracuellos825c6f42020-03-20 12:01:23 +0100242 if (IS_ERR(port->gpio_rst)) {
Sergio Paracuellos2bdd5232021-09-22 07:00:34 +0200243 dev_err(dev, "failed to get GPIO for PCIe%d\n", slot);
Sergio Paracuellos2d3d2882021-06-07 14:01:52 +0200244 err = PTR_ERR(port->gpio_rst);
245 goto remove_reset;
Sergio Paracuellos825c6f42020-03-20 12:01:23 +0100246 }
Sergio Paracuellosb27e35f2020-03-13 21:09:08 +0100247
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +0100248 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 Paracuellos2d3d2882021-06-07 14:01:52 +0200255
256remove_reset:
257 reset_control_put(port->pcie_rst);
258 return err;
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +0100259}
260
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200261static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
262{
263 struct device *dev = pcie->dev;
Sergio Paracuellos108b2f22020-11-23 10:36:36 +0100264 struct platform_device *pdev = to_platform_device(dev);
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +0100265 struct device_node *node = dev->of_node, *child;
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200266 int err;
267
Sergio Paracuellos108b2f22020-11-23 10:36:36 +0100268 pcie->base = devm_platform_ioremap_resource(pdev, 0);
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200269 if (IS_ERR(pcie->base))
270 return PTR_ERR(pcie->base);
271
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +0100272 for_each_available_child_of_node(node, child) {
273 int slot;
274
275 err = of_pci_get_devfn(child);
276 if (err < 0) {
Nishka Dasgupta2125a442019-07-16 11:29:44 +0530277 of_node_put(child);
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +0100278 dev_err(dev, "failed to parse devfn: %d\n", err);
279 return err;
280 }
281
282 slot = PCI_SLOT(err);
283
Sergio Paracuellos2d3d2882021-06-07 14:01:52 +0200284 err = mt7621_pcie_parse_port(pcie, child, slot);
Nishka Dasgupta2125a442019-07-16 11:29:44 +0530285 if (err) {
286 of_node_put(child);
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +0100287 return err;
Nishka Dasgupta2125a442019-07-16 11:29:44 +0530288 }
Sergio Paracuellosad9c87e2018-11-04 11:49:27 +0100289 }
290
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200291 return 0;
292}
293
Sergio Paracuellos4fdf3ab2018-11-04 11:49:39 +0100294static int mt7621_pcie_init_port(struct mt7621_pcie_port *port)
Sergio Paracuellos745eeea2018-11-04 11:49:30 +0100295{
296 struct mt7621_pcie *pcie = port->pcie;
297 struct device *dev = pcie->dev;
298 u32 slot = port->slot;
Sergio Paracuellos61f9bde2019-01-04 08:08:22 +0100299 int err;
Sergio Paracuellos745eeea2018-11-04 11:49:30 +0100300
Sergio Paracuellos61f9bde2019-01-04 08:08:22 +0100301 err = phy_init(port->phy);
302 if (err) {
303 dev_err(dev, "failed to initialize port%d phy\n", slot);
Sergio Paracuellos07420a02019-06-19 09:44:56 +0200304 return err;
Sergio Paracuellos61f9bde2019-01-04 08:08:22 +0100305 }
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 Paracuelloscdf6f832019-06-21 08:15:15 +0200310 phy_exit(port->phy);
Sergio Paracuellos07420a02019-06-19 09:44:56 +0200311 return err;
Sergio Paracuellos745eeea2018-11-04 11:49:30 +0100312 }
313
Sergio Paracuellos61f9bde2019-01-04 08:08:22 +0100314 port->enabled = true;
Sergio Paracuellos3beb6da2018-11-04 11:49:37 +0100315
Sergio Paracuellos745eeea2018-11-04 11:49:30 +0100316 return 0;
317}
318
Sergio Paracuellosb27e35f2020-03-13 21:09:08 +0100319static 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 Paracuellos4ab4ca22021-05-05 14:17:31 +0200331 msleep(PERST_DELAY_MS);
Sergio Paracuellosb27e35f2020-03-13 21:09:08 +0100332}
333
334static 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
342static 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 Paracuellos4ab4ca22021-05-05 14:17:31 +0200349 msleep(PERST_DELAY_MS);
Sergio Paracuellosb27e35f2020-03-13 21:09:08 +0100350}
351
Sergio Paracuellos7d761b02021-08-23 19:08:03 +0200352static int mt7621_pcie_init_ports(struct mt7621_pcie *pcie)
Sergio Paracuelloscd910f12018-11-04 11:49:46 +0100353{
354 struct device *dev = pcie->dev;
355 struct mt7621_pcie_port *port, *tmp;
Sergio Paracuellos7d761b02021-08-23 19:08:03 +0200356 u8 num_disabled = 0;
Sergio Paracuelloscd910f12018-11-04 11:49:46 +0100357 int err;
358
Sergio Paracuellosb27e35f2020-03-13 21:09:08 +0100359 mt7621_pcie_reset_assert(pcie);
360 mt7621_pcie_reset_rc_deassert(pcie);
Sergio Paracuellos07420a02019-06-19 09:44:56 +0200361
Sergio Paracuelloscd910f12018-11-04 11:49:46 +0100362 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
363 u32 slot = port->slot;
364
Sergio Paracuellosbf516f42020-03-20 12:01:21 +0100365 if (slot == 1) {
366 port->enabled = true;
367 continue;
368 }
369
Sergio Paracuelloscd910f12018-11-04 11:49:46 +0100370 err = mt7621_pcie_init_port(port);
371 if (err) {
Sergio Paracuellos2bdd5232021-09-22 07:00:34 +0200372 dev_err(dev, "initializing port %d failed\n", slot);
Sergio Paracuelloscd910f12018-11-04 11:49:46 +0100373 list_del(&port->list);
374 }
375 }
Sergio Paracuellosb8d97d432018-11-04 11:49:58 +0100376
Sergio Paracuellosb27e35f2020-03-13 21:09:08 +0100377 mt7621_pcie_reset_ep_deassert(pcie);
Sergio Paracuellos07420a02019-06-19 09:44:56 +0200378
Sergio Paracuellos5fcded52020-04-09 13:16:52 +0200379 tmp = NULL;
Sergio Paracuellos07420a02019-06-19 09:44:56 +0200380 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 Paracuellos07420a02019-06-19 09:44:56 +0200386 mt7621_control_assert(port);
387 port->enabled = false;
Sergio Paracuellos7d761b02021-08-23 19:08:03 +0200388 num_disabled++;
Sergio Paracuellos5fcded52020-04-09 13:16:52 +0200389
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 Paracuellos07420a02019-06-19 09:44:56 +0200397 }
398 }
Sergio Paracuellos7d761b02021-08-23 19:08:03 +0200399
400 return (num_disabled != PCIE_PORT_CNT) ? 0 : -ENODEV;
Sergio Paracuelloscd910f12018-11-04 11:49:46 +0100401}
402
Sergio Paracuellos07420a02019-06-19 09:44:56 +0200403static void mt7621_pcie_enable_port(struct mt7621_pcie_port *port)
Sergio Paracuellos802a2f72018-11-04 11:49:57 +0100404{
405 struct mt7621_pcie *pcie = port->pcie;
406 u32 slot = port->slot;
Sergio Paracuellos802a2f72018-11-04 11:49:57 +0100407 u32 val;
Sergio Paracuellos802a2f72018-11-04 11:49:57 +0100408
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 Paracuellos25203e32021-06-07 14:01:53 +0200415 pcie_port_write(port, PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
416 PCI_BASE_ADDRESS_0);
Sergio Paracuellos802a2f72018-11-04 11:49:57 +0100417
418 /* configure class code and revision ID */
Sergio Paracuellos25203e32021-06-07 14:01:53 +0200419 pcie_port_write(port, PCIE_CLASS_CODE | PCIE_REVISION_ID,
420 RALINK_PCI_CLASS);
Sergio Paracuellosa3bb1d02021-06-07 14:01:48 +0200421
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 Paracuellos802a2f72018-11-04 11:49:57 +0100427}
428
Sergio Paracuellosb15606e2021-06-14 12:06:16 +0200429static int mt7621_pcie_enable_ports(struct pci_host_bridge *host)
Sergio Paracuellosa41a1f82018-11-04 11:49:44 +0100430{
Sergio Paracuellosb15606e2021-06-14 12:06:16 +0200431 struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
Sergio Paracuellosa41a1f82018-11-04 11:49:44 +0100432 struct device *dev = pcie->dev;
433 struct mt7621_pcie_port *port;
Sergio Paracuellosb15606e2021-06-14 12:06:16 +0200434 struct resource_entry *entry;
Sergio Paracuelloscc4e8642021-05-05 14:17:27 +0200435 int err;
Sergio Paracuellosa41a1f82018-11-04 11:49:44 +0100436
Sergio Paracuellosb15606e2021-06-14 12:06:16 +0200437 entry = resource_list_first_type(&host->windows, IORESOURCE_IO);
438 if (!entry) {
Sergio Paracuellos2bdd5232021-09-22 07:00:34 +0200439 dev_err(dev, "cannot get io resource\n");
Sergio Paracuellosb15606e2021-06-14 12:06:16 +0200440 return -EINVAL;
441 }
442
Sergio Paracuellos09dd6292020-03-18 10:44:45 +0100443 /* Setup MEMWIN and IOWIN */
444 pcie_write(pcie, 0xffffffff, RALINK_PCI_MEMBASE);
Sergio Paracuellose0b91382021-09-25 22:32:24 +0200445 pcie_write(pcie, entry->res->start - entry->offset, RALINK_PCI_IOBASE);
Sergio Paracuellos09dd6292020-03-18 10:44:45 +0100446
Sergio Paracuellosa41a1f82018-11-04 11:49:44 +0100447 list_for_each_entry(port, &pcie->ports, list) {
Sergio Paracuellosa41a1f82018-11-04 11:49:44 +0100448 if (port->enabled) {
Sergio Paracuelloscc4e8642021-05-05 14:17:27 +0200449 err = clk_prepare_enable(port->clk);
450 if (err) {
Sergio Paracuellosa3bb1d02021-06-07 14:01:48 +0200451 dev_err(dev, "enabling clk pcie%d\n",
452 port->slot);
Sergio Paracuelloscc4e8642021-05-05 14:17:27 +0200453 return err;
454 }
455
Sergio Paracuellos07420a02019-06-19 09:44:56 +0200456 mt7621_pcie_enable_port(port);
Sergio Paracuellos7846a982020-03-20 12:01:22 +0100457 dev_info(dev, "PCIE%d enabled\n", port->slot);
Sergio Paracuellosa41a1f82018-11-04 11:49:44 +0100458 }
459 }
460
Sergio Paracuelloscc4e8642021-05-05 14:17:27 +0200461 return 0;
Sergio Paracuellosa41a1f82018-11-04 11:49:44 +0100462}
463
Sergio Paracuellos2fc08982020-11-23 10:36:35 +0100464static int mt7621_pcie_register_host(struct pci_host_bridge *host)
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200465{
466 struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
467
Bjorn Helgaas47938952021-12-22 19:10:48 -0600468 host->ops = &mt7621_pcie_ops;
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200469 host->sysdata = pcie;
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200470 return pci_host_probe(host);
471}
472
Bjorn Helgaas47938952021-12-22 19:10:48 -0600473static const struct soc_device_attribute mt7621_pcie_quirks_match[] = {
Sergio Paracuellosb483b4e2019-10-06 20:10:32 +0200474 { .soc_id = "mt7621", .revision = "E2" }
475};
476
Bjorn Helgaas47938952021-12-22 19:10:48 -0600477static int mt7621_pcie_probe(struct platform_device *pdev)
John Crispin03f152e2018-03-15 07:22:35 +1100478{
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200479 struct device *dev = &pdev->dev;
Sergio Paracuellosb483b4e2019-10-06 20:10:32 +0200480 const struct soc_device_attribute *attr;
Sergio Paracuellos2d3d2882021-06-07 14:01:52 +0200481 struct mt7621_pcie_port *port;
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200482 struct mt7621_pcie *pcie;
483 struct pci_host_bridge *bridge;
484 int err;
John Crispin03f152e2018-03-15 07:22:35 +1100485
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200486 if (!dev->of_node)
487 return -ENODEV;
488
489 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
490 if (!bridge)
Sergio Paracuellosa80775d2018-11-04 11:49:28 +0100491 return -ENOMEM;
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200492
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 Helgaas47938952021-12-22 19:10:48 -0600498 attr = soc_device_match(mt7621_pcie_quirks_match);
Sergio Paracuellosb483b4e2019-10-06 20:10:32 +0200499 if (attr)
500 pcie->resets_inverted = true;
501
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200502 err = mt7621_pcie_parse_dt(pcie);
503 if (err) {
Sergio Paracuellos2bdd5232021-09-22 07:00:34 +0200504 dev_err(dev, "parsing DT failed\n");
Sergio Paracuellos8571c622018-08-03 10:26:54 +0200505 return err;
506 }
507
Sergio Paracuellos7d761b02021-08-23 19:08:03 +0200508 err = mt7621_pcie_init_ports(pcie);
509 if (err) {
Sergio Paracuellos2bdd5232021-09-22 07:00:34 +0200510 dev_err(dev, "nothing connected in virtual bridges\n");
Sergio Paracuellos7d761b02021-08-23 19:08:03 +0200511 return 0;
512 }
John Crispin03f152e2018-03-15 07:22:35 +1100513
Sergio Paracuellosb15606e2021-06-14 12:06:16 +0200514 err = mt7621_pcie_enable_ports(bridge);
Sergio Paracuelloscc4e8642021-05-05 14:17:27 +0200515 if (err) {
Sergio Paracuellos2bdd5232021-09-22 07:00:34 +0200516 dev_err(dev, "error enabling pcie ports\n");
Sergio Paracuellos2d3d2882021-06-07 14:01:52 +0200517 goto remove_resets;
Sergio Paracuelloscc4e8642021-05-05 14:17:27 +0200518 }
John Crispin03f152e2018-03-15 07:22:35 +1100519
Sergio Paracuellos35d96b82021-05-05 14:17:32 +0200520 return mt7621_pcie_register_host(bridge);
Sergio Paracuellos2d3d2882021-06-07 14:01:52 +0200521
522remove_resets:
523 list_for_each_entry(port, &pcie->ports, list)
524 reset_control_put(port->pcie_rst);
525
526 return err;
527}
528
Bjorn Helgaas47938952021-12-22 19:10:48 -0600529static int mt7621_pcie_remove(struct platform_device *pdev)
Sergio Paracuellos2d3d2882021-06-07 14:01:52 +0200530{
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 Crispin03f152e2018-03-15 07:22:35 +1100538}
539
Bjorn Helgaas47938952021-12-22 19:10:48 -0600540static const struct of_device_id mt7621_pcie_ids[] = {
John Crispin03f152e2018-03-15 07:22:35 +1100541 { .compatible = "mediatek,mt7621-pci" },
542 {},
543};
Bjorn Helgaas47938952021-12-22 19:10:48 -0600544MODULE_DEVICE_TABLE(of, mt7621_pcie_ids);
John Crispin03f152e2018-03-15 07:22:35 +1100545
Bjorn Helgaas47938952021-12-22 19:10:48 -0600546static struct platform_driver mt7621_pcie_driver = {
547 .probe = mt7621_pcie_probe,
548 .remove = mt7621_pcie_remove,
John Crispin03f152e2018-03-15 07:22:35 +1100549 .driver = {
550 .name = "mt7621-pci",
Sergio Paracuellos4b77e4a2022-01-24 12:30:02 +0100551 .of_match_table = mt7621_pcie_ids,
John Crispin03f152e2018-03-15 07:22:35 +1100552 },
553};
Bjorn Helgaas47938952021-12-22 19:10:48 -0600554builtin_platform_driver(mt7621_pcie_driver);
Sergio Paracuellose4b1cd02021-12-07 11:49:23 +0100555
556MODULE_LICENSE("GPL v2");