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