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 | |
Duncan Laurie | bae9f85 | 2018-05-07 14:18:13 -0700 | [diff] [blame] | 293 | case USB: |
| 294 | new_d->path = ".type=DEVICE_PATH_USB,{.usb={ .port_type = %d, .port_id = %d }}"; |
| 295 | break; |
| 296 | |
Justin TerAvest | ca2ed9f | 2018-01-17 16:36:30 -0800 | [diff] [blame] | 297 | case MMIO: |
| 298 | new_d->path = ".type=DEVICE_PATH_MMIO,{.mmio={ .addr = 0x%x }}"; |
| 299 | break; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 300 | } |
Justin TerAvest | ca2ed9f | 2018-01-17 16:36:30 -0800 | [diff] [blame] | 301 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 302 | return new_d; |
| 303 | } |
| 304 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 305 | void alias_siblings(struct device *d) |
| 306 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 307 | while (d) { |
| 308 | int link = 0; |
| 309 | struct device *cmp = d->next_sibling; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 310 | while (cmp && (cmp->bus == d->bus) && (cmp->path_a == d->path_a) |
| 311 | && (cmp->path_b == d->path_b)) { |
| 312 | if (cmp->type == device && !cmp->used) { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 313 | if (device_match(d, cmp)) { |
| 314 | d->multidev = 1; |
| 315 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 316 | cmp->id = d->id; |
| 317 | cmp->name = d->name; |
| 318 | cmp->used = 1; |
| 319 | cmp->link = ++link; |
| 320 | } |
| 321 | } |
| 322 | cmp = cmp->next_sibling; |
| 323 | } |
| 324 | d = d->next_sibling; |
| 325 | } |
| 326 | } |
| 327 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 328 | void add_resource(struct device *dev, int type, int index, int base) |
| 329 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 330 | struct resource *r = malloc(sizeof(struct resource)); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 331 | memset(r, 0, sizeof(struct resource)); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 332 | r->type = type; |
| 333 | r->index = index; |
| 334 | r->base = base; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 335 | if (dev->res) { |
| 336 | struct resource *head = dev->res; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 337 | while (head->next) |
| 338 | head = head->next; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 339 | head->next = r; |
| 340 | } else { |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 341 | dev->res = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 342 | } |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 343 | dev->rescnt++; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 346 | void add_register(struct device *dev, char *name, char *val) |
| 347 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 348 | struct reg *r = malloc(sizeof(struct reg)); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 349 | memset(r, 0, sizeof(struct reg)); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 350 | r->key = name; |
| 351 | r->value = val; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 352 | if (dev->reg) { |
| 353 | struct reg *head = dev->reg; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 354 | // sorting to be equal to sconfig's behaviour |
| 355 | int sort = strcmp(r->key, head->key); |
| 356 | if (sort == 0) { |
| 357 | printf("ERROR: duplicate 'register' key.\n"); |
| 358 | exit(1); |
| 359 | } |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 360 | if (sort < 0) { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 361 | r->next = head; |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 362 | dev->reg = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 363 | } else { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 364 | while ((head->next) |
| 365 | && (strcmp(head->next->key, r->key) < 0)) |
| 366 | head = head->next; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 367 | r->next = head->next; |
| 368 | head->next = r; |
| 369 | } |
| 370 | } else { |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 371 | dev->reg = r; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 375 | void add_pci_subsystem_ids(struct device *dev, int vendor, int device, |
| 376 | int inherit) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 377 | { |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 378 | if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 379 | printf("ERROR: 'subsystem' only allowed for PCI devices\n"); |
| 380 | exit(1); |
| 381 | } |
| 382 | |
| 383 | dev->subsystem_vendor = vendor; |
| 384 | dev->subsystem_device = device; |
| 385 | dev->inherit_subsystem = inherit; |
| 386 | } |
| 387 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 388 | void add_ioapic_info(struct device *dev, int apicid, const char *_srcpin, |
| 389 | int irqpin) |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 390 | { |
| 391 | |
| 392 | int srcpin; |
| 393 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 394 | if (!_srcpin || strlen(_srcpin) < 4 || strncasecmp(_srcpin, "INT", 3) || |
| 395 | _srcpin[3] < 'A' || _srcpin[3] > 'D') { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 396 | printf("ERROR: malformed ioapic_irq args: %s\n", _srcpin); |
| 397 | exit(1); |
| 398 | } |
| 399 | |
| 400 | srcpin = _srcpin[3] - 'A'; |
| 401 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 402 | if (dev->bustype != PCI && dev->bustype != DOMAIN) { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 403 | printf("ERROR: ioapic config only allowed for PCI devices\n"); |
| 404 | exit(1); |
| 405 | } |
| 406 | |
| 407 | if (srcpin > 3) { |
Patrick Georgi | 116327e | 2012-07-20 12:47:06 +0200 | [diff] [blame] | 408 | printf("ERROR: srcpin '%d' invalid\n", srcpin); |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 409 | exit(1); |
| 410 | } |
| 411 | dev->pci_irq_info[srcpin].ioapic_irq_pin = irqpin; |
| 412 | dev->pci_irq_info[srcpin].ioapic_dst_id = apicid; |
| 413 | } |
| 414 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 415 | static void pass0(FILE * fil, struct device *ptr) |
| 416 | { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 417 | if (ptr->type == device && ptr->id == 0) |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 418 | fprintf(fil, "DEVTREE_CONST struct bus %s_links[];\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 419 | ptr->name); |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 420 | |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 421 | if ((ptr->type == device) && (ptr->id != 0) && (!ptr->used)) { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 422 | fprintf(fil, "DEVTREE_CONST static struct device %s;\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 423 | ptr->name); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 424 | if (ptr->rescnt > 0) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 425 | fprintf(fil, |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 426 | "DEVTREE_CONST struct resource %s_res[];\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 427 | ptr->name); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 428 | if (ptr->children || ptr->multidev) |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 429 | fprintf(fil, "DEVTREE_CONST struct bus %s_links[];\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 430 | ptr->name); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 431 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 434 | static void pass1(FILE * fil, struct device *ptr) |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 435 | { |
| 436 | int pin; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 437 | if (!ptr->used && (ptr->type == device)) { |
Stefan Reinauer | df61dd2 | 2010-08-09 12:02:00 +0000 | [diff] [blame] | 438 | if (ptr->id != 0) |
Patrick Georgi | 2dbfcb7 | 2012-05-30 16:26:30 +0200 | [diff] [blame] | 439 | fprintf(fil, "static "); |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 440 | fprintf(fil, "DEVTREE_CONST struct device %s = {\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 441 | ptr->name); |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 442 | fprintf(fil, "#if !DEVTREE_EARLY\n"); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 443 | fprintf(fil, "\t.ops = %s,\n", (ptr->ops) ? (ptr->ops) : "0"); |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 444 | fprintf(fil, "#endif\n"); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 445 | fprintf(fil, "\t.bus = &%s_links[%d],\n", ptr->bus->name, |
| 446 | ptr->bus->link); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 447 | fprintf(fil, "\t.path = {"); |
| 448 | fprintf(fil, ptr->path, ptr->path_a, ptr->path_b); |
| 449 | fprintf(fil, "},\n"); |
| 450 | fprintf(fil, "\t.enabled = %d,\n", ptr->enabled); |
| 451 | fprintf(fil, "\t.on_mainboard = 1,\n"); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 452 | if (ptr->subsystem_vendor > 0) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 453 | fprintf(fil, "\t.subsystem_vendor = 0x%04x,\n", |
| 454 | ptr->subsystem_vendor); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 455 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 456 | for (pin = 0; pin < 4; pin++) { |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 457 | if (ptr->pci_irq_info[pin].ioapic_irq_pin > 0) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 458 | fprintf(fil, "\t.pci_irq_info[%d].ioapic_irq_pin = %d,\n", |
| 459 | pin, ptr->pci_irq_info[pin].ioapic_irq_pin); |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 460 | |
| 461 | if (ptr->pci_irq_info[pin].ioapic_dst_id > 0) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 462 | fprintf(fil, "\t.pci_irq_info[%d].ioapic_dst_id = %d,\n", |
| 463 | pin, ptr->pci_irq_info[pin].ioapic_dst_id); |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 464 | } |
| 465 | |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 466 | if (ptr->subsystem_device > 0) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 467 | fprintf(fil, "\t.subsystem_device = 0x%04x,\n", |
| 468 | ptr->subsystem_device); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 469 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 470 | if (ptr->rescnt > 0) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 471 | fprintf(fil, "\t.resource_list = &%s_res[0],\n", |
| 472 | ptr->name); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 473 | } |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 474 | if (ptr->children || ptr->multidev) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 475 | fprintf(fil, "\t.link_list = &%s_links[0],\n", |
| 476 | ptr->name); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 477 | else |
Patrick Georgi | 2dbfcb7 | 2012-05-30 16:26:30 +0200 | [diff] [blame] | 478 | fprintf(fil, "\t.link_list = NULL,\n"); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 479 | if (ptr->sibling) |
| 480 | fprintf(fil, "\t.sibling = &%s,\n", ptr->sibling->name); |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 481 | fprintf(fil, "#if !DEVTREE_EARLY\n"); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 482 | fprintf(fil, "\t.chip_ops = &%s_ops,\n", |
| 483 | ptr->chip->name_underscore); |
Kyösti Mälkki | a93c3fe | 2012-10-09 22:28:56 +0300 | [diff] [blame] | 484 | if (ptr->chip->chip == &mainboard) |
| 485 | fprintf(fil, "\t.name = mainboard_name,\n"); |
Kyösti Mälkki | aada2e1 | 2012-10-07 15:08:32 +0200 | [diff] [blame] | 486 | fprintf(fil, "#endif\n"); |
| 487 | if (ptr->chip->chiph_exists) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 488 | fprintf(fil, "\t.chip_info = &%s_info_%d,\n", |
| 489 | ptr->chip->name_underscore, ptr->chip->id); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 490 | if (ptr->nextdev) |
| 491 | fprintf(fil, "\t.next=&%s\n", ptr->nextdev->name); |
| 492 | fprintf(fil, "};\n"); |
| 493 | } |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 494 | if (ptr->rescnt > 0) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 495 | int i = 1; |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 496 | fprintf(fil, "DEVTREE_CONST struct resource %s_res[] = {\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 497 | ptr->name); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 498 | struct resource *r = ptr->res; |
| 499 | while (r) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 500 | fprintf(fil, |
| 501 | "\t\t{ .flags=IORESOURCE_FIXED | IORESOURCE_ASSIGNED | IORESOURCE_"); |
| 502 | if (r->type == IRQ) |
| 503 | fprintf(fil, "IRQ"); |
| 504 | if (r->type == DRQ) |
| 505 | fprintf(fil, "DRQ"); |
| 506 | if (r->type == IO) |
| 507 | fprintf(fil, "IO"); |
| 508 | fprintf(fil, ", .index=0x%x, .base=0x%x,", r->index, |
| 509 | r->base); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 510 | if (r->next) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 511 | fprintf(fil, ".next=&%s_res[%d]},\n", ptr->name, |
| 512 | i++); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 513 | else |
| 514 | fprintf(fil, ".next=NULL },\n"); |
| 515 | r = r->next; |
| 516 | } |
| 517 | fprintf(fil, "\t };\n"); |
| 518 | } |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 519 | if (!ptr->used && ptr->type == device |
| 520 | && (ptr->children || ptr->multidev)) { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 521 | fprintf(fil, "DEVTREE_CONST struct bus %s_links[] = {\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 522 | ptr->name); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 523 | if (ptr->multidev) { |
| 524 | struct device *d = ptr; |
| 525 | while (d) { |
| 526 | if (device_match(d, ptr)) { |
| 527 | fprintf(fil, "\t\t[%d] = {\n", d->link); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 528 | fprintf(fil, "\t\t\t.link_num = %d,\n", |
| 529 | d->link); |
| 530 | fprintf(fil, "\t\t\t.dev = &%s,\n", |
| 531 | d->name); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 532 | if (d->children) |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 533 | fprintf(fil, |
| 534 | "\t\t\t.children = &%s,\n", |
| 535 | d->children->name); |
| 536 | if (d->next_sibling |
| 537 | && device_match(d->next_sibling, |
| 538 | ptr)) |
| 539 | fprintf(fil, |
| 540 | "\t\t\t.next=&%s_links[%d],\n", |
| 541 | d->name, d->link + 1); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 542 | else |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 543 | fprintf(fil, |
| 544 | "\t\t\t.next = NULL,\n"); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 545 | fprintf(fil, "\t\t},\n"); |
| 546 | } |
| 547 | d = d->next_sibling; |
| 548 | } |
| 549 | } else { |
| 550 | if (ptr->children) { |
| 551 | fprintf(fil, "\t\t[0] = {\n"); |
| 552 | fprintf(fil, "\t\t\t.link_num = 0,\n"); |
| 553 | fprintf(fil, "\t\t\t.dev = &%s,\n", ptr->name); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 554 | fprintf(fil, "\t\t\t.children = &%s,\n", |
| 555 | ptr->children->name); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 556 | fprintf(fil, "\t\t\t.next = NULL,\n"); |
| 557 | fprintf(fil, "\t\t},\n"); |
| 558 | } |
| 559 | } |
| 560 | fprintf(fil, "\t};\n"); |
| 561 | } |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 562 | if ((ptr->type == chip) && (ptr->chiph_exists)) { |
| 563 | if (ptr->reg) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 564 | fprintf(fil, |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 565 | "DEVTREE_CONST struct %s_config %s_info_%d = {\n", |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 566 | ptr->name_underscore, ptr->name_underscore, |
| 567 | ptr->id); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 568 | struct reg *r = ptr->reg; |
| 569 | while (r) { |
| 570 | fprintf(fil, "\t.%s = %s,\n", r->key, r->value); |
| 571 | r = r->next; |
| 572 | } |
| 573 | fprintf(fil, "};\n\n"); |
| 574 | } else { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 575 | fprintf(fil, |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 576 | "DEVTREE_CONST struct %s_config %s_info_%d = { };\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 577 | ptr->name_underscore, ptr->name_underscore, |
| 578 | ptr->id); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 583 | static void walk_device_tree(FILE * fil, struct device *ptr, |
| 584 | void (*func) (FILE *, struct device *), |
| 585 | struct device *chips) |
| 586 | { |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 587 | do { |
| 588 | func(fil, ptr); |
| 589 | ptr = ptr->next_sibling; |
| 590 | } while (ptr); |
| 591 | } |
| 592 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 593 | static void inherit_subsystem_ids(FILE * file, struct device *dev) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 594 | { |
| 595 | struct device *p; |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 596 | |
| 597 | if (dev->subsystem_vendor != -1 && dev->subsystem_device != -1) { |
| 598 | /* user already gave us a subsystem vendor/device */ |
| 599 | return; |
| 600 | } |
| 601 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 602 | for (p = dev; p && p != p->parent; p = p->parent) { |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 603 | |
Stefan Reinauer | 4aff445 | 2013-02-12 14:17:15 -0800 | [diff] [blame] | 604 | if (p->bustype != PCI && p->bustype != DOMAIN) |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 605 | continue; |
| 606 | |
| 607 | if (p->inherit_subsystem) { |
| 608 | dev->subsystem_vendor = p->subsystem_vendor; |
| 609 | dev->subsystem_device = p->subsystem_device; |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 615 | static void usage(void) |
| 616 | { |
Martin Roth | c9c27bb | 2016-08-05 18:15:06 -0600 | [diff] [blame] | 617 | printf("usage: sconfig devicetree_file output_file\n"); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 618 | exit(1); |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Martin Roth | 3205170 | 2015-11-24 12:34:16 -0700 | [diff] [blame] | 621 | enum { |
Martin Roth | c9c27bb | 2016-08-05 18:15:06 -0600 | [diff] [blame] | 622 | DEVICEFILE_ARG = 1, |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 623 | OUTPUTFILE_ARG |
| 624 | }; |
Martin Roth | 3205170 | 2015-11-24 12:34:16 -0700 | [diff] [blame] | 625 | |
Martin Roth | c9c27bb | 2016-08-05 18:15:06 -0600 | [diff] [blame] | 626 | #define ARG_COUNT 3 |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 627 | |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 628 | int main(int argc, char **argv) |
| 629 | { |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 630 | if (argc != ARG_COUNT) |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 631 | usage(); |
| 632 | |
Martin Roth | 25f8a4f | 2016-08-05 15:46:56 -0600 | [diff] [blame] | 633 | char *devtree = argv[DEVICEFILE_ARG]; |
| 634 | char *outputc = argv[OUTPUTFILE_ARG]; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 635 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 636 | headers.next = 0; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 637 | |
| 638 | FILE *filec = fopen(devtree, "r"); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 639 | if (!filec) { |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 640 | perror(NULL); |
| 641 | exit(1); |
| 642 | } |
| 643 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 644 | yyrestart(filec); |
| 645 | |
Patrick Georgi | 68befd5 | 2010-05-05 12:05:25 +0000 | [diff] [blame] | 646 | lastdev = head = &root; |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 647 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 648 | yyparse(); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 649 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 650 | fclose(filec); |
| 651 | |
| 652 | if ((head->type == chip) && (!head->chiph_exists)) { |
| 653 | struct device *tmp = head; |
| 654 | head = &root; |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 655 | while (head->next != tmp) |
| 656 | head = head->next; |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 659 | FILE *autogen = fopen(outputc, "w"); |
| 660 | if (!autogen) { |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 661 | fprintf(stderr, "Could not open file '%s' for writing: ", |
| 662 | outputc); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 663 | perror(NULL); |
| 664 | exit(1); |
| 665 | } |
| 666 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 667 | struct header *h; |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 668 | fprintf(autogen, "#include <device/device.h>\n"); |
| 669 | fprintf(autogen, "#include <device/pci.h>\n"); |
| 670 | h = &headers; |
| 671 | while (h->next) { |
| 672 | h = h->next; |
| 673 | if (h->chiph_exists) |
| 674 | fprintf(autogen, "#include \"%s/chip.h\"\n", h->name); |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 675 | } |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 676 | fprintf(autogen, "\n#if !DEVTREE_EARLY\n"); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 677 | fprintf(autogen, |
| 678 | "__attribute__((weak)) struct chip_operations mainboard_ops = {};\n"); |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 679 | h = &headers; |
| 680 | while (h->next) { |
| 681 | h = h->next; |
| 682 | char *name_underscore = translate_name(h->name, UNSLASH); |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 683 | fprintf(autogen, |
| 684 | "__attribute__((weak)) struct chip_operations %s_ops = {};\n", |
| 685 | name_underscore); |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 686 | free(name_underscore); |
| 687 | } |
| 688 | fprintf(autogen, "#endif\n"); |
| 689 | |
| 690 | walk_device_tree(autogen, &root, inherit_subsystem_ids, NULL); |
| 691 | fprintf(autogen, "\n/* pass 0 */\n"); |
| 692 | walk_device_tree(autogen, &root, pass0, NULL); |
| 693 | fprintf(autogen, "\n/* pass 1 */\n" |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 694 | "DEVTREE_CONST struct device * DEVTREE_CONST last_dev = &%s;\n", |
Martin Roth | bec0753 | 2016-08-05 18:32:18 -0600 | [diff] [blame] | 695 | lastdev->name); |
Martin Roth | 824255e | 2016-08-05 17:40:39 -0600 | [diff] [blame] | 696 | walk_device_tree(autogen, &root, pass1, NULL); |
Sven Schnelle | 270a908 | 2011-03-01 19:58:15 +0000 | [diff] [blame] | 697 | |
Kyösti Mälkki | 472d902 | 2011-12-05 20:33:55 +0200 | [diff] [blame] | 698 | fclose(autogen); |
Stefan Reinauer | 7c1f6b8 | 2010-08-16 18:21:56 +0000 | [diff] [blame] | 699 | |
Patrick Georgi | 114e7b2 | 2010-05-05 11:19:50 +0000 | [diff] [blame] | 700 | return 0; |
| 701 | } |