make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / arch / alpha / kernel / pci.c
1 /*
2  *      linux/arch/alpha/kernel/pci.c
3  *
4  * Extruded from code written by
5  *      Dave Rusling (david.rusling@reo.mts.dec.com)
6  *      David Mosberger (davidm@cs.arizona.edu)
7  */
8
9 /* 2.3.x PCI/resources, 1999 Andrea Arcangeli <andrea@suse.de> */
10
11 /*
12  * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
13  *           PCI-PCI bridges cleanup
14  */
15
16 #include <linux/string.h>
17 #include <linux/pci.h>
18 #include <linux/init.h>
19 #include <linux/ioport.h>
20 #include <linux/kernel.h>
21 #include <linux/bootmem.h>
22 #include <asm/machvec.h>
23
24 #include "proto.h"
25 #include "pci_impl.h"
26
27
28 /*
29  * Some string constants used by the various core logics. 
30  */
31
32 const char *const pci_io_names[] = {
33   "PCI IO bus 0", "PCI IO bus 1", "PCI IO bus 2", "PCI IO bus 3",
34   "PCI IO bus 4", "PCI IO bus 5", "PCI IO bus 6", "PCI IO bus 7"
35 };
36
37 const char *const pci_mem_names[] = {
38   "PCI mem bus 0", "PCI mem bus 1", "PCI mem bus 2", "PCI mem bus 3",
39   "PCI mem bus 4", "PCI mem bus 5", "PCI mem bus 6", "PCI mem bus 7"
40 };
41
42 const char pci_hae0_name[] = "HAE0";
43
44 /* Indicate whether we respect the PCI setup left by console. */
45 int __initdata pci_probe_only;
46
47 /*
48  * The PCI controller list.
49  */
50
51 struct pci_controller *hose_head, **hose_tail = &hose_head;
52 struct pci_controller *pci_isa_hose;
53
54 /*
55  * Quirks.
56  */
57
58 static void __init
59 quirk_eisa_bridge(struct pci_dev *dev)
60 {
61         dev->class = PCI_CLASS_BRIDGE_EISA << 8;
62 }
63
64 static void __init
65 quirk_isa_bridge(struct pci_dev *dev)
66 {
67         dev->class = PCI_CLASS_BRIDGE_ISA << 8;
68 }
69
70 static void __init
71 quirk_cypress(struct pci_dev *dev)
72 {
73         /* The Notorious Cy82C693 chip.  */
74
75         /* The Cypress IDE controller doesn't support native mode, but it
76            has programmable addresses of IDE command/control registers.
77            This violates PCI specifications, confuses the IDE subsystem and
78            causes resource conflicts between the primary HD_CMD register and
79            the floppy controller.  Ugh.  Fix that.  */
80         if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE) {
81                 dev->resource[0].flags = 0;
82                 dev->resource[1].flags = 0;
83         }
84
85         /* The Cypress bridge responds on the PCI bus in the address range
86            0xffff0000-0xffffffff (conventional x86 BIOS ROM).  There is no
87            way to turn this off.  The bridge also supports several extended
88            BIOS ranges (disabled after power-up), and some consoles do turn
89            them on.  So if we use a large direct-map window, or a large SG
90            window, we must avoid entire 0xfff00000-0xffffffff region.  */
91         else if (dev->class >> 8 == PCI_CLASS_BRIDGE_ISA) {
92                 if (__direct_map_base + __direct_map_size >= 0xfff00000)
93                         __direct_map_size = 0xfff00000 - __direct_map_base;
94                 else {
95                         struct pci_controller *hose = dev->sysdata;
96                         struct pci_iommu_arena *pci = hose->sg_pci;
97                         if (pci && pci->dma_base + pci->size >= 0xfff00000)
98                                 pci->size = 0xfff00000 - pci->dma_base;
99                 }
100         }
101 }
102
103 struct pci_fixup pcibios_fixups[] __initdata = {
104         { PCI_FIXUP_HEADER, PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82375,
105           quirk_eisa_bridge },
106         { PCI_FIXUP_HEADER, PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82378,
107           quirk_isa_bridge },
108         { PCI_FIXUP_HEADER, PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693,
109           quirk_cypress },
110         { 0 }
111 };
112
113 #define MAX(val1, val2)         ((val1) > (val2) ? (val1) : (val2))
114 #define ALIGN(val,align)        (((val) + ((align) - 1)) & ~((align) - 1))
115 #define KB                      1024
116 #define MB                      (1024*KB)
117 #define GB                      (1024*MB)
118
119 void
120 pcibios_align_resource(void *data, struct resource *res,
121                        unsigned long size, unsigned long align)
122 {
123         struct pci_dev *dev = data;
124         struct pci_controller *hose = dev->sysdata;
125         unsigned long alignto;
126         unsigned long start = res->start;
127
128         if (res->flags & IORESOURCE_IO) {
129                 /* Make sure we start at our min on all hoses */
130                 if (start - hose->io_space->start < PCIBIOS_MIN_IO)
131                         start = PCIBIOS_MIN_IO + hose->io_space->start;
132
133                 /*
134                  * Put everything into 0x00-0xff region modulo 0x400
135                  */
136                 if (start & 0x300)
137                         start = (start + 0x3ff) & ~0x3ff;
138         }
139         else if (res->flags & IORESOURCE_MEM) {
140                 /* Make sure we start at our min on all hoses */
141                 if (start - hose->mem_space->start < PCIBIOS_MIN_MEM)
142                         start = PCIBIOS_MIN_MEM + hose->mem_space->start;
143
144                 /*
145                  * The following holds at least for the Low Cost
146                  * Alpha implementation of the PCI interface:
147                  *
148                  * In sparse memory address space, the first
149                  * octant (16MB) of every 128MB segment is
150                  * aliased to the very first 16 MB of the
151                  * address space (i.e., it aliases the ISA
152                  * memory address space).  Thus, we try to
153                  * avoid allocating PCI devices in that range.
154                  * Can be allocated in 2nd-7th octant only.
155                  * Devices that need more than 112MB of
156                  * address space must be accessed through
157                  * dense memory space only!
158                  */
159
160                 /* Align to multiple of size of minimum base.  */
161                 alignto = MAX(0x1000, align);
162                 start = ALIGN(start, alignto);
163                 if (hose->sparse_mem_base && size <= 7 * 16*MB) {
164                         if (((start / (16*MB)) & 0x7) == 0) {
165                                 start &= ~(128*MB - 1);
166                                 start += 16*MB;
167                                 start  = ALIGN(start, alignto);
168                         }
169                         if (start/(128*MB) != (start + size - 1)/(128*MB)) {
170                                 start &= ~(128*MB - 1);
171                                 start += (128 + 16)*MB;
172                                 start  = ALIGN(start, alignto);
173                         }
174                 }
175         }
176
177         res->start = start;
178 }
179 #undef MAX
180 #undef ALIGN
181 #undef KB
182 #undef MB
183 #undef GB
184
185 void __init
186 pcibios_init(void)
187 {
188         if (!alpha_mv.init_pci)
189                 return;
190         alpha_mv.init_pci();
191 }
192
193 char * __init
194 pcibios_setup(char *str)
195 {
196         return str;
197 }
198
199 void __init
200 pcibios_fixup_resource(struct resource *res, struct resource *root)
201 {
202         res->start += root->start;
203         res->end += root->start;
204 }
205
206 void __init
207 pcibios_fixup_device_resources(struct pci_dev *dev, struct pci_bus *bus)
208 {
209         /* Update device resources.  */
210         struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
211         int i;
212
213         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
214                 if (!dev->resource[i].start)
215                         continue;
216                 if (dev->resource[i].flags & IORESOURCE_IO)
217                         pcibios_fixup_resource(&dev->resource[i],
218                                                hose->io_space);
219                 else if (dev->resource[i].flags & IORESOURCE_MEM)
220                         pcibios_fixup_resource(&dev->resource[i],
221                                                hose->mem_space);
222         }
223 }
224
225 void __init
226 pcibios_fixup_bus(struct pci_bus *bus)
227 {
228         /* Propogate hose info into the subordinate devices.  */
229
230         struct pci_controller *hose = bus->sysdata;
231         struct list_head *ln;
232         struct pci_dev *dev = bus->self;
233
234         if (!dev) {
235                 /* Root bus. */
236                 u32 pci_mem_end;
237                 u32 sg_base = hose->sg_pci ? hose->sg_pci->dma_base : ~0;
238                 unsigned long end;
239
240                 bus->resource[0] = hose->io_space;
241                 bus->resource[1] = hose->mem_space;
242
243                 /* Adjust hose mem_space limit to prevent PCI allocations
244                    in the iommu windows. */
245                 pci_mem_end = min((u32)__direct_map_base, sg_base) - 1;
246                 end = hose->mem_space->start + pci_mem_end;
247                 if (hose->mem_space->end > end)
248                         hose->mem_space->end = end;
249         } else if (pci_probe_only &&
250                    (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
251                 pci_read_bridge_bases(bus);
252                 pcibios_fixup_device_resources(dev, bus);
253         } 
254
255         for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
256                 struct pci_dev *dev = pci_dev_b(ln);
257                 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
258                         pcibios_fixup_device_resources(dev, bus);
259         }
260 }
261
262 void
263 pcibios_update_resource(struct pci_dev *dev, struct resource *root,
264                         struct resource *res, int resource)
265 {
266         struct pci_controller *hose = dev->sysdata;
267         int where;
268         u32 reg;
269
270         if (resource < PCI_ROM_RESOURCE) 
271                 where = PCI_BASE_ADDRESS_0 + (resource * 4);
272         else if (resource == PCI_ROM_RESOURCE)
273                 where = dev->rom_base_reg;
274         else {
275                 return; /* Don't update non-standard resources here. */
276         }
277
278         /* Point root at the hose root. */
279         if (res->flags & IORESOURCE_IO)
280                 root = hose->io_space;
281         if (res->flags & IORESOURCE_MEM)
282                 root = hose->mem_space;
283
284         reg = (res->start - root->start) | (res->flags & 0xf);
285         pci_write_config_dword(dev, where, reg);
286         if ((res->flags & (PCI_BASE_ADDRESS_SPACE
287                            | PCI_BASE_ADDRESS_MEM_TYPE_MASK))
288             == (PCI_BASE_ADDRESS_SPACE_MEMORY
289                 | PCI_BASE_ADDRESS_MEM_TYPE_64)) {
290                 pci_write_config_dword(dev, where+4, 0);
291                 printk(KERN_WARNING "PCI: dev %s type 64-bit\n", dev->name);
292         }
293
294         /* ??? FIXME -- record old value for shutdown.  */
295 }
296
297 void __init
298 pcibios_update_irq(struct pci_dev *dev, int irq)
299 {
300         pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
301
302         /* ??? FIXME -- record old value for shutdown.  */
303 }
304
305 /* Most Alphas have straight-forward swizzling needs.  */
306
307 u8 __init
308 common_swizzle(struct pci_dev *dev, u8 *pinp)
309 {
310         struct pci_controller *hose = dev->sysdata;
311
312         if (dev->bus->number != hose->first_busno) {
313                 u8 pin = *pinp;
314                 do {
315                         pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
316                         /* Move up the chain of bridges. */
317                         dev = dev->bus->self;
318                 } while (dev->bus->self);
319                 *pinp = pin;
320
321                 /* The slot is the slot of the last bridge. */
322         }
323
324         return PCI_SLOT(dev->devfn);
325 }
326
327 void __init
328 pcibios_fixup_pbus_ranges(struct pci_bus * bus,
329                           struct pbus_set_ranges_data * ranges)
330 {
331         struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
332
333         ranges->io_start -= hose->io_space->start;
334         ranges->io_end -= hose->io_space->start;
335         ranges->mem_start -= hose->mem_space->start;
336         ranges->mem_end -= hose->mem_space->start;
337 /* FIXME: On older alphas we could use dense memory space
338           to access prefetchable resources. */
339         ranges->prefetch_start -= hose->mem_space->start;
340         ranges->prefetch_end -= hose->mem_space->start;
341 }
342
343 int
344 pcibios_enable_device(struct pci_dev *dev, int mask)
345 {
346         /* Nothing to do, since we enable all devices at startup.  */
347         return 0;
348 }
349
350 /*
351  *  If we set up a device for bus mastering, we need to check the latency
352  *  timer as certain firmware forgets to set it properly, as seen
353  *  on SX164 and LX164 with SRM.
354  */
355 void
356 pcibios_set_master(struct pci_dev *dev)
357 {
358         u8 lat;
359         pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
360         if (lat >= 16) return;
361         printk("PCI: Setting latency timer of device %s to 64\n",
362                                                         dev->slot_name);
363         pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
364 }
365
366 static void __init
367 pcibios_claim_console_setup(void)
368 {
369         struct list_head *lb;
370
371         for(lb = pci_root_buses.next; lb != &pci_root_buses; lb = lb->next) {
372                 struct pci_bus *b = pci_bus_b(lb);
373                 struct list_head *ld;
374
375                 for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
376                         struct pci_dev *dev = pci_dev_b(ld);
377                         int i;
378
379                         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
380                                 struct resource *r = &dev->resource[i];
381
382                                 if (r->parent || !r->start || !r->flags)
383                                         continue;
384                                 pci_claim_resource(dev, i);
385                         }
386                 }
387         }
388 }
389
390 void __init
391 common_init_pci(void)
392 {
393         struct pci_controller *hose;
394         struct pci_bus *bus;
395         int next_busno;
396
397         /* Scan all of the recorded PCI controllers.  */
398         for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
399                 hose->first_busno = next_busno;
400                 hose->last_busno = 0xff;
401                 bus = pci_scan_bus(next_busno, alpha_mv.pci_ops, hose);
402                 hose->bus = bus;
403                 next_busno = hose->last_busno = bus->subordinate;
404                 next_busno += 1;
405         }
406
407         if (pci_probe_only)
408                 pcibios_claim_console_setup();
409         else    /* FIXME: `else' will be removed when
410                    pci_assign_unassigned_resources() is able to work
411                    correctly with [partially] allocated PCI tree. */
412                 pci_assign_unassigned_resources();
413         pci_fixup_irqs(alpha_mv.pci_swizzle, alpha_mv.pci_map_irq);
414 }
415
416
417 struct pci_controller * __init
418 alloc_pci_controller(void)
419 {
420         struct pci_controller *hose;
421
422         hose = alloc_bootmem(sizeof(*hose));
423
424         *hose_tail = hose;
425         hose_tail = &hose->next;
426
427         return hose;
428 }
429
430 struct resource * __init
431 alloc_resource(void)
432 {
433         struct resource *res;
434
435         res = alloc_bootmem(sizeof(*res));
436
437         return res;
438 }
439
440
441 /* Provide information on locations of various I/O regions in physical
442    memory.  Do this on a per-card basis so that we choose the right hose.  */
443
444 asmlinkage long
445 sys_pciconfig_iobase(long which, unsigned long bus, unsigned long dfn)
446 {
447         struct pci_controller *hose;
448         struct pci_dev *dev;
449
450         /* from hose or from bus.devfn */
451         if (which & IOBASE_FROM_HOSE) {
452                 for(hose = hose_head; hose; hose = hose->next) 
453                         if (hose->index == bus) break;
454                 if (!hose) return -ENODEV;
455         } else {
456                 /* Special hook for ISA access.  */
457                 if (bus == 0 && dfn == 0) {
458                         hose = pci_isa_hose;
459                 } else {
460                         dev = pci_find_slot(bus, dfn);
461                         if (!dev)
462                                 return -ENODEV;
463                         hose = dev->sysdata;
464                 }
465         }
466
467         switch (which & ~IOBASE_FROM_HOSE) {
468         case IOBASE_HOSE:
469                 return hose->index;
470         case IOBASE_SPARSE_MEM:
471                 return hose->sparse_mem_base;
472         case IOBASE_DENSE_MEM:
473                 return hose->dense_mem_base;
474         case IOBASE_SPARSE_IO:
475                 return hose->sparse_io_base;
476         case IOBASE_DENSE_IO:
477                 return hose->dense_io_base;
478         case IOBASE_ROOT_BUS:
479                 return hose->bus->number;
480         }
481
482         return -EOPNOTSUPP;
483 }
484
485 /* Return the index of the PCI controller for device PDEV. */
486 int
487 pci_controller_num(struct pci_dev *pdev)
488 {
489         struct pci_controller *hose = pdev->sysdata;
490         return (hose ? hose->index : -ENXIO);
491 }