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