xref: /netbsd-src/sys/dev/fdt/simplefb.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /* $NetBSD: simplefb.c,v 1.2 2017/09/04 18:01:28 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 <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: simplefb.c,v 1.2 2017/09/04 18:01:28 jmcneill Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36 
37 #include <dev/fdt/fdtvar.h>
38 
39 #include <dev/wsfb/genfbvar.h>
40 
41 static const char * const compatible[] = {
42 	"simple-framebuffer",
43 	NULL
44 };
45 
46 struct simplefb_softc {
47 	struct genfb_softc sc_gen;
48 	bus_space_tag_t sc_bst;
49 	bus_space_handle_t sc_bsh;
50 	int sc_phandle;
51 
52 	bus_addr_t sc_paddr;
53 };
54 
55 static int simplefb_console_phandle = -1;
56 
57 static bool
58 simplefb_shutdown(device_t self, int flags)
59 {
60 	genfb_enable_polling(self);
61 	return true;
62 }
63 
64 static int
65 simplefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
66 {
67 	struct simplefb_softc * const sc = v;
68 	struct wsdisplayio_bus_id *busid;
69 	struct wsdisplayio_fbinfo *fbi;
70 	struct rasops_info *ri;
71 	int error;
72 
73 	switch (cmd) {
74 	case WSDISPLAYIO_GTYPE:
75 		*(u_int *)data = WSDISPLAY_TYPE_GENFB;
76 		return 0;
77 	case WSDISPLAYIO_GET_BUSID:
78 		busid = data;
79 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
80 		return 0;
81 	case WSDISPLAYIO_GET_FBINFO:
82 		fbi = data;
83 		ri = &sc->sc_gen.vd.active->scr_ri;
84 		error = wsdisplayio_get_fbinfo(ri, fbi);
85 		if (error == 0)
86 			fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
87 		return error;
88 	default:
89 		return EPASSTHROUGH;
90 	}
91 }
92 
93 static paddr_t
94 simplefb_mmap(void *v, void *vs, off_t off, int prot)
95 {
96 	struct simplefb_softc * const sc = v;
97 
98 	if (off < 0 || off >= sc->sc_gen.sc_fbsize)
99 		return -1;
100 
101 	return bus_space_mmap(sc->sc_bst, sc->sc_paddr, off, prot,
102 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
103 }
104 
105 static int
106 simplefb_attach_genfb(struct simplefb_softc *sc)
107 {
108 	device_t dev = sc->sc_gen.sc_dev;
109 	prop_dictionary_t dict = device_properties(dev);
110 	const int phandle = sc->sc_phandle;
111 	struct genfb_ops ops;
112 	uint32_t width, height, stride;
113 	uint16_t depth;
114 	const char *format;
115 	bus_addr_t addr;
116 	bus_size_t size;
117 
118 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
119 		aprint_error(": couldn't get registers\n");
120 		return ENXIO;
121 	}
122 
123 	if (size == 0) {
124 		aprint_naive("\n");
125 		aprint_normal(": disabled\n");
126 		return ENXIO;
127 	}
128 
129 	if (of_getprop_uint32(phandle, "width", &width) != 0 ||
130 	    of_getprop_uint32(phandle, "height", &height) != 0 ||
131 	    of_getprop_uint32(phandle, "stride", &stride) != 0 ||
132 	    (format = fdtbus_get_string(phandle, "format")) == NULL) {
133 		aprint_error(": missing property in DT\n");
134 		return ENXIO;
135 	}
136 
137 	if (strcmp(format, "a8b8g8r8") == 0 ||
138 	    strcmp(format, "x8r8g8b8") == 0) {
139 		depth = 32;
140 	} else if (strcmp(format, "r5g6b5") == 0) {
141 		depth = 16;
142 	} else {
143 		aprint_error(": unsupported format '%s'\n", format);
144 		return ENXIO;
145 	}
146 
147 	if (bus_space_map(sc->sc_bst, addr, size, BUS_SPACE_MAP_LINEAR,
148 	    &sc->sc_bsh) != 0) {
149 		aprint_error(": failed to map fb\n");
150 		return ENXIO;
151 	}
152 
153 	sc->sc_paddr = addr;
154 
155 	prop_dictionary_set_uint32(dict, "width", width);
156 	prop_dictionary_set_uint32(dict, "height", height);
157 	prop_dictionary_set_uint8(dict, "depth", depth);
158 	prop_dictionary_set_uint16(dict, "linebytes", stride);
159 	prop_dictionary_set_uint32(dict, "address", addr);
160 	prop_dictionary_set_uint32(dict, "virtual_address",
161 	    (uintptr_t)bus_space_vaddr(sc->sc_bst, sc->sc_bsh));
162 
163 	genfb_init(&sc->sc_gen);
164 
165 	if (sc->sc_gen.sc_width == 0 || sc->sc_gen.sc_fbsize == 0) {
166 		aprint_normal(": disabled\n");
167 		return ENXIO;
168 	}
169 
170 	aprint_naive("\n");
171 	aprint_normal(": Simple Framebuffer (%ux%u %u-bpp @ 0x%" PRIxBUSADDR ")\n",
172 	    width, height, depth, addr);
173 
174 	pmf_device_register1(dev, NULL, NULL, simplefb_shutdown);
175 
176 	memset(&ops, 0, sizeof(ops));
177 	ops.genfb_ioctl = simplefb_ioctl;
178 	ops.genfb_mmap = simplefb_mmap;
179 
180 	const bool is_console = phandle == simplefb_console_phandle;
181 
182 	prop_dictionary_set_bool(dict, "is_console", is_console);
183 
184 	if (is_console)
185 		aprint_normal_dev(sc->sc_gen.sc_dev,
186 		    "switching to framebuffer console\n");
187 
188 	genfb_attach(&sc->sc_gen, &ops);
189 
190 	return 0;
191 }
192 
193 static int
194 simplefb_match(device_t parent, cfdata_t cf, void *aux)
195 {
196 	struct fdt_attach_args * const faa = aux;
197 
198 	return of_match_compatible(faa->faa_phandle, compatible);
199 }
200 
201 static void
202 simplefb_attach(device_t parent, device_t self, void *aux)
203 {
204 	struct simplefb_softc * const sc = device_private(self);
205 	struct fdt_attach_args * const faa = aux;
206 	const int phandle = faa->faa_phandle;
207 
208 	sc->sc_gen.sc_dev = self;
209 	sc->sc_phandle = phandle;
210 	sc->sc_bst = faa->faa_bst;
211 
212 	if (simplefb_attach_genfb(sc) != 0)
213 		return;
214 }
215 
216 CFATTACH_DECL_NEW(simplefb, sizeof(struct simplefb_softc),
217 	simplefb_match, simplefb_attach, NULL, NULL);
218 
219 static int
220 simplefb_console_match(int phandle)
221 {
222 	return of_match_compatible(phandle, compatible);
223 }
224 
225 static void
226 simplefb_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
227 {
228 	simplefb_console_phandle = faa->faa_phandle;
229 	genfb_cnattach();
230 }
231 
232 static const struct fdt_console simplefb_fdt_console = {
233 	.match = simplefb_console_match,
234 	.consinit = simplefb_console_consinit
235 };
236 
237 FDT_CONSOLE(simplefb, &simplefb_fdt_console);
238