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