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