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