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