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