10050ea24SMichal Meloun /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
30050ea24SMichal Meloun *
40050ea24SMichal Meloun * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.org>
50050ea24SMichal Meloun * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
60050ea24SMichal Meloun *
70050ea24SMichal Meloun * Redistribution and use in source and binary forms, with or without
80050ea24SMichal Meloun * modification, are permitted provided that the following conditions
90050ea24SMichal Meloun * are met:
100050ea24SMichal Meloun * 1. Redistributions of source code must retain the above copyright
110050ea24SMichal Meloun * notice, this list of conditions and the following disclaimer.
120050ea24SMichal Meloun * 2. Redistributions in binary form must reproduce the above copyright
130050ea24SMichal Meloun * notice, this list of conditions and the following disclaimer in the
140050ea24SMichal Meloun * documentation and/or other materials provided with the distribution.
150050ea24SMichal Meloun *
160050ea24SMichal Meloun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
170050ea24SMichal Meloun * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
180050ea24SMichal Meloun * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
190050ea24SMichal Meloun * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
200050ea24SMichal Meloun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
210050ea24SMichal Meloun * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
220050ea24SMichal Meloun * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
230050ea24SMichal Meloun * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
240050ea24SMichal Meloun * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250050ea24SMichal Meloun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260050ea24SMichal Meloun * SUCH DAMAGE.
270050ea24SMichal Meloun *
280050ea24SMichal Meloun * Based on sys/arm/ti/ti_sysc.c
290050ea24SMichal Meloun */
300050ea24SMichal Meloun
310050ea24SMichal Meloun #include <sys/param.h>
320050ea24SMichal Meloun #include <sys/systm.h>
330050ea24SMichal Meloun #include <sys/bus.h>
340050ea24SMichal Meloun #include <sys/fbio.h>
350050ea24SMichal Meloun #include <sys/kernel.h>
360050ea24SMichal Meloun #include <sys/module.h>
370050ea24SMichal Meloun #include <sys/rman.h>
380050ea24SMichal Meloun #include <sys/resource.h>
390050ea24SMichal Meloun #include <machine/bus.h>
400050ea24SMichal Meloun #include <vm/vm.h>
410050ea24SMichal Meloun #include <vm/vm_extern.h>
420050ea24SMichal Meloun #include <vm/vm_kern.h>
430050ea24SMichal Meloun #include <vm/pmap.h>
440050ea24SMichal Meloun
450050ea24SMichal Meloun #include <dev/fdt/simplebus.h>
460050ea24SMichal Meloun
470050ea24SMichal Meloun #include <dev/ofw/ofw_bus.h>
480050ea24SMichal Meloun #include <dev/ofw/ofw_bus_subr.h>
490050ea24SMichal Meloun
500050ea24SMichal Meloun #include <arm/ti/ti_omap4_cm.h>
510050ea24SMichal Meloun
520050ea24SMichal Meloun static struct ofw_compat_data compat_data[] = {
530050ea24SMichal Meloun { "ti,omap4-cm", 1 },
540050ea24SMichal Meloun { NULL, 0 }
550050ea24SMichal Meloun };
560050ea24SMichal Meloun
570050ea24SMichal Meloun struct ti_omap4_cm_softc {
580050ea24SMichal Meloun struct simplebus_softc sc;
590050ea24SMichal Meloun device_t dev;
600050ea24SMichal Meloun };
610050ea24SMichal Meloun
620050ea24SMichal Meloun uint64_t
ti_omap4_cm_get_simplebus_base_host(device_t dev)630050ea24SMichal Meloun ti_omap4_cm_get_simplebus_base_host(device_t dev) {
640050ea24SMichal Meloun struct ti_omap4_cm_softc *sc;
650050ea24SMichal Meloun
660050ea24SMichal Meloun sc = device_get_softc(dev);
670050ea24SMichal Meloun if (sc->sc.nranges == 0)
680050ea24SMichal Meloun return (0);
690050ea24SMichal Meloun
700050ea24SMichal Meloun return (sc->sc.ranges[0].host);
710050ea24SMichal Meloun }
720050ea24SMichal Meloun
730050ea24SMichal Meloun static int ti_omap4_cm_probe(device_t dev);
740050ea24SMichal Meloun static int ti_omap4_cm_attach(device_t dev);
750050ea24SMichal Meloun static int ti_omap4_cm_detach(device_t dev);
760050ea24SMichal Meloun
770050ea24SMichal Meloun static int
ti_omap4_cm_probe(device_t dev)780050ea24SMichal Meloun ti_omap4_cm_probe(device_t dev)
790050ea24SMichal Meloun {
800050ea24SMichal Meloun if (!ofw_bus_status_okay(dev))
810050ea24SMichal Meloun return (ENXIO);
820050ea24SMichal Meloun
830050ea24SMichal Meloun if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
840050ea24SMichal Meloun return (ENXIO);
850050ea24SMichal Meloun
860050ea24SMichal Meloun device_set_desc(dev, "TI OMAP4-CM");
870050ea24SMichal Meloun if (!bootverbose)
880050ea24SMichal Meloun device_quiet(dev);
890050ea24SMichal Meloun
900050ea24SMichal Meloun return (BUS_PROBE_DEFAULT);
910050ea24SMichal Meloun }
920050ea24SMichal Meloun
930050ea24SMichal Meloun static int
ti_omap4_cm_attach(device_t dev)940050ea24SMichal Meloun ti_omap4_cm_attach(device_t dev)
950050ea24SMichal Meloun {
960050ea24SMichal Meloun struct ti_omap4_cm_softc *sc;
970050ea24SMichal Meloun device_t cdev;
980050ea24SMichal Meloun phandle_t node, child;
990050ea24SMichal Meloun
1000050ea24SMichal Meloun sc = device_get_softc(dev);
1010050ea24SMichal Meloun sc->dev = dev;
1020050ea24SMichal Meloun node = ofw_bus_get_node(dev);
1030050ea24SMichal Meloun
1040050ea24SMichal Meloun simplebus_init(dev, node);
1050050ea24SMichal Meloun if (simplebus_fill_ranges(node, &sc->sc) < 0) {
1060050ea24SMichal Meloun device_printf(dev, "could not get ranges\n");
1070050ea24SMichal Meloun return (ENXIO);
1080050ea24SMichal Meloun }
1090050ea24SMichal Meloun
1100050ea24SMichal Meloun bus_generic_probe(sc->dev);
1110050ea24SMichal Meloun
1120050ea24SMichal Meloun for (child = OF_child(node); child > 0; child = OF_peer(child)) {
1130050ea24SMichal Meloun cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
1140050ea24SMichal Meloun if (cdev != NULL)
1150050ea24SMichal Meloun device_probe_and_attach(cdev);
1160050ea24SMichal Meloun }
1170050ea24SMichal Meloun
1180050ea24SMichal Meloun return (bus_generic_attach(dev));
1190050ea24SMichal Meloun }
1200050ea24SMichal Meloun
1210050ea24SMichal Meloun static int
ti_omap4_cm_detach(device_t dev)1220050ea24SMichal Meloun ti_omap4_cm_detach(device_t dev)
1230050ea24SMichal Meloun {
1240050ea24SMichal Meloun return (EBUSY);
1250050ea24SMichal Meloun }
1260050ea24SMichal Meloun
1270050ea24SMichal Meloun static device_method_t ti_omap4_cm_methods[] = {
1280050ea24SMichal Meloun /* Device interface */
1290050ea24SMichal Meloun DEVMETHOD(device_probe, ti_omap4_cm_probe),
1300050ea24SMichal Meloun DEVMETHOD(device_attach, ti_omap4_cm_attach),
1310050ea24SMichal Meloun DEVMETHOD(device_detach, ti_omap4_cm_detach),
1320050ea24SMichal Meloun
1330050ea24SMichal Meloun DEVMETHOD_END
1340050ea24SMichal Meloun };
1350050ea24SMichal Meloun
1360050ea24SMichal Meloun DEFINE_CLASS_1(ti_omap4_cm, ti_omap4_cm_driver, ti_omap4_cm_methods,
1370050ea24SMichal Meloun sizeof(struct ti_omap4_cm_softc), simplebus_driver);
1380050ea24SMichal Meloun
1398537e671SJohn Baldwin EARLY_DRIVER_MODULE(ti_omap4_cm, simplebus, ti_omap4_cm_driver, 0, 0,
1408537e671SJohn Baldwin BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
1410050ea24SMichal Meloun
1428537e671SJohn Baldwin EARLY_DRIVER_MODULE(ti_omap4_cm, ofwbus, ti_omap4_cm_driver, 0, 0,
1438537e671SJohn Baldwin BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
144