186f4a437SRobert Watson /*- 2b364a525SRobert Watson * Copyright (c) 2012-2013 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 #include <machine/vm.h> 5186f4a437SRobert Watson 5286f4a437SRobert Watson #include <vm/vm.h> 5386f4a437SRobert Watson 5486f4a437SRobert Watson #include <dev/altera/avgen/altera_avgen.h> 5586f4a437SRobert Watson 5686f4a437SRobert Watson static int 5786f4a437SRobert Watson altera_avgen_nexus_probe(device_t dev) 5886f4a437SRobert Watson { 5986f4a437SRobert Watson 6086f4a437SRobert Watson device_set_desc(dev, "Generic Altera Avalon device attachment"); 61*ccc1cefeSNathan Whitehorn return (BUS_PROBE_NOWILDCARD); 6286f4a437SRobert Watson } 6386f4a437SRobert Watson 6486f4a437SRobert Watson static int 6586f4a437SRobert Watson altera_avgen_nexus_attach(device_t dev) 6686f4a437SRobert Watson { 6786f4a437SRobert Watson struct altera_avgen_softc *sc; 6886f4a437SRobert Watson const char *str_fileio, *str_mmapio; 6986f4a437SRobert Watson const char *str_devname; 7086f4a437SRobert Watson int devunit, error; 7186f4a437SRobert Watson 7286f4a437SRobert Watson sc = device_get_softc(dev); 7386f4a437SRobert Watson sc->avg_dev = dev; 7486f4a437SRobert Watson sc->avg_unit = device_get_unit(dev); 7586f4a437SRobert Watson 7686f4a437SRobert Watson /* 7786f4a437SRobert Watson * Query non-standard hints to find out what operations are permitted 7886f4a437SRobert Watson * on the device, and whether it is cached. 7986f4a437SRobert Watson */ 8086f4a437SRobert Watson str_fileio = 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), 9486f4a437SRobert Watson device_get_unit(dev), ALTERA_AVALON_STR_MMAPIO, &str_mmapio); 9586f4a437SRobert Watson (void)resource_string_value(device_get_name(dev), 9686f4a437SRobert Watson device_get_unit(dev), ALTERA_AVALON_STR_DEVNAME, &str_devname); 9786f4a437SRobert Watson (void)resource_int_value(device_get_name(dev), device_get_unit(dev), 9886f4a437SRobert Watson ALTERA_AVALON_STR_DEVUNIT, &devunit); 9986f4a437SRobert Watson 10086f4a437SRobert Watson /* Memory allocation and checking. */ 10186f4a437SRobert Watson sc->avg_rid = 0; 10286f4a437SRobert Watson sc->avg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 10386f4a437SRobert Watson &sc->avg_rid, RF_ACTIVE); 10486f4a437SRobert Watson if (sc->avg_res == NULL) { 10586f4a437SRobert Watson device_printf(dev, "couldn't map memory\n"); 10686f4a437SRobert Watson return (ENXIO); 10786f4a437SRobert Watson } 108b364a525SRobert Watson error = altera_avgen_attach(sc, str_fileio, str_mmapio, str_devname, 109b364a525SRobert Watson devunit); 110b364a525SRobert Watson if (error != 0) 111b364a525SRobert Watson bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, 112b364a525SRobert Watson sc->avg_res); 11386f4a437SRobert Watson return (error); 11486f4a437SRobert Watson } 11586f4a437SRobert Watson 11686f4a437SRobert Watson static int 11786f4a437SRobert Watson altera_avgen_nexus_detach(device_t dev) 11886f4a437SRobert Watson { 11986f4a437SRobert Watson struct altera_avgen_softc *sc; 12086f4a437SRobert Watson 12186f4a437SRobert Watson sc = device_get_softc(dev); 122b364a525SRobert Watson altera_avgen_detach(sc); 12386f4a437SRobert Watson bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res); 12486f4a437SRobert Watson return (0); 12586f4a437SRobert Watson } 12686f4a437SRobert Watson 12786f4a437SRobert Watson static device_method_t altera_avgen_nexus_methods[] = { 12886f4a437SRobert Watson DEVMETHOD(device_probe, altera_avgen_nexus_probe), 12986f4a437SRobert Watson DEVMETHOD(device_attach, altera_avgen_nexus_attach), 13086f4a437SRobert Watson DEVMETHOD(device_detach, altera_avgen_nexus_detach), 13186f4a437SRobert Watson { 0, 0 } 13286f4a437SRobert Watson }; 13386f4a437SRobert Watson 13486f4a437SRobert Watson static driver_t altera_avgen_nexus_driver = { 13586f4a437SRobert Watson "altera_avgen", 13686f4a437SRobert Watson altera_avgen_nexus_methods, 13786f4a437SRobert Watson sizeof(struct altera_avgen_softc), 13886f4a437SRobert Watson }; 13986f4a437SRobert Watson 14086f4a437SRobert Watson DRIVER_MODULE(avgen, nexus, altera_avgen_nexus_driver, altera_avgen_devclass, 14186f4a437SRobert Watson 0, 0); 142