1/*-
2 * Copyright (c) 2012 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include <sys/param.h>
35#include <sys/bus.h>
36#include <sys/conf.h>
37#include <sys/consio.h>				/* struct vt_mode */
38#include <sys/fbio.h>				/* video_adapter_t */
39#include <sys/lock.h>
40#include <sys/mutex.h>
41#include <sys/rman.h>
42#include <sys/uio.h>
43
44#include <machine/bus.h>
45#include <machine/resource.h>
46#include <machine/vm.h>
47
48#include <dev/terasic/mtl/terasic_mtl.h>
49
50static d_mmap_t terasic_mtl_pixel_mmap;
51static d_read_t terasic_mtl_pixel_read;
52static d_write_t terasic_mtl_pixel_write;
53
54static struct cdevsw mtl_pixel_cdevsw = {
55	.d_version =	D_VERSION,
56	.d_mmap =	terasic_mtl_pixel_mmap,
57	.d_read =	terasic_mtl_pixel_read,
58	.d_write =	terasic_mtl_pixel_write,
59	.d_name =	"terasic_mtl_pixel",
60};
61
62/*
63 * All I/O to/from the MTL pixel device must be 32-bit, and aligned to 32-bit.
64 */
65static int
66terasic_mtl_pixel_read(struct cdev *dev, struct uio *uio, int flag)
67{
68	struct terasic_mtl_softc *sc;
69	u_long offset, size;
70	uint32_t v;
71	int error;
72
73	if (uio->uio_offset < 0 || uio->uio_offset % 4 != 0 ||
74	    uio->uio_resid % 4 != 0)
75		return (ENODEV);
76	sc = dev->si_drv1;
77	size = rman_get_size(sc->mtl_pixel_res);
78	error = 0;
79	if ((uio->uio_offset + uio->uio_resid < 0) ||
80	    (uio->uio_offset + uio->uio_resid > size))
81		return (ENODEV);
82	while (uio->uio_resid > 0) {
83		offset = uio->uio_offset;
84		if (offset + sizeof(v) > size)
85			return (ENODEV);
86		v = bus_read_4(sc->mtl_pixel_res, offset);
87		error = uiomove(&v, sizeof(v), uio);
88		if (error)
89			return (error);
90	}
91	return (error);
92}
93
94static int
95terasic_mtl_pixel_write(struct cdev *dev, struct uio *uio, int flag)
96{
97	struct terasic_mtl_softc *sc;
98	u_long offset, size;
99	uint32_t v;
100	int error;
101
102	if (uio->uio_offset < 0 || uio->uio_offset % 4 != 0 ||
103	    uio->uio_resid % 4 != 0)
104		return (ENODEV);
105	sc = dev->si_drv1;
106	size = rman_get_size(sc->mtl_pixel_res);
107	error = 0;
108	while (uio->uio_resid > 0) {
109		offset = uio->uio_offset;
110		if (offset + sizeof(v) > size)
111			return (ENODEV);
112		error = uiomove(&v, sizeof(v), uio);
113		if (error)
114			return (error);
115		bus_write_4(sc->mtl_pixel_res, offset, v);
116	}
117	return (error);
118}
119
120static int
121terasic_mtl_pixel_mmap(struct cdev *dev, vm_ooffset_t offset,
122    vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr)
123{
124	struct terasic_mtl_softc *sc;
125	int error;
126
127	sc = dev->si_drv1;
128	error = 0;
129	if (trunc_page(offset) == offset &&
130	    rman_get_size(sc->mtl_pixel_res) >= offset + PAGE_SIZE) {
131		*paddr = rman_get_start(sc->mtl_pixel_res) + offset;
132		*memattr = VM_MEMATTR_UNCACHEABLE;
133	} else
134		error = ENODEV;
135	return (error);
136}
137
138int
139terasic_mtl_pixel_attach(struct terasic_mtl_softc *sc)
140{
141
142	sc->mtl_pixel_cdev = make_dev(&mtl_pixel_cdevsw, sc->mtl_unit,
143	    UID_ROOT, GID_WHEEL, 0400, "mtl_pixel%d", sc->mtl_unit);
144	if (sc->mtl_pixel_cdev == NULL) {
145		device_printf(sc->mtl_dev, "%s: make_dev failed\n", __func__);
146		return (ENXIO);
147	}
148	/* XXXRW: Slight race between make_dev(9) and here. */
149	sc->mtl_pixel_cdev->si_drv1 = sc;
150	return (0);
151}
152
153void
154terasic_mtl_pixel_detach(struct terasic_mtl_softc *sc)
155{
156
157	if (sc->mtl_pixel_cdev != NULL)
158		destroy_dev(sc->mtl_pixel_cdev);
159}
160