blob: 181be387aee485e8db67b296bf7c4538e5ec7551 [file] [log] [blame]
Patrick Georgi114e7b22010-05-05 11:19:50 +00001/*
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 Georgi7fc9e292010-07-15 15:59:07 +000024extern int linenum;
25
Patrick Georgi114e7b22010-05-05 11:19:50 +000026struct device *head, *lastdev;
27
28struct header headers;
29
30static int devcount = 0;
31
Kyösti Mälkki472d9022011-12-05 20:33:55 +020032typedef enum {
33 STATIC_MODE,
34 BOOTBLOCK_MODE,
35 KCONFIG_MODE
36} scan_t;
37
38static scan_t scan_mode = STATIC_MODE;
39
Patrick Georgi114e7b22010-05-05 11:19:50 +000040static struct device root;
41static 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
51static 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
Patrick Georgi68befd52010-05-05 12:05:25 +000064static struct device *new_dev(struct device *parent, struct device *bus) {
Patrick Georgi114e7b22010-05-05 11:19:50 +000065 struct device *dev = malloc(sizeof(struct device));
66 memset(dev, 0, sizeof(struct device));
67 dev->id = ++devcount;
Patrick Georgi68befd52010-05-05 12:05:25 +000068 dev->parent = parent;
69 dev->bus = bus;
Sven Schnelle270a9082011-03-01 19:58:15 +000070 dev->subsystem_vendor = -1;
71 dev->subsystem_device = -1;
Patrick Georgi114e7b22010-05-05 11:19:50 +000072 head->next = dev;
73 head = dev;
74 return dev;
75}
76
77static 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
83void 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
96int yywrap(void) {
97 return 1;
98}
99
100void yyerror (char const *str)
101{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000102 extern char *yytext;
103 fprintf (stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
104 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000105}
106
107void 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
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200127void 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
Patrick Georgi68befd52010-05-05 12:05:25 +0000138struct device *new_chip(struct device *parent, struct device *bus, char *path) {
139 struct device *new_chip = new_dev(parent, bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000140 new_chip->chiph_exists = 1;
141 new_chip->name = path;
142 new_chip->name_underscore = strdup(new_chip->name);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200143 translate_name(new_chip->name_underscore, 0);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000144 new_chip->type = chip;
145 new_chip->chip = new_chip;
146
147 struct stat st;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200148 char *chip_h = malloc(strlen(path)+18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700149 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
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200156 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 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000165
Patrick Georgi68befd52010-05-05 12:05:25 +0000166 if (parent->latestchild) {
167 parent->latestchild->next_sibling = new_chip;
168 parent->latestchild->sibling = new_chip;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000169 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000170 parent->latestchild = new_chip;
171 if (!parent->children)
172 parent->children = new_chip;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000173 return new_chip;
174}
175
176void add_header(struct device *dev) {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200177 if ((dev->chiph_exists) || (scan_mode == KCONFIG_MODE)){
Patrick Georgi114e7b22010-05-05 11:19:50 +0000178 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
Patrick Georgi68befd52010-05-05 12:05:25 +0000199struct 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);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000201 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
Patrick Georgi68befd52010-05-05 12:05:25 +0000218 if (parent->latestchild) {
219 parent->latestchild->next_sibling = new_d;
220 parent->latestchild->sibling = new_d;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000221 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000222 parent->latestchild = new_d;
223 if (!parent->children)
224 parent->children = new_d;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000225
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
249void 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
Patrick Georgi114e7b22010-05-05 11:19:50 +0000258 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
Patrick Georgi68befd52010-05-05 12:05:25 +0000270void add_resource(struct device *dev, int type, int index, int base) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000271 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;
Patrick Georgi68befd52010-05-05 12:05:25 +0000276 if (dev->res) {
277 struct resource *head = dev->res;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000278 while (head->next) head = head->next;
279 head->next = r;
280 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000281 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000282 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000283 dev->rescnt++;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000284}
285
Patrick Georgi68befd52010-05-05 12:05:25 +0000286void add_register(struct device *dev, char *name, char *val) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000287 struct reg *r = malloc(sizeof(struct reg));
288 memset (r, 0, sizeof(struct reg));
289 r->key = name;
290 r->value = val;
Patrick Georgi68befd52010-05-05 12:05:25 +0000291 if (dev->reg) {
292 struct reg *head = dev->reg;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000293 // 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;
Patrick Georgi68befd52010-05-05 12:05:25 +0000301 dev->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000302 } 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 {
Patrick Georgi68befd52010-05-05 12:05:25 +0000308 dev->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000309 }
310}
311
Sven Schnelle270a9082011-03-01 19:58:15 +0000312void 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
Patrick Georgi114e7b22010-05-05 11:19:50 +0000324static void pass0(FILE *fil, struct device *ptr) {
Myles Watson894a3472010-06-09 22:41:35 +0000325 if (ptr->type == device && ptr->id == 0)
326 fprintf(fil, "struct bus %s_links[];\n", ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000327 if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) {
Stefan Reinauerdf61dd22010-08-09 12:02:00 +0000328 fprintf(fil, "static struct device %s;\n", ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000329 if (ptr->rescnt > 0)
330 fprintf(fil, "struct resource %s_res[];\n", ptr->name);
Myles Watson894a3472010-06-09 22:41:35 +0000331 if (ptr->children || ptr->multidev)
332 fprintf(fil, "struct bus %s_links[];\n", ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000333 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000334}
335
336static void pass1(FILE *fil, struct device *ptr) {
337 if (!ptr->used && (ptr->type == device)) {
Stefan Reinauerdf61dd22010-08-09 12:02:00 +0000338 if (ptr->id != 0)
339 fprintf(fil, "static ", ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000340 fprintf(fil, "struct device %s = {\n", ptr->name);
341 fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0");
Myles Watson894a3472010-06-09 22:41:35 +0000342 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000343 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");
Sven Schnelle270a9082011-03-01 19:58:15 +0000348 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
Patrick Georgi114e7b22010-05-05 11:19:50 +0000354 if (ptr->rescnt > 0) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000355 fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000356 }
357 int link = 0;
Myles Watson894a3472010-06-09 22:41:35 +0000358 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);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000362 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 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000372 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 }
Myles Watson894a3472010-06-09 22:41:35 +0000390 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);
Myles Watson1965a232010-06-10 04:06:52 +0000401 if (d->next_sibling && device_match(d->next_sibling, ptr))
Myles Watson894a3472010-06-09 22:41:35 +0000402 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 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000421 if ((ptr->type == chip) && (ptr->chiph_exists)) {
422 if (ptr->reg) {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200423 fprintf(fil, "struct %s_config %s_info_%d\t= {\n",
424 ptr->name_underscore, ptr->name_underscore, ptr->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000425 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 {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200432 fprintf(fil, "struct %s_config %s_info_%d;\n",
433 ptr->name_underscore, ptr->name_underscore, ptr->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000434 }
435 }
436}
437
438static 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
Sven Schnelle270a9082011-03-01 19:58:15 +0000445static void inherit_subsystem_ids(FILE *file, struct device *dev)
446{
447 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +0000448
449 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
450 /* user already gave us a subsystem vendor/device */
451 return;
452 }
453
Sylvain "ythier" Hitier5325a482011-03-01 21:57:11 +0000454 for(p = dev; p && p != p->parent; p = p->parent) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000455
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
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200467static 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
Patrick Georgi114e7b22010-05-05 11:19:50 +0000478int main(int argc, char** argv) {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200479 if (argc < 3)
480 usage();
481
Patrick Georgi114e7b22010-05-05 11:19:50 +0000482 char *mainboard=argv[1];
483 char *outputdir=argv[2];
484 char *devtree=malloc(strlen(mainboard)+30);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000485 sprintf(devtree, "src/mainboard/%s/devicetree.cb", mainboard);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200486 char *outputc;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000487
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200488 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 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000521
522 FILE *filec = fopen(devtree, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000523 if (!filec) {
524 fprintf(stderr, "Could not open file '%s' for reading: ", devtree);
525 perror(NULL);
526 exit(1);
527 }
528
Patrick Georgi114e7b22010-05-05 11:19:50 +0000529 yyrestart(filec);
530
Patrick Georgi68befd52010-05-05 12:05:25 +0000531 lastdev = head = &root;
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000532
Patrick Georgi114e7b22010-05-05 11:19:50 +0000533 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000534
Patrick Georgi114e7b22010-05-05 11:19:50 +0000535 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
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200543 FILE *autogen = fopen(outputc, "w");
544 if (!autogen) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000545 fprintf(stderr, "Could not open file '%s' for writing: ", outputc);
546 perror(NULL);
547 exit(1);
548 }
549
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200550 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 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000601 }
Sven Schnelle270a9082011-03-01 19:58:15 +0000602
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200603 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000604
Patrick Georgi114e7b22010-05-05 11:19:50 +0000605 return 0;
606}