Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 1 | /* |
| 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 Georgi | 2dbfcb7 | 2012-05-30 16:26:30 +0200 | [diff] [blame] | 21 | #include <ctype.h> |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 22 | #include "sconfig.h" |
| 23 | #include "sconfig.tab.h" |
| 24 | |
Patrick Georgi | 7fc9e29 | 2010-07-15 15:59:07 +0000 | [diff] [blame] | 25 | extern int linenum; |
| 26 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 27 | struct device *head, *lastdev; |
| 28 | |
| 29 | struct header headers; |
| 30 | |
| 31 | static int devcount = 0; |
| 32 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 33 | typedef enum { |
| 34 | STATIC_MODE, |
| 35 | BOOTBLOCK_MODE, |
| 36 | KCONFIG_MODE |
| 37 | } scan_t; |
| 38 | |
| 39 | static scan_t scan_mode = STATIC_MODE; |
| 40 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 41 | static struct device root; |
| 42 | static struct device mainboard = { |
| 43 | .name = "mainboard", |
| 44 | .name_underscore = "mainboard", |
| 45 | .id = 0, |
| 46 | .chip = &mainboard, |
| 47 | .type = chip, |
Stefan Reinauer | 188e3c2 | 2012-07-26 12:46:48 -0700 | [diff] [blame^] | 48 | #ifdef MAINBOARDS_HAVE_CHIP_H |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 49 | .chiph_exists = 1, |
Stefan Reinauer | 188e3c2 | 2012-07-26 12:46:48 -0700 | [diff] [blame^] | 50 | #else |
| 51 | .chiph_exists = 0, |
| 52 | #endif |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 53 | .children = &root |
| 54 | }; |
| 55 | |
| 56 | static 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 Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 69 | static struct device *new_dev(struct device *parent, struct device *bus) { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 70 | struct device *dev = malloc(sizeof(struct device)); |
| 71 | memset(dev, 0, sizeof(struct device)); |
| 72 | dev->id = ++devcount; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 73 | dev->parent = parent; |
| 74 | dev->bus = bus; |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 75 | dev->subsystem_vendor = -1; |
| 76 | dev->subsystem_device = -1; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 77 | head->next = dev; |
| 78 | head = dev; |
| 79 | return dev; |
| 80 | } |
| 81 | |
| 82 | static 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 | |
| 88 | void 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 | |
| 101 | int yywrap(void) { |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | void yyerror (char const *str) |
| 106 | { |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 107 | extern char *yytext; |
| 108 | fprintf (stderr, "line %d: %s: %s\n", linenum + 1, yytext, str); |
| 109 | exit(1); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void 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älkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 132 | void 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 Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 143 | struct device *new_chip(struct device *parent, struct device *bus, char *path) { |
| 144 | struct device *new_chip = new_dev(parent, bus); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 145 | new_chip->chiph_exists = 1; |
| 146 | new_chip->name = path; |
| 147 | new_chip->name_underscore = strdup(new_chip->name); |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 148 | translate_name(new_chip->name_underscore, 0); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 149 | new_chip->type = chip; |
| 150 | new_chip->chip = new_chip; |
| 151 | |
| 152 | struct stat st; |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 153 | char *chip_h = malloc(strlen(path)+18); |
Stefan Reinauer | 76c44ae | 2011-10-14 12:41:46 -0700 | [diff] [blame] | 154 | 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älkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 161 | 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 Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 170 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 171 | if (parent->latestchild) { |
| 172 | parent->latestchild->next_sibling = new_chip; |
| 173 | parent->latestchild->sibling = new_chip; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 174 | } |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 175 | parent->latestchild = new_chip; |
| 176 | if (!parent->children) |
| 177 | parent->children = new_chip; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 178 | return new_chip; |
| 179 | } |
| 180 | |
| 181 | void add_header(struct device *dev) { |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 182 | if ((dev->chiph_exists) || (scan_mode == KCONFIG_MODE)){ |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 183 | int include_exists = 0; |
| 184 | struct header *h = &headers; |
| 185 | while (h->next) { |
| 186 | int result = strcmp(dev->name, h->next->name); |
| 187 | if (result == 0) { |
| 188 | include_exists = 1; |
| 189 | break; |
| 190 | } |
| 191 | if (result < 0) break; |
| 192 | h = h->next; |
| 193 | } |
| 194 | if (!include_exists) { |
| 195 | struct header *tmp = h->next; |
| 196 | h->next = malloc(sizeof(struct header)); |
| 197 | memset(h->next, 0, sizeof(struct header)); |
| 198 | h->next->name = dev->name; |
| 199 | h->next->next = tmp; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 204 | struct device *new_device(struct device *parent, struct device *busdev, const int bus, const char *devnum, int enabled) { |
| 205 | struct device *new_d = new_dev(parent, busdev); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 206 | new_d->bustype = bus; |
| 207 | |
| 208 | char *tmp; |
| 209 | new_d->path_a = strtol(strdup(devnum), &tmp, 16); |
| 210 | if (*tmp == '.') { |
| 211 | tmp++; |
| 212 | new_d->path_b = strtol(tmp, NULL, 16); |
| 213 | } |
| 214 | |
| 215 | char *name = malloc(10); |
| 216 | sprintf(name, "_dev%d", new_d->id); |
| 217 | new_d->name = name; |
| 218 | new_d->name_underscore = name; // shouldn't be necessary, but avoid 0-ptr |
| 219 | new_d->type = device; |
| 220 | new_d->enabled = enabled; |
| 221 | new_d->chip = new_d->parent->chip; |
| 222 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 223 | if (parent->latestchild) { |
| 224 | parent->latestchild->next_sibling = new_d; |
| 225 | parent->latestchild->sibling = new_d; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 226 | } |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 227 | parent->latestchild = new_d; |
| 228 | if (!parent->children) |
| 229 | parent->children = new_d; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 230 | |
| 231 | lastdev->nextdev = new_d; |
| 232 | lastdev = new_d; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 233 | |
| 234 | switch(bus) { |
| 235 | case PCI: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 236 | new_d->path = ".type=DEVICE_PATH_PCI,{.pci={ .devfn = PCI_DEVFN(0x%x,%d)}}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 237 | break; |
| 238 | |
| 239 | case PNP: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 240 | new_d->path = ".type=DEVICE_PATH_PNP,{.pnp={ .port = 0x%x, .device = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 241 | break; |
| 242 | |
| 243 | case I2C: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 244 | new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 245 | break; |
| 246 | |
| 247 | case APIC: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 248 | new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 249 | break; |
| 250 | |
| 251 | case APIC_CLUSTER: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 252 | new_d->path = ".type=DEVICE_PATH_APIC_CLUSTER,{.apic_cluster={ .cluster = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 253 | break; |
| 254 | |
| 255 | case PCI_DOMAIN: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 256 | new_d->path = ".type=DEVICE_PATH_PCI_DOMAIN,{.pci_domain={ .domain = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 257 | break; |
| 258 | |
| 259 | case IOAPIC: |
| 260 | new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}"; |
| 261 | break; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 262 | } |
| 263 | return new_d; |
| 264 | } |
| 265 | |
| 266 | void alias_siblings(struct device *d) { |
| 267 | while (d) { |
| 268 | int link = 0; |
| 269 | struct device *cmp = d->next_sibling; |
| 270 | while (cmp && (cmp->bus == d->bus) && (cmp->path_a == d->path_a) && (cmp->path_b == d->path_b)) { |
| 271 | if (cmp->type==device && !cmp->used) { |
| 272 | if (device_match(d, cmp)) { |
| 273 | d->multidev = 1; |
| 274 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 275 | cmp->id = d->id; |
| 276 | cmp->name = d->name; |
| 277 | cmp->used = 1; |
| 278 | cmp->link = ++link; |
| 279 | } |
| 280 | } |
| 281 | cmp = cmp->next_sibling; |
| 282 | } |
| 283 | d = d->next_sibling; |
| 284 | } |
| 285 | } |
| 286 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 287 | void add_resource(struct device *dev, int type, int index, int base) { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 288 | struct resource *r = malloc(sizeof(struct resource)); |
| 289 | memset (r, 0, sizeof(struct resource)); |
| 290 | r->type = type; |
| 291 | r->index = index; |
| 292 | r->base = base; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 293 | if (dev->res) { |
| 294 | struct resource *head = dev->res; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 295 | while (head->next) head = head->next; |
| 296 | head->next = r; |
| 297 | } else { |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 298 | dev->res = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 299 | } |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 300 | dev->rescnt++; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 303 | void add_register(struct device *dev, char *name, char *val) { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 304 | struct reg *r = malloc(sizeof(struct reg)); |
| 305 | memset (r, 0, sizeof(struct reg)); |
| 306 | r->key = name; |
| 307 | r->value = val; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 308 | if (dev->reg) { |
| 309 | struct reg *head = dev->reg; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 310 | // sorting to be equal to sconfig's behaviour |
| 311 | int sort = strcmp(r->key, head->key); |
| 312 | if (sort == 0) { |
| 313 | printf("ERROR: duplicate 'register' key.\n"); |
| 314 | exit(1); |
| 315 | } |
| 316 | if (sort<0) { |
| 317 | r->next = head; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 318 | dev->reg = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 319 | } else { |
| 320 | while ((head->next) && (strcmp(head->next->key, r->key)<0)) head = head->next; |
| 321 | r->next = head->next; |
| 322 | head->next = r; |
| 323 | } |
| 324 | } else { |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 325 | dev->reg = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 329 | void add_pci_subsystem_ids(struct device *dev, int vendor, int device, int inherit) |
| 330 | { |
| 331 | if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) { |
| 332 | printf("ERROR: 'subsystem' only allowed for PCI devices\n"); |
| 333 | exit(1); |
| 334 | } |
| 335 | |
| 336 | dev->subsystem_vendor = vendor; |
| 337 | dev->subsystem_device = device; |
| 338 | dev->inherit_subsystem = inherit; |
| 339 | } |
| 340 | |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 341 | void add_ioapic_info(struct device *dev, int apicid, const char *_srcpin, int irqpin) |
| 342 | { |
| 343 | |
| 344 | int srcpin; |
| 345 | |
| 346 | if (!_srcpin || strlen(_srcpin) < 4 ||strncasecmp(_srcpin, "INT", 3) || |
| 347 | _srcpin[3] < 'A' || _srcpin[3] > 'D') { |
| 348 | printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin); |
| 349 | exit(1); |
| 350 | } |
| 351 | |
| 352 | srcpin = _srcpin[3] - 'A'; |
| 353 | |
| 354 | if (dev->bustype != PCI && dev->bustype != PCI_DOMAIN) { |
| 355 | printf("ERROR: ioapic config only allowed for PCI devices\n"); |
| 356 | exit(1); |
| 357 | } |
| 358 | |
| 359 | if (srcpin > 3) { |
Patrick Georgi | 116327e | 2012-07-20 12:47:06 +0200 | [diff] [blame] | 360 | printf("ERROR: srcpin '%d' invalid\n", srcpin); |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 361 | exit(1); |
| 362 | } |
| 363 | dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin; |
| 364 | dev->pci_irq_info[srcpin].ioapic_dst_id = apicid; |
| 365 | } |
| 366 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 367 | static void pass0(FILE *fil, struct device *ptr) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 368 | if (ptr->type == device && ptr->id == 0) |
| 369 | fprintf(fil, "struct bus %s_links[];\n", ptr->name); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 370 | if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) { |
Stefan Reinauer | df61dd2 | 2010-08-09 12:02:00 +0000 | [diff] [blame] | 371 | fprintf(fil, "static struct device %s;\n", ptr->name); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 372 | if (ptr->rescnt > 0) |
| 373 | fprintf(fil, "struct resource %s_res[];\n", ptr->name); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 374 | if (ptr->children || ptr->multidev) |
| 375 | fprintf(fil, "struct bus %s_links[];\n", ptr->name); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 376 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 379 | static void pass1(FILE *fil, struct device *ptr) |
| 380 | { |
| 381 | int pin; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 382 | if (!ptr->used && (ptr->type == device)) { |
Stefan Reinauer | df61dd2 | 2010-08-09 12:02:00 +0000 | [diff] [blame] | 383 | if (ptr->id != 0) |
Patrick Georgi | 2dbfcb7 | 2012-05-30 16:26:30 +0200 | [diff] [blame] | 384 | fprintf(fil, "static "); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 385 | fprintf(fil, "struct device %s = {\n", ptr->name); |
| 386 | fprintf(fil, "\t.ops = %s,\n", (ptr->ops)?(ptr->ops):"0"); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 387 | fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, ptr->bus->link); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 388 | fprintf(fil, "\t.path = {"); |
| 389 | fprintf(fil, ptr->path, ptr->path_a, ptr->path_b); |
| 390 | fprintf(fil, "},\n"); |
| 391 | fprintf(fil, "\t.enabled = %d,\n", ptr->enabled); |
| 392 | fprintf(fil, "\t.on_mainboard = 1,\n"); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 393 | if (ptr->subsystem_vendor > 0) |
| 394 | fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", ptr->subsystem_vendor); |
| 395 | |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 396 | for(pin = 0; pin < 4; pin++) { |
| 397 | if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0) |
| 398 | fprintf(fil, "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_irq_pin); |
| 399 | |
| 400 | if (ptr->pci_irq_info[pin].ioapic_dst_id > 0) |
| 401 | fprintf(fil, "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n", pin, ptr->pci_irq_info[pin].ioapic_dst_id); |
| 402 | } |
| 403 | |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 404 | if (ptr->subsystem_device > 0) |
| 405 | fprintf(fil, "\t.subsystem_device = 0x%04x,\n", ptr->subsystem_device); |
| 406 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 407 | if (ptr->rescnt > 0) { |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 408 | fprintf(fil, "\t.resource_list = &%s_res[0],\n", ptr->name); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 409 | } |
| 410 | int link = 0; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 411 | if (ptr->children || ptr->multidev) |
| 412 | fprintf(fil, "\t.link_list = &%s_links[0],\n", ptr->name); |
| 413 | else |
Patrick Georgi | 2dbfcb7 | 2012-05-30 16:26:30 +0200 | [diff] [blame] | 414 | fprintf(fil, "\t.link_list = NULL,\n"); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 415 | if (ptr->sibling) |
| 416 | fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name); |
| 417 | if (ptr->chip->chiph_exists) { |
| 418 | fprintf(fil, "\t.chip_ops = &%s_ops,\n", ptr->chip->name_underscore); |
| 419 | fprintf(fil, "\t.chip_info = &%s_info_%d,\n", ptr->chip->name_underscore, ptr->chip->id); |
| 420 | } |
| 421 | if (ptr->nextdev) |
| 422 | fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name); |
| 423 | fprintf(fil, "};\n"); |
| 424 | } |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 425 | if (ptr->rescnt > 0) { |
| 426 | int i=1; |
| 427 | fprintf(fil, "struct resource %s_res[] = {\n", ptr->name); |
| 428 | struct resource *r = ptr->res; |
| 429 | while (r) { |
| 430 | fprintf(fil, "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_"); |
| 431 | if (r->type == IRQ) fprintf(fil, "IRQ"); |
| 432 | if (r->type == DRQ) fprintf(fil, "DRQ"); |
| 433 | if (r->type == IO) fprintf(fil, "IO"); |
| 434 | fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, r->base); |
| 435 | if (r->next) |
| 436 | fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, i++); |
| 437 | else |
| 438 | fprintf(fil, ".next=NULL },\n"); |
| 439 | r = r->next; |
| 440 | } |
| 441 | fprintf(fil, "\t };\n"); |
| 442 | } |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 443 | if (!ptr->used && ptr->type == device && (ptr->children || ptr->multidev)) { |
| 444 | fprintf(fil, "struct bus %s_links[] = {\n", ptr->name); |
| 445 | if (ptr->multidev) { |
| 446 | struct device *d = ptr; |
| 447 | while (d) { |
| 448 | if (device_match(d, ptr)) { |
| 449 | fprintf(fil, "\t\t[%d] = {\n", d->link); |
| 450 | fprintf(fil, "\t\t\t.link_num = %d,\n", d->link); |
| 451 | fprintf(fil, "\t\t\t.dev = &%s,\n", d->name); |
| 452 | if (d->children) |
| 453 | fprintf(fil, "\t\t\t.children = &%s,\n", d->children->name); |
Myles Watson | 1965a23 | 2010-06-10 04:06:52 +0000 | [diff] [blame] | 454 | if (d->next_sibling && device_match(d->next_sibling, ptr)) |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 455 | fprintf(fil, "\t\t\t.next=&%s_links[%d],\n", d->name, d->link+1); |
| 456 | else |
| 457 | fprintf(fil, "\t\t\t.next = NULL,\n"); |
| 458 | fprintf(fil, "\t\t},\n"); |
| 459 | } |
| 460 | d = d->next_sibling; |
| 461 | } |
| 462 | } else { |
| 463 | if (ptr->children) { |
| 464 | fprintf(fil, "\t\t[0] = {\n"); |
| 465 | fprintf(fil, "\t\t\t.link_num = 0,\n"); |
| 466 | fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name); |
| 467 | fprintf(fil, "\t\t\t.children = &%s,\n", ptr->children->name); |
| 468 | fprintf(fil, "\t\t\t.next = NULL,\n"); |
| 469 | fprintf(fil, "\t\t},\n"); |
| 470 | } |
| 471 | } |
| 472 | fprintf(fil, "\t};\n"); |
| 473 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 474 | if ((ptr->type == chip) && (ptr->chiph_exists)) { |
| 475 | if (ptr->reg) { |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 476 | fprintf(fil, "struct %s_config %s_info_%d\t= {\n", |
| 477 | ptr->name_underscore, ptr->name_underscore, ptr->id); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 478 | struct reg *r = ptr->reg; |
| 479 | while (r) { |
| 480 | fprintf(fil, "\t.%s = %s,\n", r->key, r->value); |
| 481 | r = r->next; |
| 482 | } |
| 483 | fprintf(fil, "};\n\n"); |
| 484 | } else { |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 485 | fprintf(fil, "struct %s_config %s_info_%d;\n", |
| 486 | ptr->name_underscore, ptr->name_underscore, ptr->id); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | static void walk_device_tree(FILE *fil, struct device *ptr, void (*func)(FILE *, struct device*), struct device *chips) { |
| 492 | do { |
| 493 | func(fil, ptr); |
| 494 | ptr = ptr->next_sibling; |
| 495 | } while (ptr); |
| 496 | } |
| 497 | |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 498 | static void inherit_subsystem_ids(FILE *file, struct device *dev) |
| 499 | { |
| 500 | struct device *p; |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 501 | |
| 502 | if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) { |
| 503 | /* user already gave us a subsystem vendor/device */ |
| 504 | return; |
| 505 | } |
| 506 | |
Sylvain "ythier" Hitier | 5325a48 | 2011-03-01 21:57:11 +0000 | [diff] [blame] | 507 | for(p = dev; p && p != p->parent; p = p->parent) { |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 508 | |
| 509 | if (p->bustype != PCI && p->bustype != PCI_DOMAIN) |
| 510 | continue; |
| 511 | |
| 512 | if (p->inherit_subsystem) { |
| 513 | dev->subsystem_vendor = p->subsystem_vendor; |
| 514 | dev->subsystem_device = p->subsystem_device; |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 520 | static void usage(void) |
| 521 | { |
| 522 | printf("usage: sconfig vendor/mainboard outputdir [-{s|b|k} outputfile]\n"); |
| 523 | printf("\t-s file\tcreate ramstage static device map\n"); |
| 524 | printf("\t-b file\tcreate bootblock init_mainboard()\n"); |
| 525 | printf("\t-k file\tcreate Kconfig devicetree section\n"); |
| 526 | printf("Defaults to \"-s static.c\" if no {s|b|k} specified.\n"); |
| 527 | exit (1); |
| 528 | } |
| 529 | |
| 530 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 531 | int main(int argc, char** argv) { |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 532 | if (argc < 3) |
| 533 | usage(); |
| 534 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 535 | char *mainboard=argv[1]; |
| 536 | char *outputdir=argv[2]; |
| 537 | char *devtree=malloc(strlen(mainboard)+30); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 538 | sprintf(devtree, "src/mainboard/%s/devicetree.cb", mainboard); |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 539 | char *outputc; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 540 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 541 | if (argc == 3) { |
| 542 | scan_mode = STATIC_MODE; |
| 543 | outputc=malloc(strlen(outputdir)+20); |
| 544 | sprintf(outputc, "%s/static.c", outputdir); |
| 545 | } else if ((argc == 5) && (argv[3][0] == '-') && (argv[3][2] == 0)) { |
| 546 | |
| 547 | switch (argv[3][1]) { |
| 548 | case 's': |
| 549 | scan_mode = STATIC_MODE; |
| 550 | break; |
| 551 | case 'b': |
| 552 | scan_mode = BOOTBLOCK_MODE; |
| 553 | break; |
| 554 | case 'k': |
| 555 | scan_mode = KCONFIG_MODE; |
| 556 | break; |
| 557 | default: |
| 558 | usage(); |
| 559 | break; |
| 560 | } |
| 561 | char *outputfile=argv[4]; |
| 562 | |
| 563 | outputc=malloc(strlen(outputdir)+strlen(outputfile)+2); |
| 564 | sprintf(outputc, "%s/%s", outputdir, outputfile); |
| 565 | } |
| 566 | |
| 567 | headers.next = 0; |
Stefan Reinauer | 188e3c2 | 2012-07-26 12:46:48 -0700 | [diff] [blame^] | 568 | #ifdef MAINBOARDS_HAVE_CHIP_H |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 569 | if (scan_mode == STATIC_MODE) { |
| 570 | headers.next = malloc(sizeof(struct header)); |
| 571 | headers.next->name = malloc(strlen(mainboard)+12); |
| 572 | headers.next->next = 0; |
| 573 | sprintf(headers.next->name, "mainboard/%s", mainboard); |
| 574 | } |
Stefan Reinauer | 188e3c2 | 2012-07-26 12:46:48 -0700 | [diff] [blame^] | 575 | #endif |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 576 | |
| 577 | FILE *filec = fopen(devtree, "r"); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 578 | if (!filec) { |
| 579 | fprintf(stderr, "Could not open file '%s' for reading: ", devtree); |
| 580 | perror(NULL); |
| 581 | exit(1); |
| 582 | } |
| 583 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 584 | yyrestart(filec); |
| 585 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 586 | lastdev = head = &root; |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 587 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 588 | yyparse(); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 589 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 590 | fclose(filec); |
| 591 | |
| 592 | if ((head->type == chip) && (!head->chiph_exists)) { |
| 593 | struct device *tmp = head; |
| 594 | head = &root; |
| 595 | while (head->next != tmp) head = head->next; |
| 596 | } |
| 597 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 598 | FILE *autogen = fopen(outputc, "w"); |
| 599 | if (!autogen) { |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 600 | fprintf(stderr, "Could not open file '%s' for writing: ", outputc); |
| 601 | perror(NULL); |
| 602 | exit(1); |
| 603 | } |
| 604 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 605 | struct header *h; |
| 606 | if (scan_mode == STATIC_MODE) { |
| 607 | |
| 608 | fprintf(autogen, "#include <device/device.h>\n"); |
| 609 | fprintf(autogen, "#include <device/pci.h>\n"); |
| 610 | h = &headers; |
| 611 | while (h->next) { |
| 612 | h = h->next; |
| 613 | fprintf(autogen, "#include \"%s/chip.h\"\n", h->name); |
| 614 | } |
| 615 | |
| 616 | walk_device_tree(autogen, &root, inherit_subsystem_ids, NULL); |
| 617 | fprintf(autogen, "\n/* pass 0 */\n"); |
| 618 | walk_device_tree(autogen, &root, pass0, NULL); |
Stefan Reinauer | 188e3c2 | 2012-07-26 12:46:48 -0700 | [diff] [blame^] | 619 | fprintf(autogen, "\n/* pass 1 */\n" |
| 620 | "struct device *last_dev = &%s;\n", lastdev->name); |
| 621 | #ifdef MAINBOARDS_HAVE_CHIP_H |
| 622 | fprintf(autogen, "struct mainboard_config mainboard_info_0;\n"); |
| 623 | #endif |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 624 | walk_device_tree(autogen, &root, pass1, NULL); |
| 625 | |
| 626 | } else if (scan_mode == BOOTBLOCK_MODE) { |
| 627 | h = &headers; |
| 628 | while (h->next) { |
| 629 | h = h->next; |
| 630 | fprintf(autogen, "#include \"%s/bootblock.c\"\n", h->name); |
| 631 | } |
| 632 | |
| 633 | fprintf(autogen, "\n#if CONFIG_HAS_MAINBOARD_BOOTBLOCK\n"); |
| 634 | fprintf(autogen, "#include \"mainboard/%s/bootblock.c\"\n", mainboard); |
| 635 | fprintf(autogen, "#else\n"); |
| 636 | fprintf(autogen, "static unsigned long init_mainboard(int bsp_cpu)\n{\n"); |
| 637 | fprintf(autogen, "\tif (! bsp_cpu) return 0;\n"); |
| 638 | h = &headers; |
| 639 | while (h->next) { |
| 640 | h = h->next; |
| 641 | translate_name(h->name, 0); |
| 642 | fprintf(autogen, "\tinit_%s();\n", h->name); |
| 643 | } |
| 644 | |
| 645 | fprintf(autogen, "\treturn 0;\n}\n"); |
| 646 | fprintf(autogen, "#endif\n"); |
| 647 | |
| 648 | } else if (scan_mode == KCONFIG_MODE) { |
| 649 | fprintf(autogen, "\nconfig MAINBOARD_DIR\n\tstring\n"); |
| 650 | fprintf(autogen, "\tdefault %s\n", mainboard); |
| 651 | |
| 652 | fprintf(autogen, "\nconfig MAINBOARD_DEVTREE\n\tdef_bool y\n"); |
| 653 | h = &headers; |
| 654 | while (h->next) { |
| 655 | h = h->next; |
| 656 | translate_name(h->name, 1); |
| 657 | fprintf(autogen, "\tselect %s\n", h->name); |
| 658 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 659 | } |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 660 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 661 | fclose(autogen); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 662 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 663 | return 0; |
| 664 | } |