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/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/fbio.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/rman.h>
38 #include <sys/resource.h>
39 #include <machine/bus.h>
40 #include <vm/vm.h>
41 #include <vm/vm_extern.h>
42 #include <vm/vm_kern.h>
43 #include <vm/pmap.h>
44
45 #include <dev/fdt/simplebus.h>
46
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49
50 #include <arm/ti/ti_omap4_cm.h>
51
52 static struct ofw_compat_data compat_data[] = {
53 { "ti,omap4-cm", 1 },
54 { NULL, 0 }
55 };
56
57 struct ti_omap4_cm_softc {
58 struct simplebus_softc sc;
59 device_t dev;
60 };
61
62 uint64_t
ti_omap4_cm_get_simplebus_base_host(device_t dev)63 ti_omap4_cm_get_simplebus_base_host(device_t dev) {
64 struct ti_omap4_cm_softc *sc;
65
66 sc = device_get_softc(dev);
67 if (sc->sc.nranges == 0)
68 return (0);
69
70 return (sc->sc.ranges[0].host);
71 }
72
73 static int ti_omap4_cm_probe(device_t dev);
74 static int ti_omap4_cm_attach(device_t dev);
75 static int ti_omap4_cm_detach(device_t dev);
76
77 static int
ti_omap4_cm_probe(device_t dev)78 ti_omap4_cm_probe(device_t dev)
79 {
80 if (!ofw_bus_status_okay(dev))
81 return (ENXIO);
82
83 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
84 return (ENXIO);
85
86 device_set_desc(dev, "TI OMAP4-CM");
87 if (!bootverbose)
88 device_quiet(dev);
89
90 return (BUS_PROBE_DEFAULT);
91 }
92
93 static int
ti_omap4_cm_attach(device_t dev)94 ti_omap4_cm_attach(device_t dev)
95 {
96 struct ti_omap4_cm_softc *sc;
97 device_t cdev;
98 phandle_t node, child;
99
100 sc = device_get_softc(dev);
101 sc->dev = dev;
102 node = ofw_bus_get_node(dev);
103
104 simplebus_init(dev, node);
105 if (simplebus_fill_ranges(node, &sc->sc) < 0) {
106 device_printf(dev, "could not get ranges\n");
107 return (ENXIO);
108 }
109
110 bus_generic_probe(sc->dev);
111
112 for (child = OF_child(node); child > 0; child = OF_peer(child)) {
113 cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
114 if (cdev != NULL)
115 device_probe_and_attach(cdev);
116 }
117
118 return (bus_generic_attach(dev));
119 }
120
121 static int
ti_omap4_cm_detach(device_t dev)122 ti_omap4_cm_detach(device_t dev)
123 {
124 return (EBUSY);
125 }
126
127 static device_method_t ti_omap4_cm_methods[] = {
128 /* Device interface */
129 DEVMETHOD(device_probe, ti_omap4_cm_probe),
130 DEVMETHOD(device_attach, ti_omap4_cm_attach),
131 DEVMETHOD(device_detach, ti_omap4_cm_detach),
132
133 DEVMETHOD_END
134 };
135
136 DEFINE_CLASS_1(ti_omap4_cm, ti_omap4_cm_driver, ti_omap4_cm_methods,
137 sizeof(struct ti_omap4_cm_softc), simplebus_driver);
138
139 EARLY_DRIVER_MODULE(ti_omap4_cm, simplebus, ti_omap4_cm_driver, 0, 0,
140 BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
141
142 EARLY_DRIVER_MODULE(ti_omap4_cm, ofwbus, ti_omap4_cm_driver, 0, 0,
143 BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
144