1 | /* PCI (Static) Configuration Library. PCI Configuration C code console |
---|
2 | * printout routines that can be used to build a static PCI configuration. |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 2010. |
---|
5 | * Cobham Gaisler AB. |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | */ |
---|
11 | |
---|
12 | #include <rtems.h> |
---|
13 | #include <stdio.h> |
---|
14 | #include <pci/cfg.h> |
---|
15 | |
---|
16 | int pci_cfg_print_bus(struct pci_bus *bus); |
---|
17 | |
---|
18 | static void get_bus_name(struct pci_bus *bus, char *buf) |
---|
19 | { |
---|
20 | if (bus->num == 0) |
---|
21 | strcpy(buf, "pci_hb"); |
---|
22 | else |
---|
23 | sprintf(buf, "bus%d", bus->num); |
---|
24 | } |
---|
25 | |
---|
26 | static void get_device_name(struct pci_dev *dev, char *buf) |
---|
27 | { |
---|
28 | char busname[64]; |
---|
29 | |
---|
30 | if (dev->flags & PCI_DEV_BRIDGE) { |
---|
31 | get_bus_name((struct pci_bus *)dev, busname); |
---|
32 | sprintf(buf, "%s.dev", busname); |
---|
33 | } else { |
---|
34 | sprintf(buf, "dev_%x_%x_%x", PCI_DEV_EXPAND(dev->busdevfun)); |
---|
35 | } |
---|
36 | } |
---|
37 | |
---|
38 | static void pci_cfg_print_resources(struct pci_res *resources, char *prefix) |
---|
39 | { |
---|
40 | struct pci_res *res; |
---|
41 | int i; |
---|
42 | |
---|
43 | for (i = 0; i < DEV_RES_CNT; i++) { |
---|
44 | res = &resources[i]; |
---|
45 | if (((res->flags & PCI_RES_TYPE_MASK) == 0) || |
---|
46 | ((res->flags & PCI_RES_FAIL) == PCI_RES_FAIL)) { |
---|
47 | printf("%sPCIRES_EMPTY,\n", prefix); |
---|
48 | continue; |
---|
49 | } |
---|
50 | printf("%s{\n", prefix); |
---|
51 | printf("%s\t.next = NULL,\n", prefix); |
---|
52 | printf("%s\t.size = 0x%08lx,\n", prefix, res->size); |
---|
53 | printf("%s\t.boundary = 0x%08lx,\n", prefix, res->boundary); |
---|
54 | printf("%s\t.flags = 0x%x,\n", prefix, res->flags); |
---|
55 | printf("%s\t.bar = %d,\n", prefix, i); |
---|
56 | printf("%s\t.start = 0x%08lx,\n", prefix, res->start); |
---|
57 | printf("%s\t.end = 0x%08lx,\n", prefix, res->end); |
---|
58 | printf("%s},\n", prefix); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | static void pci_cfg_print_device(struct pci_dev *dev, char *prefix) |
---|
63 | { |
---|
64 | char name[32]; |
---|
65 | char buf[8]; |
---|
66 | printf("%s.resources = {\n", prefix); |
---|
67 | strcpy(buf, prefix); |
---|
68 | strcat(buf, "\t"); |
---|
69 | pci_cfg_print_resources(dev->resources, buf); |
---|
70 | printf("%s},\n", prefix); |
---|
71 | if (dev->next == NULL) { |
---|
72 | printf("%s.next = NULL,\n", prefix); |
---|
73 | } else { |
---|
74 | get_device_name(dev->next, name); |
---|
75 | printf("%s.next = &%s,\n", prefix, name); |
---|
76 | } |
---|
77 | if (!dev->bus) { /* Host Bridge? */ |
---|
78 | printf("%s.bus = NULL,\n", prefix); |
---|
79 | } else { |
---|
80 | get_bus_name(dev->bus, name); |
---|
81 | printf("%s.bus = &%s,\n", prefix, name); |
---|
82 | } |
---|
83 | |
---|
84 | printf("%s.busdevfun = 0x%04x,\n", prefix, dev->busdevfun); |
---|
85 | printf("%s.flags = 0x%x,\n", prefix, dev->flags); |
---|
86 | printf("%s.sysirq = %d,\n", prefix, dev->sysirq); |
---|
87 | printf("%s.vendor = 0x%04x,\n", prefix, dev->vendor); |
---|
88 | printf("%s.device = 0x%04x,\n", prefix, dev->device); |
---|
89 | printf("%s.subvendor = 0x%04x,\n", prefix, dev->subvendor); |
---|
90 | printf("%s.subdevice = 0x%04x,\n", prefix, dev->subdevice); |
---|
91 | printf("%s.classrev = 0x%08lx,\n", prefix, dev->classrev); |
---|
92 | printf("%s.command = 0,\n", prefix); |
---|
93 | } |
---|
94 | |
---|
95 | static int pci_cfg_print_dev(struct pci_dev *dev, void *unused) |
---|
96 | { |
---|
97 | if (dev->flags & PCI_DEV_BRIDGE) { |
---|
98 | pci_cfg_print_bus((struct pci_bus *)dev); |
---|
99 | } else { |
---|
100 | printf("\n\n/* PCI DEV at [%x:%x:%x] */\n", |
---|
101 | PCI_DEV_EXPAND(dev->busdevfun)); |
---|
102 | printf("static struct pci_dev dev_%x_%x_%x = {\n", |
---|
103 | PCI_DEV_EXPAND(dev->busdevfun)); |
---|
104 | pci_cfg_print_device(dev, "\t"); |
---|
105 | printf("};\n"); |
---|
106 | } |
---|
107 | return 0; |
---|
108 | } |
---|
109 | |
---|
110 | int pci_cfg_print_bus(struct pci_bus *bus) |
---|
111 | { |
---|
112 | char name[32]; |
---|
113 | |
---|
114 | /* Print Bus */ |
---|
115 | printf("\n\n/* PCI BUS %d - Bridge at [%x:%x:%x] */\n\n", |
---|
116 | bus->num, PCI_DEV_EXPAND(bus->dev.busdevfun)); |
---|
117 | get_bus_name(bus, name); |
---|
118 | printf("%sstruct pci_bus %s = {\n", |
---|
119 | bus->num == 0 ? "" : "static ", name); |
---|
120 | printf("\t.dev = {\n"); |
---|
121 | pci_cfg_print_device(&bus->dev, "\t\t"); |
---|
122 | printf("\t},\n"); |
---|
123 | if (bus->devs == NULL) { |
---|
124 | printf("\t.devs = NULL,\n"); |
---|
125 | } else { |
---|
126 | get_device_name(bus->devs, name); |
---|
127 | printf("\t.devs = &%s,\n", name); |
---|
128 | } |
---|
129 | printf("\t.flags = 0x%x,\n", bus->flags); |
---|
130 | printf("\t.num = %d,\n", bus->num); |
---|
131 | printf("\t.pri = %d,\n", bus->pri); |
---|
132 | printf("\t.sord = %d,\n", bus->sord); |
---|
133 | printf("};\n"); |
---|
134 | |
---|
135 | /* Print all child devices */ |
---|
136 | pci_for_each_child(bus, pci_cfg_print_dev, NULL, 0); |
---|
137 | |
---|
138 | return 0; |
---|
139 | } |
---|
140 | |
---|
141 | static int pci_cfg_print_forw_dev(struct pci_dev *dev, void *unused) |
---|
142 | { |
---|
143 | if ((dev->flags & PCI_DEV_BRIDGE) == 0) { |
---|
144 | printf("static struct pci_dev dev_%x_%x_%x;\n", |
---|
145 | PCI_DEV_EXPAND(dev->busdevfun)); |
---|
146 | } |
---|
147 | return 0; |
---|
148 | } |
---|
149 | |
---|
150 | void pci_cfg_print(void) |
---|
151 | { |
---|
152 | int i; |
---|
153 | |
---|
154 | printf("\n\n/*** PCI Configuration ***/\n\n"); |
---|
155 | printf("#include <stdlib.h>\n"); |
---|
156 | printf("#define PCI_CFG_STATIC_LIB\n"); |
---|
157 | printf("#include <pci/cfg.h>\n\n"); |
---|
158 | printf("#define PCIRES_EMPTY {0}\n\n"); |
---|
159 | |
---|
160 | /* Forward declaration for all devices / buses */ |
---|
161 | printf("/* FORWARD BUS DECLARATIONS */\n"); |
---|
162 | for (i = 0; i < pci_bus_count(); i++) { |
---|
163 | if (i == 0) |
---|
164 | printf("struct pci_bus pci_hb;\n"); |
---|
165 | else |
---|
166 | printf("static struct pci_bus bus%d;\n", i); |
---|
167 | } |
---|
168 | printf("\n/* FORWARD DEVICE DECLARATIONS */\n"); |
---|
169 | pci_for_each_dev(pci_cfg_print_forw_dev, NULL); |
---|
170 | |
---|
171 | pci_cfg_print_bus(&pci_hb); |
---|
172 | } |
---|