sci.c revision 1.49.16.1 1 /* $NetBSD: sci.c,v 1.49.16.1 2008/05/18 12:32:43 yamt 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.49.16.1 2008/05/18 12:32:43 yamt 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 struct device 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(struct device *, struct cfdata *, void *);
185 static void sci_attach(struct device *, struct device *, 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(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(struct device *parent, struct cfdata *cfp, void *aux)
362 {
363
364 if (strcmp(cfp->cf_name, "sci") || sci_attached)
365 return 0;
366
367 return 1;
368 }
369
370 static void
371 sci_attach(struct device *parent, struct device *self, void *aux)
372 {
373 struct sci_softc *sc = (struct sci_softc *)self;
374 struct tty *tp;
375
376 sci_attached = 1;
377
378 sc->sc_hwflags = 0; /* XXX */
379 sc->sc_swflags = 0; /* XXX */
380 sc->sc_fifolen = 0; /* XXX */
381
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");
389 }
390
391 callout_init(&sc->sc_diag_ch, 0);
392
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);
401
402 sc->sc_si = softint_establish(SOFTINT_SERIAL, scisoft, sc);
403 SET(sc->sc_hwflags, SCI_HW_DEV_OK);
404
405 tp = ttymalloc();
406 tp->t_oproc = scistart;
407 tp->t_param = sciparam;
408 tp->t_hwiflow = NULL;
409
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;
416 }
417 sc->sc_ebuf = sc->sc_rbuf + (sci_rbuf_size << 1);
418
419 tty_attach(tp);
420 }
421
422 /*
423 * Start or restart transmission.
424 */
425 static void
426 scistart(struct tty *tp)
427 {
428 struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(tp->t_dev)];
429 int s;
430
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;
438
439 /* Grab the first contiguous region of buffer space. */
440 {
441 u_char *tba;
442 int tbc;
443
444 tba = tp->t_outq.c_cf;
445 tbc = ndqb(&tp->t_outq, 0);
446
447 (void)splserial();
448
449 sc->sc_tba = tba;
450 sc->sc_tbc = tbc;
451 }
452
453 SET(tp->t_state, TS_BUSY);
454 sc->sc_tx_busy = 1;
455
456 /* Enable transmit completion interrupts if necessary. */
457 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
458
459 /* Output the first byte of the contiguous buffer. */
460 {
461 if (sc->sc_tbc > 0) {
462 sci_putc(*(sc->sc_tba));
463 sc->sc_tba++;
464 sc->sc_tbc--;
465 }
466 }
467 out:
468 splx(s);
469 return;
470 }
471
472 /*
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.
476 */
477 static int
478 sciparam(struct tty *tp, struct termios *t)
479 {
480 struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(tp->t_dev)];
481 int ospeed = t->c_ospeed;
482 int s;
483
484 if (!device_is_active(&sc->sc_dev))
485 return (EIO);
486
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);
492
493 /*
494 * For the console, always force CLOCAL and !HUPCL, so that the port
495 * is always active.
496 */
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);
501 }
502
503 /*
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.
507 */
508 if (tp->t_ospeed == t->c_ospeed &&
509 tp->t_cflag == t->c_cflag)
510 return (0);
511
512 #if 0
513 /* XXX (msaitoh) */
514 lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
515 #endif
516
517 s = splserial();
518
519 /*
520 * Set the FIFO threshold based on the receive speed.
521 *
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.
527 */
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
539
540 /* And copy to tty. */
541 tp->t_ispeed = 0;
542 tp->t_ospeed = t->c_ospeed;
543 tp->t_cflag = t->c_cflag;
544
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;
550 }
551 #if 0
552 /* XXX (msaitoh) */
553 else
554 sci_loadchannelregs(sc);
555 #endif
556 }
557
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);
565 }
566 } else {
567 sc->sc_r_hiwat = sci_rbuf_hiwat;
568 sc->sc_r_lowat = sci_rbuf_lowat;
569 }
570
571 splx(s);
572
573 #ifdef SCI_DEBUG
574 if (sci_debug)
575 scistatus(sc, "sciparam ");
576 #endif
577
578 if (!ISSET(t->c_cflag, CHWFLOW)) {
579 if (sc->sc_tx_stopped) {
580 sc->sc_tx_stopped = 0;
581 scistart(tp);
582 }
583 }
584
585 return (0);
586 }
587
588 void
589 sci_iflush(struct sci_softc *sc)
590 {
591 unsigned char err_c;
592 volatile unsigned char c;
593
594 if (((err_c = SHREG_SCSSR)
595 & (SCSSR_RDRF | SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) {
596
597 if ((err_c & (SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) {
598 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER);
599 return;
600 }
601
602 c = SHREG_SCRDR;
603
604 SHREG_SCSSR &= ~SCSSR_RDRF;
605 }
606 }
607
608 int
609 sciopen(dev_t dev, int flag, int mode, struct lwp *l)
610 {
611 int unit = SCIUNIT(dev);
612 struct sci_softc *sc;
613 struct tty *tp;
614 int s, s2;
615 int error;
616
617 if (unit >= sci_cd.cd_ndevs)
618 return (ENXIO);
619 sc = sci_cd.cd_devs[unit];
620 if (sc == 0 || !ISSET(sc->sc_hwflags, SCI_HW_DEV_OK) ||
621 sc->sc_rbuf == NULL)
622 return (ENXIO);
623
624 if (!device_is_active(&sc->sc_dev))
625 return (ENXIO);
626
627 #ifdef KGDB
628 /*
629 * If this is the kgdb port, no other use is permitted.
630 */
631 if (ISSET(sc->sc_hwflags, SCI_HW_KGDB))
632 return (EBUSY);
633 #endif
634
635 tp = sc->sc_tty;
636
637 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
638 return (EBUSY);
639
640 s = spltty();
641
642 /*
643 * Do the following iff this is a first open.
644 */
645 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
646 struct termios t;
647
648 tp->t_dev = dev;
649
650 s2 = splserial();
651
652 /* Turn on interrupts. */
653 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
654
655 splx(s2);
656
657 /*
658 * Initialize the termios status to the defaults. Add in the
659 * sticky bits from TIOCSFLAGS.
660 */
661 t.c_ispeed = 0;
662 if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
663 t.c_ospeed = scicn_speed;
664 t.c_cflag = sciconscflag;
665 } else {
666 t.c_ospeed = TTYDEF_SPEED;
667 t.c_cflag = TTYDEF_CFLAG;
668 }
669 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
670 SET(t.c_cflag, CLOCAL);
671 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
672 SET(t.c_cflag, CRTSCTS);
673 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
674 SET(t.c_cflag, MDMBUF);
675 /* Make sure sciparam() will do something. */
676 tp->t_ospeed = 0;
677 (void) sciparam(tp, &t);
678 tp->t_iflag = TTYDEF_IFLAG;
679 tp->t_oflag = TTYDEF_OFLAG;
680 tp->t_lflag = TTYDEF_LFLAG;
681 ttychars(tp);
682 ttsetwater(tp);
683
684 s2 = splserial();
685
686 /* Clear the input ring, and unblock. */
687 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
688 sc->sc_rbavail = sci_rbuf_size;
689 sci_iflush(sc);
690 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
691 #if 0
692 /* XXX (msaitoh) */
693 sci_hwiflow(sc);
694 #endif
695
696 #ifdef SCI_DEBUG
697 if (sci_debug)
698 scistatus(sc, "sciopen ");
699 #endif
700
701 splx(s2);
702 }
703
704 splx(s);
705
706 error = ttyopen(tp, SCIDIALOUT(dev), ISSET(flag, O_NONBLOCK));
707 if (error)
708 goto bad;
709
710 error = (*tp->t_linesw->l_open)(dev, tp);
711 if (error)
712 goto bad;
713
714 return (0);
715
716 bad:
717
718 return (error);
719 }
720
721 int
722 sciclose(dev_t dev, int flag, int mode, struct lwp *l)
723 {
724 struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
725 struct tty *tp = sc->sc_tty;
726
727 /* XXX This is for cons.c. */
728 if (!ISSET(tp->t_state, TS_ISOPEN))
729 return (0);
730
731 (*tp->t_linesw->l_close)(tp, flag);
732 ttyclose(tp);
733
734 if (!device_is_active(&sc->sc_dev))
735 return (0);
736
737 return (0);
738 }
739
740 int
741 sciread(dev_t dev, struct uio *uio, int flag)
742 {
743 struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
744 struct tty *tp = sc->sc_tty;
745
746 return ((*tp->t_linesw->l_read)(tp, uio, flag));
747 }
748
749 int
750 sciwrite(dev_t dev, struct uio *uio, int flag)
751 {
752 struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
753 struct tty *tp = sc->sc_tty;
754
755 return ((*tp->t_linesw->l_write)(tp, uio, flag));
756 }
757
758 int
759 scipoll(dev_t dev, int events, struct lwp *l)
760 {
761 struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
762 struct tty *tp = sc->sc_tty;
763
764 return ((*tp->t_linesw->l_poll)(tp, events, l));
765 }
766
767 struct tty *
768 scitty(dev_t dev)
769 {
770 struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
771 struct tty *tp = sc->sc_tty;
772
773 return (tp);
774 }
775
776 int
777 sciioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
778 {
779 struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
780 struct tty *tp = sc->sc_tty;
781 int error;
782 int s;
783
784 if (!device_is_active(&sc->sc_dev))
785 return (EIO);
786
787 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
788 if (error != EPASSTHROUGH)
789 return (error);
790
791 error = ttioctl(tp, cmd, data, flag, l);
792 if (error != EPASSTHROUGH)
793 return (error);
794
795 error = 0;
796
797 s = splserial();
798
799 switch (cmd) {
800 case TIOCSBRK:
801 sci_break(sc, 1);
802 break;
803
804 case TIOCCBRK:
805 sci_break(sc, 0);
806 break;
807
808 case TIOCGFLAGS:
809 *(int *)data = sc->sc_swflags;
810 break;
811
812 case TIOCSFLAGS:
813 error = kauth_authorize_device_tty(l->l_cred,
814 KAUTH_DEVICE_TTY_PRIVSET, tp);
815 if (error)
816 break;
817 sc->sc_swflags = *(int *)data;
818 break;
819
820 default:
821 error = EPASSTHROUGH;
822 break;
823 }
824
825 splx(s);
826
827 return (error);
828 }
829
830 integrate void
831 sci_schedrx(struct sci_softc *sc)
832 {
833
834 sc->sc_rx_ready = 1;
835
836 /* Wake up the poller. */
837 softint_schedule(sc->sc_si);
838 }
839
840 void
841 sci_break(struct sci_softc *sc, int onoff)
842 {
843
844 if (onoff)
845 SHREG_SCSSR &= ~SCSSR_TDRE;
846 else
847 SHREG_SCSSR |= SCSSR_TDRE;
848
849 #if 0 /* XXX */
850 if (!sc->sc_heldchange) {
851 if (sc->sc_tx_busy) {
852 sc->sc_heldtbc = sc->sc_tbc;
853 sc->sc_tbc = 0;
854 sc->sc_heldchange = 1;
855 } else
856 sci_loadchannelregs(sc);
857 }
858 #endif
859 }
860
861 /*
862 * Stop output, e.g., for ^S or output flush.
863 */
864 void
865 scistop(struct tty *tp, int flag)
866 {
867 struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(tp->t_dev)];
868 int s;
869
870 s = splserial();
871 if (ISSET(tp->t_state, TS_BUSY)) {
872 /* Stop transmitting at the next chunk. */
873 sc->sc_tbc = 0;
874 sc->sc_heldtbc = 0;
875 if (!ISSET(tp->t_state, TS_TTSTOP))
876 SET(tp->t_state, TS_FLUSH);
877 }
878 splx(s);
879 }
880
881 void
882 scidiag(void *arg)
883 {
884 struct sci_softc *sc = arg;
885 int overflows, floods;
886 int s;
887
888 s = splserial();
889 overflows = sc->sc_overflows;
890 sc->sc_overflows = 0;
891 floods = sc->sc_floods;
892 sc->sc_floods = 0;
893 sc->sc_errors = 0;
894 splx(s);
895
896 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
897 sc->sc_dev.dv_xname,
898 overflows, overflows == 1 ? "" : "s",
899 floods, floods == 1 ? "" : "s");
900 }
901
902 integrate void
903 sci_rxsoft(struct sci_softc *sc, struct tty *tp)
904 {
905 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
906 u_char *get, *end;
907 u_int cc, scc;
908 u_char ssr;
909 int code;
910 int s;
911
912 end = sc->sc_ebuf;
913 get = sc->sc_rbget;
914 scc = cc = sci_rbuf_size - sc->sc_rbavail;
915
916 if (cc == sci_rbuf_size) {
917 sc->sc_floods++;
918 if (sc->sc_errors++ == 0)
919 callout_reset(&sc->sc_diag_ch, 60 * hz, scidiag, sc);
920 }
921
922 while (cc) {
923 code = get[0];
924 ssr = get[1];
925 if (ISSET(ssr, SCSSR_FER | SCSSR_PER)) {
926 if (ISSET(ssr, SCSSR_FER))
927 SET(code, TTY_FE);
928 if (ISSET(ssr, SCSSR_PER))
929 SET(code, TTY_PE);
930 }
931 if ((*rint)(code, tp) == -1) {
932 /*
933 * The line discipline's buffer is out of space.
934 */
935 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
936 /*
937 * We're either not using flow control, or the
938 * line discipline didn't tell us to block for
939 * some reason. Either way, we have no way to
940 * know when there's more space available, so
941 * just drop the rest of the data.
942 */
943 get += cc << 1;
944 if (get >= end)
945 get -= sci_rbuf_size << 1;
946 cc = 0;
947 } else {
948 /*
949 * Don't schedule any more receive processing
950 * until the line discipline tells us there's
951 * space available (through scihwiflow()).
952 * Leave the rest of the data in the input
953 * buffer.
954 */
955 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
956 }
957 break;
958 }
959 get += 2;
960 if (get >= end)
961 get = sc->sc_rbuf;
962 cc--;
963 }
964
965 if (cc != scc) {
966 sc->sc_rbget = get;
967 s = splserial();
968 cc = sc->sc_rbavail += scc - cc;
969 /* Buffers should be ok again, release possible block. */
970 if (cc >= sc->sc_r_lowat) {
971 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
972 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
973 SHREG_SCSCR |= SCSCR_RIE;
974 }
975 #if 0
976 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
977 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
978 sci_hwiflow(sc);
979 }
980 #endif
981 }
982 splx(s);
983 }
984 }
985
986 integrate void
987 sci_txsoft(sc, tp)
988 struct sci_softc *sc;
989 struct tty *tp;
990 {
991
992 CLR(tp->t_state, TS_BUSY);
993 if (ISSET(tp->t_state, TS_FLUSH))
994 CLR(tp->t_state, TS_FLUSH);
995 else
996 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
997 (*tp->t_linesw->l_start)(tp);
998 }
999
1000 integrate void
1001 sci_stsoft(struct sci_softc *sc, struct tty *tp)
1002 {
1003 #if 0
1004 /* XXX (msaitoh) */
1005 u_char msr, delta;
1006 int s;
1007
1008 s = splserial();
1009 msr = sc->sc_msr;
1010 delta = sc->sc_msr_delta;
1011 sc->sc_msr_delta = 0;
1012 splx(s);
1013
1014 if (ISSET(delta, sc->sc_msr_dcd)) {
1015 /*
1016 * Inform the tty layer that carrier detect changed.
1017 */
1018 (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
1019 }
1020
1021 if (ISSET(delta, sc->sc_msr_cts)) {
1022 /* Block or unblock output according to flow control. */
1023 if (ISSET(msr, sc->sc_msr_cts)) {
1024 sc->sc_tx_stopped = 0;
1025 (*tp->t_linesw->l_start)(tp);
1026 } else {
1027 sc->sc_tx_stopped = 1;
1028 }
1029 }
1030
1031 #ifdef SCI_DEBUG
1032 if (sci_debug)
1033 scistatus(sc, "sci_stsoft");
1034 #endif
1035 #endif
1036 }
1037
1038 void
1039 scisoft(void *arg)
1040 {
1041 struct sci_softc *sc = arg;
1042 struct tty *tp;
1043
1044 if (!device_is_active(&sc->sc_dev))
1045 return;
1046
1047 tp = sc->sc_tty;
1048
1049 if (sc->sc_rx_ready) {
1050 sc->sc_rx_ready = 0;
1051 sci_rxsoft(sc, tp);
1052 }
1053
1054 #if 0
1055 if (sc->sc_st_check) {
1056 sc->sc_st_check = 0;
1057 sci_stsoft(sc, tp);
1058 }
1059 #endif
1060
1061 if (sc->sc_tx_done) {
1062 sc->sc_tx_done = 0;
1063 sci_txsoft(sc, tp);
1064 }
1065 }
1066
1067 int
1068 sciintr(void *arg)
1069 {
1070 struct sci_softc *sc = arg;
1071 u_char *put, *end;
1072 u_int cc;
1073 u_short ssr;
1074
1075 if (!device_is_active(&sc->sc_dev))
1076 return (0);
1077
1078 end = sc->sc_ebuf;
1079 put = sc->sc_rbput;
1080 cc = sc->sc_rbavail;
1081
1082 do {
1083 ssr = SHREG_SCSSR;
1084 if (ISSET(ssr, SCSSR_FER)) {
1085 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_PER | SCSSR_FER);
1086 #if defined(DDB) || defined(KGDB)
1087 #ifdef SH4
1088 if ((SHREG_SCSPTR & SCSPTR_SPB0DT) != 0) {
1089 #else
1090 if ((SHREG_SCSPDR & SCSPDR_SCP0DT) != 0) {
1091 #endif
1092 #ifdef DDB
1093 if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
1094 console_debugger();
1095 }
1096 #endif
1097 #ifdef KGDB
1098 if (ISSET(sc->sc_hwflags, SCI_HW_KGDB)) {
1099 kgdb_connect(1);
1100 }
1101 #endif
1102 }
1103 #endif /* DDB || KGDB */
1104 }
1105 if ((SHREG_SCSSR & SCSSR_RDRF) != 0) {
1106 if (cc > 0) {
1107 put[0] = SHREG_SCRDR;
1108 put[1] = SHREG_SCSSR & 0x00ff;
1109
1110 put += 2;
1111 if (put >= end)
1112 put = sc->sc_rbuf;
1113 cc--;
1114 }
1115
1116 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER |
1117 SCSSR_RDRF);
1118
1119 /*
1120 * Current string of incoming characters ended because
1121 * no more data was available or we ran out of space.
1122 * Schedule a receive event if any data was received.
1123 * If we're out of space, turn off receive interrupts.
1124 */
1125 sc->sc_rbput = put;
1126 sc->sc_rbavail = cc;
1127 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1128 sc->sc_rx_ready = 1;
1129
1130 /*
1131 * See if we are in danger of overflowing a buffer. If
1132 * so, use hardware flow control to ease the pressure.
1133 */
1134 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1135 cc < sc->sc_r_hiwat) {
1136 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1137 #if 0
1138 sci_hwiflow(sc);
1139 #endif
1140 }
1141
1142 /*
1143 * If we're out of space, disable receive interrupts
1144 * until the queue has drained a bit.
1145 */
1146 if (!cc) {
1147 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1148 SHREG_SCSCR &= ~SCSCR_RIE;
1149 }
1150 } else {
1151 if (SHREG_SCSSR & SCSSR_RDRF) {
1152 SHREG_SCSCR &= ~(SCSCR_TIE | SCSCR_RIE);
1153 delay(10);
1154 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
1155 continue;
1156 }
1157 }
1158 } while (SHREG_SCSSR & SCSSR_RDRF);
1159
1160 #if 0
1161 msr = bus_space_read_1(iot, ioh, sci_msr);
1162 delta = msr ^ sc->sc_msr;
1163 sc->sc_msr = msr;
1164 if (ISSET(delta, sc->sc_msr_mask)) {
1165 SET(sc->sc_msr_delta, delta);
1166
1167 /*
1168 * Pulse-per-second clock signal on edge of DCD?
1169 */
1170 if (ISSET(delta, sc->sc_ppsmask)) {
1171 struct timeval tv;
1172 if (ISSET(msr, sc->sc_ppsmask) ==
1173 sc->sc_ppsassert) {
1174 /* XXX nanotime() */
1175 microtime(&tv);
1176 TIMEVAL_TO_TIMESPEC(&tv,
1177 &sc->ppsinfo.assert_timestamp);
1178 if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
1179 timespecadd(&sc->ppsinfo.assert_timestamp,
1180 &sc->ppsparam.assert_offset,
1181 &sc->ppsinfo.assert_timestamp);
1182 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.assert_timestamp);
1183 }
1184
1185 #ifdef PPS_SYNC
1186 if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
1187 hardpps(&tv, tv.tv_usec);
1188 #endif
1189 sc->ppsinfo.assert_sequence++;
1190 sc->ppsinfo.current_mode =
1191 sc->ppsparam.mode;
1192
1193 } else if (ISSET(msr, sc->sc_ppsmask) ==
1194 sc->sc_ppsclear) {
1195 /* XXX nanotime() */
1196 microtime(&tv);
1197 TIMEVAL_TO_TIMESPEC(&tv,
1198 &sc->ppsinfo.clear_timestamp);
1199 if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
1200 timespecadd(&sc->ppsinfo.clear_timestamp,
1201 &sc->ppsparam.clear_offset,
1202 &sc->ppsinfo.clear_timestamp);
1203 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.clear_timestamp);
1204 }
1205
1206 #ifdef PPS_SYNC
1207 if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
1208 hardpps(&tv, tv.tv_usec);
1209 #endif
1210 sc->ppsinfo.clear_sequence++;
1211 sc->ppsinfo.current_mode =
1212 sc->ppsparam.mode;
1213 }
1214 }
1215
1216 /*
1217 * Stop output immediately if we lose the output
1218 * flow control signal or carrier detect.
1219 */
1220 if (ISSET(~msr, sc->sc_msr_mask)) {
1221 sc->sc_tbc = 0;
1222 sc->sc_heldtbc = 0;
1223 #ifdef SCI_DEBUG
1224 if (sci_debug)
1225 scistatus(sc, "sciintr ");
1226 #endif
1227 }
1228
1229 sc->sc_st_check = 1;
1230 }
1231 #endif
1232
1233 /*
1234 * Done handling any receive interrupts. See if data can be
1235 * transmitted as well. Schedule tx done event if no data left
1236 * and tty was marked busy.
1237 */
1238 if ((SHREG_SCSSR & SCSSR_TDRE) != 0) {
1239 /*
1240 * If we've delayed a parameter change, do it now, and restart
1241 * output.
1242 */
1243 if (sc->sc_heldchange) {
1244 sc->sc_heldchange = 0;
1245 sc->sc_tbc = sc->sc_heldtbc;
1246 sc->sc_heldtbc = 0;
1247 }
1248
1249 /* Output the next chunk of the contiguous buffer, if any. */
1250 if (sc->sc_tbc > 0) {
1251 sci_putc(*(sc->sc_tba));
1252 sc->sc_tba++;
1253 sc->sc_tbc--;
1254 } else {
1255 /* Disable transmit completion interrupts if necessary. */
1256 #if 0
1257 if (ISSET(sc->sc_ier, IER_ETXRDY))
1258 #endif
1259 SHREG_SCSCR &= ~SCSCR_TIE;
1260
1261 if (sc->sc_tx_busy) {
1262 sc->sc_tx_busy = 0;
1263 sc->sc_tx_done = 1;
1264 }
1265 }
1266 }
1267
1268 /* Wake up the poller. */
1269 softint_schedule(sc->sc_si);
1270
1271 #if NRND > 0 && defined(RND_SCI)
1272 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1273 #endif
1274
1275 return (1);
1276 }
1277
1278 void
1279 scicnprobe(cp)
1280 struct consdev *cp;
1281 {
1282 int maj;
1283
1284 /* locate the major number */
1285 maj = cdevsw_lookup_major(&sci_cdevsw);
1286
1287 /* Initialize required fields. */
1288 cp->cn_dev = makedev(maj, 0);
1289 #ifdef SCICONSOLE
1290 cp->cn_pri = CN_REMOTE;
1291 #else
1292 cp->cn_pri = CN_NORMAL;
1293 #endif
1294 }
1295
1296 void
1297 scicninit(struct consdev *cp)
1298 {
1299
1300 InitializeSci(scicn_speed);
1301 sciisconsole = 1;
1302 }
1303
1304 int
1305 scicngetc(dev_t dev)
1306 {
1307 int c;
1308 int s;
1309
1310 s = splserial();
1311 c = sci_getc();
1312 splx(s);
1313
1314 return (c);
1315 }
1316
1317 void
1318 scicnputc(dev_t dev, int c)
1319 {
1320 int s;
1321
1322 s = splserial();
1323 sci_putc((u_char)c);
1324 splx(s);
1325 }
1326