xref: /freebsd/sys/dev/vt/hw/ofwfb/ofwfb.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /*-
2  * Copyright (c) 2011 Nathan Whitehorn
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/fbio.h>
34 
35 #include <dev/vt/vt.h>
36 #include <dev/vt/hw/fb/vt_fb.h>
37 #include <dev/vt/colors/vt_termcolors.h>
38 
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 
42 #include <machine/bus.h>
43 #ifdef __sparc64__
44 #include <machine/bus_private.h>
45 #endif
46 
47 #include <dev/ofw/openfirm.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_pci.h>
50 
51 struct ofwfb_softc {
52 	struct fb_info	fb;
53 
54 	phandle_t	sc_node;
55 	ihandle_t	sc_handle;
56 	bus_space_tag_t	sc_memt;
57 	int		iso_palette;
58 };
59 
60 static vd_probe_t	ofwfb_probe;
61 static vd_init_t	ofwfb_init;
62 static vd_bitblt_text_t	ofwfb_bitblt_text;
63 static vd_bitblt_bmp_t	ofwfb_bitblt_bitmap;
64 
65 static const struct vt_driver vt_ofwfb_driver = {
66 	.vd_name	= "ofwfb",
67 	.vd_probe	= ofwfb_probe,
68 	.vd_init	= ofwfb_init,
69 	.vd_blank	= vt_fb_blank,
70 	.vd_bitblt_text	= ofwfb_bitblt_text,
71 	.vd_bitblt_bmp	= ofwfb_bitblt_bitmap,
72 	.vd_fb_ioctl	= vt_fb_ioctl,
73 	.vd_fb_mmap	= vt_fb_mmap,
74 	.vd_priority	= VD_PRIORITY_GENERIC+1,
75 };
76 
77 static unsigned char ofw_colors[16] = {
78 	/* See "16-color Text Extension" Open Firmware document, page 4 */
79 	0, 4, 2, 6, 1, 5, 3, 7,
80 	8, 12, 10, 14, 9, 13, 11, 15
81 };
82 
83 static struct ofwfb_softc ofwfb_conssoftc;
84 VT_DRIVER_DECLARE(vt_ofwfb, vt_ofwfb_driver);
85 
86 static int
87 ofwfb_probe(struct vt_device *vd)
88 {
89 	phandle_t chosen, node;
90 	ihandle_t stdout;
91 	char type[64];
92 
93 	chosen = OF_finddevice("/chosen");
94 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
95 	node = OF_instance_to_package(stdout);
96 	if (node == -1) {
97 		/*
98 		 * The "/chosen/stdout" does not exist try
99 		 * using "screen" directly.
100 		 */
101 		node = OF_finddevice("screen");
102 	}
103 	OF_getprop(node, "device_type", type, sizeof(type));
104 	if (strcmp(type, "display") != 0)
105 		return (CN_DEAD);
106 
107 	/* Looks OK... */
108 	return (CN_INTERNAL);
109 }
110 
111 static void
112 ofwfb_bitblt_bitmap(struct vt_device *vd, const struct vt_window *vw,
113     const uint8_t *pattern, const uint8_t *mask,
114     unsigned int width, unsigned int height,
115     unsigned int x, unsigned int y, term_color_t fg, term_color_t bg)
116 {
117 	struct fb_info *sc = vd->vd_softc;
118 	u_long line;
119 	uint32_t fgc, bgc;
120 	int c, l;
121 	uint8_t b, m;
122 	union {
123 		uint32_t l;
124 		uint8_t	 c[4];
125 	} ch1, ch2;
126 
127 	fgc = sc->fb_cmap[fg];
128 	bgc = sc->fb_cmap[bg];
129 	b = m = 0;
130 
131 	if (((struct ofwfb_softc *)vd->vd_softc)->iso_palette) {
132 		fg = ofw_colors[fg];
133 		bg = ofw_colors[bg];
134 	}
135 
136 	line = (sc->fb_stride * y) + x * sc->fb_bpp/8;
137 	if (mask == NULL && sc->fb_bpp == 8 && (width % 8 == 0)) {
138 		/* Don't try to put off screen pixels */
139 		if (((x + width) > vd->vd_width) || ((y + height) >
140 		    vd->vd_height))
141 			return;
142 
143 		for (; height > 0; height--) {
144 			for (c = 0; c < width; c += 8) {
145 				b = *pattern++;
146 
147 				/*
148 				 * Assume that there is more background than
149 				 * foreground in characters and init accordingly
150 				 */
151 				ch1.l = ch2.l = (bg << 24) | (bg << 16) |
152 				    (bg << 8) | bg;
153 
154 				/*
155 				 * Calculate 2 x 4-chars at a time, and then
156 				 * write these out.
157 				 */
158 				if (b & 0x80) ch1.c[0] = fg;
159 				if (b & 0x40) ch1.c[1] = fg;
160 				if (b & 0x20) ch1.c[2] = fg;
161 				if (b & 0x10) ch1.c[3] = fg;
162 
163 				if (b & 0x08) ch2.c[0] = fg;
164 				if (b & 0x04) ch2.c[1] = fg;
165 				if (b & 0x02) ch2.c[2] = fg;
166 				if (b & 0x01) ch2.c[3] = fg;
167 
168 				*(uint32_t *)(sc->fb_vbase + line + c) = ch1.l;
169 				*(uint32_t *)(sc->fb_vbase + line + c + 4) =
170 				    ch2.l;
171 			}
172 			line += sc->fb_stride;
173 		}
174 	} else {
175 		for (l = 0;
176 		    l < height && y + l < vw->vw_draw_area.tr_end.tp_row;
177 		    l++) {
178 			for (c = 0;
179 			    c < width && x + c < vw->vw_draw_area.tr_end.tp_col;
180 			    c++) {
181 				if (c % 8 == 0)
182 					b = *pattern++;
183 				else
184 					b <<= 1;
185 				if (mask != NULL) {
186 					if (c % 8 == 0)
187 						m = *mask++;
188 					else
189 						m <<= 1;
190 					/* Skip pixel write, if mask not set. */
191 					if ((m & 0x80) == 0)
192 						continue;
193 				}
194 				switch(sc->fb_bpp) {
195 				case 8:
196 					*(uint8_t *)(sc->fb_vbase + line + c) =
197 					    b & 0x80 ? fg : bg;
198 					break;
199 				case 32:
200 					*(uint32_t *)(sc->fb_vbase + line + 4*c)
201 					    = (b & 0x80) ? fgc : bgc;
202 					break;
203 				default:
204 					/* panic? */
205 					break;
206 				}
207 			}
208 			line += sc->fb_stride;
209 		}
210 	}
211 }
212 
213 void
214 ofwfb_bitblt_text(struct vt_device *vd, const struct vt_window *vw,
215     const term_rect_t *area)
216 {
217 	unsigned int col, row, x, y;
218 	struct vt_font *vf;
219 	term_char_t c;
220 	term_color_t fg, bg;
221 	const uint8_t *pattern;
222 
223 	vf = vw->vw_font;
224 
225 	for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) {
226 		for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col;
227 		    ++col) {
228 			x = col * vf->vf_width +
229 			    vw->vw_draw_area.tr_begin.tp_col;
230 			y = row * vf->vf_height +
231 			    vw->vw_draw_area.tr_begin.tp_row;
232 
233 			c = VTBUF_GET_FIELD(&vw->vw_buf, row, col);
234 			pattern = vtfont_lookup(vf, c);
235 			vt_determine_colors(c,
236 			    VTBUF_ISCURSOR(&vw->vw_buf, row, col), &fg, &bg);
237 
238 			ofwfb_bitblt_bitmap(vd, vw,
239 			    pattern, NULL, vf->vf_width, vf->vf_height,
240 			    x, y, fg, bg);
241 		}
242 	}
243 
244 #ifndef SC_NO_CUTPASTE
245 	if (!vd->vd_mshown)
246 		return;
247 
248 	term_rect_t drawn_area;
249 
250 	drawn_area.tr_begin.tp_col = area->tr_begin.tp_col * vf->vf_width;
251 	drawn_area.tr_begin.tp_row = area->tr_begin.tp_row * vf->vf_height;
252 	drawn_area.tr_end.tp_col = area->tr_end.tp_col * vf->vf_width;
253 	drawn_area.tr_end.tp_row = area->tr_end.tp_row * vf->vf_height;
254 
255 	if (vt_is_cursor_in_area(vd, &drawn_area)) {
256 		ofwfb_bitblt_bitmap(vd, vw,
257 		    vd->vd_mcursor->map, vd->vd_mcursor->mask,
258 		    vd->vd_mcursor->width, vd->vd_mcursor->height,
259 		    vd->vd_mx_drawn + vw->vw_draw_area.tr_begin.tp_col,
260 		    vd->vd_my_drawn + vw->vw_draw_area.tr_begin.tp_row,
261 		    vd->vd_mcursor_fg, vd->vd_mcursor_bg);
262 	}
263 #endif
264 }
265 
266 static void
267 ofwfb_initialize(struct vt_device *vd)
268 {
269 	struct ofwfb_softc *sc = vd->vd_softc;
270 	int i, err;
271 	cell_t retval;
272 	uint32_t oldpix;
273 
274 	/*
275 	 * Set up the color map
276 	 */
277 
278 	sc->iso_palette = 0;
279 	switch (sc->fb.fb_bpp) {
280 	case 8:
281 		vt_generate_cons_palette(sc->fb.fb_cmap, COLOR_FORMAT_RGB, 255,
282 		    16, 255, 8, 255, 0);
283 
284 		for (i = 0; i < 16; i++) {
285 			err = OF_call_method("color!", sc->sc_handle, 4, 1,
286 			    (cell_t)((sc->fb.fb_cmap[i] >> 16) & 0xff),
287 			    (cell_t)((sc->fb.fb_cmap[i] >> 8) & 0xff),
288 			    (cell_t)((sc->fb.fb_cmap[i] >> 0) & 0xff),
289 			    (cell_t)i, &retval);
290 			if (err)
291 				break;
292 		}
293 		if (i != 16)
294 			sc->iso_palette = 1;
295 
296 		break;
297 
298 	case 32:
299 		/*
300 		 * We bypass the usual bus_space_() accessors here, mostly
301 		 * for performance reasons. In particular, we don't want
302 		 * any barrier operations that may be performed and handle
303 		 * endianness slightly different. Figure out the host-view
304 		 * endianness of the frame buffer.
305 		 */
306 		oldpix = bus_space_read_4(sc->sc_memt, sc->fb.fb_vbase, 0);
307 		bus_space_write_4(sc->sc_memt, sc->fb.fb_vbase, 0, 0xff000000);
308 		if (*(uint8_t *)(sc->fb.fb_vbase) == 0xff)
309 			vt_generate_cons_palette(sc->fb.fb_cmap,
310 			    COLOR_FORMAT_RGB, 255, 0, 255, 8, 255, 16);
311 		else
312 			vt_generate_cons_palette(sc->fb.fb_cmap,
313 			    COLOR_FORMAT_RGB, 255, 16, 255, 8, 255, 0);
314 		bus_space_write_4(sc->sc_memt, sc->fb.fb_vbase, 0, oldpix);
315 		break;
316 
317 	default:
318 		panic("Unknown color space depth %d", sc->fb.fb_bpp);
319 		break;
320         }
321 
322 	sc->fb.fb_cmsize = 16;
323 }
324 
325 static int
326 ofwfb_init(struct vt_device *vd)
327 {
328 	struct ofwfb_softc *sc;
329 	char type[64];
330 	phandle_t chosen;
331 	phandle_t node;
332 	uint32_t depth, height, width, stride;
333 	uint32_t fb_phys;
334 	int i, len;
335 #ifdef __sparc64__
336 	static struct bus_space_tag ofwfb_memt[1];
337 	bus_addr_t phys;
338 	int space;
339 #endif
340 
341 	/* Initialize softc */
342 	vd->vd_softc = sc = &ofwfb_conssoftc;
343 
344 	chosen = OF_finddevice("/chosen");
345 	OF_getprop(chosen, "stdout", &sc->sc_handle, sizeof(ihandle_t));
346 	node = OF_instance_to_package(sc->sc_handle);
347 	if (node == -1) {
348 		/*
349 		 * The "/chosen/stdout" does not exist try
350 		 * using "screen" directly.
351 		 */
352 		node = OF_finddevice("screen");
353 		sc->sc_handle = OF_open("screen");
354 	}
355 	OF_getprop(node, "device_type", type, sizeof(type));
356 	if (strcmp(type, "display") != 0)
357 		return (CN_DEAD);
358 
359 	/* Keep track of the OF node */
360 	sc->sc_node = node;
361 
362 	/*
363 	 * Try to use a 32-bit framebuffer if possible. This may be
364 	 * unimplemented and fail. That's fine -- it just means we are
365 	 * stuck with the defaults.
366 	 */
367 	OF_call_method("set-depth", sc->sc_handle, 1, 1, (cell_t)32, &i);
368 
369 	/* Make sure we have needed properties */
370 	if (OF_getproplen(node, "height") != sizeof(height) ||
371 	    OF_getproplen(node, "width") != sizeof(width) ||
372 	    OF_getproplen(node, "depth") != sizeof(depth) ||
373 	    OF_getproplen(node, "linebytes") != sizeof(sc->fb.fb_stride))
374 		return (CN_DEAD);
375 
376 	/* Only support 8 and 32-bit framebuffers */
377 	OF_getprop(node, "depth", &depth, sizeof(depth));
378 	if (depth != 8 && depth != 32)
379 		return (CN_DEAD);
380 	sc->fb.fb_bpp = sc->fb.fb_depth = depth;
381 
382 	OF_getprop(node, "height", &height, sizeof(height));
383 	OF_getprop(node, "width", &width, sizeof(width));
384 	OF_getprop(node, "linebytes", &stride, sizeof(stride));
385 
386 	sc->fb.fb_height = height;
387 	sc->fb.fb_width = width;
388 	sc->fb.fb_stride = stride;
389 	sc->fb.fb_size = sc->fb.fb_height * sc->fb.fb_stride;
390 
391 	/*
392 	 * Grab the physical address of the framebuffer, and then map it
393 	 * into our memory space. If the MMU is not yet up, it will be
394 	 * remapped for us when relocation turns on.
395 	 */
396 	if (OF_getproplen(node, "address") == sizeof(fb_phys)) {
397 	 	/* XXX We assume #address-cells is 1 at this point. */
398 		OF_getprop(node, "address", &fb_phys, sizeof(fb_phys));
399 
400 	#if defined(__powerpc__)
401 		sc->sc_memt = &bs_be_tag;
402 		bus_space_map(sc->sc_memt, fb_phys, sc->fb.fb_size,
403 		    BUS_SPACE_MAP_PREFETCHABLE, &sc->fb.fb_vbase);
404 	#elif defined(__sparc64__)
405 		OF_decode_addr(node, 0, &space, &phys);
406 		sc->sc_memt = &ofwfb_memt[0];
407 		sc->fb.fb_vbase =
408 		    sparc64_fake_bustag(space, fb_phys, sc->sc_memt);
409 	#elif defined(__arm__)
410 		sc->sc_memt = fdtbus_bs_tag;
411 		bus_space_map(sc->sc_memt, sc->fb.fb_pbase, sc->fb.fb_size,
412 		    BUS_SPACE_MAP_PREFETCHABLE,
413 		    (bus_space_handle_t *)&sc->fb.fb_vbase);
414 	#else
415 		#error Unsupported platform!
416 	#endif
417 
418 		sc->fb.fb_pbase = fb_phys;
419 	} else {
420 		/*
421 		 * Some IBM systems don't have an address property. Try to
422 		 * guess the framebuffer region from the assigned addresses.
423 		 * This is ugly, but there doesn't seem to be an alternative.
424 		 * Linux does the same thing.
425 		 */
426 
427 		struct ofw_pci_register pciaddrs[8];
428 		int num_pciaddrs = 0;
429 
430 		/*
431 		 * Get the PCI addresses of the adapter, if present. The node
432 		 * may be the child of the PCI device: in that case, try the
433 		 * parent for the assigned-addresses property.
434 		 */
435 		len = OF_getprop(node, "assigned-addresses", pciaddrs,
436 		    sizeof(pciaddrs));
437 		if (len == -1) {
438 			len = OF_getprop(OF_parent(node), "assigned-addresses",
439 			    pciaddrs, sizeof(pciaddrs));
440 		}
441 		if (len == -1)
442 			len = 0;
443 		num_pciaddrs = len / sizeof(struct ofw_pci_register);
444 
445 		fb_phys = num_pciaddrs;
446 		for (i = 0; i < num_pciaddrs; i++) {
447 			/* If it is too small, not the framebuffer */
448 			if (pciaddrs[i].size_lo < sc->fb.fb_stride * height)
449 				continue;
450 			/* If it is not memory, it isn't either */
451 			if (!(pciaddrs[i].phys_hi &
452 			    OFW_PCI_PHYS_HI_SPACE_MEM32))
453 				continue;
454 
455 			/* This could be the framebuffer */
456 			fb_phys = i;
457 
458 			/* If it is prefetchable, it certainly is */
459 			if (pciaddrs[i].phys_hi & OFW_PCI_PHYS_HI_PREFETCHABLE)
460 				break;
461 		}
462 
463 		if (fb_phys == num_pciaddrs) /* No candidates found */
464 			return (CN_DEAD);
465 
466 	#if defined(__powerpc__)
467 		OF_decode_addr(node, fb_phys, &sc->sc_memt, &sc->fb.fb_vbase);
468 		sc->fb.fb_pbase = sc->fb.fb_vbase; /* 1:1 mapped */
469 	#else
470 		/* No ability to interpret assigned-addresses otherwise */
471 		return (CN_DEAD);
472 	#endif
473         }
474 
475 
476 	ofwfb_initialize(vd);
477 	vt_fb_init(vd);
478 
479 	return (CN_INTERNAL);
480 }
481 
482