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