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 |
Stefan Reinauer | 2e78aa5 | 2016-05-07 01:11:14 -0700 | [diff] [blame] | 5 | * written by Patrick Georgi <patrick@georgi-clan.de> |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 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 Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Patrick Georgi | 2dbfcb7 | 2012-05-30 16:26:30 +0200 | [diff] [blame] | 17 | #include <ctype.h> |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 18 | #include "sconfig.h" |
| 19 | #include "sconfig.tab.h" |
| 20 | |
Patrick Georgi | 7fc9e29 | 2010-07-15 15:59:07 +0000 | [diff] [blame] | 21 | extern int linenum; |
| 22 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 23 | struct device *head, *lastdev; |
| 24 | |
| 25 | struct header headers; |
| 26 | |
| 27 | static int devcount = 0; |
| 28 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 29 | typedef enum { |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 30 | UNSLASH, |
| 31 | SPLIT_1ST, |
| 32 | TO_LOWER, |
| 33 | TO_UPPER, |
| 34 | } translate_t; |
| 35 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 36 | static struct device root; |
| 37 | static struct device mainboard = { |
| 38 | .name = "mainboard", |
| 39 | .name_underscore = "mainboard", |
| 40 | .id = 0, |
| 41 | .chip = &mainboard, |
| 42 | .type = chip, |
Stefan Reinauer | 188e3c2 | 2012-07-26 12:46:48 -0700 | [diff] [blame] | 43 | .chiph_exists = 0, |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 44 | .children = &root |
| 45 | }; |
| 46 | |
| 47 | static struct device root = { |
| 48 | .name = "dev_root", |
| 49 | .name_underscore = "dev_root", |
| 50 | .id = 0, |
| 51 | .chip = &mainboard, |
| 52 | .type = device, |
| 53 | .path = " .type = DEVICE_PATH_ROOT ", |
| 54 | .ops = "&default_dev_ops_root", |
| 55 | .parent = &root, |
| 56 | .bus = &root, |
| 57 | .enabled = 1 |
| 58 | }; |
| 59 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 60 | static struct device *new_dev(struct device *parent, struct device *bus) |
| 61 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 62 | struct device *dev = malloc(sizeof(struct device)); |
| 63 | memset(dev, 0, sizeof(struct device)); |
| 64 | dev->id = ++devcount; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 65 | dev->parent = parent; |
| 66 | dev->bus = bus; |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 67 | dev->subsystem_vendor = -1; |
| 68 | dev->subsystem_device = -1; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 69 | head->next = dev; |
| 70 | head = dev; |
| 71 | return dev; |
| 72 | } |
| 73 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 74 | static int device_match(struct device *a, struct device *b) |
| 75 | { |
| 76 | if ((a->bustype == b->bustype) && (a->bus == b->bus) |
| 77 | && (a->path_a == b->path_a) && (a->path_b == b->path_b)) |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 78 | return 1; |
| 79 | return 0; |
| 80 | } |
| 81 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 82 | void fold_in(struct device *parent) |
| 83 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 84 | struct device *child = parent->children; |
| 85 | struct device *latest = 0; |
| 86 | while (child != latest) { |
| 87 | if (child->children) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 88 | if (!latest) |
| 89 | latest = child->children; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 90 | parent->latestchild->next_sibling = child->children; |
| 91 | parent->latestchild = child->latestchild; |
| 92 | } |
| 93 | child = child->next_sibling; |
| 94 | } |
| 95 | } |
| 96 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 97 | int yywrap(void) |
| 98 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 99 | return 1; |
| 100 | } |
| 101 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 102 | void yyerror(char const *str) |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 103 | { |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 104 | extern char *yytext; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 105 | fprintf(stderr, "line %d: %s: %s\n", linenum + 1, yytext, str); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 106 | exit(1); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 109 | void postprocess_devtree(void) |
| 110 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 111 | root.next_sibling = root.children; |
| 112 | root.next_sibling->next_sibling = root.next_sibling->children; |
| 113 | |
| 114 | struct device *dev = &root; |
| 115 | while (dev) { |
| 116 | /* skip "chip" elements in children chain */ |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 117 | while (dev->children && (dev->children->type == chip)) |
| 118 | dev->children = dev->children->children; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 119 | /* skip "chip" elements and functions of the same device in sibling chain */ |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 120 | while (dev->sibling && dev->sibling->used) |
| 121 | dev->sibling = dev->sibling->sibling; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 122 | /* If end of chain, and parent is a chip, move on */ |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 123 | if (!dev->sibling && (dev->parent->type == chip)) |
| 124 | dev->sibling = dev->parent->sibling; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 125 | /* skip chips */ |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 126 | while (dev->sibling && dev->sibling->type == chip) |
| 127 | dev->sibling = dev->sibling->children; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 128 | /* skip duplicate function elements in nextdev chain */ |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 129 | while (dev->nextdev && dev->nextdev->used) |
| 130 | dev->nextdev = dev->nextdev->nextdev; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 131 | dev = dev->next_sibling; |
| 132 | } |
| 133 | } |
| 134 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 135 | char *translate_name(const char *str, translate_t mode) |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 136 | { |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 137 | char *b, *c; |
| 138 | b = c = strdup(str); |
| 139 | while (c && *c) { |
| 140 | if ((mode == SPLIT_1ST) && (*c == '/')) { |
| 141 | *c = 0; |
| 142 | break; |
| 143 | } |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 144 | if (*c == '/') |
| 145 | *c = '_'; |
| 146 | if (*c == '-') |
| 147 | *c = '_'; |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 148 | if (mode == TO_UPPER) |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 149 | *c = toupper(*c); |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 150 | if (mode == TO_LOWER) |
| 151 | *c = tolower(*c); |
| 152 | c++; |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 153 | } |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 154 | return b; |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 157 | struct device *new_chip(struct device *parent, struct device *bus, char *path) |
| 158 | { |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 159 | struct device *new_chip = new_dev(parent, bus); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 160 | new_chip->chiph_exists = 1; |
| 161 | new_chip->name = path; |
Kyösti Mälkki | 1a2a6ee | 2012-03-18 20:19:28 +0200 | [diff] [blame] | 162 | new_chip->name_underscore = translate_name(new_chip->name, UNSLASH); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 163 | new_chip->type = chip; |
| 164 | new_chip->chip = new_chip; |
| 165 | |
| 166 | struct stat st; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 167 | char *chip_h = malloc(strlen(path) + 18); |
Stefan Reinauer | 76c44ae | 2011-10-14 12:41:46 -0700 | [diff] [blame] | 168 | sprintf(chip_h, "src/%s", path); |
| 169 | if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) { |
Patrick Georgi | 92bcaa2 | 2015-11-13 09:39:22 +0100 | [diff] [blame] | 170 | /* root_complex gets away without a separate directory, but |
| 171 | * exists on on pretty much all AMD chipsets. |
| 172 | */ |
| 173 | if (!strstr(path, "/root_complex")) { |
Kyösti Mälkki | 6aeb4a2 | 2013-06-11 17:00:11 +0300 | [diff] [blame] | 174 | fprintf(stderr, "ERROR: Chip component %s does not exist.\n", |
| 175 | path); |
| 176 | exit(1); |
| 177 | } |
Stefan Reinauer | 76c44ae | 2011-10-14 12:41:46 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 180 | sprintf(chip_h, "src/%s/chip.h", path); |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 181 | |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 182 | if ((stat(chip_h, &st) == -1) && (errno == ENOENT)) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 183 | new_chip->chiph_exists = 0; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 184 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 185 | if (parent->latestchild) { |
| 186 | parent->latestchild->next_sibling = new_chip; |
| 187 | parent->latestchild->sibling = new_chip; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 188 | } |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 189 | parent->latestchild = new_chip; |
| 190 | if (!parent->children) |
| 191 | parent->children = new_chip; |
Patrick Georgi | 1f68880 | 2014-08-03 15:51:19 +0200 | [diff] [blame] | 192 | free(chip_h); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 193 | return new_chip; |
| 194 | } |
| 195 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 196 | void add_header(struct device *dev) |
| 197 | { |
Kyösti Mälkki | aada2e1 | 2012-10-07 15:08:32 +0200 | [diff] [blame] | 198 | int include_exists = 0; |
| 199 | struct header *h = &headers; |
| 200 | while (h->next) { |
| 201 | int result = strcmp(dev->name, h->next->name); |
| 202 | if (result == 0) { |
| 203 | include_exists = 1; |
| 204 | break; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 205 | } |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 206 | if (result < 0) |
| 207 | break; |
Kyösti Mälkki | aada2e1 | 2012-10-07 15:08:32 +0200 | [diff] [blame] | 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 Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 220 | struct device *new_device(struct device *parent, struct device *busdev, |
| 221 | const int bus, const char *devnum, int enabled) |
| 222 | { |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 223 | struct device *new_d = new_dev(parent, busdev); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 224 | new_d->bustype = bus; |
| 225 | |
| 226 | char *tmp; |
Patrick Georgi | 1f68880 | 2014-08-03 15:51:19 +0200 | [diff] [blame] | 227 | new_d->path_a = strtol(devnum, &tmp, 16); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 228 | if (*tmp == '.') { |
| 229 | tmp++; |
| 230 | new_d->path_b = strtol(tmp, NULL, 16); |
| 231 | } |
| 232 | |
| 233 | char *name = malloc(10); |
| 234 | sprintf(name, "_dev%d", new_d->id); |
| 235 | new_d->name = name; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 236 | new_d->name_underscore = name; // shouldn't be necessary, but avoid 0-ptr |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 237 | new_d->type = device; |
| 238 | new_d->enabled = enabled; |
| 239 | new_d->chip = new_d->parent->chip; |
| 240 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 241 | if (parent->latestchild) { |
| 242 | parent->latestchild->next_sibling = new_d; |
| 243 | parent->latestchild->sibling = new_d; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 244 | } |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 245 | parent->latestchild = new_d; |
| 246 | if (!parent->children) |
| 247 | parent->children = new_d; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 248 | |
| 249 | lastdev->nextdev = new_d; |
| 250 | lastdev = new_d; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 251 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 252 | switch (bus) { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 253 | case PCI: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 254 | 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] | 255 | break; |
| 256 | |
| 257 | case PNP: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 258 | 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] | 259 | break; |
| 260 | |
| 261 | case I2C: |
Duncan Laurie | b7ce5fe | 2016-05-07 19:49:37 -0700 | [diff] [blame] | 262 | new_d->path = ".type=DEVICE_PATH_I2C,{.i2c={ .device = 0x%x, .mode_10bit = %d }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 263 | break; |
| 264 | |
| 265 | case APIC: |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 266 | new_d->path = ".type=DEVICE_PATH_APIC,{.apic={ .apic_id = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 267 | break; |
| 268 | |
Stefan Reinauer | 0aa37c4 | 2013-02-12 15:20:54 -0800 | [diff] [blame] | 269 | case CPU_CLUSTER: |
| 270 | new_d->path = ".type=DEVICE_PATH_CPU_CLUSTER,{.cpu_cluster={ .cluster = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 271 | break; |
| 272 | |
Aaron Durbin | ffda804b | 2014-09-03 12:40:15 -0500 | [diff] [blame] | 273 | case CPU: |
| 274 | new_d->path = ".type=DEVICE_PATH_CPU,{.cpu={ .id = 0x%x }}"; |
| 275 | break; |
| 276 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 277 | case DOMAIN: |
| 278 | new_d->path = ".type=DEVICE_PATH_DOMAIN,{.domain={ .domain = 0x%x }}"; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 279 | break; |
| 280 | |
| 281 | case IOAPIC: |
| 282 | new_d->path = ".type=DEVICE_PATH_IOAPIC,{.ioapic={ .ioapic_id = 0x%x }}"; |
| 283 | break; |
Duncan Laurie | 4650f5b | 2016-05-07 20:01:34 -0700 | [diff] [blame] | 284 | |
| 285 | case GENERIC: |
| 286 | new_d->path = ".type=DEVICE_PATH_GENERIC,{.generic={ .id = 0x%x, .subid = 0x%x }}"; |
| 287 | break; |
Furquan Shaikh | e670029 | 2017-02-11 00:50:38 -0800 | [diff] [blame] | 288 | |
| 289 | case SPI: |
| 290 | new_d->path = ".type=DEVICE_PATH_SPI,{.spi={ .cs = 0x%x }}"; |
| 291 | break; |
| 292 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 293 | } |
| 294 | return new_d; |
| 295 | } |
| 296 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 297 | void alias_siblings(struct device *d) |
| 298 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 299 | while (d) { |
| 300 | int link = 0; |
| 301 | struct device *cmp = d->next_sibling; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 302 | while (cmp && (cmp->bus == d->bus) && (cmp->path_a == d->path_a) |
| 303 | && (cmp->path_b == d->path_b)) { |
| 304 | if (cmp->type == device && !cmp->used) { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 305 | if (device_match(d, cmp)) { |
| 306 | d->multidev = 1; |
| 307 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 308 | cmp->id = d->id; |
| 309 | cmp->name = d->name; |
| 310 | cmp->used = 1; |
| 311 | cmp->link = ++link; |
| 312 | } |
| 313 | } |
| 314 | cmp = cmp->next_sibling; |
| 315 | } |
| 316 | d = d->next_sibling; |
| 317 | } |
| 318 | } |
| 319 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 320 | void add_resource(struct device *dev, int type, int index, int base) |
| 321 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 322 | struct resource *r = malloc(sizeof(struct resource)); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 323 | memset(r, 0, sizeof(struct resource)); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 324 | r->type = type; |
| 325 | r->index = index; |
| 326 | r->base = base; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 327 | if (dev->res) { |
| 328 | struct resource *head = dev->res; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 329 | while (head->next) |
| 330 | head = head->next; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 331 | head->next = r; |
| 332 | } else { |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 333 | dev->res = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 334 | } |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 335 | dev->rescnt++; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 338 | void add_register(struct device *dev, char *name, char *val) |
| 339 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 340 | struct reg *r = malloc(sizeof(struct reg)); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 341 | memset(r, 0, sizeof(struct reg)); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 342 | r->key = name; |
| 343 | r->value = val; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 344 | if (dev->reg) { |
| 345 | struct reg *head = dev->reg; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 346 | // sorting to be equal to sconfig's behaviour |
| 347 | int sort = strcmp(r->key, head->key); |
| 348 | if (sort == 0) { |
| 349 | printf("ERROR: duplicate 'register' key.\n"); |
| 350 | exit(1); |
| 351 | } |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 352 | if (sort < 0) { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 353 | r->next = head; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 354 | dev->reg = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 355 | } else { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 356 | while ((head->next) |
| 357 | && (strcmp(head->next->key, r->key) < 0)) |
| 358 | head = head->next; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 359 | r->next = head->next; |
| 360 | head->next = r; |
| 361 | } |
| 362 | } else { |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 363 | dev->reg = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 367 | void add_pci_subsystem_ids(struct device *dev, int vendor, int device, |
| 368 | int inherit) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 369 | { |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 370 | if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 371 | printf("ERROR: 'subsystem' only allowed for PCI devices\n"); |
| 372 | exit(1); |
| 373 | } |
| 374 | |
| 375 | dev->subsystem_vendor = vendor; |
| 376 | dev->subsystem_device = device; |
| 377 | dev->inherit_subsystem = inherit; |
| 378 | } |
| 379 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 380 | void add_ioapic_info(struct device *dev, int apicid, const char *_srcpin, |
| 381 | int irqpin) |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 382 | { |
| 383 | |
| 384 | int srcpin; |
| 385 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 386 | if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) || |
| 387 | _srcpin[3] < 'A' || _srcpin[3] > 'D') { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 388 | printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin); |
| 389 | exit(1); |
| 390 | } |
| 391 | |
| 392 | srcpin = _srcpin[3] - 'A'; |
| 393 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 394 | if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 395 | printf("ERROR: ioapic config only allowed for PCI devices\n"); |
| 396 | exit(1); |
| 397 | } |
| 398 | |
| 399 | if (srcpin > 3) { |
Patrick Georgi | 116327e | 2012-07-20 12:47:06 +0200 | [diff] [blame] | 400 | printf("ERROR: srcpin '%d' invalid\n", srcpin); |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 401 | exit(1); |
| 402 | } |
| 403 | dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin; |
| 404 | dev->pci_irq_info[srcpin].ioapic_dst_id = apicid; |
| 405 | } |
| 406 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 407 | static void pass0(FILE * fil, struct device *ptr) |
| 408 | { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 409 | if (ptr->type == device && ptr->id == 0) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 410 | fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[];\n", |
| 411 | ptr->name); |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 412 | |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 413 | if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 414 | fprintf(fil, "ROMSTAGE_CONST static struct device %s;\n", |
| 415 | ptr->name); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 416 | if (ptr->rescnt > 0) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 417 | fprintf(fil, |
| 418 | "ROMSTAGE_CONST struct resource %s_res[];\n", |
| 419 | ptr->name); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 420 | if (ptr->children || ptr->multidev) |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 421 | fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[];\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 422 | ptr->name); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 423 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 426 | static void pass1(FILE * fil, struct device *ptr) |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 427 | { |
| 428 | int pin; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 429 | if (!ptr->used && (ptr->type == device)) { |
Stefan Reinauer | df61dd2 | 2010-08-09 12:02:00 +0000 | [diff] [blame] | 430 | if (ptr->id != 0) |
Patrick Georgi | 2dbfcb7 | 2012-05-30 16:26:30 +0200 | [diff] [blame] | 431 | fprintf(fil, "static "); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 432 | fprintf(fil, "ROMSTAGE_CONST struct device %s = {\n", |
| 433 | ptr->name); |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 434 | fprintf(fil, "#ifndef __PRE_RAM__\n"); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 435 | fprintf(fil, "\t.ops = %s,\n", (ptr->ops) ? (ptr->ops) : "0"); |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 436 | fprintf(fil, "#endif\n"); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 437 | fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, |
| 438 | ptr->bus->link); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 439 | fprintf(fil, "\t.path = {"); |
| 440 | fprintf(fil, ptr->path, ptr->path_a, ptr->path_b); |
| 441 | fprintf(fil, "},\n"); |
| 442 | fprintf(fil, "\t.enabled = %d,\n", ptr->enabled); |
| 443 | fprintf(fil, "\t.on_mainboard = 1,\n"); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 444 | if (ptr->subsystem_vendor > 0) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 445 | fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", |
| 446 | ptr->subsystem_vendor); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 447 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 448 | for (pin = 0; pin < 4; pin++) { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 449 | if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 450 | fprintf(fil, "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n", |
| 451 | pin, ptr->pci_irq_info[pin].ioapic_irq_pin); |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 452 | |
| 453 | if (ptr->pci_irq_info[pin].ioapic_dst_id > 0) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 454 | fprintf(fil, "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n", |
| 455 | pin, ptr->pci_irq_info[pin].ioapic_dst_id); |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 458 | if (ptr->subsystem_device > 0) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 459 | fprintf(fil, "\t.subsystem_device = 0x%04x,\n", |
| 460 | ptr->subsystem_device); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 461 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 462 | if (ptr->rescnt > 0) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 463 | fprintf(fil, "\t.resource_list = &%s_res[0],\n", |
| 464 | ptr->name); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 465 | } |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 466 | if (ptr->children || ptr->multidev) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 467 | fprintf(fil, "\t.link_list = &%s_links[0],\n", |
| 468 | ptr->name); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 469 | else |
Patrick Georgi | 2dbfcb7 | 2012-05-30 16:26:30 +0200 | [diff] [blame] | 470 | fprintf(fil, "\t.link_list = NULL,\n"); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 471 | if (ptr->sibling) |
| 472 | fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name); |
Kyösti Mälkki | aada2e1 | 2012-10-07 15:08:32 +0200 | [diff] [blame] | 473 | fprintf(fil, "#ifndef __PRE_RAM__\n"); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 474 | fprintf(fil, "\t.chip_ops = &%s_ops,\n", |
| 475 | ptr->chip->name_underscore); |
Kyösti Mälkki | a93c3fe | 2012-10-09 22:28:56 +0300 | [diff] [blame] | 476 | if (ptr->chip->chip == &mainboard) |
| 477 | fprintf(fil, "\t.name = mainboard_name,\n"); |
Kyösti Mälkki | aada2e1 | 2012-10-07 15:08:32 +0200 | [diff] [blame] | 478 | fprintf(fil, "#endif\n"); |
| 479 | if (ptr->chip->chiph_exists) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 480 | fprintf(fil, "\t.chip_info = &%s_info_%d,\n", |
| 481 | ptr->chip->name_underscore, ptr->chip->id); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 482 | if (ptr->nextdev) |
| 483 | fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name); |
| 484 | fprintf(fil, "};\n"); |
| 485 | } |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 486 | if (ptr->rescnt > 0) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 487 | int i = 1; |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 488 | fprintf(fil, "ROMSTAGE_CONST struct resource %s_res[] = {\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 489 | ptr->name); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 490 | struct resource *r = ptr->res; |
| 491 | while (r) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 492 | fprintf(fil, |
| 493 | "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_"); |
| 494 | if (r->type == IRQ) |
| 495 | fprintf(fil, "IRQ"); |
| 496 | if (r->type == DRQ) |
| 497 | fprintf(fil, "DRQ"); |
| 498 | if (r->type == IO) |
| 499 | fprintf(fil, "IO"); |
| 500 | fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, |
| 501 | r->base); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 502 | if (r->next) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 503 | fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, |
| 504 | i++); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 505 | else |
| 506 | fprintf(fil, ".next=NULL },\n"); |
| 507 | r = r->next; |
| 508 | } |
| 509 | fprintf(fil, "\t };\n"); |
| 510 | } |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 511 | if (!ptr->used && ptr->type == device |
| 512 | && (ptr->children || ptr->multidev)) { |
| 513 | fprintf(fil, "ROMSTAGE_CONST struct bus %s_links[] = {\n", |
| 514 | ptr->name); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 515 | if (ptr->multidev) { |
| 516 | struct device *d = ptr; |
| 517 | while (d) { |
| 518 | if (device_match(d, ptr)) { |
| 519 | fprintf(fil, "\t\t[%d] = {\n", d->link); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 520 | fprintf(fil, "\t\t\t.link_num = %d,\n", |
| 521 | d->link); |
| 522 | fprintf(fil, "\t\t\t.dev = &%s,\n", |
| 523 | d->name); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 524 | if (d->children) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 525 | fprintf(fil, |
| 526 | "\t\t\t.children = &%s,\n", |
| 527 | d->children->name); |
| 528 | if (d->next_sibling |
| 529 | && device_match(d->next_sibling, |
| 530 | ptr)) |
| 531 | fprintf(fil, |
| 532 | "\t\t\t.next=&%s_links[%d],\n", |
| 533 | d->name, d->link + 1); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 534 | else |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 535 | fprintf(fil, |
| 536 | "\t\t\t.next = NULL,\n"); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 537 | fprintf(fil, "\t\t},\n"); |
| 538 | } |
| 539 | d = d->next_sibling; |
| 540 | } |
| 541 | } else { |
| 542 | if (ptr->children) { |
| 543 | fprintf(fil, "\t\t[0] = {\n"); |
| 544 | fprintf(fil, "\t\t\t.link_num = 0,\n"); |
| 545 | fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 546 | fprintf(fil, "\t\t\t.children = &%s,\n", |
| 547 | ptr->children->name); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 548 | fprintf(fil, "\t\t\t.next = NULL,\n"); |
| 549 | fprintf(fil, "\t\t},\n"); |
| 550 | } |
| 551 | } |
| 552 | fprintf(fil, "\t};\n"); |
| 553 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 554 | if ((ptr->type == chip) && (ptr->chiph_exists)) { |
| 555 | if (ptr->reg) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 556 | fprintf(fil, |
| 557 | "ROMSTAGE_CONST struct %s_config %s_info_%d = {\n", |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 558 | ptr->name_underscore, ptr->name_underscore, |
| 559 | ptr->id); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 560 | struct reg *r = ptr->reg; |
| 561 | while (r) { |
| 562 | fprintf(fil, "\t.%s = %s,\n", r->key, r->value); |
| 563 | r = r->next; |
| 564 | } |
| 565 | fprintf(fil, "};\n\n"); |
| 566 | } else { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 567 | fprintf(fil, |
| 568 | "ROMSTAGE_CONST struct %s_config %s_info_%d = { };\n", |
| 569 | ptr->name_underscore, ptr->name_underscore, |
| 570 | ptr->id); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 575 | static void walk_device_tree(FILE * fil, struct device *ptr, |
| 576 | void (*func) (FILE *, struct device *), |
| 577 | struct device *chips) |
| 578 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 579 | do { |
| 580 | func(fil, ptr); |
| 581 | ptr = ptr->next_sibling; |
| 582 | } while (ptr); |
| 583 | } |
| 584 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 585 | static void inherit_subsystem_ids(FILE * file, struct device *dev) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 586 | { |
| 587 | struct device *p; |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 588 | |
| 589 | if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) { |
| 590 | /* user already gave us a subsystem vendor/device */ |
| 591 | return; |
| 592 | } |
| 593 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 594 | for (p = dev; p && p != p->parent; p = p->parent) { |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 595 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 596 | if (p->bustype != PCI && p->bustype != DOMAIN) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 597 | continue; |
| 598 | |
| 599 | if (p->inherit_subsystem) { |
| 600 | dev->subsystem_vendor = p->subsystem_vendor; |
| 601 | dev->subsystem_device = p->subsystem_device; |
| 602 | break; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 607 | static void usage(void) |
| 608 | { |
Martin Roth | c9c27bb | 2016-08-05 18:15:06 -0600 | [diff] [blame] | 609 | printf("usage: sconfig devicetree_file output_file\n"); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 610 | exit(1); |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Martin Roth | 3205170 | 2015-11-24 12:34:16 -0700 | [diff] [blame] | 613 | enum { |
Martin Roth | c9c27bb | 2016-08-05 18:15:06 -0600 | [diff] [blame] | 614 | DEVICEFILE_ARG = 1, |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 615 | OUTPUTFILE_ARG |
| 616 | }; |
Martin Roth | 3205170 | 2015-11-24 12:34:16 -0700 | [diff] [blame] | 617 | |
Martin Roth | c9c27bb | 2016-08-05 18:15:06 -0600 | [diff] [blame] | 618 | #define ARG_COUNT 3 |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 619 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 620 | int main(int argc, char **argv) |
| 621 | { |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 622 | if (argc != ARG_COUNT) |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 623 | usage(); |
| 624 | |
Martin Roth | 25f8a4f | 2016-08-05 15:46:56 -0600 | [diff] [blame] | 625 | char *devtree = argv[DEVICEFILE_ARG]; |
| 626 | char *outputc = argv[OUTPUTFILE_ARG]; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 627 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 628 | headers.next = 0; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 629 | |
| 630 | FILE *filec = fopen(devtree, "r"); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 631 | if (!filec) { |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 632 | perror(NULL); |
| 633 | exit(1); |
| 634 | } |
| 635 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 636 | yyrestart(filec); |
| 637 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 638 | lastdev = head = &root; |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 639 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 640 | yyparse(); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 641 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 642 | fclose(filec); |
| 643 | |
| 644 | if ((head->type == chip) && (!head->chiph_exists)) { |
| 645 | struct device *tmp = head; |
| 646 | head = &root; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 647 | while (head->next != tmp) |
| 648 | head = head->next; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 651 | FILE *autogen = fopen(outputc, "w"); |
| 652 | if (!autogen) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 653 | fprintf(stderr, "Could not open file '%s' for writing: ", |
| 654 | outputc); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 655 | perror(NULL); |
| 656 | exit(1); |
| 657 | } |
| 658 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 659 | struct header *h; |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 660 | fprintf(autogen, "#include <device/device.h>\n"); |
| 661 | fprintf(autogen, "#include <device/pci.h>\n"); |
| 662 | h = &headers; |
| 663 | while (h->next) { |
| 664 | h = h->next; |
| 665 | if (h->chiph_exists) |
| 666 | fprintf(autogen, "#include \"%s/chip.h\"\n", h->name); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 667 | } |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 668 | fprintf(autogen, "\n#ifndef __PRE_RAM__\n"); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 669 | fprintf(autogen, |
| 670 | "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n"); |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 671 | h = &headers; |
| 672 | while (h->next) { |
| 673 | h = h->next; |
| 674 | char *name_underscore = translate_name(h->name, UNSLASH); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 675 | fprintf(autogen, |
| 676 | "__attribute__((weak)) struct chip_operations %s_ops = {};\n", |
| 677 | name_underscore); |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 678 | free(name_underscore); |
| 679 | } |
| 680 | fprintf(autogen, "#endif\n"); |
| 681 | |
| 682 | walk_device_tree(autogen, &root, inherit_subsystem_ids, NULL); |
| 683 | fprintf(autogen, "\n/* pass 0 */\n"); |
| 684 | walk_device_tree(autogen, &root, pass0, NULL); |
| 685 | fprintf(autogen, "\n/* pass 1 */\n" |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 686 | "ROMSTAGE_CONST struct device * ROMSTAGE_CONST last_dev = &%s;\n", |
| 687 | lastdev->name); |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 688 | walk_device_tree(autogen, &root, pass1, NULL); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 689 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 690 | fclose(autogen); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 691 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 692 | return 0; |
| 693 | } |