com.c revision 1.96 1 1.96 mycroft /* $NetBSD: com.c,v 1.96 1996/12/14 10:46:38 mycroft 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.82 mycroft #include <machine/bus.h>
60 1.78 cgd #include <machine/intr.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.77 cgd #include "com.h"
72 1.77 cgd
73 1.80 christos
74 1.64 christos #define COM_IBUFSIZE (2 * 512)
75 1.57 mycroft #define COM_IHIGHWATER ((3 * COM_IBUFSIZE) / 4)
76 1.57 mycroft
77 1.21 mycroft struct com_softc {
78 1.21 mycroft struct device sc_dev;
79 1.49 cgd void *sc_ih;
80 1.50 mycroft struct tty *sc_tty;
81 1.1 cgd
82 1.33 mycroft int sc_overflows;
83 1.57 mycroft int sc_floods;
84 1.88 mycroft int sc_failures;
85 1.57 mycroft int sc_errors;
86 1.57 mycroft
87 1.66 mycroft int sc_halt;
88 1.66 mycroft
89 1.41 mycroft int sc_iobase;
90 1.65 christos #ifdef COM_HAYESP
91 1.65 christos int sc_hayespbase;
92 1.65 christos #endif
93 1.75 cgd
94 1.91 thorpej bus_space_tag_t sc_iot;
95 1.91 thorpej bus_space_handle_t sc_ioh;
96 1.91 thorpej bus_space_handle_t sc_hayespioh;
97 1.75 cgd
98 1.22 cgd u_char sc_hwflags;
99 1.29 mycroft #define COM_HW_NOIEN 0x01
100 1.22 cgd #define COM_HW_FIFO 0x02
101 1.65 christos #define COM_HW_HAYESP 0x04
102 1.22 cgd #define COM_HW_CONSOLE 0x40
103 1.22 cgd u_char sc_swflags;
104 1.22 cgd #define COM_SW_SOFTCAR 0x01
105 1.22 cgd #define COM_SW_CLOCAL 0x02
106 1.22 cgd #define COM_SW_CRTSCTS 0x04
107 1.25 cgd #define COM_SW_MDMBUF 0x08
108 1.69 mycroft u_char sc_msr, sc_mcr, sc_lcr, sc_ier;
109 1.57 mycroft u_char sc_dtr;
110 1.57 mycroft
111 1.57 mycroft u_char *sc_ibuf, *sc_ibufp, *sc_ibufhigh, *sc_ibufend;
112 1.57 mycroft u_char sc_ibufs[2][COM_IBUFSIZE];
113 1.29 mycroft };
114 1.21 mycroft
115 1.75 cgd #ifdef COM_HAYESP
116 1.91 thorpej int comprobeHAYESP __P((bus_space_handle_t hayespioh, struct com_softc *sc));
117 1.75 cgd #endif
118 1.80 christos void comdiag __P((void *));
119 1.80 christos int comspeed __P((long));
120 1.80 christos int comparam __P((struct tty *, struct termios *));
121 1.80 christos void comstart __P((struct tty *));
122 1.86 mycroft void comsoft __P((void *));
123 1.80 christos
124 1.80 christos /* XXX: These belong elsewhere */
125 1.80 christos cdev_decl(com);
126 1.80 christos bdev_decl(com);
127 1.80 christos
128 1.80 christos struct consdev;
129 1.80 christos void comcnprobe __P((struct consdev *));
130 1.80 christos void comcninit __P((struct consdev *));
131 1.80 christos int comcngetc __P((dev_t));
132 1.80 christos void comcnputc __P((dev_t, int));
133 1.80 christos void comcnpollc __P((dev_t, int));
134 1.80 christos
135 1.80 christos static u_char tiocm_xxx2mcr __P((int));
136 1.1 cgd
137 1.76 thorpej /*
138 1.76 thorpej * XXX the following two cfattach structs should be different, and possibly
139 1.76 thorpej * XXX elsewhere.
140 1.76 thorpej */
141 1.94 cgd #ifdef __BROKEN_INDIRECT_CONFIG
142 1.77 cgd int comprobe __P((struct device *, void *, void *));
143 1.94 cgd #else
144 1.94 cgd int comprobe __P((struct device *, struct cfdata *, void *));
145 1.94 cgd #endif
146 1.77 cgd void comattach __P((struct device *, struct device *, void *));
147 1.77 cgd
148 1.77 cgd #if NCOM_ISA
149 1.76 thorpej struct cfattach com_isa_ca = {
150 1.76 thorpej sizeof(struct com_softc), comprobe, comattach
151 1.76 thorpej };
152 1.77 cgd #endif
153 1.76 thorpej
154 1.77 cgd #if NCOM_COMMULTI
155 1.77 cgd struct cfattach com_commulti_ca = {
156 1.76 thorpej sizeof(struct com_softc), comprobe, comattach
157 1.76 thorpej };
158 1.77 cgd #endif
159 1.75 cgd
160 1.76 thorpej struct cfdriver com_cd = {
161 1.76 thorpej NULL, "com", DV_TTY
162 1.1 cgd };
163 1.76 thorpej
164 1.1 cgd #ifdef COMCONSOLE
165 1.75 cgd int comdefaultrate = CONSPEED; /* XXX why set default? */
166 1.1 cgd #else
167 1.66 mycroft int comdefaultrate = TTYDEF_SPEED;
168 1.1 cgd #endif
169 1.75 cgd int comconsaddr;
170 1.75 cgd int comconsattached;
171 1.91 thorpej bus_space_tag_t comconstag;
172 1.91 thorpej bus_space_handle_t comconsbah;
173 1.79 cgd tcflag_t comconscflag = TTYDEF_CFLAG;
174 1.75 cgd
175 1.1 cgd int commajor;
176 1.57 mycroft int comsopen = 0;
177 1.57 mycroft int comevents = 0;
178 1.1 cgd
179 1.1 cgd #ifdef KGDB
180 1.14 mycroft #include <machine/remote-sl.h>
181 1.1 cgd extern int kgdb_dev;
182 1.1 cgd extern int kgdb_rate;
183 1.1 cgd extern int kgdb_debug_init;
184 1.1 cgd #endif
185 1.1 cgd
186 1.21 mycroft #define COMUNIT(x) (minor(x))
187 1.1 cgd
188 1.62 mycroft /* Macros to clear/set/test flags. */
189 1.62 mycroft #define SET(t, f) (t) |= (f)
190 1.62 mycroft #define CLR(t, f) (t) &= ~(f)
191 1.62 mycroft #define ISSET(t, f) ((t) & (f))
192 1.21 mycroft
193 1.21 mycroft int
194 1.21 mycroft comspeed(speed)
195 1.21 mycroft long speed;
196 1.1 cgd {
197 1.21 mycroft #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
198 1.21 mycroft
199 1.21 mycroft int x, err;
200 1.21 mycroft
201 1.21 mycroft if (speed == 0)
202 1.21 mycroft return 0;
203 1.21 mycroft if (speed < 0)
204 1.21 mycroft return -1;
205 1.21 mycroft x = divrnd((COM_FREQ / 16), speed);
206 1.21 mycroft if (x <= 0)
207 1.21 mycroft return -1;
208 1.21 mycroft err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
209 1.21 mycroft if (err < 0)
210 1.21 mycroft err = -err;
211 1.21 mycroft if (err > COM_TOLERANCE)
212 1.21 mycroft return -1;
213 1.21 mycroft return x;
214 1.21 mycroft
215 1.21 mycroft #undef divrnd(n, q)
216 1.21 mycroft }
217 1.21 mycroft
218 1.21 mycroft int
219 1.91 thorpej comprobe1(iot, ioh, iobase)
220 1.91 thorpej bus_space_tag_t iot;
221 1.91 thorpej bus_space_handle_t ioh;
222 1.41 mycroft int iobase;
223 1.21 mycroft {
224 1.21 mycroft
225 1.1 cgd /* force access to id reg */
226 1.91 thorpej bus_space_write_1(iot, ioh, com_lcr, 0);
227 1.91 thorpej bus_space_write_1(iot, ioh, com_iir, 0);
228 1.91 thorpej if (bus_space_read_1(iot, ioh, com_iir) & 0x38)
229 1.21 mycroft return 0;
230 1.21 mycroft
231 1.21 mycroft return 1;
232 1.1 cgd }
233 1.1 cgd
234 1.65 christos #ifdef COM_HAYESP
235 1.65 christos int
236 1.75 cgd comprobeHAYESP(hayespioh, sc)
237 1.91 thorpej bus_space_handle_t hayespioh;
238 1.64 christos struct com_softc *sc;
239 1.64 christos {
240 1.65 christos char val, dips;
241 1.64 christos int combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
242 1.91 thorpej bus_space_tag_t iot = sc->sc_iot;
243 1.64 christos
244 1.64 christos /*
245 1.64 christos * Hayes ESP cards have two iobases. One is for compatibility with
246 1.64 christos * 16550 serial chips, and at the same ISA PC base addresses. The
247 1.64 christos * other is for ESP-specific enhanced features, and lies at a
248 1.65 christos * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
249 1.64 christos */
250 1.64 christos
251 1.64 christos /* Test for ESP signature */
252 1.91 thorpej if ((bus_space_read_1(iot, hayespioh, 0) & 0xf3) == 0)
253 1.65 christos return 0;
254 1.64 christos
255 1.64 christos /*
256 1.64 christos * ESP is present at ESP enhanced base address; unknown com port
257 1.64 christos */
258 1.64 christos
259 1.64 christos /* Get the dip-switch configurations */
260 1.91 thorpej bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETDIPS);
261 1.91 thorpej dips = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1);
262 1.64 christos
263 1.64 christos /* Determine which com port this ESP card services: bits 0,1 of */
264 1.64 christos /* dips is the port # (0-3); combaselist[val] is the com_iobase */
265 1.65 christos if (sc->sc_iobase != combaselist[dips & 0x03])
266 1.65 christos return 0;
267 1.65 christos
268 1.90 christos printf(": ESP");
269 1.64 christos
270 1.65 christos /* Check ESP Self Test bits. */
271 1.64 christos /* Check for ESP version 2.0: bits 4,5,6 == 010 */
272 1.91 thorpej bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETTEST);
273 1.91 thorpej val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1); /* Clear reg 1 */
274 1.91 thorpej val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS2);
275 1.64 christos if ((val & 0x70) < 0x20) {
276 1.90 christos printf("-old (%o)", val & 0x70);
277 1.64 christos /* we do not support the necessary features */
278 1.65 christos return 0;
279 1.64 christos }
280 1.64 christos
281 1.64 christos /* Check for ability to emulate 16550: bit 8 == 1 */
282 1.64 christos if ((dips & 0x80) == 0) {
283 1.90 christos printf(" slave");
284 1.64 christos /* XXX Does slave really mean no 16550 support?? */
285 1.65 christos return 0;
286 1.64 christos }
287 1.64 christos
288 1.64 christos /*
289 1.64 christos * If we made it this far, we are a full-featured ESP v2.0 (or
290 1.64 christos * better), at the correct com port address.
291 1.64 christos */
292 1.64 christos
293 1.65 christos SET(sc->sc_hwflags, COM_HW_HAYESP);
294 1.90 christos printf(", 1024 byte fifo\n");
295 1.65 christos return 1;
296 1.64 christos }
297 1.65 christos #endif
298 1.64 christos
299 1.21 mycroft int
300 1.40 mycroft comprobe(parent, match, aux)
301 1.40 mycroft struct device *parent;
302 1.94 cgd #ifdef __BROKEN_INDIRECT_CONFIG
303 1.94 cgd void *match;
304 1.94 cgd #else
305 1.94 cgd struct cfdata *match;
306 1.94 cgd #endif
307 1.94 cgd void *aux;
308 1.29 mycroft {
309 1.91 thorpej bus_space_tag_t iot;
310 1.91 thorpej bus_space_handle_t ioh;
311 1.75 cgd int iobase, needioh;
312 1.75 cgd int rv = 1;
313 1.75 cgd
314 1.76 thorpej /*
315 1.76 thorpej * XXX should be broken out into functions for isa probe and
316 1.76 thorpej * XXX for commulti probe, with a helper function that contains
317 1.76 thorpej * XXX most of the interesting stuff.
318 1.76 thorpej */
319 1.77 cgd #if NCOM_ISA
320 1.75 cgd if (!strcmp(parent->dv_cfdata->cf_driver->cd_name, "isa")) {
321 1.75 cgd struct isa_attach_args *ia = aux;
322 1.75 cgd
323 1.91 thorpej iot = ia->ia_iot;
324 1.75 cgd iobase = ia->ia_iobase;
325 1.75 cgd needioh = 1;
326 1.77 cgd } else
327 1.77 cgd #endif
328 1.77 cgd #if NCOM_COMMULTI
329 1.77 cgd if (1) {
330 1.80 christos struct cfdata *cf = match;
331 1.75 cgd struct commulti_attach_args *ca = aux;
332 1.75 cgd
333 1.75 cgd if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != ca->ca_slave)
334 1.75 cgd return (0);
335 1.75 cgd
336 1.91 thorpej iot = ca->ca_iot;
337 1.75 cgd iobase = ca->ca_iobase;
338 1.75 cgd ioh = ca->ca_ioh;
339 1.75 cgd needioh = 0;
340 1.77 cgd } else
341 1.77 cgd #endif
342 1.77 cgd return(0); /* This cannot happen */
343 1.75 cgd
344 1.75 cgd /* if it's in use as console, it's there. */
345 1.75 cgd if (iobase == comconsaddr && !comconsattached)
346 1.75 cgd goto out;
347 1.75 cgd
348 1.91 thorpej if (needioh && bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
349 1.75 cgd rv = 0;
350 1.75 cgd goto out;
351 1.75 cgd }
352 1.91 thorpej rv = comprobe1(iot, ioh, iobase);
353 1.75 cgd if (needioh)
354 1.91 thorpej bus_space_unmap(iot, ioh, COM_NPORTS);
355 1.21 mycroft
356 1.75 cgd out:
357 1.77 cgd #if NCOM_ISA
358 1.75 cgd if (rv && !strcmp(parent->dv_cfdata->cf_driver->cd_name, "isa")) {
359 1.75 cgd struct isa_attach_args *ia = aux;
360 1.21 mycroft
361 1.75 cgd ia->ia_iosize = COM_NPORTS;
362 1.75 cgd ia->ia_msize = 0;
363 1.75 cgd }
364 1.77 cgd #endif
365 1.75 cgd return (rv);
366 1.21 mycroft }
367 1.1 cgd
368 1.29 mycroft void
369 1.29 mycroft comattach(parent, self, aux)
370 1.29 mycroft struct device *parent, *self;
371 1.29 mycroft void *aux;
372 1.29 mycroft {
373 1.29 mycroft struct com_softc *sc = (void *)self;
374 1.75 cgd int iobase, irq;
375 1.91 thorpej bus_space_tag_t iot;
376 1.91 thorpej bus_space_handle_t ioh;
377 1.65 christos #ifdef COM_HAYESP
378 1.65 christos int hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
379 1.65 christos int *hayespp;
380 1.65 christos #endif
381 1.1 cgd
382 1.76 thorpej /*
383 1.76 thorpej * XXX should be broken out into functions for isa attach and
384 1.76 thorpej * XXX for commulti attach, with a helper function that contains
385 1.76 thorpej * XXX most of the interesting stuff.
386 1.76 thorpej */
387 1.75 cgd sc->sc_hwflags = 0;
388 1.75 cgd sc->sc_swflags = 0;
389 1.77 cgd #if NCOM_ISA
390 1.75 cgd if (!strcmp(parent->dv_cfdata->cf_driver->cd_name, "isa")) {
391 1.75 cgd struct isa_attach_args *ia = aux;
392 1.75 cgd
393 1.75 cgd /*
394 1.75 cgd * We're living on an isa.
395 1.75 cgd */
396 1.75 cgd iobase = ia->ia_iobase;
397 1.91 thorpej iot = ia->ia_iot;
398 1.75 cgd if (iobase != comconsaddr) {
399 1.91 thorpej if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
400 1.75 cgd panic("comattach: io mapping failed");
401 1.75 cgd } else
402 1.91 thorpej ioh = comconsbah;
403 1.75 cgd irq = ia->ia_irq;
404 1.77 cgd } else
405 1.77 cgd #endif
406 1.77 cgd #if NCOM_COMMULTI
407 1.77 cgd if (1) {
408 1.75 cgd struct commulti_attach_args *ca = aux;
409 1.75 cgd
410 1.75 cgd /*
411 1.75 cgd * We're living on a commulti.
412 1.75 cgd */
413 1.75 cgd iobase = ca->ca_iobase;
414 1.91 thorpej iot = ca->ca_iot;
415 1.75 cgd ioh = ca->ca_ioh;
416 1.75 cgd irq = IRQUNK;
417 1.75 cgd
418 1.75 cgd if (ca->ca_noien)
419 1.75 cgd sc->sc_hwflags |= COM_HW_NOIEN;
420 1.77 cgd } else
421 1.77 cgd #endif
422 1.77 cgd panic("comattach: impossible");
423 1.75 cgd
424 1.91 thorpej sc->sc_iot = iot;
425 1.75 cgd sc->sc_ioh = ioh;
426 1.21 mycroft sc->sc_iobase = iobase;
427 1.21 mycroft
428 1.75 cgd if (iobase == comconsaddr) {
429 1.75 cgd comconsattached = 1;
430 1.75 cgd
431 1.96 mycroft /* Make sure the console is always "hardwired". */
432 1.75 cgd delay(1000); /* wait for output to finish */
433 1.75 cgd SET(sc->sc_hwflags, COM_HW_CONSOLE);
434 1.75 cgd SET(sc->sc_swflags, COM_SW_SOFTCAR);
435 1.75 cgd }
436 1.26 cgd
437 1.65 christos #ifdef COM_HAYESP
438 1.65 christos /* Look for a Hayes ESP board. */
439 1.75 cgd for (hayespp = hayesp_ports; *hayespp != 0; hayespp++) {
440 1.91 thorpej bus_space_handle_t hayespioh;
441 1.75 cgd
442 1.75 cgd #define HAYESP_NPORTS 8 /* XXX XXX XXX ??? ??? ??? */
443 1.92 cgd if (bus_space_map(iot, *hayespp, HAYESP_NPORTS, 0, &hayespioh))
444 1.75 cgd continue;
445 1.75 cgd if (comprobeHAYESP(hayespioh, sc)) {
446 1.65 christos sc->sc_hayespbase = *hayespp;
447 1.75 cgd sc->sc_hayespioh = hayespioh;
448 1.65 christos break;
449 1.65 christos }
450 1.91 thorpej bus_space_unmap(iot, hayespioh, HAYESP_NPORTS);
451 1.75 cgd }
452 1.65 christos /* No ESP; look for other things. */
453 1.65 christos if (*hayespp == 0) {
454 1.65 christos #endif
455 1.64 christos
456 1.1 cgd /* look for a NS 16550AF UART with FIFOs */
457 1.91 thorpej bus_space_write_1(iot, ioh, com_fifo,
458 1.21 mycroft FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
459 1.20 mycroft delay(100);
460 1.91 thorpej if (ISSET(bus_space_read_1(iot, ioh, com_iir), IIR_FIFO_MASK) == IIR_FIFO_MASK)
461 1.91 thorpej if (ISSET(bus_space_read_1(iot, ioh, com_fifo), FIFO_TRIGGER_14) == FIFO_TRIGGER_14) {
462 1.62 mycroft SET(sc->sc_hwflags, COM_HW_FIFO);
463 1.90 christos printf(": ns16550a, working fifo\n");
464 1.21 mycroft } else
465 1.90 christos printf(": ns16550, broken fifo\n");
466 1.21 mycroft else
467 1.90 christos printf(": ns8250 or ns16450, no fifo\n");
468 1.91 thorpej bus_space_write_1(iot, ioh, com_fifo, 0);
469 1.65 christos #ifdef COM_HAYESP
470 1.65 christos }
471 1.65 christos #endif
472 1.21 mycroft
473 1.21 mycroft /* disable interrupts */
474 1.91 thorpej bus_space_write_1(iot, ioh, com_ier, 0);
475 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, 0);
476 1.1 cgd
477 1.78 cgd if (irq != IRQUNK) {
478 1.78 cgd #if NCOM_ISA
479 1.78 cgd if (!strcmp(parent->dv_cfdata->cf_driver->cd_name, "isa")) {
480 1.78 cgd struct isa_attach_args *ia = aux;
481 1.78 cgd
482 1.78 cgd sc->sc_ih = isa_intr_establish(ia->ia_ic, irq,
483 1.78 cgd IST_EDGE, IPL_TTY, comintr, sc);
484 1.78 cgd } else
485 1.78 cgd #endif
486 1.78 cgd panic("comattach: IRQ but can't have one");
487 1.78 cgd }
488 1.30 mycroft
489 1.96 mycroft if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
490 1.96 mycroft cominit(iot, ioh, comdefaultrate);
491 1.96 mycroft printf("%s: console\n", sc->sc_dev.dv_xname);
492 1.96 mycroft }
493 1.96 mycroft
494 1.1 cgd #ifdef KGDB
495 1.2 cgd if (kgdb_dev == makedev(commajor, unit)) {
496 1.75 cgd if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
497 1.1 cgd kgdb_dev = -1; /* can't debug over console port */
498 1.1 cgd else {
499 1.91 thorpej cominit(iot, ioh, kgdb_rate);
500 1.1 cgd if (kgdb_debug_init) {
501 1.1 cgd /*
502 1.1 cgd * Print prefix of device name,
503 1.1 cgd * let kgdb_connect print the rest.
504 1.1 cgd */
505 1.90 christos printf("%s: ", sc->sc_dev.dv_xname);
506 1.1 cgd kgdb_connect(1);
507 1.1 cgd } else
508 1.90 christos printf("%s: kgdb enabled\n",
509 1.21 mycroft sc->sc_dev.dv_xname);
510 1.1 cgd }
511 1.1 cgd }
512 1.1 cgd #endif
513 1.1 cgd }
514 1.1 cgd
515 1.21 mycroft int
516 1.21 mycroft comopen(dev, flag, mode, p)
517 1.21 mycroft dev_t dev;
518 1.21 mycroft int flag, mode;
519 1.21 mycroft struct proc *p;
520 1.1 cgd {
521 1.21 mycroft int unit = COMUNIT(dev);
522 1.21 mycroft struct com_softc *sc;
523 1.91 thorpej bus_space_tag_t iot;
524 1.91 thorpej bus_space_handle_t ioh;
525 1.21 mycroft struct tty *tp;
526 1.21 mycroft int s;
527 1.1 cgd int error = 0;
528 1.1 cgd
529 1.76 thorpej if (unit >= com_cd.cd_ndevs)
530 1.21 mycroft return ENXIO;
531 1.76 thorpej sc = com_cd.cd_devs[unit];
532 1.29 mycroft if (!sc)
533 1.21 mycroft return ENXIO;
534 1.21 mycroft
535 1.83 cgd if (!sc->sc_tty) {
536 1.50 mycroft tp = sc->sc_tty = ttymalloc();
537 1.83 cgd tty_attach(tp);
538 1.83 cgd } else
539 1.50 mycroft tp = sc->sc_tty;
540 1.21 mycroft
541 1.1 cgd tp->t_oproc = comstart;
542 1.1 cgd tp->t_param = comparam;
543 1.1 cgd tp->t_dev = dev;
544 1.62 mycroft if (!ISSET(tp->t_state, TS_ISOPEN)) {
545 1.62 mycroft SET(tp->t_state, TS_WOPEN);
546 1.1 cgd ttychars(tp);
547 1.16 ws tp->t_iflag = TTYDEF_IFLAG;
548 1.16 ws tp->t_oflag = TTYDEF_OFLAG;
549 1.79 cgd if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
550 1.79 cgd tp->t_cflag = comconscflag;
551 1.79 cgd else
552 1.79 cgd tp->t_cflag = TTYDEF_CFLAG;
553 1.62 mycroft if (ISSET(sc->sc_swflags, COM_SW_CLOCAL))
554 1.62 mycroft SET(tp->t_cflag, CLOCAL);
555 1.62 mycroft if (ISSET(sc->sc_swflags, COM_SW_CRTSCTS))
556 1.62 mycroft SET(tp->t_cflag, CRTSCTS);
557 1.62 mycroft if (ISSET(sc->sc_swflags, COM_SW_MDMBUF))
558 1.62 mycroft SET(tp->t_cflag, MDMBUF);
559 1.16 ws tp->t_lflag = TTYDEF_LFLAG;
560 1.16 ws tp->t_ispeed = tp->t_ospeed = comdefaultrate;
561 1.57 mycroft
562 1.57 mycroft s = spltty();
563 1.57 mycroft
564 1.1 cgd comparam(tp, &tp->t_termios);
565 1.1 cgd ttsetwater(tp);
566 1.21 mycroft
567 1.57 mycroft if (comsopen++ == 0)
568 1.86 mycroft timeout(comsoft, NULL, 1);
569 1.57 mycroft
570 1.57 mycroft sc->sc_ibufp = sc->sc_ibuf = sc->sc_ibufs[0];
571 1.57 mycroft sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
572 1.57 mycroft sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
573 1.57 mycroft
574 1.91 thorpej iot = sc->sc_iot;
575 1.75 cgd ioh = sc->sc_ioh;
576 1.65 christos #ifdef COM_HAYESP
577 1.64 christos /* Setup the ESP board */
578 1.65 christos if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
579 1.91 thorpej bus_space_handle_t hayespioh = sc->sc_hayespioh;
580 1.65 christos
581 1.91 thorpej bus_space_write_1(iot, ioh, com_fifo,
582 1.64 christos FIFO_DMA_MODE|FIFO_ENABLE|
583 1.64 christos FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_8);
584 1.65 christos
585 1.64 christos /* Set 16550 compatibility mode */
586 1.91 thorpej bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_SETMODE);
587 1.91 thorpej bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
588 1.65 christos HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
589 1.65 christos HAYESP_MODE_SCALE);
590 1.65 christos
591 1.64 christos /* Set RTS/CTS flow control */
592 1.91 thorpej bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_SETFLOWTYPE);
593 1.91 thorpej bus_space_write_1(iot, hayespioh, HAYESP_CMD2, HAYESP_FLOW_RTS);
594 1.91 thorpej bus_space_write_1(iot, hayespioh, HAYESP_CMD2, HAYESP_FLOW_CTS);
595 1.65 christos
596 1.64 christos /* Set flow control levels */
597 1.91 thorpej bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_SETRXFLOW);
598 1.91 thorpej bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
599 1.65 christos HAYESP_HIBYTE(HAYESP_RXHIWMARK));
600 1.91 thorpej bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
601 1.65 christos HAYESP_LOBYTE(HAYESP_RXHIWMARK));
602 1.91 thorpej bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
603 1.65 christos HAYESP_HIBYTE(HAYESP_RXLOWMARK));
604 1.91 thorpej bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
605 1.65 christos HAYESP_LOBYTE(HAYESP_RXLOWMARK));
606 1.65 christos } else
607 1.65 christos #endif
608 1.65 christos if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
609 1.64 christos /* Set the FIFO threshold based on the receive speed. */
610 1.91 thorpej bus_space_write_1(iot, ioh, com_fifo,
611 1.36 mycroft FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
612 1.36 mycroft (tp->t_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
613 1.21 mycroft /* flush any pending I/O */
614 1.91 thorpej while (ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY))
615 1.91 thorpej (void) bus_space_read_1(iot, ioh, com_data);
616 1.21 mycroft /* you turn me on, baby */
617 1.26 cgd sc->sc_mcr = MCR_DTR | MCR_RTS;
618 1.62 mycroft if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
619 1.62 mycroft SET(sc->sc_mcr, MCR_IENABLE);
620 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr);
621 1.69 mycroft sc->sc_ier = IER_ERXRDY | IER_ERLS | IER_EMSC;
622 1.91 thorpej bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
623 1.21 mycroft
624 1.91 thorpej sc->sc_msr = bus_space_read_1(iot, ioh, com_msr);
625 1.62 mycroft if (ISSET(sc->sc_swflags, COM_SW_SOFTCAR) ||
626 1.62 mycroft ISSET(sc->sc_msr, MSR_DCD) || ISSET(tp->t_cflag, MDMBUF))
627 1.62 mycroft SET(tp->t_state, TS_CARR_ON);
628 1.21 mycroft else
629 1.62 mycroft CLR(tp->t_state, TS_CARR_ON);
630 1.62 mycroft } else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
631 1.21 mycroft return EBUSY;
632 1.62 mycroft else
633 1.57 mycroft s = spltty();
634 1.21 mycroft
635 1.21 mycroft /* wait for carrier if necessary */
636 1.62 mycroft if (!ISSET(flag, O_NONBLOCK))
637 1.62 mycroft while (!ISSET(tp->t_cflag, CLOCAL) &&
638 1.62 mycroft !ISSET(tp->t_state, TS_CARR_ON)) {
639 1.62 mycroft SET(tp->t_state, TS_WOPEN);
640 1.62 mycroft error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
641 1.62 mycroft ttopen, 0);
642 1.21 mycroft if (error) {
643 1.21 mycroft /* XXX should turn off chip if we're the
644 1.21 mycroft only waiter */
645 1.21 mycroft splx(s);
646 1.21 mycroft return error;
647 1.21 mycroft }
648 1.21 mycroft }
649 1.21 mycroft splx(s);
650 1.21 mycroft
651 1.21 mycroft return (*linesw[tp->t_line].l_open)(dev, tp);
652 1.1 cgd }
653 1.1 cgd
654 1.21 mycroft int
655 1.1 cgd comclose(dev, flag, mode, p)
656 1.1 cgd dev_t dev;
657 1.1 cgd int flag, mode;
658 1.1 cgd struct proc *p;
659 1.1 cgd {
660 1.21 mycroft int unit = COMUNIT(dev);
661 1.76 thorpej struct com_softc *sc = com_cd.cd_devs[unit];
662 1.50 mycroft struct tty *tp = sc->sc_tty;
663 1.91 thorpej bus_space_tag_t iot = sc->sc_iot;
664 1.91 thorpej bus_space_handle_t ioh = sc->sc_ioh;
665 1.57 mycroft int s;
666 1.57 mycroft
667 1.57 mycroft /* XXX This is for cons.c. */
668 1.62 mycroft if (!ISSET(tp->t_state, TS_ISOPEN))
669 1.57 mycroft return 0;
670 1.21 mycroft
671 1.1 cgd (*linesw[tp->t_line].l_close)(tp, flag);
672 1.57 mycroft s = spltty();
673 1.62 mycroft CLR(sc->sc_lcr, LCR_SBREAK);
674 1.91 thorpej bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr);
675 1.91 thorpej bus_space_write_1(iot, ioh, com_ier, 0);
676 1.62 mycroft if (ISSET(tp->t_cflag, HUPCL) &&
677 1.62 mycroft !ISSET(sc->sc_swflags, COM_SW_SOFTCAR)) {
678 1.57 mycroft /* XXX perhaps only clear DTR */
679 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, 0);
680 1.91 thorpej bus_space_write_1(iot, ioh, com_fifo,
681 1.91 thorpej FIFO_RCV_RST | FIFO_XMT_RST);
682 1.57 mycroft }
683 1.62 mycroft CLR(tp->t_state, TS_BUSY | TS_FLUSH);
684 1.57 mycroft if (--comsopen == 0)
685 1.86 mycroft untimeout(comsoft, NULL);
686 1.57 mycroft splx(s);
687 1.1 cgd ttyclose(tp);
688 1.21 mycroft #ifdef notyet /* XXXX */
689 1.75 cgd if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
690 1.21 mycroft ttyfree(tp);
691 1.50 mycroft sc->sc_tty = 0;
692 1.21 mycroft }
693 1.13 cgd #endif
694 1.21 mycroft return 0;
695 1.1 cgd }
696 1.1 cgd
697 1.21 mycroft int
698 1.1 cgd comread(dev, uio, flag)
699 1.1 cgd dev_t dev;
700 1.1 cgd struct uio *uio;
701 1.21 mycroft int flag;
702 1.1 cgd {
703 1.76 thorpej struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
704 1.52 mycroft struct tty *tp = sc->sc_tty;
705 1.1 cgd
706 1.1 cgd return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
707 1.1 cgd }
708 1.1 cgd
709 1.21 mycroft int
710 1.1 cgd comwrite(dev, uio, flag)
711 1.1 cgd dev_t dev;
712 1.1 cgd struct uio *uio;
713 1.21 mycroft int flag;
714 1.1 cgd {
715 1.76 thorpej struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
716 1.52 mycroft struct tty *tp = sc->sc_tty;
717 1.1 cgd
718 1.1 cgd return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
719 1.1 cgd }
720 1.50 mycroft
721 1.50 mycroft struct tty *
722 1.50 mycroft comtty(dev)
723 1.50 mycroft dev_t dev;
724 1.50 mycroft {
725 1.76 thorpej struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
726 1.52 mycroft struct tty *tp = sc->sc_tty;
727 1.50 mycroft
728 1.52 mycroft return (tp);
729 1.50 mycroft }
730 1.1 cgd
731 1.21 mycroft static u_char
732 1.21 mycroft tiocm_xxx2mcr(data)
733 1.21 mycroft int data;
734 1.21 mycroft {
735 1.21 mycroft u_char m = 0;
736 1.21 mycroft
737 1.62 mycroft if (ISSET(data, TIOCM_DTR))
738 1.62 mycroft SET(m, MCR_DTR);
739 1.62 mycroft if (ISSET(data, TIOCM_RTS))
740 1.62 mycroft SET(m, MCR_RTS);
741 1.21 mycroft return m;
742 1.1 cgd }
743 1.1 cgd
744 1.21 mycroft int
745 1.19 mycroft comioctl(dev, cmd, data, flag, p)
746 1.1 cgd dev_t dev;
747 1.39 cgd u_long cmd;
748 1.1 cgd caddr_t data;
749 1.19 mycroft int flag;
750 1.19 mycroft struct proc *p;
751 1.1 cgd {
752 1.21 mycroft int unit = COMUNIT(dev);
753 1.76 thorpej struct com_softc *sc = com_cd.cd_devs[unit];
754 1.50 mycroft struct tty *tp = sc->sc_tty;
755 1.91 thorpej bus_space_tag_t iot = sc->sc_iot;
756 1.91 thorpej bus_space_handle_t ioh = sc->sc_ioh;
757 1.21 mycroft int error;
758 1.21 mycroft
759 1.19 mycroft error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
760 1.1 cgd if (error >= 0)
761 1.21 mycroft return error;
762 1.19 mycroft error = ttioctl(tp, cmd, data, flag, p);
763 1.1 cgd if (error >= 0)
764 1.21 mycroft return error;
765 1.1 cgd
766 1.1 cgd switch (cmd) {
767 1.1 cgd case TIOCSBRK:
768 1.62 mycroft SET(sc->sc_lcr, LCR_SBREAK);
769 1.91 thorpej bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr);
770 1.1 cgd break;
771 1.1 cgd case TIOCCBRK:
772 1.62 mycroft CLR(sc->sc_lcr, LCR_SBREAK);
773 1.91 thorpej bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr);
774 1.1 cgd break;
775 1.1 cgd case TIOCSDTR:
776 1.62 mycroft SET(sc->sc_mcr, sc->sc_dtr);
777 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr);
778 1.1 cgd break;
779 1.1 cgd case TIOCCDTR:
780 1.62 mycroft CLR(sc->sc_mcr, sc->sc_dtr);
781 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr);
782 1.1 cgd break;
783 1.1 cgd case TIOCMSET:
784 1.62 mycroft CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
785 1.1 cgd case TIOCMBIS:
786 1.62 mycroft SET(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
787 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr);
788 1.1 cgd break;
789 1.1 cgd case TIOCMBIC:
790 1.62 mycroft CLR(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
791 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr);
792 1.1 cgd break;
793 1.21 mycroft case TIOCMGET: {
794 1.21 mycroft u_char m;
795 1.21 mycroft int bits = 0;
796 1.21 mycroft
797 1.21 mycroft m = sc->sc_mcr;
798 1.62 mycroft if (ISSET(m, MCR_DTR))
799 1.62 mycroft SET(bits, TIOCM_DTR);
800 1.62 mycroft if (ISSET(m, MCR_RTS))
801 1.62 mycroft SET(bits, TIOCM_RTS);
802 1.21 mycroft m = sc->sc_msr;
803 1.62 mycroft if (ISSET(m, MSR_DCD))
804 1.62 mycroft SET(bits, TIOCM_CD);
805 1.62 mycroft if (ISSET(m, MSR_CTS))
806 1.62 mycroft SET(bits, TIOCM_CTS);
807 1.62 mycroft if (ISSET(m, MSR_DSR))
808 1.62 mycroft SET(bits, TIOCM_DSR);
809 1.62 mycroft if (ISSET(m, MSR_RI | MSR_TERI))
810 1.62 mycroft SET(bits, TIOCM_RI);
811 1.91 thorpej if (bus_space_read_1(iot, ioh, com_ier))
812 1.62 mycroft SET(bits, TIOCM_LE);
813 1.21 mycroft *(int *)data = bits;
814 1.1 cgd break;
815 1.21 mycroft }
816 1.22 cgd case TIOCGFLAGS: {
817 1.62 mycroft int driverbits, userbits = 0;
818 1.22 cgd
819 1.62 mycroft driverbits = sc->sc_swflags;
820 1.62 mycroft if (ISSET(driverbits, COM_SW_SOFTCAR))
821 1.62 mycroft SET(userbits, TIOCFLAG_SOFTCAR);
822 1.62 mycroft if (ISSET(driverbits, COM_SW_CLOCAL))
823 1.62 mycroft SET(userbits, TIOCFLAG_CLOCAL);
824 1.62 mycroft if (ISSET(driverbits, COM_SW_CRTSCTS))
825 1.62 mycroft SET(userbits, TIOCFLAG_CRTSCTS);
826 1.62 mycroft if (ISSET(driverbits, COM_SW_MDMBUF))
827 1.62 mycroft SET(userbits, TIOCFLAG_MDMBUF);
828 1.22 cgd
829 1.62 mycroft *(int *)data = userbits;
830 1.22 cgd break;
831 1.22 cgd }
832 1.22 cgd case TIOCSFLAGS: {
833 1.22 cgd int userbits, driverbits = 0;
834 1.22 cgd
835 1.23 cgd error = suser(p->p_ucred, &p->p_acflag);
836 1.23 cgd if (error != 0)
837 1.23 cgd return(EPERM);
838 1.22 cgd
839 1.22 cgd userbits = *(int *)data;
840 1.62 mycroft if (ISSET(userbits, TIOCFLAG_SOFTCAR) ||
841 1.62 mycroft ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
842 1.62 mycroft SET(driverbits, COM_SW_SOFTCAR);
843 1.62 mycroft if (ISSET(userbits, TIOCFLAG_CLOCAL))
844 1.62 mycroft SET(driverbits, COM_SW_CLOCAL);
845 1.62 mycroft if (ISSET(userbits, TIOCFLAG_CRTSCTS))
846 1.62 mycroft SET(driverbits, COM_SW_CRTSCTS);
847 1.62 mycroft if (ISSET(userbits, TIOCFLAG_MDMBUF))
848 1.62 mycroft SET(driverbits, COM_SW_MDMBUF);
849 1.22 cgd
850 1.23 cgd sc->sc_swflags = driverbits;
851 1.22 cgd break;
852 1.22 cgd }
853 1.1 cgd default:
854 1.21 mycroft return ENOTTY;
855 1.1 cgd }
856 1.21 mycroft
857 1.21 mycroft return 0;
858 1.1 cgd }
859 1.1 cgd
860 1.21 mycroft int
861 1.1 cgd comparam(tp, t)
862 1.21 mycroft struct tty *tp;
863 1.21 mycroft struct termios *t;
864 1.1 cgd {
865 1.76 thorpej struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
866 1.91 thorpej bus_space_tag_t iot = sc->sc_iot;
867 1.91 thorpej bus_space_handle_t ioh = sc->sc_ioh;
868 1.21 mycroft int ospeed = comspeed(t->c_ospeed);
869 1.62 mycroft u_char lcr;
870 1.57 mycroft tcflag_t oldcflag;
871 1.21 mycroft int s;
872 1.21 mycroft
873 1.1 cgd /* check requested parameters */
874 1.21 mycroft if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
875 1.21 mycroft return EINVAL;
876 1.21 mycroft
877 1.66 mycroft lcr = ISSET(sc->sc_lcr, LCR_SBREAK);
878 1.62 mycroft
879 1.62 mycroft switch (ISSET(t->c_cflag, CSIZE)) {
880 1.1 cgd case CS5:
881 1.62 mycroft SET(lcr, LCR_5BITS);
882 1.21 mycroft break;
883 1.1 cgd case CS6:
884 1.62 mycroft SET(lcr, LCR_6BITS);
885 1.21 mycroft break;
886 1.1 cgd case CS7:
887 1.62 mycroft SET(lcr, LCR_7BITS);
888 1.21 mycroft break;
889 1.1 cgd case CS8:
890 1.62 mycroft SET(lcr, LCR_8BITS);
891 1.21 mycroft break;
892 1.1 cgd }
893 1.62 mycroft if (ISSET(t->c_cflag, PARENB)) {
894 1.62 mycroft SET(lcr, LCR_PENAB);
895 1.62 mycroft if (!ISSET(t->c_cflag, PARODD))
896 1.62 mycroft SET(lcr, LCR_PEVEN);
897 1.1 cgd }
898 1.62 mycroft if (ISSET(t->c_cflag, CSTOPB))
899 1.62 mycroft SET(lcr, LCR_STOPB);
900 1.62 mycroft
901 1.62 mycroft sc->sc_lcr = lcr;
902 1.1 cgd
903 1.21 mycroft s = spltty();
904 1.35 mycroft
905 1.62 mycroft if (ospeed == 0) {
906 1.62 mycroft CLR(sc->sc_mcr, MCR_DTR);
907 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr);
908 1.62 mycroft }
909 1.36 mycroft
910 1.36 mycroft /*
911 1.36 mycroft * Set the FIFO threshold based on the receive speed, if we are
912 1.36 mycroft * changing it.
913 1.36 mycroft */
914 1.95 mycroft #if 0
915 1.65 christos if (tp->t_ispeed != t->c_ispeed) {
916 1.66 mycroft #else
917 1.66 mycroft if (1) {
918 1.66 mycroft #endif
919 1.66 mycroft if (ospeed != 0) {
920 1.66 mycroft /*
921 1.66 mycroft * Make sure the transmit FIFO is empty before
922 1.67 mycroft * proceeding. If we don't do this, some revisions
923 1.67 mycroft * of the UART will hang. Interestingly enough,
924 1.67 mycroft * even if we do this will the last character is
925 1.67 mycroft * still being pushed out, they don't hang. This
926 1.67 mycroft * seems good enough.
927 1.66 mycroft */
928 1.66 mycroft while (ISSET(tp->t_state, TS_BUSY)) {
929 1.66 mycroft int error;
930 1.66 mycroft
931 1.66 mycroft ++sc->sc_halt;
932 1.66 mycroft error = ttysleep(tp, &tp->t_outq,
933 1.66 mycroft TTOPRI | PCATCH, "comprm", 0);
934 1.66 mycroft --sc->sc_halt;
935 1.66 mycroft if (error) {
936 1.66 mycroft splx(s);
937 1.66 mycroft comstart(tp);
938 1.66 mycroft return (error);
939 1.66 mycroft }
940 1.66 mycroft }
941 1.66 mycroft
942 1.91 thorpej bus_space_write_1(iot, ioh, com_lcr, lcr | LCR_DLAB);
943 1.91 thorpej bus_space_write_1(iot, ioh, com_dlbl, ospeed);
944 1.91 thorpej bus_space_write_1(iot, ioh, com_dlbh, ospeed >> 8);
945 1.91 thorpej bus_space_write_1(iot, ioh, com_lcr, lcr);
946 1.66 mycroft SET(sc->sc_mcr, MCR_DTR);
947 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr);
948 1.66 mycroft } else
949 1.91 thorpej bus_space_write_1(iot, ioh, com_lcr, lcr);
950 1.66 mycroft
951 1.66 mycroft if (!ISSET(sc->sc_hwflags, COM_HW_HAYESP) &&
952 1.66 mycroft ISSET(sc->sc_hwflags, COM_HW_FIFO))
953 1.91 thorpej bus_space_write_1(iot, ioh, com_fifo,
954 1.57 mycroft FIFO_ENABLE |
955 1.36 mycroft (t->c_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
956 1.57 mycroft } else
957 1.91 thorpej bus_space_write_1(iot, ioh, com_lcr, lcr);
958 1.21 mycroft
959 1.22 cgd /* When not using CRTSCTS, RTS follows DTR. */
960 1.62 mycroft if (!ISSET(t->c_cflag, CRTSCTS)) {
961 1.62 mycroft if (ISSET(sc->sc_mcr, MCR_DTR)) {
962 1.62 mycroft if (!ISSET(sc->sc_mcr, MCR_RTS)) {
963 1.62 mycroft SET(sc->sc_mcr, MCR_RTS);
964 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr);
965 1.62 mycroft }
966 1.21 mycroft } else {
967 1.62 mycroft if (ISSET(sc->sc_mcr, MCR_RTS)) {
968 1.62 mycroft CLR(sc->sc_mcr, MCR_RTS);
969 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr);
970 1.62 mycroft }
971 1.21 mycroft }
972 1.57 mycroft sc->sc_dtr = MCR_DTR | MCR_RTS;
973 1.57 mycroft } else
974 1.57 mycroft sc->sc_dtr = MCR_DTR;
975 1.21 mycroft
976 1.57 mycroft /* and copy to tty */
977 1.57 mycroft tp->t_ispeed = t->c_ispeed;
978 1.57 mycroft tp->t_ospeed = t->c_ospeed;
979 1.57 mycroft oldcflag = tp->t_cflag;
980 1.57 mycroft tp->t_cflag = t->c_cflag;
981 1.25 cgd
982 1.25 cgd /*
983 1.57 mycroft * If DCD is off and MDMBUF is changed, ask the tty layer if we should
984 1.57 mycroft * stop the device.
985 1.25 cgd */
986 1.62 mycroft if (!ISSET(sc->sc_msr, MSR_DCD) &&
987 1.62 mycroft !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
988 1.62 mycroft ISSET(oldcflag, MDMBUF) != ISSET(tp->t_cflag, MDMBUF) &&
989 1.57 mycroft (*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
990 1.62 mycroft CLR(sc->sc_mcr, sc->sc_dtr);
991 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr);
992 1.21 mycroft }
993 1.1 cgd
994 1.66 mycroft /* Just to be sure... */
995 1.21 mycroft splx(s);
996 1.66 mycroft comstart(tp);
997 1.21 mycroft return 0;
998 1.1 cgd }
999 1.21 mycroft
1000 1.12 deraadt void
1001 1.1 cgd comstart(tp)
1002 1.21 mycroft struct tty *tp;
1003 1.1 cgd {
1004 1.76 thorpej struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1005 1.91 thorpej bus_space_tag_t iot = sc->sc_iot;
1006 1.91 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1007 1.21 mycroft int s;
1008 1.21 mycroft
1009 1.1 cgd s = spltty();
1010 1.69 mycroft if (ISSET(tp->t_state, TS_BUSY))
1011 1.70 mycroft goto out;
1012 1.69 mycroft if (ISSET(tp->t_state, TS_TIMEOUT | TS_TTSTOP) ||
1013 1.69 mycroft sc->sc_halt > 0)
1014 1.69 mycroft goto stopped;
1015 1.62 mycroft if (ISSET(tp->t_cflag, CRTSCTS) && !ISSET(sc->sc_msr, MSR_CTS))
1016 1.69 mycroft goto stopped;
1017 1.61 mycroft if (tp->t_outq.c_cc <= tp->t_lowat) {
1018 1.62 mycroft if (ISSET(tp->t_state, TS_ASLEEP)) {
1019 1.62 mycroft CLR(tp->t_state, TS_ASLEEP);
1020 1.62 mycroft wakeup(&tp->t_outq);
1021 1.61 mycroft }
1022 1.61 mycroft if (tp->t_outq.c_cc == 0)
1023 1.69 mycroft goto stopped;
1024 1.61 mycroft selwakeup(&tp->t_wsel);
1025 1.61 mycroft }
1026 1.62 mycroft SET(tp->t_state, TS_BUSY);
1027 1.64 christos
1028 1.70 mycroft if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
1029 1.70 mycroft SET(sc->sc_ier, IER_ETXRDY);
1030 1.91 thorpej bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1031 1.70 mycroft }
1032 1.65 christos #ifdef COM_HAYESP
1033 1.65 christos if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
1034 1.64 christos u_char buffer[1024], *cp = buffer;
1035 1.64 christos int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
1036 1.64 christos do
1037 1.91 thorpej bus_space_write_1(iot, ioh, com_data, *cp++);
1038 1.64 christos while (--n);
1039 1.64 christos }
1040 1.65 christos else
1041 1.65 christos #endif
1042 1.65 christos if (ISSET(sc->sc_hwflags, COM_HW_FIFO)) {
1043 1.21 mycroft u_char buffer[16], *cp = buffer;
1044 1.21 mycroft int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
1045 1.21 mycroft do {
1046 1.91 thorpej bus_space_write_1(iot, ioh, com_data, *cp++);
1047 1.21 mycroft } while (--n);
1048 1.21 mycroft } else
1049 1.91 thorpej bus_space_write_1(iot, ioh, com_data, getc(&tp->t_outq));
1050 1.70 mycroft out:
1051 1.69 mycroft splx(s);
1052 1.69 mycroft return;
1053 1.69 mycroft stopped:
1054 1.69 mycroft if (ISSET(sc->sc_ier, IER_ETXRDY)) {
1055 1.69 mycroft CLR(sc->sc_ier, IER_ETXRDY);
1056 1.91 thorpej bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1057 1.69 mycroft }
1058 1.1 cgd splx(s);
1059 1.1 cgd }
1060 1.21 mycroft
1061 1.1 cgd /*
1062 1.1 cgd * Stop output on a line.
1063 1.1 cgd */
1064 1.85 mycroft void
1065 1.1 cgd comstop(tp, flag)
1066 1.21 mycroft struct tty *tp;
1067 1.80 christos int flag;
1068 1.1 cgd {
1069 1.21 mycroft int s;
1070 1.1 cgd
1071 1.1 cgd s = spltty();
1072 1.62 mycroft if (ISSET(tp->t_state, TS_BUSY))
1073 1.62 mycroft if (!ISSET(tp->t_state, TS_TTSTOP))
1074 1.62 mycroft SET(tp->t_state, TS_FLUSH);
1075 1.1 cgd splx(s);
1076 1.1 cgd }
1077 1.1 cgd
1078 1.33 mycroft void
1079 1.33 mycroft comdiag(arg)
1080 1.33 mycroft void *arg;
1081 1.33 mycroft {
1082 1.33 mycroft struct com_softc *sc = arg;
1083 1.88 mycroft int overflows, floods, failures;
1084 1.33 mycroft int s;
1085 1.33 mycroft
1086 1.33 mycroft s = spltty();
1087 1.57 mycroft sc->sc_errors = 0;
1088 1.33 mycroft overflows = sc->sc_overflows;
1089 1.33 mycroft sc->sc_overflows = 0;
1090 1.57 mycroft floods = sc->sc_floods;
1091 1.57 mycroft sc->sc_floods = 0;
1092 1.88 mycroft failures = sc->sc_failures;
1093 1.88 mycroft sc->sc_failures = 0;
1094 1.57 mycroft splx(s);
1095 1.57 mycroft
1096 1.88 mycroft log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf overflow%s, %d uart failure%s\n",
1097 1.57 mycroft sc->sc_dev.dv_xname,
1098 1.57 mycroft overflows, overflows == 1 ? "" : "s",
1099 1.88 mycroft floods, floods == 1 ? "" : "s",
1100 1.88 mycroft failures, failures == 1 ? "" : "s");
1101 1.57 mycroft }
1102 1.57 mycroft
1103 1.57 mycroft void
1104 1.86 mycroft comsoft(arg)
1105 1.57 mycroft void *arg;
1106 1.57 mycroft {
1107 1.57 mycroft int unit;
1108 1.57 mycroft struct com_softc *sc;
1109 1.57 mycroft struct tty *tp;
1110 1.57 mycroft register u_char *ibufp;
1111 1.57 mycroft u_char *ibufend;
1112 1.57 mycroft register int c;
1113 1.57 mycroft int s;
1114 1.57 mycroft static int lsrmap[8] = {
1115 1.57 mycroft 0, TTY_PE,
1116 1.57 mycroft TTY_FE, TTY_PE|TTY_FE,
1117 1.57 mycroft TTY_FE, TTY_PE|TTY_FE,
1118 1.57 mycroft TTY_FE, TTY_PE|TTY_FE
1119 1.57 mycroft };
1120 1.57 mycroft
1121 1.57 mycroft s = spltty();
1122 1.57 mycroft if (comevents == 0) {
1123 1.57 mycroft splx(s);
1124 1.57 mycroft goto out;
1125 1.57 mycroft }
1126 1.57 mycroft comevents = 0;
1127 1.33 mycroft splx(s);
1128 1.33 mycroft
1129 1.76 thorpej for (unit = 0; unit < com_cd.cd_ndevs; unit++) {
1130 1.76 thorpej sc = com_cd.cd_devs[unit];
1131 1.57 mycroft if (sc == 0 || sc->sc_ibufp == sc->sc_ibuf)
1132 1.57 mycroft continue;
1133 1.57 mycroft
1134 1.57 mycroft tp = sc->sc_tty;
1135 1.57 mycroft
1136 1.57 mycroft s = spltty();
1137 1.57 mycroft
1138 1.57 mycroft ibufp = sc->sc_ibuf;
1139 1.57 mycroft ibufend = sc->sc_ibufp;
1140 1.61 mycroft
1141 1.61 mycroft if (ibufp == ibufend) {
1142 1.61 mycroft splx(s);
1143 1.61 mycroft continue;
1144 1.61 mycroft }
1145 1.57 mycroft
1146 1.57 mycroft sc->sc_ibufp = sc->sc_ibuf = (ibufp == sc->sc_ibufs[0]) ?
1147 1.57 mycroft sc->sc_ibufs[1] : sc->sc_ibufs[0];
1148 1.57 mycroft sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
1149 1.57 mycroft sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
1150 1.57 mycroft
1151 1.62 mycroft if (tp == 0 || !ISSET(tp->t_state, TS_ISOPEN)) {
1152 1.57 mycroft splx(s);
1153 1.57 mycroft continue;
1154 1.57 mycroft }
1155 1.57 mycroft
1156 1.62 mycroft if (ISSET(tp->t_cflag, CRTSCTS) &&
1157 1.62 mycroft !ISSET(sc->sc_mcr, MCR_RTS)) {
1158 1.62 mycroft /* XXX */
1159 1.62 mycroft SET(sc->sc_mcr, MCR_RTS);
1160 1.91 thorpej bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr,
1161 1.75 cgd sc->sc_mcr);
1162 1.62 mycroft }
1163 1.57 mycroft
1164 1.57 mycroft splx(s);
1165 1.57 mycroft
1166 1.57 mycroft while (ibufp < ibufend) {
1167 1.57 mycroft c = *ibufp++;
1168 1.57 mycroft if (*ibufp & LSR_OE) {
1169 1.57 mycroft sc->sc_overflows++;
1170 1.57 mycroft if (sc->sc_errors++ == 0)
1171 1.57 mycroft timeout(comdiag, sc, 60 * hz);
1172 1.57 mycroft }
1173 1.57 mycroft /* This is ugly, but fast. */
1174 1.57 mycroft c |= lsrmap[(*ibufp++ & (LSR_BI|LSR_FE|LSR_PE)) >> 2];
1175 1.57 mycroft (*linesw[tp->t_line].l_rint)(c, tp);
1176 1.57 mycroft }
1177 1.57 mycroft }
1178 1.57 mycroft
1179 1.57 mycroft out:
1180 1.86 mycroft timeout(comsoft, NULL, 1);
1181 1.21 mycroft }
1182 1.1 cgd
1183 1.21 mycroft int
1184 1.49 cgd comintr(arg)
1185 1.49 cgd void *arg;
1186 1.21 mycroft {
1187 1.49 cgd struct com_softc *sc = arg;
1188 1.91 thorpej bus_space_tag_t iot = sc->sc_iot;
1189 1.91 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1190 1.21 mycroft struct tty *tp;
1191 1.88 mycroft u_char iir, lsr, data, msr, delta;
1192 1.69 mycroft #ifdef COM_DEBUG
1193 1.72 mycroft int n;
1194 1.69 mycroft struct {
1195 1.72 mycroft u_char iir, lsr, msr;
1196 1.69 mycroft } iter[32];
1197 1.69 mycroft #endif
1198 1.55 mycroft
1199 1.72 mycroft #ifdef COM_DEBUG
1200 1.72 mycroft n = 0;
1201 1.88 mycroft iter[n].iir =
1202 1.88 mycroft #endif
1203 1.91 thorpej iir = bus_space_read_1(iot, ioh, com_iir);
1204 1.88 mycroft if (ISSET(iir, IIR_NOPEND))
1205 1.55 mycroft return (0);
1206 1.21 mycroft
1207 1.55 mycroft tp = sc->sc_tty;
1208 1.21 mycroft
1209 1.21 mycroft for (;;) {
1210 1.69 mycroft #ifdef COM_DEBUG
1211 1.69 mycroft iter[n].lsr =
1212 1.69 mycroft #endif
1213 1.91 thorpej lsr = bus_space_read_1(iot, ioh, com_lsr);
1214 1.55 mycroft
1215 1.69 mycroft if (ISSET(lsr, LSR_RXRDY)) {
1216 1.57 mycroft register u_char *p = sc->sc_ibufp;
1217 1.57 mycroft
1218 1.57 mycroft comevents = 1;
1219 1.55 mycroft do {
1220 1.91 thorpej data = bus_space_read_1(iot, ioh, com_data);
1221 1.62 mycroft if (ISSET(lsr, LSR_BI)) {
1222 1.72 mycroft #ifdef notdef
1223 1.90 christos printf("break %02x %02x %02x %02x\n",
1224 1.69 mycroft sc->sc_msr, sc->sc_mcr, sc->sc_lcr,
1225 1.69 mycroft sc->sc_dtr);
1226 1.69 mycroft #endif
1227 1.57 mycroft #ifdef DDB
1228 1.75 cgd if (ISSET(sc->sc_hwflags,
1229 1.75 cgd COM_HW_CONSOLE)) {
1230 1.58 mycroft Debugger();
1231 1.58 mycroft goto next;
1232 1.59 mycroft }
1233 1.21 mycroft #endif
1234 1.62 mycroft }
1235 1.57 mycroft if (p >= sc->sc_ibufend) {
1236 1.57 mycroft sc->sc_floods++;
1237 1.57 mycroft if (sc->sc_errors++ == 0)
1238 1.57 mycroft timeout(comdiag, sc, 60 * hz);
1239 1.57 mycroft } else {
1240 1.57 mycroft *p++ = data;
1241 1.57 mycroft *p++ = lsr;
1242 1.57 mycroft if (p == sc->sc_ibufhigh &&
1243 1.62 mycroft ISSET(tp->t_cflag, CRTSCTS)) {
1244 1.62 mycroft /* XXX */
1245 1.62 mycroft CLR(sc->sc_mcr, MCR_RTS);
1246 1.91 thorpej bus_space_write_1(iot, ioh,
1247 1.88 mycroft com_mcr, sc->sc_mcr);
1248 1.62 mycroft }
1249 1.55 mycroft }
1250 1.84 cgd #ifdef DDB
1251 1.57 mycroft next:
1252 1.84 cgd #endif
1253 1.69 mycroft #ifdef COM_DEBUG
1254 1.69 mycroft if (++n >= 32)
1255 1.69 mycroft goto ohfudge;
1256 1.69 mycroft iter[n].lsr =
1257 1.69 mycroft #endif
1258 1.91 thorpej lsr = bus_space_read_1(iot, ioh, com_lsr);
1259 1.69 mycroft } while (ISSET(lsr, LSR_RXRDY));
1260 1.57 mycroft
1261 1.57 mycroft sc->sc_ibufp = p;
1262 1.88 mycroft } else {
1263 1.69 mycroft #ifdef COM_DEBUG
1264 1.88 mycroft if (ISSET(lsr, LSR_BI|LSR_FE|LSR_PE|LSR_OE))
1265 1.90 christos printf("weird lsr %02x\n", lsr);
1266 1.62 mycroft #endif
1267 1.88 mycroft if ((iir & IIR_IMASK) == IIR_RXRDY) {
1268 1.88 mycroft sc->sc_failures++;
1269 1.88 mycroft if (sc->sc_errors++ == 0)
1270 1.88 mycroft timeout(comdiag, sc, 60 * hz);
1271 1.91 thorpej bus_space_write_1(iot, ioh, com_ier, 0);
1272 1.88 mycroft delay(10);
1273 1.91 thorpej bus_space_write_1(iot, ioh, com_ier,
1274 1.91 thorpej sc->sc_ier);
1275 1.88 mycroft iir = IIR_NOPEND;
1276 1.88 mycroft continue;
1277 1.88 mycroft }
1278 1.88 mycroft }
1279 1.55 mycroft
1280 1.69 mycroft #ifdef COM_DEBUG
1281 1.69 mycroft iter[n].msr =
1282 1.69 mycroft #endif
1283 1.91 thorpej msr = bus_space_read_1(iot, ioh, com_msr);
1284 1.55 mycroft
1285 1.55 mycroft if (msr != sc->sc_msr) {
1286 1.55 mycroft delta = msr ^ sc->sc_msr;
1287 1.55 mycroft sc->sc_msr = msr;
1288 1.62 mycroft if (ISSET(delta, MSR_DCD) &&
1289 1.62 mycroft !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
1290 1.62 mycroft (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD)) == 0) {
1291 1.62 mycroft CLR(sc->sc_mcr, sc->sc_dtr);
1292 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr);
1293 1.55 mycroft }
1294 1.62 mycroft if (ISSET(delta & msr, MSR_CTS) &&
1295 1.62 mycroft ISSET(tp->t_cflag, CRTSCTS)) {
1296 1.55 mycroft /* the line is up and we want to do rts/cts flow control */
1297 1.57 mycroft (*linesw[tp->t_line].l_start)(tp);
1298 1.55 mycroft }
1299 1.55 mycroft }
1300 1.55 mycroft
1301 1.62 mycroft if (ISSET(lsr, LSR_TXRDY) && ISSET(tp->t_state, TS_BUSY)) {
1302 1.68 mycroft CLR(tp->t_state, TS_BUSY | TS_FLUSH);
1303 1.66 mycroft if (sc->sc_halt > 0)
1304 1.66 mycroft wakeup(&tp->t_outq);
1305 1.69 mycroft (*linesw[tp->t_line].l_start)(tp);
1306 1.62 mycroft }
1307 1.62 mycroft
1308 1.69 mycroft #ifdef COM_DEBUG
1309 1.69 mycroft if (++n >= 32)
1310 1.69 mycroft goto ohfudge;
1311 1.88 mycroft iter[n].iir =
1312 1.88 mycroft #endif
1313 1.91 thorpej iir = bus_space_read_1(iot, ioh, com_iir);
1314 1.88 mycroft if (ISSET(iir, IIR_NOPEND))
1315 1.72 mycroft return (1);
1316 1.1 cgd }
1317 1.88 mycroft
1318 1.69 mycroft #ifdef COM_DEBUG
1319 1.69 mycroft ohfudge:
1320 1.90 christos printf("comintr: too many iterations");
1321 1.69 mycroft for (n = 0; n < 32; n++) {
1322 1.69 mycroft if ((n % 4) == 0)
1323 1.90 christos printf("\ncomintr: iter[%02d]", n);
1324 1.90 christos printf(" %02x %02x %02x", iter[n].iir, iter[n].lsr, iter[n].msr);
1325 1.69 mycroft }
1326 1.90 christos printf("\n");
1327 1.90 christos printf("comintr: msr %02x mcr %02x lcr %02x ier %02x\n",
1328 1.72 mycroft sc->sc_msr, sc->sc_mcr, sc->sc_lcr, sc->sc_ier);
1329 1.90 christos printf("comintr: state %08x cc %d\n", sc->sc_tty->t_state,
1330 1.72 mycroft sc->sc_tty->t_outq.c_cc);
1331 1.88 mycroft return (1);
1332 1.69 mycroft #endif
1333 1.1 cgd }
1334 1.1 cgd
1335 1.1 cgd /*
1336 1.1 cgd * Following are all routines needed for COM to act as console
1337 1.1 cgd */
1338 1.17 cgd #include <dev/cons.h>
1339 1.1 cgd
1340 1.48 mycroft void
1341 1.1 cgd comcnprobe(cp)
1342 1.1 cgd struct consdev *cp;
1343 1.1 cgd {
1344 1.80 christos /* XXX NEEDS TO BE FIXED XXX */
1345 1.91 thorpej bus_space_tag_t iot = 0;
1346 1.91 thorpej bus_space_handle_t ioh;
1347 1.75 cgd int found;
1348 1.75 cgd
1349 1.91 thorpej if (bus_space_map(iot, CONADDR, COM_NPORTS, 0, &ioh)) {
1350 1.75 cgd cp->cn_pri = CN_DEAD;
1351 1.75 cgd return;
1352 1.75 cgd }
1353 1.91 thorpej found = comprobe1(iot, ioh, CONADDR);
1354 1.91 thorpej bus_space_unmap(iot, ioh, COM_NPORTS);
1355 1.75 cgd if (!found) {
1356 1.21 mycroft cp->cn_pri = CN_DEAD;
1357 1.21 mycroft return;
1358 1.21 mycroft }
1359 1.1 cgd
1360 1.1 cgd /* locate the major number */
1361 1.1 cgd for (commajor = 0; commajor < nchrdev; commajor++)
1362 1.1 cgd if (cdevsw[commajor].d_open == comopen)
1363 1.1 cgd break;
1364 1.1 cgd
1365 1.1 cgd /* initialize required fields */
1366 1.29 mycroft cp->cn_dev = makedev(commajor, CONUNIT);
1367 1.1 cgd #ifdef COMCONSOLE
1368 1.1 cgd cp->cn_pri = CN_REMOTE; /* Force a serial port console */
1369 1.1 cgd #else
1370 1.1 cgd cp->cn_pri = CN_NORMAL;
1371 1.1 cgd #endif
1372 1.1 cgd }
1373 1.1 cgd
1374 1.48 mycroft void
1375 1.1 cgd comcninit(cp)
1376 1.1 cgd struct consdev *cp;
1377 1.1 cgd {
1378 1.1 cgd
1379 1.75 cgd #if 0
1380 1.75 cgd XXX NEEDS TO BE FIXED XXX
1381 1.91 thorpej comconstag = ???;
1382 1.75 cgd #endif
1383 1.91 thorpej if (bus_space_map(comconstag, CONADDR, COM_NPORTS, 0, &comconsbah))
1384 1.75 cgd panic("comcninit: mapping failed");
1385 1.75 cgd
1386 1.91 thorpej cominit(comconstag, comconsbah, comdefaultrate);
1387 1.75 cgd comconsaddr = CONADDR;
1388 1.1 cgd }
1389 1.1 cgd
1390 1.80 christos void
1391 1.91 thorpej cominit(iot, ioh, rate)
1392 1.91 thorpej bus_space_tag_t iot;
1393 1.91 thorpej bus_space_handle_t ioh;
1394 1.75 cgd int rate;
1395 1.1 cgd {
1396 1.21 mycroft int s = splhigh();
1397 1.21 mycroft u_char stat;
1398 1.1 cgd
1399 1.91 thorpej bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
1400 1.21 mycroft rate = comspeed(comdefaultrate);
1401 1.91 thorpej bus_space_write_1(iot, ioh, com_dlbl, rate);
1402 1.91 thorpej bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
1403 1.91 thorpej bus_space_write_1(iot, ioh, com_lcr, LCR_8BITS);
1404 1.91 thorpej bus_space_write_1(iot, ioh, com_ier, IER_ERXRDY | IER_ETXRDY);
1405 1.91 thorpej bus_space_write_1(iot, ioh, com_fifo,
1406 1.87 mycroft FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
1407 1.91 thorpej bus_space_write_1(iot, ioh, com_mcr, MCR_DTR | MCR_RTS);
1408 1.87 mycroft DELAY(100);
1409 1.91 thorpej stat = bus_space_read_1(iot, ioh, com_iir);
1410 1.1 cgd splx(s);
1411 1.1 cgd }
1412 1.1 cgd
1413 1.80 christos int
1414 1.1 cgd comcngetc(dev)
1415 1.21 mycroft dev_t dev;
1416 1.1 cgd {
1417 1.21 mycroft int s = splhigh();
1418 1.91 thorpej bus_space_tag_t iot = comconstag;
1419 1.91 thorpej bus_space_handle_t ioh = comconsbah;
1420 1.21 mycroft u_char stat, c;
1421 1.1 cgd
1422 1.91 thorpej while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY))
1423 1.1 cgd ;
1424 1.91 thorpej c = bus_space_read_1(iot, ioh, com_data);
1425 1.91 thorpej stat = bus_space_read_1(iot, ioh, com_iir);
1426 1.1 cgd splx(s);
1427 1.21 mycroft return c;
1428 1.1 cgd }
1429 1.1 cgd
1430 1.1 cgd /*
1431 1.1 cgd * Console kernel output character routine.
1432 1.1 cgd */
1433 1.48 mycroft void
1434 1.1 cgd comcnputc(dev, c)
1435 1.1 cgd dev_t dev;
1436 1.21 mycroft int c;
1437 1.1 cgd {
1438 1.21 mycroft int s = splhigh();
1439 1.91 thorpej bus_space_tag_t iot = comconstag;
1440 1.91 thorpej bus_space_handle_t ioh = comconsbah;
1441 1.21 mycroft u_char stat;
1442 1.1 cgd register int timo;
1443 1.1 cgd
1444 1.1 cgd /* wait for any pending transmission to finish */
1445 1.1 cgd timo = 50000;
1446 1.91 thorpej while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY) && --timo)
1447 1.1 cgd ;
1448 1.91 thorpej bus_space_write_1(iot, ioh, com_data, c);
1449 1.1 cgd /* wait for this transmission to complete */
1450 1.1 cgd timo = 1500000;
1451 1.91 thorpej while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY) && --timo)
1452 1.1 cgd ;
1453 1.1 cgd /* clear any interrupts generated by this transmission */
1454 1.91 thorpej stat = bus_space_read_1(iot, ioh, com_iir);
1455 1.1 cgd splx(s);
1456 1.37 mycroft }
1457 1.37 mycroft
1458 1.37 mycroft void
1459 1.37 mycroft comcnpollc(dev, on)
1460 1.37 mycroft dev_t dev;
1461 1.37 mycroft int on;
1462 1.37 mycroft {
1463 1.37 mycroft
1464 1.2 cgd }
1465