xref: /freebsd/sys/dev/altera/avgen/altera_avgen_fdt.c (revision add35ed5b81bd56b4e0f1f9db0636b785cc0c8d1)
11925f295SRobert Watson /*-
21925f295SRobert Watson  * Copyright (c) 2012-2013 Robert N. M. Watson
31925f295SRobert Watson  * All rights reserved.
41925f295SRobert Watson  *
51925f295SRobert Watson  * This software was developed by SRI International and the University of
61925f295SRobert Watson  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
71925f295SRobert Watson  * ("CTSRD"), as part of the DARPA CRASH research programme.
81925f295SRobert Watson  *
91925f295SRobert Watson  * Redistribution and use in source and binary forms, with or without
101925f295SRobert Watson  * modification, are permitted provided that the following conditions
111925f295SRobert Watson  * are met:
121925f295SRobert Watson  * 1. Redistributions of source code must retain the above copyright
131925f295SRobert Watson  *    notice, this list of conditions and the following disclaimer.
141925f295SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
151925f295SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
161925f295SRobert Watson  *    documentation and/or other materials provided with the distribution.
171925f295SRobert Watson  *
181925f295SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
191925f295SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201925f295SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211925f295SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
221925f295SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231925f295SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241925f295SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251925f295SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261925f295SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271925f295SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281925f295SRobert Watson  * SUCH DAMAGE.
291925f295SRobert Watson  */
301925f295SRobert Watson 
311925f295SRobert Watson #include <sys/cdefs.h>
321925f295SRobert Watson __FBSDID("$FreeBSD$");
331925f295SRobert Watson 
341925f295SRobert Watson #include <sys/param.h>
351925f295SRobert Watson #include <sys/bus.h>
361925f295SRobert Watson #include <sys/condvar.h>
371925f295SRobert Watson #include <sys/conf.h>
381925f295SRobert Watson #include <sys/kernel.h>
391925f295SRobert Watson #include <sys/lock.h>
401925f295SRobert Watson #include <sys/malloc.h>
411925f295SRobert Watson #include <sys/module.h>
421925f295SRobert Watson #include <sys/mutex.h>
431925f295SRobert Watson #include <sys/rman.h>
441925f295SRobert Watson #include <sys/stat.h>
451925f295SRobert Watson #include <sys/systm.h>
461925f295SRobert Watson #include <sys/uio.h>
471925f295SRobert Watson 
481925f295SRobert Watson #include <machine/bus.h>
491925f295SRobert Watson #include <machine/resource.h>
501925f295SRobert Watson #include <machine/vm.h>
511925f295SRobert Watson 
521925f295SRobert Watson #include <vm/vm.h>
531925f295SRobert Watson 
540a349699SRobert Watson #include <dev/fdt/fdt_common.h>
550a349699SRobert Watson #include <dev/ofw/openfirm.h>
560a349699SRobert Watson #include <dev/ofw/ofw_bus.h>
570a349699SRobert Watson #include <dev/ofw/ofw_bus_subr.h>
580a349699SRobert Watson 
591925f295SRobert Watson #include <dev/altera/avgen/altera_avgen.h>
601925f295SRobert Watson 
611925f295SRobert Watson static int
620a349699SRobert Watson altera_avgen_fdt_probe(device_t dev)
631925f295SRobert Watson {
641925f295SRobert Watson 
65*add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
66*add35ed5SIan Lepore 		return (ENXIO);
67*add35ed5SIan Lepore 
680a349699SRobert Watson 	if (ofw_bus_is_compatible(dev, "sri-cambridge,avgen")) {
691925f295SRobert Watson 		device_set_desc(dev, "Generic Altera Avalon device attachment");
701925f295SRobert Watson 		return (BUS_PROBE_DEFAULT);
711925f295SRobert Watson 	}
720a349699SRobert Watson 	return (ENXIO);
730a349699SRobert Watson }
741925f295SRobert Watson 
751925f295SRobert Watson static int
760a349699SRobert Watson altera_avgen_fdt_attach(device_t dev)
771925f295SRobert Watson {
781925f295SRobert Watson 	struct altera_avgen_softc *sc;
790a349699SRobert Watson 	char *str_fileio, *str_mmapio;
800a349699SRobert Watson 	char *str_devname;
810a349699SRobert Watson 	phandle_t node;
820a349699SRobert Watson 	pcell_t cell;
831925f295SRobert Watson 	int devunit, error;
841925f295SRobert Watson 
851925f295SRobert Watson 	sc = device_get_softc(dev);
861925f295SRobert Watson 	sc->avg_dev = dev;
871925f295SRobert Watson 	sc->avg_unit = device_get_unit(dev);
881925f295SRobert Watson 
891925f295SRobert Watson 	/*
900a349699SRobert Watson 	 * Query driver-specific OpenFirmware properties to determine how to
910a349699SRobert Watson 	 * expose the device via /dev.
921925f295SRobert Watson 	 */
931925f295SRobert Watson 	str_fileio = NULL;
941925f295SRobert Watson 	str_mmapio = NULL;
951925f295SRobert Watson 	str_devname = NULL;
961925f295SRobert Watson 	devunit = -1;
971925f295SRobert Watson 	sc->avg_width = 1;
980a349699SRobert Watson 	node = ofw_bus_get_node(dev);
990a349699SRobert Watson 	if (OF_getprop(node, "sri-cambridge,width", &cell, sizeof(cell)) > 0)
1000a349699SRobert Watson 		sc->avg_width = cell;
1010a349699SRobert Watson 	(void)OF_getprop_alloc(node, "sri-cambridge,fileio", sizeof(char),
1020a349699SRobert Watson 	    (void **)&str_fileio);
1030a349699SRobert Watson 	(void)OF_getprop_alloc(node, "sri-cambridge,mmapio", sizeof(char),
1040a349699SRobert Watson 	    (void **)&str_mmapio);
1050a349699SRobert Watson 	(void)OF_getprop_alloc(node,  "sri-cambridge,devname", sizeof(char),
1060a349699SRobert Watson 	    (void **)&str_devname);
1070a349699SRobert Watson 	if (OF_getprop(node, "sri-cambridge,devunit", &cell, sizeof(cell)) > 0)
1080a349699SRobert Watson 		devunit = cell;
1091925f295SRobert Watson 
1101925f295SRobert Watson 	/* Memory allocation and checking. */
1111925f295SRobert Watson 	sc->avg_rid = 0;
1121925f295SRobert Watson 	sc->avg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1131925f295SRobert Watson 	    &sc->avg_rid, RF_ACTIVE);
1141925f295SRobert Watson 	if (sc->avg_res == NULL) {
1151925f295SRobert Watson 		device_printf(dev, "couldn't map memory\n");
1161925f295SRobert Watson 		return (ENXIO);
1171925f295SRobert Watson 	}
1181925f295SRobert Watson 	error = altera_avgen_attach(sc, str_fileio, str_mmapio, str_devname,
1191925f295SRobert Watson 	    devunit);
1201925f295SRobert Watson 	if (error != 0)
1211925f295SRobert Watson 		bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid,
1221925f295SRobert Watson 		    sc->avg_res);
1230a349699SRobert Watson 	if (str_fileio != NULL)
1240a349699SRobert Watson 		free(str_fileio, M_OFWPROP);
1250a349699SRobert Watson 	if (str_mmapio != NULL)
1260a349699SRobert Watson 		free(str_mmapio, M_OFWPROP);
1270a349699SRobert Watson 	if (str_devname != NULL)
1280a349699SRobert Watson 		free(str_devname, M_OFWPROP);
1291925f295SRobert Watson 	return (error);
1301925f295SRobert Watson }
1311925f295SRobert Watson 
1321925f295SRobert Watson static int
1330a349699SRobert Watson altera_avgen_fdt_detach(device_t dev)
1341925f295SRobert Watson {
1351925f295SRobert Watson 	struct altera_avgen_softc *sc;
1361925f295SRobert Watson 
1371925f295SRobert Watson 	sc = device_get_softc(dev);
1381925f295SRobert Watson 	altera_avgen_detach(sc);
1391925f295SRobert Watson 	bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res);
1401925f295SRobert Watson 	return (0);
1411925f295SRobert Watson }
1421925f295SRobert Watson 
1430a349699SRobert Watson static device_method_t altera_avgen_fdt_methods[] = {
1440a349699SRobert Watson 	DEVMETHOD(device_probe,		altera_avgen_fdt_probe),
1450a349699SRobert Watson 	DEVMETHOD(device_attach,	altera_avgen_fdt_attach),
1460a349699SRobert Watson 	DEVMETHOD(device_detach,	altera_avgen_fdt_detach),
1471925f295SRobert Watson 	{ 0, 0 }
1481925f295SRobert Watson };
1491925f295SRobert Watson 
1500a349699SRobert Watson static driver_t altera_avgen_fdt_driver = {
1511925f295SRobert Watson 	"altera_avgen",
1520a349699SRobert Watson 	altera_avgen_fdt_methods,
1531925f295SRobert Watson 	sizeof(struct altera_avgen_softc),
1541925f295SRobert Watson };
1551925f295SRobert Watson 
1560a349699SRobert Watson DRIVER_MODULE(avgen, simplebus, altera_avgen_fdt_driver,
1570a349699SRobert Watson     altera_avgen_devclass, 0, 0);
158