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