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