blob: 4b476a8d7ba4653416e536d63cf3f84b73ecc12b [file] [log] [blame]
Ronald G. Minnichc2c97232013-03-15 15:57:13 -07001/*
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. Minnichc2c97232013-03-15 15:57:13 -070014 */
15#include <stdio.h>
16/* you can't include string.h due to conflicts with coreboot prototypes. */
17void *memcpy(void *m1, void *m2, size_t s);
18
19struct 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
28extern struct device dev_root;
29struct device *all_devices = &dev_root;
30
31const 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 */
37const 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;
Justin TerAvestca2ed9f2018-01-17 16:36:30 -080094 case DEVICE_PATH_MMIO:
95 sprintf(buffer, "MMIO: %08x", dev->path.mmio.addr);
96 break;
Ronald G. Minnichc2c97232013-03-15 15:57:13 -070097 default:
98 printf("Unknown device path type: %d\n",
99 dev->path.type);
100 break;
101 }
102 }
103 return buffer;
104}
105
106
107void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum)
108{
109 char depth_str[20] = "";
110 int i;
111 struct device *sibling;
112 struct bus *link;
113
114 for (i = 0; i < depth; i++)
115 depth_str[i] = ' ';
116 depth_str[i] = '\0';
117
118 printf("%s%s: enabled %d%s\n",
119 depth_str, dev_path(dev), dev->enabled,
120 dev->chip_ops ? ":has a chip":"");
121
122 for (link = dev->link_list; link; link = link->next) {
123 for (sibling = link->children; sibling;
124 sibling = sibling->sibling)
125 show_devs_tree(sibling, debug_level, depth + 1, i);
126 }
127}
128
129void show_all_devs_tree(int debug_level, const char *msg)
130{
131 printf("Show all devs in tree form...%s\n", msg);
132
133 show_devs_tree(all_devices, debug_level, 0, -1);
134}
135
136void show_devs_subtree(struct device *root, int debug_level, const char *msg)
137{
138 printf("Show all devs in subtree %s...%s\n",
139 dev_path(root), msg);
140
141 printf("%s\n", msg);
142 show_devs_tree(root, debug_level, 0, -1);
143}
144
145void show_all_devs(int debug_level, const char *msg)
146{
147 struct device *dev;
148
149 printf("Show all devs...%s\n", msg);
150 for (dev = all_devices; dev; dev = dev->next) {
151 printf("%s: enabled %d%s\n",
152 dev_path(dev), dev->enabled,
153 dev->chip_ops ? ":has a chip":"");
154 }
155}
156
157main()
158{
159 show_all_devs(1, "");
160 show_all_devs_tree(1, "");
161}
162
163/*
164 * Example: (yank this and paste into M-x compile in emacs)
165 * or tail -2 showdt.c | head -1 |sh
166 * or whatever.
Gabe Black51edd542013-09-30 23:00:33 -0700167 cc -I ../src -I ../src/include -I ../src/arch/arm/include/ -include build/mainboard/google/snow/static.c showdt.c
Ronald G. Minnichc2c97232013-03-15 15:57:13 -0700168*/