Sconfig: parse Kconfig options from devicetree.cb
[coreboot.git] / util / sconfig / main.c
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
24 extern int linenum;
25
26 struct device *head, *lastdev;
27
28 struct header headers;
29
30 static int devcount = 0;
31
32 typedef enum {
33         STATIC_MODE,
34         BOOTBLOCK_MODE,
35         KCONFIG_MODE
36 } scan_t;
37
38 static scan_t scan_mode = STATIC_MODE;
39
40 static struct device root;
41 static struct device mainboard = {
42         .name = "mainboard",
43         .name_underscore = "mainboard",
44         .id = 0,
45         .chip = &mainboard,
46         .type = chip,
47         .chiph_exists = 1,
48         .children = &root
49 };
50
51 static struct device root = {
52         .name = "dev_root",
53         .name_underscore = "dev_root",
54         .id = 0,
55         .chip = &mainboard,
56         .type = device,
57         .path = " .type = DEVICE_PATH_ROOT ",
58         .ops = "&default_dev_ops_root",
59         .parent = &root,
60         .bus = &root,
61         .enabled = 1
62 };
63
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));
67         dev->id = ++devcount;
68         dev->parent = parent;
69         dev->bus = bus;
70         dev->subsystem_vendor = -1;
71         dev->subsystem_device = -1;
72         head->next = dev;
73         head = dev;
74         return dev;
75 }
76
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))
79                 return 1;
80         return 0;
81 }
82
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;
91                 }
92                 child = child->next_sibling;
93         }
94 }
95
96 int yywrap(void) {
97         return 1;
98 }
99
100 void yyerror (char const *str)
101 {
102         extern char *yytext;
103         fprintf (stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
104         exit(1);
105 }
106
107 void postprocess_devtree(void) {
108         root.next_sibling = root.children;
109         root.next_sibling->next_sibling = root.next_sibling->children;
110
111         struct device *dev = &root;
112         while (dev) {
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;
119                 /* skip chips */
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;
124         }
125 }
126
127 void translate_name(char *str, int uppercase)
128 {
129         char *c;
130         for (c = str; *c; c++) {
131                 if (*c == '/') *c = '_';
132                 if (*c == '-') *c = '_';
133                 if (uppercase)
134                         *c = toupper(*c);
135         }
136 }
137
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;
146
147         struct stat st;
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",
152                                 path);
153                 exit(1);
154         }
155
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);
160
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;
164         }
165
166         if (parent->latestchild) {
167                 parent->latestchild->next_sibling = new_chip;
168                 parent->latestchild->sibling = new_chip;
169         }
170         parent->latestchild = new_chip;
171         if (!parent->children)
172                 parent->children = new_chip;
173         return new_chip;
174 }
175
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;
180                 while (h->next) {
181                         int result = strcmp(dev->name, h->next->name);
182                         if (result == 0) {
183                                 include_exists = 1;
184                                 break;
185                         }
186                         if (result < 0) break;
187                         h = h->next;
188                 }
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;
194                         h->next->next = tmp;
195                 }
196         }
197 }
198
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;
202
203         char *tmp;
204         new_d->path_a = strtol(strdup(devnum), &tmp, 16);
205         if (*tmp == '.') {
206                 tmp++;
207                 new_d->path_b = strtol(tmp, NULL, 16);
208         }
209
210         char *name = malloc(10);
211         sprintf(name, "_dev%d", new_d->id);
212         new_d->name = name;
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;
217
218         if (parent->latestchild) {
219                 parent->latestchild->next_sibling = new_d;
220                 parent->latestchild->sibling = new_d;
221         }
222         parent->latestchild = new_d;
223         if (!parent->children)
224                 parent->children = new_d;
225
226         lastdev->nextdev = new_d;
227         lastdev = new_d;
228         if (bus == PCI) {
229                 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
230         }
231         if (bus == PNP) {
232                 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
233         }
234         if (bus == I2C) {
235                 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x }}";
236         }
237         if (bus == APIC) {
238                 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
239         }
240         if (bus == APIC_CLUSTER) {
241                 new_d->path = ".type=DEVICE_PATH_APIC_CLUSTER,{.apic_cluster={ .cluster = 0x%x }}";
242         }
243         if (bus == PCI_DOMAIN) {
244                 new_d->path = ".type=DEVICE_PATH_PCI_DOMAIN,{.pci_domain={ .domain = 0x%x }}";
245         }
246         return new_d;
247 }
248
249 void alias_siblings(struct device *d) {
250         while (d) {
251                 int link = 0;
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)) {
256                                         d->multidev = 1;
257
258                                         cmp->id = d->id;
259                                         cmp->name = d->name;
260                                         cmp->used = 1;
261                                         cmp->link = ++link;
262                                 }
263                         }
264                         cmp = cmp->next_sibling;
265                 }
266                 d = d->next_sibling;
267         }
268 }
269
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));
273         r->type = type;
274         r->index = index;
275         r->base = base;
276         if (dev->res) {
277                 struct resource *head = dev->res;
278                 while (head->next) head = head->next;
279                 head->next = r;
280         } else {
281                 dev->res = r;
282         }
283         dev->rescnt++;
284 }
285
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));
289         r->key = name;
290         r->value = val;
291         if (dev->reg) {
292                 struct reg *head = dev->reg;
293                 // sorting to be equal to sconfig's behaviour
294                 int sort = strcmp(r->key, head->key);
295                 if (sort == 0) {
296                         printf("ERROR: duplicate 'register' key.\n");
297                         exit(1);
298                 }
299                 if (sort<0) {
300                         r->next = head;
301                         dev->reg = r;
302                 } else {
303                         while ((head->next) && (strcmp(head->next->key, r->key)<0)) head = head->next;
304                         r->next = head->next;
305                         head->next = r;
306                 }
307         } else {
308                 dev->reg = r;
309         }
310 }
311
312 void add_pci_subsystem_ids(struct device *dev, int vendor, int device, int inherit)
313 {
314         if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) {
315                 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
316                 exit(1);
317         }
318
319         dev->subsystem_vendor = vendor;
320         dev->subsystem_device = device;
321         dev->inherit_subsystem = inherit;
322 }
323
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);
329                 if (ptr->rescnt > 0)
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);
333         }
334 }
335
336 static void pass1(FILE *fil, struct device *ptr) {
337         if (!ptr->used && (ptr->type == device)) {
338                 if (ptr->id != 0)
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);
350
351                 if (ptr->subsystem_device > 0)
352                         fprintf(fil, "\t.subsystem_device = 0x%04x,\n", ptr->subsystem_device);
353
354                 if (ptr->rescnt > 0) {
355                         fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name);
356                 }
357                 int link = 0;
358                 if (ptr->children || ptr->multidev)
359                         fprintf(fil, "\t.link_list = &%s_links[0],\n", ptr->name);
360                 else
361                         fprintf(fil, "\t.link_list = NULL,\n", ptr->name);
362                 if (ptr->sibling)
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);
367                 }
368                 if (ptr->nextdev)
369                         fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name);
370                 fprintf(fil, "};\n");
371         }
372         if (ptr->rescnt > 0) {
373                 int i=1;
374                 fprintf(fil, "struct resource %s_res[] = {\n", ptr->name);
375                 struct resource *r = ptr->res;
376                 while (r) {
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);
382                         if (r->next)
383                                 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, i++);
384                         else
385                                 fprintf(fil, ".next=NULL },\n");
386                         r = r->next;
387                 }
388                 fprintf(fil, "\t };\n");
389         }
390         if (!ptr->used && ptr->type == device && (ptr->children || ptr->multidev)) {
391                 fprintf(fil, "struct bus %s_links[] = {\n", ptr->name);
392                 if (ptr->multidev) {
393                         struct device *d = ptr;
394                         while (d) {
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);
399                                         if (d->children)
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);
403                                         else
404                                                 fprintf(fil, "\t\t\t.next = NULL,\n");
405                                         fprintf(fil, "\t\t},\n");
406                                 }
407                                 d = d->next_sibling;
408                         }
409                 } else {
410                         if (ptr->children) {
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");
417                         }
418                 }
419                 fprintf(fil, "\t};\n");
420         }
421         if ((ptr->type == chip) && (ptr->chiph_exists)) {
422                 if (ptr->reg) {
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;
426                         while (r) {
427                                 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
428                                 r = r->next;
429                         }
430                         fprintf(fil, "};\n\n");
431                 } else {
432                         fprintf(fil, "struct %s_config %s_info_%d;\n",
433                                 ptr->name_underscore, ptr->name_underscore, ptr->id);
434                 }
435         }
436 }
437
438 static void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *, struct device*), struct device *chips) {
439         do {
440                 func(fil, ptr);
441                 ptr = ptr->next_sibling;
442         } while (ptr);
443 }
444
445 static void inherit_subsystem_ids(FILE *file, struct device *dev)
446 {
447         struct device *p;
448
449         if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
450                 /* user already gave us a subsystem vendor/device */
451                 return;
452         }
453
454         for(p = dev; p && p != p->parent; p = p->parent) {
455
456                 if (p->bustype != PCI && p->bustype != PCI_DOMAIN)
457                         continue;
458
459                 if (p->inherit_subsystem) {
460                         dev->subsystem_vendor = p->subsystem_vendor;
461                         dev->subsystem_device = p->subsystem_device;
462                         break;
463                 }
464         }
465 }
466
467 static void usage(void)
468 {
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");
474         exit (1);
475 }
476
477
478 int main(int argc, char** argv) {
479         if (argc < 3)
480                 usage();
481
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);
486         char *outputc;
487
488         if (argc == 3) {
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)) {
493
494                 switch (argv[3][1]) {
495                 case 's':
496                         scan_mode = STATIC_MODE;
497                         break;
498                 case 'b':
499                         scan_mode = BOOTBLOCK_MODE;
500                         break;
501                 case 'k':
502                         scan_mode = KCONFIG_MODE;
503                         break;
504                 default:
505                         usage();
506                         break;
507                 }
508                 char *outputfile=argv[4];
509
510                 outputc=malloc(strlen(outputdir)+strlen(outputfile)+2);
511                 sprintf(outputc, "%s/%s", outputdir, outputfile);
512         }
513
514         headers.next = 0;
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);
520         }
521
522         FILE *filec = fopen(devtree, "r");
523         if (!filec) {
524                 fprintf(stderr, "Could not open file '%s' for reading: ", devtree);
525                 perror(NULL);
526                 exit(1);
527         }
528
529         yyrestart(filec);
530
531         lastdev = head = &root;
532
533         yyparse();
534
535         fclose(filec);
536
537         if ((head->type == chip) && (!head->chiph_exists)) {
538                 struct device *tmp = head;
539                 head = &root;
540                 while (head->next != tmp) head = head->next;
541         }
542
543         FILE *autogen = fopen(outputc, "w");
544         if (!autogen) {
545                 fprintf(stderr, "Could not open file '%s' for writing: ", outputc);
546                 perror(NULL);
547                 exit(1);
548         }
549
550         struct header *h;
551         if (scan_mode == STATIC_MODE) {
552
553                 fprintf(autogen, "#include <device/device.h>\n");
554                 fprintf(autogen, "#include <device/pci.h>\n");
555                 h = &headers;
556                 while (h->next) {
557                         h = h->next;
558                         fprintf(autogen, "#include \"%s/chip.h\"\n", h->name);
559                 }
560
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);
567
568         } else if (scan_mode == BOOTBLOCK_MODE) {
569                 h = &headers;
570                 while (h->next) {
571                         h = h->next;
572                         fprintf(autogen, "#include \"%s/bootblock.c\"\n", h->name);
573                 }
574
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");
580                 h = &headers;
581                 while (h->next) {
582                         h = h->next;
583                         translate_name(h->name, 0);
584                         fprintf(autogen, "\tinit_%s();\n", h->name);
585                 }
586
587                 fprintf(autogen, "\treturn 0;\n}\n");
588                 fprintf(autogen, "#endif\n");
589
590         } else if (scan_mode == KCONFIG_MODE) {
591                 fprintf(autogen, "\nconfig MAINBOARD_DIR\n\tstring\n");
592                 fprintf(autogen, "\tdefault %s\n", mainboard);
593
594                 fprintf(autogen, "\nconfig MAINBOARD_DEVTREE\n\tdef_bool y\n");
595                 h = &headers;
596                 while (h->next) {
597                         h = h->next;
598                         translate_name(h->name, 1);
599                         fprintf(autogen, "\tselect %s\n", h->name);
600                 }
601         }
602
603         fclose(autogen);
604
605         return 0;
606 }