3 * Hyperion Entertainment, Hans-JoergF@hyperion-entertainment.com
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 DECLARE_GLOBAL_DATA_PTR;
31 #undef ARTICIA_PCI_DEBUG
33 #ifdef ARTICIA_PCI_DEBUG
34 #define PRINTF(fmt,args...) printf (fmt ,##args)
36 #define PRINTF(fmt,args...)
39 struct pci_controller articiaS_hose;
41 long irq_alloc(long wanted);
43 static pci_dev_t pci_hose_find_class(struct pci_controller *hose, int bus, short find_class, int index);
44 static int articiaS_init_vga(void);
45 static void pci_cfgfunc_dummy(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table);
46 unsigned char pci_irq_alloc(void);
48 extern void via_cfgfunc_via686(struct pci_controller * host, pci_dev_t dev, struct pci_config_table *table);
49 extern void via_cfgfunc_ide_init(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table);
50 extern void via_init_irq_routing(uint8 []);
51 extern void via_init_afterscan(void);
53 #define cfgfunc_via686 1
54 #define cfgfunc_dummy 2
55 #define cfgfunc_ide_init 3
57 static struct pci_config_table config_table[] =
60 0x1106, PCI_ANY_ID, PCI_CLASS_BRIDGE_ISA, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
61 (void *)cfgfunc_via686, {0, 0, 0}
64 0x1106, PCI_ANY_ID, PCI_ANY_ID, 0,7,4,
65 (void *)cfgfunc_dummy, {0,0,0}
68 0x1106, 0x3068, PCI_ANY_ID, 0, 7, PCI_ANY_ID,
69 (void *)cfgfunc_dummy, {0,0,0}
72 0x1106, PCI_ANY_ID, PCI_ANY_ID, 0,7,1,
73 (void *)cfgfunc_ide_init, {0,0,0}
81 void pci_cfgfunc_dummy(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table)
87 unsigned long irq_penalties[16] =
90 1000, /* 1:keyboard */
92 50, /* 3:serial (COM2) */
93 50, /* 4:serial (COM1) */
109 * The following defines a hard-coded interrupt mapping for the
110 * know devices on the board.
111 * If a device isn't found here, assumed to be a device that's
112 * plugged into a PCI or AGP slot
113 * NOTE: This table is machine dependant.
116 struct pci_irq_fixup_table
118 uint8 bus; /* Bus number */
119 uint8 device; /* Device number */
120 uint8 func; /* Function number */
121 uint8 interrupt; /* Interrupt to use (0xff to disable) */
124 struct pci_irq_fixup_table fixuptab [] =
126 { 0, 0, 0, 0xff}, /* Articia S host bridge */
127 { 0, 1, 0, 0xff}, /* Articia S AGP bridge */
128 /* { 0, 6, 0, 0x05}, /###* 3COM ethernet */
129 { 0, 7, 0, 0xff}, /* VIA southbridge */
130 { 0, 7, 1, 0x0e}, /* IDE controller in legacy mode */
131 /* { 0, 7, 2, 0x05}, /###* First USB controller */
132 /* { 0, 7, 3, 0x0c}, /###* Second USB controller (shares interrupt with ethernet) */
133 { 0, 7, 4, 0xff}, /* ACPI Power Management */
134 /* { 0, 7, 5, 0x08}, /###* AC97 */
135 /* { 0, 7, 6, 0x08}, /###* MC97 */
136 { 0xff, 0xff, 0xff, 0xff}
141 * This table maps IRQ's to PCI interrupts
144 uint8 pci_intmap[4] = {0, 0, 0, 0};
147 * Map PCI slots to interrupt routings
148 * This table lists the device number assigned to a card inserted
149 * into the slot, along with a permutation for the slot's IRQ routing.
150 * NOTE: This table is machine dependant.
153 struct pci_slot_irq_routing
161 struct pci_slot_irq_routing amigaone_pci_routing[] =
163 {0, 8, {0, 1, 2, 3}}, /* Slot 1 (left of riser slot) */
164 {0, 9, {1, 2, 3, 0}}, /* Slot 2 (middle slot) */
165 {0, 10, {2, 3, 0, 1}}, /* Slot 3 (leftmost slot) */
166 {1, 0, {1, 0, 2, 3}}, /* AGP slot (only IRQA and IRQB) */
167 {1, 1, {1, 2, 3, 0}}, /* PCI slot on AGP bus */
168 {0, 6, {3, 3, 3, 3}}, /* On board ethernet */
169 {0, 7, {0, 1, 2, 3}}, /* Southbridge */
170 {0xff, 0, {0, 0, 0, 0}}
173 void articiaS_pci_irq_init(void)
177 s = getenv("pci_irqa");
179 pci_intmap[0] = simple_strtoul (s, NULL, 10);
181 pci_intmap[0] = pci_irq_alloc();
183 s = getenv("pci_irqb");
185 pci_intmap[1] = simple_strtoul (s, NULL, 10);
187 pci_intmap[1] = pci_irq_alloc();
189 s = getenv("pci_irqc");
191 pci_intmap[2] = simple_strtoul (s, NULL, 10);
193 pci_intmap[2] = pci_irq_alloc();
195 s = getenv("pci_irqd");
197 pci_intmap[3] = simple_strtoul (s, NULL, 10);
199 pci_intmap[3] = pci_irq_alloc();
203 unsigned char pci_irq_alloc(void)
207 unsigned long min_penalty = 1000;
209 /* Search for the minimal penalty, favoring interrupts at the end */
210 for (i = 0; i < 16; i++)
212 if (irq_penalties[i] <= min_penalty)
215 min_penalty = irq_penalties[i];
219 PRINTF("pci_irq_alloc: Minimal penalty is %ld for %d\n", min_penalty, interrupt);
221 irq_penalties[interrupt]++;
227 void articiaS_pci_fixup_irq(struct pci_controller *hose, pci_dev_t dev)
229 int8 bus, device, func, pin, line;
233 device = PCI_DEV(dev);
234 func = PCI_FUNC(dev);
236 PRINTF("Fixup irq of %d:%d.%d\n", bus, device, func);
238 /* Search for the device in the table */
239 for (i = 0; fixuptab[i].bus != 0xff; i++)
241 if (bus == fixuptab[i].bus && device == fixuptab[i].device && func == fixuptab[i].func)
243 /* If the device needs an interrupt, write it */
244 if (fixuptab[i].interrupt != 0xff)
246 PRINTF("Assigning IRQ %d (fixed)\n", fixuptab[i].interrupt);
247 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, fixuptab[i].interrupt);
251 /* Otherwise, see if it wants an interrupt, and disable it if needed */
252 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
255 PRINTF("Disabling IRQ\n");
256 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 0xff);
264 /* If we get here, we have another PCI device in a slot... find the appropriate IRQ */
266 /* Find matching pin */
267 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
270 /* Search for it's map */
271 for (i = 0; amigaone_pci_routing[i].bus != 0xff; i++)
273 if (bus == amigaone_pci_routing[i].bus && device == amigaone_pci_routing[i].device)
275 line = pci_intmap[amigaone_pci_routing[i].ints[pin]];
276 PRINTF("Assigning IRQ %d (pin %d)\n", line, pin);
277 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, line);
282 PRINTF("Unkonwn PCI device found\n");
285 void articiaS_pci_init (void)
290 PRINTF("atriciaS_pci_init\n");
292 /* Why aren't these relocated?? */
293 for (i=0; config_table[i].config_device; i++)
295 switch((int)config_table[i].config_device)
297 case cfgfunc_via686: config_table[i].config_device = via_cfgfunc_via686; break;
298 case cfgfunc_dummy: config_table[i].config_device = pci_cfgfunc_dummy; break;
299 case cfgfunc_ide_init: config_table[i].config_device = via_cfgfunc_ide_init; break;
300 default: PRINTF("Error: Unknown constant\n");
304 articiaS_hose.first_busno = 0;
305 articiaS_hose.last_busno = 0xff;
306 articiaS_hose.config_table = config_table;
307 articiaS_hose.fixup_irq = articiaS_pci_fixup_irq;
309 articiaS_pci_irq_init();
312 pci_set_region(articiaS_hose.regions + 0,
315 ARTICIAS_SYS_MAXSIZE,
316 PCI_REGION_MEM | PCI_REGION_MEMORY);
318 /* PCI memory space */
319 pci_set_region(articiaS_hose.regions + 1,
322 ARTICIAS_PCI_MAXSIZE,
326 pci_set_region(articiaS_hose.regions + 2,
329 ARTICIAS_PCIIO_MAXSIZE,
332 /* PCI/ISA io space */
333 pci_set_region(articiaS_hose.regions + 3,
336 ARTICIAS_ISAIO_MAXSIZE,
340 articiaS_hose.region_count = 4;
342 pci_setup_indirect(&articiaS_hose, ARTICIAS_PCI_CFGADDR, ARTICIAS_PCI_CFGDATA);
343 PRINTF("Registering articia hose...\n");
344 pci_register_hose(&articiaS_hose);
345 PRINTF("Enabling AGP...\n");
346 pci_write_config_byte(PCI_BDF(0,0,0), 0x58, 0x01);
347 PRINTF("Scanning bus...\n");
348 articiaS_hose.last_busno = pci_hose_scan(&articiaS_hose);
350 via_init_irq_routing(pci_intmap);
352 PRINTF("After-Scan results:\n");
353 PRINTF("Bus range: %d - %d\n", articiaS_hose.first_busno , articiaS_hose.last_busno);
355 via_init_afterscan();
357 pci_write_config_byte(PCI_BDF(0,1,0), PCI_INTERRUPT_LINE, 0xFF);
359 s = getenv("as_irq");
362 pci_write_config_byte(PCI_BDF(0,0,0), PCI_INTERRUPT_LINE, simple_strtoul (s, NULL, 10));
365 s = getenv("x86_run_bios");
366 if (!s || (s && strcmp(s, "on")==0))
368 if (articiaS_init_vga() == -1)
370 /* If the VGA didn't init and we have stdout set to VGA, reset to serial */
371 /* s = getenv("stdout"); */
372 /* if (s && strcmp(s, "vga") == 0) */
374 /* setenv("stdout", "serial"); */
378 pci_write_config_byte(PCI_BDF(0,1,0), PCI_INTERRUPT_LINE, 0xFF);
382 pci_dev_t pci_hose_find_class(struct pci_controller *hose, int bus, short find_class, int index)
384 unsigned int sub_bus, found_multi=0;
385 unsigned short vendor, class;
386 unsigned char header_type;
392 for (dev = PCI_BDF(bus,0,0);
393 dev < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
394 dev += PCI_BDF(0,0,1))
396 if ( dev == PCI_BDF(hose->first_busno,0,0) )
399 if (PCI_FUNC(dev) && !found_multi)
402 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
404 pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
406 if (vendor != 0xffff && vendor != 0x0000)
410 found_multi = header_type & 0x80;
411 pci_hose_read_config_byte(hose, dev, 0x0B, &c1);
412 pci_hose_read_config_byte(hose, dev, 0x0A, &c2);
414 /*printf("At %02x:%02x:%02x: class %x\n", */
415 /* PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev), class); */
416 if (class == find_class)
430 * For a given bus number, find the bridge on this hose that provides this
431 * bus number. The function scans for bridges and peeks config space offset
432 * 0x19 (PCI_SECONDARY_BUS).
434 pci_dev_t pci_find_bridge_for_bus(struct pci_controller *hose, int busnr)
438 unsigned int found_multi=0;
439 unsigned char header_type;
440 unsigned short vendor;
441 unsigned char secondary_bus;
443 if (hose == NULL) hose = &articiaS_hose;
445 if (busnr < hose->first_busno || busnr > hose->last_busno) return PCI_ANY_ID; /* Not in range */
448 * The bridge must be on a lower bus number
450 for (bus = hose->first_busno; bus < busnr; bus++)
452 for (dev = PCI_BDF(bus,0,0);
453 dev < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
454 dev += PCI_BDF(0,0,1))
456 if ( dev == PCI_BDF(hose->first_busno,0,0) )
459 if (PCI_FUNC(dev) && !found_multi)
462 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
464 pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
466 if (vendor != 0xffff && vendor != 0x0000)
470 found_multi = header_type & 0x80;
471 if (header_type == 1) /* Bridge device header */
473 pci_hose_read_config_byte(hose, dev, PCI_SECONDARY_BUS, &secondary_bus);
474 if ((int)secondary_bus == busnr) return dev;
483 static short classes[] =
485 PCI_CLASS_DISPLAY_VGA,
486 PCI_CLASS_DISPLAY_XGA,
487 PCI_CLASS_DISPLAY_3D,
488 PCI_CLASS_DISPLAY_OTHER,
492 extern int execute_bios(pci_dev_t gr_dev, void *);
496 int articiaS_init_vga (void)
498 extern void shutdown_bios(void);
503 video_dev = PCI_ANY_ID;
507 PRINTF("Trying to initialize x86 VGA Card(s)\n");
511 PRINTF("Searching for class 0x%x on bus %d\n", classes[classnr], busnr);
512 /* Find the first of this class on this bus */
513 dev = pci_hose_find_class(&articiaS_hose, busnr, classes[classnr], 0);
516 PRINTF("Found VGA Card at %02x:%02x:%02x\n", PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
520 if (busnr > articiaS_hose.last_busno)
524 if (classes[classnr] == ~0)
526 printf("NOT PRESENT\n");
533 * If we get here we have found the first graphics card.
534 * If the bus number is not 0, then it is probably behind a bridge, and the
535 * bridge needs to be told to forward VGA access.
538 if (PCI_BUS(dev) != 0)
541 PRINTF("Behind bridge, looking for bridge\n");
542 bridge = pci_find_bridge_for_bus(&articiaS_hose, PCI_BUS(dev));
543 if (dev != PCI_ANY_ID)
545 unsigned char agp_control_0;
546 PRINTF("Got the bridge at %02x:%02x:%02x\n",
547 PCI_BUS(bridge), PCI_DEV(bridge), PCI_FUNC(bridge));
548 pci_hose_read_config_byte(&articiaS_hose, bridge, 0x3E, &agp_control_0);
549 agp_control_0 |= 0x18;
550 pci_hose_write_config_byte(&articiaS_hose, bridge, 0x3E, agp_control_0);
551 PRINTF("Configured for VGA forwarding\n");
556 * Now try to run the bios
558 PRINTF("Trying to run bios now\n");
559 if (execute_bios(dev, gd->relocaddr))
569 PRINTF("Done scanning.\n");
573 if (dev == PCI_ANY_ID) return -1;