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