source: rtems/c/src/lib/libbsp/powerpc/shared/bootloader/pci.c @ 3a3e0b0e

4.104.114.84.95
Last change on this file since 3a3e0b0e was 3a3e0b0e, checked in by Joel Sherrill <joel.sherrill@…>, on 06/13/03 at 17:39:46

2003-06-13 Greg Menke <gregory.menke@…>

PR 405/bsps

  • bootloader/pci.c: Added support for configuring devices for pci busses > 0
  • pci/pci.c, pci/pci.h: Added FixupPCI() to store vectors in the INTERRUPT_LINE register of pci devices any # of hops away from the host processor.
  • motorola/motorola.c, motorola/motorola.h: Added interrupt routing tables in support of FixupPCI. This is board-specific, each board will have to supply information for FixupPCI() to do anything for it.
  • startup/bspstart.c: Extended bat2 to cover entire PCI address space.
  • irq/irq.c, irq/irq.h: Added support for shared interrupts. Existing single hander vectors are undisturbed, a new function added to allow adding/removing handlers from a vector.
  • Property mode set to 100644
File size: 39.5 KB
Line 
1/*
2 *  pci.c -- Crude pci handling for early boot.
3 *
4 *  Copyright (C) 1998, 1999 Gabriel Paubert, paubert@iram.es
5 *
6 *  Modified to compile in RTEMS development environment
7 *  by Eric Valette
8 *
9 *  Copyright (C) 1999 Eric Valette. valette@crf.canon.fr
10 *
11 *  The license and distribution terms for this file may be
12 *  found in found in the file LICENSE in this distribution or at
13 *  http://www.OARcorp.com/rtems/license.html.
14 *
15 * $Id$
16 */
17
18
19#include <sys/types.h>
20#include <rtems/bspIo.h>
21#include <libcpu/spr.h>
22#include "bootldr.h"
23#include "pci.h"
24#include <libcpu/io.h>
25#include <libcpu/page.h>
26#include <bsp/consoleIo.h>
27
28
29
30typedef unsigned int u32;
31
32/*#define DEBUG*/
33/* Used to reorganize PCI space on stupid machines which spread resources
34 * across a wide address space. This is bad when P2P bridges are present
35 * or when it limits the mappings that a resource hog like a PCI<->VME
36 * bridge can use.
37 */
38
39typedef struct _pci_resource {
40      struct _pci_resource *next;
41      struct pci_dev *dev;
42      u_long base;    /* will be 64 bits on 64 bits machines */
43      u_long size;
44      u_char type;      /* 1 is I/O else low order 4 bits of the memory type */
45      u_char reg;       /* Register # in conf space header */
46      u_short cmd;    /* Original cmd byte */
47} pci_resource;
48
49typedef struct _pci_area {
50      struct _pci_area *next;
51      u_long start;
52      u_long end;
53      struct pci_bus *bus;
54      u_int flags;
55} pci_area;
56
57typedef struct _pci_area_head {
58      pci_area *head;
59      u_long mask;
60      int high; /* To allocate from top */
61} pci_area_head;
62
63#define PCI_AREA_PREFETCHABLE 0
64#define PCI_AREA_MEMORY 1
65#define PCI_AREA_IO 2
66
67struct _pci_private {
68      volatile u_int * config_addr;
69      volatile u_char * config_data;
70      struct pci_dev **last_dev_p;
71      struct pci_bus pci_root;
72      pci_resource *resources;
73      pci_area_head io, mem;
74
75} pci_private = {
76   config_addr: NULL,
77   config_data: (volatile u_char *) 0x80800000,
78   last_dev_p: NULL,
79   resources: NULL,
80   io: {NULL, 0xfff, 0},
81   mem: {NULL, 0xfffff, 0}
82};
83
84#define pci ((struct _pci_private *)(bd->pci_private))
85#define pci_root pci->pci_root
86
87#if !defined(DEBUG)
88#undef PCI_DEBUG
89/*
90  #else
91  #define PCI_DEBUG
92*/
93#endif
94
95#if defined(PCI_DEBUG)
96static void
97print_pci_resources(const char *s) {
98   pci_resource *p;
99   printk("%s", s);
100   for (p=pci->resources; p; p=p->next) {
101/*
102      printk("  %p:%p %06x %08lx %08lx %d\n",
103             p, p->next,
104             (p->dev->devfn<<8)+(p->dev->bus->number<<16)
105             +0x10+p->reg*4,
106             p->base,
107             p->size,
108             p->type);
109*/
110
111      printk("  %p:%p %d:%02x (%04x:%04x) %08lx %08lx %d\n",
112             p, p->next,
113             p->dev->bus->number, PCI_SLOT(p->dev->devfn),
114             p->dev->vendor, p->dev->device,
115             p->base,
116             p->size,
117             p->type);
118
119   }
120}
121
122static void
123print_pci_area(pci_area *p) {
124   for (; p; p=p->next) {
125      printk("    %p:%p %p %08lx %08lx\n",
126             p, p->next, p->bus, p->start, p->end);
127   }
128}
129
130static void
131print_pci_areas(const char *s) {
132   printk("%s  PCI I/O areas:\n",s);
133   print_pci_area(pci->io.head);
134   printk("  PCI memory areas:\n");
135   print_pci_area(pci->mem.head);
136}
137#else
138#define print_pci_areas(x)
139#define print_pci_resources(x)
140#endif
141
142/* Maybe there are some devices who use a size different
143 * from the alignment. For now we assume both are the same.
144 * The blacklist might be used for other weird things in the future too,
145 * since weird non PCI complying devices seem to proliferate these days.
146 */
147
148struct blacklist_entry {
149      u_short vendor, device;
150      u_char reg;
151      u_long actual_size;
152};
153
154#define BLACKLIST(vid, did, breg, actual_size) \
155        {PCI_VENDOR_ID_##vid, PCI_DEVICE_ID_##vid##_##did, breg, actual_size}
156
157static struct blacklist_entry blacklist[] = {
158   BLACKLIST(S3, TRIO, 0, 0x04000000),
159   {0xffff, 0, 0, 0}
160};
161
162
163/* This function filters resources and then inserts them into a list of
164 * configurable pci resources.
165 */
166
167
168#define AREA(r) \
169(((r->type&PCI_BASE_ADDRESS_SPACE)==PCI_BASE_ADDRESS_SPACE_IO) ? PCI_AREA_IO :\
170          ((r->type&PCI_BASE_ADDRESS_MEM_PREFETCH) ? PCI_AREA_PREFETCHABLE :\
171           PCI_AREA_MEMORY))
172
173
174
175static int insert_before(pci_resource *e, pci_resource *t) {
176   if (e->dev->bus->number != t->dev->bus->number)
177      return e->dev->bus->number > t->dev->bus->number;
178   if (AREA(e) != AREA(t)) return AREA(e)<AREA(t);
179   return (e->size > t->size);
180}
181
182
183
184
185
186static void insert_resource(pci_resource *r) {
187   struct blacklist_entry *b;
188   pci_resource *p;
189   if (!r) return;
190
191   /* First fixup in case we have a blacklist entry. Note that this
192    * may temporarily leave a resource in an inconsistent state: with
193    * (base & (size-1)) !=0. This is harmless.
194    */
195   for (b=blacklist; b->vendor!=0xffff; b++) {
196      if ((r->dev->vendor==b->vendor) &&
197          (r->dev->device==b->device) &&
198          (r->reg==b->reg)) {
199         r->size=b->actual_size;
200         break;
201      }
202   }
203       
204   /* Motorola NT firmware does not configure pci devices which are not
205    * required for booting, others do. For now:
206    * - allocated devices in the ISA range (64kB I/O, 16Mb memory)
207    *   but non zero base registers are left as is.
208    * - all other registers, whether already allocated or not, are
209    *   reallocated unless they require an inordinate amount of
210    *   resources (>256 Mb for memory >64kB for I/O). These
211    *   devices with too large mapping requirements are simply ignored
212    *   and their bases are set to 0. This should disable the
213    *   corresponding decoders according to the PCI specification.
214    *   Many devices are buggy in this respect, however, but the
215    *   limits have hopefully been set high enough to avoid problems.
216    */
217
218#if 0
219   if ((r->type==PCI_BASE_ADDRESS_SPACE_IO)
220       ? (r->base && r->base <0x10000)
221       : (r->base && r->base <0x1000000)) {
222      sfree(r);
223      return;
224   }
225#endif
226
227   if ((r->type==PCI_BASE_ADDRESS_SPACE_IO)
228       ? (r->size >= 0x10000)
229       : (r->size >= 0x10000000)) {
230      r->size  = 0;
231      r->base  = 0;
232   }
233
234   /* Now insert into the list sorting by
235    * 1) decreasing bus number
236    * 2) space: prefetchable memory, non-prefetchable and finally I/O
237    * 3) decreasing size
238    */
239   if (!pci->resources || insert_before(r, pci->resources)) {
240      r->next = pci->resources;
241      pci->resources=r;
242   } else {
243      for (p=pci->resources; p->next; p=p->next) {
244         if (insert_before(r, p->next)) break;
245      }
246      r->next=p->next;
247      p->next=r;
248   }
249}
250
251
252
253
254
255/* This version only works for bus 0. I don't have any P2P bridges to test
256 * a more sophisticated version which has therefore not been implemented.
257 * Prefetchable memory is not yet handled correctly either.
258 * And several levels of PCI bridges much less even since there must be
259 * allocated together to be able to setup correctly the top bridge.
260 */
261
262static u_long find_range(u_char bus, u_char type,
263                         pci_resource **first,
264                         pci_resource **past, u_int *flags) {
265   pci_resource *p;
266   u_long total=0;
267   u_int fl=0;
268
269   for (p=pci->resources; p; p=p->next)
270   {
271      if ((p->dev->bus->number == bus) &&
272          AREA(p)==type) break;
273   }
274
275   *first = p;
276
277   for (; p; p=p->next)
278   {
279      if ((p->dev->bus->number != bus) ||
280          AREA(p)!=type || p->size == 0) break;
281      total = total+p->size;
282      fl |= 1<<p->type;
283   }
284
285   *past = p;
286   /* This will be used later to tell whether there are any 32 bit
287    * devices in an area which could be mapped higher than 4Gb
288    * on 64 bits architectures
289    */
290   *flags = fl;
291   return total;
292}
293
294
295
296
297
298
299static inline void init_free_area(pci_area_head *h, u_long start,
300                                  u_long end, u_int mask, int high) {
301   pci_area *p;
302   p = salloc(sizeof(pci_area));
303   if (!p) return;
304   h->head = p;
305   p->next = NULL;
306   p->start = (start+mask)&~mask;
307   p->end = (end-mask)|mask;
308   p->bus = NULL;
309   h->mask = mask;
310   h->high = high;
311}
312
313
314
315
316
317
318static void insert_area(pci_area_head *h, pci_area *p) {
319   pci_area *q = h->head;
320   if (!p) return;
321   if (q && (q->start< p->start)) {
322      for(;q->next && q->next->start<p->start; q = q->next);
323      if ((q->end >= p->start) ||
324          (q->next && p->end>=q->next->start)) {
325         sfree(p);
326         printk("Overlapping pci areas!\n");
327         return;
328      }
329      p->next = q->next;
330      q->next = p;
331   } else { /* Insert at head */
332      if (q && (p->end >= q->start)) {
333         sfree(p);
334         printk("Overlapping pci areas!\n");
335         return;
336      }
337      p->next = q;
338      h->head = p;
339   }
340}
341
342
343
344
345
346static
347void remove_area(pci_area_head *h, pci_area *p)
348{
349   pci_area *q = h->head;
350
351   if (!p || !q) return;
352   if (q==p)
353   {
354      h->head = q->next;
355      return;
356   }
357   for(;q && q->next!=p; q=q->next);
358   if (q) q->next=p->next;
359}
360
361
362
363
364
365
366static pci_area * alloc_area(pci_area_head *h, struct pci_bus *bus,
367                             u_long required, u_long mask, u_int flags) {
368   pci_area *p;
369   pci_area *from, *split, *new;
370
371   required = (required+h->mask) & ~h->mask;
372   for (p=h->head, from=NULL; p; p=p->next)
373   {
374      u_long l1 = ((p->start+required+mask)&~mask)-1;
375      u_long l2 = ((p->start+mask)&~mask)+required-1;
376      /* Allocated areas point to the bus to which they pertain */
377      if (p->bus) continue;
378      if ((p->end)>=l1 || (p->end)>=l2) from=p;
379      if (from && !h->high) break;
380   }
381   if (!from) return NULL;
382
383   split = salloc(sizeof(pci_area));
384   new = salloc(sizeof(pci_area));
385   /* If allocation of new succeeds then allocation of split has
386    * also been successful (given the current mm algorithms) !
387    */
388   if (!new) {
389      sfree(split);
390      return NULL;
391   }
392   new->bus = bus;
393   new->flags = flags;
394   /* Now allocate pci_space taking alignment into account ! */
395   if (h->high)
396   {
397      u_long l1 = ((from->end+1)&~mask)-required;
398      u_long l2 = (from->end+1-required)&~mask;
399      new->start = (l1>l2) ? l1 : l2;
400      split->end = from->end;
401      from->end = new->start-1;
402      split->start = new->start+required;
403      new->end = new->start+required-1;
404   }
405   else
406   {
407      u_long l1 = ((from->start+mask)&~mask)+required-1;
408      u_long l2 = ((from->start+required+mask)&~mask)-1;
409      new->end = (l1<l2) ? l1 : l2;
410      split->start = from->start;
411      from->start = new->end+1;
412      new->start = new->end+1-required;
413      split->end = new->start-1;
414   }
415       
416   if (from->end+1 == from->start) remove_area(h, from);
417   if (split->end+1 != split->start)
418   {
419      split->bus = NULL;
420      insert_area(h, split);
421   }
422   else
423   {
424      sfree(split);
425   }
426   insert_area(h, new);
427   print_pci_areas("alloc_area called:\n");
428   return new;
429}
430
431
432
433
434
435static inline
436void alloc_space(pci_area *p, pci_resource *r)
437{
438   if (p->start & (r->size-1)) {
439      r->base = p->end+1-r->size;
440      p->end -= r->size;
441   } else {
442      r->base = p->start;
443      p->start += r->size;
444   }
445}
446
447
448
449
450
451static void reconfigure_bus_space(u_char bus, u_char type, pci_area_head *h)
452{
453   pci_resource *first, *past, *r;
454   pci_area *area, tmp;
455   u_int flags;
456   u_int required = find_range(bus, type, &first, &past, &flags);
457
458   if (required==0) return;
459
460   area = alloc_area(h, first->dev->bus, required, first->size-1, flags);
461
462   if (!area) return;
463
464   tmp = *area;
465   for (r=first; r!=past; r=r->next)
466   {
467      alloc_space(&tmp, r);
468   }
469}
470
471
472
473
474
475
476
477#define BUS0_IO_START           0x10000
478#define BUS0_IO_END             0x1ffff
479#define BUS0_MEM_START          0x1000000
480#define BUS0_MEM_END            0xaffffff
481
482#define BUSREST_IO_START        0x20000
483#define BUSREST_IO_END          0x7ffff
484#define BUSREST_MEM_START       0xb000000
485#define BUSREST_MEM_END        0x30000000
486
487
488
489
490
491static void reconfigure_pci(void) {
492   pci_resource *r;
493   struct pci_dev *dev;
494
495   /* FIXME: for now memory is relocated from low, it's better
496    * to start from higher addresses.
497    */
498   /*
499   init_free_area(&pci->io, 0x10000, 0x7fffff, 0xfff, 0);
500   init_free_area(&pci->mem, 0x1000000, 0x3cffffff, 0xfffff, 0);
501   */
502
503   init_free_area(&pci->io, BUS0_IO_START, BUS0_IO_END, 0xfff, 0);
504   init_free_area(&pci->mem, BUS0_MEM_START, BUS0_MEM_END, 0xfffff, 0);
505
506
507   /* First reconfigure the I/O space, this will be more
508    * complex when there is more than 1 bus. And 64 bits
509    * devices are another kind of problems.
510    */
511   reconfigure_bus_space(0, PCI_AREA_IO, &pci->io);
512   reconfigure_bus_space(0, PCI_AREA_MEMORY, &pci->mem);
513   reconfigure_bus_space(0, PCI_AREA_PREFETCHABLE, &pci->mem);
514
515   /* Now we have to touch the configuration space of all
516    * the devices to remap them better than they are right now.
517    * This is done in 3 steps:
518    * 1) first disable I/O and memory response of all devices
519    * 2) modify the base registers
520    * 3) restore the original PCI_COMMAND register.
521    */
522   for (r=pci->resources; r; r= r->next) {
523      if (!r->dev->sysdata) {
524         r->dev->sysdata=r;
525         pci_read_config_word(r->dev, PCI_COMMAND, &r->cmd);
526         pci_write_config_word(r->dev, PCI_COMMAND,
527                               r->cmd & ~(PCI_COMMAND_IO|
528                                          PCI_COMMAND_MEMORY));
529      }
530   }
531
532   for (r=pci->resources; r; r= r->next) {
533      pci_write_config_dword(r->dev,
534                             PCI_BASE_ADDRESS_0+(r->reg<<2),
535                             r->base);
536      if ((r->type&
537           (PCI_BASE_ADDRESS_SPACE|
538            PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
539          (PCI_BASE_ADDRESS_SPACE_MEMORY|
540           PCI_BASE_ADDRESS_MEM_TYPE_64)) {
541         pci_write_config_dword(r->dev,
542                                PCI_BASE_ADDRESS_1+
543                                (r->reg<<2),
544                                0);
545      }
546   }
547   for (dev=bd->pci_devices; dev; dev= dev->next) {
548      if (dev->sysdata) {
549         pci_write_config_word(dev, PCI_COMMAND,
550                               ((pci_resource *)dev->sysdata)
551                               ->cmd);
552         dev->sysdata=NULL;
553      }
554   }
555}
556
557
558
559
560
561
562static int
563indirect_pci_read_config_byte(unsigned char bus, unsigned char dev_fn,
564                              unsigned char offset, unsigned char *val) {
565   out_be32(pci->config_addr,
566            0x80|(bus<<8)|(dev_fn<<16)|((offset&~3)<<24));
567   *val=in_8(pci->config_data + (offset&3));
568   return PCIBIOS_SUCCESSFUL;
569}
570
571static int
572indirect_pci_read_config_word(unsigned char bus, unsigned char dev_fn,
573                              unsigned char offset, unsigned short *val) {
574   *val = 0xffff;
575   if (offset&1) return PCIBIOS_BAD_REGISTER_NUMBER;
576   out_be32(pci->config_addr,
577            0x80|(bus<<8)|(dev_fn<<16)|((offset&~3)<<24));
578   *val=in_le16((volatile u_short *)(pci->config_data + (offset&3)));
579   return PCIBIOS_SUCCESSFUL;
580}
581
582static int
583indirect_pci_read_config_dword(unsigned char bus, unsigned char dev_fn,
584                               unsigned char offset, unsigned int *val) {
585   *val = 0xffffffff;
586   if (offset&3) return PCIBIOS_BAD_REGISTER_NUMBER;
587   out_be32(pci->config_addr,
588            0x80|(bus<<8)|(dev_fn<<16)|(offset<<24));
589   *val=in_le32((volatile u_int *)pci->config_data);
590   return PCIBIOS_SUCCESSFUL;
591}
592
593static int
594indirect_pci_write_config_byte(unsigned char bus, unsigned char dev_fn,
595                               unsigned char offset, unsigned char val) {
596   out_be32(pci->config_addr,
597            0x80|(bus<<8)|(dev_fn<<16)|((offset&~3)<<24));
598   out_8(pci->config_data + (offset&3), val);
599   return PCIBIOS_SUCCESSFUL;
600}
601
602static int
603indirect_pci_write_config_word(unsigned char bus, unsigned char dev_fn,
604                               unsigned char offset, unsigned short val) {
605   if (offset&1) return PCIBIOS_BAD_REGISTER_NUMBER;
606   out_be32(pci->config_addr,
607            0x80|(bus<<8)|(dev_fn<<16)|((offset&~3)<<24));
608   out_le16((volatile u_short *)(pci->config_data + (offset&3)), val);
609   return PCIBIOS_SUCCESSFUL;
610}
611
612static int
613indirect_pci_write_config_dword(unsigned char bus, unsigned char dev_fn,
614                                unsigned char offset, unsigned int val) {
615   if (offset&3) return PCIBIOS_BAD_REGISTER_NUMBER;
616   out_be32(pci->config_addr,
617            0x80|(bus<<8)|(dev_fn<<16)|(offset<<24));
618   out_le32((volatile u_int *)pci->config_data, val);
619   return PCIBIOS_SUCCESSFUL;
620}
621
622static const struct pci_config_access_functions indirect_functions = {
623   indirect_pci_read_config_byte,
624   indirect_pci_read_config_word,
625   indirect_pci_read_config_dword,
626   indirect_pci_write_config_byte,
627   indirect_pci_write_config_word,
628   indirect_pci_write_config_dword
629};
630
631
632static int
633direct_pci_read_config_byte(unsigned char bus, unsigned char dev_fn,
634                            unsigned char offset, unsigned char *val) {
635   if (bus != 0 || (1<<PCI_SLOT(dev_fn) & 0xff8007fe)) {
636      *val=0xff;
637      return PCIBIOS_DEVICE_NOT_FOUND;
638   }
639   *val=in_8(pci->config_data + ((1<<PCI_SLOT(dev_fn))&~1)
640             + (PCI_FUNC(dev_fn)<<8) + offset);
641   return PCIBIOS_SUCCESSFUL;
642}
643
644static int
645direct_pci_read_config_word(unsigned char bus, unsigned char dev_fn,
646                            unsigned char offset, unsigned short *val) {
647   *val = 0xffff;
648   if (offset&1) return PCIBIOS_BAD_REGISTER_NUMBER;
649   if (bus != 0 || (1<<PCI_SLOT(dev_fn) & 0xff8007fe)) {
650      return PCIBIOS_DEVICE_NOT_FOUND;
651   }
652   *val=in_le16((volatile u_short *)
653                (pci->config_data + ((1<<PCI_SLOT(dev_fn))&~1)
654                 + (PCI_FUNC(dev_fn)<<8) + offset));
655   return PCIBIOS_SUCCESSFUL;
656}
657
658static int
659direct_pci_read_config_dword(unsigned char bus, unsigned char dev_fn,
660                             unsigned char offset, unsigned int *val) {
661   *val = 0xffffffff;
662   if (offset&3) return PCIBIOS_BAD_REGISTER_NUMBER;
663   if (bus != 0 || (1<<PCI_SLOT(dev_fn) & 0xff8007fe)) {
664      return PCIBIOS_DEVICE_NOT_FOUND;
665   }
666   *val=in_le32((volatile u_int *)
667                (pci->config_data + ((1<<PCI_SLOT(dev_fn))&~1)
668                 + (PCI_FUNC(dev_fn)<<8) + offset));
669   return PCIBIOS_SUCCESSFUL;
670}
671
672static int
673direct_pci_write_config_byte(unsigned char bus, unsigned char dev_fn,
674                             unsigned char offset, unsigned char val) {
675   if (bus != 0 || (1<<PCI_SLOT(dev_fn) & 0xff8007fe)) {
676      return PCIBIOS_DEVICE_NOT_FOUND;
677   }
678   out_8(pci->config_data + ((1<<PCI_SLOT(dev_fn))&~1)
679         + (PCI_FUNC(dev_fn)<<8) + offset,
680         val);
681   return PCIBIOS_SUCCESSFUL;
682}
683
684static int
685direct_pci_write_config_word(unsigned char bus, unsigned char dev_fn,
686                             unsigned char offset, unsigned short val) {
687   if (offset&1) return PCIBIOS_BAD_REGISTER_NUMBER;
688   if (bus != 0 || (1<<PCI_SLOT(dev_fn) & 0xff8007fe)) {
689      return PCIBIOS_DEVICE_NOT_FOUND;
690   }
691   out_le16((volatile u_short *)
692            (pci->config_data + ((1<<PCI_SLOT(dev_fn))&~1)
693             + (PCI_FUNC(dev_fn)<<8) + offset),
694            val);
695   return PCIBIOS_SUCCESSFUL;
696}
697
698static int
699direct_pci_write_config_dword(unsigned char bus, unsigned char dev_fn,
700                              unsigned char offset, unsigned int val) {
701   if (offset&3) return PCIBIOS_BAD_REGISTER_NUMBER;
702   if (bus != 0 || (1<<PCI_SLOT(dev_fn) & 0xff8007fe)) {
703      return PCIBIOS_DEVICE_NOT_FOUND;
704   }
705   out_le32((volatile u_int *)
706            (pci->config_data + ((1<<PCI_SLOT(dev_fn))&~1)
707             + (PCI_FUNC(dev_fn)<<8) + offset),
708            val);
709   return PCIBIOS_SUCCESSFUL;
710}
711
712static const struct pci_config_access_functions direct_functions = {
713   direct_pci_read_config_byte,
714   direct_pci_read_config_word,
715   direct_pci_read_config_dword,
716   direct_pci_write_config_byte,
717   direct_pci_write_config_word,
718   direct_pci_write_config_dword
719};
720
721
722
723
724
725
726void pci_read_bases(struct pci_dev *dev, unsigned int howmany)
727{
728   unsigned int reg, nextreg;
729
730#define REG (PCI_BASE_ADDRESS_0 + (reg<<2))
731
732   u_short cmd;
733   u32 l, ml;
734   pci_read_config_word(dev, PCI_COMMAND, &cmd);
735
736   for(reg=0; reg<howmany; reg=nextreg)
737   {
738      pci_resource *r;
739
740      nextreg=reg+1;
741      pci_read_config_dword(dev, REG, &l);
742#if 0
743      if (l == 0xffffffff /*AJF || !l*/) continue;
744#endif
745      /* Note that disabling the memory response of a host bridge
746       * would lose data if a DMA transfer were in progress. In a
747       * bootloader we don't care however. Also we can't print any
748       * message for a while since we might just disable the console.
749       */
750      pci_write_config_word(dev, PCI_COMMAND, cmd &
751                            ~(PCI_COMMAND_IO|PCI_COMMAND_MEMORY));
752      pci_write_config_dword(dev, REG, ~0);
753      pci_read_config_dword(dev, REG, &ml);
754      pci_write_config_dword(dev, REG, l);
755
756      /* Reenable the device now that we've played with
757       * base registers.
758       */
759      pci_write_config_word(dev, PCI_COMMAND, cmd);
760
761      /* seems to be an unused entry skip it */
762      if ( ml == 0 || ml == 0xffffffff ) continue;
763
764      if ((l &
765           (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK))
766          == (PCI_BASE_ADDRESS_MEM_TYPE_64
767              |PCI_BASE_ADDRESS_SPACE_MEMORY)) {
768         nextreg=reg+2;
769      }
770      dev->base_address[reg] = l;
771      r = salloc(sizeof(pci_resource));
772      if (!r) {
773         printk("Error allocating pci_resource struct.\n");
774         continue;
775      }
776      r->dev = dev;
777      r->reg = reg;
778      if ((l&PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
779         r->type = l&~PCI_BASE_ADDRESS_IO_MASK;
780         r->base = l&PCI_BASE_ADDRESS_IO_MASK;
781         r->size = ~(ml&PCI_BASE_ADDRESS_IO_MASK)+1;
782      } else {
783         r->type = l&~PCI_BASE_ADDRESS_MEM_MASK;
784         r->base = l&PCI_BASE_ADDRESS_MEM_MASK;
785         r->size = ~(ml&PCI_BASE_ADDRESS_MEM_MASK)+1;
786      }
787      /* Check for the blacklisted entries */
788      insert_resource(r);
789   }
790}
791
792
793
794
795
796
797
798
799u_int pci_scan_bus(struct pci_bus *bus)
800{
801   unsigned int devfn, l, max, class;
802   unsigned char irq, hdr_type, is_multi = 0;
803   struct pci_dev *dev, **bus_last;
804   struct pci_bus *child;
805
806#if 0
807   printk("scanning pci bus %d\n", bus->number );
808#endif
809
810   bus_last = &bus->devices;
811   max = bus->secondary;
812   for (devfn = 0; devfn < 0xff; ++devfn) {
813      if (PCI_FUNC(devfn) && !is_multi) {
814         /* not a multi-function device */
815         continue;
816      }
817      if (pcibios_read_config_byte(bus->number, devfn, PCI_HEADER_TYPE, &hdr_type))
818         continue;
819      if (!PCI_FUNC(devfn))
820         is_multi = hdr_type & 0x80;
821
822      if (pcibios_read_config_dword(bus->number, devfn, PCI_VENDOR_ID, &l) ||
823          /* some broken boards return 0 if a slot is empty: */
824          l == 0xffffffff || l == 0x00000000 || l == 0x0000ffff || l == 0xffff0000) {
825         is_multi = 0;
826         continue;
827      }
828
829      dev = salloc(sizeof(*dev));
830      dev->bus = bus;
831      dev->devfn  = devfn;
832      dev->vendor = l & 0xffff;
833      dev->device = (l >> 16) & 0xffff;
834
835      pcibios_read_config_dword(bus->number, devfn,
836                                PCI_CLASS_REVISION, &class);
837      class >>= 8;                                  /* upper 3 bytes */
838      dev->class = class;
839      class >>= 8;
840      dev->hdr_type = hdr_type;
841
842      switch (hdr_type & 0x7f) {                    /* header type */
843         case PCI_HEADER_TYPE_NORMAL:               /* standard header */
844            if (class == PCI_CLASS_BRIDGE_PCI)
845               goto bad;
846            /*
847             * If the card generates interrupts, read IRQ number
848             * (some architectures change it during pcibios_fixup())
849             */
850            pcibios_read_config_byte(bus->number, dev->devfn, PCI_INTERRUPT_PIN, &irq);
851            if (irq)
852               pcibios_read_config_byte(bus->number, dev->devfn, PCI_INTERRUPT_LINE, &irq);
853            dev->irq = irq;
854            /*
855             * read base address registers, again pcibios_fixup() can
856             * tweak these
857             */
858            pci_read_bases(dev, 6);
859            pcibios_read_config_dword(bus->number, devfn, PCI_ROM_ADDRESS, &l);
860            dev->rom_address = (l == 0xffffffff) ? 0 : l;
861            break;
862         case PCI_HEADER_TYPE_BRIDGE:               /* bridge header */
863            if (class != PCI_CLASS_BRIDGE_PCI)
864               goto bad;
865            pci_read_bases(dev, 2);
866            pcibios_read_config_dword(bus->number, devfn, PCI_ROM_ADDRESS1, &l);
867            dev->rom_address = (l == 0xffffffff) ? 0 : l;
868            break;
869         case PCI_HEADER_TYPE_CARDBUS:              /* CardBus bridge header */
870            if (class != PCI_CLASS_BRIDGE_CARDBUS)
871               goto bad;
872            pci_read_bases(dev, 1);
873            break;
874
875         default:                                   /* unknown header */
876        bad:
877            printk("PCI device with unknown header type %d ignored.\n",
878                   hdr_type&0x7f);
879            continue;
880      }
881
882      /*
883       * Put it into the global PCI device chain. It's used to
884       * find devices once everything is set up.
885       */
886      *pci->last_dev_p = dev;
887      pci->last_dev_p = &dev->next;
888
889      /*
890       * Now insert it into the list of devices held
891       * by the parent bus.
892       */
893      *bus_last = dev;
894      bus_last = &dev->sibling;
895
896   }
897
898   /*
899    * After performing arch-dependent fixup of the bus, look behind
900    * all PCI-to-PCI bridges on this bus.
901    */
902   for(dev=bus->devices; dev; dev=dev->sibling)
903      /*
904       * If it's a bridge, scan the bus behind it.
905       */
906      if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
907         unsigned int buses;
908         unsigned int devfn = dev->devfn;
909         unsigned short cr;
910
911         /*
912          * Insert it into the tree of buses.
913          */
914         child = salloc(sizeof(*child));
915         child->next = bus->children;
916         bus->children = child;
917         child->self = dev;
918         child->parent = bus;
919
920         /*
921          * Set up the primary, secondary and subordinate
922          * bus numbers.
923          */
924         child->number = child->secondary = ++max;
925         child->primary = bus->secondary;
926         child->subordinate = 0xff;
927         /*
928          * Clear all status bits and turn off memory,
929          * I/O and master enables.
930          */
931         pcibios_read_config_word(bus->number, devfn, PCI_COMMAND, &cr);
932         pcibios_write_config_word(bus->number, devfn, PCI_COMMAND, 0x0000);
933         pcibios_write_config_word(bus->number, devfn, PCI_STATUS, 0xffff);
934         /*
935          * Read the existing primary/secondary/subordinate bus
936          * number configuration to determine if the PCI bridge
937          * has already been configured by the system.  If so,
938          * do not modify the configuration, merely note it.
939          */
940         pcibios_read_config_dword(bus->number, devfn, PCI_PRIMARY_BUS, &buses);
941         if ((buses & 0xFFFFFF) != 0)
942         {
943            unsigned int cmax;
944
945            child->primary = buses & 0xFF;
946            child->secondary = (buses >> 8) & 0xFF;
947            child->subordinate = (buses >> 16) & 0xFF;
948            child->number = child->secondary;
949            cmax = pci_scan_bus(child);
950            if (cmax > max) max = cmax;
951         }
952         else
953         {
954            /*
955             * Configure the bus numbers for this bridge:
956             */
957            buses &= 0xff000000;
958            buses |=
959               (((unsigned int)(child->primary)     <<  0) |
960                ((unsigned int)(child->secondary)   <<  8) |
961                ((unsigned int)(child->subordinate) << 16));
962            pcibios_write_config_dword(bus->number, devfn, PCI_PRIMARY_BUS, buses);
963            /*
964             * Now we can scan all subordinate buses:
965             */
966            max = pci_scan_bus(child);
967            /*
968             * Set the subordinate bus number to its real
969             * value:
970             */
971            child->subordinate = max;
972            buses = (buses & 0xff00ffff)
973               | ((unsigned int)(child->subordinate) << 16);
974            pcibios_write_config_dword(bus->number, devfn, PCI_PRIMARY_BUS, buses);
975         }
976         pcibios_write_config_word(bus->number, devfn, PCI_COMMAND, cr );
977      }
978
979   /*
980    * We've scanned the bus and so we know all about what's on
981    * the other side of any bridges that may be on this bus plus
982    * any devices.
983    *
984    * Return how far we've got finding sub-buses.
985    */
986   return max;
987}
988
989
990
991
992
993
994
995
996
997
998#if 0
999
1000void
1001pci_fixup(void)
1002{
1003   struct pci_dev *p;
1004   struct pci_bus *bus;
1005
1006   for (bus = &pci_root; bus; bus=bus->next)
1007   {
1008      for (p=bus->devices; p; p=p->sibling)
1009      {
1010      }
1011   }
1012}
1013
1014
1015
1016
1017
1018
1019static void print_pci_info()
1020{
1021   pci_resource *r;
1022   struct pci_bus *pb = &pci_root;
1023
1024   printk("\n");
1025   printk("PCI busses:\n");
1026
1027   for(pb= &pci_root; pb; pb=pb->children )
1028   {
1029      printk("   number %d, primary %d, secondary %d, subordinate %d\n",
1030             pb->number,
1031             pb->primary,
1032             pb->secondary,
1033             pb->subordinate );
1034      printk("   bridge; vendor %04x, device %04x\n",
1035             pb->self->vendor,
1036             pb->self->device );
1037
1038      {
1039         struct pci_dev *pd;
1040
1041         for(pd= pb->devices; pd; pd=pd->sibling )
1042         {
1043            printk("       vendor %04x, device %04x, irq %d\n",
1044                   pd->vendor,
1045                   pd->device,
1046                   pd->irq );
1047           
1048         }
1049         printk("\n");
1050      }
1051
1052   }
1053   printk("\n");
1054
1055   printk("PCI resources:\n");
1056   for (r=pci->resources; r; r= r->next)
1057   {
1058      printk("   bus %d, vendor %04x, device %04x, base %08x, size %08x, type %d\n",
1059             r->dev->bus->number,
1060             r->dev->vendor,
1061             r->dev->device,
1062             r->base,
1063             r->size,
1064             r->type );
1065   }
1066   printk("\n");
1067
1068   return;
1069}
1070
1071#endif
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084static struct _addr_start
1085{
1086      unsigned32 start_pcimem;
1087      unsigned32 start_pciio;
1088      unsigned32 start_prefetch;
1089} astart;
1090
1091
1092
1093
1094
1095static pci_resource *enum_device_resources( struct pci_dev *pdev, int i )
1096{
1097   pci_resource *r;
1098
1099   for(r= pci->resources; r; r= r->next )
1100   {
1101      if( r->dev == pdev )
1102      {
1103         if( i-- == 0 ) break;
1104      }
1105   }
1106   return r;
1107}
1108
1109
1110
1111
1112
1113
1114#define MEMORY_IO_GRANULARITY   256
1115
1116
1117
1118static void recursive_bus_reconfigure( struct pci_bus *pbus )
1119{
1120   struct pci_dev       *pdev;
1121   struct pci_bus       *childbus;
1122   int  isroot = 0;
1123
1124
1125   if( !pbus )
1126   {
1127      /* start with the root bus */
1128      astart.start_pcimem   = BUSREST_MEM_START;
1129      astart.start_pciio    = BUSREST_IO_START;
1130      astart.start_prefetch = ((BUSREST_MEM_END >> 16) << 16);
1131
1132      pbus = &pci_root;
1133      isroot = -1;
1134   }
1135
1136
1137#define WRITE_BRIDGE_IO
1138#define WRITE_BRIDGE_MEM
1139#define WRITE_BRIDGE_PF
1140#define WRITE_BRIDGE_ENABLE
1141
1142
1143/*
1144** Run thru the p2p bridges on this bus and recurse into subordinate busses
1145*/
1146   for( childbus= pbus->children; childbus; childbus= childbus->next )
1147   {
1148      pdev= childbus->self;
1149
1150      {
1151         struct _addr_start   addrhold;
1152         unsigned8            base8, limit8;
1153         unsigned16           base16, limit16, ubase16, ulimit16;
1154
1155         /* save the base address values */
1156         memcpy( &addrhold, &astart, sizeof(struct _addr_start));
1157
1158         recursive_bus_reconfigure( childbus );
1159
1160#ifdef PCI_DEBUG
1161         printk("pci: configuring bus %d bridge (%04x:%04x), bus %d : (%d-%d)\n",
1162                pdev->bus->number,
1163                pdev->vendor,
1164                pdev->device,
1165                childbus->primary,
1166                childbus->secondary,
1167                childbus->subordinate );
1168#endif
1169
1170   
1171
1172         /*
1173         **use the current values & the saved ones to figure out
1174         ** the address spaces for the bridge
1175         */
1176
1177         if( addrhold.start_pciio == astart.start_pciio )
1178         {
1179            base8 = limit8 = 0xff;
1180            ubase16 = ulimit16 = 0xffff;
1181         }
1182         else
1183         {
1184            base8    = (unsigned8) ((addrhold.start_pciio >> 8) & 0xf0);
1185            ubase16  = (unsigned16)(addrhold.start_pciio >> 16);
1186            limit8   = (unsigned8) ((astart.start_pciio >> 8 ) & 0xf0);
1187            ulimit16 = (unsigned16)(astart.start_pciio >> 16);
1188            astart.start_pciio += 0x1000;
1189         }
1190
1191#ifdef PCI_DEBUG
1192         printk("pci:     io base %08x limit %08x\n", (base8<<8)+(ubase16<<16), (limit8<<8)+(ulimit16<<16));
1193#endif
1194#ifdef WRITE_BRIDGE_IO
1195         pcibios_write_config_word(pdev->bus->number, pdev->devfn, PCI_IO_BASE_UPPER16, ubase16 );
1196         pcibios_write_config_byte(pdev->bus->number, pdev->devfn, PCI_IO_BASE, base8 );
1197
1198         pcibios_write_config_word(pdev->bus->number, pdev->devfn, PCI_IO_LIMIT_UPPER16, ulimit16 );
1199         pcibios_write_config_byte(pdev->bus->number, pdev->devfn, PCI_IO_LIMIT, limit8 );
1200#endif
1201
1202
1203
1204
1205         if( addrhold.start_pcimem == astart.start_pcimem )
1206         {
1207            limit16 = 0;
1208            base16 = 0xffff;
1209         }
1210         else
1211         {
1212            limit16= (unsigned16)((astart.start_pcimem >> 16) & 0xfff0);
1213            base16 = (unsigned16)((addrhold.start_pcimem >> 16) & 0xfff0);
1214            astart.start_pcimem += 0x100000;
1215         }
1216#ifdef PCI_DEBUG
1217         printk("pci:      memory %04x, limit %04x\n", base16, limit16);
1218#endif
1219#ifdef WRITE_BRIDGE_MEM
1220         pcibios_write_config_word(pdev->bus->number, pdev->devfn, PCI_MEMORY_BASE, base16 );
1221         pcibios_write_config_word(pdev->bus->number, pdev->devfn, PCI_MEMORY_LIMIT, limit16 );
1222#endif
1223
1224
1225
1226         if( astart.start_prefetch == addrhold.start_prefetch )
1227         {
1228            limit16 = 0;
1229            base16 = 0xffff;
1230         }
1231         else
1232         {
1233            limit16= (unsigned16)((addrhold.start_prefetch >> 16) & 0xfff0);
1234            base16 = (unsigned16)((astart.start_prefetch >> 16) & 0xfff0);
1235            astart.start_prefetch -= 0x100000;
1236         }
1237#ifdef PCI_DEBUG
1238         printk("pci:   pf memory %04x, limit %04x\n", base16, limit16);
1239#endif
1240#ifdef WRITE_BRIDGE_PF
1241         pcibios_write_config_dword(pdev->bus->number, pdev->devfn, PCI_PREF_BASE_UPPER32, 0); 
1242         pcibios_write_config_word(pdev->bus->number, pdev->devfn, PCI_PREF_MEMORY_BASE, base16 );
1243         pcibios_write_config_dword(pdev->bus->number, pdev->devfn, PCI_PREF_LIMIT_UPPER32, 0);
1244         pcibios_write_config_word(pdev->bus->number, pdev->devfn, PCI_PREF_MEMORY_LIMIT, limit16 );
1245
1246#endif
1247
1248#ifdef WRITE_BRIDGE_ENABLE
1249         pcibios_write_config_word(pdev->bus->number, pdev->devfn, PCI_BRIDGE_CONTROL, (unsigned16)( PCI_BRIDGE_CTL_PARITY |
1250                                                                                                     PCI_BRIDGE_CTL_SERR |
1251                                                                                                     PCI_BRIDGE_CTL_FAST_BACK));
1252
1253         pcibios_write_config_word(pdev->bus->number, pdev->devfn, PCI_COMMAND, (unsigned16)( PCI_COMMAND_IO |
1254                                                                                              PCI_COMMAND_MEMORY |
1255                                                                                              PCI_COMMAND_MASTER |
1256                                                                                              PCI_COMMAND_PARITY |
1257                                                                                              PCI_COMMAND_WAIT |
1258                                                                                              PCI_COMMAND_SERR |
1259                                                                                              PCI_COMMAND_FAST_BACK ));
1260#endif
1261      }
1262   }
1263
1264
1265
1266
1267
1268
1269   if( !isroot )
1270   {
1271#ifdef PCI_DEBUG
1272      printk("pci: Configuring devices on bus %d\n", pbus->number);
1273#endif
1274      /*
1275      ** Run thru this bus and set up addresses for all the non-bridge devices
1276      */
1277      for( pdev = pbus->devices; pdev; pdev= pdev->sibling )
1278      {
1279         if( (pdev->class >> 8) != PCI_CLASS_BRIDGE_PCI )
1280         {
1281            pci_resource *r;
1282            int i = 0;
1283            unsigned alloc;
1284
1285            /* enumerate all the resources defined by this device & reserve space
1286            ** for each of their defined regions.
1287            */
1288
1289#ifdef PCI_DEBUG
1290            printk("pci: configuring; vendor %04x, device %04x\n", pdev->vendor, pdev->device );
1291#endif
1292
1293            while( (r= enum_device_resources( pdev, i++ )) )
1294            {
1295               if( r->type & PCI_BASE_ADDRESS_MEM_PREFETCH )
1296               {
1297                  /* prefetchable space */
1298
1299                  /* shift base pointer down to an integer multiple of the size of the desired region */
1300                  astart.start_prefetch -= (alloc= ((r->size / PAGE_SIZE) + 1) * PAGE_SIZE);
1301                  /* shift base pointer down to an integer multiple of the size of the desired region */
1302                  astart.start_prefetch = (astart.start_prefetch / r->size) * r->size;
1303
1304                  r->base = astart.start_prefetch;
1305#ifdef PCI_DEBUG
1306                  printk("pci:       pf %08X, size %08X, alloc %08X\n", r->base, r->size, alloc );
1307#endif
1308               }
1309               else if( r->type & PCI_BASE_ADDRESS_SPACE_IO )
1310               {
1311                  /* io space */
1312
1313                  /* shift base pointer up to an integer multiple of the size of the desired region */
1314                  if( astart.start_pciio % r->size )
1315                     astart.start_pciio = (((astart.start_pciio / r->size) + 1) * r->size);
1316
1317                  r->base = astart.start_pciio;
1318                  astart.start_pciio += (alloc= ((r->size / MEMORY_IO_GRANULARITY) + 1) * MEMORY_IO_GRANULARITY);
1319#ifdef PCI_DEBUG
1320                  printk("pci:      io  %08X, size %08X, alloc %08X\n", r->base, r->size, alloc );
1321#endif
1322               }
1323               else
1324               {
1325                  /* memory space */
1326                 
1327                  /* shift base pointer up to an integer multiple of the size of the desired region */
1328                  if( astart.start_pcimem % r->size )
1329                     astart.start_pcimem = (((astart.start_pcimem / r->size) + 1) * r->size);
1330
1331                  r->base = astart.start_pcimem;
1332                  astart.start_pcimem += (alloc= ((r->size / MEMORY_IO_GRANULARITY) + 1) * MEMORY_IO_GRANULARITY);
1333#ifdef PCI_DEBUG
1334                  printk("pci:      mem %08X, size %08X, alloc %08X\n", r->base, r->size, alloc );
1335#endif
1336               }
1337            }
1338
1339         }
1340      }
1341   }
1342
1343}
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354void pci_init(void)
1355{
1356   PPC_DEVICE *hostbridge;
1357
1358   if (pci->last_dev_p) {
1359      printk("Two or more calls to pci_init!\n");
1360      return;
1361   }
1362   pci->last_dev_p = &(bd->pci_devices);
1363   hostbridge=residual_find_device(PROCESSORDEVICE, NULL,
1364                                   BridgeController,
1365                                   PCIBridge, -1, 0);
1366   if (hostbridge) {
1367      if (hostbridge->DeviceId.Interface==PCIBridgeIndirect) {
1368         bd->pci_functions=&indirect_functions;
1369         /* Should be extracted from residual data,
1370          * indeed MPC106 in CHRP mode is different,
1371          * but we should not use residual data in
1372          * this case anyway.
1373          */
1374         pci->config_addr = ((volatile u_int *)
1375                             (ptr_mem_map->io_base+0xcf8));
1376         pci->config_data = ptr_mem_map->io_base+0xcfc;
1377      } else if(hostbridge->DeviceId.Interface==PCIBridgeDirect) {
1378         bd->pci_functions=&direct_functions;
1379         pci->config_data=(u_char *) 0x80800000;
1380      } else {
1381      }
1382   } else {
1383      /* Let us try by experimentation at our own risk! */
1384      u_int id0;
1385      bd->pci_functions = &direct_functions;
1386      /* On all direct bridges I know the host bridge itself
1387       * appears as device 0 function 0.
1388       */
1389      pcibios_read_config_dword(0, 0, PCI_VENDOR_ID, &id0);
1390      if (id0==~0U) {
1391         bd->pci_functions = &indirect_functions;
1392         pci->config_addr = ((volatile u_int *)
1393                             (ptr_mem_map->io_base+0xcf8));
1394         pci->config_data = ptr_mem_map->io_base+0xcfc;
1395      }
1396      /* Here we should check that the host bridge is actually
1397       * present, but if it not, we are in such a desperate
1398       * situation, that we probably can't even tell it.
1399       */
1400   }
1401   /* Now build a small database of all found PCI devices */
1402   printk("\nPCI: Probing PCI hardware\n");
1403   pci_root.subordinate=pci_scan_bus(&pci_root);
1404
1405   print_pci_resources("Installed PCI resources:\n");
1406
1407   recursive_bus_reconfigure(NULL);
1408   reconfigure_pci();
1409
1410   print_pci_resources("Allocated PCI resources:\n");
1411}
1412
1413
1414/* eof */
Note: See TracBrowser for help on using the repository browser.