source: rtems/cpukit/libpci/pci_cfg_print_code.c @ 0decc806

4.115
Last change on this file since 0decc806 was 71e8a5c, checked in by Daniel Hellstrom <daniel@…>, on 02/27/15 at 15:45:59

LIBPCI: moved copyright into a single line

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