blob: 0f6074abdebc726c9175dea6c57941a543b3dc89 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * MediaTek PCIe host controller driver.
4 *
5 * Copyright (c) 2017 MediaTek Inc.
6 * Author: Ryder Lee <ryder.lee@mediatek.com>
7 * Honghui Zhang <honghui.zhang@mediatek.com>
8 */
9
10#include <linux/clk.h>
11#include <linux/delay.h>
12#include <linux/iopoll.h>
13#include <linux/irq.h>
14#include <linux/irqchip/chained_irq.h>
15#include <linux/irqdomain.h>
16#include <linux/interrupt.h>
17#include <linux/kernel.h>
18#include <linux/msi.h>
19#include <linux/module.h>
20#include <linux/of_address.h>
21#include <linux/of_clk.h>
22#include <linux/of_pci.h>
23#include <linux/of_platform.h>
24#include <linux/pci.h>
25#include <linux/phy/phy.h>
26#include <linux/platform_device.h>
27#include <linux/pm_runtime.h>
28#include <linux/reset.h>
29
30#include "../pci.h"
31
32/* PCIe shared registers */
33#define PCIE_SYS_CFG 0x00
34#define PCIE_INT_ENABLE 0x0c
35#define PCIE_CFG_ADDR 0x20
36#define PCIE_CFG_DATA 0x24
37
38/* PCIe per port registers */
39#define PCIE_BAR0_SETUP 0x10
40#define PCIE_CLASS 0x34
41#define PCIE_LINK_STATUS 0x50
42
43#define PCIE_PORT_INT_EN(x) BIT(20 + (x))
44#define PCIE_PORT_PERST(x) BIT(1 + (x))
45#define PCIE_PORT_LINKUP BIT(0)
46#define PCIE_BAR_MAP_MAX GENMASK(31, 16)
47
48#define PCIE_BAR_ENABLE BIT(0)
49#define PCIE_REVISION_ID BIT(0)
50#define PCIE_CLASS_CODE (0x60400 << 8)
51#define PCIE_CONF_REG(regn) (((regn) & GENMASK(7, 2)) | \
52 ((((regn) >> 8) & GENMASK(3, 0)) << 24))
53#define PCIE_CONF_FUN(fun) (((fun) << 8) & GENMASK(10, 8))
54#define PCIE_CONF_DEV(dev) (((dev) << 11) & GENMASK(15, 11))
55#define PCIE_CONF_BUS(bus) (((bus) << 16) & GENMASK(23, 16))
56#define PCIE_CONF_ADDR(regn, fun, dev, bus) \
57 (PCIE_CONF_REG(regn) | PCIE_CONF_FUN(fun) | \
58 PCIE_CONF_DEV(dev) | PCIE_CONF_BUS(bus))
59
60/* MediaTek specific configuration registers */
61#define PCIE_FTS_NUM 0x70c
62#define PCIE_FTS_NUM_MASK GENMASK(15, 8)
63#define PCIE_FTS_NUM_L0(x) ((x) & 0xff << 8)
64
65#define PCIE_FC_CREDIT 0x73c
66#define PCIE_FC_CREDIT_MASK (GENMASK(31, 31) | GENMASK(28, 16))
67#define PCIE_FC_CREDIT_VAL(x) ((x) << 16)
68
69/* PCIe V2 share registers */
70#define PCIE_SYS_CFG_V2 0x0
71#define PCIE_CSR_LTSSM_EN(x) BIT(0 + (x) * 8)
72#define PCIE_CSR_ASPM_L1_EN(x) BIT(1 + (x) * 8)
73
74/* PCIe V2 per-port registers */
75#define PCIE_MSI_VECTOR 0x0c0
76
77#define PCIE_CONF_VEND_ID 0x100
78#define PCIE_CONF_CLASS_ID 0x106
79
80#define PCIE_INT_MASK 0x420
81#define INTX_MASK GENMASK(19, 16)
82#define INTX_SHIFT 16
83#define INTX_NUM 4
84#define PCIE_INT_STATUS 0x424
85#define MSI_STATUS BIT(23)
86#define PCIE_IMSI_STATUS 0x42c
87#define PCIE_IMSI_ADDR 0x430
88#define MSI_MASK BIT(23)
89#define MTK_MSI_IRQS_NUM 32
90
91#define PCIE_AHB_TRANS_BASE0_L 0x438
92#define PCIE_AHB_TRANS_BASE0_H 0x43c
93#define AHB2PCIE_SIZE(x) ((x) & GENMASK(4, 0))
94#define PCIE_AXI_WINDOW0 0x448
95#define WIN_ENABLE BIT(7)
96
97/* PCIe V2 configuration transaction header */
98#define PCIE_CFG_HEADER0 0x460
99#define PCIE_CFG_HEADER1 0x464
100#define PCIE_CFG_HEADER2 0x468
101#define PCIE_CFG_WDATA 0x470
102#define PCIE_APP_TLP_REQ 0x488
103#define PCIE_CFG_RDATA 0x48c
104#define APP_CFG_REQ BIT(0)
105#define APP_CPL_STATUS GENMASK(7, 5)
106
107#define CFG_WRRD_TYPE_0 4
108#define CFG_WR_FMT 2
109#define CFG_RD_FMT 0
110
111#define CFG_DW0_LENGTH(length) ((length) & GENMASK(9, 0))
112#define CFG_DW0_TYPE(type) (((type) << 24) & GENMASK(28, 24))
113#define CFG_DW0_FMT(fmt) (((fmt) << 29) & GENMASK(31, 29))
114#define CFG_DW2_REGN(regn) ((regn) & GENMASK(11, 2))
115#define CFG_DW2_FUN(fun) (((fun) << 16) & GENMASK(18, 16))
116#define CFG_DW2_DEV(dev) (((dev) << 19) & GENMASK(23, 19))
117#define CFG_DW2_BUS(bus) (((bus) << 24) & GENMASK(31, 24))
118#define CFG_HEADER_DW0(type, fmt) \
119 (CFG_DW0_LENGTH(1) | CFG_DW0_TYPE(type) | CFG_DW0_FMT(fmt))
120#define CFG_HEADER_DW1(where, size) \
121 (GENMASK(((size) - 1), 0) << ((where) & 0x3))
122#define CFG_HEADER_DW2(regn, fun, dev, bus) \
123 (CFG_DW2_REGN(regn) | CFG_DW2_FUN(fun) | \
124 CFG_DW2_DEV(dev) | CFG_DW2_BUS(bus))
125
126#define PCIE_RST_CTRL 0x510
127#define PCIE_PHY_RSTB BIT(0)
128#define PCIE_PIPE_SRSTB BIT(1)
129#define PCIE_MAC_SRSTB BIT(2)
130#define PCIE_CRSTB BIT(3)
131#define PCIE_PERSTB BIT(8)
132#define PCIE_LINKDOWN_RST_EN GENMASK(15, 13)
133#define PCIE_LINK_STATUS_V2 0x804
134#define PCIE_PORT_LINKUP_V2 BIT(10)
135
136
137/* PCIe V3 CfgWr/CfgRd registers */
138#define PCIE_CFGNUM 0x140
139#define CFG_DEVFN(devfn) ((devfn) & GENMASK(7, 0))
140#define CFG_BUS(busno) (((busno) << 8) & GENMASK(15, 8))
141#define CFG_BYTE_EN GENMASK(19, 16)
142#define CFG_FORCE_BYTE_EN 0
143
144#define CFG_HEADER(devfn, busno) \
145 (CFG_DEVFN(devfn) | CFG_BUS(busno) | CFG_BYTE_EN | CFG_FORCE_BYTE_EN)
146
147/* PCI Interrupt registers */
148#define PCIE_INT_MASK_V3 0x180
149#define MSI_MASK_V3 BIT(8)
150#define L2_ENTRY_WAKE_MASK_V3 BIT(23)
151#define INTA_MASK_V3 BIT(24)
152#define INTB_MASK_V3 BIT(25)
153#define INTC_MASK_V3 BIT(26)
154#define INTD_MASK_V3 BIT(27)
155#define INTX_MASK_V3 (INTA_MASK_V3 | \
156 INTB_MASK_V3 | INTC_MASK_V3 | INTD_MASK_V3)
157#define MTK_PCIE_INTX_SHIFT_V3 24
158#define LTR_HP_EVENT_MASK_V3 BIT(28)
159#define PM_EVENT_MASK_V3 BIT(30)
160
161#define PCIE_INT_STATUS_V3 0x184
162#define MSI_STATUS_V3 BIT(8)
163#define L2_ENTRY_WAKE_STATUS_V3 BIT(23)
164#define INTA_STATUS_V3 BIT(24)
165#define INTB_STATUS_V3 BIT(25)
166#define INTC_STATUS_V3 BIT(26)
167#define INTD_STATUS_V3 BIT(27)
168#define LTR_HP_EVENT_STATUS_V3 BIT(28)
169#define AER_EVENT_STATUS_V3 BIT(29)
170#define PM_EVENT_STATUS_V3 BIT(30)
171
172/* PCI settings */
173#define PCIE_IF_TIMEOUT 0x344
174#define PTX_TIMEOUT_DISABLE BIT(7)
175#define PCIE_MISC_CTRL 0x348
176#define PCIE_SETTING 0x80
177#define PCIE_RC_MODE BIT(0)
178
179/* PCI MAC registers */
180#define PCIE_IDS2 0x9c
181#define PCI_CLASS(class) (class << 8)
182#define PCIE_IREG_PEX_SPC 0xd4
183#define SLOT_REG_IMPL BIT(12)
184#define PCIE_PEX_LINK 0xc8
185#define ASPM_L1_TIMER_RECOUNT BIT(21)
186#define PCIE_RST_CTRL_V3 0x148
187#define PCIE_MAC_RSTB_V3 BIT(0)
188#define PCIE_PHY_RSTB_V3 BIT(1)
189#define PCIE_BRG_RSTB_V3 BIT(2)
190#define PCIE_PE_RSTB_V3 BIT(3)
191
192#define PCIE_ICMD_PM 0x198
193#define Turn_Off_Link BIT(4)
194
195#define PCIE_LINK_STATUS_V3 0x150
196#define PCIE_PORT_LINKUP_V3 BIT(4)
197
198#define PCIE_DATA_LINK_STATUS_V3 0x154
199#define PCIE_DATA_LINKUP_V3 BIT(8)
200
201#define PCIE_INT_STATUS_V3 0x184
202#define MSI_GRP_STATUS(x) BIT(8 + (x))
203
204#define PCIE_MSI_GRP_EN 0x190
205#define MSI_GRP_ENABLE(x) BIT(x)
206
207#define PCIE_MSI_ADDR_GRP(x) (0xC00 + 0x10 * (x))
208#define PCIE_MSI_STATUS_GRP(x) (0xC04 + 0x10 * (x))
209#define PCIE_MSI_ENABLE_GRP(x) (0xC08 + 0x10 * (x))
210
211#define MSI_ENABLE 0x190
212#define MSI_VECTOR 0xC00
213#define MSI_VECTOR_MASK (~0x3fff)
214#define IMSI_STATUS 0xC04
215#define MSI_INT_MASK 0xC08
216#define MSI_IRQS 32
217#define PCIE_PORT_MSI_BIT 32
218#define MAX_MSI_IRQS (MSI_IRQS + 1)
219#define INTX_IRQ_NUM 5
220
221#define AXI_SLV0_T0_BASE 0x800
222#define AXI_SLV0_T0_PAR_SRC_LSB (AXI_SLV0_T0_BASE + 0x00)
223#define AXI_SLV0_T0_SRC_MSB (AXI_SLV0_T0_BASE + 0x04)
224#define AXI_SLV0_T0_TRSL_LSB (AXI_SLV0_T0_BASE + 0x08)
225#define AXI_SLV0_T0_TRSL_MSB (AXI_SLV0_T0_BASE + 0x0c)
226#define AXI_SLV0_T0_TRSL_PAR (AXI_SLV0_T0_BASE + 0x10)
227#define ATR_IMPL BIT(0)
228
229#define AXI_SLV0_T1_BASE 0x820
230#define AXI_SLV0_T1_PAR_SRC_LSB (AXI_SLV0_T1_BASE + 0x00)
231#define AXI_SLV0_T1_SRC_MSB (AXI_SLV0_T1_BASE + 0x04)
232#define AXI_SLV0_T1_TRSL_LSB (AXI_SLV0_T1_BASE + 0x08)
233#define AXI_SLV0_T1_TRSL_MSB (AXI_SLV0_T1_BASE + 0x0c)
234#define AXI_SLV0_T1_TRSL_PAR (AXI_SLV0_T1_BASE + 0x10)
235
236#define ATR_SIZE(size) ((size & 0x3f) << 1)
237#define ATR_SRC_ADDR_L(base) (base & GENMASK(31, 12))
238#define ATR_ID(id) (id & 0xf)
239#define ATR_PARAM(param) ((param & 0xfff) << 16)
240
241#define CFG_OFFSET_ADDR 0x1000
242
243#define PCI_VENDOR_ID_MEDIATEK 0x14c3
244
245struct mtk_pcie_port;
246
247/**
248 * struct mtk_pcie_irq_info - interrupts related register information
249 * @int_status: interrupt status register
250 * @int_mask: interrupt mask register
251 * @msi_status: MSI status register
252 * @msi_addr: MSI address register
253 * @intx_shift: INTx offset bit on interrupt status register
254 * @msi_mask_bit: MSI mask bit on interrupt mask register
255 * @intx_mask_bit: INTx mask bit on interrupt mask register
256 * @enable_msi_group: pointer to MSI group select functions
257 */
258struct mtk_pcie_irq_info {
259 u32 int_status;
260 u32 int_mask;
261 u32 msi_status;
262 u32 msi_addr;
263 int intx_shift;
264 int msi_mask_bit;
265 int intx_mask_bit;
266 void (*enable_msi_group)(struct mtk_pcie_port *port, int group);
267};
268
269/**
270 * struct mtk_pcie_soc - differentiate between host generations
271 * @need_fix_class_id: whether this host's class ID needed to be fixed or not
272 * @pm_support: whether the host's MTCMOS will be off when suspend
273 * @ops: pointer to configuration access functions
274 * @startup: pointer to controller setting functions
275 * @setup_irq: pointer to initialize IRQ functions
276 */
277struct mtk_pcie_soc {
278 bool need_fix_class_id;
279 bool pm_support;
280 struct pci_ops *ops;
281 int (*startup)(struct mtk_pcie_port *port);
282 int (*setup_irq)(struct mtk_pcie_port *port, struct device_node *node);
283 struct mtk_pcie_irq_info *irq_info;
284};
285
286/**
287 * struct mtk_pcie_port - PCIe port information
288 * @base: IO mapped register base
289 * @list: port list
290 * @pcie: pointer to PCIe host info
291 * @reset: pointer to port reset control
292 * @clks: pointer to PCIe clocks
293 * @num_clks: PCIe clock count
294 * @phy: pointer to PHY control block
295 * @lane: lane count
296 * @slot: port slot
297 * @irq_domain: legacy INTx IRQ domain
298 * @inner_domain: inner IRQ domain
299 * @msi_domain: MSI IRQ domain
300 * @lock: protect the msi_irq_in_use bitmap
301 * @msi_irq_in_use: bit map for assigned MSI IRQ
302 */
303struct mtk_pcie_port {
304 void __iomem *base;
305 struct list_head list;
306 struct mtk_pcie *pcie;
307 struct reset_control *reset;
308 struct clk **clks;
309 int num_clks;
310 struct phy *phy;
311 u32 lane;
312 u32 slot;
313 int irq;
314 struct irq_domain *irq_domain;
315 struct irq_domain *inner_domain;
316 struct irq_domain *msi_domain;
317 struct mutex lock;
318 DECLARE_BITMAP(msi_irq_in_use, MTK_MSI_IRQS_NUM);
319};
320
321/**
322 * struct mtk_pcie - PCIe host information
323 * @dev: pointer to PCIe device
324 * @base: IO mapped register base
325 * @io: IO resource
326 * @pio: PIO resource
327 * @mem: non-prefetchable memory resource
328 * @busn: bus range
329 * @offset: IO / Memory offset
330 * @ports: pointer to PCIe port information
331 * @soc: pointer to SoC-dependent operations
332 */
333struct mtk_pcie {
334 struct device *dev;
335 void __iomem *base;
336
337 struct resource io;
338 struct resource pio;
339 struct resource mem;
340 struct resource busn;
341 struct {
342 resource_size_t mem;
343 resource_size_t io;
344 } offset;
345 struct list_head ports;
346 const struct mtk_pcie_soc *soc;
347};
348
349static void mtk_pcie_subsys_powerdown(struct mtk_pcie *pcie)
350{
351 struct device *dev = pcie->dev;
352
353 if (dev->pm_domain) {
354 pm_runtime_put_sync(dev);
355 pm_runtime_disable(dev);
356 }
357}
358
359static void mtk_pcie_port_free(struct mtk_pcie_port *port)
360{
361 struct mtk_pcie *pcie = port->pcie;
362 struct device *dev = pcie->dev;
363
364 devm_iounmap(dev, port->base);
365 list_del(&port->list);
366 devm_kfree(dev, port);
367}
368
369static int mtk_pcie_disable_clk(struct mtk_pcie_port *port)
370{
371 int i;
372
373 for (i = 0; i < port->num_clks; i ++) {
374 clk_disable_unprepare(port->clks[i]);
375 clk_put(port->clks[i]);
376 }
377 port->num_clks = 0;
378
379 return 0;
380}
381
382static void mtk_pcie_put_resources(struct mtk_pcie *pcie)
383{
384 struct mtk_pcie_port *port, *tmp;
385
386 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
387 phy_power_off(port->phy);
388 phy_exit(port->phy);
389 mtk_pcie_disable_clk(port);
390 mtk_pcie_port_free(port);
391 }
392
393 mtk_pcie_subsys_powerdown(pcie);
394}
395
396static int mtk_pcie_check_cfg_cpld(struct mtk_pcie_port *port)
397{
398 u32 val;
399 int err;
400
401 err = readl_poll_timeout_atomic(port->base + PCIE_APP_TLP_REQ, val,
402 !(val & APP_CFG_REQ), 10,
403 100 * USEC_PER_MSEC);
404 if (err)
405 return PCIBIOS_SET_FAILED;
406
407 if (readl(port->base + PCIE_APP_TLP_REQ) & APP_CPL_STATUS)
408 return PCIBIOS_SET_FAILED;
409
410 return PCIBIOS_SUCCESSFUL;
411}
412
413static int mtk_pcie_hw_rd_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn,
414 int where, int size, u32 *val)
415{
416 u32 tmp;
417
418 /* Write PCIe configuration transaction header for Cfgrd */
419 writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_RD_FMT),
420 port->base + PCIE_CFG_HEADER0);
421 writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1);
422 writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus),
423 port->base + PCIE_CFG_HEADER2);
424
425 /* Trigger h/w to transmit Cfgrd TLP */
426 tmp = readl(port->base + PCIE_APP_TLP_REQ);
427 tmp |= APP_CFG_REQ;
428 writel(tmp, port->base + PCIE_APP_TLP_REQ);
429
430 /* Check completion status */
431 if (mtk_pcie_check_cfg_cpld(port))
432 return PCIBIOS_SET_FAILED;
433
434 /* Read cpld payload of Cfgrd */
435 *val = readl(port->base + PCIE_CFG_RDATA);
436
437 if (size == 1)
438 *val = (*val >> (8 * (where & 3))) & 0xff;
439 else if (size == 2)
440 *val = (*val >> (8 * (where & 3))) & 0xffff;
441
442 return PCIBIOS_SUCCESSFUL;
443}
444
445static int mtk_pcie_hw_wr_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn,
446 int where, int size, u32 val)
447{
448 /* Write PCIe configuration transaction header for Cfgwr */
449 writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_WR_FMT),
450 port->base + PCIE_CFG_HEADER0);
451 writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1);
452 writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus),
453 port->base + PCIE_CFG_HEADER2);
454
455 /* Write Cfgwr data */
456 val = val << 8 * (where & 3);
457 writel(val, port->base + PCIE_CFG_WDATA);
458
459 /* Trigger h/w to transmit Cfgwr TLP */
460 val = readl(port->base + PCIE_APP_TLP_REQ);
461 val |= APP_CFG_REQ;
462 writel(val, port->base + PCIE_APP_TLP_REQ);
463
464 /* Check completion status */
465 return mtk_pcie_check_cfg_cpld(port);
466}
467
468static struct mtk_pcie_port *mtk_pcie_find_port(struct pci_bus *bus,
469 unsigned int devfn)
470{
471 struct mtk_pcie *pcie = bus->sysdata;
472 struct mtk_pcie_port *port;
473 struct pci_dev *dev;
474 struct pci_bus *pbus;
475
476 list_for_each_entry(port, &pcie->ports, list) {
477 if (bus->number == 0 && port->slot == PCI_SLOT(devfn)) {
478 return port;
479 } else if (bus->number != 0) {
480 pbus = bus;
481 do {
482 dev = pbus->self;
483 if (port->slot == PCI_SLOT(dev->devfn))
484 return port;
485
486 pbus = dev->bus;
487 } while (dev->bus->number != 0);
488 }
489 }
490
491 return NULL;
492}
493
494static int mtk_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
495 int where, int size, u32 *val)
496{
497 struct mtk_pcie_port *port;
498 u32 bn = bus->number;
499 int ret;
500
501 port = mtk_pcie_find_port(bus, devfn);
502 if (!port) {
503 *val = ~0;
504 return PCIBIOS_DEVICE_NOT_FOUND;
505 }
506
507 ret = mtk_pcie_hw_rd_cfg(port, bn, devfn, where, size, val);
508 if (ret)
509 *val = ~0;
510
511 return ret;
512}
513
514static int mtk_pcie_config_write(struct pci_bus *bus, unsigned int devfn,
515 int where, int size, u32 val)
516{
517 struct mtk_pcie_port *port;
518 u32 bn = bus->number;
519
520 port = mtk_pcie_find_port(bus, devfn);
521 if (!port)
522 return PCIBIOS_DEVICE_NOT_FOUND;
523
524 return mtk_pcie_hw_wr_cfg(port, bn, devfn, where, size, val);
525}
526
527static struct pci_ops mtk_pcie_ops_v2 = {
528 .read = mtk_pcie_config_read,
529 .write = mtk_pcie_config_write,
530};
531
532static void mtk_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
533{
534 struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data);
535 struct mtk_pcie_irq_info *irq_info = port->pcie->soc->irq_info;
536 phys_addr_t addr;
537
538 /* MT2712/MT7622 only support 32-bit MSI addresses */
539 addr = readl(port->base + irq_info->msi_addr);
540 msg->address_hi = 0;
541 msg->address_lo = lower_32_bits(addr);
542
543 msg->data = data->hwirq;
544
545 dev_dbg(port->pcie->dev, "msi#%d address_hi %#x address_lo %#x\n",
546 (int)data->hwirq, msg->address_hi, msg->address_lo);
547}
548
549static int mtk_msi_set_affinity(struct irq_data *irq_data,
550 const struct cpumask *mask, bool force)
551{
552 return -EINVAL;
553}
554
555static void mtk_msi_ack_irq(struct irq_data *data)
556{
557 struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data);
558 struct mtk_pcie_irq_info *irq_info = port->pcie->soc->irq_info;
559 u32 hwirq = data->hwirq;
560
561 writel(1 << hwirq, port->base + irq_info->msi_status);
562}
563
564static struct irq_chip mtk_msi_bottom_irq_chip = {
565 .name = "MTK MSI",
566 .irq_compose_msi_msg = mtk_compose_msi_msg,
567 .irq_set_affinity = mtk_msi_set_affinity,
568 .irq_ack = mtk_msi_ack_irq,
569};
570
571static int mtk_pcie_irq_domain_alloc(struct irq_domain *domain,
572 unsigned int virq, unsigned int nr_irqs,
573 void *args)
574{
575 struct mtk_pcie_port *port = domain->host_data;
576 unsigned long bit;
577
578 WARN_ON(nr_irqs != 1);
579 mutex_lock(&port->lock);
580
581 bit = find_first_zero_bit(port->msi_irq_in_use, MTK_MSI_IRQS_NUM);
582 if (bit >= MTK_MSI_IRQS_NUM) {
583 mutex_unlock(&port->lock);
584 return -ENOSPC;
585 }
586
587 __set_bit(bit, port->msi_irq_in_use);
588
589 mutex_unlock(&port->lock);
590
591 irq_domain_set_info(domain, virq, bit, &mtk_msi_bottom_irq_chip,
592 domain->host_data, handle_edge_irq,
593 NULL, NULL);
594
595 return 0;
596}
597
598static void mtk_pcie_irq_domain_free(struct irq_domain *domain,
599 unsigned int virq, unsigned int nr_irqs)
600{
601 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
602 struct mtk_pcie_port *port = irq_data_get_irq_chip_data(d);
603
604 mutex_lock(&port->lock);
605
606 if (!test_bit(d->hwirq, port->msi_irq_in_use))
607 dev_err(port->pcie->dev, "trying to free unused MSI#%lu\n",
608 d->hwirq);
609 else
610 __clear_bit(d->hwirq, port->msi_irq_in_use);
611
612 mutex_unlock(&port->lock);
613
614 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
615}
616
617static const struct irq_domain_ops msi_domain_ops = {
618 .alloc = mtk_pcie_irq_domain_alloc,
619 .free = mtk_pcie_irq_domain_free,
620};
621
622static struct irq_chip mtk_msi_irq_chip = {
623 .name = "MTK PCIe MSI",
624 .irq_ack = irq_chip_ack_parent,
625 .irq_mask = pci_msi_mask_irq,
626 .irq_unmask = pci_msi_unmask_irq,
627};
628
629static struct msi_domain_info mtk_msi_domain_info = {
630 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
631 MSI_FLAG_PCI_MSIX),
632 .chip = &mtk_msi_irq_chip,
633};
634
635static int mtk_pcie_allocate_msi_domains(struct mtk_pcie_port *port)
636{
637 struct fwnode_handle *fwnode;
638
639 mutex_init(&port->lock);
640
641 fwnode = of_node_to_fwnode(port->pcie->dev->of_node);
642 port->inner_domain = irq_domain_create_linear(fwnode, MTK_MSI_IRQS_NUM,
643 &msi_domain_ops, port);
644 if (!port->inner_domain) {
645 dev_err(port->pcie->dev, "failed to create IRQ domain\n");
646 return -ENOMEM;
647 }
648
649 port->msi_domain = pci_msi_create_irq_domain(fwnode,
650 &mtk_msi_domain_info,
651 port->inner_domain);
652 if (!port->msi_domain) {
653 dev_err(port->pcie->dev, "failed to create MSI domain\n");
654 irq_domain_remove(port->inner_domain);
655 return -ENOMEM;
656 }
657
658 return 0;
659}
660
661static void mtk_pcie_enable_msi(struct mtk_pcie_port *port)
662{
663 u32 val;
664 phys_addr_t msg_addr;
665 struct mtk_pcie_irq_info *irq_info = port->pcie->soc->irq_info;
666
667 msg_addr = virt_to_phys(port->base);
668 val = lower_32_bits(msg_addr) & MSI_VECTOR_MASK;
669 writel(val, port->base + irq_info->msi_addr);
670
671 if (irq_info->enable_msi_group) {
672 irq_info->enable_msi_group(port, 0);
673 } else {
674 val = readl(port->base + irq_info->int_mask);
675 val |= irq_info->msi_mask_bit;
676 writel(val, port->base + irq_info->int_mask);
677 }
678}
679
680static void mtk_pcie_irq_teardown(struct mtk_pcie *pcie)
681{
682 struct mtk_pcie_port *port, *tmp;
683
684 if (list_empty(&pcie->ports))
685 return;
686
687 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
688 irq_set_chained_handler_and_data(port->irq, NULL, NULL);
689
690 if (port->irq_domain)
691 irq_domain_remove(port->irq_domain);
692 if (port->msi_domain)
693 irq_domain_remove(port->msi_domain);
694 if (port->inner_domain)
695 irq_domain_remove(port->inner_domain);
696
697 irq_dispose_mapping(port->irq);
698 }
699}
700
701static int mtk_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
702 irq_hw_number_t hwirq)
703{
704 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
705 irq_set_chip_data(irq, domain->host_data);
706
707 return 0;
708}
709
710static const struct irq_domain_ops intx_domain_ops = {
711 .map = mtk_pcie_intx_map,
712};
713
714static int mtk_pcie_init_irq_domain(struct mtk_pcie_port *port,
715 struct device_node *node)
716{
717 struct device *dev = port->pcie->dev;
718 struct device_node *pcie_intc_node;
719 int ret;
720
721 /* Setup INTx */
722 pcie_intc_node = of_get_next_child(node, NULL);
723 if (!pcie_intc_node) {
724 dev_err(dev, "no PCIe Intc node found\n");
725 return -ENODEV;
726 }
727
728 port->irq_domain = irq_domain_add_linear(pcie_intc_node, INTX_NUM,
729 &intx_domain_ops, port);
730 if (!port->irq_domain) {
731 dev_err(dev, "failed to get INTx IRQ domain\n");
732 return -ENODEV;
733 }
734
735 if (IS_ENABLED(CONFIG_PCI_MSI)) {
736 ret = mtk_pcie_allocate_msi_domains(port);
737 if (ret)
738 return ret;
739 }
740
741 return 0;
742}
743
744static void mtk_pcie_intr_handler(struct irq_desc *desc)
745{
746 struct mtk_pcie_port *port = irq_desc_get_handler_data(desc);
747 struct mtk_pcie_irq_info *irq_info = port->pcie->soc->irq_info;
748 struct irq_chip *irqchip = irq_desc_get_chip(desc);
749 unsigned long status;
750 u32 virq, val, mask;
751 u32 bit = irq_info->intx_shift;
752
753 chained_irq_enter(irqchip, desc);
754
755 val = readl(port->base + irq_info->int_status);
756 mask = readl(port->base + irq_info->int_mask);
757 status = val & mask;
758
759 if (status & irq_info->intx_mask_bit) {
760 for_each_set_bit_from(bit, &status, PCI_NUM_INTX +
761 irq_info->intx_shift) {
762 virq = irq_find_mapping(port->irq_domain,
763 bit - irq_info->intx_shift);
764 generic_handle_irq(virq);
765 /* Clear the INTx */
766 writel(1 << bit, port->base + irq_info->int_status);
767 }
768 }
769
770 if (IS_ENABLED(CONFIG_PCI_MSI)) {
771 if (status & irq_info->msi_mask_bit) {
772 unsigned long imsi_status;
773
774 while ((imsi_status = readl(port->base +
775 irq_info->msi_status))) {
776 for_each_set_bit(bit, &imsi_status,
777 MTK_MSI_IRQS_NUM) {
778 virq = irq_find_mapping(
779 port->inner_domain, bit);
780 generic_handle_irq(virq);
781 }
782 }
783 /* Clear MSI interrupt status */
784 writel(irq_info->msi_mask_bit, port->base +
785 irq_info->int_status);
786 }
787 }
788
789 chained_irq_exit(irqchip, desc);
790}
791
792struct mtk_pcie_irq_info irq_info_v2 = {
793 .int_status = PCIE_INT_STATUS,
794 .msi_status = PCIE_IMSI_STATUS,
795 .msi_addr = PCIE_IMSI_ADDR,
796 .int_mask = PCIE_INT_MASK,
797 .intx_shift = INTX_SHIFT,
798 .intx_mask_bit = INTX_MASK,
799 .msi_mask_bit = MSI_MASK,
800};
801
802static void mtk_enable_msi_group(struct mtk_pcie_port *port, int group)
803{
804 int val;
805
806 writel(MSI_GRP_ENABLE(group), port->base + PCIE_MSI_GRP_EN);
807
808 val = readl(port->base + PCIE_INT_MASK_V3);
809 writel(MSI_GRP_STATUS(group) | val, port->base + PCIE_INT_MASK_V3);
810 writel(~0, port->base + PCIE_MSI_ENABLE_GRP(group));
811}
812
813struct mtk_pcie_irq_info irq_info_v3 = {
814 .int_status = PCIE_INT_STATUS_V3,
815 .msi_status = IMSI_STATUS,
816 .msi_addr = MSI_VECTOR,
817 .int_mask = PCIE_INT_MASK_V3,
818 .intx_shift = MTK_PCIE_INTX_SHIFT_V3,
819 .intx_mask_bit = INTX_MASK_V3,
820 .msi_mask_bit = MSI_MASK_V3,
821 .enable_msi_group = mtk_enable_msi_group,
822};
823
824static int mtk_pcie_setup_irq(struct mtk_pcie_port *port,
825 struct device_node *node)
826{
827 struct mtk_pcie *pcie = port->pcie;
828 struct device *dev = pcie->dev;
829 struct platform_device *pdev = to_platform_device(dev);
830 int err;
831
832 err = mtk_pcie_init_irq_domain(port, node);
833 if (err) {
834 dev_err(dev, "failed to init PCIe IRQ domain\n");
835 return err;
836 }
837
838 port->irq = platform_get_irq(pdev, 0);
839 irq_set_chained_handler_and_data(port->irq,
840 mtk_pcie_intr_handler, port);
841
842 return 0;
843}
844
845static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)
846{
847 struct mtk_pcie *pcie = port->pcie;
848 struct resource *mem = &pcie->mem;
849 const struct mtk_pcie_soc *soc = port->pcie->soc;
850 u32 val;
851 size_t size;
852 int err;
853
854 /* MT7622 platforms need to enable LTSSM and ASPM from PCIe subsys */
855 if (pcie->base) {
856 val = readl(pcie->base + PCIE_SYS_CFG_V2);
857 val |= PCIE_CSR_LTSSM_EN(port->slot) |
858 PCIE_CSR_ASPM_L1_EN(port->slot);
859 writel(val, pcie->base + PCIE_SYS_CFG_V2);
860 }
861
862 /* Assert all reset signals */
863 writel(0, port->base + PCIE_RST_CTRL);
864
865 /*
866 * Enable PCIe link down reset, if link status changed from link up to
867 * link down, this will reset MAC control registers and configuration
868 * space.
869 */
870 writel(PCIE_LINKDOWN_RST_EN, port->base + PCIE_RST_CTRL);
871
872 /* De-assert PHY, PE, PIPE, MAC and configuration reset */
873 val = readl(port->base + PCIE_RST_CTRL);
874 val |= PCIE_PHY_RSTB | PCIE_PERSTB | PCIE_PIPE_SRSTB |
875 PCIE_MAC_SRSTB | PCIE_CRSTB;
876 writel(val, port->base + PCIE_RST_CTRL);
877
878 /* Set up vendor ID and class code */
879 if (soc->need_fix_class_id) {
880 val = PCI_VENDOR_ID_MEDIATEK;
881 writew(val, port->base + PCIE_CONF_VEND_ID);
882
883 val = PCI_CLASS_BRIDGE_HOST;
884 writew(val, port->base + PCIE_CONF_CLASS_ID);
885 }
886
887 /* 100ms timeout value should be enough for Gen1/2 training */
888 err = readl_poll_timeout(port->base + PCIE_LINK_STATUS_V2, val,
889 !!(val & PCIE_PORT_LINKUP_V2), 20,
890 100 * USEC_PER_MSEC);
891 if (err)
892 return -ETIMEDOUT;
893
894 /* Set INTx mask */
895 val = readl(port->base + PCIE_INT_MASK);
896 val &= ~INTX_MASK;
897 writel(val, port->base + PCIE_INT_MASK);
898
899 if (IS_ENABLED(CONFIG_PCI_MSI))
900 mtk_pcie_enable_msi(port);
901
902 /* Set AHB to PCIe translation windows */
903 size = mem->end - mem->start;
904 val = lower_32_bits(mem->start) | AHB2PCIE_SIZE(fls(size));
905 writel(val, port->base + PCIE_AHB_TRANS_BASE0_L);
906
907 val = upper_32_bits(mem->start);
908 writel(val, port->base + PCIE_AHB_TRANS_BASE0_H);
909
910 /* Set PCIe to AXI translation memory space.*/
911 val = fls(0xffffffff) | WIN_ENABLE;
912 writel(val, port->base + PCIE_AXI_WINDOW0);
913
914 return 0;
915}
916
917static void __iomem *mtk_pcie_map_bus_v3(struct pci_bus *bus,
918 unsigned int devfn, int where)
919{
920 struct mtk_pcie_port *port;
921
922 port = mtk_pcie_find_port(bus, devfn);
923 if (!port)
924 return NULL;
925
926 writel(CFG_HEADER(devfn, bus->number), port->base + PCIE_CFGNUM);
927
928 return port->base + CFG_OFFSET_ADDR + where;
929}
930
931static struct pci_ops mtk_pcie_ops_v3 = {
932 .map_bus = mtk_pcie_map_bus_v3,
933 .read = pci_generic_config_read,
934 .write = pci_generic_config_write,
935};
936
937static int mtk_pcie_port_enable(void)
938{
939 int val = 0;
940 void __iomem *infra_base, *remap_base, *phy_base;
941
942 infra_base = ioremap(0x10001000, 0x1000);
943 remap_base = ioremap(0x10041000, 0x100);
944 phy_base = ioremap(0x10005000, 0x1000);
945
946 /* Set phy mode to RC for port 0 */
947 val = readl(phy_base + 0x600);
948 val |= 1 << 14;
949 writel(val, phy_base + 0x600);
950
951 /* Enable AXI clock for port1*/
952 val = readl(infra_base + 0xe0);
953 //val |= BIT(14);
954 val |= 0x1c000; //For ECO version
955 writel(val, infra_base + 0xe0);
956
957 /* Enable PCIe LTSSM */
958 val = readl(infra_base + 0xc80);
959 val |= 0x1;
960 writel(val, infra_base + 0xc80);
961
962 /* Remap address of AP/MD to PCIe */
963 writel(0x3, remap_base + 0x28);
964 writel(0x0, remap_base + 0x2c);
965
966 iounmap(infra_base);
967 iounmap(remap_base);
968 iounmap(phy_base);
969
970 return 0;
971}
972
973static int mtk_pcie_startup_port_v3(struct mtk_pcie_port *port)
974{
975 int size, val, err;
976 struct resource *mem = &port->pcie->mem;
977 const struct mtk_pcie_soc *soc = port->pcie->soc;
978
979 mtk_pcie_port_enable();
980
981 /* disable hw trapping and set as RC mode */
982 writel(BIT(31), port->base + PCIE_MISC_CTRL);
983 val = readl(port->base + PCIE_SETTING);
984 writel(val | PCIE_RC_MODE, port->base + PCIE_SETTING);
985
986 /* Setup class code */
987 writel(0x06040000, port->base + 0x9c);
988
989 val = readl(port->base + 0x74);
990 writel(val | BIT(19), port->base + 0x74);
991
992 /* bypass dvfsrc, only used on emulation */
993 val = readl(port->base + 0x348);
994 writel(val | BIT(1), port->base + 0x348);
995
996 /* assert all reset signals */
997 val = readl(port->base + PCIE_RST_CTRL_V3);
998 val |= PCIE_MAC_RSTB_V3 | PCIE_PHY_RSTB_V3 | PCIE_BRG_RSTB_V3|
999 PCIE_PE_RSTB_V3;
1000 writel(val, port->base + PCIE_RST_CTRL_V3);
1001 usleep_range(500, 1000);
1002
1003 /* de-assert reset signals*/
1004 val &= ~(PCIE_MAC_RSTB_V3 | PCIE_PHY_RSTB_V3 | PCIE_BRG_RSTB_V3);
1005 writel(val, port->base + PCIE_RST_CTRL_V3);
1006
1007 usleep_range(100 * 1000, 120 * 1000);
1008
1009 /* de-assert pe reset signals*/
1010 val &= ~PCIE_PE_RSTB_V3;
1011 writel(val, port->base + PCIE_RST_CTRL_V3);
1012
1013 /* Set up vendor ID and class code */
1014 if (soc->need_fix_class_id) {
1015 val = readl(port->base + PCIE_IDS2) & 0xff;
1016 writel(val | PCI_CLASS(PCI_CLASS_BRIDGE_PCI << 8),
1017 port->base + PCIE_IDS2);
1018 }
1019
1020 /* 100ms timeout value should be enough for Gen1/2 training */
1021 err = readl_poll_timeout(port->base + PCIE_DATA_LINK_STATUS_V3, val,
1022 !!(val & PCIE_DATA_LINKUP_V3), 20,
1023 200 * USEC_PER_MSEC);
1024 if (err)
1025 return -ETIMEDOUT;
1026
1027 /* set INT mask */
1028 writel(INTX_MASK_V3, port->base + PCIE_INT_MASK_V3);
1029
1030 if (IS_ENABLED(CONFIG_PCI_MSI))
1031 mtk_pcie_enable_msi(port);
1032
1033 /* Set AHB to PCIe translation windows */
1034 size = fls(mem->end - mem->start) - 1;
1035 writel(ATR_SRC_ADDR_L(mem->start) | ATR_SIZE(size) | ATR_IMPL,
1036 port->base + AXI_SLV0_T0_PAR_SRC_LSB);
1037 writel(upper_32_bits(mem->start), port->base + AXI_SLV0_T0_SRC_MSB);
1038
1039 writel(ATR_SRC_ADDR_L(mem->start), port->base + AXI_SLV0_T0_TRSL_LSB);
1040 writel(upper_32_bits(mem->start), port->base + AXI_SLV0_T0_TRSL_MSB);
1041
1042 writel(ATR_ID(0) | ATR_PARAM(0), port->base + AXI_SLV0_T0_TRSL_PAR);
1043
1044 return 0;
1045}
1046
1047static void __iomem *mtk_pcie_map_bus(struct pci_bus *bus,
1048 unsigned int devfn, int where)
1049{
1050 struct mtk_pcie *pcie = bus->sysdata;
1051
1052 writel(PCIE_CONF_ADDR(where, PCI_FUNC(devfn), PCI_SLOT(devfn),
1053 bus->number), pcie->base + PCIE_CFG_ADDR);
1054
1055 return pcie->base + PCIE_CFG_DATA + (where & 3);
1056}
1057
1058static struct pci_ops mtk_pcie_ops = {
1059 .map_bus = mtk_pcie_map_bus,
1060 .read = pci_generic_config_read,
1061 .write = pci_generic_config_write,
1062};
1063
1064static int mtk_pcie_startup_port(struct mtk_pcie_port *port)
1065{
1066 struct mtk_pcie *pcie = port->pcie;
1067 u32 func = PCI_FUNC(port->slot << 3);
1068 u32 slot = PCI_SLOT(port->slot << 3);
1069 u32 val;
1070 int err;
1071
1072 /* assert port PERST_N */
1073 val = readl(pcie->base + PCIE_SYS_CFG);
1074 val |= PCIE_PORT_PERST(port->slot);
1075 writel(val, pcie->base + PCIE_SYS_CFG);
1076
1077 /* de-assert port PERST_N */
1078 val = readl(pcie->base + PCIE_SYS_CFG);
1079 val &= ~PCIE_PORT_PERST(port->slot);
1080 writel(val, pcie->base + PCIE_SYS_CFG);
1081
1082 /* 100ms timeout value should be enough for Gen1/2 training */
1083 err = readl_poll_timeout(port->base + PCIE_LINK_STATUS, val,
1084 !!(val & PCIE_PORT_LINKUP), 20,
1085 100 * USEC_PER_MSEC);
1086 if (err)
1087 return -ETIMEDOUT;
1088
1089 /* enable interrupt */
1090 val = readl(pcie->base + PCIE_INT_ENABLE);
1091 val |= PCIE_PORT_INT_EN(port->slot);
1092 writel(val, pcie->base + PCIE_INT_ENABLE);
1093
1094 /* map to all DDR region. We need to set it before cfg operation. */
1095 writel(PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
1096 port->base + PCIE_BAR0_SETUP);
1097
1098 /* configure class code and revision ID */
1099 writel(PCIE_CLASS_CODE | PCIE_REVISION_ID, port->base + PCIE_CLASS);
1100
1101 /* configure FC credit */
1102 writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0),
1103 pcie->base + PCIE_CFG_ADDR);
1104 val = readl(pcie->base + PCIE_CFG_DATA);
1105 val &= ~PCIE_FC_CREDIT_MASK;
1106 val |= PCIE_FC_CREDIT_VAL(0x806c);
1107 writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0),
1108 pcie->base + PCIE_CFG_ADDR);
1109 writel(val, pcie->base + PCIE_CFG_DATA);
1110
1111 /* configure RC FTS number to 250 when it leaves L0s */
1112 writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0),
1113 pcie->base + PCIE_CFG_ADDR);
1114 val = readl(pcie->base + PCIE_CFG_DATA);
1115 val &= ~PCIE_FTS_NUM_MASK;
1116 val |= PCIE_FTS_NUM_L0(0x50);
1117 writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0),
1118 pcie->base + PCIE_CFG_ADDR);
1119 writel(val, pcie->base + PCIE_CFG_DATA);
1120
1121 return 0;
1122}
1123
1124static int mtk_pcie_clk_init(struct mtk_pcie_port *port)
1125{
1126 struct mtk_pcie *pcie = port->pcie;
1127 struct device *dev = pcie->dev;
1128 struct device_node *np = dev->of_node;
1129 int i;
1130
1131 port->num_clks = of_clk_get_parent_count(np);
1132 if (port->num_clks == 0) {
1133 dev_warn(dev, "pcie clock is not found\n");
1134 return 0;
1135 }
1136
1137 port->clks = devm_kzalloc(dev, port->num_clks, GFP_KERNEL);
1138 if (!port->clks)
1139 return -ENOMEM;
1140
1141 for (i = 0; i < port->num_clks; i++) {
1142 struct clk *clk;
1143 int ret;
1144
1145 clk = of_clk_get(np, i);
1146 if (IS_ERR(clk)) {
1147 while (--i >= 0)
1148 clk_put(port->clks[i]);
1149 return PTR_ERR(clk);
1150 }
1151
1152 ret = clk_prepare_enable(clk);
1153 if (ret < 0) {
1154 while (--i >= 0) {
1155 clk_disable_unprepare(port->clks[i]);
1156 clk_put(port->clks[i]);
1157 }
1158 clk_put(clk);
1159
1160 return ret;
1161 }
1162
1163 port->clks[i] = clk;
1164 }
1165
1166 return 0;
1167}
1168
1169static void mtk_pcie_enable_port(struct mtk_pcie_port *port)
1170{
1171 struct mtk_pcie *pcie = port->pcie;
1172 struct device *dev = pcie->dev;
1173 int err;
1174
1175 err = phy_init(port->phy);
1176 if (err) {
1177 dev_err(dev, "failed to initialize port%d phy\n", port->slot);
1178 goto err_clk;
1179 }
1180
1181 err = phy_power_on(port->phy);
1182 if (err) {
1183 dev_err(dev, "failed to power on port%d phy\n", port->slot);
1184 goto err_phy_on;
1185 }
1186
1187 err = mtk_pcie_clk_init(port);
1188 if (err) {
1189 dev_err(dev, "failed to enable clocks\n");
1190 goto err_clk;
1191 }
1192
1193 if (!pcie->soc->startup(port)) {
1194 dev_info(dev, "Port%d link up success!\n", port->slot);
1195 /* For independent port, the slot number is always zero */
1196 port->slot = 0;
1197 return;
1198 }
1199
1200 dev_info(dev, "Port%d link down\n", port->slot);
1201
1202 phy_power_off(port->phy);
1203err_phy_on:
1204 phy_exit(port->phy);
1205 mtk_pcie_disable_clk(port);
1206err_clk:
1207 mtk_pcie_port_free(port);
1208}
1209
1210static int mtk_pcie_parse_port(struct mtk_pcie *pcie,
1211 struct device_node *node,
1212 int slot)
1213{
1214 struct mtk_pcie_port *port;
1215 struct resource *regs;
1216 struct device *dev = pcie->dev;
1217 struct platform_device *pdev = to_platform_device(dev);
1218 char name[10];
1219 int err;
1220
1221 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1222 if (!port)
1223 return -ENOMEM;
1224
1225 err = of_property_read_u32(node, "num-lanes", &port->lane);
1226 if (err) {
1227 dev_err(dev, "missing num-lanes property\n");
1228 return err;
1229 }
1230
1231 snprintf(name, sizeof(name), "port%d", slot);
1232 regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
1233 port->base = devm_ioremap_resource(dev, regs);
1234 if (IS_ERR(port->base)) {
1235 dev_err(dev, "failed to map port%d base\n", slot);
1236 return PTR_ERR(port->base);
1237 }
1238
1239 /* some platforms may use default PHY setting */
1240 port->phy = devm_phy_optional_get(dev, "pcie-phy");
1241 if (IS_ERR(port->phy))
1242 port->phy = NULL;
1243
1244 port->slot = slot;
1245 port->pcie = pcie;
1246
1247 if (pcie->soc->setup_irq) {
1248 err = pcie->soc->setup_irq(port, node);
1249 if (err)
1250 return err;
1251 }
1252
1253 INIT_LIST_HEAD(&port->list);
1254 list_add_tail(&port->list, &pcie->ports);
1255
1256 return 0;
1257}
1258
1259static int mtk_pcie_subsys_powerup(struct mtk_pcie *pcie)
1260{
1261 struct device *dev = pcie->dev;
1262 struct platform_device *pdev = to_platform_device(dev);
1263 struct resource *regs;
1264
1265 /* get shared registers, which are optional */
1266 regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "subsys");
1267 if (regs) {
1268 pcie->base = devm_ioremap_resource(dev, regs);
1269 if (IS_ERR(pcie->base)) {
1270 dev_err(dev, "failed to map shared register\n");
1271 return PTR_ERR(pcie->base);
1272 }
1273 }
1274
1275 if (dev->pm_domain) {
1276 pm_runtime_enable(dev);
1277 pm_runtime_get_sync(dev);
1278 }
1279
1280 return 0;
1281}
1282
1283static int mtk_pcie_setup(struct mtk_pcie *pcie)
1284{
1285 struct device *dev = pcie->dev;
1286 struct device_node *node = dev->of_node, *child;
1287 struct of_pci_range_parser parser;
1288 struct of_pci_range range;
1289 struct resource res;
1290 struct mtk_pcie_port *port, *tmp;
1291 int err;
1292
1293 if (of_pci_range_parser_init(&parser, node)) {
1294 dev_err(dev, "missing \"ranges\" property\n");
1295 return -EINVAL;
1296 }
1297
1298 for_each_of_pci_range(&parser, &range) {
1299 err = of_pci_range_to_resource(&range, node, &res);
1300 if (err < 0)
1301 return err;
1302
1303 switch (res.flags & IORESOURCE_TYPE_BITS) {
1304 case IORESOURCE_IO:
1305 pcie->offset.io = res.start - range.pci_addr;
1306
1307 memcpy(&pcie->pio, &res, sizeof(res));
1308 pcie->pio.name = node->full_name;
1309
1310 pcie->io.start = range.cpu_addr;
1311 pcie->io.end = range.cpu_addr + range.size - 1;
1312 pcie->io.flags = IORESOURCE_MEM;
1313 pcie->io.name = "I/O";
1314
1315 memcpy(&res, &pcie->io, sizeof(res));
1316 break;
1317
1318 case IORESOURCE_MEM:
1319 pcie->offset.mem = res.start - range.pci_addr;
1320
1321 memcpy(&pcie->mem, &res, sizeof(res));
1322 pcie->mem.name = "non-prefetchable";
1323 break;
1324 }
1325 }
1326
1327 err = of_pci_parse_bus_range(node, &pcie->busn);
1328 if (err < 0) {
1329 dev_err(dev, "failed to parse bus ranges property: %d\n", err);
1330 pcie->busn.name = node->name;
1331 pcie->busn.start = 0;
1332 pcie->busn.end = 0xff;
1333 pcie->busn.flags = IORESOURCE_BUS;
1334 }
1335
1336 for_each_available_child_of_node(node, child) {
1337 int slot;
1338
1339 err = of_pci_get_devfn(child);
1340 if (err < 0) {
1341 dev_err(dev, "failed to parse devfn: %d\n", err);
1342 return err;
1343 }
1344
1345 slot = PCI_SLOT(err);
1346
1347 err = mtk_pcie_parse_port(pcie, child, slot);
1348 if (err)
1349 return err;
1350 }
1351
1352 err = mtk_pcie_subsys_powerup(pcie);
1353 if (err)
1354 return err;
1355
1356 /* enable each port, and then check link status */
1357 list_for_each_entry_safe(port, tmp, &pcie->ports, list)
1358 mtk_pcie_enable_port(port);
1359
1360 /* power down PCIe subsys if slots are all empty (link down) */
1361 if (list_empty(&pcie->ports))
1362 mtk_pcie_subsys_powerdown(pcie);
1363
1364 return 0;
1365}
1366
1367static int mtk_pcie_request_resources(struct mtk_pcie *pcie)
1368{
1369 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1370 struct list_head *windows = &host->windows;
1371 struct device *dev = pcie->dev;
1372 int err;
1373
1374 pci_add_resource_offset(windows, &pcie->pio, pcie->offset.io);
1375 pci_add_resource_offset(windows, &pcie->mem, pcie->offset.mem);
1376 pci_add_resource(windows, &pcie->busn);
1377
1378 err = devm_request_pci_bus_resources(dev, windows);
1379 if (err < 0)
1380 return err;
1381
1382 pci_remap_iospace(&pcie->pio, pcie->io.start);
1383
1384 return 0;
1385}
1386
1387static int mtk_pcie_register_host(struct pci_host_bridge *host)
1388{
1389 struct mtk_pcie *pcie = pci_host_bridge_priv(host);
1390 struct pci_bus *child;
1391 int err;
1392
1393 host->busnr = pcie->busn.start;
1394 host->dev.parent = pcie->dev;
1395 host->ops = pcie->soc->ops;
1396 host->map_irq = of_irq_parse_and_map_pci;
1397 host->swizzle_irq = pci_common_swizzle;
1398 host->sysdata = pcie;
1399
1400 err = pci_scan_root_bus_bridge(host);
1401 if (err < 0)
1402 return err;
1403
1404 pci_bus_size_bridges(host->bus);
1405 pci_bus_assign_resources(host->bus);
1406
1407 list_for_each_entry(child, &host->bus->children, node)
1408 pcie_bus_configure_settings(child);
1409
1410 pci_bus_add_devices(host->bus);
1411
1412 return 0;
1413}
1414
1415static int mtk_pcie_probe(struct platform_device *pdev)
1416{
1417 struct device *dev = &pdev->dev;
1418 struct mtk_pcie *pcie;
1419 struct pci_host_bridge *host;
1420 int err;
1421
1422 host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
1423 if (!host)
1424 return -ENOMEM;
1425
1426 pcie = pci_host_bridge_priv(host);
1427
1428 pcie->dev = dev;
1429 pcie->soc = of_device_get_match_data(dev);
1430 platform_set_drvdata(pdev, pcie);
1431 INIT_LIST_HEAD(&pcie->ports);
1432
1433 err = mtk_pcie_setup(pcie);
1434 if (err)
1435 return err;
1436
1437 err = mtk_pcie_request_resources(pcie);
1438 if (err)
1439 goto put_resources;
1440
1441 err = mtk_pcie_register_host(host);
1442 if (err)
1443 goto put_resources;
1444
1445 return 0;
1446
1447put_resources:
1448 if (!list_empty(&pcie->ports))
1449 mtk_pcie_put_resources(pcie);
1450
1451 return err;
1452}
1453
1454static void mtk_pcie_free_resources(struct mtk_pcie *pcie)
1455{
1456 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1457 struct list_head *windows = &host->windows;
1458
1459 pci_unmap_iospace(&pcie->pio);
1460 pci_free_resource_list(windows);
1461}
1462
1463static int mtk_pcie_remove(struct platform_device *pdev)
1464{
1465 struct mtk_pcie *pcie = platform_get_drvdata(pdev);
1466 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1467
1468 pci_stop_root_bus(host->bus);
1469 pci_remove_root_bus(host->bus);
1470 mtk_pcie_free_resources(pcie);
1471
1472 if (IS_ENABLED(CONFIG_PCI_MSI))
1473 mtk_pcie_irq_teardown(pcie);
1474
1475 if (!list_empty(&pcie->ports))
1476 mtk_pcie_put_resources(pcie);
1477
1478 return 0;
1479}
1480
1481#ifdef CONFIG_PM_SLEEP
1482static int mtk_pcie_suspend_noirq(struct device *dev)
1483{
1484 struct mtk_pcie *pcie = dev_get_drvdata(dev);
1485 const struct mtk_pcie_soc *soc = pcie->soc;
1486 struct mtk_pcie_port *port;
1487 int i;
1488
1489 if (!soc->pm_support)
1490 return 0;
1491
1492 list_for_each_entry(port, &pcie->ports, list) {
1493 phy_power_off(port->phy);
1494
1495 for (i = 0; i < port->num_clks; i ++)
1496 clk_disable_unprepare(port->clks[i]);
1497 }
1498
1499 return 0;
1500}
1501
1502static int mtk_pcie_resume_noirq(struct device *dev)
1503{
1504 struct mtk_pcie *pcie = dev_get_drvdata(dev);
1505 const struct mtk_pcie_soc *soc = pcie->soc;
1506 struct mtk_pcie_port *port;
1507 int ret, i;
1508
1509 if (!soc->pm_support)
1510 return 0;
1511
1512 list_for_each_entry(port, &pcie->ports, list) {
1513 phy_power_on(port->phy);
1514 for (i = 0; i < port->num_clks; i ++)
1515 clk_prepare_enable(port->clks[i]);
1516
1517 ret = soc->startup(port);
1518 if (ret) {
1519 dev_err(dev, "Port%d link down\n", port->slot);
1520 phy_power_off(port->phy);
1521 for (i = 0; i < port->num_clks; i ++)
1522 clk_disable_unprepare(port->clks[i]);
1523 return ret;
1524 }
1525
1526 if (IS_ENABLED(CONFIG_PCI_MSI))
1527 mtk_pcie_enable_msi(port);
1528 }
1529
1530 return 0;
1531}
1532#endif
1533
1534static const struct dev_pm_ops mtk_pcie_pm_ops = {
1535 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_pcie_suspend_noirq,
1536 mtk_pcie_resume_noirq)
1537};
1538
1539static const struct mtk_pcie_soc mtk_pcie_soc_v1 = {
1540 .ops = &mtk_pcie_ops,
1541 .startup = mtk_pcie_startup_port,
1542};
1543
1544static const struct mtk_pcie_soc mtk_pcie_soc_mt2712 = {
1545 .pm_support = true,
1546 .ops = &mtk_pcie_ops_v2,
1547 .startup = mtk_pcie_startup_port_v2,
1548 .setup_irq = mtk_pcie_setup_irq,
1549 .irq_info = &irq_info_v2,
1550};
1551
1552static const struct mtk_pcie_soc mtk_pcie_soc_mt7622 = {
1553 .need_fix_class_id = true,
1554 .ops = &mtk_pcie_ops_v2,
1555 .startup = mtk_pcie_startup_port_v2,
1556 .setup_irq = mtk_pcie_setup_irq,
1557 .irq_info = &irq_info_v2,
1558};
1559
1560static const struct mtk_pcie_soc mtk_pcie_soc_v3 = {
1561 .need_fix_class_id = false,
1562 .ops = &mtk_pcie_ops_v3,
1563 .startup = mtk_pcie_startup_port_v3,
1564 .setup_irq = mtk_pcie_setup_irq,
1565 .irq_info = &irq_info_v3,
1566};
1567
1568static const struct of_device_id mtk_pcie_ids[] = {
1569 { .compatible = "mediatek,mt2701-pcie", .data = &mtk_pcie_soc_v1 },
1570 { .compatible = "mediatek,mt7623-pcie", .data = &mtk_pcie_soc_v1 },
1571 { .compatible = "mediatek,mt2712-pcie", .data = &mtk_pcie_soc_mt2712 },
1572 { .compatible = "mediatek,mt7622-pcie", .data = &mtk_pcie_soc_mt7622 },
1573 { .compatible = "mediatek,mt5895-pcie", .data = &mtk_pcie_soc_v3 },
1574 {},
1575};
1576
1577static struct platform_driver mtk_pcie_driver = {
1578 .probe = mtk_pcie_probe,
1579 .remove = mtk_pcie_remove,
1580 .driver = {
1581 .name = "mtk-pcie",
1582 .of_match_table = mtk_pcie_ids,
1583 .suppress_bind_attrs = true,
1584 .pm = &mtk_pcie_pm_ops,
1585 },
1586};
1587
1588static int __init mtk_pcie_init(void)
1589{
1590 return platform_driver_register(&mtk_pcie_driver);
1591}
1592
1593late_initcall(mtk_pcie_init);
1594MODULE_LICENSE("GPL");