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: releng/10.3/sys/dev/terasic/mtl/terasic_mtl_pixel.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/fbio.h>				/* video_adapter_t */
39239691Srwatson#include <sys/lock.h>
40239691Srwatson#include <sys/mutex.h>
41239691Srwatson#include <sys/rman.h>
42239691Srwatson#include <sys/uio.h>
43239691Srwatson
44239691Srwatson#include <machine/bus.h>
45239691Srwatson#include <machine/resource.h>
46239691Srwatson#include <machine/vm.h>
47239691Srwatson
48239691Srwatson#include <dev/terasic/mtl/terasic_mtl.h>
49239691Srwatson
50239691Srwatsonstatic d_mmap_t terasic_mtl_pixel_mmap;
51239691Srwatsonstatic d_read_t terasic_mtl_pixel_read;
52239691Srwatsonstatic d_write_t terasic_mtl_pixel_write;
53239691Srwatson
54239691Srwatsonstatic struct cdevsw mtl_pixel_cdevsw = {
55239691Srwatson	.d_version =	D_VERSION,
56239691Srwatson	.d_mmap =	terasic_mtl_pixel_mmap,
57239691Srwatson	.d_read =	terasic_mtl_pixel_read,
58239691Srwatson	.d_write =	terasic_mtl_pixel_write,
59239691Srwatson	.d_name =	"terasic_mtl_pixel",
60239691Srwatson};
61239691Srwatson
62239691Srwatson/*
63239691Srwatson * All I/O to/from the MTL pixel device must be 32-bit, and aligned to 32-bit.
64239691Srwatson */
65239691Srwatsonstatic int
66239691Srwatsonterasic_mtl_pixel_read(struct cdev *dev, struct uio *uio, int flag)
67239691Srwatson{
68239691Srwatson	struct terasic_mtl_softc *sc;
69239691Srwatson	u_long offset, size;
70239691Srwatson	uint32_t v;
71239691Srwatson	int error;
72239691Srwatson
73239691Srwatson	if (uio->uio_offset < 0 || uio->uio_offset % 4 != 0 ||
74239691Srwatson	    uio->uio_resid % 4 != 0)
75239691Srwatson		return (ENODEV);
76239691Srwatson	sc = dev->si_drv1;
77239691Srwatson	size = rman_get_size(sc->mtl_pixel_res);
78239691Srwatson	error = 0;
79239691Srwatson	if ((uio->uio_offset + uio->uio_resid < 0) ||
80239691Srwatson	    (uio->uio_offset + uio->uio_resid > size))
81239691Srwatson		return (ENODEV);
82239691Srwatson	while (uio->uio_resid > 0) {
83239691Srwatson		offset = uio->uio_offset;
84239691Srwatson		if (offset + sizeof(v) > size)
85239691Srwatson			return (ENODEV);
86239691Srwatson		v = bus_read_4(sc->mtl_pixel_res, offset);
87239691Srwatson		error = uiomove(&v, sizeof(v), uio);
88239691Srwatson		if (error)
89239691Srwatson			return (error);
90239691Srwatson	}
91239691Srwatson	return (error);
92239691Srwatson}
93239691Srwatson
94239691Srwatsonstatic int
95239691Srwatsonterasic_mtl_pixel_write(struct cdev *dev, struct uio *uio, int flag)
96239691Srwatson{
97239691Srwatson	struct terasic_mtl_softc *sc;
98239691Srwatson	u_long offset, size;
99239691Srwatson	uint32_t v;
100239691Srwatson	int error;
101239691Srwatson
102239691Srwatson	if (uio->uio_offset < 0 || uio->uio_offset % 4 != 0 ||
103239691Srwatson	    uio->uio_resid % 4 != 0)
104239691Srwatson		return (ENODEV);
105239691Srwatson	sc = dev->si_drv1;
106239691Srwatson	size = rman_get_size(sc->mtl_pixel_res);
107239691Srwatson	error = 0;
108239691Srwatson	while (uio->uio_resid > 0) {
109239691Srwatson		offset = uio->uio_offset;
110239691Srwatson		if (offset + sizeof(v) > size)
111239691Srwatson			return (ENODEV);
112239691Srwatson		error = uiomove(&v, sizeof(v), uio);
113239691Srwatson		if (error)
114239691Srwatson			return (error);
115239691Srwatson		bus_write_4(sc->mtl_pixel_res, offset, v);
116239691Srwatson	}
117239691Srwatson	return (error);
118239691Srwatson}
119239691Srwatson
120239691Srwatsonstatic int
121239691Srwatsonterasic_mtl_pixel_mmap(struct cdev *dev, vm_ooffset_t offset,
122239691Srwatson    vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr)
123239691Srwatson{
124239691Srwatson	struct terasic_mtl_softc *sc;
125239691Srwatson	int error;
126239691Srwatson
127239691Srwatson	sc = dev->si_drv1;
128239691Srwatson	error = 0;
129239691Srwatson	if (trunc_page(offset) == offset &&
130239691Srwatson	    rman_get_size(sc->mtl_pixel_res) >= offset + PAGE_SIZE) {
131239691Srwatson		*paddr = rman_get_start(sc->mtl_pixel_res) + offset;
132239691Srwatson		*memattr = VM_MEMATTR_UNCACHEABLE;
133239691Srwatson	} else
134239691Srwatson		error = ENODEV;
135239691Srwatson	return (error);
136239691Srwatson}
137239691Srwatson
138239691Srwatsonint
139239691Srwatsonterasic_mtl_pixel_attach(struct terasic_mtl_softc *sc)
140239691Srwatson{
141239691Srwatson
142239691Srwatson	sc->mtl_pixel_cdev = make_dev(&mtl_pixel_cdevsw, sc->mtl_unit,
143239691Srwatson	    UID_ROOT, GID_WHEEL, 0400, "mtl_pixel%d", sc->mtl_unit);
144239691Srwatson	if (sc->mtl_pixel_cdev == NULL) {
145239691Srwatson		device_printf(sc->mtl_dev, "%s: make_dev failed\n", __func__);
146239691Srwatson		return (ENXIO);
147239691Srwatson	}
148239691Srwatson	/* XXXRW: Slight race between make_dev(9) and here. */
149239691Srwatson	sc->mtl_pixel_cdev->si_drv1 = sc;
150239691Srwatson	return (0);
151239691Srwatson}
152239691Srwatson
153239691Srwatsonvoid
154239691Srwatsonterasic_mtl_pixel_detach(struct terasic_mtl_softc *sc)
155239691Srwatson{
156239691Srwatson
157239691Srwatson	if (sc->mtl_pixel_cdev != NULL)
158239691Srwatson		destroy_dev(sc->mtl_pixel_cdev);
159239691Srwatson}
160