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/param.h>
3486f4a437SRobert Watson #include <sys/bus.h>
3586f4a437SRobert Watson #include <sys/condvar.h>
3686f4a437SRobert Watson #include <sys/conf.h>
3786f4a437SRobert Watson #include <sys/kernel.h>
3886f4a437SRobert Watson #include <sys/lock.h>
3986f4a437SRobert Watson #include <sys/malloc.h>
4086f4a437SRobert Watson #include <sys/module.h>
4186f4a437SRobert Watson #include <sys/mutex.h>
4286f4a437SRobert Watson #include <sys/rman.h>
4386f4a437SRobert Watson #include <sys/stat.h>
4486f4a437SRobert Watson #include <sys/systm.h>
4586f4a437SRobert Watson #include <sys/uio.h>
4686f4a437SRobert Watson
4786f4a437SRobert Watson #include <machine/bus.h>
4886f4a437SRobert Watson #include <machine/resource.h>
4986f4a437SRobert Watson
5086f4a437SRobert Watson #include <vm/vm.h>
5186f4a437SRobert Watson
5286f4a437SRobert Watson #include <dev/altera/avgen/altera_avgen.h>
5386f4a437SRobert Watson
5486f4a437SRobert Watson static int
altera_avgen_nexus_probe(device_t dev)5586f4a437SRobert Watson altera_avgen_nexus_probe(device_t dev)
5686f4a437SRobert Watson {
5786f4a437SRobert Watson
5886f4a437SRobert Watson device_set_desc(dev, "Generic Altera Avalon device attachment");
59ccc1cefeSNathan Whitehorn return (BUS_PROBE_NOWILDCARD);
6086f4a437SRobert Watson }
6186f4a437SRobert Watson
6286f4a437SRobert Watson static int
altera_avgen_nexus_attach(device_t dev)6386f4a437SRobert Watson altera_avgen_nexus_attach(device_t dev)
6486f4a437SRobert Watson {
6586f4a437SRobert Watson struct altera_avgen_softc *sc;
66f00e79c8SRobert Watson const char *str_fileio, *str_geomio, *str_mmapio;
6786f4a437SRobert Watson const char *str_devname;
6886f4a437SRobert Watson int devunit, error;
6986f4a437SRobert Watson
7086f4a437SRobert Watson sc = device_get_softc(dev);
7186f4a437SRobert Watson sc->avg_dev = dev;
7286f4a437SRobert Watson sc->avg_unit = device_get_unit(dev);
7386f4a437SRobert Watson
7486f4a437SRobert Watson /*
7586f4a437SRobert Watson * Query non-standard hints to find out what operations are permitted
7686f4a437SRobert Watson * on the device, and whether it is cached.
7786f4a437SRobert Watson */
7886f4a437SRobert Watson str_fileio = NULL;
79f00e79c8SRobert Watson str_geomio = NULL;
8086f4a437SRobert Watson str_mmapio = NULL;
8186f4a437SRobert Watson str_devname = NULL;
8286f4a437SRobert Watson devunit = -1;
8386f4a437SRobert Watson sc->avg_width = 1;
8486f4a437SRobert Watson error = resource_int_value(device_get_name(dev), device_get_unit(dev),
8586f4a437SRobert Watson ALTERA_AVALON_STR_WIDTH, &sc->avg_width);
8686f4a437SRobert Watson if (error != 0 && error != ENOENT) {
8786f4a437SRobert Watson device_printf(dev, "invalid %s\n", ALTERA_AVALON_STR_WIDTH);
8886f4a437SRobert Watson return (error);
8986f4a437SRobert Watson }
9086f4a437SRobert Watson (void)resource_string_value(device_get_name(dev),
9186f4a437SRobert Watson device_get_unit(dev), ALTERA_AVALON_STR_FILEIO, &str_fileio);
9286f4a437SRobert Watson (void)resource_string_value(device_get_name(dev),
93f00e79c8SRobert Watson device_get_unit(dev), ALTERA_AVALON_STR_GEOMIO, &str_geomio);
94f00e79c8SRobert Watson (void)resource_string_value(device_get_name(dev),
9586f4a437SRobert Watson device_get_unit(dev), ALTERA_AVALON_STR_MMAPIO, &str_mmapio);
9686f4a437SRobert Watson (void)resource_string_value(device_get_name(dev),
9786f4a437SRobert Watson device_get_unit(dev), ALTERA_AVALON_STR_DEVNAME, &str_devname);
9886f4a437SRobert Watson (void)resource_int_value(device_get_name(dev), device_get_unit(dev),
9986f4a437SRobert Watson ALTERA_AVALON_STR_DEVUNIT, &devunit);
10086f4a437SRobert Watson
10186f4a437SRobert Watson /* Memory allocation and checking. */
10286f4a437SRobert Watson sc->avg_rid = 0;
10386f4a437SRobert Watson sc->avg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
10486f4a437SRobert Watson &sc->avg_rid, RF_ACTIVE);
10586f4a437SRobert Watson if (sc->avg_res == NULL) {
10686f4a437SRobert Watson device_printf(dev, "couldn't map memory\n");
10786f4a437SRobert Watson return (ENXIO);
10886f4a437SRobert Watson }
109f00e79c8SRobert Watson error = altera_avgen_attach(sc, str_fileio, str_geomio, str_mmapio,
110f00e79c8SRobert Watson str_devname, devunit);
111b364a525SRobert Watson if (error != 0)
112b364a525SRobert Watson bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid,
113b364a525SRobert Watson sc->avg_res);
11486f4a437SRobert Watson return (error);
11586f4a437SRobert Watson }
11686f4a437SRobert Watson
11786f4a437SRobert Watson static int
altera_avgen_nexus_detach(device_t dev)11886f4a437SRobert Watson altera_avgen_nexus_detach(device_t dev)
11986f4a437SRobert Watson {
12086f4a437SRobert Watson struct altera_avgen_softc *sc;
12186f4a437SRobert Watson
12286f4a437SRobert Watson sc = device_get_softc(dev);
123b364a525SRobert Watson altera_avgen_detach(sc);
12486f4a437SRobert Watson bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res);
12586f4a437SRobert Watson return (0);
12686f4a437SRobert Watson }
12786f4a437SRobert Watson
12886f4a437SRobert Watson static device_method_t altera_avgen_nexus_methods[] = {
12986f4a437SRobert Watson DEVMETHOD(device_probe, altera_avgen_nexus_probe),
13086f4a437SRobert Watson DEVMETHOD(device_attach, altera_avgen_nexus_attach),
13186f4a437SRobert Watson DEVMETHOD(device_detach, altera_avgen_nexus_detach),
13286f4a437SRobert Watson { 0, 0 }
13386f4a437SRobert Watson };
13486f4a437SRobert Watson
13586f4a437SRobert Watson static driver_t altera_avgen_nexus_driver = {
13686f4a437SRobert Watson "altera_avgen",
13786f4a437SRobert Watson altera_avgen_nexus_methods,
13886f4a437SRobert Watson sizeof(struct altera_avgen_softc),
13986f4a437SRobert Watson };
14086f4a437SRobert Watson
141b08b5f55SJohn Baldwin DRIVER_MODULE(avgen, nexus, altera_avgen_nexus_driver, 0, 0);
142