xref: /freebsd/sys/dev/altera/avgen/altera_avgen_fdt.c (revision 217d17bcd3f525fbdf124cfbc926f8033da69805)
11925f295SRobert Watson /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
4f00e79c8SRobert Watson  * Copyright (c) 2012-2013, 2016 Robert N. M. Watson
51925f295SRobert Watson  * All rights reserved.
61925f295SRobert Watson  *
71925f295SRobert Watson  * This software was developed by SRI International and the University of
81925f295SRobert Watson  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
91925f295SRobert Watson  * ("CTSRD"), as part of the DARPA CRASH research programme.
101925f295SRobert Watson  *
111925f295SRobert Watson  * Redistribution and use in source and binary forms, with or without
121925f295SRobert Watson  * modification, are permitted provided that the following conditions
131925f295SRobert Watson  * are met:
141925f295SRobert Watson  * 1. Redistributions of source code must retain the above copyright
151925f295SRobert Watson  *    notice, this list of conditions and the following disclaimer.
161925f295SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
171925f295SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
181925f295SRobert Watson  *    documentation and/or other materials provided with the distribution.
191925f295SRobert Watson  *
201925f295SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
211925f295SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221925f295SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231925f295SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
241925f295SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251925f295SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261925f295SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271925f295SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281925f295SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291925f295SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301925f295SRobert Watson  * SUCH DAMAGE.
311925f295SRobert Watson  */
321925f295SRobert Watson 
331925f295SRobert Watson #include <sys/cdefs.h>
341925f295SRobert Watson __FBSDID("$FreeBSD$");
351925f295SRobert Watson 
361925f295SRobert Watson #include <sys/param.h>
371925f295SRobert Watson #include <sys/bus.h>
381925f295SRobert Watson #include <sys/condvar.h>
391925f295SRobert Watson #include <sys/conf.h>
401925f295SRobert Watson #include <sys/kernel.h>
411925f295SRobert Watson #include <sys/lock.h>
421925f295SRobert Watson #include <sys/malloc.h>
431925f295SRobert Watson #include <sys/module.h>
441925f295SRobert Watson #include <sys/mutex.h>
451925f295SRobert Watson #include <sys/rman.h>
461925f295SRobert Watson #include <sys/stat.h>
471925f295SRobert Watson #include <sys/systm.h>
481925f295SRobert Watson #include <sys/uio.h>
491925f295SRobert Watson 
501925f295SRobert Watson #include <machine/bus.h>
511925f295SRobert Watson #include <machine/resource.h>
521925f295SRobert Watson 
531925f295SRobert Watson #include <vm/vm.h>
541925f295SRobert Watson 
550a349699SRobert Watson #include <dev/fdt/fdt_common.h>
560a349699SRobert Watson #include <dev/ofw/openfirm.h>
570a349699SRobert Watson #include <dev/ofw/ofw_bus.h>
580a349699SRobert Watson #include <dev/ofw/ofw_bus_subr.h>
590a349699SRobert Watson 
601925f295SRobert Watson #include <dev/altera/avgen/altera_avgen.h>
611925f295SRobert Watson 
621925f295SRobert Watson static int
630a349699SRobert Watson altera_avgen_fdt_probe(device_t dev)
641925f295SRobert Watson {
651925f295SRobert Watson 
66add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
67add35ed5SIan Lepore 		return (ENXIO);
68add35ed5SIan Lepore 
690a349699SRobert Watson 	if (ofw_bus_is_compatible(dev, "sri-cambridge,avgen")) {
701925f295SRobert Watson 		device_set_desc(dev, "Generic Altera Avalon device attachment");
711925f295SRobert Watson 		return (BUS_PROBE_DEFAULT);
721925f295SRobert Watson 	}
730a349699SRobert Watson 	return (ENXIO);
740a349699SRobert Watson }
751925f295SRobert Watson 
761925f295SRobert Watson static int
770a349699SRobert Watson altera_avgen_fdt_attach(device_t dev)
781925f295SRobert Watson {
791925f295SRobert Watson 	struct altera_avgen_softc *sc;
80f00e79c8SRobert Watson 	char *str_fileio, *str_geomio, *str_mmapio;
810a349699SRobert Watson 	char *str_devname;
820a349699SRobert Watson 	phandle_t node;
830a349699SRobert Watson 	pcell_t cell;
841925f295SRobert Watson 	int devunit, error;
851925f295SRobert Watson 
861925f295SRobert Watson 	sc = device_get_softc(dev);
871925f295SRobert Watson 	sc->avg_dev = dev;
881925f295SRobert Watson 	sc->avg_unit = device_get_unit(dev);
891925f295SRobert Watson 
901925f295SRobert Watson 	/*
910a349699SRobert Watson 	 * Query driver-specific OpenFirmware properties to determine how to
920a349699SRobert Watson 	 * expose the device via /dev.
931925f295SRobert Watson 	 */
941925f295SRobert Watson 	str_fileio = NULL;
95f00e79c8SRobert Watson 	str_geomio = NULL;
961925f295SRobert Watson 	str_mmapio = NULL;
971925f295SRobert Watson 	str_devname = NULL;
981925f295SRobert Watson 	devunit = -1;
991925f295SRobert Watson 	sc->avg_width = 1;
1000a349699SRobert Watson 	node = ofw_bus_get_node(dev);
1010a349699SRobert Watson 	if (OF_getprop(node, "sri-cambridge,width", &cell, sizeof(cell)) > 0)
1020a349699SRobert Watson 		sc->avg_width = cell;
103*217d17bcSOleksandr Tymoshenko 	(void)OF_getprop_alloc(node, "sri-cambridge,fileio",
1040a349699SRobert Watson 	    (void **)&str_fileio);
105*217d17bcSOleksandr Tymoshenko 	(void)OF_getprop_alloc(node, "sri-cambridge,geomio",
106f00e79c8SRobert Watson 	    (void **)&str_geomio);
107*217d17bcSOleksandr Tymoshenko 	(void)OF_getprop_alloc(node, "sri-cambridge,mmapio",
1080a349699SRobert Watson 	    (void **)&str_mmapio);
109*217d17bcSOleksandr Tymoshenko 	(void)OF_getprop_alloc(node,  "sri-cambridge,devname",
1100a349699SRobert Watson 	    (void **)&str_devname);
1110a349699SRobert Watson 	if (OF_getprop(node, "sri-cambridge,devunit", &cell, sizeof(cell)) > 0)
1120a349699SRobert Watson 		devunit = cell;
1131925f295SRobert Watson 
1141925f295SRobert Watson 	/* Memory allocation and checking. */
1151925f295SRobert Watson 	sc->avg_rid = 0;
1161925f295SRobert Watson 	sc->avg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1171925f295SRobert Watson 	    &sc->avg_rid, RF_ACTIVE);
1181925f295SRobert Watson 	if (sc->avg_res == NULL) {
1191925f295SRobert Watson 		device_printf(dev, "couldn't map memory\n");
1201925f295SRobert Watson 		return (ENXIO);
1211925f295SRobert Watson 	}
122f00e79c8SRobert Watson 	error = altera_avgen_attach(sc, str_fileio, str_geomio, str_mmapio,
123f00e79c8SRobert Watson 	    str_devname, devunit);
1241925f295SRobert Watson 	if (error != 0)
1251925f295SRobert Watson 		bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid,
1261925f295SRobert Watson 		    sc->avg_res);
1270a349699SRobert Watson 	if (str_fileio != NULL)
12827b917c8SOleksandr Tymoshenko 		OF_prop_free(str_fileio);
129f00e79c8SRobert Watson 	if (str_geomio != NULL)
130f00e79c8SRobert Watson 		OF_prop_free(str_geomio);
1310a349699SRobert Watson 	if (str_mmapio != NULL)
13227b917c8SOleksandr Tymoshenko 		OF_prop_free(str_mmapio);
1330a349699SRobert Watson 	if (str_devname != NULL)
13427b917c8SOleksandr Tymoshenko 		OF_prop_free(str_devname);
1351925f295SRobert Watson 	return (error);
1361925f295SRobert Watson }
1371925f295SRobert Watson 
1381925f295SRobert Watson static int
1390a349699SRobert Watson altera_avgen_fdt_detach(device_t dev)
1401925f295SRobert Watson {
1411925f295SRobert Watson 	struct altera_avgen_softc *sc;
1421925f295SRobert Watson 
1431925f295SRobert Watson 	sc = device_get_softc(dev);
1441925f295SRobert Watson 	altera_avgen_detach(sc);
1451925f295SRobert Watson 	bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res);
1461925f295SRobert Watson 	return (0);
1471925f295SRobert Watson }
1481925f295SRobert Watson 
1490a349699SRobert Watson static device_method_t altera_avgen_fdt_methods[] = {
1500a349699SRobert Watson 	DEVMETHOD(device_probe,		altera_avgen_fdt_probe),
1510a349699SRobert Watson 	DEVMETHOD(device_attach,	altera_avgen_fdt_attach),
1520a349699SRobert Watson 	DEVMETHOD(device_detach,	altera_avgen_fdt_detach),
1531925f295SRobert Watson 	{ 0, 0 }
1541925f295SRobert Watson };
1551925f295SRobert Watson 
1560a349699SRobert Watson static driver_t altera_avgen_fdt_driver = {
1571925f295SRobert Watson 	"altera_avgen",
1580a349699SRobert Watson 	altera_avgen_fdt_methods,
1591925f295SRobert Watson 	sizeof(struct altera_avgen_softc),
1601925f295SRobert Watson };
1611925f295SRobert Watson 
1620a349699SRobert Watson DRIVER_MODULE(avgen, simplebus, altera_avgen_fdt_driver,
1630a349699SRobert Watson     altera_avgen_devclass, 0, 0);
164