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