sscom.c revision 1.9 1 /* $NetBSD: sscom.c,v 1.9 2003/07/31 19:08:10 bsh Exp $ */
2
3 /*
4 * Copyright (c) 2002, 2003 Fujitsu Component Limited
5 * Copyright (c) 2002, 2003 Genetec Corporation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The Fujitsu Component Limited nor the name of
17 * Genetec corporation may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY FUJITSU COMPONENT LIMITED AND GENETEC
21 * CORPORATION ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL FUJITSU COMPONENT LIMITED OR GENETEC
25 * CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*-
36 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
37 * All rights reserved.
38 *
39 * This code is derived from software contributed to The NetBSD Foundation
40 * by Charles M. Hannum.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the NetBSD
53 * Foundation, Inc. and its contributors.
54 * 4. Neither the name of The NetBSD Foundation nor the names of its
55 * contributors may be used to endorse or promote products derived
56 * from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
59 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68 * POSSIBILITY OF SUCH DAMAGE.
69 */
70
71 /*
72 * Copyright (c) 1991 The Regents of the University of California.
73 * All rights reserved.
74 *
75 * Redistribution and use in source and binary forms, with or without
76 * modification, are permitted provided that the following conditions
77 * are met:
78 * 1. Redistributions of source code must retain the above copyright
79 * notice, this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright
81 * notice, this list of conditions and the following disclaimer in the
82 * documentation and/or other materials provided with the distribution.
83 * 3. All advertising materials mentioning features or use of this software
84 * must display the following acknowledgement:
85 * This product includes software developed by the University of
86 * California, Berkeley and its contributors.
87 * 4. Neither the name of the University nor the names of its contributors
88 * may be used to endorse or promote products derived from this software
89 * without specific prior written permission.
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
92 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
94 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
95 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
96 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
97 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
98 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
99 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
100 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
101 * SUCH DAMAGE.
102 *
103 * @(#)com.c 7.5 (Berkeley) 5/16/91
104 */
105
106 /*
107 * Support integrated UARTs of Samsung S3C2800/2400X/2410X
108 * Derived from sys/dev/ic/com.c
109 */
110
111 #include <sys/cdefs.h>
112 __KERNEL_RCSID(0, "$NetBSD: sscom.c,v 1.9 2003/07/31 19:08:10 bsh Exp $");
113
114 #include "opt_sscom.h"
115 #include "opt_ddb.h"
116 #include "opt_kgdb.h"
117 #include "opt_multiprocessor.h"
118 #include "opt_lockdebug.h"
119
120 #include "rnd.h"
121 #if NRND > 0 && defined(RND_COM)
122 #include <sys/rnd.h>
123 #endif
124
125 /*
126 * Override cnmagic(9) macro before including <sys/systm.h>.
127 * We need to know if cn_check_magic triggered debugger, so set a flag.
128 * Callers of cn_check_magic must declare int cn_trapped = 0;
129 * XXX: this is *ugly*!
130 */
131 #define cn_trap() \
132 do { \
133 console_debugger(); \
134 cn_trapped = 1; \
135 } while (/* CONSTCOND */ 0)
136
137 #include <sys/param.h>
138 #include <sys/systm.h>
139 #include <sys/ioctl.h>
140 #include <sys/select.h>
141 #include <sys/tty.h>
142 #include <sys/proc.h>
143 #include <sys/user.h>
144 #include <sys/conf.h>
145 #include <sys/file.h>
146 #include <sys/uio.h>
147 #include <sys/kernel.h>
148 #include <sys/syslog.h>
149 #include <sys/types.h>
150 #include <sys/device.h>
151 #include <sys/malloc.h>
152 #include <sys/timepps.h>
153 #include <sys/vnode.h>
154
155 #include <machine/intr.h>
156 #include <machine/bus.h>
157
158 #include <arm/s3c2xx0/s3c2xx0reg.h>
159 #include <arm/s3c2xx0/s3c2xx0var.h>
160 #include <arm/s3c2xx0/sscom_var.h>
161 #include <dev/cons.h>
162
163 dev_type_open(sscomopen);
164 dev_type_close(sscomclose);
165 dev_type_read(sscomread);
166 dev_type_write(sscomwrite);
167 dev_type_ioctl(sscomioctl);
168 dev_type_stop(sscomstop);
169 dev_type_tty(sscomtty);
170 dev_type_poll(sscompoll);
171
172 int sscomcngetc (dev_t);
173 void sscomcnputc (dev_t, int);
174 void sscomcnpollc (dev_t, int);
175
176 #define integrate static inline
177 void sscomsoft (void *);
178
179 integrate void sscom_rxsoft (struct sscom_softc *, struct tty *);
180 integrate void sscom_txsoft (struct sscom_softc *, struct tty *);
181 integrate void sscom_stsoft (struct sscom_softc *, struct tty *);
182 integrate void sscom_schedrx (struct sscom_softc *);
183 static void sscom_modem(struct sscom_softc *, int);
184 static void sscom_break(struct sscom_softc *, int);
185 static void sscom_iflush(struct sscom_softc *);
186 static void sscom_hwiflow(struct sscom_softc *);
187 static void sscom_loadchannelregs(struct sscom_softc *);
188 static void tiocm_to_sscom(struct sscom_softc *, u_long, int);
189 static int sscom_to_tiocm(struct sscom_softc *);
190 static void tiocm_to_sscom(struct sscom_softc *, u_long, int);
191 static int sscom_to_tiocm(struct sscom_softc *);
192 static void sscom_iflush(struct sscom_softc *);
193
194 static int sscomhwiflow(struct tty *tp, int block);
195 static int sscom_init(bus_space_tag_t, const struct sscom_uart_info *,
196 int, int, tcflag_t, bus_space_handle_t *);
197
198 extern struct cfdriver sscom_cd;
199
200 const struct cdevsw sscom_cdevsw = {
201 sscomopen, sscomclose, sscomread, sscomwrite, sscomioctl,
202 sscomstop, sscomtty, sscompoll, nommap, ttykqfilter, D_TTY
203 };
204
205 /*
206 * Make this an option variable one can patch.
207 * But be warned: this must be a power of 2!
208 */
209 u_int sscom_rbuf_size = SSCOM_RING_SIZE;
210
211 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
212 u_int sscom_rbuf_hiwat = (SSCOM_RING_SIZE * 1) / 4;
213 u_int sscom_rbuf_lowat = (SSCOM_RING_SIZE * 3) / 4;
214
215 static int sscomconsunit = -1;
216 static bus_space_tag_t sscomconstag;
217 static bus_space_handle_t sscomconsioh;
218 static int sscomconsattached;
219 static int sscomconsrate;
220 static tcflag_t sscomconscflag;
221 static struct cnm_state sscom_cnm_state;
222
223 #ifdef KGDB
224 #include <sys/kgdb.h>
225
226 static int sscom_kgdb_unit = -1;
227 static bus_space_tag_t sscom_kgdb_iot;
228 static bus_space_handle_t sscom_kgdb_ioh;
229 static int sscom_kgdb_attached;
230
231 int sscom_kgdb_getc (void *);
232 void sscom_kgdb_putc (void *, int);
233 #endif /* KGDB */
234
235 #define SSCOMUNIT_MASK 0x7f
236 #define SSCOMDIALOUT_MASK 0x80
237
238 #define SSCOMUNIT(x) (minor(x) & SSCOMUNIT_MASK)
239 #define SSCOMDIALOUT(x) (minor(x) & SSCOMDIALOUT_MASK)
240
241 #if 0
242 #define SSCOM_ISALIVE(sc) ((sc)->enabled != 0 && \
243 ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
244 #else
245 #define SSCOM_ISALIVE(sc) ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE)
246 #endif
247
248 #define BR BUS_SPACE_BARRIER_READ
249 #define BW BUS_SPACE_BARRIER_WRITE
250 #define SSCOM_BARRIER(t, h, f) /* no-op */
251
252 #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(SSCOM_MPLOCK)
253
254 #define SSCOM_LOCK(sc) simple_lock(&(sc)->sc_lock)
255 #define SSCOM_UNLOCK(sc) simple_unlock(&(sc)->sc_lock)
256
257 #else
258
259 #define SSCOM_LOCK(sc)
260 #define SSCOM_UNLOCK(sc)
261
262 #endif
263
264 #ifndef SSCOM_TOLERANCE
265 #define SSCOM_TOLERANCE 30 /* XXX: baud rate tolerance, in 0.1% units */
266 #endif
267
268 /* value for UCON */
269 #define UCON_RXINT_MASK \
270 (UCON_RXMODE_MASK|UCON_ERRINT|UCON_TOINT|UCON_RXINT_TYPE)
271 #define UCON_RXINT_ENABLE \
272 (UCON_RXMODE_INT|UCON_ERRINT|UCON_TOINT|UCON_RXINT_TYPE_LEVEL)
273 #define UCON_TXINT_MASK (UCON_TXMODE_MASK|UCON_TXINT_TYPE)
274 #define UCON_TXINT_ENABLE (UCON_TXMODE_INT|UCON_TXINT_TYPE_LEVEL)
275
276 /* we don't want tx interrupt on debug port, but it is needed to
277 have transmitter active */
278 #define UCON_DEBUGPORT (UCON_RXINT_ENABLE|UCON_TXINT_ENABLE)
279
280
281 static __inline void
282 __sscom_output_chunk(struct sscom_softc *sc, int ufstat)
283 {
284 int n, space;
285 bus_space_tag_t iot = sc->sc_iot;
286 bus_space_handle_t ioh = sc->sc_ioh;
287
288 n = sc->sc_tbc;
289 space = 16 - ((ufstat & UFSTAT_TXCOUNT) >> UFSTAT_TXCOUNT_SHIFT);
290
291 if (n > space)
292 n = space;
293
294 if (n > 0) {
295 bus_space_write_multi_1(iot, ioh, SSCOM_UTXH, sc->sc_tba, n);
296 sc->sc_tbc -= n;
297 sc->sc_tba += n;
298 }
299 }
300
301 static void
302 sscom_output_chunk(struct sscom_softc *sc)
303 {
304 int ufstat = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SSCOM_UFSTAT);
305
306 if (!(ufstat & UFSTAT_TXFULL))
307 __sscom_output_chunk(sc, ufstat);
308 }
309
310 int
311 sscomspeed(long speed, long frequency)
312 {
313 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
314
315 int x, err;
316
317 if (speed <= 0)
318 return -1;
319 x = divrnd(frequency / 16, speed);
320 if (x <= 0)
321 return -1;
322 err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
323 if (err < 0)
324 err = -err;
325 if (err > SSCOM_TOLERANCE)
326 return -1;
327 return x-1;
328
329 #undef divrnd
330 }
331
332 void sscomstatus (struct sscom_softc *, char *);
333
334 #ifdef SSCOM_DEBUG
335 int sscom_debug = 0;
336
337 void
338 sscomstatus(struct sscom_softc *sc, char *str)
339 {
340 struct tty *tp = sc->sc_tty;
341 int umstat = bus_space_read_1(sc->sc_iot, sc->sc_iot, SSCOM_UMSTAT);
342 int umcon = bus_space_read_1(sc->sc_iot, sc->sc_iot, SSCOM_UMCON);
343
344 printf("%s: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n",
345 sc->sc_dev.dv_xname, str,
346 ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
347 "+", /* DCD */
348 ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
349 "+", /* DTR */
350 sc->sc_tx_stopped ? "+" : "-");
351
352 printf("%s: %s %scrtscts %scts %sts_ttstop %srts %xrx_flags\n",
353 sc->sc_dev.dv_xname, str,
354 ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
355 ISSET(umstat, UMSTAT_CTS) ? "+" : "-",
356 ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
357 ISSET(umcon, UMCON_RTS) ? "+" : "-",
358 sc->sc_rx_flags);
359 }
360 #else
361 #define sscom_debug 0
362 #endif
363
364 static void
365 sscom_enable_debugport(struct sscom_softc *sc)
366 {
367 int s;
368
369 /* Turn on line break interrupt, set carrier. */
370 s = splserial();
371 SSCOM_LOCK(sc);
372 sc->sc_ucon = UCON_DEBUGPORT;
373 bus_space_write_2(sc->sc_iot, sc->sc_ioh, SSCOM_UCON, sc->sc_ucon);
374 sc->sc_umcon = UMCON_RTS|UMCON_DTR;
375 sc->set_modem_control(sc);
376 sscom_enable_rxint(sc);
377 sscom_disable_txint(sc);
378 SSCOM_UNLOCK(sc);
379 splx(s);
380 }
381
382 static void
383 sscom_set_modem_control(struct sscom_softc *sc)
384 {
385 /* flob RTS */
386 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
387 SSCOM_UMCON, sc->sc_umcon & UMCON_HW_MASK);
388 /* ignore DTR */
389 }
390
391 static int
392 sscom_read_modem_status(struct sscom_softc *sc)
393 {
394 int msts;
395
396 msts = bus_space_read_1(sc->sc_iot, sc->sc_ioh, SSCOM_UMSTAT);
397
398 /* DCD and DSR are always on */
399 return (msts & UMSTAT_CTS) | MSTS_DCD | MSTS_DSR;
400 }
401
402 void
403 sscom_attach_subr(struct sscom_softc *sc)
404 {
405 int unit = sc->sc_unit;
406 bus_space_tag_t iot = sc->sc_iot;
407 bus_space_handle_t ioh = sc->sc_ioh;
408 struct tty *tp;
409
410 callout_init(&sc->sc_diag_callout);
411 #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(SSCOM_MPLOCK)
412 simple_lock_init(&sc->sc_lock);
413 #endif
414
415 sc->sc_ucon = UCON_RXINT_ENABLE|UCON_TXINT_ENABLE;
416
417 /*
418 * set default for modem control hook
419 */
420 if (sc->set_modem_control == NULL)
421 sc->set_modem_control = sscom_set_modem_control;
422 if (sc->read_modem_status == NULL)
423 sc->read_modem_status = sscom_read_modem_status;
424
425 /* Disable interrupts before configuring the device. */
426 sscom_disable_txrxint(sc);
427
428 #ifdef KGDB
429 /*
430 * Allow kgdb to "take over" this port. If this is
431 * the kgdb device, it has exclusive use.
432 */
433 if (unit == sscom_kgdb_unit) {
434 SET(sc->sc_hwflags, SSCOM_HW_KGDB);
435 sc->sc_ucon = UCON_DEBUGPORT;
436 }
437 #endif
438
439 if (unit == sscomconsunit) {
440 sscomconsattached = 1;
441
442 sscomconstag = iot;
443 sscomconsioh = ioh;
444
445 /* Make sure the console is always "hardwired". */
446 delay(1000); /* XXX: wait for output to finish */
447 SET(sc->sc_hwflags, SSCOM_HW_CONSOLE);
448 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
449
450 sc->sc_ucon = UCON_DEBUGPORT;
451 }
452
453 bus_space_write_1(iot, ioh, SSCOM_UFCON,
454 UFCON_TXTRIGGER_8|UFCON_RXTRIGGER_8|UFCON_FIFO_ENABLE|
455 UFCON_TXFIFO_RESET|UFCON_RXFIFO_RESET);
456
457 bus_space_write_1(iot, ioh, SSCOM_UCON, sc->sc_ucon);
458
459 #ifdef KGDB
460 if (ISSET(sc->sc_hwflags, SSCOM_HW_KGDB)) {
461 sscom_kgdb_attached = 1;
462 printf("%s: kgdb\n", sc->sc_dev.dv_xname);
463 sscom_enable_debugport(sc);
464 return;
465 }
466 #endif
467
468
469
470 tp = ttymalloc();
471 tp->t_oproc = sscomstart;
472 tp->t_param = sscomparam;
473 tp->t_hwiflow = sscomhwiflow;
474
475 sc->sc_tty = tp;
476 sc->sc_rbuf = malloc(sscom_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
477 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
478 sc->sc_rbavail = sscom_rbuf_size;
479 if (sc->sc_rbuf == NULL) {
480 printf("%s: unable to allocate ring buffer\n",
481 sc->sc_dev.dv_xname);
482 return;
483 }
484 sc->sc_ebuf = sc->sc_rbuf + (sscom_rbuf_size << 1);
485
486 tty_attach(tp);
487
488 if (ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE)) {
489 int maj;
490
491 /* locate the major number */
492 maj = cdevsw_lookup_major(&sscom_cdevsw);
493
494 cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
495
496 printf("%s: console (major=%d)\n", sc->sc_dev.dv_xname, maj);
497 }
498
499
500 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, sscomsoft, sc);
501
502 #if NRND > 0 && defined(RND_COM)
503 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
504 RND_TYPE_TTY, 0);
505 #endif
506
507 /* if there are no enable/disable functions, assume the device
508 is always enabled */
509
510 if (ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE))
511 sscom_enable_debugport(sc);
512 else
513 sscom_disable_txrxint(sc);
514
515 SET(sc->sc_hwflags, SSCOM_HW_DEV_OK);
516 }
517
518 int
519 sscom_detach(struct device *self, int flags)
520 {
521 return 0;
522 }
523
524 int
525 sscom_activate(struct device *self, enum devact act)
526 {
527 #ifdef notyet
528 struct sscom_softc *sc = (struct sscom_softc *)self;
529 int s, rv = 0;
530
531 s = splserial();
532 SSCOM_LOCK(sc);
533 switch (act) {
534 case DVACT_ACTIVATE:
535 rv = EOPNOTSUPP;
536 break;
537
538 case DVACT_DEACTIVATE:
539 if (sc->sc_hwflags & (SSCOM_HW_CONSOLE|SSCOM_HW_KGDB)) {
540 rv = EBUSY;
541 break;
542 }
543
544 sc->enabled = 0;
545 break;
546 }
547
548 SSCOM_UNLOCK(sc);
549 splx(s);
550 return rv;
551 #else
552 return 0;
553 #endif
554 }
555
556 void
557 sscom_shutdown(struct sscom_softc *sc)
558 {
559 #ifdef notyet
560 struct tty *tp = sc->sc_tty;
561 int s;
562
563 s = splserial();
564 SSCOM_LOCK(sc);
565
566 /* If we were asserting flow control, then deassert it. */
567 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
568 sscom_hwiflow(sc);
569
570 /* Clear any break condition set with TIOCSBRK. */
571 sscom_break(sc, 0);
572
573 /*
574 * Hang up if necessary. Wait a bit, so the other side has time to
575 * notice even if we immediately open the port again.
576 * Avoid tsleeping above splhigh().
577 */
578 if (ISSET(tp->t_cflag, HUPCL)) {
579 sscom_modem(sc, 0);
580 SSCOM_UNLOCK(sc);
581 splx(s);
582 /* XXX tsleep will only timeout */
583 (void) tsleep(sc, TTIPRI, ttclos, hz);
584 s = splserial();
585 SSCOM_LOCK(sc);
586 }
587
588 if (ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE))
589 /* interrupt on break */
590 sc->sc_ucon = UCON_DEBUGPORT;
591 else
592 sc->sc_ucon = 0;
593 bus_space_write_2(sc->sc_iot, sc->sc_ioh, SSCOM_UCON, sc->sc_ucon);
594
595 #ifdef DIAGNOSTIC
596 if (!sc->enabled)
597 panic("sscom_shutdown: not enabled?");
598 #endif
599 sc->enabled = 0;
600 SSCOM_UNLOCK(sc);
601 splx(s);
602 #endif
603 }
604
605 int
606 sscomopen(dev_t dev, int flag, int mode, struct proc *p)
607 {
608 struct sscom_softc *sc;
609 struct tty *tp;
610 int s, s2;
611 int error;
612
613 sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
614 if (sc == NULL || !ISSET(sc->sc_hwflags, SSCOM_HW_DEV_OK) ||
615 sc->sc_rbuf == NULL)
616 return ENXIO;
617
618 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
619 return ENXIO;
620
621 #ifdef KGDB
622 /*
623 * If this is the kgdb port, no other use is permitted.
624 */
625 if (ISSET(sc->sc_hwflags, SSCOM_HW_KGDB))
626 return EBUSY;
627 #endif
628
629 tp = sc->sc_tty;
630
631 if (ISSET(tp->t_state, TS_ISOPEN) &&
632 ISSET(tp->t_state, TS_XCLUDE) &&
633 p->p_ucred->cr_uid != 0)
634 return EBUSY;
635
636 s = spltty();
637
638 /*
639 * Do the following iff this is a first open.
640 */
641 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
642 struct termios t;
643
644 tp->t_dev = dev;
645
646 s2 = splserial();
647 SSCOM_LOCK(sc);
648
649 /* Turn on interrupts. */
650 sscom_enable_txrxint(sc);
651
652 /* Fetch the current modem control status, needed later. */
653 sc->sc_msts = sc->read_modem_status(sc);
654
655 #if 0
656 /* Clear PPS capture state on first open. */
657 sc->sc_ppsmask = 0;
658 sc->ppsparam.mode = 0;
659 #endif
660
661 SSCOM_UNLOCK(sc);
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, SSCOM_HW_CONSOLE)) {
670 t.c_ospeed = sscomconsrate;
671 t.c_cflag = sscomconscflag;
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 sscomparam() will do something. */
683 tp->t_ospeed = 0;
684 (void) sscomparam(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 SSCOM_LOCK(sc);
693
694 /*
695 * Turn on DTR. We must always do this, even if carrier is not
696 * present, because otherwise we'd have to use TIOCSDTR
697 * immediately after setting CLOCAL, which applications do not
698 * expect. We always assert DTR while the device is open
699 * unless explicitly requested to deassert it.
700 */
701 sscom_modem(sc, 1);
702
703 /* Clear the input ring, and unblock. */
704 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
705 sc->sc_rbavail = sscom_rbuf_size;
706 sscom_iflush(sc);
707 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
708 sscom_hwiflow(sc);
709
710 if (sscom_debug)
711 sscomstatus(sc, "sscomopen ");
712
713 SSCOM_UNLOCK(sc);
714 splx(s2);
715 }
716
717 splx(s);
718
719 error = ttyopen(tp, SSCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
720 if (error)
721 goto bad;
722
723 error = (*tp->t_linesw->l_open)(dev, tp);
724 if (error)
725 goto bad;
726
727 return 0;
728
729 bad:
730 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
731 /*
732 * We failed to open the device, and nobody else had it opened.
733 * Clean up the state as appropriate.
734 */
735 sscom_shutdown(sc);
736 }
737
738 return error;
739 }
740
741 int
742 sscomclose(dev_t dev, int flag, int mode, struct proc *p)
743 {
744 struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
745 struct tty *tp = sc->sc_tty;
746
747 /* XXX This is for cons.c. */
748 if (!ISSET(tp->t_state, TS_ISOPEN))
749 return 0;
750
751 (*tp->t_linesw->l_close)(tp, flag);
752 ttyclose(tp);
753
754 if (SSCOM_ISALIVE(sc) == 0)
755 return 0;
756
757 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
758 /*
759 * Although we got a last close, the device may still be in
760 * use; e.g. if this was the dialout node, and there are still
761 * processes waiting for carrier on the non-dialout node.
762 */
763 sscom_shutdown(sc);
764 }
765
766 return 0;
767 }
768
769 int
770 sscomread(dev_t dev, struct uio *uio, int flag)
771 {
772 struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
773 struct tty *tp = sc->sc_tty;
774
775 if (SSCOM_ISALIVE(sc) == 0)
776 return EIO;
777
778 return (*tp->t_linesw->l_read)(tp, uio, flag);
779 }
780
781 int
782 sscomwrite(dev_t dev, struct uio *uio, int flag)
783 {
784 struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
785 struct tty *tp = sc->sc_tty;
786
787 if (SSCOM_ISALIVE(sc) == 0)
788 return EIO;
789
790 return (*tp->t_linesw->l_write)(tp, uio, flag);
791 }
792
793 int
794 sscompoll(dev_t dev, int events, struct proc *p)
795 {
796 struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
797 struct tty *tp = sc->sc_tty;
798
799 if (SSCOM_ISALIVE(sc) == 0)
800 return EIO;
801
802 return (*tp->t_linesw->l_poll)(tp, events, p);
803 }
804
805 struct tty *
806 sscomtty(dev_t dev)
807 {
808 struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
809 struct tty *tp = sc->sc_tty;
810
811 return tp;
812 }
813
814 int
815 sscomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
816 {
817 struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(dev));
818 struct tty *tp = sc->sc_tty;
819 int error;
820 int s;
821
822 if (SSCOM_ISALIVE(sc) == 0)
823 return EIO;
824
825 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
826 if (error != EPASSTHROUGH)
827 return error;
828
829 error = ttioctl(tp, cmd, data, flag, p);
830 if (error != EPASSTHROUGH)
831 return error;
832
833 error = 0;
834
835 s = splserial();
836 SSCOM_LOCK(sc);
837
838 switch (cmd) {
839 case TIOCSBRK:
840 sscom_break(sc, 1);
841 break;
842
843 case TIOCCBRK:
844 sscom_break(sc, 0);
845 break;
846
847 case TIOCSDTR:
848 sscom_modem(sc, 1);
849 break;
850
851 case TIOCCDTR:
852 sscom_modem(sc, 0);
853 break;
854
855 case TIOCGFLAGS:
856 *(int *)data = sc->sc_swflags;
857 break;
858
859 case TIOCSFLAGS:
860 error = suser(p->p_ucred, &p->p_acflag);
861 if (error)
862 break;
863 sc->sc_swflags = *(int *)data;
864 break;
865
866 case TIOCMSET:
867 case TIOCMBIS:
868 case TIOCMBIC:
869 tiocm_to_sscom(sc, cmd, *(int *)data);
870 break;
871
872 case TIOCMGET:
873 *(int *)data = sscom_to_tiocm(sc);
874 break;
875
876 default:
877 error = EPASSTHROUGH;
878 break;
879 }
880
881 SSCOM_UNLOCK(sc);
882 splx(s);
883
884 if (sscom_debug)
885 sscomstatus(sc, "sscomioctl ");
886
887 return error;
888 }
889
890 integrate void
891 sscom_schedrx(struct sscom_softc *sc)
892 {
893
894 sc->sc_rx_ready = 1;
895
896 /* Wake up the poller. */
897 softintr_schedule(sc->sc_si);
898 }
899
900 static void
901 sscom_break(struct sscom_softc *sc, int onoff)
902 {
903
904 if (onoff)
905 SET(sc->sc_ucon, UCON_SBREAK);
906 else
907 CLR(sc->sc_ucon, UCON_SBREAK);
908
909 if (!sc->sc_heldchange) {
910 if (sc->sc_tx_busy) {
911 sc->sc_heldtbc = sc->sc_tbc;
912 sc->sc_tbc = 0;
913 sc->sc_heldchange = 1;
914 } else
915 sscom_loadchannelregs(sc);
916 }
917 }
918
919 static void
920 sscom_modem(struct sscom_softc *sc, int onoff)
921 {
922 if (onoff)
923 SET(sc->sc_umcon, UMCON_DTR);
924 else
925 CLR(sc->sc_umcon, UMCON_DTR);
926
927 if (!sc->sc_heldchange) {
928 if (sc->sc_tx_busy) {
929 sc->sc_heldtbc = sc->sc_tbc;
930 sc->sc_tbc = 0;
931 sc->sc_heldchange = 1;
932 } else
933 sscom_loadchannelregs(sc);
934 }
935 }
936
937 static void
938 tiocm_to_sscom(struct sscom_softc *sc, u_long how, int ttybits)
939 {
940 u_char sscombits;
941
942 sscombits = 0;
943 if (ISSET(ttybits, TIOCM_DTR))
944 sscombits = UMCON_DTR;
945 if (ISSET(ttybits, TIOCM_RTS))
946 SET(sscombits, UMCON_RTS);
947
948 switch (how) {
949 case TIOCMBIC:
950 CLR(sc->sc_umcon, sscombits);
951 break;
952
953 case TIOCMBIS:
954 SET(sc->sc_umcon, sscombits);
955 break;
956
957 case TIOCMSET:
958 CLR(sc->sc_umcon, UMCON_DTR);
959 SET(sc->sc_umcon, sscombits);
960 break;
961 }
962
963 if (!sc->sc_heldchange) {
964 if (sc->sc_tx_busy) {
965 sc->sc_heldtbc = sc->sc_tbc;
966 sc->sc_tbc = 0;
967 sc->sc_heldchange = 1;
968 } else
969 sscom_loadchannelregs(sc);
970 }
971 }
972
973 static int
974 sscom_to_tiocm(struct sscom_softc *sc)
975 {
976 u_char sscombits;
977 int ttybits = 0;
978
979 sscombits = sc->sc_umcon;
980 #if 0
981 if (ISSET(sscombits, MCR_DTR))
982 SET(ttybits, TIOCM_DTR);
983 #endif
984 if (ISSET(sscombits, UMCON_RTS))
985 SET(ttybits, TIOCM_RTS);
986
987 sscombits = sc->sc_msts;
988 if (ISSET(sscombits, MSTS_DCD))
989 SET(ttybits, TIOCM_CD);
990 if (ISSET(sscombits, MSTS_DSR))
991 SET(ttybits, TIOCM_DSR);
992 if (ISSET(sscombits, MSTS_CTS))
993 SET(ttybits, TIOCM_CTS);
994
995 if (sc->sc_ucon != 0)
996 SET(ttybits, TIOCM_LE);
997
998 return ttybits;
999 }
1000
1001 static int
1002 cflag2lcr(tcflag_t cflag)
1003 {
1004 u_char lcr = ULCON_PARITY_NONE;
1005
1006 switch (cflag & (PARENB|PARODD)) {
1007 case PARENB|PARODD: lcr = ULCON_PARITY_ODD; break;
1008 case PARENB: lcr = ULCON_PARITY_EVEN;
1009 }
1010
1011 switch (ISSET(cflag, CSIZE)) {
1012 case CS5:
1013 SET(lcr, ULCON_LENGTH_5);
1014 break;
1015 case CS6:
1016 SET(lcr, ULCON_LENGTH_6);
1017 break;
1018 case CS7:
1019 SET(lcr, ULCON_LENGTH_7);
1020 break;
1021 case CS8:
1022 SET(lcr, ULCON_LENGTH_8);
1023 break;
1024 }
1025 if (ISSET(cflag, CSTOPB))
1026 SET(lcr, ULCON_STOP);
1027
1028 return lcr;
1029 }
1030
1031 int
1032 sscomparam(struct tty *tp, struct termios *t)
1033 {
1034 struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(tp->t_dev));
1035 int ospeed;
1036 u_char lcr;
1037 int s;
1038
1039 if (SSCOM_ISALIVE(sc) == 0)
1040 return EIO;
1041
1042 ospeed = sscomspeed(t->c_ospeed, sc->sc_frequency);
1043
1044 /* Check requested parameters. */
1045 if (ospeed < 0)
1046 return EINVAL;
1047 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
1048 return EINVAL;
1049
1050 /*
1051 * For the console, always force CLOCAL and !HUPCL, so that the port
1052 * is always active.
1053 */
1054 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
1055 ISSET(sc->sc_hwflags, SSCOM_HW_CONSOLE)) {
1056 SET(t->c_cflag, CLOCAL);
1057 CLR(t->c_cflag, HUPCL);
1058 }
1059
1060 /*
1061 * If there were no changes, don't do anything. This avoids dropping
1062 * input and improves performance when all we did was frob things like
1063 * VMIN and VTIME.
1064 */
1065 if (tp->t_ospeed == t->c_ospeed &&
1066 tp->t_cflag == t->c_cflag)
1067 return 0;
1068
1069 lcr = cflag2lcr(t->c_cflag);
1070
1071 s = splserial();
1072 SSCOM_LOCK(sc);
1073
1074 sc->sc_ulcon = lcr;
1075
1076 /*
1077 * If we're not in a mode that assumes a connection is present, then
1078 * ignore carrier changes.
1079 */
1080 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1081 sc->sc_msr_dcd = 0;
1082 else
1083 sc->sc_msr_dcd = MSTS_DCD;
1084
1085 /*
1086 * Set the flow control pins depending on the current flow control
1087 * mode.
1088 */
1089 if (ISSET(t->c_cflag, CRTSCTS)) {
1090 sc->sc_mcr_dtr = UMCON_DTR;
1091 sc->sc_mcr_rts = UMCON_RTS;
1092 sc->sc_msr_cts = MSTS_CTS;
1093 }
1094 else if (ISSET(t->c_cflag, MDMBUF)) {
1095 /*
1096 * For DTR/DCD flow control, make sure we don't toggle DTR for
1097 * carrier detection.
1098 */
1099 sc->sc_mcr_dtr = 0;
1100 sc->sc_mcr_rts = UMCON_DTR;
1101 sc->sc_msr_cts = MSTS_DCD;
1102 }
1103 else {
1104 /*
1105 * If no flow control, then always set RTS. This will make
1106 * the other side happy if it mistakenly thinks we're doing
1107 * RTS/CTS flow control.
1108 */
1109 sc->sc_mcr_dtr = UMCON_DTR | UMCON_RTS;
1110 sc->sc_mcr_rts = 0;
1111 sc->sc_msr_cts = 0;
1112 if (ISSET(sc->sc_umcon, UMCON_DTR))
1113 SET(sc->sc_umcon, UMCON_RTS);
1114 else
1115 CLR(sc->sc_umcon, UMCON_RTS);
1116 }
1117 sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
1118
1119 if (ospeed == 0)
1120 CLR(sc->sc_umcon, sc->sc_mcr_dtr);
1121 else
1122 SET(sc->sc_umcon, sc->sc_mcr_dtr);
1123
1124 sc->sc_ubrdiv = ospeed;
1125
1126 /* And copy to tty. */
1127 tp->t_ispeed = 0;
1128 tp->t_ospeed = t->c_ospeed;
1129 tp->t_cflag = t->c_cflag;
1130
1131 if (!sc->sc_heldchange) {
1132 if (sc->sc_tx_busy) {
1133 sc->sc_heldtbc = sc->sc_tbc;
1134 sc->sc_tbc = 0;
1135 sc->sc_heldchange = 1;
1136 } else
1137 sscom_loadchannelregs(sc);
1138 }
1139
1140 if (!ISSET(t->c_cflag, CHWFLOW)) {
1141 /* Disable the high water mark. */
1142 sc->sc_r_hiwat = 0;
1143 sc->sc_r_lowat = 0;
1144 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1145 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1146 sscom_schedrx(sc);
1147 }
1148 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1149 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1150 sscom_hwiflow(sc);
1151 }
1152 } else {
1153 sc->sc_r_hiwat = sscom_rbuf_hiwat;
1154 sc->sc_r_lowat = sscom_rbuf_lowat;
1155 }
1156
1157 SSCOM_UNLOCK(sc);
1158 splx(s);
1159
1160 /*
1161 * Update the tty layer's idea of the carrier bit, in case we changed
1162 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1163 * explicit request.
1164 */
1165 (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msts, MSTS_DCD));
1166
1167 if (sscom_debug)
1168 sscomstatus(sc, "sscomparam ");
1169
1170 if (!ISSET(t->c_cflag, CHWFLOW)) {
1171 if (sc->sc_tx_stopped) {
1172 sc->sc_tx_stopped = 0;
1173 sscomstart(tp);
1174 }
1175 }
1176
1177 return 0;
1178 }
1179
1180 static void
1181 sscom_iflush(struct sscom_softc *sc)
1182 {
1183 bus_space_tag_t iot = sc->sc_iot;
1184 bus_space_handle_t ioh = sc->sc_ioh;
1185 int timo;
1186
1187
1188 timo = 50000;
1189 /* flush any pending I/O */
1190 while ( sscom_rxrdy(iot, ioh) && --timo)
1191 (void)sscom_getc(iot,ioh);
1192 #ifdef DIAGNOSTIC
1193 if (!timo)
1194 printf("%s: sscom_iflush timeout\n", sc->sc_dev.dv_xname);
1195 #endif
1196 }
1197
1198 static void
1199 sscom_loadchannelregs(struct sscom_softc *sc)
1200 {
1201 bus_space_tag_t iot = sc->sc_iot;
1202 bus_space_handle_t ioh = sc->sc_ioh;
1203
1204 /* XXXXX necessary? */
1205 sscom_iflush(sc);
1206
1207 bus_space_write_2(iot, ioh, SSCOM_UCON, 0);
1208
1209 #if 0
1210 if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
1211 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
1212 bus_space_write_1(iot, ioh, com_efr, sc->sc_efr);
1213 }
1214 #endif
1215
1216 bus_space_write_2(iot, ioh, SSCOM_UBRDIV, sc->sc_ubrdiv);
1217 bus_space_write_1(iot, ioh, SSCOM_ULCON, sc->sc_ulcon);
1218 sc->set_modem_control(sc);
1219 bus_space_write_2(iot, ioh, SSCOM_UCON, sc->sc_ucon);
1220 }
1221
1222 static int
1223 sscomhwiflow(struct tty *tp, int block)
1224 {
1225 struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(tp->t_dev));
1226 int s;
1227
1228 if (SSCOM_ISALIVE(sc) == 0)
1229 return 0;
1230
1231 if (sc->sc_mcr_rts == 0)
1232 return 0;
1233
1234 s = splserial();
1235 SSCOM_LOCK(sc);
1236
1237 if (block) {
1238 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1239 SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
1240 sscom_hwiflow(sc);
1241 }
1242 } else {
1243 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1244 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1245 sscom_schedrx(sc);
1246 }
1247 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1248 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
1249 sscom_hwiflow(sc);
1250 }
1251 }
1252
1253 SSCOM_UNLOCK(sc);
1254 splx(s);
1255 return 1;
1256 }
1257
1258 /*
1259 * (un)block input via hw flowcontrol
1260 */
1261 static void
1262 sscom_hwiflow(struct sscom_softc *sc)
1263 {
1264 if (sc->sc_mcr_rts == 0)
1265 return;
1266
1267 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
1268 CLR(sc->sc_umcon, sc->sc_mcr_rts);
1269 CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
1270 } else {
1271 SET(sc->sc_umcon, sc->sc_mcr_rts);
1272 SET(sc->sc_mcr_active, sc->sc_mcr_rts);
1273 }
1274 sc->set_modem_control(sc);
1275 }
1276
1277
1278 void
1279 sscomstart(struct tty *tp)
1280 {
1281 struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(tp->t_dev));
1282 int s;
1283
1284 if (SSCOM_ISALIVE(sc) == 0)
1285 return;
1286
1287 s = spltty();
1288 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1289 goto out;
1290 if (sc->sc_tx_stopped)
1291 goto out;
1292
1293 if (tp->t_outq.c_cc <= tp->t_lowat) {
1294 if (ISSET(tp->t_state, TS_ASLEEP)) {
1295 CLR(tp->t_state, TS_ASLEEP);
1296 wakeup(&tp->t_outq);
1297 }
1298 selwakeup(&tp->t_wsel);
1299 if (tp->t_outq.c_cc == 0)
1300 goto out;
1301 }
1302
1303 /* Grab the first contiguous region of buffer space. */
1304 {
1305 u_char *tba;
1306 int tbc;
1307
1308 tba = tp->t_outq.c_cf;
1309 tbc = ndqb(&tp->t_outq, 0);
1310
1311 (void)splserial();
1312 SSCOM_LOCK(sc);
1313
1314 sc->sc_tba = tba;
1315 sc->sc_tbc = tbc;
1316 }
1317
1318 SET(tp->t_state, TS_BUSY);
1319 sc->sc_tx_busy = 1;
1320
1321 /* Output the first chunk of the contiguous buffer. */
1322 sscom_output_chunk(sc);
1323
1324 /* Enable transmit completion interrupts if necessary. */
1325 if ((sc->sc_hwflags & SSCOM_HW_TXINT) == 0)
1326 sscom_enable_txint(sc);
1327
1328 SSCOM_UNLOCK(sc);
1329 out:
1330 splx(s);
1331 return;
1332 }
1333
1334 /*
1335 * Stop output on a line.
1336 */
1337 void
1338 sscomstop(struct tty *tp, int flag)
1339 {
1340 struct sscom_softc *sc = device_lookup(&sscom_cd, SSCOMUNIT(tp->t_dev));
1341 int s;
1342
1343 s = splserial();
1344 SSCOM_LOCK(sc);
1345 if (ISSET(tp->t_state, TS_BUSY)) {
1346 /* Stop transmitting at the next chunk. */
1347 sc->sc_tbc = 0;
1348 sc->sc_heldtbc = 0;
1349 if (!ISSET(tp->t_state, TS_TTSTOP))
1350 SET(tp->t_state, TS_FLUSH);
1351 }
1352 SSCOM_UNLOCK(sc);
1353 splx(s);
1354 }
1355
1356 void
1357 sscomdiag(void *arg)
1358 {
1359 struct sscom_softc *sc = arg;
1360 int overflows, floods;
1361 int s;
1362
1363 s = splserial();
1364 SSCOM_LOCK(sc);
1365 overflows = sc->sc_overflows;
1366 sc->sc_overflows = 0;
1367 floods = sc->sc_floods;
1368 sc->sc_floods = 0;
1369 sc->sc_errors = 0;
1370 SSCOM_UNLOCK(sc);
1371 splx(s);
1372
1373 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1374 sc->sc_dev.dv_xname,
1375 overflows, overflows == 1 ? "" : "s",
1376 floods, floods == 1 ? "" : "s");
1377 }
1378
1379 integrate void
1380 sscom_rxsoft(struct sscom_softc *sc, struct tty *tp)
1381 {
1382 int (*rint) (int, struct tty *) = tp->t_linesw->l_rint;
1383 u_char *get, *end;
1384 u_int cc, scc;
1385 u_char rsr;
1386 int code;
1387 int s;
1388
1389 end = sc->sc_ebuf;
1390 get = sc->sc_rbget;
1391 scc = cc = sscom_rbuf_size - sc->sc_rbavail;
1392
1393 if (cc == sscom_rbuf_size) {
1394 sc->sc_floods++;
1395 if (sc->sc_errors++ == 0)
1396 callout_reset(&sc->sc_diag_callout, 60 * hz,
1397 sscomdiag, sc);
1398 }
1399
1400 while (cc) {
1401 code = get[0];
1402 rsr = get[1];
1403 if (rsr) {
1404 if (ISSET(rsr, UERSTAT_OVERRUN)) {
1405 sc->sc_overflows++;
1406 if (sc->sc_errors++ == 0)
1407 callout_reset(&sc->sc_diag_callout,
1408 60 * hz, sscomdiag, sc);
1409 }
1410 if (ISSET(rsr, UERSTAT_BREAK | UERSTAT_FRAME))
1411 SET(code, TTY_FE);
1412 if (ISSET(rsr, UERSTAT_PARITY))
1413 SET(code, TTY_PE);
1414 }
1415 if ((*rint)(code, tp) == -1) {
1416 /*
1417 * The line discipline's buffer is out of space.
1418 */
1419 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1420 /*
1421 * We're either not using flow control, or the
1422 * line discipline didn't tell us to block for
1423 * some reason. Either way, we have no way to
1424 * know when there's more space available, so
1425 * just drop the rest of the data.
1426 */
1427 get += cc << 1;
1428 if (get >= end)
1429 get -= sscom_rbuf_size << 1;
1430 cc = 0;
1431 } else {
1432 /*
1433 * Don't schedule any more receive processing
1434 * until the line discipline tells us there's
1435 * space available (through sscomhwiflow()).
1436 * Leave the rest of the data in the input
1437 * buffer.
1438 */
1439 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1440 }
1441 break;
1442 }
1443 get += 2;
1444 if (get >= end)
1445 get = sc->sc_rbuf;
1446 cc--;
1447 }
1448
1449 if (cc != scc) {
1450 sc->sc_rbget = get;
1451 s = splserial();
1452 SSCOM_LOCK(sc);
1453
1454 cc = sc->sc_rbavail += scc - cc;
1455 /* Buffers should be ok again, release possible block. */
1456 if (cc >= sc->sc_r_lowat) {
1457 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1458 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1459 sscom_enable_rxint(sc);
1460 sc->sc_ucon |= UCON_ERRINT;
1461 bus_space_write_2(sc->sc_iot, sc->sc_ioh, SSCOM_UCON,
1462 sc->sc_ucon);
1463
1464 }
1465 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1466 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1467 sscom_hwiflow(sc);
1468 }
1469 }
1470 SSCOM_UNLOCK(sc);
1471 splx(s);
1472 }
1473 }
1474
1475 integrate void
1476 sscom_txsoft(struct sscom_softc *sc, struct tty *tp)
1477 {
1478
1479 CLR(tp->t_state, TS_BUSY);
1480 if (ISSET(tp->t_state, TS_FLUSH))
1481 CLR(tp->t_state, TS_FLUSH);
1482 else
1483 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1484 (*tp->t_linesw->l_start)(tp);
1485 }
1486
1487 integrate void
1488 sscom_stsoft(struct sscom_softc *sc, struct tty *tp)
1489 {
1490 u_char msr, delta;
1491 int s;
1492
1493 s = splserial();
1494 SSCOM_LOCK(sc);
1495 msr = sc->sc_msts;
1496 delta = sc->sc_msr_delta;
1497 sc->sc_msr_delta = 0;
1498 SSCOM_UNLOCK(sc);
1499 splx(s);
1500
1501 if (ISSET(delta, sc->sc_msr_dcd)) {
1502 /*
1503 * Inform the tty layer that carrier detect changed.
1504 */
1505 (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSTS_DCD));
1506 }
1507
1508 if (ISSET(delta, sc->sc_msr_cts)) {
1509 /* Block or unblock output according to flow control. */
1510 if (ISSET(msr, sc->sc_msr_cts)) {
1511 sc->sc_tx_stopped = 0;
1512 (*tp->t_linesw->l_start)(tp);
1513 } else {
1514 sc->sc_tx_stopped = 1;
1515 }
1516 }
1517
1518 if (sscom_debug)
1519 sscomstatus(sc, "sscom_stsoft");
1520 }
1521
1522 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1523 void
1524 sscomsoft(void *arg)
1525 {
1526 struct sscom_softc *sc = arg;
1527 struct tty *tp;
1528
1529 if (SSCOM_ISALIVE(sc) == 0)
1530 return;
1531
1532 {
1533 tp = sc->sc_tty;
1534
1535 if (sc->sc_rx_ready) {
1536 sc->sc_rx_ready = 0;
1537 sscom_rxsoft(sc, tp);
1538 }
1539
1540 if (sc->sc_st_check) {
1541 sc->sc_st_check = 0;
1542 sscom_stsoft(sc, tp);
1543 }
1544
1545 if (sc->sc_tx_done) {
1546 sc->sc_tx_done = 0;
1547 sscom_txsoft(sc, tp);
1548 }
1549 }
1550 }
1551 #else
1552 #error sscom needs GENERIC_SOFT_INERRUPTS
1553 #endif
1554
1555
1556 int
1557 sscomrxintr(void *arg)
1558 {
1559 struct sscom_softc *sc = arg;
1560 bus_space_tag_t iot = sc->sc_iot;
1561 bus_space_handle_t ioh = sc->sc_ioh;
1562 u_char *put, *end;
1563 u_int cc;
1564
1565 if (SSCOM_ISALIVE(sc) == 0)
1566 return 0;
1567
1568 SSCOM_LOCK(sc);
1569
1570 end = sc->sc_ebuf;
1571 put = sc->sc_rbput;
1572 cc = sc->sc_rbavail;
1573
1574 do {
1575 u_char msts, delta;
1576 u_char uerstat;
1577 uint16_t ufstat;
1578
1579 ufstat = bus_space_read_2(iot, ioh, SSCOM_UFSTAT);
1580
1581 /* XXX: break interrupt with no character? */
1582
1583 if ( (ufstat & (UFSTAT_RXCOUNT|UFSTAT_RXFULL)) &&
1584 !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1585
1586 while (cc > 0) {
1587 int cn_trapped = 0;
1588
1589 /* get status and received character.
1590 read status register first */
1591 uerstat = sscom_geterr(iot, ioh);
1592 put[0] = sscom_getc(iot, ioh);
1593
1594 if (ISSET(uerstat, UERSTAT_BREAK)) {
1595 int cn_trapped = 0;
1596 cn_check_magic(sc->sc_tty->t_dev,
1597 CNC_BREAK, sscom_cnm_state);
1598 if (cn_trapped)
1599 continue;
1600 #if defined(KGDB)
1601 if (ISSET(sc->sc_hwflags,
1602 SSCOM_HW_KGDB)) {
1603 kgdb_connect(1);
1604 continue;
1605 }
1606 #endif
1607 }
1608
1609 put[1] = uerstat;
1610 cn_check_magic(sc->sc_tty->t_dev,
1611 put[0], sscom_cnm_state);
1612 if (!cn_trapped) {
1613 put += 2;
1614 if (put >= end)
1615 put = sc->sc_rbuf;
1616 cc--;
1617 }
1618
1619 ufstat = bus_space_read_2(iot, ioh, SSCOM_UFSTAT);
1620 if ( (ufstat & (UFSTAT_RXFULL|UFSTAT_RXCOUNT)) == 0 )
1621 break;
1622 }
1623
1624 /*
1625 * Current string of incoming characters ended because
1626 * no more data was available or we ran out of space.
1627 * Schedule a receive event if any data was received.
1628 * If we're out of space, turn off receive interrupts.
1629 */
1630 sc->sc_rbput = put;
1631 sc->sc_rbavail = cc;
1632 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1633 sc->sc_rx_ready = 1;
1634
1635 /*
1636 * See if we are in danger of overflowing a buffer. If
1637 * so, use hardware flow control to ease the pressure.
1638 */
1639 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1640 cc < sc->sc_r_hiwat) {
1641 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1642 sscom_hwiflow(sc);
1643 }
1644
1645 /*
1646 * If we're out of space, disable receive interrupts
1647 * until the queue has drained a bit.
1648 */
1649 if (!cc) {
1650 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1651 sscom_disable_rxint(sc);
1652 sc->sc_ucon &= ~UCON_ERRINT;
1653 bus_space_write_2(iot, ioh, SSCOM_UCON, sc->sc_ucon);
1654 }
1655 }
1656
1657
1658 msts = sc->read_modem_status(sc);
1659 delta = msts ^ sc->sc_msts;
1660 sc->sc_msts = msts;
1661
1662 #ifdef notyet
1663 /*
1664 * Pulse-per-second (PSS) signals on edge of DCD?
1665 * Process these even if line discipline is ignoring DCD.
1666 */
1667 if (delta & sc->sc_ppsmask) {
1668 struct timeval tv;
1669 if ((msr & sc->sc_ppsmask) == sc->sc_ppsassert) {
1670 /* XXX nanotime() */
1671 microtime(&tv);
1672 TIMEVAL_TO_TIMESPEC(&tv,
1673 &sc->ppsinfo.assert_timestamp);
1674 if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
1675 timespecadd(&sc->ppsinfo.assert_timestamp,
1676 &sc->ppsparam.assert_offset,
1677 &sc->ppsinfo.assert_timestamp);
1678 }
1679
1680 #ifdef PPS_SYNC
1681 if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
1682 hardpps(&tv, tv.tv_usec);
1683 #endif
1684 sc->ppsinfo.assert_sequence++;
1685 sc->ppsinfo.current_mode = sc->ppsparam.mode;
1686
1687 } else if ((msr & sc->sc_ppsmask) == sc->sc_ppsclear) {
1688 /* XXX nanotime() */
1689 microtime(&tv);
1690 TIMEVAL_TO_TIMESPEC(&tv,
1691 &sc->ppsinfo.clear_timestamp);
1692 if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
1693 timespecadd(&sc->ppsinfo.clear_timestamp,
1694 &sc->ppsparam.clear_offset,
1695 &sc->ppsinfo.clear_timestamp);
1696 }
1697
1698 #ifdef PPS_SYNC
1699 if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
1700 hardpps(&tv, tv.tv_usec);
1701 #endif
1702 sc->ppsinfo.clear_sequence++;
1703 sc->ppsinfo.current_mode = sc->ppsparam.mode;
1704 }
1705 }
1706 #endif
1707
1708 /*
1709 * Process normal status changes
1710 */
1711 if (ISSET(delta, sc->sc_msr_mask)) {
1712 SET(sc->sc_msr_delta, delta);
1713
1714 /*
1715 * Stop output immediately if we lose the output
1716 * flow control signal or carrier detect.
1717 */
1718 if (ISSET(~msts, sc->sc_msr_mask)) {
1719 sc->sc_tbc = 0;
1720 sc->sc_heldtbc = 0;
1721 #ifdef SSCOM_DEBUG
1722 if (sscom_debug)
1723 sscomstatus(sc, "sscomintr ");
1724 #endif
1725 }
1726
1727 sc->sc_st_check = 1;
1728 }
1729
1730 /*
1731 * Done handling any receive interrupts.
1732 */
1733
1734 /*
1735 * If we've delayed a parameter change, do it
1736 * now, and restart * output.
1737 */
1738 if ((ufstat & UFSTAT_TXCOUNT) == 0) {
1739 /* XXX: we should check transmitter empty also */
1740
1741 if (sc->sc_heldchange) {
1742 sscom_loadchannelregs(sc);
1743 sc->sc_heldchange = 0;
1744 sc->sc_tbc = sc->sc_heldtbc;
1745 sc->sc_heldtbc = 0;
1746 }
1747 }
1748
1749
1750 } while (0);
1751
1752 SSCOM_UNLOCK(sc);
1753
1754 /* Wake up the poller. */
1755 softintr_schedule(sc->sc_si);
1756
1757 #if NRND > 0 && defined(RND_COM)
1758 rnd_add_uint32(&sc->rnd_source, iir | rsr);
1759 #endif
1760
1761 return 1;
1762 }
1763
1764 int
1765 sscomtxintr(void *arg)
1766 {
1767 struct sscom_softc *sc = arg;
1768 bus_space_tag_t iot = sc->sc_iot;
1769 bus_space_handle_t ioh = sc->sc_ioh;
1770 uint16_t ufstat;
1771
1772 if (SSCOM_ISALIVE(sc) == 0)
1773 return 0;
1774
1775 SSCOM_LOCK(sc);
1776
1777 ufstat = bus_space_read_2(iot, ioh, SSCOM_UFSTAT);
1778
1779 /*
1780 * If we've delayed a parameter change, do it
1781 * now, and restart * output.
1782 */
1783 if (sc->sc_heldchange && (ufstat & UFSTAT_TXCOUNT) == 0) {
1784 /* XXX: we should check transmitter empty also */
1785 sscom_loadchannelregs(sc);
1786 sc->sc_heldchange = 0;
1787 sc->sc_tbc = sc->sc_heldtbc;
1788 sc->sc_heldtbc = 0;
1789 }
1790
1791 /*
1792 * See if data can be transmitted as well. Schedule tx
1793 * done event if no data left and tty was marked busy.
1794 */
1795 if (!ISSET(ufstat,UFSTAT_TXFULL)) {
1796 /*
1797 * Output the next chunk of the contiguous
1798 * buffer, if any.
1799 */
1800 if (sc->sc_tbc > 0) {
1801 __sscom_output_chunk(sc, ufstat);
1802 }
1803 else {
1804 /*
1805 * Disable transmit sscompletion
1806 * interrupts if necessary.
1807 */
1808 if (sc->sc_hwflags & SSCOM_HW_TXINT)
1809 sscom_disable_txint(sc);
1810 if (sc->sc_tx_busy) {
1811 sc->sc_tx_busy = 0;
1812 sc->sc_tx_done = 1;
1813 }
1814 }
1815 }
1816
1817 SSCOM_UNLOCK(sc);
1818
1819 /* Wake up the poller. */
1820 softintr_schedule(sc->sc_si);
1821
1822 #if NRND > 0 && defined(RND_COM)
1823 rnd_add_uint32(&sc->rnd_source, iir | rsr);
1824 #endif
1825
1826 return 1;
1827 }
1828
1829
1830 #if defined(KGDB) || defined(SSCOM0CONSOLE) || defined(SSCOM1CONSOLE)
1831 /*
1832 * Initialize UART for use as console or KGDB line.
1833 */
1834 static int
1835 sscom_init(bus_space_tag_t iot, const struct sscom_uart_info *config,
1836 int rate, int frequency, tcflag_t cflag, bus_space_handle_t *iohp)
1837 {
1838 bus_space_handle_t ioh;
1839 bus_addr_t iobase = config->iobase;
1840
1841 if (bus_space_map(iot, iobase, SSCOM_SIZE, 0, &ioh))
1842 return ENOMEM; /* ??? */
1843
1844 bus_space_write_2(iot, ioh, SSCOM_UCON, 0);
1845 bus_space_write_1(iot, ioh, SSCOM_UFCON,
1846 UFCON_TXTRIGGER_8 | UFCON_RXTRIGGER_8 |
1847 UFCON_TXFIFO_RESET | UFCON_RXFIFO_RESET |
1848 UFCON_FIFO_ENABLE );
1849 /* tx/rx fifo reset are auto-cleared */
1850
1851 rate = sscomspeed(rate, frequency);
1852 bus_space_write_2(iot, ioh, SSCOM_UBRDIV, rate);
1853 bus_space_write_2(iot, ioh, SSCOM_ULCON, cflag2lcr(cflag));
1854
1855 /* enable UART */
1856 bus_space_write_2(iot, ioh, SSCOM_UCON,
1857 UCON_TXMODE_INT|UCON_RXMODE_INT);
1858 bus_space_write_2(iot, ioh, SSCOM_UMCON, UMCON_RTS);
1859
1860 *iohp = ioh;
1861 return 0;
1862 }
1863
1864 #endif
1865
1866 #if defined(SSCOM0CONSOLE) || defined(SSCOM1CONSOLE)
1867 /*
1868 * Following are all routines needed for SSCOM to act as console
1869 */
1870 struct consdev sscomcons = {
1871 NULL, NULL, sscomcngetc, sscomcnputc, sscomcnpollc, NULL,
1872 NULL, NULL, NODEV, CN_NORMAL
1873 };
1874
1875
1876 int
1877 sscom_cnattach(bus_space_tag_t iot, const struct sscom_uart_info *config,
1878 int rate, int frequency, tcflag_t cflag)
1879 {
1880 int res;
1881
1882 res = sscom_init(iot, config, rate, frequency, cflag, &sscomconsioh);
1883 if (res)
1884 return res;
1885
1886 cn_tab = &sscomcons;
1887 cn_init_magic(&sscom_cnm_state);
1888 cn_set_magic("\047\001"); /* default magic is BREAK */
1889
1890 sscomconstag = iot;
1891 sscomconsunit = config->unit;
1892 sscomconsrate = rate;
1893 sscomconscflag = cflag;
1894
1895 return 0;
1896 }
1897
1898 void
1899 sscom_cndetach(void)
1900 {
1901 bus_space_unmap(sscomconstag, sscomconsioh, SSCOM_SIZE);
1902 sscomconstag = NULL;
1903
1904 cn_tab = NULL;
1905 }
1906
1907 /*
1908 * The read-ahead code is so that you can detect pending in-band
1909 * cn_magic in polled mode while doing output rather than having to
1910 * wait until the kernel decides it needs input.
1911 */
1912
1913 #define MAX_READAHEAD 20
1914 static int sscom_readahead[MAX_READAHEAD];
1915 static int sscom_readaheadcount = 0;
1916
1917 int
1918 sscomcngetc(dev_t dev)
1919 {
1920 int s = splserial();
1921 u_char stat, c;
1922
1923 /* got a character from reading things earlier */
1924 if (sscom_readaheadcount > 0) {
1925 int i;
1926
1927 c = sscom_readahead[0];
1928 for (i = 1; i < sscom_readaheadcount; i++) {
1929 sscom_readahead[i-1] = sscom_readahead[i];
1930 }
1931 sscom_readaheadcount--;
1932 splx(s);
1933 return c;
1934 }
1935
1936 /* block until a character becomes available */
1937 while (!sscom_rxrdy(sscomconstag, sscomconsioh))
1938 ;
1939
1940 c = sscom_getc(sscomconstag, sscomconsioh);
1941 stat = sscom_geterr(sscomconstag, sscomconsioh);
1942 {
1943 int cn_trapped = 0; /* unused */
1944 #ifdef DDB
1945 extern int db_active;
1946 if (!db_active)
1947 #endif
1948 cn_check_magic(dev, c, sscom_cnm_state);
1949 }
1950 splx(s);
1951 return c;
1952 }
1953
1954 /*
1955 * Console kernel output character routine.
1956 */
1957 void
1958 sscomcnputc(dev_t dev, int c)
1959 {
1960 int s = splserial();
1961 int timo;
1962
1963 int cin, stat;
1964 if (sscom_readaheadcount < MAX_READAHEAD &&
1965 sscom_rxrdy(sscomconstag, sscomconsioh)) {
1966
1967 int cn_trapped = 0;
1968 cin = sscom_getc(sscomconstag, sscomconsioh);
1969 stat = sscom_geterr(sscomconstag, sscomconsioh);
1970 cn_check_magic(dev, cin, sscom_cnm_state);
1971 sscom_readahead[sscom_readaheadcount++] = cin;
1972 }
1973
1974 /* wait for any pending transmission to finish */
1975 timo = 150000;
1976 while (ISSET(bus_space_read_2(sscomconstag, sscomconsioh, SSCOM_UFSTAT),
1977 UFSTAT_TXFULL) && --timo)
1978 continue;
1979
1980 bus_space_write_1(sscomconstag, sscomconsioh, SSCOM_UTXH, c);
1981 SSCOM_BARRIER(sscomconstag, sscomconsioh, BR | BW);
1982
1983 #if 0
1984 /* wait for this transmission to complete */
1985 timo = 1500000;
1986 while (!ISSET(bus_space_read_1(sscomconstag, sscomconsioh, SSCOM_UTRSTAT),
1987 UTRSTAT_TXEMPTY) && --timo)
1988 continue;
1989 #endif
1990 splx(s);
1991 }
1992
1993 void
1994 sscomcnpollc(dev_t dev, int on)
1995 {
1996
1997 }
1998
1999 #endif /* SSCOM0CONSOLE||SSCOM1CONSOLE */
2000
2001 #ifdef KGDB
2002 int
2003 sscom_kgdb_attach(bus_space_tag_t iot, const struct sscom_uart_info *config,
2004 int rate, int frequency, tcflag_t cflag)
2005 {
2006 int res;
2007
2008 if (iot == sscomconstag && config->unit == sscomconsunit) {
2009 printf( "console==kgdb_port (%d): kgdb disabled\n", sscomconsunit);
2010 return EBUSY; /* cannot share with console */
2011 }
2012
2013 res = sscom_init(iot, config, rate, frequency, cflag, &sscom_kgdb_ioh);
2014 if (res)
2015 return res;
2016
2017 kgdb_attach(sscom_kgdb_getc, sscom_kgdb_putc, NULL);
2018 kgdb_dev = 123; /* unneeded, only to satisfy some tests */
2019
2020 sscom_kgdb_iot = iot;
2021 sscom_kgdb_unit = config->unit;
2022
2023 return 0;
2024 }
2025
2026 /* ARGSUSED */
2027 int
2028 sscom_kgdb_getc(void *arg)
2029 {
2030 int c, stat;
2031
2032 /* block until a character becomes available */
2033 while (!sscom_rxrdy(sscom_kgdb_iot, sscom_kgdb_ioh))
2034 ;
2035
2036 c = sscom_getc(sscom_kgdb_iot, sscom_kgdb_ioh);
2037 stat = sscom_geterr(sscom_kgdb_iot, sscom_kgdb_ioh);
2038
2039 return c;
2040 }
2041
2042 /* ARGSUSED */
2043 void
2044 sscom_kgdb_putc(void *arg, int c)
2045 {
2046 int timo;
2047
2048 /* wait for any pending transmission to finish */
2049 timo = 150000;
2050 while (ISSET(bus_space_read_2(sscom_kgdb_iot, sscom_kgdb_ioh,
2051 SSCOM_UFSTAT), UFSTAT_TXFULL) && --timo)
2052 continue;
2053
2054 bus_space_write_1(sscom_kgdb_iot, sscom_kgdb_ioh, SSCOM_UTXH, c);
2055 SSCOM_BARRIER(sscom_kgdb_iot, sscom_kgdb_ioh, BR | BW);
2056
2057 #if 0
2058 /* wait for this transmission to complete */
2059 timo = 1500000;
2060 while (!ISSET(bus_space_read_1(sscom_kgdb_iot, sscom_kgdb_ioh,
2061 SSCOM_UTRSTAT), UTRSTAT_TXEMPTY) && --timo)
2062 continue;
2063 #endif
2064 }
2065 #endif /* KGDB */
2066
2067 /* helper function to identify the sscom ports used by
2068 console or KGDB (and not yet autoconf attached) */
2069 int
2070 sscom_is_console(bus_space_tag_t iot, int unit,
2071 bus_space_handle_t *ioh)
2072 {
2073 bus_space_handle_t help;
2074
2075 if (!sscomconsattached &&
2076 iot == sscomconstag && unit == sscomconsunit)
2077 help = sscomconsioh;
2078 #ifdef KGDB
2079 else if (!sscom_kgdb_attached &&
2080 iot == sscom_kgdb_iot && unit == sscom_kgdb_unit)
2081 help = sscom_kgdb_ioh;
2082 #endif
2083 else
2084 return 0;
2085
2086 if (ioh)
2087 *ioh = help;
2088 return 1;
2089 }
2090