1 /* $NetBSD: sio.c,v 1.3 2013/01/21 11:58:12 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 <luna68k/stand/boot/samachdep.h> 79 #include <luna68k/stand/boot/sioreg.h> 80 #include <luna68k/stand/boot/rcvbuf.h> 81 #include <luna68k/stand/boot/kbdreg.h> 82 83 static void siointr(int); 84 static int sioreg(int, int); 85 86 struct rcvbuf rcvbuf[NSIO]; 87 88 int sioconsole = -1; 89 struct siodevice *sio_addr[2]; 90 int cur_unit; 91 92 93 #define siounit(x) ( x & 0xffff ) 94 #define isprint(c) ((c >= 0x20) && (c < 0x7F) ? 1 : 0) 95 96 97 void 98 _siointr(void) 99 { 100 int unit; 101 102 for (unit = 0; unit < NSIO; unit++) 103 siointr(unit); 104 } 105 106 void 107 siointr(int unit) 108 { 109 /* struct siodevice *sio = sio_addr[unit]; */ 110 int rr0 = sioreg(REG(unit, RR0), 0); 111 int rr1 = sioreg(REG(unit, RR1), 0); 112 113 if (rr0 & RR0_RXAVAIL) { 114 if (rr1 & RR1_FRAMING) 115 return; 116 117 if (rr1 & (RR1_PARITY | RR1_OVERRUN)) 118 sioreg(REG(unit, WR0), WR0_ERRRST); /* Channel-A Error Reset */ 119 120 if (unit == 1) { 121 int c = kbd_decode(sio_addr[unit]->sio_data); 122 123 if ((c & KC_TYPE) == KC_CODE) 124 PUSH_RBUF(unit, c); 125 } else { 126 PUSH_RBUF(unit, sio_addr[unit]->sio_data); 127 } 128 } 129 } 130 131 /* 132 * Following are all routines needed for SIO to act as console 133 */ 134 #include <dev/cons.h> 135 #include <luna68k/stand/boot/romvec.h> 136 137 void 138 siocnprobe(struct consdev *cp) 139 { 140 sio_addr[0] = (struct siodevice *) 0x51000000; 141 sio_addr[1] = (struct siodevice *) 0x51000004; 142 143 /* make sure hardware exists */ 144 if (badaddr((short *)sio_addr[0])) { 145 cp->cn_pri = CN_DEAD; 146 return; 147 } 148 149 /* locate the major number */ 150 151 /* initialize required fields */ 152 cp->cn_dev = cur_unit = 0; 153 cp->cn_pri = CN_NORMAL; 154 } 155 156 void 157 siocninit(struct consdev *cp) 158 { 159 int unit = siounit(cp->cn_dev); 160 161 sioinit(); 162 sioconsole = unit; 163 } 164 165 int 166 siocngetc(dev_t dev) 167 { 168 int c, unit = siounit(dev); 169 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 = siounit(dev); 182 int s; 183 184 if (sioconsole == -1) { 185 (void) sioinit(); 186 sioconsole = unit; 187 } 188 189 s = splsio(); 190 191 /* wait for any pending transmission to finish */ 192 while ((sioreg(REG(unit, RR0), 0) & RR0_TXEMPTY) == 0); 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 199 splx(s); 200 } 201 202 /* SIO misc routines */ 203 204 void 205 sioinit(void) 206 { 207 int s; 208 209 RBUF_INIT(0); 210 RBUF_INIT(1); 211 212 s = splsio(); 213 214 sioreg(REG(0, WR0), WR0_CHANRST); /* Channel-A Reset */ 215 216 sioreg(WR2A, WR2_VEC86 | WR2_INTR_1); /* Set CPU BUS Interface Mode */ 217 sioreg(WR2B, 0); /* Set Interrupt Vector */ 218 219 sioreg(REG(0, WR0), WR0_RSTINT); /* Reset E/S Interrupt */ 220 sioreg(REG(0, WR4), WR4_BAUD96 | WR4_STOP1 | WR4_NPARITY); /* Tx/Rx */ 221 sioreg(REG(0, WR3), WR3_RX8BIT | WR3_RXENBL); /* Rx */ 222 sioreg(REG(0, WR5), WR5_TX8BIT | WR5_TXENBL | WR5_DTR | WR5_RTS); /* Tx */ 223 sioreg(REG(0, WR0), WR0_RSTINT); /* Reset E/S Interrupt */ 224 sioreg(REG(0, WR1), WR1_RXALLS); /* Interrupted All Char. */ 225 226 sioreg(REG(1, WR0), WR0_CHANRST); /* Channel-A Reset */ 227 228 sioreg(REG(1, WR0), WR0_RSTINT); /* Reset E/S Interrupt */ 229 sioreg(REG(1, WR4), WR4_BAUD96 | WR4_STOP1 | WR4_NPARITY); /* Tx/Rx */ 230 sioreg(REG(1, WR3), WR3_RX8BIT | WR3_RXENBL); /* Rx */ 231 sioreg(REG(1, WR5), WR5_TX8BIT | WR5_TXENBL); /* Tx */ 232 sioreg(REG(1, WR0), WR0_RSTINT); /* Reset E/S Interrupt */ 233 sioreg(REG(1, WR1), WR1_RXALLS); /* Interrupted All Char. */ 234 235 splx(s); 236 } 237 238 int 239 sioreg(int reg, int val) 240 { 241 int chan; 242 243 chan = CHANNEL(reg); 244 245 if (isStatusReg(reg)) { 246 if (REGNO(reg) != 0) 247 sio_addr[chan]->sio_cmd = REGNO(reg); 248 return(sio_addr[chan]->sio_stat); 249 } else { 250 if (REGNO(reg) != 0) 251 sio_addr[chan]->sio_cmd = REGNO(reg); 252 sio_addr[chan]->sio_cmd = val; 253 return(val); 254 } 255 } 256