xref: /freebsd/sys/arm/ti/ti_omap4_cm.c (revision d0b2dbfa)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.org>
5  * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * Based on sys/arm/ti/ti_sysc.c
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/fbio.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/rman.h>
39 #include <sys/resource.h>
40 #include <machine/bus.h>
41 #include <vm/vm.h>
42 #include <vm/vm_extern.h>
43 #include <vm/vm_kern.h>
44 #include <vm/pmap.h>
45 
46 #include <dev/fdt/simplebus.h>
47 
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 
51 #include <arm/ti/ti_omap4_cm.h>
52 
53 static struct ofw_compat_data compat_data[] = {
54 	{ "ti,omap4-cm",	1 },
55 	{ NULL,				0 }
56 };
57 
58 struct ti_omap4_cm_softc {
59 	struct simplebus_softc	sc;
60 	device_t		dev;
61 };
62 
63 uint64_t
64 ti_omap4_cm_get_simplebus_base_host(device_t dev) {
65 	struct ti_omap4_cm_softc *sc;
66 
67 	sc = device_get_softc(dev);
68 	if (sc->sc.nranges == 0)
69 		return (0);
70 
71 	return (sc->sc.ranges[0].host);
72 }
73 
74 static int ti_omap4_cm_probe(device_t dev);
75 static int ti_omap4_cm_attach(device_t dev);
76 static int ti_omap4_cm_detach(device_t dev);
77 
78 static int
79 ti_omap4_cm_probe(device_t dev)
80 {
81 	if (!ofw_bus_status_okay(dev))
82 		return (ENXIO);
83 
84 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
85 		return (ENXIO);
86 
87 	device_set_desc(dev, "TI OMAP4-CM");
88 	if (!bootverbose)
89 		device_quiet(dev);
90 
91 	return (BUS_PROBE_DEFAULT);
92 }
93 
94 static int
95 ti_omap4_cm_attach(device_t dev)
96 {
97 	struct ti_omap4_cm_softc *sc;
98 	device_t cdev;
99 	phandle_t node, child;
100 
101 	sc = device_get_softc(dev);
102 	sc->dev = dev;
103 	node = ofw_bus_get_node(dev);
104 
105 	simplebus_init(dev, node);
106 	if (simplebus_fill_ranges(node, &sc->sc) < 0) {
107 		device_printf(dev, "could not get ranges\n");
108 		return (ENXIO);
109 	}
110 
111 	bus_generic_probe(sc->dev);
112 
113 	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
114 		cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
115 		if (cdev != NULL)
116 			device_probe_and_attach(cdev);
117 	}
118 
119 	return (bus_generic_attach(dev));
120 }
121 
122 static int
123 ti_omap4_cm_detach(device_t dev)
124 {
125 	return (EBUSY);
126 }
127 
128 static device_method_t ti_omap4_cm_methods[] = {
129 	/* Device interface */
130 	DEVMETHOD(device_probe,		ti_omap4_cm_probe),
131 	DEVMETHOD(device_attach,	ti_omap4_cm_attach),
132 	DEVMETHOD(device_detach,	ti_omap4_cm_detach),
133 
134 	DEVMETHOD_END
135 };
136 
137 DEFINE_CLASS_1(ti_omap4_cm, ti_omap4_cm_driver, ti_omap4_cm_methods,
138     sizeof(struct ti_omap4_cm_softc), simplebus_driver);
139 
140 EARLY_DRIVER_MODULE(ti_omap4_cm, simplebus, ti_omap4_cm_driver, 0, 0,
141     BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
142 
143 EARLY_DRIVER_MODULE(ti_omap4_cm, ofwbus, ti_omap4_cm_driver, 0, 0,
144     BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
145