3 * sconfig, coreboot device tree compiler
5 * Copyright (C) 2010 coresystems GmbH
6 * written by Patrick Georgi <patrick.georgi@coresystems.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
25 #include <sys/types.h>
30 enum devtype { chip, device };
37 struct resource *next;
59 char *name_underscore;
65 struct device *parent;
68 struct device *nextdev;
69 struct device *children;
70 struct device *latestchild;
71 struct device *next_sibling;
72 struct device *sibling;
76 } *head, *lastdev, *cur_parent, *cur_bus, root;
86 struct device *new_dev() {
87 struct device *dev = malloc(sizeof(struct device));
88 memset(dev, 0, sizeof(struct device));
90 dev->parent = cur_parent;
97 int device_match(struct device *a, struct device *b) {
98 if ((a->bustype == b->bustype) && (a->bus == b->bus) && (a->path_a == b->path_a) && (a->path_b == b->path_b))
103 void fold_in(struct device *parent) {
104 struct device *child = parent->children;
105 struct device *latest = 0;
106 while (child != latest) {
107 if (child->children) {
108 if (!latest) latest = child->children;
109 parent->latestchild->next_sibling = child->children;
110 parent->latestchild = child->latestchild;
112 child = child->next_sibling;
120 void yyerror (char const *str)
122 fprintf (stderr, "%s\n", str);
126 struct device *device;
130 %token CHIP DEVICE REGISTER BOOL BUS RESOURCE END EQUALS HEX STRING PCI PNP I2C APIC APIC_CLUSTER PCI_DOMAIN IRQ DRQ IO NUMBER
133 root.next_sibling = root.children;
134 root.next_sibling->next_sibling = root.next_sibling->children;
136 struct device *dev = &root;
138 /* skip "chip" elements in children chain */
139 while (dev->children && (dev->children->type == chip)) dev->children = dev->children->children;
140 /* skip "chip" elements and functions of the same device in sibling chain */
141 while (dev->sibling && dev->sibling->used) dev->sibling = dev->sibling->sibling;
142 /* If end of chain, and parent is a chip, move on */
143 if (!dev->sibling && (dev->parent->type == chip)) dev->sibling = dev->parent->sibling;
145 while (dev->sibling && dev->sibling->type == chip) dev->sibling = dev->sibling->children;
146 /* skip duplicate function elements in nextdev chain */
147 while (dev->nextdev && dev->nextdev->used) dev->nextdev = dev->nextdev->nextdev;
148 dev = dev->next_sibling;
152 devchip: chip | device ;
154 devices: devices devchip | devices registers | ;
156 devicesorresources: devicesorresources devchip | devicesorresources resource | ;
158 chip: CHIP STRING /* == path */ {
159 $<device>$ = new_dev();
160 $<device>$->chiph_exists = 1;
161 $<device>$->name = $<string>2;
162 $<device>$->name_underscore = strdup($<device>$->name);
164 for (c = $<device>$->name_underscore; *c; c++) {
165 if (*c == '/') *c = '_';
166 if (*c == '-') *c = '_';
168 $<device>$->type = chip;
169 $<device>$->chip = $<device>$;
172 char *chip_h = malloc(strlen($<string>2)+12);
173 sprintf(chip_h, "src/%s/chip.h", $<string>2);
174 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
175 $<device>$->chiph_exists = 0;
177 if (cur_parent->latestchild) {
178 cur_parent->latestchild->next_sibling = $<device>$;
179 cur_parent->latestchild->sibling = $<device>$;
181 cur_parent->latestchild = $<device>$;
182 if (!cur_parent->children)
183 cur_parent->children = $<device>$;
185 cur_parent = $<device>$;
188 cur_parent = $<device>3->parent;
192 if ($<device>3->chiph_exists) {
193 int include_exists = 0;
194 struct header *h = &headers;
196 int result = strcmp($<device>3->name, h->next->name);
201 if (result < 0) break;
204 if (!include_exists) {
205 struct header *tmp = h->next;
206 h->next = malloc(sizeof(struct header));
207 memset(h->next, 0, sizeof(struct header));
208 h->next->name = $<device>3->name;
215 device: DEVICE BUS NUMBER /* == devnum */ BOOL {
216 $<device>$ = new_dev();
217 $<device>$->bustype = $<number>2;
220 $<device>$->path_a = strtol(strdup($<string>3), &tmp, 16);
223 $<device>$->path_b = strtol(tmp, NULL, 16);
226 char *name = malloc(10);
227 sprintf(name, "_dev%d", $<device>$->id);
228 $<device>$->name = name;
229 $<device>$->name_underscore = name; // shouldn't be necessary, but avoid 0-ptr
230 $<device>$->type = device;
231 $<device>$->enabled = $<number>4;
232 $<device>$->chip = $<device>$->parent->chip;
234 if (cur_parent->latestchild) {
235 cur_parent->latestchild->next_sibling = $<device>$;
236 cur_parent->latestchild->sibling = $<device>$;
238 cur_parent->latestchild = $<device>$;
239 if (!cur_parent->children)
240 cur_parent->children = $<device>$;
242 lastdev->nextdev = $<device>$;
243 lastdev = $<device>$;
244 if ($<number>2 == PCI) {
245 $<device>$->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
247 if ($<number>2 == PNP) {
248 $<device>$->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
250 if ($<number>2 == I2C) {
251 $<device>$->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x }}";
253 if ($<number>2 == APIC) {
254 $<device>$->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
256 if ($<number>2 == APIC_CLUSTER) {
257 $<device>$->path = ".type=DEVICE_PATH_APIC_CLUSTER,{.apic_cluster={ .cluster = 0x%x }}";
259 if ($<number>2 == PCI_DOMAIN) {
260 $<device>$->path = ".type=DEVICE_PATH_PCI_DOMAIN,{.pci_domain={ .domain = 0x%x }}";
262 cur_parent = $<device>$;
263 cur_bus = $<device>$;
265 devicesorresources END {
266 cur_parent = $<device>5->parent;
267 cur_bus = $<device>5->bus;
271 struct device *d = $<device>5->children;
274 struct device *cmp = d->next_sibling;
275 while (cmp && (cmp->bus == d->bus) && (cmp->path_a == d->path_a) && (cmp->path_b == d->path_b)) {
276 if (cmp->type==device && !cmp->used) {
277 if (device_match(d, cmp)) {
280 cmp->aliased_name = malloc(12);
281 sprintf(cmp->aliased_name, "_dev%d", cmp->id);
288 cmp = cmp->next_sibling;
294 resource: RESOURCE NUMBER /* == resnum */ EQUALS NUMBER /* == resval */
296 struct resource *r = malloc(sizeof(struct resource));
297 memset (r, 0, sizeof(struct resource));
298 r->type = $<number>1;
299 r->index = strtol($<string>2, NULL, 0);
300 r->base = strtol($<string>4, NULL, 0);
301 if (cur_parent->res) {
302 struct resource *head = cur_parent->res;
303 while (head->next) head = head->next;
308 cur_parent->rescnt++;
312 registers: REGISTER STRING /* == regname */ EQUALS STRING /* == regval */
314 struct reg *r = malloc(sizeof(struct reg));
315 memset (r, 0, sizeof(struct reg));
317 r->value = $<string>4;
318 if (cur_parent->reg) {
319 struct reg *head = cur_parent->reg;
320 // sorting to be equal to sconfig's behaviour
321 int sort = strcmp(r->key, head->key);
323 printf("ERROR: duplicate 'register' key.\n");
330 while ((head->next) && (strcmp(head->next->key, r->key)<0)) head = head->next;
331 r->next = head->next;
341 void pass0(FILE *fil, struct device *ptr) {
342 if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used))
343 fprintf(fil, "struct device %s;\n", ptr->name);
344 if ((ptr->type == device) && (ptr->id != 0) && ptr->used)
345 fprintf(fil, "struct device %s;\n", ptr->aliased_name);
348 void pass1(FILE *fil, struct device *ptr) {
349 if (!ptr->used && (ptr->type == device)) {
350 fprintf(fil, "struct device %s = {\n", ptr->name);
351 fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0");
352 fprintf(fil, "\t.bus = &%s.link[%d],\n", ptr->bus->name, ptr->bus->link);
353 fprintf(fil, "\t.path = {");
354 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
355 fprintf(fil, "},\n");
356 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
357 fprintf(fil, "\t.on_mainboard = 1,\n");
358 if (ptr->rescnt > 0) {
359 fprintf(fil, "\t.resources = %d,\n", ptr->rescnt);
360 fprintf(fil, "\t.resource = {\n");
361 struct resource *r = ptr->res;
363 fprintf(fil, "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_");
364 if (r->type == IRQ) fprintf(fil, "IRQ");
365 if (r->type == DRQ) fprintf(fil, "DRQ");
366 if (r->type == IO) fprintf(fil, "IO");
367 fprintf(fil, ", .index=0x%x, .base=0x%x},\n", r->index, r->base);
370 fprintf(fil, "\t },\n");
373 fprintf(fil, "\t.link = {\n");
375 struct device *d = ptr;
377 if (device_match(d, ptr)) {
378 fprintf(fil, "\t\t[%d] = {\n", d->link);
379 fprintf(fil, "\t\t\t.link = %d,\n", d->link);
380 fprintf(fil, "\t\t\t.dev = &%s,\n", d->name);
382 fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name);
383 fprintf(fil, "\t\t},\n");
390 fprintf(fil, "\t\t[0] = {\n");
391 fprintf(fil, "\t\t\t.link = 0,\n");
392 fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name);
393 fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name);
394 fprintf(fil, "\t\t},\n");
398 fprintf(fil, "\t},\n");
399 fprintf(fil, "\t.links = %d,\n", link);
401 fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name);
402 if (ptr->chip->chiph_exists) {
403 fprintf(fil, "\t.chip_ops = &%s_ops,\n", ptr->chip->name_underscore);
404 fprintf(fil, "\t.chip_info = &%s_info_%d,\n", ptr->chip->name_underscore, ptr->chip->id);
407 fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name);
408 fprintf(fil, "};\n");
410 if ((ptr->type == chip) && (ptr->chiph_exists)) {
412 fprintf(fil, "struct %s_config %s_info_%d\t= {\n", ptr->name_underscore, ptr->name_underscore, ptr->id);
413 struct reg *r = ptr->reg;
415 fprintf(fil, "\t.%s = %s,\n", r->key, r->value);
418 fprintf(fil, "};\n\n");
420 fprintf(fil, "struct %s_config %s_info_%d;\n", ptr->name_underscore, ptr->name_underscore, ptr->id);
425 void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *, struct device*), struct device *chips) {
428 ptr = ptr->next_sibling;
432 struct device mainboard = {
434 .name_underscore = "mainboard",
442 struct device root = {
444 .name_underscore = "dev_root",
448 .path = " .type = DEVICE_PATH_ROOT ",
449 .ops = "&default_dev_ops_root",
455 int main(int argc, char** argv) {
457 printf("usage: sconfig vendor/mainboard outputdir\n");
460 char *mainboard=argv[1];
461 char *outputdir=argv[2];
462 char *devtree=malloc(strlen(mainboard)+30);
463 char *outputc=malloc(strlen(outputdir)+10);
464 sprintf(devtree, "src/mainboard/%s/devicetree.cb", mainboard);
465 sprintf(outputc, "%s/static.c", outputdir);
467 headers.next = malloc(sizeof(struct header));
468 headers.next->name = malloc(strlen(mainboard)+12);
469 headers.next->next = 0;
470 sprintf(headers.next->name, "mainboard/%s", mainboard);
472 FILE *filec = fopen(devtree, "r");
475 FILE *staticc = fopen(outputc, "w");
477 cur_bus = cur_parent = lastdev = head = &root;
481 if ((head->type == chip) && (!head->chiph_exists)) {
482 struct device *tmp = head;
484 while (head->next != tmp) head = head->next;
487 fprintf(staticc, "#include <device/device.h>\n");
488 fprintf(staticc, "#include <device/pci.h>\n");
489 struct header *h = &headers;
492 fprintf(staticc, "#include \"%s/chip.h\"\n", h->name);
494 fprintf(staticc, "\n/* pass 0 */\n");
495 walk_device_tree(staticc, &root, pass0, NULL);
496 fprintf(staticc, "\n/* pass 1 */\nstruct mainboard_config mainboard_info_0;\nstruct device **last_dev_p = &%s.next;\n", lastdev->name);
497 walk_device_tree(staticc, &root, pass1, NULL);