1 /* $NetBSD: sci.c,v 1.65 2023/09/17 14:22:28 andvar 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.65 2023/09/17 14:22:28 andvar 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/kmem.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 #ifdef SCI_DEBUG
123 int sci_debug = 1;
124 #define DPRINTF(x) if (sci_debug) printf x
125 #else
126 #define DPRINTF(x)
127 #endif
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 device_t 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(device_t, cfdata_t, void *);
192 static void sci_attach(device_t, device_t, 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(x) TTUNIT(x)
207 #define SCIDIALOUT(x) TTDIALOUT(x)
208
209 /* Hardware flag masks */
210 #define SCI_HW_NOIEN 0x01
211 #define SCI_HW_FIFO 0x02
212 #define SCI_HW_FLOW 0x08
213 #define SCI_HW_DEV_OK 0x20
214 #define SCI_HW_CONSOLE 0x40
215 #define SCI_HW_KGDB 0x80
216
217 /* Buffer size for character buffer */
218 #define SCI_RING_SIZE 2048
219
220 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
221 u_int sci_rbuf_hiwat = (SCI_RING_SIZE * 1) / 4;
222 u_int sci_rbuf_lowat = (SCI_RING_SIZE * 3) / 4;
223
224 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
225 int sciconscflag = CONMODE;
226 int sciisconsole = 0;
227
228 #ifdef SCICN_SPEED
229 int scicn_speed = SCICN_SPEED;
230 #else
231 int scicn_speed = 9600;
232 #endif
233
234 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
235
236 u_int sci_rbuf_size = SCI_RING_SIZE;
237
238 CFATTACH_DECL_NEW(sci, sizeof(struct sci_softc),
239 sci_match, sci_attach, NULL, NULL);
240
241 extern struct cfdriver sci_cd;
242
243 static int sci_attached;
244
245 dev_type_open(sciopen);
246 dev_type_close(sciclose);
247 dev_type_read(sciread);
248 dev_type_write(sciwrite);
249 dev_type_ioctl(sciioctl);
250 dev_type_stop(scistop);
251 dev_type_tty(scitty);
252 dev_type_poll(scipoll);
253
254 const struct cdevsw sci_cdevsw = {
255 .d_open = sciopen,
256 .d_close = sciclose,
257 .d_read = sciread,
258 .d_write = sciwrite,
259 .d_ioctl = sciioctl,
260 .d_stop = scistop,
261 .d_tty = scitty,
262 .d_poll = scipoll,
263 .d_mmap = nommap,
264 .d_kqfilter = ttykqfilter,
265 .d_discard = nodiscard,
266 .d_flag = D_TTY
267 };
268
269 void InitializeSci (unsigned int);
270
271 /*
272 * following functions are debugging prupose only
273 */
274 #define CR 0x0D
275 #define I2C_ADRS (*(volatile unsigned int *)0xa8000000)
276 #define USART_ON (unsigned int)~0x08
277
278 void sci_putc(unsigned char);
279 unsigned char sci_getc(void);
280 int SciErrCheck(void);
281
282 /*
283 * InitializeSci
284 * : unsigned int bps;
285 * : SCI(Serial Communication Interface)
286 */
287
288 void
InitializeSci(unsigned int bps)289 InitializeSci(unsigned int bps)
290 {
291
292 /* Initialize SCR */
293 SHREG_SCSCR = 0x00;
294
295 /* Serial Mode Register */
296 SHREG_SCSMR = 0x00; /* Async,8bit,NonParity,Even,1Stop,NoMulti */
297
298 /* Bit Rate Register */
299 SHREG_SCBRR = divrnd(sh_clock_get_pclock(), 32 * bps) - 1;
300
301 /*
302 * wait 1mSec, because Send/Recv must begin 1 bit period after
303 * BRR is set.
304 */
305 delay(1000);
306
307 /* Send permission, Receive permission ON */
308 SHREG_SCSCR = SCSCR_TE | SCSCR_RE;
309
310 /* Serial Status Register */
311 SHREG_SCSSR &= SCSSR_TDRE; /* Clear Status */
312
313 #if 0
314 I2C_ADRS &= ~0x08; /* enable RS-232C */
315 #endif
316 }
317
318
319 /*
320 * sci_putc
321 * : unsigned char c;
322 */
323 void
sci_putc(unsigned char c)324 sci_putc(unsigned char c)
325 {
326
327 /* wait for ready */
328 while ((SHREG_SCSSR & SCSSR_TDRE) == 0)
329 ;
330
331 /* write send data to send register */
332 SHREG_SCTDR = c;
333
334 /* clear ready flag */
335 SHREG_SCSSR &= ~SCSSR_TDRE;
336 }
337
338 /*
339 * : SciErrCheck
340 * 0x20 = over run
341 * 0x10 = frame error
342 * 0x80 = parity error
343 */
344 int
SciErrCheck(void)345 SciErrCheck(void)
346 {
347
348 return(SHREG_SCSSR & (SCSSR_ORER | SCSSR_FER | SCSSR_PER));
349 }
350
351 /*
352 * sci_getc
353 */
354 unsigned char
sci_getc(void)355 sci_getc(void)
356 {
357 unsigned char c, err_c;
358
359 while (((err_c = SHREG_SCSSR)
360 & (SCSSR_RDRF | SCSSR_ORER | SCSSR_FER | SCSSR_PER)) == 0)
361 ;
362 if ((err_c & (SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) {
363 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER);
364 return(err_c |= 0x80);
365 }
366
367 c = SHREG_SCRDR;
368
369 SHREG_SCSSR &= ~SCSSR_RDRF;
370
371 return(c);
372 }
373
374 static int
sci_match(device_t parent,cfdata_t cf,void * aux)375 sci_match(device_t parent, cfdata_t cf, void *aux)
376 {
377
378 if (strcmp(cf->cf_name, "sci") || sci_attached)
379 return 0;
380
381 return 1;
382 }
383
384 static void
sci_attach(device_t parent,device_t self,void * aux)385 sci_attach(device_t parent, device_t self, void *aux)
386 {
387 struct sci_softc *sc = device_private(self);
388 struct tty *tp;
389
390 sci_attached = 1;
391
392 sc->sc_dev = self;
393 sc->sc_hwflags = 0; /* XXX */
394 sc->sc_swflags = 0; /* XXX */
395 sc->sc_fifolen = 0; /* XXX */
396
397 if (sciisconsole) {
398 SET(sc->sc_hwflags, SCI_HW_CONSOLE);
399 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
400 printf("\n%s: console\n", device_xname(self));
401 } else {
402 InitializeSci(9600);
403 printf("\n");
404 }
405
406 callout_init(&sc->sc_diag_ch, 0);
407
408 intc_intr_establish(SH_INTEVT_SCI_ERI, IST_LEVEL, IPL_SERIAL, sciintr,
409 sc);
410 intc_intr_establish(SH_INTEVT_SCI_RXI, IST_LEVEL, IPL_SERIAL, sciintr,
411 sc);
412 intc_intr_establish(SH_INTEVT_SCI_TXI, IST_LEVEL, IPL_SERIAL, sciintr,
413 sc);
414 intc_intr_establish(SH_INTEVT_SCI_TEI, IST_LEVEL, IPL_SERIAL, sciintr,
415 sc);
416
417 sc->sc_si = softint_establish(SOFTINT_SERIAL, scisoft, sc);
418 SET(sc->sc_hwflags, SCI_HW_DEV_OK);
419
420 tp = tty_alloc();
421 tp->t_oproc = scistart;
422 tp->t_param = sciparam;
423 tp->t_hwiflow = NULL;
424
425 sc->sc_tty = tp;
426 sc->sc_rbuf = kmem_alloc(sci_rbuf_size << 1, KM_SLEEP);
427 sc->sc_ebuf = sc->sc_rbuf + (sci_rbuf_size << 1);
428
429 tty_attach(tp);
430 }
431
432 /*
433 * Start or restart transmission.
434 */
435 static void
scistart(struct tty * tp)436 scistart(struct tty *tp)
437 {
438 struct sci_softc *sc = device_lookup_private(&sci_cd,SCIUNIT(tp->t_dev));
439 int s;
440
441 s = spltty();
442 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
443 goto out;
444 if (sc->sc_tx_stopped)
445 goto out;
446 if (!ttypull(tp))
447 goto out;
448
449 /* Grab the first contiguous region of buffer space. */
450 {
451 u_char *tba;
452 int tbc;
453
454 tba = tp->t_outq.c_cf;
455 tbc = ndqb(&tp->t_outq, 0);
456
457 (void)splserial();
458
459 sc->sc_tba = tba;
460 sc->sc_tbc = tbc;
461 }
462
463 SET(tp->t_state, TS_BUSY);
464 sc->sc_tx_busy = 1;
465
466 /* Enable transmit completion interrupts if necessary. */
467 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
468
469 /* Output the first byte of the contiguous buffer. */
470 {
471 if (sc->sc_tbc > 0) {
472 sci_putc(*(sc->sc_tba));
473 sc->sc_tba++;
474 sc->sc_tbc--;
475 }
476 }
477 out:
478 splx(s);
479 return;
480 }
481
482 /*
483 * Set SCI tty parameters from termios.
484 * XXX - Should just copy the whole termios after
485 * making sure all the changes could be done.
486 */
487 static int
sciparam(struct tty * tp,struct termios * t)488 sciparam(struct tty *tp, struct termios *t)
489 {
490 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(tp->t_dev));
491 int ospeed = t->c_ospeed;
492 int s;
493
494 if (!device_is_active(sc->sc_dev))
495 return (EIO);
496
497 /* Check requested parameters. */
498 if (ospeed < 0)
499 return (EINVAL);
500 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
501 return (EINVAL);
502
503 /*
504 * For the console, always force CLOCAL and !HUPCL, so that the port
505 * is always active.
506 */
507 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
508 ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
509 SET(t->c_cflag, CLOCAL);
510 CLR(t->c_cflag, HUPCL);
511 }
512
513 /*
514 * If there were no changes, don't do anything. This avoids dropping
515 * input and improves performance when all we did was frob things like
516 * VMIN and VTIME.
517 */
518 if (tp->t_ospeed == t->c_ospeed &&
519 tp->t_cflag == t->c_cflag)
520 return (0);
521
522 #if 0
523 /* XXX (msaitoh) */
524 lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
525 #endif
526
527 s = splserial();
528
529 /*
530 * Set the FIFO threshold based on the receive speed.
531 *
532 * * If it's a low speed, it's probably a mouse or some other
533 * interactive device, so set the threshold low.
534 * * If it's a high speed, trim the trigger level down to prevent
535 * overflows.
536 * * Otherwise set it a bit higher.
537 */
538 #if 0
539 /* XXX (msaitoh) */
540 if (ISSET(sc->sc_hwflags, SCI_HW_HAYESP))
541 sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
542 else if (ISSET(sc->sc_hwflags, SCI_HW_FIFO))
543 sc->sc_fifo = FIFO_ENABLE |
544 (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
545 t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
546 else
547 sc->sc_fifo = 0;
548 #endif
549
550 /* And copy to tty. */
551 tp->t_ispeed = 0;
552 tp->t_ospeed = t->c_ospeed;
553 tp->t_cflag = t->c_cflag;
554
555 if (!sc->sc_heldchange) {
556 if (sc->sc_tx_busy) {
557 sc->sc_heldtbc = sc->sc_tbc;
558 sc->sc_tbc = 0;
559 sc->sc_heldchange = 1;
560 }
561 #if 0
562 /* XXX (msaitoh) */
563 else
564 sci_loadchannelregs(sc);
565 #endif
566 }
567
568 if (!ISSET(t->c_cflag, CHWFLOW)) {
569 /* Disable the high water mark. */
570 sc->sc_r_hiwat = 0;
571 sc->sc_r_lowat = 0;
572 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
573 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
574 sci_schedrx(sc);
575 }
576 } else {
577 sc->sc_r_hiwat = sci_rbuf_hiwat;
578 sc->sc_r_lowat = sci_rbuf_lowat;
579 }
580
581 splx(s);
582
583 DPRINTF(("%s: sciparam\n", device_xname(sc->sc_dev)));
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
sci_iflush(struct sci_softc * sc)596 sci_iflush(struct sci_softc *sc)
597 {
598 unsigned char err_c;
599
600 if (((err_c = SHREG_SCSSR)
601 & (SCSSR_RDRF | SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) {
602
603 if ((err_c & (SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) {
604 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER);
605 return;
606 }
607
608 (void)SHREG_SCRDR;
609
610 SHREG_SCSSR &= ~SCSSR_RDRF;
611 }
612 }
613
614 int
sciopen(dev_t dev,int flag,int mode,struct lwp * l)615 sciopen(dev_t dev, int flag, int mode, struct lwp *l)
616 {
617 struct sci_softc *sc;
618 struct tty *tp;
619 int s, s2;
620 int error;
621
622 sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
623 if (sc == 0 || !ISSET(sc->sc_hwflags, SCI_HW_DEV_OK) ||
624 sc->sc_rbuf == NULL)
625 return (ENXIO);
626
627 if (!device_is_active(sc->sc_dev))
628 return (ENXIO);
629
630 #ifdef KGDB
631 /*
632 * If this is the kgdb port, no other use is permitted.
633 */
634 if (ISSET(sc->sc_hwflags, SCI_HW_KGDB))
635 return (EBUSY);
636 #endif
637
638 tp = sc->sc_tty;
639
640 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
641 return (EBUSY);
642
643 s = spltty();
644
645 /*
646 * Do the following iff this is a first open.
647 */
648 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
649 struct termios t;
650
651 tp->t_dev = dev;
652
653 s2 = splserial();
654
655 /* Turn on interrupts. */
656 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
657
658 splx(s2);
659
660 /*
661 * Initialize the termios status to the defaults. Add in the
662 * sticky bits from TIOCSFLAGS.
663 */
664 t.c_ispeed = 0;
665 if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
666 t.c_ospeed = scicn_speed;
667 t.c_cflag = sciconscflag;
668 } else {
669 t.c_ospeed = TTYDEF_SPEED;
670 t.c_cflag = TTYDEF_CFLAG;
671 }
672 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
673 SET(t.c_cflag, CLOCAL);
674 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
675 SET(t.c_cflag, CRTSCTS);
676 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
677 SET(t.c_cflag, MDMBUF);
678 /* Make sure sciparam() will do something. */
679 tp->t_ospeed = 0;
680 (void) sciparam(tp, &t);
681 tp->t_iflag = TTYDEF_IFLAG;
682 tp->t_oflag = TTYDEF_OFLAG;
683 tp->t_lflag = TTYDEF_LFLAG;
684 ttychars(tp);
685 ttsetwater(tp);
686
687 s2 = splserial();
688
689 /* Clear the input ring, and unblock. */
690 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
691 sc->sc_rbavail = sci_rbuf_size;
692 sci_iflush(sc);
693 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
694 #if 0
695 /* XXX (msaitoh) */
696 sci_hwiflow(sc);
697 #endif
698
699 DPRINTF(("%s: sciopen\n", device_xname(sc->sc_dev)));
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
sciclose(dev_t dev,int flag,int mode,struct lwp * l)722 sciclose(dev_t dev, int flag, int mode, struct lwp *l)
723 {
724 struct sci_softc *sc = device_lookup_private(&sci_cd, 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
sciread(dev_t dev,struct uio * uio,int flag)741 sciread(dev_t dev, struct uio *uio, int flag)
742 {
743 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
744 struct tty *tp = sc->sc_tty;
745
746 return ((*tp->t_linesw->l_read)(tp, uio, flag));
747 }
748
749 int
sciwrite(dev_t dev,struct uio * uio,int flag)750 sciwrite(dev_t dev, struct uio *uio, int flag)
751 {
752 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
753 struct tty *tp = sc->sc_tty;
754
755 return ((*tp->t_linesw->l_write)(tp, uio, flag));
756 }
757
758 int
scipoll(dev_t dev,int events,struct lwp * l)759 scipoll(dev_t dev, int events, struct lwp *l)
760 {
761 struct sci_softc *sc = device_lookup_private(&sci_cd, 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 *
scitty(dev_t dev)768 scitty(dev_t dev)
769 {
770 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev));
771 struct tty *tp = sc->sc_tty;
772
773 return (tp);
774 }
775
776 int
sciioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)777 sciioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
778 {
779 struct sci_softc *sc = device_lookup_private(&sci_cd, 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
sci_schedrx(struct sci_softc * sc)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
sci_break(struct sci_softc * sc,int onoff)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
scistop(struct tty * tp,int flag)865 scistop(struct tty *tp, int flag)
866 {
867 struct sci_softc *sc = device_lookup_private(&sci_cd, 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
scidiag(void * arg)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 device_xname(sc->sc_dev),
898 overflows, overflows == 1 ? "" : "s",
899 floods, floods == 1 ? "" : "s");
900 }
901
902 integrate void
sci_rxsoft(struct sci_softc * sc,struct tty * tp)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
sci_txsoft(struct sci_softc * sc,struct tty * tp)987 sci_txsoft(struct sci_softc *sc, struct tty *tp)
988 {
989
990 CLR(tp->t_state, TS_BUSY);
991 if (ISSET(tp->t_state, TS_FLUSH))
992 CLR(tp->t_state, TS_FLUSH);
993 else
994 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
995 (*tp->t_linesw->l_start)(tp);
996 }
997
998 integrate void
sci_stsoft(struct sci_softc * sc,struct tty * tp)999 sci_stsoft(struct sci_softc *sc, struct tty *tp)
1000 {
1001 #if 0
1002 /* XXX (msaitoh) */
1003 u_char msr, delta;
1004 int s;
1005
1006 s = splserial();
1007 msr = sc->sc_msr;
1008 delta = sc->sc_msr_delta;
1009 sc->sc_msr_delta = 0;
1010 splx(s);
1011
1012 if (ISSET(delta, sc->sc_msr_dcd)) {
1013 /*
1014 * Inform the tty layer that carrier detect changed.
1015 */
1016 (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
1017 }
1018
1019 if (ISSET(delta, sc->sc_msr_cts)) {
1020 /* Block or unblock output according to flow control. */
1021 if (ISSET(msr, sc->sc_msr_cts)) {
1022 sc->sc_tx_stopped = 0;
1023 (*tp->t_linesw->l_start)(tp);
1024 } else {
1025 sc->sc_tx_stopped = 1;
1026 }
1027 }
1028
1029 DPRINTF(("%s: sci_stsoft\n", device_xname(sc->sc_dev)));
1030 #endif
1031 }
1032
1033 void
scisoft(void * arg)1034 scisoft(void *arg)
1035 {
1036 struct sci_softc *sc = arg;
1037 struct tty *tp;
1038
1039 if (!device_is_active(sc->sc_dev))
1040 return;
1041
1042 tp = sc->sc_tty;
1043
1044 if (sc->sc_rx_ready) {
1045 sc->sc_rx_ready = 0;
1046 sci_rxsoft(sc, tp);
1047 }
1048
1049 #if 0
1050 if (sc->sc_st_check) {
1051 sc->sc_st_check = 0;
1052 sci_stsoft(sc, tp);
1053 }
1054 #endif
1055
1056 if (sc->sc_tx_done) {
1057 sc->sc_tx_done = 0;
1058 sci_txsoft(sc, tp);
1059 }
1060 }
1061
1062 int
sciintr(void * arg)1063 sciintr(void *arg)
1064 {
1065 struct sci_softc *sc = arg;
1066 u_char *put, *end;
1067 u_int cc;
1068 u_short ssr;
1069
1070 if (!device_is_active(sc->sc_dev))
1071 return (0);
1072
1073 end = sc->sc_ebuf;
1074 put = sc->sc_rbput;
1075 cc = sc->sc_rbavail;
1076
1077 do {
1078 ssr = SHREG_SCSSR;
1079 if (ISSET(ssr, SCSSR_FER)) {
1080 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_PER | SCSSR_FER);
1081 #if defined(DDB) || defined(KGDB)
1082 #ifdef SH4
1083 if ((SHREG_SCSPTR & SCSPTR_SPB0DT) != 0) {
1084 #else
1085 if ((SHREG_SCSPDR & SCSPDR_SCP0DT) != 0) {
1086 #endif
1087 #ifdef DDB
1088 if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
1089 console_debugger();
1090 }
1091 #endif
1092 #ifdef KGDB
1093 if (ISSET(sc->sc_hwflags, SCI_HW_KGDB)) {
1094 kgdb_connect(1);
1095 }
1096 #endif
1097 }
1098 #endif /* DDB || KGDB */
1099 }
1100 if ((SHREG_SCSSR & SCSSR_RDRF) != 0) {
1101 if (cc > 0) {
1102 put[0] = SHREG_SCRDR;
1103 put[1] = SHREG_SCSSR & 0x00ff;
1104
1105 put += 2;
1106 if (put >= end)
1107 put = sc->sc_rbuf;
1108 cc--;
1109 }
1110
1111 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER |
1112 SCSSR_RDRF);
1113
1114 /*
1115 * Current string of incoming characters ended because
1116 * no more data was available or we ran out of space.
1117 * Schedule a receive event if any data was received.
1118 * If we're out of space, turn off receive interrupts.
1119 */
1120 sc->sc_rbput = put;
1121 sc->sc_rbavail = cc;
1122 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1123 sc->sc_rx_ready = 1;
1124
1125 /*
1126 * See if we are in danger of overflowing a buffer. If
1127 * so, use hardware flow control to ease the pressure.
1128 */
1129 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1130 cc < sc->sc_r_hiwat) {
1131 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1132 #if 0
1133 sci_hwiflow(sc);
1134 #endif
1135 }
1136
1137 /*
1138 * If we're out of space, disable receive interrupts
1139 * until the queue has drained a bit.
1140 */
1141 if (!cc) {
1142 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1143 SHREG_SCSCR &= ~SCSCR_RIE;
1144 }
1145 } else {
1146 if (SHREG_SCSSR & SCSSR_RDRF) {
1147 SHREG_SCSCR &= ~(SCSCR_TIE | SCSCR_RIE);
1148 delay(10);
1149 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
1150 continue;
1151 }
1152 }
1153 } while (SHREG_SCSSR & SCSSR_RDRF);
1154
1155 #if 0
1156 msr = bus_space_read_1(iot, ioh, sci_msr);
1157 delta = msr ^ sc->sc_msr;
1158 sc->sc_msr = msr;
1159 if (ISSET(delta, sc->sc_msr_mask)) {
1160 SET(sc->sc_msr_delta, delta);
1161
1162 /*
1163 * Pulse-per-second clock signal on edge of DCD?
1164 */
1165 if (ISSET(delta, sc->sc_ppsmask)) {
1166 struct timeval tv;
1167 if (ISSET(msr, sc->sc_ppsmask) ==
1168 sc->sc_ppsassert) {
1169 /* XXX nanotime() */
1170 microtime(&tv);
1171 TIMEVAL_TO_TIMESPEC(&tv,
1172 &sc->ppsinfo.assert_timestamp);
1173 if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
1174 timespecadd(&sc->ppsinfo.assert_timestamp,
1175 &sc->ppsparam.assert_offset,
1176 &sc->ppsinfo.assert_timestamp);
1177 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.assert_timestamp);
1178 }
1179
1180 #ifdef PPS_SYNC
1181 if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
1182 hardpps(&tv, tv.tv_usec);
1183 #endif
1184 sc->ppsinfo.assert_sequence++;
1185 sc->ppsinfo.current_mode =
1186 sc->ppsparam.mode;
1187
1188 } else if (ISSET(msr, sc->sc_ppsmask) ==
1189 sc->sc_ppsclear) {
1190 /* XXX nanotime() */
1191 microtime(&tv);
1192 TIMEVAL_TO_TIMESPEC(&tv,
1193 &sc->ppsinfo.clear_timestamp);
1194 if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
1195 timespecadd(&sc->ppsinfo.clear_timestamp,
1196 &sc->ppsparam.clear_offset,
1197 &sc->ppsinfo.clear_timestamp);
1198 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.clear_timestamp);
1199 }
1200
1201 #ifdef PPS_SYNC
1202 if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
1203 hardpps(&tv, tv.tv_usec);
1204 #endif
1205 sc->ppsinfo.clear_sequence++;
1206 sc->ppsinfo.current_mode =
1207 sc->ppsparam.mode;
1208 }
1209 }
1210
1211 /*
1212 * Stop output immediately if we lose the output
1213 * flow control signal or carrier detect.
1214 */
1215 if (ISSET(~msr, sc->sc_msr_mask)) {
1216 sc->sc_tbc = 0;
1217 sc->sc_heldtbc = 0;
1218
1219 DPRINTF(("%s: sciintr\n", device_xname(sc->sc_dev)));
1220 }
1221
1222 sc->sc_st_check = 1;
1223 }
1224 #endif
1225
1226 /*
1227 * Done handling any receive interrupts. See if data can be
1228 * transmitted as well. Schedule tx done event if no data left
1229 * and tty was marked busy.
1230 */
1231 if ((SHREG_SCSSR & SCSSR_TDRE) != 0) {
1232 /*
1233 * If we've delayed a parameter change, do it now, and restart
1234 * output.
1235 */
1236 if (sc->sc_heldchange) {
1237 sc->sc_heldchange = 0;
1238 sc->sc_tbc = sc->sc_heldtbc;
1239 sc->sc_heldtbc = 0;
1240 }
1241
1242 /* Output the next chunk of the contiguous buffer, if any. */
1243 if (sc->sc_tbc > 0) {
1244 sci_putc(*(sc->sc_tba));
1245 sc->sc_tba++;
1246 sc->sc_tbc--;
1247 } else {
1248 /* Disable transmit completion interrupts if necessary. */
1249 #if 0
1250 if (ISSET(sc->sc_ier, IER_ETXRDY))
1251 #endif
1252 SHREG_SCSCR &= ~SCSCR_TIE;
1253
1254 if (sc->sc_tx_busy) {
1255 sc->sc_tx_busy = 0;
1256 sc->sc_tx_done = 1;
1257 }
1258 }
1259 }
1260
1261 /* Wake up the poller. */
1262 softint_schedule(sc->sc_si);
1263
1264 #ifdef RND_SCI
1265 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1266 #endif
1267
1268 return (1);
1269 }
1270
1271 void
1272 scicnprobe(struct consdev *cp)
1273 {
1274 int maj;
1275
1276 /* locate the major number */
1277 maj = cdevsw_lookup_major(&sci_cdevsw);
1278
1279 /* Initialize required fields. */
1280 cp->cn_dev = makedev(maj, 0);
1281 #ifdef SCICONSOLE
1282 cp->cn_pri = CN_REMOTE;
1283 #else
1284 cp->cn_pri = CN_NORMAL;
1285 #endif
1286 }
1287
1288 void
1289 scicninit(struct consdev *cp)
1290 {
1291
1292 InitializeSci(scicn_speed);
1293 sciisconsole = 1;
1294 }
1295
1296 int
1297 scicngetc(dev_t dev)
1298 {
1299 int c;
1300 int s;
1301
1302 s = splserial();
1303 c = sci_getc();
1304 splx(s);
1305
1306 return (c);
1307 }
1308
1309 void
1310 scicnputc(dev_t dev, int c)
1311 {
1312 int s;
1313
1314 s = splserial();
1315 sci_putc((u_char)c);
1316 splx(s);
1317 }
1318