Ronald G. Minnich | c2c9723 | 2013-03-15 15:57:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Compile and dump the device tree |
| 3 | * |
| 4 | * Copyright 2013 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 18 | */ |
| 19 | #include <stdio.h> |
| 20 | /* you can't include string.h due to conflicts with coreboot prototypes. */ |
| 21 | void *memcpy(void *m1, void *m2, size_t s); |
| 22 | |
| 23 | struct device_operations default_dev_ops_root = { |
| 24 | .read_resources = NULL, |
| 25 | .set_resources = NULL, |
| 26 | .enable_resources = NULL, |
| 27 | .init = NULL, |
| 28 | .scan_bus = NULL, |
| 29 | .reset_bus = NULL, |
| 30 | }; |
| 31 | |
| 32 | extern struct device dev_root; |
| 33 | struct device *all_devices = &dev_root; |
| 34 | |
| 35 | const char mainboard_name[] = "showdt"; |
| 36 | |
| 37 | /* |
| 38 | * Warning: This function uses a static buffer. Don't call it more than once |
| 39 | * from the same print statement! |
| 40 | */ |
| 41 | const char *dev_path(device_t dev) |
| 42 | { |
| 43 | static char buffer[DEVICE_PATH_MAX]; |
| 44 | |
| 45 | buffer[0] = '\0'; |
| 46 | if (!dev) { |
| 47 | memcpy(buffer, "<null>", 7); |
| 48 | } else { |
| 49 | switch(dev->path.type) { |
| 50 | case DEVICE_PATH_ROOT: |
| 51 | memcpy(buffer, "Root Device", 12); |
| 52 | break; |
| 53 | case DEVICE_PATH_PCI: |
| 54 | #if CONFIG_PCI_BUS_SEGN_BITS |
| 55 | sprintf(buffer, "PCI: %04x:%02x:%02x.%01x", |
| 56 | dev->bus->secondary >> 8, |
| 57 | dev->bus->secondary & 0xff, |
| 58 | PCI_SLOT(dev->path.pci.devfn), |
| 59 | PCI_FUNC(dev->path.pci.devfn)); |
| 60 | #else |
| 61 | sprintf(buffer, "PCI: %02x:%02x.%01x", |
| 62 | dev->bus->secondary, |
| 63 | PCI_SLOT(dev->path.pci.devfn), |
| 64 | PCI_FUNC(dev->path.pci.devfn)); |
| 65 | #endif |
| 66 | break; |
| 67 | case DEVICE_PATH_PNP: |
| 68 | sprintf(buffer, "PNP: %04x.%01x", |
| 69 | dev->path.pnp.port, dev->path.pnp.device); |
| 70 | break; |
| 71 | case DEVICE_PATH_I2C: |
| 72 | sprintf(buffer, "I2C: %02x:%02x", |
| 73 | dev->bus->secondary, |
| 74 | dev->path.i2c.device); |
| 75 | break; |
| 76 | case DEVICE_PATH_APIC: |
| 77 | sprintf(buffer, "APIC: %02x", |
| 78 | dev->path.apic.apic_id); |
| 79 | break; |
| 80 | case DEVICE_PATH_IOAPIC: |
| 81 | sprintf(buffer, "IOAPIC: %02x", |
| 82 | dev->path.ioapic.ioapic_id); |
| 83 | break; |
| 84 | case DEVICE_PATH_DOMAIN: |
| 85 | sprintf(buffer, "DOMAIN: %04x", |
| 86 | dev->path.domain.domain); |
| 87 | break; |
| 88 | case DEVICE_PATH_CPU_CLUSTER: |
| 89 | sprintf(buffer, "CPU_CLUSTER: %01x", |
| 90 | dev->path.cpu_cluster.cluster); |
| 91 | break; |
| 92 | case DEVICE_PATH_CPU: |
| 93 | sprintf(buffer, "CPU: %02x", dev->path.cpu.id); |
| 94 | break; |
| 95 | case DEVICE_PATH_CPU_BUS: |
| 96 | sprintf(buffer, "CPU_BUS: %02x", dev->path.cpu_bus.id); |
| 97 | break; |
| 98 | default: |
| 99 | printf("Unknown device path type: %d\n", |
| 100 | dev->path.type); |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | return buffer; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum) |
| 109 | { |
| 110 | char depth_str[20] = ""; |
| 111 | int i; |
| 112 | struct device *sibling; |
| 113 | struct bus *link; |
| 114 | |
| 115 | for (i = 0; i < depth; i++) |
| 116 | depth_str[i] = ' '; |
| 117 | depth_str[i] = '\0'; |
| 118 | |
| 119 | printf("%s%s: enabled %d%s\n", |
| 120 | depth_str, dev_path(dev), dev->enabled, |
| 121 | dev->chip_ops ? ":has a chip":""); |
| 122 | |
| 123 | for (link = dev->link_list; link; link = link->next) { |
| 124 | for (sibling = link->children; sibling; |
| 125 | sibling = sibling->sibling) |
| 126 | show_devs_tree(sibling, debug_level, depth + 1, i); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void show_all_devs_tree(int debug_level, const char *msg) |
| 131 | { |
| 132 | printf("Show all devs in tree form...%s\n", msg); |
| 133 | |
| 134 | show_devs_tree(all_devices, debug_level, 0, -1); |
| 135 | } |
| 136 | |
| 137 | void show_devs_subtree(struct device *root, int debug_level, const char *msg) |
| 138 | { |
| 139 | printf("Show all devs in subtree %s...%s\n", |
| 140 | dev_path(root), msg); |
| 141 | |
| 142 | printf("%s\n", msg); |
| 143 | show_devs_tree(root, debug_level, 0, -1); |
| 144 | } |
| 145 | |
| 146 | void show_all_devs(int debug_level, const char *msg) |
| 147 | { |
| 148 | struct device *dev; |
| 149 | |
| 150 | printf("Show all devs...%s\n", msg); |
| 151 | for (dev = all_devices; dev; dev = dev->next) { |
| 152 | printf("%s: enabled %d%s\n", |
| 153 | dev_path(dev), dev->enabled, |
| 154 | dev->chip_ops ? ":has a chip":""); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | main() |
| 159 | { |
| 160 | show_all_devs(1, ""); |
| 161 | show_all_devs_tree(1, ""); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Example: (yank this and paste into M-x compile in emacs) |
| 166 | * or tail -2 showdt.c | head -1 |sh |
| 167 | * or whatever. |
| 168 | cc -I ../src -I ../src/include -I ../src/arch/armv7/include/ -include build/mainboard/google/snow/static.c showdt.c |
| 169 | */ |