ramips: basic support for Planex MZK-750DHP
[openwrt/openwrt.git] / target / linux / ramips / patches-3.10 / 0115-PCI-MIPS-adds-mt7620a-pcie-driver.patch
1 From 686f5642c74323f7e7eafb93c2b85df589cbf66e Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sat, 18 May 2013 22:06:15 +0200
4 Subject: [PATCH 115/133] PCI: MIPS: adds mt7620a pcie driver
5
6 Signed-off-by: John Crispin <blogic@openwrt.org>
7 ---
8 arch/mips/pci/Makefile | 1 +
9 arch/mips/pci/pci-mt7620a.c | 363 +++++++++++++++++++++++++++++++++++++++++++
10 arch/mips/ralink/Kconfig | 1 +
11 3 files changed, 365 insertions(+)
12 create mode 100644 arch/mips/pci/pci-mt7620a.c
13
14 --- a/arch/mips/pci/Makefile
15 +++ b/arch/mips/pci/Makefile
16 @@ -41,6 +41,7 @@ obj-$(CONFIG_SIBYTE_BCM1x80) += pci-bcm1
17 obj-$(CONFIG_SNI_RM) += fixup-sni.o ops-sni.o
18 obj-$(CONFIG_LANTIQ) += fixup-lantiq.o
19 obj-$(CONFIG_PCI_LANTIQ) += pci-lantiq.o ops-lantiq.o
20 +obj-$(CONFIG_SOC_MT7620) += pci-mt7620a.o
21 obj-$(CONFIG_SOC_RT2880) += pci-rt2880.o
22 obj-$(CONFIG_SOC_RT3883) += pci-rt3883.o
23 obj-$(CONFIG_TANBAC_TB0219) += fixup-tb0219.o
24 --- /dev/null
25 +++ b/arch/mips/pci/pci-mt7620a.c
26 @@ -0,0 +1,363 @@
27 +/*
28 + * Ralink MT7620A SoC PCI support
29 + *
30 + * Copyright (C) 2007-2013 Bruce Chang
31 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
32 + *
33 + * This program is free software; you can redistribute it and/or modify it
34 + * under the terms of the GNU General Public License version 2 as published
35 + * by the Free Software Foundation.
36 + */
37 +
38 +#include <linux/types.h>
39 +#include <linux/pci.h>
40 +#include <linux/io.h>
41 +#include <linux/init.h>
42 +#include <linux/delay.h>
43 +#include <linux/interrupt.h>
44 +#include <linux/module.h>
45 +#include <linux/of.h>
46 +#include <linux/of_irq.h>
47 +#include <linux/of_pci.h>
48 +#include <linux/reset.h>
49 +#include <linux/platform_device.h>
50 +
51 +#include <asm/mach-ralink/ralink_regs.h>
52 +
53 +#define RALINK_PCI_MM_MAP_BASE 0x20000000
54 +#define RALINK_PCI_IO_MAP_BASE 0x10160000
55 +
56 +#define RALINK_INT_PCIE0 4
57 +#define RALINK_SYSTEM_CONTROL_BASE 0xb0000000
58 +#define RALINK_SYSCFG1 0x14
59 +#define RALINK_CLKCFG1 0x30
60 +#define RALINK_GPIOMODE 0x60
61 +#define RALINK_PCIE_CLK_GEN 0x7c
62 +#define RALINK_PCIE_CLK_GEN1 0x80
63 +#define PCIEPHY0_CFG 0x90
64 +#define PPLL_CFG1 0x9c
65 +#define PPLL_DRV 0xa0
66 +#define RALINK_PCI_HOST_MODE_EN (1<<7)
67 +#define RALINK_PCIE_RC_MODE_EN (1<<8)
68 +#define RALINK_PCIE_RST (1<<23)
69 +#define RALINK_PCI_RST (1<<24)
70 +#define RALINK_PCI_CLK_EN (1<<19)
71 +#define RALINK_PCIE_CLK_EN (1<<21)
72 +#define PCI_SLOTx2 (1<<11)
73 +#define PCI_SLOTx1 (2<<11)
74 +#define PDRV_SW_SET (1<<31)
75 +#define LC_CKDRVPD_ (1<<19)
76 +
77 +#define RALINK_PCI_CONFIG_ADDR 0x20
78 +#define RALINK_PCI_CONFIG_DATA_VIRTUAL_REG 0x24
79 +#define MEMORY_BASE 0x0
80 +#define RALINK_PCIE0_RST (1<<26)
81 +#define RALINK_PCI_BASE 0xB0140000
82 +#define RALINK_PCI_MEMBASE 0x28
83 +#define RALINK_PCI_IOBASE 0x2C
84 +
85 +#define RT6855_PCIE0_OFFSET 0x2000
86 +
87 +#define RALINK_PCI_PCICFG_ADDR 0x00
88 +#define RALINK_PCI0_BAR0SETUP_ADDR 0x10
89 +#define RALINK_PCI0_IMBASEBAR0_ADDR 0x18
90 +#define RALINK_PCI0_ID 0x30
91 +#define RALINK_PCI0_CLASS 0x34
92 +#define RALINK_PCI0_SUBID 0x38
93 +#define RALINK_PCI0_STATUS 0x50
94 +#define RALINK_PCI_PCIMSK_ADDR 0x0C
95 +
96 +#define RALINK_PCIE0_CLK_EN (1 << 26)
97 +
98 +#define BUSY 0x80000000
99 +#define WAITRETRY_MAX 10
100 +#define WRITE_MODE (1UL << 23)
101 +#define DATA_SHIFT 0
102 +#define ADDR_SHIFT 8
103 +
104 +
105 +static void __iomem *bridge_base;
106 +static void __iomem *pcie_base;
107 +
108 +static struct reset_control *rstpcie0;
109 +
110 +static inline void bridge_w32(u32 val, unsigned reg)
111 +{
112 + iowrite32(val, bridge_base + reg);
113 +}
114 +
115 +static inline u32 bridge_r32(unsigned reg)
116 +{
117 + return ioread32(bridge_base + reg);
118 +}
119 +
120 +static inline void pcie_w32(u32 val, unsigned reg)
121 +{
122 + iowrite32(val, pcie_base + reg);
123 +}
124 +
125 +static inline u32 pcie_r32(unsigned reg)
126 +{
127 + return ioread32(pcie_base + reg);
128 +}
129 +
130 +static inline void pcie_m32(u32 clr, u32 set, unsigned reg)
131 +{
132 + u32 val = pcie_r32(reg);
133 + val &= ~clr;
134 + val |= set;
135 + pcie_w32(val, reg);
136 +}
137 +
138 +int wait_pciephy_busy(void)
139 +{
140 + unsigned long reg_value = 0x0, retry = 0;
141 +
142 + while (1) {
143 + //reg_value = rareg(READMODE, PCIEPHY0_CFG, 0);
144 + reg_value = pcie_r32(PCIEPHY0_CFG);
145 +
146 + if (reg_value & BUSY)
147 + mdelay(100);
148 + else
149 + break;
150 + if (retry++ > WAITRETRY_MAX){
151 + printk("PCIE-PHY retry failed.\n");
152 + return -1;
153 + }
154 + }
155 + return 0;
156 +}
157 +
158 +static void pcie_phy(unsigned long addr, unsigned long val)
159 +{
160 + wait_pciephy_busy();
161 + pcie_w32(WRITE_MODE | (val << DATA_SHIFT) | (addr << ADDR_SHIFT), PCIEPHY0_CFG);
162 + mdelay(1);
163 + wait_pciephy_busy();
164 +}
165 +
166 +static int pci_config_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 * val)
167 +{
168 + unsigned int slot = PCI_SLOT(devfn);
169 + u8 func = PCI_FUNC(devfn);
170 + u32 address;
171 + u32 data;
172 +
173 + address = (((where & 0xF00) >> 8) << 24) | (bus->number << 16) | (slot << 11) | (func << 8) | (where & 0xfc) | 0x80000000;
174 + bridge_w32(address, RALINK_PCI_CONFIG_ADDR);
175 + data = bridge_r32(RALINK_PCI_CONFIG_DATA_VIRTUAL_REG);
176 +
177 + switch (size) {
178 + case 1:
179 + *val = (data >> ((where & 3) << 3)) & 0xff;
180 + break;
181 + case 2:
182 + *val = (data >> ((where & 3) << 3)) & 0xffff;
183 + break;
184 + case 4:
185 + *val = data;
186 + break;
187 + }
188 +
189 + return PCIBIOS_SUCCESSFUL;
190 +}
191 +
192 +static int pci_config_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 val)
193 +{
194 + unsigned int slot = PCI_SLOT(devfn);
195 + u8 func = PCI_FUNC(devfn);
196 + u32 address;
197 + u32 data;
198 +
199 + address = (((where & 0xF00) >> 8) << 24) | (bus->number << 16) | (slot << 11) | (func << 8) | (where & 0xfc) | 0x80000000;
200 + bridge_w32(address, RALINK_PCI_CONFIG_ADDR);
201 + data = bridge_r32(RALINK_PCI_CONFIG_DATA_VIRTUAL_REG);
202 +
203 + switch (size) {
204 + case 1:
205 + data = (data & ~(0xff << ((where & 3) << 3))) |
206 + (val << ((where & 3) << 3));
207 + break;
208 + case 2:
209 + data = (data & ~(0xffff << ((where & 3) << 3))) |
210 + (val << ((where & 3) << 3));
211 + break;
212 + case 4:
213 + data = val;
214 + break;
215 + }
216 +
217 + bridge_w32(data, RALINK_PCI_CONFIG_DATA_VIRTUAL_REG);
218 +
219 + return PCIBIOS_SUCCESSFUL;
220 +}
221 +
222 +struct pci_ops mt7620a_pci_ops= {
223 + .read = pci_config_read,
224 + .write = pci_config_write,
225 +};
226 +
227 +static struct resource mt7620a_res_pci_mem1 = {
228 + .name = "pci memory",
229 + .start = RALINK_PCI_MM_MAP_BASE,
230 + .end = (u32) ((RALINK_PCI_MM_MAP_BASE + (unsigned char *)0x0fffffff)),
231 + .flags = IORESOURCE_MEM,
232 +};
233 +static struct resource mt7620a_res_pci_io1 = {
234 + .name = "pci io",
235 + .start = RALINK_PCI_IO_MAP_BASE,
236 + .end = (u32) ((RALINK_PCI_IO_MAP_BASE + (unsigned char *)0x0ffff)),
237 + .flags = IORESOURCE_IO,
238 +};
239 +
240 +struct pci_controller mt7620a_controller = {
241 + .pci_ops = &mt7620a_pci_ops,
242 + .mem_resource = &mt7620a_res_pci_mem1,
243 + .io_resource = &mt7620a_res_pci_io1,
244 + .mem_offset = 0x00000000UL,
245 + .io_offset = 0x00000000UL,
246 + .io_map_base = 0xa0000000,
247 +};
248 +
249 +static int mt7620a_pci_probe(struct platform_device *pdev)
250 +{
251 + struct resource *bridge_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
252 + struct resource *pcie_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
253 +
254 + rstpcie0 = devm_reset_control_get(&pdev->dev, "pcie0");
255 + if (IS_ERR(rstpcie0))
256 + return PTR_ERR(rstpcie0);
257 +
258 + bridge_base = devm_request_and_ioremap(&pdev->dev, bridge_res);
259 + if (!bridge_base)
260 + return -ENOMEM;
261 +
262 + pcie_base = devm_request_and_ioremap(&pdev->dev, pcie_res);
263 + if (!pcie_base)
264 + return -ENOMEM;
265 +
266 + iomem_resource.start = 0;
267 + iomem_resource.end= ~0;
268 + ioport_resource.start= 0;
269 + ioport_resource.end = ~0;
270 +
271 + /* PCIE: bypass PCIe DLL */
272 + pcie_phy(0x0, 0x80);
273 + pcie_phy(0x1, 0x04);
274 + /* PCIE: Elastic buffer control */
275 + pcie_phy(0x68, 0xB4);
276 +
277 + reset_control_assert(rstpcie0);
278 + rt_sysc_m32(RALINK_PCIE0_CLK_EN, 0, RALINK_CLKCFG1);
279 + rt_sysc_m32(1<<19, 1<<31, PPLL_DRV);
280 + rt_sysc_m32(0x3 << 16, 0, RALINK_GPIOMODE);
281 +
282 + reset_control_deassert(rstpcie0);
283 + rt_sysc_m32(0, RALINK_PCIE0_CLK_EN, RALINK_CLKCFG1);
284 +
285 + mdelay(100);
286 +
287 + if (!(rt_sysc_r32(PPLL_CFG1) & 1<<23)) {
288 + printk("MT7620 PPLL unlock\n");
289 + reset_control_assert(rstpcie0);
290 + rt_sysc_m32(BIT(26), 0, RALINK_CLKCFG1);
291 + return 0;
292 + }
293 + rt_sysc_m32((0x1<<18) | (0x1<<17), (0x1 << 19) | (0x1 << 31), PPLL_DRV);
294 +
295 + mdelay(100);
296 + reset_control_assert(rstpcie0);
297 + rt_sysc_m32(0x30, 2 << 4, RALINK_SYSCFG1);
298 +
299 + rt_sysc_m32(~0x7fffffff, 0x80000000, RALINK_PCIE_CLK_GEN);
300 + rt_sysc_m32(~0x80ffffff, 0xa << 24, RALINK_PCIE_CLK_GEN1);
301 +
302 + mdelay(50);
303 + reset_control_deassert(rstpcie0);
304 + pcie_m32(BIT(1), 0, RALINK_PCI_PCICFG_ADDR);
305 + mdelay(100);
306 +
307 + if (( pcie_r32(RALINK_PCI0_STATUS) & 0x1) == 0) {
308 + reset_control_assert(rstpcie0);
309 + rt_sysc_m32(RALINK_PCIE0_CLK_EN, 0, RALINK_CLKCFG1);
310 + rt_sysc_m32(LC_CKDRVPD_, PDRV_SW_SET, PPLL_DRV);
311 + printk("PCIE0 no card, disable it(RST&CLK)\n");
312 + }
313 +
314 + bridge_w32(0xffffffff, RALINK_PCI_MEMBASE);
315 + bridge_w32(RALINK_PCI_IO_MAP_BASE, RALINK_PCI_IOBASE);
316 +
317 + pcie_w32(0x7FFF0000, RALINK_PCI0_BAR0SETUP_ADDR);
318 + pcie_w32(MEMORY_BASE, RALINK_PCI0_IMBASEBAR0_ADDR);
319 + pcie_w32(0x08021814, RALINK_PCI0_ID);
320 + pcie_w32(0x06040001, RALINK_PCI0_CLASS);
321 + pcie_w32(0x28801814, RALINK_PCI0_SUBID);
322 + pcie_m32(0, BIT(20), RALINK_PCI_PCIMSK_ADDR);
323 +
324 + register_pci_controller(&mt7620a_controller);
325 +
326 + return 0;
327 +}
328 +
329 +int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
330 +{
331 + const struct resource *res;
332 + u16 cmd;
333 + u32 val;
334 + int i, irq = 0;
335 +
336 + if ((dev->bus->number == 0) && (slot == 0)) {
337 + pcie_w32(0x7FFF0001, RALINK_PCI0_BAR0SETUP_ADDR); //open 7FFF:2G; ENABLE
338 + pci_config_write(dev->bus, 0, PCI_BASE_ADDRESS_0, 4, MEMORY_BASE);
339 + pci_config_read(dev->bus, 0, PCI_BASE_ADDRESS_0, 4, &val);
340 + } else if ((dev->bus->number == 1) && (slot == 0x0)) {
341 + irq = RALINK_INT_PCIE0;
342 + } else {
343 + printk("bus=0x%x, slot = 0x%x\n", dev->bus->number, slot);
344 + return 0;
345 + }
346 +
347 + for (i = 0; i < 6; i++) {
348 + res = &dev->resource[i];
349 + }
350 +
351 + pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 0x14); //configure cache line size 0x14
352 + pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0xFF); //configure latency timer 0x10
353 + pci_read_config_word(dev, PCI_COMMAND, &cmd);
354 +
355 + // FIXME
356 + cmd = cmd | PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
357 + pci_write_config_word(dev, PCI_COMMAND, cmd);
358 + pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
359 + //pci_write_config_byte(dev, PCI_INTERRUPT_PIN, dev->irq);
360 +
361 + return irq;
362 +}
363 +
364 +int pcibios_plat_dev_init(struct pci_dev *dev)
365 +{
366 + return 0;
367 +}
368 +
369 +static const struct of_device_id mt7620a_pci_ids[] = {
370 + { .compatible = "ralink,mt7620a-pci" },
371 + {},
372 +};
373 +MODULE_DEVICE_TABLE(of, mt7620a_pci_ids);
374 +
375 +static struct platform_driver mt7620a_pci_driver = {
376 + .probe = mt7620a_pci_probe,
377 + .driver = {
378 + .name = "mt7620a-pci",
379 + .owner = THIS_MODULE,
380 + .of_match_table = of_match_ptr(mt7620a_pci_ids),
381 + },
382 +};
383 +
384 +static int __init mt7620a_pci_init(void)
385 +{
386 + return platform_driver_register(&mt7620a_pci_driver);
387 +}
388 +
389 +arch_initcall(mt7620a_pci_init);
390 --- a/arch/mips/ralink/Kconfig
391 +++ b/arch/mips/ralink/Kconfig
392 @@ -33,6 +33,7 @@ choice
393 bool "MT7620"
394 select USB_ARCH_HAS_OHCI
395 select USB_ARCH_HAS_EHCI
396 + select HW_HAS_PCI
397
398 endchoice
399