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