xref: /netbsd-src/sys/arch/luna68k/stand/boot/sio.c (revision 3dd15d69a9fe59a5a1616670bdd96da1f4872008)
1*3dd15d69Stsutsui /*	$NetBSD: sio.c,v 1.5 2019/06/30 05:04:49 tsutsui Exp $	*/
2df6945b8Stsutsui 
3df6945b8Stsutsui /*
4df6945b8Stsutsui  * Copyright (c) 1992 OMRON Corporation.
5df6945b8Stsutsui  *
6df6945b8Stsutsui  * This code is derived from software contributed to Berkeley by
7df6945b8Stsutsui  * OMRON Corporation.
8df6945b8Stsutsui  *
9df6945b8Stsutsui  * Redistribution and use in source and binary forms, with or without
10df6945b8Stsutsui  * modification, are permitted provided that the following conditions
11df6945b8Stsutsui  * are met:
12df6945b8Stsutsui  * 1. Redistributions of source code must retain the above copyright
13df6945b8Stsutsui  *    notice, this list of conditions and the following disclaimer.
14df6945b8Stsutsui  * 2. Redistributions in binary form must reproduce the above copyright
15df6945b8Stsutsui  *    notice, this list of conditions and the following disclaimer in the
16df6945b8Stsutsui  *    documentation and/or other materials provided with the distribution.
17df6945b8Stsutsui  * 3. All advertising materials mentioning features or use of this software
18df6945b8Stsutsui  *    must display the following acknowledgement:
19df6945b8Stsutsui  *	This product includes software developed by the University of
20df6945b8Stsutsui  *	California, Berkeley and its contributors.
21df6945b8Stsutsui  * 4. Neither the name of the University nor the names of its contributors
22df6945b8Stsutsui  *    may be used to endorse or promote products derived from this software
23df6945b8Stsutsui  *    without specific prior written permission.
24df6945b8Stsutsui  *
25df6945b8Stsutsui  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df6945b8Stsutsui  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df6945b8Stsutsui  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df6945b8Stsutsui  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df6945b8Stsutsui  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df6945b8Stsutsui  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df6945b8Stsutsui  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df6945b8Stsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df6945b8Stsutsui  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df6945b8Stsutsui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df6945b8Stsutsui  * SUCH DAMAGE.
36df6945b8Stsutsui  *
37df6945b8Stsutsui  *	@(#)sio.c	8.1 (Berkeley) 6/10/93
38df6945b8Stsutsui  */
39df6945b8Stsutsui /*
40df6945b8Stsutsui  * Copyright (c) 1992, 1993
41df6945b8Stsutsui  *	The Regents of the University of California.  All rights reserved.
42df6945b8Stsutsui  *
43df6945b8Stsutsui  * This code is derived from software contributed to Berkeley by
44df6945b8Stsutsui  * OMRON Corporation.
45df6945b8Stsutsui  *
46df6945b8Stsutsui  * Redistribution and use in source and binary forms, with or without
47df6945b8Stsutsui  * modification, are permitted provided that the following conditions
48df6945b8Stsutsui  * are met:
49df6945b8Stsutsui  * 1. Redistributions of source code must retain the above copyright
50df6945b8Stsutsui  *    notice, this list of conditions and the following disclaimer.
51df6945b8Stsutsui  * 2. Redistributions in binary form must reproduce the above copyright
52df6945b8Stsutsui  *    notice, this list of conditions and the following disclaimer in the
53df6945b8Stsutsui  *    documentation and/or other materials provided with the distribution.
54df6945b8Stsutsui  * 3. Neither the name of the University nor the names of its contributors
55df6945b8Stsutsui  *    may be used to endorse or promote products derived from this software
56df6945b8Stsutsui  *    without specific prior written permission.
57df6945b8Stsutsui  *
58df6945b8Stsutsui  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59df6945b8Stsutsui  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60df6945b8Stsutsui  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61df6945b8Stsutsui  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62df6945b8Stsutsui  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63df6945b8Stsutsui  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64df6945b8Stsutsui  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65df6945b8Stsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66df6945b8Stsutsui  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67df6945b8Stsutsui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68df6945b8Stsutsui  * SUCH DAMAGE.
69df6945b8Stsutsui  *
70df6945b8Stsutsui  *	@(#)sio.c	8.1 (Berkeley) 6/10/93
71df6945b8Stsutsui  */
72df6945b8Stsutsui 
73df6945b8Stsutsui /* sio.c   NOV-25-1991 */
74df6945b8Stsutsui 
75df6945b8Stsutsui #define NSIO 2
76df6945b8Stsutsui 
77df6945b8Stsutsui #include <sys/param.h>
78*3dd15d69Stsutsui #include <machine/board.h>
79df6945b8Stsutsui #include <luna68k/stand/boot/samachdep.h>
80df6945b8Stsutsui #include <luna68k/stand/boot/sioreg.h>
81df6945b8Stsutsui #include <luna68k/stand/boot/rcvbuf.h>
82df6945b8Stsutsui #include <luna68k/stand/boot/kbdreg.h>
83df6945b8Stsutsui 
84df6945b8Stsutsui static void siointr(int);
85df6945b8Stsutsui static int sioreg(int, int);
86df6945b8Stsutsui 
87df6945b8Stsutsui struct rcvbuf	rcvbuf[NSIO];
88df6945b8Stsutsui 
89df6945b8Stsutsui int	sioconsole = -1;
90df6945b8Stsutsui struct	siodevice *sio_addr[2];
91df6945b8Stsutsui 
92df6945b8Stsutsui void
_siointr(void)93df6945b8Stsutsui _siointr(void)
94df6945b8Stsutsui {
95df6945b8Stsutsui 	int unit;
96df6945b8Stsutsui 
97df6945b8Stsutsui 	for (unit = 0; unit < NSIO; unit++)
98df6945b8Stsutsui 		siointr(unit);
99df6945b8Stsutsui }
100df6945b8Stsutsui 
101df6945b8Stsutsui void
siointr(int unit)102df6945b8Stsutsui siointr(int unit)
103df6945b8Stsutsui {
104681c174aStsutsui #if 0
105681c174aStsutsui 	struct siodevice *sio = sio_addr[unit];
106681c174aStsutsui #endif
107df6945b8Stsutsui 	int rr0 = sioreg(REG(unit, RR0), 0);
108df6945b8Stsutsui 	int rr1 = sioreg(REG(unit, RR1), 0);
109df6945b8Stsutsui 
110df6945b8Stsutsui 	if (rr0 & RR0_RXAVAIL) {
111df6945b8Stsutsui 		if (rr1 & RR1_FRAMING)
112df6945b8Stsutsui 			return;
113df6945b8Stsutsui 
114681c174aStsutsui 		if (rr1 & (RR1_PARITY | RR1_OVERRUN)) {
115681c174aStsutsui 			/* Channel-A Error Reset */
116681c174aStsutsui 			sioreg(REG(unit, WR0), WR0_ERRRST);
117681c174aStsutsui 		}
118df6945b8Stsutsui 
119df6945b8Stsutsui 		if (unit == 1) {
120df6945b8Stsutsui 			int c = kbd_decode(sio_addr[unit]->sio_data);
121df6945b8Stsutsui 
122df6945b8Stsutsui 			if ((c & KC_TYPE) == KC_CODE)
123df6945b8Stsutsui 				PUSH_RBUF(unit, c);
124df6945b8Stsutsui 		} else {
125df6945b8Stsutsui 			PUSH_RBUF(unit, sio_addr[unit]->sio_data);
126df6945b8Stsutsui 		}
127df6945b8Stsutsui 	}
128df6945b8Stsutsui }
129df6945b8Stsutsui 
130df6945b8Stsutsui /*
131df6945b8Stsutsui  * Following are all routines needed for SIO to act as console
132df6945b8Stsutsui  */
133df6945b8Stsutsui #include <dev/cons.h>
134df6945b8Stsutsui #include <luna68k/stand/boot/romvec.h>
135df6945b8Stsutsui 
136df6945b8Stsutsui void
siocnprobe(struct consdev * cp)137df6945b8Stsutsui siocnprobe(struct consdev *cp)
138df6945b8Stsutsui {
139*3dd15d69Stsutsui 	sio_addr[0] = (struct siodevice *)(OBIO_SIO + 0);
140*3dd15d69Stsutsui 	sio_addr[1] = (struct siodevice *)(OBIO_SIO + 4);
141df6945b8Stsutsui 
142df6945b8Stsutsui 	/* make sure hardware exists */
143df6945b8Stsutsui 	if (badaddr((short *)sio_addr[0])) {
144df6945b8Stsutsui 		cp->cn_pri = CN_DEAD;
145df6945b8Stsutsui 		return;
146df6945b8Stsutsui 	}
147df6945b8Stsutsui 
148df6945b8Stsutsui 	/* locate the major number */
149df6945b8Stsutsui 
150df6945b8Stsutsui 	/* initialize required fields */
151681c174aStsutsui 	cp->cn_dev = 0;
152df6945b8Stsutsui 	cp->cn_pri = CN_NORMAL;
153df6945b8Stsutsui }
154df6945b8Stsutsui 
155df6945b8Stsutsui void
siocninit(struct consdev * cp)156df6945b8Stsutsui siocninit(struct consdev *cp)
157df6945b8Stsutsui {
158681c174aStsutsui 	int unit = cp->cn_dev;
159df6945b8Stsutsui 
160df6945b8Stsutsui 	sioinit();
161df6945b8Stsutsui 	sioconsole = unit;
162df6945b8Stsutsui }
163df6945b8Stsutsui 
164df6945b8Stsutsui int
siocngetc(dev_t dev)165df6945b8Stsutsui siocngetc(dev_t dev)
166df6945b8Stsutsui {
167681c174aStsutsui 	int c, unit = dev;
168df6945b8Stsutsui 
169bd88a8a6Stsutsui 	if (RBUF_EMPTY(unit))
170bd88a8a6Stsutsui 		return 0;
171df6945b8Stsutsui 
172df6945b8Stsutsui 	POP_RBUF(unit, c);
173df6945b8Stsutsui 
174681c174aStsutsui 	return c;
175df6945b8Stsutsui }
176df6945b8Stsutsui 
177df6945b8Stsutsui void
siocnputc(dev_t dev,int c)178df6945b8Stsutsui siocnputc(dev_t dev, int c)
179df6945b8Stsutsui {
180681c174aStsutsui 	int unit = dev;
181df6945b8Stsutsui 	int s;
182df6945b8Stsutsui 
183df6945b8Stsutsui 	if (sioconsole == -1) {
184df6945b8Stsutsui 		(void)sioinit();
185df6945b8Stsutsui 		sioconsole = unit;
186df6945b8Stsutsui 	}
187df6945b8Stsutsui 
188df6945b8Stsutsui 	s = splsio();
189df6945b8Stsutsui 
190df6945b8Stsutsui 	/* wait for any pending transmission to finish */
191681c174aStsutsui 	while ((sioreg(REG(unit, RR0), 0) & RR0_TXEMPTY) == 0)
192681c174aStsutsui 		continue;
193df6945b8Stsutsui 
194df6945b8Stsutsui 	sio_addr[unit]->sio_data = (c & 0xFF);
195df6945b8Stsutsui 
196df6945b8Stsutsui 	/* wait for any pending transmission to finish */
197681c174aStsutsui 	while ((sioreg(REG(unit, RR0), 0) & RR0_TXEMPTY) == 0)
198681c174aStsutsui 		continue;
199df6945b8Stsutsui 
200df6945b8Stsutsui 	splx(s);
201df6945b8Stsutsui }
202df6945b8Stsutsui 
203df6945b8Stsutsui /* SIO misc routines */
204df6945b8Stsutsui 
205df6945b8Stsutsui void
sioinit(void)206df6945b8Stsutsui sioinit(void)
207df6945b8Stsutsui {
208df6945b8Stsutsui 	int s;
209df6945b8Stsutsui 
210df6945b8Stsutsui 	RBUF_INIT(0);
211df6945b8Stsutsui 	RBUF_INIT(1);
212df6945b8Stsutsui 
213df6945b8Stsutsui 	s = splsio();
214df6945b8Stsutsui 
215681c174aStsutsui 	/* Channel-A Reset */
216681c174aStsutsui 	sioreg(REG(0, WR0), WR0_CHANRST);
217df6945b8Stsutsui 
218681c174aStsutsui 	/* Set CPU BUS Interface Mode */
219681c174aStsutsui 	sioreg(WR2A, WR2_VEC86  | WR2_INTR_1);
220681c174aStsutsui 	/* Set Interrupt Vector */
221681c174aStsutsui 	sioreg(WR2B, 0);
222df6945b8Stsutsui 
223681c174aStsutsui 	/* Reset E/S Interrupt */
224681c174aStsutsui 	sioreg(REG(0, WR0), WR0_RSTINT);
225681c174aStsutsui 	/* Tx/Rx */
226681c174aStsutsui 	sioreg(REG(0, WR4), WR4_BAUD96 | WR4_STOP1 | WR4_NPARITY);
227681c174aStsutsui 	/* Rx */
228681c174aStsutsui 	sioreg(REG(0, WR3), WR3_RX8BIT | WR3_RXENBL);
229681c174aStsutsui 	/* Tx */
230681c174aStsutsui 	sioreg(REG(0, WR5), WR5_TX8BIT | WR5_TXENBL | WR5_DTR | WR5_RTS);
231681c174aStsutsui 	/* Reset E/S Interrupt */
232681c174aStsutsui 	sioreg(REG(0, WR0), WR0_RSTINT);
233681c174aStsutsui 	/* Interrupted All Char. */
234681c174aStsutsui 	sioreg(REG(0, WR1), WR1_RXALLS);
235df6945b8Stsutsui 
236681c174aStsutsui 	/* Channel-A Reset */
237681c174aStsutsui 	sioreg(REG(1, WR0), WR0_CHANRST);
238df6945b8Stsutsui 
239681c174aStsutsui 	/* Reset E/S Interrupt */
240681c174aStsutsui 	sioreg(REG(1, WR0), WR0_RSTINT);
241681c174aStsutsui 	/* Tx/Rx */
242681c174aStsutsui 	sioreg(REG(1, WR4), WR4_BAUD96 | WR4_STOP1 | WR4_NPARITY);
243681c174aStsutsui 	/* Rx */
244681c174aStsutsui 	sioreg(REG(1, WR3), WR3_RX8BIT | WR3_RXENBL);
245681c174aStsutsui 	/* Tx */
246681c174aStsutsui 	sioreg(REG(1, WR5), WR5_TX8BIT | WR5_TXENBL);
247681c174aStsutsui 	/* Reset E/S Interrupt */
248681c174aStsutsui 	sioreg(REG(1, WR0), WR0_RSTINT);
249681c174aStsutsui 	/* Interrupted All Char. */
250681c174aStsutsui 	sioreg(REG(1, WR1), WR1_RXALLS);
251df6945b8Stsutsui 
252df6945b8Stsutsui 	splx(s);
253df6945b8Stsutsui }
254df6945b8Stsutsui 
255df6945b8Stsutsui int
sioreg(int reg,int val)256df6945b8Stsutsui sioreg(int reg, int val)
257df6945b8Stsutsui {
258df6945b8Stsutsui 	int chan;
259df6945b8Stsutsui 
260df6945b8Stsutsui 	chan = CHANNEL(reg);
261df6945b8Stsutsui 
262df6945b8Stsutsui 	if (isStatusReg(reg)) {
263df6945b8Stsutsui 		if (REGNO(reg) != 0)
264df6945b8Stsutsui 			sio_addr[chan]->sio_cmd = REGNO(reg);
265681c174aStsutsui 		return sio_addr[chan]->sio_stat;
266df6945b8Stsutsui 	} else {
267df6945b8Stsutsui 		if (REGNO(reg) != 0)
268df6945b8Stsutsui 			sio_addr[chan]->sio_cmd = REGNO(reg);
269df6945b8Stsutsui 		sio_addr[chan]->sio_cmd = val;
270681c174aStsutsui 		return val;
271df6945b8Stsutsui 	}
272df6945b8Stsutsui }
273