altera_avgen_fdt.c revision 245378
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: head/sys/dev/altera/avgen/altera_avgen_fdt.c 245378 2013-01-13 16:51:57Z rwatson $"); 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 54245378Srwatson#include <dev/fdt/fdt_common.h> 55245378Srwatson#include <dev/ofw/openfirm.h> 56245378Srwatson#include <dev/ofw/ofw_bus.h> 57245378Srwatson#include <dev/ofw/ofw_bus_subr.h> 58245378Srwatson 59239674Srwatson#include <dev/altera/avgen/altera_avgen.h> 60239674Srwatson 61239674Srwatsonstatic int 62245378Srwatsonaltera_avgen_fdt_probe(device_t dev) 63239674Srwatson{ 64239674Srwatson 65245378Srwatson if (ofw_bus_is_compatible(dev, "sri-cambridge,avgen")) { 66245378Srwatson device_set_desc(dev, "Generic Altera Avalon device attachment"); 67245378Srwatson return (BUS_PROBE_DEFAULT); 68245378Srwatson } 69245378Srwatson return (ENXIO); 70239674Srwatson} 71239674Srwatson 72239674Srwatsonstatic int 73245378Srwatsonaltera_avgen_fdt_attach(device_t dev) 74239674Srwatson{ 75239674Srwatson struct altera_avgen_softc *sc; 76245378Srwatson char *str_fileio, *str_mmapio; 77245378Srwatson char *str_devname; 78245378Srwatson phandle_t node; 79245378Srwatson pcell_t cell; 80239674Srwatson int devunit, error; 81239674Srwatson 82239674Srwatson sc = device_get_softc(dev); 83239674Srwatson sc->avg_dev = dev; 84239674Srwatson sc->avg_unit = device_get_unit(dev); 85239674Srwatson 86239674Srwatson /* 87245378Srwatson * Query driver-specific OpenFirmware properties to determine how to 88245378Srwatson * expose the device via /dev. 89239674Srwatson */ 90239674Srwatson str_fileio = NULL; 91239674Srwatson str_mmapio = NULL; 92239674Srwatson str_devname = NULL; 93239674Srwatson devunit = -1; 94239674Srwatson sc->avg_width = 1; 95245378Srwatson node = ofw_bus_get_node(dev); 96245378Srwatson if (OF_getprop(node, "sri-cambridge,width", &cell, sizeof(cell)) > 0) 97245378Srwatson sc->avg_width = cell; 98245378Srwatson (void)OF_getprop_alloc(node, "sri-cambridge,fileio", sizeof(char), 99245378Srwatson (void **)&str_fileio); 100245378Srwatson (void)OF_getprop_alloc(node, "sri-cambridge,mmapio", sizeof(char), 101245378Srwatson (void **)&str_mmapio); 102245378Srwatson (void)OF_getprop_alloc(node, "sri-cambridge,devname", sizeof(char), 103245378Srwatson (void **)&str_devname); 104245378Srwatson if (OF_getprop(node, "sri-cambridge,devunit", &cell, sizeof(cell)) > 0) 105245378Srwatson devunit = cell; 106239674Srwatson 107239674Srwatson /* Memory allocation and checking. */ 108239674Srwatson sc->avg_rid = 0; 109239674Srwatson sc->avg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 110239674Srwatson &sc->avg_rid, RF_ACTIVE); 111239674Srwatson if (sc->avg_res == NULL) { 112239674Srwatson device_printf(dev, "couldn't map memory\n"); 113239674Srwatson return (ENXIO); 114239674Srwatson } 115245376Srwatson error = altera_avgen_attach(sc, str_fileio, str_mmapio, str_devname, 116245376Srwatson devunit); 117245376Srwatson if (error != 0) 118245376Srwatson bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, 119245376Srwatson sc->avg_res); 120245378Srwatson if (str_fileio != NULL) 121245378Srwatson free(str_fileio, M_OFWPROP); 122245378Srwatson if (str_mmapio != NULL) 123245378Srwatson free(str_mmapio, M_OFWPROP); 124245378Srwatson if (str_devname != NULL) 125245378Srwatson free(str_devname, M_OFWPROP); 126239674Srwatson return (error); 127239674Srwatson} 128239674Srwatson 129239674Srwatsonstatic int 130245378Srwatsonaltera_avgen_fdt_detach(device_t dev) 131239674Srwatson{ 132239674Srwatson struct altera_avgen_softc *sc; 133239674Srwatson 134239674Srwatson sc = device_get_softc(dev); 135245376Srwatson altera_avgen_detach(sc); 136239674Srwatson bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res); 137239674Srwatson return (0); 138239674Srwatson} 139239674Srwatson 140245378Srwatsonstatic device_method_t altera_avgen_fdt_methods[] = { 141245378Srwatson DEVMETHOD(device_probe, altera_avgen_fdt_probe), 142245378Srwatson DEVMETHOD(device_attach, altera_avgen_fdt_attach), 143245378Srwatson DEVMETHOD(device_detach, altera_avgen_fdt_detach), 144239674Srwatson { 0, 0 } 145239674Srwatson}; 146239674Srwatson 147245378Srwatsonstatic driver_t altera_avgen_fdt_driver = { 148239674Srwatson "altera_avgen", 149245378Srwatson altera_avgen_fdt_methods, 150239674Srwatson sizeof(struct altera_avgen_softc), 151239674Srwatson}; 152239674Srwatson 153239674Srwatsonstatic devclass_t altera_avgen_devclass; 154239674Srwatson 155245378SrwatsonDRIVER_MODULE(avgen, simplebus, altera_avgen_fdt_driver, 156245378Srwatson altera_avgen_devclass, 0, 0); 157