xref: /openbsd-src/sys/arch/luna88k/stand/boot/sio.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: sio.c,v 1.3 2013/10/29 21:49:07 miod Exp $	*/
2 /*	$NetBSD: sio.c,v 1.3 2013/01/21 11:58:12 tsutsui Exp $	*/
3 
4 /*
5  * Copyright (c) 1992 OMRON Corporation.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * OMRON Corporation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)sio.c	8.1 (Berkeley) 6/10/93
39  */
40 /*
41  * Copyright (c) 1992, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  *
44  * This code is derived from software contributed to Berkeley by
45  * OMRON Corporation.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  *	@(#)sio.c	8.1 (Berkeley) 6/10/93
72  */
73 
74 /* sio.c   NOV-25-1991 */
75 
76 #define NSIO 2
77 
78 #include <sys/param.h>
79 #include <machine/board.h>
80 #include <luna88k/stand/boot/samachdep.h>
81 #include <luna88k/stand/boot/sioreg.h>
82 #include <luna88k/stand/boot/rcvbuf.h>
83 #include <luna88k/stand/boot/kbdreg.h>
84 
85 static int siointr(int);
86 static int sioreg(int, int);
87 
88 struct rcvbuf	rcvbuf[NSIO];
89 
90 int	sioconsole = -1;
91 struct	siodevice *sio_addr[2];
92 
93 void
94 _siointr(void)
95 {
96 	int unit;
97 
98 	for (unit = 0; unit < NSIO; unit++)
99 		while (siointr(unit) != 0)
100 			continue;
101 }
102 
103 int
104 siointr(int unit)
105 {
106 /*	struct siodevice *sio = sio_addr[unit]; */
107 	int rr0 = sioreg(REG(unit, RR0), 0);
108 	int rr1 = sioreg(REG(unit, RR1), 0);
109 
110 	if (rr0 & RR0_RXAVAIL) {
111 		if (rr1 & RR1_FRAMING)
112 			return 1;
113 
114 		if (rr1 & (RR1_PARITY | RR1_OVERRUN))
115 		    sioreg(REG(unit, WR0), WR0_ERRRST); /* Channel-A Error Reset */
116 
117 		if (unit == 1) {
118 			int c = kbd_decode(sio_addr[unit]->sio_data);
119 
120 			if ((c & KC_TYPE) == KC_CODE)
121 				PUSH_RBUF(unit, c);
122 		} else {
123 			PUSH_RBUF(unit, sio_addr[unit]->sio_data);
124 		}
125 		return 1;
126 	}
127 
128 	return 0;
129 }
130 
131 /*
132  * Following are all routines needed for SIO to act as console
133  */
134 #include <dev/cons.h>
135 
136 void
137 siocnprobe(struct consdev *cp)
138 {
139 	sio_addr[0] = (struct siodevice *)OBIO_SIO;
140 	sio_addr[1] = (struct siodevice *)OBIO_SIO + 1;
141 
142 	/* make sure hardware exists */
143 	if (badaddr(sio_addr[0], 4) != 0) {
144 		cp->cn_pri = CN_DEAD;
145 		return;
146 	}
147 
148 	/* locate the major number */
149 
150 	/* initialize required fields */
151 	cp->cn_dev = 0;
152 	cp->cn_pri = CN_LOWPRI;
153 }
154 
155 void
156 siocninit(struct consdev *cp)
157 {
158 	int unit = cp->cn_dev;
159 
160 	sioinit();
161 	sioconsole = unit;
162 }
163 
164 int
165 siocngetc(dev_t dev)
166 {
167 	int c, unit = dev;
168 
169 	_siointr();
170 	if (RBUF_EMPTY(unit))
171 		return 0;
172 
173 	POP_RBUF(unit, c);
174 
175 	return(c);
176 }
177 
178 void
179 siocnputc(dev_t dev, int c)
180 {
181 	int unit = dev;
182 
183 	if (sioconsole == -1) {
184 		(void) sioinit();
185 		sioconsole = unit;
186 	}
187 
188 	/* wait for any pending transmission to finish */
189 	while ((sioreg(REG(unit, RR0), 0) & RR0_TXEMPTY) == 0);
190 
191 	sio_addr[unit]->sio_data = (c & 0xFF);
192 
193 	/* wait for any pending transmission to finish */
194 	while ((sioreg(REG(unit, RR0), 0) & RR0_TXEMPTY) == 0);
195 }
196 
197 /* SIO misc routines */
198 
199 void
200 sioinit(void)
201 {
202 	RBUF_INIT(0);
203 	RBUF_INIT(1);
204 
205 	sioreg(REG(0, WR0), WR0_CHANRST);		/* Channel-A Reset */
206 
207 	sioreg(WR2A, WR2_VEC86  | WR2_INTR_1);		/* Set CPU BUS Interface Mode */
208 	sioreg(WR2B, 0);				/* Set Interrupt Vector */
209 
210 	sioreg(REG(0, WR0), WR0_RSTINT);		/* Reset E/S Interrupt */
211 	sioreg(REG(0, WR4), WR4_BAUD96 | WR4_STOP1 | WR4_NPARITY);	/* Tx/Rx */
212 	sioreg(REG(0, WR3), WR3_RX8BIT | WR3_RXENBL);		/* Rx */
213 	sioreg(REG(0, WR5), WR5_TX8BIT | WR5_TXENBL | WR5_DTR | WR5_RTS);		/* Tx */
214 	sioreg(REG(0, WR0), WR0_RSTINT);		/* Reset E/S Interrupt */
215 	sioreg(REG(0, WR1), WR1_RXALLS);		/* Interrupted All Char. */
216 
217 	sioreg(REG(1, WR0), WR0_CHANRST);		/* Channel-A Reset */
218 
219 	sioreg(REG(1, WR0), WR0_RSTINT);		/* Reset E/S Interrupt */
220 	sioreg(REG(1, WR4), WR4_BAUD96 | WR4_STOP1 | WR4_NPARITY);	/* Tx/Rx */
221 	sioreg(REG(1, WR3), WR3_RX8BIT | WR3_RXENBL);		/* Rx */
222 	sioreg(REG(1, WR5), WR5_TX8BIT | WR5_TXENBL);		/* Tx */
223 	sioreg(REG(1, WR0), WR0_RSTINT);		/* Reset E/S Interrupt */
224 	sioreg(REG(1, WR1), WR1_RXALLS);		/* Interrupted All Char. */
225 }
226 
227 int
228 sioreg(int reg, int val)
229 {
230 	int chan;
231 
232 	chan = CHANNEL(reg);
233 
234 	if (isStatusReg(reg)) {
235 		if (REGNO(reg) != 0)
236 		    sio_addr[chan]->sio_cmd = REGNO(reg);
237 		return(sio_addr[chan]->sio_stat);
238 	} else {
239 		if (REGNO(reg) != 0)
240 		    sio_addr[chan]->sio_cmd = REGNO(reg);
241 		sio_addr[chan]->sio_cmd = val;
242 		return(val);
243 	}
244 }
245