Force coreboot mconf to create temp files in the output directory
[coreboot.git] / util / sconfig / main.c
old mode 100755 (executable)
new mode 100644 (file)
index a844111..181be38
 #include "sconfig.h"
 #include "sconfig.tab.h"
 
+extern int linenum;
+
 struct device *head, *lastdev;
 
 struct header headers;
 
 static int devcount = 0;
 
+typedef enum {
+       STATIC_MODE,
+       BOOTBLOCK_MODE,
+       KCONFIG_MODE
+} scan_t;
+
+static scan_t scan_mode = STATIC_MODE;
+
 static struct device root;
 static struct device mainboard = {
        .name = "mainboard",
@@ -57,6 +67,8 @@ static struct device *new_dev(struct device *parent, struct device *bus) {
        dev->id = ++devcount;
        dev->parent = parent;
        dev->bus = bus;
+       dev->subsystem_vendor = -1;
+       dev->subsystem_device = -1;
        head->next = dev;
        head = dev;
        return dev;
@@ -87,7 +99,9 @@ int yywrap(void) {
 
 void yyerror (char const *str)
 {
-       fprintf (stderr, "%s\n", str);
+       extern char *yytext;
+       fprintf (stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
+       exit(1);
 }
 
 void postprocess_devtree(void) {
@@ -110,24 +124,44 @@ void postprocess_devtree(void) {
        }
 }
 
+void translate_name(char *str, int uppercase)
+{
+       char *c;
+       for (c = str; *c; c++) {
+               if (*c == '/') *c = '_';
+               if (*c == '-') *c = '_';
+               if (uppercase)
+                       *c = toupper(*c);
+       }
+}
+
 struct device *new_chip(struct device *parent, struct device *bus, char *path) {
        struct device *new_chip = new_dev(parent, bus);
        new_chip->chiph_exists = 1;
        new_chip->name = path;
        new_chip->name_underscore = strdup(new_chip->name);
-       char *c;
-       for (c = new_chip->name_underscore; *c; c++) {
-               if (*c == '/') *c = '_';
-               if (*c == '-') *c = '_';
-       }
+       translate_name(new_chip->name_underscore, 0);
        new_chip->type = chip;
        new_chip->chip = new_chip;
 
        struct stat st;
-       char *chip_h = malloc(strlen(path)+12);
-       sprintf(chip_h, "src/%s/chip.h", path);
-       if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
-               new_chip->chiph_exists = 0;
+       char *chip_h = malloc(strlen(path)+18);
+       sprintf(chip_h, "src/%s", path);
+       if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
+               fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
+                               path);
+               exit(1);
+       }
+
+       if (scan_mode == STATIC_MODE)
+               sprintf(chip_h, "src/%s/chip.h", path);
+       else if (scan_mode == BOOTBLOCK_MODE)
+               sprintf(chip_h, "src/%s/bootblock.c", path);
+
+       if ((scan_mode == STATIC_MODE) || (scan_mode == BOOTBLOCK_MODE)) {
+               if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
+                       new_chip->chiph_exists = 0;
+       }
 
        if (parent->latestchild) {
                parent->latestchild->next_sibling = new_chip;
@@ -140,7 +174,7 @@ struct device *new_chip(struct device *parent, struct device *bus, char *path) {
 }
 
 void add_header(struct device *dev) {
-       if (dev->chiph_exists) {
+       if ((dev->chiph_exists) || (scan_mode == KCONFIG_MODE)){
                int include_exists = 0;
                struct header *h = &headers;
                while (h->next) {
@@ -221,8 +255,6 @@ void alias_siblings(struct device *d) {
                                if (device_match(d, cmp)) {
                                        d->multidev = 1;
 
-                                       cmp->aliased_name = malloc(12);
-                                       sprintf(cmp->aliased_name, "_dev%d", cmp->id);
                                        cmp->id = d->id;
                                        cmp->name = d->name;
                                        cmp->used = 1;
@@ -277,57 +309,56 @@ void add_register(struct device *dev, char *name, char *val) {
        }
 }
 
+void add_pci_subsystem_ids(struct device *dev, int vendor, int device, int inherit)
+{
+       if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) {
+               printf("ERROR: 'subsystem' only allowed for PCI devices\n");
+               exit(1);
+       }
+
+       dev->subsystem_vendor = vendor;
+       dev->subsystem_device = device;
+       dev->inherit_subsystem = inherit;
+}
+
 static void pass0(FILE *fil, struct device *ptr) {
+       if (ptr->type == device && ptr->id == 0)
+               fprintf(fil, "struct bus %s_links[];\n", ptr->name);
        if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) {
-               fprintf(fil, "struct device %s;\n", ptr->name);
+               fprintf(fil, "static struct device %s;\n", ptr->name);
                if (ptr->rescnt > 0)
                        fprintf(fil, "struct resource %s_res[];\n", ptr->name);
+               if (ptr->children || ptr->multidev)
+                       fprintf(fil, "struct bus %s_links[];\n", ptr->name);
        }
-       if ((ptr->type == device) && (ptr->id != 0) && ptr->used)
-               fprintf(fil, "struct device %s;\n", ptr->aliased_name);
 }
 
 static void pass1(FILE *fil, struct device *ptr) {
        if (!ptr->used && (ptr->type == device)) {
+               if (ptr->id != 0)
+                       fprintf(fil, "static ", ptr->name);
                fprintf(fil, "struct device %s = {\n", ptr->name);
                fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0");
-               fprintf(fil, "\t.bus = &%s.link[%d],\n", ptr->bus->name, ptr->bus->link);
+               fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link);
                fprintf(fil, "\t.path = {");
                fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
                fprintf(fil, "},\n");
                fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
                fprintf(fil, "\t.on_mainboard = 1,\n");
+               if (ptr->subsystem_vendor > 0)
+                       fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", ptr->subsystem_vendor);
+
+               if (ptr->subsystem_device > 0)
+                       fprintf(fil, "\t.subsystem_device = 0x%04x,\n", ptr->subsystem_device);
+
                if (ptr->rescnt > 0) {
                        fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name);
                }
                int link = 0;
-               fprintf(fil, "\t.link = {\n");
-               if (ptr->multidev) {
-                       struct device *d = ptr;
-                       while (d) {
-                               if (device_match(d, ptr)) {
-                                       fprintf(fil, "\t\t[%d] = {\n", d->link);
-                                       fprintf(fil, "\t\t\t.link = %d,\n", d->link);
-                                       fprintf(fil, "\t\t\t.dev = &%s,\n", d->name);
-                                       if (d->children)
-                                               fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name);
-                                       fprintf(fil, "\t\t},\n");
-                                       link++;
-                               }
-                               d = d->next_sibling;
-                       }
-               } else {
-                       if (ptr->children) {
-                               fprintf(fil, "\t\t[0] = {\n");
-                               fprintf(fil, "\t\t\t.link = 0,\n");
-                               fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name);
-                               fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name);
-                               fprintf(fil, "\t\t},\n");
-                               link++;
-                       }
-               }
-               fprintf(fil, "\t},\n");
-               fprintf(fil, "\t.links = %d,\n", link);
+               if (ptr->children || ptr->multidev)
+                       fprintf(fil, "\t.link_list = &%s_links[0],\n", ptr->name);
+               else
+                       fprintf(fil, "\t.link_list = NULL,\n", ptr->name);
                if (ptr->sibling)
                        fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
                if (ptr->chip->chiph_exists) {
@@ -356,9 +387,41 @@ static void pass1(FILE *fil, struct device *ptr) {
                }
                fprintf(fil, "\t };\n");
        }
+       if (!ptr->used && ptr->type == device && (ptr->children || ptr->multidev)) {
+               fprintf(fil, "struct bus %s_links[] = {\n", ptr->name);
+               if (ptr->multidev) {
+                       struct device *d = ptr;
+                       while (d) {
+                               if (device_match(d, ptr)) {
+                                       fprintf(fil, "\t\t[%d] = {\n", d->link);
+                                       fprintf(fil, "\t\t\t.link_num = %d,\n", d->link);
+                                       fprintf(fil, "\t\t\t.dev = &%s,\n", d->name);
+                                       if (d->children)
+                                               fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name);
+                                       if (d->next_sibling && device_match(d->next_sibling, ptr))
+                                               fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", d->name, d->link+1);
+                                       else
+                                               fprintf(fil, "\t\t\t.next = NULL,\n");
+                                       fprintf(fil, "\t\t},\n");
+                               }
+                               d = d->next_sibling;
+                       }
+               } else {
+                       if (ptr->children) {
+                               fprintf(fil, "\t\t[0] = {\n");
+                               fprintf(fil, "\t\t\t.link_num = 0,\n");
+                               fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name);
+                               fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name);
+                               fprintf(fil, "\t\t\t.next = NULL,\n");
+                               fprintf(fil, "\t\t},\n");
+                       }
+               }
+               fprintf(fil, "\t};\n");
+       }
        if ((ptr->type == chip) && (ptr->chiph_exists)) {
                if (ptr->reg) {
-                       fprintf(fil, "struct %s_config %s_info_%d\t= {\n", ptr->name_underscore, ptr->name_underscore, ptr->id);
+                       fprintf(fil, "struct %s_config %s_info_%d\t= {\n",
+                               ptr->name_underscore, ptr->name_underscore, ptr->id);
                        struct reg *r = ptr->reg;
                        while (r) {
                                fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
@@ -366,7 +429,8 @@ static void pass1(FILE *fil, struct device *ptr) {
                        }
                        fprintf(fil, "};\n\n");
                } else {
-                       fprintf(fil, "struct %s_config %s_info_%d;\n", ptr->name_underscore, ptr->name_underscore, ptr->id);
+                       fprintf(fil, "struct %s_config %s_info_%d;\n",
+                               ptr->name_underscore, ptr->name_underscore, ptr->id);
                }
        }
 }
@@ -378,30 +442,96 @@ static void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *,
        } while (ptr);
 }
 
