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