sa11x0_com.c revision 1.1 1 /* $NetBSD: sa11x0_com.c,v 1.1 2001/07/08 23:37:52 rjs Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by IWAMOTO Toshihiro.
9 *
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Charles M. Hannum.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 /*
43 * Copyright (c) 1991 The Regents of the University of California.
44 * All rights reserved.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Berkeley and its contributors.
58 * 4. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
73 *
74 * @(#)com.c 7.5 (Berkeley) 5/16/91
75 */
76
77 #include "opt_com.h"
78 #include "opt_ddb.h"
79 #include "opt_ddbparam.h"
80 #include "opt_kgdb.h"
81
82 #include "rnd.h"
83 #if NRND > 0 && defined(RND_COM)
84 #include <sys/rnd.h>
85 #endif
86
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/types.h>
90 #include <sys/conf.h>
91 #include <sys/file.h>
92 #include <sys/device.h>
93 #include <sys/kernel.h>
94 #include <sys/malloc.h>
95 #include <sys/tty.h>
96 #include <sys/uio.h>
97 #include <sys/vnode.h>
98
99 #include <dev/cons.h>
100
101 #include <machine/bus.h>
102 #include <arm/sa11x0/sa11x0_reg.h>
103 #include <arm/sa11x0/sa11x0_var.h>
104 #include <arm/sa11x0/sa11x0_comreg.h>
105 #include <arm/sa11x0/sa11x0_comvar.h>
106
107 #include "sacom.h"
108
109 cdev_decl(sacom);
110
111 static int sacom_match(struct device *, struct cfdata *, void *);
112 static void sacom_attach(struct device *, struct device *, void *);
113 static void sacom_filltx(struct sacom_softc *);
114 static void sacom_attach_subr(struct sacom_softc *);
115 #if defined(DDB) || defined(KGDB)
116 static void sacom_enable_debugport(struct sacom_softc *);
117 #endif
118 void sacom_config(struct sacom_softc *);
119 void sacom_shutdown(struct sacom_softc *);
120 static u_int cflag2cr0(tcflag_t);
121 int sacomparam(struct tty *, struct termios *);
122 void sacomstart(struct tty *);
123 void sacomstop(struct tty *, int);
124 int sacomhwiflow(struct tty *, int);
125
126 void sacom_loadchannelregs(struct sacom_softc *);
127 void sacom_hwiflow(struct sacom_softc *);
128 void sacom_break(struct sacom_softc *, int);
129 void sacom_modem(struct sacom_softc *, int);
130 void tiocm_to_sacom(struct sacom_softc *, u_long, int);
131 int sacom_to_tiocm(struct sacom_softc *);
132 void sacom_iflush(struct sacom_softc *);
133
134 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
135 void sacomsoft(void *);
136 #else
137 void sacomsoft(void);
138 #endif
139
140 static inline void sacom_rxsoft(struct sacom_softc *, struct tty *);
141 static inline void sacom_txsoft(struct sacom_softc *, struct tty *);
142 static inline void sacom_stsoft(struct sacom_softc *, struct tty *);
143 static inline void sacom_schedrx(struct sacom_softc *);
144
145
146 #define COMUNIT_MASK 0x7ffff
147 #define COMDIALOUT_MASK 0x80000
148
149 #define COMUNIT(x) (minor(x) & COMUNIT_MASK)
150 #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK)
151
152 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && \
153 ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
154
155 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
156 #define COM_LOCK(sc)
157 #define COM_UNLOCK(sc)
158
159 #define SET(t, f) (t) |= (f)
160 #define CLR(t, f) (t) &= ~(f)
161 #define ISSET(t, f) ((t) & (f))
162
163 int sacomintr(void *);
164 int sacomcngetc(dev_t);
165 void sacomcnputc(dev_t, int);
166 void sacomcnpollc(dev_t, int);
167
168 void sacomcnprobe(struct consdev *);
169 void sacomcninit(struct consdev *);
170
171 extern struct bus_space sa11x0_bs_tag;
172
173 static bus_space_tag_t sacomconstag;
174 static bus_space_handle_t sacomconsioh;
175 static bus_addr_t sacomconsaddr = SACOM3_BASE; /* XXX */
176
177 static int sacomconsattached;
178 static int sacomconsrate;
179 static tcflag_t sacomconscflag;
180
181 struct cfattach sacom_ca = {
182 sizeof(struct sacom_softc), sacom_match, sacom_attach
183 };
184 extern struct cfdriver sacom_cd;
185
186 struct consdev sacomcons = {
187 NULL, NULL, sacomcngetc, sacomcnputc, sacomcnpollc, NULL,
188 NODEV, CN_NORMAL
189 };
190
191 #ifndef CONMODE
192 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
193 #endif
194 #ifndef CONSPEED
195 #define CONSPEED 9600
196 #endif
197 #ifndef CONADDR
198 #define CONADDR SACOM3_BASE
199 #endif
200
201 static int
202 sacom_match(parent, match, aux)
203 struct device *parent;
204 struct cfdata *match;
205 void *aux;
206 {
207 return (1);
208 }
209
210 void
211 sacom_attach(parent, self, aux)
212 struct device *parent;
213 struct device *self;
214 void *aux;
215 {
216 struct sacom_softc *sc = (struct sacom_softc*)self;
217 struct sa11x0_attach_args *sa = aux;
218
219 printf("\n");
220
221 sc->sc_iot = sa->sa_iot;
222 sc->sc_baseaddr = sa->sa_addr;
223
224 if(bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
225 &sc->sc_ioh)) {
226 printf("%s: unable to map registers\n", sc->sc_dev.dv_xname);
227 return;
228 }
229
230 printf("%s: ", sc->sc_dev.dv_xname);
231 switch(sc->sc_baseaddr) {
232 case 0x80050000:
233 printf("SA11x0 UART3\n");
234 break;
235 case 0x80010000:
236 printf("SA11x0 UART1\n");
237 break;
238 case 0x80030000:
239 printf("SA11x0 UART2 (IRDA)\n");
240 break;
241 default:
242 printf("unknown SA11x0 UART\n");
243 break;
244 }
245
246 sacom_attach_subr(sc);
247
248 sa11x0_intr_establish(0, sa->sa_intr, 1, IPL_SERIAL, sacomintr, sc);
249 }
250
251 void
252 sacom_attach_subr(sc)
253 struct sacom_softc *sc;
254 {
255 bus_addr_t iobase = sc->sc_baseaddr;
256 bus_space_tag_t iot = sc->sc_iot;
257 struct tty *tp;
258
259 #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(COM_MPLOCK)
260 simple_lock_init(&sc->sc_lock);
261 #endif
262
263 /* XXX Do we need to disable interrupts here? */
264
265 if (iot == sacomconstag && iobase == sacomconsaddr) {
266 sacomconsattached = 1;
267 sc->sc_speed = SACOMSPEED(sacomconsrate);
268
269 /* Make sure the console is always "hardwired". */
270 delay(10000); /* wait for output to finish */
271 SET(sc->sc_hwflags, COM_HW_CONSOLE);
272 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
273 }
274
275 tp = ttymalloc();
276 tp->t_oproc = sacomstart;
277 tp->t_param = sacomparam;
278 tp->t_hwiflow = sacomhwiflow;
279
280 sc->sc_tty = tp;
281 sc->sc_rbuf = malloc(SACOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
282 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
283 sc->sc_rbavail = SACOM_RING_SIZE;
284 if (sc->sc_rbuf == NULL) {
285 printf("%s: unable to allocate ring buffer\n",
286 sc->sc_dev.dv_xname);
287 return;
288 }
289 sc->sc_ebuf = sc->sc_rbuf + (SACOM_RING_SIZE << 1);
290 sc->sc_tbc = 0;
291
292 tty_attach(tp);
293
294 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
295 int maj;
296
297 /* locate the major number */
298 for (maj = 0; maj < nchrdev; maj++)
299 if (cdevsw[maj].d_open == sacomopen)
300 break;
301
302 cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
303
304 delay(10000); /* XXX */
305 printf("%s: console\n", sc->sc_dev.dv_xname);
306 delay(10000); /* XXX */
307 }
308
309
310 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
311 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, sacomsoft, sc);
312 #endif
313
314 #if NRND > 0 && defined(RND_COM)
315 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
316 RND_TYPE_TTY, 0);
317 #endif
318
319 /* if there are no enable/disable functions, assume the device
320 is always enabled */
321 if (!sc->enable)
322 sc->enabled = 1;
323
324 sacom_config(sc);
325
326 SET(sc->sc_hwflags, COM_HW_DEV_OK);
327 }
328
329 /* This is necessary when dynamically changing SAIP configuration. */
330 int
331 sacom_detach(self, flags)
332 struct device *self;
333 int flags;
334 {
335 struct sacom_softc *sc = (struct sacom_softc *)self;
336 int maj, mn;
337
338 /* locate the major number */
339 for (maj = 0; maj < nchrdev; maj++)
340 if (cdevsw[maj].d_open == sacomopen)
341 break;
342
343 /* Nuke the vnodes for any open instances. */
344 mn = self->dv_unit;
345 vdevgone(maj, mn, mn, VCHR);
346
347 mn |= COMDIALOUT_MASK;
348 vdevgone(maj, mn, mn, VCHR);
349
350 /* Free the receive buffer. */
351 free(sc->sc_rbuf, M_DEVBUF);
352
353 /* Detach and free the tty. */
354 tty_detach(sc->sc_tty);
355 ttyfree(sc->sc_tty);
356
357 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
358 /* Unhook the soft interrupt handler. */
359 softintr_disestablish(sc->sc_si);
360 #endif
361
362 #if NRND > 0 && defined(RND_COM)
363 /* Unhook the entropy source. */
364 rnd_detach_source(&sc->rnd_source);
365 #endif
366
367 return (0);
368 }
369
370 void
371 sacom_config(sc)
372 struct sacom_softc *sc;
373 {
374 bus_space_tag_t iot = sc->sc_iot;
375 bus_space_handle_t ioh = sc->sc_ioh;
376
377 /* Disable engine before configuring the device. */
378 sc->sc_cr3 = 0;
379 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
380
381 #ifdef DDB
382 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
383 sacom_enable_debugport(sc);
384 #endif
385 }
386
387 #ifdef DDB
388 static void
389 sacom_enable_debugport(sc)
390 struct sacom_softc *sc;
391 {
392 bus_space_tag_t iot = sc->sc_iot;
393 bus_space_handle_t ioh = sc->sc_ioh;
394 int s;
395
396 s = splserial();
397 COM_LOCK(sc);
398 sc->sc_cr3 = CR3_RXE | CR3_TXE;
399 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
400 COM_UNLOCK(sc);
401 splx(s);
402 }
403 #endif
404
405 int
406 sacom_activate(self, act)
407 struct device *self;
408 enum devact act;
409 {
410 struct sacom_softc *sc = (struct sacom_softc *)self;
411 int s, rv = 0;
412
413 s = splserial();
414 COM_LOCK(sc);
415 switch (act) {
416 case DVACT_ACTIVATE:
417 rv = EOPNOTSUPP;
418 break;
419
420 case DVACT_DEACTIVATE:
421 if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB)) {
422 rv = EBUSY;
423 break;
424 }
425
426 if (sc->disable != NULL && sc->enabled != 0) {
427 (*sc->disable)(sc);
428 sc->enabled = 0;
429 }
430 break;
431 }
432
433 COM_UNLOCK(sc);
434 splx(s);
435 return (rv);
436 }
437
438 void
439 sacom_shutdown(sc)
440 struct sacom_softc *sc;
441 {
442 struct tty *tp = sc->sc_tty;
443 int s;
444
445 s = splserial();
446 COM_LOCK(sc);
447
448 /* Clear any break condition set with TIOCSBRK. */
449 sacom_break(sc, 0);
450
451 /*
452 * Hang up if necessary. Wait a bit, so the other side has time to
453 * notice even if we immediately open the port again.
454 * Avoid tsleeping above splhigh().
455 */
456 if (ISSET(tp->t_cflag, HUPCL)) {
457 sacom_modem(sc, 0);
458 COM_UNLOCK(sc);
459 splx(s);
460 /* XXX tsleep will only timeout */
461 (void) tsleep(sc, TTIPRI, ttclos, hz);
462 s = splserial();
463 COM_LOCK(sc);
464 }
465
466 /* Turn off interrupts. */
467 sc->sc_cr3 = 0;
468 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3, sc->sc_cr3);
469
470 if (sc->disable) {
471 #ifdef DIAGNOSTIC
472 if (!sc->enabled)
473 panic("sacom_shutdown: not enabled?");
474 #endif
475 (*sc->disable)(sc);
476 sc->enabled = 0;
477 }
478 COM_UNLOCK(sc);
479 splx(s);
480 }
481
482 int
483 sacomopen(dev, flag, mode, p)
484 dev_t dev;
485 int flag, mode;
486 struct proc *p;
487 {
488 struct sacom_softc *sc;
489 struct tty *tp;
490 int s, s2;
491 int error;
492
493 sc = device_lookup(&sacom_cd, COMUNIT(dev));
494 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
495 sc->sc_rbuf == NULL)
496 return (ENXIO);
497
498 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
499 return (ENXIO);
500
501 tp = sc->sc_tty;
502
503 if (ISSET(tp->t_state, TS_ISOPEN) &&
504 ISSET(tp->t_state, TS_XCLUDE) &&
505 p->p_ucred->cr_uid != 0)
506 return (EBUSY);
507
508 s = spltty();
509
510 /*
511 * Do the following iff this is a first open.
512 */
513 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
514 struct termios t;
515
516 tp->t_dev = dev;
517
518 s2 = splserial();
519 COM_LOCK(sc);
520
521 if (sc->enable) {
522 if ((*sc->enable)(sc)) {
523 COM_UNLOCK(sc);
524 splx(s2);
525 splx(s);
526 printf("%s: device enable failed\n",
527 sc->sc_dev.dv_xname);
528 return (EIO);
529 }
530 sc->enabled = 1;
531 sacom_config(sc);
532 }
533
534 /* Turn on interrupts. */
535 sc->sc_cr3 = CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE;
536 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3,
537 sc->sc_cr3);
538
539
540 COM_UNLOCK(sc);
541 splx(s2);
542
543 /*
544 * Initialize the termios status to the defaults. Add in the
545 * sticky bits from TIOCSFLAGS.
546 */
547 t.c_ispeed = 0;
548 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
549 t.c_ospeed = sacomconsrate;
550 t.c_cflag = sacomconscflag;
551 } else {
552 t.c_ospeed = TTYDEF_SPEED;
553 t.c_cflag = TTYDEF_CFLAG;
554 }
555 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
556 SET(t.c_cflag, CLOCAL);
557 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
558 SET(t.c_cflag, CRTSCTS);
559 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
560 SET(t.c_cflag, MDMBUF);
561 /* Make sure sacomparam() will do something. */
562 tp->t_ospeed = 0;
563 (void) sacomparam(tp, &t);
564 tp->t_iflag = TTYDEF_IFLAG;
565 tp->t_oflag = TTYDEF_OFLAG;
566 tp->t_lflag = TTYDEF_LFLAG;
567 ttychars(tp);
568 ttsetwater(tp);
569
570 s2 = splserial();
571 COM_LOCK(sc);
572
573 /*
574 * Turn on DTR. We must always do this, even if carrier is not
575 * present, because otherwise we'd have to use TIOCSDTR
576 * immediately after setting CLOCAL, which applications do not
577 * expect. We always assert DTR while the device is open
578 * unless explicitly requested to deassert it.
579 */
580 sacom_modem(sc, 1);
581
582 /* Clear the input ring, and unblock. */
583 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
584 sc->sc_rbavail = SACOM_RING_SIZE;
585 sacom_iflush(sc);
586 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
587 sacom_hwiflow(sc);
588
589 #ifdef COM_DEBUG
590 if (sacom_debug)
591 comstatus(sc, "sacomopen ");
592 #endif
593
594 COM_UNLOCK(sc);
595 splx(s2);
596 }
597
598 splx(s);
599
600 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
601 if (error)
602 goto bad;
603
604 error = (*tp->t_linesw->l_open)(dev, tp);
605 if (error)
606 goto bad;
607
608 return (0);
609
610 bad:
611 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
612 /*
613 * We failed to open the device, and nobody else had it opened.
614 * Clean up the state as appropriate.
615 */
616 sacom_shutdown(sc);
617 }
618
619 return (error);
620 }
621
622 int
623 sacomclose(dev, flag, mode, p)
624 dev_t dev;
625 int flag, mode;
626 struct proc *p;
627 {
628 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
629 struct tty *tp = sc->sc_tty;
630
631 /* XXX This is for cons.c. */
632 if (!ISSET(tp->t_state, TS_ISOPEN))
633 return (0);
634
635 (*tp->t_linesw->l_close)(tp, flag);
636 ttyclose(tp);
637
638 if (COM_ISALIVE(sc) == 0)
639 return (0);
640
641 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
642 /*
643 * Although we got a last close, the device may still be in
644 * use; e.g. if this was the dialout node, and there are still
645 * processes waiting for carrier on the non-dialout node.
646 */
647 sacom_shutdown(sc);
648 }
649
650 return (0);
651 }
652
653 int
654 sacomread(dev, uio, flag)
655 dev_t dev;
656 struct uio *uio;
657 int flag;
658 {
659 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
660 struct tty *tp = sc->sc_tty;
661
662 if (COM_ISALIVE(sc) == 0)
663 return (EIO);
664
665 return ((*tp->t_linesw->l_read)(tp, uio, flag));
666 }
667
668 int
669 sacomwrite(dev, uio, flag)
670 dev_t dev;
671 struct uio *uio;
672 int flag;
673 {
674 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
675 struct tty *tp = sc->sc_tty;
676
677 if (COM_ISALIVE(sc) == 0)
678 return (EIO);
679
680 return ((*tp->t_linesw->l_write)(tp, uio, flag));
681 }
682
683 int
684 sacompoll(dev, events, p)
685 dev_t dev;
686 int events;
687 struct proc *p;
688 {
689 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
690 struct tty *tp = sc->sc_tty;
691
692 if (COM_ISALIVE(sc) == 0)
693 return (EIO);
694
695 return ((*tp->t_linesw->l_poll)(tp, events, p));
696 }
697
698 struct tty *
699 sacomtty(dev)
700 dev_t dev;
701 {
702 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
703 struct tty *tp = sc->sc_tty;
704
705 return (tp);
706 }
707
708 int
709 sacomioctl(dev, cmd, data, flag, p)
710 dev_t dev;
711 u_long cmd;
712 caddr_t data;
713 int flag;
714 struct proc *p;
715 {
716 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
717 struct tty *tp = sc->sc_tty;
718 int error;
719 int s;
720
721 if (COM_ISALIVE(sc) == 0)
722 return (EIO);
723
724 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
725 if (error >= 0)
726 return (error);
727
728 error = ttioctl(tp, cmd, data, flag, p);
729 if (error >= 0)
730 return (error);
731
732 error = 0;
733
734 s = splserial();
735 COM_LOCK(sc);
736
737 switch (cmd) {
738 case TIOCSBRK:
739 sacom_break(sc, 1);
740 break;
741
742 case TIOCCBRK:
743 sacom_break(sc, 0);
744 break;
745
746 case TIOCSDTR:
747 sacom_modem(sc, 1);
748 break;
749
750 case TIOCCDTR:
751 sacom_modem(sc, 0);
752 break;
753
754 case TIOCGFLAGS:
755 *(int *)data = sc->sc_swflags;
756 break;
757
758 case TIOCSFLAGS:
759 error = suser(p->p_ucred, &p->p_acflag);
760 if (error)
761 break;
762 sc->sc_swflags = *(int *)data;
763 break;
764
765 case TIOCMSET:
766 case TIOCMBIS:
767 case TIOCMBIC:
768 tiocm_to_sacom(sc, cmd, *(int *)data);
769 break;
770
771 case TIOCMGET:
772 *(int *)data = sacom_to_tiocm(sc);
773 break;
774
775 default:
776 error = ENOTTY;
777 break;
778 }
779
780 COM_UNLOCK(sc);
781 splx(s);
782
783 #ifdef COM_DEBUG
784 if (sacom_debug)
785 comstatus(sc, "comioctl ");
786 #endif
787
788 return (error);
789 }
790
791 static inline void
792 sacom_schedrx(sc)
793 struct sacom_softc *sc;
794 {
795
796 sc->sc_rx_ready = 1;
797
798 /* Wake up the poller. */
799 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
800 softintr_schedule(sc->sc_si);
801 #else
802 setsoftserial();
803 #endif
804 }
805
806 void
807 sacom_break(sc, onoff)
808 struct sacom_softc *sc;
809 int onoff;
810 {
811
812 if (onoff)
813 SET(sc->sc_cr3, CR3_BRK);
814 else
815 CLR(sc->sc_cr3, CR3_BRK);
816
817 if (!sc->sc_heldchange) {
818 if (sc->sc_tx_busy) {
819 sc->sc_heldtbc = sc->sc_tbc;
820 sc->sc_tbc = 0;
821 sc->sc_heldchange = 1;
822 } else
823 sacom_loadchannelregs(sc);
824 }
825 }
826
827 void
828 sacom_modem(sc, onoff)
829 struct sacom_softc *sc;
830 int onoff;
831 {
832 if (!sc->sc_heldchange) {
833 if (sc->sc_tx_busy) {
834 sc->sc_heldtbc = sc->sc_tbc;
835 sc->sc_tbc = 0;
836 sc->sc_heldchange = 1;
837 } else
838 sacom_loadchannelregs(sc);
839 }
840 }
841
842 void
843 tiocm_to_sacom(sc, how, ttybits)
844 struct sacom_softc *sc;
845 u_long how;
846 int ttybits;
847 {
848 }
849
850 int
851 sacom_to_tiocm(sc)
852 struct sacom_softc *sc;
853 {
854 int ttybits = 0;
855
856 if (sc->sc_cr3 != 0)
857 SET(ttybits, TIOCM_LE);
858
859 return (ttybits);
860 }
861
862 static u_int
863 cflag2cr0(cflag)
864 tcflag_t cflag;
865 {
866 u_int cr0;
867
868 cr0 = (cflag & PARENB) ? CR0_PE : 0;
869 cr0 |= (cflag & PARODD) ? 0 : CR0_OES;
870 cr0 |= (cflag & CSTOPB) ? CR0_SBS : 0;
871 cr0 |= ((cflag & CSIZE) == CS8) ? CR0_DSS : 0;
872
873 return (cr0);
874 }
875
876 int
877 sacomparam(tp, t)
878 struct tty *tp;
879 struct termios *t;
880 {
881 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
882 int ospeed = SACOMSPEED(t->c_ospeed);
883 u_int cr0;
884 int s;
885
886 if (COM_ISALIVE(sc) == 0)
887 return (EIO);
888
889 /* Check requested parameters. */
890 if (ospeed < 0)
891 return (EINVAL);
892 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
893 return (EINVAL);
894
895 /*
896 * For the console, always force CLOCAL and !HUPCL, so that the port
897 * is always active.
898 */
899 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
900 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
901 SET(t->c_cflag, CLOCAL);
902 CLR(t->c_cflag, HUPCL);
903 }
904
905 /*
906 * If there were no changes, don't do anything. This avoids dropping
907 * input and improves performance when all we did was frob things like
908 * VMIN and VTIME.
909 */
910 if (tp->t_ospeed == t->c_ospeed &&
911 tp->t_cflag == t->c_cflag)
912 return (0);
913
914 cr0 = cflag2cr0(t->c_cflag);
915
916 s = splserial();
917 COM_LOCK(sc);
918
919 sc->sc_cr0 = cr0;
920
921 sc->sc_speed = ospeed;
922
923 /* And copy to tty. */
924 tp->t_ispeed = 0;
925 tp->t_ospeed = t->c_ospeed;
926 tp->t_cflag = t->c_cflag;
927
928 if (!sc->sc_heldchange) {
929 if (sc->sc_tx_busy) {
930 sc->sc_heldtbc = sc->sc_tbc;
931 sc->sc_tbc = 0;
932 sc->sc_heldchange = 1;
933 } else
934 sacom_loadchannelregs(sc);
935 }
936
937 if (!ISSET(t->c_cflag, CHWFLOW)) {
938 /* Disable the high water mark. */
939 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
940 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
941 sacom_schedrx(sc);
942 }
943 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
944 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
945 sacom_hwiflow(sc);
946 }
947 }
948
949 COM_UNLOCK(sc);
950 splx(s);
951
952 (void) (*tp->t_linesw->l_modem)(tp, 1);
953
954 #ifdef COM_DEBUG
955 if (sacom_debug)
956 comstatus(sc, "comparam ");
957 #endif
958
959 return (0);
960 }
961
962 void
963 sacom_iflush(sc)
964 struct sacom_softc *sc;
965 {
966 bus_space_tag_t iot = sc->sc_iot;
967 bus_space_handle_t ioh = sc->sc_ioh;
968 #ifdef DIAGNOSTIC
969 int reg;
970 #endif
971 int timo;
972
973 #ifdef DIAGNOSTIC
974 reg = 0xffff;
975 #endif
976 timo = 50;
977 /* flush any pending I/O */
978 if (sc->sc_cr3 & CR3_RXE) {
979 while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_RNE)
980 && --timo)
981 #ifdef DIAGNOSTIC
982 reg =
983 #else
984 (void)
985 #endif
986 bus_space_read_4(iot, ioh, SACOM_DR);
987 }
988 #if 0
989 /* XXX is it good idea to wait TX finish? */
990 if (sc->sc_cr3 & CR3_TXE) {
991 timo = 500;
992 while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_TBY)
993 && --timo)
994 delay(100);
995 }
996 #endif
997 #ifdef DIAGNOSTIC
998 if (!timo)
999 printf("%s: sacom_iflush timeout %02x\n", sc->sc_dev.dv_xname,
1000 reg);
1001 #endif
1002 }
1003
1004 void
1005 sacom_loadchannelregs(sc)
1006 struct sacom_softc *sc;
1007 {
1008 bus_space_tag_t iot = sc->sc_iot;
1009 bus_space_handle_t ioh = sc->sc_ioh;
1010
1011 /* XXXXX necessary? */
1012 sacom_iflush(sc);
1013
1014 /* Need to stop engines first. */
1015 bus_space_write_4(iot, ioh, SACOM_CR3, 0);
1016
1017 bus_space_write_4(iot, ioh, SACOM_CR1, sc->sc_speed >> 8);
1018 bus_space_write_4(iot, ioh, SACOM_CR2, sc->sc_speed & 0xff);
1019
1020 bus_space_write_4(iot, ioh, SACOM_CR0, sc->sc_cr0);
1021 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
1022 }
1023
1024 int
1025 sacomhwiflow(tp, block)
1026 struct tty *tp;
1027 int block;
1028 {
1029 #if 0
1030 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
1031 int s;
1032
1033 if (COM_ISALIVE(sc) == 0)
1034 return (0);
1035
1036 if (sc->sc_mcr_rts == 0)
1037 return (0);
1038
1039 s = splserial();
1040 COM_LOCK(sc);
1041
1042 if (block) {
1043 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1044 SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
1045 sacom_hwiflow(sc);
1046 }
1047 } else {
1048 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1049 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1050 sacom_schedrx(sc);
1051 }
1052 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1053 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
1054 sacom_hwiflow(sc);
1055 }
1056 }
1057
1058 COM_UNLOCK(sc);
1059 splx(s);
1060 #endif
1061 return (1);
1062 }
1063
1064 /*
1065 * (un)block input via hw flowcontrol
1066 */
1067 void
1068 sacom_hwiflow(sc)
1069 struct sacom_softc *sc;
1070 {
1071 #if 0
1072 bus_space_tag_t iot = sc->sc_iot;
1073 bus_space_handle_t ioh = sc->sc_ioh;
1074
1075 /* XXX implement */
1076 #endif
1077 }
1078
1079
1080 void
1081 sacomstart(tp)
1082 struct tty *tp;
1083 {
1084 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
1085 bus_space_tag_t iot = sc->sc_iot;
1086 bus_space_handle_t ioh = sc->sc_ioh;
1087 int s;
1088
1089 if (COM_ISALIVE(sc) == 0)
1090 return;
1091
1092 s = spltty();
1093 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1094 goto out;
1095
1096 if (tp->t_outq.c_cc <= tp->t_lowat) {
1097 if (ISSET(tp->t_state, TS_ASLEEP)) {
1098 CLR(tp->t_state, TS_ASLEEP);
1099 wakeup(&tp->t_outq);
1100 }
1101 selwakeup(&tp->t_wsel);
1102 if (tp->t_outq.c_cc == 0)
1103 goto out;
1104 }
1105
1106 /* Grab the first contiguous region of buffer space. */
1107 {
1108 u_char *tba;
1109 int tbc;
1110
1111 tba = tp->t_outq.c_cf;
1112 tbc = ndqb(&tp->t_outq, 0);
1113
1114 (void)splserial();
1115 COM_LOCK(sc);
1116
1117 sc->sc_tba = tba;
1118 sc->sc_tbc = tbc;
1119 }
1120
1121 SET(tp->t_state, TS_BUSY);
1122 sc->sc_tx_busy = 1;
1123
1124 /* Enable transmit completion interrupts if necessary. */
1125 if (!ISSET(sc->sc_cr3, CR3_TIE)) {
1126 SET(sc->sc_cr3, CR3_TIE);
1127 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
1128 }
1129
1130 /* Output the first chunk of the contiguous buffer. */
1131 sacom_filltx(sc);
1132
1133 COM_UNLOCK(sc);
1134 out:
1135 splx(s);
1136 return;
1137 }
1138
1139 void
1140 sacom_filltx(sc)
1141 struct sacom_softc *sc;
1142 {
1143 bus_space_tag_t iot = sc->sc_iot;
1144 bus_space_handle_t ioh = sc->sc_ioh;
1145 int c, n;
1146
1147 n = 0;
1148 while(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
1149 & SR1_TNF) {
1150 if (n == SACOM_TXFIFOLEN || n == sc->sc_tbc)
1151 break;
1152 c = *(sc->sc_tba + n);
1153 c &= 0xff;
1154 bus_space_write_4(iot, ioh, SACOM_DR, c);
1155 n++;
1156 }
1157 sc->sc_tbc -= n;
1158 sc->sc_tba += n;
1159 }
1160
1161 /*
1162 * Stop output on a line.
1163 */
1164 void
1165 sacomstop(tp, flag)
1166 struct tty *tp;
1167 int flag;
1168 {
1169 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
1170 int s;
1171
1172 s = splserial();
1173 COM_LOCK(sc);
1174 if (ISSET(tp->t_state, TS_BUSY)) {
1175 /* Stop transmitting at the next chunk. */
1176 sc->sc_tbc = 0;
1177 sc->sc_heldtbc = 0;
1178 if (!ISSET(tp->t_state, TS_TTSTOP))
1179 SET(tp->t_state, TS_FLUSH);
1180 }
1181 COM_UNLOCK(sc);
1182 splx(s);
1183 }
1184
1185 static inline void
1186 sacom_rxsoft(sc, tp)
1187 struct sacom_softc *sc;
1188 struct tty *tp;
1189 {
1190 int (*rint)(int c, struct tty *tp) = tp->t_linesw->l_rint;
1191 u_char *get, *end;
1192 u_int cc, scc;
1193 u_char sr1;
1194 int code;
1195 int s;
1196
1197 end = sc->sc_ebuf;
1198 get = sc->sc_rbget;
1199 scc = cc = SACOM_RING_SIZE - sc->sc_rbavail;
1200
1201 while (cc) {
1202 code = get[0];
1203 sr1 = get[1];
1204 if (ISSET(sr1, SR1_FRE))
1205 SET(code, TTY_FE);
1206 if (ISSET(sr1, SR1_PRE))
1207 SET(code, TTY_PE);
1208 if ((*rint)(code, tp) == -1) {
1209 /*
1210 * The line discipline's buffer is out of space.
1211 */
1212 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1213 /*
1214 * We're either not using flow control, or the
1215 * line discipline didn't tell us to block for
1216 * some reason. Either way, we have no way to
1217 * know when there's more space available, so
1218 * just drop the rest of the data.
1219 */
1220 get += cc << 1;
1221 if (get >= end)
1222 get -= SACOM_RING_SIZE << 1;
1223 cc = 0;
1224 } else {
1225 /*
1226 * Don't schedule any more receive processing
1227 * until the line discipline tells us there's
1228 * space available (through comhwiflow()).
1229 * Leave the rest of the data in the input
1230 * buffer.
1231 */
1232 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1233 }
1234 break;
1235 }
1236 get += 2;
1237 if (get >= end)
1238 get = sc->sc_rbuf;
1239 cc--;
1240 }
1241
1242 if (cc != scc) {
1243 sc->sc_rbget = get;
1244 s = splserial();
1245 COM_LOCK(sc);
1246
1247 cc = sc->sc_rbavail += scc - cc;
1248 /* Buffers should be ok again, release possible block. */
1249 if (cc >= 1) {
1250 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1251 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1252 SET(sc->sc_cr3, CR3_RIE);
1253 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
1254 SACOM_CR3, sc->sc_cr3);
1255 }
1256 }
1257 COM_UNLOCK(sc);
1258 splx(s);
1259 }
1260 }
1261
1262 static inline void
1263 sacom_txsoft(sc, tp)
1264 struct sacom_softc *sc;
1265 struct tty *tp;
1266 {
1267
1268 CLR(tp->t_state, TS_BUSY);
1269 if (ISSET(tp->t_state, TS_FLUSH))
1270 CLR(tp->t_state, TS_FLUSH);
1271 else
1272 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1273 (*tp->t_linesw->l_start)(tp);
1274 }
1275
1276 static inline void
1277 sacom_stsoft(sc, tp)
1278 struct sacom_softc *sc;
1279 struct tty *tp;
1280 {
1281 }
1282
1283 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1284 void
1285 sacomsoft(arg)
1286 void *arg;
1287 {
1288 struct sacom_softc *sc = arg;
1289 struct tty *tp;
1290
1291 if (COM_ISALIVE(sc) == 0)
1292 return;
1293
1294 {
1295 #else
1296 void
1297 sacomsoft()
1298 {
1299 struct sacom_softc *sc;
1300 struct tty *tp;
1301 int unit;
1302
1303 for (unit = 0; unit < sacom_cd.cd_ndevs; unit++) {
1304 sc = device_lookup(&sacom_cd, unit);
1305 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
1306 continue;
1307
1308 if (COM_ISALIVE(sc) == 0)
1309 continue;
1310
1311 tp = sc->sc_tty;
1312 if (tp == NULL)
1313 continue;
1314 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
1315 continue;
1316 #endif
1317 tp = sc->sc_tty;
1318
1319 if (sc->sc_rx_ready) {
1320 sc->sc_rx_ready = 0;
1321 sacom_rxsoft(sc, tp);
1322 }
1323
1324 if (sc->sc_st_check) {
1325 sc->sc_st_check = 0;
1326 sacom_stsoft(sc, tp);
1327 }
1328
1329 if (sc->sc_tx_done) {
1330 sc->sc_tx_done = 0;
1331 sacom_txsoft(sc, tp);
1332 }
1333 }
1334
1335 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
1336 #endif
1337 }
1338
1339 #ifdef __ALIGN_BRACKET_LEVEL_FOR_CTAGS
1340 /* there has got to be a better way to do comsoft() */
1341 }}
1342 #endif
1343
1344 int
1345 sacomintr(arg)
1346 void *arg;
1347 {
1348 struct sacom_softc *sc = arg;
1349 bus_space_tag_t iot = sc->sc_iot;
1350 bus_space_handle_t ioh = sc->sc_ioh;
1351 u_char *put, *end;
1352 u_int cc;
1353 u_int sr0, sr1;
1354
1355 if (COM_ISALIVE(sc) == 0)
1356 return (0);
1357
1358 COM_LOCK(sc);
1359 sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
1360 if (! sr0) {
1361 COM_UNLOCK(sc);
1362 return (0);
1363 }
1364 if (ISSET(sr0, SR0_EIF))
1365 /* XXX silently discard error bits */
1366 bus_space_read_4(iot, ioh, SACOM_DR);
1367 if (ISSET(sr0, SR0_RBB))
1368 bus_space_write_4(iot, ioh, SACOM_SR0, SR0_RBB);
1369 if (ISSET(sr0, SR0_REB)) {
1370 bus_space_write_4(iot, ioh, SACOM_SR0, SR0_REB);
1371 #if defined(DDB) || defined(KGDB)
1372 #ifndef DDB_BREAK_CHAR
1373 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1374 console_debugger();
1375 }
1376 #endif
1377 #endif /* DDB || KGDB */
1378 }
1379
1380
1381 end = sc->sc_ebuf;
1382 put = sc->sc_rbput;
1383 cc = sc->sc_rbavail;
1384
1385 sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
1386 if (ISSET(sr0, SR0_RFS | SR0_RID)) {
1387 if (! ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1388 while (cc > 0) {
1389 if (!ISSET(sr1, SR1_RNE)) {
1390 bus_space_write_4(iot, ioh, SACOM_SR0,
1391 SR0_RID);
1392 break;
1393 }
1394 put[0] = bus_space_read_4(iot, ioh, SACOM_DR);
1395 put[1] = sr1;
1396 #if defined(DDB) && defined(DDB_BREAK_CHAR)
1397 if (put[0] == DDB_BREAK_CHAR &&
1398 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1399 console_debugger();
1400
1401 sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
1402 continue;
1403 }
1404 #endif
1405 put += 2;
1406 if (put >= end)
1407 put = sc->sc_rbuf;
1408 cc--;
1409
1410 sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
1411 }
1412
1413 /*
1414 * Current string of incoming characters ended because
1415 * no more data was available or we ran out of space.
1416 * Schedule a receive event if any data was received.
1417 * If we're out of space, turn off receive interrupts.
1418 */
1419 sc->sc_rbput = put;
1420 sc->sc_rbavail = cc;
1421 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1422 sc->sc_rx_ready = 1;
1423
1424 /* XXX do RX hardware flow control */
1425
1426 /*
1427 * If we're out of space, disable receive interrupts
1428 * until the queue has drained a bit.
1429 */
1430 if (!cc) {
1431 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1432 CLR(sc->sc_cr3, CR3_RIE);
1433 bus_space_write_4(iot, ioh, SACOM_CR3,
1434 sc->sc_cr3);
1435 }
1436 } else {
1437 #ifdef DIAGNOSTIC
1438 panic("sacomintr: we shouldn't reach here\n");
1439 #endif
1440 CLR(sc->sc_cr3, CR3_RIE);
1441 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
1442 }
1443 }
1444
1445 /*
1446 * Done handling any receive interrupts. See if data can be
1447 * transmitted as well. Schedule tx done event if no data left
1448 * and tty was marked busy.
1449 */
1450 sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
1451 if (ISSET(sr0, SR0_TFS)) {
1452 /*
1453 * If we've delayed a parameter change, do it now, and restart
1454 * output.
1455 * XXX sacom_loadchanelregs() waits TX completion,
1456 * XXX resulting in ~0.1s hang (300bps, 4 bytes) in worst case
1457 */
1458 if (sc->sc_heldchange) {
1459 sacom_loadchannelregs(sc);
1460 sc->sc_heldchange = 0;
1461 sc->sc_tbc = sc->sc_heldtbc;
1462 sc->sc_heldtbc = 0;
1463 }
1464
1465 /* Output the next chunk of the contiguous buffer, if any. */
1466 if (sc->sc_tbc > 0) {
1467 sacom_filltx(sc);
1468 } else {
1469 /* Disable transmit completion interrupts if necessary. */
1470 if (ISSET(sc->sc_cr3, CR3_TIE)) {
1471 CLR(sc->sc_cr3, CR3_TIE);
1472 bus_space_write_4(iot, ioh, SACOM_CR3,
1473 sc->sc_cr3);
1474 }
1475 if (sc->sc_tx_busy) {
1476 sc->sc_tx_busy = 0;
1477 sc->sc_tx_done = 1;
1478 }
1479 }
1480 }
1481 COM_UNLOCK(sc);
1482
1483 /* Wake up the poller. */
1484 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1485 softintr_schedule(sc->sc_si);
1486 #else
1487 setsoftserial();
1488 #endif
1489
1490 #if NRND > 0 && defined(RND_COM)
1491 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1492 #endif
1493 return (1);
1494 }
1495
1496 /* Initialization for serial console */
1497 int
1498 sacominit(iot, iobase, baud, cflag, iohp)
1499 bus_space_tag_t iot;
1500 bus_addr_t iobase;
1501 int baud;
1502 tcflag_t cflag;
1503 bus_space_handle_t *iohp;
1504 {
1505 int brd, cr0;
1506
1507 if (bus_space_map(iot, iobase, SACOM_NPORTS, 0, iohp))
1508 printf("register map failed\n");
1509
1510 /* wait for the Tx queue to drain and disable the UART */
1511 while(bus_space_read_4(iot, *iohp, SACOM_SR1) & SR1_TBY)
1512 ;
1513 bus_space_write_4(iot, *iohp, SACOM_CR3, 0);
1514
1515 cr0 = cflag2cr0(cflag);
1516 bus_space_write_4(iot, *iohp, SACOM_CR0, cr0);
1517
1518 brd = SACOMSPEED(baud);
1519 sacomconsrate = baud;
1520 sacomconsaddr = iobase;
1521 sacomconscflag = cflag;
1522 /* XXX assumes little endian */
1523 bus_space_write_4(iot, *iohp, SACOM_CR1, brd >> 8);
1524 bus_space_write_4(iot, *iohp, SACOM_CR2, brd & 0xff);
1525
1526 /* enable the UART */
1527 bus_space_write_4(iot, *iohp, SACOM_CR3, CR3_RXE | CR3_TXE);
1528
1529 return (0);
1530 }
1531
1532 void
1533 sacomcnprobe(cp)
1534 struct consdev *cp;
1535 {
1536 cp->cn_pri = CN_REMOTE;
1537 }
1538
1539 void
1540 sacomcninit(cp)
1541 struct consdev *cp;
1542 {
1543 if (cp == NULL) {
1544 /* XXX cp == NULL means that MMU is disabled. */
1545 sacomconsioh = SACOM3_HW_BASE;
1546 sacomconstag = &sa11x0_bs_tag;
1547 cn_tab = &sacomcons;
1548 return;
1549 }
1550
1551 if (sacominit(&sa11x0_bs_tag, CONADDR, CONSPEED,
1552 CONMODE, &sacomconsioh))
1553 panic("can't init serial console @%x", CONADDR);
1554 cn_tab = &sacomcons;
1555 sacomconstag = &sa11x0_bs_tag;
1556 }
1557
1558 int
1559 sacomcngetc(dev)
1560 dev_t dev;
1561 {
1562 int c, s;
1563
1564 s = spltty(); /* XXX do we need this? */
1565
1566 while(! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
1567 & SR1_RNE)) {
1568 #if defined(DDB) || defined(KGDB)
1569 #ifndef DDB_BREAK_CHAR
1570 u_int sr0;
1571 extern int db_active;
1572
1573 sr0 = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR0);
1574 if (ISSET(sr0, SR0_RBB))
1575 bus_space_write_4(sacomconstag, sacomconsioh,
1576 SACOM_SR0, SR0_RBB);
1577 if (ISSET(sr0, SR0_REB)) {
1578 bus_space_write_4(sacomconstag, sacomconsioh,
1579 SACOM_SR0, SR0_REB);
1580 if (db_active == 0)
1581 console_debugger();
1582 }
1583 #endif
1584 #endif /* DDB || KGDB */
1585 }
1586
1587 c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
1588 c &= 0xff;
1589 splx(s);
1590
1591 return (c);
1592 }
1593
1594 void
1595 sacomcnputc(dev, c)
1596 dev_t dev;
1597 int c;
1598 {
1599 int s;
1600
1601 s = spltty(); /* XXX do we need this? */
1602
1603 while(! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
1604 & SR1_TNF))
1605 ;
1606
1607 bus_space_write_4(sacomconstag, sacomconsioh, SACOM_DR, c);
1608 splx(s);
1609 }
1610
1611 void
1612 sacomcnpollc(dev, on)
1613 dev_t dev;
1614 int on;
1615 {
1616
1617 }
1618
1619 #ifdef DEBUG
1620 int
1621 sacomcncharpoll()
1622 {
1623 int c;
1624
1625 if (! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
1626 & SR1_RNE))
1627 return -1;
1628
1629 c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
1630 c &= 0xff;
1631
1632 return (c);
1633 }
1634
1635 #endif
1636
1637 /* helper function to identify the com ports used by
1638 console or KGDB (and not yet autoconf attached) */
1639 int
1640 sacom_is_console(iot, iobase, ioh)
1641 bus_space_tag_t iot;
1642 bus_addr_t iobase;
1643 bus_space_handle_t *ioh;
1644 {
1645 bus_space_handle_t help;
1646
1647 if (! sacomconsattached &&
1648 iot == sacomconstag && iobase == sacomconsaddr)
1649 help = sacomconsioh;
1650 else
1651 return (0);
1652
1653 if (ioh)
1654 *ioh = help;
1655 return (1);
1656 }
1657