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