blob: 5b3da5856ca2bd5a60167ef6b98baecbaa0d5018 [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>
14#include <linux/irqdomain.h>
Ryder Lee637cfaca2017-05-21 11:42:24 +080015#include <linux/kernel.h>
16#include <linux/of_address.h>
17#include <linux/of_pci.h>
18#include <linux/of_platform.h>
19#include <linux/pci.h>
20#include <linux/phy/phy.h>
21#include <linux/platform_device.h>
22#include <linux/pm_runtime.h>
23#include <linux/reset.h>
24
Rob Herring9e2aee82018-05-11 12:15:30 -050025#include "../pci.h"
26
Ryder Lee637cfaca2017-05-21 11:42:24 +080027/* PCIe shared registers */
28#define PCIE_SYS_CFG 0x00
29#define PCIE_INT_ENABLE 0x0c
30#define PCIE_CFG_ADDR 0x20
31#define PCIE_CFG_DATA 0x24
32
33/* PCIe per port registers */
34#define PCIE_BAR0_SETUP 0x10
35#define PCIE_CLASS 0x34
36#define PCIE_LINK_STATUS 0x50
37
38#define PCIE_PORT_INT_EN(x) BIT(20 + (x))
39#define PCIE_PORT_PERST(x) BIT(1 + (x))
40#define PCIE_PORT_LINKUP BIT(0)
41#define PCIE_BAR_MAP_MAX GENMASK(31, 16)
42
43#define PCIE_BAR_ENABLE BIT(0)
44#define PCIE_REVISION_ID BIT(0)
45#define PCIE_CLASS_CODE (0x60400 << 8)
46#define PCIE_CONF_REG(regn) (((regn) & GENMASK(7, 2)) | \
47 ((((regn) >> 8) & GENMASK(3, 0)) << 24))
48#define PCIE_CONF_FUN(fun) (((fun) << 8) & GENMASK(10, 8))
49#define PCIE_CONF_DEV(dev) (((dev) << 11) & GENMASK(15, 11))
50#define PCIE_CONF_BUS(bus) (((bus) << 16) & GENMASK(23, 16))
51#define PCIE_CONF_ADDR(regn, fun, dev, bus) \
52 (PCIE_CONF_REG(regn) | PCIE_CONF_FUN(fun) | \
53 PCIE_CONF_DEV(dev) | PCIE_CONF_BUS(bus))
54
55/* MediaTek specific configuration registers */
56#define PCIE_FTS_NUM 0x70c
57#define PCIE_FTS_NUM_MASK GENMASK(15, 8)
58#define PCIE_FTS_NUM_L0(x) ((x) & 0xff << 8)
59
60#define PCIE_FC_CREDIT 0x73c
61#define PCIE_FC_CREDIT_MASK (GENMASK(31, 31) | GENMASK(28, 16))
62#define PCIE_FC_CREDIT_VAL(x) ((x) << 16)
63
Ryder Leeb0996312017-08-10 14:34:59 +080064/* PCIe V2 share registers */
65#define PCIE_SYS_CFG_V2 0x0
66#define PCIE_CSR_LTSSM_EN(x) BIT(0 + (x) * 8)
67#define PCIE_CSR_ASPM_L1_EN(x) BIT(1 + (x) * 8)
68
69/* PCIe V2 per-port registers */
Honghui Zhang43e64092017-08-14 21:04:28 +080070#define PCIE_MSI_VECTOR 0x0c0
Ryder Leeb0996312017-08-10 14:34:59 +080071#define PCIE_INT_MASK 0x420
72#define INTX_MASK GENMASK(19, 16)
73#define INTX_SHIFT 16
Ryder Leeb0996312017-08-10 14:34:59 +080074#define PCIE_INT_STATUS 0x424
Honghui Zhang43e64092017-08-14 21:04:28 +080075#define MSI_STATUS BIT(23)
76#define PCIE_IMSI_STATUS 0x42c
77#define PCIE_IMSI_ADDR 0x430
78#define MSI_MASK BIT(23)
79#define MTK_MSI_IRQS_NUM 32
Ryder Leeb0996312017-08-10 14:34:59 +080080
81#define PCIE_AHB_TRANS_BASE0_L 0x438
82#define PCIE_AHB_TRANS_BASE0_H 0x43c
83#define AHB2PCIE_SIZE(x) ((x) & GENMASK(4, 0))
84#define PCIE_AXI_WINDOW0 0x448
85#define WIN_ENABLE BIT(7)
86
87/* PCIe V2 configuration transaction header */
88#define PCIE_CFG_HEADER0 0x460
89#define PCIE_CFG_HEADER1 0x464
90#define PCIE_CFG_HEADER2 0x468
91#define PCIE_CFG_WDATA 0x470
92#define PCIE_APP_TLP_REQ 0x488
93#define PCIE_CFG_RDATA 0x48c
94#define APP_CFG_REQ BIT(0)
95#define APP_CPL_STATUS GENMASK(7, 5)
96
97#define CFG_WRRD_TYPE_0 4
98#define CFG_WR_FMT 2
99#define CFG_RD_FMT 0
100
101#define CFG_DW0_LENGTH(length) ((length) & GENMASK(9, 0))
102#define CFG_DW0_TYPE(type) (((type) << 24) & GENMASK(28, 24))
103#define CFG_DW0_FMT(fmt) (((fmt) << 29) & GENMASK(31, 29))
104#define CFG_DW2_REGN(regn) ((regn) & GENMASK(11, 2))
105#define CFG_DW2_FUN(fun) (((fun) << 16) & GENMASK(18, 16))
106#define CFG_DW2_DEV(dev) (((dev) << 19) & GENMASK(23, 19))
107#define CFG_DW2_BUS(bus) (((bus) << 24) & GENMASK(31, 24))
108#define CFG_HEADER_DW0(type, fmt) \
109 (CFG_DW0_LENGTH(1) | CFG_DW0_TYPE(type) | CFG_DW0_FMT(fmt))
110#define CFG_HEADER_DW1(where, size) \
111 (GENMASK(((size) - 1), 0) << ((where) & 0x3))
112#define CFG_HEADER_DW2(regn, fun, dev, bus) \
113 (CFG_DW2_REGN(regn) | CFG_DW2_FUN(fun) | \
114 CFG_DW2_DEV(dev) | CFG_DW2_BUS(bus))
115
116#define PCIE_RST_CTRL 0x510
117#define PCIE_PHY_RSTB BIT(0)
118#define PCIE_PIPE_SRSTB BIT(1)
119#define PCIE_MAC_SRSTB BIT(2)
120#define PCIE_CRSTB BIT(3)
121#define PCIE_PERSTB BIT(8)
122#define PCIE_LINKDOWN_RST_EN GENMASK(15, 13)
123#define PCIE_LINK_STATUS_V2 0x804
124#define PCIE_PORT_LINKUP_V2 BIT(10)
125
Honghui Zhangc681c932017-08-10 14:34:56 +0800126struct mtk_pcie_port;
127
128/**
129 * struct mtk_pcie_soc - differentiate between host generations
Honghui Zhang43e64092017-08-14 21:04:28 +0800130 * @has_msi: whether this host supports MSI interrupts or not
Honghui Zhangc681c932017-08-10 14:34:56 +0800131 * @ops: pointer to configuration access functions
132 * @startup: pointer to controller setting functions
Ryder Leeb0996312017-08-10 14:34:59 +0800133 * @setup_irq: pointer to initialize IRQ functions
Honghui Zhangc681c932017-08-10 14:34:56 +0800134 */
135struct mtk_pcie_soc {
Honghui Zhang43e64092017-08-14 21:04:28 +0800136 bool has_msi;
Honghui Zhangc681c932017-08-10 14:34:56 +0800137 struct pci_ops *ops;
138 int (*startup)(struct mtk_pcie_port *port);
Ryder Leeb0996312017-08-10 14:34:59 +0800139 int (*setup_irq)(struct mtk_pcie_port *port, struct device_node *node);
Honghui Zhangc681c932017-08-10 14:34:56 +0800140};
141
Ryder Lee637cfaca2017-05-21 11:42:24 +0800142/**
143 * struct mtk_pcie_port - PCIe port information
144 * @base: IO mapped register base
145 * @list: port list
146 * @pcie: pointer to PCIe host info
147 * @reset: pointer to port reset control
Ryder Leeb0996312017-08-10 14:34:59 +0800148 * @sys_ck: pointer to transaction/data link layer clock
149 * @ahb_ck: pointer to AHB slave interface operating clock for CSR access
150 * and RC initiated MMIO access
151 * @axi_ck: pointer to application layer MMIO channel operating clock
152 * @aux_ck: pointer to pe2_mac_bridge and pe2_mac_core operating clock
153 * when pcie_mac_ck/pcie_pipe_ck is turned off
154 * @obff_ck: pointer to OBFF functional block operating clock
155 * @pipe_ck: pointer to LTSSM and PHY/MAC layer operating clock
156 * @phy: pointer to PHY control block
Ryder Lee637cfaca2017-05-21 11:42:24 +0800157 * @lane: lane count
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800158 * @slot: port slot
Ryder Leeb0996312017-08-10 14:34:59 +0800159 * @irq_domain: legacy INTx IRQ domain
Honghui Zhang43e64092017-08-14 21:04:28 +0800160 * @msi_domain: MSI IRQ domain
161 * @msi_irq_in_use: bit map for assigned MSI IRQ
Ryder Lee637cfaca2017-05-21 11:42:24 +0800162 */
163struct mtk_pcie_port {
164 void __iomem *base;
165 struct list_head list;
166 struct mtk_pcie *pcie;
167 struct reset_control *reset;
168 struct clk *sys_ck;
Ryder Leeb0996312017-08-10 14:34:59 +0800169 struct clk *ahb_ck;
170 struct clk *axi_ck;
171 struct clk *aux_ck;
172 struct clk *obff_ck;
173 struct clk *pipe_ck;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800174 struct phy *phy;
175 u32 lane;
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800176 u32 slot;
Ryder Leeb0996312017-08-10 14:34:59 +0800177 struct irq_domain *irq_domain;
Honghui Zhang43e64092017-08-14 21:04:28 +0800178 struct irq_domain *msi_domain;
179 DECLARE_BITMAP(msi_irq_in_use, MTK_MSI_IRQS_NUM);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800180};
181
182/**
183 * struct mtk_pcie - PCIe host information
184 * @dev: pointer to PCIe device
185 * @base: IO mapped register base
186 * @free_ck: free-run reference clock
187 * @io: IO resource
188 * @pio: PIO resource
189 * @mem: non-prefetchable memory resource
190 * @busn: bus range
191 * @offset: IO / Memory offset
192 * @ports: pointer to PCIe port information
Honghui Zhangc681c932017-08-10 14:34:56 +0800193 * @soc: pointer to SoC-dependent operations
Ryder Lee637cfaca2017-05-21 11:42:24 +0800194 */
195struct mtk_pcie {
196 struct device *dev;
197 void __iomem *base;
198 struct clk *free_ck;
199
200 struct resource io;
201 struct resource pio;
202 struct resource mem;
203 struct resource busn;
204 struct {
205 resource_size_t mem;
206 resource_size_t io;
207 } offset;
208 struct list_head ports;
Honghui Zhangc681c932017-08-10 14:34:56 +0800209 const struct mtk_pcie_soc *soc;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800210};
211
Ryder Lee637cfaca2017-05-21 11:42:24 +0800212static void mtk_pcie_subsys_powerdown(struct mtk_pcie *pcie)
213{
214 struct device *dev = pcie->dev;
215
216 clk_disable_unprepare(pcie->free_ck);
217
218 if (dev->pm_domain) {
219 pm_runtime_put_sync(dev);
220 pm_runtime_disable(dev);
221 }
222}
223
224static void mtk_pcie_port_free(struct mtk_pcie_port *port)
225{
226 struct mtk_pcie *pcie = port->pcie;
227 struct device *dev = pcie->dev;
228
229 devm_iounmap(dev, port->base);
230 list_del(&port->list);
231 devm_kfree(dev, port);
232}
233
234static void mtk_pcie_put_resources(struct mtk_pcie *pcie)
235{
236 struct mtk_pcie_port *port, *tmp;
237
238 list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
239 phy_power_off(port->phy);
Ryder Leeb0996312017-08-10 14:34:59 +0800240 phy_exit(port->phy);
241 clk_disable_unprepare(port->pipe_ck);
242 clk_disable_unprepare(port->obff_ck);
243 clk_disable_unprepare(port->axi_ck);
244 clk_disable_unprepare(port->aux_ck);
245 clk_disable_unprepare(port->ahb_ck);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800246 clk_disable_unprepare(port->sys_ck);
247 mtk_pcie_port_free(port);
248 }
249
250 mtk_pcie_subsys_powerdown(pcie);
251}
252
Ryder Leeb0996312017-08-10 14:34:59 +0800253static int mtk_pcie_check_cfg_cpld(struct mtk_pcie_port *port)
254{
255 u32 val;
256 int err;
257
258 err = readl_poll_timeout_atomic(port->base + PCIE_APP_TLP_REQ, val,
259 !(val & APP_CFG_REQ), 10,
260 100 * USEC_PER_MSEC);
261 if (err)
262 return PCIBIOS_SET_FAILED;
263
264 if (readl(port->base + PCIE_APP_TLP_REQ) & APP_CPL_STATUS)
265 return PCIBIOS_SET_FAILED;
266
267 return PCIBIOS_SUCCESSFUL;
268}
269
270static int mtk_pcie_hw_rd_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn,
271 int where, int size, u32 *val)
272{
273 u32 tmp;
274
275 /* Write PCIe configuration transaction header for Cfgrd */
276 writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_RD_FMT),
277 port->base + PCIE_CFG_HEADER0);
278 writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1);
279 writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus),
280 port->base + PCIE_CFG_HEADER2);
281
282 /* Trigger h/w to transmit Cfgrd TLP */
283 tmp = readl(port->base + PCIE_APP_TLP_REQ);
284 tmp |= APP_CFG_REQ;
285 writel(tmp, port->base + PCIE_APP_TLP_REQ);
286
287 /* Check completion status */
288 if (mtk_pcie_check_cfg_cpld(port))
289 return PCIBIOS_SET_FAILED;
290
291 /* Read cpld payload of Cfgrd */
292 *val = readl(port->base + PCIE_CFG_RDATA);
293
294 if (size == 1)
295 *val = (*val >> (8 * (where & 3))) & 0xff;
296 else if (size == 2)
297 *val = (*val >> (8 * (where & 3))) & 0xffff;
298
299 return PCIBIOS_SUCCESSFUL;
300}
301
302static int mtk_pcie_hw_wr_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn,
303 int where, int size, u32 val)
304{
305 /* Write PCIe configuration transaction header for Cfgwr */
306 writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_WR_FMT),
307 port->base + PCIE_CFG_HEADER0);
308 writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1);
309 writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus),
310 port->base + PCIE_CFG_HEADER2);
311
312 /* Write Cfgwr data */
313 val = val << 8 * (where & 3);
314 writel(val, port->base + PCIE_CFG_WDATA);
315
316 /* Trigger h/w to transmit Cfgwr TLP */
317 val = readl(port->base + PCIE_APP_TLP_REQ);
318 val |= APP_CFG_REQ;
319 writel(val, port->base + PCIE_APP_TLP_REQ);
320
321 /* Check completion status */
322 return mtk_pcie_check_cfg_cpld(port);
323}
324
325static struct mtk_pcie_port *mtk_pcie_find_port(struct pci_bus *bus,
326 unsigned int devfn)
327{
328 struct mtk_pcie *pcie = bus->sysdata;
329 struct mtk_pcie_port *port;
330
331 list_for_each_entry(port, &pcie->ports, list)
332 if (port->slot == PCI_SLOT(devfn))
333 return port;
334
335 return NULL;
336}
337
338static int mtk_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
339 int where, int size, u32 *val)
340{
341 struct mtk_pcie_port *port;
342 u32 bn = bus->number;
343 int ret;
344
345 port = mtk_pcie_find_port(bus, devfn);
346 if (!port) {
347 *val = ~0;
348 return PCIBIOS_DEVICE_NOT_FOUND;
349 }
350
351 ret = mtk_pcie_hw_rd_cfg(port, bn, devfn, where, size, val);
352 if (ret)
353 *val = ~0;
354
355 return ret;
356}
357
358static int mtk_pcie_config_write(struct pci_bus *bus, unsigned int devfn,
359 int where, int size, u32 val)
360{
361 struct mtk_pcie_port *port;
362 u32 bn = bus->number;
363
364 port = mtk_pcie_find_port(bus, devfn);
365 if (!port)
366 return PCIBIOS_DEVICE_NOT_FOUND;
367
368 return mtk_pcie_hw_wr_cfg(port, bn, devfn, where, size, val);
369}
370
371static struct pci_ops mtk_pcie_ops_v2 = {
372 .read = mtk_pcie_config_read,
373 .write = mtk_pcie_config_write,
374};
375
376static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)
377{
378 struct mtk_pcie *pcie = port->pcie;
379 struct resource *mem = &pcie->mem;
380 u32 val;
381 size_t size;
382 int err;
383
384 /* MT7622 platforms need to enable LTSSM and ASPM from PCIe subsys */
385 if (pcie->base) {
386 val = readl(pcie->base + PCIE_SYS_CFG_V2);
387 val |= PCIE_CSR_LTSSM_EN(port->slot) |
388 PCIE_CSR_ASPM_L1_EN(port->slot);
389 writel(val, pcie->base + PCIE_SYS_CFG_V2);
390 }
391
392 /* Assert all reset signals */
393 writel(0, port->base + PCIE_RST_CTRL);
394
395 /*
396 * Enable PCIe link down reset, if link status changed from link up to
397 * link down, this will reset MAC control registers and configuration
398 * space.
399 */
400 writel(PCIE_LINKDOWN_RST_EN, port->base + PCIE_RST_CTRL);
401
402 /* De-assert PHY, PE, PIPE, MAC and configuration reset */
403 val = readl(port->base + PCIE_RST_CTRL);
404 val |= PCIE_PHY_RSTB | PCIE_PERSTB | PCIE_PIPE_SRSTB |
405 PCIE_MAC_SRSTB | PCIE_CRSTB;
406 writel(val, port->base + PCIE_RST_CTRL);
407
408 /* 100ms timeout value should be enough for Gen1/2 training */
409 err = readl_poll_timeout(port->base + PCIE_LINK_STATUS_V2, val,
410 !!(val & PCIE_PORT_LINKUP_V2), 20,
411 100 * USEC_PER_MSEC);
412 if (err)
413 return -ETIMEDOUT;
414
415 /* Set INTx mask */
416 val = readl(port->base + PCIE_INT_MASK);
417 val &= ~INTX_MASK;
418 writel(val, port->base + PCIE_INT_MASK);
419
420 /* Set AHB to PCIe translation windows */
421 size = mem->end - mem->start;
422 val = lower_32_bits(mem->start) | AHB2PCIE_SIZE(fls(size));
423 writel(val, port->base + PCIE_AHB_TRANS_BASE0_L);
424
425 val = upper_32_bits(mem->start);
426 writel(val, port->base + PCIE_AHB_TRANS_BASE0_H);
427
428 /* Set PCIe to AXI translation memory space.*/
429 val = fls(0xffffffff) | WIN_ENABLE;
430 writel(val, port->base + PCIE_AXI_WINDOW0);
431
432 return 0;
433}
434
Honghui Zhang43e64092017-08-14 21:04:28 +0800435static int mtk_pcie_msi_alloc(struct mtk_pcie_port *port)
436{
437 int msi;
438
439 msi = find_first_zero_bit(port->msi_irq_in_use, MTK_MSI_IRQS_NUM);
440 if (msi < MTK_MSI_IRQS_NUM)
441 set_bit(msi, port->msi_irq_in_use);
442 else
443 return -ENOSPC;
444
445 return msi;
446}
447
448static void mtk_pcie_msi_free(struct mtk_pcie_port *port, unsigned long hwirq)
449{
450 clear_bit(hwirq, port->msi_irq_in_use);
451}
452
453static int mtk_pcie_msi_setup_irq(struct msi_controller *chip,
454 struct pci_dev *pdev, struct msi_desc *desc)
455{
456 struct mtk_pcie_port *port;
457 struct msi_msg msg;
458 unsigned int irq;
459 int hwirq;
460 phys_addr_t msg_addr;
461
462 port = mtk_pcie_find_port(pdev->bus, pdev->devfn);
463 if (!port)
464 return -EINVAL;
465
466 hwirq = mtk_pcie_msi_alloc(port);
467 if (hwirq < 0)
468 return hwirq;
469
470 irq = irq_create_mapping(port->msi_domain, hwirq);
471 if (!irq) {
472 mtk_pcie_msi_free(port, hwirq);
473 return -EINVAL;
474 }
475
476 chip->dev = &pdev->dev;
477
478 irq_set_msi_desc(irq, desc);
479
480 /* MT2712/MT7622 only support 32-bit MSI addresses */
481 msg_addr = virt_to_phys(port->base + PCIE_MSI_VECTOR);
482 msg.address_hi = 0;
483 msg.address_lo = lower_32_bits(msg_addr);
484 msg.data = hwirq;
485
486 pci_write_msi_msg(irq, &msg);
487
488 return 0;
489}
490
491static void mtk_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
492{
493 struct pci_dev *pdev = to_pci_dev(chip->dev);
494 struct irq_data *d = irq_get_irq_data(irq);
495 irq_hw_number_t hwirq = irqd_to_hwirq(d);
496 struct mtk_pcie_port *port;
497
498 port = mtk_pcie_find_port(pdev->bus, pdev->devfn);
499 if (!port)
500 return;
501
502 irq_dispose_mapping(irq);
503 mtk_pcie_msi_free(port, hwirq);
504}
505
506static struct msi_controller mtk_pcie_msi_chip = {
507 .setup_irq = mtk_pcie_msi_setup_irq,
508 .teardown_irq = mtk_msi_teardown_irq,
509};
510
511static struct irq_chip mtk_msi_irq_chip = {
512 .name = "MTK PCIe MSI",
513 .irq_enable = pci_msi_unmask_irq,
514 .irq_disable = pci_msi_mask_irq,
515 .irq_mask = pci_msi_mask_irq,
516 .irq_unmask = pci_msi_unmask_irq,
517};
518
519static int mtk_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
520 irq_hw_number_t hwirq)
521{
522 irq_set_chip_and_handler(irq, &mtk_msi_irq_chip, handle_simple_irq);
523 irq_set_chip_data(irq, domain->host_data);
524
525 return 0;
526}
527
528static const struct irq_domain_ops msi_domain_ops = {
529 .map = mtk_pcie_msi_map,
530};
531
532static void mtk_pcie_enable_msi(struct mtk_pcie_port *port)
533{
534 u32 val;
535 phys_addr_t msg_addr;
536
537 msg_addr = virt_to_phys(port->base + PCIE_MSI_VECTOR);
538 val = lower_32_bits(msg_addr);
539 writel(val, port->base + PCIE_IMSI_ADDR);
540
541 val = readl(port->base + PCIE_INT_MASK);
542 val &= ~MSI_MASK;
543 writel(val, port->base + PCIE_INT_MASK);
544}
545
Ryder Leeb0996312017-08-10 14:34:59 +0800546static int mtk_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
547 irq_hw_number_t hwirq)
548{
549 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
550 irq_set_chip_data(irq, domain->host_data);
551
552 return 0;
553}
554
555static const struct irq_domain_ops intx_domain_ops = {
556 .map = mtk_pcie_intx_map,
557};
558
559static int mtk_pcie_init_irq_domain(struct mtk_pcie_port *port,
560 struct device_node *node)
561{
562 struct device *dev = port->pcie->dev;
563 struct device_node *pcie_intc_node;
564
565 /* Setup INTx */
566 pcie_intc_node = of_get_next_child(node, NULL);
567 if (!pcie_intc_node) {
568 dev_err(dev, "no PCIe Intc node found\n");
569 return -ENODEV;
570 }
571
Honghui Zhangd84c2462017-08-30 09:19:14 +0800572 port->irq_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
Ryder Leeb0996312017-08-10 14:34:59 +0800573 &intx_domain_ops, port);
574 if (!port->irq_domain) {
575 dev_err(dev, "failed to get INTx IRQ domain\n");
576 return -ENODEV;
577 }
578
Honghui Zhang43e64092017-08-14 21:04:28 +0800579 if (IS_ENABLED(CONFIG_PCI_MSI)) {
580 port->msi_domain = irq_domain_add_linear(node, MTK_MSI_IRQS_NUM,
581 &msi_domain_ops,
582 &mtk_pcie_msi_chip);
583 if (!port->msi_domain) {
584 dev_err(dev, "failed to create MSI IRQ domain\n");
585 return -ENODEV;
586 }
587 mtk_pcie_enable_msi(port);
588 }
589
Ryder Leeb0996312017-08-10 14:34:59 +0800590 return 0;
591}
592
593static irqreturn_t mtk_pcie_intr_handler(int irq, void *data)
594{
595 struct mtk_pcie_port *port = (struct mtk_pcie_port *)data;
596 unsigned long status;
597 u32 virq;
598 u32 bit = INTX_SHIFT;
599
600 while ((status = readl(port->base + PCIE_INT_STATUS)) & INTX_MASK) {
Honghui Zhangd84c2462017-08-30 09:19:14 +0800601 for_each_set_bit_from(bit, &status, PCI_NUM_INTX + INTX_SHIFT) {
Ryder Leeb0996312017-08-10 14:34:59 +0800602 /* Clear the INTx */
603 writel(1 << bit, port->base + PCIE_INT_STATUS);
604 virq = irq_find_mapping(port->irq_domain,
605 bit - INTX_SHIFT);
606 generic_handle_irq(virq);
607 }
608 }
609
Honghui Zhang43e64092017-08-14 21:04:28 +0800610 if (IS_ENABLED(CONFIG_PCI_MSI)) {
611 while ((status = readl(port->base + PCIE_INT_STATUS)) & MSI_STATUS) {
612 unsigned long imsi_status;
613
614 while ((imsi_status = readl(port->base + PCIE_IMSI_STATUS))) {
615 for_each_set_bit(bit, &imsi_status, MTK_MSI_IRQS_NUM) {
616 /* Clear the MSI */
617 writel(1 << bit, port->base + PCIE_IMSI_STATUS);
618 virq = irq_find_mapping(port->msi_domain, bit);
619 generic_handle_irq(virq);
620 }
621 }
622 /* Clear MSI interrupt status */
623 writel(MSI_STATUS, port->base + PCIE_INT_STATUS);
624 }
625 }
626
Ryder Leeb0996312017-08-10 14:34:59 +0800627 return IRQ_HANDLED;
628}
629
630static int mtk_pcie_setup_irq(struct mtk_pcie_port *port,
631 struct device_node *node)
632{
633 struct mtk_pcie *pcie = port->pcie;
634 struct device *dev = pcie->dev;
635 struct platform_device *pdev = to_platform_device(dev);
636 int err, irq;
637
638 irq = platform_get_irq(pdev, port->slot);
639 err = devm_request_irq(dev, irq, mtk_pcie_intr_handler,
640 IRQF_SHARED, "mtk-pcie", port);
641 if (err) {
642 dev_err(dev, "unable to request IRQ %d\n", irq);
643 return err;
644 }
645
646 err = mtk_pcie_init_irq_domain(port, node);
647 if (err) {
Honghui Zhang43e64092017-08-14 21:04:28 +0800648 dev_err(dev, "failed to init PCIe IRQ domain\n");
Ryder Leeb0996312017-08-10 14:34:59 +0800649 return err;
650 }
651
652 return 0;
653}
654
Ryder Lee637cfaca2017-05-21 11:42:24 +0800655static void __iomem *mtk_pcie_map_bus(struct pci_bus *bus,
656 unsigned int devfn, int where)
657{
Honghui Zhangdb271742017-08-14 21:04:27 +0800658 struct mtk_pcie *pcie = bus->sysdata;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800659
660 writel(PCIE_CONF_ADDR(where, PCI_FUNC(devfn), PCI_SLOT(devfn),
661 bus->number), pcie->base + PCIE_CFG_ADDR);
662
663 return pcie->base + PCIE_CFG_DATA + (where & 3);
664}
665
666static struct pci_ops mtk_pcie_ops = {
667 .map_bus = mtk_pcie_map_bus,
668 .read = pci_generic_config_read,
669 .write = pci_generic_config_write,
670};
671
Ryder Leee10b7a12017-08-10 14:34:54 +0800672static int mtk_pcie_startup_port(struct mtk_pcie_port *port)
Ryder Lee637cfaca2017-05-21 11:42:24 +0800673{
674 struct mtk_pcie *pcie = port->pcie;
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800675 u32 func = PCI_FUNC(port->slot << 3);
676 u32 slot = PCI_SLOT(port->slot << 3);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800677 u32 val;
Ryder Leee10b7a12017-08-10 14:34:54 +0800678 int err;
679
680 /* assert port PERST_N */
681 val = readl(pcie->base + PCIE_SYS_CFG);
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800682 val |= PCIE_PORT_PERST(port->slot);
Ryder Leee10b7a12017-08-10 14:34:54 +0800683 writel(val, pcie->base + PCIE_SYS_CFG);
684
685 /* de-assert port PERST_N */
686 val = readl(pcie->base + PCIE_SYS_CFG);
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800687 val &= ~PCIE_PORT_PERST(port->slot);
Ryder Leee10b7a12017-08-10 14:34:54 +0800688 writel(val, pcie->base + PCIE_SYS_CFG);
689
690 /* 100ms timeout value should be enough for Gen1/2 training */
691 err = readl_poll_timeout(port->base + PCIE_LINK_STATUS, val,
692 !!(val & PCIE_PORT_LINKUP), 20,
693 100 * USEC_PER_MSEC);
694 if (err)
695 return -ETIMEDOUT;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800696
697 /* enable interrupt */
698 val = readl(pcie->base + PCIE_INT_ENABLE);
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800699 val |= PCIE_PORT_INT_EN(port->slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800700 writel(val, pcie->base + PCIE_INT_ENABLE);
701
702 /* map to all DDR region. We need to set it before cfg operation. */
703 writel(PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
704 port->base + PCIE_BAR0_SETUP);
705
706 /* configure class code and revision ID */
707 writel(PCIE_CLASS_CODE | PCIE_REVISION_ID, port->base + PCIE_CLASS);
708
709 /* configure FC credit */
710 writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0),
711 pcie->base + PCIE_CFG_ADDR);
712 val = readl(pcie->base + PCIE_CFG_DATA);
713 val &= ~PCIE_FC_CREDIT_MASK;
714 val |= PCIE_FC_CREDIT_VAL(0x806c);
715 writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0),
716 pcie->base + PCIE_CFG_ADDR);
717 writel(val, pcie->base + PCIE_CFG_DATA);
718
719 /* configure RC FTS number to 250 when it leaves L0s */
720 writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0),
721 pcie->base + PCIE_CFG_ADDR);
722 val = readl(pcie->base + PCIE_CFG_DATA);
723 val &= ~PCIE_FTS_NUM_MASK;
724 val |= PCIE_FTS_NUM_L0(0x50);
725 writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0),
726 pcie->base + PCIE_CFG_ADDR);
727 writel(val, pcie->base + PCIE_CFG_DATA);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800728
Ryder Leee10b7a12017-08-10 14:34:54 +0800729 return 0;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800730}
731
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800732static void mtk_pcie_enable_port(struct mtk_pcie_port *port)
Ryder Lee637cfaca2017-05-21 11:42:24 +0800733{
Honghui Zhangc681c932017-08-10 14:34:56 +0800734 struct mtk_pcie *pcie = port->pcie;
735 struct device *dev = pcie->dev;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800736 int err;
737
738 err = clk_prepare_enable(port->sys_ck);
739 if (err) {
Ryder Leeb0996312017-08-10 14:34:59 +0800740 dev_err(dev, "failed to enable sys_ck%d clock\n", port->slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800741 goto err_sys_clk;
742 }
743
Ryder Leeb0996312017-08-10 14:34:59 +0800744 err = clk_prepare_enable(port->ahb_ck);
745 if (err) {
746 dev_err(dev, "failed to enable ahb_ck%d\n", port->slot);
747 goto err_ahb_clk;
748 }
749
750 err = clk_prepare_enable(port->aux_ck);
751 if (err) {
752 dev_err(dev, "failed to enable aux_ck%d\n", port->slot);
753 goto err_aux_clk;
754 }
755
756 err = clk_prepare_enable(port->axi_ck);
757 if (err) {
758 dev_err(dev, "failed to enable axi_ck%d\n", port->slot);
759 goto err_axi_clk;
760 }
761
762 err = clk_prepare_enable(port->obff_ck);
763 if (err) {
764 dev_err(dev, "failed to enable obff_ck%d\n", port->slot);
765 goto err_obff_clk;
766 }
767
768 err = clk_prepare_enable(port->pipe_ck);
769 if (err) {
770 dev_err(dev, "failed to enable pipe_ck%d\n", port->slot);
771 goto err_pipe_clk;
772 }
773
Ryder Lee637cfaca2017-05-21 11:42:24 +0800774 reset_control_assert(port->reset);
775 reset_control_deassert(port->reset);
776
Ryder Leeb0996312017-08-10 14:34:59 +0800777 err = phy_init(port->phy);
778 if (err) {
779 dev_err(dev, "failed to initialize port%d phy\n", port->slot);
780 goto err_phy_init;
781 }
782
Ryder Lee637cfaca2017-05-21 11:42:24 +0800783 err = phy_power_on(port->phy);
784 if (err) {
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800785 dev_err(dev, "failed to power on port%d phy\n", port->slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800786 goto err_phy_on;
787 }
788
Honghui Zhangc681c932017-08-10 14:34:56 +0800789 if (!pcie->soc->startup(port))
Ryder Lee637cfaca2017-05-21 11:42:24 +0800790 return;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800791
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800792 dev_info(dev, "Port%d link down\n", port->slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800793
794 phy_power_off(port->phy);
795err_phy_on:
Ryder Leeb0996312017-08-10 14:34:59 +0800796 phy_exit(port->phy);
797err_phy_init:
798 clk_disable_unprepare(port->pipe_ck);
799err_pipe_clk:
800 clk_disable_unprepare(port->obff_ck);
801err_obff_clk:
802 clk_disable_unprepare(port->axi_ck);
803err_axi_clk:
804 clk_disable_unprepare(port->aux_ck);
805err_aux_clk:
806 clk_disable_unprepare(port->ahb_ck);
807err_ahb_clk:
Ryder Lee637cfaca2017-05-21 11:42:24 +0800808 clk_disable_unprepare(port->sys_ck);
809err_sys_clk:
810 mtk_pcie_port_free(port);
811}
812
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800813static int mtk_pcie_parse_port(struct mtk_pcie *pcie,
814 struct device_node *node,
815 int slot)
Ryder Lee637cfaca2017-05-21 11:42:24 +0800816{
817 struct mtk_pcie_port *port;
818 struct resource *regs;
819 struct device *dev = pcie->dev;
820 struct platform_device *pdev = to_platform_device(dev);
821 char name[10];
822 int err;
823
824 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
825 if (!port)
826 return -ENOMEM;
827
828 err = of_property_read_u32(node, "num-lanes", &port->lane);
829 if (err) {
830 dev_err(dev, "missing num-lanes property\n");
831 return err;
832 }
833
Ryder Lee1eacd7b2017-08-10 14:34:57 +0800834 snprintf(name, sizeof(name), "port%d", slot);
835 regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800836 port->base = devm_ioremap_resource(dev, regs);
837 if (IS_ERR(port->base)) {
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800838 dev_err(dev, "failed to map port%d base\n", slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800839 return PTR_ERR(port->base);
840 }
841
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800842 snprintf(name, sizeof(name), "sys_ck%d", slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800843 port->sys_ck = devm_clk_get(dev, name);
844 if (IS_ERR(port->sys_ck)) {
Ryder Leeb0996312017-08-10 14:34:59 +0800845 dev_err(dev, "failed to get sys_ck%d clock\n", slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800846 return PTR_ERR(port->sys_ck);
847 }
848
Ryder Leeb0996312017-08-10 14:34:59 +0800849 /* sys_ck might be divided into the following parts in some chips */
850 snprintf(name, sizeof(name), "ahb_ck%d", slot);
851 port->ahb_ck = devm_clk_get(dev, name);
852 if (IS_ERR(port->ahb_ck)) {
853 if (PTR_ERR(port->ahb_ck) == -EPROBE_DEFER)
854 return -EPROBE_DEFER;
855
856 port->ahb_ck = NULL;
857 }
858
859 snprintf(name, sizeof(name), "axi_ck%d", slot);
860 port->axi_ck = devm_clk_get(dev, name);
861 if (IS_ERR(port->axi_ck)) {
862 if (PTR_ERR(port->axi_ck) == -EPROBE_DEFER)
863 return -EPROBE_DEFER;
864
865 port->axi_ck = NULL;
866 }
867
868 snprintf(name, sizeof(name), "aux_ck%d", slot);
869 port->aux_ck = devm_clk_get(dev, name);
870 if (IS_ERR(port->aux_ck)) {
871 if (PTR_ERR(port->aux_ck) == -EPROBE_DEFER)
872 return -EPROBE_DEFER;
873
874 port->aux_ck = NULL;
875 }
876
877 snprintf(name, sizeof(name), "obff_ck%d", slot);
878 port->obff_ck = devm_clk_get(dev, name);
879 if (IS_ERR(port->obff_ck)) {
880 if (PTR_ERR(port->obff_ck) == -EPROBE_DEFER)
881 return -EPROBE_DEFER;
882
883 port->obff_ck = NULL;
884 }
885
886 snprintf(name, sizeof(name), "pipe_ck%d", slot);
887 port->pipe_ck = devm_clk_get(dev, name);
888 if (IS_ERR(port->pipe_ck)) {
889 if (PTR_ERR(port->pipe_ck) == -EPROBE_DEFER)
890 return -EPROBE_DEFER;
891
892 port->pipe_ck = NULL;
893 }
894
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800895 snprintf(name, sizeof(name), "pcie-rst%d", slot);
Philipp Zabel608fcac2017-07-19 17:26:00 +0200896 port->reset = devm_reset_control_get_optional_exclusive(dev, name);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800897 if (PTR_ERR(port->reset) == -EPROBE_DEFER)
898 return PTR_ERR(port->reset);
899
900 /* some platforms may use default PHY setting */
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800901 snprintf(name, sizeof(name), "pcie-phy%d", slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +0800902 port->phy = devm_phy_optional_get(dev, name);
903 if (IS_ERR(port->phy))
904 return PTR_ERR(port->phy);
905
Honghui Zhang4f6f0462017-08-10 14:34:55 +0800906 port->slot = slot;
Ryder Lee637cfaca2017-05-21 11:42:24 +0800907 port->pcie = pcie;
908
Ryder Leeb0996312017-08-10 14:34:59 +0800909 if (pcie->soc->setup_irq) {
910 err = pcie->soc->setup_irq(port, node);
911 if (err)
912 return err;
913 }
914
Ryder Lee637cfaca2017-05-21 11:42:24 +0800915 INIT_LIST_HEAD(&port->list);
916 list_add_tail(&port->list, &pcie->ports);
917
918 return 0;
919}
920
921static int mtk_pcie_subsys_powerup(struct mtk_pcie *pcie)
922{
923 struct device *dev = pcie->dev;
924 struct platform_device *pdev = to_platform_device(dev);
925 struct resource *regs;
926 int err;
927
Ryder Lee1eacd7b2017-08-10 14:34:57 +0800928 /* get shared registers, which are optional */
929 regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "subsys");
930 if (regs) {
931 pcie->base = devm_ioremap_resource(dev, regs);
932 if (IS_ERR(pcie->base)) {
933 dev_err(dev, "failed to map shared register\n");
934 return PTR_ERR(pcie->base);
935 }
Ryder Lee637cfaca2017-05-21 11:42:24 +0800936 }
937
938 pcie->free_ck = devm_clk_get(dev, "free_ck");
939 if (IS_ERR(pcie->free_ck)) {
940 if (PTR_ERR(pcie->free_ck) == -EPROBE_DEFER)
941 return -EPROBE_DEFER;
942
943 pcie->free_ck = NULL;
944 }
945
946 if (dev->pm_domain) {
947 pm_runtime_enable(dev);
948 pm_runtime_get_sync(dev);
949 }
950
951 /* enable top level clock */
952 err = clk_prepare_enable(pcie->free_ck);
953 if (err) {
954 dev_err(dev, "failed to enable free_ck\n");
955 goto err_free_ck;
956 }
957
958 return 0;
959
960err_free_ck:
961 if (dev->pm_domain) {
962 pm_runtime_put_sync(dev);
963 pm_runtime_disable(dev);
964 }
965
966 return err;
967}
968
969static int mtk_pcie_setup(struct mtk_pcie *pcie)
970{
971 struct device *dev = pcie->dev;
972 struct device_node *node = dev->of_node, *child;
973 struct of_pci_range_parser parser;
974 struct of_pci_range range;
975 struct resource res;
976 struct mtk_pcie_port *port, *tmp;
977 int err;
978
979 if (of_pci_range_parser_init(&parser, node)) {
980 dev_err(dev, "missing \"ranges\" property\n");
981 return -EINVAL;
982 }
983
984 for_each_of_pci_range(&parser, &range) {
985 err = of_pci_range_to_resource(&range, node, &res);
986 if (err < 0)
987 return err;
988
989 switch (res.flags & IORESOURCE_TYPE_BITS) {
990 case IORESOURCE_IO:
991 pcie->offset.io = res.start - range.pci_addr;
992
993 memcpy(&pcie->pio, &res, sizeof(res));
994 pcie->pio.name = node->full_name;
995
996 pcie->io.start = range.cpu_addr;
997 pcie->io.end = range.cpu_addr + range.size - 1;
998 pcie->io.flags = IORESOURCE_MEM;
999 pcie->io.name = "I/O";
1000
1001 memcpy(&res, &pcie->io, sizeof(res));
1002 break;
1003
1004 case IORESOURCE_MEM:
1005 pcie->offset.mem = res.start - range.pci_addr;
1006
1007 memcpy(&pcie->mem, &res, sizeof(res));
1008 pcie->mem.name = "non-prefetchable";
1009 break;
1010 }
1011 }
1012
1013 err = of_pci_parse_bus_range(node, &pcie->busn);
1014 if (err < 0) {
1015 dev_err(dev, "failed to parse bus ranges property: %d\n", err);
1016 pcie->busn.name = node->name;
1017 pcie->busn.start = 0;
1018 pcie->busn.end = 0xff;
1019 pcie->busn.flags = IORESOURCE_BUS;
1020 }
1021
1022 for_each_available_child_of_node(node, child) {
Honghui Zhang4f6f0462017-08-10 14:34:55 +08001023 int slot;
Ryder Lee637cfaca2017-05-21 11:42:24 +08001024
1025 err = of_pci_get_devfn(child);
1026 if (err < 0) {
1027 dev_err(dev, "failed to parse devfn: %d\n", err);
1028 return err;
1029 }
1030
Honghui Zhang4f6f0462017-08-10 14:34:55 +08001031 slot = PCI_SLOT(err);
Ryder Lee637cfaca2017-05-21 11:42:24 +08001032
Honghui Zhang4f6f0462017-08-10 14:34:55 +08001033 err = mtk_pcie_parse_port(pcie, child, slot);
Ryder Lee637cfaca2017-05-21 11:42:24 +08001034 if (err)
1035 return err;
1036 }
1037
1038 err = mtk_pcie_subsys_powerup(pcie);
1039 if (err)
1040 return err;
1041
1042 /* enable each port, and then check link status */
1043 list_for_each_entry_safe(port, tmp, &pcie->ports, list)
Honghui Zhang4f6f0462017-08-10 14:34:55 +08001044 mtk_pcie_enable_port(port);
Ryder Lee637cfaca2017-05-21 11:42:24 +08001045
1046 /* power down PCIe subsys if slots are all empty (link down) */
1047 if (list_empty(&pcie->ports))
1048 mtk_pcie_subsys_powerdown(pcie);
1049
1050 return 0;
1051}
1052
1053static int mtk_pcie_request_resources(struct mtk_pcie *pcie)
1054{
1055 struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1056 struct list_head *windows = &host->windows;
1057 struct device *dev = pcie->dev;
1058 int err;
1059
1060 pci_add_resource_offset(windows, &pcie->pio, pcie->offset.io);
1061 pci_add_resource_offset(windows, &pcie->mem, pcie->offset.mem);
1062 pci_add_resource(windows, &pcie->busn);
1063
1064 err = devm_request_pci_bus_resources(dev, windows);
1065 if (err < 0)
1066 return err;
1067
1068 pci_remap_iospace(&pcie->pio, pcie->io.start);
1069
1070 return 0;
1071}
1072
1073static int mtk_pcie_register_host(struct pci_host_bridge *host)
1074{
1075 struct mtk_pcie *pcie = pci_host_bridge_priv(host);
1076 struct pci_bus *child;
1077 int err;
1078
1079 host->busnr = pcie->busn.start;
1080 host->dev.parent = pcie->dev;
Honghui Zhangc681c932017-08-10 14:34:56 +08001081 host->ops = pcie->soc->ops;
Ryder Lee637cfaca2017-05-21 11:42:24 +08001082 host->map_irq = of_irq_parse_and_map_pci;
1083 host->swizzle_irq = pci_common_swizzle;
Ryder Leeb0996312017-08-10 14:34:59 +08001084 host->sysdata = pcie;
Honghui Zhang43e64092017-08-14 21:04:28 +08001085 if (IS_ENABLED(CONFIG_PCI_MSI) && pcie->soc->has_msi)
1086 host->msi = &mtk_pcie_msi_chip;
Ryder Lee637cfaca2017-05-21 11:42:24 +08001087
1088 err = pci_scan_root_bus_bridge(host);
1089 if (err < 0)
1090 return err;
1091
1092 pci_bus_size_bridges(host->bus);
1093 pci_bus_assign_resources(host->bus);
1094
1095 list_for_each_entry(child, &host->bus->children, node)
1096 pcie_bus_configure_settings(child);
1097
1098 pci_bus_add_devices(host->bus);
1099
1100 return 0;
1101}
1102
1103static int mtk_pcie_probe(struct platform_device *pdev)
1104{
1105 struct device *dev = &pdev->dev;
1106 struct mtk_pcie *pcie;
1107 struct pci_host_bridge *host;
1108 int err;
1109
1110 host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
1111 if (!host)
1112 return -ENOMEM;
1113
1114 pcie = pci_host_bridge_priv(host);
1115
1116 pcie->dev = dev;
Honghui Zhangc681c932017-08-10 14:34:56 +08001117 pcie->soc = of_device_get_match_data(dev);
Ryder Lee637cfaca2017-05-21 11:42:24 +08001118 platform_set_drvdata(pdev, pcie);
1119 INIT_LIST_HEAD(&pcie->ports);
1120
1121 err = mtk_pcie_setup(pcie);
1122 if (err)
1123 return err;
1124
1125 err = mtk_pcie_request_resources(pcie);
1126 if (err)
1127 goto put_resources;
1128
1129 err = mtk_pcie_register_host(host);
1130 if (err)
1131 goto put_resources;
1132
1133 return 0;
1134
1135put_resources:
1136 if (!list_empty(&pcie->ports))
1137 mtk_pcie_put_resources(pcie);
1138
1139 return err;
1140}
1141
Honghui Zhangc681c932017-08-10 14:34:56 +08001142static const struct mtk_pcie_soc mtk_pcie_soc_v1 = {
1143 .ops = &mtk_pcie_ops,
1144 .startup = mtk_pcie_startup_port,
1145};
1146
Ryder Leeb0996312017-08-10 14:34:59 +08001147static const struct mtk_pcie_soc mtk_pcie_soc_v2 = {
Honghui Zhang43e64092017-08-14 21:04:28 +08001148 .has_msi = true,
Ryder Leeb0996312017-08-10 14:34:59 +08001149 .ops = &mtk_pcie_ops_v2,
1150 .startup = mtk_pcie_startup_port_v2,
1151 .setup_irq = mtk_pcie_setup_irq,
1152};
1153
Ryder Lee637cfaca2017-05-21 11:42:24 +08001154static const struct of_device_id mtk_pcie_ids[] = {
Honghui Zhangc681c932017-08-10 14:34:56 +08001155 { .compatible = "mediatek,mt2701-pcie", .data = &mtk_pcie_soc_v1 },
1156 { .compatible = "mediatek,mt7623-pcie", .data = &mtk_pcie_soc_v1 },
Ryder Leeb0996312017-08-10 14:34:59 +08001157 { .compatible = "mediatek,mt2712-pcie", .data = &mtk_pcie_soc_v2 },
1158 { .compatible = "mediatek,mt7622-pcie", .data = &mtk_pcie_soc_v2 },
Ryder Lee637cfaca2017-05-21 11:42:24 +08001159 {},
1160};
1161
1162static struct platform_driver mtk_pcie_driver = {
1163 .probe = mtk_pcie_probe,
1164 .driver = {
1165 .name = "mtk-pcie",
1166 .of_match_table = mtk_pcie_ids,
1167 .suppress_bind_attrs = true,
1168 },
1169};
1170builtin_platform_driver(mtk_pcie_driver);