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