com.c revision 1.74 1 1.74 cgd /* $NetBSD: com.c,v 1.74 1996/03/09 01:02:08 cgd Exp $ */
2 1.38 cgd
3 1.1 cgd /*-
4 1.71 mycroft * Copyright (c) 1993, 1994, 1995, 1996
5 1.71 mycroft * Charles M. Hannum. All rights reserved.
6 1.1 cgd * Copyright (c) 1991 The Regents of the University of California.
7 1.1 cgd * All rights reserved.
8 1.1 cgd *
9 1.1 cgd * Redistribution and use in source and binary forms, with or without
10 1.1 cgd * modification, are permitted provided that the following conditions
11 1.1 cgd * are met:
12 1.1 cgd * 1. Redistributions of source code must retain the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer.
14 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer in the
16 1.1 cgd * documentation and/or other materials provided with the distribution.
17 1.1 cgd * 3. All advertising materials mentioning features or use of this software
18 1.1 cgd * must display the following acknowledgement:
19 1.1 cgd * This product includes software developed by the University of
20 1.1 cgd * California, Berkeley and its contributors.
21 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
22 1.1 cgd * may be used to endorse or promote products derived from this software
23 1.1 cgd * without specific prior written permission.
24 1.1 cgd *
25 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 cgd * SUCH DAMAGE.
36 1.1 cgd *
37 1.38 cgd * @(#)com.c 7.5 (Berkeley) 5/16/91
38 1.1 cgd */
39 1.1 cgd
40 1.1 cgd /*
41 1.1 cgd * COM driver, based on HP dca driver
42 1.1 cgd * uses National Semiconductor NS16450/NS16550AF UART
43 1.1 cgd */
44 1.14 mycroft #include <sys/param.h>
45 1.14 mycroft #include <sys/systm.h>
46 1.14 mycroft #include <sys/ioctl.h>
47 1.14 mycroft #include <sys/select.h>
48 1.14 mycroft #include <sys/tty.h>
49 1.14 mycroft #include <sys/proc.h>
50 1.14 mycroft #include <sys/user.h>
51 1.14 mycroft #include <sys/conf.h>
52 1.14 mycroft #include <sys/file.h>
53 1.14 mycroft #include <sys/uio.h>
54 1.14 mycroft #include <sys/kernel.h>
55 1.14 mycroft #include <sys/syslog.h>
56 1.14 mycroft #include <sys/types.h>
57 1.21 mycroft #include <sys/device.h>
58 1.14 mycroft
59 1.21 mycroft #include <machine/cpu.h>
60 1.14 mycroft #include <machine/pio.h>
61 1.14 mycroft
62 1.49 cgd #include <dev/isa/isavar.h>
63 1.47 cgd #include <dev/isa/comreg.h>
64 1.74 cgd #include <dev/isa/comvar.h>
65 1.60 cgd #include <dev/ic/ns16550reg.h>
66 1.65 christos #ifdef COM_HAYESP
67 1.65 christos #include <dev/ic/hayespreg.h>
68 1.65 christos #endif
69 1.62 mycroft #define com_lcr com_cfcr
70 1.14 mycroft
71 1.64 christos #define COM_IBUFSIZE (2 * 512)
72 1.57 mycroft #define COM_IHIGHWATER ((3 * COM_IBUFSIZE) / 4)
73 1.57 mycroft
74 1.21 mycroft struct com_softc {
75 1.21 mycroft struct device sc_dev;
76 1.49 cgd void *sc_ih;
77 1.50 mycroft struct tty *sc_tty;
78 1.1 cgd
79 1.33 mycroft int sc_overflows;
80 1.57 mycroft int sc_floods;
81 1.57 mycroft int sc_errors;
82 1.57 mycroft
83 1.66 mycroft int sc_halt;
84 1.66 mycroft
85 1.41 mycroft int sc_iobase;
86 1.65 christos #ifdef COM_HAYESP
87 1.65 christos int sc_hayespbase;
88 1.65 christos #endif
89 1.22 cgd u_char sc_hwflags;
90 1.29 mycroft #define COM_HW_NOIEN 0x01
91 1.22 cgd #define COM_HW_FIFO 0x02
92 1.65 christos #define COM_HW_HAYESP 0x04
93 1.22 cgd #define COM_HW_CONSOLE 0x40
94 1.22 cgd u_char sc_swflags;
95 1.22 cgd #define COM_SW_SOFTCAR 0x01
96 1.22 cgd #define COM_SW_CLOCAL 0x02
97 1.22 cgd #define COM_SW_CRTSCTS 0x04
98 1.25 cgd #define COM_SW_MDMBUF 0x08
99 1.69 mycroft u_char sc_msr, sc_mcr, sc_lcr, sc_ier;
100 1.57 mycroft u_char sc_dtr;
101 1.57 mycroft
102 1.57 mycroft u_char *sc_ibuf, *sc_ibufp, *sc_ibufhigh, *sc_ibufend;
103 1.57 mycroft u_char sc_ibufs[2][COM_IBUFSIZE];
104 1.29 mycroft };
105 1.21 mycroft
106 1.40 mycroft int comprobe __P((struct device *, void *, void *));
107 1.40 mycroft void comattach __P((struct device *, struct device *, void *));
108 1.21 mycroft int comopen __P((dev_t, int, int, struct proc *));
109 1.21 mycroft int comclose __P((dev_t, int, int, struct proc *));
110 1.33 mycroft void comdiag __P((void *));
111 1.49 cgd int comintr __P((void *));
112 1.57 mycroft void compoll __P((void *));
113 1.21 mycroft int comparam __P((struct tty *, struct termios *));
114 1.21 mycroft void comstart __P((struct tty *));
115 1.1 cgd
116 1.29 mycroft struct cfdriver comcd = {
117 1.29 mycroft NULL, "com", comprobe, comattach, DV_TTY, sizeof(struct com_softc)
118 1.1 cgd };
119 1.1 cgd
120 1.1 cgd #ifdef COMCONSOLE
121 1.66 mycroft int comdefaultrate = CONSPEED;
122 1.1 cgd int comconsole = COMCONSOLE;
123 1.1 cgd #else
124 1.66 mycroft int comdefaultrate = TTYDEF_SPEED;
125 1.1 cgd int comconsole = -1;
126 1.1 cgd #endif
127 1.1 cgd int comconsinit;
128 1.1 cgd int commajor;
129 1.57 mycroft int comsopen = 0;
130 1.57 mycroft int comevents = 0;
131 1.1 cgd
132 1.1 cgd #ifdef KGDB
133 1.14 mycroft #include <machine/remote-sl.h>
134 1.1 cgd extern int kgdb_dev;
135 1.1 cgd extern int kgdb_rate;
136 1.1 cgd extern int kgdb_debug_init;
137 1.1 cgd #endif
138 1.1 cgd
139 1.21 mycroft #define COMUNIT(x) (minor(x))
140 1.1 cgd
141 1.62 mycroft /* Macros to clear/set/test flags. */
142 1.62 mycroft #define SET(t, f) (t) |= (f)
143 1.62 mycroft #define CLR(t, f) (t) &= ~(f)
144 1.62 mycroft #define ISSET(t, f) ((t) & (f))
145 1.21 mycroft
146 1.21 mycroft int
147 1.21 mycroft comspeed(speed)
148 1.21 mycroft long speed;
149 1.1 cgd {
150 1.21 mycroft #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
151 1.21 mycroft
152 1.21 mycroft int x, err;
153 1.21 mycroft
154 1.21 mycroft if (speed == 0)
155 1.21 mycroft return 0;
156 1.21 mycroft if (speed < 0)
157 1.21 mycroft return -1;
158 1.21 mycroft x = divrnd((COM_FREQ / 16), speed);
159 1.21 mycroft if (x <= 0)
160 1.21 mycroft return -1;
161 1.21 mycroft err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
162 1.21 mycroft if (err < 0)
163 1.21 mycroft err = -err;
164 1.21 mycroft if (err > COM_TOLERANCE)
165 1.21 mycroft return -1;
166 1.21 mycroft return x;
167 1.21 mycroft
168 1.21 mycroft #undef divrnd(n, q)
169 1.21 mycroft }
170 1.21 mycroft
171 1.21 mycroft int
172 1.21 mycroft comprobe1(iobase)
173 1.41 mycroft int iobase;
174 1.21 mycroft {
175 1.21 mycroft
176 1.1 cgd /* force access to id reg */
177 1.62 mycroft outb(iobase + com_lcr, 0);
178 1.21 mycroft outb(iobase + com_iir, 0);
179 1.21 mycroft if (inb(iobase + com_iir) & 0x38)
180 1.21 mycroft return 0;
181 1.21 mycroft
182 1.21 mycroft return 1;
183 1.1 cgd }
184 1.1 cgd
185 1.65 christos #ifdef COM_HAYESP
186 1.65 christos int
187 1.65 christos comprobeHAYESP(iobase, sc)
188 1.65 christos int iobase;
189 1.64 christos struct com_softc *sc;
190 1.64 christos {
191 1.65 christos char val, dips;
192 1.64 christos int combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
193 1.64 christos
194 1.64 christos /*
195 1.64 christos * Hayes ESP cards have two iobases. One is for compatibility with
196 1.64 christos * 16550 serial chips, and at the same ISA PC base addresses. The
197 1.64 christos * other is for ESP-specific enhanced features, and lies at a
198 1.65 christos * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
199 1.64 christos */
200 1.64 christos
201 1.64 christos /* Test for ESP signature */
202 1.65 christos if ((inb(iobase) & 0xf3) == 0)
203 1.65 christos return 0;
204 1.64 christos
205 1.64 christos /*
206 1.64 christos * ESP is present at ESP enhanced base address; unknown com port
207 1.64 christos */
208 1.64 christos
209 1.64 christos /* Get the dip-switch configurations */
210 1.65 christos outb(iobase + HAYESP_CMD1, HAYESP_GETDIPS);
211 1.65 christos dips = inb(iobase + HAYESP_STATUS1);
212 1.64 christos
213 1.64 christos /* Determine which com port this ESP card services: bits 0,1 of */
214 1.64 christos /* dips is the port # (0-3); combaselist[val] is the com_iobase */
215 1.65 christos if (sc->sc_iobase != combaselist[dips & 0x03])
216 1.65 christos return 0;
217 1.65 christos
218 1.65 christos printf(": ESP");
219 1.64 christos
220 1.65 christos /* Check ESP Self Test bits. */
221 1.64 christos /* Check for ESP version 2.0: bits 4,5,6 == 010 */
222 1.65 christos outb(iobase + HAYESP_CMD1, HAYESP_GETTEST);
223 1.65 christos val = inb(iobase + HAYESP_STATUS1); /* Clear reg 1 */
224 1.65 christos val = inb(iobase + HAYESP_STATUS2);
225 1.64 christos if ((val & 0x70) < 0x20) {
226 1.64 christos printf("-old (%o)", val & 0x70);
227 1.64 christos /* we do not support the necessary features */
228 1.65 christos return 0;
229 1.64 christos }
230 1.64 christos
231 1.64 christos /* Check for ability to emulate 16550: bit 8 == 1 */
232 1.64 christos if ((dips & 0x80) == 0) {
233 1.64 christos printf(" slave");
234 1.64 christos /* XXX Does slave really mean no 16550 support?? */
235 1.65 christos return 0;
236 1.64 christos }
237 1.64 christos
238 1.64 christos /*
239 1.64 christos * If we made it this far, we are a full-featured ESP v2.0 (or
240 1.64 christos * better), at the correct com port address.
241 1.64 christos */
242 1.64 christos
243 1.65 christos SET(sc->sc_hwflags, COM_HW_HAYESP);
244 1.73 scottr printf(", 1024 byte fifo\n");
245 1.65 christos return 1;
246 1.64 christos }
247 1.65 christos #endif
248 1.64 christos
249 1.21 mycroft int
250 1.40 mycroft comprobe(parent, match, aux)
251 1.40 mycroft struct device *parent;
252 1.40 mycroft void *match, *aux;
253 1.29 mycroft {
254 1.29 mycroft struct isa_attach_args *ia = aux;
255 1.41 mycroft int iobase = ia->ia_iobase;
256 1.21 mycroft
257 1.21 mycroft if (!comprobe1(iobase))
258 1.21 mycroft return 0;
259 1.21 mycroft
260 1.29 mycroft ia->ia_iosize = COM_NPORTS;
261 1.29 mycroft ia->ia_msize = 0;
262 1.29 mycroft return 1;
263 1.21 mycroft }
264 1.1 cgd
265 1.29 mycroft void
266 1.29 mycroft comattach(parent, self, aux)
267 1.29 mycroft struct device *parent, *self;
268 1.29 mycroft void *aux;
269 1.29 mycroft {
270 1.29 mycroft struct com_softc *sc = (void *)self;
271 1.29 mycroft struct isa_attach_args *ia = aux;
272 1.29 mycroft struct cfdata *cf = sc->sc_dev.dv_cfdata;
273 1.41 mycroft int iobase = ia->ia_iobase;
274 1.21 mycroft struct tty *tp;
275 1.65 christos #ifdef COM_HAYESP
276 1.65 christos int hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
277 1.65 christos int *hayespp;
278 1.65 christos #endif
279 1.1 cgd
280 1.21 mycroft sc->sc_iobase = iobase;
281 1.62 mycroft sc->sc_hwflags = ISSET(cf->cf_flags, COM_HW_NOIEN);
282 1.22 cgd sc->sc_swflags = 0;
283 1.21 mycroft
284 1.29 mycroft if (sc->sc_dev.dv_unit == comconsole)
285 1.29 mycroft delay(1000);
286 1.26 cgd
287 1.65 christos #ifdef COM_HAYESP
288 1.65 christos /* Look for a Hayes ESP board. */
289 1.65 christos for (hayespp = hayesp_ports; *hayespp != 0; hayespp++)
290 1.65 christos if (comprobeHAYESP(*hayespp, sc)) {
291 1.65 christos sc->sc_hayespbase = *hayespp;
292 1.65 christos break;
293 1.65 christos }
294 1.65 christos /* No ESP; look for other things. */
295 1.65 christos if (*hayespp == 0) {
296 1.65 christos #endif
297 1.64 christos
298 1.1 cgd /* look for a NS 16550AF UART with FIFOs */
299 1.21 mycroft outb(iobase + com_fifo,
300 1.21 mycroft FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
301 1.20 mycroft delay(100);
302 1.62 mycroft if (ISSET(inb(iobase + com_iir), IIR_FIFO_MASK) == IIR_FIFO_MASK)
303 1.62 mycroft if (ISSET(inb(iobase + com_fifo), FIFO_TRIGGER_14) == FIFO_TRIGGER_14) {
304 1.62 mycroft SET(sc->sc_hwflags, COM_HW_FIFO);
305 1.29 mycroft printf(": ns16550a, working fifo\n");
306 1.21 mycroft } else
307 1.56 jtc printf(": ns16550, broken fifo\n");
308 1.21 mycroft else
309 1.56 jtc printf(": ns8250 or ns16450, no fifo\n");
310 1.21 mycroft outb(iobase + com_fifo, 0);
311 1.65 christos #ifdef COM_HAYESP
312 1.65 christos }
313 1.65 christos #endif
314 1.21 mycroft
315 1.21 mycroft /* disable interrupts */
316 1.21 mycroft outb(iobase + com_ier, 0);
317 1.21 mycroft outb(iobase + com_mcr, 0);
318 1.1 cgd
319 1.49 cgd if (ia->ia_irq != IRQUNK)
320 1.63 mycroft sc->sc_ih = isa_intr_establish(ia->ia_irq, IST_EDGE, IPL_TTY,
321 1.63 mycroft comintr, sc);
322 1.30 mycroft
323 1.1 cgd #ifdef KGDB
324 1.2 cgd if (kgdb_dev == makedev(commajor, unit)) {
325 1.1 cgd if (comconsole == unit)
326 1.1 cgd kgdb_dev = -1; /* can't debug over console port */
327 1.1 cgd else {
328 1.1 cgd (void) cominit(unit, kgdb_rate);
329 1.1 cgd if (kgdb_debug_init) {
330 1.1 cgd /*
331 1.1 cgd * Print prefix of device name,
332 1.1 cgd * let kgdb_connect print the rest.
333 1.1 cgd */
334 1.21 mycroft printf("%s: ", sc->sc_dev.dv_xname);
335 1.1 cgd kgdb_connect(1);
336 1.1 cgd } else
337 1.21 mycroft printf("%s: kgdb enabled\n",
338 1.21 mycroft sc->sc_dev.dv_xname);
339 1.1 cgd }
340 1.1 cgd }
341 1.1 cgd #endif
342 1.21 mycroft
343 1.29 mycroft if (sc->sc_dev.dv_unit == comconsole) {
344 1.29 mycroft /*
345 1.29 mycroft * Need to reset baud rate, etc. of next print so reset
346 1.29 mycroft * comconsinit. Also make sure console is always "hardwired".
347 1.29 mycroft */
348 1.1 cgd comconsinit = 0;
349 1.62 mycroft SET(sc->sc_hwflags, COM_HW_CONSOLE);
350 1.62 mycroft SET(sc->sc_swflags, COM_SW_SOFTCAR);
351 1.1 cgd }
352 1.1 cgd }
353 1.1 cgd
354 1.21 mycroft int
355 1.21 mycroft comopen(dev, flag, mode, p)
356 1.21 mycroft dev_t dev;
357 1.21 mycroft int flag, mode;
358 1.21 mycroft struct proc *p;
359 1.1 cgd {
360 1.21 mycroft int unit = COMUNIT(dev);
361 1.21 mycroft struct com_softc *sc;
362 1.41 mycroft int iobase;
363 1.21 mycroft struct tty *tp;
364 1.21 mycroft int s;
365 1.1 cgd int error = 0;
366 1.1 cgd
367 1.29 mycroft if (unit >= comcd.cd_ndevs)
368 1.21 mycroft return ENXIO;
369 1.29 mycroft sc = comcd.cd_devs[unit];
370 1.29 mycroft if (!sc)
371 1.21 mycroft return ENXIO;
372 1.21 mycroft
373 1.50 mycroft if (!sc->sc_tty)
374 1.50 mycroft tp = sc->sc_tty = ttymalloc();
375 1.21 mycroft else
376 1.50 mycroft tp = sc->sc_tty;
377 1.21 mycroft
378 1.1 cgd tp->t_oproc = comstart;
379 1.1 cgd tp->t_param = comparam;
380 1.1 cgd tp->t_dev = dev;
381 1.62 mycroft if (!ISSET(tp->t_state, TS_ISOPEN)) {
382 1.62 mycroft SET(tp->t_state, TS_WOPEN);
383 1.1 cgd ttychars(tp);
384 1.16 ws tp->t_iflag = TTYDEF_IFLAG;
385 1.16 ws tp->t_oflag = TTYDEF_OFLAG;
386 1.16 ws tp->t_cflag = TTYDEF_CFLAG;
387 1.62 mycroft if (ISSET(sc->sc_swflags, COM_SW_CLOCAL))
388 1.62 mycroft SET(tp->t_cflag, CLOCAL);
389 1.62 mycroft if (ISSET(sc->sc_swflags, COM_SW_CRTSCTS))
390 1.62 mycroft SET(tp->t_cflag, CRTSCTS);
391 1.62 mycroft if (ISSET(sc->sc_swflags, COM_SW_MDMBUF))
392 1.62 mycroft SET(tp->t_cflag, MDMBUF);
393 1.16 ws tp->t_lflag = TTYDEF_LFLAG;
394 1.16 ws tp->t_ispeed = tp->t_ospeed = comdefaultrate;
395 1.57 mycroft
396 1.57 mycroft s = spltty();
397 1.57 mycroft
398 1.1 cgd comparam(tp, &tp->t_termios);
399 1.1 cgd ttsetwater(tp);
400 1.21 mycroft
401 1.57 mycroft if (comsopen++ == 0)
402 1.57 mycroft timeout(compoll, NULL, 1);
403 1.57 mycroft
404 1.57 mycroft sc->sc_ibufp = sc->sc_ibuf = sc->sc_ibufs[0];
405 1.57 mycroft sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
406 1.57 mycroft sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
407 1.57 mycroft
408 1.21 mycroft iobase = sc->sc_iobase;
409 1.65 christos #ifdef COM_HAYESP
410 1.64 christos /* Setup the ESP board */
411 1.65 christos if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
412 1.65 christos int hayespbase = sc->sc_hayespbase;
413 1.65 christos
414 1.64 christos outb(iobase + com_fifo,
415 1.64 christos FIFO_DMA_MODE|FIFO_ENABLE|
416 1.64 christos FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_8);
417 1.65 christos
418 1.64 christos /* Set 16550 compatibility mode */
419 1.65 christos outb(hayespbase + HAYESP_CMD1, HAYESP_SETMODE);
420 1.65 christos outb(hayespbase + HAYESP_CMD2,
421 1.65 christos HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
422 1.65 christos HAYESP_MODE_SCALE);
423 1.65 christos
424 1.64 christos /* Set RTS/CTS flow control */
425 1.65 christos outb(hayespbase + HAYESP_CMD1, HAYESP_SETFLOWTYPE);
426 1.65 christos outb(hayespbase + HAYESP_CMD2, HAYESP_FLOW_RTS);
427 1.65 christos outb(hayespbase + HAYESP_CMD2, HAYESP_FLOW_CTS);
428 1.65 christos
429 1.64 christos /* Set flow control levels */
430 1.65 christos outb(hayespbase + HAYESP_CMD1, HAYESP_SETRXFLOW);
431 1.65 christos outb(hayespbase + HAYESP_CMD2,
432 1.65 christos HAYESP_HIBYTE(HAYESP_RXHIWMARK));
433 1.65 christos outb(hayespbase + HAYESP_CMD2,
434 1.65 christos HAYESP_LOBYTE(HAYESP_RXHIWMARK));
435 1.65 christos outb(hayespbase + HAYESP_CMD2,
436 1.65 christos HAYESP_HIBYTE(HAYESP_RXLOWMARK));
437 1.65 christos outb(hayespbase + HAYESP_CMD2,
438 1.65 christos HAYESP_LOBYTE(HAYESP_RXLOWMARK));
439 1.65 christos } else
440 1.65 christos #endif
441 1.65 christos if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
442 1.64 christos /* Set the FIFO threshold based on the receive speed. */
443 1.36 mycroft outb(iobase + com_fifo,
444 1.36 mycroft FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
445 1.36 mycroft (tp->t_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
446 1.21 mycroft /* flush any pending I/O */
447 1.62 mycroft while (ISSET(inb(iobase + com_lsr), LSR_RXRDY))
448 1.57 mycroft (void) inb(iobase + com_data);
449 1.21 mycroft /* you turn me on, baby */
450 1.26 cgd sc->sc_mcr = MCR_DTR | MCR_RTS;
451 1.62 mycroft if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
452 1.62 mycroft SET(sc->sc_mcr, MCR_IENABLE);
453 1.26 cgd outb(iobase + com_mcr, sc->sc_mcr);
454 1.69 mycroft sc->sc_ier = IER_ERXRDY | IER_ERLS | IER_EMSC;
455 1.69 mycroft outb(iobase + com_ier, sc->sc_ier);
456 1.21 mycroft
457 1.21 mycroft sc->sc_msr = inb(iobase + com_msr);
458 1.62 mycroft if (ISSET(sc->sc_swflags, COM_SW_SOFTCAR) ||
459 1.62 mycroft ISSET(sc->sc_msr, MSR_DCD) || ISSET(tp->t_cflag, MDMBUF))
460 1.62 mycroft SET(tp->t_state, TS_CARR_ON);
461 1.21 mycroft else
462 1.62 mycroft CLR(tp->t_state, TS_CARR_ON);
463 1.62 mycroft } else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
464 1.21 mycroft return EBUSY;
465 1.62 mycroft else
466 1.57 mycroft s = spltty();
467 1.21 mycroft
468 1.21 mycroft /* wait for carrier if necessary */
469 1.62 mycroft if (!ISSET(flag, O_NONBLOCK))
470 1.62 mycroft while (!ISSET(tp->t_cflag, CLOCAL) &&
471 1.62 mycroft !ISSET(tp->t_state, TS_CARR_ON)) {
472 1.62 mycroft SET(tp->t_state, TS_WOPEN);
473 1.62 mycroft error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
474 1.62 mycroft ttopen, 0);
475 1.21 mycroft if (error) {
476 1.21 mycroft /* XXX should turn off chip if we're the
477 1.21 mycroft only waiter */
478 1.21 mycroft splx(s);
479 1.21 mycroft return error;
480 1.21 mycroft }
481 1.21 mycroft }
482 1.21 mycroft splx(s);
483 1.21 mycroft
484 1.21 mycroft return (*linesw[tp->t_line].l_open)(dev, tp);
485 1.1 cgd }
486 1.1 cgd
487 1.21 mycroft int
488 1.1 cgd comclose(dev, flag, mode, p)
489 1.1 cgd dev_t dev;
490 1.1 cgd int flag, mode;
491 1.1 cgd struct proc *p;
492 1.1 cgd {
493 1.21 mycroft int unit = COMUNIT(dev);
494 1.29 mycroft struct com_softc *sc = comcd.cd_devs[unit];
495 1.50 mycroft struct tty *tp = sc->sc_tty;
496 1.41 mycroft int iobase = sc->sc_iobase;
497 1.57 mycroft int s;
498 1.57 mycroft
499 1.57 mycroft /* XXX This is for cons.c. */
500 1.62 mycroft if (!ISSET(tp->t_state, TS_ISOPEN))
501 1.57 mycroft return 0;
502 1.21 mycroft
503 1.1 cgd (*linesw[tp->t_line].l_close)(tp, flag);
504 1.57 mycroft s = spltty();
505 1.62 mycroft CLR(sc->sc_lcr, LCR_SBREAK);
506 1.62 mycroft outb(iobase + com_lcr, sc->sc_lcr);
507 1.57 mycroft outb(iobase + com_ier, 0);
508 1.62 mycroft if (ISSET(tp->t_cflag, HUPCL) &&
509 1.62 mycroft !ISSET(sc->sc_swflags, COM_SW_SOFTCAR)) {
510 1.57 mycroft /* XXX perhaps only clear DTR */
511 1.57 mycroft outb(iobase + com_mcr, 0);
512 1.57 mycroft }
513 1.62 mycroft CLR(tp->t_state, TS_BUSY | TS_FLUSH);
514 1.57 mycroft if (--comsopen == 0)
515 1.57 mycroft untimeout(compoll, NULL);
516 1.57 mycroft splx(s);
517 1.1 cgd ttyclose(tp);
518 1.21 mycroft #ifdef notyet /* XXXX */
519 1.21 mycroft if (unit != comconsole) {
520 1.21 mycroft ttyfree(tp);
521 1.50 mycroft sc->sc_tty = 0;
522 1.21 mycroft }
523 1.13 cgd #endif
524 1.21 mycroft return 0;
525 1.1 cgd }
526 1.1 cgd
527 1.21 mycroft int
528 1.1 cgd comread(dev, uio, flag)
529 1.1 cgd dev_t dev;
530 1.1 cgd struct uio *uio;
531 1.21 mycroft int flag;
532 1.1 cgd {
533 1.52 mycroft struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
534 1.52 mycroft struct tty *tp = sc->sc_tty;
535 1.1 cgd
536 1.1 cgd return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
537 1.1 cgd }
538 1.1 cgd
539 1.21 mycroft int
540 1.1 cgd comwrite(dev, uio, flag)
541 1.1 cgd dev_t dev;
542 1.1 cgd struct uio *uio;
543 1.21 mycroft int flag;
544 1.1 cgd {
545 1.52 mycroft struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
546 1.52 mycroft struct tty *tp = sc->sc_tty;
547 1.1 cgd
548 1.1 cgd return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
549 1.1 cgd }
550 1.50 mycroft
551 1.50 mycroft struct tty *
552 1.50 mycroft comtty(dev)
553 1.50 mycroft dev_t dev;
554 1.50 mycroft {
555 1.52 mycroft struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
556 1.52 mycroft struct tty *tp = sc->sc_tty;
557 1.50 mycroft
558 1.52 mycroft return (tp);
559 1.50 mycroft }
560 1.1 cgd
561 1.21 mycroft static u_char
562 1.21 mycroft tiocm_xxx2mcr(data)
563 1.21 mycroft int data;
564 1.21 mycroft {
565 1.21 mycroft u_char m = 0;
566 1.21 mycroft
567 1.62 mycroft if (ISSET(data, TIOCM_DTR))
568 1.62 mycroft SET(m, MCR_DTR);
569 1.62 mycroft if (ISSET(data, TIOCM_RTS))
570 1.62 mycroft SET(m, MCR_RTS);
571 1.21 mycroft return m;
572 1.1 cgd }
573 1.1 cgd
574 1.21 mycroft int
575 1.19 mycroft comioctl(dev, cmd, data, flag, p)
576 1.1 cgd dev_t dev;
577 1.39 cgd u_long cmd;
578 1.1 cgd caddr_t data;
579 1.19 mycroft int flag;
580 1.19 mycroft struct proc *p;
581 1.1 cgd {
582 1.21 mycroft int unit = COMUNIT(dev);
583 1.29 mycroft struct com_softc *sc = comcd.cd_devs[unit];
584 1.50 mycroft struct tty *tp = sc->sc_tty;
585 1.41 mycroft int iobase = sc->sc_iobase;
586 1.21 mycroft int error;
587 1.21 mycroft
588 1.19 mycroft error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
589 1.1 cgd if (error >= 0)
590 1.21 mycroft return error;
591 1.19 mycroft error = ttioctl(tp, cmd, data, flag, p);
592 1.1 cgd if (error >= 0)
593 1.21 mycroft return error;
594 1.1 cgd
595 1.1 cgd switch (cmd) {
596 1.1 cgd case TIOCSBRK:
597 1.62 mycroft SET(sc->sc_lcr, LCR_SBREAK);
598 1.62 mycroft outb(iobase + com_lcr, sc->sc_lcr);
599 1.1 cgd break;
600 1.1 cgd case TIOCCBRK:
601 1.62 mycroft CLR(sc->sc_lcr, LCR_SBREAK);
602 1.62 mycroft outb(iobase + com_lcr, sc->sc_lcr);
603 1.1 cgd break;
604 1.1 cgd case TIOCSDTR:
605 1.62 mycroft SET(sc->sc_mcr, sc->sc_dtr);
606 1.62 mycroft outb(iobase + com_mcr, sc->sc_mcr);
607 1.1 cgd break;
608 1.1 cgd case TIOCCDTR:
609 1.62 mycroft CLR(sc->sc_mcr, sc->sc_dtr);
610 1.62 mycroft outb(iobase + com_mcr, sc->sc_mcr);
611 1.1 cgd break;
612 1.1 cgd case TIOCMSET:
613 1.62 mycroft CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
614 1.1 cgd case TIOCMBIS:
615 1.62 mycroft SET(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
616 1.62 mycroft outb(iobase + com_mcr, sc->sc_mcr);
617 1.1 cgd break;
618 1.1 cgd case TIOCMBIC:
619 1.62 mycroft CLR(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
620 1.62 mycroft outb(iobase + com_mcr, sc->sc_mcr);
621 1.1 cgd break;
622 1.21 mycroft case TIOCMGET: {
623 1.21 mycroft u_char m;
624 1.21 mycroft int bits = 0;
625 1.21 mycroft
626 1.21 mycroft m = sc->sc_mcr;
627 1.62 mycroft if (ISSET(m, MCR_DTR))
628 1.62 mycroft SET(bits, TIOCM_DTR);
629 1.62 mycroft if (ISSET(m, MCR_RTS))
630 1.62 mycroft SET(bits, TIOCM_RTS);
631 1.21 mycroft m = sc->sc_msr;
632 1.62 mycroft if (ISSET(m, MSR_DCD))
633 1.62 mycroft SET(bits, TIOCM_CD);
634 1.62 mycroft if (ISSET(m, MSR_CTS))
635 1.62 mycroft SET(bits, TIOCM_CTS);
636 1.62 mycroft if (ISSET(m, MSR_DSR))
637 1.62 mycroft SET(bits, TIOCM_DSR);
638 1.62 mycroft if (ISSET(m, MSR_RI | MSR_TERI))
639 1.62 mycroft SET(bits, TIOCM_RI);
640 1.21 mycroft if (inb(iobase + com_ier))
641 1.62 mycroft SET(bits, TIOCM_LE);
642 1.21 mycroft *(int *)data = bits;
643 1.1 cgd break;
644 1.21 mycroft }
645 1.22 cgd case TIOCGFLAGS: {
646 1.62 mycroft int driverbits, userbits = 0;
647 1.22 cgd
648 1.62 mycroft driverbits = sc->sc_swflags;
649 1.62 mycroft if (ISSET(driverbits, COM_SW_SOFTCAR))
650 1.62 mycroft SET(userbits, TIOCFLAG_SOFTCAR);
651 1.62 mycroft if (ISSET(driverbits, COM_SW_CLOCAL))
652 1.62 mycroft SET(userbits, TIOCFLAG_CLOCAL);
653 1.62 mycroft if (ISSET(driverbits, COM_SW_CRTSCTS))
654 1.62 mycroft SET(userbits, TIOCFLAG_CRTSCTS);
655 1.62 mycroft if (ISSET(driverbits, COM_SW_MDMBUF))
656 1.62 mycroft SET(userbits, TIOCFLAG_MDMBUF);
657 1.22 cgd
658 1.62 mycroft *(int *)data = userbits;
659 1.22 cgd break;
660 1.22 cgd }
661 1.22 cgd case TIOCSFLAGS: {
662 1.22 cgd int userbits, driverbits = 0;
663 1.22 cgd
664 1.23 cgd error = suser(p->p_ucred, &p->p_acflag);
665 1.23 cgd if (error != 0)
666 1.23 cgd return(EPERM);
667 1.22 cgd
668 1.22 cgd userbits = *(int *)data;
669 1.62 mycroft if (ISSET(userbits, TIOCFLAG_SOFTCAR) ||
670 1.62 mycroft ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
671 1.62 mycroft SET(driverbits, COM_SW_SOFTCAR);
672 1.62 mycroft if (ISSET(userbits, TIOCFLAG_CLOCAL))
673 1.62 mycroft SET(driverbits, COM_SW_CLOCAL);
674 1.62 mycroft if (ISSET(userbits, TIOCFLAG_CRTSCTS))
675 1.62 mycroft SET(driverbits, COM_SW_CRTSCTS);
676 1.62 mycroft if (ISSET(userbits, TIOCFLAG_MDMBUF))
677 1.62 mycroft SET(driverbits, COM_SW_MDMBUF);
678 1.22 cgd
679 1.23 cgd sc->sc_swflags = driverbits;
680 1.22 cgd break;
681 1.22 cgd }
682 1.1 cgd default:
683 1.21 mycroft return ENOTTY;
684 1.1 cgd }
685 1.21 mycroft
686 1.21 mycroft return 0;
687 1.1 cgd }
688 1.1 cgd
689 1.21 mycroft int
690 1.1 cgd comparam(tp, t)
691 1.21 mycroft struct tty *tp;
692 1.21 mycroft struct termios *t;
693 1.1 cgd {
694 1.29 mycroft struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
695 1.41 mycroft int iobase = sc->sc_iobase;
696 1.21 mycroft int ospeed = comspeed(t->c_ospeed);
697 1.62 mycroft u_char lcr;
698 1.57 mycroft tcflag_t oldcflag;
699 1.21 mycroft int s;
700 1.21 mycroft
701 1.1 cgd /* check requested parameters */
702 1.21 mycroft if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
703 1.21 mycroft return EINVAL;
704 1.21 mycroft
705 1.66 mycroft lcr = ISSET(sc->sc_lcr, LCR_SBREAK);
706 1.62 mycroft
707 1.62 mycroft switch (ISSET(t->c_cflag, CSIZE)) {
708 1.1 cgd case CS5:
709 1.62 mycroft SET(lcr, LCR_5BITS);
710 1.21 mycroft break;
711 1.1 cgd case CS6:
712 1.62 mycroft SET(lcr, LCR_6BITS);
713 1.21 mycroft break;
714 1.1 cgd case CS7:
715 1.62 mycroft SET(lcr, LCR_7BITS);
716 1.21 mycroft break;
717 1.1 cgd case CS8:
718 1.62 mycroft SET(lcr, LCR_8BITS);
719 1.21 mycroft break;
720 1.1 cgd }
721 1.62 mycroft if (ISSET(t->c_cflag, PARENB)) {
722 1.62 mycroft SET(lcr, LCR_PENAB);
723 1.62 mycroft if (!ISSET(t->c_cflag, PARODD))
724 1.62 mycroft SET(lcr, LCR_PEVEN);
725 1.1 cgd }
726 1.62 mycroft if (ISSET(t->c_cflag, CSTOPB))
727 1.62 mycroft SET(lcr, LCR_STOPB);
728 1.62 mycroft
729 1.62 mycroft sc->sc_lcr = lcr;
730 1.1 cgd
731 1.21 mycroft s = spltty();
732 1.35 mycroft
733 1.62 mycroft if (ospeed == 0) {
734 1.62 mycroft CLR(sc->sc_mcr, MCR_DTR);
735 1.62 mycroft outb(iobase + com_mcr, sc->sc_mcr);
736 1.62 mycroft }
737 1.36 mycroft
738 1.36 mycroft /*
739 1.36 mycroft * Set the FIFO threshold based on the receive speed, if we are
740 1.36 mycroft * changing it.
741 1.36 mycroft */
742 1.66 mycroft #if 1
743 1.65 christos if (tp->t_ispeed != t->c_ispeed) {
744 1.66 mycroft #else
745 1.66 mycroft if (1) {
746 1.66 mycroft #endif
747 1.66 mycroft if (ospeed != 0) {
748 1.66 mycroft /*
749 1.66 mycroft * Make sure the transmit FIFO is empty before
750 1.67 mycroft * proceeding. If we don't do this, some revisions
751 1.67 mycroft * of the UART will hang. Interestingly enough,
752 1.67 mycroft * even if we do this will the last character is
753 1.67 mycroft * still being pushed out, they don't hang. This
754 1.67 mycroft * seems good enough.
755 1.66 mycroft */
756 1.66 mycroft while (ISSET(tp->t_state, TS_BUSY)) {
757 1.66 mycroft int error;
758 1.66 mycroft
759 1.66 mycroft ++sc->sc_halt;
760 1.66 mycroft error = ttysleep(tp, &tp->t_outq,
761 1.66 mycroft TTOPRI | PCATCH, "comprm", 0);
762 1.66 mycroft --sc->sc_halt;
763 1.66 mycroft if (error) {
764 1.66 mycroft splx(s);
765 1.66 mycroft comstart(tp);
766 1.66 mycroft return (error);
767 1.66 mycroft }
768 1.66 mycroft }
769 1.66 mycroft
770 1.66 mycroft outb(iobase + com_lcr, lcr | LCR_DLAB);
771 1.66 mycroft outb(iobase + com_dlbl, ospeed);
772 1.66 mycroft outb(iobase + com_dlbh, ospeed >> 8);
773 1.66 mycroft outb(iobase + com_lcr, lcr);
774 1.66 mycroft SET(sc->sc_mcr, MCR_DTR);
775 1.66 mycroft outb(iobase + com_mcr, sc->sc_mcr);
776 1.66 mycroft } else
777 1.66 mycroft outb(iobase + com_lcr, lcr);
778 1.66 mycroft
779 1.66 mycroft if (!ISSET(sc->sc_hwflags, COM_HW_HAYESP) &&
780 1.66 mycroft ISSET(sc->sc_hwflags, COM_HW_FIFO))
781 1.36 mycroft outb(iobase + com_fifo,
782 1.57 mycroft FIFO_ENABLE |
783 1.36 mycroft (t->c_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
784 1.57 mycroft } else
785 1.62 mycroft outb(iobase + com_lcr, lcr);
786 1.21 mycroft
787 1.22 cgd /* When not using CRTSCTS, RTS follows DTR. */
788 1.62 mycroft if (!ISSET(t->c_cflag, CRTSCTS)) {
789 1.62 mycroft if (ISSET(sc->sc_mcr, MCR_DTR)) {
790 1.62 mycroft if (!ISSET(sc->sc_mcr, MCR_RTS)) {
791 1.62 mycroft SET(sc->sc_mcr, MCR_RTS);
792 1.62 mycroft outb(iobase + com_mcr, sc->sc_mcr);
793 1.62 mycroft }
794 1.21 mycroft } else {
795 1.62 mycroft if (ISSET(sc->sc_mcr, MCR_RTS)) {
796 1.62 mycroft CLR(sc->sc_mcr, MCR_RTS);
797 1.62 mycroft outb(iobase + com_mcr, sc->sc_mcr);
798 1.62 mycroft }
799 1.21 mycroft }
800 1.57 mycroft sc->sc_dtr = MCR_DTR | MCR_RTS;
801 1.57 mycroft } else
802 1.57 mycroft sc->sc_dtr = MCR_DTR;
803 1.21 mycroft
804 1.57 mycroft /* and copy to tty */
805 1.57 mycroft tp->t_ispeed = t->c_ispeed;
806 1.57 mycroft tp->t_ospeed = t->c_ospeed;
807 1.57 mycroft oldcflag = tp->t_cflag;
808 1.57 mycroft tp->t_cflag = t->c_cflag;
809 1.25 cgd
810 1.25 cgd /*
811 1.57 mycroft * If DCD is off and MDMBUF is changed, ask the tty layer if we should
812 1.57 mycroft * stop the device.
813 1.25 cgd */
814 1.62 mycroft if (!ISSET(sc->sc_msr, MSR_DCD) &&
815 1.62 mycroft !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
816 1.62 mycroft ISSET(oldcflag, MDMBUF) != ISSET(tp->t_cflag, MDMBUF) &&
817 1.57 mycroft (*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
818 1.62 mycroft CLR(sc->sc_mcr, sc->sc_dtr);
819 1.62 mycroft outb(iobase + com_mcr, sc->sc_mcr);
820 1.21 mycroft }
821 1.1 cgd
822 1.66 mycroft /* Just to be sure... */
823 1.21 mycroft splx(s);
824 1.66 mycroft comstart(tp);
825 1.21 mycroft return 0;
826 1.1 cgd }
827 1.21 mycroft
828 1.12 deraadt void
829 1.1 cgd comstart(tp)
830 1.21 mycroft struct tty *tp;
831 1.1 cgd {
832 1.29 mycroft struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
833 1.41 mycroft int iobase = sc->sc_iobase;
834 1.21 mycroft int s;
835 1.21 mycroft
836 1.1 cgd s = spltty();
837 1.69 mycroft if (ISSET(tp->t_state, TS_BUSY))
838 1.70 mycroft goto out;
839 1.69 mycroft if (ISSET(tp->t_state, TS_TIMEOUT | TS_TTSTOP) ||
840 1.69 mycroft sc->sc_halt > 0)
841 1.69 mycroft goto stopped;
842 1.62 mycroft if (ISSET(tp->t_cflag, CRTSCTS) && !ISSET(sc->sc_msr, MSR_CTS))
843 1.69 mycroft goto stopped;
844 1.61 mycroft if (tp->t_outq.c_cc <= tp->t_lowat) {
845 1.62 mycroft if (ISSET(tp->t_state, TS_ASLEEP)) {
846 1.62 mycroft CLR(tp->t_state, TS_ASLEEP);
847 1.62 mycroft wakeup(&tp->t_outq);
848 1.61 mycroft }
849 1.61 mycroft if (tp->t_outq.c_cc == 0)
850 1.69 mycroft goto stopped;
851 1.61 mycroft selwakeup(&tp->t_wsel);
852 1.61 mycroft }
853 1.62 mycroft SET(tp->t_state, TS_BUSY);
854 1.64 christos
855 1.70 mycroft if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
856 1.70 mycroft SET(sc->sc_ier, IER_ETXRDY);
857 1.70 mycroft outb(iobase + com_ier, sc->sc_ier);
858 1.70 mycroft }
859 1.65 christos #ifdef COM_HAYESP
860 1.65 christos if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
861 1.64 christos u_char buffer[1024], *cp = buffer;
862 1.64 christos int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
863 1.64 christos do
864 1.64 christos outb(iobase + com_data, *cp++);
865 1.64 christos while (--n);
866 1.64 christos }
867 1.65 christos else
868 1.65 christos #endif
869 1.65 christos if (ISSET(sc->sc_hwflags, COM_HW_FIFO)) {
870 1.21 mycroft u_char buffer[16], *cp = buffer;
871 1.21 mycroft int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
872 1.21 mycroft do {
873 1.21 mycroft outb(iobase + com_data, *cp++);
874 1.21 mycroft } while (--n);
875 1.21 mycroft } else
876 1.21 mycroft outb(iobase + com_data, getc(&tp->t_outq));
877 1.70 mycroft out:
878 1.69 mycroft splx(s);
879 1.69 mycroft return;
880 1.69 mycroft stopped:
881 1.69 mycroft if (ISSET(sc->sc_ier, IER_ETXRDY)) {
882 1.69 mycroft CLR(sc->sc_ier, IER_ETXRDY);
883 1.69 mycroft outb(iobase + com_ier, sc->sc_ier);
884 1.69 mycroft }
885 1.1 cgd splx(s);
886 1.1 cgd }
887 1.21 mycroft
888 1.1 cgd /*
889 1.1 cgd * Stop output on a line.
890 1.1 cgd */
891 1.21 mycroft void
892 1.1 cgd comstop(tp, flag)
893 1.21 mycroft struct tty *tp;
894 1.1 cgd {
895 1.21 mycroft int s;
896 1.1 cgd
897 1.1 cgd s = spltty();
898 1.62 mycroft if (ISSET(tp->t_state, TS_BUSY))
899 1.62 mycroft if (!ISSET(tp->t_state, TS_TTSTOP))
900 1.62 mycroft SET(tp->t_state, TS_FLUSH);
901 1.1 cgd splx(s);
902 1.1 cgd }
903 1.1 cgd
904 1.33 mycroft void
905 1.33 mycroft comdiag(arg)
906 1.33 mycroft void *arg;
907 1.33 mycroft {
908 1.33 mycroft struct com_softc *sc = arg;
909 1.57 mycroft int overflows, floods;
910 1.33 mycroft int s;
911 1.33 mycroft
912 1.33 mycroft s = spltty();
913 1.57 mycroft sc->sc_errors = 0;
914 1.33 mycroft overflows = sc->sc_overflows;
915 1.33 mycroft sc->sc_overflows = 0;
916 1.57 mycroft floods = sc->sc_floods;
917 1.57 mycroft sc->sc_floods = 0;
918 1.57 mycroft splx(s);
919 1.57 mycroft
920 1.57 mycroft log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf overflow%s\n",
921 1.57 mycroft sc->sc_dev.dv_xname,
922 1.57 mycroft overflows, overflows == 1 ? "" : "s",
923 1.57 mycroft floods, floods == 1 ? "" : "s");
924 1.57 mycroft }
925 1.57 mycroft
926 1.57 mycroft void
927 1.57 mycroft compoll(arg)
928 1.57 mycroft void *arg;
929 1.57 mycroft {
930 1.57 mycroft int unit;
931 1.57 mycroft struct com_softc *sc;
932 1.57 mycroft struct tty *tp;
933 1.57 mycroft register u_char *ibufp;
934 1.57 mycroft u_char *ibufend;
935 1.57 mycroft register int c;
936 1.57 mycroft int s;
937 1.57 mycroft static int lsrmap[8] = {
938 1.57 mycroft 0, TTY_PE,
939 1.57 mycroft TTY_FE, TTY_PE|TTY_FE,
940 1.57 mycroft TTY_FE, TTY_PE|TTY_FE,
941 1.57 mycroft TTY_FE, TTY_PE|TTY_FE
942 1.57 mycroft };
943 1.57 mycroft
944 1.57 mycroft s = spltty();
945 1.57 mycroft if (comevents == 0) {
946 1.57 mycroft splx(s);
947 1.57 mycroft goto out;
948 1.57 mycroft }
949 1.57 mycroft comevents = 0;
950 1.33 mycroft splx(s);
951 1.33 mycroft
952 1.57 mycroft for (unit = 0; unit < comcd.cd_ndevs; unit++) {
953 1.57 mycroft sc = comcd.cd_devs[unit];
954 1.57 mycroft if (sc == 0 || sc->sc_ibufp == sc->sc_ibuf)
955 1.57 mycroft continue;
956 1.57 mycroft
957 1.57 mycroft tp = sc->sc_tty;
958 1.57 mycroft
959 1.57 mycroft s = spltty();
960 1.57 mycroft
961 1.57 mycroft ibufp = sc->sc_ibuf;
962 1.57 mycroft ibufend = sc->sc_ibufp;
963 1.61 mycroft
964 1.61 mycroft if (ibufp == ibufend) {
965 1.61 mycroft splx(s);
966 1.61 mycroft continue;
967 1.61 mycroft }
968 1.57 mycroft
969 1.57 mycroft sc->sc_ibufp = sc->sc_ibuf = (ibufp == sc->sc_ibufs[0]) ?
970 1.57 mycroft sc->sc_ibufs[1] : sc->sc_ibufs[0];
971 1.57 mycroft sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
972 1.57 mycroft sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
973 1.57 mycroft
974 1.62 mycroft if (tp == 0 || !ISSET(tp->t_state, TS_ISOPEN)) {
975 1.57 mycroft splx(s);
976 1.57 mycroft continue;
977 1.57 mycroft }
978 1.57 mycroft
979 1.62 mycroft if (ISSET(tp->t_cflag, CRTSCTS) &&
980 1.62 mycroft !ISSET(sc->sc_mcr, MCR_RTS)) {
981 1.62 mycroft /* XXX */
982 1.62 mycroft SET(sc->sc_mcr, MCR_RTS);
983 1.62 mycroft outb(sc->sc_iobase + com_mcr, sc->sc_mcr);
984 1.62 mycroft }
985 1.57 mycroft
986 1.57 mycroft splx(s);
987 1.57 mycroft
988 1.57 mycroft while (ibufp < ibufend) {
989 1.57 mycroft c = *ibufp++;
990 1.57 mycroft if (*ibufp & LSR_OE) {
991 1.57 mycroft sc->sc_overflows++;
992 1.57 mycroft if (sc->sc_errors++ == 0)
993 1.57 mycroft timeout(comdiag, sc, 60 * hz);
994 1.57 mycroft }
995 1.57 mycroft /* This is ugly, but fast. */
996 1.57 mycroft c |= lsrmap[(*ibufp++ & (LSR_BI|LSR_FE|LSR_PE)) >> 2];
997 1.57 mycroft (*linesw[tp->t_line].l_rint)(c, tp);
998 1.57 mycroft }
999 1.57 mycroft }
1000 1.57 mycroft
1001 1.57 mycroft out:
1002 1.57 mycroft timeout(compoll, NULL, 1);
1003 1.21 mycroft }
1004 1.1 cgd
1005 1.21 mycroft int
1006 1.49 cgd comintr(arg)
1007 1.49 cgd void *arg;
1008 1.21 mycroft {
1009 1.49 cgd struct com_softc *sc = arg;
1010 1.41 mycroft int iobase = sc->sc_iobase;
1011 1.21 mycroft struct tty *tp;
1012 1.55 mycroft u_char lsr, data, msr, delta;
1013 1.69 mycroft #ifdef COM_DEBUG
1014 1.72 mycroft int n;
1015 1.69 mycroft struct {
1016 1.72 mycroft u_char iir, lsr, msr;
1017 1.69 mycroft } iter[32];
1018 1.69 mycroft #endif
1019 1.55 mycroft
1020 1.72 mycroft #ifdef COM_DEBUG
1021 1.72 mycroft n = 0;
1022 1.72 mycroft if (ISSET(iter[n].iir = inb(iobase + com_iir), IIR_NOPEND))
1023 1.72 mycroft return (0);
1024 1.72 mycroft #else
1025 1.62 mycroft if (ISSET(inb(iobase + com_iir), IIR_NOPEND))
1026 1.55 mycroft return (0);
1027 1.72 mycroft #endif
1028 1.21 mycroft
1029 1.55 mycroft tp = sc->sc_tty;
1030 1.21 mycroft
1031 1.21 mycroft for (;;) {
1032 1.69 mycroft #ifdef COM_DEBUG
1033 1.69 mycroft iter[n].lsr =
1034 1.69 mycroft #endif
1035 1.55 mycroft lsr = inb(iobase + com_lsr);
1036 1.55 mycroft
1037 1.69 mycroft if (ISSET(lsr, LSR_RXRDY)) {
1038 1.57 mycroft register u_char *p = sc->sc_ibufp;
1039 1.57 mycroft
1040 1.57 mycroft comevents = 1;
1041 1.55 mycroft do {
1042 1.69 mycroft data = inb(iobase + com_data);
1043 1.62 mycroft if (ISSET(lsr, LSR_BI)) {
1044 1.72 mycroft #ifdef notdef
1045 1.69 mycroft printf("break %02x %02x %02x %02x\n",
1046 1.69 mycroft sc->sc_msr, sc->sc_mcr, sc->sc_lcr,
1047 1.69 mycroft sc->sc_dtr);
1048 1.69 mycroft #endif
1049 1.57 mycroft #ifdef DDB
1050 1.58 mycroft if (sc->sc_dev.dv_unit == comconsole) {
1051 1.58 mycroft Debugger();
1052 1.58 mycroft goto next;
1053 1.59 mycroft }
1054 1.21 mycroft #endif
1055 1.62 mycroft }
1056 1.57 mycroft if (p >= sc->sc_ibufend) {
1057 1.57 mycroft sc->sc_floods++;
1058 1.57 mycroft if (sc->sc_errors++ == 0)
1059 1.57 mycroft timeout(comdiag, sc, 60 * hz);
1060 1.57 mycroft } else {
1061 1.57 mycroft *p++ = data;
1062 1.57 mycroft *p++ = lsr;
1063 1.57 mycroft if (p == sc->sc_ibufhigh &&
1064 1.62 mycroft ISSET(tp->t_cflag, CRTSCTS)) {
1065 1.62 mycroft /* XXX */
1066 1.62 mycroft CLR(sc->sc_mcr, MCR_RTS);
1067 1.57 mycroft outb(iobase + com_mcr,
1068 1.62 mycroft sc->sc_mcr);
1069 1.62 mycroft }
1070 1.55 mycroft }
1071 1.57 mycroft next:
1072 1.69 mycroft #ifdef COM_DEBUG
1073 1.69 mycroft if (++n >= 32)
1074 1.69 mycroft goto ohfudge;
1075 1.69 mycroft iter[n].lsr =
1076 1.69 mycroft #endif
1077 1.55 mycroft lsr = inb(iobase + com_lsr);
1078 1.69 mycroft } while (ISSET(lsr, LSR_RXRDY));
1079 1.57 mycroft
1080 1.57 mycroft sc->sc_ibufp = p;
1081 1.55 mycroft }
1082 1.69 mycroft #ifdef COM_DEBUG
1083 1.62 mycroft else if (ISSET(lsr, LSR_BI|LSR_FE|LSR_PE|LSR_OE))
1084 1.62 mycroft printf("weird lsr %02x\n", lsr);
1085 1.62 mycroft #endif
1086 1.55 mycroft
1087 1.69 mycroft #ifdef COM_DEBUG
1088 1.69 mycroft iter[n].msr =
1089 1.69 mycroft #endif
1090 1.55 mycroft msr = inb(iobase + com_msr);
1091 1.55 mycroft
1092 1.55 mycroft if (msr != sc->sc_msr) {
1093 1.55 mycroft delta = msr ^ sc->sc_msr;
1094 1.55 mycroft sc->sc_msr = msr;
1095 1.62 mycroft if (ISSET(delta, MSR_DCD) &&
1096 1.62 mycroft !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
1097 1.62 mycroft (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD)) == 0) {
1098 1.62 mycroft CLR(sc->sc_mcr, sc->sc_dtr);
1099 1.62 mycroft outb(iobase + com_mcr, sc->sc_mcr);
1100 1.55 mycroft }
1101 1.62 mycroft if (ISSET(delta & msr, MSR_CTS) &&
1102 1.62 mycroft ISSET(tp->t_cflag, CRTSCTS)) {
1103 1.55 mycroft /* the line is up and we want to do rts/cts flow control */
1104 1.57 mycroft (*linesw[tp->t_line].l_start)(tp);
1105 1.55 mycroft }
1106 1.55 mycroft }
1107 1.55 mycroft
1108 1.62 mycroft if (ISSET(lsr, LSR_TXRDY) && ISSET(tp->t_state, TS_BUSY)) {
1109 1.68 mycroft CLR(tp->t_state, TS_BUSY | TS_FLUSH);
1110 1.66 mycroft if (sc->sc_halt > 0)
1111 1.66 mycroft wakeup(&tp->t_outq);
1112 1.69 mycroft (*linesw[tp->t_line].l_start)(tp);
1113 1.62 mycroft }
1114 1.62 mycroft
1115 1.69 mycroft #ifdef COM_DEBUG
1116 1.69 mycroft if (++n >= 32)
1117 1.69 mycroft goto ohfudge;
1118 1.72 mycroft if (ISSET(iter[n].iir = inb(iobase + com_iir), IIR_NOPEND))
1119 1.72 mycroft return (1);
1120 1.72 mycroft #else
1121 1.72 mycroft if (ISSET(inb(iobase + com_iir), IIR_NOPEND))
1122 1.72 mycroft return (1);
1123 1.69 mycroft #endif
1124 1.1 cgd }
1125 1.69 mycroft #ifdef COM_DEBUG
1126 1.69 mycroft ohfudge:
1127 1.69 mycroft printf("comintr: too many iterations");
1128 1.69 mycroft for (n = 0; n < 32; n++) {
1129 1.69 mycroft if ((n % 4) == 0)
1130 1.72 mycroft printf("\ncomintr: iter[%02d]", n);
1131 1.72 mycroft printf(" %02x %02x %02x", iter[n].iir, iter[n].lsr, iter[n].msr);
1132 1.69 mycroft }
1133 1.69 mycroft printf("\n");
1134 1.72 mycroft printf("comintr: msr %02x mcr %02x lcr %02x ier %02x\n",
1135 1.72 mycroft sc->sc_msr, sc->sc_mcr, sc->sc_lcr, sc->sc_ier);
1136 1.72 mycroft printf("comintr: state %08x cc %d\n", sc->sc_tty->t_state,
1137 1.72 mycroft sc->sc_tty->t_outq.c_cc);
1138 1.69 mycroft #endif
1139 1.1 cgd }
1140 1.1 cgd
1141 1.1 cgd /*
1142 1.1 cgd * Following are all routines needed for COM to act as console
1143 1.1 cgd */
1144 1.17 cgd #include <dev/cons.h>
1145 1.1 cgd
1146 1.48 mycroft void
1147 1.1 cgd comcnprobe(cp)
1148 1.1 cgd struct consdev *cp;
1149 1.1 cgd {
1150 1.21 mycroft
1151 1.21 mycroft if (!comprobe1(CONADDR)) {
1152 1.21 mycroft cp->cn_pri = CN_DEAD;
1153 1.21 mycroft return;
1154 1.21 mycroft }
1155 1.1 cgd
1156 1.1 cgd /* locate the major number */
1157 1.1 cgd for (commajor = 0; commajor < nchrdev; commajor++)
1158 1.1 cgd if (cdevsw[commajor].d_open == comopen)
1159 1.1 cgd break;
1160 1.1 cgd
1161 1.1 cgd /* initialize required fields */
1162 1.29 mycroft cp->cn_dev = makedev(commajor, CONUNIT);
1163 1.1 cgd #ifdef COMCONSOLE
1164 1.1 cgd cp->cn_pri = CN_REMOTE; /* Force a serial port console */
1165 1.1 cgd #else
1166 1.1 cgd cp->cn_pri = CN_NORMAL;
1167 1.1 cgd #endif
1168 1.1 cgd }
1169 1.1 cgd
1170 1.48 mycroft void
1171 1.1 cgd comcninit(cp)
1172 1.1 cgd struct consdev *cp;
1173 1.1 cgd {
1174 1.1 cgd
1175 1.29 mycroft cominit(CONUNIT, comdefaultrate);
1176 1.29 mycroft comconsole = CONUNIT;
1177 1.21 mycroft comconsinit = 0;
1178 1.1 cgd }
1179 1.1 cgd
1180 1.1 cgd cominit(unit, rate)
1181 1.1 cgd int unit, rate;
1182 1.1 cgd {
1183 1.21 mycroft int s = splhigh();
1184 1.41 mycroft int iobase = CONADDR;
1185 1.21 mycroft u_char stat;
1186 1.1 cgd
1187 1.62 mycroft outb(iobase + com_lcr, LCR_DLAB);
1188 1.21 mycroft rate = comspeed(comdefaultrate);
1189 1.21 mycroft outb(iobase + com_dlbl, rate);
1190 1.21 mycroft outb(iobase + com_dlbh, rate >> 8);
1191 1.62 mycroft outb(iobase + com_lcr, LCR_8BITS);
1192 1.21 mycroft outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY);
1193 1.21 mycroft outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
1194 1.21 mycroft stat = inb(iobase + com_iir);
1195 1.1 cgd splx(s);
1196 1.1 cgd }
1197 1.1 cgd
1198 1.1 cgd comcngetc(dev)
1199 1.21 mycroft dev_t dev;
1200 1.1 cgd {
1201 1.21 mycroft int s = splhigh();
1202 1.41 mycroft int iobase = CONADDR;
1203 1.21 mycroft u_char stat, c;
1204 1.1 cgd
1205 1.62 mycroft while (!ISSET(stat = inb(iobase + com_lsr), LSR_RXRDY))
1206 1.1 cgd ;
1207 1.21 mycroft c = inb(iobase + com_data);
1208 1.21 mycroft stat = inb(iobase + com_iir);
1209 1.1 cgd splx(s);
1210 1.21 mycroft return c;
1211 1.1 cgd }
1212 1.1 cgd
1213 1.1 cgd /*
1214 1.1 cgd * Console kernel output character routine.
1215 1.1 cgd */
1216 1.48 mycroft void
1217 1.1 cgd comcnputc(dev, c)
1218 1.1 cgd dev_t dev;
1219 1.21 mycroft int c;
1220 1.1 cgd {
1221 1.21 mycroft int s = splhigh();
1222 1.41 mycroft int iobase = CONADDR;
1223 1.21 mycroft u_char stat;
1224 1.1 cgd register int timo;
1225 1.1 cgd
1226 1.1 cgd #ifdef KGDB
1227 1.1 cgd if (dev != kgdb_dev)
1228 1.1 cgd #endif
1229 1.1 cgd if (comconsinit == 0) {
1230 1.21 mycroft (void) cominit(COMUNIT(dev), comdefaultrate);
1231 1.1 cgd comconsinit = 1;
1232 1.1 cgd }
1233 1.1 cgd /* wait for any pending transmission to finish */
1234 1.1 cgd timo = 50000;
1235 1.62 mycroft while (!ISSET(stat = inb(iobase + com_lsr), LSR_TXRDY) && --timo)
1236 1.1 cgd ;
1237 1.21 mycroft outb(iobase + com_data, c);
1238 1.1 cgd /* wait for this transmission to complete */
1239 1.1 cgd timo = 1500000;
1240 1.62 mycroft while (!ISSET(stat = inb(iobase + com_lsr), LSR_TXRDY) && --timo)
1241 1.1 cgd ;
1242 1.1 cgd /* clear any interrupts generated by this transmission */
1243 1.21 mycroft stat = inb(iobase + com_iir);
1244 1.1 cgd splx(s);
1245 1.37 mycroft }
1246 1.37 mycroft
1247 1.37 mycroft void
1248 1.37 mycroft comcnpollc(dev, on)
1249 1.37 mycroft dev_t dev;
1250 1.37 mycroft int on;
1251 1.37 mycroft {
1252 1.37 mycroft
1253 1.2 cgd }
1254