1 From 29ceb2449cb3622ccfba9eb1c77bf2ac4162464b Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sat, 27 Jun 2015 13:15:29 +0200
4 Subject: [PATCH 64/76] arm: mediatek: add mt7623 pcie support
6 Signed-off-by: John Crispin <blogic@openwrt.org>
8 arch/arm/mach-mediatek/Makefile | 2 +-
9 arch/arm/mach-mediatek/pcie.c | 383 +++++++++++++++++++++++++++++++++++++++
10 arch/arm/mach-mediatek/pcie.h | 14 ++
11 3 files changed, 398 insertions(+), 1 deletion(-)
12 create mode 100644 arch/arm/mach-mediatek/pcie.c
13 create mode 100644 arch/arm/mach-mediatek/pcie.h
15 diff --git a/arch/arm/mach-mediatek/Makefile b/arch/arm/mach-mediatek/Makefile
16 index 2116460..aca28a2 100644
17 --- a/arch/arm/mach-mediatek/Makefile
18 +++ b/arch/arm/mach-mediatek/Makefile
20 ifeq ($(CONFIG_SMP),y)
21 obj-$(CONFIG_ARCH_MEDIATEK) += platsmp.o
23 -obj-$(CONFIG_ARCH_MEDIATEK) += mediatek.o
24 +obj-$(CONFIG_ARCH_MEDIATEK) += mediatek.o pcie.o
25 diff --git a/arch/arm/mach-mediatek/pcie.c b/arch/arm/mach-mediatek/pcie.c
27 index 0000000..8394712
29 +++ b/arch/arm/mach-mediatek/pcie.c
32 + * Mediatek MT7623 SoC PCIE support
34 + * Copyright (C) 2015 Mediatek
35 + * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
37 + * This program is free software; you can redistribute it and/or modify it
38 + * under the terms of the GNU General Public License version 2 as published
39 + * by the Free Software Foundation.
42 +#include <linux/kernel.h>
43 +#include <linux/pci.h>
44 +#include <linux/ioport.h>
45 +#include <linux/interrupt.h>
46 +#include <linux/spinlock.h>
47 +#include <linux/init.h>
48 +#include <linux/io.h>
49 +#include <linux/delay.h>
51 +#include <asm/mach/pci.h>
52 +#include <linux/module.h>
53 +#include <linux/of.h>
54 +#include <linux/of_irq.h>
55 +#include <linux/of_pci.h>
56 +#include <linux/reset.h>
57 +#include <linux/platform_device.h>
69 +#define BAR0SETUP 0x10
70 +#define IMBASEBAR0 0x18
71 +#define PCIE_CLASS 0x34
72 +#define PCIE_SISTAT 0x50
74 +#define MTK_PCIE_HIGH_PERF BIT(14)
75 +#define PCIEP0_BASE 0x2000
76 +#define PCIEP1_BASE 0x3000
77 +#define PCIEP2_BASE 0x4000
79 +#define PHY_P0_CTL 0x9000
80 +#define PHY_P1_CTL 0xA000
81 +#define PHY_P2_CTL 0x4000
83 +#define RSTCTL_PCIE0_RST BIT(24)
84 +#define RSTCTL_PCIE1_RST BIT(25)
85 +#define RSTCTL_PCIE2_RST BIT(26)
87 +static void __iomem *pcie_base;
88 +static int pcie_card_link;
90 +static struct mtk_pcie_port {
99 +} mtk_pcie_port[] = {
100 + { 0, 1, PCIEP0_BASE, PHY_P0_CTL, BIT(1), RSTCTL_PCIE0_RST, BIT(20) },
101 + { 1, 1, PCIEP1_BASE, PHY_P1_CTL, BIT(2), RSTCTL_PCIE1_RST, BIT(21) },
102 + { 2, 0, PCIEP2_BASE, PHY_P2_CTL, BIT(3), RSTCTL_PCIE2_RST, BIT(22) },
105 +#define mtk_foreach_port(p) \
106 + for (p = mtk_pcie_port; p != &mtk_pcie_port[ARRAY_SIZE(mtk_pcie_port)]; p++)
108 +#define mtk_foreach_port_enabled(p) \
109 + mtk_foreach_port(p) \
112 +#define mtk_foreach_port_link(p) \
113 + mtk_foreach_port(p) \
116 +static struct mtk_phy_init {
120 +} mtk_phy_init[] = {
121 + { 0xC00, 0x33000, 0x22000 },
122 + { 0xB04, 0xe0000000, 0x40000000 },
123 + { 0xB00, 0xe, 0x4 },
124 + { 0xC3C, 0xffff0000, 0x3c0000 },
125 + { 0xC48, 0xffff, 0x36 },
126 + { 0xC0C, 0x30000000, 0x10000000 },
127 + { 0xC08, 0x3800c0, 0xc0 },
128 + { 0xC10, 0xf0000, 0x20000 },
129 + { 0xC0C, 0xf000, 0x1000 },
130 + { 0xC14, 0xf0000, 0xa0000 },
133 +static inline void pcie_w32(u32 val, unsigned reg)
135 + iowrite32(val, pcie_base + reg);
138 +static inline u32 pcie_r32(unsigned reg)
140 + return ioread32(pcie_base + reg);
143 +static inline void pcie_m32(u32 mask, u32 val, unsigned reg)
145 + u32 v = pcie_r32(reg);
152 +static int pcie_config_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 * val)
154 + unsigned int slot = PCI_SLOT(devfn);
155 + u8 func = PCI_FUNC(devfn);
163 + address = (((where & 0xF00) >> 8) << 24) | (num << 16) | (slot << 11) | (func << 8) | (where & 0xfc);
164 + pcie_m32(0xf0000000, address, CFGADDR);
165 + data = pcie_r32(CFGDATA);
169 + *val = (data >> ((where & 3) << 3)) & 0xff;
172 + *val = (data >> ((where & 3) << 3)) & 0xffff;
179 + return PCIBIOS_SUCCESSFUL;
182 +static int pcie_config_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 val)
184 + unsigned int slot = PCI_SLOT(devfn);
185 + u8 func = PCI_FUNC(devfn);
193 + address = (((where & 0xF00) >> 8) << 24) | (num << 16) | (slot << 11) | (func << 8) | (where & 0xfc);
194 + pcie_m32(0xf0000000, address, CFGADDR);
195 + data = pcie_r32(CFGDATA);
199 + data = (data & ~(0xff << ((where & 3) << 3))) |
200 + (val << ((where & 3) << 3));
203 + data = (data & ~(0xffff << ((where & 3) << 3))) |
204 + (val << ((where & 3) << 3));
211 + pcie_w32(data, CFGDATA);
213 + return PCIBIOS_SUCCESSFUL;
216 +static struct pci_ops mtk_pcie_ops = {
217 + .read = pcie_config_read,
218 + .write = pcie_config_write,
221 +static struct resource pci_mem = {
222 + .name = "PCIe Memory space",
223 + .start = MEM_DIRECT1,
224 + .end = (u32) (MEM_DIRECT1 + (unsigned char *) 0x0fffffff),
225 + .flags = IORESOURCE_MEM,
228 +static struct resource pci_io = {
229 + .name = "PCIe IO space",
231 + .end = (u32) (IO_WIN + (unsigned char *) 0x0ffff),
232 + .flags = IORESOURCE_IO,
235 +static int __init mtk_pcie_setup(int nr, struct pci_sys_data *sys)
237 + sys->mem_offset = 0;
238 + sys->io_offset = 0;
240 + request_resource(&ioport_resource, &pci_io);
241 + request_resource(&iomem_resource, &pci_mem);
243 + pci_add_resource_offset(&sys->resources, &pci_io, sys->io_offset);
244 + pci_add_resource_offset(&sys->resources, &pci_mem, sys->mem_offset);
249 +static struct pci_bus * __init mtk_pcie_scan_bus(int nr, struct pci_sys_data *sys)
251 + return pci_scan_root_bus(NULL, sys->busnr, &mtk_pcie_ops, sys,
255 +static int __init mtk_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
260 + if (dev->bus->number == 0) {
261 + pcie_config_write(NULL, slot, 0, PCI_BASE_ADDRESS_0, MEMORY_BASE);
262 + pcie_config_read(NULL, slot, 0, PCI_BASE_ADDRESS_0, &val);
263 + printk("BAR0 at bus %d, slot %d\n", dev->bus->number, slot);
266 + printk("bus=0x%x, slot = 0x%x, pin=0x%x, irq=0x%x\n", dev->bus->number, slot, pin, dev->irq);
268 + pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 0x14);
269 + pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xFF);
270 + pci_read_config_word(dev, PCI_COMMAND, &cmd);
271 + cmd = cmd | PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
272 + pci_write_config_word(dev, PCI_COMMAND, cmd);
273 + pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
278 +static void __init mtk_pcie_preinit(void)
280 + struct mtk_pcie_port *port;
284 + pcibios_min_io = 0;
285 + pcibios_min_mem = 0;
287 +#if defined (CONFIG_PCIE_PORT2)
288 + printk("%s: PCIe/USB3 combo PHY mode (%x) =%x\n", __func__, SYSCFG1, REGDATA(SYSCFG1));
289 + REGDATA(SYSCFG1) &= ~(0x300000);
290 + printk("%s: PCIe/USB3 combo PHY mode (%x) =%x\n", __func__, SYSCFG1, REGDATA(SYSCFG1));
293 + /* PCIe RC Reset */
295 + mtk_foreach_port_enabled(port)
296 + val |= port->reset;
297 + REGDATA(RSTCTL) |= val;
299 + REGDATA(RSTCTL) &= ~val;
302 + /* Configure PCIe PHY */
303 + mtk_foreach_port_enabled(port) {
304 + for (i = 0; i < ARRAY_SIZE(mtk_phy_init); i++) {
305 + u32 val = pcie_r32(port->phy_base + mtk_phy_init[i].reg);
306 + val &= ~mtk_phy_init[i].mask;
307 + val |= mtk_phy_init[i].val;
308 + pcie_w32(val, port->phy_base + mtk_phy_init[i].reg);
314 + mtk_foreach_port_enabled(port) {
316 + pcie_config_read(NULL, port->id, 0, 0x73c, &val);
317 + val &= ~(0x9fff)<<16;
319 + pcie_config_write(NULL, port->id, 0, 0x73c, val);
322 + /* PCIe EP reset */
324 + mtk_foreach_port_enabled(port)
325 + val |= port->perst_n;
326 + val |= MTK_PCIE_HIGH_PERF;
327 + pcie_w32(pcie_r32(PCICFG) | val, PCICFG);
329 + pcie_w32(pcie_r32(PCICFG) & ~val, PCICFG);
332 + /* check the link status */
334 + mtk_foreach_port_enabled(port) {
335 + if ((pcie_r32(port->base + PCIE_SISTAT) & 0x1))
338 + val |= port->reset;
340 + REGDATA(RSTCTL) |= val;
342 + mtk_foreach_port_link(port)
345 + printk("PCIe Link count = %d\n", pcie_card_link);
346 + if (!pcie_card_link)
349 + pcie_w32(MEM_WIN, MEMBASE);
350 + pcie_w32(IO_WIN, IOBASE);
352 + mtk_foreach_port_link(port) {
353 + pcie_m32(0, port->interrupt, PCIENA);
354 + pcie_w32(0x7FFF0001, port->base + BAR0SETUP);
355 + pcie_w32(MEMORY_BASE, port->base + IMBASEBAR0);
356 + pcie_w32(0x06040001, port->base + PCIE_CLASS);
357 + printk("PCIE%d Setup OK\n", port->id);
361 + pcie_config_read(NULL, pcie_card_link - 1, 0, 0x4, &val);
362 + pcie_config_write(NULL, pcie_card_link - 1, 0, 0x4, val|0x4);
363 + pcie_config_read(NULL, pcie_card_link - 1, 0, 0x70c, &val);
364 + val &= ~(0xff3) << 8;
366 + pcie_config_write(NULL, pcie_card_link - 1, 0, 0x70c, val);
367 + pcie_config_read(NULL, pcie_card_link - 1, 0, 0x70c, &val);
370 +static struct hw_pci mtk_pci __initdata = {
371 + .nr_controllers = 1,
372 + .map_irq = mtk_pcie_map_irq,
373 + .setup = mtk_pcie_setup,
374 + .scan = mtk_pcie_scan_bus,
375 + .preinit = mtk_pcie_preinit,
378 +extern void mt7623_ethifsys_init(void);
379 +static int mtk_pcie_probe(struct platform_device *pdev)
381 + struct resource *pcie_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
383 + pcie_base = devm_ioremap_resource(&pdev->dev, pcie_res);
387 + mt7623_ethifsys_init();
388 + pci_common_init_dev(&pdev->dev, &mtk_pci);
393 +static const struct of_device_id mtk_pcie_ids[] = {
394 + { .compatible = "mediatek,mt7623-pcie" },
397 +MODULE_DEVICE_TABLE(of, mtk_pcie_ids);
399 +static struct platform_driver mtk_pcie_driver = {
400 + .probe = mtk_pcie_probe,
402 + .name = "mt7623-pcie",
403 + .owner = THIS_MODULE,
404 + .of_match_table = of_match_ptr(mtk_pcie_ids),
408 +static int __init mtk_pcie_init(void)
410 + return platform_driver_register(&mtk_pcie_driver);
413 +late_initcall(mtk_pcie_init);
414 diff --git a/arch/arm/mach-mediatek/pcie.h b/arch/arm/mach-mediatek/pcie.h
416 index 0000000..400a760e
418 +++ b/arch/arm/mach-mediatek/pcie.h
420 +#define SYSCTL_BASE 0xFA000000
421 +#define MEM_WIN 0x1A150000
422 +#define IO_WIN 0x1A160000
423 +#define MEM_DIRECT1 0x60000000
424 +#define MEMORY_BASE 0x80000000
426 +#define REGADDR(x, y) (x##_BASE + y)
427 +#define REGDATA(x) *((volatile unsigned int *)(x))
429 +#define SYSCFG1 REGADDR(SYSCTL, 0x14)
430 +#define RSTCTL REGADDR(SYSCTL, 0x34)