xref: /freebsd/sys/dev/altera/avgen/altera_avgen_fdt.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
11925f295SRobert Watson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4f00e79c8SRobert Watson  * Copyright (c) 2012-2013, 2016 Robert N. M. Watson
51925f295SRobert Watson  * All rights reserved.
61925f295SRobert Watson  *
71925f295SRobert Watson  * This software was developed by SRI International and the University of
81925f295SRobert Watson  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
91925f295SRobert Watson  * ("CTSRD"), as part of the DARPA CRASH research programme.
101925f295SRobert Watson  *
111925f295SRobert Watson  * Redistribution and use in source and binary forms, with or without
121925f295SRobert Watson  * modification, are permitted provided that the following conditions
131925f295SRobert Watson  * are met:
141925f295SRobert Watson  * 1. Redistributions of source code must retain the above copyright
151925f295SRobert Watson  *    notice, this list of conditions and the following disclaimer.
161925f295SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
171925f295SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
181925f295SRobert Watson  *    documentation and/or other materials provided with the distribution.
191925f295SRobert Watson  *
201925f295SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
211925f295SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221925f295SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231925f295SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
241925f295SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251925f295SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261925f295SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271925f295SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281925f295SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291925f295SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301925f295SRobert Watson  * SUCH DAMAGE.
311925f295SRobert Watson  */
321925f295SRobert Watson 
331925f295SRobert Watson #include <sys/param.h>
341925f295SRobert Watson #include <sys/bus.h>
351925f295SRobert Watson #include <sys/condvar.h>
361925f295SRobert Watson #include <sys/conf.h>
371925f295SRobert Watson #include <sys/kernel.h>
381925f295SRobert Watson #include <sys/lock.h>
391925f295SRobert Watson #include <sys/malloc.h>
401925f295SRobert Watson #include <sys/module.h>
411925f295SRobert Watson #include <sys/mutex.h>
421925f295SRobert Watson #include <sys/rman.h>
431925f295SRobert Watson #include <sys/stat.h>
441925f295SRobert Watson #include <sys/systm.h>
451925f295SRobert Watson #include <sys/uio.h>
461925f295SRobert Watson 
471925f295SRobert Watson #include <machine/bus.h>
481925f295SRobert Watson #include <machine/resource.h>
491925f295SRobert Watson 
501925f295SRobert Watson #include <vm/vm.h>
511925f295SRobert Watson 
520a349699SRobert Watson #include <dev/fdt/fdt_common.h>
530a349699SRobert Watson #include <dev/ofw/openfirm.h>
540a349699SRobert Watson #include <dev/ofw/ofw_bus.h>
550a349699SRobert Watson #include <dev/ofw/ofw_bus_subr.h>
560a349699SRobert Watson 
571925f295SRobert Watson #include <dev/altera/avgen/altera_avgen.h>
581925f295SRobert Watson 
591925f295SRobert Watson static int
altera_avgen_fdt_probe(device_t dev)600a349699SRobert Watson altera_avgen_fdt_probe(device_t dev)
611925f295SRobert Watson {
621925f295SRobert Watson 
63add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
64add35ed5SIan Lepore 		return (ENXIO);
65add35ed5SIan Lepore 
660a349699SRobert Watson 	if (ofw_bus_is_compatible(dev, "sri-cambridge,avgen")) {
671925f295SRobert Watson 		device_set_desc(dev, "Generic Altera Avalon device attachment");
681925f295SRobert Watson 		return (BUS_PROBE_DEFAULT);
691925f295SRobert Watson 	}
700a349699SRobert Watson 	return (ENXIO);
710a349699SRobert Watson }
721925f295SRobert Watson 
731925f295SRobert Watson static int
altera_avgen_fdt_attach(device_t dev)740a349699SRobert Watson altera_avgen_fdt_attach(device_t dev)
751925f295SRobert Watson {
761925f295SRobert Watson 	struct altera_avgen_softc *sc;
77f00e79c8SRobert Watson 	char *str_fileio, *str_geomio, *str_mmapio;
780a349699SRobert Watson 	char *str_devname;
790a349699SRobert Watson 	phandle_t node;
800a349699SRobert Watson 	pcell_t cell;
811925f295SRobert Watson 	int devunit, error;
821925f295SRobert Watson 
831925f295SRobert Watson 	sc = device_get_softc(dev);
841925f295SRobert Watson 	sc->avg_dev = dev;
851925f295SRobert Watson 	sc->avg_unit = device_get_unit(dev);
861925f295SRobert Watson 
871925f295SRobert Watson 	/*
880a349699SRobert Watson 	 * Query driver-specific OpenFirmware properties to determine how to
890a349699SRobert Watson 	 * expose the device via /dev.
901925f295SRobert Watson 	 */
911925f295SRobert Watson 	str_fileio = NULL;
92f00e79c8SRobert Watson 	str_geomio = NULL;
931925f295SRobert Watson 	str_mmapio = NULL;
941925f295SRobert Watson 	str_devname = NULL;
951925f295SRobert Watson 	devunit = -1;
961925f295SRobert Watson 	sc->avg_width = 1;
970a349699SRobert Watson 	node = ofw_bus_get_node(dev);
980a349699SRobert Watson 	if (OF_getprop(node, "sri-cambridge,width", &cell, sizeof(cell)) > 0)
990a349699SRobert Watson 		sc->avg_width = cell;
100217d17bcSOleksandr Tymoshenko 	(void)OF_getprop_alloc(node, "sri-cambridge,fileio",
1010a349699SRobert Watson 	    (void **)&str_fileio);
102217d17bcSOleksandr Tymoshenko 	(void)OF_getprop_alloc(node, "sri-cambridge,geomio",
103f00e79c8SRobert Watson 	    (void **)&str_geomio);
104217d17bcSOleksandr Tymoshenko 	(void)OF_getprop_alloc(node, "sri-cambridge,mmapio",
1050a349699SRobert Watson 	    (void **)&str_mmapio);
106217d17bcSOleksandr Tymoshenko 	(void)OF_getprop_alloc(node,  "sri-cambridge,devname",
1070a349699SRobert Watson 	    (void **)&str_devname);
1080a349699SRobert Watson 	if (OF_getprop(node, "sri-cambridge,devunit", &cell, sizeof(cell)) > 0)
1090a349699SRobert Watson 		devunit = cell;
1101925f295SRobert Watson 
1111925f295SRobert Watson 	/* Memory allocation and checking. */
1121925f295SRobert Watson 	sc->avg_rid = 0;
1131925f295SRobert Watson 	sc->avg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1141925f295SRobert Watson 	    &sc->avg_rid, RF_ACTIVE);
1151925f295SRobert Watson 	if (sc->avg_res == NULL) {
1161925f295SRobert Watson 		device_printf(dev, "couldn't map memory\n");
1171925f295SRobert Watson 		return (ENXIO);
1181925f295SRobert Watson 	}
119f00e79c8SRobert Watson 	error = altera_avgen_attach(sc, str_fileio, str_geomio, str_mmapio,
120f00e79c8SRobert Watson 	    str_devname, devunit);
1211925f295SRobert Watson 	if (error != 0)
1221925f295SRobert Watson 		bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid,
1231925f295SRobert Watson 		    sc->avg_res);
1240a349699SRobert Watson 	if (str_fileio != NULL)
12527b917c8SOleksandr Tymoshenko 		OF_prop_free(str_fileio);
126f00e79c8SRobert Watson 	if (str_geomio != NULL)
127f00e79c8SRobert Watson 		OF_prop_free(str_geomio);
1280a349699SRobert Watson 	if (str_mmapio != NULL)
12927b917c8SOleksandr Tymoshenko 		OF_prop_free(str_mmapio);
1300a349699SRobert Watson 	if (str_devname != NULL)
13127b917c8SOleksandr Tymoshenko 		OF_prop_free(str_devname);
1321925f295SRobert Watson 	return (error);
1331925f295SRobert Watson }
1341925f295SRobert Watson 
1351925f295SRobert Watson static int
altera_avgen_fdt_detach(device_t dev)1360a349699SRobert Watson altera_avgen_fdt_detach(device_t dev)
1371925f295SRobert Watson {
1381925f295SRobert Watson 	struct altera_avgen_softc *sc;
1391925f295SRobert Watson 
1401925f295SRobert Watson 	sc = device_get_softc(dev);
1411925f295SRobert Watson 	altera_avgen_detach(sc);
1421925f295SRobert Watson 	bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res);
1431925f295SRobert Watson 	return (0);
1441925f295SRobert Watson }
1451925f295SRobert Watson 
1460a349699SRobert Watson static device_method_t altera_avgen_fdt_methods[] = {
1470a349699SRobert Watson 	DEVMETHOD(device_probe,		altera_avgen_fdt_probe),
1480a349699SRobert Watson 	DEVMETHOD(device_attach,	altera_avgen_fdt_attach),
1490a349699SRobert Watson 	DEVMETHOD(device_detach,	altera_avgen_fdt_detach),
1501925f295SRobert Watson 	{ 0, 0 }
1511925f295SRobert Watson };
1521925f295SRobert Watson 
1530a349699SRobert Watson static driver_t altera_avgen_fdt_driver = {
1541925f295SRobert Watson 	"altera_avgen",
1550a349699SRobert Watson 	altera_avgen_fdt_methods,
1561925f295SRobert Watson 	sizeof(struct altera_avgen_softc),
1571925f295SRobert Watson };
1581925f295SRobert Watson 
159b08b5f55SJohn Baldwin DRIVER_MODULE(avgen, simplebus, altera_avgen_fdt_driver, 0, 0);
160