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