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