Pull kmalloc into release branch
[pandora-kernel.git] / arch / sh / drivers / pci / pci-sh7751.c
1 /*
2  *      Low-Level PCI Support for the SH7751
3  *
4  *  Dustin McIntire (dustin@sensoria.com)
5  *      Derived from arch/i386/kernel/pci-*.c which bore the message:
6  *      (c) 1999--2000 Martin Mares <mj@ucw.cz>
7  *
8  *  Ported to the new API by Paul Mundt <lethal@linux-sh.org>
9  *  With cleanup by Paul van Gool <pvangool@mimotech.com>
10  *
11  *  May be copied or modified under the terms of the GNU General Public
12  *  License.  See linux/COPYING for more information.
13  *
14  */
15
16 #undef DEBUG
17
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/pci.h>
22 #include <linux/sched.h>
23 #include <linux/ioport.h>
24 #include <linux/errno.h>
25 #include <linux/irq.h>
26 #include <linux/delay.h>
27
28 #include <asm/machvec.h>
29 #include <asm/io.h>
30 #include "pci-sh7751.h"
31
32 static unsigned int pci_probe = PCI_PROBE_CONF1;
33 extern int pci_fixup_pcic(void);
34
35 void pcibios_fixup_irqs(void) __attribute__ ((weak));
36
37 /*
38  * Direct access to PCI hardware...
39  */
40
41 #define CONFIG_CMD(bus, devfn, where) (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
42
43 /*
44  * Functions for accessing PCI configuration space with type 1 accesses
45  */
46 static int sh7751_pci_read(struct pci_bus *bus, unsigned int devfn,
47                            int where, int size, u32 *val)
48 {
49         unsigned long flags;
50         u32 data;
51
52         /* 
53          * PCIPDR may only be accessed as 32 bit words, 
54          * so we must do byte alignment by hand 
55          */
56         local_irq_save(flags);
57         outl(CONFIG_CMD(bus,devfn,where), PCI_REG(SH7751_PCIPAR));
58         data = inl(PCI_REG(SH7751_PCIPDR));
59         local_irq_restore(flags);
60
61         switch (size) {
62         case 1:
63                 *val = (data >> ((where & 3) << 3)) & 0xff;
64                 break;
65         case 2:
66                 *val = (data >> ((where & 2) << 3)) & 0xffff;
67                 break;
68         case 4:
69                 *val = data;
70                 break;
71         default:
72                 return PCIBIOS_FUNC_NOT_SUPPORTED;
73         }
74
75         return PCIBIOS_SUCCESSFUL;
76 }
77
78 /* 
79  * Since SH7751 only does 32bit access we'll have to do a read,
80  * mask,write operation.
81  * We'll allow an odd byte offset, though it should be illegal.
82  */ 
83 static int sh7751_pci_write(struct pci_bus *bus, unsigned int devfn,
84                             int where, int size, u32 val)
85 {
86         unsigned long flags;
87         int shift;
88         u32 data;
89
90         local_irq_save(flags);
91         outl(CONFIG_CMD(bus,devfn,where), PCI_REG(SH7751_PCIPAR));
92         data = inl(PCI_REG(SH7751_PCIPDR));
93         local_irq_restore(flags);
94
95         switch (size) {
96         case 1:
97                 shift = (where & 3) << 3;
98                 data &= ~(0xff << shift);
99                 data |= ((val & 0xff) << shift);
100                 break;
101         case 2:
102                 shift = (where & 2) << 3;
103                 data &= ~(0xffff << shift);
104                 data |= ((val & 0xffff) << shift);
105                 break;
106         case 4:
107                 data = val;
108                 break;
109         default:
110                 return PCIBIOS_FUNC_NOT_SUPPORTED;
111         }
112
113         outl(data, PCI_REG(SH7751_PCIPDR));
114
115         return PCIBIOS_SUCCESSFUL;
116 }
117
118 #undef CONFIG_CMD
119
120 struct pci_ops sh7751_pci_ops = {
121         .read           = sh7751_pci_read,
122         .write          = sh7751_pci_write,
123 };
124
125 static int __init pci_check_direct(void)
126 {
127         unsigned int tmp, id;
128
129         /* check for SH7751/SH7751R hardware */
130         id = inl(SH7751_PCIREG_BASE+SH7751_PCICONF0);
131         if (id != ((SH7751_DEVICE_ID << 16) | SH7751_VENDOR_ID) &&
132             id != ((SH7751R_DEVICE_ID << 16) | SH7751_VENDOR_ID)) {
133                 pr_debug("PCI: This is not an SH7751(R) (%x)\n", id);
134                 return -ENODEV;
135         }
136
137         /*
138          * Check if configuration works.
139          */
140         if (pci_probe & PCI_PROBE_CONF1) {
141                 tmp = inl (PCI_REG(SH7751_PCIPAR));
142                 outl (0x80000000, PCI_REG(SH7751_PCIPAR));
143                 if (inl (PCI_REG(SH7751_PCIPAR)) == 0x80000000) {
144                         outl (tmp, PCI_REG(SH7751_PCIPAR));
145                         printk(KERN_INFO "PCI: Using configuration type 1\n");
146                         request_region(PCI_REG(SH7751_PCIPAR), 8, "PCI conf1");
147                         return 0;
148                 }
149                 outl (tmp, PCI_REG(SH7751_PCIPAR));
150         }
151
152         pr_debug("PCI: pci_check_direct failed\n");
153         return -EINVAL;
154 }
155
156 /***************************************************************************************/
157
158 /*
159  *  Handle bus scanning and fixups ....
160  */
161
162 static void __init pci_fixup_ide_bases(struct pci_dev *d)
163 {
164         int i;
165
166         /*
167          * PCI IDE controllers use non-standard I/O port decoding, respect it.
168          */
169         if ((d->class >> 8) != PCI_CLASS_STORAGE_IDE)
170                 return;
171         pr_debug("PCI: IDE base address fixup for %s\n", pci_name(d));
172         for(i=0; i<4; i++) {
173                 struct resource *r = &d->resource[i];
174                 if ((r->start & ~0x80) == 0x374) {
175                         r->start |= 2;
176                         r->end = r->start;
177                 }
178         }
179 }
180
181 DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pci_fixup_ide_bases);
182
183 /*
184  *  Called after each bus is probed, but before its children
185  *  are examined.
186  */
187
188 void __init pcibios_fixup_bus(struct pci_bus *b)
189 {
190         pci_read_bridge_bases(b);
191 }
192
193 /*
194  * Initialization. Try all known PCI access methods. Note that we support
195  * using both PCI BIOS and direct access: in such cases, we use I/O ports
196  * to access config space.
197  * 
198  * Note that the platform specific initialization (BSC registers, and memory
199  * space mapping) will be called via the machine vectors (sh_mv.mv_pci_init()) if it
200  * exitst and via the platform defined function pcibios_init_platform().  
201  * See pci_bigsur.c for implementation;
202  * 
203  * The BIOS version of the pci functions is not yet implemented but it is left
204  * in for completeness.  Currently an error will be genereated at compile time. 
205  */
206
207 static int __init sh7751_pci_init(void)
208 {
209         int ret;
210
211         pr_debug("PCI: Starting intialization.\n");
212         if ((ret = pci_check_direct()) != 0)
213                 return ret;
214
215         return pcibios_init_platform();
216 }
217
218 subsys_initcall(sh7751_pci_init);
219
220 static int __init __area_sdram_check(unsigned int area)
221 {
222         u32 word;
223
224         word = inl(SH7751_BCR1);
225         /* check BCR for SDRAM in area */
226         if(((word >> area) & 1) == 0) {
227                 printk("PCI: Area %d is not configured for SDRAM. BCR1=0x%x\n",
228                        area, word);
229                 return 0;
230         }
231         outl(word, PCI_REG(SH7751_PCIBCR1));
232
233         word = (u16)inw(SH7751_BCR2);
234         /* check BCR2 for 32bit SDRAM interface*/
235         if(((word >> (area << 1)) & 0x3) != 0x3) {
236                 printk("PCI: Area %d is not 32 bit SDRAM. BCR2=0x%x\n",
237                        area, word);
238                 return 0;
239         }
240         outl(word, PCI_REG(SH7751_PCIBCR2));
241
242         return 1;
243 }
244
245 int __init sh7751_pcic_init(struct sh7751_pci_address_map *map)
246 {
247         u32 reg;
248         u32 word;
249
250         /* Set the BCR's to enable PCI access */
251         reg = inl(SH7751_BCR1);
252         reg |= 0x80000;
253         outl(reg, SH7751_BCR1);
254         
255         /* Turn the clocks back on (not done in reset)*/
256         outl(0, PCI_REG(SH7751_PCICLKR));
257         /* Clear Powerdown IRQ's (not done in reset) */
258         word = SH7751_PCIPINT_D3 | SH7751_PCIPINT_D0;
259         outl(word, PCI_REG(SH7751_PCIPINT));
260
261         /*
262          * This code is unused for some boards as it is done in the
263          * bootloader and doing it here means the MAC addresses loaded
264          * by the bootloader get lost.
265          */
266         if (!(map->flags & SH7751_PCIC_NO_RESET)) {
267                 /* toggle PCI reset pin */
268                 word = SH7751_PCICR_PREFIX | SH7751_PCICR_PRST;
269                 outl(word,PCI_REG(SH7751_PCICR));
270                 /* Wait for a long time... not 1 sec. but long enough */
271                 mdelay(100);
272                 word = SH7751_PCICR_PREFIX;
273                 outl(word,PCI_REG(SH7751_PCICR));
274         }
275         
276         /* set the command/status bits to:
277          * Wait Cycle Control + Parity Enable + Bus Master +
278          * Mem space enable
279          */
280         word = SH7751_PCICONF1_WCC | SH7751_PCICONF1_PER | 
281                SH7751_PCICONF1_BUM | SH7751_PCICONF1_MES;
282         outl(word, PCI_REG(SH7751_PCICONF1));
283
284         /* define this host as the host bridge */
285         word = SH7751_PCI_HOST_BRIDGE << 24;
286         outl(word, PCI_REG(SH7751_PCICONF2));
287
288         /* Set IO and Mem windows to local address 
289          * Make PCI and local address the same for easy 1 to 1 mapping 
290          * Window0 = map->window0.size @ non-cached area base = SDRAM
291          * Window1 = map->window1.size @ cached area base = SDRAM 
292          */
293         word = map->window0.size - 1;
294         outl(word, PCI_REG(SH7751_PCILSR0));
295         word = map->window1.size - 1;
296         outl(word, PCI_REG(SH7751_PCILSR1));
297         /* Set the values on window 0 PCI config registers */
298         word = P2SEGADDR(map->window0.base);
299         outl(word, PCI_REG(SH7751_PCILAR0));
300         outl(word, PCI_REG(SH7751_PCICONF5));
301         /* Set the values on window 1 PCI config registers */
302         word =  PHYSADDR(map->window1.base);
303         outl(word, PCI_REG(SH7751_PCILAR1));
304         outl(word, PCI_REG(SH7751_PCICONF6));
305
306         /* Set the local 16MB PCI memory space window to 
307          * the lowest PCI mapped address
308          */
309         word = PCIBIOS_MIN_MEM & SH7751_PCIMBR_MASK;
310         PCIDBG(2,"PCI: Setting upper bits of Memory window to 0x%x\n", word);
311         outl(word , PCI_REG(SH7751_PCIMBR));
312
313         /* Map IO space into PCI IO window
314          * The IO window is 64K-PCIBIOS_MIN_IO in size
315          * IO addresses will be translated to the 
316          * PCI IO window base address
317          */
318         PCIDBG(3,"PCI: Mapping IO address 0x%x - 0x%x to base 0x%x\n", PCIBIOS_MIN_IO,
319             (64*1024), SH7751_PCI_IO_BASE+PCIBIOS_MIN_IO);
320
321         /* 
322          * XXX: For now, leave this board-specific. In the event we have other
323          * boards that need to do similar work, this can be wrapped.
324          */
325 #ifdef CONFIG_SH_BIGSUR
326         bigsur_port_map(PCIBIOS_MIN_IO, (64*1024), SH7751_PCI_IO_BASE+PCIBIOS_MIN_IO,0);
327 #endif
328
329         /* Make sure the MSB's of IO window are set to access PCI space correctly */
330         word = PCIBIOS_MIN_IO & SH7751_PCIIOBR_MASK;
331         PCIDBG(2,"PCI: Setting upper bits of IO window to 0x%x\n", word);
332         outl(word, PCI_REG(SH7751_PCIIOBR));
333         
334         /* Set PCI WCRx, BCRx's, copy from BSC locations */
335
336         /* check BCR for SDRAM in specified area */
337         switch (map->window0.base) {
338         case SH7751_CS0_BASE_ADDR: word = __area_sdram_check(0); break;
339         case SH7751_CS1_BASE_ADDR: word = __area_sdram_check(1); break;
340         case SH7751_CS2_BASE_ADDR: word = __area_sdram_check(2); break;
341         case SH7751_CS3_BASE_ADDR: word = __area_sdram_check(3); break;
342         case SH7751_CS4_BASE_ADDR: word = __area_sdram_check(4); break;
343         case SH7751_CS5_BASE_ADDR: word = __area_sdram_check(5); break;
344         case SH7751_CS6_BASE_ADDR: word = __area_sdram_check(6); break;
345         }
346         
347         if (!word)
348                 return 0;
349
350         /* configure the wait control registers */
351         word = inl(SH7751_WCR1);
352         outl(word, PCI_REG(SH7751_PCIWCR1));
353         word = inl(SH7751_WCR2);
354         outl(word, PCI_REG(SH7751_PCIWCR2));
355         word = inl(SH7751_WCR3);
356         outl(word, PCI_REG(SH7751_PCIWCR3));
357         word = inl(SH7751_MCR);
358         outl(word, PCI_REG(SH7751_PCIMCR));
359
360         /* NOTE: I'm ignoring the PCI error IRQs for now..
361          * TODO: add support for the internal error interrupts and
362          * DMA interrupts...
363          */
364
365 #ifdef CONFIG_SH_RTS7751R2D
366         pci_fixup_pcic();
367 #endif
368
369         /* SH7751 init done, set central function init complete */
370         /* use round robin mode to stop a device starving/overruning */
371         word = SH7751_PCICR_PREFIX | SH7751_PCICR_CFIN | SH7751_PCICR_ARBM;
372         outl(word,PCI_REG(SH7751_PCICR)); 
373
374         return 1;
375 }
376
377 char * __init pcibios_setup(char *str)
378 {
379         if (!strcmp(str, "off")) {
380                 pci_probe = 0;
381                 return NULL;
382         }
383
384         return str;
385 }
386
387 /* 
388  *      IRQ functions 
389  */
390 static u8 __init sh7751_no_swizzle(struct pci_dev *dev, u8 *pin)
391 {
392         /* no swizzling */
393         return PCI_SLOT(dev->devfn);
394 }
395
396 static int sh7751_pci_lookup_irq(struct pci_dev *dev, u8 slot, u8 pin)
397 {
398         int irq = -1;
399
400         /* now lookup the actual IRQ on a platform specific basis (pci-'platform'.c) */
401         irq = pcibios_map_platform_irq(slot,pin);
402         if( irq < 0 ) {
403                 pr_debug("PCI: Error mapping IRQ on device %s\n", pci_name(dev));
404                 return irq;
405         }
406
407         pr_debug("Setting IRQ for slot %s to %d\n", pci_name(dev), irq);
408
409         return irq;
410 }
411
412 void __init pcibios_fixup_irqs(void)
413 {
414         pci_fixup_irqs(sh7751_no_swizzle, sh7751_pci_lookup_irq);
415 }
416