Pull thermal into release branch
[linux-drm-fsl-dcu.git] / arch / arm / mach-versatile / pci.c
1 /*
2  *  linux/arch/arm/mach-versatile/pci.c
3  *
4  * (C) Copyright Koninklijke Philips Electronics NV 2004. All rights reserved.
5  * You can redistribute and/or modify this software under the terms of version 2
6  * of the GNU General Public License as published by the Free Software Foundation.
7  * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
8  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9  * General Public License for more details.
10  * Koninklijke Philips Electronics nor its subsidiaries is obligated to provide any support for this software.
11  *
12  * ARM Versatile PCI driver.
13  *
14  * 14/04/2005 Initial version, colin.king@philips.com
15  *
16  */
17 #include <linux/kernel.h>
18 #include <linux/pci.h>
19 #include <linux/slab.h>
20 #include <linux/ioport.h>
21 #include <linux/interrupt.h>
22 #include <linux/spinlock.h>
23 #include <linux/init.h>
24
25 #include <asm/hardware.h>
26 #include <asm/io.h>
27 #include <asm/irq.h>
28 #include <asm/system.h>
29 #include <asm/mach/pci.h>
30
31 /*
32  * these spaces are mapped using the following base registers:
33  *
34  * Usage Local Bus Memory         Base/Map registers used
35  *
36  * Mem   50000000 - 5FFFFFFF      LB_BASE0/LB_MAP0,  non prefetch
37  * Mem   60000000 - 6FFFFFFF      LB_BASE1/LB_MAP1,  prefetch
38  * IO    44000000 - 4FFFFFFF      LB_BASE2/LB_MAP2,  IO
39  * Cfg   42000000 - 42FFFFFF      PCI config
40  *
41  */
42 #define __IO_ADDRESS(n) ((void __iomem *)(unsigned long)IO_ADDRESS(n))
43 #define SYS_PCICTL              __IO_ADDRESS(VERSATILE_SYS_PCICTL)
44 #define PCI_IMAP0               __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x0)
45 #define PCI_IMAP1               __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x4)
46 #define PCI_IMAP2               __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x8)
47 #define PCI_SMAP0               __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x10)
48 #define PCI_SMAP1               __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x14)
49 #define PCI_SMAP2               __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0x18)
50 #define PCI_SELFID              __IO_ADDRESS(VERSATILE_PCI_CORE_BASE+0xc)
51
52 #define DEVICE_ID_OFFSET                0x00
53 #define CSR_OFFSET                      0x04
54 #define CLASS_ID_OFFSET                 0x08
55
56 #define VP_PCI_DEVICE_ID                0x030010ee
57 #define VP_PCI_CLASS_ID                 0x0b400000
58
59 static unsigned long pci_slot_ignore = 0;
60
61 static int __init versatile_pci_slot_ignore(char *str)
62 {
63         int retval;
64         int slot;
65
66         while ((retval = get_option(&str,&slot))) {
67                 if ((slot < 0) || (slot > 31)) {
68                         printk("Illegal slot value: %d\n",slot);
69                 } else {
70                         pci_slot_ignore |= (1 << slot);
71                 }
72         }
73         return 1;
74 }
75
76 __setup("pci_slot_ignore=", versatile_pci_slot_ignore);
77
78
79 static void __iomem *__pci_addr(struct pci_bus *bus,
80                                 unsigned int devfn, int offset)
81 {
82         unsigned int busnr = bus->number;
83
84         /*
85          * Trap out illegal values
86          */
87         if (offset > 255)
88                 BUG();
89         if (busnr > 255)
90                 BUG();
91         if (devfn > 255)
92                 BUG();
93
94         return VERSATILE_PCI_CFG_VIRT_BASE + ((busnr << 16) |
95                 (PCI_SLOT(devfn) << 11) | (PCI_FUNC(devfn) << 8) | offset);
96 }
97
98 static int versatile_read_config(struct pci_bus *bus, unsigned int devfn, int where,
99                                  int size, u32 *val)
100 {
101         void __iomem *addr = __pci_addr(bus, devfn, where & ~3);
102         u32 v;
103         int slot = PCI_SLOT(devfn);
104
105         if (pci_slot_ignore & (1 << slot)) {
106                 /* Ignore this slot */
107                 switch (size) {
108                 case 1:
109                         v = 0xff;
110                         break;
111                 case 2:
112                         v = 0xffff;
113                         break;
114                 default:
115                         v = 0xffffffff;
116                 }
117         } else {
118                 switch (size) {
119                 case 1:
120                         v = __raw_readb(addr);
121                         break;
122
123                 case 2:
124                         v = __raw_readl(addr);
125                         if (where & 2) v >>= 16;
126                         v &= 0xffff;
127                         break;
128
129                 default:
130                         v = __raw_readl(addr);
131                         break;
132                 }
133         }
134
135         *val = v;
136         return PCIBIOS_SUCCESSFUL;
137 }
138
139 static int versatile_write_config(struct pci_bus *bus, unsigned int devfn, int where,
140                                   int size, u32 val)
141 {
142         void __iomem *addr = __pci_addr(bus, devfn, where);
143         int slot = PCI_SLOT(devfn);
144
145         if (pci_slot_ignore & (1 << slot)) {
146                 return PCIBIOS_SUCCESSFUL;
147         }
148
149         switch (size) {
150         case 1:
151                 __raw_writeb((u8)val, addr);
152                 break;
153
154         case 2:
155                 __raw_writew((u16)val, addr);
156                 break;
157
158         case 4:
159                 __raw_writel(val, addr);
160                 break;
161         }
162
163         return PCIBIOS_SUCCESSFUL;
164 }
165
166 static struct pci_ops pci_versatile_ops = {
167         .read   = versatile_read_config,
168         .write  = versatile_write_config,
169 };
170
171 static struct resource io_mem = {
172         .name   = "PCI I/O space",
173         .start  = VERSATILE_PCI_MEM_BASE0,
174         .end    = VERSATILE_PCI_MEM_BASE0+VERSATILE_PCI_MEM_BASE0_SIZE-1,
175         .flags  = IORESOURCE_IO,
176 };
177
178 static struct resource non_mem = {
179         .name   = "PCI non-prefetchable",
180         .start  = VERSATILE_PCI_MEM_BASE1,
181         .end    = VERSATILE_PCI_MEM_BASE1+VERSATILE_PCI_MEM_BASE1_SIZE-1,
182         .flags  = IORESOURCE_MEM,
183 };
184
185 static struct resource pre_mem = {
186         .name   = "PCI prefetchable",
187         .start  = VERSATILE_PCI_MEM_BASE2,
188         .end    = VERSATILE_PCI_MEM_BASE2+VERSATILE_PCI_MEM_BASE2_SIZE-1,
189         .flags  = IORESOURCE_MEM | IORESOURCE_PREFETCH,
190 };
191
192 static int __init pci_versatile_setup_resources(struct resource **resource)
193 {
194         int ret = 0;
195
196         ret = request_resource(&iomem_resource, &io_mem);
197         if (ret) {
198                 printk(KERN_ERR "PCI: unable to allocate I/O "
199                        "memory region (%d)\n", ret);
200                 goto out;
201         }
202         ret = request_resource(&iomem_resource, &non_mem);
203         if (ret) {
204                 printk(KERN_ERR "PCI: unable to allocate non-prefetchable "
205                        "memory region (%d)\n", ret);
206                 goto release_io_mem;
207         }
208         ret = request_resource(&iomem_resource, &pre_mem);
209         if (ret) {
210                 printk(KERN_ERR "PCI: unable to allocate prefetchable "
211                        "memory region (%d)\n", ret);
212                 goto release_non_mem;
213         }
214
215         /*
216          * bus->resource[0] is the IO resource for this bus
217          * bus->resource[1] is the mem resource for this bus
218          * bus->resource[2] is the prefetch mem resource for this bus
219          */
220         resource[0] = &io_mem;
221         resource[1] = &non_mem;
222         resource[2] = &pre_mem;
223
224         goto out;
225
226  release_non_mem:
227         release_resource(&non_mem);
228  release_io_mem:
229         release_resource(&io_mem);
230  out:
231         return ret;
232 }
233
234 int __init pci_versatile_setup(int nr, struct pci_sys_data *sys)
235 {
236         int ret = 0;
237         int i;
238         int myslot = -1;
239         unsigned long val;
240         void __iomem *local_pci_cfg_base;
241
242         val = __raw_readl(SYS_PCICTL);
243         if (!(val & 1)) {
244                 printk("Not plugged into PCI backplane!\n");
245                 ret = -EIO;
246                 goto out;
247         }
248
249         if (nr == 0) {
250                 sys->mem_offset = 0;
251                 ret = pci_versatile_setup_resources(sys->resource);
252                 if (ret < 0) {
253                         printk("pci_versatile_setup: resources... oops?\n");
254                         goto out;
255                 }
256         } else {
257                 printk("pci_versatile_setup: resources... nr == 0??\n");
258                 goto out;
259         }
260
261         /*
262          *  We need to discover the PCI core first to configure itself
263          *  before the main PCI probing is performed
264          */
265         for (i=0; i<32; i++)
266                 if ((__raw_readl(VERSATILE_PCI_VIRT_BASE+(i<<11)+DEVICE_ID_OFFSET) == VP_PCI_DEVICE_ID) &&
267                     (__raw_readl(VERSATILE_PCI_VIRT_BASE+(i<<11)+CLASS_ID_OFFSET) == VP_PCI_CLASS_ID)) {
268                         myslot = i;
269                         break;
270                 }
271
272         if (myslot == -1) {
273                 printk("Cannot find PCI core!\n");
274                 ret = -EIO;
275                 goto out;
276         }
277
278         printk("PCI core found (slot %d)\n",myslot);
279
280         __raw_writel(myslot, PCI_SELFID);
281         local_pci_cfg_base = VERSATILE_PCI_CFG_VIRT_BASE + (myslot << 11);
282
283         val = __raw_readl(local_pci_cfg_base + CSR_OFFSET);
284         val |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE;
285         __raw_writel(val, local_pci_cfg_base + CSR_OFFSET);
286
287         /*
288          * Configure the PCI inbound memory windows to be 1:1 mapped to SDRAM
289          */
290         __raw_writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_0);
291         __raw_writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_1);
292         __raw_writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_2);
293
294         /*
295          * Do not to map Versatile FPGA PCI device into memory space
296          */
297         pci_slot_ignore |= (1 << myslot);
298         ret = 1;
299
300  out:
301         return ret;
302 }
303
304
305 struct pci_bus *pci_versatile_scan_bus(int nr, struct pci_sys_data *sys)
306 {
307         return pci_scan_bus(sys->busnr, &pci_versatile_ops, sys);
308 }
309
310 void __init pci_versatile_preinit(void)
311 {
312         __raw_writel(VERSATILE_PCI_MEM_BASE0 >> 28, PCI_IMAP0);
313         __raw_writel(VERSATILE_PCI_MEM_BASE1 >> 28, PCI_IMAP1);
314         __raw_writel(VERSATILE_PCI_MEM_BASE2 >> 28, PCI_IMAP2);
315
316         __raw_writel(PHYS_OFFSET >> 28, PCI_SMAP0);
317         __raw_writel(PHYS_OFFSET >> 28, PCI_SMAP1);
318         __raw_writel(PHYS_OFFSET >> 28, PCI_SMAP2);
319
320         __raw_writel(1, SYS_PCICTL);
321 }
322
323 /*
324  * map the specified device/slot/pin to an IRQ.   Different backplanes may need to modify this.
325  */
326 static int __init versatile_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
327 {
328         int irq;
329         int devslot = PCI_SLOT(dev->devfn);
330
331         /* slot,  pin,  irq
332          *  24     1     27
333          *  25     1     28
334          *  26     1     29
335          *  27     1     30
336          */
337         irq = 27 + ((slot + pin - 1) & 3);
338
339         printk("PCI map irq: slot %d, pin %d, devslot %d, irq: %d\n",slot,pin,devslot,irq);
340
341         return irq;
342 }
343
344 static struct hw_pci versatile_pci __initdata = {
345         .swizzle                = NULL,
346         .map_irq                = versatile_map_irq,
347         .nr_controllers         = 1,
348         .setup                  = pci_versatile_setup,
349         .scan                   = pci_versatile_scan_bus,
350         .preinit                = pci_versatile_preinit,
351 };
352
353 static int __init versatile_pci_init(void)
354 {
355         pci_common_init(&versatile_pci);
356         return 0;
357 }
358
359 subsys_initcall(versatile_pci_init);