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