xref: /freebsd/sys/powerpc/ofw/ofw_syscons.c (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
1 /*-
2  * Copyright (c) 2003 Peter Grehan
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  * $FreeBSD$
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/limits.h>
38 #include <sys/conf.h>
39 #include <sys/cons.h>
40 #include <sys/proc.h>
41 #include <sys/fcntl.h>
42 #include <sys/malloc.h>
43 #include <sys/fbio.h>
44 #include <sys/consio.h>
45 
46 #include <machine/bus.h>
47 #include <machine/sc_machdep.h>
48 
49 #include <sys/rman.h>
50 
51 #include <dev/fb/fbreg.h>
52 #include <dev/syscons/syscons.h>
53 
54 #include <dev/ofw/openfirm.h>
55 #include <powerpc/ofw/ofw_syscons.h>
56 #include <machine/nexusvar.h>
57 
58 extern u_char dflt_font_16[];
59 extern u_char dflt_font_14[];
60 extern u_char dflt_font_8[];
61 
62 static int ofwfb_configure(int flags);
63 
64 static vi_probe_t ofwfb_probe;
65 static vi_init_t ofwfb_init;
66 static vi_get_info_t ofwfb_get_info;
67 static vi_query_mode_t ofwfb_query_mode;
68 static vi_set_mode_t ofwfb_set_mode;
69 static vi_save_font_t ofwfb_save_font;
70 static vi_load_font_t ofwfb_load_font;
71 static vi_show_font_t ofwfb_show_font;
72 static vi_save_palette_t ofwfb_save_palette;
73 static vi_load_palette_t ofwfb_load_palette;
74 static vi_set_border_t ofwfb_set_border;
75 static vi_save_state_t ofwfb_save_state;
76 static vi_load_state_t ofwfb_load_state;
77 static vi_set_win_org_t ofwfb_set_win_org;
78 static vi_read_hw_cursor_t ofwfb_read_hw_cursor;
79 static vi_set_hw_cursor_t ofwfb_set_hw_cursor;
80 static vi_set_hw_cursor_shape_t ofwfb_set_hw_cursor_shape;
81 static vi_blank_display_t ofwfb_blank_display;
82 static vi_mmap_t ofwfb_mmap;
83 static vi_ioctl_t ofwfb_ioctl;
84 static vi_clear_t ofwfb_clear;
85 static vi_fill_rect_t ofwfb_fill_rect;
86 static vi_bitblt_t ofwfb_bitblt;
87 static vi_diag_t ofwfb_diag;
88 static vi_save_cursor_palette_t ofwfb_save_cursor_palette;
89 static vi_load_cursor_palette_t ofwfb_load_cursor_palette;
90 static vi_copy_t ofwfb_copy;
91 static vi_putp_t ofwfb_putp;
92 static vi_putc_t ofwfb_putc;
93 static vi_puts_t ofwfb_puts;
94 static vi_putm_t ofwfb_putm;
95 
96 static video_switch_t ofwfbvidsw = {
97 	.probe			= ofwfb_probe,
98 	.init			= ofwfb_init,
99 	.get_info		= ofwfb_get_info,
100 	.query_mode		= ofwfb_query_mode,
101 	.set_mode		= ofwfb_set_mode,
102 	.save_font		= ofwfb_save_font,
103 	.load_font		= ofwfb_load_font,
104 	.show_font		= ofwfb_show_font,
105 	.save_palette		= ofwfb_save_palette,
106 	.load_palette		= ofwfb_load_palette,
107 	.set_border		= ofwfb_set_border,
108 	.save_state		= ofwfb_save_state,
109 	.load_state		= ofwfb_load_state,
110 	.set_win_org		= ofwfb_set_win_org,
111 	.read_hw_cursor		= ofwfb_read_hw_cursor,
112 	.set_hw_cursor		= ofwfb_set_hw_cursor,
113 	.set_hw_cursor_shape	= ofwfb_set_hw_cursor_shape,
114 	.blank_display		= ofwfb_blank_display,
115 	.mmap			= ofwfb_mmap,
116 	.ioctl			= ofwfb_ioctl,
117 	.clear			= ofwfb_clear,
118 	.fill_rect		= ofwfb_fill_rect,
119 	.bitblt			= ofwfb_bitblt,
120 	.diag			= ofwfb_diag,
121 	.save_cursor_palette	= ofwfb_save_cursor_palette,
122 	.load_cursor_palette	= ofwfb_load_cursor_palette,
123 	.copy			= ofwfb_copy,
124 	.putp			= ofwfb_putp,
125 	.putc			= ofwfb_putc,
126 	.puts			= ofwfb_puts,
127 	.putm			= ofwfb_putm,
128 };
129 
130 VIDEO_DRIVER(ofwfb, ofwfbvidsw, ofwfb_configure);
131 
132 extern sc_rndr_sw_t txtrndrsw;
133 RENDERER(ofwfb, 0, txtrndrsw, gfb_set);
134 
135 RENDERER_MODULE(ofwfb, gfb_set);
136 
137 /*
138  * Define the iso6429-1983 colormap
139  */
140 static struct {
141 	u_int8_t	red;
142 	u_int8_t	green;
143 	u_int8_t	blue;
144 } ofwfb_cmap[16] = {		/*  #     R    G    B   Color */
145 				/*  -     -    -    -   ----- */
146 	{ 0x00, 0x00, 0x00 },	/*  0     0    0    0   Black */
147 	{ 0x00, 0x00, 0xaa },	/*  1     0    0  2/3   Blue  */
148 	{ 0x00, 0xaa, 0x00 },	/*  2     0  2/3    0   Green */
149 	{ 0x00, 0xaa, 0xaa },	/*  3     0  2/3  2/3   Cyan  */
150 	{ 0xaa, 0x00, 0x00 },	/*  4   2/3    0    0   Red   */
151 	{ 0xaa, 0x00, 0xaa },	/*  5   2/3    0  2/3   Magenta */
152 	{ 0xaa, 0x55, 0x00 },	/*  6   2/3  1/3    0   Brown */
153 	{ 0xaa, 0xaa, 0xaa },	/*  7   2/3  2/3  2/3   White */
154         { 0x55, 0x55, 0x55 },	/*  8   1/3  1/3  1/3   Gray  */
155 	{ 0x55, 0x55, 0xff },	/*  9   1/3  1/3    1   Bright Blue  */
156 	{ 0x55, 0xff, 0x55 },	/* 10   1/3    1  1/3   Bright Green */
157 	{ 0x55, 0xff, 0xff },	/* 11   1/3    1    1   Bright Cyan  */
158 	{ 0xff, 0x55, 0x55 },	/* 12     1  1/3  1/3   Bright Red   */
159 	{ 0xff, 0x55, 0xff },	/* 13     1  1/3    1   Bright Magenta */
160 	{ 0xff, 0xff, 0x80 },	/* 14     1    1  1/3   Bright Yellow */
161 	{ 0xff, 0xff, 0xff }	/* 15     1    1    1   Bright White */
162 };
163 
164 #define	TODO	printf("%s: unimplemented\n", __func__)
165 
166 static u_int16_t ofwfb_static_window[ROW*COL];
167 
168 static struct ofwfb_softc ofwfb_softc;
169 
170 static int
171 ofwfb_background(u_int8_t attr)
172 {
173 	return (attr >> 4);
174 }
175 
176 static int
177 ofwfb_foreground(u_int8_t attr)
178 {
179 	return (attr & 0x0f);
180 }
181 
182 static int
183 ofwfb_configure(int flags)
184 {
185 	struct ofwfb_softc *sc;
186         phandle_t chosen;
187         ihandle_t stdout;
188 	phandle_t node;
189 	int depth;
190 	int disable;
191 	char type[16];
192 	static int done = 0;
193 
194 	disable = 0;
195 	TUNABLE_INT_FETCH("hw.syscons.disable", &disable);
196 	if (disable != 0)
197 		return (0);
198 
199 	if (done != 0)
200 		return (0);
201 	done = 1;
202 
203 	sc = &ofwfb_softc;
204 
205 	chosen = OF_finddevice("/chosen");
206 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
207         node = OF_instance_to_package(stdout);
208         OF_getprop(node, "device_type", type, sizeof(type));
209         if (strcmp(type, "display") != 0)
210                 return (0);
211 
212 	/* Only support 8-bit framebuffers */
213 	OF_getprop(node, "depth", &depth, sizeof(depth));
214 	if (depth != 8)
215 		return (0);
216 
217 	sc->sc_node = node;
218 	sc->sc_console = 1;
219 	OF_getprop(node, "height", &sc->sc_height, sizeof(sc->sc_height));
220 	OF_getprop(node, "width", &sc->sc_width, sizeof(sc->sc_width));
221 	OF_getprop(node, "linebytes", &sc->sc_stride, sizeof(sc->sc_stride));
222 
223 	/*
224 	 * XXX the physical address of the frame buffer is assumed to be
225 	 * BAT-mapped so it can be accessed directly
226 	 */
227 	OF_getprop(node, "address", &sc->sc_addr, sizeof(sc->sc_addr));
228 
229 	ofwfb_init(0, &sc->sc_va, 0);
230 
231 	return (0);
232 }
233 
234 static int
235 ofwfb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
236 {
237 	TODO;
238 	return (0);
239 }
240 
241 static int
242 ofwfb_init(int unit, video_adapter_t *adp, int flags)
243 {
244 	struct ofwfb_softc *sc;
245 	video_info_t *vi;
246 	char name[64];
247 	ihandle_t ih;
248 	int i;
249 	int cborder;
250 	int font_height;
251 	int retval;
252 
253 	sc = (struct ofwfb_softc *)adp;
254 	vi = &adp->va_info;
255 
256 	vid_init_struct(adp, "ofwfb", -1, unit);
257 
258 	/*
259 	 * Install the ISO6429 colormap - older OFW systems
260 	 * don't do this by default
261 	 */
262 	memset(name, 0, sizeof(name));
263 	OF_package_to_path(sc->sc_node, name, sizeof(name));
264 	ih = OF_open(name);
265 	for (i = 0; i < 16; i++) {
266 		OF_call_method("color!", ih, 4, 1,
267 			       ofwfb_cmap[i].red,
268 			       ofwfb_cmap[i].green,
269 			       ofwfb_cmap[i].blue,
270 			       i,
271 			       &retval);
272 	}
273 
274 	/* The default font size can be overridden by loader */
275 	font_height = 16;
276 	TUNABLE_INT_FETCH("hw.syscons.fsize", &font_height);
277 	if (font_height == 8) {
278 		sc->sc_font = dflt_font_8;
279 		sc->sc_font_height = 8;
280 	} else if (font_height == 14) {
281 		sc->sc_font = dflt_font_14;
282 		sc->sc_font_height = 14;
283 	} else {
284 		/* default is 8x16 */
285 		sc->sc_font = dflt_font_16;
286 		sc->sc_font_height = 16;
287 	}
288 
289 	/* The user can set a border in chars - default is 1 char width */
290 	cborder = 1;
291 	TUNABLE_INT_FETCH("hw.syscons.border", &cborder);
292 
293 	vi->vi_cheight = sc->sc_font_height;
294 	vi->vi_width = sc->sc_width/8 - 2*cborder;
295 	vi->vi_height = sc->sc_height/sc->sc_font_height - 2*cborder;
296 	vi->vi_cwidth = 8;
297 
298 	/*
299 	 * Clamp width/height to syscons maximums
300 	 */
301 	if (vi->vi_width > COL)
302 		vi->vi_width = COL;
303 	if (vi->vi_height > ROW)
304 		vi->vi_height = ROW;
305 
306 	sc->sc_xmargin = (sc->sc_width - (vi->vi_width * vi->vi_cwidth)) / 2;
307 	sc->sc_ymargin = (sc->sc_height - (vi->vi_height * vi->vi_cheight))/2;
308 
309 	/*
310 	 * Avoid huge amounts of conditional code in syscons by
311 	 * defining a dummy h/w text display buffer.
312 	 */
313 	adp->va_window = (vm_offset_t) ofwfb_static_window;
314 
315 	/* Enable future font-loading... */
316 	adp->va_flags |= V_ADP_FONT;
317 
318 	ofwfb_blank_display(&sc->sc_va, V_DISPLAY_ON);
319 
320 	ofwfb_set_mode(&sc->sc_va, 0);
321 
322 	vid_register(&sc->sc_va);
323 
324 	return (0);
325 }
326 
327 static int
328 ofwfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
329 {
330 	bcopy(&adp->va_info, info, sizeof(*info));
331 	return (0);
332 }
333 
334 static int
335 ofwfb_query_mode(video_adapter_t *adp, video_info_t *info)
336 {
337 	TODO;
338 	return (0);
339 }
340 
341 static int
342 ofwfb_set_mode(video_adapter_t *adp, int mode)
343 {
344 
345 	return (0);
346 }
347 
348 static int
349 ofwfb_save_font(video_adapter_t *adp, int page, int size, u_char *data,
350     int c, int count)
351 {
352 	TODO;
353 	return (0);
354 }
355 
356 static int
357 ofwfb_load_font(video_adapter_t *adp, int page, int size, u_char *data,
358     int c, int count)
359 {
360 	struct ofwfb_softc *sc;
361 
362 	sc = (struct ofwfb_softc *)adp;
363 
364 	/*
365 	 * syscons code has already determined that current width/height
366 	 * are unchanged for this new font
367 	 */
368 	sc->sc_font = data;
369 	return (0);
370 }
371 
372 static int
373 ofwfb_show_font(video_adapter_t *adp, int page)
374 {
375 	TODO;
376 	return (0);
377 }
378 
379 static int
380 ofwfb_save_palette(video_adapter_t *adp, u_char *palette)
381 {
382 	/* TODO; */
383 	return (0);
384 }
385 
386 static int
387 ofwfb_load_palette(video_adapter_t *adp, u_char *palette)
388 {
389 	/* TODO; */
390 	return (0);
391 }
392 
393 static int
394 ofwfb_set_border(video_adapter_t *adp, int border)
395 {
396 	/* TODO; */
397 	return (0);
398 }
399 
400 static int
401 ofwfb_save_state(video_adapter_t *adp, void *p, size_t size)
402 {
403 	TODO;
404 	return (0);
405 }
406 
407 static int
408 ofwfb_load_state(video_adapter_t *adp, void *p)
409 {
410 	TODO;
411 	return (0);
412 }
413 
414 static int
415 ofwfb_set_win_org(video_adapter_t *adp, off_t offset)
416 {
417 	TODO;
418 	return (0);
419 }
420 
421 static int
422 ofwfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
423 {
424 	*col = 0;
425 	*row = 0;
426 
427 	return (0);
428 }
429 
430 static int
431 ofwfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
432 {
433 
434 	return (0);
435 }
436 
437 static int
438 ofwfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
439     int celsize, int blink)
440 {
441 	return (0);
442 }
443 
444 static int
445 ofwfb_blank_display(video_adapter_t *adp, int mode)
446 {
447 	struct ofwfb_softc *sc;
448 	int i;
449 	u_int8_t *addr;
450 
451 	sc = (struct ofwfb_softc *)adp;
452 	addr = (u_int8_t *) sc->sc_addr;
453 
454 	/* Could be done a lot faster e.g. 32-bits, or Altivec'd */
455 	for (i = 0; i < sc->sc_stride*sc->sc_height; i++)
456 		*(addr + i) = ofwfb_background(SC_NORM_ATTR);
457 
458 	return (0);
459 }
460 
461 static int
462 ofwfb_mmap(video_adapter_t *adp, vm_offset_t offset, vm_paddr_t *paddr,
463     int prot)
464 {
465 	TODO;
466 	return (0);
467 }
468 
469 static int
470 ofwfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
471 {
472 	TODO;
473 	return (0);
474 }
475 
476 static int
477 ofwfb_clear(video_adapter_t *adp)
478 {
479 	TODO;
480 	return (0);
481 }
482 
483 static int
484 ofwfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
485 {
486 	TODO;
487 	return (0);
488 }
489 
490 static int
491 ofwfb_bitblt(video_adapter_t *adp, ...)
492 {
493 	TODO;
494 	return (0);
495 }
496 
497 static int
498 ofwfb_diag(video_adapter_t *adp, int level)
499 {
500 	TODO;
501 	return (0);
502 }
503 
504 static int
505 ofwfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
506 {
507 	TODO;
508 	return (0);
509 }
510 
511 static int
512 ofwfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
513 {
514 	TODO;
515 	return (0);
516 }
517 
518 static int
519 ofwfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
520 {
521 	TODO;
522 	return (0);
523 }
524 
525 static int
526 ofwfb_putp(video_adapter_t *adp, vm_offset_t off, u_int32_t p, u_int32_t a,
527     int size, int bpp, int bit_ltor, int byte_ltor)
528 {
529 	TODO;
530 	return (0);
531 }
532 
533 static int
534 ofwfb_putc(video_adapter_t *adp, vm_offset_t off, u_int8_t c, u_int8_t a)
535 {
536 	struct ofwfb_softc *sc;
537 	int row;
538 	int col;
539 	int i, j, k;
540 	u_int8_t *addr;
541 	u_char *p;
542 
543 	sc = (struct ofwfb_softc *)adp;
544         row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
545         col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
546 	p = sc->sc_font + c*sc->sc_font_height;
547 	addr = (u_int8_t *)sc->sc_addr + (row + sc->sc_ymargin)*sc->sc_stride
548 		+ col + sc->sc_xmargin;
549 
550 	for (i = 0; i < sc->sc_font_height; i++) {
551 		for (j = 0, k = 7; j < 8; j++, k--) {
552 			if ((p[i] & (1 << k)) == 0)
553 				*(addr + j) = ofwfb_background(a);
554 			else
555 				*(addr + j) = ofwfb_foreground(a);
556 		}
557 		addr += sc->sc_stride;
558 	}
559 
560 	return (0);
561 }
562 
563 static int
564 ofwfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
565 {
566 	int i;
567 
568 	for (i = 0; i < len; i++) {
569 		ofwfb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
570 	}
571 	return (0);
572 }
573 
574 static int
575 ofwfb_putm(video_adapter_t *adp, int x, int y, u_int8_t *pixel_image,
576     u_int32_t pixel_mask, int size)
577 {
578 	struct ofwfb_softc *sc;
579 
580 	sc = (struct ofwfb_softc *)adp;
581 
582 	/* put mouse */
583 
584 	return (0);
585 }
586 
587 /*
588  * Define the syscons nexus device attachment
589  */
590 static void
591 ofwfb_scidentify(driver_t *driver, device_t parent)
592 {
593 	device_t child;
594 
595 	/*
596 	 * Add with a priority guaranteed to make it last on
597 	 * the device list
598 	 */
599 	child = BUS_ADD_CHILD(parent, INT_MAX, SC_DRIVER_NAME, 0);
600 	if (child != NULL)
601 		nexus_set_device_type(child, "syscons");
602 }
603 
604 static int
605 ofwfb_scprobe(device_t dev)
606 {
607 	char *name;
608 
609 	name = nexus_get_name(dev);
610 	if (strcmp(SC_DRIVER_NAME, name) != 0)
611 		return (ENXIO);
612 
613 	device_set_desc(dev, "System console");
614 	return (sc_probe_unit(device_get_unit(dev),
615 	    device_get_flags(dev) | SC_AUTODETECT_KBD));
616 }
617 
618 static int
619 ofwfb_scattach(device_t dev)
620 {
621 	return (sc_attach_unit(device_get_unit(dev),
622 	    device_get_flags(dev) | SC_AUTODETECT_KBD));
623 }
624 
625 static device_method_t ofwfb_sc_methods[] = {
626   	DEVMETHOD(device_identify,	ofwfb_scidentify),
627 	DEVMETHOD(device_probe,		ofwfb_scprobe),
628 	DEVMETHOD(device_attach,	ofwfb_scattach),
629 	{ 0, 0 }
630 };
631 
632 static driver_t ofwfb_sc_driver = {
633 	SC_DRIVER_NAME,
634 	ofwfb_sc_methods,
635 	sizeof(sc_softc_t),
636 };
637 
638 static devclass_t	sc_devclass;
639 
640 DRIVER_MODULE(sc, nexus, ofwfb_sc_driver, sc_devclass, 0, 0);
641 
642 /*
643  * Define a stub keyboard driver in case one hasn't been
644  * compiled into the kernel
645  */
646 #include <sys/kbio.h>
647 #include <dev/kbd/kbdreg.h>
648 
649 static int dummy_kbd_configure(int flags);
650 
651 keyboard_switch_t dummysw;
652 
653 static int
654 dummy_kbd_configure(int flags)
655 {
656 
657 	return (0);
658 }
659 KEYBOARD_DRIVER(dummy, dummysw, dummy_kbd_configure);
660 
661 /*
662  * Utility routines from <dev/fb/fbreg.h>
663  */
664 void
665 ofwfb_bcopy(const void *s, void *d, size_t c)
666 {
667 	bcopy(s, d, c);
668 }
669 
670 void
671 ofwfb_bzero(void *d, size_t c)
672 {
673 	bzero(d, c);
674 }
675 
676 void
677 ofwfb_fillw(int pat, void *base, size_t cnt)
678 {
679 	u_int16_t *bptr = base;
680 
681 	while (cnt--)
682 		*bptr++ = pat;
683 }
684 
685 u_int16_t
686 ofwfb_readw(u_int16_t *addr)
687 {
688 	return (*addr);
689 }
690 
691 void
692 ofwfb_writew(u_int16_t *addr, u_int16_t val)
693 {
694 	*addr = val;
695 }
696