blob: 91d85fed87b128bb3de422ecfd7681b561011d86 [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);
Kyösti Mälkkia93c3fe2012-10-09 22:28:56 +0300422 if (ptr->chip->chip == &mainboard)
423 fprintf(fil, "\t.name = mainboard_name,\n");
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200424 fprintf(fil, "#endif\n");
425 if (ptr->chip->chiph_exists)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000426 fprintf(fil, "\t.chip_info = &%s_info_%d,\n", ptr->chip->name_underscore, ptr->chip->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000427 if (ptr->nextdev)
428 fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name);
429 fprintf(fil, "};\n");
430 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000431 if (ptr->rescnt > 0) {
432 int i=1;
Stefan Reinauer57879c92012-07-31 16:47:25 -0700433 fprintf(fil, "ROMSTAGE_CONST struct resource %s_res[] = {\n",
434 ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000435 struct resource *r = ptr->res;
436 while (r) {
437 fprintf(fil, "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
438 if (r->type == IRQ) fprintf(fil, "IRQ");
439 if (r->type == DRQ) fprintf(fil, "DRQ");
440 if (r->type == IO) fprintf(fil, "IO");
441 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, r->base);
442 if (r->next)
443 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, i++);
444 else
445 fprintf(fil, ".next=NULL },\n");
446 r = r->next;
447 }
448 fprintf(fil, "\t };\n");
449 }
Myles Watson894a3472010-06-09 22:41:35 +0000450 if (!ptr->used && ptr->type == device && (ptr->children || ptr->multidev)) {
Stefan Reinauer57879c92012-07-31 16:47:25 -0700451 fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[] = {\n", ptr->name);
Myles Watson894a3472010-06-09 22:41:35 +0000452 if (ptr->multidev) {
453 struct device *d = ptr;
454 while (d) {
455 if (device_match(d, ptr)) {
456 fprintf(fil, "\t\t[%d] = {\n", d->link);
457 fprintf(fil, "\t\t\t.link_num = %d,\n", d->link);
458 fprintf(fil, "\t\t\t.dev = &%s,\n", d->name);
459 if (d->children)
460 fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name);
Myles Watson1965a232010-06-10 04:06:52 +0000461 if (d->next_sibling && device_match(d->next_sibling, ptr))
Myles Watson894a3472010-06-09 22:41:35 +0000462 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", d->name, d->link+1);
463 else
464 fprintf(fil, "\t\t\t.next = NULL,\n");
465 fprintf(fil, "\t\t},\n");
466 }
467 d = d->next_sibling;
468 }
469 } else {
470 if (ptr->children) {
471 fprintf(fil, "\t\t[0] = {\n");
472 fprintf(fil, "\t\t\t.link_num = 0,\n");
473 fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name);
474 fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name);
475 fprintf(fil, "\t\t\t.next = NULL,\n");
476 fprintf(fil, "\t\t},\n");
477 }
478 }
479 fprintf(fil, "\t};\n");
480 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000481 if ((ptr->type == chip) && (ptr->chiph_exists)) {
482 if (ptr->reg) {
Stefan Reinauer57879c92012-07-31 16:47:25 -0700483 fprintf(fil, "ROMSTAGE_CONST struct %s_config ROMSTAGE_CONST %s_info_%d = {\n",
484 ptr->name_underscore, ptr->name_underscore,
485 ptr->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000486 struct reg *r = ptr->reg;
487 while (r) {
488 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
489 r = r->next;
490 }
491 fprintf(fil, "};\n\n");
492 } else {
Stefan Reinauer57879c92012-07-31 16:47:25 -0700493 fprintf(fil, "ROMSTAGE_CONST struct %s_config ROMSTAGE_CONST %s_info_%d = { };\n",
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200494 ptr->name_underscore, ptr->name_underscore, ptr->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000495 }
496 }
497}
498
499static void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *, struct device*), struct device *chips) {
500 do {
501 func(fil, ptr);
502 ptr = ptr->next_sibling;
503 } while (ptr);
504}
505
Sven Schnelle270a9082011-03-01 19:58:15 +0000506static void inherit_subsystem_ids(FILE *file, struct device *dev)
507{
508 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +0000509
510 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
511 /* user already gave us a subsystem vendor/device */
512 return;
513 }
514
Sylvain "ythier" Hitier5325a482011-03-01 21:57:11 +0000515 for(p = dev; p && p != p->parent; p = p->parent) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000516
517 if (p->bustype != PCI && p->bustype != PCI_DOMAIN)
518 continue;
519
520 if (p->inherit_subsystem) {
521 dev->subsystem_vendor = p->subsystem_vendor;
522 dev->subsystem_device = p->subsystem_device;
523 break;
524 }
525 }
526}
527
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200528static void usage(void)
529{
530 printf("usage: sconfig vendor/mainboard outputdir [-{s|b|k} outputfile]\n");
531 printf("\t-s file\tcreate ramstage static device map\n");
532 printf("\t-b file\tcreate bootblock init_mainboard()\n");
533 printf("\t-k file\tcreate Kconfig devicetree section\n");
534 printf("Defaults to \"-s static.c\" if no {s|b|k} specified.\n");
535 exit (1);
536}
537
538
Patrick Georgi114e7b22010-05-05 11:19:50 +0000539int main(int argc, char** argv) {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200540 if (argc < 3)
541 usage();
542
Patrick Georgi114e7b22010-05-05 11:19:50 +0000543 char *mainboard=argv[1];
544 char *outputdir=argv[2];
545 char *devtree=malloc(strlen(mainboard)+30);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000546 sprintf(devtree, "src/mainboard/%s/devicetree.cb", mainboard);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200547 char *outputc;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000548
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200549 if (argc == 3) {
550 scan_mode = STATIC_MODE;
551 outputc=malloc(strlen(outputdir)+20);
552 sprintf(outputc, "%s/static.c", outputdir);
553 } else if ((argc == 5) && (argv[3][0] == '-') && (argv[3][2] == 0)) {
554
555 switch (argv[3][1]) {
556 case 's':
557 scan_mode = STATIC_MODE;
558 break;
559 case 'b':
560 scan_mode = BOOTBLOCK_MODE;
561 break;
562 case 'k':
563 scan_mode = KCONFIG_MODE;
564 break;
565 default:
566 usage();
567 break;
568 }
569 char *outputfile=argv[4];
570
571 outputc=malloc(strlen(outputdir)+strlen(outputfile)+2);
572 sprintf(outputc, "%s/%s", outputdir, outputfile);
573 }
574
575 headers.next = 0;
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700576#ifdef MAINBOARDS_HAVE_CHIP_H
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200577 if (scan_mode == STATIC_MODE) {
578 headers.next = malloc(sizeof(struct header));
579 headers.next->name = malloc(strlen(mainboard)+12);
580 headers.next->next = 0;
581 sprintf(headers.next->name, "mainboard/%s", mainboard);
582 }
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700583#endif
Patrick Georgi114e7b22010-05-05 11:19:50 +0000584
585 FILE *filec = fopen(devtree, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000586 if (!filec) {
587 fprintf(stderr, "Could not open file '%s' for reading: ", devtree);
588 perror(NULL);
589 exit(1);
590 }
591
Patrick Georgi114e7b22010-05-05 11:19:50 +0000592 yyrestart(filec);
593
Patrick Georgi68befd52010-05-05 12:05:25 +0000594 lastdev = head = &root;
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000595
Patrick Georgi114e7b22010-05-05 11:19:50 +0000596 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000597
Patrick Georgi114e7b22010-05-05 11:19:50 +0000598 fclose(filec);
599
600 if ((head->type == chip) && (!head->chiph_exists)) {
601 struct device *tmp = head;
602 head = &root;
603 while (head->next != tmp) head = head->next;
604 }
605
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200606 FILE *autogen = fopen(outputc, "w");
607 if (!autogen) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000608 fprintf(stderr, "Could not open file '%s' for writing: ", outputc);
609 perror(NULL);
610 exit(1);
611 }
612
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200613 struct header *h;
614 if (scan_mode == STATIC_MODE) {
615
616 fprintf(autogen, "#include <device/device.h>\n");
617 fprintf(autogen, "#include <device/pci.h>\n");
618 h = &headers;
619 while (h->next) {
620 h = h->next;
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200621 if (h->chiph_exists)
622 fprintf(autogen, "#include \"%s/chip.h\"\n", h->name);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200623 }
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200624 fprintf(autogen, "\n#ifndef __PRE_RAM__\n");
Kyösti Mälkkifee73df2012-08-21 11:37:11 +0300625 h = &headers;
626 while (h->next) {
627 h = h->next;
628 char *name_underscore = strdup(h->name);
629 translate_name(name_underscore, 0);
630 fprintf(autogen, "extern struct chip_operations %s_ops;\n", name_underscore);
631 free(name_underscore);
632 }
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200633 fprintf(autogen, "#endif\n");
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200634
635 walk_device_tree(autogen, &root, inherit_subsystem_ids, NULL);
636 fprintf(autogen, "\n/* pass 0 */\n");
637 walk_device_tree(autogen, &root, pass0, NULL);
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700638 fprintf(autogen, "\n/* pass 1 */\n"
Stefan Reinauer57879c92012-07-31 16:47:25 -0700639 "ROMSTAGE_CONST struct device * ROMSTAGE_CONST last_dev = &%s;\n", lastdev->name);
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700640#ifdef MAINBOARDS_HAVE_CHIP_H
Stefan Reinauer57879c92012-07-31 16:47:25 -0700641 fprintf(autogen, "static ROMSTAGE_CONST struct mainboard_config ROMSTAGE_CONST mainboard_info_0;\n");
Stefan Reinauera675d492012-08-07 14:50:47 -0700642#else
643 fprintf(autogen, "extern struct chip_operations mainboard_ops;\n");
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700644#endif
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200645 walk_device_tree(autogen, &root, pass1, NULL);
646
647 } else if (scan_mode == BOOTBLOCK_MODE) {
648 h = &headers;
649 while (h->next) {
650 h = h->next;
651 fprintf(autogen, "#include \"%s/bootblock.c\"\n", h->name);
652 }
653
654 fprintf(autogen, "\n#if CONFIG_HAS_MAINBOARD_BOOTBLOCK\n");
655 fprintf(autogen, "#include \"mainboard/%s/bootblock.c\"\n", mainboard);
656 fprintf(autogen, "#else\n");
657 fprintf(autogen, "static unsigned long init_mainboard(int bsp_cpu)\n{\n");
658 fprintf(autogen, "\tif (! bsp_cpu) return 0;\n");
659 h = &headers;
660 while (h->next) {
661 h = h->next;
662 translate_name(h->name, 0);
663 fprintf(autogen, "\tinit_%s();\n", h->name);
664 }
665
666 fprintf(autogen, "\treturn 0;\n}\n");
667 fprintf(autogen, "#endif\n");
668
669 } else if (scan_mode == KCONFIG_MODE) {
670 fprintf(autogen, "\nconfig MAINBOARD_DIR\n\tstring\n");
671 fprintf(autogen, "\tdefault %s\n", mainboard);
672
673 fprintf(autogen, "\nconfig MAINBOARD_DEVTREE\n\tdef_bool y\n");
674 h = &headers;
675 while (h->next) {
676 h = h->next;
677 translate_name(h->name, 1);
678 fprintf(autogen, "\tselect %s\n", h->name);
679 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000680 }
Sven Schnelle270a9082011-03-01 19:58:15 +0000681
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200682 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000683
Patrick Georgi114e7b22010-05-05 11:19:50 +0000684 return 0;
685}