summaryrefslogtreecommitdiff
path: root/src/devices/root_device.c
blob: 91c706e4b6ab605db93e476ac988bca998ad89bd (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <console/console.h>
#include <device/device.h>
#include <device/pci.h>

/** 
 * Read the resources for the root device,
 * that encompass 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)
{
	struct resource *resource;

	/* Initialize the system wide io space constraints */
	resource = new_resource(root, 0);
	resource->base  = 0x400;
	resource->size  = 0;
	resource->align = 0;
	resource->gran  = 0;
	resource->limit = 0xffffUL;
	resource->flags = IORESOURCE_IO;
	compute_allocate_resource(&root->link[0], resource, 
		IORESOURCE_IO, IORESOURCE_IO);

	/* Initialize the system wide memory resources constraints */
	resource = new_resource(root, 1);
	resource->base  = 0;
	resource->size  = 0;
	resource->align = 0;
	resource->gran  = 0;
	resource->limit = 0xffffffffUL;
	resource->flags = IORESOURCE_MEM;
	compute_allocate_resource(&root->link[0], resource,
		IORESOURCE_MEM, IORESOURCE_MEM);
}

/**
 * @brief 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;

	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);
}

/**
 * @brief Scan devices on static buses.
 *
 * The enumeration of certain buses is purely static. The existence of
 * devices on those buses can be completely determined at compile time
 * and is specified in the config file. Typical exapmles are the 'PNP'
 * devices on a legacy ISA/LPC bus. There is no need of probing of any
 * kind, the only thing we have to do is to walk through the bus and
 * enable or disable devices as indicated in the config file.
 *
 * On the other hand, some devices are virtual and their existence is
 * artificial. They can not be probed at run time. One example is the
 * debug device. Those virtual devices have to be listed in the config
 * file under some static bus in order to be enumerated at run time.
 *
 * This function is the default scan_bus() method for the root device and
 * LPC bridges.
 *
 * @param root Pointer to the root device which the static buses are attached
 * @param max  Maximum bus number currently used before scanning.
 * @return Largest bus number used after scanning.
 */
unsigned int scan_static_bus(device_t root, unsigned int max)
{
	device_t child;
	unsigned link;

	printk_spew("%s for %s\n", __func__, dev_path(root));

	for (link = 0; link < root->links; link++) {
		for (child = root->link[link].children; child; child = child->sibling) {
			if (child->chip_ops && child->chip_ops->enable_dev) {
				child->chip_ops->enable_dev(child);
			}
			if (child->ops && child->ops->enable) {
				child->ops->enable(child);
			}
			if (child->path.type == DEVICE_PATH_I2C)	
				printk_debug("smbus: %s[%d]->",  dev_path(child->bus->dev), child->bus->link );
			printk_debug("%s %s\n", dev_path(child),
				     child->enabled?"enabled": "disabled");
		}
	}
	for (link = 0; link < root->links; link++) {
		for (child = root->link[link].children; child; child = child->sibling) {
			if (!child->ops || !child->ops->scan_bus)
				continue;
			printk_spew("%s scanning...\n", dev_path(child));
			max = child->ops->scan_bus(child, max);
		}
	}

	printk_spew("%s for  %s done\n", __func__, dev_path(root));

	return max;
}

/**
 * @brief Enable resources for children devices
 *
 * @param dev the device whos children's resources are to be enabled
 *
 * This function is call by the global enable_resources() indirectly via the
 * device_operation::enable_resources() method of devices.
 *
 * Indirect mutual recursion:
 *	enable_childrens_resources() -> enable_resources()
 *	enable_resources() -> device_operation::enable_resources()
 *	device_operation::enable_resources() -> enable_children_resources()
 */
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);
		}
	}
}

void root_dev_enable_resources(device_t dev)
{
	enable_childrens_resources(dev);
}

/**
 * @brief Scan root bus for generic systems
 *
 * @param root The root device structure
 * @param max The current bus number scanned so far, usually 0x00
 *
 * This function is the default scan_bus() method of the root device.
 */
unsigned int root_dev_scan_bus(device_t root, unsigned int max)
{
	return scan_static_bus(root, max);
}

void root_dev_init(device_t root)
{
}

/**
 * @brief Default device operation for root device
 *
 * This is the default device operation for root devices. These operations
 * should be fully usable as is.  However the chip_operations::enable_dev()
 * of a motherboard can override this if you want non-default behavior.
 */
struct device_operations default_dev_ops_root = {
	.read_resources   = root_dev_read_resources,
	.set_resources    = root_dev_set_resources,
	.enable_resources = root_dev_enable_resources,
	.init             = root_dev_init,
	.scan_bus         = root_dev_scan_bus,
};

/**
 * @brief The root of device tree.
 *
 * This is the root of the device tree. The device tree is defined in the
 * static.c file and is generated by config tool during compile time.
 */
extern struct device dev_root;