1 /* $NetBSD: sci.c,v 1.51 2008/06/13 13:08:57 cegger Exp $ */
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
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.
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
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
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
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 $");
101 #include <sys/param.h>
102 #include <sys/systm.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);
133 struct device sc_dev
; /* boilerplate */
136 callout_t sc_diag_ch
;
139 bus_space_tag_t sc_iot
; /* ISA i/o space identifier */
140 bus_space_handle_t sc_ioh
; /* ISA io handle */
149 sc_errors
; /* number of retries so far */
150 u_char sc_status
[7]; /* copy of registers */
154 u_int sc_fifolen
; /* XXX always 0? */
158 u_char
*volatile sc_rbget
,
160 volatile u_int sc_rbavail
;
164 u_char
*sc_tba
; /* transmit buffer address */
165 u_int sc_tbc
, /* transmit byte count */
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 */
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;
225 int scicn_speed
= SCICN_SPEED
;
227 int scicn_speed
= 9600;
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
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);
270 * : unsigned int bps;
271 * : SCI(Serial Communication Interface)
275 InitializeSci(unsigned int bps
)
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
293 /* Send permission, Receive permission ON */
294 SHREG_SCSCR
= SCSCR_TE
| SCSCR_RE
;
296 /* Serial Status Register */
297 SHREG_SCSSR
&= SCSSR_TDRE
; /* Clear Status */
300 I2C_ADRS
&= ~0x08; /* enable RS-232C */
310 sci_putc(unsigned char c
)
314 while ((SHREG_SCSSR
& SCSSR_TDRE
) == 0)
317 /* write send data to send register */
320 /* clear ready flag */
321 SHREG_SCSSR
&= ~SCSSR_TDRE
;
328 * 0x80 = parity error
334 return(SHREG_SCSSR
& (SCSSR_ORER
| SCSSR_FER
| SCSSR_PER
));
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);
355 SHREG_SCSSR
&= ~SCSSR_RDRF
;
361 sci_match(struct device
*parent
, struct cfdata
*cfp
, void *aux
)
364 if (strcmp(cfp
->cf_name
, "sci") || sci_attached
)
371 sci_attach(struct device
*parent
, struct device
*self
, void *aux
)
373 struct sci_softc
*sc
= (struct sci_softc
*)self
;
378 sc
->sc_hwflags
= 0; /* XXX */
379 sc
->sc_swflags
= 0; /* XXX */
380 sc
->sc_fifolen
= 0; /* XXX */
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
);
391 callout_init(&sc
->sc_diag_ch
, 0);
393 intc_intr_establish(SH_INTEVT_SCI_ERI
, IST_LEVEL
, IPL_SERIAL
, sciintr
,
395 intc_intr_establish(SH_INTEVT_SCI_RXI
, IST_LEVEL
, IPL_SERIAL
, sciintr
,
397 intc_intr_establish(SH_INTEVT_SCI_TXI
, IST_LEVEL
, IPL_SERIAL
, sciintr
,
399 intc_intr_establish(SH_INTEVT_SCI_TEI
, IST_LEVEL
, IPL_SERIAL
, sciintr
,
402 sc
->sc_si
= softint_establish(SOFTINT_SERIAL
, scisoft
, sc
);
403 SET(sc
->sc_hwflags
, SCI_HW_DEV_OK
);
406 tp
->t_oproc
= scistart
;
407 tp
->t_param
= sciparam
;
408 tp
->t_hwiflow
= NULL
;
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
);
417 sc
->sc_ebuf
= sc
->sc_rbuf
+ (sci_rbuf_size
<< 1);
423 * Start or restart transmission.
426 scistart(struct tty
*tp
)
428 struct sci_softc
*sc
= device_lookup_private(&sci_cd
,SCIUNIT(tp
->t_dev
));
432 if (ISSET(tp
->t_state
, TS_BUSY
| TS_TIMEOUT
| TS_TTSTOP
))
434 if (sc
->sc_tx_stopped
)
439 /* Grab the first contiguous region of buffer space. */
444 tba
= tp
->t_outq
.c_cf
;
445 tbc
= ndqb(&tp
->t_outq
, 0);
453 SET(tp
->t_state
, TS_BUSY
);
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
));
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.
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
;
484 if (!device_is_active(&sc
->sc_dev
))
487 /* Check requested parameters. */
490 if (t
->c_ispeed
&& t
->c_ispeed
!= t
->c_ospeed
)
494 * For the console, always force CLOCAL and !HUPCL, so that the port
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
508 if (tp
->t_ospeed
== t
->c_ospeed
&&
509 tp
->t_cflag
== t
->c_cflag
)
514 lcr
= ISSET(sc
->sc_lcr
, LCR_SBREAK
) | cflag2lcr(t
->c_cflag
);
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
526 * * Otherwise set it a bit higher.
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
);
540 /* And copy to tty. */
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
;
549 sc
->sc_heldchange
= 1;
554 sci_loadchannelregs(sc
);
558 if (!ISSET(t
->c_cflag
, CHWFLOW
)) {
559 /* Disable the high water mark. */
562 if (ISSET(sc
->sc_rx_flags
, RX_TTY_OVERFLOWED
)) {
563 CLR(sc
->sc_rx_flags
, RX_TTY_OVERFLOWED
);
567 sc
->sc_r_hiwat
= sci_rbuf_hiwat
;
568 sc
->sc_r_lowat
= sci_rbuf_lowat
;
575 scistatus(sc
, "sciparam ");
578 if (!ISSET(t
->c_cflag
, CHWFLOW
)) {
579 if (sc
->sc_tx_stopped
) {
580 sc
->sc_tx_stopped
= 0;
589 sci_iflush(struct sci_softc
*sc
)
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
);
604 SHREG_SCSSR
&= ~SCSSR_RDRF
;
609 sciopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
611 struct sci_softc
*sc
;
616 sc
= device_lookup_private(&sci_cd
, SCIUNIT(dev
));
617 if (sc
== 0 || !ISSET(sc
->sc_hwflags
, SCI_HW_DEV_OK
) ||
621 if (!device_is_active(&sc
->sc_dev
))
626 * If this is the kgdb port, no other use is permitted.
628 if (ISSET(sc
->sc_hwflags
, SCI_HW_KGDB
))
634 if (kauth_authorize_device_tty(l
->l_cred
, KAUTH_DEVICE_TTY_OPEN
, tp
))
640 * Do the following iff this is a first open.
642 if (!ISSET(tp
->t_state
, TS_ISOPEN
) && tp
->t_wopen
== 0) {
649 /* Turn on interrupts. */
650 SHREG_SCSCR
|= SCSCR_TIE
| SCSCR_RIE
;
655 * Initialize the termios status to the defaults. Add in the
656 * sticky bits from TIOCSFLAGS.
659 if (ISSET(sc
->sc_hwflags
, SCI_HW_CONSOLE
)) {
660 t
.c_ospeed
= scicn_speed
;
661 t
.c_cflag
= sciconscflag
;
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. */
674 (void) sciparam(tp
, &t
);
675 tp
->t_iflag
= TTYDEF_IFLAG
;
676 tp
->t_oflag
= TTYDEF_OFLAG
;
677 tp
->t_lflag
= TTYDEF_LFLAG
;
683 /* Clear the input ring, and unblock. */
684 sc
->sc_rbput
= sc
->sc_rbget
= sc
->sc_rbuf
;
685 sc
->sc_rbavail
= sci_rbuf_size
;
687 CLR(sc
->sc_rx_flags
, RX_ANY_BLOCK
);
695 scistatus(sc
, "sciopen ");
703 error
= ttyopen(tp
, SCIDIALOUT(dev
), ISSET(flag
, O_NONBLOCK
));
707 error
= (*tp
->t_linesw
->l_open
)(dev
, tp
);
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
))
728 (*tp
->t_linesw
->l_close
)(tp
, flag
);
731 if (!device_is_active(&sc
->sc_dev
))
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
));
767 struct sci_softc
*sc
= device_lookup_private(&sci_cd
, SCIUNIT(dev
));
768 struct tty
*tp
= sc
->sc_tty
;
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
;
781 if (!device_is_active(&sc
->sc_dev
))
784 error
= (*tp
->t_linesw
->l_ioctl
)(tp
, cmd
, data
, flag
, l
);
785 if (error
!= EPASSTHROUGH
)
788 error
= ttioctl(tp
, cmd
, data
, flag
, l
);
789 if (error
!= EPASSTHROUGH
)
806 *(int *)data
= sc
->sc_swflags
;
810 error
= kauth_authorize_device_tty(l
->l_cred
,
811 KAUTH_DEVICE_TTY_PRIVSET
, tp
);
814 sc
->sc_swflags
= *(int *)data
;
818 error
= EPASSTHROUGH
;
828 sci_schedrx(struct sci_softc
*sc
)
833 /* Wake up the poller. */
834 softint_schedule(sc
->sc_si
);
838 sci_break(struct sci_softc
*sc
, int onoff
)
842 SHREG_SCSSR
&= ~SCSSR_TDRE
;
844 SHREG_SCSSR
|= SCSSR_TDRE
;
847 if (!sc
->sc_heldchange
) {
848 if (sc
->sc_tx_busy
) {
849 sc
->sc_heldtbc
= sc
->sc_tbc
;
851 sc
->sc_heldchange
= 1;
853 sci_loadchannelregs(sc
);
859 * Stop output, e.g., for ^S or output flush.
862 scistop(struct tty
*tp
, int flag
)
864 struct sci_softc
*sc
= device_lookup_private(&sci_cd
, SCIUNIT(tp
->t_dev
));
868 if (ISSET(tp
->t_state
, TS_BUSY
)) {
869 /* Stop transmitting at the next chunk. */
872 if (!ISSET(tp
->t_state
, TS_TTSTOP
))
873 SET(tp
->t_state
, TS_FLUSH
);
881 struct sci_softc
*sc
= arg
;
882 int overflows
, floods
;
886 overflows
= sc
->sc_overflows
;
887 sc
->sc_overflows
= 0;
888 floods
= sc
->sc_floods
;
893 log(LOG_WARNING
, "%s: %d silo overflow%s, %d ibuf flood%s\n",
895 overflows
, overflows
== 1 ? "" : "s",
896 floods
, floods
== 1 ? "" : "s");
900 sci_rxsoft(struct sci_softc
*sc
, struct tty
*tp
)
902 int (*rint
)(int, struct tty
*) = tp
->t_linesw
->l_rint
;
911 scc
= cc
= sci_rbuf_size
- sc
->sc_rbavail
;
913 if (cc
== sci_rbuf_size
) {
915 if (sc
->sc_errors
++ == 0)
916 callout_reset(&sc
->sc_diag_ch
, 60 * hz
, scidiag
, sc
);
922 if (ISSET(ssr
, SCSSR_FER
| SCSSR_PER
)) {
923 if (ISSET(ssr
, SCSSR_FER
))
925 if (ISSET(ssr
, SCSSR_PER
))
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.
942 get
-= sci_rbuf_size
<< 1;
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
952 SET(sc
->sc_rx_flags
, RX_TTY_OVERFLOWED
);
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
;
973 if (ISSET(sc
->sc_rx_flags
, RX_IBUF_BLOCKED
)) {
974 CLR(sc
->sc_rx_flags
, RX_IBUF_BLOCKED
);
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
);
991 ndflush(&tp
->t_outq
, (int)(sc
->sc_tba
- tp
->t_outq
.c_cf
));
992 (*tp
->t_linesw
->l_start
)(tp
);
996 sci_stsoft(struct sci_softc
*sc
, struct tty
*tp
)
1005 delta
= sc
->sc_msr_delta
;
1006 sc
->sc_msr_delta
= 0;
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
);
1022 sc
->sc_tx_stopped
= 1;
1028 scistatus(sc
, "sci_stsoft");
1036 struct sci_softc
*sc
= arg
;
1039 if (!device_is_active(&sc
->sc_dev
))
1044 if (sc
->sc_rx_ready
) {
1045 sc
->sc_rx_ready
= 0;
1050 if (sc
->sc_st_check
) {
1051 sc
->sc_st_check
= 0;
1056 if (sc
->sc_tx_done
) {
1065 struct sci_softc
*sc
= arg
;
1070 if (!device_is_active(&sc
->sc_dev
))
1075 cc
= sc
->sc_rbavail
;
1079 if (ISSET(ssr
, SCSSR_FER
)) {
1080 SHREG_SCSSR
&= ~(SCSSR_ORER
| SCSSR_PER
| SCSSR_FER
);
1081 #if defined(DDB) || defined(KGDB)
1083 if ((SHREG_SCSPTR
& SCSPTR_SPB0DT
) != 0) {
1085 if ((SHREG_SCSPDR
& SCSPDR_SCP0DT
) != 0) {
1088 if (ISSET(sc
->sc_hwflags
, SCI_HW_CONSOLE
)) {
1093 if (ISSET(sc
->sc_hwflags
, SCI_HW_KGDB
)) {
1098 #endif /* DDB || KGDB */
1100 if ((SHREG_SCSSR
& SCSSR_RDRF
) != 0) {
1102 put
[0] = SHREG_SCRDR
;
1103 put
[1] = SHREG_SCSSR
& 0x00ff;
1111 SHREG_SCSSR
&= ~(SCSSR_ORER
| SCSSR_FER
| SCSSR_PER
|
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.
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
);
1138 * If we're out of space, disable receive interrupts
1139 * until the queue has drained a bit.
1142 SET(sc
->sc_rx_flags
, RX_IBUF_OVERFLOWED
);
1143 SHREG_SCSCR
&= ~SCSCR_RIE
;
1146 if (SHREG_SCSSR
& SCSSR_RDRF
) {
1147 SHREG_SCSCR
&= ~(SCSCR_TIE
| SCSCR_RIE
);
1149 SHREG_SCSCR
|= SCSCR_TIE
| SCSCR_RIE
;
1153 } while (SHREG_SCSSR
& SCSSR_RDRF
);
1156 msr
= bus_space_read_1(iot
, ioh
, sci_msr
);
1157 delta
= msr
^ sc
->sc_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
)) {
1167 if (ISSET(msr
, sc
->sc_ppsmask
) ==
1169 /* XXX nanotime() */
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
);
1181 if (sc
->ppsparam
.mode
& PPS_HARDPPSONASSERT
)
1182 hardpps(&tv
, tv
.tv_usec
);
1184 sc
->ppsinfo
.assert_sequence
++;
1185 sc
->ppsinfo
.current_mode
=
1188 } else if (ISSET(msr
, sc
->sc_ppsmask
) ==
1190 /* XXX nanotime() */
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
);
1202 if (sc
->ppsparam
.mode
& PPS_HARDPPSONCLEAR
)
1203 hardpps(&tv
, tv
.tv_usec
);
1205 sc
->ppsinfo
.clear_sequence
++;
1206 sc
->ppsinfo
.current_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
)) {
1220 scistatus(sc
, "sciintr ");
1224 sc
->sc_st_check
= 1;
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
1238 if (sc
->sc_heldchange
) {
1239 sc
->sc_heldchange
= 0;
1240 sc
->sc_tbc
= sc
->sc_heldtbc
;
1244 /* Output the next chunk of the contiguous buffer, if any. */
1245 if (sc
->sc_tbc
> 0) {
1246 sci_putc(*(sc
->sc_tba
));
1250 /* Disable transmit completion interrupts if necessary. */
1252 if (ISSET(sc
->sc_ier
, IER_ETXRDY
))
1254 SHREG_SCSCR
&= ~SCSCR_TIE
;
1256 if (sc
->sc_tx_busy
) {
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
);
1274 scicnprobe(struct consdev
*cp
)
1278 /* locate the major number */
1279 maj
= cdevsw_lookup_major(&sci_cdevsw
);
1281 /* Initialize required fields. */
1282 cp
->cn_dev
= makedev(maj
, 0);
1284 cp
->cn_pri
= CN_REMOTE
;
1286 cp
->cn_pri
= CN_NORMAL
;
1291 scicninit(struct consdev
*cp
)
1294 InitializeSci(scicn_speed
);
1299 scicngetc(dev_t dev
)
1312 scicnputc(dev_t dev
, int c
)
1317 sci_putc((u_char
)c
);