blob: f066d7bad2466202e0cf7ea9954188fa06852c33 [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älkkiaada2e12012-10-07 15:08:32 +0200182 int include_exists = 0;
183 struct header *h = &headers;
184 while (h->next) {
185 int result = strcmp(dev->name, h->next->name);
186 if (result == 0) {
187 include_exists = 1;
188 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000189 }
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200190 if (result < 0) break;
191 h = h->next;
192 }
193 if (!include_exists) {
194 struct header *tmp = h->next;
195 h->next = malloc(sizeof(struct header));
196 memset(h->next, 0, sizeof(struct header));
197 h->next->chiph_exists = dev->chiph_exists;
198 h->next->name = dev->name;
199 h->next->next = tmp;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000200 }
201}
202
Patrick Georgi68befd52010-05-05 12:05:25 +0000203struct device *new_device(struct device *parent, struct device *busdev, const int bus, const char *devnum, int enabled) {
204 struct device *new_d = new_dev(parent, busdev);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000205 new_d->bustype = bus;
206
207 char *tmp;
208 new_d->path_a = strtol(strdup(devnum), &tmp, 16);
209 if (*tmp == '.') {
210 tmp++;
211 new_d->path_b = strtol(tmp, NULL, 16);
212 }
213
214 char *name = malloc(10);
215 sprintf(name, "_dev%d", new_d->id);
216 new_d->name = name;
217 new_d->name_underscore = name; // shouldn't be necessary, but avoid 0-ptr
218 new_d->type = device;
219 new_d->enabled = enabled;
220 new_d->chip = new_d->parent->chip;
221
Patrick Georgi68befd52010-05-05 12:05:25 +0000222 if (parent->latestchild) {
223 parent->latestchild->next_sibling = new_d;
224 parent->latestchild->sibling = new_d;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000225 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000226 parent->latestchild = new_d;
227 if (!parent->children)
228 parent->children = new_d;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000229
230 lastdev->nextdev = new_d;
231 lastdev = new_d;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200232
233 switch(bus) {
234 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000235 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200236 break;
237
238 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000239 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200240 break;
241
242 case I2C:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000243 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200244 break;
245
246 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000247 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200248 break;
249
250 case APIC_CLUSTER:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000251 new_d->path = ".type=DEVICE_PATH_APIC_CLUSTER,{.apic_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200252 break;
253
254 case PCI_DOMAIN:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000255 new_d->path = ".type=DEVICE_PATH_PCI_DOMAIN,{.pci_domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200256 break;
257
258 case IOAPIC:
259 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
260 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000261 }
262 return new_d;
263}
264
265void alias_siblings(struct device *d) {
266 while (d) {
267 int link = 0;
268 struct device *cmp = d->next_sibling;
269 while (cmp && (cmp->bus == d->bus) && (cmp->path_a == d->path_a) && (cmp->path_b == d->path_b)) {
270 if (cmp->type==device && !cmp->used) {
271 if (device_match(d, cmp)) {
272 d->multidev = 1;
273
Patrick Georgi114e7b22010-05-05 11:19:50 +0000274 cmp->id = d->id;
275 cmp->name = d->name;
276 cmp->used = 1;
277 cmp->link = ++link;
278 }
279 }
280 cmp = cmp->next_sibling;
281 }
282 d = d->next_sibling;
283 }
284}
285
Patrick Georgi68befd52010-05-05 12:05:25 +0000286void add_resource(struct device *dev, int type, int index, int base) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000287 struct resource *r = malloc(sizeof(struct resource));
288 memset (r, 0, sizeof(struct resource));
289 r->type = type;
290 r->index = index;
291 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000292 if (dev->res) {
293 struct resource *head = dev->res;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000294 while (head->next) head = head->next;
295 head->next = r;
296 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000297 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000298 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000299 dev->rescnt++;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000300}
301
Patrick Georgi68befd52010-05-05 12:05:25 +0000302void add_register(struct device *dev, char *name, char *val) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000303 struct reg *r = malloc(sizeof(struct reg));
304 memset (r, 0, sizeof(struct reg));
305 r->key = name;
306 r->value = val;
Patrick Georgi68befd52010-05-05 12:05:25 +0000307 if (dev->reg) {
308 struct reg *head = dev->reg;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000309 // sorting to be equal to sconfig's behaviour
310 int sort = strcmp(r->key, head->key);
311 if (sort == 0) {
312 printf("ERROR: duplicate 'register' key.\n");
313 exit(1);
314 }
315 if (sort<0) {
316 r->next = head;
Patrick Georgi68befd52010-05-05 12:05:25 +0000317 dev->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000318 } else {
319 while ((head->next) && (strcmp(head->next->key, r->key)<0)) head = head->next;
320 r->next = head->next;
321 head->next = r;
322 }
323 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000324 dev->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000325 }
326}
327
Sven Schnelle270a9082011-03-01 19:58:15 +0000328void add_pci_subsystem_ids(struct device *dev, int vendor, int device, int inherit)
329{
330 if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) {
331 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
332 exit(1);
333 }
334
335 dev->subsystem_vendor = vendor;
336 dev->subsystem_device = device;
337 dev->inherit_subsystem = inherit;
338}
339
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200340void add_ioapic_info(struct device *dev, int apicid, const char *_srcpin, int irqpin)
341{
342
343 int srcpin;
344
345 if (!_srcpin || strlen(_srcpin) < 4 ||strncasecmp(_srcpin, "INT", 3) ||
346 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
347 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
348 exit(1);
349 }
350
351 srcpin = _srcpin[3] - 'A';
352
353 if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) {
354 printf("ERROR: ioapic config only allowed for PCI devices\n");
355 exit(1);
356 }
357
358 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200359 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200360 exit(1);
361 }
362 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
363 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
364}
365
Patrick Georgi114e7b22010-05-05 11:19:50 +0000366static void pass0(FILE *fil, struct device *ptr) {
Myles Watson894a3472010-06-09 22:41:35 +0000367 if (ptr->type == device && ptr->id == 0)
Stefan Reinauer57879c92012-07-31 16:47:25 -0700368 fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[];\n", ptr->name);
369
Myles Watsonc25cc112010-05-21 14:33:48 +0000370 if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) {
Stefan Reinauer57879c92012-07-31 16:47:25 -0700371 fprintf(fil, "ROMSTAGE_CONST static struct device %s;\n", ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000372 if (ptr->rescnt > 0)
Stefan Reinauer57879c92012-07-31 16:47:25 -0700373 fprintf(fil, "ROMSTAGE_CONST struct resource %s_res[];\n", ptr->name);
Myles Watson894a3472010-06-09 22:41:35 +0000374 if (ptr->children || ptr->multidev)
Stefan Reinauer57879c92012-07-31 16:47:25 -0700375 fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[];\n",
376 ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000377 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000378}
379
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200380static void pass1(FILE *fil, struct device *ptr)
381{
382 int pin;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000383 if (!ptr->used && (ptr->type == device)) {
Stefan Reinauerdf61dd22010-08-09 12:02:00 +0000384 if (ptr->id != 0)
Patrick Georgi2dbfcb72012-05-30 16:26:30 +0200385 fprintf(fil, "static ");
Stefan Reinauer57879c92012-07-31 16:47:25 -0700386 fprintf(fil, "ROMSTAGE_CONST struct device %s = {\n", ptr->name);
387 fprintf(fil, "#ifndef __PRE_RAM__\n");
Patrick Georgi114e7b22010-05-05 11:19:50 +0000388 fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0");
Stefan Reinauer57879c92012-07-31 16:47:25 -0700389 fprintf(fil, "#endif\n");
Myles Watson894a3472010-06-09 22:41:35 +0000390 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000391 fprintf(fil, "\t.path = {");
392 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
393 fprintf(fil, "},\n");
394 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
395 fprintf(fil, "\t.on_mainboard = 1,\n");
Sven Schnelle270a9082011-03-01 19:58:15 +0000396 if (ptr->subsystem_vendor > 0)
397 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", ptr->subsystem_vendor);
398
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200399 for(pin = 0; pin < 4; pin++) {
400 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
401 fprintf(fil, "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
402
403 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
404 fprintf(fil, "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_dst_id);
405 }
406
Sven Schnelle270a9082011-03-01 19:58:15 +0000407 if (ptr->subsystem_device > 0)
408 fprintf(fil, "\t.subsystem_device = 0x%04x,\n", ptr->subsystem_device);
409
Patrick Georgi114e7b22010-05-05 11:19:50 +0000410 if (ptr->rescnt > 0) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000411 fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000412 }
413 int link = 0;
Myles Watson894a3472010-06-09 22:41:35 +0000414 if (ptr->children || ptr->multidev)
415 fprintf(fil, "\t.link_list = &%s_links[0],\n", ptr->name);
416 else
Patrick Georgi2dbfcb72012-05-30 16:26:30 +0200417 fprintf(fil, "\t.link_list = NULL,\n");
Patrick Georgi114e7b22010-05-05 11:19:50 +0000418 if (ptr->sibling)
419 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200420 fprintf(fil, "#ifndef __PRE_RAM__\n");
421 fprintf(fil, "\t.chip_ops = &%s_ops,\n", ptr->chip->name_underscore);
422 fprintf(fil, "#endif\n");
423 if (ptr->chip->chiph_exists)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000424 fprintf(fil, "\t.chip_info = &%s_info_%d,\n", ptr->chip->name_underscore, ptr->chip->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000425 if (ptr->nextdev)
426 fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name);
427 fprintf(fil, "};\n");
428 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000429 if (ptr->rescnt > 0) {
430 int i=1;
Stefan Reinauer57879c92012-07-31 16:47:25 -0700431 fprintf(fil, "ROMSTAGE_CONST struct resource %s_res[] = {\n",
432 ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000433 struct resource *r = ptr->res;
434 while (r) {
435 fprintf(fil, "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
436 if (r->type == IRQ) fprintf(fil, "IRQ");
437 if (r->type == DRQ) fprintf(fil, "DRQ");
438 if (r->type == IO) fprintf(fil, "IO");
439 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, r->base);
440 if (r->next)
441 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, i++);
442 else
443 fprintf(fil, ".next=NULL },\n");
444 r = r->next;
445 }
446 fprintf(fil, "\t };\n");
447 }
Myles Watson894a3472010-06-09 22:41:35 +0000448 if (!ptr->used && ptr->type == device && (ptr->children || ptr->multidev)) {
Stefan Reinauer57879c92012-07-31 16:47:25 -0700449 fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[] = {\n", ptr->name);
Myles Watson894a3472010-06-09 22:41:35 +0000450 if (ptr->multidev) {
451 struct device *d = ptr;
452 while (d) {
453 if (device_match(d, ptr)) {
454 fprintf(fil, "\t\t[%d] = {\n", d->link);
455 fprintf(fil, "\t\t\t.link_num = %d,\n", d->link);
456 fprintf(fil, "\t\t\t.dev = &%s,\n", d->name);
457 if (d->children)
458 fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name);
Myles Watson1965a232010-06-10 04:06:52 +0000459 if (d->next_sibling && device_match(d->next_sibling, ptr))
Myles Watson894a3472010-06-09 22:41:35 +0000460 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", d->name, d->link+1);
461 else
462 fprintf(fil, "\t\t\t.next = NULL,\n");
463 fprintf(fil, "\t\t},\n");
464 }
465 d = d->next_sibling;
466 }
467 } else {
468 if (ptr->children) {
469 fprintf(fil, "\t\t[0] = {\n");
470 fprintf(fil, "\t\t\t.link_num = 0,\n");
471 fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name);
472 fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name);
473 fprintf(fil, "\t\t\t.next = NULL,\n");
474 fprintf(fil, "\t\t},\n");
475 }
476 }
477 fprintf(fil, "\t};\n");
478 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000479 if ((ptr->type == chip) && (ptr->chiph_exists)) {
480 if (ptr->reg) {
Stefan Reinauer57879c92012-07-31 16:47:25 -0700481 fprintf(fil, "ROMSTAGE_CONST struct %s_config ROMSTAGE_CONST %s_info_%d = {\n",
482 ptr->name_underscore, ptr->name_underscore,
483 ptr->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000484 struct reg *r = ptr->reg;
485 while (r) {
486 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
487 r = r->next;
488 }
489 fprintf(fil, "};\n\n");
490 } else {
Stefan Reinauer57879c92012-07-31 16:47:25 -0700491 fprintf(fil, "ROMSTAGE_CONST struct %s_config ROMSTAGE_CONST %s_info_%d = { };\n",
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200492 ptr->name_underscore, ptr->name_underscore, ptr->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000493 }
494 }
495}
496
497static void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *, struct device*), struct device *chips) {
498 do {
499 func(fil, ptr);
500 ptr = ptr->next_sibling;
501 } while (ptr);
502}
503
Sven Schnelle270a9082011-03-01 19:58:15 +0000504static void inherit_subsystem_ids(FILE *file, struct device *dev)
505{
506 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +0000507
508 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
509 /* user already gave us a subsystem vendor/device */
510 return;
511 }
512
Sylvain "ythier" Hitier5325a482011-03-01 21:57:11 +0000513 for(p = dev; p && p != p->parent; p = p->parent) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000514
515 if (p->bustype != PCI && p->bustype != PCI_DOMAIN)
516 continue;
517
518 if (p->inherit_subsystem) {
519 dev->subsystem_vendor = p->subsystem_vendor;
520 dev->subsystem_device = p->subsystem_device;
521 break;
522 }
523 }
524}
525
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200526static void usage(void)
527{
528 printf("usage: sconfig vendor/mainboard outputdir [-{s|b|k} outputfile]\n");
529 printf("\t-s file\tcreate ramstage static device map\n");
530 printf("\t-b file\tcreate bootblock init_mainboard()\n");
531 printf("\t-k file\tcreate Kconfig devicetree section\n");
532 printf("Defaults to \"-s static.c\" if no {s|b|k} specified.\n");
533 exit (1);
534}
535
536
Patrick Georgi114e7b22010-05-05 11:19:50 +0000537int main(int argc, char** argv) {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200538 if (argc < 3)
539 usage();
540
Patrick Georgi114e7b22010-05-05 11:19:50 +0000541 char *mainboard=argv[1];
542 char *outputdir=argv[2];
543 char *devtree=malloc(strlen(mainboard)+30);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000544 sprintf(devtree, "src/mainboard/%s/devicetree.cb", mainboard);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200545 char *outputc;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000546
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200547 if (argc == 3) {
548 scan_mode = STATIC_MODE;
549 outputc=malloc(strlen(outputdir)+20);
550 sprintf(outputc, "%s/static.c", outputdir);
551 } else if ((argc == 5) && (argv[3][0] == '-') && (argv[3][2] == 0)) {
552
553 switch (argv[3][1]) {
554 case 's':
555 scan_mode = STATIC_MODE;
556 break;
557 case 'b':
558 scan_mode = BOOTBLOCK_MODE;
559 break;
560 case 'k':
561 scan_mode = KCONFIG_MODE;
562 break;
563 default:
564 usage();
565 break;
566 }
567 char *outputfile=argv[4];
568
569 outputc=malloc(strlen(outputdir)+strlen(outputfile)+2);
570 sprintf(outputc, "%s/%s", outputdir, outputfile);
571 }
572
573 headers.next = 0;
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700574#ifdef MAINBOARDS_HAVE_CHIP_H
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200575 if (scan_mode == STATIC_MODE) {
576 headers.next = malloc(sizeof(struct header));
577 headers.next->name = malloc(strlen(mainboard)+12);
578 headers.next->next = 0;
579 sprintf(headers.next->name, "mainboard/%s", mainboard);
580 }
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700581#endif
Patrick Georgi114e7b22010-05-05 11:19:50 +0000582
583 FILE *filec = fopen(devtree, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000584 if (!filec) {
585 fprintf(stderr, "Could not open file '%s' for reading: ", devtree);
586 perror(NULL);
587 exit(1);
588 }
589
Patrick Georgi114e7b22010-05-05 11:19:50 +0000590 yyrestart(filec);
591
Patrick Georgi68befd52010-05-05 12:05:25 +0000592 lastdev = head = &root;
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000593
Patrick Georgi114e7b22010-05-05 11:19:50 +0000594 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000595
Patrick Georgi114e7b22010-05-05 11:19:50 +0000596 fclose(filec);
597
598 if ((head->type == chip) && (!head->chiph_exists)) {
599 struct device *tmp = head;
600 head = &root;
601 while (head->next != tmp) head = head->next;
602 }
603
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200604 FILE *autogen = fopen(outputc, "w");
605 if (!autogen) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000606 fprintf(stderr, "Could not open file '%s' for writing: ", outputc);
607 perror(NULL);
608 exit(1);
609 }
610
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200611 struct header *h;
612 if (scan_mode == STATIC_MODE) {
613
614 fprintf(autogen, "#include <device/device.h>\n");
615 fprintf(autogen, "#include <device/pci.h>\n");
616 h = &headers;
617 while (h->next) {
618 h = h->next;
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200619 if (h->chiph_exists)
620 fprintf(autogen, "#include \"%s/chip.h\"\n", h->name);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200621 }
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200622 fprintf(autogen, "\n#ifndef __PRE_RAM__\n");
Kyösti Mälkkifee73df2012-08-21 11:37:11 +0300623 h = &headers;
624 while (h->next) {
625 h = h->next;
626 char *name_underscore = strdup(h->name);
627 translate_name(name_underscore, 0);
628 fprintf(autogen, "extern struct chip_operations %s_ops;\n", name_underscore);
629 free(name_underscore);
630 }
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200631 fprintf(autogen, "#endif\n");
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200632
633 walk_device_tree(autogen, &root, inherit_subsystem_ids, NULL);
634 fprintf(autogen, "\n/* pass 0 */\n");
635 walk_device_tree(autogen, &root, pass0, NULL);
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700636 fprintf(autogen, "\n/* pass 1 */\n"
Stefan Reinauer57879c92012-07-31 16:47:25 -0700637 "ROMSTAGE_CONST struct device * ROMSTAGE_CONST last_dev = &%s;\n", lastdev->name);
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700638#ifdef MAINBOARDS_HAVE_CHIP_H
Stefan Reinauer57879c92012-07-31 16:47:25 -0700639 fprintf(autogen, "static ROMSTAGE_CONST struct mainboard_config ROMSTAGE_CONST mainboard_info_0;\n");
Stefan Reinauera675d492012-08-07 14:50:47 -0700640#else
641 fprintf(autogen, "extern struct chip_operations mainboard_ops;\n");
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700642#endif
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200643 walk_device_tree(autogen, &root, pass1, NULL);
644
645 } else if (scan_mode == BOOTBLOCK_MODE) {
646 h = &headers;
647 while (h->next) {
648 h = h->next;
649 fprintf(autogen, "#include \"%s/bootblock.c\"\n", h->name);
650 }
651
652 fprintf(autogen, "\n#if CONFIG_HAS_MAINBOARD_BOOTBLOCK\n");
653 fprintf(autogen, "#include \"mainboard/%s/bootblock.c\"\n", mainboard);
654 fprintf(autogen, "#else\n");
655 fprintf(autogen, "static unsigned long init_mainboard(int bsp_cpu)\n{\n");
656 fprintf(autogen, "\tif (! bsp_cpu) return 0;\n");
657 h = &headers;
658 while (h->next) {
659 h = h->next;
660 translate_name(h->name, 0);
661 fprintf(autogen, "\tinit_%s();\n", h->name);
662 }
663
664 fprintf(autogen, "\treturn 0;\n}\n");
665 fprintf(autogen, "#endif\n");
666
667 } else if (scan_mode == KCONFIG_MODE) {
668 fprintf(autogen, "\nconfig MAINBOARD_DIR\n\tstring\n");
669 fprintf(autogen, "\tdefault %s\n", mainboard);
670
671 fprintf(autogen, "\nconfig MAINBOARD_DEVTREE\n\tdef_bool y\n");
672 h = &headers;
673 while (h->next) {
674 h = h->next;
675 translate_name(h->name, 1);
676 fprintf(autogen, "\tselect %s\n", h->name);
677 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000678 }
Sven Schnelle270a9082011-03-01 19:58:15 +0000679
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200680 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000681
Patrick Georgi114e7b22010-05-05 11:19:50 +0000682 return 0;
683}