xref: /freebsd/sys/powerpc/ofw/ofw_syscons.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003 Peter Grehan
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/sysctl.h>
35 #include <sys/limits.h>
36 #include <sys/conf.h>
37 #include <sys/cons.h>
38 #include <sys/proc.h>
39 #include <sys/fcntl.h>
40 #include <sys/malloc.h>
41 #include <sys/fbio.h>
42 #include <sys/consio.h>
43 
44 #include <machine/bus.h>
45 #include <machine/sc_machdep.h>
46 #include <machine/vm.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 = 1;
59 static int ofwfb_reset_on_switch = 1;
60 static SYSCTL_NODE(_hw, OID_AUTO, ofwfb, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
61     "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 	sc = (struct ofwfb_softc *)adp;
818         row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
819         col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
820 	p = sc->sc_font + c*sc->sc_font_height;
821 	addr = (u_int32_t *)((uintptr_t)sc->sc_addr
822 		+ (row + sc->sc_ymargin)*sc->sc_stride
823 		+ col + sc->sc_xmargin);
824 
825 	fg = ofwfb_foreground(a);
826 	bg = ofwfb_background(a);
827 
828 	for (i = 0; i < sc->sc_font_height; i++) {
829 		u_char fline = p[i];
830 
831 		/*
832 		 * Assume that there is more background than foreground
833 		 * in characters and init accordingly
834 		 */
835 		ch1.l = ch2.l = (bg << 24) | (bg << 16) | (bg << 8) | bg;
836 
837 		/*
838 		 * Calculate 2 x 4-chars at a time, and then
839 		 * write these out.
840 		 */
841 		if (fline & 0x80) ch1.c[0] = fg;
842 		if (fline & 0x40) ch1.c[1] = fg;
843 		if (fline & 0x20) ch1.c[2] = fg;
844 		if (fline & 0x10) ch1.c[3] = fg;
845 
846 		if (fline & 0x08) ch2.c[0] = fg;
847 		if (fline & 0x04) ch2.c[1] = fg;
848 		if (fline & 0x02) ch2.c[2] = fg;
849 		if (fline & 0x01) ch2.c[3] = fg;
850 
851 		addr[0] = ch1.l;
852 		addr[1] = ch2.l;
853 		addr += (sc->sc_stride / sizeof(u_int32_t));
854 	}
855 
856 	return (0);
857 }
858 
859 static int
860 ofwfb_putc32(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
861 {
862 	struct ofwfb_softc *sc;
863 	int row;
864 	int col;
865 	int i, j, k;
866 	uint32_t *addr, fg, bg;
867 	u_char *p;
868 
869 	sc = (struct ofwfb_softc *)adp;
870         row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
871         col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
872 	p = sc->sc_font + c*sc->sc_font_height;
873 	addr = (uint32_t *)sc->sc_addr
874 		+ (row + sc->sc_ymargin)*(sc->sc_stride/4)
875 		+ col + sc->sc_xmargin;
876 
877 	fg = ofwfb_pix32(sc, ofwfb_foreground(a));
878 	bg = ofwfb_pix32(sc, ofwfb_background(a));
879 
880 	for (i = 0; i < sc->sc_font_height; i++) {
881 		for (j = 0, k = 7; j < 8; j++, k--) {
882 			if ((p[i] & (1 << k)) == 0)
883 				*(addr + j) = bg;
884 			else
885 				*(addr + j) = fg;
886 		}
887 		addr += (sc->sc_stride/4);
888 	}
889 
890 	return (0);
891 }
892 
893 static int
894 ofwfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
895 {
896 	struct ofwfb_softc *sc;
897 
898 	sc = (struct ofwfb_softc *)adp;
899 
900 	return ((*sc->sc_putc)(adp, off, c, a));
901 }
902 
903 static int
904 ofwfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
905 {
906 	int i;
907 
908 	for (i = 0; i < len; i++) {
909 		ofwfb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
910 	}
911 	return (0);
912 }
913 
914 static int
915 ofwfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
916     uint32_t pixel_mask, int size, int width)
917 {
918 	struct ofwfb_softc *sc;
919 
920 	sc = (struct ofwfb_softc *)adp;
921 
922 	return ((*sc->sc_putm)(adp, x, y, pixel_image, pixel_mask, size,
923 	    width));
924 }
925 
926 static int
927 ofwfb_putm8(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
928     uint32_t pixel_mask, int size, int width)
929 {
930 	struct ofwfb_softc *sc;
931 	int i, j, k;
932 	uint8_t *addr;
933 	u_char fg, bg;
934 
935 	sc = (struct ofwfb_softc *)adp;
936 	addr = (u_int8_t *)((uintptr_t)sc->sc_addr
937 		+ (y + sc->sc_ymargin)*sc->sc_stride
938 		+ x + sc->sc_xmargin);
939 
940 	fg = ofwfb_foreground(SC_NORM_ATTR);
941 	bg = ofwfb_background(SC_NORM_ATTR);
942 
943 	for (i = 0; i < size && i+y < sc->sc_height - 2*sc->sc_ymargin; i++) {
944 		/*
945 		 * Calculate 2 x 4-chars at a time, and then
946 		 * write these out.
947 		 */
948 		for (j = 0, k = width; j < 8; j++, k--) {
949 			if (x + j >= sc->sc_width - 2*sc->sc_xmargin)
950 				continue;
951 
952 			if (pixel_image[i] & (1 << k))
953 				addr[j] = (addr[j] == fg) ? bg : fg;
954 		}
955 
956 		addr += (sc->sc_stride / sizeof(u_int8_t));
957 	}
958 
959 	return (0);
960 }
961 
962 static int
963 ofwfb_putm32(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
964     uint32_t pixel_mask, int size, int width)
965 {
966 	struct ofwfb_softc *sc;
967 	int i, j, k;
968 	uint32_t fg, bg;
969 	uint32_t *addr;
970 
971 	sc = (struct ofwfb_softc *)adp;
972 	addr = (uint32_t *)sc->sc_addr
973 		+ (y + sc->sc_ymargin)*(sc->sc_stride/4)
974 		+ x + sc->sc_xmargin;
975 
976 	fg = ofwfb_pix32(sc, ofwfb_foreground(SC_NORM_ATTR));
977 	bg = ofwfb_pix32(sc, ofwfb_background(SC_NORM_ATTR));
978 
979 	for (i = 0; i < size && i+y < sc->sc_height - 2*sc->sc_ymargin; i++) {
980 		for (j = 0, k = width; j < 8; j++, k--) {
981 			if (x + j >= sc->sc_width - 2*sc->sc_xmargin)
982 				continue;
983 
984 			if (pixel_image[i] & (1 << k))
985 				*(addr + j) = (*(addr + j) == fg) ? bg : fg;
986 		}
987 		addr += (sc->sc_stride/4);
988 	}
989 
990 	return (0);
991 }
992 
993 /*
994  * Define the syscons nexus device attachment
995  */
996 static void
997 ofwfb_scidentify(driver_t *driver, device_t parent)
998 {
999 	device_t child;
1000 
1001 	/*
1002 	 * Add with a priority guaranteed to make it last on
1003 	 * the device list
1004 	 */
1005 	child = BUS_ADD_CHILD(parent, INT_MAX, SC_DRIVER_NAME, 0);
1006 }
1007 
1008 static int
1009 ofwfb_scprobe(device_t dev)
1010 {
1011 	int error;
1012 
1013 	device_set_desc(dev, "System console");
1014 
1015 	error = sc_probe_unit(device_get_unit(dev),
1016 	    device_get_flags(dev) | SC_AUTODETECT_KBD);
1017 	if (error != 0)
1018 		return (error);
1019 
1020 	/* This is a fake device, so make sure we added it ourselves */
1021 	return (BUS_PROBE_NOWILDCARD);
1022 }
1023 
1024 static int
1025 ofwfb_scattach(device_t dev)
1026 {
1027 	return (sc_attach_unit(device_get_unit(dev),
1028 	    device_get_flags(dev) | SC_AUTODETECT_KBD));
1029 }
1030 
1031 static device_method_t ofwfb_sc_methods[] = {
1032   	DEVMETHOD(device_identify,	ofwfb_scidentify),
1033 	DEVMETHOD(device_probe,		ofwfb_scprobe),
1034 	DEVMETHOD(device_attach,	ofwfb_scattach),
1035 	{ 0, 0 }
1036 };
1037 
1038 static driver_t ofwfb_sc_driver = {
1039 	SC_DRIVER_NAME,
1040 	ofwfb_sc_methods,
1041 	sizeof(sc_softc_t),
1042 };
1043 
1044 DRIVER_MODULE(ofwfb, nexus, ofwfb_sc_driver, 0, 0);
1045 
1046 /*
1047  * Utility routines from <dev/fb/fbreg.h>
1048  */
1049 void
1050 ofwfb_bcopy(const void *s, void *d, size_t c)
1051 {
1052 	bcopy(s, d, c);
1053 }
1054 
1055 void
1056 ofwfb_bzero(void *d, size_t c)
1057 {
1058 	bzero(d, c);
1059 }
1060 
1061 void
1062 ofwfb_fillw(int pat, void *base, size_t cnt)
1063 {
1064 	u_int16_t *bptr = base;
1065 
1066 	while (cnt--)
1067 		*bptr++ = pat;
1068 }
1069 
1070 u_int16_t
1071 ofwfb_readw(u_int16_t *addr)
1072 {
1073 	return (*addr);
1074 }
1075 
1076 void
1077 ofwfb_writew(u_int16_t *addr, u_int16_t val)
1078 {
1079 	*addr = val;
1080 }
1081