xref: /netbsd-src/sys/dev/fdt/simplefb.c (revision 87d689fb734c654d2486f87f7be32f1b53ecdbec)
1 /* $NetBSD: simplefb.c,v 1.3 2017/12/18 19:06:32 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "opt_wsdisplay_compat.h"
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: simplefb.c,v 1.3 2017/12/18 19:06:32 jmcneill Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/device.h>
37 #include <sys/systm.h>
38 
39 #include <dev/fdt/fdtvar.h>
40 
41 #include <dev/wsfb/genfbvar.h>
42 
43 static const char * const compatible[] = {
44 	"simple-framebuffer",
45 	NULL
46 };
47 
48 struct simplefb_softc {
49 	struct genfb_softc sc_gen;
50 	bus_space_tag_t sc_bst;
51 	bus_space_handle_t sc_bsh;
52 	int sc_phandle;
53 
54 	bus_addr_t sc_paddr;
55 };
56 
57 static int simplefb_console_phandle = -1;
58 
59 static bool
60 simplefb_shutdown(device_t self, int flags)
61 {
62 	genfb_enable_polling(self);
63 	return true;
64 }
65 
66 static int
67 simplefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
68 {
69 	struct simplefb_softc * const sc = v;
70 	struct wsdisplayio_bus_id *busid;
71 	struct wsdisplayio_fbinfo *fbi;
72 	struct rasops_info *ri;
73 	int error;
74 
75 	switch (cmd) {
76 	case WSDISPLAYIO_GTYPE:
77 		*(u_int *)data = WSDISPLAY_TYPE_GENFB;
78 		return 0;
79 	case WSDISPLAYIO_GET_BUSID:
80 		busid = data;
81 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
82 		return 0;
83 	case WSDISPLAYIO_GET_FBINFO:
84 		fbi = data;
85 		ri = &sc->sc_gen.vd.active->scr_ri;
86 		error = wsdisplayio_get_fbinfo(ri, fbi);
87 		if (error == 0)
88 			fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
89 		return error;
90 	default:
91 		return EPASSTHROUGH;
92 	}
93 }
94 
95 static paddr_t
96 simplefb_mmap(void *v, void *vs, off_t off, int prot)
97 {
98 	struct simplefb_softc * const sc = v;
99 
100 	if (off < 0 || off >= sc->sc_gen.sc_fbsize)
101 		return -1;
102 
103 	return bus_space_mmap(sc->sc_bst, sc->sc_paddr, off, prot,
104 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
105 }
106 
107 static int
108 simplefb_attach_genfb(struct simplefb_softc *sc)
109 {
110 	device_t dev = sc->sc_gen.sc_dev;
111 	prop_dictionary_t dict = device_properties(dev);
112 	const int phandle = sc->sc_phandle;
113 	struct genfb_ops ops;
114 	uint32_t width, height, stride;
115 	uint16_t depth;
116 	const char *format;
117 	bus_addr_t addr;
118 	bus_size_t size;
119 
120 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
121 		aprint_error(": couldn't get registers\n");
122 		return ENXIO;
123 	}
124 
125 	if (size == 0) {
126 		aprint_naive("\n");
127 		aprint_normal(": disabled\n");
128 		return ENXIO;
129 	}
130 
131 	if (of_getprop_uint32(phandle, "width", &width) != 0 ||
132 	    of_getprop_uint32(phandle, "height", &height) != 0 ||
133 	    of_getprop_uint32(phandle, "stride", &stride) != 0 ||
134 	    (format = fdtbus_get_string(phandle, "format")) == NULL) {
135 		aprint_error(": missing property in DT\n");
136 		return ENXIO;
137 	}
138 
139 	if (strcmp(format, "a8b8g8r8") == 0 ||
140 	    strcmp(format, "x8r8g8b8") == 0) {
141 		depth = 32;
142 	} else if (strcmp(format, "r5g6b5") == 0) {
143 		depth = 16;
144 	} else {
145 		aprint_error(": unsupported format '%s'\n", format);
146 		return ENXIO;
147 	}
148 
149 	if (bus_space_map(sc->sc_bst, addr, size, BUS_SPACE_MAP_LINEAR,
150 	    &sc->sc_bsh) != 0) {
151 		aprint_error(": failed to map fb\n");
152 		return ENXIO;
153 	}
154 
155 	sc->sc_paddr = addr;
156 
157 	prop_dictionary_set_uint32(dict, "width", width);
158 	prop_dictionary_set_uint32(dict, "height", height);
159 	prop_dictionary_set_uint8(dict, "depth", depth);
160 	prop_dictionary_set_uint16(dict, "linebytes", stride);
161 	prop_dictionary_set_uint32(dict, "address", addr);
162 	prop_dictionary_set_uint32(dict, "virtual_address",
163 	    (uintptr_t)bus_space_vaddr(sc->sc_bst, sc->sc_bsh));
164 
165 	genfb_init(&sc->sc_gen);
166 
167 	if (sc->sc_gen.sc_width == 0 || sc->sc_gen.sc_fbsize == 0) {
168 		aprint_normal(": disabled\n");
169 		return ENXIO;
170 	}
171 
172 	aprint_naive("\n");
173 	aprint_normal(": Simple Framebuffer (%ux%u %u-bpp @ 0x%" PRIxBUSADDR ")\n",
174 	    width, height, depth, addr);
175 
176 	pmf_device_register1(dev, NULL, NULL, simplefb_shutdown);
177 
178 	memset(&ops, 0, sizeof(ops));
179 	ops.genfb_ioctl = simplefb_ioctl;
180 	ops.genfb_mmap = simplefb_mmap;
181 
182 #ifdef WSDISPLAY_MULTICONS
183 	const bool is_console = true;
184 #else
185 	const bool is_console = phandle == simplefb_console_phandle;
186 	if (is_console)
187 		aprint_normal_dev(sc->sc_gen.sc_dev,
188 		    "switching to framebuffer console\n");
189 #endif
190 
191 	prop_dictionary_set_bool(dict, "is_console", is_console);
192 
193 	genfb_attach(&sc->sc_gen, &ops);
194 
195 	return 0;
196 }
197 
198 static int
199 simplefb_match(device_t parent, cfdata_t cf, void *aux)
200 {
201 	struct fdt_attach_args * const faa = aux;
202 
203 	return of_match_compatible(faa->faa_phandle, compatible);
204 }
205 
206 static void
207 simplefb_attach(device_t parent, device_t self, void *aux)
208 {
209 	struct simplefb_softc * const sc = device_private(self);
210 	struct fdt_attach_args * const faa = aux;
211 	const int phandle = faa->faa_phandle;
212 
213 	sc->sc_gen.sc_dev = self;
214 	sc->sc_phandle = phandle;
215 	sc->sc_bst = faa->faa_bst;
216 
217 	if (simplefb_attach_genfb(sc) != 0)
218 		return;
219 }
220 
221 CFATTACH_DECL_NEW(simplefb, sizeof(struct simplefb_softc),
222 	simplefb_match, simplefb_attach, NULL, NULL);
223 
224 static int
225 simplefb_console_match(int phandle)
226 {
227 	return of_match_compatible(phandle, compatible);
228 }
229 
230 static void
231 simplefb_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
232 {
233 	simplefb_console_phandle = faa->faa_phandle;
234 	genfb_cnattach();
235 }
236 
237 static const struct fdt_console simplefb_fdt_console = {
238 	.match = simplefb_console_match,
239 	.consinit = simplefb_console_consinit
240 };
241 
242 FDT_CONSOLE(simplefb, &simplefb_fdt_console);
243