vserver 1.9.3
[linux-2.6.git] / arch / m68k / kernel / bios32.c
1 /*
2  * bios32.c - PCI BIOS functions for m68k systems.
3  *
4  * Written by Wout Klaren.
5  *
6  * Based on the DEC Alpha bios32.c by Dave Rusling and David Mosberger.
7  */
8
9 #include <linux/config.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12
13 #if 0
14 # define DBG_DEVS(args)         printk args
15 #else
16 # define DBG_DEVS(args)
17 #endif
18
19 #ifdef CONFIG_PCI
20
21 /*
22  * PCI support for Linux/m68k. Currently only the Hades is supported.
23  *
24  * The support for PCI bridges in the DEC Alpha version has
25  * been removed in this version.
26  */
27
28 #include <linux/pci.h>
29 #include <linux/slab.h>
30 #include <linux/mm.h>
31
32 #include <asm/io.h>
33 #include <asm/pci.h>
34 #include <asm/uaccess.h>
35
36 #define KB              1024
37 #define MB              (1024*KB)
38 #define GB              (1024*MB)
39
40 #define MAJOR_REV       0
41 #define MINOR_REV       5
42
43 /*
44  * Align VAL to ALIGN, which must be a power of two.
45  */
46
47 #define ALIGN(val,align)        (((val) + ((align) - 1)) & ~((align) - 1))
48
49 #define MAX(val1, val2)         (((val1) > (val2)) ? val1 : val2)
50
51 /*
52  * Offsets relative to the I/O and memory base addresses from where resources
53  * are allocated.
54  */
55
56 #define IO_ALLOC_OFFSET         0x00004000
57 #define MEM_ALLOC_OFFSET        0x04000000
58
59 /*
60  * Declarations of hardware specific initialisation functions.
61  */
62
63 extern struct pci_bus_info *init_hades_pci(void);
64
65 /*
66  * Bus info structure of the PCI bus. A pointer to this structure is
67  * put in the sysdata member of the pci_bus structure.
68  */
69
70 static struct pci_bus_info *bus_info;
71
72 static int pci_modify = 1;              /* If set, layout the PCI bus ourself. */
73 static int skip_vga;                    /* If set do not modify base addresses
74                                            of vga cards.*/
75 static int disable_pci_burst;           /* If set do not allow PCI bursts. */
76
77 static unsigned int io_base;
78 static unsigned int mem_base;
79
80 /*
81  * static void disable_dev(struct pci_dev *dev)
82  *
83  * Disable PCI device DEV so that it does not respond to I/O or memory
84  * accesses.
85  *
86  * Parameters:
87  *
88  * dev  - device to disable.
89  */
90
91 static void __init disable_dev(struct pci_dev *dev)
92 {
93         unsigned short cmd;
94
95         if (((dev->class >> 8 == PCI_CLASS_NOT_DEFINED_VGA) ||
96              (dev->class >> 8 == PCI_CLASS_DISPLAY_VGA) ||
97              (dev->class >> 8 == PCI_CLASS_DISPLAY_XGA)) && skip_vga)
98                 return;
99
100         pci_read_config_word(dev, PCI_COMMAND, &cmd);
101
102         cmd &= (~PCI_COMMAND_IO & ~PCI_COMMAND_MEMORY & ~PCI_COMMAND_MASTER);
103         pci_write_config_word(dev, PCI_COMMAND, cmd);
104 }
105
106 /*
107  * static void layout_dev(struct pci_dev *dev)
108  *
109  * Layout memory and I/O for a device.
110  *
111  * Parameters:
112  *
113  * device       - device to layout memory and I/O for.
114  */
115
116 static void __init layout_dev(struct pci_dev *dev)
117 {
118         unsigned short cmd;
119         unsigned int base, mask, size, reg;
120         unsigned int alignto;
121         int i;
122
123         /*
124          * Skip video cards if requested.
125          */
126
127         if (((dev->class >> 8 == PCI_CLASS_NOT_DEFINED_VGA) ||
128              (dev->class >> 8 == PCI_CLASS_DISPLAY_VGA) ||
129              (dev->class >> 8 == PCI_CLASS_DISPLAY_XGA)) && skip_vga)
130                 return;
131
132         pci_read_config_word(dev, PCI_COMMAND, &cmd);
133
134         for (reg = PCI_BASE_ADDRESS_0, i = 0; reg <= PCI_BASE_ADDRESS_5; reg += 4, i++)
135         {
136                 /*
137                  * Figure out how much space and of what type this
138                  * device wants.
139                  */
140
141                 pci_write_config_dword(dev, reg, 0xffffffff);
142                 pci_read_config_dword(dev, reg, &base);
143
144                 if (!base)
145                 {
146                         /* this base-address register is unused */
147                         dev->resource[i].start = 0;
148                         dev->resource[i].end = 0;
149                         dev->resource[i].flags = 0;
150                         continue;
151                 }
152
153                 /*
154                  * We've read the base address register back after
155                  * writing all ones and so now we must decode it.
156                  */
157
158                 if (base & PCI_BASE_ADDRESS_SPACE_IO)
159                 {
160                         /*
161                          * I/O space base address register.
162                          */
163
164                         cmd |= PCI_COMMAND_IO;
165
166                         base &= PCI_BASE_ADDRESS_IO_MASK;
167                         mask = (~base << 1) | 0x1;
168                         size = (mask & base) & 0xffffffff;
169
170                         /*
171                          * Align to multiple of size of minimum base.
172                          */
173
174                         alignto = MAX(0x040, size) ;
175                         base = ALIGN(io_base, alignto);
176                         io_base = base + size;
177                         pci_write_config_dword(dev, reg, base | PCI_BASE_ADDRESS_SPACE_IO);
178
179                         dev->resource[i].start = base;
180                         dev->resource[i].end = dev->resource[i].start + size - 1;
181                         dev->resource[i].flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
182
183                         DBG_DEVS(("layout_dev: IO address: %lX\n", base));
184                 }
185                 else
186                 {
187                         unsigned int type;
188
189                         /*
190                          * Memory space base address register.
191                          */
192
193                         cmd |= PCI_COMMAND_MEMORY;
194                         type = base & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
195                         base &= PCI_BASE_ADDRESS_MEM_MASK;
196                         mask = (~base << 1) | 0x1;
197                         size = (mask & base) & 0xffffffff;
198                         switch (type)
199                         {
200                         case PCI_BASE_ADDRESS_MEM_TYPE_32:
201                         case PCI_BASE_ADDRESS_MEM_TYPE_64:
202                                 break;
203
204                         case PCI_BASE_ADDRESS_MEM_TYPE_1M:
205                                 printk("bios32 WARNING: slot %d, function %d "
206                                        "requests memory below 1MB---don't "
207                                        "know how to do that.\n",
208                                        PCI_SLOT(dev->devfn),
209                                        PCI_FUNC(dev->devfn));
210                                 continue;
211                         }
212
213                         /*
214                          * Align to multiple of size of minimum base.
215                          */
216
217                         alignto = MAX(0x1000, size) ;
218                         base = ALIGN(mem_base, alignto);
219                         mem_base = base + size;
220                         pci_write_config_dword(dev, reg, base);
221
222                         dev->resource[i].start = base;
223                         dev->resource[i].end = dev->resource[i].start + size - 1;
224                         dev->resource[i].flags = IORESOURCE_MEM;
225
226                         if (type == PCI_BASE_ADDRESS_MEM_TYPE_64)
227                         {
228                                 /*
229                                  * 64-bit address, set the highest 32 bits
230                                  * to zero.
231                                  */
232
233                                 reg += 4;
234                                 pci_write_config_dword(dev, reg, 0);
235
236                                 i++;
237                                 dev->resource[i].start = 0;
238                                 dev->resource[i].end = 0;
239                                 dev->resource[i].flags = 0;
240                         }
241                 }
242         }
243
244         /*
245          * Enable device:
246          */
247
248         if (dev->class >> 8 == PCI_CLASS_NOT_DEFINED ||
249             dev->class >> 8 == PCI_CLASS_NOT_DEFINED_VGA ||
250             dev->class >> 8 == PCI_CLASS_DISPLAY_VGA ||
251             dev->class >> 8 == PCI_CLASS_DISPLAY_XGA)
252         {
253                 /*
254                  * All of these (may) have I/O scattered all around
255                  * and may not use i/o-base address registers at all.
256                  * So we just have to always enable I/O to these
257                  * devices.
258                  */
259                 cmd |= PCI_COMMAND_IO;
260         }
261
262         pci_write_config_word(dev, PCI_COMMAND, cmd | PCI_COMMAND_MASTER);
263
264         pci_write_config_byte(dev, PCI_LATENCY_TIMER, (disable_pci_burst) ? 0 : 32);
265
266         if (bus_info != NULL)
267                 bus_info->conf_device(dev);     /* Machine dependent configuration. */
268
269         DBG_DEVS(("layout_dev: bus %d  slot 0x%x  VID 0x%x  DID 0x%x  class 0x%x\n",
270                   dev->bus->number, PCI_SLOT(dev->devfn), dev->vendor, dev->device, dev->class));
271 }
272
273 /*
274  * static void layout_bus(struct pci_bus *bus)
275  *
276  * Layout memory and I/O for all devices on the given bus.
277  *
278  * Parameters:
279  *
280  * bus  - bus.
281  */
282
283 static void __init layout_bus(struct pci_bus *bus)
284 {
285         unsigned int bio, bmem;
286         struct pci_dev *dev;
287
288         DBG_DEVS(("layout_bus: starting bus %d\n", bus->number));
289
290         if (!bus->devices && !bus->children)
291                 return;
292
293         /*
294          * Align the current bases on appropriate boundaries (4K for
295          * IO and 1MB for memory).
296          */
297
298         bio = io_base = ALIGN(io_base, 4*KB);
299         bmem = mem_base = ALIGN(mem_base, 1*MB);
300
301         /*
302          * PCI devices might have been setup by a PCI BIOS emulation
303          * running under TOS. In these cases there is a
304          * window during which two devices may have an overlapping
305          * address range. To avoid this causing trouble, we first
306          * turn off the I/O and memory address decoders for all PCI
307          * devices.  They'll be re-enabled only once all address
308          * decoders are programmed consistently.
309          */
310
311         DBG_DEVS(("layout_bus: disable_dev for bus %d\n", bus->number));
312
313         for (dev = bus->devices; dev; dev = dev->sibling)
314         {
315                 if ((dev->class >> 16 != PCI_BASE_CLASS_BRIDGE) ||
316                     (dev->class >> 8 == PCI_CLASS_BRIDGE_PCMCIA))
317                         disable_dev(dev);
318         }
319
320         /*
321          * Allocate space to each device:
322          */
323
324         DBG_DEVS(("layout_bus: starting bus %d devices\n", bus->number));
325
326         for (dev = bus->devices; dev; dev = dev->sibling)
327         {
328                 if ((dev->class >> 16 != PCI_BASE_CLASS_BRIDGE) ||
329                     (dev->class >> 8 == PCI_CLASS_BRIDGE_PCMCIA))
330                         layout_dev(dev);
331         }
332
333         DBG_DEVS(("layout_bus: bus %d finished\n", bus->number));
334 }
335
336 /*
337  * static void pcibios_fixup(void)
338  *
339  * Layout memory and I/O of all devices on the PCI bus if 'pci_modify' is
340  * true. This might be necessary because not every m68k machine with a PCI
341  * bus has a PCI BIOS. This function should be called right after
342  * pci_scan_bus() in pcibios_init().
343  */
344
345 static void __init pcibios_fixup(void)
346 {
347         if (pci_modify)
348         {
349                 /*
350                  * Set base addresses for allocation of I/O and memory space.
351                  */
352
353                 io_base = bus_info->io_space.start + IO_ALLOC_OFFSET;
354                 mem_base = bus_info->mem_space.start + MEM_ALLOC_OFFSET;
355
356                 /*
357                  * Scan the tree, allocating PCI memory and I/O space.
358                  */
359
360                 layout_bus(pci_bus_b(pci_root.next));
361         }
362
363         /*
364          * Fix interrupt assignments, etc.
365          */
366
367         bus_info->fixup(pci_modify);
368 }
369
370 /*
371  * static void pcibios_claim_resources(struct pci_bus *bus)
372  *
373  * Claim all resources that are assigned to devices on the given bus.
374  *
375  * Parameters:
376  *
377  * bus  - bus.
378  */
379
380 static void __init pcibios_claim_resources(struct pci_bus *bus)
381 {
382         struct pci_dev *dev;
383         int i;
384
385         while (bus)
386         {
387                 for (dev = bus->devices; (dev != NULL); dev = dev->sibling)
388                 {
389                         for (i = 0; i < PCI_NUM_RESOURCES; i++)
390                         {
391                                 struct resource *r = &dev->resource[i];
392                                 struct resource *pr;
393                                 struct pci_bus_info *bus_info = (struct pci_bus_info *) dev->sysdata;
394
395                                 if ((r->start == 0) || (r->parent != NULL))
396                                         continue;
397 #if 1
398                                 if (r->flags & IORESOURCE_IO)
399                                         pr = &bus_info->io_space;
400                                 else
401                                         pr = &bus_info->mem_space;
402 #else
403                                 if (r->flags & IORESOURCE_IO)
404                                         pr = &ioport_resource;
405                                 else
406                                         pr = &iomem_resource;
407 #endif
408                                 if (request_resource(pr, r) < 0)
409                                 {
410                                         printk(KERN_ERR "PCI: Address space collision on region %d of device %s\n", i, dev->name);
411                                 }
412                         }
413                 }
414
415                 if (bus->children)
416                         pcibios_claim_resources(bus->children);
417
418                 bus = bus->next;
419         }
420 }
421
422 /*
423  * int pcibios_assign_resource(struct pci_dev *dev, int i)
424  *
425  * Assign a new address to a PCI resource.
426  *
427  * Parameters:
428  *
429  * dev  - device.
430  * i    - resource.
431  *
432  * Result: 0 if successful.
433  */
434
435 int __init pcibios_assign_resource(struct pci_dev *dev, int i)
436 {
437         struct resource *r = &dev->resource[i];
438         struct resource *pr = pci_find_parent_resource(dev, r);
439         unsigned long size = r->end + 1;
440
441         if (!pr)
442                 return -EINVAL;
443
444         if (r->flags & IORESOURCE_IO)
445         {
446                 if (size > 0x100)
447                         return -EFBIG;
448
449                 if (allocate_resource(pr, r, size, bus_info->io_space.start +
450                                       IO_ALLOC_OFFSET,  bus_info->io_space.end, 1024))
451                         return -EBUSY;
452         }
453         else
454         {
455                 if (allocate_resource(pr, r, size, bus_info->mem_space.start +
456                                       MEM_ALLOC_OFFSET, bus_info->mem_space.end, size))
457                         return -EBUSY;
458         }
459
460         if (i < 6)
461                 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, r->start);
462
463         return 0;
464 }
465
466 void __init pcibios_fixup_bus(struct pci_bus *bus)
467 {
468         struct pci_dev *dev;
469         void *sysdata;
470
471         sysdata = (bus->parent) ? bus->parent->sysdata : bus->sysdata;
472
473         for (dev = bus->devices; (dev != NULL); dev = dev->sibling)
474                 dev->sysdata = sysdata;
475 }
476
477 void __init pcibios_init(void)
478 {
479         printk("Linux/m68k PCI BIOS32 revision %x.%02x\n", MAJOR_REV, MINOR_REV);
480
481         bus_info = NULL;
482 #ifdef CONFIG_HADES
483         if (MACH_IS_HADES)
484                 bus_info = init_hades_pci();
485 #endif
486         if (bus_info != NULL)
487         {
488                 printk("PCI: Probing PCI hardware\n");
489                 pci_scan_bus(0, bus_info->m68k_pci_ops, bus_info);
490                 pcibios_fixup();
491                 pcibios_claim_resources(pci_root);
492         }
493         else
494                 printk("PCI: No PCI bus detected\n");
495 }
496
497 char * __init pcibios_setup(char *str)
498 {
499         if (!strcmp(str, "nomodify"))
500         {
501                 pci_modify = 0;
502                 return NULL;
503         }
504         else if (!strcmp(str, "skipvga"))
505         {
506                 skip_vga = 1;
507                 return NULL;
508         }
509         else if (!strcmp(str, "noburst"))
510         {
511                 disable_pci_burst = 1;
512                 return NULL;
513         }
514
515         return str;
516 }
517 #endif /* CONFIG_PCI */