blob: 27027f567c9cbca3b9d59b80ab25856782c05e96 [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.
Patrick Georgi114e7b22010-05-05 11:19:50 +000015 */
16
Patrick Georgi2dbfcb72012-05-30 16:26:30 +020017#include <ctype.h>
Patrick Georgi114e7b22010-05-05 11:19:50 +000018#include "sconfig.h"
19#include "sconfig.tab.h"
20
Patrick Georgi7fc9e292010-07-15 15:59:07 +000021extern int linenum;
22
Patrick Georgi114e7b22010-05-05 11:19:50 +000023struct device *head, *lastdev;
24
25struct header headers;
26
27static int devcount = 0;
28
Kyösti Mälkki472d9022011-12-05 20:33:55 +020029typedef enum {
30 STATIC_MODE,
31 BOOTBLOCK_MODE,
32 KCONFIG_MODE
33} scan_t;
34
35static scan_t scan_mode = STATIC_MODE;
36
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +020037typedef enum {
38 UNSLASH,
39 SPLIT_1ST,
40 TO_LOWER,
41 TO_UPPER,
42} translate_t;
43
Patrick Georgi114e7b22010-05-05 11:19:50 +000044static struct device root;
45static struct device mainboard = {
46 .name = "mainboard",
47 .name_underscore = "mainboard",
48 .id = 0,
49 .chip = &mainboard,
50 .type = chip,
Stefan Reinauer188e3c22012-07-26 12:46:48 -070051#ifdef MAINBOARDS_HAVE_CHIP_H
Patrick Georgi114e7b22010-05-05 11:19:50 +000052 .chiph_exists = 1,
Stefan Reinauer188e3c22012-07-26 12:46:48 -070053#else
54 .chiph_exists = 0,
55#endif
Patrick Georgi114e7b22010-05-05 11:19:50 +000056 .children = &root
57};
58
59static struct device root = {
60 .name = "dev_root",
61 .name_underscore = "dev_root",
62 .id = 0,
63 .chip = &mainboard,
64 .type = device,
65 .path = " .type = DEVICE_PATH_ROOT ",
66 .ops = "&default_dev_ops_root",
67 .parent = &root,
68 .bus = &root,
69 .enabled = 1
70};
71
Patrick Georgi68befd52010-05-05 12:05:25 +000072static struct device *new_dev(struct device *parent, struct device *bus) {
Patrick Georgi114e7b22010-05-05 11:19:50 +000073 struct device *dev = malloc(sizeof(struct device));
74 memset(dev, 0, sizeof(struct device));
75 dev->id = ++devcount;
Patrick Georgi68befd52010-05-05 12:05:25 +000076 dev->parent = parent;
77 dev->bus = bus;
Sven Schnelle270a9082011-03-01 19:58:15 +000078 dev->subsystem_vendor = -1;
79 dev->subsystem_device = -1;
Patrick Georgi114e7b22010-05-05 11:19:50 +000080 head->next = dev;
81 head = dev;
82 return dev;
83}
84
85static int device_match(struct device *a, struct device *b) {
86 if ((a->bustype == b->bustype) && (a->bus == b->bus) && (a->path_a == b->path_a) && (a->path_b == b->path_b))
87 return 1;
88 return 0;
89}
90
91void fold_in(struct device *parent) {
92 struct device *child = parent->children;
93 struct device *latest = 0;
94 while (child != latest) {
95 if (child->children) {
96 if (!latest) latest = child->children;
97 parent->latestchild->next_sibling = child->children;
98 parent->latestchild = child->latestchild;
99 }
100 child = child->next_sibling;
101 }
102}
103
104int yywrap(void) {
105 return 1;
106}
107
108void yyerror (char const *str)
109{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000110 extern char *yytext;
111 fprintf (stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
112 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000113}
114
115void postprocess_devtree(void) {
116 root.next_sibling = root.children;
117 root.next_sibling->next_sibling = root.next_sibling->children;
118
119 struct device *dev = &root;
120 while (dev) {
121 /* skip "chip" elements in children chain */
122 while (dev->children && (dev->children->type == chip)) dev->children = dev->children->children;
123 /* skip "chip" elements and functions of the same device in sibling chain */
124 while (dev->sibling && dev->sibling->used) dev->sibling = dev->sibling->sibling;
125 /* If end of chain, and parent is a chip, move on */
126 if (!dev->sibling && (dev->parent->type == chip)) dev->sibling = dev->parent->sibling;
127 /* skip chips */
128 while (dev->sibling && dev->sibling->type == chip) dev->sibling = dev->sibling->children;
129 /* skip duplicate function elements in nextdev chain */
130 while (dev->nextdev && dev->nextdev->used) dev->nextdev = dev->nextdev->nextdev;
131 dev = dev->next_sibling;
132 }
133}
134
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200135char * translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200136{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200137 char *b, *c;
138 b = c = strdup(str);
139 while (c && *c) {
140 if ((mode == SPLIT_1ST) && (*c == '/')) {
141 *c = 0;
142 break;
143 }
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200144 if (*c == '/') *c = '_';
145 if (*c == '-') *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200146 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200147 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200148 if (mode == TO_LOWER)
149 *c = tolower(*c);
150 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200151 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200152 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200153}
154
Patrick Georgi68befd52010-05-05 12:05:25 +0000155struct device *new_chip(struct device *parent, struct device *bus, char *path) {
156 struct device *new_chip = new_dev(parent, bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000157 new_chip->chiph_exists = 1;
158 new_chip->name = path;
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200159 new_chip->name_underscore = translate_name(new_chip->name, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000160 new_chip->type = chip;
161 new_chip->chip = new_chip;
162
163 struct stat st;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200164 char *chip_h = malloc(strlen(path)+18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700165 sprintf(chip_h, "src/%s", path);
166 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Patrick Georgi92bcaa22015-11-13 09:39:22 +0100167 /* root_complex gets away without a separate directory, but
168 * exists on on pretty much all AMD chipsets.
169 */
170 if (!strstr(path, "/root_complex")) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300171 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
172 path);
173 exit(1);
174 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700175 }
176
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200177 if (scan_mode == STATIC_MODE)
178 sprintf(chip_h, "src/%s/chip.h", path);
179 else if (scan_mode == BOOTBLOCK_MODE)
180 sprintf(chip_h, "src/%s/bootblock.c", path);
181
182 if ((scan_mode == STATIC_MODE) || (scan_mode == BOOTBLOCK_MODE)) {
183 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
184 new_chip->chiph_exists = 0;
185 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000186
Patrick Georgi68befd52010-05-05 12:05:25 +0000187 if (parent->latestchild) {
188 parent->latestchild->next_sibling = new_chip;
189 parent->latestchild->sibling = new_chip;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000190 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000191 parent->latestchild = new_chip;
192 if (!parent->children)
193 parent->children = new_chip;
Patrick Georgi1f688802014-08-03 15:51:19 +0200194 free(chip_h);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000195 return new_chip;
196}
197
198void add_header(struct device *dev) {
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200199 int include_exists = 0;
200 struct header *h = &headers;
201 while (h->next) {
202 int result = strcmp(dev->name, h->next->name);
203 if (result == 0) {
204 include_exists = 1;
205 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000206 }
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200207 if (result < 0) break;
208 h = h->next;
209 }
210 if (!include_exists) {
211 struct header *tmp = h->next;
212 h->next = malloc(sizeof(struct header));
213 memset(h->next, 0, sizeof(struct header));
214 h->next->chiph_exists = dev->chiph_exists;
215 h->next->name = dev->name;
216 h->next->next = tmp;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000217 }
218}
219
Patrick Georgi68befd52010-05-05 12:05:25 +0000220struct device *new_device(struct device *parent, struct device *busdev, const int bus, const char *devnum, int enabled) {
221 struct device *new_d = new_dev(parent, busdev);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000222 new_d->bustype = bus;
223
224 char *tmp;
Patrick Georgi1f688802014-08-03 15:51:19 +0200225 new_d->path_a = strtol(devnum, &tmp, 16);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000226 if (*tmp == '.') {
227 tmp++;
228 new_d->path_b = strtol(tmp, NULL, 16);
229 }
230
231 char *name = malloc(10);
232 sprintf(name, "_dev%d", new_d->id);
233 new_d->name = name;
234 new_d->name_underscore = name; // shouldn't be necessary, but avoid 0-ptr
235 new_d->type = device;
236 new_d->enabled = enabled;
237 new_d->chip = new_d->parent->chip;
238
Patrick Georgi68befd52010-05-05 12:05:25 +0000239 if (parent->latestchild) {
240 parent->latestchild->next_sibling = new_d;
241 parent->latestchild->sibling = new_d;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000242 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000243 parent->latestchild = new_d;
244 if (!parent->children)
245 parent->children = new_d;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000246
247 lastdev->nextdev = new_d;
248 lastdev = new_d;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200249
250 switch(bus) {
251 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000252 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200253 break;
254
255 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000256 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200257 break;
258
259 case I2C:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000260 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200261 break;
262
263 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000264 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200265 break;
266
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800267 case CPU_CLUSTER:
268 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200269 break;
270
Aaron Durbinffda804b2014-09-03 12:40:15 -0500271 case CPU:
272 new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}";
273 break;
274
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800275 case DOMAIN:
276 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200277 break;
278
279 case IOAPIC:
280 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
281 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000282 }
283 return new_d;
284}
285
286void alias_siblings(struct device *d) {
287 while (d) {
288 int link = 0;
289 struct device *cmp = d->next_sibling;
290 while (cmp && (cmp->bus == d->bus) && (cmp->path_a == d->path_a) && (cmp->path_b == d->path_b)) {
291 if (cmp->type==device && !cmp->used) {
292 if (device_match(d, cmp)) {
293 d->multidev = 1;
294
Patrick Georgi114e7b22010-05-05 11:19:50 +0000295 cmp->id = d->id;
296 cmp->name = d->name;
297 cmp->used = 1;
298 cmp->link = ++link;
299 }
300 }
301 cmp = cmp->next_sibling;
302 }
303 d = d->next_sibling;
304 }
305}
306
Patrick Georgi68befd52010-05-05 12:05:25 +0000307void add_resource(struct device *dev, int type, int index, int base) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000308 struct resource *r = malloc(sizeof(struct resource));
309 memset (r, 0, sizeof(struct resource));
310 r->type = type;
311 r->index = index;
312 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000313 if (dev->res) {
314 struct resource *head = dev->res;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000315 while (head->next) head = head->next;
316 head->next = r;
317 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000318 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000319 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000320 dev->rescnt++;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000321}
322
Patrick Georgi68befd52010-05-05 12:05:25 +0000323void add_register(struct device *dev, char *name, char *val) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000324 struct reg *r = malloc(sizeof(struct reg));
325 memset (r, 0, sizeof(struct reg));
326 r->key = name;
327 r->value = val;
Patrick Georgi68befd52010-05-05 12:05:25 +0000328 if (dev->reg) {
329 struct reg *head = dev->reg;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000330 // sorting to be equal to sconfig's behaviour
331 int sort = strcmp(r->key, head->key);
332 if (sort == 0) {
333 printf("ERROR: duplicate 'register' key.\n");
334 exit(1);
335 }
336 if (sort<0) {
337 r->next = head;
Patrick Georgi68befd52010-05-05 12:05:25 +0000338 dev->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000339 } else {
340 while ((head->next) && (strcmp(head->next->key, r->key)<0)) head = head->next;
341 r->next = head->next;
342 head->next = r;
343 }
344 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000345 dev->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000346 }
347}
348
Sven Schnelle270a9082011-03-01 19:58:15 +0000349void add_pci_subsystem_ids(struct device *dev, int vendor, int device, int inherit)
350{
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800351 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000352 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
353 exit(1);
354 }
355
356 dev->subsystem_vendor = vendor;
357 dev->subsystem_device = device;
358 dev->inherit_subsystem = inherit;
359}
360
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200361void add_ioapic_info(struct device *dev, int apicid, const char *_srcpin, int irqpin)
362{
363
364 int srcpin;
365
366 if (!_srcpin || strlen(_srcpin) < 4 ||strncasecmp(_srcpin, "INT", 3) ||
367 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
368 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
369 exit(1);
370 }
371
372 srcpin = _srcpin[3] - 'A';
373
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800374 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200375 printf("ERROR: ioapic config only allowed for PCI devices\n");
376 exit(1);
377 }
378
379 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200380 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200381 exit(1);
382 }
383 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
384 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
385}
386
Patrick Georgi114e7b22010-05-05 11:19:50 +0000387static void pass0(FILE *fil, struct device *ptr) {
Myles Watson894a3472010-06-09 22:41:35 +0000388 if (ptr->type == device && ptr->id == 0)
Stefan Reinauer57879c92012-07-31 16:47:25 -0700389 fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[];\n", ptr->name);
390
Myles Watsonc25cc112010-05-21 14:33:48 +0000391 if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) {
Stefan Reinauer57879c92012-07-31 16:47:25 -0700392 fprintf(fil, "ROMSTAGE_CONST static struct device %s;\n", ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000393 if (ptr->rescnt > 0)
Stefan Reinauer57879c92012-07-31 16:47:25 -0700394 fprintf(fil, "ROMSTAGE_CONST struct resource %s_res[];\n", ptr->name);
Myles Watson894a3472010-06-09 22:41:35 +0000395 if (ptr->children || ptr->multidev)
Stefan Reinauer57879c92012-07-31 16:47:25 -0700396 fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[];\n",
397 ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000398 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000399}
400
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200401static void pass1(FILE *fil, struct device *ptr)
402{
403 int pin;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000404 if (!ptr->used && (ptr->type == device)) {
Stefan Reinauerdf61dd22010-08-09 12:02:00 +0000405 if (ptr->id != 0)
Patrick Georgi2dbfcb72012-05-30 16:26:30 +0200406 fprintf(fil, "static ");
Stefan Reinauer57879c92012-07-31 16:47:25 -0700407 fprintf(fil, "ROMSTAGE_CONST struct device %s = {\n", ptr->name);
408 fprintf(fil, "#ifndef __PRE_RAM__\n");
Patrick Georgi114e7b22010-05-05 11:19:50 +0000409 fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0");
Stefan Reinauer57879c92012-07-31 16:47:25 -0700410 fprintf(fil, "#endif\n");
Myles Watson894a3472010-06-09 22:41:35 +0000411 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000412 fprintf(fil, "\t.path = {");
413 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
414 fprintf(fil, "},\n");
415 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
416 fprintf(fil, "\t.on_mainboard = 1,\n");
Sven Schnelle270a9082011-03-01 19:58:15 +0000417 if (ptr->subsystem_vendor > 0)
418 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", ptr->subsystem_vendor);
419
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200420 for(pin = 0; pin < 4; pin++) {
421 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
422 fprintf(fil, "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
423
424 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
425 fprintf(fil, "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_dst_id);
426 }
427
Sven Schnelle270a9082011-03-01 19:58:15 +0000428 if (ptr->subsystem_device > 0)
429 fprintf(fil, "\t.subsystem_device = 0x%04x,\n", ptr->subsystem_device);
430
Patrick Georgi114e7b22010-05-05 11:19:50 +0000431 if (ptr->rescnt > 0) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000432 fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000433 }
Myles Watson894a3472010-06-09 22:41:35 +0000434 if (ptr->children || ptr->multidev)
435 fprintf(fil, "\t.link_list = &%s_links[0],\n", ptr->name);
436 else
Patrick Georgi2dbfcb72012-05-30 16:26:30 +0200437 fprintf(fil, "\t.link_list = NULL,\n");
Patrick Georgi114e7b22010-05-05 11:19:50 +0000438 if (ptr->sibling)
439 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200440 fprintf(fil, "#ifndef __PRE_RAM__\n");
441 fprintf(fil, "\t.chip_ops = &%s_ops,\n", ptr->chip->name_underscore);
Kyösti Mälkkia93c3fe2012-10-09 22:28:56 +0300442 if (ptr->chip->chip == &mainboard)
443 fprintf(fil, "\t.name = mainboard_name,\n");
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200444 fprintf(fil, "#endif\n");
445 if (ptr->chip->chiph_exists)
Patrick Georgi114e7b22010-05-05 11:19:50 +0000446 fprintf(fil, "\t.chip_info = &%s_info_%d,\n", ptr->chip->name_underscore, ptr->chip->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000447 if (ptr->nextdev)
448 fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name);
449 fprintf(fil, "};\n");
450 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000451 if (ptr->rescnt > 0) {
452 int i=1;
Stefan Reinauer57879c92012-07-31 16:47:25 -0700453 fprintf(fil, "ROMSTAGE_CONST struct resource %s_res[] = {\n",
454 ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000455 struct resource *r = ptr->res;
456 while (r) {
457 fprintf(fil, "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
458 if (r->type == IRQ) fprintf(fil, "IRQ");
459 if (r->type == DRQ) fprintf(fil, "DRQ");
460 if (r->type == IO) fprintf(fil, "IO");
461 fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, r->base);
462 if (r->next)
463 fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, i++);
464 else
465 fprintf(fil, ".next=NULL },\n");
466 r = r->next;
467 }
468 fprintf(fil, "\t };\n");
469 }
Myles Watson894a3472010-06-09 22:41:35 +0000470 if (!ptr->used && ptr->type == device && (ptr->children || ptr->multidev)) {
Stefan Reinauer57879c92012-07-31 16:47:25 -0700471 fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[] = {\n", ptr->name);
Myles Watson894a3472010-06-09 22:41:35 +0000472 if (ptr->multidev) {
473 struct device *d = ptr;
474 while (d) {
475 if (device_match(d, ptr)) {
476 fprintf(fil, "\t\t[%d] = {\n", d->link);
477 fprintf(fil, "\t\t\t.link_num = %d,\n", d->link);
478 fprintf(fil, "\t\t\t.dev = &%s,\n", d->name);
479 if (d->children)
480 fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name);
Myles Watson1965a232010-06-10 04:06:52 +0000481 if (d->next_sibling && device_match(d->next_sibling, ptr))
Myles Watson894a3472010-06-09 22:41:35 +0000482 fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", d->name, d->link+1);
483 else
484 fprintf(fil, "\t\t\t.next = NULL,\n");
485 fprintf(fil, "\t\t},\n");
486 }
487 d = d->next_sibling;
488 }
489 } else {
490 if (ptr->children) {
491 fprintf(fil, "\t\t[0] = {\n");
492 fprintf(fil, "\t\t\t.link_num = 0,\n");
493 fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name);
494 fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name);
495 fprintf(fil, "\t\t\t.next = NULL,\n");
496 fprintf(fil, "\t\t},\n");
497 }
498 }
499 fprintf(fil, "\t};\n");
500 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000501 if ((ptr->type == chip) && (ptr->chiph_exists)) {
502 if (ptr->reg) {
Patrick Georgi73586432014-01-18 16:23:32 +0100503 fprintf(fil, "ROMSTAGE_CONST struct %s_config %s_info_%d = {\n",
Stefan Reinauer57879c92012-07-31 16:47:25 -0700504 ptr->name_underscore, ptr->name_underscore,
505 ptr->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000506 struct reg *r = ptr->reg;
507 while (r) {
508 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
509 r = r->next;
510 }
511 fprintf(fil, "};\n\n");
512 } else {
Patrick Georgi73586432014-01-18 16:23:32 +0100513 fprintf(fil, "ROMSTAGE_CONST struct %s_config %s_info_%d = { };\n",
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200514 ptr->name_underscore, ptr->name_underscore, ptr->id);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000515 }
516 }
517}
518
519static void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *, struct device*), struct device *chips) {
520 do {
521 func(fil, ptr);
522 ptr = ptr->next_sibling;
523 } while (ptr);
524}
525
Sven Schnelle270a9082011-03-01 19:58:15 +0000526static void inherit_subsystem_ids(FILE *file, struct device *dev)
527{
528 struct device *p;
Sven Schnelle270a9082011-03-01 19:58:15 +0000529
530 if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) {
531 /* user already gave us a subsystem vendor/device */
532 return;
533 }
534
Sylvain "ythier" Hitier5325a482011-03-01 21:57:11 +0000535 for(p = dev; p && p != p->parent; p = p->parent) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000536
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800537 if (p->bustype != PCI && p->bustype != DOMAIN)
Sven Schnelle270a9082011-03-01 19:58:15 +0000538 continue;
539
540 if (p->inherit_subsystem) {
541 dev->subsystem_vendor = p->subsystem_vendor;
542 dev->subsystem_device = p->subsystem_device;
543 break;
544 }
545 }
546}
547
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200548static void usage(void)
549{
550 printf("usage: sconfig vendor/mainboard outputdir [-{s|b|k} outputfile]\n");
551 printf("\t-s file\tcreate ramstage static device map\n");
552 printf("\t-b file\tcreate bootblock init_mainboard()\n");
553 printf("\t-k file\tcreate Kconfig devicetree section\n");
554 printf("Defaults to \"-s static.c\" if no {s|b|k} specified.\n");
555 exit (1);
556}
557
558
Patrick Georgi114e7b22010-05-05 11:19:50 +0000559int main(int argc, char** argv) {
Daniele Forsiddf54b12014-07-27 11:02:15 +0200560 if (argc != 3 && argc != 5)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200561 usage();
562
Patrick Georgi114e7b22010-05-05 11:19:50 +0000563 char *mainboard=argv[1];
564 char *outputdir=argv[2];
565 char *devtree=malloc(strlen(mainboard)+30);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000566 sprintf(devtree, "src/mainboard/%s/devicetree.cb", mainboard);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200567 char *outputc;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000568
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200569 if (argc == 3) {
570 scan_mode = STATIC_MODE;
571 outputc=malloc(strlen(outputdir)+20);
572 sprintf(outputc, "%s/static.c", outputdir);
Patrick Georgi6fa68432014-08-03 15:27:35 +0200573 } else if (argc == 5) {
574 if ((argv[3][0] != '-') || (argv[3][2] == 0)) {
575 usage();
576 }
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200577
578 switch (argv[3][1]) {
579 case 's':
580 scan_mode = STATIC_MODE;
581 break;
582 case 'b':
583 scan_mode = BOOTBLOCK_MODE;
584 break;
585 case 'k':
586 scan_mode = KCONFIG_MODE;
587 break;
588 default:
589 usage();
590 break;
591 }
592 char *outputfile=argv[4];
593
594 outputc=malloc(strlen(outputdir)+strlen(outputfile)+2);
595 sprintf(outputc, "%s/%s", outputdir, outputfile);
596 }
597
598 headers.next = 0;
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700599#ifdef MAINBOARDS_HAVE_CHIP_H
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200600 if (scan_mode == STATIC_MODE) {
601 headers.next = malloc(sizeof(struct header));
602 headers.next->name = malloc(strlen(mainboard)+12);
603 headers.next->next = 0;
604 sprintf(headers.next->name, "mainboard/%s", mainboard);
605 }
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700606#endif
Patrick Georgi114e7b22010-05-05 11:19:50 +0000607
608 FILE *filec = fopen(devtree, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000609 if (!filec) {
610 fprintf(stderr, "Could not open file '%s' for reading: ", devtree);
611 perror(NULL);
612 exit(1);
613 }
614
Patrick Georgi114e7b22010-05-05 11:19:50 +0000615 yyrestart(filec);
616
Patrick Georgi68befd52010-05-05 12:05:25 +0000617 lastdev = head = &root;
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000618
Patrick Georgi114e7b22010-05-05 11:19:50 +0000619 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000620
Patrick Georgi114e7b22010-05-05 11:19:50 +0000621 fclose(filec);
622
623 if ((head->type == chip) && (!head->chiph_exists)) {
624 struct device *tmp = head;
625 head = &root;
626 while (head->next != tmp) head = head->next;
627 }
628
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200629 FILE *autogen = fopen(outputc, "w");
630 if (!autogen) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000631 fprintf(stderr, "Could not open file '%s' for writing: ", outputc);
632 perror(NULL);
633 exit(1);
634 }
635
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200636 struct header *h;
637 if (scan_mode == STATIC_MODE) {
638
639 fprintf(autogen, "#include <device/device.h>\n");
640 fprintf(autogen, "#include <device/pci.h>\n");
641 h = &headers;
642 while (h->next) {
643 h = h->next;
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200644 if (h->chiph_exists)
645 fprintf(autogen, "#include \"%s/chip.h\"\n", h->name);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200646 }
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200647 fprintf(autogen, "\n#ifndef __PRE_RAM__\n");
Kyösti Mälkkie773c922012-11-15 07:05:43 +0200648 fprintf(autogen, "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Kyösti Mälkkifee73df2012-08-21 11:37:11 +0300649 h = &headers;
650 while (h->next) {
651 h = h->next;
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200652 char *name_underscore = translate_name(h->name, UNSLASH);
Patrick Georgib6f765e2012-10-07 22:04:52 +0200653 fprintf(autogen, "__attribute__((weak)) struct chip_operations %s_ops = {};\n", name_underscore);
Kyösti Mälkkifee73df2012-08-21 11:37:11 +0300654 free(name_underscore);
655 }
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200656 fprintf(autogen, "#endif\n");
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200657
658 walk_device_tree(autogen, &root, inherit_subsystem_ids, NULL);
659 fprintf(autogen, "\n/* pass 0 */\n");
660 walk_device_tree(autogen, &root, pass0, NULL);
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700661 fprintf(autogen, "\n/* pass 1 */\n"
Stefan Reinauer57879c92012-07-31 16:47:25 -0700662 "ROMSTAGE_CONST struct device * ROMSTAGE_CONST last_dev = &%s;\n", lastdev->name);
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700663#ifdef MAINBOARDS_HAVE_CHIP_H
Stefan Reinauer57879c92012-07-31 16:47:25 -0700664 fprintf(autogen, "static ROMSTAGE_CONST struct mainboard_config ROMSTAGE_CONST mainboard_info_0;\n");
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700665#endif
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200666 walk_device_tree(autogen, &root, pass1, NULL);
667
668 } else if (scan_mode == BOOTBLOCK_MODE) {
669 h = &headers;
670 while (h->next) {
671 h = h->next;
672 fprintf(autogen, "#include \"%s/bootblock.c\"\n", h->name);
673 }
674
675 fprintf(autogen, "\n#if CONFIG_HAS_MAINBOARD_BOOTBLOCK\n");
676 fprintf(autogen, "#include \"mainboard/%s/bootblock.c\"\n", mainboard);
677 fprintf(autogen, "#else\n");
678 fprintf(autogen, "static unsigned long init_mainboard(int bsp_cpu)\n{\n");
679 fprintf(autogen, "\tif (! bsp_cpu) return 0;\n");
680 h = &headers;
681 while (h->next) {
682 h = h->next;
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200683 char * buf = translate_name(h->name, UNSLASH);
684 if (buf) {
685 fprintf(autogen, "\tinit_%s();\n", buf);
686 free(buf);
687 }
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200688 }
689
690 fprintf(autogen, "\treturn 0;\n}\n");
691 fprintf(autogen, "#endif\n");
692
693 } else if (scan_mode == KCONFIG_MODE) {
694 fprintf(autogen, "\nconfig MAINBOARD_DIR\n\tstring\n");
695 fprintf(autogen, "\tdefault %s\n", mainboard);
696
697 fprintf(autogen, "\nconfig MAINBOARD_DEVTREE\n\tdef_bool y\n");
698 h = &headers;
699 while (h->next) {
700 h = h->next;
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200701 char * buf = translate_name(h->name, TO_UPPER);
702 if (buf) {
703 fprintf(autogen, "\tselect %s\n", buf);
704 free(buf);
705 }
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200706 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000707 }
Sven Schnelle270a9082011-03-01 19:58:15 +0000708
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200709 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000710
Patrick Georgi114e7b22010-05-05 11:19:50 +0000711 return 0;
712}