Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * sconfig, coreboot device tree compiler |
| 3 | * |
| 4 | * Copyright (C) 2010 coresystems GmbH |
| 5 | * written by Patrick Georgi <patrick.georgi@coresystems.de> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include "sconfig.h" |
| 22 | #include "sconfig.tab.h" |
| 23 | |
Patrick Georgi | 7fc9e29 | 2010-07-15 15:59:07 +0000 | [diff] [blame] | 24 | extern int linenum; |
| 25 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 26 | struct device *head, *lastdev; |
| 27 | |
| 28 | struct header headers; |
| 29 | |
| 30 | static int devcount = 0; |
| 31 | |
| 32 | static struct device root; |
| 33 | static struct device mainboard = { |
| 34 | .name = "mainboard", |
| 35 | .name_underscore = "mainboard", |
| 36 | .id = 0, |
| 37 | .chip = &mainboard, |
| 38 | .type = chip, |
| 39 | .chiph_exists = 1, |
| 40 | .children = &root |
| 41 | }; |
| 42 | |
| 43 | static struct device root = { |
| 44 | .name = "dev_root", |
| 45 | .name_underscore = "dev_root", |
| 46 | .id = 0, |
| 47 | .chip = &mainboard, |
| 48 | .type = device, |
| 49 | .path = " .type = DEVICE_PATH_ROOT ", |
| 50 | .ops = "&default_dev_ops_root", |
| 51 | .parent = &root, |
| 52 | .bus = &root, |
| 53 | .enabled = 1 |
| 54 | }; |
| 55 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 56 | static struct device *new_dev(struct device *parent, struct device *bus) { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 57 | struct device *dev = malloc(sizeof(struct device)); |
| 58 | memset(dev, 0, sizeof(struct device)); |
| 59 | dev->id = ++devcount; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 60 | dev->parent = parent; |
| 61 | dev->bus = bus; |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 62 | dev->subsystem_vendor = -1; |
| 63 | dev->subsystem_device = -1; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 64 | head->next = dev; |
| 65 | head = dev; |
| 66 | return dev; |
| 67 | } |
| 68 | |
| 69 | static int device_match(struct device *a, struct device *b) { |
| 70 | if ((a->bustype == b->bustype) && (a->bus == b->bus) && (a->path_a == b->path_a) && (a->path_b == b->path_b)) |
| 71 | return 1; |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | void fold_in(struct device *parent) { |
| 76 | struct device *child = parent->children; |
| 77 | struct device *latest = 0; |
| 78 | while (child != latest) { |
| 79 | if (child->children) { |
| 80 | if (!latest) latest = child->children; |
| 81 | parent->latestchild->next_sibling = child->children; |
| 82 | parent->latestchild = child->latestchild; |
| 83 | } |
| 84 | child = child->next_sibling; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | int yywrap(void) { |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | void yyerror (char const *str) |
| 93 | { |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 94 | extern char *yytext; |
| 95 | fprintf (stderr, "line %d: %s: %s\n", linenum + 1, yytext, str); |
| 96 | exit(1); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void postprocess_devtree(void) { |
| 100 | root.next_sibling = root.children; |
| 101 | root.next_sibling->next_sibling = root.next_sibling->children; |
| 102 | |
| 103 | struct device *dev = &root; |
| 104 | while (dev) { |
| 105 | /* skip "chip" elements in children chain */ |
| 106 | while (dev->children && (dev->children->type == chip)) dev->children = dev->children->children; |
| 107 | /* skip "chip" elements and functions of the same device in sibling chain */ |
| 108 | while (dev->sibling && dev->sibling->used) dev->sibling = dev->sibling->sibling; |
| 109 | /* If end of chain, and parent is a chip, move on */ |
| 110 | if (!dev->sibling && (dev->parent->type == chip)) dev->sibling = dev->parent->sibling; |
| 111 | /* skip chips */ |
| 112 | while (dev->sibling && dev->sibling->type == chip) dev->sibling = dev->sibling->children; |
| 113 | /* skip duplicate function elements in nextdev chain */ |
| 114 | while (dev->nextdev && dev->nextdev->used) dev->nextdev = dev->nextdev->nextdev; |
| 115 | dev = dev->next_sibling; |
| 116 | } |
| 117 | } |
| 118 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 119 | struct device *new_chip(struct device *parent, struct device *bus, char *path) { |
| 120 | struct device *new_chip = new_dev(parent, bus); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 121 | new_chip->chiph_exists = 1; |
| 122 | new_chip->name = path; |
| 123 | new_chip->name_underscore = strdup(new_chip->name); |
| 124 | char *c; |
| 125 | for (c = new_chip->name_underscore; *c; c++) { |
| 126 | if (*c == '/') *c = '_'; |
| 127 | if (*c == '-') *c = '_'; |
| 128 | } |
| 129 | new_chip->type = chip; |
| 130 | new_chip->chip = new_chip; |
| 131 | |
| 132 | struct stat st; |
| 133 | char *chip_h = malloc(strlen(path)+12); |
| 134 | sprintf(chip_h, "src/%s/chip.h", path); |
| 135 | if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) |
| 136 | new_chip->chiph_exists = 0; |
| 137 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 138 | if (parent->latestchild) { |
| 139 | parent->latestchild->next_sibling = new_chip; |
| 140 | parent->latestchild->sibling = new_chip; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 141 | } |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 142 | parent->latestchild = new_chip; |
| 143 | if (!parent->children) |
| 144 | parent->children = new_chip; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 145 | return new_chip; |
| 146 | } |
| 147 | |
| 148 | void add_header(struct device *dev) { |
| 149 | if (dev->chiph_exists) { |
| 150 | int include_exists = 0; |
| 151 | struct header *h = &headers; |
| 152 | while (h->next) { |
| 153 | int result = strcmp(dev->name, h->next->name); |
| 154 | if (result == 0) { |
| 155 | include_exists = 1; |
| 156 | break; |
| 157 | } |
| 158 | if (result < 0) break; |
| 159 | h = h->next; |
| 160 | } |
| 161 | if (!include_exists) { |
| 162 | struct header *tmp = h->next; |
| 163 | h->next = malloc(sizeof(struct header)); |
| 164 | memset(h->next, 0, sizeof(struct header)); |
| 165 | h->next->name = dev->name; |
| 166 | h->next->next = tmp; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 171 | struct device *new_device(struct device *parent, struct device *busdev, const int bus, const char *devnum, int enabled) { |
| 172 | struct device *new_d = new_dev(parent, busdev); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 173 | new_d->bustype = bus; |
| 174 | |
| 175 | char *tmp; |
| 176 | new_d->path_a = strtol(strdup(devnum), &tmp, 16); |
| 177 | if (*tmp == '.') { |
| 178 | tmp++; |
| 179 | new_d->path_b = strtol(tmp, NULL, 16); |
| 180 | } |
| 181 | |
| 182 | char *name = malloc(10); |
| 183 | sprintf(name, "_dev%d", new_d->id); |
| 184 | new_d->name = name; |
| 185 | new_d->name_underscore = name; // shouldn't be necessary, but avoid 0-ptr |
| 186 | new_d->type = device; |
| 187 | new_d->enabled = enabled; |
| 188 | new_d->chip = new_d->parent->chip; |
| 189 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 190 | if (parent->latestchild) { |
| 191 | parent->latestchild->next_sibling = new_d; |
| 192 | parent->latestchild->sibling = new_d; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 193 | } |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 194 | parent->latestchild = new_d; |
| 195 | if (!parent->children) |
| 196 | parent->children = new_d; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 197 | |
| 198 | lastdev->nextdev = new_d; |
| 199 | lastdev = new_d; |
| 200 | if (bus == PCI) { |
| 201 | new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}"; |
| 202 | } |
| 203 | if (bus == PNP) { |
| 204 | new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}"; |
| 205 | } |
| 206 | if (bus == I2C) { |
| 207 | new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x }}"; |
| 208 | } |
| 209 | if (bus == APIC) { |
| 210 | new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}"; |
| 211 | } |
| 212 | if (bus == APIC_CLUSTER) { |
| 213 | new_d->path = ".type=DEVICE_PATH_APIC_CLUSTER,{.apic_cluster={ .cluster = 0x%x }}"; |
| 214 | } |
| 215 | if (bus == PCI_DOMAIN) { |
| 216 | new_d->path = ".type=DEVICE_PATH_PCI_DOMAIN,{.pci_domain={ .domain = 0x%x }}"; |
| 217 | } |
| 218 | return new_d; |
| 219 | } |
| 220 | |
| 221 | void alias_siblings(struct device *d) { |
| 222 | while (d) { |
| 223 | int link = 0; |
| 224 | struct device *cmp = d->next_sibling; |
| 225 | while (cmp && (cmp->bus == d->bus) && (cmp->path_a == d->path_a) && (cmp->path_b == d->path_b)) { |
| 226 | if (cmp->type==device && !cmp->used) { |
| 227 | if (device_match(d, cmp)) { |
| 228 | d->multidev = 1; |
| 229 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 230 | cmp->id = d->id; |
| 231 | cmp->name = d->name; |
| 232 | cmp->used = 1; |
| 233 | cmp->link = ++link; |
| 234 | } |
| 235 | } |
| 236 | cmp = cmp->next_sibling; |
| 237 | } |
| 238 | d = d->next_sibling; |
| 239 | } |
| 240 | } |
| 241 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 242 | void add_resource(struct device *dev, int type, int index, int base) { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 243 | struct resource *r = malloc(sizeof(struct resource)); |
| 244 | memset (r, 0, sizeof(struct resource)); |
| 245 | r->type = type; |
| 246 | r->index = index; |
| 247 | r->base = base; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 248 | if (dev->res) { |
| 249 | struct resource *head = dev->res; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 250 | while (head->next) head = head->next; |
| 251 | head->next = r; |
| 252 | } else { |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 253 | dev->res = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 254 | } |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 255 | dev->rescnt++; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 258 | void add_register(struct device *dev, char *name, char *val) { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 259 | struct reg *r = malloc(sizeof(struct reg)); |
| 260 | memset (r, 0, sizeof(struct reg)); |
| 261 | r->key = name; |
| 262 | r->value = val; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 263 | if (dev->reg) { |
| 264 | struct reg *head = dev->reg; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 265 | // sorting to be equal to sconfig's behaviour |
| 266 | int sort = strcmp(r->key, head->key); |
| 267 | if (sort == 0) { |
| 268 | printf("ERROR: duplicate 'register' key.\n"); |
| 269 | exit(1); |
| 270 | } |
| 271 | if (sort<0) { |
| 272 | r->next = head; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 273 | dev->reg = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 274 | } else { |
| 275 | while ((head->next) && (strcmp(head->next->key, r->key)<0)) head = head->next; |
| 276 | r->next = head->next; |
| 277 | head->next = r; |
| 278 | } |
| 279 | } else { |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 280 | dev->reg = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 284 | void add_pci_subsystem_ids(struct device *dev, int vendor, int device, int inherit) |
| 285 | { |
| 286 | if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) { |
| 287 | printf("ERROR: 'subsystem' only allowed for PCI devices\n"); |
| 288 | exit(1); |
| 289 | } |
| 290 | |
| 291 | dev->subsystem_vendor = vendor; |
| 292 | dev->subsystem_device = device; |
| 293 | dev->inherit_subsystem = inherit; |
| 294 | } |
| 295 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 296 | static void pass0(FILE *fil, struct device *ptr) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 297 | if (ptr->type == device && ptr->id == 0) |
| 298 | fprintf(fil, "struct bus %s_links[];\n", ptr->name); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 299 | if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) { |
Stefan Reinauer | df61dd2 | 2010-08-09 12:02:00 +0000 | [diff] [blame] | 300 | fprintf(fil, "static struct device %s;\n", ptr->name); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 301 | if (ptr->rescnt > 0) |
| 302 | fprintf(fil, "struct resource %s_res[];\n", ptr->name); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 303 | if (ptr->children || ptr->multidev) |
| 304 | fprintf(fil, "struct bus %s_links[];\n", ptr->name); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 305 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static void pass1(FILE *fil, struct device *ptr) { |
| 309 | if (!ptr->used && (ptr->type == device)) { |
Stefan Reinauer | df61dd2 | 2010-08-09 12:02:00 +0000 | [diff] [blame] | 310 | if (ptr->id != 0) |
| 311 | fprintf(fil, "static ", ptr->name); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 312 | fprintf(fil, "struct device %s = {\n", ptr->name); |
| 313 | fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0"); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 314 | fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 315 | fprintf(fil, "\t.path = {"); |
| 316 | fprintf(fil, ptr->path, ptr->path_a, ptr->path_b); |
| 317 | fprintf(fil, "},\n"); |
| 318 | fprintf(fil, "\t.enabled = %d,\n", ptr->enabled); |
| 319 | fprintf(fil, "\t.on_mainboard = 1,\n"); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 320 | if (ptr->subsystem_vendor > 0) |
| 321 | fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", ptr->subsystem_vendor); |
| 322 | |
| 323 | if (ptr->subsystem_device > 0) |
| 324 | fprintf(fil, "\t.subsystem_device = 0x%04x,\n", ptr->subsystem_device); |
| 325 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 326 | if (ptr->rescnt > 0) { |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 327 | fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 328 | } |
| 329 | int link = 0; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 330 | if (ptr->children || ptr->multidev) |
| 331 | fprintf(fil, "\t.link_list = &%s_links[0],\n", ptr->name); |
| 332 | else |
| 333 | fprintf(fil, "\t.link_list = NULL,\n", ptr->name); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 334 | if (ptr->sibling) |
| 335 | fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name); |
| 336 | if (ptr->chip->chiph_exists) { |
| 337 | fprintf(fil, "\t.chip_ops = &%s_ops,\n", ptr->chip->name_underscore); |
| 338 | fprintf(fil, "\t.chip_info = &%s_info_%d,\n", ptr->chip->name_underscore, ptr->chip->id); |
| 339 | } |
| 340 | if (ptr->nextdev) |
| 341 | fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name); |
| 342 | fprintf(fil, "};\n"); |
| 343 | } |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 344 | if (ptr->rescnt > 0) { |
| 345 | int i=1; |
| 346 | fprintf(fil, "struct resource %s_res[] = {\n", ptr->name); |
| 347 | struct resource *r = ptr->res; |
| 348 | while (r) { |
| 349 | fprintf(fil, "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_"); |
| 350 | if (r->type == IRQ) fprintf(fil, "IRQ"); |
| 351 | if (r->type == DRQ) fprintf(fil, "DRQ"); |
| 352 | if (r->type == IO) fprintf(fil, "IO"); |
| 353 | fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, r->base); |
| 354 | if (r->next) |
| 355 | fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, i++); |
| 356 | else |
| 357 | fprintf(fil, ".next=NULL },\n"); |
| 358 | r = r->next; |
| 359 | } |
| 360 | fprintf(fil, "\t };\n"); |
| 361 | } |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 362 | if (!ptr->used && ptr->type == device && (ptr->children || ptr->multidev)) { |
| 363 | fprintf(fil, "struct bus %s_links[] = {\n", ptr->name); |
| 364 | if (ptr->multidev) { |
| 365 | struct device *d = ptr; |
| 366 | while (d) { |
| 367 | if (device_match(d, ptr)) { |
| 368 | fprintf(fil, "\t\t[%d] = {\n", d->link); |
| 369 | fprintf(fil, "\t\t\t.link_num = %d,\n", d->link); |
| 370 | fprintf(fil, "\t\t\t.dev = &%s,\n", d->name); |
| 371 | if (d->children) |
| 372 | fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name); |
Myles Watson | 1965a23 | 2010-06-10 04:06:52 +0000 | [diff] [blame] | 373 | if (d->next_sibling && device_match(d->next_sibling, ptr)) |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 374 | fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", d->name, d->link+1); |
| 375 | else |
| 376 | fprintf(fil, "\t\t\t.next = NULL,\n"); |
| 377 | fprintf(fil, "\t\t},\n"); |
| 378 | } |
| 379 | d = d->next_sibling; |
| 380 | } |
| 381 | } else { |
| 382 | if (ptr->children) { |
| 383 | fprintf(fil, "\t\t[0] = {\n"); |
| 384 | fprintf(fil, "\t\t\t.link_num = 0,\n"); |
| 385 | fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name); |
| 386 | fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name); |
| 387 | fprintf(fil, "\t\t\t.next = NULL,\n"); |
| 388 | fprintf(fil, "\t\t},\n"); |
| 389 | } |
| 390 | } |
| 391 | fprintf(fil, "\t};\n"); |
| 392 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 393 | if ((ptr->type == chip) && (ptr->chiph_exists)) { |
| 394 | if (ptr->reg) { |
| 395 | fprintf(fil, "struct %s_config %s_info_%d\t= {\n", ptr->name_underscore, ptr->name_underscore, ptr->id); |
| 396 | struct reg *r = ptr->reg; |
| 397 | while (r) { |
| 398 | fprintf(fil, "\t.%s = %s,\n", r->key, r->value); |
| 399 | r = r->next; |
| 400 | } |
| 401 | fprintf(fil, "};\n\n"); |
| 402 | } else { |
| 403 | fprintf(fil, "struct %s_config %s_info_%d;\n", ptr->name_underscore, ptr->name_underscore, ptr->id); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | static void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *, struct device*), struct device *chips) { |
| 409 | do { |
| 410 | func(fil, ptr); |
| 411 | ptr = ptr->next_sibling; |
| 412 | } while (ptr); |
| 413 | } |
| 414 | |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 415 | static void inherit_subsystem_ids(FILE *file, struct device *dev) |
| 416 | { |
| 417 | struct device *p; |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 418 | |
| 419 | if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) { |
| 420 | /* user already gave us a subsystem vendor/device */ |
| 421 | return; |
| 422 | } |
| 423 | |
Sylvain "ythier" Hitier | 5325a48 | 2011-03-01 21:57:11 +0000 | [diff] [blame^] | 424 | for(p = dev; p && p != p->parent; p = p->parent) { |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 425 | |
| 426 | if (p->bustype != PCI && p->bustype != PCI_DOMAIN) |
| 427 | continue; |
| 428 | |
| 429 | if (p->inherit_subsystem) { |
| 430 | dev->subsystem_vendor = p->subsystem_vendor; |
| 431 | dev->subsystem_device = p->subsystem_device; |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 437 | int main(int argc, char** argv) { |
| 438 | if (argc != 3) { |
| 439 | printf("usage: sconfig vendor/mainboard outputdir\n"); |
| 440 | return 1; |
| 441 | } |
| 442 | char *mainboard=argv[1]; |
| 443 | char *outputdir=argv[2]; |
| 444 | char *devtree=malloc(strlen(mainboard)+30); |
| 445 | char *outputc=malloc(strlen(outputdir)+10); |
| 446 | sprintf(devtree, "src/mainboard/%s/devicetree.cb", mainboard); |
| 447 | sprintf(outputc, "%s/static.c", outputdir); |
| 448 | |
| 449 | headers.next = malloc(sizeof(struct header)); |
| 450 | headers.next->name = malloc(strlen(mainboard)+12); |
| 451 | headers.next->next = 0; |
| 452 | sprintf(headers.next->name, "mainboard/%s", mainboard); |
| 453 | |
| 454 | FILE *filec = fopen(devtree, "r"); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 455 | if (!filec) { |
| 456 | fprintf(stderr, "Could not open file '%s' for reading: ", devtree); |
| 457 | perror(NULL); |
| 458 | exit(1); |
| 459 | } |
| 460 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 461 | yyrestart(filec); |
| 462 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 463 | lastdev = head = &root; |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 464 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 465 | yyparse(); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 466 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 467 | fclose(filec); |
| 468 | |
| 469 | if ((head->type == chip) && (!head->chiph_exists)) { |
| 470 | struct device *tmp = head; |
| 471 | head = &root; |
| 472 | while (head->next != tmp) head = head->next; |
| 473 | } |
| 474 | |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 475 | FILE *staticc = fopen(outputc, "w"); |
| 476 | if (!staticc) { |
| 477 | fprintf(stderr, "Could not open file '%s' for writing: ", outputc); |
| 478 | perror(NULL); |
| 479 | exit(1); |
| 480 | } |
| 481 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 482 | fprintf(staticc, "#include <device/device.h>\n"); |
| 483 | fprintf(staticc, "#include <device/pci.h>\n"); |
| 484 | struct header *h = &headers; |
| 485 | while (h->next) { |
| 486 | h = h->next; |
| 487 | fprintf(staticc, "#include \"%s/chip.h\"\n", h->name); |
| 488 | } |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 489 | |
| 490 | walk_device_tree(staticc, &root, inherit_subsystem_ids, NULL); |
| 491 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 492 | fprintf(staticc, "\n/* pass 0 */\n"); |
| 493 | walk_device_tree(staticc, &root, pass0, NULL); |
Myles Watson | 70679a0 | 2010-09-01 21:03:03 +0000 | [diff] [blame] | 494 | fprintf(staticc, "\n/* pass 1 */\nstruct mainboard_config mainboard_info_0;\nstruct device *last_dev = &%s;\n", lastdev->name); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 495 | walk_device_tree(staticc, &root, pass1, NULL); |
| 496 | |
| 497 | fclose(staticc); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 498 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 499 | return 0; |
| 500 | } |