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