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