1/*	$NetBSD: cons.c,v 1.9 2008/04/29 15:24:50 tsutsui 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
43#include <machine/cpu.h>
44
45#include "boot.h"
46#include "cons.h"
47
48#ifdef CONS_SERIAL
49void comcnprobe(struct consdev *);
50void comcninit(struct consdev *);
51void comcnputchar(void *, int);
52int comcngetchar(void *);
53int comcnscan(void *);
54# include "ns16550.h"
55# ifndef COMPORT
56#  define COMPORT COM1
57# endif
58# ifndef COMSPEED
59#  define COMSPEED 9600
60# endif
61#endif
62
63#ifdef CONS_ZS
64void zscnprobe(struct consdev *);
65void zscninit(struct consdev *);
66void zscnputchar(void *, int);
67int zscngetchar(void *);
68int zscnscan(void *);
69#include "zs.h"
70#ifndef ZSCHAN
71#define ZSCHAN ZS_CHAN_A
72#endif
73#ifndef ZSSPEED
74#define ZSSPEED 115200
75#endif
76#endif
77
78struct consdev constab[] = {
79#ifdef CONS_SERIAL
80	{ "com", COMPORT, COMSPEED,
81	    comcnprobe, comcninit, comcngetchar, comcnputchar, comcnscan },
82#endif
83#ifdef CONS_ZS
84	{ "zs", ZSCHAN, ZSSPEED,
85	    zscnprobe, zscninit, zscngetchar, zscnputchar, zscnscan },
86#endif
87	{ 0 }
88};
89
90struct consdev *cn_tab;
91
92char *
93cninit(int *addr, int *speed)
94{
95	register struct consdev *cp;
96
97	cn_tab = NULL;
98	for (cp = constab; cp->cn_probe; cp++) {
99		(*cp->cn_probe)(cp);
100		if (cp->cn_pri > CN_DEAD &&
101		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
102			cn_tab = cp;
103	}
104	if (cn_tab) {
105		(*cn_tab->cn_init)(cn_tab);
106		*addr = cn_tab->address;
107		*speed = cn_tab->speed;
108		return cn_tab->cn_name;
109	}
110
111	return NULL;
112}
113
114int
115cngetc(void)
116{
117
118	if (cn_tab)
119		return (*cn_tab->cn_getc)(cn_tab->cn_dev);
120	return 0;
121}
122
123void
124cnputc(int c)
125{
126
127	if (cn_tab)
128		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
129}
130
131int
132cnscan(void)
133{
134
135	if (cn_tab)
136		return (*cn_tab->cn_scan)(cn_tab->cn_dev);
137	return -1;
138}
139
140#ifdef CONS_SERIAL
141/*
142 * serial console
143 */
144void
145comcnprobe(struct consdev *cp)
146{
147
148	if (*((uint32_t *)COMPROBE) != 0 &&
149	    cobalt_id != COBALT_ID_QUBE2700)
150		cp->cn_pri = CN_REMOTE;
151}
152
153void
154comcninit(struct consdev *cp)
155{
156
157	cp->cn_dev = com_init(cp->address, cp->speed);
158}
159
160int
161comcngetchar(void *dev)
162{
163
164	return com_getc(dev);
165}
166
167void
168comcnputchar(void *dev, int c)
169{
170
171	if (c == '\n')
172		com_putc(dev, '\r');
173	com_putc(dev, c);
174}
175
176int
177comcnscan(void *dev)
178{
179
180	return com_scankbd(dev);
181}
182#endif /* CONS_SERIAL */
183
184#ifdef CONS_ZS
185/*
186 * optional z85c30 serial console on Qube2700
187 */
188void
189zscnprobe(struct consdev *cp)
190{
191
192	if (*((uint32_t *)ZSPROBE) != 0 &&
193	    cobalt_id == COBALT_ID_QUBE2700)
194		cp->cn_pri = CN_REMOTE;
195}
196
197void
198zscninit(struct consdev *cp)
199{
200
201	cp->cn_dev = zs_init(cp->address, cp->speed);
202}
203
204int
205zscngetchar(void *dev)
206{
207
208	return zs_getc(dev);
209}
210
211void
212zscnputchar(void *dev, int c)
213{
214
215	if (c == '\n')
216		zs_putc(dev, '\r');
217	zs_putc(dev, c);
218}
219
220int
221zscnscan(void *dev)
222{
223
224	return zs_scan(dev);
225}
226#endif	/* CONS_ZS */
227