blob: 547e4c76edbb98297d14f71ccd9a124897fd08f7 [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Ryder Lee637cfaca2017-05-21 11:42:24 +08002/*
3 * MediaTek PCIe host controller driver.
4 *
5 * Copyright (c) 2017 MediaTek Inc.
6 * Author: Ryder Lee <ryder.lee@mediatek.com>
Ryder Leeb0996312017-08-10 14:34:59 +08007 * Honghui Zhang <honghui.zhang@mediatek.com>
Ryder Lee637cfaca2017-05-21 11:42:24 +08008 */
9
10#include <linux/clk.h>
11#include <linux/delay.h>
Ryder Leee10b7a12017-08-10 14:34:54 +080012#include <linux/iopoll.h>
Ryder Leeb0996312017-08-10 14:34:59 +080013#include <linux/irq.h>
Honghui Zhang42fe2f92018-05-04 13:47:33 +080014#include <linux/irqchip/chained_irq.h>
Ryder Leeb0996312017-08-10 14:34:59 +080015#include <linux/irqdomain.h>
Ryder Lee637cfaca2017-05-21 11:42:24 +080016#include <linux/kernel.h>
Honghui Zhang42fe2f92018-05-04 13:47:33 +080017#include <linux/msi.h>
Ryder Lee637cfaca2017-05-21 11:42:24 +080018#include <linux/of_address.h>
19#include <linux/of_pci.h>
20#include <linux/of_platform.h>
21#include <linux/pci.h>
22#include <linux/phy/phy.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/reset.h>
26
Rob Herring9e2aee82018-05-11 12:15:30 -050027#include "../pci.h"
28
Ryder Lee637cfaca2017-05-21 11:42:24 +080029/* PCIe shared registers */
30#define PCIE_SYS_CFG 0x00
31#define PCIE_INT_ENABLE 0x0c
32#define PCIE_CFG_ADDR 0x20
33#define PCIE_CFG_DATA 0x24
34
35/* PCIe per port registers */
36#define PCIE_BAR0_SETUP 0x10
37#define PCIE_CLASS 0x34
38#define PCIE_LINK_STATUS 0x50
39
40#define PCIE_PORT_INT_EN(x) BIT(20 + (x))
41#define PCIE_PORT_PERST(x) BIT(1 + (x))
42#define PCIE_PORT_LINKUP BIT(0)
43#define PCIE_BAR_MAP_MAX GENMASK(31, 16)
44
45#define PCIE_BAR_ENABLE BIT(0)
46#define PCIE_REVISION_ID BIT(0)
47#define PCIE_CLASS_CODE (0x60400 << 8)
48#define PCIE_CONF_REG(regn) (((regn) & GENMASK(7, 2)) | \
49 ((((regn) >> 8) & GENMASK(3, 0)) << 24))
50#define PCIE_CONF_FUN(fun) (((fun) << 8) & GENMASK(10, 8))
51#define PCIE_CONF_DEV(dev) (((dev) << 11) & GENMASK(15, 11))
52#define PCIE_CONF_BUS(bus) (((bus) << 16) & GENMASK(23, 16))
53#define PCIE_CONF_ADDR(regn, fun, dev, bus) \
54 (PCIE_CONF_REG(regn) | PCIE_CONF_FUN(fun) | \
55 PCIE_CONF_DEV(dev) | PCIE_CONF_BUS(bus))
56
57/* MediaTek specific configuration registers */
58#define PCIE_FTS_NUM 0x70c
59#define PCIE_FTS_NUM_MASK GENMASK(15, 8)
60#define PCIE_FTS_NUM_L0(x) ((x) & 0xff << 8)
61
62#define PCIE_FC_CREDIT 0x73c
63#define PCIE_FC_CREDIT_MASK (GENMASK(31, 31) | GENMASK(28, 16))
64#define PCIE_FC_CREDIT_VAL(x) ((x) << 16)
65
Ryder Leeb0996312017-08-10 14:34:59 +080066/* PCIe V2 share registers */
67#define PCIE_SYS_CFG_V2 0x0
68#define PCIE_CSR_LTSSM_EN(x) BIT(0 + (x) * 8)
69#define PCIE_CSR_ASPM_L1_EN(x) BIT(1 + (x) * 8)
70
71/* PCIe V2 per-port registers */
Honghui Zhang43e64092017-08-14 21:04:28 +080072#define PCIE_MSI_VECTOR 0x0c0
Honghui Zhang101c92d2018-05-04 13:47:32 +080073
74#define PCIE_CONF_VEND_ID 0x100
75#define PCIE_CONF_CLASS_ID 0x106
76
Ryder Leeb0996312017-08-10 14:34:59 +080077#define PCIE_INT_MASK 0x420
78#define INTX_MASK GENMASK(19, 16)
79#define INTX_SHIFT 16
Ryder Leeb0996312017-08-10 14:34:59 +080080#define PCIE_INT_STATUS 0x424
Honghui Zhang43e64092017-08-14 21:04:28 +080081#define MSI_STATUS BIT(23)
82#define PCIE_IMSI_STATUS 0x42c
83#define PCIE_IMSI_ADDR 0x430
84#define MSI_MASK BIT(23)
85#define MTK_MSI_IRQS_NUM 32
Ryder Leeb0996312017-08-10 14:34:59 +080086
87#define PCIE_AHB_TRANS_BASE0_L 0x438
88#define PCIE_AHB_TRANS_BASE0_H 0x43c
89#define AHB2PCIE_SIZE(x) ((x) & GENMASK(4, 0))
90#define PCIE_AXI_WINDOW0 0x448
91#define WIN_ENABLE BIT(7)
92
93/* PCIe V2 configuration transaction header */
94#define PCIE_CFG_HEADER0 0x460
95#define PCIE_CFG_HEADER1 0x464
96#define PCIE_CFG_HEADER2 0x468
97#define PCIE_CFG_WDATA 0x470
98#define PCIE_APP_TLP_REQ 0x488
99#define PCIE_CFG_RDATA 0x48c
100#define APP_CFG_REQ BIT(0)
101#define APP_CPL_STATUS GENMASK(7, 5)
102
103#define CFG_WRRD_TYPE_0 4
104#define CFG_WR_FMT 2
105#define CFG_RD_FMT 0
106
107#define CFG_DW0_LENGTH(length) ((length) & GENMASK(9, 0))
108#define CFG_DW0_TYPE(type) (((type) << 24) & GENMASK(28, 24))
109#define CFG_DW0_FMT(fmt) (((fmt) << 29) & GENMASK(31, 29))
110#define CFG_DW2_REGN(regn) ((regn) & GENMASK(11, 2))
111#define CFG_DW2_FUN(fun) (((fun) << 16) & GENMASK(18, 16))
112#define CFG_DW2_DEV(dev) (((dev) << 19) & GENMASK(23, 19))
113#define CFG_DW2_BUS(bus) (((bus) << 24) & GENMASK(31, 24))
114#define CFG_HEADER_DW0(type, fmt) \
115 (CFG_DW0_LENGTH(1) | CFG_DW0_TYPE(type) | CFG_DW0_FMT(fmt))
116#define CFG_HEADER_DW1(where, size) \
117 (GENMASK(((size) - 1), 0) << ((where) & 0x3))
118#define CFG_HEADER_DW2(regn, fun, dev, bus) \
119 (CFG_DW2_REGN(regn) | CFG_DW2_FUN(fun) | \
120 CFG_DW2_DEV(dev) | CFG_DW2_BUS(bus))
121
122#define PCIE_RST_CTRL 0x510
123#define PCIE_PHY_RSTB BIT(0)
124#define PCIE_PIPE_SRSTB BIT(1)
125#define PCIE_MAC_SRSTB BIT(2)
126#define PCIE_CRSTB BIT(3)
127#define PCIE_PERSTB BIT(8)
128#define PCIE_LINKDOWN_RST_EN GENMASK(15, 13)
129#define PCIE_LINK_STATUS_V2 0x804
130#define PCIE_PORT_LINKUP_V2 BIT(10)
131
Honghui Zhangc681c932017-08-10 14:34:56 +0800132struct mtk_pcie_port;
133
134/**
135 * struct mtk_pcie_soc - differentiate between host generations
Honghui Zhang101c92d2018-05-04 13:47:32 +0800136 * @need_fix_class_id: whether this host's class ID needed to be fixed or not
Honghui Zhangc681c932017-08-10 14:34:56 +0800137 * @ops: pointer to configuration access functions
138 * @startup: pointer to controller setting functions
Ryder Leeb0996312017-08-10 14:34:59 +0800139 * @setup_irq: pointer to initialize IRQ functions
Honghui Zhangc681c932017-08-10 14:34:56 +0800140 */
141struct mtk_pcie_soc {
Honghui Zhang101c92d2018-05-04 13:47:32 +0800142 bool need_fix_class_id;
Honghui Zhangc681c932017-08-10 14:34:56 +0800143 struct pci_ops *ops;
144 int (*startup)(struct mtk_pcie_port *port);
Ryder Leeb0996312017-08-10 14:34:59 +0800145 int (*setup_irq)(struct mtk_pcie_port *port, struct device_node *node);
Honghui Zhangc681c932017-08-10 14:34:56 +0800146};
147
Ryder Lee637cfaca2017-05-21 11:42:24 +0800148/**
149 * struct mtk_pcie_port - PCIe port information
150 * @base: IO mapped register base
151 * @list: port list
152 * @pcie: pointer to PCIe host info
153 * @reset: pointer to port reset control
Ryder Leeb0996312017-08-10 14:34:59 +0800154 * @sys_ck: pointer to transaction/data link layer clock
155 * @ahb_ck: pointer to AHB slave interface operating clock for CSR access
156 * and RC initiated MMIO access
157 * @axi_ck: pointer to application layer MMIO channel operating clock
158 * @aux_ck: pointer to pe2_mac_bridge and pe2_mac_core operating clock
159 * when pcie_mac_ck/pcie_pipe_ck is turned off
160 * @obff_ck: pointer to OBFF functional block operating clock
161 * @pipe_ck: pointer to LTSSM and PHY/MAC layer operating clock
162 * @phy: pointer to PHY control block
Ryder Lee637cfaca2017-05-21 11:42:24 +0800163 * @lane: lane count
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800164 * @slot: port slot
Ryder Leeb0996312017-08-10 14:34:59 +0800165 * @irq_domain: legacy INTx IRQ domain
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800166 * @inner_domain: inner IRQ domain
Honghui Zhang43e64092017-08-14 21:04:28 +0800167 * @msi_domain: MSI IRQ domain
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800168 * @lock: protect the msi_irq_in_use bitmap
Honghui Zhang43e64092017-08-14 21:04:28 +0800169 * @msi_irq_in_use: bit map for assigned MSI IRQ
Ryder Lee637cfaca2017-05-21 11:42:24 +0800170 */
171struct mtk_pcie_port {
172 void __iomem *base;
173 struct list_head list;
174 struct mtk_pcie *pcie;
175 struct reset_control *reset;
176 struct clk *sys_ck;
Ryder Leeb0996312017-08-10 14:34:59 +0800177 struct clk *ahb_ck;
178 struct clk *axi_ck;
179 struct clk *aux_ck;
180 struct clk *obff_ck;
181 struct clk *pipe_ck;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800182 struct phy *phy;
183 u32 lane;
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800184 u32 slot;
Ryder Leeb0996312017-08-10 14:34:59 +0800185 struct irq_domain *irq_domain;
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800186 struct irq_domain *inner_domain;
Honghui Zhang43e64092017-08-14 21:04:28 +0800187 struct irq_domain *msi_domain;
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800188 struct mutex lock;
Honghui Zhang43e64092017-08-14 21:04:28 +0800189 DECLARE_BITMAP(msi_irq_in_use, MTK_MSI_IRQS_NUM);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800190};
191
192/**
193 * struct mtk_pcie - PCIe host information
194 * @dev: pointer to PCIe device
195 * @base: IO mapped register base
196 * @free_ck: free-run reference clock
197 * @io: IO resource
198 * @pio: PIO resource
199 * @mem: non-prefetchable memory resource
200 * @busn: bus range
201 * @offset: IO / Memory offset
202 * @ports: pointer to PCIe port information
Honghui Zhangc681c932017-08-10 14:34:56 +0800203 * @soc: pointer to SoC-dependent operations
Ryder Lee637cfaca2017-05-21 11:42:24 +0800204 */
205struct mtk_pcie {
206 struct device *dev;
207 void __iomem *base;
208 struct clk *free_ck;
209
210 struct resource io;
211 struct resource pio;
212 struct resource mem;
213 struct resource busn;
214 struct {
215 resource_size_t mem;
216 resource_size_t io;
217 } offset;
218 struct list_head ports;
Honghui Zhangc681c932017-08-10 14:34:56 +0800219 const struct mtk_pcie_soc *soc;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800220};
221
Ryder Lee637cfaca2017-05-21 11:42:24 +0800222static void mtk_pcie_subsys_powerdown(struct mtk_pcie *pcie)
223{
224 struct device *dev = pcie->dev;
225
226 clk_disable_unprepare(pcie->free_ck);
227
Honghui Zhang88c0e232018-10-15 16:08:54 +0800228 pm_runtime_put_sync(dev);
229 pm_runtime_disable(dev);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800230}
231
232static void mtk_pcie_port_free(struct mtk_pcie_port *port)
233{
234 struct mtk_pcie *pcie = port->pcie;
235 struct device *dev = pcie->dev;
236
237 devm_iounmap(dev, port->base);
238 list_del(&port->list);
239 devm_kfree(dev, port);
240}
241
242static void mtk_pcie_put_resources(struct mtk_pcie *pcie)
243{
244 struct mtk_pcie_port *port, *tmp;
245
246 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
247 phy_power_off(port->phy);
Ryder Leeb0996312017-08-10 14:34:59 +0800248 phy_exit(port->phy);
249 clk_disable_unprepare(port->pipe_ck);
250 clk_disable_unprepare(port->obff_ck);
251 clk_disable_unprepare(port->axi_ck);
252 clk_disable_unprepare(port->aux_ck);
253 clk_disable_unprepare(port->ahb_ck);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800254 clk_disable_unprepare(port->sys_ck);
255 mtk_pcie_port_free(port);
256 }
257
258 mtk_pcie_subsys_powerdown(pcie);
259}
260
Ryder Leeb0996312017-08-10 14:34:59 +0800261static int mtk_pcie_check_cfg_cpld(struct mtk_pcie_port *port)
262{
263 u32 val;
264 int err;
265
266 err = readl_poll_timeout_atomic(port->base + PCIE_APP_TLP_REQ, val,
267 !(val & APP_CFG_REQ), 10,
268 100 * USEC_PER_MSEC);
269 if (err)
270 return PCIBIOS_SET_FAILED;
271
272 if (readl(port->base + PCIE_APP_TLP_REQ) & APP_CPL_STATUS)
273 return PCIBIOS_SET_FAILED;
274
275 return PCIBIOS_SUCCESSFUL;
276}
277
278static int mtk_pcie_hw_rd_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn,
279 int where, int size, u32 *val)
280{
281 u32 tmp;
282
283 /* Write PCIe configuration transaction header for Cfgrd */
284 writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_RD_FMT),
285 port->base + PCIE_CFG_HEADER0);
286 writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1);
287 writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus),
288 port->base + PCIE_CFG_HEADER2);
289
290 /* Trigger h/w to transmit Cfgrd TLP */
291 tmp = readl(port->base + PCIE_APP_TLP_REQ);
292 tmp |= APP_CFG_REQ;
293 writel(tmp, port->base + PCIE_APP_TLP_REQ);
294
295 /* Check completion status */
296 if (mtk_pcie_check_cfg_cpld(port))
297 return PCIBIOS_SET_FAILED;
298
299 /* Read cpld payload of Cfgrd */
300 *val = readl(port->base + PCIE_CFG_RDATA);
301
302 if (size == 1)
303 *val = (*val >> (8 * (where & 3))) & 0xff;
304 else if (size == 2)
305 *val = (*val >> (8 * (where & 3))) & 0xffff;
306
307 return PCIBIOS_SUCCESSFUL;
308}
309
310static int mtk_pcie_hw_wr_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn,
311 int where, int size, u32 val)
312{
313 /* Write PCIe configuration transaction header for Cfgwr */
314 writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_WR_FMT),
315 port->base + PCIE_CFG_HEADER0);
316 writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1);
317 writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus),
318 port->base + PCIE_CFG_HEADER2);
319
320 /* Write Cfgwr data */
321 val = val << 8 * (where & 3);
322 writel(val, port->base + PCIE_CFG_WDATA);
323
324 /* Trigger h/w to transmit Cfgwr TLP */
325 val = readl(port->base + PCIE_APP_TLP_REQ);
326 val |= APP_CFG_REQ;
327 writel(val, port->base + PCIE_APP_TLP_REQ);
328
329 /* Check completion status */
330 return mtk_pcie_check_cfg_cpld(port);
331}
332
333static struct mtk_pcie_port *mtk_pcie_find_port(struct pci_bus *bus,
334 unsigned int devfn)
335{
336 struct mtk_pcie *pcie = bus->sysdata;
337 struct mtk_pcie_port *port;
Honghui Zhang074d6f32018-10-15 16:08:52 +0800338 struct pci_dev *dev = NULL;
339
340 /*
341 * Walk the bus hierarchy to get the devfn value
342 * of the port in the root bus.
343 */
344 while (bus && bus->number) {
345 dev = bus->self;
346 bus = dev->bus;
347 devfn = dev->devfn;
348 }
Ryder Leeb0996312017-08-10 14:34:59 +0800349
350 list_for_each_entry(port, &pcie->ports, list)
351 if (port->slot == PCI_SLOT(devfn))
352 return port;
353
354 return NULL;
355}
356
357static int mtk_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
358 int where, int size, u32 *val)
359{
360 struct mtk_pcie_port *port;
361 u32 bn = bus->number;
362 int ret;
363
364 port = mtk_pcie_find_port(bus, devfn);
365 if (!port) {
366 *val = ~0;
367 return PCIBIOS_DEVICE_NOT_FOUND;
368 }
369
370 ret = mtk_pcie_hw_rd_cfg(port, bn, devfn, where, size, val);
371 if (ret)
372 *val = ~0;
373
374 return ret;
375}
376
377static int mtk_pcie_config_write(struct pci_bus *bus, unsigned int devfn,
378 int where, int size, u32 val)
379{
380 struct mtk_pcie_port *port;
381 u32 bn = bus->number;
382
383 port = mtk_pcie_find_port(bus, devfn);
384 if (!port)
385 return PCIBIOS_DEVICE_NOT_FOUND;
386
387 return mtk_pcie_hw_wr_cfg(port, bn, devfn, where, size, val);
388}
389
390static struct pci_ops mtk_pcie_ops_v2 = {
391 .read = mtk_pcie_config_read,
392 .write = mtk_pcie_config_write,
393};
394
395static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)
396{
397 struct mtk_pcie *pcie = port->pcie;
398 struct resource *mem = &pcie->mem;
Honghui Zhang101c92d2018-05-04 13:47:32 +0800399 const struct mtk_pcie_soc *soc = port->pcie->soc;
Ryder Leeb0996312017-08-10 14:34:59 +0800400 u32 val;
401 size_t size;
402 int err;
403
404 /* MT7622 platforms need to enable LTSSM and ASPM from PCIe subsys */
405 if (pcie->base) {
406 val = readl(pcie->base + PCIE_SYS_CFG_V2);
407 val |= PCIE_CSR_LTSSM_EN(port->slot) |
408 PCIE_CSR_ASPM_L1_EN(port->slot);
409 writel(val, pcie->base + PCIE_SYS_CFG_V2);
410 }
411
412 /* Assert all reset signals */
413 writel(0, port->base + PCIE_RST_CTRL);
414
415 /*
416 * Enable PCIe link down reset, if link status changed from link up to
417 * link down, this will reset MAC control registers and configuration
418 * space.
419 */
420 writel(PCIE_LINKDOWN_RST_EN, port->base + PCIE_RST_CTRL);
421
422 /* De-assert PHY, PE, PIPE, MAC and configuration reset */
423 val = readl(port->base + PCIE_RST_CTRL);
424 val |= PCIE_PHY_RSTB | PCIE_PERSTB | PCIE_PIPE_SRSTB |
425 PCIE_MAC_SRSTB | PCIE_CRSTB;
426 writel(val, port->base + PCIE_RST_CTRL);
427
Honghui Zhang101c92d2018-05-04 13:47:32 +0800428 /* Set up vendor ID and class code */
429 if (soc->need_fix_class_id) {
430 val = PCI_VENDOR_ID_MEDIATEK;
431 writew(val, port->base + PCIE_CONF_VEND_ID);
432
Honghui Zhanga7f172a2018-10-15 16:08:53 +0800433 val = PCI_CLASS_BRIDGE_PCI;
Honghui Zhang101c92d2018-05-04 13:47:32 +0800434 writew(val, port->base + PCIE_CONF_CLASS_ID);
435 }
436
Ryder Leeb0996312017-08-10 14:34:59 +0800437 /* 100ms timeout value should be enough for Gen1/2 training */
438 err = readl_poll_timeout(port->base + PCIE_LINK_STATUS_V2, val,
439 !!(val & PCIE_PORT_LINKUP_V2), 20,
440 100 * USEC_PER_MSEC);
441 if (err)
442 return -ETIMEDOUT;
443
444 /* Set INTx mask */
445 val = readl(port->base + PCIE_INT_MASK);
446 val &= ~INTX_MASK;
447 writel(val, port->base + PCIE_INT_MASK);
448
449 /* Set AHB to PCIe translation windows */
450 size = mem->end - mem->start;
451 val = lower_32_bits(mem->start) | AHB2PCIE_SIZE(fls(size));
452 writel(val, port->base + PCIE_AHB_TRANS_BASE0_L);
453
454 val = upper_32_bits(mem->start);
455 writel(val, port->base + PCIE_AHB_TRANS_BASE0_H);
456
457 /* Set PCIe to AXI translation memory space.*/
458 val = fls(0xffffffff) | WIN_ENABLE;
459 writel(val, port->base + PCIE_AXI_WINDOW0);
460
461 return 0;
462}
463
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800464static void mtk_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
Honghui Zhang43e64092017-08-14 21:04:28 +0800465{
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800466 struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data);
467 phys_addr_t addr;
Honghui Zhang43e64092017-08-14 21:04:28 +0800468
469 /* MT2712/MT7622 only support 32-bit MSI addresses */
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800470 addr = virt_to_phys(port->base + PCIE_MSI_VECTOR);
471 msg->address_hi = 0;
472 msg->address_lo = lower_32_bits(addr);
Honghui Zhang43e64092017-08-14 21:04:28 +0800473
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800474 msg->data = data->hwirq;
475
476 dev_dbg(port->pcie->dev, "msi#%d address_hi %#x address_lo %#x\n",
477 (int)data->hwirq, msg->address_hi, msg->address_lo);
478}
479
480static int mtk_msi_set_affinity(struct irq_data *irq_data,
481 const struct cpumask *mask, bool force)
482{
483 return -EINVAL;
484}
485
486static void mtk_msi_ack_irq(struct irq_data *data)
487{
488 struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data);
489 u32 hwirq = data->hwirq;
490
491 writel(1 << hwirq, port->base + PCIE_IMSI_STATUS);
492}
493
494static struct irq_chip mtk_msi_bottom_irq_chip = {
495 .name = "MTK MSI",
496 .irq_compose_msi_msg = mtk_compose_msi_msg,
497 .irq_set_affinity = mtk_msi_set_affinity,
498 .irq_ack = mtk_msi_ack_irq,
499};
500
501static int mtk_pcie_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
502 unsigned int nr_irqs, void *args)
503{
504 struct mtk_pcie_port *port = domain->host_data;
505 unsigned long bit;
506
507 WARN_ON(nr_irqs != 1);
508 mutex_lock(&port->lock);
509
510 bit = find_first_zero_bit(port->msi_irq_in_use, MTK_MSI_IRQS_NUM);
511 if (bit >= MTK_MSI_IRQS_NUM) {
512 mutex_unlock(&port->lock);
513 return -ENOSPC;
514 }
515
516 __set_bit(bit, port->msi_irq_in_use);
517
518 mutex_unlock(&port->lock);
519
520 irq_domain_set_info(domain, virq, bit, &mtk_msi_bottom_irq_chip,
521 domain->host_data, handle_edge_irq,
522 NULL, NULL);
Honghui Zhang43e64092017-08-14 21:04:28 +0800523
524 return 0;
525}
526
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800527static void mtk_pcie_irq_domain_free(struct irq_domain *domain,
528 unsigned int virq, unsigned int nr_irqs)
Honghui Zhang43e64092017-08-14 21:04:28 +0800529{
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800530 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
531 struct mtk_pcie_port *port = irq_data_get_irq_chip_data(d);
Honghui Zhang43e64092017-08-14 21:04:28 +0800532
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800533 mutex_lock(&port->lock);
Honghui Zhang43e64092017-08-14 21:04:28 +0800534
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800535 if (!test_bit(d->hwirq, port->msi_irq_in_use))
536 dev_err(port->pcie->dev, "trying to free unused MSI#%lu\n",
537 d->hwirq);
538 else
539 __clear_bit(d->hwirq, port->msi_irq_in_use);
Honghui Zhang43e64092017-08-14 21:04:28 +0800540
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800541 mutex_unlock(&port->lock);
Honghui Zhang43e64092017-08-14 21:04:28 +0800542
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800543 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
Honghui Zhang43e64092017-08-14 21:04:28 +0800544}
545
546static const struct irq_domain_ops msi_domain_ops = {
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800547 .alloc = mtk_pcie_irq_domain_alloc,
548 .free = mtk_pcie_irq_domain_free,
Honghui Zhang43e64092017-08-14 21:04:28 +0800549};
550
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800551static struct irq_chip mtk_msi_irq_chip = {
552 .name = "MTK PCIe MSI",
553 .irq_ack = irq_chip_ack_parent,
554 .irq_mask = pci_msi_mask_irq,
555 .irq_unmask = pci_msi_unmask_irq,
556};
557
558static struct msi_domain_info mtk_msi_domain_info = {
559 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
560 MSI_FLAG_PCI_MSIX),
561 .chip = &mtk_msi_irq_chip,
562};
563
564static int mtk_pcie_allocate_msi_domains(struct mtk_pcie_port *port)
565{
566 struct fwnode_handle *fwnode = of_node_to_fwnode(port->pcie->dev->of_node);
567
568 mutex_init(&port->lock);
569
570 port->inner_domain = irq_domain_create_linear(fwnode, MTK_MSI_IRQS_NUM,
571 &msi_domain_ops, port);
572 if (!port->inner_domain) {
573 dev_err(port->pcie->dev, "failed to create IRQ domain\n");
574 return -ENOMEM;
575 }
576
577 port->msi_domain = pci_msi_create_irq_domain(fwnode, &mtk_msi_domain_info,
578 port->inner_domain);
579 if (!port->msi_domain) {
580 dev_err(port->pcie->dev, "failed to create MSI domain\n");
581 irq_domain_remove(port->inner_domain);
582 return -ENOMEM;
583 }
584
585 return 0;
586}
587
Honghui Zhang43e64092017-08-14 21:04:28 +0800588static void mtk_pcie_enable_msi(struct mtk_pcie_port *port)
589{
590 u32 val;
591 phys_addr_t msg_addr;
592
593 msg_addr = virt_to_phys(port->base + PCIE_MSI_VECTOR);
594 val = lower_32_bits(msg_addr);
595 writel(val, port->base + PCIE_IMSI_ADDR);
596
597 val = readl(port->base + PCIE_INT_MASK);
598 val &= ~MSI_MASK;
599 writel(val, port->base + PCIE_INT_MASK);
600}
601
Ryder Leeb0996312017-08-10 14:34:59 +0800602static int mtk_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
603 irq_hw_number_t hwirq)
604{
605 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
606 irq_set_chip_data(irq, domain->host_data);
607
608 return 0;
609}
610
611static const struct irq_domain_ops intx_domain_ops = {
612 .map = mtk_pcie_intx_map,
613};
614
615static int mtk_pcie_init_irq_domain(struct mtk_pcie_port *port,
616 struct device_node *node)
617{
618 struct device *dev = port->pcie->dev;
619 struct device_node *pcie_intc_node;
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800620 int ret;
Ryder Leeb0996312017-08-10 14:34:59 +0800621
622 /* Setup INTx */
623 pcie_intc_node = of_get_next_child(node, NULL);
624 if (!pcie_intc_node) {
625 dev_err(dev, "no PCIe Intc node found\n");
626 return -ENODEV;
627 }
628
Honghui Zhangd84c2462017-08-30 09:19:14 +0800629 port->irq_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
Ryder Leeb0996312017-08-10 14:34:59 +0800630 &intx_domain_ops, port);
631 if (!port->irq_domain) {
632 dev_err(dev, "failed to get INTx IRQ domain\n");
633 return -ENODEV;
634 }
635
Honghui Zhang43e64092017-08-14 21:04:28 +0800636 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800637 ret = mtk_pcie_allocate_msi_domains(port);
638 if (ret)
639 return ret;
640
Honghui Zhang43e64092017-08-14 21:04:28 +0800641 mtk_pcie_enable_msi(port);
642 }
643
Ryder Leeb0996312017-08-10 14:34:59 +0800644 return 0;
645}
646
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800647static void mtk_pcie_intr_handler(struct irq_desc *desc)
Ryder Leeb0996312017-08-10 14:34:59 +0800648{
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800649 struct mtk_pcie_port *port = irq_desc_get_handler_data(desc);
650 struct irq_chip *irqchip = irq_desc_get_chip(desc);
Ryder Leeb0996312017-08-10 14:34:59 +0800651 unsigned long status;
652 u32 virq;
653 u32 bit = INTX_SHIFT;
654
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800655 chained_irq_enter(irqchip, desc);
656
657 status = readl(port->base + PCIE_INT_STATUS);
658 if (status & INTX_MASK) {
Honghui Zhangd84c2462017-08-30 09:19:14 +0800659 for_each_set_bit_from(bit, &status, PCI_NUM_INTX + INTX_SHIFT) {
Ryder Leeb0996312017-08-10 14:34:59 +0800660 /* Clear the INTx */
661 writel(1 << bit, port->base + PCIE_INT_STATUS);
662 virq = irq_find_mapping(port->irq_domain,
663 bit - INTX_SHIFT);
664 generic_handle_irq(virq);
665 }
666 }
667
Honghui Zhang43e64092017-08-14 21:04:28 +0800668 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800669 if (status & MSI_STATUS){
Honghui Zhang43e64092017-08-14 21:04:28 +0800670 unsigned long imsi_status;
671
672 while ((imsi_status = readl(port->base + PCIE_IMSI_STATUS))) {
673 for_each_set_bit(bit, &imsi_status, MTK_MSI_IRQS_NUM) {
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800674 virq = irq_find_mapping(port->inner_domain, bit);
Honghui Zhang43e64092017-08-14 21:04:28 +0800675 generic_handle_irq(virq);
676 }
677 }
678 /* Clear MSI interrupt status */
679 writel(MSI_STATUS, port->base + PCIE_INT_STATUS);
680 }
681 }
682
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800683 chained_irq_exit(irqchip, desc);
684
685 return;
Ryder Leeb0996312017-08-10 14:34:59 +0800686}
687
688static int mtk_pcie_setup_irq(struct mtk_pcie_port *port,
689 struct device_node *node)
690{
691 struct mtk_pcie *pcie = port->pcie;
692 struct device *dev = pcie->dev;
693 struct platform_device *pdev = to_platform_device(dev);
694 int err, irq;
695
Ryder Leeb0996312017-08-10 14:34:59 +0800696 err = mtk_pcie_init_irq_domain(port, node);
697 if (err) {
Honghui Zhang43e64092017-08-14 21:04:28 +0800698 dev_err(dev, "failed to init PCIe IRQ domain\n");
Ryder Leeb0996312017-08-10 14:34:59 +0800699 return err;
700 }
701
Honghui Zhang42fe2f92018-05-04 13:47:33 +0800702 irq = platform_get_irq(pdev, port->slot);
703 irq_set_chained_handler_and_data(irq, mtk_pcie_intr_handler, port);
704
Ryder Leeb0996312017-08-10 14:34:59 +0800705 return 0;
706}
707
Ryder Lee637cfaca2017-05-21 11:42:24 +0800708static void __iomem *mtk_pcie_map_bus(struct pci_bus *bus,
709 unsigned int devfn, int where)
710{
Honghui Zhangdb271742017-08-14 21:04:27 +0800711 struct mtk_pcie *pcie = bus->sysdata;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800712
713 writel(PCIE_CONF_ADDR(where, PCI_FUNC(devfn), PCI_SLOT(devfn),
714 bus->number), pcie->base + PCIE_CFG_ADDR);
715
716 return pcie->base + PCIE_CFG_DATA + (where & 3);
717}
718
719static struct pci_ops mtk_pcie_ops = {
720 .map_bus = mtk_pcie_map_bus,
721 .read = pci_generic_config_read,
722 .write = pci_generic_config_write,
723};
724
Ryder Leee10b7a12017-08-10 14:34:54 +0800725static int mtk_pcie_startup_port(struct mtk_pcie_port *port)
Ryder Lee637cfaca2017-05-21 11:42:24 +0800726{
727 struct mtk_pcie *pcie = port->pcie;
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800728 u32 func = PCI_FUNC(port->slot << 3);
729 u32 slot = PCI_SLOT(port->slot << 3);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800730 u32 val;
Ryder Leee10b7a12017-08-10 14:34:54 +0800731 int err;
732
733 /* assert port PERST_N */
734 val = readl(pcie->base + PCIE_SYS_CFG);
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800735 val |= PCIE_PORT_PERST(port->slot);
Ryder Leee10b7a12017-08-10 14:34:54 +0800736 writel(val, pcie->base + PCIE_SYS_CFG);
737
738 /* de-assert port PERST_N */
739 val = readl(pcie->base + PCIE_SYS_CFG);
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800740 val &= ~PCIE_PORT_PERST(port->slot);
Ryder Leee10b7a12017-08-10 14:34:54 +0800741 writel(val, pcie->base + PCIE_SYS_CFG);
742
743 /* 100ms timeout value should be enough for Gen1/2 training */
744 err = readl_poll_timeout(port->base + PCIE_LINK_STATUS, val,
745 !!(val & PCIE_PORT_LINKUP), 20,
746 100 * USEC_PER_MSEC);
747 if (err)
748 return -ETIMEDOUT;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800749
750 /* enable interrupt */
751 val = readl(pcie->base + PCIE_INT_ENABLE);
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800752 val |= PCIE_PORT_INT_EN(port->slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800753 writel(val, pcie->base + PCIE_INT_ENABLE);
754
755 /* map to all DDR region. We need to set it before cfg operation. */
756 writel(PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
757 port->base + PCIE_BAR0_SETUP);
758
759 /* configure class code and revision ID */
760 writel(PCIE_CLASS_CODE | PCIE_REVISION_ID, port->base + PCIE_CLASS);
761
762 /* configure FC credit */
763 writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0),
764 pcie->base + PCIE_CFG_ADDR);
765 val = readl(pcie->base + PCIE_CFG_DATA);
766 val &= ~PCIE_FC_CREDIT_MASK;
767 val |= PCIE_FC_CREDIT_VAL(0x806c);
768 writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0),
769 pcie->base + PCIE_CFG_ADDR);
770 writel(val, pcie->base + PCIE_CFG_DATA);
771
772 /* configure RC FTS number to 250 when it leaves L0s */
773 writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0),
774 pcie->base + PCIE_CFG_ADDR);
775 val = readl(pcie->base + PCIE_CFG_DATA);
776 val &= ~PCIE_FTS_NUM_MASK;
777 val |= PCIE_FTS_NUM_L0(0x50);
778 writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0),
779 pcie->base + PCIE_CFG_ADDR);
780 writel(val, pcie->base + PCIE_CFG_DATA);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800781
Ryder Leee10b7a12017-08-10 14:34:54 +0800782 return 0;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800783}
784
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800785static void mtk_pcie_enable_port(struct mtk_pcie_port *port)
Ryder Lee637cfaca2017-05-21 11:42:24 +0800786{
Honghui Zhangc681c932017-08-10 14:34:56 +0800787 struct mtk_pcie *pcie = port->pcie;
788 struct device *dev = pcie->dev;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800789 int err;
790
791 err = clk_prepare_enable(port->sys_ck);
792 if (err) {
Ryder Leeb0996312017-08-10 14:34:59 +0800793 dev_err(dev, "failed to enable sys_ck%d clock\n", port->slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800794 goto err_sys_clk;
795 }
796
Ryder Leeb0996312017-08-10 14:34:59 +0800797 err = clk_prepare_enable(port->ahb_ck);
798 if (err) {
799 dev_err(dev, "failed to enable ahb_ck%d\n", port->slot);
800 goto err_ahb_clk;
801 }
802
803 err = clk_prepare_enable(port->aux_ck);
804 if (err) {
805 dev_err(dev, "failed to enable aux_ck%d\n", port->slot);
806 goto err_aux_clk;
807 }
808
809 err = clk_prepare_enable(port->axi_ck);
810 if (err) {
811 dev_err(dev, "failed to enable axi_ck%d\n", port->slot);
812 goto err_axi_clk;
813 }
814
815 err = clk_prepare_enable(port->obff_ck);
816 if (err) {
817 dev_err(dev, "failed to enable obff_ck%d\n", port->slot);
818 goto err_obff_clk;
819 }
820
821 err = clk_prepare_enable(port->pipe_ck);
822 if (err) {
823 dev_err(dev, "failed to enable pipe_ck%d\n", port->slot);
824 goto err_pipe_clk;
825 }
826
Ryder Lee637cfaca2017-05-21 11:42:24 +0800827 reset_control_assert(port->reset);
828 reset_control_deassert(port->reset);
829
Ryder Leeb0996312017-08-10 14:34:59 +0800830 err = phy_init(port->phy);
831 if (err) {
832 dev_err(dev, "failed to initialize port%d phy\n", port->slot);
833 goto err_phy_init;
834 }
835
Ryder Lee637cfaca2017-05-21 11:42:24 +0800836 err = phy_power_on(port->phy);
837 if (err) {
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800838 dev_err(dev, "failed to power on port%d phy\n", port->slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800839 goto err_phy_on;
840 }
841
Honghui Zhangc681c932017-08-10 14:34:56 +0800842 if (!pcie->soc->startup(port))
Ryder Lee637cfaca2017-05-21 11:42:24 +0800843 return;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800844
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800845 dev_info(dev, "Port%d link down\n", port->slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800846
847 phy_power_off(port->phy);
848err_phy_on:
Ryder Leeb0996312017-08-10 14:34:59 +0800849 phy_exit(port->phy);
850err_phy_init:
851 clk_disable_unprepare(port->pipe_ck);
852err_pipe_clk:
853 clk_disable_unprepare(port->obff_ck);
854err_obff_clk:
855 clk_disable_unprepare(port->axi_ck);
856err_axi_clk:
857 clk_disable_unprepare(port->aux_ck);
858err_aux_clk:
859 clk_disable_unprepare(port->ahb_ck);
860err_ahb_clk:
Ryder Lee637cfaca2017-05-21 11:42:24 +0800861 clk_disable_unprepare(port->sys_ck);
862err_sys_clk:
863 mtk_pcie_port_free(port);
864}
865
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800866static int mtk_pcie_parse_port(struct mtk_pcie *pcie,
867 struct device_node *node,
868 int slot)
Ryder Lee637cfaca2017-05-21 11:42:24 +0800869{
870 struct mtk_pcie_port *port;
871 struct resource *regs;
872 struct device *dev = pcie->dev;
873 struct platform_device *pdev = to_platform_device(dev);
874 char name[10];
875 int err;
876
877 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
878 if (!port)
879 return -ENOMEM;
880
881 err = of_property_read_u32(node, "num-lanes", &port->lane);
882 if (err) {
883 dev_err(dev, "missing num-lanes property\n");
884 return err;
885 }
886
Ryder Lee1eacd7b2017-08-10 14:34:57 +0800887 snprintf(name, sizeof(name), "port%d", slot);
888 regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800889 port->base = devm_ioremap_resource(dev, regs);
890 if (IS_ERR(port->base)) {
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800891 dev_err(dev, "failed to map port%d base\n", slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800892 return PTR_ERR(port->base);
893 }
894
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800895 snprintf(name, sizeof(name), "sys_ck%d", slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800896 port->sys_ck = devm_clk_get(dev, name);
897 if (IS_ERR(port->sys_ck)) {
Ryder Leeb0996312017-08-10 14:34:59 +0800898 dev_err(dev, "failed to get sys_ck%d clock\n", slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800899 return PTR_ERR(port->sys_ck);
900 }
901
Ryder Leeb0996312017-08-10 14:34:59 +0800902 /* sys_ck might be divided into the following parts in some chips */
903 snprintf(name, sizeof(name), "ahb_ck%d", slot);
904 port->ahb_ck = devm_clk_get(dev, name);
905 if (IS_ERR(port->ahb_ck)) {
906 if (PTR_ERR(port->ahb_ck) == -EPROBE_DEFER)
907 return -EPROBE_DEFER;
908
909 port->ahb_ck = NULL;
910 }
911
912 snprintf(name, sizeof(name), "axi_ck%d", slot);
913 port->axi_ck = devm_clk_get(dev, name);
914 if (IS_ERR(port->axi_ck)) {
915 if (PTR_ERR(port->axi_ck) == -EPROBE_DEFER)
916 return -EPROBE_DEFER;
917
918 port->axi_ck = NULL;
919 }
920
921 snprintf(name, sizeof(name), "aux_ck%d", slot);
922 port->aux_ck = devm_clk_get(dev, name);
923 if (IS_ERR(port->aux_ck)) {
924 if (PTR_ERR(port->aux_ck) == -EPROBE_DEFER)
925 return -EPROBE_DEFER;
926
927 port->aux_ck = NULL;
928 }
929
930 snprintf(name, sizeof(name), "obff_ck%d", slot);
931 port->obff_ck = devm_clk_get(dev, name);
932 if (IS_ERR(port->obff_ck)) {
933 if (PTR_ERR(port->obff_ck) == -EPROBE_DEFER)
934 return -EPROBE_DEFER;
935
936 port->obff_ck = NULL;
937 }
938
939 snprintf(name, sizeof(name), "pipe_ck%d", slot);
940 port->pipe_ck = devm_clk_get(dev, name);
941 if (IS_ERR(port->pipe_ck)) {
942 if (PTR_ERR(port->pipe_ck) == -EPROBE_DEFER)
943 return -EPROBE_DEFER;
944
945 port->pipe_ck = NULL;
946 }
947
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800948 snprintf(name, sizeof(name), "pcie-rst%d", slot);
Philipp Zabel608fcac2017-07-19 17:26:00 +0200949 port->reset = devm_reset_control_get_optional_exclusive(dev, name);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800950 if (PTR_ERR(port->reset) == -EPROBE_DEFER)
951 return PTR_ERR(port->reset);
952
953 /* some platforms may use default PHY setting */
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800954 snprintf(name, sizeof(name), "pcie-phy%d", slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800955 port->phy = devm_phy_optional_get(dev, name);
956 if (IS_ERR(port->phy))
957 return PTR_ERR(port->phy);
958
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800959 port->slot = slot;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800960 port->pcie = pcie;
961
Ryder Leeb0996312017-08-10 14:34:59 +0800962 if (pcie->soc->setup_irq) {
963 err = pcie->soc->setup_irq(port, node);
964 if (err)
965 return err;
966 }
967
Ryder Lee637cfaca2017-05-21 11:42:24 +0800968 INIT_LIST_HEAD(&port->list);
969 list_add_tail(&port->list, &pcie->ports);
970
971 return 0;
972}
973
974static int mtk_pcie_subsys_powerup(struct mtk_pcie *pcie)
975{
976 struct device *dev = pcie->dev;
977 struct platform_device *pdev = to_platform_device(dev);
978 struct resource *regs;
979 int err;
980
Ryder Lee1eacd7b2017-08-10 14:34:57 +0800981 /* get shared registers, which are optional */
982 regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "subsys");
983 if (regs) {
984 pcie->base = devm_ioremap_resource(dev, regs);
985 if (IS_ERR(pcie->base)) {
986 dev_err(dev, "failed to map shared register\n");
987 return PTR_ERR(pcie->base);
988 }
Ryder Lee637cfaca2017-05-21 11:42:24 +0800989 }
990
991 pcie->free_ck = devm_clk_get(dev, "free_ck");
992 if (IS_ERR(pcie->free_ck)) {
993 if (PTR_ERR(pcie->free_ck) == -EPROBE_DEFER)
994 return -EPROBE_DEFER;
995
996 pcie->free_ck = NULL;
997 }
998
Honghui Zhang88c0e232018-10-15 16:08:54 +0800999 pm_runtime_enable(dev);
1000 pm_runtime_get_sync(dev);
Ryder Lee637cfaca2017-05-21 11:42:24 +08001001
1002 /* enable top level clock */
1003 err = clk_prepare_enable(pcie->free_ck);
1004 if (err) {
1005 dev_err(dev, "failed to enable free_ck\n");
1006 goto err_free_ck;
1007 }
1008
1009 return 0;
1010
1011err_free_ck:
Honghui Zhang88c0e232018-10-15 16:08:54 +08001012 pm_runtime_put_sync(dev);
1013 pm_runtime_disable(dev);
Ryder Lee637cfaca2017-05-21 11:42:24 +08001014
1015 return err;
1016}
1017
1018static int mtk_pcie_setup(struct mtk_pcie *pcie)
1019{
1020 struct device *dev = pcie->dev;
1021 struct device_node *node = dev->of_node, *child;
1022 struct of_pci_range_parser parser;
1023 struct of_pci_range range;
1024 struct resource res;
1025 struct mtk_pcie_port *port, *tmp;
1026 int err;
1027
1028 if (of_pci_range_parser_init(&parser, node)) {
1029 dev_err(dev, "missing \"ranges\" property\n");
1030 return -EINVAL;
1031 }
1032
1033 for_each_of_pci_range(&parser, &range) {
1034 err = of_pci_range_to_resource(&range, node, &res);
1035 if (err < 0)
1036 return err;
1037
1038 switch (res.flags & IORESOURCE_TYPE_BITS) {
1039 case IORESOURCE_IO:
1040 pcie->offset.io = res.start - range.pci_addr;
1041
1042 memcpy(&pcie->pio, &res, sizeof(res));
1043 pcie->pio.name = node->full_name;
1044
1045 pcie->io.start = range.cpu_addr;
1046 pcie->io.end = range.cpu_addr + range.size - 1;
1047 pcie->io.flags = IORESOURCE_MEM;
1048 pcie->io.name = "I/O";
1049
1050 memcpy(&res, &pcie->io, sizeof(res));
1051 break;
1052
1053 case IORESOURCE_MEM:
1054 pcie->offset.mem = res.start - range.pci_addr;
1055
1056 memcpy(&pcie->mem, &res, sizeof(res));
1057 pcie->mem.name = "non-prefetchable";
1058 break;
1059 }
1060 }
1061
1062 err = of_pci_parse_bus_range(node, &pcie->busn);
1063 if (err < 0) {
1064 dev_err(dev, "failed to parse bus ranges property: %d\n", err);
1065 pcie->busn.name = node->name;
1066 pcie->busn.start = 0;
1067 pcie->busn.end = 0xff;
1068 pcie->busn.flags = IORESOURCE_BUS;
1069 }
1070
1071 for_each_available_child_of_node(node, child) {
Honghui Zhang4f6f0462017-08-10 14:34:55 +08001072 int slot;
Ryder Lee637cfaca2017-05-21 11:42:24 +08001073
1074 err = of_pci_get_devfn(child);
1075 if (err < 0) {
1076 dev_err(dev, "failed to parse devfn: %d\n", err);
1077 return err;
1078 }
1079
Honghui Zhang4f6f0462017-08-10 14:34:55 +08001080 slot = PCI_SLOT(err);
Ryder Lee637cfaca2017-05-21 11:42:24 +08001081
Honghui Zhang4f6f0462017-08-10 14:34:55 +08001082 err = mtk_pcie_parse_port(pcie, child, slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +08001083 if (err)
1084 return err;
1085 }
1086
1087 err = mtk_pcie_subsys_powerup(pcie);
1088 if (err)
1089 return err;
1090
1091 /* enable each port, and then check link status */
1092 list_for_each_entry_safe(port, tmp, &pcie->ports, list)
Honghui Zhang4f6f0462017-08-10 14:34:55 +08001093 mtk_pcie_enable_port(port);
Ryder Lee637cfaca2017-05-21 11:42:24 +08001094
1095 /* power down PCIe subsys if slots are all empty (link down) */
1096 if (list_empty(&pcie->ports))
1097 mtk_pcie_subsys_powerdown(pcie);
1098
1099 return 0;
1100}
1101
1102static int mtk_pcie_request_resources(struct mtk_pcie *pcie)
1103{
1104 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1105 struct list_head *windows = &host->windows;
1106 struct device *dev = pcie->dev;
1107 int err;
1108
1109 pci_add_resource_offset(windows, &pcie->pio, pcie->offset.io);
1110 pci_add_resource_offset(windows, &pcie->mem, pcie->offset.mem);
1111 pci_add_resource(windows, &pcie->busn);
1112
1113 err = devm_request_pci_bus_resources(dev, windows);
1114 if (err < 0)
1115 return err;
1116
Gustavo A. R. Silva17a0a1e2018-07-20 10:01:58 -05001117 err = devm_pci_remap_iospace(dev, &pcie->pio, pcie->io.start);
1118 if (err)
1119 return err;
Ryder Lee637cfaca2017-05-21 11:42:24 +08001120
1121 return 0;
1122}
1123
1124static int mtk_pcie_register_host(struct pci_host_bridge *host)
1125{
1126 struct mtk_pcie *pcie = pci_host_bridge_priv(host);
1127 struct pci_bus *child;
1128 int err;
1129
1130 host->busnr = pcie->busn.start;
1131 host->dev.parent = pcie->dev;
Honghui Zhangc681c932017-08-10 14:34:56 +08001132 host->ops = pcie->soc->ops;
Ryder Lee637cfaca2017-05-21 11:42:24 +08001133 host->map_irq = of_irq_parse_and_map_pci;
1134 host->swizzle_irq = pci_common_swizzle;
Ryder Leeb0996312017-08-10 14:34:59 +08001135 host->sysdata = pcie;
Ryder Lee637cfaca2017-05-21 11:42:24 +08001136
1137 err = pci_scan_root_bus_bridge(host);
1138 if (err < 0)
1139 return err;
1140
1141 pci_bus_size_bridges(host->bus);
1142 pci_bus_assign_resources(host->bus);
1143
1144 list_for_each_entry(child, &host->bus->children, node)
1145 pcie_bus_configure_settings(child);
1146
1147 pci_bus_add_devices(host->bus);
1148
1149 return 0;
1150}
1151
1152static int mtk_pcie_probe(struct platform_device *pdev)
1153{
1154 struct device *dev = &pdev->dev;
1155 struct mtk_pcie *pcie;
1156 struct pci_host_bridge *host;
1157 int err;
1158
1159 host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
1160 if (!host)
1161 return -ENOMEM;
1162
1163 pcie = pci_host_bridge_priv(host);
1164
1165 pcie->dev = dev;
Honghui Zhangc681c932017-08-10 14:34:56 +08001166 pcie->soc = of_device_get_match_data(dev);
Ryder Lee637cfaca2017-05-21 11:42:24 +08001167 platform_set_drvdata(pdev, pcie);
1168 INIT_LIST_HEAD(&pcie->ports);
1169
1170 err = mtk_pcie_setup(pcie);
1171 if (err)
1172 return err;
1173
1174 err = mtk_pcie_request_resources(pcie);
1175 if (err)
1176 goto put_resources;
1177
1178 err = mtk_pcie_register_host(host);
1179 if (err)
1180 goto put_resources;
1181
1182 return 0;
1183
1184put_resources:
1185 if (!list_empty(&pcie->ports))
1186 mtk_pcie_put_resources(pcie);
1187
1188 return err;
1189}
1190
Honghui Zhangc681c932017-08-10 14:34:56 +08001191static const struct mtk_pcie_soc mtk_pcie_soc_v1 = {
1192 .ops = &mtk_pcie_ops,
1193 .startup = mtk_pcie_startup_port,
1194};
1195
Honghui Zhang101c92d2018-05-04 13:47:32 +08001196static const struct mtk_pcie_soc mtk_pcie_soc_mt2712 = {
Honghui Zhang101c92d2018-05-04 13:47:32 +08001197 .ops = &mtk_pcie_ops_v2,
1198 .startup = mtk_pcie_startup_port_v2,
1199 .setup_irq = mtk_pcie_setup_irq,
1200};
1201
1202static const struct mtk_pcie_soc mtk_pcie_soc_mt7622 = {
1203 .need_fix_class_id = true,
Ryder Leeb0996312017-08-10 14:34:59 +08001204 .ops = &mtk_pcie_ops_v2,
1205 .startup = mtk_pcie_startup_port_v2,
1206 .setup_irq = mtk_pcie_setup_irq,
1207};
1208
Ryder Lee637cfaca2017-05-21 11:42:24 +08001209static const struct of_device_id mtk_pcie_ids[] = {
Honghui Zhangc681c932017-08-10 14:34:56 +08001210 { .compatible = "mediatek,mt2701-pcie", .data = &mtk_pcie_soc_v1 },
1211 { .compatible = "mediatek,mt7623-pcie", .data = &mtk_pcie_soc_v1 },
Honghui Zhang101c92d2018-05-04 13:47:32 +08001212 { .compatible = "mediatek,mt2712-pcie", .data = &mtk_pcie_soc_mt2712 },
1213 { .compatible = "mediatek,mt7622-pcie", .data = &mtk_pcie_soc_mt7622 },
Ryder Lee637cfaca2017-05-21 11:42:24 +08001214 {},
1215};
1216
1217static struct platform_driver mtk_pcie_driver = {
1218 .probe = mtk_pcie_probe,
1219 .driver = {
1220 .name = "mtk-pcie",
1221 .of_match_table = mtk_pcie_ids,
1222 .suppress_bind_attrs = true,
1223 },
1224};
1225builtin_platform_driver(mtk_pcie_driver);