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_fdt.c 266152 2014-05-15 16:11:06Z 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
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
65266152Sian	if (!ofw_bus_status_okay(dev))
66266152Sian		return (ENXIO);
67266152Sian
68245378Srwatson	if (ofw_bus_is_compatible(dev, "sri-cambridge,avgen")) {
69245378Srwatson		device_set_desc(dev, "Generic Altera Avalon device attachment");
70245378Srwatson		return (BUS_PROBE_DEFAULT);
71245378Srwatson	}
72245378Srwatson	return (ENXIO);
73239674Srwatson}
74239674Srwatson
75239674Srwatsonstatic int
76245378Srwatsonaltera_avgen_fdt_attach(device_t dev)
77239674Srwatson{
78239674Srwatson	struct altera_avgen_softc *sc;
79245378Srwatson	char *str_fileio, *str_mmapio;
80245378Srwatson	char *str_devname;
81245378Srwatson	phandle_t node;
82245378Srwatson	pcell_t cell;
83239674Srwatson	int devunit, error;
84239674Srwatson
85239674Srwatson	sc = device_get_softc(dev);
86239674Srwatson	sc->avg_dev = dev;
87239674Srwatson	sc->avg_unit = device_get_unit(dev);
88239674Srwatson
89239674Srwatson	/*
90245378Srwatson	 * Query driver-specific OpenFirmware properties to determine how to
91245378Srwatson	 * expose the device via /dev.
92239674Srwatson	 */
93239674Srwatson	str_fileio = NULL;
94239674Srwatson	str_mmapio = NULL;
95239674Srwatson	str_devname = NULL;
96239674Srwatson	devunit = -1;
97239674Srwatson	sc->avg_width = 1;
98245378Srwatson	node = ofw_bus_get_node(dev);
99245378Srwatson	if (OF_getprop(node, "sri-cambridge,width", &cell, sizeof(cell)) > 0)
100245378Srwatson		sc->avg_width = cell;
101245378Srwatson	(void)OF_getprop_alloc(node, "sri-cambridge,fileio", sizeof(char),
102245378Srwatson	    (void **)&str_fileio);
103245378Srwatson	(void)OF_getprop_alloc(node, "sri-cambridge,mmapio", sizeof(char),
104245378Srwatson	    (void **)&str_mmapio);
105245378Srwatson	(void)OF_getprop_alloc(node,  "sri-cambridge,devname", sizeof(char),
106245378Srwatson	    (void **)&str_devname);
107245378Srwatson	if (OF_getprop(node, "sri-cambridge,devunit", &cell, sizeof(cell)) > 0)
108245378Srwatson		devunit = cell;
109239674Srwatson
110239674Srwatson	/* Memory allocation and checking. */
111239674Srwatson	sc->avg_rid = 0;
112239674Srwatson	sc->avg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
113239674Srwatson	    &sc->avg_rid, RF_ACTIVE);
114239674Srwatson	if (sc->avg_res == NULL) {
115239674Srwatson		device_printf(dev, "couldn't map memory\n");
116239674Srwatson		return (ENXIO);
117239674Srwatson	}
118245376Srwatson	error = altera_avgen_attach(sc, str_fileio, str_mmapio, str_devname,
119245376Srwatson	    devunit);
120245376Srwatson	if (error != 0)
121245376Srwatson		bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid,
122245376Srwatson		    sc->avg_res);
123245378Srwatson	if (str_fileio != NULL)
124245378Srwatson		free(str_fileio, M_OFWPROP);
125245378Srwatson	if (str_mmapio != NULL)
126245378Srwatson		free(str_mmapio, M_OFWPROP);
127245378Srwatson	if (str_devname != NULL)
128245378Srwatson		free(str_devname, M_OFWPROP);
129239674Srwatson	return (error);
130239674Srwatson}
131239674Srwatson
132239674Srwatsonstatic int
133245378Srwatsonaltera_avgen_fdt_detach(device_t dev)
134239674Srwatson{
135239674Srwatson	struct altera_avgen_softc *sc;
136239674Srwatson
137239674Srwatson	sc = device_get_softc(dev);
138245376Srwatson	altera_avgen_detach(sc);
139239674Srwatson	bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res);
140239674Srwatson	return (0);
141239674Srwatson}
142239674Srwatson
143245378Srwatsonstatic device_method_t altera_avgen_fdt_methods[] = {
144245378Srwatson	DEVMETHOD(device_probe,		altera_avgen_fdt_probe),
145245378Srwatson	DEVMETHOD(device_attach,	altera_avgen_fdt_attach),
146245378Srwatson	DEVMETHOD(device_detach,	altera_avgen_fdt_detach),
147239674Srwatson	{ 0, 0 }
148239674Srwatson};
149239674Srwatson
150245378Srwatsonstatic driver_t altera_avgen_fdt_driver = {
151239674Srwatson	"altera_avgen",
152245378Srwatson	altera_avgen_fdt_methods,
153239674Srwatson	sizeof(struct altera_avgen_softc),
154239674Srwatson};
155239674Srwatson
156245378SrwatsonDRIVER_MODULE(avgen, simplebus, altera_avgen_fdt_driver,
157245378Srwatson    altera_avgen_devclass, 0, 0);
158