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