blob: 371f67f41d9b22afa1a030f543901b024c368551 [file] [log] [blame]
wdenk452cfd62002-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
Wolfgang Denk6405a152006-03-31 18:32:53 +020029DECLARE_GLOBAL_DATA_PTR;
30
wdenkbb444c92002-12-07 00:20:59 +000031#undef ARTICIA_PCI_DEBUG
wdenk452cfd62002-11-19 11:04:11 +000032
33#ifdef ARTICIA_PCI_DEBUG
34#define PRINTF(fmt,args...) printf (fmt ,##args)
35#else
36#define PRINTF(fmt,args...)
37#endif
38
39struct pci_controller articiaS_hose;
40
41long irq_alloc(long wanted);
42
43static pci_dev_t pci_hose_find_class(struct pci_controller *hose, int bus, short find_class, int index);
44static int articiaS_init_vga(void);
45static void pci_cfgfunc_dummy(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table);
46unsigned char pci_irq_alloc(void);
47
48extern void via_cfgfunc_via686(struct pci_controller * host, pci_dev_t dev, struct pci_config_table *table);
49extern void via_cfgfunc_ide_init(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table);
50extern void via_init_irq_routing(uint8 []);
51extern void via_init_afterscan(void);
52
53#define cfgfunc_via686 1
54#define cfgfunc_dummy 2
55#define cfgfunc_ide_init 3
56
57static struct pci_config_table config_table[] =
58{
59 {
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}
62 },
63 {
64 0x1106, PCI_ANY_ID, PCI_ANY_ID, 0,7,4,
65 (void *)cfgfunc_dummy, {0,0,0}
66 },
67 {
68 0x1106, 0x3068, PCI_ANY_ID, 0, 7, PCI_ANY_ID,
69 (void *)cfgfunc_dummy, {0,0,0}
70 },
71 {
72 0x1106, PCI_ANY_ID, PCI_ANY_ID, 0,7,1,
73 (void *)cfgfunc_ide_init, {0,0,0}
74 },
75 {
76 0,
77 }
78};
79
80
81void pci_cfgfunc_dummy(struct pci_controller *host, pci_dev_t dev, struct pci_config_table *table)
82{
83
84
85}
86
87unsigned long irq_penalties[16] =
88{
89 1000, /* 0:timer */
90 1000, /* 1:keyboard */
91 1000, /* 2:cascade */
92 50, /* 3:serial (COM2) */
93 50, /* 4:serial (COM1) */
94 4, /* 5:USB2 */
95 100, /* 6:floppy */
96 3, /* 7:parallel */
97 50, /* 8:AC97/MC97 */
98 0, /* 9: */
99 3, /* 10:: */
100 0, /* 11: */
101 3, /* 12: USB1 */
102 0, /* 13: */
103 100, /* 14: ide0 */
104 100, /* 15: ide1 */
105};
106
107
108/*
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.
114 */
115
116struct pci_irq_fixup_table
117{
118 uint8 bus; /* Bus number */
119 uint8 device; /* Device number */
120 uint8 func; /* Function number */
121 uint8 interrupt; /* Interrupt to use (0xff to disable) */
122};
123
124struct pci_irq_fixup_table fixuptab [] =
125{
126 { 0, 0, 0, 0xff}, /* Articia S host bridge */
127 { 0, 1, 0, 0xff}, /* Articia S AGP bridge */
wdenk57b2d802003-06-27 21:31:46 +0000128/* { 0, 6, 0, 0x05}, /###* 3COM ethernet */
wdenk452cfd62002-11-19 11:04:11 +0000129 { 0, 7, 0, 0xff}, /* VIA southbridge */
130 { 0, 7, 1, 0x0e}, /* IDE controller in legacy mode */
wdenk57b2d802003-06-27 21:31:46 +0000131/* { 0, 7, 2, 0x05}, /###* First USB controller */
132/* { 0, 7, 3, 0x0c}, /###* Second USB controller (shares interrupt with ethernet) */
wdenk452cfd62002-11-19 11:04:11 +0000133 { 0, 7, 4, 0xff}, /* ACPI Power Management */
wdenk57b2d802003-06-27 21:31:46 +0000134/* { 0, 7, 5, 0x08}, /###* AC97 */
135/* { 0, 7, 6, 0x08}, /###* MC97 */
wdenk452cfd62002-11-19 11:04:11 +0000136 { 0xff, 0xff, 0xff, 0xff}
137};
138
139
140/*
141 * This table maps IRQ's to PCI interrupts
142 */
143
144uint8 pci_intmap[4] = {0, 0, 0, 0};
145
146/*
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.
151 */
152
153struct pci_slot_irq_routing
154{
155 uint8 bus;
156 uint8 device;
157
158 uint8 ints[4];
159};
160
161struct pci_slot_irq_routing amigaone_pci_routing[] =
162{
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}}
171};
172
173void articiaS_pci_irq_init(void)
174{
175 char *s;
176
177 s = getenv("pci_irqa");
178 if (s)
179 pci_intmap[0] = simple_strtoul (s, NULL, 10);
180 else
181 pci_intmap[0] = pci_irq_alloc();
182
183 s = getenv("pci_irqb");
184 if (s)
185 pci_intmap[1] = simple_strtoul (s, NULL, 10);
186 else
187 pci_intmap[1] = pci_irq_alloc();
188
189 s = getenv("pci_irqc");
190 if (s)
191 pci_intmap[2] = simple_strtoul (s, NULL, 10);
192 else
193 pci_intmap[2] = pci_irq_alloc();
194
195 s = getenv("pci_irqd");
196 if (s)
197 pci_intmap[3] = simple_strtoul (s, NULL, 10);
198 else
199 pci_intmap[3] = pci_irq_alloc();
200}
201
202
203unsigned char pci_irq_alloc(void)
204{
205 int i;
206 int interrupt = 10;
207 unsigned long min_penalty = 1000;
208
209 /* Search for the minimal penalty, favoring interrupts at the end */
210 for (i = 0; i < 16; i++)
211 {
212 if (irq_penalties[i] <= min_penalty)
213 {
214 interrupt = i;
215 min_penalty = irq_penalties[i];
216 }
217 }
218
219 PRINTF("pci_irq_alloc: Minimal penalty is %ld for %d\n", min_penalty, interrupt);
220
221 irq_penalties[interrupt]++;
222
223 return interrupt;
224}
225
226
227void articiaS_pci_fixup_irq(struct pci_controller *hose, pci_dev_t dev)
228{
229 int8 bus, device, func, pin, line;
230 int i;
231
232 bus = PCI_BUS(dev);
233 device = PCI_DEV(dev);
234 func = PCI_FUNC(dev);
235
236 PRINTF("Fixup irq of %d:%d.%d\n", bus, device, func);
237
238 /* Search for the device in the table */
239 for (i = 0; fixuptab[i].bus != 0xff; i++)
240 {
241 if (bus == fixuptab[i].bus && device == fixuptab[i].device && func == fixuptab[i].func)
242 {
243 /* If the device needs an interrupt, write it */
244 if (fixuptab[i].interrupt != 0xff)
245 {
246 PRINTF("Assigning IRQ %d (fixed)\n", fixuptab[i].interrupt);
247 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, fixuptab[i].interrupt);
248 }
249 else
250 {
251 /* Otherwise, see if it wants an interrupt, and disable it if needed */
252 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
253 if (pin)
254 {
255 PRINTF("Disabling IRQ\n");
256 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 0xff);
257 }
258 }
259
260 return;
261 }
262 }
263
264 /* If we get here, we have another PCI device in a slot... find the appropriate IRQ */
265
266 /* Find matching pin */
267 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
268 pin--;
269
270 /* Search for it's map */
271 for (i = 0; amigaone_pci_routing[i].bus != 0xff; i++)
272 {
273 if (bus == amigaone_pci_routing[i].bus && device == amigaone_pci_routing[i].device)
274 {
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);
278 return;
279 }
280 }
281
282 PRINTF("Unkonwn PCI device found\n");
283}
284
285void articiaS_pci_init (void)
286{
287 int i;
288 char *s;
289
290 PRINTF("atriciaS_pci_init\n");
291
wdenk57b2d802003-06-27 21:31:46 +0000292 /* Why aren't these relocated?? */
wdenk452cfd62002-11-19 11:04:11 +0000293 for (i=0; config_table[i].config_device; i++)
294 {
295 switch((int)config_table[i].config_device)
296 {
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");
301 }
302 }
303
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;
308
309 articiaS_pci_irq_init();
310
311 /* System memory */
312 pci_set_region(articiaS_hose.regions + 0,
313 ARTICIAS_SYS_BUS,
314 ARTICIAS_SYS_PHYS,
315 ARTICIAS_SYS_MAXSIZE,
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600316 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
wdenk452cfd62002-11-19 11:04:11 +0000317
318 /* PCI memory space */
319 pci_set_region(articiaS_hose.regions + 1,
320 ARTICIAS_PCI_BUS,
321 ARTICIAS_PCI_PHYS,
322 ARTICIAS_PCI_MAXSIZE,
323 PCI_REGION_MEM);
324
325 /* PCI io space */
326 pci_set_region(articiaS_hose.regions + 2,
327 ARTICIAS_PCIIO_BUS,
328 ARTICIAS_PCIIO_PHYS,
329 ARTICIAS_PCIIO_MAXSIZE,
330 PCI_REGION_IO);
331
332 /* PCI/ISA io space */
333 pci_set_region(articiaS_hose.regions + 3,
334 ARTICIAS_ISAIO_BUS,
335 ARTICIAS_ISAIO_PHYS,
336 ARTICIAS_ISAIO_MAXSIZE,
337 PCI_REGION_IO);
338
339
wdenk452cfd62002-11-19 11:04:11 +0000340 articiaS_hose.region_count = 4;
341
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);
349
350 via_init_irq_routing(pci_intmap);
351
352 PRINTF("After-Scan results:\n");
353 PRINTF("Bus range: %d - %d\n", articiaS_hose.first_busno , articiaS_hose.last_busno);
354
355 via_init_afterscan();
356
357 pci_write_config_byte(PCI_BDF(0,1,0), PCI_INTERRUPT_LINE, 0xFF);
358
359 s = getenv("as_irq");
360 if (s)
361 {
362 pci_write_config_byte(PCI_BDF(0,0,0), PCI_INTERRUPT_LINE, simple_strtoul (s, NULL, 10));
363 }
364
365 s = getenv("x86_run_bios");
366 if (!s || (s && strcmp(s, "on")==0))
367 {
368 if (articiaS_init_vga() == -1)
369 {
370 /* If the VGA didn't init and we have stdout set to VGA, reset to serial */
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100371/* s = getenv("stdout"); */
372/* if (s && strcmp(s, "vga") == 0) */
373/* { */
374/* setenv("stdout", "serial"); */
375/* } */
wdenk452cfd62002-11-19 11:04:11 +0000376 }
377 }
378 pci_write_config_byte(PCI_BDF(0,1,0), PCI_INTERRUPT_LINE, 0xFF);
379
380}
381
382pci_dev_t pci_hose_find_class(struct pci_controller *hose, int bus, short find_class, int index)
383{
384 unsigned int sub_bus, found_multi=0;
385 unsigned short vendor, class;
386 unsigned char header_type;
387 pci_dev_t dev;
388 u8 c1, c2;
389
390 sub_bus = bus;
391
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))
395 {
396 if ( dev == PCI_BDF(hose->first_busno,0,0) )
397 continue;
398
399 if (PCI_FUNC(dev) && !found_multi)
400 continue;
401
402 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
403
404 pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
405
406 if (vendor != 0xffff && vendor != 0x0000)
407 {
408
409 if (!PCI_FUNC(dev))
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);
413 class = c1<<8 | c2;
wdenk57b2d802003-06-27 21:31:46 +0000414 /*printf("At %02x:%02x:%02x: class %x\n", */
415 /* PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev), class); */
wdenk452cfd62002-11-19 11:04:11 +0000416 if (class == find_class)
417 {
418 if (index == 0)
419 return dev;
420 else index--;
421 }
422 }
423 }
424
425 return ~0;
426}
427
428
429/*
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).
433 */
434pci_dev_t pci_find_bridge_for_bus(struct pci_controller *hose, int busnr)
435{
436 pci_dev_t dev;
437 int bus;
438 unsigned int found_multi=0;
439 unsigned char header_type;
440 unsigned short vendor;
441 unsigned char secondary_bus;
442
443 if (hose == NULL) hose = &articiaS_hose;
444
wdenk57b2d802003-06-27 21:31:46 +0000445 if (busnr < hose->first_busno || busnr > hose->last_busno) return PCI_ANY_ID; /* Not in range */
wdenk452cfd62002-11-19 11:04:11 +0000446
447 /*
448 * The bridge must be on a lower bus number
449 */
450 for (bus = hose->first_busno; bus < busnr; bus++)
451 {
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))
455 {
456 if ( dev == PCI_BDF(hose->first_busno,0,0) )
457 continue;
458
459 if (PCI_FUNC(dev) && !found_multi)
460 continue;
461
462 pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
463
464 pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
465
466 if (vendor != 0xffff && vendor != 0x0000)
467 {
468
469 if (!PCI_FUNC(dev))
470 found_multi = header_type & 0x80;
wdenk57b2d802003-06-27 21:31:46 +0000471 if (header_type == 1) /* Bridge device header */
wdenk452cfd62002-11-19 11:04:11 +0000472 {
473 pci_hose_read_config_byte(hose, dev, PCI_SECONDARY_BUS, &secondary_bus);
474 if ((int)secondary_bus == busnr) return dev;
475 }
476
477 }
478 }
479 }
480 return PCI_ANY_ID;
481}
482
483static short classes[] =
484{
485 PCI_CLASS_DISPLAY_VGA,
486 PCI_CLASS_DISPLAY_XGA,
487 PCI_CLASS_DISPLAY_3D,
488 PCI_CLASS_DISPLAY_OTHER,
489 ~0
490};
491
492extern int execute_bios(pci_dev_t gr_dev, void *);
493
494pci_dev_t video_dev;
495
496int articiaS_init_vga (void)
497{
wdenk452cfd62002-11-19 11:04:11 +0000498 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);
wdenk57b2d802003-06-27 21:31:46 +0000514 if (dev != ~0)
wdenkbb444c92002-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 }
wdenk452cfd62002-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 */
wdenkbb444c92002-12-07 00:20:59 +0000558 PRINTF("Trying to run bios now\n");
wdenk452cfd62002-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}