freebsd-dev/sys/dev/terasic/mtl/terasic_mtl_pixel.c
Robert Watson ec5bd1da7d 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
2012-08-25 22:35:29 +00:00

160 lines
4.6 KiB
C

/*-
* 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);
}