xref: /freebsd-13-stable/sys/dev/terasic/mtl/terasic_mtl_nexus.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Robert N. M. Watson
5  * All rights reserved.
6  *
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.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/condvar.h>
37 #include <sys/conf.h>
38 #include <sys/consio.h>				/* struct vt_mode */
39 #include <sys/fbio.h>				/* video_adapter_t */
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/rman.h>
46 #include <sys/systm.h>
47 
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 
51 #include <dev/terasic/mtl/terasic_mtl.h>
52 
53 #include "fb_if.h"
54 
55 static int
terasic_mtl_nexus_probe(device_t dev)56 terasic_mtl_nexus_probe(device_t dev)
57 {
58 
59 	device_set_desc(dev, "Terasic Multi-touch LCD (MTL)");
60 	return (BUS_PROBE_NOWILDCARD);
61 }
62 
63 static int
terasic_mtl_nexus_attach(device_t dev)64 terasic_mtl_nexus_attach(device_t dev)
65 {
66 	struct terasic_mtl_softc *sc;
67 	u_long pixel_maddr, text_maddr, reg_maddr;
68 	u_long pixel_msize, text_msize, reg_msize;
69 	int error;
70 
71 	sc = device_get_softc(dev);
72 	sc->mtl_dev = dev;
73 	sc->mtl_unit = device_get_unit(dev);
74 
75 	/*
76 	 * Query non-standard hints to find the locations of our two memory
77 	 * regions.  Enforce certain alignment and size requirements.
78 	 */
79 	if (resource_long_value(device_get_name(dev), device_get_unit(dev),
80 	    "reg_maddr", &reg_maddr) != 0 || (reg_maddr % PAGE_SIZE != 0)) {
81 		device_printf(dev, "improper register address\n");
82 		return (ENXIO);
83 	}
84 	if (resource_long_value(device_get_name(dev), device_get_unit(dev),
85 	    "reg_msize", &reg_msize) != 0 || (reg_msize % PAGE_SIZE != 0)) {
86 		device_printf(dev, "improper register size\n");
87 		return (ENXIO);
88 	}
89 	if (resource_long_value(device_get_name(dev), device_get_unit(dev),
90 	    "pixel_maddr", &pixel_maddr) != 0 ||
91 	    (pixel_maddr % PAGE_SIZE != 0)) {
92 		device_printf(dev, "improper pixel frame buffer address\n");
93 		return (ENXIO);
94 	}
95 	if (resource_long_value(device_get_name(dev), device_get_unit(dev),
96 	    "pixel_msize", &pixel_msize) != 0 ||
97 	    (pixel_msize % PAGE_SIZE != 0)) {
98 		device_printf(dev, "improper pixel frame buffer size\n");
99 		return (ENXIO);
100 	}
101 	if (resource_long_value(device_get_name(dev), device_get_unit(dev),
102 	    "text_maddr", &text_maddr) != 0 ||
103 	    (text_maddr % PAGE_SIZE != 0)) {
104 		device_printf(dev, "improper text frame buffer address\n");
105 		return (ENXIO);
106 	}
107 	if (resource_long_value(device_get_name(dev), device_get_unit(dev),
108 	    "text_msize", &text_msize) != 0 ||
109 	    (text_msize % PAGE_SIZE != 0)) {
110 		device_printf(dev, "improper text frame buffer size\n");
111 		return (ENXIO);
112 	}
113 
114 	/*
115 	 * Allocate resources.
116 	 */
117 	sc->mtl_reg_rid = 0;
118 	sc->mtl_reg_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
119 	    &sc->mtl_reg_rid, reg_maddr, reg_maddr + reg_msize - 1,
120 	    reg_msize, RF_ACTIVE);
121 	if (sc->mtl_reg_res == NULL) {
122 		device_printf(dev, "couldn't map register memory\n");
123 		error = ENXIO;
124 		goto error;
125 	}
126 	device_printf(sc->mtl_dev, "registers at mem %p-%p\n",
127 	    (void *)reg_maddr, (void *)(reg_maddr + reg_msize));
128 	sc->mtl_pixel_rid = 0;
129 	sc->mtl_pixel_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
130 	    &sc->mtl_pixel_rid, pixel_maddr, pixel_maddr + pixel_msize - 1,
131 	    pixel_msize, RF_ACTIVE);
132 	if (sc->mtl_pixel_res == NULL) {
133 		device_printf(dev, "couldn't map pixel memory\n");
134 		error = ENXIO;
135 		goto error;
136 	}
137 	device_printf(sc->mtl_dev, "pixel frame buffer at mem %p-%p\n",
138 	    (void *)pixel_maddr, (void *)(pixel_maddr + pixel_msize));
139 	sc->mtl_text_rid = 0;
140 	sc->mtl_text_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
141 	    &sc->mtl_text_rid, text_maddr, text_maddr + text_msize - 1,
142 	    text_msize, RF_ACTIVE);
143 	if (sc->mtl_text_res == NULL) {
144 		device_printf(dev, "couldn't map text memory\n");
145 		error = ENXIO;
146 		goto error;
147 	}
148 	device_printf(sc->mtl_dev, "text frame buffer at mem %p-%p\n",
149 	    (void *)text_maddr, (void *)(text_maddr + text_msize));
150 	error = terasic_mtl_attach(sc);
151 	if (error == 0)
152 		return (0);
153 error:
154 	if (sc->mtl_text_res != NULL)
155 		bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_text_rid,
156 		    sc->mtl_text_res);
157 	if (sc->mtl_pixel_res != NULL)
158 		bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_pixel_rid,
159 		    sc->mtl_pixel_res);
160 	if (sc->mtl_reg_res != NULL)
161 		bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_reg_rid,
162 		    sc->mtl_reg_res);
163 	return (error);
164 }
165 
166 static int
terasic_mtl_nexus_detach(device_t dev)167 terasic_mtl_nexus_detach(device_t dev)
168 {
169 	struct terasic_mtl_softc *sc;
170 
171 	sc = device_get_softc(dev);
172 	terasic_mtl_detach(sc);
173 	bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_text_rid,
174 	    sc->mtl_text_res);
175 	bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_pixel_rid,
176 	    sc->mtl_pixel_res);
177 	bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_reg_rid,
178 	    sc->mtl_reg_res);
179 	return (0);
180 }
181 
182 static struct fb_info *
terasic_mtl_fb_getinfo(device_t dev)183 terasic_mtl_fb_getinfo(device_t dev)
184 {
185 	struct terasic_mtl_softc *sc;
186 
187 	sc = device_get_softc(dev);
188 	return (&sc->mtl_fb_info);
189 }
190 
191 static device_method_t terasic_mtl_nexus_methods[] = {
192 	DEVMETHOD(device_probe,		terasic_mtl_nexus_probe),
193 	DEVMETHOD(device_attach,	terasic_mtl_nexus_attach),
194 	DEVMETHOD(device_detach,	terasic_mtl_nexus_detach),
195 	DEVMETHOD(fb_getinfo,		terasic_mtl_fb_getinfo),
196 	{ 0, 0 }
197 };
198 
199 static driver_t terasic_mtl_nexus_driver = {
200 	"terasic_mtl",
201 	terasic_mtl_nexus_methods,
202 	sizeof(struct terasic_mtl_softc),
203 };
204 
205 DRIVER_MODULE(mtl, nexus, terasic_mtl_nexus_driver, terasic_mtl_devclass, 0,
206     0);
207