xref: /netbsd-src/sys/arch/mvme68k/stand/sboot/console.c (revision bbde328be4e75ea9ad02e9715ea13ca54b797ada)
1 /*	$NetBSD: console.c,v 1.6 2008/01/12 09:54:33 tsutsui Exp $	*/
2 
3 /*
4  *
5  * Copyright (c) 1995 Charles D. Cranor and Seth Widoff
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Charles D. Cranor
19  *	and Seth Widoff.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /*
35  * console i/o
36  */
37 
38 #include "sboot.h"
39 
40 /*
41  * hardware
42  */
43 
44 struct zs_hw {
45 	volatile u_char ctl;
46 	volatile u_char data;
47 };
48 
49 struct zs_hw *zs =  (struct zs_hw *)CONS_ZS_ADDR;
50 
51 /*
52  * consinit: init console
53  */
54 
55 void
56 consinit(void)
57 {
58 	int mark = time();
59 	int rr1;
60 	for (;;) {
61 		if (time() > mark + 5) break;
62 		zs->ctl = 1; rr1 = zs->ctl;
63 		zs->ctl = 0;
64 		if ((rr1 & 0x1) == 1 && (zs->ctl & 0x4) == 4)
65 			break; /* zs_drain! */
66 	}
67 	zs->ctl =  9;
68 	zs->ctl = 0x00; /* clear interrupt */
69 	zs->ctl =  4;
70 	zs->ctl = 0x44; /* 16x clk, 1 stop bit */
71 	zs->ctl =  5;
72 	zs->ctl = 0xea; /* DTR on, 8 bit xmit, xmit on, RTS on */
73 	zs->ctl =  3;
74 	zs->ctl = 0xc1; /* 8 bit recv, auto cd_cts, recv on */
75 	zs->ctl =  1;
76 	zs->ctl = 0x00; /* no intrs */
77 	zs->ctl =  2;
78 	zs->ctl = 0x00; /* no vector */
79 	zs->ctl = 10;
80 	zs->ctl = 0x00; /* */
81 	zs->ctl = 11;
82 	zs->ctl = 0x50; /* clocking options */
83 	zs->ctl = 12;
84 	zs->ctl = 0x0e; /* 9600 baud, part 1 */
85 	zs->ctl = 13;
86 	zs->ctl = 0x00; /* 9600 baud, part 2 */
87 	zs->ctl = 14;
88 	zs->ctl = 0x03; /* more clocking options */
89 	zs->ctl = 15;
90 	zs->ctl = 0x00; /* clear intrs */
91 }
92 
93 /*
94  * putchar: put char to console
95  */
96 
97 void putchar(int c)
98 {
99 	if (c == '\n')
100 		putchar('\r');	/* avoid the need for \r\n in printf */
101 	zs->ctl = 0;
102 	while ((zs->ctl & 0x04) == 0) {
103 		zs->ctl = 0;
104 	}
105 	zs->ctl = 8;
106 	zs->ctl = (char)c;
107 }
108 
109 /*
110  * cngetc: get 1 char from console
111  */
112 
113 char cngetc(void)
114 {
115 
116 	zs->ctl = 0;
117 	while ((zs->ctl & 0x1) == 0) {
118 		zs->ctl = 0;
119 	}
120 	zs->ctl = 8;
121 	return zs->ctl;
122 }
123 
124 /*
125  * puts: put string to console
126  */
127 
128 void
129 puts(char *str)
130 {
131 
132 	while (*str != '\0') {
133 		putchar(*str);
134 		str++;
135 	}
136 }
137 
138 /*
139  * ngets: get string from console
140  */
141 
142 char *
143 ngets(char *str, int size)
144 {
145 
146 	int i = 0;
147 	while ((i < size - 1) && (str[i] = cngetc()) != '\r') {
148 		if (str[i] == '\b' || str[i] == 0x7F) {
149 			if (i == 0)
150 				continue;
151 			i--;
152 			puts("\b \b");
153 			continue;
154 		}
155 		putchar(str[i]);
156 		i++;
157 	}
158 	puts("\n");
159 	str[i] = '\0';
160 	return &str[i];
161 }
162