Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / sh3 / dev / sci.c
blob34b49c4654ea00c68dc0f47aad621dfae6300f26
1 /* $NetBSD: sci.c,v 1.51 2008/06/13 13:08:57 cegger Exp $ */
3 /*-
4 * Copyright (C) 1999 T.Horiuchi and SAITOH Masanobu. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /*-
30 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
31 * All rights reserved.
33 * This code is derived from software contributed to The NetBSD Foundation
34 * by Charles M. Hannum.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55 * POSSIBILITY OF SUCH DAMAGE.
59 * Copyright (c) 1991 The Regents of the University of California.
60 * All rights reserved.
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
64 * are met:
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in the
69 * documentation and/or other materials provided with the distribution.
70 * 3. Neither the name of the University nor the names of its contributors
71 * may be used to endorse or promote products derived from this software
72 * without specific prior written permission.
74 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
75 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
78 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
79 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
80 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
83 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84 * SUCH DAMAGE.
86 * @(#)com.c 7.5 (Berkeley) 5/16/91
90 * SH internal serial driver
92 * This code is derived from both z8530tty.c and com.c
95 #include <sys/cdefs.h>
96 __KERNEL_RCSID(0, "$NetBSD: sci.c,v 1.51 2008/06/13 13:08:57 cegger Exp $");
98 #include "opt_kgdb.h"
99 #include "opt_sci.h"
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/tty.h>
104 #include <sys/proc.h>
105 #include <sys/conf.h>
106 #include <sys/file.h>
107 #include <sys/syslog.h>
108 #include <sys/kernel.h>
109 #include <sys/device.h>
110 #include <sys/malloc.h>
111 #include <sys/kauth.h>
112 #include <sys/intr.h>
114 #include <dev/cons.h>
116 #include <sh3/clock.h>
117 #include <sh3/scireg.h>
118 #include <sh3/pfcreg.h>
119 #include <sh3/tmureg.h>
120 #include <sh3/exception.h>
122 static void scistart(struct tty *);
123 static int sciparam(struct tty *, struct termios *);
125 void scicnprobe(struct consdev *);
126 void scicninit(struct consdev *);
127 void scicnputc(dev_t, int);
128 int scicngetc(dev_t);
129 void scicnpoolc(dev_t, int);
130 int sciintr(void *);
132 struct sci_softc {
133 struct device sc_dev; /* boilerplate */
134 struct tty *sc_tty;
135 void *sc_si;
136 callout_t sc_diag_ch;
138 #if 0
139 bus_space_tag_t sc_iot; /* ISA i/o space identifier */
140 bus_space_handle_t sc_ioh; /* ISA io handle */
142 int sc_drq;
144 int sc_frequency;
145 #endif
147 u_int sc_overflows,
148 sc_floods,
149 sc_errors; /* number of retries so far */
150 u_char sc_status[7]; /* copy of registers */
152 int sc_hwflags;
153 int sc_swflags;
154 u_int sc_fifolen; /* XXX always 0? */
156 u_int sc_r_hiwat,
157 sc_r_lowat;
158 u_char *volatile sc_rbget,
159 *volatile sc_rbput;
160 volatile u_int sc_rbavail;
161 u_char *sc_rbuf,
162 *sc_ebuf;
164 u_char *sc_tba; /* transmit buffer address */
165 u_int sc_tbc, /* transmit byte count */
166 sc_heldtbc;
168 volatile u_char sc_rx_flags, /* receiver blocked */
169 #define RX_TTY_BLOCKED 0x01
170 #define RX_TTY_OVERFLOWED 0x02
171 #define RX_IBUF_BLOCKED 0x04
172 #define RX_IBUF_OVERFLOWED 0x08
173 #define RX_ANY_BLOCK 0x0f
174 sc_tx_busy, /* working on an output chunk */
175 sc_tx_done, /* done with one output chunk */
176 sc_tx_stopped, /* H/W level stop (lost CTS) */
177 sc_st_check, /* got a status interrupt */
178 sc_rx_ready;
180 volatile u_char sc_heldchange;
183 /* controller driver configuration */
184 static int sci_match(struct device *, struct cfdata *, void *);
185 static void sci_attach(struct device *, struct device *, void *);
187 void sci_break(struct sci_softc *, int);
188 void sci_iflush(struct sci_softc *);
190 #define integrate static inline
191 void scisoft(void *);
193 integrate void sci_rxsoft(struct sci_softc *, struct tty *);
194 integrate void sci_txsoft(struct sci_softc *, struct tty *);
195 integrate void sci_stsoft(struct sci_softc *, struct tty *);
196 integrate void sci_schedrx(struct sci_softc *);
197 void scidiag(void *);
199 #define SCIUNIT_MASK 0x7ffff
200 #define SCIDIALOUT_MASK 0x80000
202 #define SCIUNIT(x) (minor(x) & SCIUNIT_MASK)
203 #define SCIDIALOUT(x) (minor(x) & SCIDIALOUT_MASK)
205 /* Hardware flag masks */
206 #define SCI_HW_NOIEN 0x01
207 #define SCI_HW_FIFO 0x02
208 #define SCI_HW_FLOW 0x08
209 #define SCI_HW_DEV_OK 0x20
210 #define SCI_HW_CONSOLE 0x40
211 #define SCI_HW_KGDB 0x80
213 /* Buffer size for character buffer */
214 #define SCI_RING_SIZE 2048
216 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
217 u_int sci_rbuf_hiwat = (SCI_RING_SIZE * 1) / 4;
218 u_int sci_rbuf_lowat = (SCI_RING_SIZE * 3) / 4;
220 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
221 int sciconscflag = CONMODE;
222 int sciisconsole = 0;
224 #ifdef SCICN_SPEED
225 int scicn_speed = SCICN_SPEED;
226 #else
227 int scicn_speed = 9600;
228 #endif
230 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
232 u_int sci_rbuf_size = SCI_RING_SIZE;
234 CFATTACH_DECL(sci, sizeof(struct sci_softc),
235 sci_match, sci_attach, NULL, NULL);
237 extern struct cfdriver sci_cd;
239 static int sci_attached;
241 dev_type_open(sciopen);
242 dev_type_close(sciclose);
243 dev_type_read(sciread);
244 dev_type_write(sciwrite);
245 dev_type_ioctl(sciioctl);
246 dev_type_stop(scistop);
247 dev_type_tty(scitty);
248 dev_type_poll(scipoll);
250 const struct cdevsw sci_cdevsw = {
251 sciopen, sciclose, sciread, sciwrite, sciioctl,
252 scistop, scitty, scipoll, nommap, ttykqfilter, D_TTY
255 void InitializeSci (unsigned int);
258 * following functions are debugging prupose only
260 #define CR 0x0D
261 #define I2C_ADRS (*(volatile unsigned int *)0xa8000000)
262 #define USART_ON (unsigned int)~0x08
264 void sci_putc(unsigned char);
265 unsigned char sci_getc(void);
266 int SciErrCheck(void);
269 * InitializeSci
270 * : unsigned int bps;
271 * : SCI(Serial Communication Interface)
274 void
275 InitializeSci(unsigned int bps)
278 /* Initialize SCR */
279 SHREG_SCSCR = 0x00;
281 /* Serial Mode Register */
282 SHREG_SCSMR = 0x00; /* Async,8bit,NonParity,Even,1Stop,NoMulti */
284 /* Bit Rate Register */
285 SHREG_SCBRR = divrnd(sh_clock_get_pclock(), 32 * bps) - 1;
288 * wait 1mSec, because Send/Recv must begin 1 bit period after
289 * BRR is set.
291 delay(1000);
293 /* Send permission, Receive permission ON */
294 SHREG_SCSCR = SCSCR_TE | SCSCR_RE;
296 /* Serial Status Register */
297 SHREG_SCSSR &= SCSSR_TDRE; /* Clear Status */
299 #if 0
300 I2C_ADRS &= ~0x08; /* enable RS-232C */
301 #endif
306 * sci_putc
307 * : unsigned char c;
309 void
310 sci_putc(unsigned char c)
313 /* wait for ready */
314 while ((SHREG_SCSSR & SCSSR_TDRE) == 0)
317 /* write send data to send register */
318 SHREG_SCTDR = c;
320 /* clear ready flag */
321 SHREG_SCSSR &= ~SCSSR_TDRE;
325 * : SciErrCheck
326 * 0x20 = over run
327 * 0x10 = frame error
328 * 0x80 = parity error
331 SciErrCheck(void)
334 return(SHREG_SCSSR & (SCSSR_ORER | SCSSR_FER | SCSSR_PER));
338 * sci_getc
340 unsigned char
341 sci_getc(void)
343 unsigned char c, err_c;
345 while (((err_c = SHREG_SCSSR)
346 & (SCSSR_RDRF | SCSSR_ORER | SCSSR_FER | SCSSR_PER)) == 0)
348 if ((err_c & (SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) {
349 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER);
350 return(err_c |= 0x80);
353 c = SHREG_SCRDR;
355 SHREG_SCSSR &= ~SCSSR_RDRF;
357 return(c);
360 static int
361 sci_match(struct device *parent, struct cfdata *cfp, void *aux)
364 if (strcmp(cfp->cf_name, "sci") || sci_attached)
365 return 0;
367 return 1;
370 static void
371 sci_attach(struct device *parent, struct device *self, void *aux)
373 struct sci_softc *sc = (struct sci_softc *)self;
374 struct tty *tp;
376 sci_attached = 1;
378 sc->sc_hwflags = 0; /* XXX */
379 sc->sc_swflags = 0; /* XXX */
380 sc->sc_fifolen = 0; /* XXX */
382 if (sciisconsole) {
383 SET(sc->sc_hwflags, SCI_HW_CONSOLE);
384 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
385 printf("\n%s: console\n", sc->sc_dev.dv_xname);
386 } else {
387 InitializeSci(9600);
388 printf("\n");
391 callout_init(&sc->sc_diag_ch, 0);
393 intc_intr_establish(SH_INTEVT_SCI_ERI, IST_LEVEL, IPL_SERIAL, sciintr,
394 sc);
395 intc_intr_establish(SH_INTEVT_SCI_RXI, IST_LEVEL, IPL_SERIAL, sciintr,
396 sc);
397 intc_intr_establish(SH_INTEVT_SCI_TXI, IST_LEVEL, IPL_SERIAL, sciintr,
398 sc);
399 intc_intr_establish(SH_INTEVT_SCI_TEI, IST_LEVEL, IPL_SERIAL, sciintr,
400 sc);
402 sc->sc_si = softint_establish(SOFTINT_SERIAL, scisoft, sc);
403 SET(sc->sc_hwflags, SCI_HW_DEV_OK);
405 tp = ttymalloc();
406 tp->t_oproc = scistart;
407 tp->t_param = sciparam;
408 tp->t_hwiflow = NULL;
410 sc->sc_tty = tp;
411 sc->sc_rbuf = malloc(sci_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
412 if (sc->sc_rbuf == NULL) {
413 printf("%s: unable to allocate ring buffer\n",
414 sc->sc_dev.dv_xname);
415 return;
417 sc->sc_ebuf = sc->sc_rbuf + (sci_rbuf_size << 1);
419 tty_attach(tp);
423 * Start or restart transmission.
425 static void
426 scistart(struct tty *tp)
428 struct sci_softc *sc = device_lookup_private(&sci_cd,SCIUNIT(tp->t_dev));
429 int s;
431 s = spltty();
432 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
433 goto out;
434 if (sc->sc_tx_stopped)
435 goto out;
436 if (!ttypull(tp))
437 goto out;
439 /* Grab the first contiguous region of buffer space. */
441 u_char *tba;
442 int tbc;
444 tba = tp->t_outq.c_cf;
445 tbc = ndqb(&tp->t_outq, 0);
447 (void)splserial();
449 sc->sc_tba = tba;
450 sc->sc_tbc = tbc;
453 SET(tp->t_state, TS_BUSY);
454 sc->sc_tx_busy = 1;
456 /* Enable transmit completion interrupts if necessary. */
457 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
459 /* Output the first byte of the contiguous buffer. */
461 if (sc->sc_tbc > 0) {
462 sci_putc(*(sc->sc_tba));
463 sc->sc_tba++;
464 sc->sc_tbc--;
467 out:
468 splx(s);
469 return;
473 * Set SCI tty parameters from termios.
474 * XXX - Should just copy the whole termios after
475 * making sure all the changes could be done.
477 static int
478 sciparam(struct tty *tp, struct termios *t)
480 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(tp->t_dev));
481 int ospeed = t->c_ospeed;
482 int s;
484 if (!device_is_active(&sc->sc_dev))
485 return (EIO);
487 /* Check requested parameters. */
488 if (ospeed < 0)
489 return (EINVAL);
490 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
491 return (EINVAL);
494 * For the console, always force CLOCAL and !HUPCL, so that the port
495 * is always active.
497 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
498 ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
499 SET(t->c_cflag, CLOCAL);
500 CLR(t->c_cflag, HUPCL);
504 * If there were no changes, don't do anything. This avoids dropping
505 * input and improves performance when all we did was frob things like
506 * VMIN and VTIME.
508 if (tp->t_ospeed == t->c_ospeed &&
509 tp->t_cflag == t->c_cflag)
510 return (0);
512 #if 0
513 /* XXX (msaitoh) */
514 lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
515 #endif
517 s = splserial();
520 * Set the FIFO threshold based on the receive speed.
522 * * If it's a low speed, it's probably a mouse or some other
523 * interactive device, so set the threshold low.
524 * * If it's a high speed, trim the trigger level down to prevent
525 * overflows.
526 * * Otherwise set it a bit higher.
528 #if 0
529 /* XXX (msaitoh) */
530 if (ISSET(sc->sc_hwflags, SCI_HW_HAYESP))
531 sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
532 else if (ISSET(sc->sc_hwflags, SCI_HW_FIFO))
533 sc->sc_fifo = FIFO_ENABLE |
534 (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
535 t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
536 else
537 sc->sc_fifo = 0;
538 #endif
540 /* And copy to tty. */
541 tp->t_ispeed = 0;
542 tp->t_ospeed = t->c_ospeed;
543 tp->t_cflag = t->c_cflag;
545 if (!sc->sc_heldchange) {
546 if (sc->sc_tx_busy) {
547 sc->sc_heldtbc = sc->sc_tbc;
548 sc->sc_tbc = 0;
549 sc->sc_heldchange = 1;
551 #if 0
552 /* XXX (msaitoh) */
553 else
554 sci_loadchannelregs(sc);
555 #endif
558 if (!ISSET(t->c_cflag, CHWFLOW)) {
559 /* Disable the high water mark. */
560 sc->sc_r_hiwat = 0;
561 sc->sc_r_lowat = 0;
562 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
563 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
564 sci_schedrx(sc);
566 } else {
567 sc->sc_r_hiwat = sci_rbuf_hiwat;
568 sc->sc_r_lowat = sci_rbuf_lowat;
571 splx(s);
573 #ifdef SCI_DEBUG
574 if (sci_debug)
575 scistatus(sc, "sciparam ");
576 #endif
578 if (!ISSET(t->c_cflag, CHWFLOW)) {
579 if (sc->sc_tx_stopped) {
580 sc->sc_tx_stopped = 0;
581 scistart(tp);
585 return (0);
588 void
589 sci_iflush(struct sci_softc *sc)
591 unsigned char err_c;
592 volatile unsigned char c;
594 if (((err_c = SHREG_SCSSR)
595 & (SCSSR_RDRF | SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) {
597 if ((err_c & (SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) {
598 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER);
599 return;
602 c = SHREG_SCRDR;
604 SHREG_SCSSR &= ~SCSSR_RDRF;
609 sciopen(dev_t dev, int flag, int mode, struct lwp *l)
611 struct sci_softc *sc;
612 struct tty *tp;
613 int s, s2;
614 int error;
616 sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
617 if (sc == 0 || !ISSET(sc->sc_hwflags, SCI_HW_DEV_OK) ||
618 sc->sc_rbuf == NULL)
619 return (ENXIO);
621 if (!device_is_active(&sc->sc_dev))
622 return (ENXIO);
624 #ifdef KGDB
626 * If this is the kgdb port, no other use is permitted.
628 if (ISSET(sc->sc_hwflags, SCI_HW_KGDB))
629 return (EBUSY);
630 #endif
632 tp = sc->sc_tty;
634 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
635 return (EBUSY);
637 s = spltty();
640 * Do the following iff this is a first open.
642 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
643 struct termios t;
645 tp->t_dev = dev;
647 s2 = splserial();
649 /* Turn on interrupts. */
650 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
652 splx(s2);
655 * Initialize the termios status to the defaults. Add in the
656 * sticky bits from TIOCSFLAGS.
658 t.c_ispeed = 0;
659 if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
660 t.c_ospeed = scicn_speed;
661 t.c_cflag = sciconscflag;
662 } else {
663 t.c_ospeed = TTYDEF_SPEED;
664 t.c_cflag = TTYDEF_CFLAG;
666 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
667 SET(t.c_cflag, CLOCAL);
668 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
669 SET(t.c_cflag, CRTSCTS);
670 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
671 SET(t.c_cflag, MDMBUF);
672 /* Make sure sciparam() will do something. */
673 tp->t_ospeed = 0;
674 (void) sciparam(tp, &t);
675 tp->t_iflag = TTYDEF_IFLAG;
676 tp->t_oflag = TTYDEF_OFLAG;
677 tp->t_lflag = TTYDEF_LFLAG;
678 ttychars(tp);
679 ttsetwater(tp);
681 s2 = splserial();
683 /* Clear the input ring, and unblock. */
684 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
685 sc->sc_rbavail = sci_rbuf_size;
686 sci_iflush(sc);
687 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
688 #if 0
689 /* XXX (msaitoh) */
690 sci_hwiflow(sc);
691 #endif
693 #ifdef SCI_DEBUG
694 if (sci_debug)
695 scistatus(sc, "sciopen ");
696 #endif
698 splx(s2);
701 splx(s);
703 error = ttyopen(tp, SCIDIALOUT(dev), ISSET(flag, O_NONBLOCK));
704 if (error)
705 goto bad;
707 error = (*tp->t_linesw->l_open)(dev, tp);
708 if (error)
709 goto bad;
711 return (0);
713 bad:
715 return (error);
719 sciclose(dev_t dev, int flag, int mode, struct lwp *l)
721 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
722 struct tty *tp = sc->sc_tty;
724 /* XXX This is for cons.c. */
725 if (!ISSET(tp->t_state, TS_ISOPEN))
726 return (0);
728 (*tp->t_linesw->l_close)(tp, flag);
729 ttyclose(tp);
731 if (!device_is_active(&sc->sc_dev))
732 return (0);
734 return (0);
738 sciread(dev_t dev, struct uio *uio, int flag)
740 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
741 struct tty *tp = sc->sc_tty;
743 return ((*tp->t_linesw->l_read)(tp, uio, flag));
747 sciwrite(dev_t dev, struct uio *uio, int flag)
749 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
750 struct tty *tp = sc->sc_tty;
752 return ((*tp->t_linesw->l_write)(tp, uio, flag));
756 scipoll(dev_t dev, int events, struct lwp *l)
758 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
759 struct tty *tp = sc->sc_tty;
761 return ((*tp->t_linesw->l_poll)(tp, events, l));
764 struct tty *
765 scitty(dev_t dev)
767 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
768 struct tty *tp = sc->sc_tty;
770 return (tp);
774 sciioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
776 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
777 struct tty *tp = sc->sc_tty;
778 int error;
779 int s;
781 if (!device_is_active(&sc->sc_dev))
782 return (EIO);
784 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
785 if (error != EPASSTHROUGH)
786 return (error);
788 error = ttioctl(tp, cmd, data, flag, l);
789 if (error != EPASSTHROUGH)
790 return (error);
792 error = 0;
794 s = splserial();
796 switch (cmd) {
797 case TIOCSBRK:
798 sci_break(sc, 1);
799 break;
801 case TIOCCBRK:
802 sci_break(sc, 0);
803 break;
805 case TIOCGFLAGS:
806 *(int *)data = sc->sc_swflags;
807 break;
809 case TIOCSFLAGS:
810 error = kauth_authorize_device_tty(l->l_cred,
811 KAUTH_DEVICE_TTY_PRIVSET, tp);
812 if (error)
813 break;
814 sc->sc_swflags = *(int *)data;
815 break;
817 default:
818 error = EPASSTHROUGH;
819 break;
822 splx(s);
824 return (error);
827 integrate void
828 sci_schedrx(struct sci_softc *sc)
831 sc->sc_rx_ready = 1;
833 /* Wake up the poller. */
834 softint_schedule(sc->sc_si);
837 void
838 sci_break(struct sci_softc *sc, int onoff)
841 if (onoff)
842 SHREG_SCSSR &= ~SCSSR_TDRE;
843 else
844 SHREG_SCSSR |= SCSSR_TDRE;
846 #if 0 /* XXX */
847 if (!sc->sc_heldchange) {
848 if (sc->sc_tx_busy) {
849 sc->sc_heldtbc = sc->sc_tbc;
850 sc->sc_tbc = 0;
851 sc->sc_heldchange = 1;
852 } else
853 sci_loadchannelregs(sc);
855 #endif
859 * Stop output, e.g., for ^S or output flush.
861 void
862 scistop(struct tty *tp, int flag)
864 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(tp->t_dev));
865 int s;
867 s = splserial();
868 if (ISSET(tp->t_state, TS_BUSY)) {
869 /* Stop transmitting at the next chunk. */
870 sc->sc_tbc = 0;
871 sc->sc_heldtbc = 0;
872 if (!ISSET(tp->t_state, TS_TTSTOP))
873 SET(tp->t_state, TS_FLUSH);
875 splx(s);
878 void
879 scidiag(void *arg)
881 struct sci_softc *sc = arg;
882 int overflows, floods;
883 int s;
885 s = splserial();
886 overflows = sc->sc_overflows;
887 sc->sc_overflows = 0;
888 floods = sc->sc_floods;
889 sc->sc_floods = 0;
890 sc->sc_errors = 0;
891 splx(s);
893 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
894 sc->sc_dev.dv_xname,
895 overflows, overflows == 1 ? "" : "s",
896 floods, floods == 1 ? "" : "s");
899 integrate void
900 sci_rxsoft(struct sci_softc *sc, struct tty *tp)
902 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
903 u_char *get, *end;
904 u_int cc, scc;
905 u_char ssr;
906 int code;
907 int s;
909 end = sc->sc_ebuf;
910 get = sc->sc_rbget;
911 scc = cc = sci_rbuf_size - sc->sc_rbavail;
913 if (cc == sci_rbuf_size) {
914 sc->sc_floods++;
915 if (sc->sc_errors++ == 0)
916 callout_reset(&sc->sc_diag_ch, 60 * hz, scidiag, sc);
919 while (cc) {
920 code = get[0];
921 ssr = get[1];
922 if (ISSET(ssr, SCSSR_FER | SCSSR_PER)) {
923 if (ISSET(ssr, SCSSR_FER))
924 SET(code, TTY_FE);
925 if (ISSET(ssr, SCSSR_PER))
926 SET(code, TTY_PE);
928 if ((*rint)(code, tp) == -1) {
930 * The line discipline's buffer is out of space.
932 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
934 * We're either not using flow control, or the
935 * line discipline didn't tell us to block for
936 * some reason. Either way, we have no way to
937 * know when there's more space available, so
938 * just drop the rest of the data.
940 get += cc << 1;
941 if (get >= end)
942 get -= sci_rbuf_size << 1;
943 cc = 0;
944 } else {
946 * Don't schedule any more receive processing
947 * until the line discipline tells us there's
948 * space available (through scihwiflow()).
949 * Leave the rest of the data in the input
950 * buffer.
952 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
954 break;
956 get += 2;
957 if (get >= end)
958 get = sc->sc_rbuf;
959 cc--;
962 if (cc != scc) {
963 sc->sc_rbget = get;
964 s = splserial();
965 cc = sc->sc_rbavail += scc - cc;
966 /* Buffers should be ok again, release possible block. */
967 if (cc >= sc->sc_r_lowat) {
968 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
969 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
970 SHREG_SCSCR |= SCSCR_RIE;
972 #if 0
973 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
974 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
975 sci_hwiflow(sc);
977 #endif
979 splx(s);
983 integrate void
984 sci_txsoft(struct sci_softc *sc, struct tty *tp)
987 CLR(tp->t_state, TS_BUSY);
988 if (ISSET(tp->t_state, TS_FLUSH))
989 CLR(tp->t_state, TS_FLUSH);
990 else
991 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
992 (*tp->t_linesw->l_start)(tp);
995 integrate void
996 sci_stsoft(struct sci_softc *sc, struct tty *tp)
998 #if 0
999 /* XXX (msaitoh) */
1000 u_char msr, delta;
1001 int s;
1003 s = splserial();
1004 msr = sc->sc_msr;
1005 delta = sc->sc_msr_delta;
1006 sc->sc_msr_delta = 0;
1007 splx(s);
1009 if (ISSET(delta, sc->sc_msr_dcd)) {
1011 * Inform the tty layer that carrier detect changed.
1013 (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
1016 if (ISSET(delta, sc->sc_msr_cts)) {
1017 /* Block or unblock output according to flow control. */
1018 if (ISSET(msr, sc->sc_msr_cts)) {
1019 sc->sc_tx_stopped = 0;
1020 (*tp->t_linesw->l_start)(tp);
1021 } else {
1022 sc->sc_tx_stopped = 1;
1026 #ifdef SCI_DEBUG
1027 if (sci_debug)
1028 scistatus(sc, "sci_stsoft");
1029 #endif
1030 #endif
1033 void
1034 scisoft(void *arg)
1036 struct sci_softc *sc = arg;
1037 struct tty *tp;
1039 if (!device_is_active(&sc->sc_dev))
1040 return;
1042 tp = sc->sc_tty;
1044 if (sc->sc_rx_ready) {
1045 sc->sc_rx_ready = 0;
1046 sci_rxsoft(sc, tp);
1049 #if 0
1050 if (sc->sc_st_check) {
1051 sc->sc_st_check = 0;
1052 sci_stsoft(sc, tp);
1054 #endif
1056 if (sc->sc_tx_done) {
1057 sc->sc_tx_done = 0;
1058 sci_txsoft(sc, tp);
1063 sciintr(void *arg)
1065 struct sci_softc *sc = arg;
1066 u_char *put, *end;
1067 u_int cc;
1068 u_short ssr;
1070 if (!device_is_active(&sc->sc_dev))
1071 return (0);
1073 end = sc->sc_ebuf;
1074 put = sc->sc_rbput;
1075 cc = sc->sc_rbavail;
1077 do {
1078 ssr = SHREG_SCSSR;
1079 if (ISSET(ssr, SCSSR_FER)) {
1080 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_PER | SCSSR_FER);
1081 #if defined(DDB) || defined(KGDB)
1082 #ifdef SH4
1083 if ((SHREG_SCSPTR & SCSPTR_SPB0DT) != 0) {
1084 #else
1085 if ((SHREG_SCSPDR & SCSPDR_SCP0DT) != 0) {
1086 #endif
1087 #ifdef DDB
1088 if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
1089 console_debugger();
1091 #endif
1092 #ifdef KGDB
1093 if (ISSET(sc->sc_hwflags, SCI_HW_KGDB)) {
1094 kgdb_connect(1);
1096 #endif
1098 #endif /* DDB || KGDB */
1100 if ((SHREG_SCSSR & SCSSR_RDRF) != 0) {
1101 if (cc > 0) {
1102 put[0] = SHREG_SCRDR;
1103 put[1] = SHREG_SCSSR & 0x00ff;
1105 put += 2;
1106 if (put >= end)
1107 put = sc->sc_rbuf;
1108 cc--;
1111 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER |
1112 SCSSR_RDRF);
1115 * Current string of incoming characters ended because
1116 * no more data was available or we ran out of space.
1117 * Schedule a receive event if any data was received.
1118 * If we're out of space, turn off receive interrupts.
1120 sc->sc_rbput = put;
1121 sc->sc_rbavail = cc;
1122 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1123 sc->sc_rx_ready = 1;
1126 * See if we are in danger of overflowing a buffer. If
1127 * so, use hardware flow control to ease the pressure.
1129 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1130 cc < sc->sc_r_hiwat) {
1131 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1132 #if 0
1133 sci_hwiflow(sc);
1134 #endif
1138 * If we're out of space, disable receive interrupts
1139 * until the queue has drained a bit.
1141 if (!cc) {
1142 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1143 SHREG_SCSCR &= ~SCSCR_RIE;
1145 } else {
1146 if (SHREG_SCSSR & SCSSR_RDRF) {
1147 SHREG_SCSCR &= ~(SCSCR_TIE | SCSCR_RIE);
1148 delay(10);
1149 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
1150 continue;
1153 } while (SHREG_SCSSR & SCSSR_RDRF);
1155 #if 0
1156 msr = bus_space_read_1(iot, ioh, sci_msr);
1157 delta = msr ^ sc->sc_msr;
1158 sc->sc_msr = msr;
1159 if (ISSET(delta, sc->sc_msr_mask)) {
1160 SET(sc->sc_msr_delta, delta);
1163 * Pulse-per-second clock signal on edge of DCD?
1165 if (ISSET(delta, sc->sc_ppsmask)) {
1166 struct timeval tv;
1167 if (ISSET(msr, sc->sc_ppsmask) ==
1168 sc->sc_ppsassert) {
1169 /* XXX nanotime() */
1170 microtime(&tv);
1171 TIMEVAL_TO_TIMESPEC(&tv,
1172 &sc->ppsinfo.assert_timestamp);
1173 if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
1174 timespecadd(&sc->ppsinfo.assert_timestamp,
1175 &sc->ppsparam.assert_offset,
1176 &sc->ppsinfo.assert_timestamp);
1177 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.assert_timestamp);
1180 #ifdef PPS_SYNC
1181 if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
1182 hardpps(&tv, tv.tv_usec);
1183 #endif
1184 sc->ppsinfo.assert_sequence++;
1185 sc->ppsinfo.current_mode =
1186 sc->ppsparam.mode;
1188 } else if (ISSET(msr, sc->sc_ppsmask) ==
1189 sc->sc_ppsclear) {
1190 /* XXX nanotime() */
1191 microtime(&tv);
1192 TIMEVAL_TO_TIMESPEC(&tv,
1193 &sc->ppsinfo.clear_timestamp);
1194 if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
1195 timespecadd(&sc->ppsinfo.clear_timestamp,
1196 &sc->ppsparam.clear_offset,
1197 &sc->ppsinfo.clear_timestamp);
1198 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.clear_timestamp);
1201 #ifdef PPS_SYNC
1202 if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
1203 hardpps(&tv, tv.tv_usec);
1204 #endif
1205 sc->ppsinfo.clear_sequence++;
1206 sc->ppsinfo.current_mode =
1207 sc->ppsparam.mode;
1212 * Stop output immediately if we lose the output
1213 * flow control signal or carrier detect.
1215 if (ISSET(~msr, sc->sc_msr_mask)) {
1216 sc->sc_tbc = 0;
1217 sc->sc_heldtbc = 0;
1218 #ifdef SCI_DEBUG
1219 if (sci_debug)
1220 scistatus(sc, "sciintr ");
1221 #endif
1224 sc->sc_st_check = 1;
1226 #endif
1229 * Done handling any receive interrupts. See if data can be
1230 * transmitted as well. Schedule tx done event if no data left
1231 * and tty was marked busy.
1233 if ((SHREG_SCSSR & SCSSR_TDRE) != 0) {
1235 * If we've delayed a parameter change, do it now, and restart
1236 * output.
1238 if (sc->sc_heldchange) {
1239 sc->sc_heldchange = 0;
1240 sc->sc_tbc = sc->sc_heldtbc;
1241 sc->sc_heldtbc = 0;
1244 /* Output the next chunk of the contiguous buffer, if any. */
1245 if (sc->sc_tbc > 0) {
1246 sci_putc(*(sc->sc_tba));
1247 sc->sc_tba++;
1248 sc->sc_tbc--;
1249 } else {
1250 /* Disable transmit completion interrupts if necessary. */
1251 #if 0
1252 if (ISSET(sc->sc_ier, IER_ETXRDY))
1253 #endif
1254 SHREG_SCSCR &= ~SCSCR_TIE;
1256 if (sc->sc_tx_busy) {
1257 sc->sc_tx_busy = 0;
1258 sc->sc_tx_done = 1;
1263 /* Wake up the poller. */
1264 softint_schedule(sc->sc_si);
1266 #if NRND > 0 && defined(RND_SCI)
1267 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1268 #endif
1270 return (1);
1273 void
1274 scicnprobe(struct consdev *cp)
1276 int maj;
1278 /* locate the major number */
1279 maj = cdevsw_lookup_major(&sci_cdevsw);
1281 /* Initialize required fields. */
1282 cp->cn_dev = makedev(maj, 0);
1283 #ifdef SCICONSOLE
1284 cp->cn_pri = CN_REMOTE;
1285 #else
1286 cp->cn_pri = CN_NORMAL;
1287 #endif
1290 void
1291 scicninit(struct consdev *cp)
1294 InitializeSci(scicn_speed);
1295 sciisconsole = 1;
1299 scicngetc(dev_t dev)
1301 int c;
1302 int s;
1304 s = splserial();
1305 c = sci_getc();
1306 splx(s);
1308 return (c);
1311 void
1312 scicnputc(dev_t dev, int c)
1314 int s;
1316 s = splserial();
1317 sci_putc((u_char)c);
1318 splx(s);