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