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