-int main(int argc, char** argv) {
-       if (argc != 3) {
-               printf("usage: sconfig vendor/mainboard outputdir\n");
-               return 1;
+static void inherit_subsystem_ids(FILE *file, struct device *dev)
+{
+       struct device *p;
+
+       if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
+               /* user already gave us a subsystem vendor/device */
+               return;
        }
+
+       for(p = dev; p && p != p->parent; p = p->parent) {
+
+               if (p->bustype != PCI && p->bustype != PCI_DOMAIN)
+                       continue;
+
+               if (p->inherit_subsystem) {
+                       dev->subsystem_vendor = p->subsystem_vendor;
+                       dev->subsystem_device = p->subsystem_device;
+                       break;
+               }
+       }
+}
+
+static void usage(void)
+{
+       printf("usage: sconfig vendor/mainboard outputdir [-{s|b|k} outputfile]\n");
+       printf("\t-s file\tcreate ramstage static device map\n");
+       printf("\t-b file\tcreate bootblock init_mainboard()\n");
+       printf("\t-k file\tcreate Kconfig devicetree section\n");
+       printf("Defaults to \"-s static.c\" if no {s|b|k} specified.\n");
+       exit (1);
+}
+
+
+int main(int argc, char** argv) {
+       if (argc < 3)
+               usage();
+
        char *mainboard=argv[1];
        char *outputdir=argv[2];
        char *devtree=malloc(strlen(mainboard)+30);
-       char *outputc=malloc(strlen(outputdir)+10);
        sprintf(devtree, "src/mainboard/%s/devicetree.cb", mainboard);
-       sprintf(outputc, "%s/static.c", outputdir);
+       char *outputc;
+
+       if (argc == 3) {
+               scan_mode = STATIC_MODE;
+               outputc=malloc(strlen(outputdir)+20);
+               sprintf(outputc, "%s/static.c", outputdir);
+       } else if ((argc == 5) && (argv[3][0] == '-') && (argv[3][2] == 0)) {
+
+               switch (argv[3][1]) {
+               case 's':
+                       scan_mode = STATIC_MODE;
+                       break;
+               case 'b':
+                       scan_mode = BOOTBLOCK_MODE;
+                       break;
+               case 'k':
+                       scan_mode = KCONFIG_MODE;
+                       break;
+               default:
+                       usage();
+                       break;
+               }
+               char *outputfile=argv[4];
 
-       headers.next = malloc(sizeof(struct header));
-       headers.next->name = malloc(strlen(mainboard)+12);
-       headers.next->next = 0;
-       sprintf(headers.next->name, "mainboard/%s", mainboard);
+               outputc=malloc(strlen(outputdir)+strlen(outputfile)+2);
+               sprintf(outputc, "%s/%s", outputdir, outputfile);
+       }
+
+       headers.next = 0;
+       if (scan_mode == STATIC_MODE) {
+               headers.next = malloc(sizeof(struct header));
+               headers.next->name = malloc(strlen(mainboard)+12);
+               headers.next->next = 0;
+               sprintf(headers.next->name, "mainboard/%s", mainboard);
+       }
 
        FILE *filec = fopen(devtree, "r");
-       yyrestart(filec);
+       if (!filec) {
+               fprintf(stderr, "Could not open file '%s' for reading: ", devtree);
+               perror(NULL);
+               exit(1);
+       }
 
-       FILE *staticc = fopen(outputc, "w");
+       yyrestart(filec);
 
        lastdev = head = &root;
+
        yyparse();
+
        fclose(filec);
 
        if ((head->type == chip) && (!head->chiph_exists)) {
@@ -410,18 +540,67 @@ int main(int argc, char** argv) {
                while (head->next != tmp) head = head->next;
        }
 
-       fprintf(staticc, "#include <device/device.h>\n");
-       fprintf(staticc, "#include <device/pci.h>\n");
-       struct header *h = &headers;
-       while (h->next) {
-               h = h->next;
-               fprintf(staticc, "#include \"%s/chip.h\"\n", h->name);
+       FILE *autogen = fopen(outputc, "w");
+       if (!autogen) {
+               fprintf(stderr, "Could not open file '%s' for writing: ", outputc);
+               perror(NULL);
+               exit(1);
+       }
+
+       struct header *h;
+       if (scan_mode == STATIC_MODE) {
+
+               fprintf(autogen, "#include <device/device.h>\n");
+               fprintf(autogen, "#include <device/pci.h>\n");
+               h = &headers;
+               while (h->next) {
+                       h = h->next;
+                       fprintf(autogen, "#include \"%s/chip.h\"\n", h->name);
+               }
+
+               walk_device_tree(autogen, &root, inherit_subsystem_ids, NULL);
+               fprintf(autogen, "\n/* pass 0 */\n");
+               walk_device_tree(autogen, &root, pass0, NULL);
+               fprintf(autogen, "\n/* pass 1 */\nstruct mainboard_config mainboard_info_0;\n"
+                                               "struct device *last_dev = &%s;\n", lastdev->name);
+               walk_device_tree(autogen, &root, pass1, NULL);
+
+       } else if (scan_mode == BOOTBLOCK_MODE) {
+               h = &headers;
+               while (h->next) {
+                       h = h->next;
+                       fprintf(autogen, "#include \"%s/bootblock.c\"\n", h->name);
+               }
+
+               fprintf(autogen, "\n#if CONFIG_HAS_MAINBOARD_BOOTBLOCK\n");
+               fprintf(autogen, "#include \"mainboard/%s/bootblock.c\"\n", mainboard);
+               fprintf(autogen, "#else\n");
+               fprintf(autogen, "static unsigned long init_mainboard(int bsp_cpu)\n{\n");
+               fprintf(autogen, "\tif (! bsp_cpu) return 0;\n");
+               h = &headers;
+               while (h->next) {
+                       h = h->next;
+                       translate_name(h->name, 0);
+                       fprintf(autogen, "\tinit_%s();\n", h->name);
+               }
+
+               fprintf(autogen, "\treturn 0;\n}\n");
+               fprintf(autogen, "#endif\n");
+
+       } else if (scan_mode == KCONFIG_MODE) {
+               fprintf(autogen, "\nconfig MAINBOARD_DIR\n\tstring\n");
+               fprintf(autogen, "\tdefault %s\n", mainboard);
+
+               fprintf(autogen, "\nconfig MAINBOARD_DEVTREE\n\tdef_bool y\n");
+               h = &headers;
+               while (h->next) {
+                       h = h->next;
+                       translate_name(h->name, 1);
+                       fprintf(autogen, "\tselect %s\n", h->name);
+               }
        }
-       fprintf(staticc, "\n/* pass 0 */\n");
-       walk_device_tree(staticc, &root, pass0, NULL);
-       fprintf(staticc, "\n/* pass 1 */\nstruct mainboard_config mainboard_info_0;\nstruct device **last_dev_p = &%s.next;\n", lastdev->name);
-       walk_device_tree(staticc, &root, pass1, NULL);
 
-       fclose(staticc);
+       fclose(autogen);
+
        return 0;
 }