xref: /freebsd-src/sys/dev/clk/clk_bus.c (revision 18250ec6c089c0c50cbd9fd87d78e03ff89916df)
1be82b3a0SEmmanuel Vadot /*-
2be82b3a0SEmmanuel Vadot  * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
3be82b3a0SEmmanuel Vadot  * All rights reserved.
4be82b3a0SEmmanuel Vadot  *
5be82b3a0SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
6be82b3a0SEmmanuel Vadot  * modification, are permitted provided that the following conditions
7be82b3a0SEmmanuel Vadot  * are met:
8be82b3a0SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
9be82b3a0SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
10be82b3a0SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
11be82b3a0SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
12be82b3a0SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
13be82b3a0SEmmanuel Vadot  *
14be82b3a0SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15be82b3a0SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16be82b3a0SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17be82b3a0SEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18be82b3a0SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19be82b3a0SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20be82b3a0SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21be82b3a0SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22be82b3a0SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23be82b3a0SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24be82b3a0SEmmanuel Vadot  * SUCH DAMAGE.
25be82b3a0SEmmanuel Vadot  */
26be82b3a0SEmmanuel Vadot 
27be82b3a0SEmmanuel Vadot #include <sys/param.h>
28be82b3a0SEmmanuel Vadot #include <sys/systm.h>
29be82b3a0SEmmanuel Vadot #include <sys/kernel.h>
30be82b3a0SEmmanuel Vadot #include <sys/module.h>
31be82b3a0SEmmanuel Vadot #include <sys/bus.h>
32be82b3a0SEmmanuel Vadot 
33be82b3a0SEmmanuel Vadot #include <dev/fdt/simplebus.h>
34be82b3a0SEmmanuel Vadot 
35be82b3a0SEmmanuel Vadot #include <dev/ofw/openfirm.h>
36be82b3a0SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
37be82b3a0SEmmanuel Vadot 
38be82b3a0SEmmanuel Vadot struct ofw_clkbus_softc {
39be82b3a0SEmmanuel Vadot 	struct simplebus_softc simplebus_sc;
40be82b3a0SEmmanuel Vadot };
41be82b3a0SEmmanuel Vadot 
42be82b3a0SEmmanuel Vadot static int
43be82b3a0SEmmanuel Vadot ofw_clkbus_probe(device_t dev)
44be82b3a0SEmmanuel Vadot {
45be82b3a0SEmmanuel Vadot 	const char	*name;
46be82b3a0SEmmanuel Vadot 
47be82b3a0SEmmanuel Vadot 	name = ofw_bus_get_name(dev);
48be82b3a0SEmmanuel Vadot 
49be82b3a0SEmmanuel Vadot 	if (name == NULL || strcmp(name, "clocks") != 0)
50be82b3a0SEmmanuel Vadot 		return (ENXIO);
51be82b3a0SEmmanuel Vadot 
52be82b3a0SEmmanuel Vadot 	device_set_desc(dev, "OFW clocks bus");
53be82b3a0SEmmanuel Vadot 
54be82b3a0SEmmanuel Vadot 	return (BUS_PROBE_GENERIC);
55be82b3a0SEmmanuel Vadot }
56be82b3a0SEmmanuel Vadot 
57be82b3a0SEmmanuel Vadot static int
58be82b3a0SEmmanuel Vadot ofw_clkbus_attach(device_t dev)
59be82b3a0SEmmanuel Vadot {
60be82b3a0SEmmanuel Vadot 	phandle_t node, child;
61be82b3a0SEmmanuel Vadot 	device_t cdev;
62be82b3a0SEmmanuel Vadot 
63be82b3a0SEmmanuel Vadot 	node  = ofw_bus_get_node(dev);
64be82b3a0SEmmanuel Vadot 	simplebus_init(dev, node);
65be82b3a0SEmmanuel Vadot 
66be82b3a0SEmmanuel Vadot 	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
67be82b3a0SEmmanuel Vadot 		cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
68be82b3a0SEmmanuel Vadot 		if (cdev != NULL)
69be82b3a0SEmmanuel Vadot 			device_probe_and_attach(cdev);
70be82b3a0SEmmanuel Vadot 	}
71be82b3a0SEmmanuel Vadot 
72*18250ec6SJohn Baldwin 	bus_attach_children(dev);
73*18250ec6SJohn Baldwin 	return (0);
74be82b3a0SEmmanuel Vadot }
75be82b3a0SEmmanuel Vadot 
76be82b3a0SEmmanuel Vadot static device_method_t ofw_clkbus_methods[] = {
77be82b3a0SEmmanuel Vadot 	/* Device interface */
78be82b3a0SEmmanuel Vadot 	DEVMETHOD(device_probe,		ofw_clkbus_probe),
79be82b3a0SEmmanuel Vadot 	DEVMETHOD(device_attach,	ofw_clkbus_attach),
80be82b3a0SEmmanuel Vadot 
81be82b3a0SEmmanuel Vadot 	DEVMETHOD_END
82be82b3a0SEmmanuel Vadot };
83be82b3a0SEmmanuel Vadot 
84be82b3a0SEmmanuel Vadot DEFINE_CLASS_1(ofw_clkbus, ofw_clkbus_driver, ofw_clkbus_methods,
85be82b3a0SEmmanuel Vadot     sizeof(struct ofw_clkbus_softc), simplebus_driver);
86be82b3a0SEmmanuel Vadot EARLY_DRIVER_MODULE(ofw_clkbus, simplebus, ofw_clkbus_driver, 0, 0,
87be82b3a0SEmmanuel Vadot     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
88be82b3a0SEmmanuel Vadot MODULE_VERSION(ofw_clkbus, 1);
89