terasic_mtl_text.c revision 239691
1239691Srwatson/*- 2239691Srwatson * Copyright (c) 2012 Robert N. M. Watson 3239691Srwatson * All rights reserved. 4239691Srwatson * 5239691Srwatson * This software was developed by SRI International and the University of 6239691Srwatson * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7239691Srwatson * ("CTSRD"), as part of the DARPA CRASH research programme. 8239691Srwatson * 9239691Srwatson * Redistribution and use in source and binary forms, with or without 10239691Srwatson * modification, are permitted provided that the following conditions 11239691Srwatson * are met: 12239691Srwatson * 1. Redistributions of source code must retain the above copyright 13239691Srwatson * notice, this list of conditions and the following disclaimer. 14239691Srwatson * 2. Redistributions in binary form must reproduce the above copyright 15239691Srwatson * notice, this list of conditions and the following disclaimer in the 16239691Srwatson * documentation and/or other materials provided with the distribution. 17239691Srwatson * 18239691Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19239691Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20239691Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21239691Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22239691Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23239691Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24239691Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25239691Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26239691Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27239691Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28239691Srwatson * SUCH DAMAGE. 29239691Srwatson */ 30239691Srwatson 31239691Srwatson#include <sys/cdefs.h> 32239691Srwatson__FBSDID("$FreeBSD: head/sys/dev/terasic/mtl/terasic_mtl_text.c 239691 2012-08-25 22:35:29Z rwatson $"); 33239691Srwatson 34239691Srwatson#include <sys/param.h> 35239691Srwatson#include <sys/bus.h> 36239691Srwatson#include <sys/conf.h> 37239691Srwatson#include <sys/consio.h> /* struct vt_mode */ 38239691Srwatson#include <sys/endian.h> 39239691Srwatson#include <sys/fbio.h> /* video_adapter_t */ 40239691Srwatson#include <sys/lock.h> 41239691Srwatson#include <sys/mutex.h> 42239691Srwatson#include <sys/rman.h> 43239691Srwatson#include <sys/systm.h> 44239691Srwatson#include <sys/uio.h> 45239691Srwatson 46239691Srwatson#include <machine/bus.h> 47239691Srwatson#include <machine/resource.h> 48239691Srwatson#include <machine/vm.h> 49239691Srwatson 50239691Srwatson#include <dev/terasic/mtl/terasic_mtl.h> 51239691Srwatson 52239691Srwatsonstatic d_mmap_t terasic_mtl_text_mmap; 53239691Srwatsonstatic d_read_t terasic_mtl_text_read; 54239691Srwatsonstatic d_write_t terasic_mtl_text_write; 55239691Srwatson 56239691Srwatsonstatic struct cdevsw terasic_mtl_text_cdevsw = { 57239691Srwatson .d_version = D_VERSION, 58239691Srwatson .d_mmap = terasic_mtl_text_mmap, 59239691Srwatson .d_read = terasic_mtl_text_read, 60239691Srwatson .d_write = terasic_mtl_text_write, 61239691Srwatson .d_name = "terasic_mtl_text", 62239691Srwatson}; 63239691Srwatson 64239691Srwatson/* 65239691Srwatson * All I/O to/from the mtl device must be 16-bit, and aligned to 16-bit. 66239691Srwatson */ 67239691Srwatsonstatic int 68239691Srwatsonterasic_mtl_text_read(struct cdev *dev, struct uio *uio, int flag) 69239691Srwatson{ 70239691Srwatson struct terasic_mtl_softc *sc; 71239691Srwatson u_long offset, size; 72239691Srwatson uint16_t v; 73239691Srwatson int error; 74239691Srwatson 75239691Srwatson if (uio->uio_offset < 0 || uio->uio_offset % 2 != 0 || 76239691Srwatson uio->uio_resid % 2 != 0) 77239691Srwatson return (ENODEV); 78239691Srwatson sc = dev->si_drv1; 79239691Srwatson size = rman_get_size(sc->mtl_text_res); 80239691Srwatson error = 0; 81239691Srwatson if ((uio->uio_offset + uio->uio_resid < 0) || 82239691Srwatson (uio->uio_offset + uio->uio_resid > size)) 83239691Srwatson return (ENODEV); 84239691Srwatson while (uio->uio_resid > 0) { 85239691Srwatson offset = uio->uio_offset; 86239691Srwatson if (offset + sizeof(v) > size) 87239691Srwatson return (ENODEV); 88239691Srwatson v = bus_read_2(sc->mtl_text_res, offset); 89239691Srwatson error = uiomove(&v, sizeof(v), uio); 90239691Srwatson if (error) 91239691Srwatson return (error); 92239691Srwatson } 93239691Srwatson return (error); 94239691Srwatson} 95239691Srwatson 96239691Srwatsonstatic int 97239691Srwatsonterasic_mtl_text_write(struct cdev *dev, struct uio *uio, int flag) 98239691Srwatson{ 99239691Srwatson struct terasic_mtl_softc *sc; 100239691Srwatson u_long offset, size; 101239691Srwatson uint16_t v; 102239691Srwatson int error; 103239691Srwatson 104239691Srwatson if (uio->uio_offset < 0 || uio->uio_offset % 2 != 0 || 105239691Srwatson uio->uio_resid % 2 != 0) 106239691Srwatson return (ENODEV); 107239691Srwatson sc = dev->si_drv1; 108239691Srwatson size = rman_get_size(sc->mtl_text_res); 109239691Srwatson error = 0; 110239691Srwatson while (uio->uio_resid > 0) { 111239691Srwatson offset = uio->uio_offset; 112239691Srwatson if (offset + sizeof(v) > size) 113239691Srwatson return (ENODEV); 114239691Srwatson error = uiomove(&v, sizeof(v), uio); 115239691Srwatson if (error) 116239691Srwatson return (error); 117239691Srwatson bus_write_2(sc->mtl_text_res, offset, v); 118239691Srwatson } 119239691Srwatson return (error); 120239691Srwatson} 121239691Srwatson 122239691Srwatsonstatic int 123239691Srwatsonterasic_mtl_text_mmap(struct cdev *dev, vm_ooffset_t offset, 124239691Srwatson vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr) 125239691Srwatson{ 126239691Srwatson struct terasic_mtl_softc *sc; 127239691Srwatson int error; 128239691Srwatson 129239691Srwatson sc = dev->si_drv1; 130239691Srwatson error = 0; 131239691Srwatson if (trunc_page(offset) == offset && 132239691Srwatson rman_get_size(sc->mtl_text_res) >= offset + PAGE_SIZE) { 133239691Srwatson *paddr = rman_get_start(sc->mtl_text_res) + offset; 134239691Srwatson *memattr = VM_MEMATTR_UNCACHEABLE; 135239691Srwatson } else 136239691Srwatson error = ENODEV; 137239691Srwatson return (error); 138239691Srwatson} 139239691Srwatson 140239691Srwatsonvoid 141239691Srwatsonterasic_mtl_text_putc(struct terasic_mtl_softc *sc, u_int x, u_int y, 142239691Srwatson uint8_t c, uint8_t a) 143239691Srwatson{ 144239691Srwatson u_int offset; 145239691Srwatson uint16_t v; 146239691Srwatson 147239691Srwatson KASSERT(x < TERASIC_MTL_COLS, ("%s: TERASIC_MTL_COLS", __func__)); 148239691Srwatson KASSERT(y < TERASIC_MTL_ROWS, ("%s: TERASIC_MTL_ROWS", __func__)); 149239691Srwatson 150239691Srwatson offset = sizeof(uint16_t) * (x + y * TERASIC_MTL_COLS); 151239691Srwatson v = (c << TERASIC_MTL_TEXTFRAMEBUF_CHAR_SHIFT) | 152239691Srwatson (a << TERASIC_MTL_TEXTFRAMEBUF_ATTR_SHIFT); 153239691Srwatson v = htole16(v); 154239691Srwatson bus_write_2(sc->mtl_text_res, offset, v); 155239691Srwatson} 156239691Srwatson 157239691Srwatsonint 158239691Srwatsonterasic_mtl_text_attach(struct terasic_mtl_softc *sc) 159239691Srwatson{ 160239691Srwatson uint32_t v; 161239691Srwatson 162239691Srwatson terasic_mtl_reg_textframebufaddr_get(sc, &v); 163239691Srwatson if (v != TERASIC_MTL_TEXTFRAMEBUF_EXPECTED_ADDR) { 164239691Srwatson device_printf(sc->mtl_dev, "%s: unexpected text frame buffer " 165239691Srwatson "address (%08x); cannot attach\n", __func__, v); 166239691Srwatson return (ENXIO); 167239691Srwatson } 168239691Srwatson 169239691Srwatson sc->mtl_text_cdev = make_dev(&terasic_mtl_text_cdevsw, sc->mtl_unit, 170239691Srwatson UID_ROOT, GID_WHEEL, 0400, "mtl_text%d", sc->mtl_unit); 171239691Srwatson if (sc->mtl_text_cdev == NULL) { 172239691Srwatson device_printf(sc->mtl_dev, "%s: make_dev failed\n", __func__); 173239691Srwatson return (ENXIO); 174239691Srwatson } 175239691Srwatson /* XXXRW: Slight race between make_dev(9) and here. */ 176239691Srwatson TERASIC_MTL_LOCK_INIT(sc); 177239691Srwatson sc->mtl_text_cdev->si_drv1 = sc; 178239691Srwatson return (0); 179239691Srwatson} 180239691Srwatson 181239691Srwatsonvoid 182239691Srwatsonterasic_mtl_text_detach(struct terasic_mtl_softc *sc) 183239691Srwatson{ 184239691Srwatson 185239691Srwatson if (sc->mtl_text_cdev != NULL) { 186239691Srwatson destroy_dev(sc->mtl_text_cdev); 187239691Srwatson TERASIC_MTL_LOCK_DESTROY(sc); 188239691Srwatson } 189239691Srwatson} 190