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