aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/terasic
diff options
context:
space:
mode:
authorRobert Watson <rwatson@FreeBSD.org>2012-08-25 22:35:29 +0000
committerRobert Watson <rwatson@FreeBSD.org>2012-08-25 22:35:29 +0000
commitec5bd1da7d0b0d636e67c4a12eb45c31c1371550 (patch)
tree3e28595bf2655db19f449d3fff418afa3c3779b7 /sys/dev/terasic
parent5b88bb19d64bf62cd29f0af967b95b08ec43dce0 (diff)
downloadsrc-ec5bd1da7d0b0d636e67c4a12eb45c31c1371550.tar.gz
src-ec5bd1da7d0b0d636e67c4a12eb45c31c1371550.zip
Add terasic_mtl(4), a device driver for the Terasic Multi-Touch LCD,
used with Terasic's DE-4 and other similar FPGA boards. This display is 800x480 and includes a capacitive touch screen, multi-touch gesture recognition, etc. This device driver depends on a Cambridge- provided IP core that allows the MTL device to be hooked up to the Altera Avalon SoC bus, and also provides a VGA-like text frame buffer. Although it is compiled as a single device driver, it actually implements a number of different device nodes exporting various aspects of this multi-function device to userspace: - Simple memory-mapped driver for the MTL 24-bit pixel frame buffer. - Simple memory-mapped driver for the MTL control register set. - Simple memory-mapped driver for the MTL text frame buffer. - syscons attachment for the MTL text frame buffer. This driver attaches directly to Nexus as is common for SoC device drivers, and for the time being is considered BERI-specific, although in principle it might be used with other hard and soft cores on Altera FPGAs. Control registers, including touchscreen input, are simply memory mapped; in the future it would be desirable to hook up a more conventional device node that can stream events, support kqueue(2)/ poll(2)/select(2), etc. This is the first use of syscons on MIPS, as far as I can tell, and there are some loose ends, such as an inability to use the hardware cursor. More fundamentally, it appears that syscons(4) assumes that either a host is PC-like (i386, amd64) *or* it must be using a graphical frame buffer. While the MTL supports a graphical frame buffer, using the text frame buffer is preferable for console use. Fixing this issue in syscons(4) requires non-trivial changes, as the text frame buffer support assumes that direct memory access can be done to the text frame buffer without using bus accessor methods, which is not the case on MIPS. As a workaround for this, we instead double-buffer and pretend to be a graphical frame buffer exposing text accessor methods, leading to some quirks in syscons behaviour. Sponsored by: DARPA, AFRL
Notes
Notes: svn path=/head/; revision=239691
Diffstat (limited to 'sys/dev/terasic')
-rw-r--r--sys/dev/terasic/mtl/terasic_mtl.c110
-rw-r--r--sys/dev/terasic/mtl/terasic_mtl.h210
-rw-r--r--sys/dev/terasic/mtl/terasic_mtl_nexus.c196
-rw-r--r--sys/dev/terasic/mtl/terasic_mtl_pixel.c159
-rw-r--r--sys/dev/terasic/mtl/terasic_mtl_reg.c278
-rw-r--r--sys/dev/terasic/mtl/terasic_mtl_syscons.c490
-rw-r--r--sys/dev/terasic/mtl/terasic_mtl_text.c189
7 files changed, 1632 insertions, 0 deletions
diff --git a/sys/dev/terasic/mtl/terasic_mtl.c b/sys/dev/terasic/mtl/terasic_mtl.c
new file mode 100644
index 000000000000..48e60c765ba2
--- /dev/null
+++ b/sys/dev/terasic/mtl/terasic_mtl.c
@@ -0,0 +1,110 @@
+/*-
+ * 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/consio.h> /* struct vt_mode */
+#include <sys/fbio.h> /* video_adapter_t */
+#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/mtl/terasic_mtl.h>
+
+/*
+ * Device driver for the Terasic Multitouch LCD (MTL). Three separate
+ * sub-drivers that support, respectively, access to device control registers,
+ * the pixel frame buffer, and the text frame buffer. The last of these is
+ * also hooked up to syscons.
+ *
+ * Eventually, the frame buffer control registers and touch screen input FIFO
+ * will end up being separate sub-drivers as well.
+ *
+ * Note: sub-driver detach routines must check whether or not they have
+ * attached as they may be called even if the attach routine hasn't been, on
+ * an error.
+ */
+int
+terasic_mtl_attach(struct terasic_mtl_softc *sc)
+{
+ int error;
+
+ error = terasic_mtl_reg_attach(sc);
+ if (error)
+ goto error;
+ error = terasic_mtl_pixel_attach(sc);
+ if (error)
+ goto error;
+ error = terasic_mtl_text_attach(sc);
+ if (error)
+ goto error;
+ /*
+ * XXXRW: Once we've attached syscons, we can't detach it, so do it
+ * last.
+ */
+ error = terasic_mtl_syscons_attach(sc);
+ if (error)
+ goto error;
+ terasic_mtl_blend_default_set(sc, TERASIC_MTL_COLOR_BLACK);
+ terasic_mtl_blend_pixel_set(sc, TERASIC_MTL_ALPHA_TRANSPARENT);
+ terasic_mtl_blend_textfg_set(sc, TERASIC_MTL_ALPHA_OPAQUE);
+ terasic_mtl_blend_textbg_set(sc, TERASIC_MTL_ALPHA_OPAQUE);
+ return (0);
+error:
+ terasic_mtl_text_detach(sc);
+ terasic_mtl_pixel_detach(sc);
+ terasic_mtl_reg_detach(sc);
+ return (error);
+}
+
+void
+terasic_mtl_detach(struct terasic_mtl_softc *sc)
+{
+
+ /* XXXRW: syscons can't detach, but we try anyway, only to panic. */
+ terasic_mtl_syscons_detach(sc);
+
+ /* All other aspects of the driver can detach just fine. */
+ terasic_mtl_text_detach(sc);
+ terasic_mtl_pixel_detach(sc);
+ terasic_mtl_reg_detach(sc);
+}
diff --git a/sys/dev/terasic/mtl/terasic_mtl.h b/sys/dev/terasic/mtl/terasic_mtl.h
new file mode 100644
index 000000000000..ae4106735e09
--- /dev/null
+++ b/sys/dev/terasic/mtl/terasic_mtl.h
@@ -0,0 +1,210 @@
+/*-
+ * 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_MTL_H_
+#define _DEV_TERASIC_MTL_H_
+
+struct terasic_mtl_softc {
+ /*
+ * syscons requires that its video_adapter_t be at the front of the
+ * softc, so place syscons fields first, which we otherwise would
+ * probably not do.
+ */
+ video_adapter_t mtl_va;
+
+ /*
+ * Bus-related fields.
+ */
+ device_t mtl_dev;
+ int mtl_unit;
+
+ /*
+ * The MTL driver doesn't require a lot of synchronisation; however,
+ * the lock is used to protect read-modify-write operations on MTL
+ * registers.
+ */
+ struct mtx mtl_lock;
+
+ /*
+ * Control register device -- mappable from userspace.
+ */
+ struct cdev *mtl_reg_cdev;
+ struct resource *mtl_reg_res;
+ int mtl_reg_rid;
+
+ /*
+ * Graphics frame buffer device -- mappable form userspace.
+ */
+ struct cdev *mtl_pixel_cdev;
+ struct resource *mtl_pixel_res;
+ int mtl_pixel_rid;
+
+ /*
+ * Text frame buffer device -- mappable from userspace, and syscons
+ * hookup.
+ */
+ struct cdev *mtl_text_cdev;
+ struct resource *mtl_text_res;
+ int mtl_text_rid;
+ uint16_t *mtl_text_soft;
+};
+
+#define TERASIC_MTL_LOCK(sc) mtx_lock(&(sc)->mtl_lock)
+#define TERASIC_MTL_LOCK_ASSERT(sc) mtx_assert(&(sc)->mtl_lock, MA_OWNED)
+#define TERASIC_MTL_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtl_lock)
+#define TERASIC_MTL_LOCK_INIT(sc) mtx_init(&(sc)->mtl_lock, \
+ "terasic_mtl", NULL, MTX_DEF)
+#define TERASIC_MTL_UNLOCK(sc) mtx_unlock(&(sc)->mtl_lock)
+
+/*
+ * Constant properties of the MTL text frame buffer.
+ */
+#define TERASIC_MTL_COLS 100
+#define TERASIC_MTL_ROWS 40
+
+/*
+ * MTL control register offsets.
+ */
+#define TERASIC_MTL_OFF_BLEND 0
+#define TERASIC_MTL_OFF_TEXTCURSOR 4
+#define TERASIC_MTL_OFF_TEXTFRAMEBUFADDR 8
+#define TERASIC_MTL_OFF_TOUCHPOINT_X1 12
+#define TERASIC_MTL_OFF_TOUCHPOINT_Y1 16
+#define TERASIC_MTL_OFF_TOUCHPOINT_X2 20
+#define TERASIC_MTL_OFF_TOUCHPOINT_Y2 24
+#define TERASIC_MTL_OFF_TOUCHGESTURE 28
+
+/*
+ * Constants to help interpret various control registers.
+ */
+#define TERASIC_MTL_BLEND_DEFAULT_MASK 0x0f000000
+#define TERASIC_MTL_BLEND_DEFAULT_SHIFT 24
+#define TERASIC_MTL_BLEND_PIXEL_MASK 0x00ff0000
+#define TERASIC_MTL_BLEND_PIXEL_SHIFT 16
+#define TERASIC_MTL_BLEND_TEXTFG_MASK 0x0000ff00
+#define TERASIC_MTL_BLEND_TEXTFG_SHIFT 8
+#define TERASIC_MTL_BLEND_TEXTBG_MASK 0x000000ff
+#define TERASIC_MTL_BLEND_TEXTBG_SHIFT 0
+#define TERASIC_MTL_TEXTCURSOR_COL_MASK 0xff00
+#define TERASIC_MTL_TEXTCURSOR_COL_SHIFT 8
+#define TERASIC_MTL_TEXTCURSOR_ROW_MASK 0xff
+
+/*
+ * Colours used both by VGA-like text rendering, and for the default display
+ * colour.
+ */
+#define TERASIC_MTL_COLOR_BLACK 0
+#define TERASIC_MTL_COLOR_DARKBLUE 1
+#define TERASIC_MTL_COLOR_DARKGREEN 2
+#define TERASIC_MTL_COLOR_DARKCYAN 3
+#define TERASIC_MTL_COLOR_DARKRED 4
+#define TERASIC_MTL_COLOR_DARKMAGENTA 5
+#define TERASIC_MTL_COLOR_BROWN 6
+#define TERASIC_MTL_COLOR_LIGHTGREY 7
+#define TERASIC_MTL_COLOR_DARKGREY 8
+#define TERASIC_MTL_COLOR_LIGHTBLUE 9
+#define TERASIC_MTL_COLOR_LIGHTGREEN 10
+#define TERASIC_MTL_COLOR_LIGHTCYAN 11
+#define TERASIC_MTL_COLOR_LIGHTRED 12
+#define TERASIC_MTL_COLOR_LIGHTMAGENTA 13
+#define TERASIC_MTL_COLOR_LIGHTYELLOW 14
+#define TERASIC_MTL_COLOR_WHITE 15
+#define TERASIC_MTL_COLORMASK_BLINK 0x80
+
+/*
+ * Constants to help interpret the text frame buffer.
+ */
+#define TERASIC_MTL_TEXTFRAMEBUF_EXPECTED_ADDR 0x0177000
+#define TERASIC_MTL_TEXTFRAMEBUF_CHAR_SHIFT 0
+#define TERASIC_MTL_TEXTFRAMEBUF_ATTR_SHIFT 8
+
+/*
+ * Alpha-blending constants.
+ */
+#define TERASIC_MTL_ALPHA_TRANSPARENT 0
+#define TERASIC_MTL_ALPHA_OPAQUE 255
+
+/*
+ * Driver setup routines from the bus attachment/teardown.
+ */
+int terasic_mtl_attach(struct terasic_mtl_softc *sc);
+void terasic_mtl_detach(struct terasic_mtl_softc *sc);
+
+/*
+ * Sub-driver setup routines.
+ */
+int terasic_mtl_pixel_attach(struct terasic_mtl_softc *sc);
+void terasic_mtl_pixel_detach(struct terasic_mtl_softc *sc);
+int terasic_mtl_reg_attach(struct terasic_mtl_softc *sc);
+void terasic_mtl_reg_detach(struct terasic_mtl_softc *sc);
+int terasic_mtl_syscons_attach(struct terasic_mtl_softc *sc);
+void terasic_mtl_syscons_detach(struct terasic_mtl_softc *sc);
+int terasic_mtl_text_attach(struct terasic_mtl_softc *sc);
+void terasic_mtl_text_detach(struct terasic_mtl_softc *sc);
+
+/*
+ * Control register I/O routines.
+ */
+void terasic_mtl_reg_blank(struct terasic_mtl_softc *sc);
+
+void terasic_mtl_reg_blend_get(struct terasic_mtl_softc *sc,
+ uint32_t *blendp);
+void terasic_mtl_reg_blend_set(struct terasic_mtl_softc *sc,
+ uint32_t blend);
+void terasic_mtl_reg_textcursor_get(struct terasic_mtl_softc *sc,
+ uint8_t *colp, uint8_t *rowp);
+void terasic_mtl_reg_textcursor_set(struct terasic_mtl_softc *sc,
+ uint8_t col, uint8_t row);
+void terasic_mtl_reg_textframebufaddr_get(struct terasic_mtl_softc *sc,
+ uint32_t *addrp);
+void terasic_mtl_reg_textframebufaddr_set(struct terasic_mtl_softc *sc,
+ uint32_t addr);
+
+/*
+ * Read-modify-write updates of sub-bytes of the blend register.
+ */
+void terasic_mtl_blend_default_set(struct terasic_mtl_softc *sc,
+ uint8_t colour);
+void terasic_mtl_blend_pixel_set(struct terasic_mtl_softc *sc,
+ uint8_t alpha);
+void terasic_mtl_blend_textfg_set(struct terasic_mtl_softc *sc,
+ uint8_t alpha);
+void terasic_mtl_blend_textbg_set(struct terasic_mtl_softc *sc,
+ uint8_t alpha);
+
+/*
+ * Text frame buffer I/O routines.
+ */
+void terasic_mtl_text_putc(struct terasic_mtl_softc *sc, u_int x, u_int y,
+ uint8_t c, uint8_t a);
+
+#endif /* _DEV_TERASIC_MTL_H_ */
diff --git a/sys/dev/terasic/mtl/terasic_mtl_nexus.c b/sys/dev/terasic/mtl/terasic_mtl_nexus.c
new file mode 100644
index 000000000000..4a6f2ba5c1f9
--- /dev/null
+++ b/sys/dev/terasic/mtl/terasic_mtl_nexus.c
@@ -0,0 +1,196 @@
+/*-
+ * 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/consio.h> /* struct vt_mode */
+#include <sys/fbio.h> /* video_adapter_t */
+#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/mtl/terasic_mtl.h>
+
+static int
+terasic_mtl_nexus_probe(device_t dev)
+{
+
+ device_set_desc(dev, "Terasic Multi-touch LCD (MTL)");
+ return (BUS_PROBE_DEFAULT);
+}
+
+static int
+terasic_mtl_nexus_attach(device_t dev)
+{
+ struct terasic_mtl_softc *sc;
+ u_long pixel_maddr, text_maddr, reg_maddr;
+ u_long pixel_msize, text_msize, reg_msize;
+ int error;
+
+ sc = device_get_softc(dev);
+ sc->mtl_dev = dev;
+ sc->mtl_unit = device_get_unit(dev);
+
+ /*
+ * Query non-standard hints to find the locations of our two memory
+ * regions. Enforce certain alignment and size requirements.
+ */
+ if (resource_long_value(device_get_name(dev), device_get_unit(dev),
+ "reg_maddr", &reg_maddr) != 0 || (reg_maddr % PAGE_SIZE != 0)) {
+ device_printf(dev, "improper register address");
+ return (ENXIO);
+ }
+ if (resource_long_value(device_get_name(dev), device_get_unit(dev),
+ "reg_msize", &reg_msize) != 0 || (reg_msize % PAGE_SIZE != 0)) {
+ device_printf(dev, "improper register size");
+ return (ENXIO);
+ }
+ if (resource_long_value(device_get_name(dev), device_get_unit(dev),
+ "pixel_maddr", &pixel_maddr) != 0 ||
+ (pixel_maddr % PAGE_SIZE != 0)) {
+ device_printf(dev, "improper pixel frame buffer address");
+ return (ENXIO);
+ }
+ if (resource_long_value(device_get_name(dev), device_get_unit(dev),
+ "pixel_msize", &pixel_msize) != 0 ||
+ (pixel_msize % PAGE_SIZE != 0)) {
+ device_printf(dev, "improper pixel frame buffer size");
+ return (ENXIO);
+ }
+ if (resource_long_value(device_get_name(dev), device_get_unit(dev),
+ "text_maddr", &text_maddr) != 0 ||
+ (text_maddr % PAGE_SIZE != 0)) {
+ device_printf(dev, "improper text frame buffer address");
+ return (ENXIO);
+ }
+ if (resource_long_value(device_get_name(dev), device_get_unit(dev),
+ "text_msize", &text_msize) != 0 ||
+ (text_msize % PAGE_SIZE != 0)) {
+ device_printf(dev, "improper text frame buffer size");
+ return (ENXIO);
+ }
+
+ /*
+ * Allocate resources.
+ */
+ sc->mtl_reg_rid = 0;
+ sc->mtl_reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
+ &sc->mtl_reg_rid, reg_maddr, reg_maddr + reg_msize - 1,
+ reg_msize, RF_ACTIVE);
+ if (sc->mtl_reg_res == NULL) {
+ device_printf(dev, "couldn't map register memory\n");
+ error = ENXIO;
+ goto error;
+ }
+ device_printf(sc->mtl_dev, "registers at mem %p-%p\n",
+ (void *)reg_maddr, (void *)(reg_maddr + reg_msize));
+ sc->mtl_pixel_rid = 0;
+ sc->mtl_pixel_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
+ &sc->mtl_pixel_rid, pixel_maddr, pixel_maddr + pixel_msize - 1,
+ pixel_msize, RF_ACTIVE);
+ if (sc->mtl_pixel_res == NULL) {
+ device_printf(dev, "couldn't map pixel memory\n");
+ error = ENXIO;
+ goto error;
+ }
+ device_printf(sc->mtl_dev, "pixel frame buffer at mem %p-%p\n",
+ (void *)pixel_maddr, (void *)(pixel_maddr + pixel_msize));
+ sc->mtl_text_rid = 0;
+ sc->mtl_text_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
+ &sc->mtl_text_rid, text_maddr, text_maddr + text_msize - 1,
+ text_msize, RF_ACTIVE);
+ if (sc->mtl_text_res == NULL) {
+ device_printf(dev, "couldn't map text memory\n");
+ error = ENXIO;
+ goto error;
+ }
+ device_printf(sc->mtl_dev, "text frame buffer at mem %p-%p\n",
+ (void *)text_maddr, (void *)(text_maddr + text_msize));
+ error = terasic_mtl_attach(sc);
+ if (error == 0)
+ return (0);
+error:
+ if (sc->mtl_text_res != NULL)
+ bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_text_rid,
+ sc->mtl_text_res);
+ if (sc->mtl_pixel_res != NULL)
+ bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_pixel_rid,
+ sc->mtl_pixel_res);
+ if (sc->mtl_reg_res != NULL)
+ bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_reg_rid,
+ sc->mtl_reg_res);
+ return (error);
+}
+
+static int
+terasic_mtl_nexus_detach(device_t dev)
+{
+ struct terasic_mtl_softc *sc;
+
+ sc = device_get_softc(dev);
+ terasic_mtl_detach(sc);
+ bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_text_rid,
+ sc->mtl_text_res);
+ bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_pixel_rid,
+ sc->mtl_pixel_res);
+ bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_reg_rid,
+ sc->mtl_reg_res);
+ return (0);
+}
+
+static device_method_t terasic_mtl_nexus_methods[] = {
+ DEVMETHOD(device_probe, terasic_mtl_nexus_probe),
+ DEVMETHOD(device_attach, terasic_mtl_nexus_attach),
+ DEVMETHOD(device_detach, terasic_mtl_nexus_detach),
+ { 0, 0 }
+};
+
+static driver_t terasic_mtl_nexus_driver = {
+ "terasic_mtl",
+ terasic_mtl_nexus_methods,
+ sizeof(struct terasic_mtl_softc),
+};
+
+static devclass_t terasic_mtl_devclass;
+
+DRIVER_MODULE(mtl, nexus, terasic_mtl_nexus_driver, terasic_mtl_devclass, 0,
+ 0);
diff --git a/sys/dev/terasic/mtl/terasic_mtl_pixel.c b/sys/dev/terasic/mtl/terasic_mtl_pixel.c
new file mode 100644
index 000000000000..a7cbbde2c4af
--- /dev/null
+++ b/sys/dev/terasic/mtl/terasic_mtl_pixel.c
@@ -0,0 +1,159 @@
+/*-
+ * 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/conf.h>
+#include <sys/consio.h> /* struct vt_mode */
+#include <sys/fbio.h> /* video_adapter_t */
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/rman.h>
+#include <sys/uio.h>
+
+#include <machine/bus.h>
+#include <machine/resource.h>
+#include <machine/vm.h>
+
+#include <dev/terasic/mtl/terasic_mtl.h>
+
+static d_mmap_t terasic_mtl_pixel_mmap;
+static d_read_t terasic_mtl_pixel_read;
+static d_write_t terasic_mtl_pixel_write;
+
+static struct cdevsw mtl_pixel_cdevsw = {
+ .d_version = D_VERSION,
+ .d_mmap = terasic_mtl_pixel_mmap,
+ .d_read = terasic_mtl_pixel_read,
+ .d_write = terasic_mtl_pixel_write,
+ .d_name = "terasic_mtl_pixel",
+};
+
+/*
+ * All I/O to/from the MTL pixel device must be 32-bit, and aligned to 32-bit.
+ */
+static int
+terasic_mtl_pixel_read(struct cdev *dev, struct uio *uio, int flag)
+{
+ struct terasic_mtl_softc *sc;
+ u_long offset, size;
+ uint32_t v;
+ int error;
+
+ if (uio->uio_offset < 0 || uio->uio_offset % 4 != 0 ||
+ uio->uio_resid % 4 != 0)
+ return (ENODEV);
+ sc = dev->si_drv1;
+ size = rman_get_size(sc->mtl_pixel_res);
+ error = 0;
+ if ((uio->uio_offset + uio->uio_resid < 0) ||
+ (uio->uio_offset + uio->uio_resid > size))
+ return (ENODEV);
+ while (uio->uio_resid > 0) {
+ offset = uio->uio_offset;
+ if (offset + sizeof(v) > size)
+ return (ENODEV);
+ v = bus_read_4(sc->mtl_pixel_res, offset);
+ error = uiomove(&v, sizeof(v), uio);
+ if (error)
+ return (error);
+ }
+ return (error);
+}
+
+static int
+terasic_mtl_pixel_write(struct cdev *dev, struct uio *uio, int flag)
+{
+ struct terasic_mtl_softc *sc;
+ u_long offset, size;
+ uint32_t v;
+ int error;
+
+ if (uio->uio_offset < 0 || uio->uio_offset % 4 != 0 ||
+ uio->uio_resid % 4 != 0)
+ return (ENODEV);
+ sc = dev->si_drv1;
+ size = rman_get_size(sc->mtl_pixel_res);
+ error = 0;
+ while (uio->uio_resid > 0) {
+ offset = uio->uio_offset;
+ if (offset + sizeof(v) > size)
+ return (ENODEV);
+ error = uiomove(&v, sizeof(v), uio);
+ if (error)
+ return (error);
+ bus_write_4(sc->mtl_pixel_res, offset, v);
+ }
+ return (error);
+}
+
+static int
+terasic_mtl_pixel_mmap(struct cdev *dev, vm_ooffset_t offset,
+ vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr)
+{
+ struct terasic_mtl_softc *sc;
+ int error;
+
+ sc = dev->si_drv1;
+ error = 0;
+ if (trunc_page(offset) == offset &&
+ rman_get_size(sc->mtl_pixel_res) >= offset + PAGE_SIZE) {
+ *paddr = rman_get_start(sc->mtl_pixel_res) + offset;
+ *memattr = VM_MEMATTR_UNCACHEABLE;
+ } else
+ error = ENODEV;
+ return (error);
+}
+
+int
+terasic_mtl_pixel_attach(struct terasic_mtl_softc *sc)
+{
+
+ sc->mtl_pixel_cdev = make_dev(&mtl_pixel_cdevsw, sc->mtl_unit,
+ UID_ROOT, GID_WHEEL, 0400, "mtl_pixel%d", sc->mtl_unit);
+ if (sc->mtl_pixel_cdev == NULL) {
+ device_printf(sc->mtl_dev, "%s: make_dev failed\n", __func__);
+ return (ENXIO);
+ }
+ /* XXXRW: Slight race between make_dev(9) and here. */
+ sc->mtl_pixel_cdev->si_drv1 = sc;
+ return (0);
+}
+
+void
+terasic_mtl_pixel_detach(struct terasic_mtl_softc *sc)
+{
+
+ if (sc->mtl_pixel_cdev != NULL)
+ destroy_dev(sc->mtl_pixel_cdev);
+}
diff --git a/sys/dev/terasic/mtl/terasic_mtl_reg.c b/sys/dev/terasic/mtl/terasic_mtl_reg.c
new file mode 100644
index 000000000000..542bd983ef11
--- /dev/null
+++ b/sys/dev/terasic/mtl/terasic_mtl_reg.c
@@ -0,0 +1,278 @@
+/*-
+ * 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/conf.h>
+#include <sys/consio.h> /* struct vt_mode */
+#include <sys/endian.h>
+#include <sys/fbio.h> /* video_adapter_t */
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/rman.h>
+#include <sys/systm.h>
+#include <sys/uio.h>
+
+#include <machine/bus.h>
+#include <machine/resource.h>
+#include <machine/vm.h>
+
+#include <dev/terasic/mtl/terasic_mtl.h>
+
+static d_mmap_t terasic_mtl_reg_mmap;
+static d_read_t terasic_mtl_reg_read;
+static d_write_t terasic_mtl_reg_write;
+
+static struct cdevsw terasic_mtl_reg_cdevsw = {
+ .d_version = D_VERSION,
+ .d_mmap = terasic_mtl_reg_mmap,
+ .d_read = terasic_mtl_reg_read,
+ .d_write = terasic_mtl_reg_write,
+ .d_name = "terasic_mtl_reg",
+};
+
+/*
+ * All I/O to/from the MTL register device must be 32-bit, and aligned to
+ * 32-bit.
+ */
+static int
+terasic_mtl_reg_read(struct cdev *dev, struct uio *uio, int flag)
+{
+ struct terasic_mtl_softc *sc;
+ u_long offset, size;
+ uint32_t v;
+ int error;
+
+ if (uio->uio_offset < 0 || uio->uio_offset % 4 != 0 ||
+ uio->uio_resid % 4 != 0)
+ return (ENODEV);
+ sc = dev->si_drv1;
+ size = rman_get_size(sc->mtl_reg_res);
+ error = 0;
+ if ((uio->uio_offset + uio->uio_resid < 0) ||
+ (uio->uio_offset + uio->uio_resid > size))
+ return (ENODEV);
+ while (uio->uio_resid > 0) {
+ offset = uio->uio_offset;
+ if (offset + sizeof(v) > size)
+ return (ENODEV);
+ v = bus_read_4(sc->mtl_reg_res, offset);
+ error = uiomove(&v, sizeof(v), uio);
+ if (error)
+ return (error);
+ }
+ return (error);
+}
+
+static int
+terasic_mtl_reg_write(struct cdev *dev, struct uio *uio, int flag)
+{
+ struct terasic_mtl_softc *sc;
+ u_long offset, size;
+ uint32_t v;
+ int error;
+
+ if (uio->uio_offset < 0 || uio->uio_offset % 4 != 0 ||
+ uio->uio_resid % 4 != 0)
+ return (ENODEV);
+ sc = dev->si_drv1;
+ size = rman_get_size(sc->mtl_reg_res);
+ error = 0;
+ while (uio->uio_resid > 0) {
+ offset = uio->uio_offset;
+ if (offset + sizeof(v) > size)
+ return (ENODEV);
+ error = uiomove(&v, sizeof(v), uio);
+ if (error)
+ return (error);
+ bus_write_4(sc->mtl_reg_res, offset, v);
+ }
+ return (error);
+}
+
+static int
+terasic_mtl_reg_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
+ int nprot, vm_memattr_t *memattr)
+{
+ struct terasic_mtl_softc *sc;
+ int error;
+
+ sc = dev->si_drv1;
+ error = 0;
+ if (trunc_page(offset) == offset &&
+ rman_get_size(sc->mtl_reg_res) >= offset + PAGE_SIZE) {
+ *paddr = rman_get_start(sc->mtl_reg_res) + offset;
+ *memattr = VM_MEMATTR_UNCACHEABLE;
+ } else
+ error = ENODEV;
+ return (error);
+}
+
+void
+terasic_mtl_reg_blend_get(struct terasic_mtl_softc *sc, uint32_t *blendp)
+{
+
+ *blendp = le32toh(bus_read_4(sc->mtl_reg_res, TERASIC_MTL_OFF_BLEND));
+}
+
+void
+terasic_mtl_reg_blend_set(struct terasic_mtl_softc *sc, uint32_t blend)
+{
+
+ bus_write_4(sc->mtl_reg_res, TERASIC_MTL_OFF_BLEND, htole32(blend));
+}
+
+void
+terasic_mtl_blend_default_set(struct terasic_mtl_softc *sc, uint8_t colour)
+{
+ uint32_t v;
+
+ TERASIC_MTL_LOCK(sc);
+ terasic_mtl_reg_blend_get(sc, &v);
+ v &= ~TERASIC_MTL_BLEND_DEFAULT_MASK;
+ v |= colour << TERASIC_MTL_BLEND_DEFAULT_SHIFT;
+ terasic_mtl_reg_blend_set(sc, v);
+ TERASIC_MTL_UNLOCK(sc);
+}
+
+void
+terasic_mtl_blend_pixel_set(struct terasic_mtl_softc *sc, uint8_t alpha)
+{
+ uint32_t v;
+
+ TERASIC_MTL_LOCK(sc);
+ terasic_mtl_reg_blend_get(sc, &v);
+ v &= ~TERASIC_MTL_BLEND_PIXEL_MASK;
+ v |= alpha << TERASIC_MTL_BLEND_PIXEL_SHIFT;
+ terasic_mtl_reg_blend_set(sc, v);
+ TERASIC_MTL_UNLOCK(sc);
+}
+
+void
+terasic_mtl_blend_textfg_set(struct terasic_mtl_softc *sc, uint8_t alpha)
+{
+ uint32_t v;
+
+ TERASIC_MTL_LOCK(sc);
+ terasic_mtl_reg_blend_get(sc, &v);
+ v &= ~TERASIC_MTL_BLEND_TEXTFG_MASK;
+ v |= alpha << TERASIC_MTL_BLEND_TEXTFG_SHIFT;
+ terasic_mtl_reg_blend_set(sc, v);
+ TERASIC_MTL_UNLOCK(sc);
+}
+
+void
+terasic_mtl_blend_textbg_set(struct terasic_mtl_softc *sc, uint8_t alpha)
+{
+ uint32_t v;
+
+ TERASIC_MTL_LOCK(sc);
+ terasic_mtl_reg_blend_get(sc, &v);
+ v &= ~TERASIC_MTL_BLEND_TEXTBG_MASK;
+ v |= alpha << TERASIC_MTL_BLEND_TEXTBG_SHIFT;
+ terasic_mtl_reg_blend_set(sc, v);
+ TERASIC_MTL_UNLOCK(sc);
+}
+
+void
+terasic_mtl_reg_textcursor_get(struct terasic_mtl_softc *sc, uint8_t *colp,
+ uint8_t *rowp)
+{
+ uint32_t v;
+
+ v = bus_read_4(sc->mtl_reg_res, TERASIC_MTL_OFF_TEXTCURSOR);
+ v = le32toh(v);
+ *colp = (v & TERASIC_MTL_TEXTCURSOR_COL_MASK) >>
+ TERASIC_MTL_TEXTCURSOR_COL_SHIFT;
+ *rowp = (v & TERASIC_MTL_TEXTCURSOR_ROW_MASK);
+}
+
+void
+terasic_mtl_reg_textcursor_set(struct terasic_mtl_softc *sc, uint8_t col,
+ uint8_t row)
+{
+ uint32_t v;
+
+ v = (col << TERASIC_MTL_TEXTCURSOR_COL_SHIFT) | row;
+ v = htole32(v);
+ bus_write_4(sc->mtl_reg_res, TERASIC_MTL_OFF_TEXTCURSOR, v);
+}
+
+void
+terasic_mtl_reg_blank(struct terasic_mtl_softc *sc)
+{
+
+ device_printf(sc->mtl_dev, "%s: not yet", __func__);
+}
+
+void
+terasic_mtl_reg_textframebufaddr_get(struct terasic_mtl_softc *sc,
+ uint32_t *addrp)
+{
+ uint32_t addr;
+
+ addr = bus_read_4(sc->mtl_reg_res, TERASIC_MTL_OFF_TEXTFRAMEBUFADDR);
+ *addrp = le32toh(addr);
+}
+
+void
+terasic_mtl_reg_textframebufaddr_set(struct terasic_mtl_softc *sc,
+ uint32_t addr)
+{
+
+ addr = htole32(addr);
+ bus_write_4(sc->mtl_reg_res, TERASIC_MTL_OFF_TEXTFRAMEBUFADDR, addr);
+}
+
+int
+terasic_mtl_reg_attach(struct terasic_mtl_softc *sc)
+{
+
+ sc->mtl_reg_cdev = make_dev(&terasic_mtl_reg_cdevsw, sc->mtl_unit,
+ UID_ROOT, GID_WHEEL, 0400, "mtl_reg%d", sc->mtl_unit);
+ if (sc->mtl_reg_cdev == NULL) {
+ device_printf(sc->mtl_dev, "%s: make_dev failed\n", __func__);
+ return (ENXIO);
+ }
+ /* XXXRW: Slight race between make_dev(9) and here. */
+ sc->mtl_reg_cdev->si_drv1 = sc;
+ return (0);
+}
+
+void
+terasic_mtl_reg_detach(struct terasic_mtl_softc *sc)
+{
+
+ if (sc->mtl_reg_cdev != NULL)
+ destroy_dev(sc->mtl_reg_cdev);
+}
diff --git a/sys/dev/terasic/mtl/terasic_mtl_syscons.c b/sys/dev/terasic/mtl/terasic_mtl_syscons.c
new file mode 100644
index 000000000000..97d989161f91
--- /dev/null
+++ b/sys/dev/terasic/mtl/terasic_mtl_syscons.c
@@ -0,0 +1,490 @@
+/*-
+ * 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/conf.h>
+#include <sys/consio.h>
+#include <sys/fbio.h>
+#include <sys/kbio.h>
+#include <sys/kernel.h>
+#include <sys/lock.h>
+#include <sys/malloc.h>
+#include <sys/mutex.h>
+#include <sys/module.h>
+#include <sys/rman.h>
+#include <sys/systm.h>
+#include <sys/uio.h>
+
+#include <machine/bus.h>
+#include <machine/resource.h>
+#include <machine/vm.h>
+
+#include <dev/fb/fbreg.h>
+
+#include <dev/kbd/kbdreg.h>
+
+#include <dev/syscons/syscons.h>
+
+#include <dev/terasic/mtl/terasic_mtl.h>
+
+/*
+ * Terasic Multitouch LCD (MTL) syscons driver. Implement syscons(4)'s
+ * video_switch_t KPI using MTL's text frame buffer. In principle, we could
+ * actually implement sc_rndr_sw_t, since the MTL text frame buffer implements
+ * a VGA-like memory mapping. However, this requires a lot more book-keeping
+ * with only minor performance improvements (avoiding indirection), as well as
+ * introducing potential endianness issues. Instead we accept one additional
+ * memory copy between a software frame buffer and the hardware frame buffer
+ * and the generic frame buffer (gfb) framework.
+ */
+
+MALLOC_DEFINE(M_TERASIC_MTL, "mtl_syscons", "MTL syscons frame buffer");
+
+/*
+ * Run early so that boot-time console support can be initialised before
+ * newbus gets around to configuring syscons.
+ *
+ * XXXRW: We may need to do more here in order to see earlier boot messages.
+ */
+static int
+terasic_mtl_syscons_configure(int flags)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (0);
+}
+
+static int
+terasic_mtl_vidsw_probe(int unit, video_adapter_t **adp, void *args,
+ int flags)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (0);
+}
+
+static int
+terasic_mtl_vidsw_init(int unit, video_adapter_t *adp, int flags)
+{
+ struct terasic_mtl_softc *sc;
+ video_info_t *vi;
+
+ sc = (struct terasic_mtl_softc *)adp;
+
+ vi = &adp->va_info;
+ vid_init_struct(adp, "terasic_mtl_syscons", -1, unit);
+
+ vi->vi_width = TERASIC_MTL_COLS;
+ if (vi->vi_width > COL)
+ vi->vi_width = COL;
+ vi->vi_height = TERASIC_MTL_ROWS;
+ if (vi->vi_height > ROW)
+ vi->vi_height = ROW;
+
+ /*
+ * XXXRW: It's not quite clear how these should be initialised.
+ */
+ vi->vi_cwidth = 0;
+ vi->vi_cheight = 0;
+ vi->vi_flags = V_INFO_COLOR;
+ vi->vi_mem_model = V_INFO_MM_OTHER;
+
+ /*
+ * Software text frame buffer from which we update the actual MTL
+ * frame buffer when asked to.
+ */
+ adp->va_window = (vm_offset_t)sc->mtl_text_soft;
+
+ /*
+ * Declare video adapter capabilities -- at this point, simply color
+ * support, as MTL doesn't support screen borders, font loading, or
+ * mode changes.
+ *
+ * XXXRW: It's unclear if V_ADP_INITIALIZED is needed here; other
+ * syscons(4) drivers are inconsistent about this and
+ * V_ADP_REGISTERED.
+ */
+ adp->va_flags |= V_ADP_COLOR | V_ADP_INITIALIZED;
+ if (vid_register(adp) < 0) {
+ device_printf(sc->mtl_dev, "%s: vid_register failed\n",
+ __func__);
+ return (ENXIO);
+ }
+ adp->va_flags |= V_ADP_REGISTERED;
+ return (0);
+}
+
+static int
+terasic_mtl_vidsw_get_info(video_adapter_t *adp, int mode, video_info_t *info)
+{
+
+ bcopy(&adp->va_info, info, sizeof(*info));
+ return (0);
+}
+
+static int
+terasic_mtl_vidsw_query_mode(video_adapter_t *adp, video_info_t *info)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_set_mode(video_adapter_t *adp, int mode)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_save_font(video_adapter_t *adp, int page, int size,
+ int width, u_char *data, int c, int count)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_load_font(video_adapter_t *adp, int page, int size,
+ int width, u_char *data, int c, int count)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_show_font(video_adapter_t *adp, int page)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_save_palette(video_adapter_t *adp, u_char *palette)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_load_palette(video_adapter_t *adp, u_char *palette)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_set_border(video_adapter_t *adp, int border)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_save_state(video_adapter_t *adp, void *p, size_t size)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_load_state(video_adapter_t *adp, void *p)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_set_win_org(video_adapter_t *adp, off_t offset)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_read_hw_cursor(video_adapter_t *adp, int *colp, int *rowp)
+{
+ struct terasic_mtl_softc *sc;
+ uint8_t col, row;
+
+ sc = (struct terasic_mtl_softc *)adp;
+ terasic_mtl_reg_textcursor_get(sc, &col, &row);
+ *colp = col;
+ *rowp = row;
+ return (0);
+}
+
+static int
+terasic_mtl_vidsw_set_hw_cursor(video_adapter_t *adp, int col, int row)
+{
+ struct terasic_mtl_softc *sc;
+
+ sc = (struct terasic_mtl_softc *)adp;
+ terasic_mtl_reg_textcursor_set(sc, col, row);
+ return (0);
+}
+
+static int
+terasic_mtl_vidsw_set_hw_cursor_shape(video_adapter_t *adp, int base,
+ int height, int celsize, int blink)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_blank_display(video_adapter_t *adp, int mode)
+{
+ struct terasic_mtl_softc *sc;
+
+ sc = (struct terasic_mtl_softc *)adp;
+ terasic_mtl_reg_blank(sc);
+ return (0);
+}
+
+static int
+terasic_mtl_vidsw_mmap(video_adapter_t *adp, vm_ooffset_t offset,
+ vm_paddr_t *paddr, int prot, vm_memattr_t *memattr)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
+{
+
+ return (fb_commonioctl(adp, cmd, data));
+}
+
+static int
+terasic_mtl_vidsw_clear(video_adapter_t *adp)
+{
+ struct terasic_mtl_softc *sc;
+
+ sc = (struct terasic_mtl_softc *)adp;
+ printf("%s: not yet terasic_mtl_io_clear(sc);\n", __func__);
+ return (0);
+}
+
+static int
+terasic_mtl_vidsw_fill_rect(video_adapter_t *adp, int val, int x, int y,
+ int cx, int cy)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_bitblt(video_adapter_t *adp, ...)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_diag(video_adapter_t *adp, int level)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_save_cursor_palette(video_adapter_t *adp, u_char *palette)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_load_cursor_palette(video_adapter_t *adp, u_char *palette)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst,
+ int n)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p,
+ uint32_t a, int size, int bpp, int bit_ltor, int byte_ltor)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+static int
+terasic_mtl_vidsw_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c,
+ uint8_t a)
+{
+ struct terasic_mtl_softc *sc;
+ u_int col, row;
+
+ sc = (struct terasic_mtl_softc *)adp;
+ col = (off % adp->va_info.vi_width);
+ row = (off / adp->va_info.vi_width);
+ terasic_mtl_text_putc(sc, col, row, c, a);
+ return (0);
+}
+
+static int
+terasic_mtl_vidsw_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s,
+ int len)
+{
+ int i;
+
+ for (i = 0; i < len; i++)
+ vidd_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
+ return (0);
+}
+
+static int
+terasic_mtl_vidsw_putm(video_adapter_t *adp, int x, int y,
+ uint8_t *pixel_image, uint32_t pixel_mask, int size, int width)
+{
+
+ printf("%s: not yet\n", __func__);
+ return (ENODEV);
+}
+
+/*
+ * XXXRW: For historical reasons, syscons can't register video consoles
+ * without a keyboard implementation. Provide a dummy.
+ */
+static keyboard_switch_t terasic_mtl_keyboard_switch;
+
+static int
+terasic_mtl_kbd_configure(int flags)
+{
+
+ return (0);
+}
+
+KEYBOARD_DRIVER(mtl_kbd, terasic_mtl_keyboard_switch,
+ terasic_mtl_kbd_configure);
+
+int
+terasic_mtl_syscons_attach(struct terasic_mtl_softc *sc)
+{
+ int error;
+
+ sc->mtl_text_soft =
+ malloc(sizeof(uint16_t) * TERASIC_MTL_ROWS * TERASIC_MTL_COLS,
+ M_TERASIC_MTL, M_WAITOK | M_ZERO);
+ error = terasic_mtl_vidsw_init(0, &sc->mtl_va, 0);
+ if (error)
+ goto out;
+ error = sc_attach_unit(sc->mtl_unit, device_get_flags(sc->mtl_dev) |
+ SC_AUTODETECT_KBD);
+ if (error)
+ device_printf(sc->mtl_dev, "%s: sc_attach_unit failed (%d)\n",
+ __func__, error);
+out:
+ if (error)
+ free(sc->mtl_text_soft, M_TERASIC_MTL);
+ return (error);
+}
+
+void
+terasic_mtl_syscons_detach(struct terasic_mtl_softc *sc)
+{
+
+ free(sc->mtl_text_soft, M_TERASIC_MTL);
+ panic("%s: not supported by syscons", __func__);
+}
+
+static video_switch_t terasic_mtl_vidsw = {
+ .probe = terasic_mtl_vidsw_probe,
+ .init = terasic_mtl_vidsw_init,
+ .get_info = terasic_mtl_vidsw_get_info,
+ .query_mode = terasic_mtl_vidsw_query_mode,
+ .set_mode = terasic_mtl_vidsw_set_mode,
+ .save_font = terasic_mtl_vidsw_save_font,
+ .load_font = terasic_mtl_vidsw_load_font,
+ .show_font = terasic_mtl_vidsw_show_font,
+ .save_palette = terasic_mtl_vidsw_save_palette,
+ .load_palette = terasic_mtl_vidsw_load_palette,
+ .set_border = terasic_mtl_vidsw_set_border,
+ .save_state = terasic_mtl_vidsw_save_state,
+ .load_state = terasic_mtl_vidsw_load_state,
+ .set_win_org = terasic_mtl_vidsw_set_win_org,
+ .read_hw_cursor = terasic_mtl_vidsw_read_hw_cursor,
+ .set_hw_cursor = terasic_mtl_vidsw_set_hw_cursor,
+ .set_hw_cursor_shape = terasic_mtl_vidsw_set_hw_cursor_shape,
+ .blank_display = terasic_mtl_vidsw_blank_display,
+ .mmap = terasic_mtl_vidsw_mmap,
+ .ioctl = terasic_mtl_vidsw_ioctl,
+ .clear = terasic_mtl_vidsw_clear,
+ .fill_rect = terasic_mtl_vidsw_fill_rect,
+ .bitblt = terasic_mtl_vidsw_bitblt,
+ .diag = terasic_mtl_vidsw_diag,
+ .save_cursor_palette = terasic_mtl_vidsw_save_cursor_palette,
+ .load_cursor_palette = terasic_mtl_vidsw_load_cursor_palette,
+ .copy = terasic_mtl_vidsw_copy,
+ .putp = terasic_mtl_vidsw_putp,
+ .putc = terasic_mtl_vidsw_putc,
+ .puts = terasic_mtl_vidsw_puts,
+ .putm = terasic_mtl_vidsw_putm,
+};
+VIDEO_DRIVER(terasic_mtl_syscons, terasic_mtl_vidsw,
+ terasic_mtl_syscons_configure);
+extern sc_rndr_sw_t txtrndrsw;
+RENDERER(terasic_mtl_syscons, 0, txtrndrsw, gfb_set);
+RENDERER_MODULE(terasic_mtl_syscons, gfb_set);
diff --git a/sys/dev/terasic/mtl/terasic_mtl_text.c b/sys/dev/terasic/mtl/terasic_mtl_text.c
new file mode 100644
index 000000000000..3b9377f16f54
--- /dev/null
+++ b/sys/dev/terasic/mtl/terasic_mtl_text.c
@@ -0,0 +1,189 @@
+/*-
+ * 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/conf.h>
+#include <sys/consio.h> /* struct vt_mode */
+#include <sys/endian.h>
+#include <sys/fbio.h> /* video_adapter_t */
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/rman.h>
+#include <sys/systm.h>
+#include <sys/uio.h>
+
+#include <machine/bus.h>
+#include <machine/resource.h>
+#include <machine/vm.h>
+
+#include <dev/terasic/mtl/terasic_mtl.h>
+
+static d_mmap_t terasic_mtl_text_mmap;
+static d_read_t terasic_mtl_text_read;
+static d_write_t terasic_mtl_text_write;
+
+static struct cdevsw terasic_mtl_text_cdevsw = {
+ .d_version = D_VERSION,
+ .d_mmap = terasic_mtl_text_mmap,
+ .d_read = terasic_mtl_text_read,
+ .d_write = terasic_mtl_text_write,
+ .d_name = "terasic_mtl_text",
+};
+
+/*
+ * All I/O to/from the mtl device must be 16-bit, and aligned to 16-bit.
+ */
+static int
+terasic_mtl_text_read(struct cdev *dev, struct uio *uio, int flag)
+{
+ struct terasic_mtl_softc *sc;
+ u_long offset, size;
+ uint16_t v;
+ int error;
+
+ if (uio->uio_offset < 0 || uio->uio_offset % 2 != 0 ||
+ uio->uio_resid % 2 != 0)
+ return (ENODEV);
+ sc = dev->si_drv1;
+ size = rman_get_size(sc->mtl_text_res);
+ error = 0;
+ if ((uio->uio_offset + uio->uio_resid < 0) ||
+ (uio->uio_offset + uio->uio_resid > size))
+ return (ENODEV);
+ while (uio->uio_resid > 0) {
+ offset = uio->uio_offset;
+ if (offset + sizeof(v) > size)
+ return (ENODEV);
+ v = bus_read_2(sc->mtl_text_res, offset);
+ error = uiomove(&v, sizeof(v), uio);
+ if (error)
+ return (error);
+ }
+ return (error);
+}
+
+static int
+terasic_mtl_text_write(struct cdev *dev, struct uio *uio, int flag)
+{
+ struct terasic_mtl_softc *sc;
+ u_long offset, size;
+ uint16_t v;
+ int error;
+
+ if (uio->uio_offset < 0 || uio->uio_offset % 2 != 0 ||
+ uio->uio_resid % 2 != 0)
+ return (ENODEV);
+ sc = dev->si_drv1;
+ size = rman_get_size(sc->mtl_text_res);
+ error = 0;
+ while (uio->uio_resid > 0) {
+ offset = uio->uio_offset;
+ if (offset + sizeof(v) > size)
+ return (ENODEV);
+ error = uiomove(&v, sizeof(v), uio);
+ if (error)
+ return (error);
+ bus_write_2(sc->mtl_text_res, offset, v);
+ }
+ return (error);
+}
+
+static int
+terasic_mtl_text_mmap(struct cdev *dev, vm_ooffset_t offset,
+ vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr)
+{
+ struct terasic_mtl_softc *sc;
+ int error;
+
+ sc = dev->si_drv1;
+ error = 0;
+ if (trunc_page(offset) == offset &&
+ rman_get_size(sc->mtl_text_res) >= offset + PAGE_SIZE) {
+ *paddr = rman_get_start(sc->mtl_text_res) + offset;
+ *memattr = VM_MEMATTR_UNCACHEABLE;
+ } else
+ error = ENODEV;
+ return (error);
+}
+
+void
+terasic_mtl_text_putc(struct terasic_mtl_softc *sc, u_int x, u_int y,
+ uint8_t c, uint8_t a)
+{
+ u_int offset;
+ uint16_t v;
+
+ KASSERT(x < TERASIC_MTL_COLS, ("%s: TERASIC_MTL_COLS", __func__));
+ KASSERT(y < TERASIC_MTL_ROWS, ("%s: TERASIC_MTL_ROWS", __func__));
+
+ offset = sizeof(uint16_t) * (x + y * TERASIC_MTL_COLS);
+ v = (c << TERASIC_MTL_TEXTFRAMEBUF_CHAR_SHIFT) |
+ (a << TERASIC_MTL_TEXTFRAMEBUF_ATTR_SHIFT);
+ v = htole16(v);
+ bus_write_2(sc->mtl_text_res, offset, v);
+}
+
+int
+terasic_mtl_text_attach(struct terasic_mtl_softc *sc)
+{
+ uint32_t v;
+
+ terasic_mtl_reg_textframebufaddr_get(sc, &v);
+ if (v != TERASIC_MTL_TEXTFRAMEBUF_EXPECTED_ADDR) {
+ device_printf(sc->mtl_dev, "%s: unexpected text frame buffer "
+ "address (%08x); cannot attach\n", __func__, v);
+ return (ENXIO);
+ }
+
+ sc->mtl_text_cdev = make_dev(&terasic_mtl_text_cdevsw, sc->mtl_unit,
+ UID_ROOT, GID_WHEEL, 0400, "mtl_text%d", sc->mtl_unit);
+ if (sc->mtl_text_cdev == NULL) {
+ device_printf(sc->mtl_dev, "%s: make_dev failed\n", __func__);
+ return (ENXIO);
+ }
+ /* XXXRW: Slight race between make_dev(9) and here. */
+ TERASIC_MTL_LOCK_INIT(sc);
+ sc->mtl_text_cdev->si_drv1 = sc;
+ return (0);
+}
+
+void
+terasic_mtl_text_detach(struct terasic_mtl_softc *sc)
+{
+
+ if (sc->mtl_text_cdev != NULL) {
+ destroy_dev(sc->mtl_text_cdev);
+ TERASIC_MTL_LOCK_DESTROY(sc);
+ }
+}