1/*	$OpenBSD: sio.c,v 1.4 2023/01/10 17:10:57 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
85static int sioreg(int, int);
86
87struct rcvbuf	rcvbuf[NSIO];
88
89int	sioconsole = -1;
90struct	siodevice *sio_addr[2];
91
92int
93siointr(int unit)
94{
95/*	struct siodevice *sio = sio_addr[unit]; */
96	int rr0 = sioreg(REG(unit, RR0), 0);
97	int rr1 = sioreg(REG(unit, RR1), 0);
98
99	if (rr0 & RR0_RXAVAIL) {
100		if (rr1 & RR1_FRAMING)
101			return 1;
102
103		if (rr1 & (RR1_PARITY | RR1_OVERRUN))
104		    sioreg(REG(unit, WR0), WR0_ERRRST); /* Channel-A Error Reset */
105
106		if (unit == 1) {
107			int c = kbd_decode(sio_addr[unit]->sio_data);
108
109			if ((c & KC_TYPE) == KC_CODE)
110				PUSH_RBUF(unit, c);
111		} else {
112			PUSH_RBUF(unit, sio_addr[unit]->sio_data);
113		}
114		return 1;
115	}
116
117	return 0;
118}
119
120/*
121 * Following are all routines needed for SIO to act as console
122 */
123#include <dev/cons.h>
124
125void
126siocnprobe(struct consdev *cp)
127{
128	sio_addr[0] = (struct siodevice *)OBIO_SIO;
129	sio_addr[1] = (struct siodevice *)OBIO_SIO + 1;
130
131	/* make sure hardware exists */
132	if (badaddr(sio_addr[0], 4) != 0) {
133		cp->cn_pri = CN_DEAD;
134		return;
135	}
136
137	/* locate the major number */
138
139	/* initialize required fields */
140	cp->cn_dev = 0;
141	cp->cn_pri = CN_LOWPRI;
142}
143
144void
145siocninit(struct consdev *cp)
146{
147	int unit = cp->cn_dev;
148
149	sioinit();
150	sioconsole = unit;
151}
152
153int
154siocngetc(dev_t dev)
155{
156	int c, unit = dev & ~0x80, poll = (dev & 0x80) != 0;
157
158	siointr(unit);
159
160	if (poll) {
161		if (RBUF_EMPTY(unit))
162			return 0;
163		PEEK_RBUF(unit, c);
164		return c;
165
166	}
167
168	while (RBUF_EMPTY(unit)) {
169		DELAY(1);
170		siointr(unit);
171	}
172
173	POP_RBUF(unit, c);
174	return c;
175}
176
177void
178siocnputc(dev_t dev, int c)
179{
180	int unit = dev;
181
182	if (sioconsole == -1) {
183		(void) sioinit();
184		sioconsole = unit;
185	}
186
187	/* wait for any pending transmission to finish */
188	while ((sioreg(REG(unit, RR0), 0) & RR0_TXEMPTY) == 0);
189
190	sio_addr[unit]->sio_data = (c & 0xFF);
191
192	/* wait for any pending transmission to finish */
193	while ((sioreg(REG(unit, RR0), 0) & RR0_TXEMPTY) == 0);
194}
195
196/* SIO misc routines */
197
198void
199sioinit(void)
200{
201	RBUF_INIT(0);
202	RBUF_INIT(1);
203
204	sioreg(REG(0, WR0), WR0_CHANRST);		/* Channel-A Reset */
205
206	sioreg(WR2A, WR2_VEC86  | WR2_INTR_1);		/* Set CPU BUS Interface Mode */
207	sioreg(WR2B, 0);				/* Set Interrupt Vector */
208
209	sioreg(REG(0, WR0), WR0_RSTINT);		/* Reset E/S Interrupt */
210	sioreg(REG(0, WR4), WR4_BAUD96 | WR4_STOP1 | WR4_NPARITY);	/* Tx/Rx */
211	sioreg(REG(0, WR3), WR3_RX8BIT | WR3_RXENBL);		/* Rx */
212	sioreg(REG(0, WR5), WR5_TX8BIT | WR5_TXENBL | WR5_DTR | WR5_RTS);		/* Tx */
213	sioreg(REG(0, WR0), WR0_RSTINT);		/* Reset E/S Interrupt */
214	sioreg(REG(0, WR1), WR1_RXALLS);		/* Interrupted All Char. */
215
216	sioreg(REG(1, WR0), WR0_CHANRST);		/* Channel-A Reset */
217
218	sioreg(REG(1, WR0), WR0_RSTINT);		/* Reset E/S Interrupt */
219	sioreg(REG(1, WR4), WR4_BAUD96 | WR4_STOP1 | WR4_NPARITY);	/* Tx/Rx */
220	sioreg(REG(1, WR3), WR3_RX8BIT | WR3_RXENBL);		/* Rx */
221	sioreg(REG(1, WR5), WR5_TX8BIT | WR5_TXENBL);		/* Tx */
222	sioreg(REG(1, WR0), WR0_RSTINT);		/* Reset E/S Interrupt */
223	sioreg(REG(1, WR1), WR1_RXALLS);		/* Interrupted All Char. */
224}
225
226int
227sioreg(int reg, int val)
228{
229	int chan;
230
231	chan = CHANNEL(reg);
232
233	if (isStatusReg(reg)) {
234		if (REGNO(reg) != 0)
235		    sio_addr[chan]->sio_cmd = REGNO(reg);
236		return(sio_addr[chan]->sio_stat);
237	} else {
238		if (REGNO(reg) != 0)
239		    sio_addr[chan]->sio_cmd = REGNO(reg);
240		sio_addr[chan]->sio_cmd = val;
241		return(val);
242	}
243}
244