2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012-2013 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>
38 #include <sys/condvar.h>
40 #include <sys/consio.h> /* struct vt_mode */
41 #include <sys/fbio.h> /* video_adapter_t */
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/mutex.h>
48 #include <sys/systm.h>
50 #include <machine/bus.h>
51 #include <machine/resource.h>
53 #include <dev/fdt/fdt_common.h>
54 #include <dev/ofw/openfirm.h>
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
58 #include <dev/terasic/mtl/terasic_mtl.h>
63 terasic_mtl_fdt_probe(device_t dev)
66 if (!ofw_bus_status_okay(dev))
69 if (ofw_bus_is_compatible(dev, "sri-cambridge,mtl")) {
70 device_set_desc(dev, "Terasic Multi-touch LCD (MTL)");
71 return (BUS_PROBE_DEFAULT);
77 terasic_mtl_fdt_attach(device_t dev)
79 struct terasic_mtl_softc *sc;
82 sc = device_get_softc(dev);
84 sc->mtl_unit = device_get_unit(dev);
87 * FDT allows multiple memory resources to be defined for a device;
88 * query them in the order registers, pixel buffer, text buffer.
89 * However, we need to sanity-check that they are page-aligned and
90 * page-sized, so we may still abort.
93 sc->mtl_reg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
94 &sc->mtl_reg_rid, RF_ACTIVE);
95 if (sc->mtl_reg_res == NULL) {
96 device_printf(dev, "couldn't map register memory\n");
100 if (rman_get_start(sc->mtl_reg_res) % PAGE_SIZE != 0) {
101 device_printf(dev, "improper register address\n");
105 if (rman_get_size(sc->mtl_reg_res) % PAGE_SIZE != 0) {
106 device_printf(dev, "improper register size\n");
110 device_printf(sc->mtl_dev, "registers at mem %p-%p\n",
111 (void *)rman_get_start(sc->mtl_reg_res),
112 (void *)(rman_get_start(sc->mtl_reg_res) +
113 rman_get_size(sc->mtl_reg_res)));
115 sc->mtl_pixel_rid = 1;
116 sc->mtl_pixel_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
117 &sc->mtl_pixel_rid, RF_ACTIVE);
118 if (sc->mtl_pixel_res == NULL) {
119 device_printf(dev, "couldn't map pixel memory\n");
123 if (rman_get_start(sc->mtl_pixel_res) % PAGE_SIZE != 0) {
124 device_printf(dev, "improper pixel address\n");
128 if (rman_get_size(sc->mtl_pixel_res) % PAGE_SIZE != 0) {
129 device_printf(dev, "improper pixel size\n");
133 device_printf(sc->mtl_dev, "pixel frame buffer at mem %p-%p\n",
134 (void *)rman_get_start(sc->mtl_pixel_res),
135 (void *)(rman_get_start(sc->mtl_pixel_res) +
136 rman_get_size(sc->mtl_pixel_res)));
138 sc->mtl_text_rid = 2;
139 sc->mtl_text_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
140 &sc->mtl_text_rid, RF_ACTIVE);
141 if (sc->mtl_text_res == NULL) {
142 device_printf(dev, "couldn't map text memory\n");
146 if (rman_get_start(sc->mtl_text_res) % PAGE_SIZE != 0) {
147 device_printf(dev, "improper text address\n");
151 if (rman_get_size(sc->mtl_text_res) % PAGE_SIZE != 0) {
152 device_printf(dev, "improper text size\n");
156 device_printf(sc->mtl_dev, "text frame buffer at mem %p-%p\n",
157 (void *)rman_get_start(sc->mtl_text_res),
158 (void *)(rman_get_start(sc->mtl_text_res) +
159 rman_get_size(sc->mtl_text_res)));
161 error = terasic_mtl_attach(sc);
165 if (sc->mtl_text_res != NULL)
166 bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_text_rid,
168 if (sc->mtl_pixel_res != NULL)
169 bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_pixel_rid,
171 if (sc->mtl_reg_res != NULL)
172 bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_reg_rid,
178 terasic_mtl_fdt_detach(device_t dev)
180 struct terasic_mtl_softc *sc;
182 sc = device_get_softc(dev);
183 terasic_mtl_detach(sc);
184 bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_text_rid,
186 bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_pixel_rid,
188 bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_reg_rid,
193 static struct fb_info *
194 terasic_mtl_fb_getinfo(device_t dev)
196 struct terasic_mtl_softc *sc;
198 sc = device_get_softc(dev);
199 return (&sc->mtl_fb_info);
202 static device_method_t terasic_mtl_fdt_methods[] = {
203 DEVMETHOD(device_probe, terasic_mtl_fdt_probe),
204 DEVMETHOD(device_attach, terasic_mtl_fdt_attach),
205 DEVMETHOD(device_detach, terasic_mtl_fdt_detach),
206 DEVMETHOD(fb_getinfo, terasic_mtl_fb_getinfo),
210 static driver_t terasic_mtl_fdt_driver = {
212 terasic_mtl_fdt_methods,
213 sizeof(struct terasic_mtl_softc),
216 DRIVER_MODULE(mtl, simplebus, terasic_mtl_fdt_driver, terasic_mtl_devclass, 0,