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