1/*	$NetBSD: cons.c,v 1.11 2009/03/18 10:22:27 cegger Exp $	*/
2
3/*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1990, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: Utah Hdr: cons.c 1.7 92/02/28
37 *
38 *	@(#)cons.c	8.1 (Berkeley) 6/10/93
39 */
40
41#include <lib/libsa/stand.h>
42#include <sys/param.h>
43#include "boot.h"
44#include "cons.h"
45
46#ifdef CONS_BE
47void becnprobe(struct consdev *);
48void becninit(struct consdev *);
49void becnputchar(void *, register int);
50int becngetchar(void *);
51int becnscan(void *);
52#endif
53
54#ifdef CONS_VGA
55void vgacnprobe(struct consdev *);
56void vgacninit(struct consdev *);
57void vgacnputchar(void *, register int);
58int vgacngetchar(void *);
59int vgacnscan(void *);
60#endif
61
62#ifdef CONS_SERIAL
63void siocnprobe(struct consdev *);
64void siocninit(struct consdev *);
65void siocnputchar(void *, register int);
66int siocngetchar(void *);
67int siocnscan(void *);
68# include "ns16550.h"
69# ifndef COMPORT
70#  define COMPORT COM1
71# endif
72# ifndef COMSPEED
73#  define COMSPEED 9600
74# endif
75#endif
76
77struct consdev constab[] = {
78#ifdef CONS_BE
79	{ "be", 0xd0000000, 0,
80	    becnprobe, becninit, becngetchar, becnputchar, becnscan },
81#endif
82#ifdef CONS_VGA
83	{ "vga", 0xc0000000, 0,
84	    vgacnprobe, vgacninit, vgacngetchar, vgacnputchar, vgacnscan },
85#endif
86#ifdef CONS_SERIAL
87	{ "com", COMPORT, COMSPEED,
88	    siocnprobe, siocninit, siocngetchar, siocnputchar, siocnscan },
89#endif
90	{ 0 }
91};
92
93struct consdev *cn_tab;
94
95char *
96cninit(int *addr, int *speed)
97{
98	register struct consdev *cp;
99
100	cn_tab = NULL;
101	for (cp = constab; cp->cn_probe; cp++) {
102		(*cp->cn_probe)(cp);
103		if (cp->cn_pri > CN_DEAD &&
104		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
105			cn_tab = cp;
106	}
107	if (cn_tab) {
108		(*cn_tab->cn_init)(cn_tab);
109		*addr = cn_tab->address;
110		*speed = cn_tab->speed;
111		return (cn_tab->cn_name);
112	}
113
114	return (NULL);
115}
116
117int
118cngetc(void)
119{
120
121	if (cn_tab)
122		return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
123	return (0);
124}
125
126void
127cnputc(int c)
128{
129
130	if (cn_tab)
131		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
132}
133
134int
135cnscan(void)
136{
137
138	if (cn_tab)
139		return ((*cn_tab->cn_scan)(cn_tab->cn_dev));
140	return -1;
141}
142
143#ifdef CONS_BE
144/*
145 * BeBox default console
146 */
147void
148becnprobe(struct consdev *cp)
149{
150
151	cp->cn_pri = CN_INTERNAL;
152}
153
154void
155becninit(struct consdev *cp)
156{
157
158	video_init((u_char *)cp->address);
159	kbdreset();
160	kbd(1);		/* read out ugly data */
161}
162
163int
164becngetchar(void *dev)
165{
166
167	return (kbd_getc());
168}
169
170void
171becnputchar(void *dev, register int c)
172{
173
174	video_putc(c);
175}
176
177int
178becnscan(void *dev)
179{
180
181	return (kbd(1));
182}
183#endif /* CONS_BE */
184
185#ifdef CONS_VGA
186/*
187 * VGA console
188 */
189void
190vgacnprobe(struct consdev *cp)
191{
192
193	cp->cn_pri = CN_NORMAL;
194}
195
196void
197vgacninit(struct consdev *cp)
198{
199
200	vga_reset((u_char *)cp->address);
201	vga_init((u_char *)cp->address);
202	kbdreset();
203}
204
205int
206vgacngetchar(void *dev)
207{
208
209	return (kbd_getc());
210}
211
212void
213vgacnputchar(void *dev, register int c)
214{
215
216	vga_putc(c);
217}
218
219int
220vgacnscan(void *dev)
221{
222
223	return (kbd(1));
224}
225#endif /* CONS_VGA */
226
227#ifdef CONS_SERIAL
228/*
229 * serial console
230 */
231void
232siocnprobe(struct consdev *cp)
233{
234
235	cp->cn_pri = CN_REMOTE;
236}
237
238void
239siocninit(struct consdev *cp)
240{
241
242	cp->cn_dev = (void *)NS16550_init(cp->address, cp->speed);
243}
244
245int
246siocngetchar(void *dev)
247{
248
249	return (NS16550_getc((struct NS16550 *)dev));
250}
251
252void
253siocnputchar(void *dev, register int c)
254{
255
256	if (c == '\n')
257		NS16550_putc((struct NS16550 *)dev, '\r');
258	NS16550_putc((struct NS16550 *)dev, c);
259}
260
261int
262siocnscan(void *dev)
263{
264
265	return (NS16550_scankbd((struct NS16550 *)dev));
266}
267#endif /* CONS_SERIAL */
268