blob: 74193e674033d1b1c6b77ceba2416e82fd38e297 [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
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +020041typedef enum {
42 UNSLASH,
43 SPLIT_1ST,
44 TO_LOWER,
45 TO_UPPER,
46} translate_t;
47
Patrick Georgi114e7b22010-05-05 11:19:50 +000048static struct device root;
49static struct device mainboard = {
50 .name = "mainboard",
51 .name_underscore = "mainboard",
52 .id = 0,
53 .chip = &mainboard,
54 .type = chip,
Stefan Reinauer188e3c22012-07-26 12:46:48 -070055#ifdef MAINBOARDS_HAVE_CHIP_H
Patrick Georgi114e7b22010-05-05 11:19:50 +000056 .chiph_exists = 1,
Stefan Reinauer188e3c22012-07-26 12:46:48 -070057#else
58 .chiph_exists = 0,
59#endif
Patrick Georgi114e7b22010-05-05 11:19:50 +000060 .children = &root
61};
62
63static struct device root = {
64 .name = "dev_root",
65 .name_underscore = "dev_root",
66 .id = 0,
67 .chip = &mainboard,
68 .type = device,
69 .path = " .type = DEVICE_PATH_ROOT ",
70 .ops = "&default_dev_ops_root",
71 .parent = &root,
72 .bus = &root,
73 .enabled = 1
74};
75
Patrick Georgi68befd52010-05-05 12:05:25 +000076static struct device *new_dev(struct device *parent, struct device *bus) {
Patrick Georgi114e7b22010-05-05 11:19:50 +000077 struct device *dev = malloc(sizeof(struct device));
78 memset(dev, 0, sizeof(struct device));
79 dev->id = ++devcount;
Patrick Georgi68befd52010-05-05 12:05:25 +000080 dev->parent = parent;
81 dev->bus = bus;
Sven Schnelle270a9082011-03-01 19:58:15 +000082 dev->subsystem_vendor = -1;
83 dev->subsystem_device = -1;
Patrick Georgi114e7b22010-05-05 11:19:50 +000084 head->next = dev;
85 head = dev;
86 return dev;
87}
88
89static int device_match(struct device *a, struct device *b) {
90 if ((a->bustype == b->bustype) && (a->bus == b->bus) && (a->path_a == b->path_a) && (a->path_b == b->path_b))
91 return 1;
92 return 0;
93}
94
95void fold_in(struct device *parent) {
96 struct device *child = parent->children;
97 struct device *latest = 0;
98 while (child != latest) {
99 if (child->children) {
100 if (!latest) latest = child->children;
101 parent->latestchild->next_sibling = child->children;
102 parent->latestchild = child->latestchild;
103 }
104 child = child->next_sibling;
105 }
106}
107
108int yywrap(void) {
109 return 1;
110}
111
112void yyerror (char const *str)
113{
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000114 extern char *yytext;
115 fprintf (stderr, "line %d: %s: %s\n", linenum + 1, yytext, str);
116 exit(1);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000117}
118
119void postprocess_devtree(void) {
120 root.next_sibling = root.children;
121 root.next_sibling->next_sibling = root.next_sibling->children;
122
123 struct device *dev = &root;
124 while (dev) {
125 /* skip "chip" elements in children chain */
126 while (dev->children && (dev->children->type == chip)) dev->children = dev->children->children;
127 /* skip "chip" elements and functions of the same device in sibling chain */
128 while (dev->sibling && dev->sibling->used) dev->sibling = dev->sibling->sibling;
129 /* If end of chain, and parent is a chip, move on */
130 if (!dev->sibling && (dev->parent->type == chip)) dev->sibling = dev->parent->sibling;
131 /* skip chips */
132 while (dev->sibling && dev->sibling->type == chip) dev->sibling = dev->sibling->children;
133 /* skip duplicate function elements in nextdev chain */
134 while (dev->nextdev && dev->nextdev->used) dev->nextdev = dev->nextdev->nextdev;
135 dev = dev->next_sibling;
136 }
137}
138
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200139char * translate_name(const char *str, translate_t mode)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200140{
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200141 char *b, *c;
142 b = c = strdup(str);
143 while (c && *c) {
144 if ((mode == SPLIT_1ST) && (*c == '/')) {
145 *c = 0;
146 break;
147 }
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200148 if (*c == '/') *c = '_';
149 if (*c == '-') *c = '_';
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200150 if (mode == TO_UPPER)
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200151 *c = toupper(*c);
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200152 if (mode == TO_LOWER)
153 *c = tolower(*c);
154 c++;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200155 }
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200156 return b;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200157}
158
Patrick Georgi68befd52010-05-05 12:05:25 +0000159struct device *new_chip(struct device *parent, struct device *bus, char *path) {
160 struct device *new_chip = new_dev(parent, bus);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000161 new_chip->chiph_exists = 1;
162 new_chip->name = path;
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200163 new_chip->name_underscore = translate_name(new_chip->name, UNSLASH);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000164 new_chip->type = chip;
165 new_chip->chip = new_chip;
166
167 struct stat st;
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200168 char *chip_h = malloc(strlen(path)+18);
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700169 sprintf(chip_h, "src/%s", path);
170 if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) {
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300171 if (strstr(path, "/root_complex")) {
172 fprintf(stderr, "WARNING: Use of deprecated chip component %s\n",
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700173 path);
Kyösti Mälkki6aeb4a22013-06-11 17:00:11 +0300174 } else {
175 fprintf(stderr, "ERROR: Chip component %s does not exist.\n",
176 path);
177 exit(1);
178 }
Stefan Reinauer76c44ae2011-10-14 12:41:46 -0700179 }
180
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200181 if (scan_mode == STATIC_MODE)
182 sprintf(chip_h, "src/%s/chip.h", path);
183 else if (scan_mode == BOOTBLOCK_MODE)
184 sprintf(chip_h, "src/%s/bootblock.c", path);
185
186 if ((scan_mode == STATIC_MODE) || (scan_mode == BOOTBLOCK_MODE)) {
187 if ((stat(chip_h, &st) == -1) && (errno == ENOENT))
188 new_chip->chiph_exists = 0;
189 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000190
Patrick Georgi68befd52010-05-05 12:05:25 +0000191 if (parent->latestchild) {
192 parent->latestchild->next_sibling = new_chip;
193 parent->latestchild->sibling = new_chip;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000194 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000195 parent->latestchild = new_chip;
196 if (!parent->children)
197 parent->children = new_chip;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000198 return new_chip;
199}
200
201void add_header(struct device *dev) {
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200202 int include_exists = 0;
203 struct header *h = &headers;
204 while (h->next) {
205 int result = strcmp(dev->name, h->next->name);
206 if (result == 0) {
207 include_exists = 1;
208 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000209 }
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200210 if (result < 0) break;
211 h = h->next;
212 }
213 if (!include_exists) {
214 struct header *tmp = h->next;
215 h->next = malloc(sizeof(struct header));
216 memset(h->next, 0, sizeof(struct header));
217 h->next->chiph_exists = dev->chiph_exists;
218 h->next->name = dev->name;
219 h->next->next = tmp;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000220 }
221}
222
Patrick Georgi68befd52010-05-05 12:05:25 +0000223struct device *new_device(struct device *parent, struct device *busdev, const int bus, const char *devnum, int enabled) {
224 struct device *new_d = new_dev(parent, busdev);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000225 new_d->bustype = bus;
226
227 char *tmp;
228 new_d->path_a = strtol(strdup(devnum), &tmp, 16);
229 if (*tmp == '.') {
230 tmp++;
231 new_d->path_b = strtol(tmp, NULL, 16);
232 }
233
234 char *name = malloc(10);
235 sprintf(name, "_dev%d", new_d->id);
236 new_d->name = name;
237 new_d->name_underscore = name; // shouldn't be necessary, but avoid 0-ptr
238 new_d->type = device;
239 new_d->enabled = enabled;
240 new_d->chip = new_d->parent->chip;
241
Patrick Georgi68befd52010-05-05 12:05:25 +0000242 if (parent->latestchild) {
243 parent->latestchild->next_sibling = new_d;
244 parent->latestchild->sibling = new_d;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000245 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000246 parent->latestchild = new_d;
247 if (!parent->children)
248 parent->children = new_d;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000249
250 lastdev->nextdev = new_d;
251 lastdev = new_d;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200252
253 switch(bus) {
254 case PCI:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000255 new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200256 break;
257
258 case PNP:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000259 new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200260 break;
261
262 case I2C:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000263 new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200264 break;
265
266 case APIC:
Patrick Georgi114e7b22010-05-05 11:19:50 +0000267 new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200268 break;
269
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800270 case CPU_CLUSTER:
271 new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200272 break;
273
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800274 case DOMAIN:
275 new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}";
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200276 break;
277
278 case IOAPIC:
279 new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}";
280 break;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000281 }
282 return new_d;
283}
284
285void alias_siblings(struct device *d) {
286 while (d) {
287 int link = 0;
288 struct device *cmp = d->next_sibling;
289 while (cmp && (cmp->bus == d->bus) && (cmp->path_a == d->path_a) && (cmp->path_b == d->path_b)) {
290 if (cmp->type==device && !cmp->used) {
291 if (device_match(d, cmp)) {
292 d->multidev = 1;
293
Patrick Georgi114e7b22010-05-05 11:19:50 +0000294 cmp->id = d->id;
295 cmp->name = d->name;
296 cmp->used = 1;
297 cmp->link = ++link;
298 }
299 }
300 cmp = cmp->next_sibling;
301 }
302 d = d->next_sibling;
303 }
304}
305
Patrick Georgi68befd52010-05-05 12:05:25 +0000306void add_resource(struct device *dev, int type, int index, int base) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000307 struct resource *r = malloc(sizeof(struct resource));
308 memset (r, 0, sizeof(struct resource));
309 r->type = type;
310 r->index = index;
311 r->base = base;
Patrick Georgi68befd52010-05-05 12:05:25 +0000312 if (dev->res) {
313 struct resource *head = dev->res;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000314 while (head->next) head = head->next;
315 head->next = r;
316 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000317 dev->res = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000318 }
Patrick Georgi68befd52010-05-05 12:05:25 +0000319 dev->rescnt++;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000320}
321
Patrick Georgi68befd52010-05-05 12:05:25 +0000322void add_register(struct device *dev, char *name, char *val) {
Patrick Georgi114e7b22010-05-05 11:19:50 +0000323 struct reg *r = malloc(sizeof(struct reg));
324 memset (r, 0, sizeof(struct reg));
325 r->key = name;
326 r->value = val;
Patrick Georgi68befd52010-05-05 12:05:25 +0000327 if (dev->reg) {
328 struct reg *head = dev->reg;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000329 // sorting to be equal to sconfig's behaviour
330 int sort = strcmp(r->key, head->key);
331 if (sort == 0) {
332 printf("ERROR: duplicate 'register' key.\n");
333 exit(1);
334 }
335 if (sort<0) {
336 r->next = head;
Patrick Georgi68befd52010-05-05 12:05:25 +0000337 dev->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000338 } else {
339 while ((head->next) && (strcmp(head->next->key, r->key)<0)) head = head->next;
340 r->next = head->next;
341 head->next = r;
342 }
343 } else {
Patrick Georgi68befd52010-05-05 12:05:25 +0000344 dev->reg = r;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000345 }
346}
347
Sven Schnelle270a9082011-03-01 19:58:15 +0000348void add_pci_subsystem_ids(struct device *dev, int vendor, int device, int inherit)
349{
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800350 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle270a9082011-03-01 19:58:15 +0000351 printf("ERROR: 'subsystem' only allowed for PCI devices\n");
352 exit(1);
353 }
354
355 dev->subsystem_vendor = vendor;
356 dev->subsystem_device = device;
357 dev->inherit_subsystem = inherit;
358}
359
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200360void add_ioapic_info(struct device *dev, int apicid, const char *_srcpin, int irqpin)
361{
362
363 int srcpin;
364
365 if (!_srcpin || strlen(_srcpin) < 4 ||strncasecmp(_srcpin, "INT", 3) ||
366 _srcpin[3] < 'A' || _srcpin[3] > 'D') {
367 printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin);
368 exit(1);
369 }
370
371 srcpin = _srcpin[3] - 'A';
372
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800373 if (dev->bustype != PCI && dev->bustype != DOMAIN) {
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200374 printf("ERROR: ioapic config only allowed for PCI devices\n");
375 exit(1);
376 }
377
378 if (srcpin > 3) {
Patrick Georgi116327e2012-07-20 12:47:06 +0200379 printf("ERROR: srcpin '%d' invalid\n", srcpin);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200380 exit(1);
381 }
382 dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin;
383 dev->pci_irq_info[srcpin].ioapic_dst_id = apicid;
384}
385
Patrick Georgi114e7b22010-05-05 11:19:50 +0000386static void pass0(FILE *fil, struct device *ptr) {
Myles Watson894a3472010-06-09 22:41:35 +0000387 if (ptr->type == device && ptr->id == 0)
Stefan Reinauer57879c92012-07-31 16:47:25 -0700388 fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[];\n", ptr->name);
389
Myles Watsonc25cc112010-05-21 14:33:48 +0000390 if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) {
Stefan Reinauer57879c92012-07-31 16:47:25 -0700391 fprintf(fil, "ROMSTAGE_CONST static struct device %s;\n", ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000392 if (ptr->rescnt > 0)
Stefan Reinauer57879c92012-07-31 16:47:25 -0700393 fprintf(fil, "ROMSTAGE_CONST struct resource %s_res[];\n", ptr->name);
Myles Watson894a3472010-06-09 22:41:35 +0000394 if (ptr->children || ptr->multidev)
Stefan Reinauer57879c92012-07-31 16:47:25 -0700395 fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[];\n",
396 ptr->name);
Myles Watsonc25cc112010-05-21 14:33:48 +0000397 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000398}
399
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200400static void pass1(FILE *fil, struct device *ptr)
401{
402 int pin;
Patrick Georgi114e7b22010-05-05 11:19:50 +0000403 if (!ptr->used && (ptr->type == device)) {
Stefan Reinauerdf61dd22010-08-09 12:02:00 +0000404 if (ptr->id != 0)
Patrick Georgi2dbfcb72012-05-30 16:26:30 +0200405 fprintf(fil, "static ");
Stefan Reinauer57879c92012-07-31 16:47:25 -0700406 fprintf(fil, "ROMSTAGE_CONST struct device %s = {\n", ptr->name);
407 fprintf(fil, "#ifndef __PRE_RAM__\n");
Patrick Georgi114e7b22010-05-05 11:19:50 +0000408 fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0");
Stefan Reinauer57879c92012-07-31 16:47:25 -0700409 fprintf(fil, "#endif\n");
Myles Watson894a3472010-06-09 22:41:35 +0000410 fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000411 fprintf(fil, "\t.path = {");
412 fprintf(fil, ptr->path, ptr->path_a, ptr->path_b);
413 fprintf(fil, "},\n");
414 fprintf(fil, "\t.enabled = %d,\n", ptr->enabled);
415 fprintf(fil, "\t.on_mainboard = 1,\n");
Sven Schnelle270a9082011-03-01 19:58:15 +0000416 if (ptr->subsystem_vendor > 0)
417 fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", ptr->subsystem_vendor);
418
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200419 for(pin = 0; pin < 4; pin++) {
420 if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0)
421 fprintf(fil, "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_irq_pin);
422
423 if (ptr->pci_irq_info[pin].ioapic_dst_id > 0)
424 fprintf(fil, "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_dst_id);
425 }
426
Sven Schnelle270a9082011-03-01 19:58:15 +0000427 if (ptr->subsystem_device > 0)
428 fprintf(fil, "\t.subsystem_device = 0x%04x,\n", ptr->subsystem_device);
429
Patrick Georgi114e7b22010-05-05 11:19:50 +0000430 if (ptr->rescnt > 0) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000431 fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name);
Patrick Georgi114e7b22010-05-05 11:19:50 +0000432 }
433 int link = 0;
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) {
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200560 if (argc < 3)
561 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);
573 } else if ((argc == 5) && (argv[3][0] == '-') && (argv[3][2] == 0)) {
574
575 switch (argv[3][1]) {
576 case 's':
577 scan_mode = STATIC_MODE;
578 break;
579 case 'b':
580 scan_mode = BOOTBLOCK_MODE;
581 break;
582 case 'k':
583 scan_mode = KCONFIG_MODE;
584 break;
585 default:
586 usage();
587 break;
588 }
589 char *outputfile=argv[4];
590
591 outputc=malloc(strlen(outputdir)+strlen(outputfile)+2);
592 sprintf(outputc, "%s/%s", outputdir, outputfile);
593 }
594
595 headers.next = 0;
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700596#ifdef MAINBOARDS_HAVE_CHIP_H
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200597 if (scan_mode == STATIC_MODE) {
598 headers.next = malloc(sizeof(struct header));
599 headers.next->name = malloc(strlen(mainboard)+12);
600 headers.next->next = 0;
601 sprintf(headers.next->name, "mainboard/%s", mainboard);
602 }
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700603#endif
Patrick Georgi114e7b22010-05-05 11:19:50 +0000604
605 FILE *filec = fopen(devtree, "r");
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000606 if (!filec) {
607 fprintf(stderr, "Could not open file '%s' for reading: ", devtree);
608 perror(NULL);
609 exit(1);
610 }
611
Patrick Georgi114e7b22010-05-05 11:19:50 +0000612 yyrestart(filec);
613
Patrick Georgi68befd52010-05-05 12:05:25 +0000614 lastdev = head = &root;
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000615
Patrick Georgi114e7b22010-05-05 11:19:50 +0000616 yyparse();
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000617
Patrick Georgi114e7b22010-05-05 11:19:50 +0000618 fclose(filec);
619
620 if ((head->type == chip) && (!head->chiph_exists)) {
621 struct device *tmp = head;
622 head = &root;
623 while (head->next != tmp) head = head->next;
624 }
625
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200626 FILE *autogen = fopen(outputc, "w");
627 if (!autogen) {
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000628 fprintf(stderr, "Could not open file '%s' for writing: ", outputc);
629 perror(NULL);
630 exit(1);
631 }
632
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200633 struct header *h;
634 if (scan_mode == STATIC_MODE) {
635
636 fprintf(autogen, "#include <device/device.h>\n");
637 fprintf(autogen, "#include <device/pci.h>\n");
638 h = &headers;
639 while (h->next) {
640 h = h->next;
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200641 if (h->chiph_exists)
642 fprintf(autogen, "#include \"%s/chip.h\"\n", h->name);
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200643 }
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200644 fprintf(autogen, "\n#ifndef __PRE_RAM__\n");
Kyösti Mälkkie773c922012-11-15 07:05:43 +0200645 fprintf(autogen, "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n");
Kyösti Mälkkifee73df2012-08-21 11:37:11 +0300646 h = &headers;
647 while (h->next) {
648 h = h->next;
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200649 char *name_underscore = translate_name(h->name, UNSLASH);
Patrick Georgib6f765e2012-10-07 22:04:52 +0200650 fprintf(autogen, "__attribute__((weak)) struct chip_operations %s_ops = {};\n", name_underscore);
Kyösti Mälkkifee73df2012-08-21 11:37:11 +0300651 free(name_underscore);
652 }
Kyösti Mälkkiaada2e12012-10-07 15:08:32 +0200653 fprintf(autogen, "#endif\n");
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200654
655 walk_device_tree(autogen, &root, inherit_subsystem_ids, NULL);
656 fprintf(autogen, "\n/* pass 0 */\n");
657 walk_device_tree(autogen, &root, pass0, NULL);
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700658 fprintf(autogen, "\n/* pass 1 */\n"
Stefan Reinauer57879c92012-07-31 16:47:25 -0700659 "ROMSTAGE_CONST struct device * ROMSTAGE_CONST last_dev = &%s;\n", lastdev->name);
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700660#ifdef MAINBOARDS_HAVE_CHIP_H
Stefan Reinauer57879c92012-07-31 16:47:25 -0700661 fprintf(autogen, "static ROMSTAGE_CONST struct mainboard_config ROMSTAGE_CONST mainboard_info_0;\n");
Stefan Reinauer188e3c22012-07-26 12:46:48 -0700662#endif
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200663 walk_device_tree(autogen, &root, pass1, NULL);
664
665 } else if (scan_mode == BOOTBLOCK_MODE) {
666 h = &headers;
667 while (h->next) {
668 h = h->next;
669 fprintf(autogen, "#include \"%s/bootblock.c\"\n", h->name);
670 }
671
672 fprintf(autogen, "\n#if CONFIG_HAS_MAINBOARD_BOOTBLOCK\n");
673 fprintf(autogen, "#include \"mainboard/%s/bootblock.c\"\n", mainboard);
674 fprintf(autogen, "#else\n");
675 fprintf(autogen, "static unsigned long init_mainboard(int bsp_cpu)\n{\n");
676 fprintf(autogen, "\tif (! bsp_cpu) return 0;\n");
677 h = &headers;
678 while (h->next) {
679 h = h->next;
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200680 char * buf = translate_name(h->name, UNSLASH);
681 if (buf) {
682 fprintf(autogen, "\tinit_%s();\n", buf);
683 free(buf);
684 }
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200685 }
686
687 fprintf(autogen, "\treturn 0;\n}\n");
688 fprintf(autogen, "#endif\n");
689
690 } else if (scan_mode == KCONFIG_MODE) {
691 fprintf(autogen, "\nconfig MAINBOARD_DIR\n\tstring\n");
692 fprintf(autogen, "\tdefault %s\n", mainboard);
693
694 fprintf(autogen, "\nconfig MAINBOARD_DEVTREE\n\tdef_bool y\n");
695 h = &headers;
696 while (h->next) {
697 h = h->next;
Kyösti Mälkki1a2a6ee2012-03-18 20:19:28 +0200698 char * buf = translate_name(h->name, TO_UPPER);
699 if (buf) {
700 fprintf(autogen, "\tselect %s\n", buf);
701 free(buf);
702 }
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200703 }
Patrick Georgi114e7b22010-05-05 11:19:50 +0000704 }
Sven Schnelle270a9082011-03-01 19:58:15 +0000705
Kyösti Mälkki472d9022011-12-05 20:33:55 +0200706 fclose(autogen);
Stefan Reinauer7c1f6b82010-08-16 18:21:56 +0000707
Patrick Georgi114e7b22010-05-05 11:19:50 +0000708 return 0;
709}