2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Robert N. M. Watson
7 * This software was developed by SRI International and the University of
8 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
9 * ("CTSRD"), as part of the DARPA CRASH research programme.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
39 #include <sys/consio.h> /* struct vt_mode */
40 #include <sys/fbio.h> /* video_adapter_t */
42 #include <sys/mutex.h>
46 #include <machine/bus.h>
47 #include <machine/resource.h>
48 #include <machine/vm.h>
50 #include <dev/terasic/mtl/terasic_mtl.h>
52 static d_mmap_t terasic_mtl_pixel_mmap;
53 static d_read_t terasic_mtl_pixel_read;
54 static d_write_t terasic_mtl_pixel_write;
56 static struct cdevsw mtl_pixel_cdevsw = {
57 .d_version = D_VERSION,
58 .d_mmap = terasic_mtl_pixel_mmap,
59 .d_read = terasic_mtl_pixel_read,
60 .d_write = terasic_mtl_pixel_write,
61 .d_name = "terasic_mtl_pixel",
65 * All I/O to/from the MTL pixel device must be 32-bit, and aligned to 32-bit.
68 terasic_mtl_pixel_read(struct cdev *dev, struct uio *uio, int flag)
70 struct terasic_mtl_softc *sc;
75 if (uio->uio_offset < 0 || uio->uio_offset % 4 != 0 ||
76 uio->uio_resid % 4 != 0)
79 size = rman_get_size(sc->mtl_pixel_res);
81 if ((uio->uio_offset + uio->uio_resid < 0) ||
82 (uio->uio_offset + uio->uio_resid > size))
84 while (uio->uio_resid > 0) {
85 offset = uio->uio_offset;
86 if (offset + sizeof(v) > size)
88 v = bus_read_4(sc->mtl_pixel_res, offset);
89 error = uiomove(&v, sizeof(v), uio);
97 terasic_mtl_pixel_write(struct cdev *dev, struct uio *uio, int flag)
99 struct terasic_mtl_softc *sc;
104 if (uio->uio_offset < 0 || uio->uio_offset % 4 != 0 ||
105 uio->uio_resid % 4 != 0)
108 size = rman_get_size(sc->mtl_pixel_res);
110 while (uio->uio_resid > 0) {
111 offset = uio->uio_offset;
112 if (offset + sizeof(v) > size)
114 error = uiomove(&v, sizeof(v), uio);
117 bus_write_4(sc->mtl_pixel_res, offset, v);
123 terasic_mtl_pixel_mmap(struct cdev *dev, vm_ooffset_t offset,
124 vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr)
126 struct terasic_mtl_softc *sc;
131 if (trunc_page(offset) == offset &&
132 rman_get_size(sc->mtl_pixel_res) >= offset + PAGE_SIZE) {
133 *paddr = rman_get_start(sc->mtl_pixel_res) + offset;
134 *memattr = VM_MEMATTR_UNCACHEABLE;
141 terasic_mtl_pixel_attach(struct terasic_mtl_softc *sc)
144 sc->mtl_pixel_cdev = make_dev(&mtl_pixel_cdevsw, sc->mtl_unit,
145 UID_ROOT, GID_WHEEL, 0400, "mtl_pixel%d", sc->mtl_unit);
146 if (sc->mtl_pixel_cdev == NULL) {
147 device_printf(sc->mtl_dev, "%s: make_dev failed\n", __func__);
150 /* XXXRW: Slight race between make_dev(9) and here. */
151 sc->mtl_pixel_cdev->si_drv1 = sc;
156 terasic_mtl_pixel_detach(struct terasic_mtl_softc *sc)
159 if (sc->mtl_pixel_cdev != NULL)
160 destroy_dev(sc->mtl_pixel_cdev);