[lantiq] adds new lantiq kernel. once the codebase is fully tested and know to be...
[openwrt.git] / target / linux / lantiq / patches / 260-pci.patch
1 --- a/arch/mips/pci/Makefile
2 +++ b/arch/mips/pci/Makefile
3 @@ -55,6 +55,7 @@ obj-$(CONFIG_ZAO_CAPCELLA)    += fixup-capc
4  obj-$(CONFIG_WR_PPMC)          += fixup-wrppmc.o
5  obj-$(CONFIG_MIKROTIK_RB532)   += pci-rc32434.o ops-rc32434.o fixup-rc32434.o
6  obj-$(CONFIG_CPU_CAVIUM_OCTEON)        += pci-octeon.o pcie-octeon.o
7 +obj-$(CONFIG_LANTIQ)                   += pci-lantiq.o ops-lantiq.o
8  
9  ifdef CONFIG_PCI_MSI
10  obj-$(CONFIG_CPU_CAVIUM_OCTEON)        += msi-octeon.o
11 --- /dev/null
12 +++ b/arch/mips/pci/ops-lantiq.c
13 @@ -0,0 +1,127 @@
14 +/*
15 + *  This program is free software; you can redistribute it and/or modify it
16 + *  under the terms of the GNU General Public License version 2 as published
17 + *  by the Free Software Foundation.
18 + *
19 + *  Copyright (C) 2010 John Crispin <blogic@openwrt.org>
20 + */
21 +
22 +#include <linux/types.h>
23 +#include <linux/pci.h>
24 +#include <linux/kernel.h>
25 +#include <linux/init.h>
26 +#include <linux/delay.h>
27 +#include <linux/mm.h>
28 +#include <asm/addrspace.h>
29 +#include <linux/vmalloc.h>
30 +
31 +#include <xway.h>
32 +
33 +#define LQ_PCI_CFG_BUSNUM_SHF 16
34 +#define LQ_PCI_CFG_DEVNUM_SHF 11
35 +#define LQ_PCI_CFG_FUNNUM_SHF 8
36 +
37 +#define PCI_ACCESS_READ  0
38 +#define PCI_ACCESS_WRITE 1
39 +
40 +extern u32 lq_pci_mapped_cfg;
41 +
42 +static int
43 +lq_pci_config_access(unsigned char access_type,
44 +               struct pci_bus *bus, unsigned int devfn, unsigned int where, u32 *data)
45 +{
46 +       unsigned long cfg_base;
47 +       unsigned long flags;
48 +
49 +       u32 temp;
50 +
51 +       /* we support slot from 0 to 15 */
52 +       /* dev_fn 0&0x68 (AD29) is ifxmips itself */
53 +       if ((bus->number != 0) || ((devfn & 0xf8) > 0x78)
54 +                       || ((devfn & 0xf8) == 0) || ((devfn & 0xf8) == 0x68))
55 +               return 1;
56 +
57 +       spin_lock_irqsave(&ebu_lock, flags);
58 +
59 +       cfg_base = lq_pci_mapped_cfg;
60 +       cfg_base |= (bus->number << LQ_PCI_CFG_BUSNUM_SHF) | (devfn <<
61 +                       LQ_PCI_CFG_FUNNUM_SHF) | (where & ~0x3);
62 +
63 +       /* Perform access */
64 +       if (access_type == PCI_ACCESS_WRITE)
65 +       {
66 +#ifdef CONFIG_SWAP_IO_SPACE
67 +               lq_w32(swab32(*data), ((u32*)cfg_base));
68 +#else
69 +               lq_w32(*data, ((u32*)cfg_base));
70 +#endif
71 +       } else {
72 +               *data = lq_r32(((u32*)(cfg_base)));
73 +#ifdef CONFIG_SWAP_IO_SPACE
74 +               *data = swab32(*data);
75 +#endif
76 +       }
77 +       wmb();
78 +
79 +       /* clean possible Master abort */
80 +       cfg_base = (lq_pci_mapped_cfg | (0x0 << LQ_PCI_CFG_FUNNUM_SHF)) + 4;
81 +       temp = lq_r32(((u32*)(cfg_base)));
82 +#ifdef CONFIG_SWAP_IO_SPACE
83 +       temp = swab32 (temp);
84 +#endif
85 +       cfg_base = (lq_pci_mapped_cfg | (0x68 << LQ_PCI_CFG_FUNNUM_SHF)) + 4;
86 +       lq_w32(temp, ((u32*)cfg_base));
87 +
88 +       spin_unlock_irqrestore(&ebu_lock, flags);
89 +
90 +       if (((*data) == 0xffffffff) && (access_type == PCI_ACCESS_READ))
91 +               return 1;
92 +
93 +       return 0;
94 +}
95 +
96 +int
97 +lq_pci_read_config_dword(struct pci_bus *bus, unsigned int devfn,
98 +               int where, int size, u32 * val)
99 +{
100 +       u32 data = 0;
101 +
102 +       if (lq_pci_config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
103 +               return PCIBIOS_DEVICE_NOT_FOUND;
104 +
105 +       if (size == 1)
106 +               *val = (data >> ((where & 3) << 3)) & 0xff;
107 +       else if (size == 2)
108 +               *val = (data >> ((where & 3) << 3)) & 0xffff;
109 +       else
110 +               *val = data;
111 +
112 +       return PCIBIOS_SUCCESSFUL;
113 +}
114 +
115 +int
116 +lq_pci_write_config_dword(struct pci_bus *bus, unsigned int devfn,
117 +               int where, int size, u32 val)
118 +{
119 +       u32 data = 0;
120 +
121 +       if (size == 4)
122 +       {
123 +               data = val;
124 +       } else {
125 +               if (lq_pci_config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
126 +                       return PCIBIOS_DEVICE_NOT_FOUND;
127 +
128 +               if (size == 1)
129 +                       data = (data & ~(0xff << ((where & 3) << 3))) |
130 +                               (val << ((where & 3) << 3));
131 +               else if (size == 2)
132 +                       data = (data & ~(0xffff << ((where & 3) << 3))) |
133 +                               (val << ((where & 3) << 3));
134 +       }
135 +
136 +       if (lq_pci_config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
137 +               return PCIBIOS_DEVICE_NOT_FOUND;
138 +
139 +       return PCIBIOS_SUCCESSFUL;
140 +}
141 --- /dev/null
142 +++ b/arch/mips/pci/pci-lantiq.c
143 @@ -0,0 +1,293 @@
144 +/*
145 + *  This program is free software; you can redistribute it and/or modify it
146 + *  under the terms of the GNU General Public License version 2 as published
147 + *  by the Free Software Foundation.
148 + *
149 + *  Copyright (C) 2010 John Crispin <blogic@openwrt.org>
150 + */
151 +
152 +#include <linux/types.h>
153 +#include <linux/pci.h>
154 +#include <linux/kernel.h>
155 +#include <linux/init.h>
156 +#include <linux/delay.h>
157 +#include <linux/mm.h>
158 +#include <linux/vmalloc.h>
159 +#include <linux/platform_device.h>
160 +
161 +#include <asm/gpio.h>
162 +#include <asm/addrspace.h>
163 +
164 +#include <xway.h>
165 +#include <xway_irq.h>
166 +#include <lantiq_platform.h>
167 +
168 +#define LQ_PCI_CFG_BASE                0x17000000
169 +#define LQ_PCI_CFG_SIZE                0x00008000
170 +#define LQ_PCI_MEM_BASE                0x18000000
171 +#define LQ_PCI_MEM_SIZE                0x02000000
172 +#define LQ_PCI_IO_BASE         0x1AE00000
173 +#define LQ_PCI_IO_SIZE         0x00200000
174 +
175 +#define PCI_CR_FCI_ADDR_MAP0           ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x00C0))
176 +#define PCI_CR_FCI_ADDR_MAP1           ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x00C4))
177 +#define PCI_CR_FCI_ADDR_MAP2           ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x00C8))
178 +#define PCI_CR_FCI_ADDR_MAP3           ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x00CC))
179 +#define PCI_CR_FCI_ADDR_MAP4           ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x00D0))
180 +#define PCI_CR_FCI_ADDR_MAP5           ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x00D4))
181 +#define PCI_CR_FCI_ADDR_MAP6           ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x00D8))
182 +#define PCI_CR_FCI_ADDR_MAP7           ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x00DC))
183 +#define PCI_CR_CLK_CTRL                        ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x0000))
184 +#define PCI_CR_PCI_MOD                 ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x0030))
185 +#define PCI_CR_PC_ARB                  ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x0080))
186 +#define PCI_CR_FCI_ADDR_MAP11hg                ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x00E4))
187 +#define PCI_CR_BAR11MASK               ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x0044))
188 +#define PCI_CR_BAR12MASK               ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x0048))
189 +#define PCI_CR_BAR13MASK               ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x004C))
190 +#define PCI_CS_BASE_ADDR1              ((u32 *)(PCI_CS_PR_BASE_ADDR + 0x0010))
191 +#define PCI_CR_PCI_ADDR_MAP11          ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x0064))
192 +#define PCI_CR_FCI_BURST_LENGTH                ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x00E8))
193 +#define PCI_CR_PCI_EOI                 ((u32 *)(PCI_CR_PR_BASE_ADDR + 0x002C))
194 +
195 +
196 +#define PCI_CS_STS_CMD                 ((u32 *)(PCI_CS_PR_BASE_ADDR + 0x0004))
197 +
198 +#define PCI_MASTER0_REQ_MASK_2BITS     8
199 +#define PCI_MASTER1_REQ_MASK_2BITS     10
200 +#define PCI_MASTER2_REQ_MASK_2BITS     12
201 +#define INTERNAL_ARB_ENABLE_BIT                0
202 +
203 +#define LQ_CGU_IFCCR           ((u32 *)(LQ_CGU_BASE_ADDR + 0x0018))
204 +#define LQ_CGU_PCICR           ((u32 *)(LQ_CGU_BASE_ADDR + 0x0034))
205 +
206 +extern int lq_pci_read_config_dword(struct pci_bus *bus,
207 +       unsigned int devfn, int where, int size, u32 *val);
208 +extern int lq_pci_write_config_dword(struct pci_bus *bus,
209 +       unsigned int devfn, int where, int size, u32 val);
210 +
211 +u32 lq_pci_mapped_cfg;
212 +
213 +/* Since the PCI REQ pins can be reused for other functionality, make it possible
214 +   to exclude those from interpretation by the PCI controller */
215 +static int lq_pci_req_mask = 0xf;
216 +
217 +struct pci_ops lq_pci_ops =
218 +{
219 +       .read = lq_pci_read_config_dword,
220 +       .write = lq_pci_write_config_dword
221 +};
222 +
223 +static struct resource pci_io_resource =
224 +{
225 +       .name = "pci io space",
226 +       .start = LQ_PCI_IO_BASE,
227 +       .end = LQ_PCI_IO_BASE + LQ_PCI_IO_SIZE - 1,
228 +       .flags = IORESOURCE_IO
229 +};
230 +
231 +static struct resource pci_mem_resource =
232 +{
233 +       .name = "pci memory space",
234 +       .start = LQ_PCI_MEM_BASE,
235 +       .end = LQ_PCI_MEM_BASE + LQ_PCI_MEM_SIZE - 1,
236 +       .flags = IORESOURCE_MEM
237 +};
238 +
239 +static struct pci_controller lq_pci_controller =
240 +{
241 +       .pci_ops = &lq_pci_ops,
242 +       .mem_resource = &pci_mem_resource,
243 +       .mem_offset     = 0x00000000UL,
244 +       .io_resource = &pci_io_resource,
245 +       .io_offset      = 0x00000000UL,
246 +};
247 +
248 +int
249 +pcibios_plat_dev_init(struct pci_dev *dev)
250 +{
251 +       u8 pin;
252 +
253 +       pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
254 +       switch(pin)
255 +       {
256 +               case 0:
257 +                       break;
258 +               case 1:
259 +                       //falling edge level triggered:0x4, low level:0xc, rising edge:0x2
260 +                       lq_w32(lq_r32(LQ_EBU_PCC_CON) | 0xc, LQ_EBU_PCC_CON);
261 +                       lq_w32(lq_r32(LQ_EBU_PCC_IEN) | 0x10, LQ_EBU_PCC_IEN);
262 +                       break;
263 +               case 2:
264 +               case 3:
265 +               case 4:
266 +                       printk ("WARNING: interrupt pin %d not supported yet!\n", pin);
267 +               default:
268 +                       printk ("WARNING: invalid interrupt pin %d\n", pin);
269 +                       return 1;
270 +       }
271 +       return 0;
272 +}
273 +
274 +static u32
275 +lq_calc_bar11mask(void)
276 +{
277 +       u32 mem, bar11mask;
278 +
279 +       /* BAR11MASK value depends on available memory on system. */
280 +       mem = num_physpages * PAGE_SIZE;
281 +       bar11mask = (0x0ffffff0 & ~((1 << (fls(mem) -1)) -1)) | 8;
282 +
283 +       return bar11mask;
284 +}
285 +
286 +static void
287 +lq_pci_setup_clk(int external_clock)
288 +{
289 +       /* set clock to 33Mhz */
290 +       lq_w32(lq_r32(LQ_CGU_IFCCR) & ~0xf00000, LQ_CGU_IFCCR);
291 +       lq_w32(lq_r32(LQ_CGU_IFCCR) | 0x800000, LQ_CGU_IFCCR);
292 +       if (external_clock)
293 +       {
294 +               lq_w32(lq_r32(LQ_CGU_IFCCR) & ~(1 << 16), LQ_CGU_IFCCR);
295 +               lq_w32((1 << 30), LQ_CGU_PCICR);
296 +       } else {
297 +               lq_w32(lq_r32(LQ_CGU_IFCCR) | (1 << 16), LQ_CGU_IFCCR);
298 +               lq_w32((1 << 31) | (1 << 30), LQ_CGU_PCICR);
299 +       }
300 +}
301 +
302 +static void
303 +lq_pci_setup_gpio(void)
304 +{
305 +       /* PCI reset line is gpio driven */
306 +       lq_gpio_request(21, 0, 0, 1, "pci-reset");
307 +
308 +       /* PCI_REQ line */
309 +       lq_gpio_request(29, 1, 0, 0, "pci-req");
310 +
311 +       /* PCI_GNT line */
312 +       lq_gpio_request(30, 1, 0, 1, "pci-gnt");
313 +}
314 +
315 +static int __init
316 +lq_pci_startup(void)
317 +{
318 +       u32 temp_buffer;
319 +
320 +       /* setup pci clock and gpis used by pci */
321 +       lq_pci_setup_gpio();
322 +
323 +       /* enable auto-switching between PCI and EBU */
324 +       lq_w32(0xa, PCI_CR_CLK_CTRL);
325 +
326 +       /* busy, i.e. configuration is not done, PCI access has to be retried */
327 +       lq_w32(lq_r32(PCI_CR_PCI_MOD) & ~(1 << 24), PCI_CR_PCI_MOD);
328 +       wmb ();
329 +       /* BUS Master/IO/MEM access */
330 +       lq_w32(lq_r32(PCI_CS_STS_CMD) | 7, PCI_CS_STS_CMD);
331 +
332 +       /* enable external 2 PCI masters */
333 +       temp_buffer = lq_r32(PCI_CR_PC_ARB);
334 +       temp_buffer &= (~(lq_pci_req_mask << 16));
335 +       /* enable internal arbiter */
336 +       temp_buffer |= (1 << INTERNAL_ARB_ENABLE_BIT);
337 +       /* enable internal PCI master reqest */
338 +       temp_buffer &= (~(3 << PCI_MASTER0_REQ_MASK_2BITS));
339 +
340 +       /* enable EBU request */
341 +       temp_buffer &= (~(3 << PCI_MASTER1_REQ_MASK_2BITS));
342 +
343 +       /* enable all external masters request */
344 +       temp_buffer &= (~(3 << PCI_MASTER2_REQ_MASK_2BITS));
345 +       lq_w32(temp_buffer, PCI_CR_PC_ARB);
346 +       wmb ();
347 +
348 +       /* setup BAR memory regions */
349 +       lq_w32(0x18000000, PCI_CR_FCI_ADDR_MAP0);
350 +       lq_w32(0x18400000, PCI_CR_FCI_ADDR_MAP1);
351 +       lq_w32(0x18800000, PCI_CR_FCI_ADDR_MAP2);
352 +       lq_w32(0x18c00000, PCI_CR_FCI_ADDR_MAP3);
353 +       lq_w32(0x19000000, PCI_CR_FCI_ADDR_MAP4);
354 +       lq_w32(0x19400000, PCI_CR_FCI_ADDR_MAP5);
355 +       lq_w32(0x19800000, PCI_CR_FCI_ADDR_MAP6);
356 +       lq_w32(0x19c00000, PCI_CR_FCI_ADDR_MAP7);
357 +       lq_w32(0x1ae00000, PCI_CR_FCI_ADDR_MAP11hg);
358 +       lq_w32(lq_calc_bar11mask(), PCI_CR_BAR11MASK);
359 +       lq_w32(0, PCI_CR_PCI_ADDR_MAP11);
360 +       lq_w32(0, PCI_CS_BASE_ADDR1);
361 +#ifdef CONFIG_SWAP_IO_SPACE
362 +       /* both TX and RX endian swap are enabled */
363 +       lq_w32(lq_r32(PCI_CR_PCI_EOI) | 3, PCI_CR_PCI_EOI);
364 +       wmb ();
365 +#endif
366 +       /*TODO: disable BAR2 & BAR3 - why was this in the origianl infineon code */
367 +       lq_w32(lq_r32(PCI_CR_BAR12MASK) | 0x80000000, PCI_CR_BAR12MASK);
368 +       lq_w32(lq_r32(PCI_CR_BAR13MASK) | 0x80000000, PCI_CR_BAR13MASK);
369 +       /*use 8 dw burst length */
370 +       lq_w32(0x303, PCI_CR_FCI_BURST_LENGTH);
371 +       lq_w32(lq_r32(PCI_CR_PCI_MOD) | (1 << 24), PCI_CR_PCI_MOD);
372 +       wmb();
373 +
374 +       /* toggle reset pin */
375 +       __gpio_set_value(21, 0);
376 +       wmb();
377 +       mdelay(1);
378 +       __gpio_set_value(21, 1);
379 +       return 0;
380 +}
381 +
382 +int __init
383 +pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin){
384 +       switch(slot)
385 +       {
386 +               case 13:
387 +                       /* IDSEL = AD29 --> USB Host Controller */
388 +                       return (INT_NUM_IM1_IRL0 + 17);
389 +               case 14:
390 +                       /* IDSEL = AD30 --> mini PCI connector */
391 +                       return (INT_NUM_IM0_IRL0 + 22);
392 +               default:
393 +                       printk("lq_pci: no IRQ found for slot %d, pin %d\n", slot, pin);
394 +                       return 0;
395 +       }
396 +}
397 +
398 +static int
399 +lq_pci_probe(struct platform_device *pdev)
400 +{
401 +       struct lq_pci_data *lq_pci_data = (struct lq_pci_data*) pdev->dev.platform_data;
402 +       extern int pci_probe_only;
403 +
404 +       pci_probe_only = 0;
405 +       lq_pci_req_mask = lq_pci_data->req_mask;
406 +       lq_pci_setup_clk(lq_pci_data->clock);
407 +
408 +       lq_pci_startup();
409 +       lq_pci_mapped_cfg =
410 +               (u32)ioremap_nocache(LQ_PCI_CFG_BASE, LQ_PCI_CFG_BASE);
411 +       lq_pci_controller.io_map_base =
412 +               (unsigned long)ioremap(LQ_PCI_IO_BASE, LQ_PCI_IO_SIZE - 1);
413 +
414 +       register_pci_controller(&lq_pci_controller);
415 +       return 0;
416 +}
417 +
418 +static struct platform_driver
419 +lq_pci_driver = {
420 +       .probe = lq_pci_probe,
421 +       .driver = {
422 +               .name = "lq_pci",
423 +               .owner = THIS_MODULE,
424 +       },
425 +};
426 +
427 +int __init
428 +pcibios_init(void)
429 +{
430 +       int ret = platform_driver_register(&lq_pci_driver);
431 +       if(ret)
432 +               printk(KERN_INFO "lq_pci: Error registering platfom driver!");
433 +       return ret;
434 +}
435 +
436 +arch_initcall(pcibios_init);