xref: /freebsd/sys/dev/altera/avgen/altera_avgen_fdt.c (revision 1925f2953741ff7f43e49243cde1e0fbfc41d50d)
1*1925f295SRobert Watson /*-
2*1925f295SRobert Watson  * Copyright (c) 2012-2013 Robert N. M. Watson
3*1925f295SRobert Watson  * All rights reserved.
4*1925f295SRobert Watson  *
5*1925f295SRobert Watson  * This software was developed by SRI International and the University of
6*1925f295SRobert Watson  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7*1925f295SRobert Watson  * ("CTSRD"), as part of the DARPA CRASH research programme.
8*1925f295SRobert Watson  *
9*1925f295SRobert Watson  * Redistribution and use in source and binary forms, with or without
10*1925f295SRobert Watson  * modification, are permitted provided that the following conditions
11*1925f295SRobert Watson  * are met:
12*1925f295SRobert Watson  * 1. Redistributions of source code must retain the above copyright
13*1925f295SRobert Watson  *    notice, this list of conditions and the following disclaimer.
14*1925f295SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
15*1925f295SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
16*1925f295SRobert Watson  *    documentation and/or other materials provided with the distribution.
17*1925f295SRobert Watson  *
18*1925f295SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*1925f295SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*1925f295SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*1925f295SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*1925f295SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*1925f295SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*1925f295SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*1925f295SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*1925f295SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*1925f295SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*1925f295SRobert Watson  * SUCH DAMAGE.
29*1925f295SRobert Watson  */
30*1925f295SRobert Watson 
31*1925f295SRobert Watson #include <sys/cdefs.h>
32*1925f295SRobert Watson __FBSDID("$FreeBSD$");
33*1925f295SRobert Watson 
34*1925f295SRobert Watson #include <sys/param.h>
35*1925f295SRobert Watson #include <sys/bus.h>
36*1925f295SRobert Watson #include <sys/condvar.h>
37*1925f295SRobert Watson #include <sys/conf.h>
38*1925f295SRobert Watson #include <sys/kernel.h>
39*1925f295SRobert Watson #include <sys/lock.h>
40*1925f295SRobert Watson #include <sys/malloc.h>
41*1925f295SRobert Watson #include <sys/module.h>
42*1925f295SRobert Watson #include <sys/mutex.h>
43*1925f295SRobert Watson #include <sys/rman.h>
44*1925f295SRobert Watson #include <sys/stat.h>
45*1925f295SRobert Watson #include <sys/systm.h>
46*1925f295SRobert Watson #include <sys/uio.h>
47*1925f295SRobert Watson 
48*1925f295SRobert Watson #include <machine/bus.h>
49*1925f295SRobert Watson #include <machine/resource.h>
50*1925f295SRobert Watson #include <machine/vm.h>
51*1925f295SRobert Watson 
52*1925f295SRobert Watson #include <vm/vm.h>
53*1925f295SRobert Watson 
54*1925f295SRobert Watson #include <dev/altera/avgen/altera_avgen.h>
55*1925f295SRobert Watson 
56*1925f295SRobert Watson static int
57*1925f295SRobert Watson altera_avgen_nexus_probe(device_t dev)
58*1925f295SRobert Watson {
59*1925f295SRobert Watson 
60*1925f295SRobert Watson 	device_set_desc(dev, "Generic Altera Avalon device attachment");
61*1925f295SRobert Watson 	return (BUS_PROBE_DEFAULT);
62*1925f295SRobert Watson }
63*1925f295SRobert Watson 
64*1925f295SRobert Watson static int
65*1925f295SRobert Watson altera_avgen_nexus_attach(device_t dev)
66*1925f295SRobert Watson {
67*1925f295SRobert Watson 	struct altera_avgen_softc *sc;
68*1925f295SRobert Watson 	const char *str_fileio, *str_mmapio;
69*1925f295SRobert Watson 	const char *str_devname;
70*1925f295SRobert Watson 	int devunit, error;
71*1925f295SRobert Watson 
72*1925f295SRobert Watson 	sc = device_get_softc(dev);
73*1925f295SRobert Watson 	sc->avg_dev = dev;
74*1925f295SRobert Watson 	sc->avg_unit = device_get_unit(dev);
75*1925f295SRobert Watson 
76*1925f295SRobert Watson 	/*
77*1925f295SRobert Watson 	 * Query non-standard hints to find out what operations are permitted
78*1925f295SRobert Watson 	 * on the device, and whether it is cached.
79*1925f295SRobert Watson 	 */
80*1925f295SRobert Watson 	str_fileio = NULL;
81*1925f295SRobert Watson 	str_mmapio = NULL;
82*1925f295SRobert Watson 	str_devname = NULL;
83*1925f295SRobert Watson 	devunit = -1;
84*1925f295SRobert Watson 	sc->avg_width = 1;
85*1925f295SRobert Watson 	error = resource_int_value(device_get_name(dev), device_get_unit(dev),
86*1925f295SRobert Watson 	    ALTERA_AVALON_STR_WIDTH, &sc->avg_width);
87*1925f295SRobert Watson 	if (error != 0 && error != ENOENT) {
88*1925f295SRobert Watson 		device_printf(dev, "invalid %s\n", ALTERA_AVALON_STR_WIDTH);
89*1925f295SRobert Watson 		return (error);
90*1925f295SRobert Watson 	}
91*1925f295SRobert Watson 	(void)resource_string_value(device_get_name(dev),
92*1925f295SRobert Watson 	    device_get_unit(dev), ALTERA_AVALON_STR_FILEIO, &str_fileio);
93*1925f295SRobert Watson 	(void)resource_string_value(device_get_name(dev),
94*1925f295SRobert Watson 	    device_get_unit(dev), ALTERA_AVALON_STR_MMAPIO, &str_mmapio);
95*1925f295SRobert Watson 	(void)resource_string_value(device_get_name(dev),
96*1925f295SRobert Watson 	    device_get_unit(dev), ALTERA_AVALON_STR_DEVNAME, &str_devname);
97*1925f295SRobert Watson 	(void)resource_int_value(device_get_name(dev), device_get_unit(dev),
98*1925f295SRobert Watson 	    ALTERA_AVALON_STR_DEVUNIT, &devunit);
99*1925f295SRobert Watson 
100*1925f295SRobert Watson 	/* Memory allocation and checking. */
101*1925f295SRobert Watson 	sc->avg_rid = 0;
102*1925f295SRobert Watson 	sc->avg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
103*1925f295SRobert Watson 	    &sc->avg_rid, RF_ACTIVE);
104*1925f295SRobert Watson 	if (sc->avg_res == NULL) {
105*1925f295SRobert Watson 		device_printf(dev, "couldn't map memory\n");
106*1925f295SRobert Watson 		return (ENXIO);
107*1925f295SRobert Watson 	}
108*1925f295SRobert Watson 	error = altera_avgen_attach(sc, str_fileio, str_mmapio, str_devname,
109*1925f295SRobert Watson 	    devunit);
110*1925f295SRobert Watson 	if (error != 0)
111*1925f295SRobert Watson 		bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid,
112*1925f295SRobert Watson 		    sc->avg_res);
113*1925f295SRobert Watson 	return (error);
114*1925f295SRobert Watson }
115*1925f295SRobert Watson 
116*1925f295SRobert Watson static int
117*1925f295SRobert Watson altera_avgen_nexus_detach(device_t dev)
118*1925f295SRobert Watson {
119*1925f295SRobert Watson 	struct altera_avgen_softc *sc;
120*1925f295SRobert Watson 
121*1925f295SRobert Watson 	sc = device_get_softc(dev);
122*1925f295SRobert Watson 	altera_avgen_detach(sc);
123*1925f295SRobert Watson 	bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res);
124*1925f295SRobert Watson 	return (0);
125*1925f295SRobert Watson }
126*1925f295SRobert Watson 
127*1925f295SRobert Watson static device_method_t altera_avgen_nexus_methods[] = {
128*1925f295SRobert Watson 	DEVMETHOD(device_probe,		altera_avgen_nexus_probe),
129*1925f295SRobert Watson 	DEVMETHOD(device_attach,	altera_avgen_nexus_attach),
130*1925f295SRobert Watson 	DEVMETHOD(device_detach,	altera_avgen_nexus_detach),
131*1925f295SRobert Watson 	{ 0, 0 }
132*1925f295SRobert Watson };
133*1925f295SRobert Watson 
134*1925f295SRobert Watson static driver_t altera_avgen_nexus_driver = {
135*1925f295SRobert Watson 	"altera_avgen",
136*1925f295SRobert Watson 	altera_avgen_nexus_methods,
137*1925f295SRobert Watson 	sizeof(struct altera_avgen_softc),
138*1925f295SRobert Watson };
139*1925f295SRobert Watson 
140*1925f295SRobert Watson static devclass_t altera_avgen_devclass;
141*1925f295SRobert Watson 
142*1925f295SRobert Watson DRIVER_MODULE(avgen, nexus, altera_avgen_nexus_driver, altera_avgen_devclass,
143*1925f295SRobert Watson     0, 0);
144