summaryrefslogtreecommitdiff
path: root/src/devices/root_device.c
blob: c2173d3947ba849567000838216a9667084af76c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <console/console.h>
#include <device/device.h>
#include <device/pci.h>

/** 
 * Read the resources for the root device,
 * that encompase the resources for the entire system.
 * @param root Pointer to the device structure for the system root device
 */
void root_dev_read_resources(device_t root)
{
	int res = 0;

	printk_spew("%s . Root is %p\n", __FUNCTION__, root);
	/* Initialize the system wide io space constraints */
	root->resource[res].base  = 0x400;
	root->resource[res].size  = 0;
	root->resource[res].align = 0;
	root->resource[res].gran  = 0;
	root->resource[res].limit = 0xffffUL;
	root->resource[res].flags = IORESOURCE_IO;
	root->resource[res].index = 0;
	printk_spew("%s . link %p, resource %p\n", __FUNCTION__, 
		    &root->link[0], &root->resource[res]);
	compute_allocate_resource(&root->link[0], &root->resource[res], 
				  IORESOURCE_IO, IORESOURCE_IO);
	res++;

	/* Initialize the system wide memory resources constraints */
	root->resource[res].base  = 0;
	root->resource[res].size  = 0;
	root->resource[res].align = 0;
	root->resource[res].gran  = 0;
	root->resource[res].limit = 0xffffffffUL;
	root->resource[res].flags = IORESOURCE_MEM;
	root->resource[res].index = 1;
	printk_spew("%s . link %p, resource %p\n", __FUNCTION__, 
		    &root->link[0], &root->resource[res]);
	compute_allocate_resource(&root->link[0], &root->resource[res], 
				  IORESOURCE_MEM, IORESOURCE_MEM);
	res++;

	root->resources = res;
	printk_spew("%s DONE\n", __FUNCTION__);
}

/**
 * Write the resources for the root device,
 * and every device under it which are all of the devices.
 * @param root Pointer to the device structure for the system root device
 */
void root_dev_set_resources(device_t root)
{
	struct bus *bus = &root->link[0];

	compute_allocate_resource(bus, &root->resource[0],
				  IORESOURCE_IO, IORESOURCE_IO);
	compute_allocate_resource(bus, &root->resource[1],
				  IORESOURCE_MEM, IORESOURCE_MEM);
	assign_resources(bus);
}

/**
 * Walk through devices on the motherboard and scan for devices behind
 * them.
 * @param root Pointer to the device structure for the system root device
 * @param max  Maximum bus number allowed in the system.
 * @return Largest bus number used.
 */
unsigned int walk_static_devices(device_t bus, unsigned int max)
{
	device_t child;
	unsigned link;

	for (link = 0; link < bus->links; link++) {
		for (child = bus->link[link].children; child; child = child->sibling) {
			if (child->ops && child->ops->enable) {
				child->ops->enable(child);
			}
			printk_debug("%s %s\n", dev_path(child),
				     child->enabled?"enabled": "disabled");
		}
	}
	for (link = 0; link < bus->links; link++) {
		for (child = bus->link[link].children; child; child = child->sibling) {
			if (!child->ops || !child->ops->scan_bus)
				continue;
			printk_debug("%s scanning...\n", dev_path(child));
			max = child->ops->scan_bus(child, max);
		}
	}
	return max;
}

void enable_childrens_resources(device_t dev)
{
	unsigned link;
	for (link = 0; link < dev->links; link++) {
		device_t child;
		for (child = dev->link[link].children; child; child = child->sibling) {
			enable_resources(child);
		}
	}
}

unsigned int root_dev_scan_pci_bus(device_t root, unsigned int max)
{
	return pci_scan_bus(&root->link[0], 0, 0xff, max);
}

struct device_operations default_dev_ops_root = {
	.read_resources   = root_dev_read_resources,
	.set_resources    = root_dev_set_resources,
	.enable_resources = enable_childrens_resources,
	.init             = 0,
	.scan_bus         = root_dev_scan_pci_bus,
};

/**
 * This is the root of the device tree. A PCI tree always has 
 * one bus, bus 0. Bus 0 contains devices and bridges. 
 */
struct device dev_root = {
	.ops = &default_dev_ops_root,
	.bus = &dev_root.link[0],
	.path = { .type = DEVICE_PATH_ROOT },
	.enabled = 1,
	.links   = 1,
	.link    = {
		 [0] = {
			 .dev = &dev_root,
			 .link = 0,
		 },
	 },
};