terasic_mtl_fdt.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012-2013 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: stable/11/sys/dev/terasic/mtl/terasic_mtl_fdt.c 330897 2018-03-14 03:19:51Z eadler $");
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/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>
57
58#include <dev/terasic/mtl/terasic_mtl.h>
59
60#include "fb_if.h"
61
62static int
63terasic_mtl_fdt_probe(device_t dev)
64{
65
66	if (!ofw_bus_status_okay(dev))
67		return (ENXIO);
68
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);
72	}
73        return (ENXIO);
74}
75
76static int
77terasic_mtl_fdt_attach(device_t dev)
78{
79	struct terasic_mtl_softc *sc;
80	int error;
81
82	sc = device_get_softc(dev);
83	sc->mtl_dev = dev;
84	sc->mtl_unit = device_get_unit(dev);
85
86	/*
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.
91	 */
92	sc->mtl_reg_rid = 0;
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");
97		error = ENXIO;
98		goto error;
99	}
100	if (rman_get_start(sc->mtl_reg_res) % PAGE_SIZE != 0) {
101		device_printf(dev, "improper register address\n");
102		error = ENXIO;
103		goto error;
104	}
105	if (rman_get_size(sc->mtl_reg_res) % PAGE_SIZE != 0) {
106		device_printf(dev, "improper register size\n");
107		error = ENXIO;
108		goto error;
109	}
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)));
114
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");
120		error = ENXIO;
121		goto error;
122	}
123	if (rman_get_start(sc->mtl_pixel_res) % PAGE_SIZE != 0) {
124		device_printf(dev, "improper pixel address\n");
125		error = ENXIO;
126		goto error;
127	}
128	if (rman_get_size(sc->mtl_pixel_res) % PAGE_SIZE != 0) {
129		device_printf(dev, "improper pixel size\n");
130		error = ENXIO;
131		goto error;
132	}
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)));
137
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");
143		error = ENXIO;
144		goto error;
145	}
146	if (rman_get_start(sc->mtl_text_res) % PAGE_SIZE != 0) {
147		device_printf(dev, "improper text address\n");
148		error = ENXIO;
149		goto error;
150	}
151	if (rman_get_size(sc->mtl_text_res) % PAGE_SIZE != 0) {
152		device_printf(dev, "improper text size\n");
153		error = ENXIO;
154		goto error;
155	}
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)));
160
161	error = terasic_mtl_attach(sc);
162	if (error == 0)
163		return (0);
164error:
165	if (sc->mtl_text_res != NULL)
166		bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_text_rid,
167		    sc->mtl_text_res);
168	if (sc->mtl_pixel_res != NULL)
169		bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_pixel_rid,
170		    sc->mtl_pixel_res);
171	if (sc->mtl_reg_res != NULL)
172		bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_reg_rid,
173		    sc->mtl_reg_res);
174	return (error);
175}
176
177static int
178terasic_mtl_fdt_detach(device_t dev)
179{
180	struct terasic_mtl_softc *sc;
181
182	sc = device_get_softc(dev);
183	terasic_mtl_detach(sc);
184	bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_text_rid,
185	    sc->mtl_text_res);
186	bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_pixel_rid,
187	    sc->mtl_pixel_res);
188	bus_release_resource(dev, SYS_RES_MEMORY, sc->mtl_reg_rid,
189	    sc->mtl_reg_res);
190	return (0);
191}
192
193static struct fb_info *
194terasic_mtl_fb_getinfo(device_t dev)
195{
196        struct terasic_mtl_softc *sc;
197
198        sc = device_get_softc(dev);
199        return (&sc->mtl_fb_info);
200}
201
202static 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),
207	{ 0, 0 }
208};
209
210static driver_t terasic_mtl_fdt_driver = {
211	"terasic_mtl",
212	terasic_mtl_fdt_methods,
213	sizeof(struct terasic_mtl_softc),
214};
215
216DRIVER_MODULE(mtl, simplebus, terasic_mtl_fdt_driver, terasic_mtl_devclass, 0,
217    0);
218