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 #undef ARTICIA_PCI_DEBUG
31 #ifdef ARTICIA_PCI_DEBUG
32 #define PRINTF(fmt,args...) printf (fmt ,##args)
34 #define PRINTF(fmt,args...)
37 struct pci_controller articiaS_hose;
39 long irq_alloc(long wanted);
41 static pci_dev_t pci_hose_find_class(struct pci_controller *hose, int bus, short find_class, int index);
42 static int articiaS_init_vga(void);
43 static void pci_cfgfunc_dummy(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table);
44 unsigned char pci_irq_alloc(void);
46 extern void via_cfgfunc_via686(struct pci_controller * host, pci_dev_t dev, struct pci_config_table *table);
47 extern void via_cfgfunc_ide_init(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table);
48 extern void via_init_irq_routing(uint8 []);
49 extern void via_init_afterscan(void);
51 #define cfgfunc_via686 1
52 #define cfgfunc_dummy 2
53 #define cfgfunc_ide_init 3
55 static struct pci_config_table config_table[] =
58 0x1106, PCI_ANY_ID, PCI_CLASS_BRIDGE_ISA, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
59 (void *)cfgfunc_via686, {0, 0, 0}
62 0x1106, PCI_ANY_ID, PCI_ANY_ID, 0,7,4,
63 (void *)cfgfunc_dummy, {0,0,0}
66 0x1106, 0x3068, PCI_ANY_ID, 0, 7, PCI_ANY_ID,
67 (void *)cfgfunc_dummy, {0,0,0}
70 0x1106, PCI_ANY_ID, PCI_ANY_ID, 0,7,1,
71 (void *)cfgfunc_ide_init, {0,0,0}
79 void pci_cfgfunc_dummy(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table)
85 unsigned long irq_penalties[16] =
88 1000, /* 1:keyboard */
90 50, /* 3:serial (COM2) */
91 50, /* 4:serial (COM1) */
107 * The following defines a hard-coded interrupt mapping for the
108 * know devices on the board.
109 * If a device isn't found here, assumed to be a device that's
110 * plugged into a PCI or AGP slot
111 * NOTE: This table is machine dependant.
114 struct pci_irq_fixup_table
116 uint8 bus; /* Bus number */
117 uint8 device; /* Device number */
118 uint8 func; /* Function number */
119 uint8 interrupt; /* Interrupt to use (0xff to disable) */
122 struct pci_irq_fixup_table fixuptab [] =
124 { 0, 0, 0, 0xff}, /* Articia S host bridge */
125 { 0, 1, 0, 0xff}, /* Articia S AGP bridge */
126 /* { 0, 6, 0, 0x05}, /###* 3COM ethernet */
127 { 0, 7, 0, 0xff}, /* VIA southbridge */
128 { 0, 7, 1, 0x0e}, /* IDE controller in legacy mode */
129 /* { 0, 7, 2, 0x05}, /###* First USB controller */
130 /* { 0, 7, 3, 0x0c}, /###* Second USB controller (shares interrupt with ethernet) */
131 { 0, 7, 4, 0xff}, /* ACPI Power Management */
132 /* { 0, 7, 5, 0x08}, /###* AC97 */
133 /* { 0, 7, 6, 0x08}, /###* MC97 */
134 { 0xff, 0xff, 0xff, 0xff}
139 * This table maps IRQ's to PCI interrupts
142 uint8 pci_intmap[4] = {0, 0, 0, 0};
145 * Map PCI slots to interrupt routings
146 * This table lists the device number assigned to a card inserted
147 * into the slot, along with a permutation for the slot's IRQ routing.
148 * NOTE: This table is machine dependant.
151 struct pci_slot_irq_routing
159 struct pci_slot_irq_routing amigaone_pci_routing[] =
161 {0, 8, {0, 1, 2, 3}}, /* Slot 1 (left of riser slot) */
162 {0, 9, {1, 2, 3, 0}}, /* Slot 2 (middle slot) */
163 {0, 10, {2, 3, 0, 1}}, /* Slot 3 (leftmost slot) */
164 {1, 0, {1, 0, 2, 3}}, /* AGP slot (only IRQA and IRQB) */
165 {1, 1, {1, 2, 3, 0}}, /* PCI slot on AGP bus */
166 {0, 6, {3, 3, 3, 3}}, /* On board ethernet */
167 {0, 7, {0, 1, 2, 3}}, /* Southbridge */
168 {0xff, 0, {0, 0, 0, 0}}
171 void articiaS_pci_irq_init(void)
175 s = getenv("pci_irqa");
177 pci_intmap[0] = simple_strtoul (s, NULL, 10);
179 pci_intmap[0] = pci_irq_alloc();
181 s = getenv("pci_irqb");
183 pci_intmap[1] = simple_strtoul (s, NULL, 10);
185 pci_intmap[1] = pci_irq_alloc();
187 s = getenv("pci_irqc");
189 pci_intmap[2] = simple_strtoul (s, NULL, 10);
191 pci_intmap[2] = pci_irq_alloc();
193 s = getenv("pci_irqd");
195 pci_intmap[3] = simple_strtoul (s, NULL, 10);
197 pci_intmap[3] = pci_irq_alloc();
201 unsigned char pci_irq_alloc(void)
205 unsigned long min_penalty = 1000;
207 /* Search for the minimal penalty, favoring interrupts at the end */
208 for (i = 0; i < 16; i++)
210 if (irq_penalties[i] <= min_penalty)
213 min_penalty = irq_penalties[i];
217 PRINTF("pci_irq_alloc: Minimal penalty is %ld for %d\n", min_penalty, interrupt);
219 irq_penalties[interrupt]++;
225 void articiaS_pci_fixup_irq(struct pci_controller *hose, pci_dev_t dev)
227 int8 bus, device, func, pin, line;
231 device = PCI_DEV(dev);
232 func = PCI_FUNC(dev);
234 PRINTF("Fixup irq of %d:%d.%d\n", bus, device, func);
236 /* Search for the device in the table */
237 for (i = 0; fixuptab[i].bus != 0xff; i++)
239 if (bus == fixuptab[i].bus && device == fixuptab[i].device && func == fixuptab[i].func)
241 /* If the device needs an interrupt, write it */
242 if (fixuptab[i].interrupt != 0xff)
244 PRINTF("Assigning IRQ %d (fixed)\n", fixuptab[i].interrupt);
245 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, fixuptab[i].interrupt);
249 /* Otherwise, see if it wants an interrupt, and disable it if needed */
250 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
253 PRINTF("Disabling IRQ\n");
254 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 0xff);
262 /* If we get here, we have another PCI device in a slot... find the appropriate IRQ */
264 /* Find matching pin */
265 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
268 /* Search for it's map */
269 for (i = 0; amigaone_pci_routing[i].bus != 0xff; i++)
271 if (bus == amigaone_pci_routing[i].bus && device == amigaone_pci_routing[i].device)
273 line = pci_intmap[amigaone_pci_routing[i].ints[pin]];
274 PRINTF("Assigning IRQ %d (pin %d)\n", line, pin);
275 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, line);
280 PRINTF("Unkonwn PCI device found\n");
283 void articiaS_pci_init (void)
288 PRINTF("atriciaS_pci_init\n");
290 /* Why aren't these relocated?? */
291 for (i=0; config_table[i].config_device; i++)
293 switch((int)config_table[i].config_device)
295 case cfgfunc_via686: config_table[i].config_device = via_cfgfunc_via686; break;
296 case cfgfunc_dummy: config_table[i].config_device = pci_cfgfunc_dummy; break;
297 case cfgfunc_ide_init: config_table[i].config_device = via_cfgfunc_ide_init; break;
298 default: PRINTF("Error: Unknown constant\n");
302 articiaS_hose.first_busno = 0;
303 articiaS_hose.last_busno = 0xff;
304 articiaS_hose.config_table = config_table;
305 articiaS_hose.fixup_irq = articiaS_pci_fixup_irq;
307 articiaS_pci_irq_init();
310 pci_set_region(articiaS_hose.regions + 0,
313 ARTICIAS_SYS_MAXSIZE,
314 PCI_REGION_MEM | PCI_REGION_MEMORY);
316 /* PCI memory space */
317 pci_set_region(articiaS_hose.regions + 1,
320 ARTICIAS_PCI_MAXSIZE,
324 pci_set_region(articiaS_hose.regions + 2,
327 ARTICIAS_PCIIO_MAXSIZE,
330 /* PCI/ISA io space */
331 pci_set_region(articiaS_hose.regions + 3,
334 ARTICIAS_ISAIO_MAXSIZE,
338 articiaS_hose.region_count = 4;
340 pci_setup_indirect(&articiaS_hose, ARTICIAS_PCI_CFGADDR, ARTICIAS_PCI_CFGDATA);
341 PRINTF("Registering articia hose...\n");
342 pci_register_hose(&articiaS_hose);
343 PRINTF("Enabling AGP...\n");
344 pci_write_config_byte(PCI_BDF(0,0,0), 0x58, 0x01);
345 PRINTF("Scanning bus...\n");
346 articiaS_hose.last_busno = pci_hose_scan(&articiaS_hose);
348 via_init_irq_routing(pci_intmap);
350 PRINTF("After-Scan results:\n");
351 PRINTF("Bus range: %d - %d\n", articiaS_hose.first_busno , articiaS_hose.last_busno);
353 via_init_afterscan();
355 pci_write_config_byte(PCI_BDF(0,1,0), PCI_INTERRUPT_LINE, 0xFF);
357 s = getenv("as_irq");
360 pci_write_config_byte(PCI_BDF(0,0,0), PCI_INTERRUPT_LINE, simple_strtoul (s, NULL, 10));
363 s = getenv("x86_run_bios");
364 if (!s || (s && strcmp(s, "on")==0))
366 if (articiaS_init_vga() == -1)
368 /* If the VGA didn't init and we have stdout set to VGA, reset to serial */
369 /* s = getenv("stdout"); */
370 /* if (s && strcmp(s, "vga") == 0) */
372 /* setenv("stdout", "serial"); */
376 pci_write_config_byte(PCI_BDF(0,1,0), PCI_INTERRUPT_LINE, 0xFF);
380 pci_dev_t pci_hose_find_class(struct pci_controller *hose, int bus, short find_class, int index)
382 unsigned int sub_bus, found_multi=0;
383 unsigned short vendor, class;
384 unsigned char header_type;
390 for (dev = PCI_BDF(bus,0,0);
391 dev < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
392 dev += PCI_BDF(0,0,1))
394 if ( dev == PCI_BDF(hose->first_busno,0,0) )
397 if (PCI_FUNC(dev) && !found_multi)
400 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
402 pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
404 if (vendor != 0xffff && vendor != 0x0000)
408 found_multi = header_type & 0x80;
409 pci_hose_read_config_byte(hose, dev, 0x0B, &c1);
410 pci_hose_read_config_byte(hose, dev, 0x0A, &c2);
412 /*printf("At %02x:%02x:%02x: class %x\n", */
413 /* PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev), class); */
414 if (class == find_class)
428 * For a given bus number, find the bridge on this hose that provides this
429 * bus number. The function scans for bridges and peeks config space offset
430 * 0x19 (PCI_SECONDARY_BUS).
432 pci_dev_t pci_find_bridge_for_bus(struct pci_controller *hose, int busnr)
436 unsigned int found_multi=0;
437 unsigned char header_type;
438 unsigned short vendor;
439 unsigned char secondary_bus;
441 if (hose == NULL) hose = &articiaS_hose;
443 if (busnr < hose->first_busno || busnr > hose->last_busno) return PCI_ANY_ID; /* Not in range */
446 * The bridge must be on a lower bus number
448 for (bus = hose->first_busno; bus < busnr; bus++)
450 for (dev = PCI_BDF(bus,0,0);
451 dev < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
452 dev += PCI_BDF(0,0,1))
454 if ( dev == PCI_BDF(hose->first_busno,0,0) )
457 if (PCI_FUNC(dev) && !found_multi)
460 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
462 pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
464 if (vendor != 0xffff && vendor != 0x0000)
468 found_multi = header_type & 0x80;
469 if (header_type == 1) /* Bridge device header */
471 pci_hose_read_config_byte(hose, dev, PCI_SECONDARY_BUS, &secondary_bus);
472 if ((int)secondary_bus == busnr) return dev;
481 static short classes[] =
483 PCI_CLASS_DISPLAY_VGA,
484 PCI_CLASS_DISPLAY_XGA,
485 PCI_CLASS_DISPLAY_3D,
486 PCI_CLASS_DISPLAY_OTHER,
490 extern int execute_bios(pci_dev_t gr_dev, void *);
494 int articiaS_init_vga (void)
496 DECLARE_GLOBAL_DATA_PTR;
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;