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

4.104.114.84.95
Last change on this file since f05b2ac was f05b2ac, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/21/04 at 16:01:48

Remove duplicate white lines.

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