sci.c revision 1.57 1 /* $NetBSD: sci.c,v 1.57 2012/12/12 13:32:37 tsutsui Exp $ */
2
3 /*-
4 * Copyright (C) 1999 T.Horiuchi and SAITOH Masanobu. All rights reserved.
5 *
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.
16 *
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.
27 */
28
29 /*-
30 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
31 * All rights reserved.
32 *
33 * This code is derived from software contributed to The NetBSD Foundation
34 * by Charles M. Hannum.
35 *
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.
44 *
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.
56 */
57
58 /*
59 * Copyright (c) 1991 The Regents of the University of California.
60 * All rights reserved.
61 *
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.
73 *
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.
85 *
86 * @(#)com.c 7.5 (Berkeley) 5/16/91
87 */
88
89 /*
90 * SH internal serial driver
91 *
92 * This code is derived from both z8530tty.c and com.c
93 */
94
95 #include <sys/cdefs.h>
96 __KERNEL_RCSID(0, "$NetBSD: sci.c,v 1.57 2012/12/12 13:32:37 tsutsui Exp $");
97
98 #include "opt_kgdb.h"
99 #include "opt_sci.h"
100
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>
113
114 #include <dev/cons.h>
115
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>
121
122 static void scistart(struct tty *);
123 static int sciparam(struct tty *, struct termios *);
124
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 *);
131
132 struct sci_softc {
133 device_t sc_dev; /* boilerplate */
134 struct tty *sc_tty;
135 void *sc_si;
136 callout_t sc_diag_ch;
137
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 */
141
142 int sc_drq;
143
144 int sc_frequency;
145 #endif
146
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 */
151
152 int sc_hwflags;
153 int sc_swflags;
154 u_int sc_fifolen; /* XXX always 0? */
155
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;
163
164 u_char *sc_tba; /* transmit buffer address */
165 u_int sc_tbc, /* transmit byte count */
166 sc_heldtbc;
167
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;
179
180 volatile u_char sc_heldchange;
181 };
182
183 /* controller driver configuration */
184 static int sci_match(device_t, cfdata_t, void *);
185 static void sci_attach(device_t, device_t, void *);
186
187 void sci_break(struct sci_softc *, int);
188 void sci_iflush(struct sci_softc *);
189
190 #define integrate static inline
191 void scisoft(void *);
192
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 *);
198
199 #define SCIUNIT_MASK 0x7ffff
200 #define SCIDIALOUT_MASK 0x80000
201
202 #define SCIUNIT(x) (minor(x) & SCIUNIT_MASK)
203 #define SCIDIALOUT(x) (minor(x) & SCIDIALOUT_MASK)
204
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
212
213 /* Buffer size for character buffer */
214 #define SCI_RING_SIZE 2048
215
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;
219
220 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
221 int sciconscflag = CONMODE;
222 int sciisconsole = 0;
223
224 #ifdef SCICN_SPEED
225 int scicn_speed = SCICN_SPEED;
226 #else
227 int scicn_speed = 9600;
228 #endif
229
230 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
231
232 u_int sci_rbuf_size = SCI_RING_SIZE;
233
234 CFATTACH_DECL_NEW(sci, sizeof(struct sci_softc),
235 sci_match, sci_attach, NULL, NULL);
236
237 extern struct cfdriver sci_cd;
238
239 static int sci_attached;
240
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);
249
250 const struct cdevsw sci_cdevsw = {
251 sciopen, sciclose, sciread, sciwrite, sciioctl,
252 scistop, scitty, scipoll, nommap, ttykqfilter, D_TTY
253 };
254
255 void InitializeSci (unsigned int);
256
257 /*
258 * following functions are debugging prupose only
259 */
260 #define CR 0x0D
261 #define I2C_ADRS (*(volatile unsigned int *)0xa8000000)
262 #define USART_ON (unsigned int)~0x08
263
264 void sci_putc(unsigned char);
265 unsigned char sci_getc(void);
266 int SciErrCheck(void);
267
268 /*
269 * InitializeSci
270 * : unsigned int bps;
271 * : SCI(Serial Communication Interface)
272 */
273
274 void
275 InitializeSci(unsigned int bps)
276 {
277
278 /* Initialize SCR */
279 SHREG_SCSCR = 0x00;
280
281 /* Serial Mode Register */
282 SHREG_SCSMR = 0x00; /* Async,8bit,NonParity,Even,1Stop,NoMulti */
283
284 /* Bit Rate Register */
285 SHREG_SCBRR = divrnd(sh_clock_get_pclock(), 32 * bps) - 1;
286
287 /*
288 * wait 1mSec, because Send/Recv must begin 1 bit period after
289 * BRR is set.
290 */
291 delay(1000);
292
293 /* Send permission, Receive permission ON */
294 SHREG_SCSCR = SCSCR_TE | SCSCR_RE;
295
296 /* Serial Status Register */
297 SHREG_SCSSR &= SCSSR_TDRE; /* Clear Status */
298
299 #if 0
300 I2C_ADRS &= ~0x08; /* enable RS-232C */
301 #endif
302 }
303
304
305 /*
306 * sci_putc
307 * : unsigned char c;
308 */
309 void
310 sci_putc(unsigned char c)
311 {
312
313 /* wait for ready */
314 while ((SHREG_SCSSR & SCSSR_TDRE) == 0)
315 ;
316
317 /* write send data to send register */
318 SHREG_SCTDR = c;
319
320 /* clear ready flag */
321 SHREG_SCSSR &= ~SCSSR_TDRE;
322 }
323
324 /*
325 * : SciErrCheck
326 * 0x20 = over run
327 * 0x10 = frame error
328 * 0x80 = parity error
329 */
330 int
331 SciErrCheck(void)
332 {
333
334 return(SHREG_SCSSR & (SCSSR_ORER | SCSSR_FER | SCSSR_PER));
335 }
336
337 /*
338 * sci_getc
339 */
340 unsigned char
341 sci_getc(void)
342 {
343 unsigned char c, err_c;
344
345 while (((err_c = SHREG_SCSSR)
346 & (SCSSR_RDRF | SCSSR_ORER | SCSSR_FER | SCSSR_PER)) == 0)
347 ;
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);
351 }
352
353 c = SHREG_SCRDR;
354
355 SHREG_SCSSR &= ~SCSSR_RDRF;
356
357 return(c);
358 }
359
360 static int
361 sci_match(device_t parent, cfdata_t cf, void *aux)
362 {
363
364 if (strcmp(cf->cf_name, "sci") || sci_attached)
365 return 0;
366
367 return 1;
368 }
369
370 static void
371 sci_attach(device_t parent, device_t self, void *aux)
372 {
373 struct sci_softc *sc = device_private(self);
374 struct tty *tp;
375
376 sci_attached = 1;
377
378 sc->sc_dev = self;
379 sc->sc_hwflags = 0; /* XXX */
380 sc->sc_swflags = 0; /* XXX */
381 sc->sc_fifolen = 0; /* XXX */
382
383 if (sciisconsole) {
384 SET(sc->sc_hwflags, SCI_HW_CONSOLE);
385 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
386 printf("\n%s: console\n", device_xname(self));
387 } else {
388 InitializeSci(9600);
389 printf("\n");
390 }
391
392 callout_init(&sc->sc_diag_ch, 0);
393
394 intc_intr_establish(SH_INTEVT_SCI_ERI, IST_LEVEL, IPL_SERIAL, sciintr,
395 sc);
396 intc_intr_establish(SH_INTEVT_SCI_RXI, IST_LEVEL, IPL_SERIAL, sciintr,
397 sc);
398 intc_intr_establish(SH_INTEVT_SCI_TXI, IST_LEVEL, IPL_SERIAL, sciintr,
399 sc);
400 intc_intr_establish(SH_INTEVT_SCI_TEI, IST_LEVEL, IPL_SERIAL, sciintr,
401 sc);
402
403 sc->sc_si = softint_establish(SOFTINT_SERIAL, scisoft, sc);
404 SET(sc->sc_hwflags, SCI_HW_DEV_OK);
405
406 tp = tty_alloc();
407 tp->t_oproc = scistart;
408 tp->t_param = sciparam;
409 tp->t_hwiflow = NULL;
410
411 sc->sc_tty = tp;
412 sc->sc_rbuf = malloc(sci_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
413 if (sc->sc_rbuf == NULL) {
414 printf("%s: unable to allocate ring buffer\n",
415 device_xname(self));
416 return;
417 }
418 sc->sc_ebuf = sc->sc_rbuf + (sci_rbuf_size << 1);
419
420 tty_attach(tp);
421 }
422
423 /*
424 * Start or restart transmission.
425 */
426 static void
427 scistart(struct tty *tp)
428 {
429 struct sci_softc *sc = device_lookup_private(&sci_cd,SCIUNIT(tp->t_dev));
430 int s;
431
432 s = spltty();
433 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
434 goto out;
435 if (sc->sc_tx_stopped)
436 goto out;
437 if (!ttypull(tp))
438 goto out;
439
440 /* Grab the first contiguous region of buffer space. */
441 {
442 u_char *tba;
443 int tbc;
444
445 tba = tp->t_outq.c_cf;
446 tbc = ndqb(&tp->t_outq, 0);
447
448 (void)splserial();
449
450 sc->sc_tba = tba;
451 sc->sc_tbc = tbc;
452 }
453
454 SET(tp->t_state, TS_BUSY);
455 sc->sc_tx_busy = 1;
456
457 /* Enable transmit completion interrupts if necessary. */
458 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
459
460 /* Output the first byte of the contiguous buffer. */
461 {
462 if (sc->sc_tbc > 0) {
463 sci_putc(*(sc->sc_tba));
464 sc->sc_tba++;
465 sc->sc_tbc--;
466 }
467 }
468 out:
469 splx(s);
470 return;
471 }
472
473 /*
474 * Set SCI tty parameters from termios.
475 * XXX - Should just copy the whole termios after
476 * making sure all the changes could be done.
477 */
478 static int
479 sciparam(struct tty *tp, struct termios *t)
480 {
481 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(tp->t_dev));
482 int ospeed = t->c_ospeed;
483 int s;
484
485 if (!device_is_active(sc->sc_dev))
486 return (EIO);
487
488 /* Check requested parameters. */
489 if (ospeed < 0)
490 return (EINVAL);
491 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
492 return (EINVAL);
493
494 /*
495 * For the console, always force CLOCAL and !HUPCL, so that the port
496 * is always active.
497 */
498 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
499 ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
500 SET(t->c_cflag, CLOCAL);
501 CLR(t->c_cflag, HUPCL);
502 }
503
504 /*
505 * If there were no changes, don't do anything. This avoids dropping
506 * input and improves performance when all we did was frob things like
507 * VMIN and VTIME.
508 */
509 if (tp->t_ospeed == t->c_ospeed &&
510 tp->t_cflag == t->c_cflag)
511 return (0);
512
513 #if 0
514 /* XXX (msaitoh) */
515 lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
516 #endif
517
518 s = splserial();
519
520 /*
521 * Set the FIFO threshold based on the receive speed.
522 *
523 * * If it's a low speed, it's probably a mouse or some other
524 * interactive device, so set the threshold low.
525 * * If it's a high speed, trim the trigger level down to prevent
526 * overflows.
527 * * Otherwise set it a bit higher.
528 */
529 #if 0
530 /* XXX (msaitoh) */
531 if (ISSET(sc->sc_hwflags, SCI_HW_HAYESP))
532 sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
533 else if (ISSET(sc->sc_hwflags, SCI_HW_FIFO))
534 sc->sc_fifo = FIFO_ENABLE |
535 (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
536 t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
537 else
538 sc->sc_fifo = 0;
539 #endif
540
541 /* And copy to tty. */
542 tp->t_ispeed = 0;
543 tp->t_ospeed = t->c_ospeed;
544 tp->t_cflag = t->c_cflag;
545
546 if (!sc->sc_heldchange) {
547 if (sc->sc_tx_busy) {
548 sc->sc_heldtbc = sc->sc_tbc;
549 sc->sc_tbc = 0;
550 sc->sc_heldchange = 1;
551 }
552 #if 0
553 /* XXX (msaitoh) */
554 else
555 sci_loadchannelregs(sc);
556 #endif
557 }
558
559 if (!ISSET(t->c_cflag, CHWFLOW)) {
560 /* Disable the high water mark. */
561 sc->sc_r_hiwat = 0;
562 sc->sc_r_lowat = 0;
563 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
564 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
565 sci_schedrx(sc);
566 }
567 } else {
568 sc->sc_r_hiwat = sci_rbuf_hiwat;
569 sc->sc_r_lowat = sci_rbuf_lowat;
570 }
571
572 splx(s);
573
574 #ifdef SCI_DEBUG
575 if (sci_debug)
576 scistatus(sc, "sciparam ");
577 #endif
578
579 if (!ISSET(t->c_cflag, CHWFLOW)) {
580 if (sc->sc_tx_stopped) {
581 sc->sc_tx_stopped = 0;
582 scistart(tp);
583 }
584 }
585
586 return (0);
587 }
588
589 void
590 sci_iflush(struct sci_softc *sc)
591 {
592 unsigned char err_c;
593 volatile unsigned char c;
594
595 if (((err_c = SHREG_SCSSR)
596 & (SCSSR_RDRF | SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) {
597
598 if ((err_c & (SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) {
599 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER);
600 return;
601 }
602
603 c = SHREG_SCRDR;
604
605 SHREG_SCSSR &= ~SCSSR_RDRF;
606 }
607 }
608
609 int
610 sciopen(dev_t dev, int flag, int mode, struct lwp *l)
611 {
612 struct sci_softc *sc;
613 struct tty *tp;
614 int s, s2;
615 int error;
616
617 sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
618 if (sc == 0 || !ISSET(sc->sc_hwflags, SCI_HW_DEV_OK) ||
619 sc->sc_rbuf == NULL)
620 return (ENXIO);
621
622 if (!device_is_active(sc->sc_dev))
623 return (ENXIO);
624
625 #ifdef KGDB
626 /*
627 * If this is the kgdb port, no other use is permitted.
628 */
629 if (ISSET(sc->sc_hwflags, SCI_HW_KGDB))
630 return (EBUSY);
631 #endif
632
633 tp = sc->sc_tty;
634
635 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
636 return (EBUSY);
637
638 s = spltty();
639
640 /*
641 * Do the following iff this is a first open.
642 */
643 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
644 struct termios t;
645
646 tp->t_dev = dev;
647
648 s2 = splserial();
649
650 /* Turn on interrupts. */
651 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
652
653 splx(s2);
654
655 /*
656 * Initialize the termios status to the defaults. Add in the
657 * sticky bits from TIOCSFLAGS.
658 */
659 t.c_ispeed = 0;
660 if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
661 t.c_ospeed = scicn_speed;
662 t.c_cflag = sciconscflag;
663 } else {
664 t.c_ospeed = TTYDEF_SPEED;
665 t.c_cflag = TTYDEF_CFLAG;
666 }
667 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
668 SET(t.c_cflag, CLOCAL);
669 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
670 SET(t.c_cflag, CRTSCTS);
671 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
672 SET(t.c_cflag, MDMBUF);
673 /* Make sure sciparam() will do something. */
674 tp->t_ospeed = 0;
675 (void) sciparam(tp, &t);
676 tp->t_iflag = TTYDEF_IFLAG;
677 tp->t_oflag = TTYDEF_OFLAG;
678 tp->t_lflag = TTYDEF_LFLAG;
679 ttychars(tp);
680 ttsetwater(tp);
681
682 s2 = splserial();
683
684 /* Clear the input ring, and unblock. */
685 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
686 sc->sc_rbavail = sci_rbuf_size;
687 sci_iflush(sc);
688 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
689 #if 0
690 /* XXX (msaitoh) */
691 sci_hwiflow(sc);
692 #endif
693
694 #ifdef SCI_DEBUG
695 if (sci_debug)
696 scistatus(sc, "sciopen ");
697 #endif
698
699 splx(s2);
700 }
701
702 splx(s);
703
704 error = ttyopen(tp, SCIDIALOUT(dev), ISSET(flag, O_NONBLOCK));
705 if (error)
706 goto bad;
707
708 error = (*tp->t_linesw->l_open)(dev, tp);
709 if (error)
710 goto bad;
711
712 return (0);
713
714 bad:
715
716 return (error);
717 }
718
719 int
720 sciclose(dev_t dev, int flag, int mode, struct lwp *l)
721 {
722 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
723 struct tty *tp = sc->sc_tty;
724
725 /* XXX This is for cons.c. */
726 if (!ISSET(tp->t_state, TS_ISOPEN))
727 return (0);
728
729 (*tp->t_linesw->l_close)(tp, flag);
730 ttyclose(tp);
731
732 if (!device_is_active(sc->sc_dev))
733 return (0);
734
735 return (0);
736 }
737
738 int
739 sciread(dev_t dev, struct uio *uio, int flag)
740 {
741 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
742 struct tty *tp = sc->sc_tty;
743
744 return ((*tp->t_linesw->l_read)(tp, uio, flag));
745 }
746
747 int
748 sciwrite(dev_t dev, struct uio *uio, int flag)
749 {
750 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
751 struct tty *tp = sc->sc_tty;
752
753 return ((*tp->t_linesw->l_write)(tp, uio, flag));
754 }
755
756 int
757 scipoll(dev_t dev, int events, struct lwp *l)
758 {
759 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
760 struct tty *tp = sc->sc_tty;
761
762 return ((*tp->t_linesw->l_poll)(tp, events, l));
763 }
764
765 struct tty *
766 scitty(dev_t dev)
767 {
768 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
769 struct tty *tp = sc->sc_tty;
770
771 return (tp);
772 }
773
774 int
775 sciioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
776 {
777 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
778 struct tty *tp = sc->sc_tty;
779 int error;
780 int s;
781
782 if (!device_is_active(sc->sc_dev))
783 return (EIO);
784
785 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
786 if (error != EPASSTHROUGH)
787 return (error);
788
789 error = ttioctl(tp, cmd, data, flag, l);
790 if (error != EPASSTHROUGH)
791 return (error);
792
793 error = 0;
794
795 s = splserial();
796
797 switch (cmd) {
798 case TIOCSBRK:
799 sci_break(sc, 1);
800 break;
801
802 case TIOCCBRK:
803 sci_break(sc, 0);
804 break;
805
806 case TIOCGFLAGS:
807 *(int *)data = sc->sc_swflags;
808 break;
809
810 case TIOCSFLAGS:
811 error = kauth_authorize_device_tty(l->l_cred,
812 KAUTH_DEVICE_TTY_PRIVSET, tp);
813 if (error)
814 break;
815 sc->sc_swflags = *(int *)data;
816 break;
817
818 default:
819 error = EPASSTHROUGH;
820 break;
821 }
822
823 splx(s);
824
825 return (error);
826 }
827
828 integrate void
829 sci_schedrx(struct sci_softc *sc)
830 {
831
832 sc->sc_rx_ready = 1;
833
834 /* Wake up the poller. */
835 softint_schedule(sc->sc_si);
836 }
837
838 void
839 sci_break(struct sci_softc *sc, int onoff)
840 {
841
842 if (onoff)
843 SHREG_SCSSR &= ~SCSSR_TDRE;
844 else
845 SHREG_SCSSR |= SCSSR_TDRE;
846
847 #if 0 /* XXX */
848 if (!sc->sc_heldchange) {
849 if (sc->sc_tx_busy) {
850 sc->sc_heldtbc = sc->sc_tbc;
851 sc->sc_tbc = 0;
852 sc->sc_heldchange = 1;
853 } else
854 sci_loadchannelregs(sc);
855 }
856 #endif
857 }
858
859 /*
860 * Stop output, e.g., for ^S or output flush.
861 */
862 void
863 scistop(struct tty *tp, int flag)
864 {
865 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(tp->t_dev));
866 int s;
867
868 s = splserial();
869 if (ISSET(tp->t_state, TS_BUSY)) {
870 /* Stop transmitting at the next chunk. */
871 sc->sc_tbc = 0;
872 sc->sc_heldtbc = 0;
873 if (!ISSET(tp->t_state, TS_TTSTOP))
874 SET(tp->t_state, TS_FLUSH);
875 }
876 splx(s);
877 }
878
879 void
880 scidiag(void *arg)
881 {
882 struct sci_softc *sc = arg;
883 int overflows, floods;
884 int s;
885
886 s = splserial();
887 overflows = sc->sc_overflows;
888 sc->sc_overflows = 0;
889 floods = sc->sc_floods;
890 sc->sc_floods = 0;
891 sc->sc_errors = 0;
892 splx(s);
893
894 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
895 device_xname(sc->sc_dev),
896 overflows, overflows == 1 ? "" : "s",
897 floods, floods == 1 ? "" : "s");
898 }
899
900 integrate void
901 sci_rxsoft(struct sci_softc *sc, struct tty *tp)
902 {
903 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
904 u_char *get, *end;
905 u_int cc, scc;
906 u_char ssr;
907 int code;
908 int s;
909
910 end = sc->sc_ebuf;
911 get = sc->sc_rbget;
912 scc = cc = sci_rbuf_size - sc->sc_rbavail;
913
914 if (cc == sci_rbuf_size) {
915 sc->sc_floods++;
916 if (sc->sc_errors++ == 0)
917 callout_reset(&sc->sc_diag_ch, 60 * hz, scidiag, sc);
918 }
919
920 while (cc) {
921 code = get[0];
922 ssr = get[1];
923 if (ISSET(ssr, SCSSR_FER | SCSSR_PER)) {
924 if (ISSET(ssr, SCSSR_FER))
925 SET(code, TTY_FE);
926 if (ISSET(ssr, SCSSR_PER))
927 SET(code, TTY_PE);
928 }
929 if ((*rint)(code, tp) == -1) {
930 /*
931 * The line discipline's buffer is out of space.
932 */
933 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
934 /*
935 * We're either not using flow control, or the
936 * line discipline didn't tell us to block for
937 * some reason. Either way, we have no way to
938 * know when there's more space available, so
939 * just drop the rest of the data.
940 */
941 get += cc << 1;
942 if (get >= end)
943 get -= sci_rbuf_size << 1;
944 cc = 0;
945 } else {
946 /*
947 * Don't schedule any more receive processing
948 * until the line discipline tells us there's
949 * space available (through scihwiflow()).
950 * Leave the rest of the data in the input
951 * buffer.
952 */
953 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
954 }
955 break;
956 }
957 get += 2;
958 if (get >= end)
959 get = sc->sc_rbuf;
960 cc--;
961 }
962
963 if (cc != scc) {
964 sc->sc_rbget = get;
965 s = splserial();
966 cc = sc->sc_rbavail += scc - cc;
967 /* Buffers should be ok again, release possible block. */
968 if (cc >= sc->sc_r_lowat) {
969 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
970 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
971 SHREG_SCSCR |= SCSCR_RIE;
972 }
973 #if 0
974 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
975 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
976 sci_hwiflow(sc);
977 }
978 #endif
979 }
980 splx(s);
981 }
982 }
983
984 integrate void
985 sci_txsoft(struct sci_softc *sc, struct tty *tp)
986 {
987
988 CLR(tp->t_state, TS_BUSY);
989 if (ISSET(tp->t_state, TS_FLUSH))
990 CLR(tp->t_state, TS_FLUSH);
991 else
992 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
993 (*tp->t_linesw->l_start)(tp);
994 }
995
996 integrate void
997 sci_stsoft(struct sci_softc *sc, struct tty *tp)
998 {
999 #if 0
1000 /* XXX (msaitoh) */
1001 u_char msr, delta;
1002 int s;
1003
1004 s = splserial();
1005 msr = sc->sc_msr;
1006 delta = sc->sc_msr_delta;
1007 sc->sc_msr_delta = 0;
1008 splx(s);
1009
1010 if (ISSET(delta, sc->sc_msr_dcd)) {
1011 /*
1012 * Inform the tty layer that carrier detect changed.
1013 */
1014 (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
1015 }
1016
1017 if (ISSET(delta, sc->sc_msr_cts)) {
1018 /* Block or unblock output according to flow control. */
1019 if (ISSET(msr, sc->sc_msr_cts)) {
1020 sc->sc_tx_stopped = 0;
1021 (*tp->t_linesw->l_start)(tp);
1022 } else {
1023 sc->sc_tx_stopped = 1;
1024 }
1025 }
1026
1027 #ifdef SCI_DEBUG
1028 if (sci_debug)
1029 scistatus(sc, "sci_stsoft");
1030 #endif
1031 #endif
1032 }
1033
1034 void
1035 scisoft(void *arg)
1036 {
1037 struct sci_softc *sc = arg;
1038 struct tty *tp;
1039
1040 if (!device_is_active(sc->sc_dev))
1041 return;
1042
1043 tp = sc->sc_tty;
1044
1045 if (sc->sc_rx_ready) {
1046 sc->sc_rx_ready = 0;
1047 sci_rxsoft(sc, tp);
1048 }
1049
1050 #if 0
1051 if (sc->sc_st_check) {
1052 sc->sc_st_check = 0;
1053 sci_stsoft(sc, tp);
1054 }
1055 #endif
1056
1057 if (sc->sc_tx_done) {
1058 sc->sc_tx_done = 0;
1059 sci_txsoft(sc, tp);
1060 }
1061 }
1062
1063 int
1064 sciintr(void *arg)
1065 {
1066 struct sci_softc *sc = arg;
1067 u_char *put, *end;
1068 u_int cc;
1069 u_short ssr;
1070
1071 if (!device_is_active(sc->sc_dev))
1072 return (0);
1073
1074 end = sc->sc_ebuf;
1075 put = sc->sc_rbput;
1076 cc = sc->sc_rbavail;
1077
1078 do {
1079 ssr = SHREG_SCSSR;
1080 if (ISSET(ssr, SCSSR_FER)) {
1081 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_PER | SCSSR_FER);
1082 #if defined(DDB) || defined(KGDB)
1083 #ifdef SH4
1084 if ((SHREG_SCSPTR & SCSPTR_SPB0DT) != 0) {
1085 #else
1086 if ((SHREG_SCSPDR & SCSPDR_SCP0DT) != 0) {
1087 #endif
1088 #ifdef DDB
1089 if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
1090 console_debugger();
1091 }
1092 #endif
1093 #ifdef KGDB
1094 if (ISSET(sc->sc_hwflags, SCI_HW_KGDB)) {
1095 kgdb_connect(1);
1096 }
1097 #endif
1098 }
1099 #endif /* DDB || KGDB */
1100 }
1101 if ((SHREG_SCSSR & SCSSR_RDRF) != 0) {
1102 if (cc > 0) {
1103 put[0] = SHREG_SCRDR;
1104 put[1] = SHREG_SCSSR & 0x00ff;
1105
1106 put += 2;
1107 if (put >= end)
1108 put = sc->sc_rbuf;
1109 cc--;
1110 }
1111
1112 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER |
1113 SCSSR_RDRF);
1114
1115 /*
1116 * Current string of incoming characters ended because
1117 * no more data was available or we ran out of space.
1118 * Schedule a receive event if any data was received.
1119 * If we're out of space, turn off receive interrupts.
1120 */
1121 sc->sc_rbput = put;
1122 sc->sc_rbavail = cc;
1123 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1124 sc->sc_rx_ready = 1;
1125
1126 /*
1127 * See if we are in danger of overflowing a buffer. If
1128 * so, use hardware flow control to ease the pressure.
1129 */
1130 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1131 cc < sc->sc_r_hiwat) {
1132 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1133 #if 0
1134 sci_hwiflow(sc);
1135 #endif
1136 }
1137
1138 /*
1139 * If we're out of space, disable receive interrupts
1140 * until the queue has drained a bit.
1141 */
1142 if (!cc) {
1143 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1144 SHREG_SCSCR &= ~SCSCR_RIE;
1145 }
1146 } else {
1147 if (SHREG_SCSSR & SCSSR_RDRF) {
1148 SHREG_SCSCR &= ~(SCSCR_TIE | SCSCR_RIE);
1149 delay(10);
1150 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
1151 continue;
1152 }
1153 }
1154 } while (SHREG_SCSSR & SCSSR_RDRF);
1155
1156 #if 0
1157 msr = bus_space_read_1(iot, ioh, sci_msr);
1158 delta = msr ^ sc->sc_msr;
1159 sc->sc_msr = msr;
1160 if (ISSET(delta, sc->sc_msr_mask)) {
1161 SET(sc->sc_msr_delta, delta);
1162
1163 /*
1164 * Pulse-per-second clock signal on edge of DCD?
1165 */
1166 if (ISSET(delta, sc->sc_ppsmask)) {
1167 struct timeval tv;
1168 if (ISSET(msr, sc->sc_ppsmask) ==
1169 sc->sc_ppsassert) {
1170 /* XXX nanotime() */
1171 microtime(&tv);
1172 TIMEVAL_TO_TIMESPEC(&tv,
1173 &sc->ppsinfo.assert_timestamp);
1174 if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
1175 timespecadd(&sc->ppsinfo.assert_timestamp,
1176 &sc->ppsparam.assert_offset,
1177 &sc->ppsinfo.assert_timestamp);
1178 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.assert_timestamp);
1179 }
1180
1181 #ifdef PPS_SYNC
1182 if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
1183 hardpps(&tv, tv.tv_usec);
1184 #endif
1185 sc->ppsinfo.assert_sequence++;
1186 sc->ppsinfo.current_mode =
1187 sc->ppsparam.mode;
1188
1189 } else if (ISSET(msr, sc->sc_ppsmask) ==
1190 sc->sc_ppsclear) {
1191 /* XXX nanotime() */
1192 microtime(&tv);
1193 TIMEVAL_TO_TIMESPEC(&tv,
1194 &sc->ppsinfo.clear_timestamp);
1195 if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
1196 timespecadd(&sc->ppsinfo.clear_timestamp,
1197 &sc->ppsparam.clear_offset,
1198 &sc->ppsinfo.clear_timestamp);
1199 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.clear_timestamp);
1200 }
1201
1202 #ifdef PPS_SYNC
1203 if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
1204 hardpps(&tv, tv.tv_usec);
1205 #endif
1206 sc->ppsinfo.clear_sequence++;
1207 sc->ppsinfo.current_mode =
1208 sc->ppsparam.mode;
1209 }
1210 }
1211
1212 /*
1213 * Stop output immediately if we lose the output
1214 * flow control signal or carrier detect.
1215 */
1216 if (ISSET(~msr, sc->sc_msr_mask)) {
1217 sc->sc_tbc = 0;
1218 sc->sc_heldtbc = 0;
1219 #ifdef SCI_DEBUG
1220 if (sci_debug)
1221 scistatus(sc, "sciintr ");
1222 #endif
1223 }
1224
1225 sc->sc_st_check = 1;
1226 }
1227 #endif
1228
1229 /*
1230 * Done handling any receive interrupts. See if data can be
1231 * transmitted as well. Schedule tx done event if no data left
1232 * and tty was marked busy.
1233 */
1234 if ((SHREG_SCSSR & SCSSR_TDRE) != 0) {
1235 /*
1236 * If we've delayed a parameter change, do it now, and restart
1237 * output.
1238 */
1239 if (sc->sc_heldchange) {
1240 sc->sc_heldchange = 0;
1241 sc->sc_tbc = sc->sc_heldtbc;
1242 sc->sc_heldtbc = 0;
1243 }
1244
1245 /* Output the next chunk of the contiguous buffer, if any. */
1246 if (sc->sc_tbc > 0) {
1247 sci_putc(*(sc->sc_tba));
1248 sc->sc_tba++;
1249 sc->sc_tbc--;
1250 } else {
1251 /* Disable transmit completion interrupts if necessary. */
1252 #if 0
1253 if (ISSET(sc->sc_ier, IER_ETXRDY))
1254 #endif
1255 SHREG_SCSCR &= ~SCSCR_TIE;
1256
1257 if (sc->sc_tx_busy) {
1258 sc->sc_tx_busy = 0;
1259 sc->sc_tx_done = 1;
1260 }
1261 }
1262 }
1263
1264 /* Wake up the poller. */
1265 softint_schedule(sc->sc_si);
1266
1267 #ifdef RND_SCI
1268 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1269 #endif
1270
1271 return (1);
1272 }
1273
1274 void
1275 scicnprobe(struct consdev *cp)
1276 {
1277 int maj;
1278
1279 /* locate the major number */
1280 maj = cdevsw_lookup_major(&sci_cdevsw);
1281
1282 /* Initialize required fields. */
1283 cp->cn_dev = makedev(maj, 0);
1284 #ifdef SCICONSOLE
1285 cp->cn_pri = CN_REMOTE;
1286 #else
1287 cp->cn_pri = CN_NORMAL;
1288 #endif
1289 }
1290
1291 void
1292 scicninit(struct consdev *cp)
1293 {
1294
1295 InitializeSci(scicn_speed);
1296 sciisconsole = 1;
1297 }
1298
1299 int
1300 scicngetc(dev_t dev)
1301 {
1302 int c;
1303 int s;
1304
1305 s = splserial();
1306 c = sci_getc();
1307 splx(s);
1308
1309 return (c);
1310 }
1311
1312 void
1313 scicnputc(dev_t dev, int c)
1314 {
1315 int s;
1316
1317 s = splserial();
1318 sci_putc((u_char)c);
1319 splx(s);
1320 }
1321