1 /* $NetBSD: scif.c,v 1.58 2009/03/18 10:22:36 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: scif.c,v 1.58 2009/03/18 10:22:36 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/kgdb.h>
112 #include <sys/kauth.h>
113 #include <sys/intr.h>
115 #include <dev/cons.h>
117 #include <sh3/clock.h>
118 #include <sh3/exception.h>
119 #include <sh3/scifreg.h>
121 #include <sh3/dev/scifvar.h>
123 #include "locators.h"
132 callout_t sc_diag_ch
;
135 bus_space_tag_t sc_iot
; /* ISA i/o space identifier */
136 bus_space_handle_t sc_ioh
; /* ISA io handle */
145 sc_errors
; /* number of retries so far */
146 u_char sc_status
[7]; /* copy of registers */
154 u_char
*volatile sc_rbget
,
156 volatile u_int sc_rbavail
;
160 u_char
*sc_tba
; /* transmit buffer address */
161 u_int sc_tbc
, /* transmit byte count */
164 volatile u_char sc_rx_flags
,
165 #define RX_TTY_BLOCKED 0x01
166 #define RX_TTY_OVERFLOWED 0x02
167 #define RX_IBUF_BLOCKED 0x04
168 #define RX_IBUF_OVERFLOWED 0x08
169 #define RX_ANY_BLOCK 0x0f
170 sc_tx_busy
, /* working on an output chunk */
171 sc_tx_done
, /* done with one output chunk */
172 sc_tx_stopped
, /* H/W level stop (lost CTS) */
173 sc_st_check
, /* got a status interrupt */
176 volatile u_char sc_heldchange
;
180 static int scif_match(device_t
, cfdata_t
, void *);
181 static void scif_attach(device_t
, device_t
, void *);
183 CFATTACH_DECL_NEW(scif
, sizeof(struct scif_softc
),
184 scif_match
, scif_attach
, NULL
, NULL
);
186 static int scif_attached
= 0; /* XXX: FIXME: don't limit to just one! */
188 extern struct cfdriver scif_cd
;
190 #define SCIFUNIT_MASK 0x7ffff
191 #define SCIFDIALOUT_MASK 0x80000
193 #define SCIFUNIT(x) (minor(x) & SCIFUNIT_MASK)
194 #define SCIFDIALOUT(x) (minor(x) & SCIFDIALOUT_MASK)
198 dev_type_cnprobe(scifcnprobe
);
199 dev_type_cninit(scifcninit
);
200 dev_type_cngetc(scifcngetc
);
201 dev_type_cnputc(scifcnputc
);
205 dev_type_open(scifopen
);
206 dev_type_close(scifclose
);
207 dev_type_read(scifread
);
208 dev_type_write(scifwrite
);
209 dev_type_ioctl(scifioctl
);
210 dev_type_stop(scifstop
);
211 dev_type_tty(sciftty
);
212 dev_type_poll(scifpoll
);
214 const struct cdevsw scif_cdevsw
= {
215 scifopen
, scifclose
, scifread
, scifwrite
, scifioctl
,
216 scifstop
, sciftty
, scifpoll
, nommap
, ttykqfilter
, D_TTY
221 static void scifstart(struct tty
*);
222 static int scifparam(struct tty
*, struct termios
*);
225 void InitializeScif (unsigned int);
226 int ScifErrCheck(void);
227 void scif_putc(unsigned char);
228 unsigned char scif_getc(void);
230 static int scifintr(void *);
231 static void scifsoft(void *);
232 static void scif_rxsoft(struct scif_softc
*, struct tty
*);
233 static void scif_txsoft(struct scif_softc
*, struct tty
*);
235 static void scif_stsoft(struct scif_softc
*, struct tty
*);
237 static void scif_schedrx(struct scif_softc
*);
238 static void scifdiag(void *);
240 static void scif_break(struct scif_softc
*, int);
241 static void scif_iflush(struct scif_softc
*);
244 /* Hardware flag masks (sc_hwflags) */
245 #define SCIF_HW_NOIEN 0x01
246 #define SCIF_HW_FIFO 0x02
247 #define SCIF_HW_FLOW 0x08
248 #define SCIF_HW_DEV_OK 0x20
249 #define SCIF_HW_CONSOLE 0x40
250 #define SCIF_HW_KGDB 0x80
253 /* Buffer size for character buffer */
254 #define SCIF_RING_SIZE 2048
255 static unsigned int scif_rbuf_size
= SCIF_RING_SIZE
;
257 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
258 static unsigned int scif_rbuf_hiwat
= (SCIF_RING_SIZE
* 1) / 4;
259 static unsigned int scif_rbuf_lowat
= (SCIF_RING_SIZE
* 3) / 4;
263 unsigned int scifcn_speed
= SCIFCN_SPEED
;
265 unsigned int scifcn_speed
= 9600;
268 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
269 int scifconscflag
= CONMODE
;
271 static int scifisconsole
= 0;
274 static int kgdb_attached
= 0;
278 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
282 * Prepare for bus_spacification. The difference in access widths is
283 * still handled by the magic definitions in scifreg.h
285 #define scif_smr_read() SHREG_SCSMR2
286 #define scif_smr_write(v) (SHREG_SCSMR2 = (v))
288 #define scif_brr_read() SHREG_SCBRR2
289 #define scif_brr_write(v) (SHREG_SCBRR2 = (v))
291 #define scif_scr_read() SHREG_SCSCR2
292 #define scif_scr_write(v) (SHREG_SCSCR2 = (v))
294 #define scif_ftdr_write(v) (SHREG_SCFTDR2 = (v))
296 #define scif_ssr_read() SHREG_SCSSR2
297 #define scif_ssr_write(v) (SHREG_SCSSR2 = (v))
299 #define scif_frdr_read() SHREG_SCFRDR2
301 #define scif_fcr_read() SHREG_SCFCR2
302 #define scif_fcr_write(v) (SHREG_SCFCR2 = (v))
304 #define scif_fdr_read() SHREG_SCFDR2
306 #ifdef SH4 /* additional registers in sh4 */
308 #define scif_sptr_read() SHREG_SCSPTR2
309 #define scif_sptr_write(v) (SHREG_SCSPTR2 = (v))
311 #define scif_lsr_read() SHREG_SCLSR2
312 #define scif_lsr_write(v) (SHREG_SCLSR2 = (v))
318 InitializeScif(unsigned int bps
)
322 scif_scr_write(0x00);
325 scif_fcr_write(SCFCR2_TFRST
| SCFCR2_RFRST
| SCFCR2_MCE
);
327 scif_fcr_write(SCFCR2_TFRST
| SCFCR2_RFRST
);
329 /* Serial Mode Register */
330 scif_smr_write(0x00); /* 8bit,NonParity,Even,1Stop */
332 /* Bit Rate Register */
333 scif_brr_write(divrnd(sh_clock_get_pclock(), 32 * bps
) - 1);
336 * wait 2m Sec, because Send/Recv must begin 1 bit period after
342 scif_fcr_write(FIFO_RCV_TRIGGER_14
| FIFO_XMT_TRIGGER_1
| SCFCR2_MCE
);
344 scif_fcr_write(FIFO_RCV_TRIGGER_14
| FIFO_XMT_TRIGGER_1
);
347 /* Send permission, Receive permission ON */
348 scif_scr_write(SCSCR2_TE
| SCSCR2_RE
);
350 /* Serial Status Register */
351 scif_ssr_write(scif_ssr_read() & SCSSR2_TDFE
); /* Clear Status */
358 return (scif_ssr_read() & (SCSSR2_ER
| SCSSR2_FER
| SCSSR2_PER
));
362 scif_putc(unsigned char c
)
366 while ((scif_fdr_read() & SCFDR2_TXCNT
) == SCFDR2_TXF_FULL
)
369 /* write send data to send register */
372 /* clear ready flag */
373 scif_ssr_write(scif_ssr_read() & ~(SCSSR2_TDFE
| SCSSR2_TEND
));
379 unsigned char c
, err_c
;
381 unsigned short err_c2
= 0; /* XXXGCC: -Wuninitialized */
386 while ((scif_fdr_read() & SCFDR2_RECVCNT
) == 0)
389 c
= scif_frdr_read();
390 err_c
= scif_ssr_read();
391 scif_ssr_write(scif_ssr_read()
392 & ~(SCSSR2_ER
| SCSSR2_BRK
| SCSSR2_RDF
| SCSSR2_DR
));
395 err_c2
= scif_lsr_read();
396 scif_lsr_write(scif_lsr_read() & ~SCLSR2_ORER
);
399 if ((err_c
& (SCSSR2_ER
| SCSSR2_BRK
| SCSSR2_FER
403 if (CPU_IS_SH4
&& ((err_c2
& SCLSR2_ORER
) == 0))
412 scif_match(device_t parent
, cfdata_t cfp
, void *aux
)
418 if (strcmp(cfp
->cf_name
, "scif") != 0)
425 scif_attach(device_t parent
, device_t self
, void *aux
)
427 struct scif_softc
*sc
;
430 sc
= device_private(self
);
435 sc
->sc_hwflags
= 0; /* XXX */
436 sc
->sc_swflags
= 0; /* XXX */
441 aprint_naive(" (console)\n");
442 aprint_normal_dev(self
, "console\n");
443 SET(sc
->sc_hwflags
, SCIF_HW_CONSOLE
);
444 SET(sc
->sc_swflags
, TIOCFLAG_SOFTCAR
);
447 else if (kgdb_attached
) {
448 aprint_naive(" (kgdb)\n");
449 aprint_normal_dev(self
, "kgdb\n");
450 SET(sc
->sc_hwflags
, SCIF_HW_KGDB
);
451 SET(sc
->sc_swflags
, TIOCFLAG_SOFTCAR
);
456 InitializeScif(9600); /* XXX */
459 callout_init(&sc
->sc_diag_ch
, 0);
461 intc_intr_establish(SH4_INTEVT_SCIF_ERI
, IST_LEVEL
, IPL_SERIAL
,
463 intc_intr_establish(SH4_INTEVT_SCIF_RXI
, IST_LEVEL
, IPL_SERIAL
,
465 intc_intr_establish(SH4_INTEVT_SCIF_BRI
, IST_LEVEL
, IPL_SERIAL
,
467 intc_intr_establish(SH4_INTEVT_SCIF_TXI
, IST_LEVEL
, IPL_SERIAL
,
470 intc_intr_establish(SH7709_INTEVT2_SCIF_ERI
, IST_LEVEL
, IPL_SERIAL
,
472 intc_intr_establish(SH7709_INTEVT2_SCIF_RXI
, IST_LEVEL
, IPL_SERIAL
,
474 intc_intr_establish(SH7709_INTEVT2_SCIF_BRI
, IST_LEVEL
, IPL_SERIAL
,
476 intc_intr_establish(SH7709_INTEVT2_SCIF_TXI
, IST_LEVEL
, IPL_SERIAL
,
480 sc
->sc_si
= softint_establish(SOFTINT_SERIAL
, scifsoft
, sc
);
481 SET(sc
->sc_hwflags
, SCIF_HW_DEV_OK
);
484 tp
->t_oproc
= scifstart
;
485 tp
->t_param
= scifparam
;
486 tp
->t_hwiflow
= NULL
;
489 sc
->sc_rbuf
= malloc(scif_rbuf_size
<< 1, M_DEVBUF
, M_NOWAIT
);
490 if (sc
->sc_rbuf
== NULL
) {
491 aprint_error_dev(self
, "unable to allocate ring buffer\n");
494 sc
->sc_ebuf
= sc
->sc_rbuf
+ (scif_rbuf_size
<< 1);
499 if (!pmf_device_register(self
, NULL
, NULL
))
500 aprint_error_dev(self
, "unable to establish power handler\n");
504 * Start or restart transmission.
507 scifstart(struct tty
*tp
)
509 struct scif_softc
*sc
;
512 sc
= device_lookup_private(&scif_cd
, SCIFUNIT(tp
->t_dev
));
515 if (ISSET(tp
->t_state
, TS_BUSY
| TS_TIMEOUT
| TS_TTSTOP
))
517 if (sc
->sc_tx_stopped
)
522 /* Grab the first contiguous region of buffer space. */
527 tba
= tp
->t_outq
.c_cf
;
528 tbc
= ndqb(&tp
->t_outq
, 0);
536 SET(tp
->t_state
, TS_BUSY
);
539 /* Enable transmit completion interrupts if necessary. */
540 scif_scr_write(scif_scr_read() | SCSCR2_TIE
| SCSCR2_RIE
);
542 /* Output the first chunk of the contiguous buffer. */
549 maxchars
= sc
->sc_fifolen
550 - ((scif_fdr_read() & SCFDR2_TXCNT
) >> 8);
554 for (i
= 0; i
< n
; i
++) {
555 scif_putc(*(sc
->sc_tba
));
566 * Set SCIF tty parameters from termios.
567 * XXX - Should just copy the whole termios after
568 * making sure all the changes could be done.
571 scifparam(struct tty
*tp
, struct termios
*t
)
573 struct scif_softc
*sc
;
574 int ospeed
= t
->c_ospeed
;
577 sc
= device_lookup_private(&scif_cd
, SCIFUNIT(tp
->t_dev
));
578 if (!device_is_active(sc
->sc_dev
))
581 /* Check requested parameters. */
584 if (t
->c_ispeed
&& t
->c_ispeed
!= t
->c_ospeed
)
588 * For the console, always force CLOCAL and !HUPCL, so that the port
591 if (ISSET(sc
->sc_swflags
, TIOCFLAG_SOFTCAR
) ||
592 ISSET(sc
->sc_hwflags
, SCIF_HW_CONSOLE
)) {
593 SET(t
->c_cflag
, CLOCAL
);
594 CLR(t
->c_cflag
, HUPCL
);
598 * If there were no changes, don't do anything. This avoids dropping
599 * input and improves performance when all we did was frob things like
602 if (tp
->t_ospeed
== t
->c_ospeed
&&
603 tp
->t_cflag
== t
->c_cflag
)
608 lcr
= ISSET(sc
->sc_lcr
, LCR_SBREAK
) | cflag2lcr(t
->c_cflag
);
614 * Set the flow control pins depending on the current flow control
617 if (ISSET(t
->c_cflag
, CRTSCTS
)) {
618 scif_fcr_write(scif_fcr_read() | SCFCR2_MCE
);
620 scif_fcr_write(scif_fcr_read() & ~SCFCR2_MCE
);
623 scif_brr_write(divrnd(sh_clock_get_pclock(), 32 * ospeed
) -1);
626 * Set the FIFO threshold based on the receive speed.
628 * * If it's a low speed, it's probably a mouse or some other
629 * interactive device, so set the threshold low.
630 * * If it's a high speed, trim the trigger level down to prevent
632 * * Otherwise set it a bit higher.
636 if (ISSET(sc
->sc_hwflags
, SCIF_HW_HAYESP
))
637 sc
->sc_fifo
= FIFO_DMA_MODE
| FIFO_ENABLE
| FIFO_TRIGGER_8
;
638 else if (ISSET(sc
->sc_hwflags
, SCIF_HW_FIFO
))
639 sc
->sc_fifo
= FIFO_ENABLE
|
640 (t
->c_ospeed
<= 1200 ? FIFO_TRIGGER_1
:
641 t
->c_ospeed
<= 38400 ? FIFO_TRIGGER_8
: FIFO_TRIGGER_4
);
646 /* And copy to tty. */
648 tp
->t_ospeed
= t
->c_ospeed
;
649 tp
->t_cflag
= t
->c_cflag
;
651 if (!sc
->sc_heldchange
) {
652 if (sc
->sc_tx_busy
) {
653 sc
->sc_heldtbc
= sc
->sc_tbc
;
655 sc
->sc_heldchange
= 1;
660 scif_loadchannelregs(sc
);
664 if (!ISSET(t
->c_cflag
, CHWFLOW
)) {
665 /* Disable the high water mark. */
668 if (ISSET(sc
->sc_rx_flags
, RX_TTY_OVERFLOWED
)) {
669 CLR(sc
->sc_rx_flags
, RX_TTY_OVERFLOWED
);
673 sc
->sc_r_hiwat
= scif_rbuf_hiwat
;
674 sc
->sc_r_lowat
= scif_rbuf_lowat
;
681 scifstatus(sc
, "scifparam ");
684 if (!ISSET(t
->c_cflag
, CHWFLOW
)) {
685 if (sc
->sc_tx_stopped
) {
686 sc
->sc_tx_stopped
= 0;
695 scif_iflush(struct scif_softc
*sc
)
700 i
= scif_fdr_read() & SCFDR2_RECVCNT
;
703 c
= scif_frdr_read();
704 scif_ssr_write(scif_ssr_read() & ~(SCSSR2_RDF
| SCSSR2_DR
));
710 scifopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
712 struct scif_softc
*sc
;
717 sc
= device_lookup_private(&scif_cd
, SCIFUNIT(dev
));
718 if (sc
== 0 || !ISSET(sc
->sc_hwflags
, SCIF_HW_DEV_OK
) ||
722 if (!device_is_active(sc
->sc_dev
))
727 * If this is the kgdb port, no other use is permitted.
729 if (ISSET(sc
->sc_hwflags
, SCIF_HW_KGDB
))
735 if (kauth_authorize_device_tty(l
->l_cred
, KAUTH_DEVICE_TTY_OPEN
, tp
))
741 * Do the following iff this is a first open.
743 if (!ISSET(tp
->t_state
, TS_ISOPEN
) && tp
->t_wopen
== 0) {
750 /* Turn on interrupts. */
751 scif_scr_write(scif_scr_read() | SCSCR2_TIE
| SCSCR2_RIE
);
756 * Initialize the termios status to the defaults. Add in the
757 * sticky bits from TIOCSFLAGS.
760 if (ISSET(sc
->sc_hwflags
, SCIF_HW_CONSOLE
)) {
761 t
.c_ospeed
= scifcn_speed
; /* XXX (msaitoh) */
762 t
.c_cflag
= scifconscflag
;
764 t
.c_ospeed
= TTYDEF_SPEED
;
765 t
.c_cflag
= TTYDEF_CFLAG
;
767 if (ISSET(sc
->sc_swflags
, TIOCFLAG_CLOCAL
))
768 SET(t
.c_cflag
, CLOCAL
);
769 if (ISSET(sc
->sc_swflags
, TIOCFLAG_CRTSCTS
))
770 SET(t
.c_cflag
, CRTSCTS
);
771 if (ISSET(sc
->sc_swflags
, TIOCFLAG_MDMBUF
))
772 SET(t
.c_cflag
, MDMBUF
);
773 /* Make sure scifparam() will do something. */
775 (void) scifparam(tp
, &t
);
776 tp
->t_iflag
= TTYDEF_IFLAG
;
777 tp
->t_oflag
= TTYDEF_OFLAG
;
778 tp
->t_lflag
= TTYDEF_LFLAG
;
784 /* Clear the input ring, and unblock. */
785 sc
->sc_rbput
= sc
->sc_rbget
= sc
->sc_rbuf
;
786 sc
->sc_rbavail
= scif_rbuf_size
;
788 CLR(sc
->sc_rx_flags
, RX_ANY_BLOCK
);
796 scifstatus(sc
, "scifopen ");
804 error
= ttyopen(tp
, SCIFDIALOUT(dev
), ISSET(flag
, O_NONBLOCK
));
808 error
= (*tp
->t_linesw
->l_open
)(dev
, tp
);
820 scifclose(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
822 struct scif_softc
*sc
;
825 sc
= device_lookup_private(&scif_cd
, SCIFUNIT(dev
));
828 /* XXX This is for cons.c. */
829 if (!ISSET(tp
->t_state
, TS_ISOPEN
))
832 (*tp
->t_linesw
->l_close
)(tp
, flag
);
835 if (!device_is_active(sc
->sc_dev
))
842 scifread(dev_t dev
, struct uio
*uio
, int flag
)
844 struct scif_softc
*sc
;
847 sc
= device_lookup_private(&scif_cd
, SCIFUNIT(dev
));
850 return ((*tp
->t_linesw
->l_read
)(tp
, uio
, flag
));
854 scifwrite(dev_t dev
, struct uio
*uio
, int flag
)
856 struct scif_softc
*sc
;
859 sc
= device_lookup_private(&scif_cd
, SCIFUNIT(dev
));
862 return ((*tp
->t_linesw
->l_write
)(tp
, uio
, flag
));
866 scifpoll(dev_t dev
, int events
, struct lwp
*l
)
868 struct scif_softc
*sc
;
871 sc
= device_lookup_private(&scif_cd
, SCIFUNIT(dev
));
874 return ((*tp
->t_linesw
->l_poll
)(tp
, events
, l
));
880 struct scif_softc
*sc
;
883 sc
= device_lookup_private(&scif_cd
, SCIFUNIT(dev
));
890 scifioctl(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
892 struct scif_softc
*sc
;
897 sc
= device_lookup_private(&scif_cd
, SCIFUNIT(dev
));
898 if (!device_is_active(sc
->sc_dev
))
902 error
= (*tp
->t_linesw
->l_ioctl
)(tp
, cmd
, data
, flag
, l
);
903 if (error
!= EPASSTHROUGH
)
906 error
= ttioctl(tp
, cmd
, data
, flag
, l
);
907 if (error
!= EPASSTHROUGH
)
924 *(int *)data
= sc
->sc_swflags
;
928 error
= kauth_authorize_device_tty(l
->l_cred
,
929 KAUTH_DEVICE_TTY_PRIVSET
, tp
);
932 sc
->sc_swflags
= *(int *)data
;
936 error
= EPASSTHROUGH
;
946 scif_schedrx(struct scif_softc
*sc
)
951 /* Wake up the poller. */
952 softint_schedule(sc
->sc_si
);
956 scif_break(struct scif_softc
*sc
, int onoff
)
960 scif_ssr_write(scif_ssr_read() & ~SCSSR2_TDFE
);
962 scif_ssr_write(scif_ssr_read() | SCSSR2_TDFE
);
965 if (!sc
->sc_heldchange
) {
966 if (sc
->sc_tx_busy
) {
967 sc
->sc_heldtbc
= sc
->sc_tbc
;
969 sc
->sc_heldchange
= 1;
971 scif_loadchannelregs(sc
);
977 * Stop output, e.g., for ^S or output flush.
980 scifstop(struct tty
*tp
, int flag
)
982 struct scif_softc
*sc
;
985 sc
= device_lookup_private(&scif_cd
, SCIFUNIT(tp
->t_dev
));
988 if (ISSET(tp
->t_state
, TS_BUSY
)) {
989 /* Stop transmitting at the next chunk. */
992 if (!ISSET(tp
->t_state
, TS_TTSTOP
))
993 SET(tp
->t_state
, TS_FLUSH
);
1001 struct scif_softc
*sc
= arg
;
1002 int overflows
, floods
;
1006 overflows
= sc
->sc_overflows
;
1007 sc
->sc_overflows
= 0;
1008 floods
= sc
->sc_floods
;
1013 log(LOG_WARNING
, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1014 device_xname(sc
->sc_dev
),
1015 overflows
, overflows
== 1 ? "" : "s",
1016 floods
, floods
== 1 ? "" : "s");
1020 scif_rxsoft(struct scif_softc
*sc
, struct tty
*tp
)
1022 int (*rint
)(int, struct tty
*) = tp
->t_linesw
->l_rint
;
1031 scc
= cc
= scif_rbuf_size
- sc
->sc_rbavail
;
1033 if (cc
== scif_rbuf_size
) {
1035 if (sc
->sc_errors
++ == 0)
1036 callout_reset(&sc
->sc_diag_ch
, 60 * hz
, scifdiag
, sc
);
1042 if (ISSET(ssr2
, SCSSR2_BRK
| SCSSR2_FER
| SCSSR2_PER
)) {
1043 if (ISSET(ssr2
, SCSSR2_BRK
| SCSSR2_FER
))
1045 if (ISSET(ssr2
, SCSSR2_PER
))
1048 if ((*rint
)(code
, tp
) == -1) {
1050 * The line discipline's buffer is out of space.
1052 if (!ISSET(sc
->sc_rx_flags
, RX_TTY_BLOCKED
)) {
1054 * We're either not using flow control, or the
1055 * line discipline didn't tell us to block for
1056 * some reason. Either way, we have no way to
1057 * know when there's more space available, so
1058 * just drop the rest of the data.
1062 get
-= scif_rbuf_size
<< 1;
1066 * Don't schedule any more receive processing
1067 * until the line discipline tells us there's
1068 * space available (through scifhwiflow()).
1069 * Leave the rest of the data in the input
1072 SET(sc
->sc_rx_flags
, RX_TTY_OVERFLOWED
);
1085 cc
= sc
->sc_rbavail
+= scc
- cc
;
1086 /* Buffers should be ok again, release possible block. */
1087 if (cc
>= sc
->sc_r_lowat
) {
1088 if (ISSET(sc
->sc_rx_flags
, RX_IBUF_OVERFLOWED
)) {
1089 CLR(sc
->sc_rx_flags
, RX_IBUF_OVERFLOWED
);
1090 scif_scr_write(scif_scr_read() | SCSCR2_RIE
);
1093 if (ISSET(sc
->sc_rx_flags
, RX_IBUF_BLOCKED
)) {
1094 CLR(sc
->sc_rx_flags
, RX_IBUF_BLOCKED
);
1104 scif_txsoft(struct scif_softc
*sc
, struct tty
*tp
)
1107 CLR(tp
->t_state
, TS_BUSY
);
1108 if (ISSET(tp
->t_state
, TS_FLUSH
))
1109 CLR(tp
->t_state
, TS_FLUSH
);
1111 ndflush(&tp
->t_outq
, (int)(sc
->sc_tba
- tp
->t_outq
.c_cf
));
1112 (*tp
->t_linesw
->l_start
)(tp
);
1115 #if 0 /* XXX (msaitoh) */
1117 scif_stsoft(struct scif_softc
*sc
, struct tty
*tp
)
1124 delta
= sc
->sc_msr_delta
;
1125 sc
->sc_msr_delta
= 0;
1128 if (ISSET(delta
, sc
->sc_msr_dcd
)) {
1130 * Inform the tty layer that carrier detect changed.
1132 (void) (*tp
->t_linesw
->l_modem
)(tp
, ISSET(msr
, MSR_DCD
));
1135 if (ISSET(delta
, sc
->sc_msr_cts
)) {
1136 /* Block or unblock output according to flow control. */
1137 if (ISSET(msr
, sc
->sc_msr_cts
)) {
1138 sc
->sc_tx_stopped
= 0;
1139 (*tp
->t_linesw
->l_start
)(tp
);
1141 sc
->sc_tx_stopped
= 1;
1147 scifstatus(sc
, "scif_stsoft");
1155 struct scif_softc
*sc
= arg
;
1158 if (!device_is_active(sc
->sc_dev
))
1163 if (sc
->sc_rx_ready
) {
1164 sc
->sc_rx_ready
= 0;
1165 scif_rxsoft(sc
, tp
);
1169 if (sc
->sc_st_check
) {
1170 sc
->sc_st_check
= 0;
1171 scif_stsoft(sc
, tp
);
1175 if (sc
->sc_tx_done
) {
1177 scif_txsoft(sc
, tp
);
1184 struct scif_softc
*sc
= arg
;
1190 if (!device_is_active(sc
->sc_dev
))
1195 cc
= sc
->sc_rbavail
;
1198 ssr2
= scif_ssr_read();
1199 if (ISSET(ssr2
, SCSSR2_BRK
)) {
1200 scif_ssr_write(scif_ssr_read()
1201 & ~(SCSSR2_ER
| SCSSR2_BRK
| SCSSR2_DR
));
1203 if (ISSET(sc
->sc_hwflags
, SCIF_HW_CONSOLE
)) {
1208 if (ISSET(sc
->sc_hwflags
, SCIF_HW_KGDB
)) {
1213 count
= scif_fdr_read() & SCFDR2_RECVCNT
;
1216 u_char c
= scif_frdr_read();
1217 u_char err
= (u_char
)(scif_ssr_read() & 0x00ff);
1219 scif_ssr_write(scif_ssr_read()
1220 & ~(SCSSR2_ER
| SCSSR2_RDF
| SCSSR2_DR
));
1223 scif_lsr_write(scif_lsr_read()
1226 if ((cc
> 0) && (count
> 0)) {
1239 * Current string of incoming characters ended because
1240 * no more data was available or we ran out of space.
1241 * Schedule a receive event if any data was received.
1242 * If we're out of space, turn off receive interrupts.
1245 sc
->sc_rbavail
= cc
;
1246 if (!ISSET(sc
->sc_rx_flags
, RX_TTY_OVERFLOWED
))
1247 sc
->sc_rx_ready
= 1;
1250 * See if we are in danger of overflowing a buffer. If
1251 * so, use hardware flow control to ease the pressure.
1253 if (!ISSET(sc
->sc_rx_flags
, RX_IBUF_BLOCKED
) &&
1254 cc
< sc
->sc_r_hiwat
) {
1255 SET(sc
->sc_rx_flags
, RX_IBUF_BLOCKED
);
1262 * If we're out of space, disable receive interrupts
1263 * until the queue has drained a bit.
1266 SET(sc
->sc_rx_flags
, RX_IBUF_OVERFLOWED
);
1267 scif_scr_write(scif_scr_read() & ~SCSCR2_RIE
);
1270 if (scif_ssr_read() & (SCSSR2_RDF
| SCSSR2_DR
)) {
1271 scif_scr_write(scif_scr_read()
1272 & ~(SCSCR2_TIE
| SCSCR2_RIE
));
1274 scif_scr_write(scif_scr_read()
1275 | SCSCR2_TIE
| SCSCR2_RIE
);
1279 } while (scif_ssr_read() & (SCSSR2_RDF
| SCSSR2_DR
));
1282 msr
= bus_space_read_1(iot
, ioh
, scif_msr
);
1283 delta
= msr
^ sc
->sc_msr
;
1285 if (ISSET(delta
, sc
->sc_msr_mask
)) {
1286 SET(sc
->sc_msr_delta
, delta
);
1289 * Pulse-per-second clock signal on edge of DCD?
1291 if (ISSET(delta
, sc
->sc_ppsmask
)) {
1293 if (ISSET(msr
, sc
->sc_ppsmask
) ==
1295 /* XXX nanotime() */
1297 TIMEVAL_TO_TIMESPEC(&tv
,
1298 &sc
->ppsinfo
.assert_timestamp
);
1299 if (sc
->ppsparam
.mode
& PPS_OFFSETASSERT
) {
1300 timespecadd(&sc
->ppsinfo
.assert_timestamp
,
1301 &sc
->ppsparam
.assert_offset
,
1302 &sc
->ppsinfo
.assert_timestamp
);
1303 TIMESPEC_TO_TIMEVAL(&tv
, &sc
->ppsinfo
.assert_timestamp
);
1307 if (sc
->ppsparam
.mode
& PPS_HARDPPSONASSERT
)
1308 hardpps(&tv
, tv
.tv_usec
);
1310 sc
->ppsinfo
.assert_sequence
++;
1311 sc
->ppsinfo
.current_mode
=
1314 } else if (ISSET(msr
, sc
->sc_ppsmask
) ==
1316 /* XXX nanotime() */
1318 TIMEVAL_TO_TIMESPEC(&tv
,
1319 &sc
->ppsinfo
.clear_timestamp
);
1320 if (sc
->ppsparam
.mode
& PPS_OFFSETCLEAR
) {
1321 timespecadd(&sc
->ppsinfo
.clear_timestamp
,
1322 &sc
->ppsparam
.clear_offset
,
1323 &sc
->ppsinfo
.clear_timestamp
);
1324 TIMESPEC_TO_TIMEVAL(&tv
, &sc
->ppsinfo
.clear_timestamp
);
1328 if (sc
->ppsparam
.mode
& PPS_HARDPPSONCLEAR
)
1329 hardpps(&tv
, tv
.tv_usec
);
1331 sc
->ppsinfo
.clear_sequence
++;
1332 sc
->ppsinfo
.current_mode
=
1338 * Stop output immediately if we lose the output
1339 * flow control signal or carrier detect.
1341 if (ISSET(~msr
, sc
->sc_msr_mask
)) {
1346 scifstatus(sc
, "scifintr ");
1350 sc
->sc_st_check
= 1;
1355 * Done handling any receive interrupts. See if data can be
1356 * transmitted as well. Schedule tx done event if no data left
1357 * and tty was marked busy.
1359 if (((scif_fdr_read() & SCFDR2_TXCNT
) >> 8) != 16) { /* XXX (msaitoh) */
1361 * If we've delayed a parameter change, do it now, and restart
1364 if (sc
->sc_heldchange
) {
1365 sc
->sc_heldchange
= 0;
1366 sc
->sc_tbc
= sc
->sc_heldtbc
;
1370 /* Output the next chunk of the contiguous buffer, if any. */
1371 if (sc
->sc_tbc
> 0) {
1377 maxchars
= sc
->sc_fifolen
-
1378 ((scif_fdr_read() & SCFDR2_TXCNT
) >> 8);
1382 for (i
= 0; i
< n
; i
++) {
1383 scif_putc(*(sc
->sc_tba
));
1388 /* Disable transmit completion interrupts if necessary. */
1390 if (ISSET(sc
->sc_ier
, IER_ETXRDY
))
1392 scif_scr_write(scif_scr_read() & ~SCSCR2_TIE
);
1394 if (sc
->sc_tx_busy
) {
1401 /* Wake up the poller. */
1402 softint_schedule(sc
->sc_si
);
1404 #if NRND > 0 && defined(RND_SCIF)
1405 rnd_add_uint32(&sc
->rnd_source
, iir
| lsr
);
1412 scifcnprobe(struct consdev
*cp
)
1416 /* locate the major number */
1417 maj
= cdevsw_lookup_major(&scif_cdevsw
);
1419 /* Initialize required fields. */
1420 cp
->cn_dev
= makedev(maj
, 0);
1422 cp
->cn_pri
= CN_REMOTE
;
1424 cp
->cn_pri
= CN_NORMAL
;
1429 scifcninit(struct consdev
*cp
)
1432 InitializeScif(scifcn_speed
);
1437 scifcngetc(dev_t dev
)
1450 scifcnputc(dev_t dev
, int c
)
1455 scif_putc((u_char
)c
);
1461 scif_kgdb_init(void)
1464 if (strcmp(kgdb_devname
, "scif") != 0)
1468 return (1); /* can't share with console */
1470 InitializeScif(kgdb_rate
);
1472 kgdb_attach((int (*)(void *))scifcngetc
,
1473 (void (*)(void *, int))scifcnputc
, NULL
);
1474 kgdb_dev
= 123; /* unneeded, only to satisfy some tests */