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