1239674Srwatson/*-
2245376Srwatson * Copyright (c) 2012-2013 Robert N. M. Watson
3239674Srwatson * All rights reserved.
4239674Srwatson *
5239674Srwatson * This software was developed by SRI International and the University of
6239674Srwatson * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7239674Srwatson * ("CTSRD"), as part of the DARPA CRASH research programme.
8239674Srwatson *
9239674Srwatson * Redistribution and use in source and binary forms, with or without
10239674Srwatson * modification, are permitted provided that the following conditions
11239674Srwatson * are met:
12239674Srwatson * 1. Redistributions of source code must retain the above copyright
13239674Srwatson *    notice, this list of conditions and the following disclaimer.
14239674Srwatson * 2. Redistributions in binary form must reproduce the above copyright
15239674Srwatson *    notice, this list of conditions and the following disclaimer in the
16239674Srwatson *    documentation and/or other materials provided with the distribution.
17239674Srwatson *
18239674Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19239674Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20239674Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21239674Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22239674Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23239674Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24239674Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25239674Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26239674Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27239674Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28239674Srwatson * SUCH DAMAGE.
29239674Srwatson */
30239674Srwatson
31239674Srwatson#include <sys/cdefs.h>
32239674Srwatson__FBSDID("$FreeBSD: releng/10.3/sys/dev/altera/avgen/altera_avgen_nexus.c 265999 2014-05-14 01:35:43Z ian $");
33239674Srwatson
34239674Srwatson#include <sys/param.h>
35239674Srwatson#include <sys/bus.h>
36239674Srwatson#include <sys/condvar.h>
37239674Srwatson#include <sys/conf.h>
38239674Srwatson#include <sys/kernel.h>
39239674Srwatson#include <sys/lock.h>
40239674Srwatson#include <sys/malloc.h>
41239674Srwatson#include <sys/module.h>
42239674Srwatson#include <sys/mutex.h>
43239674Srwatson#include <sys/rman.h>
44239674Srwatson#include <sys/stat.h>
45239674Srwatson#include <sys/systm.h>
46239674Srwatson#include <sys/uio.h>
47239674Srwatson
48239674Srwatson#include <machine/bus.h>
49239674Srwatson#include <machine/resource.h>
50239674Srwatson#include <machine/vm.h>
51239674Srwatson
52239674Srwatson#include <vm/vm.h>
53239674Srwatson
54239674Srwatson#include <dev/altera/avgen/altera_avgen.h>
55239674Srwatson
56239674Srwatsonstatic int
57239674Srwatsonaltera_avgen_nexus_probe(device_t dev)
58239674Srwatson{
59239674Srwatson
60239674Srwatson	device_set_desc(dev, "Generic Altera Avalon device attachment");
61265999Sian	return (BUS_PROBE_NOWILDCARD);
62239674Srwatson}
63239674Srwatson
64239674Srwatsonstatic int
65239674Srwatsonaltera_avgen_nexus_attach(device_t dev)
66239674Srwatson{
67239674Srwatson	struct altera_avgen_softc *sc;
68239674Srwatson	const char *str_fileio, *str_mmapio;
69239674Srwatson	const char *str_devname;
70239674Srwatson	int devunit, error;
71239674Srwatson
72239674Srwatson	sc = device_get_softc(dev);
73239674Srwatson	sc->avg_dev = dev;
74239674Srwatson	sc->avg_unit = device_get_unit(dev);
75239674Srwatson
76239674Srwatson	/*
77239674Srwatson	 * Query non-standard hints to find out what operations are permitted
78239674Srwatson	 * on the device, and whether it is cached.
79239674Srwatson	 */
80239674Srwatson	str_fileio = NULL;
81239674Srwatson	str_mmapio = NULL;
82239674Srwatson	str_devname = NULL;
83239674Srwatson	devunit = -1;
84239674Srwatson	sc->avg_width = 1;
85239674Srwatson	error = resource_int_value(device_get_name(dev), device_get_unit(dev),
86239674Srwatson	    ALTERA_AVALON_STR_WIDTH, &sc->avg_width);
87239674Srwatson	if (error != 0 && error != ENOENT) {
88239674Srwatson		device_printf(dev, "invalid %s\n", ALTERA_AVALON_STR_WIDTH);
89239674Srwatson		return (error);
90239674Srwatson	}
91239674Srwatson	(void)resource_string_value(device_get_name(dev),
92239674Srwatson	    device_get_unit(dev), ALTERA_AVALON_STR_FILEIO, &str_fileio);
93239674Srwatson	(void)resource_string_value(device_get_name(dev),
94239674Srwatson	    device_get_unit(dev), ALTERA_AVALON_STR_MMAPIO, &str_mmapio);
95239674Srwatson	(void)resource_string_value(device_get_name(dev),
96239674Srwatson	    device_get_unit(dev), ALTERA_AVALON_STR_DEVNAME, &str_devname);
97239674Srwatson	(void)resource_int_value(device_get_name(dev), device_get_unit(dev),
98239674Srwatson	    ALTERA_AVALON_STR_DEVUNIT, &devunit);
99239674Srwatson
100239674Srwatson	/* Memory allocation and checking. */
101239674Srwatson	sc->avg_rid = 0;
102239674Srwatson	sc->avg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
103239674Srwatson	    &sc->avg_rid, RF_ACTIVE);
104239674Srwatson	if (sc->avg_res == NULL) {
105239674Srwatson		device_printf(dev, "couldn't map memory\n");
106239674Srwatson		return (ENXIO);
107239674Srwatson	}
108245376Srwatson	error = altera_avgen_attach(sc, str_fileio, str_mmapio, str_devname,
109245376Srwatson	    devunit);
110245376Srwatson	if (error != 0)
111245376Srwatson		bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid,
112245376Srwatson		    sc->avg_res);
113239674Srwatson	return (error);
114239674Srwatson}
115239674Srwatson
116239674Srwatsonstatic int
117239674Srwatsonaltera_avgen_nexus_detach(device_t dev)
118239674Srwatson{
119239674Srwatson	struct altera_avgen_softc *sc;
120239674Srwatson
121239674Srwatson	sc = device_get_softc(dev);
122245376Srwatson	altera_avgen_detach(sc);
123239674Srwatson	bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res);
124239674Srwatson	return (0);
125239674Srwatson}
126239674Srwatson
127239674Srwatsonstatic device_method_t altera_avgen_nexus_methods[] = {
128239674Srwatson	DEVMETHOD(device_probe,		altera_avgen_nexus_probe),
129239674Srwatson	DEVMETHOD(device_attach,	altera_avgen_nexus_attach),
130239674Srwatson	DEVMETHOD(device_detach,	altera_avgen_nexus_detach),
131239674Srwatson	{ 0, 0 }
132239674Srwatson};
133239674Srwatson
134239674Srwatsonstatic driver_t altera_avgen_nexus_driver = {
135239674Srwatson	"altera_avgen",
136239674Srwatson	altera_avgen_nexus_methods,
137239674Srwatson	sizeof(struct altera_avgen_softc),
138239674Srwatson};
139239674Srwatson
140239674SrwatsonDRIVER_MODULE(avgen, nexus, altera_avgen_nexus_driver, altera_avgen_devclass,
141239674Srwatson    0, 0);
142