xref: /freebsd/sys/powerpc/ofw/ofw_syscons.c (revision 39beb93c)
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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/sysctl.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 <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_pci.h>
56 #include <powerpc/ofw/ofw_syscons.h>
57 
58 static int ofwfb_ignore_mmap_checks;
59 SYSCTL_NODE(_hw, OID_AUTO, ofwfb, CTLFLAG_RD, 0, "ofwfb");
60 SYSCTL_INT(_hw_ofwfb, OID_AUTO, relax_mmap, CTLFLAG_RW,
61     &ofwfb_ignore_mmap_checks, 0, "relax mmap bounds checking");
62 
63 extern u_char dflt_font_16[];
64 extern u_char dflt_font_14[];
65 extern u_char dflt_font_8[];
66 
67 static int ofwfb_configure(int flags);
68 
69 static vi_probe_t ofwfb_probe;
70 static vi_init_t ofwfb_init;
71 static vi_get_info_t ofwfb_get_info;
72 static vi_query_mode_t ofwfb_query_mode;
73 static vi_set_mode_t ofwfb_set_mode;
74 static vi_save_font_t ofwfb_save_font;
75 static vi_load_font_t ofwfb_load_font;
76 static vi_show_font_t ofwfb_show_font;
77 static vi_save_palette_t ofwfb_save_palette;
78 static vi_load_palette_t ofwfb_load_palette;
79 static vi_set_border_t ofwfb_set_border;
80 static vi_save_state_t ofwfb_save_state;
81 static vi_load_state_t ofwfb_load_state;
82 static vi_set_win_org_t ofwfb_set_win_org;
83 static vi_read_hw_cursor_t ofwfb_read_hw_cursor;
84 static vi_set_hw_cursor_t ofwfb_set_hw_cursor;
85 static vi_set_hw_cursor_shape_t ofwfb_set_hw_cursor_shape;
86 static vi_blank_display_t ofwfb_blank_display;
87 static vi_mmap_t ofwfb_mmap;
88 static vi_ioctl_t ofwfb_ioctl;
89 static vi_clear_t ofwfb_clear;
90 static vi_fill_rect_t ofwfb_fill_rect;
91 static vi_bitblt_t ofwfb_bitblt;
92 static vi_diag_t ofwfb_diag;
93 static vi_save_cursor_palette_t ofwfb_save_cursor_palette;
94 static vi_load_cursor_palette_t ofwfb_load_cursor_palette;
95 static vi_copy_t ofwfb_copy;
96 static vi_putp_t ofwfb_putp;
97 static vi_putc_t ofwfb_putc;
98 static vi_puts_t ofwfb_puts;
99 static vi_putm_t ofwfb_putm;
100 
101 static video_switch_t ofwfbvidsw = {
102 	.probe			= ofwfb_probe,
103 	.init			= ofwfb_init,
104 	.get_info		= ofwfb_get_info,
105 	.query_mode		= ofwfb_query_mode,
106 	.set_mode		= ofwfb_set_mode,
107 	.save_font		= ofwfb_save_font,
108 	.load_font		= ofwfb_load_font,
109 	.show_font		= ofwfb_show_font,
110 	.save_palette		= ofwfb_save_palette,
111 	.load_palette		= ofwfb_load_palette,
112 	.set_border		= ofwfb_set_border,
113 	.save_state		= ofwfb_save_state,
114 	.load_state		= ofwfb_load_state,
115 	.set_win_org		= ofwfb_set_win_org,
116 	.read_hw_cursor		= ofwfb_read_hw_cursor,
117 	.set_hw_cursor		= ofwfb_set_hw_cursor,
118 	.set_hw_cursor_shape	= ofwfb_set_hw_cursor_shape,
119 	.blank_display		= ofwfb_blank_display,
120 	.mmap			= ofwfb_mmap,
121 	.ioctl			= ofwfb_ioctl,
122 	.clear			= ofwfb_clear,
123 	.fill_rect		= ofwfb_fill_rect,
124 	.bitblt			= ofwfb_bitblt,
125 	.diag			= ofwfb_diag,
126 	.save_cursor_palette	= ofwfb_save_cursor_palette,
127 	.load_cursor_palette	= ofwfb_load_cursor_palette,
128 	.copy			= ofwfb_copy,
129 	.putp			= ofwfb_putp,
130 	.putc			= ofwfb_putc,
131 	.puts			= ofwfb_puts,
132 	.putm			= ofwfb_putm,
133 };
134 
135 /*
136  * bitmap depth-specific routines
137  */
138 static vi_blank_display_t ofwfb_blank_display8;
139 static vi_putc_t ofwfb_putc8;
140 static vi_putm_t ofwfb_putm8;
141 static vi_set_border_t ofwfb_set_border8;
142 
143 static vi_blank_display_t ofwfb_blank_display32;
144 static vi_putc_t ofwfb_putc32;
145 static vi_putm_t ofwfb_putm32;
146 static vi_set_border_t ofwfb_set_border32;
147 
148 VIDEO_DRIVER(ofwfb, ofwfbvidsw, ofwfb_configure);
149 
150 extern sc_rndr_sw_t txtrndrsw;
151 RENDERER(ofwfb, 0, txtrndrsw, gfb_set);
152 
153 RENDERER_MODULE(ofwfb, gfb_set);
154 
155 /*
156  * Define the iso6429-1983 colormap
157  */
158 static struct {
159 	uint8_t	red;
160 	uint8_t	green;
161 	uint8_t	blue;
162 } ofwfb_cmap[16] = {		/*  #     R    G    B   Color */
163 				/*  -     -    -    -   ----- */
164 	{ 0x00, 0x00, 0x00 },	/*  0     0    0    0   Black */
165 	{ 0x00, 0x00, 0xaa },	/*  1     0    0  2/3   Blue  */
166 	{ 0x00, 0xaa, 0x00 },	/*  2     0  2/3    0   Green */
167 	{ 0x00, 0xaa, 0xaa },	/*  3     0  2/3  2/3   Cyan  */
168 	{ 0xaa, 0x00, 0x00 },	/*  4   2/3    0    0   Red   */
169 	{ 0xaa, 0x00, 0xaa },	/*  5   2/3    0  2/3   Magenta */
170 	{ 0xaa, 0x55, 0x00 },	/*  6   2/3  1/3    0   Brown */
171 	{ 0xaa, 0xaa, 0xaa },	/*  7   2/3  2/3  2/3   White */
172         { 0x55, 0x55, 0x55 },	/*  8   1/3  1/3  1/3   Gray  */
173 	{ 0x55, 0x55, 0xff },	/*  9   1/3  1/3    1   Bright Blue  */
174 	{ 0x55, 0xff, 0x55 },	/* 10   1/3    1  1/3   Bright Green */
175 	{ 0x55, 0xff, 0xff },	/* 11   1/3    1    1   Bright Cyan  */
176 	{ 0xff, 0x55, 0x55 },	/* 12     1  1/3  1/3   Bright Red   */
177 	{ 0xff, 0x55, 0xff },	/* 13     1  1/3    1   Bright Magenta */
178 	{ 0xff, 0xff, 0x80 },	/* 14     1    1  1/3   Bright Yellow */
179 	{ 0xff, 0xff, 0xff }	/* 15     1    1    1   Bright White */
180 };
181 
182 #define	TODO	printf("%s: unimplemented\n", __func__)
183 
184 static u_int16_t ofwfb_static_window[ROW*COL];
185 
186 static struct ofwfb_softc ofwfb_softc;
187 
188 static __inline int
189 ofwfb_background(uint8_t attr)
190 {
191 	return (attr >> 4);
192 }
193 
194 static __inline int
195 ofwfb_foreground(uint8_t attr)
196 {
197 	return (attr & 0x0f);
198 }
199 
200 static u_int
201 ofwfb_pix32(int attr)
202 {
203 	u_int retval;
204 
205 	retval = (ofwfb_cmap[attr].blue  << 16) |
206 		(ofwfb_cmap[attr].green << 8) |
207 		ofwfb_cmap[attr].red;
208 
209 	return (retval);
210 }
211 
212 static int
213 ofwfb_configure(int flags)
214 {
215 	struct ofwfb_softc *sc;
216         phandle_t chosen;
217         ihandle_t stdout;
218 	phandle_t node;
219 	int depth;
220 	int disable;
221 	int len;
222 	char type[16];
223 	static int done = 0;
224 
225 	disable = 0;
226 	TUNABLE_INT_FETCH("hw.syscons.disable", &disable);
227 	if (disable != 0)
228 		return (0);
229 
230 	if (done != 0)
231 		return (0);
232 	done = 1;
233 
234 	sc = &ofwfb_softc;
235 
236 	chosen = OF_finddevice("/chosen");
237 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
238         node = OF_instance_to_package(stdout);
239 	if (node == -1) {
240 		/*
241 		 * The "/chosen/stdout" does not exist try
242 		 * using "screen" directly.
243 		 */
244 		node = OF_finddevice("screen");
245 	}
246 	OF_getprop(node, "device_type", type, sizeof(type));
247 	if (strcmp(type, "display") != 0)
248 		return (0);
249 
250 	/* Only support 8 and 32-bit framebuffers */
251 	OF_getprop(node, "depth", &depth, sizeof(depth));
252 	if (depth == 8) {
253 		sc->sc_blank = ofwfb_blank_display8;
254 		sc->sc_putc = ofwfb_putc8;
255 		sc->sc_putm = ofwfb_putm8;
256 		sc->sc_set_border = ofwfb_set_border8;
257 	} else if (depth == 32) {
258 		sc->sc_blank = ofwfb_blank_display32;
259 		sc->sc_putc = ofwfb_putc32;
260 		sc->sc_putm = ofwfb_putm32;
261 		sc->sc_set_border = ofwfb_set_border32;
262 	} else
263 		return (0);
264 
265 	sc->sc_depth = depth;
266 	sc->sc_node = node;
267 	sc->sc_console = 1;
268 	OF_getprop(node, "height", &sc->sc_height, sizeof(sc->sc_height));
269 	OF_getprop(node, "width", &sc->sc_width, sizeof(sc->sc_width));
270 	OF_getprop(node, "linebytes", &sc->sc_stride, sizeof(sc->sc_stride));
271 
272 	/*
273 	 * XXX the physical address of the frame buffer is assumed to be
274 	 * BAT-mapped so it can be accessed directly
275 	 */
276 	OF_getprop(node, "address", &sc->sc_addr, sizeof(sc->sc_addr));
277 
278 	/*
279 	 * Get the PCI addresses of the adapter. The node may be the
280 	 * child of the PCI device: in that case, try the parent for
281 	 * the assigned-addresses property.
282 	 */
283 	len = OF_getprop(node, "assigned-addresses", sc->sc_pciaddrs,
284 	          sizeof(sc->sc_pciaddrs));
285 	if (len == -1) {
286 		len = OF_getprop(OF_parent(node), "assigned-addresses", sc->sc_pciaddrs,
287 		          sizeof(sc->sc_pciaddrs));
288 	}
289 
290 	if (len != -1) {
291 		sc->sc_num_pciaddrs = len / sizeof(struct ofw_pci_register);
292 	}
293 
294 	ofwfb_init(0, &sc->sc_va, 0);
295 
296 	return (0);
297 }
298 
299 static int
300 ofwfb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
301 {
302 	TODO;
303 	return (0);
304 }
305 
306 static int
307 ofwfb_init(int unit, video_adapter_t *adp, int flags)
308 {
309 	struct ofwfb_softc *sc;
310 	video_info_t *vi;
311 	int cborder;
312 	int font_height;
313 
314 	sc = (struct ofwfb_softc *)adp;
315 	vi = &adp->va_info;
316 
317 	vid_init_struct(adp, "ofwfb", -1, unit);
318 
319 	/* The default font size can be overridden by loader */
320 	font_height = 16;
321 	TUNABLE_INT_FETCH("hw.syscons.fsize", &font_height);
322 	if (font_height == 8) {
323 		sc->sc_font = dflt_font_8;
324 		sc->sc_font_height = 8;
325 	} else if (font_height == 14) {
326 		sc->sc_font = dflt_font_14;
327 		sc->sc_font_height = 14;
328 	} else {
329 		/* default is 8x16 */
330 		sc->sc_font = dflt_font_16;
331 		sc->sc_font_height = 16;
332 	}
333 
334 	/* The user can set a border in chars - default is 1 char width */
335 	cborder = 1;
336 	TUNABLE_INT_FETCH("hw.syscons.border", &cborder);
337 
338 	vi->vi_cheight = sc->sc_font_height;
339 	vi->vi_width = sc->sc_width/8 - 2*cborder;
340 	vi->vi_height = sc->sc_height/sc->sc_font_height - 2*cborder;
341 	vi->vi_cwidth = 8;
342 
343 	/*
344 	 * Clamp width/height to syscons maximums
345 	 */
346 	if (vi->vi_width > COL)
347 		vi->vi_width = COL;
348 	if (vi->vi_height > ROW)
349 		vi->vi_height = ROW;
350 
351 	sc->sc_xmargin = (sc->sc_width - (vi->vi_width * vi->vi_cwidth)) / 2;
352 	sc->sc_ymargin = (sc->sc_height - (vi->vi_height * vi->vi_cheight))/2;
353 
354 	/*
355 	 * Avoid huge amounts of conditional code in syscons by
356 	 * defining a dummy h/w text display buffer.
357 	 */
358 	adp->va_window = (vm_offset_t) ofwfb_static_window;
359 
360 	/*
361 	 * Enable future font-loading and flag color support, as well as
362 	 * adding V_ADP_MODECHANGE so that we ofwfb_set_mode() gets called
363 	 * when the X server shuts down. This enables us to get the console
364 	 * back when X disappears.
365 	 */
366 	adp->va_flags |= V_ADP_FONT | V_ADP_COLOR | V_ADP_MODECHANGE;
367 
368 	ofwfb_set_mode(&sc->sc_va, 0);
369 
370 	vid_register(&sc->sc_va);
371 
372 	return (0);
373 }
374 
375 static int
376 ofwfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
377 {
378 	bcopy(&adp->va_info, info, sizeof(*info));
379 	return (0);
380 }
381 
382 static int
383 ofwfb_query_mode(video_adapter_t *adp, video_info_t *info)
384 {
385 	TODO;
386 	return (0);
387 }
388 
389 static int
390 ofwfb_set_mode(video_adapter_t *adp, int mode)
391 {
392 	struct ofwfb_softc *sc;
393 	char name[64];
394 	ihandle_t ih;
395 	int i, retval;
396 
397 	sc = (struct ofwfb_softc *)adp;
398 
399 	/*
400 	 * Open the display device, which will initialize it.
401 	 */
402 
403 	memset(name, 0, sizeof(name));
404 	OF_package_to_path(sc->sc_node, name, sizeof(name));
405 	ih = OF_open(name);
406 
407 	if (sc->sc_depth == 8) {
408 		/*
409 		 * Install the ISO6429 colormap - older OFW systems
410 		 * don't do this by default
411 		 */
412 		for (i = 0; i < 16; i++) {
413 			OF_call_method("color!", ih, 4, 1,
414 				       ofwfb_cmap[i].red,
415 				       ofwfb_cmap[i].green,
416 				       ofwfb_cmap[i].blue,
417 				       i,
418 				       &retval);
419 		}
420 	}
421 
422 	ofwfb_blank_display(&sc->sc_va, V_DISPLAY_ON);
423 
424 	return (0);
425 }
426 
427 static int
428 ofwfb_save_font(video_adapter_t *adp, int page, int size, int width,
429     u_char *data, int c, int count)
430 {
431 	TODO;
432 	return (0);
433 }
434 
435 static int
436 ofwfb_load_font(video_adapter_t *adp, int page, int size, int width,
437     u_char *data, int c, int count)
438 {
439 	struct ofwfb_softc *sc;
440 
441 	sc = (struct ofwfb_softc *)adp;
442 
443 	/*
444 	 * syscons code has already determined that current width/height
445 	 * are unchanged for this new font
446 	 */
447 	sc->sc_font = data;
448 	return (0);
449 }
450 
451 static int
452 ofwfb_show_font(video_adapter_t *adp, int page)
453 {
454 
455 	return (0);
456 }
457 
458 static int
459 ofwfb_save_palette(video_adapter_t *adp, u_char *palette)
460 {
461 	/* TODO; */
462 	return (0);
463 }
464 
465 static int
466 ofwfb_load_palette(video_adapter_t *adp, u_char *palette)
467 {
468 	/* TODO; */
469 	return (0);
470 }
471 
472 static int
473 ofwfb_set_border8(video_adapter_t *adp, int border)
474 {
475 	struct ofwfb_softc *sc;
476 	int i, j;
477 	uint8_t *addr;
478 	uint8_t bground;
479 
480 	sc = (struct ofwfb_softc *)adp;
481 
482 	bground = ofwfb_background(border);
483 
484 	/* Set top margin */
485 	addr = (uint8_t *) sc->sc_addr;
486 	for (i = 0; i < sc->sc_ymargin; i++) {
487 		for (j = 0; j < sc->sc_width; j++) {
488 			*(addr + j) = bground;
489 		}
490 		addr += sc->sc_stride;
491 	}
492 
493 	/* bottom margin */
494 	addr = (uint8_t *) sc->sc_addr + (sc->sc_height - sc->sc_ymargin)*sc->sc_stride;
495 	for (i = 0; i < sc->sc_ymargin; i++) {
496 		for (j = 0; j < sc->sc_width; j++) {
497 			*(addr + j) = bground;
498 		}
499 		addr += sc->sc_stride;
500 	}
501 
502 	/* remaining left and right borders */
503 	addr = (uint8_t *) sc->sc_addr + sc->sc_ymargin*sc->sc_stride;
504 	for (i = 0; i < sc->sc_height - 2*sc->sc_xmargin; i++) {
505 		for (j = 0; j < sc->sc_xmargin; j++) {
506 			*(addr + j) = bground;
507 			*(addr + j + sc->sc_width - sc->sc_xmargin) = bground;
508 		}
509 		addr += sc->sc_stride;
510 	}
511 
512 	return (0);
513 }
514 
515 static int
516 ofwfb_set_border32(video_adapter_t *adp, int border)
517 {
518 	/* XXX Be lazy for now and blank entire screen */
519 	return (ofwfb_blank_display32(adp, border));
520 }
521 
522 static int
523 ofwfb_set_border(video_adapter_t *adp, int border)
524 {
525 	struct ofwfb_softc *sc;
526 
527 	sc = (struct ofwfb_softc *)adp;
528 
529 	return ((*sc->sc_set_border)(adp, border));
530 }
531 
532 static int
533 ofwfb_save_state(video_adapter_t *adp, void *p, size_t size)
534 {
535 	TODO;
536 	return (0);
537 }
538 
539 static int
540 ofwfb_load_state(video_adapter_t *adp, void *p)
541 {
542 	TODO;
543 	return (0);
544 }
545 
546 static int
547 ofwfb_set_win_org(video_adapter_t *adp, off_t offset)
548 {
549 	TODO;
550 	return (0);
551 }
552 
553 static int
554 ofwfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
555 {
556 	*col = 0;
557 	*row = 0;
558 
559 	return (0);
560 }
561 
562 static int
563 ofwfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
564 {
565 
566 	return (0);
567 }
568 
569 static int
570 ofwfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
571     int celsize, int blink)
572 {
573 	return (0);
574 }
575 
576 static int
577 ofwfb_blank_display8(video_adapter_t *adp, int mode)
578 {
579 	struct ofwfb_softc *sc;
580 	int i;
581 	uint8_t *addr;
582 
583 	sc = (struct ofwfb_softc *)adp;
584 	addr = (uint8_t *) sc->sc_addr;
585 
586 	/* Could be done a lot faster e.g. 32-bits, or Altivec'd */
587 	for (i = 0; i < sc->sc_stride*sc->sc_height; i++)
588 		*(addr + i) = ofwfb_background(SC_NORM_ATTR);
589 
590 	return (0);
591 }
592 
593 static int
594 ofwfb_blank_display32(video_adapter_t *adp, int mode)
595 {
596 	struct ofwfb_softc *sc;
597 	int i;
598 	uint32_t *addr;
599 
600 	sc = (struct ofwfb_softc *)adp;
601 	addr = (uint32_t *) sc->sc_addr;
602 
603 	for (i = 0; i < (sc->sc_stride/4)*sc->sc_height; i++)
604 		*(addr + i) = ofwfb_pix32(ofwfb_background(SC_NORM_ATTR));
605 
606 	return (0);
607 }
608 
609 static int
610 ofwfb_blank_display(video_adapter_t *adp, int mode)
611 {
612 	struct ofwfb_softc *sc;
613 
614 	sc = (struct ofwfb_softc *)adp;
615 
616 	return ((*sc->sc_blank)(adp, mode));
617 }
618 
619 static int
620 ofwfb_mmap(video_adapter_t *adp, vm_offset_t offset, vm_paddr_t *paddr,
621     int prot)
622 {
623 	struct ofwfb_softc *sc;
624 	int i;
625 
626 	sc = (struct ofwfb_softc *)adp;
627 
628 	if (sc->sc_num_pciaddrs == 0)
629 		return (ENOMEM);
630 
631 	/*
632 	 * Hack for Radeon...
633 	 */
634 	if (ofwfb_ignore_mmap_checks) {
635 		*paddr = offset;
636 		return (0);
637 	}
638 
639 	/*
640 	 * Make sure the requested address lies within the PCI device's assigned addrs
641 	 */
642 	for (i = 0; i < sc->sc_num_pciaddrs; i++)
643 		if (offset >= sc->sc_pciaddrs[i].phys_lo &&
644 		    offset < (sc->sc_pciaddrs[i].phys_lo + sc->sc_pciaddrs[i].size_lo)) {
645 			*paddr = offset;
646 			return (0);
647 		}
648 
649 	/*
650 	 * This might be a legacy VGA mem request: if so, just point it at the
651 	 * framebuffer, since it shouldn't be touched
652 	 */
653 	if (offset < sc->sc_stride*sc->sc_height) {
654 		*paddr = sc->sc_addr + offset;
655 		return (0);
656 	}
657 
658 	return (EINVAL);
659 }
660 
661 static int
662 ofwfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
663 {
664 
665 	return (0);
666 }
667 
668 static int
669 ofwfb_clear(video_adapter_t *adp)
670 {
671 	TODO;
672 	return (0);
673 }
674 
675 static int
676 ofwfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
677 {
678 	TODO;
679 	return (0);
680 }
681 
682 static int
683 ofwfb_bitblt(video_adapter_t *adp, ...)
684 {
685 	TODO;
686 	return (0);
687 }
688 
689 static int
690 ofwfb_diag(video_adapter_t *adp, int level)
691 {
692 	TODO;
693 	return (0);
694 }
695 
696 static int
697 ofwfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
698 {
699 	TODO;
700 	return (0);
701 }
702 
703 static int
704 ofwfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
705 {
706 	TODO;
707 	return (0);
708 }
709 
710 static int
711 ofwfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
712 {
713 	TODO;
714 	return (0);
715 }
716 
717 static int
718 ofwfb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
719     int size, int bpp, int bit_ltor, int byte_ltor)
720 {
721 	TODO;
722 	return (0);
723 }
724 
725 static int
726 ofwfb_putc8(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
727 {
728 	struct ofwfb_softc *sc;
729 	int row;
730 	int col;
731 	int i;
732 	uint32_t *addr;
733 	u_char *p, fg, bg;
734 	union {
735 		uint32_t l;
736 		uint8_t  c[4];
737 	} ch1, ch2;
738 
739 
740 	sc = (struct ofwfb_softc *)adp;
741         row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
742         col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
743 	p = sc->sc_font + c*sc->sc_font_height;
744 	addr = (u_int32_t *)((int)sc->sc_addr
745 		+ (row + sc->sc_ymargin)*sc->sc_stride
746 		+ col + sc->sc_xmargin);
747 
748 	fg = ofwfb_foreground(a);
749 	bg = ofwfb_background(a);
750 
751 	for (i = 0; i < sc->sc_font_height; i++) {
752 		u_char fline = p[i];
753 
754 		/*
755 		 * Assume that there is more background than foreground
756 		 * in characters and init accordingly
757 		 */
758 		ch1.l = ch2.l = (bg << 24) | (bg << 16) | (bg << 8) | bg;
759 
760 		/*
761 		 * Calculate 2 x 4-chars at a time, and then
762 		 * write these out.
763 		 */
764 		if (fline & 0x80) ch1.c[0] = fg;
765 		if (fline & 0x40) ch1.c[1] = fg;
766 		if (fline & 0x20) ch1.c[2] = fg;
767 		if (fline & 0x10) ch1.c[3] = fg;
768 
769 		if (fline & 0x08) ch2.c[0] = fg;
770 		if (fline & 0x04) ch2.c[1] = fg;
771 		if (fline & 0x02) ch2.c[2] = fg;
772 		if (fline & 0x01) ch2.c[3] = fg;
773 
774 		addr[0] = ch1.l;
775 		addr[1] = ch2.l;
776 		addr += (sc->sc_stride / sizeof(u_int32_t));
777 	}
778 
779 	return (0);
780 }
781 
782 static int
783 ofwfb_putc32(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
784 {
785 	struct ofwfb_softc *sc;
786 	int row;
787 	int col;
788 	int i, j, k;
789 	uint32_t *addr;
790 	u_char *p;
791 
792 	sc = (struct ofwfb_softc *)adp;
793         row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
794         col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
795 	p = sc->sc_font + c*sc->sc_font_height;
796 	addr = (uint32_t *)sc->sc_addr
797 		+ (row + sc->sc_ymargin)*(sc->sc_stride/4)
798 		+ col + sc->sc_xmargin;
799 
800 	for (i = 0; i < sc->sc_font_height; i++) {
801 		for (j = 0, k = 7; j < 8; j++, k--) {
802 			if ((p[i] & (1 << k)) == 0)
803 				*(addr + j) = ofwfb_pix32(ofwfb_background(a));
804 			else
805 				*(addr + j) = ofwfb_pix32(ofwfb_foreground(a));
806 		}
807 		addr += (sc->sc_stride/4);
808 	}
809 
810 	return (0);
811 }
812 
813 static int
814 ofwfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
815 {
816 	struct ofwfb_softc *sc;
817 
818 	sc = (struct ofwfb_softc *)adp;
819 
820 	return ((*sc->sc_putc)(adp, off, c, a));
821 }
822 
823 static int
824 ofwfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
825 {
826 	int i;
827 
828 	for (i = 0; i < len; i++) {
829 		ofwfb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
830 	}
831 	return (0);
832 }
833 
834 static int
835 ofwfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
836     uint32_t pixel_mask, int size, int width)
837 {
838 	struct ofwfb_softc *sc;
839 
840 	sc = (struct ofwfb_softc *)adp;
841 
842 	return ((*sc->sc_putm)(adp, x, y, pixel_image, pixel_mask, size,
843 	    width));
844 }
845 
846 static int
847 ofwfb_putm8(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
848     uint32_t pixel_mask, int size, int width)
849 {
850 	struct ofwfb_softc *sc;
851 	int i, j, k;
852 	uint32_t *addr;
853 	u_char fg, bg;
854 	union {
855 		uint32_t l[2];
856 		uint8_t  c[8];
857 	} ch;
858 
859 
860 	sc = (struct ofwfb_softc *)adp;
861 	addr = (u_int32_t *)((int)sc->sc_addr
862 		+ (y + sc->sc_ymargin)*sc->sc_stride
863 		+ x + sc->sc_xmargin);
864 
865 	fg = ofwfb_foreground(SC_NORM_ATTR);
866 	bg = ofwfb_background(SC_NORM_ATTR);
867 
868 	for (i = 0; i < size && i+y < sc->sc_height - 2*sc->sc_ymargin; i++) {
869 		/*
870 		 * Use the current values for the line
871 		 */
872 		ch.l[0] = addr[0];
873 		ch.l[1] = addr[1];
874 
875 		/*
876 		 * Calculate 2 x 4-chars at a time, and then
877 		 * write these out.
878 		 */
879 		for (j = 0, k = width; j < 8; j++, k--) {
880 			if (x + j >= sc->sc_width - 2*sc->sc_xmargin)
881 				continue;
882 
883 			if (pixel_image[i] & (1 << k))
884 				ch.c[j] = (ch.c[j] == fg) ? bg : fg;
885 		}
886 
887 		addr[0] = ch.l[0];
888 		addr[1] = ch.l[1];
889 		addr += (sc->sc_stride / sizeof(u_int32_t));
890 	}
891 
892 	return (0);
893 }
894 
895 static int
896 ofwfb_putm32(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
897     uint32_t pixel_mask, int size, int width)
898 {
899 	struct ofwfb_softc *sc;
900 	int i, j, k;
901 	uint32_t fg, bg;
902 	uint32_t *addr;
903 
904 	sc = (struct ofwfb_softc *)adp;
905 	addr = (uint32_t *)sc->sc_addr
906 		+ (y + sc->sc_ymargin)*(sc->sc_stride/4)
907 		+ x + sc->sc_xmargin;
908 
909 	fg = ofwfb_pix32(ofwfb_foreground(SC_NORM_ATTR));
910 	bg = ofwfb_pix32(ofwfb_background(SC_NORM_ATTR));
911 
912 	for (i = 0; i < size && i+y < sc->sc_height - 2*sc->sc_ymargin; i++) {
913 		for (j = 0, k = width; j < 8; j++, k--) {
914 			if (x + j >= sc->sc_width - 2*sc->sc_xmargin)
915 				continue;
916 
917 			if (pixel_image[i] & (1 << k))
918 				*(addr + j) = (*(addr + j) == fg) ? bg : fg;
919 		}
920 		addr += (sc->sc_stride/4);
921 	}
922 
923 	return (0);
924 }
925 
926 /*
927  * Define the syscons nexus device attachment
928  */
929 static void
930 ofwfb_scidentify(driver_t *driver, device_t parent)
931 {
932 	device_t child;
933 
934 	/*
935 	 * Add with a priority guaranteed to make it last on
936 	 * the device list
937 	 */
938 	child = BUS_ADD_CHILD(parent, INT_MAX, SC_DRIVER_NAME, 0);
939 }
940 
941 static int
942 ofwfb_scprobe(device_t dev)
943 {
944 	/* This is a fake device, so make sure there is no OF node for it */
945 	if (ofw_bus_get_node(dev) != -1)
946 		return ENXIO;
947 
948 	device_set_desc(dev, "System console");
949 	return (sc_probe_unit(device_get_unit(dev),
950 	    device_get_flags(dev) | SC_AUTODETECT_KBD));
951 }
952 
953 static int
954 ofwfb_scattach(device_t dev)
955 {
956 	return (sc_attach_unit(device_get_unit(dev),
957 	    device_get_flags(dev) | SC_AUTODETECT_KBD));
958 }
959 
960 static device_method_t ofwfb_sc_methods[] = {
961   	DEVMETHOD(device_identify,	ofwfb_scidentify),
962 	DEVMETHOD(device_probe,		ofwfb_scprobe),
963 	DEVMETHOD(device_attach,	ofwfb_scattach),
964 	{ 0, 0 }
965 };
966 
967 static driver_t ofwfb_sc_driver = {
968 	SC_DRIVER_NAME,
969 	ofwfb_sc_methods,
970 	sizeof(sc_softc_t),
971 };
972 
973 static devclass_t	sc_devclass;
974 
975 DRIVER_MODULE(sc, nexus, ofwfb_sc_driver, sc_devclass, 0, 0);
976 
977 /*
978  * Define a stub keyboard driver in case one hasn't been
979  * compiled into the kernel
980  */
981 #include <sys/kbio.h>
982 #include <dev/kbd/kbdreg.h>
983 
984 static int dummy_kbd_configure(int flags);
985 
986 keyboard_switch_t dummysw;
987 
988 static int
989 dummy_kbd_configure(int flags)
990 {
991 
992 	return (0);
993 }
994 KEYBOARD_DRIVER(dummy, dummysw, dummy_kbd_configure);
995 
996 /*
997  * Utility routines from <dev/fb/fbreg.h>
998  */
999 void
1000 ofwfb_bcopy(const void *s, void *d, size_t c)
1001 {
1002 	bcopy(s, d, c);
1003 }
1004 
1005 void
1006 ofwfb_bzero(void *d, size_t c)
1007 {
1008 	bzero(d, c);
1009 }
1010 
1011 void
1012 ofwfb_fillw(int pat, void *base, size_t cnt)
1013 {
1014 	u_int16_t *bptr = base;
1015 
1016 	while (cnt--)
1017 		*bptr++ = pat;
1018 }
1019 
1020 u_int16_t
1021 ofwfb_readw(u_int16_t *addr)
1022 {
1023 	return (*addr);
1024 }
1025 
1026 void
1027 ofwfb_writew(u_int16_t *addr, u_int16_t val)
1028 {
1029 	*addr = val;
1030 }
1031