Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / arm / sa11x0 / sa11x0_com.c
blob0a6796ff08518f90628b8a6a9a205c648b014b04
1 /* $NetBSD: sa11x0_com.c,v 1.45 2009/05/29 14:15:44 rjs Exp $ */
3 /*-
4 * Copyright (c) 1998, 1999, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by IWAMOTO Toshihiro.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Charles M. Hannum.
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.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
36 * Copyright (c) 1991 The Regents of the University of California.
37 * All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
63 * @(#)com.c 7.5 (Berkeley) 5/16/91
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: sa11x0_com.c,v 1.45 2009/05/29 14:15:44 rjs Exp $");
69 #include "opt_com.h"
70 #include "opt_ddb.h"
71 #include "opt_ddbparam.h"
72 #include "opt_kgdb.h"
73 #include "opt_multiprocessor.h"
74 #include "opt_lockdebug.h"
76 #include "rnd.h"
77 #if NRND > 0 && defined(RND_COM)
78 #include <sys/rnd.h>
79 #endif
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/types.h>
84 #include <sys/conf.h>
85 #include <sys/file.h>
86 #include <sys/device.h>
87 #include <sys/kernel.h>
88 #include <sys/malloc.h>
89 #include <sys/tty.h>
90 #include <sys/uio.h>
91 #include <sys/vnode.h>
92 #include <sys/kauth.h>
94 #include <dev/cons.h>
96 #include <machine/bus.h>
97 #include <arm/sa11x0/sa11x0_reg.h>
98 #include <arm/sa11x0/sa11x0_var.h>
99 #include <arm/sa11x0/sa11x0_comreg.h>
100 #include <arm/sa11x0/sa11x0_comvar.h>
101 #include <arm/sa11x0/sa11x0_gpioreg.h>
103 #ifdef hpcarm
104 #include <hpc/include/platid.h>
105 #include <hpc/include/platid_mask.h>
106 #endif
108 #include "sacom.h"
110 dev_type_open(sacomopen);
111 dev_type_close(sacomclose);
112 dev_type_read(sacomread);
113 dev_type_write(sacomwrite);
114 dev_type_ioctl(sacomioctl);
115 dev_type_stop(sacomstop);
116 dev_type_tty(sacomtty);
117 dev_type_poll(sacompoll);
119 const struct cdevsw sacom_cdevsw = {
120 sacomopen, sacomclose, sacomread, sacomwrite, sacomioctl,
121 sacomstop, sacomtty, sacompoll, nommap, ttykqfilter, D_TTY
124 static int sacom_match(device_t, cfdata_t, void *);
125 static void sacom_attach(device_t, device_t, void *);
126 static void sacom_filltx(struct sacom_softc *);
127 static void sacom_attach_subr(struct sacom_softc *);
128 #if defined(DDB) || defined(KGDB)
129 static void sacom_enable_debugport(struct sacom_softc *);
130 #endif
131 int sacom_detach(device_t, int);
132 void sacom_config(struct sacom_softc *);
133 int sacom_activate(device_t, enum devact);
134 void sacom_shutdown(struct sacom_softc *);
135 static u_int cflag2cr0(tcflag_t);
136 int sacomparam(struct tty *, struct termios *);
137 void sacomstart(struct tty *);
138 int sacomhwiflow(struct tty *, int);
140 void sacom_loadchannelregs(struct sacom_softc *);
141 void sacom_hwiflow(struct sacom_softc *);
142 void sacom_break(struct sacom_softc *, int);
143 void sacom_modem(struct sacom_softc *, int);
144 void tiocm_to_sacom(struct sacom_softc *, u_long, int);
145 int sacom_to_tiocm(struct sacom_softc *);
146 void sacom_iflush(struct sacom_softc *);
148 int sacominit(bus_space_tag_t, bus_addr_t, int, tcflag_t,
149 bus_space_handle_t *);
150 int sacom_is_console(bus_space_tag_t, bus_addr_t,
151 bus_space_handle_t *);
153 void sacomsoft(void *);
155 static inline void sacom_rxsoft(struct sacom_softc *, struct tty *);
156 static inline void sacom_txsoft(struct sacom_softc *, struct tty *);
157 static inline void sacom_stsoft(struct sacom_softc *, struct tty *);
158 static inline void sacom_schedrx(struct sacom_softc *);
160 #ifdef hpcarm
161 /* HPCARM specific functions */
162 static void sacom_j720_init(struct sa11x0_softc *, struct sacom_softc *);
163 #endif
165 #define COMUNIT_MASK 0x7ffff
166 #define COMDIALOUT_MASK 0x80000
168 #define COMUNIT(x) (minor(x) & COMUNIT_MASK)
169 #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK)
171 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && \
172 device_is_active((sc)->sc_dev))
174 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
175 #define COM_LOCK(sc)
176 #define COM_UNLOCK(sc)
178 int sacomintr(void *);
179 int sacomcngetc(dev_t);
180 void sacomcnputc(dev_t, int);
181 void sacomcnpollc(dev_t, int);
183 void sacomcnprobe(struct consdev *);
184 void sacomcninit(struct consdev *);
186 extern struct bus_space sa11x0_bs_tag;
188 static bus_space_tag_t sacomconstag;
189 static bus_space_handle_t sacomconsioh;
190 static bus_addr_t sacomconsaddr = SACOM3_BASE; /* XXX */
192 static int sacomconsattached;
193 static int sacomconsrate;
194 static tcflag_t sacomconscflag;
196 CFATTACH_DECL_NEW(sacom, sizeof(struct sacom_softc),
197 sacom_match, sacom_attach, NULL, NULL);
198 extern struct cfdriver sacom_cd;
200 #ifdef hpcarm
201 struct platid_data sacom_platid_table[] = {
202 { &platid_mask_MACH_HP_JORNADA_7XX, sacom_j720_init },
203 { NULL, NULL }
205 #endif
207 struct consdev sacomcons = {
208 NULL, NULL, sacomcngetc, sacomcnputc, sacomcnpollc, NULL,
209 NULL, NULL, NODEV, CN_NORMAL
212 #ifndef CONMODE
213 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
214 #endif
215 #ifndef CONSPEED
216 #define CONSPEED 9600
217 #endif
218 #ifndef CONADDR
219 #define CONADDR SACOM3_BASE
220 #endif
222 static int
223 sacom_match(device_t parent, cfdata_t match, void *aux)
226 return 1;
229 void
230 sacom_attach(device_t parent, device_t self, void *aux)
232 struct sacom_softc *sc = device_private(self);
233 struct sa11x0_attach_args *sa = aux;
235 #ifdef hpcarm
236 struct platid_data *p;
237 void (*mdinit)(device_t, struct sacom_softc *);
238 #endif
240 aprint_normal("\n");
242 sc->sc_dev = self;
243 sc->sc_iot = sa->sa_iot;
244 sc->sc_baseaddr = sa->sa_addr;
246 if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
247 &sc->sc_ioh)) {
248 aprint_normal_dev(self, "unable to map registers\n");
249 return;
252 switch (sc->sc_baseaddr) {
253 case SACOM1_BASE:
254 aprint_normal_dev(self, "SA-11x0 UART1\n");
255 break;
256 case SACOM2_BASE:
257 aprint_normal_dev(self, "SA-11x0 UART2 (IRDA)\n");
258 break;
259 case SACOM3_BASE:
260 aprint_normal_dev(self, "SA-11x0 UART3\n");
261 break;
262 default:
263 aprint_normal_dev(self, "unknown SA-11x0 UART\n");
264 break;
267 sacom_attach_subr(sc);
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);
275 #endif
277 sa11x0_intr_establish(0, sa->sa_intr, 1, IPL_SERIAL, sacomintr, sc);
280 void
281 sacom_attach_subr(struct sacom_softc *sc)
283 bus_addr_t iobase = sc->sc_baseaddr;
284 bus_space_tag_t iot = sc->sc_iot;
285 struct tty *tp;
287 /* XXX Do we need to disable interrupts here? */
289 if (iot == sacomconstag && iobase == sacomconsaddr) {
290 sacomconsattached = 1;
291 sc->sc_speed = SACOMSPEED(sacomconsrate);
293 /* Make sure the console is always "hardwired". */
294 delay(10000); /* wait for output to finish */
295 SET(sc->sc_hwflags, COM_HW_CONSOLE);
296 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
299 tp = ttymalloc();
300 tp->t_oproc = sacomstart;
301 tp->t_param = sacomparam;
302 tp->t_hwiflow = sacomhwiflow;
304 sc->sc_tty = tp;
305 sc->sc_rbuf = malloc(SACOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
306 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
307 sc->sc_rbavail = SACOM_RING_SIZE;
308 if (sc->sc_rbuf == NULL) {
309 aprint_normal_dev(sc->sc_dev, "unable to allocate ring buffer\n");
310 return;
312 sc->sc_ebuf = sc->sc_rbuf + (SACOM_RING_SIZE << 1);
313 sc->sc_tbc = 0;
315 tty_attach(tp);
317 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
318 int maj;
320 /* locate the major number */
321 maj = cdevsw_lookup_major(&sacom_cdevsw);
323 cn_tab->cn_dev = makedev(maj, device_unit(sc->sc_dev));
325 delay(10000); /* XXX */
326 aprint_normal_dev(sc->sc_dev, "console\n");
327 delay(10000); /* XXX */
331 sc->sc_si = softint_establish(SOFTINT_SERIAL, sacomsoft, sc);
333 #if NRND > 0 && defined(RND_COM)
334 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
335 RND_TYPE_TTY, 0);
336 #endif
338 /* if there are no enable/disable functions, assume the device
339 is always enabled */
340 if (!sc->enable)
341 sc->enabled = 1;
343 sacom_config(sc);
345 SET(sc->sc_hwflags, COM_HW_DEV_OK);
348 /* This is necessary when dynamically changing SAIP configuration. */
350 sacom_detach(device_t dev, int flags)
352 struct sacom_softc *sc = device_private(dev);
353 int maj, mn;
355 if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB))
356 return EBUSY;
358 if (sc->disable != NULL && sc->enabled != 0) {
359 (*sc->disable)(sc);
360 sc->enabled = 0;
363 /* locate the major number */
364 maj = cdevsw_lookup_major(&sacom_cdevsw);
366 /* Nuke the vnodes for any open instances. */
367 mn = device_unit(dev);
368 vdevgone(maj, mn, mn, VCHR);
370 mn |= COMDIALOUT_MASK;
371 vdevgone(maj, mn, mn, VCHR);
373 /* Free the receive buffer. */
374 free(sc->sc_rbuf, M_DEVBUF);
376 /* Detach and free the tty. */
377 tty_detach(sc->sc_tty);
378 ttyfree(sc->sc_tty);
380 /* Unhook the soft interrupt handler. */
381 softint_disestablish(sc->sc_si);
383 #if NRND > 0 && defined(RND_COM)
384 /* Unhook the entropy source. */
385 rnd_detach_source(&sc->rnd_source);
386 #endif
388 return 0;
391 void
392 sacom_config(struct sacom_softc *sc)
394 bus_space_tag_t iot = sc->sc_iot;
395 bus_space_handle_t ioh = sc->sc_ioh;
397 /* Disable engine before configuring the device. */
398 sc->sc_cr3 = 0;
399 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
401 #ifdef DDB
402 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
403 sacom_enable_debugport(sc);
404 #endif
407 #ifdef DDB
408 static void
409 sacom_enable_debugport(struct sacom_softc *sc)
411 bus_space_tag_t iot = sc->sc_iot;
412 bus_space_handle_t ioh = sc->sc_ioh;
413 int s;
415 s = splserial();
416 COM_LOCK(sc);
417 sc->sc_cr3 = CR3_RXE | CR3_TXE;
418 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
419 COM_UNLOCK(sc);
420 splx(s);
422 #endif
425 sacom_activate(device_t dev, enum devact act)
427 struct sacom_softc *sc = device_private(dev);
429 switch (act) {
430 case DVACT_DEACTIVATE:
431 sc->enabled = 0;
432 return 0;
433 default:
434 return EOPNOTSUPP;
438 void
439 sacom_shutdown(struct sacom_softc *sc)
441 struct tty *tp = sc->sc_tty;
442 int s;
444 s = splserial();
445 COM_LOCK(sc);
447 /* Clear any break condition set with TIOCSBRK. */
448 sacom_break(sc, 0);
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().
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);
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);
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;
477 COM_UNLOCK(sc);
478 splx(s);
482 sacomopen(dev_t dev, int flag, int mode, struct lwp *l)
484 struct sacom_softc *sc;
485 struct tty *tp;
486 int s, s2;
487 int error;
489 sc = device_lookup_private(&sacom_cd, COMUNIT(dev));
490 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
491 sc->sc_rbuf == NULL)
492 return ENXIO;
494 if (!device_is_active(sc->sc_dev))
495 return ENXIO;
497 tp = sc->sc_tty;
499 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
500 return (EBUSY);
502 s = spltty();
505 * Do the following iff this is a first open.
507 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
508 struct termios t;
510 tp->t_dev = dev;
512 s2 = splserial();
513 COM_LOCK(sc);
515 if (sc->enable) {
516 if ((*sc->enable)(sc)) {
517 COM_UNLOCK(sc);
518 splx(s2);
519 splx(s);
520 aprint_normal_dev(sc->sc_dev, "device enable failed\n");
521 return EIO;
523 sc->enabled = 1;
524 sacom_config(sc);
527 /* Turn on interrupts. */
528 sc->sc_cr3 = CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE;
529 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3,
530 sc->sc_cr3);
533 COM_UNLOCK(sc);
534 splx(s2);
537 * Initialize the termios status to the defaults. Add in the
538 * sticky bits from TIOCSFLAGS.
540 t.c_ispeed = 0;
541 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
542 t.c_ospeed = sacomconsrate;
543 t.c_cflag = sacomconscflag;
544 } else {
545 t.c_ospeed = TTYDEF_SPEED;
546 t.c_cflag = TTYDEF_CFLAG;
548 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
549 SET(t.c_cflag, CLOCAL);
550 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
551 SET(t.c_cflag, CRTSCTS);
552 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
553 SET(t.c_cflag, MDMBUF);
554 /* Make sure sacomparam() will do something. */
555 tp->t_ospeed = 0;
556 (void) sacomparam(tp, &t);
557 tp->t_iflag = TTYDEF_IFLAG;
558 tp->t_oflag = TTYDEF_OFLAG;
559 tp->t_lflag = TTYDEF_LFLAG;
560 ttychars(tp);
561 ttsetwater(tp);
563 s2 = splserial();
564 COM_LOCK(sc);
567 * Turn on DTR. We must always do this, even if carrier is not
568 * present, because otherwise we'd have to use TIOCSDTR
569 * immediately after setting CLOCAL, which applications do not
570 * expect. We always assert DTR while the device is open
571 * unless explicitly requested to deassert it.
573 sacom_modem(sc, 1);
575 /* Clear the input ring, and unblock. */
576 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
577 sc->sc_rbavail = SACOM_RING_SIZE;
578 sacom_iflush(sc);
579 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
580 sacom_hwiflow(sc);
582 #ifdef COM_DEBUG
583 if (sacom_debug)
584 comstatus(sc, "sacomopen ");
585 #endif
587 COM_UNLOCK(sc);
588 splx(s2);
591 splx(s);
593 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
594 if (error)
595 goto bad;
597 error = (*tp->t_linesw->l_open)(dev, tp);
598 if (error)
599 goto bad;
601 return 0;
603 bad:
604 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
606 * We failed to open the device, and nobody else had it opened.
607 * Clean up the state as appropriate.
609 sacom_shutdown(sc);
612 return error;
616 sacomclose(dev_t dev, int flag, int mode, struct lwp *l)
618 struct sacom_softc *sc =
619 device_lookup_private(&sacom_cd, COMUNIT(dev));
620 struct tty *tp = sc->sc_tty;
622 /* XXX This is for cons.c. */
623 if (!ISSET(tp->t_state, TS_ISOPEN))
624 return 0;
626 (*tp->t_linesw->l_close)(tp, flag);
627 ttyclose(tp);
629 if (COM_ISALIVE(sc) == 0)
630 return 0;
632 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
634 * Although we got a last close, the device may still be in
635 * use; e.g. if this was the dialout node, and there are still
636 * processes waiting for carrier on the non-dialout node.
638 sacom_shutdown(sc);
641 return 0;
645 sacomread(dev_t dev, struct uio *uio, int flag)
647 struct sacom_softc *sc =
648 device_lookup_private(&sacom_cd, COMUNIT(dev));
649 struct tty *tp = sc->sc_tty;
651 if (COM_ISALIVE(sc) == 0)
652 return EIO;
654 return (*tp->t_linesw->l_read)(tp, uio, flag);
658 sacomwrite(dev_t dev, struct uio *uio, int flag)
660 struct sacom_softc *sc =
661 device_lookup_private(&sacom_cd, COMUNIT(dev));
662 struct tty *tp = sc->sc_tty;
664 if (COM_ISALIVE(sc) == 0)
665 return EIO;
667 return (*tp->t_linesw->l_write)(tp, uio, flag);
671 sacompoll(dev_t dev, int events, struct lwp *l)
673 struct sacom_softc *sc =
674 device_lookup_private(&sacom_cd, COMUNIT(dev));
675 struct tty *tp = sc->sc_tty;
677 if (COM_ISALIVE(sc) == 0)
678 return EIO;
680 return (*tp->t_linesw->l_poll)(tp, events, l);
683 struct tty *
684 sacomtty(dev_t dev)
686 struct sacom_softc *sc =
687 device_lookup_private(&sacom_cd, COMUNIT(dev));
688 struct tty *tp = sc->sc_tty;
690 return tp;
694 sacomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
696 struct sacom_softc *sc =
697 device_lookup_private(&sacom_cd, COMUNIT(dev));
698 struct tty *tp = sc->sc_tty;
699 int error;
700 int s;
702 if (COM_ISALIVE(sc) == 0)
703 return EIO;
705 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
706 if (error != EPASSTHROUGH)
707 return error;
709 error = ttioctl(tp, cmd, data, flag, l);
710 if (error != EPASSTHROUGH)
711 return error;
713 error = 0;
715 s = splserial();
716 COM_LOCK(sc);
718 switch (cmd) {
719 case TIOCSBRK:
720 sacom_break(sc, 1);
721 break;
723 case TIOCCBRK:
724 sacom_break(sc, 0);
725 break;
727 case TIOCSDTR:
728 sacom_modem(sc, 1);
729 break;
731 case TIOCCDTR:
732 sacom_modem(sc, 0);
733 break;
735 case TIOCGFLAGS:
736 *(int *)data = sc->sc_swflags;
737 break;
739 case TIOCSFLAGS:
740 error = kauth_authorize_device_tty(l->l_cred,
741 KAUTH_DEVICE_TTY_PRIVSET, tp);
742 if (error)
743 break;
744 sc->sc_swflags = *(int *)data;
745 break;
747 case TIOCMSET:
748 case TIOCMBIS:
749 case TIOCMBIC:
750 tiocm_to_sacom(sc, cmd, *(int *)data);
751 break;
753 case TIOCMGET:
754 *(int *)data = sacom_to_tiocm(sc);
755 break;
757 default:
758 error = EPASSTHROUGH;
759 break;
762 COM_UNLOCK(sc);
763 splx(s);
765 #ifdef COM_DEBUG
766 if (sacom_debug)
767 comstatus(sc, "comioctl ");
768 #endif
770 return error;
773 static inline void
774 sacom_schedrx(struct sacom_softc *sc)
777 sc->sc_rx_ready = 1;
779 /* Wake up the poller. */
780 softint_schedule(sc->sc_si);
783 void
784 sacom_break(struct sacom_softc *sc, int onoff)
787 if (onoff)
788 SET(sc->sc_cr3, CR3_BRK);
789 else
790 CLR(sc->sc_cr3, CR3_BRK);
792 if (!sc->sc_heldchange) {
793 if (sc->sc_tx_busy) {
794 sc->sc_heldtbc = sc->sc_tbc;
795 sc->sc_tbc = 0;
796 sc->sc_heldchange = 1;
797 } else
798 sacom_loadchannelregs(sc);
802 void
803 sacom_modem(struct sacom_softc *sc, int onoff)
805 if (!sc->sc_heldchange) {
806 if (sc->sc_tx_busy) {
807 sc->sc_heldtbc = sc->sc_tbc;
808 sc->sc_tbc = 0;
809 sc->sc_heldchange = 1;
810 } else
811 sacom_loadchannelregs(sc);
815 void
816 tiocm_to_sacom(struct sacom_softc *sc, u_long how, int ttybits)
821 sacom_to_tiocm(struct sacom_softc *sc)
823 int ttybits = 0;
825 if (sc->sc_cr3 != 0)
826 SET(ttybits, TIOCM_LE);
828 return ttybits;
831 static u_int
832 cflag2cr0(tcflag_t cflag)
834 u_int cr0;
836 cr0 = (cflag & PARENB) ? CR0_PE : 0;
837 cr0 |= (cflag & PARODD) ? 0 : CR0_OES;
838 cr0 |= (cflag & CSTOPB) ? CR0_SBS : 0;
839 cr0 |= ((cflag & CSIZE) == CS8) ? CR0_DSS : 0;
841 return cr0;
845 sacomparam(struct tty *tp, struct termios *t)
847 struct sacom_softc *sc =
848 device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev));
849 int ospeed = SACOMSPEED(t->c_ospeed);
850 u_int cr0;
851 int s;
853 if (COM_ISALIVE(sc) == 0)
854 return EIO;
856 /* Check requested parameters. */
857 if (ospeed < 0)
858 return EINVAL;
859 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
860 return EINVAL;
863 * For the console, always force CLOCAL and !HUPCL, so that the port
864 * is always active.
866 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
867 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
868 SET(t->c_cflag, CLOCAL);
869 CLR(t->c_cflag, HUPCL);
873 * If there were no changes, don't do anything. This avoids dropping
874 * input and improves performance when all we did was frob things like
875 * VMIN and VTIME.
877 if (tp->t_ospeed == t->c_ospeed &&
878 tp->t_cflag == t->c_cflag)
879 return 0;
881 cr0 = cflag2cr0(t->c_cflag);
883 s = splserial();
884 COM_LOCK(sc);
886 sc->sc_cr0 = cr0;
888 sc->sc_speed = ospeed;
890 /* And copy to tty. */
891 tp->t_ispeed = 0;
892 tp->t_ospeed = t->c_ospeed;
893 tp->t_cflag = t->c_cflag;
895 if (!sc->sc_heldchange) {
896 if (sc->sc_tx_busy) {
897 sc->sc_heldtbc = sc->sc_tbc;
898 sc->sc_tbc = 0;
899 sc->sc_heldchange = 1;
900 } else
901 sacom_loadchannelregs(sc);
904 if (!ISSET(t->c_cflag, CHWFLOW)) {
905 /* Disable the high water mark. */
906 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
907 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
908 sacom_schedrx(sc);
910 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
911 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
912 sacom_hwiflow(sc);
916 COM_UNLOCK(sc);
917 splx(s);
919 (void) (*tp->t_linesw->l_modem)(tp, 1);
921 #ifdef COM_DEBUG
922 if (sacom_debug)
923 comstatus(sc, "comparam ");
924 #endif
926 return 0;
929 void
930 sacom_iflush(struct sacom_softc *sc)
932 bus_space_tag_t iot = sc->sc_iot;
933 bus_space_handle_t ioh = sc->sc_ioh;
934 #ifdef DIAGNOSTIC
935 int reg;
936 #endif
937 int timo;
939 #ifdef DIAGNOSTIC
940 reg = 0xffff;
941 #endif
942 timo = 50;
943 /* flush any pending I/O */
944 if (sc->sc_cr3 & CR3_RXE) {
945 while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_RNE)
946 && --timo)
947 #ifdef DIAGNOSTIC
948 reg =
949 #else
950 (void)
951 #endif
952 bus_space_read_4(iot, ioh, SACOM_DR);
954 #if 0
955 /* XXX is it good idea to wait TX finish? */
956 if (sc->sc_cr3 & CR3_TXE) {
957 timo = 500;
958 while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_TBY)
959 && --timo)
960 delay(100);
962 #endif
963 #ifdef DIAGNOSTIC
964 if (!timo)
965 aprint_debug_dev(sc->sc_dev, "sacom_iflush timeout %02x\n", reg);
966 #endif
969 void
970 sacom_loadchannelregs(struct sacom_softc *sc)
972 bus_space_tag_t iot = sc->sc_iot;
973 bus_space_handle_t ioh = sc->sc_ioh;
975 /* XXXXX necessary? */
976 sacom_iflush(sc);
978 /* Need to stop engines first. */
979 bus_space_write_4(iot, ioh, SACOM_CR3, 0);
981 bus_space_write_4(iot, ioh, SACOM_CR1, sc->sc_speed >> 8);
982 bus_space_write_4(iot, ioh, SACOM_CR2, sc->sc_speed & 0xff);
984 bus_space_write_4(iot, ioh, SACOM_CR0, sc->sc_cr0);
985 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
989 sacomhwiflow(struct tty *tp, int block)
991 #if 0
992 struct sacom_softc *sc =
993 device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev));
994 int s;
996 if (COM_ISALIVE(sc) == 0)
997 return 0;
999 if (sc->sc_mcr_rts == 0)
1000 return 0;
1002 s = splserial();
1003 COM_LOCK(sc);
1005 if (block) {
1006 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1007 SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
1008 sacom_hwiflow(sc);
1010 } else {
1011 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1012 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1013 sacom_schedrx(sc);
1015 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1016 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
1017 sacom_hwiflow(sc);
1021 COM_UNLOCK(sc);
1022 splx(s);
1023 #endif
1024 return 1;
1028 * (un)block input via hw flowcontrol
1030 void
1031 sacom_hwiflow(struct sacom_softc *sc)
1033 #if 0
1034 bus_space_tag_t iot = sc->sc_iot;
1035 bus_space_handle_t ioh = sc->sc_ioh;
1037 /* XXX implement */
1038 #endif
1042 void
1043 sacomstart(struct tty *tp)
1045 struct sacom_softc *sc =
1046 device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev));
1047 bus_space_tag_t iot = sc->sc_iot;
1048 bus_space_handle_t ioh = sc->sc_ioh;
1049 int s;
1051 if (COM_ISALIVE(sc) == 0)
1052 return;
1054 s = spltty();
1055 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1056 goto out;
1057 if (!ttypull(tp))
1058 goto out;
1060 /* Grab the first contiguous region of buffer space. */
1062 u_char *tba;
1063 int tbc;
1065 tba = tp->t_outq.c_cf;
1066 tbc = ndqb(&tp->t_outq, 0);
1068 (void)splserial();
1069 COM_LOCK(sc);
1071 sc->sc_tba = tba;
1072 sc->sc_tbc = tbc;
1075 SET(tp->t_state, TS_BUSY);
1076 sc->sc_tx_busy = 1;
1078 /* Enable transmit completion interrupts if necessary. */
1079 if (!ISSET(sc->sc_cr3, CR3_TIE)) {
1080 SET(sc->sc_cr3, CR3_TIE);
1081 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
1084 /* Output the first chunk of the contiguous buffer. */
1085 sacom_filltx(sc);
1087 COM_UNLOCK(sc);
1088 out:
1089 splx(s);
1090 return;
1093 void
1094 sacom_filltx(struct sacom_softc *sc)
1096 bus_space_tag_t iot = sc->sc_iot;
1097 bus_space_handle_t ioh = sc->sc_ioh;
1098 int c, n;
1100 n = 0;
1101 while (bus_space_read_4(iot, ioh, SACOM_SR1) & SR1_TNF) {
1102 if (n == SACOM_TXFIFOLEN || n == sc->sc_tbc)
1103 break;
1104 c = *(sc->sc_tba + n);
1105 c &= 0xff;
1106 bus_space_write_4(iot, ioh, SACOM_DR, c);
1107 n++;
1109 sc->sc_tbc -= n;
1110 sc->sc_tba += n;
1114 * Stop output on a line.
1116 void
1117 sacomstop(struct tty *tp, int flag)
1119 struct sacom_softc *sc =
1120 device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev));
1121 int s;
1123 s = splserial();
1124 COM_LOCK(sc);
1125 if (ISSET(tp->t_state, TS_BUSY)) {
1126 /* Stop transmitting at the next chunk. */
1127 sc->sc_tbc = 0;
1128 sc->sc_heldtbc = 0;
1129 if (!ISSET(tp->t_state, TS_TTSTOP))
1130 SET(tp->t_state, TS_FLUSH);
1132 COM_UNLOCK(sc);
1133 splx(s);
1136 static inline void
1137 sacom_rxsoft(struct sacom_softc *sc, struct tty *tp)
1139 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1140 u_char *get, *end;
1141 u_int cc, scc;
1142 u_char sr1;
1143 int code;
1144 int s;
1146 end = sc->sc_ebuf;
1147 get = sc->sc_rbget;
1148 scc = cc = SACOM_RING_SIZE - sc->sc_rbavail;
1150 while (cc) {
1151 code = get[0];
1152 sr1 = get[1];
1153 if (ISSET(sr1, SR1_FRE))
1154 SET(code, TTY_FE);
1155 if (ISSET(sr1, SR1_PRE))
1156 SET(code, TTY_PE);
1157 if ((*rint)(code, tp) == -1) {
1159 * The line discipline's buffer is out of space.
1161 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1163 * We're either not using flow control, or the
1164 * line discipline didn't tell us to block for
1165 * some reason. Either way, we have no way to
1166 * know when there's more space available, so
1167 * just drop the rest of the data.
1169 get += cc << 1;
1170 if (get >= end)
1171 get -= SACOM_RING_SIZE << 1;
1172 cc = 0;
1173 } else {
1175 * Don't schedule any more receive processing
1176 * until the line discipline tells us there's
1177 * space available (through comhwiflow()).
1178 * Leave the rest of the data in the input
1179 * buffer.
1181 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1183 break;
1185 get += 2;
1186 if (get >= end)
1187 get = sc->sc_rbuf;
1188 cc--;
1191 if (cc != scc) {
1192 sc->sc_rbget = get;
1193 s = splserial();
1194 COM_LOCK(sc);
1196 cc = sc->sc_rbavail += scc - cc;
1197 /* Buffers should be ok again, release possible block. */
1198 if (cc >= 1) {
1199 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1200 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1201 SET(sc->sc_cr3, CR3_RIE);
1202 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
1203 SACOM_CR3, sc->sc_cr3);
1206 COM_UNLOCK(sc);
1207 splx(s);
1211 static inline void
1212 sacom_txsoft(struct sacom_softc *sc, struct tty *tp)
1215 CLR(tp->t_state, TS_BUSY);
1216 if (ISSET(tp->t_state, TS_FLUSH))
1217 CLR(tp->t_state, TS_FLUSH);
1218 else
1219 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1220 (*tp->t_linesw->l_start)(tp);
1223 static inline void
1224 sacom_stsoft(struct sacom_softc *sc, struct tty *tp)
1228 void
1229 sacomsoft(void *arg)
1231 struct sacom_softc *sc = arg;
1232 struct tty *tp;
1234 if (COM_ISALIVE(sc) == 0)
1235 return;
1237 tp = sc->sc_tty;
1239 if (sc->sc_rx_ready) {
1240 sc->sc_rx_ready = 0;
1241 sacom_rxsoft(sc, tp);
1244 if (sc->sc_st_check) {
1245 sc->sc_st_check = 0;
1246 sacom_stsoft(sc, tp);
1249 if (sc->sc_tx_done) {
1250 sc->sc_tx_done = 0;
1251 sacom_txsoft(sc, tp);
1256 sacomintr(void *arg)
1258 struct sacom_softc *sc = arg;
1259 bus_space_tag_t iot = sc->sc_iot;
1260 bus_space_handle_t ioh = sc->sc_ioh;
1261 u_char *put, *end;
1262 u_int cc;
1263 u_int sr0, sr1;
1265 if (COM_ISALIVE(sc) == 0)
1266 return 0;
1268 COM_LOCK(sc);
1269 sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
1270 if (!sr0) {
1271 COM_UNLOCK(sc);
1272 return 0;
1274 if (ISSET(sr0, SR0_EIF))
1275 /* XXX silently discard error bits */
1276 bus_space_read_4(iot, ioh, SACOM_DR);
1277 if (ISSET(sr0, SR0_RBB))
1278 bus_space_write_4(iot, ioh, SACOM_SR0, SR0_RBB);
1279 if (ISSET(sr0, SR0_REB)) {
1280 bus_space_write_4(iot, ioh, SACOM_SR0, SR0_REB);
1281 #if defined(DDB) || defined(KGDB)
1282 #ifndef DDB_BREAK_CHAR
1283 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1284 console_debugger();
1286 #endif
1287 #endif /* DDB || KGDB */
1291 end = sc->sc_ebuf;
1292 put = sc->sc_rbput;
1293 cc = sc->sc_rbavail;
1295 sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
1296 if (ISSET(sr0, SR0_RFS | SR0_RID)) {
1297 if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1298 while (cc > 0) {
1299 if (!ISSET(sr1, SR1_RNE)) {
1300 bus_space_write_4(iot, ioh, SACOM_SR0,
1301 SR0_RID);
1302 break;
1304 put[0] = bus_space_read_4(iot, ioh, SACOM_DR);
1305 put[1] = sr1;
1306 #if defined(DDB) && defined(DDB_BREAK_CHAR)
1307 if (put[0] == DDB_BREAK_CHAR &&
1308 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1309 console_debugger();
1311 sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
1312 continue;
1314 #endif
1315 put += 2;
1316 if (put >= end)
1317 put = sc->sc_rbuf;
1318 cc--;
1320 sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
1324 * Current string of incoming characters ended because
1325 * no more data was available or we ran out of space.
1326 * Schedule a receive event if any data was received.
1327 * If we're out of space, turn off receive interrupts.
1329 sc->sc_rbput = put;
1330 sc->sc_rbavail = cc;
1331 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1332 sc->sc_rx_ready = 1;
1334 /* XXX do RX hardware flow control */
1337 * If we're out of space, disable receive interrupts
1338 * until the queue has drained a bit.
1340 if (!cc) {
1341 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1342 CLR(sc->sc_cr3, CR3_RIE);
1343 bus_space_write_4(iot, ioh, SACOM_CR3,
1344 sc->sc_cr3);
1346 } else {
1347 #ifdef DIAGNOSTIC
1348 panic("sacomintr: we shouldn't reach here");
1349 #endif
1350 CLR(sc->sc_cr3, CR3_RIE);
1351 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
1356 * Done handling any receive interrupts. See if data can be
1357 * transmitted as well. Schedule tx done event if no data left
1358 * and tty was marked busy.
1360 sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
1361 if (ISSET(sr0, SR0_TFS)) {
1363 * If we've delayed a parameter change, do it now, and restart
1364 * output.
1365 * XXX sacom_loadchannelregs() waits TX completion,
1366 * XXX resulting in ~0.1s hang (300bps, 4 bytes) in worst case
1368 if (sc->sc_heldchange) {
1369 sacom_loadchannelregs(sc);
1370 sc->sc_heldchange = 0;
1371 sc->sc_tbc = sc->sc_heldtbc;
1372 sc->sc_heldtbc = 0;
1375 /* Output the next chunk of the contiguous buffer, if any. */
1376 if (sc->sc_tbc > 0) {
1377 sacom_filltx(sc);
1378 } else {
1379 /* Disable transmit completion interrupts if necessary. */
1380 if (ISSET(sc->sc_cr3, CR3_TIE)) {
1381 CLR(sc->sc_cr3, CR3_TIE);
1382 bus_space_write_4(iot, ioh, SACOM_CR3,
1383 sc->sc_cr3);
1385 if (sc->sc_tx_busy) {
1386 sc->sc_tx_busy = 0;
1387 sc->sc_tx_done = 1;
1391 COM_UNLOCK(sc);
1393 /* Wake up the poller. */
1394 softint_schedule(sc->sc_si);
1396 #if NRND > 0 && defined(RND_COM)
1397 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1398 #endif
1399 return 1;
1402 static void
1403 sacom_j720_init(struct sa11x0_softc *parent, struct sacom_softc *sc) {
1405 /* XXX this should be done at sc->enable function */
1406 bus_space_write_4(parent->sc_iot, parent->sc_gpioh,
1407 SAGPIO_PCR, 0xa0000);
1408 bus_space_write_4(parent->sc_iot, parent->sc_gpioh,
1409 SAGPIO_PSR, 0x100);
1412 /* Initialization for serial console */
1414 sacominit(bus_space_tag_t iot, bus_addr_t iobase, int baud, tcflag_t cflag,
1415 bus_space_handle_t *iohp)
1417 int brd, cr0;
1419 if (bus_space_map(iot, iobase, SACOM_NPORTS, 0, iohp))
1420 aprint_normal("register map failed\n");
1422 /* wait for the Tx queue to drain and disable the UART */
1423 while (bus_space_read_4(iot, *iohp, SACOM_SR1) & SR1_TBY)
1424 continue;
1425 bus_space_write_4(iot, *iohp, SACOM_CR3, 0);
1427 cr0 = cflag2cr0(cflag);
1428 bus_space_write_4(iot, *iohp, SACOM_CR0, cr0);
1430 brd = SACOMSPEED(baud);
1431 sacomconsrate = baud;
1432 sacomconsaddr = iobase;
1433 sacomconscflag = cflag;
1434 /* XXX assumes little endian */
1435 bus_space_write_4(iot, *iohp, SACOM_CR1, brd >> 8);
1436 bus_space_write_4(iot, *iohp, SACOM_CR2, brd & 0xff);
1438 /* enable the UART */
1439 bus_space_write_4(iot, *iohp, SACOM_CR3, CR3_RXE | CR3_TXE);
1441 return 0;
1444 void
1445 sacomcnprobe(struct consdev *cp)
1447 cp->cn_pri = CN_REMOTE;
1450 void
1451 sacomcninit(struct consdev *cp)
1453 if (cp == NULL) {
1454 /* XXX cp == NULL means that MMU is disabled. */
1455 sacomconsioh = SACOM3_BASE;
1456 sacomconstag = &sa11x0_bs_tag;
1457 cn_tab = &sacomcons;
1458 return;
1461 if (sacominit(&sa11x0_bs_tag, CONADDR, CONSPEED,
1462 CONMODE, &sacomconsioh))
1463 panic("can't init serial console @%x", CONADDR);
1464 cn_tab = &sacomcons;
1465 sacomconstag = &sa11x0_bs_tag;
1469 sacomcngetc(dev_t dev)
1471 int c, s;
1473 s = spltty(); /* XXX do we need this? */
1475 while (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
1476 & SR1_RNE)) {
1477 #if defined(DDB) || defined(KGDB)
1478 #ifndef DDB_BREAK_CHAR
1479 u_int sr0;
1480 extern int db_active;
1482 sr0 = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR0);
1483 if (ISSET(sr0, SR0_RBB))
1484 bus_space_write_4(sacomconstag, sacomconsioh,
1485 SACOM_SR0, SR0_RBB);
1486 if (ISSET(sr0, SR0_REB)) {
1487 bus_space_write_4(sacomconstag, sacomconsioh,
1488 SACOM_SR0, SR0_REB);
1489 if (db_active == 0)
1490 console_debugger();
1492 #endif
1493 #endif /* DDB || KGDB */
1496 c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
1497 c &= 0xff;
1498 splx(s);
1500 return c;
1503 void
1504 sacomcnputc(dev_t dev, int c)
1506 int s;
1508 s = spltty(); /* XXX do we need this? */
1510 while (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
1511 & SR1_TNF))
1512 continue;
1514 bus_space_write_4(sacomconstag, sacomconsioh, SACOM_DR, c);
1515 splx(s);
1518 void
1519 sacomcnpollc(dev_t dev, int on)
1524 #if 0
1525 #ifdef DEBUG
1527 sacomcncharpoll(void)
1529 int c;
1531 if (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
1532 & SR1_RNE))
1533 return -1;
1535 c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
1536 c &= 0xff;
1538 return c;
1540 #endif
1541 #endif
1543 /* helper function to identify the com ports used by
1544 console or KGDB (and not yet autoconf attached) */
1546 sacom_is_console(bus_space_tag_t iot, bus_addr_t iobase,
1547 bus_space_handle_t *ioh)
1549 bus_space_handle_t help;
1551 if (!sacomconsattached &&
1552 iot == sacomconstag && iobase == sacomconsaddr)
1553 help = sacomconsioh;
1554 else
1555 return 0;
1557 if (ioh)
1558 *ioh = help;
1559 return 1;