blob: 824bc6c17840c64ad7140dba938582a81b94eabd [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,
Stefan Reinauer188e3c22012-07-26 12:46:48 -070048#ifdef MAINBOARDS_HAVE_CHIP_H
Patrick Georgi114e7b22010-05-05 11:19:50 +000049 .chiph_exists = 1,
Stefan Reinauer188e3c22012-07-26 12:46:48 -070050#else
51 .chiph_exists = 0,
52#endif
Patrick Georgi114e7b22010-05-05 11:19:50 +000053 .children = &root
54};
55
56static struct device root = {
57 .name = "dev_root",
58 .name_underscore = "dev_root",
59 .id = 0,
60 .chip = &mainboard,
61 .type = device,
62 .path = " .type = DEVICE_PATH_ROOT ",
63 .ops = "&default_dev_ops_root",
64 .parent = &root,
65 .bus = &root,
66 .enabled = 1
67};
68
Patrick Georgi68befd52010-05-05 12:05:25 +000069static struct device *new_dev(struct device *parent, struct device *bus) {
Patrick Georgi114e7b22010-05-05 11:19:50 +000070 struct device *dev = malloc(sizeof(struct device));
71 memset(dev, 0, sizeof(struct device));
72 dev->id = ++devcount;
Patrick Georgi68befd52010-05-05 12:05:25 +000073 dev->parent = parent;
74 dev->bus = bus;
Sven Schnelle270a9082011-03-01 19:58:15 +000075 dev->subsystem_vendor = -1;
76 dev->subsystem_device = -1;
Patrick Georgi114e7b22010-05-05 11:19:50 +000077 head->next = dev;
78 head = dev;
79 return dev;
80}
81
82static int device_match(struct device *a, struct device *b) {
83 if ((a->bustype == b->bustype) && (a->bus == b->bus) && (a->path_a == b->path_a) && (a->path_b == b->path_b))
84 return 1;
85 return 0;
86}
87
88void fold_in(struct device *parent) {
89 struct device *child = parent->children;
90 struct device *latest = 0;
91 while (child != latest) {
92 if (child->children) {
93 if (!latest) latest = child->children;
94 parent->latestchild->next_sibling = child->children;
95 parent->latestchild = child->latestchild;
96 }
97 child = child->next_sibling;
98 }
99}
100
101int yywrap(void) {
102 return 1;
103}
104
105void yyerror (char const *str)
106{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000107 extern char *yytext;
108 fprintf (stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
109 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000110}
111
112void postprocess_devtree(void) {
113 root.next_sibling = root.children;
114 root.next_sibling->next_sibling = root.next_sibling->children;
115
116 struct device *dev = &root;
117 while (dev) {
118 /* skip "chip" elements in children chain */
119 while (dev->children && (dev->children->type == chip)) dev->children = dev->children->children;
120 /* skip "chip" elements and functions of the same device in sibling chain */
121 while (dev->sibling && dev->sibling->used) dev->sibling = dev->sibling->sibling;
122 /* If end of chain, and parent is a chip, move on */
123 if (!dev->sibling && (dev->parent->type == chip)) dev->sibling = dev->parent->sibling;
124 /* skip chips */
125 while (dev->sibling && dev->sibling->type == chip) dev->sibling = dev->sibling->children;
126 /* skip duplicate function elements in nextdev chain */
127 while (dev->nextdev && dev->nextdev->used) dev->nextdev = dev->nextdev->nextdev;
128 dev = dev->next_sibling;
129 }
130}
131
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200132void translate_name(char *str, int uppercase)
133{
134 char *c;
135 for (c = str; *c; c++) {
136 if (*c == '/') *c = '_';
137 if (*c == '-') *c = '_';
138 if (uppercase)
139 *c = toupper(*c);
140 }
141}
142
Patrick Georgi68befd52010-05-05 12:05:25 +0000143struct device *new_chip(struct device *parent, struct device *bus, char *path) {
144 struct device *new_chip = new_dev(parent, bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000145 new_chip->chiph_exists = 1;
146 new_chip->name = path;
147 new_chip->name_underscore = strdup(new_chip->name);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200148 translate_name(new_chip->name_underscore, 0);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000149 new_chip->type = chip;
150 new_chip->chip = new_chip;
151
152 struct stat st;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200153 char *chip_h = malloc(strlen(path)+18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700154 sprintf(chip_h, "src/%s", path);
155 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
156 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
157 path);
158 exit(1);
159 }
160
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200161 if (scan_mode == STATIC_MODE)
162 sprintf(chip_h, "src/%s/chip.h", path);
163 else if (scan_mode == BOOTBLOCK_MODE)
164 sprintf(chip_h, "src/%s/bootblock.c", path);
165
166 if ((scan_mode == STATIC_MODE) || (scan_mode == BOOTBLOCK_MODE)) {
167 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
168 new_chip->chiph_exists = 0;
169 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000170
Patrick Georgi68befd52010-05-05 12:05:25 +0000171 if (parent->latestchild) {
172 parent->latestchild->next_sibling = new_chip;
173 parent->latestchild->sibling = new_chip;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000174 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000175 parent->latestchild = new_chip;
176 if (!parent->children)
177 parent->children = new_chip;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000178 return new_chip;
179}
180
181void add_header(struct device *dev) {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200182 if ((dev->chiph_exists) || (scan_mode == KCONFIG_MODE)){
Patrick Georgi114e7b22010-05-05 11:19:50 +0000183 int include_exists = 0;
184 struct header *h = &headers;
185 while (h->next) {
186 int result = strcmp(dev->name, h->next->name);
187 if (result == 0) {
188 include_exists = 1;
189 break;
190 }
191 if (result < 0) break;
192 h = h->next;
193 }
194 if (!include_exists) {
195 struct header *tmp = h->next;
196 h->next = malloc(sizeof(struct header));
197 memset(h->next, 0, sizeof(struct header));
198 h->next->name = dev->name;
199 h->next->next = tmp;
200 }
201 }
202}
203
Patrick Georgi68befd52010-05-05 12:05:25 +0000204struct device *new_device(struct device *parent, struct device *busdev, const int bus, const char *devnum, int enabled) {
205 struct device *new_d = new_dev(parent, busdev);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000206 new_d->bustype = bus;
207
208 char *tmp;
209 new_d->path_a = strtol(strdup(devnum), &tmp, 16);
210 if (*tmp == '.') {
211 tmp++;
212 new_d->path_b = strtol(tmp, NULL, 16);
213 }
214
215 char *name = malloc(10);
216 sprintf(name, "_dev%d", new_d->id);
217 new_d->name = name;
218 new_d->name_underscore = name; // shouldn't be necessary, but avoid 0-ptr
219 new_d->type = device;
220 new_d->enabled = enabled;
221 new_d->chip = new_d->parent->chip;
222
Patrick Georgi68befd52010-05-05 12:05:25 +0000223 if (parent->latestchild) {
224 parent->latestchild->next_sibling = new_d;
225 parent->latestchild->sibling = new_d;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000226 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000227 parent->latestchild = new_d;
228 if (!parent->children)
229 parent->children = new_d;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000230
231 lastdev->nextdev = new_d;
232 lastdev = new_d;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200233
234 switch(bus) {
235 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000236 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200237 break;
238
239 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000240 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200241 break;
242
243 case I2C:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000244 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200245 break;
246
247 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000248 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200249 break;
250
251 case APIC_CLUSTER:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000252 new_d->path = ".type=DEVICE_PATH_APIC_CLUSTER,{.apic_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200253 break;
254
255 case PCI_DOMAIN:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000256 new_d->path = ".type=DEVICE_PATH_PCI_DOMAIN,{.pci_domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200257 break;
258
259 case IOAPIC:
260 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
261 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000262 }
263 return new_d;
264}
265
266void alias_siblings(struct device *d) {
267 while (d) {
268 int link = 0;
269 struct device *cmp = d->next_sibling;
270 while (cmp && (cmp->bus == d->bus) && (cmp->path_a == d->path_a) && (cmp->path_b == d->path_b)) {
271 if (cmp->type==device && !cmp->used) {
272 if (device_match(d, cmp)) {
273 d->multidev = 1;
274
Patrick Georgi114e7b22010-05-05 11:19:50 +0000275 cmp->id = d->id;
276 cmp->name = d->name;
277 cmp->used = 1;
278 cmp->link = ++link;
279 }
280 }
281 cmp = cmp->next_sibling;
282 }
283 d = d->next_sibling;
284 }
285}
286
Patrick Georgi68befd52010-05-05 12:05:25 +0000287void add_resource(struct device *dev, int type, int index, int base) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000288 struct resource *r = malloc(sizeof(struct resource));
289 memset (r, 0, sizeof(struct resource));
290 r->type = type;
291 r->index = index;
292 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000293 if (dev->res) {
294 struct resource *head = dev->res;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000295 while (head->next) head = head->next;
296 head->next = r;
297 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000298 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000299 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000300 dev->rescnt++;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000301}
302
Patrick Georgi68befd52010-05-05 12:05:25 +0000303void add_register(struct device *dev, char *name, char *val) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000304 struct reg *r = malloc(sizeof(struct reg));
305 memset (r, 0, sizeof(struct reg));
306 r->key = name;
307 r->value = val;
Patrick Georgi68befd52010-05-05 12:05:25 +0000308 if (dev->reg) {
309 struct reg *head = dev->reg;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000310 // sorting to be equal to sconfig's behaviour
311 int sort = strcmp(r->key, head->key);
312 if (sort == 0) {
313 printf("ERROR: duplicate 'register' key.\n");
314 exit(1);
315 }
316 if (sort<0) {
317 r->next = head;
Patrick Georgi68befd52010-05-05 12:05:25 +0000318 dev->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000319 } else {
320 while ((head->next) && (strcmp(head->next->key, r->key)<0)) head = head->next;
321 r->next = head->next;
322 head->next = r;
323 }
324 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000325 dev->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000326 }
327}
328
Sven Schnelle270a9082011-03-01 19:58:15 +0000329void add_pci_subsystem_ids(struct device *dev, int vendor, int device, int inherit)
330{
331 if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) {
332 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
333 exit(1);
334 }
335
336 dev->subsystem_vendor = vendor;
337 dev->subsystem_device = device;
338 dev->inherit_subsystem = inherit;
339}
340
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200341void add_ioapic_info(struct device *dev, int apicid, const char *_srcpin, int irqpin)
342{
343
344 int srcpin;
345
346 if (!_srcpin || strlen(_srcpin) < 4 ||strncasecmp(_srcpin, "INT", 3) ||
347 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
348 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
349 exit(1);
350 }
351
352 srcpin = _srcpin[3] - 'A';
353
354 if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) {
355 printf("ERROR: ioapic config only allowed for PCI devices\n");
356 exit(1);
357 }
358
359 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200360 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200361 exit(1);
362 }
363 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
364 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
365}
366
Patrick Georgi114e7b22010-05-05 11:19:50 +0000367static void pass0(FILE *fil, struct device *ptr) {
Myles Watson894a3472010-06-09 22:41:35 +0000368 if (ptr->type == device && ptr->id == 0)
369 fprintf(fil, "struct bus %s_links[];\n", ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000370 if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) {
Stefan Reinauerdf61dd22010-08-09 12:02:00 +0000371 fprintf(fil, "static struct device %s;\n", ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000372 if (ptr->rescnt > 0)
373 fprintf(fil, "struct resource %s_res[];\n", ptr->name);
Myles Watson894a3472010-06-09 22:41:35 +0000374 if (ptr->children || ptr->multidev)
375 fprintf(fil, "struct bus %s_links[];\n", ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000376 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000377}
378
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200379static void pass1(FILE *fil, struct device *ptr)
380{
381 int pin;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000382 if (!ptr->used && (ptr->type == device)) {
Stefan Reinauerdf61dd22010-08-09 12:02:00 +0000383 if (ptr->id != 0)
Patrick Georgi2dbfcb72012-05-30 16:26:30 +0200384 fprintf(fil, "static ");
Patrick Georgi114e7b22010-05-05 11:19:50 +0000385 fprintf(fil, "struct device %s = {\n", ptr->name);
386 fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0");
Myles Watson894a3472010-06-09 22:41:35 +0000387 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000388 fprintf(fil, "\t.path = {");
389 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
390 fprintf(fil, "},\n");
391 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
392 fprintf(fil, "\t.on_mainboard = 1,\n");
Sven Schnelle270a9082011-03-01 19:58:15 +0000393 if (ptr->subsystem_vendor > 0)
394 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", ptr->subsystem_vendor);
395
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200396 for(pin = 0; pin < 4; pin++) {
397 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
398 fprintf(fil, "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
399
400 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
401 fprintf(fil, "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_dst_id);
402 }
403
Sven Schnelle270a9082011-03-01 19:58:15 +0000404 if (ptr->subsystem_device > 0)
405 fprintf(fil, "\t.subsystem_device = 0x%04x,\n", ptr->subsystem_device);
406
Patrick Georgi114e7b22010-05-05 11:19:50 +0000407 if (ptr->rescnt > 0) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000408 fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000409 }
410 int link = 0;
Myles Watson894a3472010-06-09 22:41:35 +0000411 if (ptr->children || ptr->multidev)
412 fprintf(fil, "\t.link_list = &%s_links[0],\n", ptr->name);
413 else
Patrick Georgi2dbfcb72012-05-30 16:26:30 +0200414 fprintf(fil, "\t.link_list = NULL,\n");
Patrick Georgi114e7b22010-05-05 11:19:50 +0000415 if (ptr->sibling)
416 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
417 if (ptr->chip->chiph_exists) {
418 fprintf(fil, "\t.chip_ops = &%s_ops,\n", ptr->chip->name_underscore);
419 fprintf(fil, "\t.chip_info = &%s_info_%d,\n", ptr->chip->name_underscore, ptr->chip->id);
420 }
421 if (ptr->nextdev)
422 fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name);
423 fprintf(fil, "};\n");
424 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000425 if (ptr->rescnt > 0) {
426 int i=1;
427 fprintf(fil, "struct resource %s_res[] = {\n", ptr->name);
428 struct resource *r = ptr->res;
429 while (r) {
430 fprintf(fil, "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
431 if (r->type == IRQ) fprintf(fil, "IRQ");
432 if (r->type == DRQ) fprintf(fil, "DRQ");
433 if (r->type == IO) fprintf(fil, "IO");
434 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, r->base);
435 if (r->next)
436 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, i++);
437 else
438 fprintf(fil, ".next=NULL },\n");
439 r = r->next;
440 }
441 fprintf(fil, "\t };\n");
442 }
Myles Watson894a3472010-06-09 22:41:35 +0000443 if (!ptr->used && ptr->type == device && (ptr->children || ptr->multidev)) {
444 fprintf(fil, "struct bus %s_links[] = {\n", ptr->name);
445 if (ptr->multidev) {
446 struct device *d = ptr;
447 while (d) {
448 if (device_match(d, ptr)) {
449 fprintf(fil, "\t\t[%d] = {\n", d->link);
450 fprintf(fil, "\t\t\t.link_num = %d,\n", d->link);
451 fprintf(fil, "\t\t\t.dev = &%s,\n", d->name);
452 if (d->children)
453 fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name);
Myles Watson1965a232010-06-10 04:06:52 +0000454 if (d->next_sibling && device_match(d->next_sibling, ptr))
Myles Watson894a3472010-06-09 22:41:35 +0000455 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", d->name, d->link+1);
456 else
457 fprintf(fil, "\t\t\t.next = NULL,\n");
458 fprintf(fil, "\t\t},\n");
459 }
460 d = d->next_sibling;
461 }
462 } else {
463 if (ptr->children) {
464 fprintf(fil, "\t\t[0] = {\n");
465 fprintf(fil, "\t\t\t.link_num = 0,\n");
466 fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name);
467 fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name);
468 fprintf(fil, "\t\t\t.next = NULL,\n");
469 fprintf(fil, "\t\t},\n");
470 }
471 }
472 fprintf(fil, "\t};\n");
473 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000474 if ((ptr->type == chip) && (ptr->chiph_exists)) {
475 if (ptr->reg) {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200476 fprintf(fil, "struct %s_config %s_info_%d\t= {\n",
477 ptr->name_underscore, ptr->name_underscore, ptr->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000478 struct reg *r = ptr->reg;
479 while (r) {
480 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
481 r = r->next;
482 }
483 fprintf(fil, "};\n\n");
484 } else {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200485 fprintf(fil, "struct %s_config %s_info_%d;\n",
486 ptr->name_underscore, ptr->name_underscore, ptr->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000487 }
488 }
489}
490
491static void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *, struct device*), struct device *chips) {
492 do {
493 func(fil, ptr);
494 ptr = ptr->next_sibling;
495 } while (ptr);
496}
497
Sven Schnelle270a9082011-03-01 19:58:15 +0000498static void inherit_subsystem_ids(FILE *file, struct device *dev)
499{
500 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +0000501
502 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
503 /* user already gave us a subsystem vendor/device */
504 return;
505 }
506
Sylvain "ythier" Hitier5325a482011-03-01 21:57:11 +0000507 for(p = dev; p && p != p->parent; p = p->parent) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000508
509 if (p->bustype != PCI && p->bustype != PCI_DOMAIN)
510 continue;
511
512 if (p->inherit_subsystem) {
513 dev->subsystem_vendor = p->subsystem_vendor;
514 dev->subsystem_device = p->subsystem_device;
515 break;
516 }
517 }
518}
519
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200520static void usage(void)
521{
522 printf("usage: sconfig vendor/mainboard outputdir [-{s|b|k} outputfile]\n");
523 printf("\t-s file\tcreate ramstage static device map\n");
524 printf("\t-b file\tcreate bootblock init_mainboard()\n");
525 printf("\t-k file\tcreate Kconfig devicetree section\n");
526 printf("Defaults to \"-s static.c\" if no {s|b|k} specified.\n");
527 exit (1);
528}
529
530
Patrick Georgi114e7b22010-05-05 11:19:50 +0000531int main(int argc, char** argv) {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200532 if (argc < 3)
533 usage();
534
Patrick Georgi114e7b22010-05-05 11:19:50 +0000535 char *mainboard=argv[1];
536 char *outputdir=argv[2];
537 char *devtree=malloc(strlen(mainboard)+30);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000538 sprintf(devtree, "src/mainboard/%s/devicetree.cb", mainboard);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200539 char *outputc;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000540
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200541 if (argc == 3) {
542 scan_mode = STATIC_MODE;
543 outputc=malloc(strlen(outputdir)+20);
544 sprintf(outputc, "%s/static.c", outputdir);
545 } else if ((argc == 5) && (argv[3][0] == '-') && (argv[3][2] == 0)) {
546
547 switch (argv[3][1]) {
548 case 's':
549 scan_mode = STATIC_MODE;
550 break;
551 case 'b':
552 scan_mode = BOOTBLOCK_MODE;
553 break;
554 case 'k':
555 scan_mode = KCONFIG_MODE;
556 break;
557 default:
558 usage();
559 break;
560 }
561 char *outputfile=argv[4];
562
563 outputc=malloc(strlen(outputdir)+strlen(outputfile)+2);
564 sprintf(outputc, "%s/%s", outputdir, outputfile);
565 }
566
567 headers.next = 0;
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700568#ifdef MAINBOARDS_HAVE_CHIP_H
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200569 if (scan_mode == STATIC_MODE) {
570 headers.next = malloc(sizeof(struct header));
571 headers.next->name = malloc(strlen(mainboard)+12);
572 headers.next->next = 0;
573 sprintf(headers.next->name, "mainboard/%s", mainboard);
574 }
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700575#endif
Patrick Georgi114e7b22010-05-05 11:19:50 +0000576
577 FILE *filec = fopen(devtree, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000578 if (!filec) {
579 fprintf(stderr, "Could not open file '%s' for reading: ", devtree);
580 perror(NULL);
581 exit(1);
582 }
583
Patrick Georgi114e7b22010-05-05 11:19:50 +0000584 yyrestart(filec);
585
Patrick Georgi68befd52010-05-05 12:05:25 +0000586 lastdev = head = &root;
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000587
Patrick Georgi114e7b22010-05-05 11:19:50 +0000588 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000589
Patrick Georgi114e7b22010-05-05 11:19:50 +0000590 fclose(filec);
591
592 if ((head->type == chip) && (!head->chiph_exists)) {
593 struct device *tmp = head;
594 head = &root;
595 while (head->next != tmp) head = head->next;
596 }
597
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200598 FILE *autogen = fopen(outputc, "w");
599 if (!autogen) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000600 fprintf(stderr, "Could not open file '%s' for writing: ", outputc);
601 perror(NULL);
602 exit(1);
603 }
604
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200605 struct header *h;
606 if (scan_mode == STATIC_MODE) {
607
608 fprintf(autogen, "#include <device/device.h>\n");
609 fprintf(autogen, "#include <device/pci.h>\n");
610 h = &headers;
611 while (h->next) {
612 h = h->next;
613 fprintf(autogen, "#include \"%s/chip.h\"\n", h->name);
614 }
615
616 walk_device_tree(autogen, &root, inherit_subsystem_ids, NULL);
617 fprintf(autogen, "\n/* pass 0 */\n");
618 walk_device_tree(autogen, &root, pass0, NULL);
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700619 fprintf(autogen, "\n/* pass 1 */\n"
620 "struct device *last_dev = &%s;\n", lastdev->name);
621#ifdef MAINBOARDS_HAVE_CHIP_H
622 fprintf(autogen, "struct mainboard_config mainboard_info_0;\n");
623#endif
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200624 walk_device_tree(autogen, &root, pass1, NULL);
625
626 } else if (scan_mode == BOOTBLOCK_MODE) {
627 h = &headers;
628 while (h->next) {
629 h = h->next;
630 fprintf(autogen, "#include \"%s/bootblock.c\"\n", h->name);
631 }
632
633 fprintf(autogen, "\n#if CONFIG_HAS_MAINBOARD_BOOTBLOCK\n");
634 fprintf(autogen, "#include \"mainboard/%s/bootblock.c\"\n", mainboard);
635 fprintf(autogen, "#else\n");
636 fprintf(autogen, "static unsigned long init_mainboard(int bsp_cpu)\n{\n");
637 fprintf(autogen, "\tif (! bsp_cpu) return 0;\n");
638 h = &headers;
639 while (h->next) {
640 h = h->next;
641 translate_name(h->name, 0);
642 fprintf(autogen, "\tinit_%s();\n", h->name);
643 }
644
645 fprintf(autogen, "\treturn 0;\n}\n");
646 fprintf(autogen, "#endif\n");
647
648 } else if (scan_mode == KCONFIG_MODE) {
649 fprintf(autogen, "\nconfig MAINBOARD_DIR\n\tstring\n");
650 fprintf(autogen, "\tdefault %s\n", mainboard);
651
652 fprintf(autogen, "\nconfig MAINBOARD_DEVTREE\n\tdef_bool y\n");
653 h = &headers;
654 while (h->next) {
655 h = h->next;
656 translate_name(h->name, 1);
657 fprintf(autogen, "\tselect %s\n", h->name);
658 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000659 }
Sven Schnelle270a9082011-03-01 19:58:15 +0000660
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200661 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000662
Patrick Georgi114e7b22010-05-05 11:19:50 +0000663 return 0;
664}