diff options
author | rwatson <rwatson@FreeBSD.org> | 2012-08-26 09:21:59 +0000 |
---|---|---|
committer | rwatson <rwatson@FreeBSD.org> | 2012-08-26 09:21:59 +0000 |
commit | 5b348c0d216b555a1eebeecb755b6a30764154f8 (patch) | |
tree | 1c4feba7fc520429710a82a95fa744b4972c75bb | |
parent | c789e6f653a3b31b2a687d229588c7f140ea2846 (diff) | |
download | FreeBSD-src-5b348c0d216b555a1eebeecb755b6a30764154f8.zip FreeBSD-src-5b348c0d216b555a1eebeecb755b6a30764154f8.tar.gz |
Add terasic_de4led, a led(4) driver for the on-board 8-element LED on the
Terasic DE-4 board. Allow LED configuration to be set using loader
tunables, not just from userspace, and preconfigure LED 8 as a kernel
heartbeat. For now, this is a Nexus-attached, BERI-only driver, but it
could be used with other hard and soft cores on Altera FPGAs as well, in
principle.
Sponsored by: DARPA, AFRL
-rw-r--r-- | sys/dev/terasic/de4led/terasic_de4led.c | 188 | ||||
-rw-r--r-- | sys/dev/terasic/de4led/terasic_de4led.h | 75 | ||||
-rw-r--r-- | sys/dev/terasic/de4led/terasic_de4led_nexus.c | 112 | ||||
-rw-r--r-- | sys/mips/beri/files.beri | 2 | ||||
-rw-r--r-- | sys/mips/conf/BERI_DE4.hints | 8 | ||||
-rw-r--r-- | sys/mips/conf/BERI_DE4_MDROOT | 1 | ||||
-rw-r--r-- | sys/mips/conf/BERI_DE4_SDROOT | 1 |
7 files changed, 387 insertions, 0 deletions
diff --git a/sys/dev/terasic/de4led/terasic_de4led.c b/sys/dev/terasic/de4led/terasic_de4led.c new file mode 100644 index 0000000..1fb92d2 --- /dev/null +++ b/sys/dev/terasic/de4led/terasic_de4led.c @@ -0,0 +1,188 @@ +/*- + * Copyright (c) 2012 Robert N. M. Watson + * All rights reserved. + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) + * ("CTSRD"), as part of the DARPA CRASH research programme. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/bus.h> +#include <sys/lock.h> +#include <sys/mutex.h> +#include <sys/rman.h> + +#include <machine/bus.h> +#include <machine/resource.h> + +#include <dev/led/led.h> +#include <dev/terasic/de4led/terasic_de4led.h> + +static void +terasic_de4led_update(struct terasic_de4led_softc *sc) +{ + + TERASIC_DE4LED_LOCK_ASSERT(sc); + + bus_write_1(sc->tdl_res, TERASIC_DE4LED_OFF_LED, sc->tdl_bits); +} + +static void +led_update(struct terasic_de4led_softc *sc, int bit, int onoff) +{ + + TERASIC_DE4LED_LOCK(sc); + TERASIC_DE4LED_SETLED(sc, bit, onoff); + terasic_de4led_update(sc); + TERASIC_DE4LED_UNLOCK(sc); +} + +static void +led_0(void *arg, int onoff) +{ + + led_update(arg, 0, onoff); +} + +static void +led_1(void *arg, int onoff) +{ + + led_update(arg, 1, onoff); +} + +static void +led_2(void *arg, int onoff) +{ + + led_update(arg, 2, onoff); +} + +static void +led_3(void *arg, int onoff) +{ + + led_update(arg, 3, onoff); +} + +static void +led_4(void *arg, int onoff) +{ + + led_update(arg, 4, onoff); +} + +static void +led_5(void *arg, int onoff) +{ + + led_update(arg, 5, onoff); +} + +static void +led_6(void *arg, int onoff) +{ + + led_update(arg, 6, onoff); +} + +static void +led_7(void *arg, int onoff) +{ + + led_update(arg, 7, onoff); +} + +void +terasic_de4led_attach(struct terasic_de4led_softc *sc) +{ + const char *cmd; + + TERASIC_DE4LED_LOCK_INIT(sc); + + /* + * Clear the LED array before we start. + */ + TERASIC_DE4LED_LOCK(sc); + TERASIC_DE4LED_CLEARBAR(sc); + terasic_de4led_update(sc); + TERASIC_DE4LED_UNLOCK(sc); + + /* + * Register the LED array with led(4). + */ + sc->tdl_leds[0] = led_create(led_0, sc, "de4led_0"); + sc->tdl_leds[1] = led_create(led_1, sc, "de4led_1"); + sc->tdl_leds[2] = led_create(led_2, sc, "de4led_2"); + sc->tdl_leds[3] = led_create(led_3, sc, "de4led_3"); + sc->tdl_leds[4] = led_create(led_4, sc, "de4led_4"); + sc->tdl_leds[5] = led_create(led_5, sc, "de4led_5"); + sc->tdl_leds[6] = led_create(led_6, sc, "de4led_6"); + sc->tdl_leds[7] = led_create(led_7, sc, "de4led_7"); + + if (resource_string_value(device_get_name(sc->tdl_dev), + sc->tdl_unit, "de4led_0_cmd", &cmd) == 0) + led_set("de4led_0", cmd); + if (resource_string_value(device_get_name(sc->tdl_dev), + sc->tdl_unit, "de4led_1_cmd", &cmd) == 0) + led_set("de4led_1", cmd); + if (resource_string_value(device_get_name(sc->tdl_dev), + sc->tdl_unit, "de4led_2_cmd", &cmd) == 0) + led_set("de4led_2", cmd); + if (resource_string_value(device_get_name(sc->tdl_dev), + sc->tdl_unit, "de4led_3_cmd", &cmd) == 0) + led_set("de4led_3", cmd); + if (resource_string_value(device_get_name(sc->tdl_dev), + sc->tdl_unit, "de4led_4_cmd", &cmd) == 0) + led_set("de4led_4", cmd); + if (resource_string_value(device_get_name(sc->tdl_dev), + sc->tdl_unit, "de4led_5_cmd", &cmd) == 0) + led_set("de4led_5", cmd); + if (resource_string_value(device_get_name(sc->tdl_dev), + sc->tdl_unit, "de4led_6_cmd", &cmd) == 0) + led_set("de4led_6", cmd); + if (resource_string_value(device_get_name(sc->tdl_dev), + sc->tdl_unit, "de4led_7_cmd", &cmd) == 0) + led_set("de4led_7", cmd); +} + +void +terasic_de4led_detach(struct terasic_de4led_softc *sc) +{ + int i; + + for (i = 0; i < 8; i++) { + led_destroy(sc->tdl_leds[i]); + sc->tdl_leds[i] = NULL; + } + TERASIC_DE4LED_LOCK(sc); + TERASIC_DE4LED_CLEARBAR(sc); + terasic_de4led_update(sc); + TERASIC_DE4LED_UNLOCK(sc); + TERASIC_DE4LED_LOCK_DESTROY(sc); +} diff --git a/sys/dev/terasic/de4led/terasic_de4led.h b/sys/dev/terasic/de4led/terasic_de4led.h new file mode 100644 index 0000000..89461d1 --- /dev/null +++ b/sys/dev/terasic/de4led/terasic_de4led.h @@ -0,0 +1,75 @@ +/*- + * Copyright (c) 2012 Robert N. M. Watson + * All rights reserved. + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) + * ("CTSRD"), as part of the DARPA CRASH research programme. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _DEV_TERASIC_DE4LED_H_ +#define _DEV_TERASIC_DE4LED_H_ + +#define TERASIC_DE4LED_NUMLEDS 8 +struct terasic_de4led_softc { + device_t tdl_dev; + int tdl_unit; + struct resource *tdl_res; + int tdl_rid; + struct mtx tdl_lock; + uint8_t tdl_bits; + struct cdev *tdl_leds[TERASIC_DE4LED_NUMLEDS]; +}; + +#define TERASIC_DE4LED_LOCK(sc) mtx_lock(&(sc)->tdl_lock) +#define TERASIC_DE4LED_LOCK_ASSERT(sc) mtx_assert(&(sc)->tdl_lock, MA_OWNED) +#define TERASIC_DE4LED_LOCK_DESTROY(sc) mtx_destroy(&(sc)->tdl_lock) +#define TERASIC_DE4LED_LOCK_INIT(sc) mtx_init(&(sc)->tdl_lock, \ + "terasic_de4led", NULL, MTX_DEF) +#define TERASIC_DE4LED_UNLOCK(sc) mtx_unlock(&(sc)->tdl_lock) + +/* + * Setting and clearing LEDs. tdl_bits is in the bit order preferred for I/O. + * The LED elements are labelled 1..8 on the DE-4, so bit 0 is LED 1, and so + * on. + */ +#define TERASIC_DE4LED_CLEARBAR(sc) do { \ + (sc)->tdl_bits = 0; \ +} while (0) +#define TERASIC_DE4LED_SETLED(sc, led, onoff) do { \ + (sc)->tdl_bits &= ~(1 << (led)); \ + (sc)->tdl_bits |= ((onoff != 0) ? 1 : 0) << (led); \ +} while (0) + +/* + * Only one offset matters for this device -- 0. + */ +#define TERASIC_DE4LED_OFF_LED 0 + +void terasic_de4led_attach(struct terasic_de4led_softc *sc); +void terasic_de4led_detach(struct terasic_de4led_softc *sc); + +#endif /* _DEV_TERASIC_DE4LED_H_ */ diff --git a/sys/dev/terasic/de4led/terasic_de4led_nexus.c b/sys/dev/terasic/de4led/terasic_de4led_nexus.c new file mode 100644 index 0000000..92da503 --- /dev/null +++ b/sys/dev/terasic/de4led/terasic_de4led_nexus.c @@ -0,0 +1,112 @@ +/*- + * Copyright (c) 2012 Robert N. M. Watson + * All rights reserved. + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) + * ("CTSRD"), as part of the DARPA CRASH research programme. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/bus.h> +#include <sys/condvar.h> +#include <sys/conf.h> +#include <sys/bio.h> +#include <sys/kernel.h> +#include <sys/lock.h> +#include <sys/malloc.h> +#include <sys/module.h> +#include <sys/mutex.h> +#include <sys/rman.h> +#include <sys/systm.h> + +#include <machine/bus.h> +#include <machine/resource.h> + +#include <dev/terasic/de4led/terasic_de4led.h> + +/* + * Nexus bus attachment for the 8-element LED on the Terasic DE-4 FPGA board, + * which is hooked up to the processor via a memory-mapped Avalon bus. + */ +static int +terasic_de4led_nexus_probe(device_t dev) +{ + + device_set_desc(dev, "Terasic DE4 8-element LED"); + return (BUS_PROBE_DEFAULT); +} + +static int +terasic_de4led_nexus_attach(device_t dev) +{ + struct terasic_de4led_softc *sc; + + sc = device_get_softc(dev); + sc->tdl_dev = dev; + sc->tdl_unit = device_get_unit(dev); + sc->tdl_rid = 0; + sc->tdl_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, + &sc->tdl_rid, RF_ACTIVE); + if (sc->tdl_res == NULL) { + device_printf(dev, "couldn't map memory\n"); + return (ENXIO); + } + terasic_de4led_attach(sc); + return (0); +} + +static int +terasic_de4led_nexus_detach(device_t dev) +{ + struct terasic_de4led_softc *sc; + + sc = device_get_softc(dev); + KASSERT(sc->tdl_res != NULL, ("%s: resources not allocated", + __func__)); + terasic_de4led_detach(sc); + bus_release_resource(dev, SYS_RES_MEMORY, sc->tdl_rid, sc->tdl_res); + return (0); +} + +static device_method_t terasic_de4led_nexus_methods[] = { + DEVMETHOD(device_probe, terasic_de4led_nexus_probe), + DEVMETHOD(device_attach, terasic_de4led_nexus_attach), + DEVMETHOD(device_detach, terasic_de4led_nexus_detach), + { 0, 0 } +}; + +static driver_t terasic_de4led_nexus_driver = { + "terasic_de4led", + terasic_de4led_nexus_methods, + sizeof(struct terasic_de4led_softc), +}; + +static devclass_t terasic_de4led_devclass; + +DRIVER_MODULE(terasic_de4led, nexus, terasic_de4led_nexus_driver, + terasic_de4led_devclass, 0, 0); diff --git a/sys/mips/beri/files.beri b/sys/mips/beri/files.beri index 78c3958..02a27f8 100644 --- a/sys/mips/beri/files.beri +++ b/sys/mips/beri/files.beri @@ -2,6 +2,8 @@ dev/altera/jtag_uart/altera_jtag_uart_cons.c optional altera_jtag_uart dev/altera/jtag_uart/altera_jtag_uart_tty.c optional altera_jtag_uart dev/altera/jtag_uart/altera_jtag_uart_nexus.c optional altera_jtag_uart +dev/terasic/de4led/terasic_de4led.c optional terasic_de4led +dev/terasic/de4led/terasic_de4led_nexus.c optional terasic_de4led dev/terasic/mtl/terasic_mtl.c optional terasic_mtl dev/terasic/mtl/terasic_mtl_nexus.c optional terasic_mtl dev/terasic/mtl/terasic_mtl_pixel.c optional terasic_mtl diff --git a/sys/mips/conf/BERI_DE4.hints b/sys/mips/conf/BERI_DE4.hints index b2a0e0a..4f787b9 100644 --- a/sys/mips/conf/BERI_DE4.hints +++ b/sys/mips/conf/BERI_DE4.hints @@ -25,6 +25,14 @@ hint.altera_sdcardc.0.maddr=0x7f008000 hint.altera_sdcardc.0.msize=0x400 # +# On-board DE4 8-element LED +# +hint.terasic_de4led.0.at="nexus0" +hint.terasic_de4led.0.maddr=0x7f006000 +hint.terasic_de4led.0.msize=1 +hint.terasic_de4led.0.de4led_0_cmd="f9" + +# # Terasic Multi-touch LCD (MTL), an optional feature in DE-4 configurations. # hint.terasic_mtl.0.at="nexus0" diff --git a/sys/mips/conf/BERI_DE4_MDROOT b/sys/mips/conf/BERI_DE4_MDROOT index 75d9b4e..37acf4b 100644 --- a/sys/mips/conf/BERI_DE4_MDROOT +++ b/sys/mips/conf/BERI_DE4_MDROOT @@ -24,6 +24,7 @@ options ROOTDEVNAME=\"ufs:md0\" device altera_avgen device altera_jtag_uart device altera_sdcard +device terasic_de4led device terasic_mtl device isf diff --git a/sys/mips/conf/BERI_DE4_SDROOT b/sys/mips/conf/BERI_DE4_SDROOT index a638e46..304cce5 100644 --- a/sys/mips/conf/BERI_DE4_SDROOT +++ b/sys/mips/conf/BERI_DE4_SDROOT @@ -17,6 +17,7 @@ options ROOTDEVNAME=\"ufs:altera_sdcard0\" device altera_avgen device altera_jtag_uart device altera_sdcard +device terasic_de4led device terasic_mtl device isf |