1/*	$NetBSD: console.c,v 1.3 2007/02/22 05:31:53 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/param.h>
33#include <sys/systm.h>
34
35#include <lib/libsa/stand.h>
36
37#include "local.h"
38
39void __putchar(int);
40
41#define	PUTCHAR		__putchar	/* "boot" hooks log staff. */
42
43#include <machine/sbd.h>
44
45#include "console.h"
46
47struct cons cons;
48void nullcursor(int, int);
49void nullscroll(void);
50
51
52void
53console_init(void)
54{
55	uint8_t nvsram_cons;
56
57	cons.cursor = nullcursor;
58	cons.scroll = nullscroll;
59	cons.cursor_enable = false;
60	cons.erace_previous_cursor = false;
61
62	switch (SBD_INFO->machine) {
63	case MACHINE_TR2:
64		/* untested */
65		nvsram_cons = *(volatile uint8_t *)0xbb023010;
66
67		switch (nvsram_cons) {
68		case 0x00:
69			cons.type = CONS_FB_KSEG2;
70			fb_set_addr(0xf0000000, 0x01000000, 0xbfc0ec00);
71			zskbd_set_addr(0xbb010000, 0xbb010004);
72			cons.init();
73			break;
74		case 0x01:
75			/* sio 1 (zs channel A) */
76			cons.type = CONS_SIO1;
77			zs_set_addr(0xbb011008, 0xbb01100c, 4915200);
78			cons.init();
79			break;
80		case 0x02:
81			/* sio 2 (zs channel B) */
82			cons.type = CONS_SIO2;
83			zs_set_addr(0xbb011000, 0xbb011004, 4915200);
84			cons.init();
85			break;
86		default:
87			goto rom_cons;
88		}
89		break;
90	case MACHINE_TR2A:
91		nvsram_cons = *(volatile uint8_t *)0xbe4932a0;
92
93		switch (nvsram_cons) {
94		case 0x80:
95			/* on-board FB on 360AD, 360ADII */
96			cons.type = CONS_FB_KSEG2;
97			fb_set_addr(0xf0000000, 0x01000000, 0xbfc0ec00);
98			zskbd_set_addr(0xbe480000, 0xbe480004);
99			cons.init();
100			break;
101		case 0x01:
102			/* sio 1 (zs channel A) */
103			cons.type = CONS_SIO1;
104			zs_set_addr(0xbe440008, 0xbe44000c, 4915200);
105			cons.init();
106			break;
107		case 0x02:
108			/* sio 2 (zs channel B) */
109			cons.type = CONS_SIO2;
110			zs_set_addr(0xbe440000, 0xbe440004, 4915200);
111			cons.init();
112			break;
113		default:
114			goto rom_cons;
115		}
116		break;
117	default:
118 rom_cons:
119		cons.getc = ROM_GETC;
120		cons.scan = rom_scan;
121		cons.putc = ROM_PUTC;
122		cons.init = cons_rom_init;
123		cons.scroll = cons_rom_scroll;
124		cons.type = CONS_ROM;
125		cons.init();
126
127		break;
128	}
129}
130
131void
132console_cursor(bool on)
133{
134
135	cons.cursor_enable = on;
136}
137
138enum console_type
139console_type(void)
140{
141
142	return cons.type;
143}
144
145void
146PUTCHAR(int c)
147{
148	int i;
149
150	if (cons.type == CONS_SIO1 || cons.type == CONS_SIO2) {
151		if (c == '\n')
152			cons.putc(0, 0, '\r');
153		cons.putc(0, 0, c);
154		return;
155	}
156
157	if (cons.cursor_enable && cons.erace_previous_cursor)
158		cons.cursor(cons.x * ROM_FONT_WIDTH, cons.y * ROM_FONT_HEIGHT);
159
160	switch (c) {
161	default:
162		cons.putc(cons.x * ROM_FONT_WIDTH, cons.y * ROM_FONT_HEIGHT, c);
163		if (++cons.x == CONS_WIDTH) {
164			cons.x = X_INIT;
165			cons.y++;
166		}
167		break;
168	case '\b':
169		cons.putc(cons.x * ROM_FONT_WIDTH, cons.y * ROM_FONT_HEIGHT, c);
170		cons.x = cons.x == X_INIT ? X_INIT : cons.x - 1;
171		if (cons.cursor_enable)
172			cons.putc(cons.x * ROM_FONT_WIDTH,
173			    cons.y * ROM_FONT_HEIGHT, ' ');
174		cons.putc(cons.x * ROM_FONT_WIDTH, cons.y * ROM_FONT_HEIGHT, c);
175		break;
176	case '\t':
177		for (i = cons.x % 8; i < 8; i++) {
178			cons.putc(cons.x * ROM_FONT_WIDTH,
179			    cons.y * ROM_FONT_HEIGHT, ' ');
180			if (++cons.x == CONS_WIDTH) {
181				cons.x = X_INIT;
182				if (++cons.y == CONS_HEIGHT)
183					cons.scroll();
184			}
185		}
186		break;
187	case '\r':
188		cons.putc(cons.x * ROM_FONT_WIDTH, cons.y * ROM_FONT_HEIGHT, c);
189		cons.x = X_INIT;
190		break;
191	case '\n':
192		cons.putc(cons.x * ROM_FONT_WIDTH, cons.y * ROM_FONT_HEIGHT,
193		    '\r');
194		cons.putc(cons.x * ROM_FONT_WIDTH, cons.y * ROM_FONT_HEIGHT, c);
195		cons.x = X_INIT;
196		cons.y++;
197		break;
198	case 2:	/* Ctrl-b */
199		if (--cons.x < X_INIT)
200			cons.x = X_INIT;
201		break;
202	case 6:	/* Ctrl-f */
203		if (++cons.x >= CONS_WIDTH)
204			cons.x = CONS_WIDTH;
205		break;
206	case 11:/* Ctrl-k */
207		for (i = cons.x; i < CONS_WIDTH; i++)
208			cons.putc(i * ROM_FONT_WIDTH, cons.y * ROM_FONT_HEIGHT,
209			     ' ');
210		break;
211	}
212
213	if (cons.y == CONS_HEIGHT)
214		cons.scroll();
215
216	if (cons.cursor_enable) {
217		cons.cursor(cons.x * ROM_FONT_WIDTH, cons.y * ROM_FONT_HEIGHT);
218		cons.erace_previous_cursor = true;
219	} else {
220		cons.erace_previous_cursor = false;
221	}
222}
223
224int
225getchar(void)
226{
227
228	return cons.getc();
229}
230
231int
232cnscan(void)
233{
234
235	return cons.scan();
236}
237
238void
239nullcursor(int x, int y)
240{
241	/* place holder */
242}
243
244void
245nullscroll(void)
246{
247	/* place holder */
248}
249