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