2 * sconfig, coreboot device tree compiler
4 * Copyright (C) 2010 coresystems GmbH
5 * written by Patrick Georgi <patrick.georgi@coresystems.de>
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.
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.
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
22 #include "sconfig.tab.h"
26 struct device *head, *lastdev;
28 struct header headers;
30 static int devcount = 0;
38 static scan_t scan_mode = STATIC_MODE;
40 static struct device root;
41 static struct device mainboard = {
43 .name_underscore = "mainboard",
51 static struct device root = {
53 .name_underscore = "dev_root",
57 .path = " .type = DEVICE_PATH_ROOT ",
58 .ops = "&default_dev_ops_root",
64 static struct device *new_dev(struct device *parent, struct device *bus) {
65 struct device *dev = malloc(sizeof(struct device));
66 memset(dev, 0, sizeof(struct device));
70 dev->subsystem_vendor = -1;
71 dev->subsystem_device = -1;
77 static int device_match(struct device *a, struct device *b) {
78 if ((a->bustype == b->bustype) && (a->bus == b->bus) && (a->path_a == b->path_a) && (a->path_b == b->path_b))
83 void fold_in(struct device *parent) {
84 struct device *child = parent->children;
85 struct device *latest = 0;
86 while (child != latest) {
87 if (child->children) {
88 if (!latest) latest = child->children;
89 parent->latestchild->next_sibling = child->children;
90 parent->latestchild = child->latestchild;
92 child = child->next_sibling;
100 void yyerror (char const *str)
103 fprintf (stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
107 void postprocess_devtree(void) {
108 root.next_sibling = root.children;
109 root.next_sibling->next_sibling = root.next_sibling->children;
111 struct device *dev = &root;
113 /* skip "chip" elements in children chain */
114 while (dev->children && (dev->children->type == chip)) dev->children = dev->children->children;
115 /* skip "chip" elements and functions of the same device in sibling chain */
116 while (dev->sibling && dev->sibling->used) dev->sibling = dev->sibling->sibling;
117 /* If end of chain, and parent is a chip, move on */
118 if (!dev->sibling && (dev->parent->type == chip)) dev->sibling = dev->parent->sibling;
120 while (dev->sibling && dev->sibling->type == chip) dev->sibling = dev->sibling->children;
121 /* skip duplicate function elements in nextdev chain */
122 while (dev->nextdev && dev->nextdev->used) dev->nextdev = dev->nextdev->nextdev;
123 dev = dev->next_sibling;
127 void translate_name(char *str, int uppercase)
130 for (c = str; *c; c++) {
131 if (*c == '/') *c = '_';
132 if (*c == '-') *c = '_';
138 struct device *new_chip(struct device *parent, struct device *bus, char *path) {
139 struct device *new_chip = new_dev(parent, bus);
140 new_chip->chiph_exists = 1;
141 new_chip->name = path;
142 new_chip->name_underscore = strdup(new_chip->name);
143 translate_name(new_chip->name_underscore, 0);
144 new_chip->type = chip;
145 new_chip->chip = new_chip;
148 char *chip_h = malloc(strlen(path)+18);
149 sprintf(chip_h, "src/%s", path);
150 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
151 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
156 if (scan_mode == STATIC_MODE)
157 sprintf(chip_h, "src/%s/chip.h", path);
158 else if (scan_mode == BOOTBLOCK_MODE)
159 sprintf(chip_h, "src/%s/bootblock.c", path);
161 if ((scan_mode == STATIC_MODE) || (scan_mode == BOOTBLOCK_MODE)) {
162 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
163 new_chip->chiph_exists = 0;
166 if (parent->latestchild) {
167 parent->latestchild->next_sibling = new_chip;
168 parent->latestchild->sibling = new_chip;
170 parent->latestchild = new_chip;
171 if (!parent->children)
172 parent->children = new_chip;
176 void add_header(struct device *dev) {
177 if ((dev->chiph_exists) || (scan_mode == KCONFIG_MODE)){
178 int include_exists = 0;
179 struct header *h = &headers;
181 int result = strcmp(dev->name, h->next->name);
186 if (result < 0) break;
189 if (!include_exists) {
190 struct header *tmp = h->next;
191 h->next = malloc(sizeof(struct header));
192 memset(h->next, 0, sizeof(struct header));
193 h->next->name = dev->name;
199 struct device *new_device(struct device *parent, struct device *busdev, const int bus, const char *devnum, int enabled) {
200 struct device *new_d = new_dev(parent, busdev);
201 new_d->bustype = bus;
204 new_d->path_a = strtol(strdup(devnum), &tmp, 16);
207 new_d->path_b = strtol(tmp, NULL, 16);
210 char *name = malloc(10);
211 sprintf(name, "_dev%d", new_d->id);
213 new_d->name_underscore = name; // shouldn't be necessary, but avoid 0-ptr
214 new_d->type = device;
215 new_d->enabled = enabled;
216 new_d->chip = new_d->parent->chip;
218 if (parent->latestchild) {
219 parent->latestchild->next_sibling = new_d;
220 parent->latestchild->sibling = new_d;
222 parent->latestchild = new_d;
223 if (!parent->children)
224 parent->children = new_d;
226 lastdev->nextdev = new_d;
229 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
232 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
235 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x }}";
238 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
240 if (bus == APIC_CLUSTER) {
241 new_d->path = ".type=DEVICE_PATH_APIC_CLUSTER,{.apic_cluster={ .cluster = 0x%x }}";
243 if (bus == PCI_DOMAIN) {
244 new_d->path = ".type=DEVICE_PATH_PCI_DOMAIN,{.pci_domain={ .domain = 0x%x }}";
249 void alias_siblings(struct device *d) {
252 struct device *cmp = d->next_sibling;
253 while (cmp && (cmp->bus == d->bus) && (cmp->path_a == d->path_a) && (cmp->path_b == d->path_b)) {
254 if (cmp->type==device && !cmp->used) {
255 if (device_match(d, cmp)) {
264 cmp = cmp->next_sibling;
270 void add_resource(struct device *dev, int type, int index, int base) {
271 struct resource *r = malloc(sizeof(struct resource));
272 memset (r, 0, sizeof(struct resource));
277 struct resource *head = dev->res;
278 while (head->next) head = head->next;
286 void add_register(struct device *dev, char *name, char *val) {
287 struct reg *r = malloc(sizeof(struct reg));
288 memset (r, 0, sizeof(struct reg));
292 struct reg *head = dev->reg;
293 // sorting to be equal to sconfig's behaviour
294 int sort = strcmp(r->key, head->key);
296 printf("ERROR: duplicate 'register' key.\n");
303 while ((head->next) && (strcmp(head->next->key, r->key)<0)) head = head->next;
304 r->next = head->next;
312 void add_pci_subsystem_ids(struct device *dev, int vendor, int device, int inherit)
314 if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) {
315 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
319 dev->subsystem_vendor = vendor;
320 dev->subsystem_device = device;
321 dev->inherit_subsystem = inherit;
324 static void pass0(FILE *fil, struct device *ptr) {
325 if (ptr->type == device && ptr->id == 0)
326 fprintf(fil, "struct bus %s_links[];\n", ptr->name);
327 if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) {
328 fprintf(fil, "static struct device %s;\n", ptr->name);
330 fprintf(fil, "struct resource %s_res[];\n", ptr->name);
331 if (ptr->children || ptr->multidev)
332 fprintf(fil, "struct bus %s_links[];\n", ptr->name);
336 static void pass1(FILE *fil, struct device *ptr) {
337 if (!ptr->used && (ptr->type == device)) {
339 fprintf(fil, "static ", ptr->name);
340 fprintf(fil, "struct device %s = {\n", ptr->name);
341 fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0");
342 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link);
343 fprintf(fil, "\t.path = {");
344 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
345 fprintf(fil, "},\n");
346 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
347 fprintf(fil, "\t.on_mainboard = 1,\n");
348 if (ptr->subsystem_vendor > 0)
349 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", ptr->subsystem_vendor);
351 if (ptr->subsystem_device > 0)
352 fprintf(fil, "\t.subsystem_device = 0x%04x,\n", ptr->subsystem_device);
354 if (ptr->rescnt > 0) {
355 fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name);
358 if (ptr->children || ptr->multidev)
359 fprintf(fil, "\t.link_list = &%s_links[0],\n", ptr->name);
361 fprintf(fil, "\t.link_list = NULL,\n", ptr->name);
363 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
364 if (ptr->chip->chiph_exists) {
365 fprintf(fil, "\t.chip_ops = &%s_ops,\n", ptr->chip->name_underscore);
366 fprintf(fil, "\t.chip_info = &%s_info_%d,\n", ptr->chip->name_underscore, ptr->chip->id);
369 fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name);
370 fprintf(fil, "};\n");
372 if (ptr->rescnt > 0) {
374 fprintf(fil, "struct resource %s_res[] = {\n", ptr->name);
375 struct resource *r = ptr->res;
377 fprintf(fil, "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
378 if (r->type == IRQ) fprintf(fil, "IRQ");
379 if (r->type == DRQ) fprintf(fil, "DRQ");
380 if (r->type == IO) fprintf(fil, "IO");
381 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, r->base);
383 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, i++);
385 fprintf(fil, ".next=NULL },\n");
388 fprintf(fil, "\t };\n");
390 if (!ptr->used && ptr->type == device && (ptr->children || ptr->multidev)) {
391 fprintf(fil, "struct bus %s_links[] = {\n", ptr->name);
393 struct device *d = ptr;
395 if (device_match(d, ptr)) {
396 fprintf(fil, "\t\t[%d] = {\n", d->link);
397 fprintf(fil, "\t\t\t.link_num = %d,\n", d->link);
398 fprintf(fil, "\t\t\t.dev = &%s,\n", d->name);
400 fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name);
401 if (d->next_sibling && device_match(d->next_sibling, ptr))
402 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", d->name, d->link+1);
404 fprintf(fil, "\t\t\t.next = NULL,\n");
405 fprintf(fil, "\t\t},\n");
411 fprintf(fil, "\t\t[0] = {\n");
412 fprintf(fil, "\t\t\t.link_num = 0,\n");
413 fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name);
414 fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name);
415 fprintf(fil, "\t\t\t.next = NULL,\n");
416 fprintf(fil, "\t\t},\n");
419 fprintf(fil, "\t};\n");
421 if ((ptr->type == chip) && (ptr->chiph_exists)) {
423 fprintf(fil, "struct %s_config %s_info_%d\t= {\n",
424 ptr->name_underscore, ptr->name_underscore, ptr->id);
425 struct reg *r = ptr->reg;
427 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
430 fprintf(fil, "};\n\n");
432 fprintf(fil, "struct %s_config %s_info_%d;\n",
433 ptr->name_underscore, ptr->name_underscore, ptr->id);
438 static void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *, struct device*), struct device *chips) {
441 ptr = ptr->next_sibling;
445 static void inherit_subsystem_ids(FILE *file, struct device *dev)
449 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
450 /* user already gave us a subsystem vendor/device */
454 for(p = dev; p && p != p->parent; p = p->parent) {
456 if (p->bustype != PCI && p->bustype != PCI_DOMAIN)
459 if (p->inherit_subsystem) {
460 dev->subsystem_vendor = p->subsystem_vendor;
461 dev->subsystem_device = p->subsystem_device;
467 static void usage(void)
469 printf("usage: sconfig vendor/mainboard outputdir [-{s|b|k} outputfile]\n");
470 printf("\t-s file\tcreate ramstage static device map\n");
471 printf("\t-b file\tcreate bootblock init_mainboard()\n");
472 printf("\t-k file\tcreate Kconfig devicetree section\n");
473 printf("Defaults to \"-s static.c\" if no {s|b|k} specified.\n");
478 int main(int argc, char** argv) {
482 char *mainboard=argv[1];
483 char *outputdir=argv[2];
484 char *devtree=malloc(strlen(mainboard)+30);
485 sprintf(devtree, "src/mainboard/%s/devicetree.cb", mainboard);
489 scan_mode = STATIC_MODE;
490 outputc=malloc(strlen(outputdir)+20);
491 sprintf(outputc, "%s/static.c", outputdir);
492 } else if ((argc == 5) && (argv[3][0] == '-') && (argv[3][2] == 0)) {
494 switch (argv[3][1]) {
496 scan_mode = STATIC_MODE;
499 scan_mode = BOOTBLOCK_MODE;
502 scan_mode = KCONFIG_MODE;
508 char *outputfile=argv[4];
510 outputc=malloc(strlen(outputdir)+strlen(outputfile)+2);
511 sprintf(outputc, "%s/%s", outputdir, outputfile);
515 if (scan_mode == STATIC_MODE) {
516 headers.next = malloc(sizeof(struct header));
517 headers.next->name = malloc(strlen(mainboard)+12);
518 headers.next->next = 0;
519 sprintf(headers.next->name, "mainboard/%s", mainboard);
522 FILE *filec = fopen(devtree, "r");
524 fprintf(stderr, "Could not open file '%s' for reading: ", devtree);
531 lastdev = head = &root;
537 if ((head->type == chip) && (!head->chiph_exists)) {
538 struct device *tmp = head;
540 while (head->next != tmp) head = head->next;
543 FILE *autogen = fopen(outputc, "w");
545 fprintf(stderr, "Could not open file '%s' for writing: ", outputc);
551 if (scan_mode == STATIC_MODE) {
553 fprintf(autogen, "#include <device/device.h>\n");
554 fprintf(autogen, "#include <device/pci.h>\n");
558 fprintf(autogen, "#include \"%s/chip.h\"\n", h->name);
561 walk_device_tree(autogen, &root, inherit_subsystem_ids, NULL);
562 fprintf(autogen, "\n/* pass 0 */\n");
563 walk_device_tree(autogen, &root, pass0, NULL);
564 fprintf(autogen, "\n/* pass 1 */\nstruct mainboard_config mainboard_info_0;\n"
565 "struct device *last_dev = &%s;\n", lastdev->name);
566 walk_device_tree(autogen, &root, pass1, NULL);
568 } else if (scan_mode == BOOTBLOCK_MODE) {
572 fprintf(autogen, "#include \"%s/bootblock.c\"\n", h->name);
575 fprintf(autogen, "\n#if CONFIG_HAS_MAINBOARD_BOOTBLOCK\n");
576 fprintf(autogen, "#include \"mainboard/%s/bootblock.c\"\n", mainboard);
577 fprintf(autogen, "#else\n");
578 fprintf(autogen, "static unsigned long init_mainboard(int bsp_cpu)\n{\n");
579 fprintf(autogen, "\tif (! bsp_cpu) return 0;\n");
583 translate_name(h->name, 0);
584 fprintf(autogen, "\tinit_%s();\n", h->name);
587 fprintf(autogen, "\treturn 0;\n}\n");
588 fprintf(autogen, "#endif\n");
590 } else if (scan_mode == KCONFIG_MODE) {
591 fprintf(autogen, "\nconfig MAINBOARD_DIR\n\tstring\n");
592 fprintf(autogen, "\tdefault %s\n", mainboard);
594 fprintf(autogen, "\nconfig MAINBOARD_DEVTREE\n\tdef_bool y\n");
598 translate_name(h->name, 1);
599 fprintf(autogen, "\tselect %s\n", h->name);