ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / kernel / pci.c
1 /*
2  * Port for PPC64 David Engebretsen, IBM Corp.
3  * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
4  * 
5  * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
6  *   Rework, based on alpha PCI code.
7  *
8  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  */
13
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/delay.h>
18 #include <linux/string.h>
19 #include <linux/init.h>
20 #include <linux/capability.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/bootmem.h>
24 #include <linux/module.h>
25 #include <linux/mm.h>
26
27 #include <asm/processor.h>
28 #include <asm/io.h>
29 #include <asm/prom.h>
30 #include <asm/pci-bridge.h>
31 #include <asm/byteorder.h>
32 #include <asm/irq.h>
33 #include <asm/uaccess.h>
34 #include <asm/ppcdebug.h>
35 #include <asm/naca.h>
36 #include <asm/iommu.h>
37 #include <asm/machdep.h>
38
39 #include "pci.h"
40
41 unsigned long pci_probe_only = 1;
42 unsigned long pci_assign_all_buses = 0;
43
44 unsigned int pcibios_assign_all_busses(void)
45 {
46         return pci_assign_all_buses;
47 }
48
49 /* pci_io_base -- the base address from which io bars are offsets.
50  * This is the lowest I/O base address (so bar values are always positive),
51  * and it *must* be the start of ISA space if an ISA bus exists because
52  * ISA drivers use hard coded offsets.  If no ISA bus exists a dummy
53  * page is mapped and isa_io_limit prevents access to it.
54  */
55 unsigned long isa_io_base;      /* NULL if no ISA bus */
56 unsigned long pci_io_base;
57
58 void pcibios_name_device(struct pci_dev* dev);
59 void pcibios_final_fixup(void);
60 static void fixup_broken_pcnet32(struct pci_dev* dev);
61 static void fixup_windbond_82c105(struct pci_dev* dev);
62 extern void fixup_k2_sata(struct pci_dev* dev);
63
64 void iSeries_pcibios_init(void);
65
66 struct pci_controller *hose_head;
67 struct pci_controller **hose_tail = &hose_head;
68
69 struct pci_dma_ops pci_dma_ops;
70 EXPORT_SYMBOL(pci_dma_ops);
71
72 int global_phb_number;          /* Global phb counter */
73
74 /* Cached ISA bridge dev. */
75 struct pci_dev *ppc64_isabridge_dev = NULL;
76
77 struct pci_fixup pcibios_fixups[] = {
78         { PCI_FIXUP_HEADER,     PCI_VENDOR_ID_TRIDENT,          PCI_ANY_ID,
79           fixup_broken_pcnet32 },
80         { PCI_FIXUP_HEADER,     PCI_VENDOR_ID_WINBOND,          PCI_DEVICE_ID_WINBOND_82C105,
81           fixup_windbond_82c105 },
82         { PCI_FIXUP_HEADER,     PCI_ANY_ID,                     PCI_ANY_ID,
83           pcibios_name_device },
84 #ifdef CONFIG_PPC_PMAC
85         { PCI_FIXUP_HEADER,     PCI_VENDOR_ID_SERVERWORKS,      0x0240,
86           fixup_k2_sata },
87 #endif
88         { 0 }
89 };
90
91 static void fixup_broken_pcnet32(struct pci_dev* dev)
92 {
93         if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
94                 dev->vendor = PCI_VENDOR_ID_AMD;
95                 pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
96                 pci_name_device(dev);
97         }
98 }
99
100 static void fixup_windbond_82c105(struct pci_dev* dev)
101 {
102         /* Assume the windbond 82c105 is the IDE controller on a
103          * p610.  We should probably be more careful in case
104          * someone tries to plug in a similar adapter.
105          */
106         int i;
107         unsigned int reg;
108
109         printk("Using INTC for W82c105 IDE controller.\n");
110         pci_read_config_dword(dev, 0x40, &reg);
111         /* Enable LEGIRQ to use INTC instead of ISA interrupts */
112         pci_write_config_dword(dev, 0x40, reg | (1<<11));
113
114         for (i = 0; i < DEVICE_COUNT_RESOURCE; ++i) {
115                 /* zap the 2nd function of the winbond chip */
116                 if (dev->resource[i].flags & IORESOURCE_IO
117                     && dev->bus->number == 0 && dev->devfn == 0x81)
118                         dev->resource[i].flags &= ~IORESOURCE_IO;
119         }
120 }
121
122 void 
123 pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
124                         struct resource *res)
125 {
126         unsigned long offset = 0;
127         struct pci_controller *hose = PCI_GET_PHB_PTR(dev);
128
129         if (!hose)
130                 return;
131
132         if (res->flags & IORESOURCE_IO)
133                 offset = (unsigned long)hose->io_base_virt - pci_io_base;
134
135         if (res->flags & IORESOURCE_MEM)
136                 offset = hose->pci_mem_offset;
137
138         region->start = res->start - offset;
139         region->end = res->end - offset;
140 }
141
142 #ifdef CONFIG_HOTPLUG
143 EXPORT_SYMBOL(pcibios_resource_to_bus);
144 #endif
145
146 /*
147  * We need to avoid collisions with `mirrored' VGA ports
148  * and other strange ISA hardware, so we always want the
149  * addresses to be allocated in the 0x000-0x0ff region
150  * modulo 0x400.
151  *
152  * Why? Because some silly external IO cards only decode
153  * the low 10 bits of the IO address. The 0x00-0xff region
154  * is reserved for motherboard devices that decode all 16
155  * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
156  * but we want to try to avoid allocating at 0x2900-0x2bff
157  * which might have be mirrored at 0x0100-0x03ff..
158  */
159 void pcibios_align_resource(void *data, struct resource *res,
160                             unsigned long size, unsigned long align)
161 {
162         struct pci_dev *dev = data;
163         struct pci_controller *hose = PCI_GET_PHB_PTR(dev);
164         unsigned long start = res->start;
165         unsigned long alignto;
166
167         if (res->flags & IORESOURCE_IO) {
168                 unsigned long offset = (unsigned long)hose->io_base_virt -
169                                         pci_io_base;
170                 /* Make sure we start at our min on all hoses */
171                 if (start - offset < PCIBIOS_MIN_IO)
172                         start = PCIBIOS_MIN_IO + offset;
173
174                 /*
175                  * Put everything into 0x00-0xff region modulo 0x400
176                  */
177                 if (start & 0x300)
178                         start = (start + 0x3ff) & ~0x3ff;
179
180         } else if (res->flags & IORESOURCE_MEM) {
181                 /* Make sure we start at our min on all hoses */
182                 if (start - hose->pci_mem_offset < PCIBIOS_MIN_MEM)
183                         start = PCIBIOS_MIN_MEM + hose->pci_mem_offset;
184
185                 /* Align to multiple of size of minimum base.  */
186                 alignto = max(0x1000UL, align);
187                 start = ALIGN(start, alignto);
188         }
189
190         res->start = start;
191 }
192
193 /* 
194  * Allocate pci_controller(phb) initialized common variables. 
195  */
196 struct pci_controller * __init
197 pci_alloc_pci_controller(enum phb_types controller_type)
198 {
199         struct pci_controller *hose;
200         char *model;
201
202 #ifdef CONFIG_PPC_ISERIES
203         hose = (struct pci_controller *)kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
204 #else
205         hose = (struct pci_controller *)alloc_bootmem(sizeof(struct pci_controller));
206 #endif
207         if(hose == NULL) {
208                 printk(KERN_ERR "PCI: Allocate pci_controller failed.\n");
209                 return NULL;
210         }
211         memset(hose, 0, sizeof(struct pci_controller));
212
213         switch(controller_type) {
214 #ifdef CONFIG_PPC_ISERIES
215         case phb_type_hypervisor:
216                 model = "PHB HV";
217                 break;
218 #endif
219         case phb_type_python:
220                 model = "PHB PY";
221                 break;
222         case phb_type_speedwagon:
223                 model = "PHB SW";
224                 break;
225         case phb_type_winnipeg:
226                 model = "PHB WP";
227                 break;
228         case phb_type_apple:
229                 model = "PHB APPLE";
230                 break;
231         default:
232                 model = "PHB UK";
233                 break;
234         }
235
236         if(strlen(model) < 8)
237                 strcpy(hose->what,model);
238         else
239                 memcpy(hose->what,model,7);
240         hose->type = controller_type;
241         hose->global_number = global_phb_number++;
242         
243         *hose_tail = hose;
244         hose_tail = &hose->next;
245         return hose;
246 }
247
248 static void __init pcibios_claim_one_bus(struct pci_bus *b)
249 {
250         struct list_head *ld;
251         struct pci_bus *child_bus;
252
253         for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
254                 struct pci_dev *dev = pci_dev_b(ld);
255                 int i;
256
257                 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
258                         struct resource *r = &dev->resource[i];
259
260                         if (r->parent || !r->start || !r->flags)
261                                 continue;
262                         pci_claim_resource(dev, i);
263                 }
264         }
265
266         list_for_each_entry(child_bus, &b->children, node)
267                 pcibios_claim_one_bus(child_bus);
268 }
269
270 #ifndef CONFIG_PPC_ISERIES
271 static void __init pcibios_claim_of_setup(void)
272 {
273         struct list_head *lb;
274
275         for (lb = pci_root_buses.next; lb != &pci_root_buses; lb = lb->next) {
276                 struct pci_bus *b = pci_bus_b(lb);
277                 pcibios_claim_one_bus(b);
278         }
279 }
280 #endif
281
282 static int __init pcibios_init(void)
283 {
284         struct pci_controller *hose;
285         struct pci_bus *bus;
286
287 #ifdef CONFIG_PPC_ISERIES
288         iSeries_pcibios_init(); 
289 #endif
290
291         //ppc64_boot_msg(0x40, "PCI Probe");
292         printk("PCI: Probing PCI hardware\n");
293
294         /* Scan all of the recorded PCI controllers.  */
295         for (hose = hose_head; hose; hose = hose->next) {
296                 hose->last_busno = 0xff;
297                 bus = pci_scan_bus(hose->first_busno, hose->ops,
298                                    hose->arch_data);
299                 hose->bus = bus;
300                 hose->last_busno = bus->subordinate;
301         }
302
303 #ifndef CONFIG_PPC_ISERIES
304         if (pci_probe_only)
305                 pcibios_claim_of_setup();
306         else
307                 /* FIXME: `else' will be removed when
308                    pci_assign_unassigned_resources() is able to work
309                    correctly with [partially] allocated PCI tree. */
310                 pci_assign_unassigned_resources();
311 #endif
312
313         /* Call machine dependent final fixup */
314         if (ppc_md.pcibios_fixup)
315                 ppc_md.pcibios_fixup();
316
317         /* Cache the location of the ISA bridge (if we have one) */
318         ppc64_isabridge_dev = pci_find_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
319         if (ppc64_isabridge_dev != NULL)
320                 printk("ISA bridge at %s\n", pci_name(ppc64_isabridge_dev));
321
322         printk("PCI: Probing PCI hardware done\n");
323         //ppc64_boot_msg(0x41, "PCI Done");
324
325 #ifdef CONFIG_PPC_PSERIES
326         pci_addr_cache_build();
327 #endif
328
329         return 0;
330 }
331
332 subsys_initcall(pcibios_init);
333
334 char __init *pcibios_setup(char *str)
335 {
336         return str;
337 }
338
339 int pcibios_enable_device(struct pci_dev *dev, int mask)
340 {
341         u16 cmd, oldcmd;
342         int i;
343
344         pci_read_config_word(dev, PCI_COMMAND, &cmd);
345         oldcmd = cmd;
346
347         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
348                 struct resource *res = &dev->resource[i];
349
350                 /* Only set up the requested stuff */
351                 if (!(mask & (1<<i)))
352                         continue;
353
354                 if (res->flags & IORESOURCE_IO)
355                         cmd |= PCI_COMMAND_IO;
356                 if (res->flags & IORESOURCE_MEM)
357                         cmd |= PCI_COMMAND_MEMORY;
358         }
359
360         if (cmd != oldcmd) {
361                 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
362                        pci_name(dev), cmd);
363                 /* Enable the appropriate bits in the PCI command register.  */
364                 pci_write_config_word(dev, PCI_COMMAND, cmd);
365         }
366         return 0;
367 }
368
369 /*
370  * Return the domain number for this bus.
371  */
372 int pci_domain_nr(struct pci_bus *bus)
373 {
374 #ifdef CONFIG_PPC_ISERIES
375         return 0;
376 #else
377         struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
378
379         return hose->global_number;
380 #endif
381 }
382
383 EXPORT_SYMBOL(pci_domain_nr);
384
385 /* Set the name of the bus as it appears in /proc/bus/pci */
386 int pci_name_bus(char *name, struct pci_bus *bus)
387 {
388 #ifndef CONFIG_PPC_ISERIES
389         struct pci_controller *hose = PCI_GET_PHB_PTR(bus);
390
391         if (hose->buid)
392                 sprintf(name, "%04x:%02x", pci_domain_nr(bus), bus->number);
393         else
394 #endif
395                 sprintf(name, "%02x", bus->number);
396
397         return 0;
398 }
399
400 /*
401  * Platform support for /proc/bus/pci/X/Y mmap()s,
402  * modelled on the sparc64 implementation by Dave Miller.
403  *  -- paulus.
404  */
405
406 /*
407  * Adjust vm_pgoff of VMA such that it is the physical page offset
408  * corresponding to the 32-bit pci bus offset for DEV requested by the user.
409  *
410  * Basically, the user finds the base address for his device which he wishes
411  * to mmap.  They read the 32-bit value from the config space base register,
412  * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
413  * offset parameter of mmap on /proc/bus/pci/XXX for that device.
414  *
415  * Returns negative error code on failure, zero on success.
416  */
417 static __inline__ int
418 __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
419                        enum pci_mmap_state mmap_state)
420 {
421         struct pci_controller *hose = PCI_GET_PHB_PTR(dev);
422         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
423         unsigned long io_offset = 0;
424         int i, res_bit;
425
426         if (hose == 0)
427                 return -EINVAL;         /* should never happen */
428
429         /* If memory, add on the PCI bridge address offset */
430         if (mmap_state == pci_mmap_mem) {
431                 offset += hose->pci_mem_offset;
432                 res_bit = IORESOURCE_MEM;
433         } else {
434                 io_offset = (unsigned long)hose->io_base_virt;
435                 offset += io_offset;
436                 res_bit = IORESOURCE_IO;
437         }
438
439         /*
440          * Check that the offset requested corresponds to one of the
441          * resources of the device.
442          */
443         for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
444                 struct resource *rp = &dev->resource[i];
445                 int flags = rp->flags;
446
447                 /* treat ROM as memory (should be already) */
448                 if (i == PCI_ROM_RESOURCE)
449                         flags |= IORESOURCE_MEM;
450
451                 /* Active and same type? */
452                 if ((flags & res_bit) == 0)
453                         continue;
454
455                 /* In the range of this resource? */
456                 if (offset < (rp->start & PAGE_MASK) || offset > rp->end)
457                         continue;
458
459                 /* found it! construct the final physical address */
460                 if (mmap_state == pci_mmap_io)
461                         offset += hose->io_base_phys - io_offset;
462
463                 vma->vm_pgoff = offset >> PAGE_SHIFT;
464                 return 0;
465         }
466
467         return -EINVAL;
468 }
469
470 /*
471  * Set vm_flags of VMA, as appropriate for this architecture, for a pci device
472  * mapping.
473  */
474 static __inline__ void
475 __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
476                      enum pci_mmap_state mmap_state)
477 {
478         vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
479 }
480
481 /*
482  * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
483  * device mapping.
484  */
485 static __inline__ void
486 __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
487                       enum pci_mmap_state mmap_state, int write_combine)
488 {
489         long prot = pgprot_val(vma->vm_page_prot);
490
491         /* XXX would be nice to have a way to ask for write-through */
492         prot |= _PAGE_NO_CACHE;
493         if (!write_combine)
494                 prot |= _PAGE_GUARDED;
495         vma->vm_page_prot = __pgprot(prot);
496 }
497
498 /*
499  * Perform the actual remap of the pages for a PCI device mapping, as
500  * appropriate for this architecture.  The region in the process to map
501  * is described by vm_start and vm_end members of VMA, the base physical
502  * address is found in vm_pgoff.
503  * The pci device structure is provided so that architectures may make mapping
504  * decisions on a per-device or per-bus basis.
505  *
506  * Returns a negative error code on failure, zero on success.
507  */
508 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
509                         enum pci_mmap_state mmap_state,
510                         int write_combine)
511 {
512         int ret;
513
514         ret = __pci_mmap_make_offset(dev, vma, mmap_state);
515         if (ret < 0)
516                 return ret;
517
518         __pci_mmap_set_flags(dev, vma, mmap_state);
519         __pci_mmap_set_pgprot(dev, vma, mmap_state, write_combine);
520
521         ret = remap_page_range(vma, vma->vm_start, vma->vm_pgoff << PAGE_SHIFT,
522                                vma->vm_end - vma->vm_start, vma->vm_page_prot);
523
524         return ret;
525 }
526
527 #ifdef CONFIG_PPC_PSERIES
528 static ssize_t pci_show_devspec(struct device *dev, char *buf)
529 {
530         struct pci_dev *pdev;
531         struct device_node *np;
532
533         pdev = to_pci_dev (dev);
534         np = pci_device_to_OF_node(pdev);
535         if (np == NULL || np->full_name == NULL)
536                 return 0;
537         return sprintf(buf, "%s", np->full_name);
538 }
539 static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
540 #endif /* CONFIG_PPC_PSERIES */
541
542 void pcibios_add_platform_entries(struct pci_dev *pdev)
543 {
544 #ifdef CONFIG_PPC_PSERIES
545         device_create_file(&pdev->dev, &dev_attr_devspec);
546 #endif /* CONFIG_PPC_PSERIES */
547 }