xref: /freebsd/sys/dev/altera/avgen/altera_avgen_fdt.c (revision 0a349699f4e6157244d013b7742bc2dc0c99dd85)
11925f295SRobert Watson /*-
21925f295SRobert Watson  * Copyright (c) 2012-2013 Robert N. M. Watson
31925f295SRobert Watson  * All rights reserved.
41925f295SRobert Watson  *
51925f295SRobert Watson  * This software was developed by SRI International and the University of
61925f295SRobert Watson  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
71925f295SRobert Watson  * ("CTSRD"), as part of the DARPA CRASH research programme.
81925f295SRobert Watson  *
91925f295SRobert Watson  * Redistribution and use in source and binary forms, with or without
101925f295SRobert Watson  * modification, are permitted provided that the following conditions
111925f295SRobert Watson  * are met:
121925f295SRobert Watson  * 1. Redistributions of source code must retain the above copyright
131925f295SRobert Watson  *    notice, this list of conditions and the following disclaimer.
141925f295SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
151925f295SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
161925f295SRobert Watson  *    documentation and/or other materials provided with the distribution.
171925f295SRobert Watson  *
181925f295SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
191925f295SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201925f295SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211925f295SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
221925f295SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231925f295SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241925f295SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251925f295SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261925f295SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271925f295SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281925f295SRobert Watson  * SUCH DAMAGE.
291925f295SRobert Watson  */
301925f295SRobert Watson 
311925f295SRobert Watson #include <sys/cdefs.h>
321925f295SRobert Watson __FBSDID("$FreeBSD$");
331925f295SRobert Watson 
341925f295SRobert Watson #include <sys/param.h>
351925f295SRobert Watson #include <sys/bus.h>
361925f295SRobert Watson #include <sys/condvar.h>
371925f295SRobert Watson #include <sys/conf.h>
381925f295SRobert Watson #include <sys/kernel.h>
391925f295SRobert Watson #include <sys/lock.h>
401925f295SRobert Watson #include <sys/malloc.h>
411925f295SRobert Watson #include <sys/module.h>
421925f295SRobert Watson #include <sys/mutex.h>
431925f295SRobert Watson #include <sys/rman.h>
441925f295SRobert Watson #include <sys/stat.h>
451925f295SRobert Watson #include <sys/systm.h>
461925f295SRobert Watson #include <sys/uio.h>
471925f295SRobert Watson 
481925f295SRobert Watson #include <machine/bus.h>
491925f295SRobert Watson #include <machine/resource.h>
501925f295SRobert Watson #include <machine/vm.h>
511925f295SRobert Watson 
521925f295SRobert Watson #include <vm/vm.h>
531925f295SRobert Watson 
54*0a349699SRobert Watson #include <dev/fdt/fdt_common.h>
55*0a349699SRobert Watson #include <dev/ofw/openfirm.h>
56*0a349699SRobert Watson #include <dev/ofw/ofw_bus.h>
57*0a349699SRobert Watson #include <dev/ofw/ofw_bus_subr.h>
58*0a349699SRobert Watson 
591925f295SRobert Watson #include <dev/altera/avgen/altera_avgen.h>
601925f295SRobert Watson 
611925f295SRobert Watson static int
62*0a349699SRobert Watson altera_avgen_fdt_probe(device_t dev)
631925f295SRobert Watson {
641925f295SRobert Watson 
65*0a349699SRobert Watson 	if (ofw_bus_is_compatible(dev, "sri-cambridge,avgen")) {
661925f295SRobert Watson 		device_set_desc(dev, "Generic Altera Avalon device attachment");
671925f295SRobert Watson 		return (BUS_PROBE_DEFAULT);
681925f295SRobert Watson 	}
69*0a349699SRobert Watson 	return (ENXIO);
70*0a349699SRobert Watson }
711925f295SRobert Watson 
721925f295SRobert Watson static int
73*0a349699SRobert Watson altera_avgen_fdt_attach(device_t dev)
741925f295SRobert Watson {
751925f295SRobert Watson 	struct altera_avgen_softc *sc;
76*0a349699SRobert Watson 	char *str_fileio, *str_mmapio;
77*0a349699SRobert Watson 	char *str_devname;
78*0a349699SRobert Watson 	phandle_t node;
79*0a349699SRobert Watson 	pcell_t cell;
801925f295SRobert Watson 	int devunit, error;
811925f295SRobert Watson 
821925f295SRobert Watson 	sc = device_get_softc(dev);
831925f295SRobert Watson 	sc->avg_dev = dev;
841925f295SRobert Watson 	sc->avg_unit = device_get_unit(dev);
851925f295SRobert Watson 
861925f295SRobert Watson 	/*
87*0a349699SRobert Watson 	 * Query driver-specific OpenFirmware properties to determine how to
88*0a349699SRobert Watson 	 * expose the device via /dev.
891925f295SRobert Watson 	 */
901925f295SRobert Watson 	str_fileio = NULL;
911925f295SRobert Watson 	str_mmapio = NULL;
921925f295SRobert Watson 	str_devname = NULL;
931925f295SRobert Watson 	devunit = -1;
941925f295SRobert Watson 	sc->avg_width = 1;
95*0a349699SRobert Watson 	node = ofw_bus_get_node(dev);
96*0a349699SRobert Watson 	if (OF_getprop(node, "sri-cambridge,width", &cell, sizeof(cell)) > 0)
97*0a349699SRobert Watson 		sc->avg_width = cell;
98*0a349699SRobert Watson 	(void)OF_getprop_alloc(node, "sri-cambridge,fileio", sizeof(char),
99*0a349699SRobert Watson 	    (void **)&str_fileio);
100*0a349699SRobert Watson 	(void)OF_getprop_alloc(node, "sri-cambridge,mmapio", sizeof(char),
101*0a349699SRobert Watson 	    (void **)&str_mmapio);
102*0a349699SRobert Watson 	(void)OF_getprop_alloc(node,  "sri-cambridge,devname", sizeof(char),
103*0a349699SRobert Watson 	    (void **)&str_devname);
104*0a349699SRobert Watson 	if (OF_getprop(node, "sri-cambridge,devunit", &cell, sizeof(cell)) > 0)
105*0a349699SRobert Watson 		devunit = cell;
1061925f295SRobert Watson 
1071925f295SRobert Watson 	/* Memory allocation and checking. */
1081925f295SRobert Watson 	sc->avg_rid = 0;
1091925f295SRobert Watson 	sc->avg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1101925f295SRobert Watson 	    &sc->avg_rid, RF_ACTIVE);
1111925f295SRobert Watson 	if (sc->avg_res == NULL) {
1121925f295SRobert Watson 		device_printf(dev, "couldn't map memory\n");
1131925f295SRobert Watson 		return (ENXIO);
1141925f295SRobert Watson 	}
1151925f295SRobert Watson 	error = altera_avgen_attach(sc, str_fileio, str_mmapio, str_devname,
1161925f295SRobert Watson 	    devunit);
1171925f295SRobert Watson 	if (error != 0)
1181925f295SRobert Watson 		bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid,
1191925f295SRobert Watson 		    sc->avg_res);
120*0a349699SRobert Watson 	if (str_fileio != NULL)
121*0a349699SRobert Watson 		free(str_fileio, M_OFWPROP);
122*0a349699SRobert Watson 	if (str_mmapio != NULL)
123*0a349699SRobert Watson 		free(str_mmapio, M_OFWPROP);
124*0a349699SRobert Watson 	if (str_devname != NULL)
125*0a349699SRobert Watson 		free(str_devname, M_OFWPROP);
1261925f295SRobert Watson 	return (error);
1271925f295SRobert Watson }
1281925f295SRobert Watson 
1291925f295SRobert Watson static int
130*0a349699SRobert Watson altera_avgen_fdt_detach(device_t dev)
1311925f295SRobert Watson {
1321925f295SRobert Watson 	struct altera_avgen_softc *sc;
1331925f295SRobert Watson 
1341925f295SRobert Watson 	sc = device_get_softc(dev);
1351925f295SRobert Watson 	altera_avgen_detach(sc);
1361925f295SRobert Watson 	bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res);
1371925f295SRobert Watson 	return (0);
1381925f295SRobert Watson }
1391925f295SRobert Watson 
140*0a349699SRobert Watson static device_method_t altera_avgen_fdt_methods[] = {
141*0a349699SRobert Watson 	DEVMETHOD(device_probe,		altera_avgen_fdt_probe),
142*0a349699SRobert Watson 	DEVMETHOD(device_attach,	altera_avgen_fdt_attach),
143*0a349699SRobert Watson 	DEVMETHOD(device_detach,	altera_avgen_fdt_detach),
1441925f295SRobert Watson 	{ 0, 0 }
1451925f295SRobert Watson };
1461925f295SRobert Watson 
147*0a349699SRobert Watson static driver_t altera_avgen_fdt_driver = {
1481925f295SRobert Watson 	"altera_avgen",
149*0a349699SRobert Watson 	altera_avgen_fdt_methods,
1501925f295SRobert Watson 	sizeof(struct altera_avgen_softc),
1511925f295SRobert Watson };
1521925f295SRobert Watson 
1531925f295SRobert Watson static devclass_t altera_avgen_devclass;
1541925f295SRobert Watson 
155*0a349699SRobert Watson DRIVER_MODULE(avgen, simplebus, altera_avgen_fdt_driver,
156*0a349699SRobert Watson     altera_avgen_devclass, 0, 0);
157