terasic_de4led_fdt.c revision 245379
1239709Srwatson/*-
2239709Srwatson * Copyright (c) 2012 Robert N. M. Watson
3239709Srwatson * All rights reserved.
4239709Srwatson *
5239709Srwatson * This software was developed by SRI International and the University of
6239709Srwatson * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7239709Srwatson * ("CTSRD"), as part of the DARPA CRASH research programme.
8239709Srwatson *
9239709Srwatson * Redistribution and use in source and binary forms, with or without
10239709Srwatson * modification, are permitted provided that the following conditions
11239709Srwatson * are met:
12239709Srwatson * 1. Redistributions of source code must retain the above copyright
13239709Srwatson *    notice, this list of conditions and the following disclaimer.
14239709Srwatson * 2. Redistributions in binary form must reproduce the above copyright
15239709Srwatson *    notice, this list of conditions and the following disclaimer in the
16239709Srwatson *    documentation and/or other materials provided with the distribution.
17239709Srwatson *
18239709Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19239709Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20239709Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21239709Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22239709Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23239709Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24239709Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25239709Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26239709Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27239709Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28239709Srwatson * SUCH DAMAGE.
29239709Srwatson */
30239709Srwatson
31239709Srwatson#include <sys/cdefs.h>
32239709Srwatson__FBSDID("$FreeBSD: head/sys/dev/terasic/de4led/terasic_de4led_fdt.c 245379 2013-01-13 16:53:31Z rwatson $");
33239709Srwatson
34239709Srwatson#include <sys/param.h>
35239709Srwatson#include <sys/bus.h>
36239709Srwatson#include <sys/condvar.h>
37239709Srwatson#include <sys/conf.h>
38239709Srwatson#include <sys/bio.h>
39239709Srwatson#include <sys/kernel.h>
40239709Srwatson#include <sys/lock.h>
41239709Srwatson#include <sys/malloc.h>
42239709Srwatson#include <sys/module.h>
43239709Srwatson#include <sys/mutex.h>
44239709Srwatson#include <sys/rman.h>
45239709Srwatson#include <sys/systm.h>
46239709Srwatson
47239709Srwatson#include <machine/bus.h>
48239709Srwatson#include <machine/resource.h>
49239709Srwatson
50245367Srwatson#include <dev/fdt/fdt_common.h>
51245367Srwatson#include <dev/ofw/openfirm.h>
52245367Srwatson#include <dev/ofw/ofw_bus.h>
53245367Srwatson#include <dev/ofw/ofw_bus_subr.h>
54245367Srwatson
55239709Srwatson#include <dev/terasic/de4led/terasic_de4led.h>
56239709Srwatson
57239709Srwatson/*
58239709Srwatson * Nexus bus attachment for the 8-element LED on the Terasic DE-4 FPGA board,
59239709Srwatson * which is hooked up to the processor via a memory-mapped Avalon bus.
60239709Srwatson */
61239709Srwatsonstatic int
62245367Srwatsonterasic_de4led_fdt_probe(device_t dev)
63239709Srwatson{
64239709Srwatson
65245379Srwatson	if (ofw_bus_is_compatible(dev, "sri-cambridge,de4led")) {
66245367Srwatson		device_set_desc(dev, "Terasic DE4 8-element LED");
67245367Srwatson		return (BUS_PROBE_DEFAULT);
68245367Srwatson	}
69245367Srwatson	return (ENXIO);
70239709Srwatson}
71239709Srwatson
72239709Srwatsonstatic int
73245367Srwatsonterasic_de4led_fdt_attach(device_t dev)
74239709Srwatson{
75239709Srwatson	struct terasic_de4led_softc *sc;
76239709Srwatson
77239709Srwatson	sc = device_get_softc(dev);
78239709Srwatson	sc->tdl_dev = dev;
79239709Srwatson	sc->tdl_unit = device_get_unit(dev);
80239709Srwatson	sc->tdl_rid = 0;
81239709Srwatson	sc->tdl_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
82239709Srwatson	    &sc->tdl_rid, RF_ACTIVE);
83239709Srwatson	if (sc->tdl_res == NULL) {
84239709Srwatson		device_printf(dev, "couldn't map memory\n");
85239709Srwatson		return (ENXIO);
86239709Srwatson	}
87239709Srwatson	terasic_de4led_attach(sc);
88239709Srwatson	return (0);
89239709Srwatson}
90239709Srwatson
91239709Srwatsonstatic int
92245367Srwatsonterasic_de4led_fdt_detach(device_t dev)
93239709Srwatson{
94239709Srwatson	struct terasic_de4led_softc *sc;
95239709Srwatson
96239709Srwatson	sc = device_get_softc(dev);
97239709Srwatson	KASSERT(sc->tdl_res != NULL, ("%s: resources not allocated",
98239709Srwatson	    __func__));
99239709Srwatson	terasic_de4led_detach(sc);
100239709Srwatson	bus_release_resource(dev, SYS_RES_MEMORY, sc->tdl_rid, sc->tdl_res);
101239709Srwatson	return (0);
102239709Srwatson}
103239709Srwatson
104245367Srwatsonstatic device_method_t terasic_de4led_fdt_methods[] = {
105245367Srwatson	DEVMETHOD(device_probe,		terasic_de4led_fdt_probe),
106245367Srwatson	DEVMETHOD(device_attach,	terasic_de4led_fdt_attach),
107245367Srwatson	DEVMETHOD(device_detach,	terasic_de4led_fdt_detach),
108239709Srwatson	{ 0, 0 }
109239709Srwatson};
110239709Srwatson
111245367Srwatsonstatic driver_t terasic_de4led_fdt_driver = {
112239709Srwatson	"terasic_de4led",
113245367Srwatson	terasic_de4led_fdt_methods,
114239709Srwatson	sizeof(struct terasic_de4led_softc),
115239709Srwatson};
116239709Srwatson
117239709Srwatsonstatic devclass_t terasic_de4led_devclass;
118239709Srwatson
119245367SrwatsonDRIVER_MODULE(terasic_de4led, simplebus, terasic_de4led_fdt_driver,
120239709Srwatson    terasic_de4led_devclass, 0, 0);
121