commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / arch / arm / kernel / bios32.c
1 /*
2  *  linux/arch/arm/kernel/bios32.c
3  *
4  *  PCI bios-type initialisation for PCI machines
5  *
6  *  Bits taken from various places.
7  */
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/pci.h>
12 #include <linux/slab.h>
13 #include <linux/init.h>
14
15 #include <asm/bug.h>
16 #include <asm/io.h>
17 #include <asm/irq.h>
18 #include <asm/mach-types.h>
19 #include <asm/mach/pci.h>
20
21 static int debug_pci;
22
23 void pcibios_report_status(u_int status_mask, int warn)
24 {
25         struct pci_dev *dev = NULL;
26
27         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
28                 u16 status;
29
30                 /*
31                  * ignore host bridge - we handle
32                  * that separately
33                  */
34                 if (dev->bus->number == 0 && dev->devfn == 0)
35                         continue;
36
37                 pci_read_config_word(dev, PCI_STATUS, &status);
38
39                 status &= status_mask;
40                 if (status == 0)
41                         continue;
42
43                 /* clear the status errors */
44                 pci_write_config_word(dev, PCI_STATUS, status);
45
46                 if (warn)
47                         printk("(%02x:%02x.%d: %04X) ", dev->bus->number,
48                                 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
49                                 status);
50         }
51 }
52
53 /*
54  * We don't use this to fix the device, but initialisation of it.
55  * It's not the correct use for this, but it works.
56  * Note that the arbiter/ISA bridge appears to be buggy, specifically in
57  * the following area:
58  * 1. park on CPU
59  * 2. ISA bridge ping-pong
60  * 3. ISA bridge master handling of target RETRY
61  *
62  * Bug 3 is responsible for the sound DMA grinding to a halt.  We now
63  * live with bug 2.
64  */
65 static void __devinit pci_fixup_83c553(struct pci_dev *dev)
66 {
67         /*
68          * Set memory region to start at address 0, and enable IO
69          */
70         pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, PCI_BASE_ADDRESS_SPACE_MEMORY);
71         pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_IO);
72
73         dev->resource[0].end -= dev->resource[0].start;
74         dev->resource[0].start = 0;
75
76         /*
77          * All memory requests from ISA to be channelled to PCI
78          */
79         pci_write_config_byte(dev, 0x48, 0xff);
80
81         /*
82          * Enable ping-pong on bus master to ISA bridge transactions.
83          * This improves the sound DMA substantially.  The fixed
84          * priority arbiter also helps (see below).
85          */
86         pci_write_config_byte(dev, 0x42, 0x01);
87
88         /*
89          * Enable PCI retry
90          */
91         pci_write_config_byte(dev, 0x40, 0x22);
92
93         /*
94          * We used to set the arbiter to "park on last master" (bit
95          * 1 set), but unfortunately the CyberPro does not park the
96          * bus.  We must therefore park on CPU.  Unfortunately, this
97          * may trigger yet another bug in the 553.
98          */
99         pci_write_config_byte(dev, 0x83, 0x02);
100
101         /*
102          * Make the ISA DMA request lowest priority, and disable
103          * rotating priorities completely.
104          */
105         pci_write_config_byte(dev, 0x80, 0x11);
106         pci_write_config_byte(dev, 0x81, 0x00);
107
108         /*
109          * Route INTA input to IRQ 11, and set IRQ11 to be level
110          * sensitive.
111          */
112         pci_write_config_word(dev, 0x44, 0xb000);
113         outb(0x08, 0x4d1);
114 }
115
116 static void __devinit pci_fixup_unassign(struct pci_dev *dev)
117 {
118         dev->resource[0].end -= dev->resource[0].start;
119         dev->resource[0].start = 0;
120 }
121
122 /*
123  * Prevent the PCI layer from seeing the resources allocated to this device
124  * if it is the host bridge by marking it as such.  These resources are of
125  * no consequence to the PCI layer (they are handled elsewhere).
126  */
127 static void __devinit pci_fixup_dec21285(struct pci_dev *dev)
128 {
129         int i;
130
131         if (dev->devfn == 0) {
132                 dev->class &= 0xff;
133                 dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
134                 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
135                         dev->resource[i].start = 0;
136                         dev->resource[i].end   = 0;
137                         dev->resource[i].flags = 0;
138                 }
139         }
140 }
141
142 /*
143  * PCI IDE controllers use non-standard I/O port decoding, respect it.
144  */
145 static void __devinit pci_fixup_ide_bases(struct pci_dev *dev)
146 {
147         struct resource *r;
148         int i;
149
150         if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
151                 return;
152
153         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
154                 r = dev->resource + i;
155                 if ((r->start & ~0x80) == 0x374) {
156                         r->start |= 2;
157                         r->end = r->start;
158                 }
159         }
160 }
161
162 /*
163  * Put the DEC21142 to sleep
164  */
165 static void __devinit pci_fixup_dec21142(struct pci_dev *dev)
166 {
167         pci_write_config_dword(dev, 0x40, 0x80000000);
168 }
169
170 /*
171  * The CY82C693 needs some rather major fixups to ensure that it does
172  * the right thing.  Idea from the Alpha people, with a few additions.
173  *
174  * We ensure that the IDE base registers are set to 1f0/3f4 for the
175  * primary bus, and 170/374 for the secondary bus.  Also, hide them
176  * from the PCI subsystem view as well so we won't try to perform
177  * our own auto-configuration on them.
178  *
179  * In addition, we ensure that the PCI IDE interrupts are routed to
180  * IRQ 14 and IRQ 15 respectively.
181  *
182  * The above gets us to a point where the IDE on this device is
183  * functional.  However, The CY82C693U _does not work_ in bus
184  * master mode without locking the PCI bus solid.
185  */
186 static void __devinit pci_fixup_cy82c693(struct pci_dev *dev)
187 {
188         if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
189                 u32 base0, base1;
190
191                 if (dev->class & 0x80) {        /* primary */
192                         base0 = 0x1f0;
193                         base1 = 0x3f4;
194                 } else {                        /* secondary */
195                         base0 = 0x170;
196                         base1 = 0x374;
197                 }
198
199                 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0,
200                                        base0 | PCI_BASE_ADDRESS_SPACE_IO);
201                 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1,
202                                        base1 | PCI_BASE_ADDRESS_SPACE_IO);
203
204                 dev->resource[0].start = 0;
205                 dev->resource[0].end   = 0;
206                 dev->resource[0].flags = 0;
207
208                 dev->resource[1].start = 0;
209                 dev->resource[1].end   = 0;
210                 dev->resource[1].flags = 0;
211         } else if (PCI_FUNC(dev->devfn) == 0) {
212                 /*
213                  * Setup IDE IRQ routing.
214                  */
215                 pci_write_config_byte(dev, 0x4b, 14);
216                 pci_write_config_byte(dev, 0x4c, 15);
217
218                 /*
219                  * Disable FREQACK handshake, enable USB.
220                  */
221                 pci_write_config_byte(dev, 0x4d, 0x41);
222
223                 /*
224                  * Enable PCI retry, and PCI post-write buffer.
225                  */
226                 pci_write_config_byte(dev, 0x44, 0x17);
227
228                 /*
229                  * Enable ISA master and DMA post write buffering.
230                  */
231                 pci_write_config_byte(dev, 0x45, 0x03);
232         }
233 }
234
235 struct pci_fixup pcibios_fixups[] = {
236         {
237                 PCI_FIXUP_HEADER,
238                 PCI_VENDOR_ID_CONTAQ,   PCI_DEVICE_ID_CONTAQ_82C693,
239                 pci_fixup_cy82c693
240         }, {
241                 PCI_FIXUP_HEADER,
242                 PCI_VENDOR_ID_DEC,      PCI_DEVICE_ID_DEC_21142,
243                 pci_fixup_dec21142
244         }, {
245                 PCI_FIXUP_HEADER,
246                 PCI_VENDOR_ID_DEC,      PCI_DEVICE_ID_DEC_21285,
247                 pci_fixup_dec21285
248         }, {
249                 PCI_FIXUP_HEADER,
250                 PCI_VENDOR_ID_WINBOND,  PCI_DEVICE_ID_WINBOND_83C553,
251                 pci_fixup_83c553
252         }, {
253                 PCI_FIXUP_HEADER,
254                 PCI_VENDOR_ID_WINBOND2, PCI_DEVICE_ID_WINBOND2_89C940F,
255                 pci_fixup_unassign
256         }, {
257                 PCI_FIXUP_HEADER,
258                 PCI_ANY_ID,             PCI_ANY_ID,
259                 pci_fixup_ide_bases
260         }, { 0 }
261 };
262
263 void __devinit pcibios_update_irq(struct pci_dev *dev, int irq)
264 {
265         if (debug_pci)
266                 printk("PCI: Assigning IRQ %02d to %s\n", irq, dev->dev.name);
267         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
268 }
269
270 /*
271  * If the bus contains any of these devices, then we must not turn on
272  * parity checking of any kind.  Currently this is CyberPro 20x0 only.
273  */
274 static inline int pdev_bad_for_parity(struct pci_dev *dev)
275 {
276         return (dev->vendor == PCI_VENDOR_ID_INTERG &&
277                 (dev->device == PCI_DEVICE_ID_INTERG_2000 ||
278                  dev->device == PCI_DEVICE_ID_INTERG_2010));
279 }
280
281 /*
282  * Adjust the device resources from bus-centric to Linux-centric.
283  */
284 static void __devinit
285 pdev_fixup_device_resources(struct pci_sys_data *root, struct pci_dev *dev)
286 {
287         unsigned long offset;
288         int i;
289
290         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
291                 if (dev->resource[i].start == 0)
292                         continue;
293                 if (dev->resource[i].flags & IORESOURCE_MEM)
294                         offset = root->mem_offset;
295                 else
296                         offset = root->io_offset;
297
298                 dev->resource[i].start += offset;
299                 dev->resource[i].end   += offset;
300         }
301 }
302
303 static void __devinit
304 pbus_assign_bus_resources(struct pci_bus *bus, struct pci_sys_data *root)
305 {
306         struct pci_dev *dev = bus->self;
307         int i;
308
309         if (!dev) {
310                 /*
311                  * Assign root bus resources.
312                  */
313                 for (i = 0; i < 3; i++)
314                         bus->resource[i] = root->resource[i];
315         }
316 }
317
318 /*
319  * pcibios_fixup_bus - Called after each bus is probed,
320  * but before its children are examined.
321  */
322 void __devinit pcibios_fixup_bus(struct pci_bus *bus)
323 {
324         struct pci_sys_data *root = bus->sysdata;
325         struct pci_dev *dev;
326         u16 features = PCI_COMMAND_SERR | PCI_COMMAND_PARITY | PCI_COMMAND_FAST_BACK;
327
328         pbus_assign_bus_resources(bus, root);
329
330         /*
331          * Walk the devices on this bus, working out what we can
332          * and can't support.
333          */
334         list_for_each_entry(dev, &bus->devices, bus_list) {
335                 u16 status;
336
337                 pdev_fixup_device_resources(root, dev);
338
339                 pci_read_config_word(dev, PCI_STATUS, &status);
340
341                 /*
342                  * If any device on this bus does not support fast back
343                  * to back transfers, then the bus as a whole is not able
344                  * to support them.  Having fast back to back transfers
345                  * on saves us one PCI cycle per transaction.
346                  */
347                 if (!(status & PCI_STATUS_FAST_BACK))
348                         features &= ~PCI_COMMAND_FAST_BACK;
349
350                 if (pdev_bad_for_parity(dev))
351                         features &= ~(PCI_COMMAND_SERR | PCI_COMMAND_PARITY);
352
353                 switch (dev->class >> 8) {
354 #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
355                 case PCI_CLASS_BRIDGE_ISA:
356                 case PCI_CLASS_BRIDGE_EISA:
357                         /*
358                          * If this device is an ISA bridge, set isa_bridge
359                          * to point at this device.  We will then go looking
360                          * for things like keyboard, etc.
361                          */
362                         isa_bridge = dev;
363                         break;
364 #endif
365         }
366
367         /*
368          * Now walk the devices again, this time setting them up.
369          */
370         list_for_each_entry(dev, &bus->devices, bus_list) {
371                 u16 cmd;
372
373                 pci_read_config_word(dev, PCI_COMMAND, &cmd);
374                 cmd |= features;
375                 pci_write_config_word(dev, PCI_COMMAND, cmd);
376
377                 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
378                                       L1_CACHE_BYTES >> 2);
379         }
380
381         /*
382          * Propagate the flags to the PCI bridge.
383          */
384         if (bus->self && bus->self->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
385                 if (features & PCI_COMMAND_FAST_BACK)
386                         bus->bridge_ctl |= PCI_BRIDGE_CTL_FAST_BACK;
387                 if (features & PCI_COMMAND_PARITY)
388                         bus->bridge_ctl |= PCI_BRIDGE_CTL_PARITY;
389         }
390
391         /*
392          * Report what we did for this bus
393          */
394         printk(KERN_INFO "PCI: bus%d: Fast back to back transfers %sabled\n",
395                 bus->number, (features & PCI_COMMAND_FAST_BACK) ? "en" : "dis");
396 }
397
398 /*
399  * Convert from Linux-centric to bus-centric addresses for bridge devices.
400  */
401 void __devinit
402 pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
403                          struct resource *res)
404 {
405         struct pci_sys_data *root = dev->sysdata;
406         unsigned long offset = 0;
407
408         if (res->flags & IORESOURCE_IO)
409                 offset = root->io_offset;
410         if (res->flags & IORESOURCE_MEM)
411                 offset = root->mem_offset;
412
413         region->start = res->start - offset;
414         region->end   = res->end - offset;
415 }
416
417 #ifdef CONFIG_HOTPLUG
418 EXPORT_SYMBOL(pcibios_fixup_bus);
419 EXPORT_SYMBOL(pcibios_resource_to_bus);
420 #endif
421
422 /*
423  * This is the standard PCI-PCI bridge swizzling algorithm:
424  *
425  *   Dev: 0  1  2  3
426  *    A   A  B  C  D
427  *    B   B  C  D  A
428  *    C   C  D  A  B
429  *    D   D  A  B  C
430  *        ^^^^^^^^^^ irq pin on bridge
431  */
432 u8 __devinit pci_std_swizzle(struct pci_dev *dev, u8 *pinp)
433 {
434         int pin = *pinp - 1;
435
436         while (dev->bus->self) {
437                 pin = (pin + PCI_SLOT(dev->devfn)) & 3;
438                 /*
439                  * move up the chain of bridges,
440                  * swizzling as we go.
441                  */
442                 dev = dev->bus->self;
443         }
444         *pinp = pin + 1;
445
446         return PCI_SLOT(dev->devfn);
447 }
448
449 /*
450  * Swizzle the device pin each time we cross a bridge.
451  * This might update pin and returns the slot number.
452  */
453 static u8 __devinit pcibios_swizzle(struct pci_dev *dev, u8 *pin)
454 {
455         struct pci_sys_data *sys = dev->sysdata;
456         int slot = 0, oldpin = *pin;
457
458         if (sys->swizzle)
459                 slot = sys->swizzle(dev, pin);
460
461         if (debug_pci)
462                 printk("PCI: %s swizzling pin %d => pin %d slot %d\n",
463                         pci_name(dev), oldpin, *pin, slot);
464
465         return slot;
466 }
467
468 /*
469  * Map a slot/pin to an IRQ.
470  */
471 static int pcibios_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
472 {
473         struct pci_sys_data *sys = dev->sysdata;
474         int irq = -1;
475
476         if (sys->map_irq)
477                 irq = sys->map_irq(dev, slot, pin);
478
479         if (debug_pci)
480                 printk("PCI: %s mapping slot %d pin %d => irq %d\n",
481                         pci_name(dev), slot, pin, irq);
482
483         return irq;
484 }
485
486 static void __init pcibios_init_hw(struct hw_pci *hw)
487 {
488         struct pci_sys_data *sys = NULL;
489         int ret;
490         int nr, busnr;
491
492         for (nr = busnr = 0; nr < hw->nr_controllers; nr++) {
493                 sys = kmalloc(sizeof(struct pci_sys_data), GFP_KERNEL);
494                 if (!sys)
495                         panic("PCI: unable to allocate sys data!");
496
497                 memset(sys, 0, sizeof(struct pci_sys_data));
498
499                 sys->hw      = hw;
500                 sys->busnr   = busnr;
501                 sys->swizzle = hw->swizzle;
502                 sys->map_irq = hw->map_irq;
503                 sys->resource[0] = &ioport_resource;
504                 sys->resource[1] = &iomem_resource;
505
506                 ret = hw->setup(nr, sys);
507
508                 if (ret > 0) {
509                         sys->bus = hw->scan(nr, sys);
510
511                         if (!sys->bus)
512                                 panic("PCI: unable to scan bus!");
513
514                         busnr = sys->bus->subordinate + 1;
515
516                         list_add(&sys->node, &hw->buses);
517                 } else {
518                         kfree(sys);
519                         if (ret < 0)
520                                 break;
521                 }
522         }
523 }
524
525 void __init pci_common_init(struct hw_pci *hw)
526 {
527         struct pci_sys_data *sys;
528
529         INIT_LIST_HEAD(&hw->buses);
530
531         if (hw->preinit)
532                 hw->preinit();
533         pcibios_init_hw(hw);
534         if (hw->postinit)
535                 hw->postinit();
536
537         pci_fixup_irqs(pcibios_swizzle, pcibios_map_irq);
538
539         list_for_each_entry(sys, &hw->buses, node) {
540                 struct pci_bus *bus = sys->bus;
541
542                 /*
543                  * Size the bridge windows.
544                  */
545                 pci_bus_size_bridges(bus);
546
547                 /*
548                  * Assign resources.
549                  */
550                 pci_bus_assign_resources(bus);
551
552                 /*
553                  * Tell drivers about devices found.
554                  */
555                 pci_bus_add_devices(bus);
556         }
557 }
558
559 char * __init pcibios_setup(char *str)
560 {
561         if (!strcmp(str, "debug")) {
562                 debug_pci = 1;
563                 return NULL;
564         }
565         return str;
566 }
567
568 /*
569  * From arch/i386/kernel/pci-i386.c:
570  *
571  * We need to avoid collisions with `mirrored' VGA ports
572  * and other strange ISA hardware, so we always want the
573  * addresses to be allocated in the 0x000-0x0ff region
574  * modulo 0x400.
575  *
576  * Why? Because some silly external IO cards only decode
577  * the low 10 bits of the IO address. The 0x00-0xff region
578  * is reserved for motherboard devices that decode all 16
579  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
580  * but we want to try to avoid allocating at 0x2900-0x2bff
581  * which might be mirrored at 0x0100-0x03ff..
582  */
583 void pcibios_align_resource(void *data, struct resource *res,
584                             unsigned long size, unsigned long align)
585 {
586         unsigned long start = res->start;
587
588         if (res->flags & IORESOURCE_IO && start & 0x300)
589                 start = (start + 0x3ff) & ~0x3ff;
590
591         res->start = (start + align - 1) & ~(align - 1);
592 }
593
594 /**
595  * pcibios_enable_device - Enable I/O and memory.
596  * @dev: PCI device to be enabled
597  */
598 int pcibios_enable_device(struct pci_dev *dev, int mask)
599 {
600         u16 cmd, old_cmd;
601         int idx;
602         struct resource *r;
603
604         pci_read_config_word(dev, PCI_COMMAND, &cmd);
605         old_cmd = cmd;
606         for (idx = 0; idx < 6; idx++) {
607                 /* Only set up the requested stuff */
608                 if (!(mask & (1 << idx)))
609                         continue;
610
611                 r = dev->resource + idx;
612                 if (!r->start && r->end) {
613                         printk(KERN_ERR "PCI: Device %s not available because"
614                                " of resource collisions\n", pci_name(dev));
615                         return -EINVAL;
616                 }
617                 if (r->flags & IORESOURCE_IO)
618                         cmd |= PCI_COMMAND_IO;
619                 if (r->flags & IORESOURCE_MEM)
620                         cmd |= PCI_COMMAND_MEMORY;
621         }
622
623         /*
624          * Bridges (eg, cardbus bridges) need to be fully enabled
625          */
626         if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE)
627                 cmd |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
628
629         if (cmd != old_cmd) {
630                 printk("PCI: enabling device %s (%04x -> %04x)\n",
631                        pci_name(dev), old_cmd, cmd);
632                 pci_write_config_word(dev, PCI_COMMAND, cmd);
633         }
634         return 0;
635 }
636
637 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
638                         enum pci_mmap_state mmap_state, int write_combine)
639 {
640         struct pci_sys_data *root = dev->sysdata;
641         unsigned long phys;
642
643         if (mmap_state == pci_mmap_io) {
644                 return -EINVAL;
645         } else {
646                 phys = root->mem_offset + (vma->vm_pgoff << PAGE_SHIFT);
647         }
648
649         /*
650          * Mark this as IO
651          */
652         vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
653         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
654
655         if (remap_page_range(vma, vma->vm_start, phys,
656                              vma->vm_end - vma->vm_start,
657                              vma->vm_page_prot))
658                 return -EAGAIN;
659
660         return 0;
661 }