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