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