ofw_syscons.c revision 142414
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 * $FreeBSD: head/sys/powerpc/ofw/ofw_syscons.c 142414 2005-02-25 02:29:01Z grehan $
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/powerpc/ofw/ofw_syscons.c 142414 2005-02-25 02:29:01Z grehan $");
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
50#include <sys/rman.h>
51
52#include <dev/fb/fbreg.h>
53#include <dev/syscons/syscons.h>
54
55#include <dev/ofw/openfirm.h>
56#include <dev/ofw/ofw_pci.h>
57#include <powerpc/ofw/ofw_syscons.h>
58#include <machine/nexusvar.h>
59
60extern u_char dflt_font_16[];
61extern u_char dflt_font_14[];
62extern u_char dflt_font_8[];
63
64static int ofwfb_configure(int flags);
65
66static vi_probe_t ofwfb_probe;
67static vi_init_t ofwfb_init;
68static vi_get_info_t ofwfb_get_info;
69static vi_query_mode_t ofwfb_query_mode;
70static vi_set_mode_t ofwfb_set_mode;
71static vi_save_font_t ofwfb_save_font;
72static vi_load_font_t ofwfb_load_font;
73static vi_show_font_t ofwfb_show_font;
74static vi_save_palette_t ofwfb_save_palette;
75static vi_load_palette_t ofwfb_load_palette;
76static vi_set_border_t ofwfb_set_border;
77static vi_save_state_t ofwfb_save_state;
78static vi_load_state_t ofwfb_load_state;
79static vi_set_win_org_t ofwfb_set_win_org;
80static vi_read_hw_cursor_t ofwfb_read_hw_cursor;
81static vi_set_hw_cursor_t ofwfb_set_hw_cursor;
82static vi_set_hw_cursor_shape_t ofwfb_set_hw_cursor_shape;
83static vi_blank_display_t ofwfb_blank_display;
84static vi_mmap_t ofwfb_mmap;
85static vi_ioctl_t ofwfb_ioctl;
86static vi_clear_t ofwfb_clear;
87static vi_fill_rect_t ofwfb_fill_rect;
88static vi_bitblt_t ofwfb_bitblt;
89static vi_diag_t ofwfb_diag;
90static vi_save_cursor_palette_t ofwfb_save_cursor_palette;
91static vi_load_cursor_palette_t ofwfb_load_cursor_palette;
92static vi_copy_t ofwfb_copy;
93static vi_putp_t ofwfb_putp;
94static vi_putc_t ofwfb_putc;
95static vi_puts_t ofwfb_puts;
96static vi_putm_t ofwfb_putm;
97
98static video_switch_t ofwfbvidsw = {
99	.probe			= ofwfb_probe,
100	.init			= ofwfb_init,
101	.get_info		= ofwfb_get_info,
102	.query_mode		= ofwfb_query_mode,
103	.set_mode		= ofwfb_set_mode,
104	.save_font		= ofwfb_save_font,
105	.load_font		= ofwfb_load_font,
106	.show_font		= ofwfb_show_font,
107	.save_palette		= ofwfb_save_palette,
108	.load_palette		= ofwfb_load_palette,
109	.set_border		= ofwfb_set_border,
110	.save_state		= ofwfb_save_state,
111	.load_state		= ofwfb_load_state,
112	.set_win_org		= ofwfb_set_win_org,
113	.read_hw_cursor		= ofwfb_read_hw_cursor,
114	.set_hw_cursor		= ofwfb_set_hw_cursor,
115	.set_hw_cursor_shape	= ofwfb_set_hw_cursor_shape,
116	.blank_display		= ofwfb_blank_display,
117	.mmap			= ofwfb_mmap,
118	.ioctl			= ofwfb_ioctl,
119	.clear			= ofwfb_clear,
120	.fill_rect		= ofwfb_fill_rect,
121	.bitblt			= ofwfb_bitblt,
122	.diag			= ofwfb_diag,
123	.save_cursor_palette	= ofwfb_save_cursor_palette,
124	.load_cursor_palette	= ofwfb_load_cursor_palette,
125	.copy			= ofwfb_copy,
126	.putp			= ofwfb_putp,
127	.putc			= ofwfb_putc,
128	.puts			= ofwfb_puts,
129	.putm			= ofwfb_putm,
130};
131
132/*
133 * bitmap depth-specific routines
134 */
135static vi_blank_display_t ofwfb_blank_display8;
136static vi_putc_t ofwfb_putc8;
137static vi_set_border_t ofwfb_set_border8;
138
139static vi_blank_display_t ofwfb_blank_display32;
140static vi_putc_t ofwfb_putc32;
141static vi_set_border_t ofwfb_set_border32;
142
143VIDEO_DRIVER(ofwfb, ofwfbvidsw, ofwfb_configure);
144
145extern sc_rndr_sw_t txtrndrsw;
146RENDERER(ofwfb, 0, txtrndrsw, gfb_set);
147
148RENDERER_MODULE(ofwfb, gfb_set);
149
150/*
151 * Define the iso6429-1983 colormap
152 */
153static struct {
154	uint8_t	red;
155	uint8_t	green;
156	uint8_t	blue;
157} ofwfb_cmap[16] = {		/*  #     R    G    B   Color */
158				/*  -     -    -    -   ----- */
159	{ 0x00, 0x00, 0x00 },	/*  0     0    0    0   Black */
160	{ 0x00, 0x00, 0xaa },	/*  1     0    0  2/3   Blue  */
161	{ 0x00, 0xaa, 0x00 },	/*  2     0  2/3    0   Green */
162	{ 0x00, 0xaa, 0xaa },	/*  3     0  2/3  2/3   Cyan  */
163	{ 0xaa, 0x00, 0x00 },	/*  4   2/3    0    0   Red   */
164	{ 0xaa, 0x00, 0xaa },	/*  5   2/3    0  2/3   Magenta */
165	{ 0xaa, 0x55, 0x00 },	/*  6   2/3  1/3    0   Brown */
166	{ 0xaa, 0xaa, 0xaa },	/*  7   2/3  2/3  2/3   White */
167        { 0x55, 0x55, 0x55 },	/*  8   1/3  1/3  1/3   Gray  */
168	{ 0x55, 0x55, 0xff },	/*  9   1/3  1/3    1   Bright Blue  */
169	{ 0x55, 0xff, 0x55 },	/* 10   1/3    1  1/3   Bright Green */
170	{ 0x55, 0xff, 0xff },	/* 11   1/3    1    1   Bright Cyan  */
171	{ 0xff, 0x55, 0x55 },	/* 12     1  1/3  1/3   Bright Red   */
172	{ 0xff, 0x55, 0xff },	/* 13     1  1/3    1   Bright Magenta */
173	{ 0xff, 0xff, 0x80 },	/* 14     1    1  1/3   Bright Yellow */
174	{ 0xff, 0xff, 0xff }	/* 15     1    1    1   Bright White */
175};
176
177#define	TODO	printf("%s: unimplemented\n", __func__)
178
179static u_int16_t ofwfb_static_window[ROW*COL];
180
181static struct ofwfb_softc ofwfb_softc;
182
183static int
184ofwfb_background(uint8_t attr)
185{
186	return (attr >> 4);
187}
188
189static int
190ofwfb_foreground(uint8_t attr)
191{
192	return (attr & 0x0f);
193}
194
195static u_int
196ofwfb_pix32(int attr)
197{
198	u_int retval;
199
200	retval = (ofwfb_cmap[attr].blue  << 16) |
201		(ofwfb_cmap[attr].green << 8) |
202		ofwfb_cmap[attr].red;
203
204	return (retval);
205}
206
207static int
208ofwfb_configure(int flags)
209{
210	struct ofwfb_softc *sc;
211        phandle_t chosen;
212        ihandle_t stdout;
213	phandle_t node;
214	int depth;
215	int disable;
216	int len;
217	char type[16];
218	static int done = 0;
219
220	disable = 0;
221	TUNABLE_INT_FETCH("hw.syscons.disable", &disable);
222	if (disable != 0)
223		return (0);
224
225	if (done != 0)
226		return (0);
227	done = 1;
228
229	sc = &ofwfb_softc;
230
231	chosen = OF_finddevice("/chosen");
232	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
233        node = OF_instance_to_package(stdout);
234        OF_getprop(node, "device_type", type, sizeof(type));
235        if (strcmp(type, "display") != 0)
236                return (0);
237
238	/* Only support 8 and 32-bit framebuffers */
239	OF_getprop(node, "depth", &depth, sizeof(depth));
240	if (depth == 8) {
241		sc->sc_blank = ofwfb_blank_display8;
242		sc->sc_putc = ofwfb_putc8;
243		sc->sc_set_border = ofwfb_set_border8;
244	} else if (depth == 32) {
245		sc->sc_blank = ofwfb_blank_display32;
246		sc->sc_putc = ofwfb_putc32;
247		sc->sc_set_border = ofwfb_set_border32;
248	} else
249		return (0);
250
251	sc->sc_depth = depth;
252	sc->sc_node = node;
253	sc->sc_console = 1;
254	OF_getprop(node, "height", &sc->sc_height, sizeof(sc->sc_height));
255	OF_getprop(node, "width", &sc->sc_width, sizeof(sc->sc_width));
256	OF_getprop(node, "linebytes", &sc->sc_stride, sizeof(sc->sc_stride));
257
258	/*
259	 * XXX the physical address of the frame buffer is assumed to be
260	 * BAT-mapped so it can be accessed directly
261	 */
262	OF_getprop(node, "address", &sc->sc_addr, sizeof(sc->sc_addr));
263
264	/*
265	 * Get the PCI addresses of the adapter. The node may be the
266	 * child of the PCI device: in that case, try the parent for
267	 * the assigned-addresses property.
268	 */
269	len = OF_getprop(node, "assigned-addresses", sc->sc_pciaddrs,
270	          sizeof(sc->sc_pciaddrs));
271	if (len == -1) {
272		len = OF_getprop(OF_parent(node), "assigned-addresses", sc->sc_pciaddrs,
273		          sizeof(sc->sc_pciaddrs));
274	}
275
276	if (len != -1) {
277		sc->sc_num_pciaddrs = len / sizeof(struct ofw_pci_register);
278	}
279
280	ofwfb_init(0, &sc->sc_va, 0);
281
282	return (0);
283}
284
285static int
286ofwfb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
287{
288	TODO;
289	return (0);
290}
291
292static int
293ofwfb_init(int unit, video_adapter_t *adp, int flags)
294{
295	struct ofwfb_softc *sc;
296	video_info_t *vi;
297	char name[64];
298	ihandle_t ih;
299	int i;
300	int cborder;
301	int font_height;
302	int retval;
303
304	sc = (struct ofwfb_softc *)adp;
305	vi = &adp->va_info;
306
307	vid_init_struct(adp, "ofwfb", -1, unit);
308
309	if (sc->sc_depth == 8) {
310		/*
311		 * Install the ISO6429 colormap - older OFW systems
312		 * don't do this by default
313		 */
314		memset(name, 0, sizeof(name));
315		OF_package_to_path(sc->sc_node, name, sizeof(name));
316		ih = OF_open(name);
317		for (i = 0; i < 16; i++) {
318			OF_call_method("color!", ih, 4, 1,
319				       ofwfb_cmap[i].red,
320				       ofwfb_cmap[i].green,
321				       ofwfb_cmap[i].blue,
322				       i,
323				       &retval);
324		}
325	}
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	/* Enable future font-loading and flag color support */
369	adp->va_flags |= V_ADP_FONT | V_ADP_COLOR;
370
371	ofwfb_blank_display(&sc->sc_va, V_DISPLAY_ON);
372
373	ofwfb_set_mode(&sc->sc_va, 0);
374
375	vid_register(&sc->sc_va);
376
377	return (0);
378}
379
380static int
381ofwfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
382{
383	bcopy(&adp->va_info, info, sizeof(*info));
384	return (0);
385}
386
387static int
388ofwfb_query_mode(video_adapter_t *adp, video_info_t *info)
389{
390	TODO;
391	return (0);
392}
393
394static int
395ofwfb_set_mode(video_adapter_t *adp, int mode)
396{
397
398	return (0);
399}
400
401static int
402ofwfb_save_font(video_adapter_t *adp, int page, int size, u_char *data,
403    int c, int count)
404{
405	TODO;
406	return (0);
407}
408
409static int
410ofwfb_load_font(video_adapter_t *adp, int page, int size, u_char *data,
411    int c, int count)
412{
413	struct ofwfb_softc *sc;
414
415	sc = (struct ofwfb_softc *)adp;
416
417	/*
418	 * syscons code has already determined that current width/height
419	 * are unchanged for this new font
420	 */
421	sc->sc_font = data;
422	return (0);
423}
424
425static int
426ofwfb_show_font(video_adapter_t *adp, int page)
427{
428
429	return (0);
430}
431
432static int
433ofwfb_save_palette(video_adapter_t *adp, u_char *palette)
434{
435	/* TODO; */
436	return (0);
437}
438
439static int
440ofwfb_load_palette(video_adapter_t *adp, u_char *palette)
441{
442	/* TODO; */
443	return (0);
444}
445
446static int
447ofwfb_set_border8(video_adapter_t *adp, int border)
448{
449	struct ofwfb_softc *sc;
450	int i, j;
451	uint8_t *addr;
452	uint8_t bground;
453
454	sc = (struct ofwfb_softc *)adp;
455
456	bground = ofwfb_background(border);
457
458	/* Set top margin */
459	addr = (uint8_t *) sc->sc_addr;
460	for (i = 0; i < sc->sc_ymargin; i++) {
461		for (j = 0; j < sc->sc_width; j++) {
462			*(addr + j) = bground;
463		}
464		addr += sc->sc_stride;
465	}
466
467	/* bottom margin */
468	addr = (uint8_t *) sc->sc_addr + (sc->sc_height - sc->sc_ymargin)*sc->sc_stride;
469	for (i = 0; i < sc->sc_ymargin; i++) {
470		for (j = 0; j < sc->sc_width; j++) {
471			*(addr + j) = bground;
472		}
473		addr += sc->sc_stride;
474	}
475
476	/* remaining left and right borders */
477	addr = (uint8_t *) sc->sc_addr + sc->sc_ymargin*sc->sc_stride;
478	for (i = 0; i < sc->sc_height - 2*sc->sc_xmargin; i++) {
479		for (j = 0; j < sc->sc_xmargin; j++) {
480			*(addr + j) = bground;
481			*(addr + j + sc->sc_width - sc->sc_xmargin) = bground;
482		}
483		addr += sc->sc_stride;
484	}
485
486	return (0);
487}
488
489static int
490ofwfb_set_border32(video_adapter_t *adp, int border)
491{
492	/* XXX Be lazy for now and blank entire screen */
493	return (ofwfb_blank_display32(adp, border));
494}
495
496static int
497ofwfb_set_border(video_adapter_t *adp, int border)
498{
499	struct ofwfb_softc *sc;
500
501	sc = (struct ofwfb_softc *)adp;
502
503	return ((*sc->sc_set_border)(adp, border));
504}
505
506static int
507ofwfb_save_state(video_adapter_t *adp, void *p, size_t size)
508{
509	TODO;
510	return (0);
511}
512
513static int
514ofwfb_load_state(video_adapter_t *adp, void *p)
515{
516	TODO;
517	return (0);
518}
519
520static int
521ofwfb_set_win_org(video_adapter_t *adp, off_t offset)
522{
523	TODO;
524	return (0);
525}
526
527static int
528ofwfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
529{
530	*col = 0;
531	*row = 0;
532
533	return (0);
534}
535
536static int
537ofwfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
538{
539
540	return (0);
541}
542
543static int
544ofwfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
545    int celsize, int blink)
546{
547	return (0);
548}
549
550static int
551ofwfb_blank_display8(video_adapter_t *adp, int mode)
552{
553	struct ofwfb_softc *sc;
554	int i;
555	uint8_t *addr;
556
557	sc = (struct ofwfb_softc *)adp;
558	addr = (uint8_t *) sc->sc_addr;
559
560	/* Could be done a lot faster e.g. 32-bits, or Altivec'd */
561	for (i = 0; i < sc->sc_stride*sc->sc_height; i++)
562		*(addr + i) = ofwfb_background(SC_NORM_ATTR);
563
564	return (0);
565}
566
567static int
568ofwfb_blank_display32(video_adapter_t *adp, int mode)
569{
570	struct ofwfb_softc *sc;
571	int i;
572	uint32_t *addr;
573
574	sc = (struct ofwfb_softc *)adp;
575	addr = (uint32_t *) sc->sc_addr;
576
577	for (i = 0; i < (sc->sc_stride/4)*sc->sc_height; i++)
578		*(addr + i) = ofwfb_pix32(ofwfb_background(SC_NORM_ATTR));
579
580	return (0);
581}
582
583static int
584ofwfb_blank_display(video_adapter_t *adp, int mode)
585{
586	struct ofwfb_softc *sc;
587
588	sc = (struct ofwfb_softc *)adp;
589
590	return ((*sc->sc_blank)(adp, mode));
591}
592
593static int
594ofwfb_mmap(video_adapter_t *adp, vm_offset_t offset, vm_paddr_t *paddr,
595    int prot)
596{
597	struct ofwfb_softc *sc;
598	int i;
599
600	sc = (struct ofwfb_softc *)adp;
601
602	if (sc->sc_num_pciaddrs == 0)
603		return (ENOMEM);
604
605	/*
606	 * Make sure the requested address lies within the PCI device's assigned addrs
607	 */
608	for (i = 0; i < sc->sc_num_pciaddrs; i++)
609		if (offset >= sc->sc_pciaddrs[i].phys_lo &&
610		    offset < (sc->sc_pciaddrs[i].phys_lo + sc->sc_pciaddrs[i].size_lo)) {
611			*paddr = offset;
612			return (0);
613		}
614
615	/*
616	 * This might be a legacy VGA mem request: if so, just point it at the
617	 * framebuffer, since it shouldn't be touched
618	 */
619	if (offset < sc->sc_stride*sc->sc_height) {
620		*paddr = sc->sc_addr + offset;
621		return (0);
622	}
623
624	return (EINVAL);
625}
626
627static int
628ofwfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
629{
630	TODO;
631	return (0);
632}
633
634static int
635ofwfb_clear(video_adapter_t *adp)
636{
637	TODO;
638	return (0);
639}
640
641static int
642ofwfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
643{
644	TODO;
645	return (0);
646}
647
648static int
649ofwfb_bitblt(video_adapter_t *adp, ...)
650{
651	TODO;
652	return (0);
653}
654
655static int
656ofwfb_diag(video_adapter_t *adp, int level)
657{
658	TODO;
659	return (0);
660}
661
662static int
663ofwfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
664{
665	TODO;
666	return (0);
667}
668
669static int
670ofwfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
671{
672	TODO;
673	return (0);
674}
675
676static int
677ofwfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
678{
679	TODO;
680	return (0);
681}
682
683static int
684ofwfb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
685    int size, int bpp, int bit_ltor, int byte_ltor)
686{
687	TODO;
688	return (0);
689}
690
691static int
692ofwfb_putc8(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
693{
694	struct ofwfb_softc *sc;
695	int row;
696	int col;
697	int i, j, k;
698	uint8_t *addr;
699	u_char *p;
700
701	sc = (struct ofwfb_softc *)adp;
702        row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
703        col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
704	p = sc->sc_font + c*sc->sc_font_height;
705	addr = (uint8_t *)sc->sc_addr + (row + sc->sc_ymargin)*sc->sc_stride
706		+ col + sc->sc_xmargin;
707
708	for (i = 0; i < sc->sc_font_height; i++) {
709		for (j = 0, k = 7; j < 8; j++, k--) {
710			if ((p[i] & (1 << k)) == 0)
711				*(addr + j) = ofwfb_background(a);
712			else
713				*(addr + j) = ofwfb_foreground(a);
714		}
715		addr += sc->sc_stride;
716	}
717
718	return (0);
719}
720
721static int
722ofwfb_putc32(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
723{
724	struct ofwfb_softc *sc;
725	int row;
726	int col;
727	int i, j, k;
728	uint32_t *addr;
729	u_char *p;
730
731	sc = (struct ofwfb_softc *)adp;
732        row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
733        col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
734	p = sc->sc_font + c*sc->sc_font_height;
735	addr = (uint32_t *)sc->sc_addr
736		+ (row + sc->sc_ymargin)*(sc->sc_stride/4)
737		+ col + sc->sc_xmargin;
738
739	for (i = 0; i < sc->sc_font_height; i++) {
740		for (j = 0, k = 7; j < 8; j++, k--) {
741			if ((p[i] & (1 << k)) == 0)
742				*(addr + j) = ofwfb_pix32(ofwfb_background(a));
743			else
744				*(addr + j) = ofwfb_pix32(ofwfb_foreground(a));
745		}
746		addr += (sc->sc_stride/4);
747	}
748
749	return (0);
750}
751
752static int
753ofwfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
754{
755	struct ofwfb_softc *sc;
756
757	sc = (struct ofwfb_softc *)adp;
758
759	return ((*sc->sc_putc)(adp, off, c, a));
760}
761
762static int
763ofwfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
764{
765	int i;
766
767	for (i = 0; i < len; i++) {
768		ofwfb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
769	}
770	return (0);
771}
772
773static int
774ofwfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
775    uint32_t pixel_mask, int size)
776{
777	struct ofwfb_softc *sc;
778
779	sc = (struct ofwfb_softc *)adp;
780
781	/* put mouse */
782
783	return (0);
784}
785
786/*
787 * Define the syscons nexus device attachment
788 */
789static void
790ofwfb_scidentify(driver_t *driver, device_t parent)
791{
792	device_t child;
793
794	/*
795	 * Add with a priority guaranteed to make it last on
796	 * the device list
797	 */
798	child = BUS_ADD_CHILD(parent, INT_MAX, SC_DRIVER_NAME, 0);
799	if (child != NULL)
800		nexus_set_device_type(child, "syscons");
801}
802
803static int
804ofwfb_scprobe(device_t dev)
805{
806	char *name;
807
808	name = nexus_get_name(dev);
809	if (strcmp(SC_DRIVER_NAME, name) != 0)
810		return (ENXIO);
811
812	device_set_desc(dev, "System console");
813	return (sc_probe_unit(device_get_unit(dev),
814	    device_get_flags(dev) | SC_AUTODETECT_KBD));
815}
816
817static int
818ofwfb_scattach(device_t dev)
819{
820	return (sc_attach_unit(device_get_unit(dev),
821	    device_get_flags(dev) | SC_AUTODETECT_KBD));
822}
823
824static device_method_t ofwfb_sc_methods[] = {
825  	DEVMETHOD(device_identify,	ofwfb_scidentify),
826	DEVMETHOD(device_probe,		ofwfb_scprobe),
827	DEVMETHOD(device_attach,	ofwfb_scattach),
828	{ 0, 0 }
829};
830
831static driver_t ofwfb_sc_driver = {
832	SC_DRIVER_NAME,
833	ofwfb_sc_methods,
834	sizeof(sc_softc_t),
835};
836
837static devclass_t	sc_devclass;
838
839DRIVER_MODULE(sc, nexus, ofwfb_sc_driver, sc_devclass, 0, 0);
840
841/*
842 * Define a stub keyboard driver in case one hasn't been
843 * compiled into the kernel
844 */
845#include <sys/kbio.h>
846#include <dev/kbd/kbdreg.h>
847
848static int dummy_kbd_configure(int flags);
849
850keyboard_switch_t dummysw;
851
852static int
853dummy_kbd_configure(int flags)
854{
855
856	return (0);
857}
858KEYBOARD_DRIVER(dummy, dummysw, dummy_kbd_configure);
859
860/*
861 * Utility routines from <dev/fb/fbreg.h>
862 */
863void
864ofwfb_bcopy(const void *s, void *d, size_t c)
865{
866	bcopy(s, d, c);
867}
868
869void
870ofwfb_bzero(void *d, size_t c)
871{
872	bzero(d, c);
873}
874
875void
876ofwfb_fillw(int pat, void *base, size_t cnt)
877{
878	u_int16_t *bptr = base;
879
880	while (cnt--)
881		*bptr++ = pat;
882}
883
884u_int16_t
885ofwfb_readw(u_int16_t *addr)
886{
887	return (*addr);
888}
889
890void
891ofwfb_writew(u_int16_t *addr, u_int16_t val)
892{
893	*addr = val;
894}
895