blob: 989f7471ff8aebdedbb3cfef057c862c2a3651cf [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
Patrick Georgi2dbfcb72012-05-30 16:26:30 +020021#include <ctype.h>
Patrick Georgi114e7b22010-05-05 11:19:50 +000022#include "sconfig.h"
23#include "sconfig.tab.h"
24
Patrick Georgi7fc9e292010-07-15 15:59:07 +000025extern int linenum;
26
Patrick Georgi114e7b22010-05-05 11:19:50 +000027struct device *head, *lastdev;
28
29struct header headers;
30
31static int devcount = 0;
32
Kyösti Mälkki472d9022011-12-05 20:33:55 +020033typedef enum {
34 STATIC_MODE,
35 BOOTBLOCK_MODE,
36 KCONFIG_MODE
37} scan_t;
38
39static scan_t scan_mode = STATIC_MODE;
40
Patrick Georgi114e7b22010-05-05 11:19:50 +000041static struct device root;
42static struct device mainboard = {
43 .name = "mainboard",
44 .name_underscore = "mainboard",
45 .id = 0,
46 .chip = &mainboard,
47 .type = chip,
48 .chiph_exists = 1,
49 .children = &root
50};
51
52static struct device root = {
53 .name = "dev_root",
54 .name_underscore = "dev_root",
55 .id = 0,
56 .chip = &mainboard,
57 .type = device,
58 .path = " .type = DEVICE_PATH_ROOT ",
59 .ops = "&default_dev_ops_root",
60 .parent = &root,
61 .bus = &root,
62 .enabled = 1
63};
64
Patrick Georgi68befd52010-05-05 12:05:25 +000065static struct device *new_dev(struct device *parent, struct device *bus) {
Patrick Georgi114e7b22010-05-05 11:19:50 +000066 struct device *dev = malloc(sizeof(struct device));
67 memset(dev, 0, sizeof(struct device));
68 dev->id = ++devcount;
Patrick Georgi68befd52010-05-05 12:05:25 +000069 dev->parent = parent;
70 dev->bus = bus;
Sven Schnelle270a9082011-03-01 19:58:15 +000071 dev->subsystem_vendor = -1;
72 dev->subsystem_device = -1;
Patrick Georgi114e7b22010-05-05 11:19:50 +000073 head->next = dev;
74 head = dev;
75 return dev;
76}
77
78static int device_match(struct device *a, struct device *b) {
79 if ((a->bustype == b->bustype) && (a->bus == b->bus) && (a->path_a == b->path_a) && (a->path_b == b->path_b))
80 return 1;
81 return 0;
82}
83
84void fold_in(struct device *parent) {
85 struct device *child = parent->children;
86 struct device *latest = 0;
87 while (child != latest) {
88 if (child->children) {
89 if (!latest) latest = child->children;
90 parent->latestchild->next_sibling = child->children;
91 parent->latestchild = child->latestchild;
92 }
93 child = child->next_sibling;
94 }
95}
96
97int yywrap(void) {
98 return 1;
99}
100
101void yyerror (char const *str)
102{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000103 extern char *yytext;
104 fprintf (stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
105 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000106}
107
108void postprocess_devtree(void) {
109 root.next_sibling = root.children;
110 root.next_sibling->next_sibling = root.next_sibling->children;
111
112 struct device *dev = &root;
113 while (dev) {
114 /* skip "chip" elements in children chain */
115 while (dev->children && (dev->children->type == chip)) dev->children = dev->children->children;
116 /* skip "chip" elements and functions of the same device in sibling chain */
117 while (dev->sibling && dev->sibling->used) dev->sibling = dev->sibling->sibling;
118 /* If end of chain, and parent is a chip, move on */
119 if (!dev->sibling && (dev->parent->type == chip)) dev->sibling = dev->parent->sibling;
120 /* skip chips */
121 while (dev->sibling && dev->sibling->type == chip) dev->sibling = dev->sibling->children;
122 /* skip duplicate function elements in nextdev chain */
123 while (dev->nextdev && dev->nextdev->used) dev->nextdev = dev->nextdev->nextdev;
124 dev = dev->next_sibling;
125 }
126}
127
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200128void translate_name(char *str, int uppercase)
129{
130 char *c;
131 for (c = str; *c; c++) {
132 if (*c == '/') *c = '_';
133 if (*c == '-') *c = '_';
134 if (uppercase)
135 *c = toupper(*c);
136 }
137}
138
Patrick Georgi68befd52010-05-05 12:05:25 +0000139struct device *new_chip(struct device *parent, struct device *bus, char *path) {
140 struct device *new_chip = new_dev(parent, bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000141 new_chip->chiph_exists = 1;
142 new_chip->name = path;
143 new_chip->name_underscore = strdup(new_chip->name);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200144 translate_name(new_chip->name_underscore, 0);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000145 new_chip->type = chip;
146 new_chip->chip = new_chip;
147
148 struct stat st;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200149 char *chip_h = malloc(strlen(path)+18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700150 sprintf(chip_h, "src/%s", path);
151 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
152 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
153 path);
154 exit(1);
155 }
156
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200157 if (scan_mode == STATIC_MODE)
158 sprintf(chip_h, "src/%s/chip.h", path);
159 else if (scan_mode == BOOTBLOCK_MODE)
160 sprintf(chip_h, "src/%s/bootblock.c", path);
161
162 if ((scan_mode == STATIC_MODE) || (scan_mode == BOOTBLOCK_MODE)) {
163 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
164 new_chip->chiph_exists = 0;
165 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000166
Patrick Georgi68befd52010-05-05 12:05:25 +0000167 if (parent->latestchild) {
168 parent->latestchild->next_sibling = new_chip;
169 parent->latestchild->sibling = new_chip;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000170 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000171 parent->latestchild = new_chip;
172 if (!parent->children)
173 parent->children = new_chip;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000174 return new_chip;
175}
176
177void add_header(struct device *dev) {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200178 if ((dev->chiph_exists) || (scan_mode == KCONFIG_MODE)){
Patrick Georgi114e7b22010-05-05 11:19:50 +0000179 int include_exists = 0;
180 struct header *h = &headers;
181 while (h->next) {
182 int result = strcmp(dev->name, h->next->name);
183 if (result == 0) {
184 include_exists = 1;
185 break;
186 }
187 if (result < 0) break;
188 h = h->next;
189 }
190 if (!include_exists) {
191 struct header *tmp = h->next;
192 h->next = malloc(sizeof(struct header));
193 memset(h->next, 0, sizeof(struct header));
194 h->next->name = dev->name;
195 h->next->next = tmp;
196 }
197 }
198}
199
Patrick Georgi68befd52010-05-05 12:05:25 +0000200struct device *new_device(struct device *parent, struct device *busdev, const int bus, const char *devnum, int enabled) {
201 struct device *new_d = new_dev(parent, busdev);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000202 new_d->bustype = bus;
203
204 char *tmp;
205 new_d->path_a = strtol(strdup(devnum), &tmp, 16);
206 if (*tmp == '.') {
207 tmp++;
208 new_d->path_b = strtol(tmp, NULL, 16);
209 }
210
211 char *name = malloc(10);
212 sprintf(name, "_dev%d", new_d->id);
213 new_d->name = name;
214 new_d->name_underscore = name; // shouldn't be necessary, but avoid 0-ptr
215 new_d->type = device;
216 new_d->enabled = enabled;
217 new_d->chip = new_d->parent->chip;
218
Patrick Georgi68befd52010-05-05 12:05:25 +0000219 if (parent->latestchild) {
220 parent->latestchild->next_sibling = new_d;
221 parent->latestchild->sibling = new_d;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000222 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000223 parent->latestchild = new_d;
224 if (!parent->children)
225 parent->children = new_d;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000226
227 lastdev->nextdev = new_d;
228 lastdev = new_d;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200229
230 switch(bus) {
231 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000232 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200233 break;
234
235 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000236 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200237 break;
238
239 case I2C:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000240 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200241 break;
242
243 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000244 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200245 break;
246
247 case APIC_CLUSTER:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000248 new_d->path = ".type=DEVICE_PATH_APIC_CLUSTER,{.apic_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200249 break;
250
251 case PCI_DOMAIN:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000252 new_d->path = ".type=DEVICE_PATH_PCI_DOMAIN,{.pci_domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200253 break;
254
255 case IOAPIC:
256 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
257 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000258 }
259 return new_d;
260}
261
262void alias_siblings(struct device *d) {
263 while (d) {
264 int link = 0;
265 struct device *cmp = d->next_sibling;
266 while (cmp && (cmp->bus == d->bus) && (cmp->path_a == d->path_a) && (cmp->path_b == d->path_b)) {
267 if (cmp->type==device && !cmp->used) {
268 if (device_match(d, cmp)) {
269 d->multidev = 1;
270
Patrick Georgi114e7b22010-05-05 11:19:50 +0000271 cmp->id = d->id;
272 cmp->name = d->name;
273 cmp->used = 1;
274 cmp->link = ++link;
275 }
276 }
277 cmp = cmp->next_sibling;
278 }
279 d = d->next_sibling;
280 }
281}
282
Patrick Georgi68befd52010-05-05 12:05:25 +0000283void add_resource(struct device *dev, int type, int index, int base) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000284 struct resource *r = malloc(sizeof(struct resource));
285 memset (r, 0, sizeof(struct resource));
286 r->type = type;
287 r->index = index;
288 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000289 if (dev->res) {
290 struct resource *head = dev->res;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000291 while (head->next) head = head->next;
292 head->next = r;
293 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000294 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000295 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000296 dev->rescnt++;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000297}
298
Patrick Georgi68befd52010-05-05 12:05:25 +0000299void add_register(struct device *dev, char *name, char *val) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000300 struct reg *r = malloc(sizeof(struct reg));
301 memset (r, 0, sizeof(struct reg));
302 r->key = name;
303 r->value = val;
Patrick Georgi68befd52010-05-05 12:05:25 +0000304 if (dev->reg) {
305 struct reg *head = dev->reg;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000306 // sorting to be equal to sconfig's behaviour
307 int sort = strcmp(r->key, head->key);
308 if (sort == 0) {
309 printf("ERROR: duplicate 'register' key.\n");
310 exit(1);
311 }
312 if (sort<0) {
313 r->next = head;
Patrick Georgi68befd52010-05-05 12:05:25 +0000314 dev->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000315 } else {
316 while ((head->next) && (strcmp(head->next->key, r->key)<0)) head = head->next;
317 r->next = head->next;
318 head->next = r;
319 }
320 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000321 dev->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000322 }
323}
324
Sven Schnelle270a9082011-03-01 19:58:15 +0000325void add_pci_subsystem_ids(struct device *dev, int vendor, int device, int inherit)
326{
327 if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) {
328 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
329 exit(1);
330 }
331
332 dev->subsystem_vendor = vendor;
333 dev->subsystem_device = device;
334 dev->inherit_subsystem = inherit;
335}
336
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200337void add_ioapic_info(struct device *dev, int apicid, const char *_srcpin, int irqpin)
338{
339
340 int srcpin;
341
342 if (!_srcpin || strlen(_srcpin) < 4 ||strncasecmp(_srcpin, "INT", 3) ||
343 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
344 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
345 exit(1);
346 }
347
348 srcpin = _srcpin[3] - 'A';
349
350 if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) {
351 printf("ERROR: ioapic config only allowed for PCI devices\n");
352 exit(1);
353 }
354
355 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200356 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200357 exit(1);
358 }
359 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
360 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
361}
362
Patrick Georgi114e7b22010-05-05 11:19:50 +0000363static void pass0(FILE *fil, struct device *ptr) {
Myles Watson894a3472010-06-09 22:41:35 +0000364 if (ptr->type == device && ptr->id == 0)
365 fprintf(fil, "struct bus %s_links[];\n", ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000366 if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) {
Stefan Reinauerdf61dd22010-08-09 12:02:00 +0000367 fprintf(fil, "static struct device %s;\n", ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000368 if (ptr->rescnt > 0)
369 fprintf(fil, "struct resource %s_res[];\n", ptr->name);
Myles Watson894a3472010-06-09 22:41:35 +0000370 if (ptr->children || ptr->multidev)
371 fprintf(fil, "struct bus %s_links[];\n", ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000372 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000373}
374
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200375static void pass1(FILE *fil, struct device *ptr)
376{
377 int pin;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000378 if (!ptr->used && (ptr->type == device)) {
Stefan Reinauerdf61dd22010-08-09 12:02:00 +0000379 if (ptr->id != 0)
Patrick Georgi2dbfcb72012-05-30 16:26:30 +0200380 fprintf(fil, "static ");
Patrick Georgi114e7b22010-05-05 11:19:50 +0000381 fprintf(fil, "struct device %s = {\n", ptr->name);
382 fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0");
Myles Watson894a3472010-06-09 22:41:35 +0000383 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000384 fprintf(fil, "\t.path = {");
385 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
386 fprintf(fil, "},\n");
387 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
388 fprintf(fil, "\t.on_mainboard = 1,\n");
Sven Schnelle270a9082011-03-01 19:58:15 +0000389 if (ptr->subsystem_vendor > 0)
390 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", ptr->subsystem_vendor);
391
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200392 for(pin = 0; pin < 4; pin++) {
393 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
394 fprintf(fil, "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
395
396 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
397 fprintf(fil, "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_dst_id);
398 }
399
Sven Schnelle270a9082011-03-01 19:58:15 +0000400 if (ptr->subsystem_device > 0)
401 fprintf(fil, "\t.subsystem_device = 0x%04x,\n", ptr->subsystem_device);
402
Patrick Georgi114e7b22010-05-05 11:19:50 +0000403 if (ptr->rescnt > 0) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000404 fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000405 }
406 int link = 0;
Myles Watson894a3472010-06-09 22:41:35 +0000407 if (ptr->children || ptr->multidev)
408 fprintf(fil, "\t.link_list = &%s_links[0],\n", ptr->name);
409 else
Patrick Georgi2dbfcb72012-05-30 16:26:30 +0200410 fprintf(fil, "\t.link_list = NULL,\n");
Patrick Georgi114e7b22010-05-05 11:19:50 +0000411 if (ptr->sibling)
412 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
413 if (ptr->chip->chiph_exists) {
414 fprintf(fil, "\t.chip_ops = &%s_ops,\n", ptr->chip->name_underscore);
415 fprintf(fil, "\t.chip_info = &%s_info_%d,\n", ptr->chip->name_underscore, ptr->chip->id);
416 }
417 if (ptr->nextdev)
418 fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name);
419 fprintf(fil, "};\n");
420 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000421 if (ptr->rescnt > 0) {
422 int i=1;
423 fprintf(fil, "struct resource %s_res[] = {\n", ptr->name);
424 struct resource *r = ptr->res;
425 while (r) {
426 fprintf(fil, "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
427 if (r->type == IRQ) fprintf(fil, "IRQ");
428 if (r->type == DRQ) fprintf(fil, "DRQ");
429 if (r->type == IO) fprintf(fil, "IO");
430 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, r->base);
431 if (r->next)
432 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, i++);
433 else
434 fprintf(fil, ".next=NULL },\n");
435 r = r->next;
436 }
437 fprintf(fil, "\t };\n");
438 }
Myles Watson894a3472010-06-09 22:41:35 +0000439 if (!ptr->used && ptr->type == device && (ptr->children || ptr->multidev)) {
440 fprintf(fil, "struct bus %s_links[] = {\n", ptr->name);
441 if (ptr->multidev) {
442 struct device *d = ptr;
443 while (d) {
444 if (device_match(d, ptr)) {
445 fprintf(fil, "\t\t[%d] = {\n", d->link);
446 fprintf(fil, "\t\t\t.link_num = %d,\n", d->link);
447 fprintf(fil, "\t\t\t.dev = &%s,\n", d->name);
448 if (d->children)
449 fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name);
Myles Watson1965a232010-06-10 04:06:52 +0000450 if (d->next_sibling && device_match(d->next_sibling, ptr))
Myles Watson894a3472010-06-09 22:41:35 +0000451 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", d->name, d->link+1);
452 else
453 fprintf(fil, "\t\t\t.next = NULL,\n");
454 fprintf(fil, "\t\t},\n");
455 }
456 d = d->next_sibling;
457 }
458 } else {
459 if (ptr->children) {
460 fprintf(fil, "\t\t[0] = {\n");
461 fprintf(fil, "\t\t\t.link_num = 0,\n");
462 fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name);
463 fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name);
464 fprintf(fil, "\t\t\t.next = NULL,\n");
465 fprintf(fil, "\t\t},\n");
466 }
467 }
468 fprintf(fil, "\t};\n");
469 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000470 if ((ptr->type == chip) && (ptr->chiph_exists)) {
471 if (ptr->reg) {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200472 fprintf(fil, "struct %s_config %s_info_%d\t= {\n",
473 ptr->name_underscore, ptr->name_underscore, ptr->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000474 struct reg *r = ptr->reg;
475 while (r) {
476 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
477 r = r->next;
478 }
479 fprintf(fil, "};\n\n");
480 } else {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200481 fprintf(fil, "struct %s_config %s_info_%d;\n",
482 ptr->name_underscore, ptr->name_underscore, ptr->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000483 }
484 }
485}
486
487static void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *, struct device*), struct device *chips) {
488 do {
489 func(fil, ptr);
490 ptr = ptr->next_sibling;
491 } while (ptr);
492}
493
Sven Schnelle270a9082011-03-01 19:58:15 +0000494static void inherit_subsystem_ids(FILE *file, struct device *dev)
495{
496 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +0000497
498 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
499 /* user already gave us a subsystem vendor/device */
500 return;
501 }
502
Sylvain "ythier" Hitier5325a482011-03-01 21:57:11 +0000503 for(p = dev; p && p != p->parent; p = p->parent) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000504
505 if (p->bustype != PCI && p->bustype != PCI_DOMAIN)
506 continue;
507
508 if (p->inherit_subsystem) {
509 dev->subsystem_vendor = p->subsystem_vendor;
510 dev->subsystem_device = p->subsystem_device;
511 break;
512 }
513 }
514}
515
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200516static void usage(void)
517{
518 printf("usage: sconfig vendor/mainboard outputdir [-{s|b|k} outputfile]\n");
519 printf("\t-s file\tcreate ramstage static device map\n");
520 printf("\t-b file\tcreate bootblock init_mainboard()\n");
521 printf("\t-k file\tcreate Kconfig devicetree section\n");
522 printf("Defaults to \"-s static.c\" if no {s|b|k} specified.\n");
523 exit (1);
524}
525
526
Patrick Georgi114e7b22010-05-05 11:19:50 +0000527int main(int argc, char** argv) {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200528 if (argc < 3)
529 usage();
530
Patrick Georgi114e7b22010-05-05 11:19:50 +0000531 char *mainboard=argv[1];
532 char *outputdir=argv[2];
533 char *devtree=malloc(strlen(mainboard)+30);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000534 sprintf(devtree, "src/mainboard/%s/devicetree.cb", mainboard);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200535 char *outputc;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000536
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200537 if (argc == 3) {
538 scan_mode = STATIC_MODE;
539 outputc=malloc(strlen(outputdir)+20);
540 sprintf(outputc, "%s/static.c", outputdir);
541 } else if ((argc == 5) && (argv[3][0] == '-') && (argv[3][2] == 0)) {
542
543 switch (argv[3][1]) {
544 case 's':
545 scan_mode = STATIC_MODE;
546 break;
547 case 'b':
548 scan_mode = BOOTBLOCK_MODE;
549 break;
550 case 'k':
551 scan_mode = KCONFIG_MODE;
552 break;
553 default:
554 usage();
555 break;
556 }
557 char *outputfile=argv[4];
558
559 outputc=malloc(strlen(outputdir)+strlen(outputfile)+2);
560 sprintf(outputc, "%s/%s", outputdir, outputfile);
561 }
562
563 headers.next = 0;
564 if (scan_mode == STATIC_MODE) {
565 headers.next = malloc(sizeof(struct header));
566 headers.next->name = malloc(strlen(mainboard)+12);
567 headers.next->next = 0;
568 sprintf(headers.next->name, "mainboard/%s", mainboard);
569 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000570
571 FILE *filec = fopen(devtree, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000572 if (!filec) {
573 fprintf(stderr, "Could not open file '%s' for reading: ", devtree);
574 perror(NULL);
575 exit(1);
576 }
577
Patrick Georgi114e7b22010-05-05 11:19:50 +0000578 yyrestart(filec);
579
Patrick Georgi68befd52010-05-05 12:05:25 +0000580 lastdev = head = &root;
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000581
Patrick Georgi114e7b22010-05-05 11:19:50 +0000582 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000583
Patrick Georgi114e7b22010-05-05 11:19:50 +0000584 fclose(filec);
585
586 if ((head->type == chip) && (!head->chiph_exists)) {
587 struct device *tmp = head;
588 head = &root;
589 while (head->next != tmp) head = head->next;
590 }
591
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200592 FILE *autogen = fopen(outputc, "w");
593 if (!autogen) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000594 fprintf(stderr, "Could not open file '%s' for writing: ", outputc);
595 perror(NULL);
596 exit(1);
597 }
598
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200599 struct header *h;
600 if (scan_mode == STATIC_MODE) {
601
602 fprintf(autogen, "#include <device/device.h>\n");
603 fprintf(autogen, "#include <device/pci.h>\n");
604 h = &headers;
605 while (h->next) {
606 h = h->next;
607 fprintf(autogen, "#include \"%s/chip.h\"\n", h->name);
608 }
609
610 walk_device_tree(autogen, &root, inherit_subsystem_ids, NULL);
611 fprintf(autogen, "\n/* pass 0 */\n");
612 walk_device_tree(autogen, &root, pass0, NULL);
613 fprintf(autogen, "\n/* pass 1 */\nstruct mainboard_config mainboard_info_0;\n"
614 "struct device *last_dev = &%s;\n", lastdev->name);
615 walk_device_tree(autogen, &root, pass1, NULL);
616
617 } else if (scan_mode == BOOTBLOCK_MODE) {
618 h = &headers;
619 while (h->next) {
620 h = h->next;
621 fprintf(autogen, "#include \"%s/bootblock.c\"\n", h->name);
622 }
623
624 fprintf(autogen, "\n#if CONFIG_HAS_MAINBOARD_BOOTBLOCK\n");
625 fprintf(autogen, "#include \"mainboard/%s/bootblock.c\"\n", mainboard);
626 fprintf(autogen, "#else\n");
627 fprintf(autogen, "static unsigned long init_mainboard(int bsp_cpu)\n{\n");
628 fprintf(autogen, "\tif (! bsp_cpu) return 0;\n");
629 h = &headers;
630 while (h->next) {
631 h = h->next;
632 translate_name(h->name, 0);
633 fprintf(autogen, "\tinit_%s();\n", h->name);
634 }
635
636 fprintf(autogen, "\treturn 0;\n}\n");
637 fprintf(autogen, "#endif\n");
638
639 } else if (scan_mode == KCONFIG_MODE) {
640 fprintf(autogen, "\nconfig MAINBOARD_DIR\n\tstring\n");
641 fprintf(autogen, "\tdefault %s\n", mainboard);
642
643 fprintf(autogen, "\nconfig MAINBOARD_DEVTREE\n\tdef_bool y\n");
644 h = &headers;
645 while (h->next) {
646 h = h->next;
647 translate_name(h->name, 1);
648 fprintf(autogen, "\tselect %s\n", h->name);
649 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000650 }
Sven Schnelle270a9082011-03-01 19:58:15 +0000651
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200652 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000653
Patrick Georgi114e7b22010-05-05 11:19:50 +0000654 return 0;
655}