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