xref: /freebsd/sys/dev/altera/avgen/altera_avgen_nexus.c (revision f00e79c8470318cef8b419e33daa6c72b43f8fdb)
186f4a437SRobert Watson /*-
2*f00e79c8SRobert Watson  * Copyright (c) 2012-2013, 2016 Robert N. M. Watson
386f4a437SRobert Watson  * All rights reserved.
486f4a437SRobert Watson  *
586f4a437SRobert Watson  * This software was developed by SRI International and the University of
686f4a437SRobert Watson  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
786f4a437SRobert Watson  * ("CTSRD"), as part of the DARPA CRASH research programme.
886f4a437SRobert Watson  *
986f4a437SRobert Watson  * Redistribution and use in source and binary forms, with or without
1086f4a437SRobert Watson  * modification, are permitted provided that the following conditions
1186f4a437SRobert Watson  * are met:
1286f4a437SRobert Watson  * 1. Redistributions of source code must retain the above copyright
1386f4a437SRobert Watson  *    notice, this list of conditions and the following disclaimer.
1486f4a437SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
1586f4a437SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
1686f4a437SRobert Watson  *    documentation and/or other materials provided with the distribution.
1786f4a437SRobert Watson  *
1886f4a437SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1986f4a437SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2086f4a437SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2186f4a437SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2286f4a437SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2386f4a437SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2486f4a437SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2586f4a437SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2686f4a437SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2786f4a437SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2886f4a437SRobert Watson  * SUCH DAMAGE.
2986f4a437SRobert Watson  */
3086f4a437SRobert Watson 
3186f4a437SRobert Watson #include <sys/cdefs.h>
3286f4a437SRobert Watson __FBSDID("$FreeBSD$");
3386f4a437SRobert Watson 
3486f4a437SRobert Watson #include <sys/param.h>
3586f4a437SRobert Watson #include <sys/bus.h>
3686f4a437SRobert Watson #include <sys/condvar.h>
3786f4a437SRobert Watson #include <sys/conf.h>
3886f4a437SRobert Watson #include <sys/kernel.h>
3986f4a437SRobert Watson #include <sys/lock.h>
4086f4a437SRobert Watson #include <sys/malloc.h>
4186f4a437SRobert Watson #include <sys/module.h>
4286f4a437SRobert Watson #include <sys/mutex.h>
4386f4a437SRobert Watson #include <sys/rman.h>
4486f4a437SRobert Watson #include <sys/stat.h>
4586f4a437SRobert Watson #include <sys/systm.h>
4686f4a437SRobert Watson #include <sys/uio.h>
4786f4a437SRobert Watson 
4886f4a437SRobert Watson #include <machine/bus.h>
4986f4a437SRobert Watson #include <machine/resource.h>
5086f4a437SRobert Watson 
5186f4a437SRobert Watson #include <vm/vm.h>
5286f4a437SRobert Watson 
5386f4a437SRobert Watson #include <dev/altera/avgen/altera_avgen.h>
5486f4a437SRobert Watson 
5586f4a437SRobert Watson static int
5686f4a437SRobert Watson altera_avgen_nexus_probe(device_t dev)
5786f4a437SRobert Watson {
5886f4a437SRobert Watson 
5986f4a437SRobert Watson 	device_set_desc(dev, "Generic Altera Avalon device attachment");
60ccc1cefeSNathan Whitehorn 	return (BUS_PROBE_NOWILDCARD);
6186f4a437SRobert Watson }
6286f4a437SRobert Watson 
6386f4a437SRobert Watson static int
6486f4a437SRobert Watson altera_avgen_nexus_attach(device_t dev)
6586f4a437SRobert Watson {
6686f4a437SRobert Watson 	struct altera_avgen_softc *sc;
67*f00e79c8SRobert Watson 	const char *str_fileio, *str_geomio, *str_mmapio;
6886f4a437SRobert Watson 	const char *str_devname;
6986f4a437SRobert Watson 	int devunit, error;
7086f4a437SRobert Watson 
7186f4a437SRobert Watson 	sc = device_get_softc(dev);
7286f4a437SRobert Watson 	sc->avg_dev = dev;
7386f4a437SRobert Watson 	sc->avg_unit = device_get_unit(dev);
7486f4a437SRobert Watson 
7586f4a437SRobert Watson 	/*
7686f4a437SRobert Watson 	 * Query non-standard hints to find out what operations are permitted
7786f4a437SRobert Watson 	 * on the device, and whether it is cached.
7886f4a437SRobert Watson 	 */
7986f4a437SRobert Watson 	str_fileio = NULL;
80*f00e79c8SRobert Watson 	str_geomio = NULL;
8186f4a437SRobert Watson 	str_mmapio = NULL;
8286f4a437SRobert Watson 	str_devname = NULL;
8386f4a437SRobert Watson 	devunit = -1;
8486f4a437SRobert Watson 	sc->avg_width = 1;
8586f4a437SRobert Watson 	error = resource_int_value(device_get_name(dev), device_get_unit(dev),
8686f4a437SRobert Watson 	    ALTERA_AVALON_STR_WIDTH, &sc->avg_width);
8786f4a437SRobert Watson 	if (error != 0 && error != ENOENT) {
8886f4a437SRobert Watson 		device_printf(dev, "invalid %s\n", ALTERA_AVALON_STR_WIDTH);
8986f4a437SRobert Watson 		return (error);
9086f4a437SRobert Watson 	}
9186f4a437SRobert Watson 	(void)resource_string_value(device_get_name(dev),
9286f4a437SRobert Watson 	    device_get_unit(dev), ALTERA_AVALON_STR_FILEIO, &str_fileio);
9386f4a437SRobert Watson 	(void)resource_string_value(device_get_name(dev),
94*f00e79c8SRobert Watson 	    device_get_unit(dev), ALTERA_AVALON_STR_GEOMIO, &str_geomio);
95*f00e79c8SRobert Watson 	(void)resource_string_value(device_get_name(dev),
9686f4a437SRobert Watson 	    device_get_unit(dev), ALTERA_AVALON_STR_MMAPIO, &str_mmapio);
9786f4a437SRobert Watson 	(void)resource_string_value(device_get_name(dev),
9886f4a437SRobert Watson 	    device_get_unit(dev), ALTERA_AVALON_STR_DEVNAME, &str_devname);
9986f4a437SRobert Watson 	(void)resource_int_value(device_get_name(dev), device_get_unit(dev),
10086f4a437SRobert Watson 	    ALTERA_AVALON_STR_DEVUNIT, &devunit);
10186f4a437SRobert Watson 
10286f4a437SRobert Watson 	/* Memory allocation and checking. */
10386f4a437SRobert Watson 	sc->avg_rid = 0;
10486f4a437SRobert Watson 	sc->avg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
10586f4a437SRobert Watson 	    &sc->avg_rid, RF_ACTIVE);
10686f4a437SRobert Watson 	if (sc->avg_res == NULL) {
10786f4a437SRobert Watson 		device_printf(dev, "couldn't map memory\n");
10886f4a437SRobert Watson 		return (ENXIO);
10986f4a437SRobert Watson 	}
110*f00e79c8SRobert Watson 	error = altera_avgen_attach(sc, str_fileio, str_geomio, str_mmapio,
111*f00e79c8SRobert Watson 	    str_devname, devunit);
112b364a525SRobert Watson 	if (error != 0)
113b364a525SRobert Watson 		bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid,
114b364a525SRobert Watson 		    sc->avg_res);
11586f4a437SRobert Watson 	return (error);
11686f4a437SRobert Watson }
11786f4a437SRobert Watson 
11886f4a437SRobert Watson static int
11986f4a437SRobert Watson altera_avgen_nexus_detach(device_t dev)
12086f4a437SRobert Watson {
12186f4a437SRobert Watson 	struct altera_avgen_softc *sc;
12286f4a437SRobert Watson 
12386f4a437SRobert Watson 	sc = device_get_softc(dev);
124b364a525SRobert Watson 	altera_avgen_detach(sc);
12586f4a437SRobert Watson 	bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res);
12686f4a437SRobert Watson 	return (0);
12786f4a437SRobert Watson }
12886f4a437SRobert Watson 
12986f4a437SRobert Watson static device_method_t altera_avgen_nexus_methods[] = {
13086f4a437SRobert Watson 	DEVMETHOD(device_probe,		altera_avgen_nexus_probe),
13186f4a437SRobert Watson 	DEVMETHOD(device_attach,	altera_avgen_nexus_attach),
13286f4a437SRobert Watson 	DEVMETHOD(device_detach,	altera_avgen_nexus_detach),
13386f4a437SRobert Watson 	{ 0, 0 }
13486f4a437SRobert Watson };
13586f4a437SRobert Watson 
13686f4a437SRobert Watson static driver_t altera_avgen_nexus_driver = {
13786f4a437SRobert Watson 	"altera_avgen",
13886f4a437SRobert Watson 	altera_avgen_nexus_methods,
13986f4a437SRobert Watson 	sizeof(struct altera_avgen_softc),
14086f4a437SRobert Watson };
14186f4a437SRobert Watson 
14286f4a437SRobert Watson DRIVER_MODULE(avgen, nexus, altera_avgen_nexus_driver, altera_avgen_devclass,
14386f4a437SRobert Watson     0, 0);
144