blob: d2e9f292e084d8dec2c55dbc418b782976489f58 [file] [log] [blame]
wdenkc7de8292002-11-19 11:04:11 +00001/*
2 * (C) Copyright 2002
3 * Hyperion Entertainment, Hans-JoergF@hyperion-entertainment.com
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <pci.h>
26#include "memio.h"
27#include "articiaS.h"
28
wdenk7c7a23b2002-12-07 00:20:59 +000029#undef ARTICIA_PCI_DEBUG
wdenkc7de8292002-11-19 11:04:11 +000030
31#ifdef ARTICIA_PCI_DEBUG
32#define PRINTF(fmt,args...) printf (fmt ,##args)
33#else
34#define PRINTF(fmt,args...)
35#endif
36
37struct pci_controller articiaS_hose;
38
39long irq_alloc(long wanted);
40
41static pci_dev_t pci_hose_find_class(struct pci_controller *hose, int bus, short find_class, int index);
42static int articiaS_init_vga(void);
43static void pci_cfgfunc_dummy(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table);
44unsigned char pci_irq_alloc(void);
45
46extern void via_cfgfunc_via686(struct pci_controller * host, pci_dev_t dev, struct pci_config_table *table);
47extern void via_cfgfunc_ide_init(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table);
48extern void via_init_irq_routing(uint8 []);
49extern void via_init_afterscan(void);
50
51#define cfgfunc_via686 1
52#define cfgfunc_dummy 2
53#define cfgfunc_ide_init 3
54
55static struct pci_config_table config_table[] =
56{
57 {
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}
60 },
61 {
62 0x1106, PCI_ANY_ID, PCI_ANY_ID, 0,7,4,
63 (void *)cfgfunc_dummy, {0,0,0}
64 },
65 {
66 0x1106, 0x3068, PCI_ANY_ID, 0, 7, PCI_ANY_ID,
67 (void *)cfgfunc_dummy, {0,0,0}
68 },
69 {
70 0x1106, PCI_ANY_ID, PCI_ANY_ID, 0,7,1,
71 (void *)cfgfunc_ide_init, {0,0,0}
72 },
73 {
74 0,
75 }
76};
77
78
79void pci_cfgfunc_dummy(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table)
80{
81
82
83}
84
85unsigned long irq_penalties[16] =
86{
87 1000, /* 0:timer */
88 1000, /* 1:keyboard */
89 1000, /* 2:cascade */
90 50, /* 3:serial (COM2) */
91 50, /* 4:serial (COM1) */
92 4, /* 5:USB2 */
93 100, /* 6:floppy */
94 3, /* 7:parallel */
95 50, /* 8:AC97/MC97 */
96 0, /* 9: */
97 3, /* 10:: */
98 0, /* 11: */
99 3, /* 12: USB1 */
100 0, /* 13: */
101 100, /* 14: ide0 */
102 100, /* 15: ide1 */
103};
104
105
106/*
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.
112 */
113
114struct pci_irq_fixup_table
115{
116 uint8 bus; /* Bus number */
117 uint8 device; /* Device number */
118 uint8 func; /* Function number */
119 uint8 interrupt; /* Interrupt to use (0xff to disable) */
120};
121
122struct pci_irq_fixup_table fixuptab [] =
123{
124 { 0, 0, 0, 0xff}, /* Articia S host bridge */
125 { 0, 1, 0, 0xff}, /* Articia S AGP bridge */
wdenk8bde7f72003-06-27 21:31:46 +0000126/* { 0, 6, 0, 0x05}, /###* 3COM ethernet */
wdenkc7de8292002-11-19 11:04:11 +0000127 { 0, 7, 0, 0xff}, /* VIA southbridge */
128 { 0, 7, 1, 0x0e}, /* IDE controller in legacy mode */
wdenk8bde7f72003-06-27 21:31:46 +0000129/* { 0, 7, 2, 0x05}, /###* First USB controller */
130/* { 0, 7, 3, 0x0c}, /###* Second USB controller (shares interrupt with ethernet) */
wdenkc7de8292002-11-19 11:04:11 +0000131 { 0, 7, 4, 0xff}, /* ACPI Power Management */
wdenk8bde7f72003-06-27 21:31:46 +0000132/* { 0, 7, 5, 0x08}, /###* AC97 */
133/* { 0, 7, 6, 0x08}, /###* MC97 */
wdenkc7de8292002-11-19 11:04:11 +0000134 { 0xff, 0xff, 0xff, 0xff}
135};
136
137
138/*
139 * This table maps IRQ's to PCI interrupts
140 */
141
142uint8 pci_intmap[4] = {0, 0, 0, 0};
143
144/*
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.
149 */
150
151struct pci_slot_irq_routing
152{
153 uint8 bus;
154 uint8 device;
155
156 uint8 ints[4];
157};
158
159struct pci_slot_irq_routing amigaone_pci_routing[] =
160{
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}}
169};
170
171void articiaS_pci_irq_init(void)
172{
173 char *s;
174
175 s = getenv("pci_irqa");
176 if (s)
177 pci_intmap[0] = simple_strtoul (s, NULL, 10);
178 else
179 pci_intmap[0] = pci_irq_alloc();
180
181 s = getenv("pci_irqb");
182 if (s)
183 pci_intmap[1] = simple_strtoul (s, NULL, 10);
184 else
185 pci_intmap[1] = pci_irq_alloc();
186
187 s = getenv("pci_irqc");
188 if (s)
189 pci_intmap[2] = simple_strtoul (s, NULL, 10);
190 else
191 pci_intmap[2] = pci_irq_alloc();
192
193 s = getenv("pci_irqd");
194 if (s)
195 pci_intmap[3] = simple_strtoul (s, NULL, 10);
196 else
197 pci_intmap[3] = pci_irq_alloc();
198}
199
200
201unsigned char pci_irq_alloc(void)
202{
203 int i;
204 int interrupt = 10;
205 unsigned long min_penalty = 1000;
206
207 /* Search for the minimal penalty, favoring interrupts at the end */
208 for (i = 0; i < 16; i++)
209 {
210 if (irq_penalties[i] <= min_penalty)
211 {
212 interrupt = i;
213 min_penalty = irq_penalties[i];
214 }
215 }
216
217 PRINTF("pci_irq_alloc: Minimal penalty is %ld for %d\n", min_penalty, interrupt);
218
219 irq_penalties[interrupt]++;
220
221 return interrupt;
222}
223
224
225void articiaS_pci_fixup_irq(struct pci_controller *hose, pci_dev_t dev)
226{
227 int8 bus, device, func, pin, line;
228 int i;
229
230 bus = PCI_BUS(dev);
231 device = PCI_DEV(dev);
232 func = PCI_FUNC(dev);
233
234 PRINTF("Fixup irq of %d:%d.%d\n", bus, device, func);
235
236 /* Search for the device in the table */
237 for (i = 0; fixuptab[i].bus != 0xff; i++)
238 {
239 if (bus == fixuptab[i].bus && device == fixuptab[i].device && func == fixuptab[i].func)
240 {
241 /* If the device needs an interrupt, write it */
242 if (fixuptab[i].interrupt != 0xff)
243 {
244 PRINTF("Assigning IRQ %d (fixed)\n", fixuptab[i].interrupt);
245 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, fixuptab[i].interrupt);
246 }
247 else
248 {
249 /* Otherwise, see if it wants an interrupt, and disable it if needed */
250 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
251 if (pin)
252 {
253 PRINTF("Disabling IRQ\n");
254 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 0xff);
255 }
256 }
257
258 return;
259 }
260 }
261
262 /* If we get here, we have another PCI device in a slot... find the appropriate IRQ */
263
264 /* Find matching pin */
265 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
266 pin--;
267
268 /* Search for it's map */
269 for (i = 0; amigaone_pci_routing[i].bus != 0xff; i++)
270 {
271 if (bus == amigaone_pci_routing[i].bus && device == amigaone_pci_routing[i].device)
272 {
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);
276 return;
277 }
278 }
279
280 PRINTF("Unkonwn PCI device found\n");
281}
282
283void articiaS_pci_init (void)
284{
285 int i;
286 char *s;
287
288 PRINTF("atriciaS_pci_init\n");
289
wdenk8bde7f72003-06-27 21:31:46 +0000290 /* Why aren't these relocated?? */
wdenkc7de8292002-11-19 11:04:11 +0000291 for (i=0; config_table[i].config_device; i++)
292 {
293 switch((int)config_table[i].config_device)
294 {
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");
299 }
300 }
301
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;
306
307 articiaS_pci_irq_init();
308
309 /* System memory */
310 pci_set_region(articiaS_hose.regions + 0,
311 ARTICIAS_SYS_BUS,
312 ARTICIAS_SYS_PHYS,
313 ARTICIAS_SYS_MAXSIZE,
314 PCI_REGION_MEM | PCI_REGION_MEMORY);
315
316 /* PCI memory space */
317 pci_set_region(articiaS_hose.regions + 1,
318 ARTICIAS_PCI_BUS,
319 ARTICIAS_PCI_PHYS,
320 ARTICIAS_PCI_MAXSIZE,
321 PCI_REGION_MEM);
322
323 /* PCI io space */
324 pci_set_region(articiaS_hose.regions + 2,
325 ARTICIAS_PCIIO_BUS,
326 ARTICIAS_PCIIO_PHYS,
327 ARTICIAS_PCIIO_MAXSIZE,
328 PCI_REGION_IO);
329
330 /* PCI/ISA io space */
331 pci_set_region(articiaS_hose.regions + 3,
332 ARTICIAS_ISAIO_BUS,
333 ARTICIAS_ISAIO_PHYS,
334 ARTICIAS_ISAIO_MAXSIZE,
335 PCI_REGION_IO);
336
337
wdenkc7de8292002-11-19 11:04:11 +0000338 articiaS_hose.region_count = 4;
339
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);
347
348 via_init_irq_routing(pci_intmap);
349
350 PRINTF("After-Scan results:\n");
351 PRINTF("Bus range: %d - %d\n", articiaS_hose.first_busno , articiaS_hose.last_busno);
352
353 via_init_afterscan();
354
355 pci_write_config_byte(PCI_BDF(0,1,0), PCI_INTERRUPT_LINE, 0xFF);
356
357 s = getenv("as_irq");
358 if (s)
359 {
360 pci_write_config_byte(PCI_BDF(0,0,0), PCI_INTERRUPT_LINE, simple_strtoul (s, NULL, 10));
361 }
362
363 s = getenv("x86_run_bios");
364 if (!s || (s && strcmp(s, "on")==0))
365 {
366 if (articiaS_init_vga() == -1)
367 {
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) */
371/* { */
372/* setenv("stdout", "serial"); */
373/* } */
374 }
375 }
376 pci_write_config_byte(PCI_BDF(0,1,0), PCI_INTERRUPT_LINE, 0xFF);
377
378}
379
380pci_dev_t pci_hose_find_class(struct pci_controller *hose, int bus, short find_class, int index)
381{
382 unsigned int sub_bus, found_multi=0;
383 unsigned short vendor, class;
384 unsigned char header_type;
385 pci_dev_t dev;
386 u8 c1, c2;
387
388 sub_bus = bus;
389
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))
393 {
394 if ( dev == PCI_BDF(hose->first_busno,0,0) )
395 continue;
396
397 if (PCI_FUNC(dev) && !found_multi)
398 continue;
399
400 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
401
402 pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
403
404 if (vendor != 0xffff && vendor != 0x0000)
405 {
406
407 if (!PCI_FUNC(dev))
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);
411 class = c1<<8 | c2;
wdenk8bde7f72003-06-27 21:31:46 +0000412 /*printf("At %02x:%02x:%02x: class %x\n", */
413 /* PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev), class); */
wdenkc7de8292002-11-19 11:04:11 +0000414 if (class == find_class)
415 {
416 if (index == 0)
417 return dev;
418 else index--;
419 }
420 }
421 }
422
423 return ~0;
424}
425
426
427/*
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).
431 */
432pci_dev_t pci_find_bridge_for_bus(struct pci_controller *hose, int busnr)
433{
434 pci_dev_t dev;
435 int bus;
436 unsigned int found_multi=0;
437 unsigned char header_type;
438 unsigned short vendor;
439 unsigned char secondary_bus;
440
441 if (hose == NULL) hose = &articiaS_hose;
442
wdenk8bde7f72003-06-27 21:31:46 +0000443 if (busnr < hose->first_busno || busnr > hose->last_busno) return PCI_ANY_ID; /* Not in range */
wdenkc7de8292002-11-19 11:04:11 +0000444
445 /*
446 * The bridge must be on a lower bus number
447 */
448 for (bus = hose->first_busno; bus < busnr; bus++)
449 {
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))
453 {
454 if ( dev == PCI_BDF(hose->first_busno,0,0) )
455 continue;
456
457 if (PCI_FUNC(dev) && !found_multi)
458 continue;
459
460 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
461
462 pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
463
464 if (vendor != 0xffff && vendor != 0x0000)
465 {
466
467 if (!PCI_FUNC(dev))
468 found_multi = header_type & 0x80;
wdenk8bde7f72003-06-27 21:31:46 +0000469 if (header_type == 1) /* Bridge device header */
wdenkc7de8292002-11-19 11:04:11 +0000470 {
471 pci_hose_read_config_byte(hose, dev, PCI_SECONDARY_BUS, &secondary_bus);
472 if ((int)secondary_bus == busnr) return dev;
473 }
474
475 }
476 }
477 }
478 return PCI_ANY_ID;
479}
480
481static short classes[] =
482{
483 PCI_CLASS_DISPLAY_VGA,
484 PCI_CLASS_DISPLAY_XGA,
485 PCI_CLASS_DISPLAY_3D,
486 PCI_CLASS_DISPLAY_OTHER,
487 ~0
488};
489
490extern int execute_bios(pci_dev_t gr_dev, void *);
491
492pci_dev_t video_dev;
493
494int articiaS_init_vga (void)
495{
496 DECLARE_GLOBAL_DATA_PTR;
497
498 extern void shutdown_bios(void);
499 pci_dev_t dev = ~0;
500 int busnr = 0;
501 int classnr = 0;
502
503 video_dev = PCI_ANY_ID;
504
505 printf("VGA: ");
506
507 PRINTF("Trying to initialize x86 VGA Card(s)\n");
508
509 while (dev == ~0)
510 {
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);
wdenk8bde7f72003-06-27 21:31:46 +0000514 if (dev != ~0)
wdenk7c7a23b2002-12-07 00:20:59 +0000515 {
516 PRINTF("Found VGA Card at %02x:%02x:%02x\n", PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
517 break;
518 }
wdenkc7de8292002-11-19 11:04:11 +0000519 busnr++;
520 if (busnr > articiaS_hose.last_busno)
521 {
522 busnr = 0;
523 classnr ++;
524 if (classes[classnr] == ~0)
525 {
526 printf("NOT PRESENT\n");
527 return -1;
528 }
529 }
530 }
531
532 /*
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.
536 */
537
538 if (PCI_BUS(dev) != 0)
539 {
540 pci_dev_t bridge;
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)
544 {
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");
552 }
553 }
554
555 /*
556 * Now try to run the bios
557 */
wdenk7c7a23b2002-12-07 00:20:59 +0000558 PRINTF("Trying to run bios now\n");
wdenkc7de8292002-11-19 11:04:11 +0000559 if (execute_bios(dev, gd->relocaddr))
560 {
561 printf("OK\n");
562 video_dev = dev;
563 }
564 else
565 {
566 printf("ERROR\n");
567 }
568
569 PRINTF("Done scanning.\n");
570
571 shutdown_bios();
572
573 if (dev == PCI_ANY_ID) return -1;
574 else return 0;
575
576}