ixp12x0_com.c revision 1.31 1 /* $NetBSD: ixp12x0_com.c,v 1.31 2007/11/19 18:51:38 ad Exp $ */
2 /*
3 * Copyright (c) 1998, 1999, 2001, 2002 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Ichiro FUKUHARA and Naoto Shimazaki.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by IWAMOTO Toshihiro.
11 *
12 * This code is derived from software contributed to The NetBSD Foundation
13 * by Charles M. Hannum.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the NetBSD
26 * Foundation, Inc. and its contributors.
27 * 4. Neither the name of The NetBSD Foundation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 /*
45 * Copyright (c) 1991 The Regents of the University of California.
46 * All rights reserved.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)com.c 7.5 (Berkeley) 5/16/91
73 */
74
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: ixp12x0_com.c,v 1.31 2007/11/19 18:51:38 ad Exp $");
77
78 #include "opt_ddb.h"
79 #include "opt_kgdb.h"
80
81 #include "rnd.h"
82 #if NRND > 0 && defined(RND_COM)
83 #include <sys/rnd.h>
84 #endif
85
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/types.h>
89 #include <sys/conf.h>
90 #include <sys/file.h>
91 #include <sys/device.h>
92 #include <sys/kernel.h>
93 #include <sys/malloc.h>
94 #include <sys/tty.h>
95 #include <sys/uio.h>
96 #include <sys/vnode.h>
97 #include <sys/kauth.h>
98
99 #include <machine/intr.h>
100 #include <machine/bus.h>
101
102 #include <arm/ixp12x0/ixp12x0_comreg.h>
103 #include <arm/ixp12x0/ixp12x0_comvar.h>
104 #include <arm/ixp12x0/ixp12x0reg.h>
105 #include <arm/ixp12x0/ixp12x0var.h>
106
107 #include <arm/ixp12x0/ixpsipvar.h>
108
109 #include <dev/cons.h>
110 #include "ixpcom.h"
111
112 static int ixpcomparam(struct tty *, struct termios *);
113 static void ixpcomstart(struct tty *);
114 static int ixpcomhwiflow(struct tty *, int);
115
116 static u_int cflag2cr(tcflag_t);
117 static void ixpcom_iflush(struct ixpcom_softc *);
118 static void ixpcom_set_cr(struct ixpcom_softc *);
119
120 int ixpcomcngetc(dev_t);
121 void ixpcomcnputc(dev_t, int);
122 void ixpcomcnpollc(dev_t, int);
123
124 static void ixpcomsoft(void* arg);
125 inline static void ixpcom_txsoft(struct ixpcom_softc *, struct tty *);
126 inline static void ixpcom_rxsoft(struct ixpcom_softc *, struct tty *);
127
128 void ixpcomcnprobe(struct consdev *);
129 void ixpcomcninit(struct consdev *);
130
131 u_int32_t ixpcom_cr = 0; /* tell cr to *_intr.c */
132 u_int32_t ixpcom_imask = 0; /* intrrupt mask from *_intr.c */
133
134
135 static struct ixpcom_cons_softc {
136 bus_space_tag_t sc_iot;
137 bus_space_handle_t sc_ioh;
138 bus_addr_t sc_baseaddr;
139 int sc_ospeed;
140 tcflag_t sc_cflag;
141 int sc_attached;
142 } ixpcomcn_sc;
143
144 static struct cnm_state ixpcom_cnm_state;
145
146 struct ixpcom_softc* ixpcom_sc = NULL;
147
148 extern struct cfdriver ixpcom_cd;
149
150 dev_type_open(ixpcomopen);
151 dev_type_close(ixpcomclose);
152 dev_type_read(ixpcomread);
153 dev_type_write(ixpcomwrite);
154 dev_type_ioctl(ixpcomioctl);
155 dev_type_stop(ixpcomstop);
156 dev_type_tty(ixpcomtty);
157 dev_type_poll(ixpcompoll);
158
159 const struct cdevsw ixpcom_cdevsw = {
160 ixpcomopen, ixpcomclose, ixpcomread, ixpcomwrite, ixpcomioctl,
161 ixpcomstop, ixpcomtty, ixpcompoll, nommap, ttykqfilter, D_TTY
162 };
163
164 struct consdev ixpcomcons = {
165 NULL, NULL, ixpcomcngetc, ixpcomcnputc, ixpcomcnpollc, NULL,
166 NULL, NULL, NODEV, CN_NORMAL
167 };
168
169 #ifndef DEFAULT_COMSPEED
170 #define DEFAULT_COMSPEED 38400
171 #endif
172
173 #define COMUNIT_MASK 0x7ffff
174 #define COMDIALOUT_MASK 0x80000
175
176 #define COMUNIT(x) (minor(x) & COMUNIT_MASK)
177 #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK)
178
179 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && \
180 device_is_active(&(sc)->sc_dev))
181
182 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
183
184 #define COM_LOCK(sc);
185 #define COM_UNLOCK(sc);
186
187 #define CFLAGS2CR_MASK (CR_PE | CR_OES | CR_SBS | CR_DSS | CR_BRD)
188
189 void
190 ixpcom_attach_subr(sc)
191 struct ixpcom_softc *sc;
192 {
193 struct tty *tp;
194
195 ixpcom_sc = sc;
196
197 /* force to use ixpcom0 for console */
198 if (sc->sc_iot == ixpcomcn_sc.sc_iot
199 && sc->sc_baseaddr == ixpcomcn_sc.sc_baseaddr) {
200 ixpcomcn_sc.sc_attached = 1;
201 sc->sc_speed = IXPCOMSPEED2BRD(ixpcomcn_sc.sc_ospeed);
202
203 /* Make sure the console is always "hardwired". */
204 /* XXX IXPCOM_SR should be checked */
205 delay(10000); /* wait for output to finish */
206 SET(sc->sc_hwflags, COM_HW_CONSOLE);
207 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
208 }
209
210 tp = ttymalloc();
211 tp->t_oproc = ixpcomstart;
212 tp->t_param = ixpcomparam;
213 tp->t_hwiflow = ixpcomhwiflow;
214
215 sc->sc_tty = tp;
216 sc->sc_rbuf = malloc(IXPCOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
217 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
218 sc->sc_rbavail = IXPCOM_RING_SIZE;
219 if (sc->sc_rbuf == NULL) {
220 printf("%s: unable to allocate ring buffer\n",
221 sc->sc_dev.dv_xname);
222 return;
223 }
224 sc->sc_ebuf = sc->sc_rbuf + (IXPCOM_RING_SIZE << 1);
225 sc->sc_tbc = 0;
226
227 sc->sc_rie = sc->sc_xie = 0;
228 ixpcom_cr = IXPCOMSPEED2BRD(DEFAULT_COMSPEED)
229 | CR_UE | sc->sc_rie | sc->sc_xie | DSS_8BIT;
230
231 tty_attach(tp);
232
233 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
234 int maj;
235
236 /* locate the major number */
237 maj = cdevsw_lookup_major(&ixpcom_cdevsw);
238
239 cn_tab->cn_dev = makedev(maj, device_unit(&sc->sc_dev));
240
241 aprint_normal("%s: console\n", sc->sc_dev.dv_xname);
242 }
243
244 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, ixpcomsoft, sc);
245
246 #if NRND > 0 && defined(RND_COM)
247 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
248 RND_TYPE_TTY, 0);
249 #endif
250
251 /* if there are no enable/disable functions, assume the device
252 is always enabled */
253 if (!sc->enable)
254 sc->enabled = 1;
255
256 /* XXX configure register */
257 /* xxx_config(sc) */
258
259 SET(sc->sc_hwflags, COM_HW_DEV_OK);
260 }
261
262 static int
263 ixpcomparam(tp, t)
264 struct tty *tp;
265 struct termios *t;
266 {
267 struct ixpcom_softc *sc
268 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
269 u_int32_t cr;
270 int s;
271
272 if (COM_ISALIVE(sc) == 0)
273 return (EIO);
274
275 cr = IXPCOMSPEED2BRD(t->c_ospeed);
276
277 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
278 return (EINVAL);
279
280 /*
281 * For the console, always force CLOCAL and !HUPCL, so that the port
282 * is always active.
283 */
284 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
285 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
286 SET(t->c_cflag, CLOCAL);
287 CLR(t->c_cflag, HUPCL);
288 }
289
290 /*
291 * If there were no changes, don't do anything. This avoids dropping
292 * input and improves performance when all we did was frob things like
293 * VMIN and VTIME.
294 */
295 if (tp->t_ospeed == t->c_ospeed &&
296 tp->t_cflag == t->c_cflag)
297 return (0);
298
299 cr |= cflag2cr(t->c_cflag);
300
301 s = splserial();
302 COM_LOCK(sc);
303
304 ixpcom_cr = (ixpcom_cr & ~CFLAGS2CR_MASK) | cr;
305
306 /*
307 * ixpcom don't have any hardware flow control.
308 * we skip it.
309 */
310
311 /* And copy to tty. */
312 tp->t_ispeed = 0;
313 tp->t_ospeed = t->c_ospeed;
314 tp->t_cflag = t->c_cflag;
315
316 if (!sc->sc_heldchange) {
317 if (sc->sc_tx_busy) {
318 sc->sc_heldtbc = sc->sc_tbc;
319 sc->sc_tbc = 0;
320 sc->sc_heldchange = 1;
321 } else
322 ixpcom_set_cr(sc);
323 }
324
325 COM_UNLOCK(sc);
326 splx(s);
327
328 /*
329 * Update the tty layer's idea of the carrier bit.
330 * We tell tty the carrier is always on.
331 */
332 (void) (*tp->t_linesw->l_modem)(tp, 1);
333
334 #ifdef COM_DEBUG
335 if (com_debug)
336 comstatus(sc, "comparam ");
337 #endif
338
339 if (!ISSET(t->c_cflag, CHWFLOW)) {
340 if (sc->sc_tx_stopped) {
341 sc->sc_tx_stopped = 0;
342 ixpcomstart(tp);
343 }
344 }
345
346 return (0);
347 }
348
349 static int
350 ixpcomhwiflow(tp, block)
351 struct tty *tp;
352 int block;
353 {
354 return (0);
355 }
356
357 static void
358 ixpcom_filltx(struct ixpcom_softc *sc)
359 {
360 bus_space_tag_t iot = sc->sc_iot;
361 bus_space_handle_t ioh = sc->sc_ioh;
362 int n;
363
364 n = 0;
365 while (bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXR) {
366 if (n >= sc->sc_tbc)
367 break;
368 bus_space_write_4(iot, ioh, IXPCOM_DR,
369 0xff & *(sc->sc_tba + n));
370 n++;
371 }
372 sc->sc_tbc -= n;
373 sc->sc_tba += n;
374 }
375
376 static void
377 ixpcomstart(tp)
378 struct tty *tp;
379 {
380 struct ixpcom_softc *sc
381 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
382 int s;
383
384 if (COM_ISALIVE(sc) == 0)
385 return;
386
387 s = spltty();
388 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
389 goto out;
390 if (sc->sc_tx_stopped)
391 goto out;
392
393 if (!ttypull(tp))
394 goto out;
395
396 /* Grab the first contiguous region of buffer space. */
397 {
398 u_char *tba;
399 int tbc;
400
401 tba = tp->t_outq.c_cf;
402 tbc = ndqb(&tp->t_outq, 0);
403
404 (void)splserial();
405 COM_LOCK(sc);
406
407 sc->sc_tba = tba;
408 sc->sc_tbc = tbc;
409 }
410
411 SET(tp->t_state, TS_BUSY);
412 sc->sc_tx_busy = 1;
413
414 /* Enable transmit completion interrupts if necessary. */
415 if (!ISSET(sc->sc_xie, CR_XIE)) {
416 SET(sc->sc_xie, CR_XIE);
417 ixpcom_set_cr(sc);
418 }
419
420 /* Output the first chunk of the contiguous buffer. */
421 ixpcom_filltx(sc);
422
423 COM_UNLOCK(sc);
424 out:
425 splx(s);
426 return;
427 }
428
429 static void
430 ixpcom_break(struct ixpcom_softc *sc, int onoff)
431 {
432 if (onoff)
433 SET(ixpcom_cr, CR_BRK);
434 else
435 CLR(ixpcom_cr, CR_BRK);
436 if (!sc->sc_heldchange) {
437 if (sc->sc_tx_busy) {
438 sc->sc_heldtbc = sc->sc_tbc;
439 sc->sc_tbc = 0;
440 sc->sc_heldchange = 1;
441 } else
442 ixpcom_set_cr(sc);
443 }
444 }
445
446 static void
447 ixpcom_shutdown(struct ixpcom_softc *sc)
448 {
449 int s;
450
451 s = splserial();
452 COM_LOCK(sc);
453
454 /* Clear any break condition set with TIOCSBRK. */
455 ixpcom_break(sc, 0);
456
457 /* Turn off interrupts. */
458 sc->sc_rie = sc->sc_xie = 0;
459 ixpcom_set_cr(sc);
460
461 if (sc->disable) {
462 #ifdef DIAGNOSTIC
463 if (!sc->enabled)
464 panic("ixpcom_shutdown: not enabled?");
465 #endif
466 (*sc->disable)(sc);
467 sc->enabled = 0;
468 }
469 COM_UNLOCK(sc);
470 splx(s);
471 }
472
473 int
474 ixpcomopen(dev, flag, mode, l)
475 dev_t dev;
476 int flag, mode;
477 struct lwp *l;
478 {
479 struct ixpcom_softc *sc;
480 struct tty *tp;
481 int s, s2;
482 int error;
483
484 sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
485 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
486 sc->sc_rbuf == NULL)
487 return (ENXIO);
488
489 if (!device_is_active(&sc->sc_dev))
490 return (ENXIO);
491
492 #ifdef KGDB
493 /*
494 * If this is the kgdb port, no other use is permitted.
495 */
496 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
497 return (EBUSY);
498 #endif
499
500 tp = sc->sc_tty;
501
502 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
503 return (EBUSY);
504
505 s = spltty();
506
507 /*
508 * Do the following iff this is a first open.
509 */
510 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
511 struct termios t;
512
513 tp->t_dev = dev;
514
515 s2 = splserial();
516 COM_LOCK(sc);
517
518 if (sc->enable) {
519 if ((*sc->enable)(sc)) {
520 COM_UNLOCK(sc);
521 splx(s2);
522 splx(s);
523 printf("%s: device enable failed\n",
524 sc->sc_dev.dv_xname);
525 return (EIO);
526 }
527 sc->enabled = 1;
528 #if 0
529 /* XXXXXXXXXXXXXXX */
530 com_config(sc);
531 #endif
532 }
533
534 /* Turn on interrupts. */
535 SET(sc->sc_rie, CR_RIE);
536 ixpcom_set_cr(sc);
537
538 #if 0
539 /* Fetch the current modem control status, needed later. */
540 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
541
542 /* Clear PPS capture state on first open. */
543 sc->sc_ppsmask = 0;
544 sc->ppsparam.mode = 0;
545 #endif
546
547 COM_UNLOCK(sc);
548 splx(s2);
549
550 /*
551 * Initialize the termios status to the defaults. Add in the
552 * sticky bits from TIOCSFLAGS.
553 */
554 t.c_ispeed = 0;
555 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
556 t.c_ospeed = ixpcomcn_sc.sc_ospeed;
557 t.c_cflag = ixpcomcn_sc.sc_cflag;
558 } else {
559 t.c_ospeed = TTYDEF_SPEED;
560 t.c_cflag = TTYDEF_CFLAG;
561 }
562 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
563 SET(t.c_cflag, CLOCAL);
564 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
565 SET(t.c_cflag, CRTSCTS);
566 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
567 SET(t.c_cflag, MDMBUF);
568 /* Make sure ixpcomparam() will do something. */
569 tp->t_ospeed = 0;
570 (void) ixpcomparam(tp, &t);
571 tp->t_iflag = TTYDEF_IFLAG;
572 tp->t_oflag = TTYDEF_OFLAG;
573 tp->t_lflag = TTYDEF_LFLAG;
574 ttychars(tp);
575 ttsetwater(tp);
576
577 s2 = splserial();
578 COM_LOCK(sc);
579
580 /* Clear the input ring, and unblock. */
581 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
582 sc->sc_rbavail = IXPCOM_RING_SIZE;
583 ixpcom_iflush(sc);
584 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
585
586 #ifdef COM_DEBUG
587 if (ixpcom_debug)
588 comstatus(sc, "ixpcomopen ");
589 #endif
590
591 COM_UNLOCK(sc);
592 splx(s2);
593 }
594
595 splx(s);
596
597 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
598 if (error)
599 goto bad;
600
601 error = (*tp->t_linesw->l_open)(dev, tp);
602 if (error)
603 goto bad;
604
605 return (0);
606
607 bad:
608 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
609 /*
610 * We failed to open the device, and nobody else had it opened.
611 * Clean up the state as appropriate.
612 */
613 ixpcom_shutdown(sc);
614 }
615
616 return (error);
617 }
618
619 int
620 ixpcomclose(dev, flag, mode, l)
621 dev_t dev;
622 int flag, mode;
623 struct lwp *l;
624 {
625 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
626 struct tty *tp = sc->sc_tty;
627
628 /* XXX This is for cons.c. */
629 if (!ISSET(tp->t_state, TS_ISOPEN))
630 return (0);
631
632 (*tp->t_linesw->l_close)(tp, flag);
633 ttyclose(tp);
634
635 if (COM_ISALIVE(sc) == 0)
636 return (0);
637
638 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
639 /*
640 * Although we got a last close, the device may still be in
641 * use; e.g. if this was the dialout node, and there are still
642 * processes waiting for carrier on the non-dialout node.
643 */
644 ixpcom_shutdown(sc);
645 }
646
647 return (0);
648 }
649
650 int
651 ixpcomread(dev, uio, flag)
652 dev_t dev;
653 struct uio *uio;
654 int flag;
655 {
656 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
657 struct tty *tp = sc->sc_tty;
658
659 if (COM_ISALIVE(sc) == 0)
660 return (EIO);
661
662 return ((*tp->t_linesw->l_read)(tp, uio, flag));
663 }
664
665 int
666 ixpcomwrite(dev, uio, flag)
667 dev_t dev;
668 struct uio *uio;
669 int flag;
670 {
671 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
672 struct tty *tp = sc->sc_tty;
673
674 if (COM_ISALIVE(sc) == 0)
675 return (EIO);
676
677 return ((*tp->t_linesw->l_write)(tp, uio, flag));
678 }
679
680 int
681 ixpcompoll(dev, events, l)
682 dev_t dev;
683 int events;
684 struct lwp *l;
685 {
686 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
687 struct tty *tp = sc->sc_tty;
688
689 if (COM_ISALIVE(sc) == 0)
690 return (EIO);
691
692 return ((*tp->t_linesw->l_poll)(tp, events, l));
693 }
694
695 struct tty *
696 ixpcomtty(dev)
697 dev_t dev;
698 {
699 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
700 struct tty *tp = sc->sc_tty;
701
702 return (tp);
703 }
704
705 int
706 ixpcomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
707 {
708 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
709 struct tty *tp = sc->sc_tty;
710 int error;
711 int s;
712
713 if (COM_ISALIVE(sc) == 0)
714 return (EIO);
715
716 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
717 if (error != EPASSTHROUGH)
718 return (error);
719
720 error = ttioctl(tp, cmd, data, flag, l);
721 if (error != EPASSTHROUGH)
722 return (error);
723
724 error = 0;
725
726 s = splserial();
727 COM_LOCK(sc);
728
729 switch (cmd) {
730 case TIOCSBRK:
731 ixpcom_break(sc, 1);
732 break;
733
734 case TIOCCBRK:
735 ixpcom_break(sc, 0);
736 break;
737
738 case TIOCGFLAGS:
739 *(int *)data = sc->sc_swflags;
740 break;
741
742 case TIOCSFLAGS:
743 error = kauth_authorize_device_tty(l->l_cred,
744 KAUTH_DEVICE_TTY_PRIVSET, tp);
745 if (error)
746 break;
747 sc->sc_swflags = *(int *)data;
748 break;
749
750 default:
751 error = EPASSTHROUGH;
752 break;
753 }
754
755 COM_UNLOCK(sc);
756 splx(s);
757
758 return (error);
759 }
760
761 /*
762 * Stop output on a line.
763 */
764 void
765 ixpcomstop(tp, flag)
766 struct tty *tp;
767 int flag;
768 {
769 struct ixpcom_softc *sc
770 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
771 int s;
772
773 s = splserial();
774 COM_LOCK(sc);
775 if (ISSET(tp->t_state, TS_BUSY)) {
776 /* Stop transmitting at the next chunk. */
777 sc->sc_tbc = 0;
778 sc->sc_heldtbc = 0;
779 if (!ISSET(tp->t_state, TS_TTSTOP))
780 SET(tp->t_state, TS_FLUSH);
781 }
782 COM_UNLOCK(sc);
783 splx(s);
784 }
785
786 static u_int
787 cflag2cr(cflag)
788 tcflag_t cflag;
789 {
790 u_int cr;
791
792 cr = (cflag & PARENB) ? CR_PE : 0;
793 cr |= (cflag & PARODD) ? CR_OES : 0;
794 cr |= (cflag & CSTOPB) ? CR_SBS : 0;
795 cr |= ((cflag & CSIZE) == CS8) ? DSS_8BIT : 0;
796
797 return (cr);
798 }
799
800 static void
801 ixpcom_iflush(sc)
802 struct ixpcom_softc *sc;
803 {
804 bus_space_tag_t iot = sc->sc_iot;
805 bus_space_handle_t ioh = sc->sc_ioh;
806 #ifdef DIAGNOSTIC
807 int reg;
808 #endif
809 int timo;
810
811 #ifdef DIAGNOSTIC
812 reg = 0xffff;
813 #endif
814 timo = 50000;
815 /* flush any pending I/O */
816 while (ISSET(bus_space_read_4(iot, ioh, IXPCOM_SR), SR_RXR)
817 && --timo)
818 #ifdef DIAGNOSTIC
819 reg =
820 #else
821 (void)
822 #endif
823 bus_space_read_4(iot, ioh, IXPCOM_DR);
824 #ifdef DIAGNOSTIC
825 if (!timo)
826 printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
827 reg);
828 #endif
829 }
830
831 static void
832 ixpcom_set_cr(struct ixpcom_softc *sc)
833 {
834 /* XXX */
835 ixpcom_cr &= ~(CR_RIE | CR_XIE);
836 ixpcom_cr |= sc->sc_rie | sc->sc_xie;
837 bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXPCOM_CR,
838 ixpcom_cr & ~ixpcom_imask);
839 }
840
841 int
842 ixpcomcnattach(iot, iobase, ioh, ospeed, cflag)
843 bus_space_tag_t iot;
844 bus_addr_t iobase;
845 bus_space_handle_t ioh;
846 int ospeed;
847 tcflag_t cflag;
848 {
849 int cr;
850
851 cn_tab = &ixpcomcons;
852 cn_init_magic(&ixpcom_cnm_state);
853 /*
854 * XXX
855 *
856 * ixpcom cannot detect a break. It can only detect framing
857 * errors. And, a sequence of "framing error, framing error"
858 * meens a break. So I made this hack:
859 *
860 * 1. Set default magic is a sequence of "BREAK, BREAK".
861 * 2. Tell cn_check_magic() a "framing error" as BREAK.
862 *
863 * see ixpcom_intr() too.
864 *
865 */
866 /* default magic is a couple of BREAK. */
867 cn_set_magic("\047\001\047\001");
868
869 ixpcomcn_sc.sc_iot = iot;
870 ixpcomcn_sc.sc_ioh = ioh;
871 ixpcomcn_sc.sc_baseaddr = iobase;
872 ixpcomcn_sc.sc_ospeed = ospeed;
873 ixpcomcn_sc.sc_cflag = cflag;
874
875 cr = cflag2cr(cflag);
876 cr |= IXPCOMSPEED2BRD(ospeed);
877 cr |= CR_UE;
878 ixpcom_cr = cr;
879
880 /* enable the UART */
881 bus_space_write_4(iot, iobase, IXPCOM_CR, cr);
882
883 return (0);
884 }
885
886 void
887 ixpcomcnprobe(cp)
888 struct consdev *cp;
889 {
890 cp->cn_pri = CN_REMOTE;
891 }
892
893 void
894 ixpcomcnpollc(dev, on)
895 dev_t dev;
896 int on;
897 {
898 }
899
900 void
901 ixpcomcnputc(dev, c)
902 dev_t dev;
903 int c;
904 {
905 int s;
906 bus_space_tag_t iot = ixpcomcn_sc.sc_iot;
907 bus_space_handle_t ioh = ixpcomcn_sc.sc_ioh;
908
909 s = splserial();
910
911 while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXR))
912 ;
913
914 bus_space_write_4(iot, ioh, IXPCOM_DR, c);
915
916 #ifdef DEBUG
917 if (c == '\r') {
918 while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXE))
919 ;
920 }
921 #endif
922
923 splx(s);
924 }
925
926 int
927 ixpcomcngetc(dev)
928 dev_t dev;
929 {
930 int c;
931 int s;
932 bus_space_tag_t iot = ixpcomcn_sc.sc_iot;
933 bus_space_handle_t ioh = ixpcomcn_sc.sc_ioh;
934
935 s = splserial();
936
937 while(!(bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_RXR))
938 ;
939
940 c = bus_space_read_4(iot, ioh, IXPCOM_DR);
941 c &= 0xff;
942 splx(s);
943
944 return (c);
945 }
946
947 inline static void
948 ixpcom_txsoft(sc, tp)
949 struct ixpcom_softc *sc;
950 struct tty *tp;
951 {
952 CLR(tp->t_state, TS_BUSY);
953 if (ISSET(tp->t_state, TS_FLUSH))
954 CLR(tp->t_state, TS_FLUSH);
955 else
956 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
957 (*tp->t_linesw->l_start)(tp);
958 }
959
960 inline static void
961 ixpcom_rxsoft(sc, tp)
962 struct ixpcom_softc *sc;
963 struct tty *tp;
964 {
965 int (*rint) __P((int, struct tty *)) = tp->t_linesw->l_rint;
966 u_char *get, *end;
967 u_int cc, scc;
968 u_char lsr;
969 int code;
970 int s;
971
972 end = sc->sc_ebuf;
973 get = sc->sc_rbget;
974 scc = cc = IXPCOM_RING_SIZE - sc->sc_rbavail;
975 #if 0
976 if (cc == IXPCOM_RING_SIZE) {
977 sc->sc_floods++;
978 if (sc->sc_errors++ == 0)
979 callout_reset(&sc->sc_diag_callout, 60 * hz,
980 comdiag, sc);
981 }
982 #endif
983 while (cc) {
984 code = get[0];
985 lsr = get[1];
986 if (ISSET(lsr, DR_ROR | DR_FRE | DR_PRE)) {
987 #if 0
988 if (ISSET(lsr, DR_ROR)) {
989 sc->sc_overflows++;
990 if (sc->sc_errors++ == 0)
991 callout_reset(&sc->sc_diag_callout,
992 60 * hz, comdiag, sc);
993 }
994 #endif
995 if (ISSET(lsr, DR_FRE))
996 SET(code, TTY_FE);
997 if (ISSET(lsr, DR_PRE))
998 SET(code, TTY_PE);
999 }
1000 if ((*rint)(code, tp) == -1) {
1001 /*
1002 * The line discipline's buffer is out of space.
1003 */
1004 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1005 /*
1006 * We're either not using flow control, or the
1007 * line discipline didn't tell us to block for
1008 * some reason. Either way, we have no way to
1009 * know when there's more space available, so
1010 * just drop the rest of the data.
1011 */
1012 get += cc << 1;
1013 if (get >= end)
1014 get -= IXPCOM_RING_SIZE << 1;
1015 cc = 0;
1016 } else {
1017 /*
1018 * Don't schedule any more receive processing
1019 * until the line discipline tells us there's
1020 * space available (through comhwiflow()).
1021 * Leave the rest of the data in the input
1022 * buffer.
1023 */
1024 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1025 }
1026 break;
1027 }
1028 get += 2;
1029 if (get >= end)
1030 get = sc->sc_rbuf;
1031 cc--;
1032 }
1033
1034 if (cc != scc) {
1035 sc->sc_rbget = get;
1036 s = splserial();
1037 COM_LOCK(sc);
1038
1039 cc = sc->sc_rbavail += scc - cc;
1040 /* Buffers should be ok again, release possible block. */
1041 if (cc >= 1) {
1042 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1043 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1044 SET(sc->sc_rie, CR_RIE);
1045 ixpcom_set_cr(sc);
1046 }
1047 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1048 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1049 #if 0
1050 com_hwiflow(sc);
1051 #endif
1052 }
1053 }
1054 COM_UNLOCK(sc);
1055 splx(s);
1056 }
1057 }
1058
1059 static void
1060 ixpcomsoft(void* arg)
1061 {
1062 struct ixpcom_softc *sc = arg;
1063
1064 if (COM_ISALIVE(sc) == 0)
1065 return;
1066
1067 if (sc->sc_rx_ready) {
1068 sc->sc_rx_ready = 0;
1069 ixpcom_rxsoft(sc, sc->sc_tty);
1070 }
1071 if (sc->sc_tx_done) {
1072 sc->sc_tx_done = 0;
1073 ixpcom_txsoft(sc, sc->sc_tty);
1074 }
1075 }
1076
1077 int
1078 ixpcomintr(void* arg)
1079 {
1080 struct ixpcom_softc *sc = arg;
1081 bus_space_tag_t iot = sc->sc_iot;
1082 bus_space_handle_t ioh = sc->sc_ioh;
1083 u_char *put, *end;
1084 u_int cc;
1085 u_int cr;
1086 u_int sr;
1087 u_int32_t c;
1088
1089 if (COM_ISALIVE(sc) == 0)
1090 return (0);
1091
1092 COM_LOCK(sc);
1093 cr = bus_space_read_4(iot, ioh, IXPCOM_CR) & CR_UE;
1094
1095 if (!cr) {
1096 COM_UNLOCK(sc);
1097 return (0);
1098 }
1099
1100 sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
1101 if (!ISSET(sr, SR_TXR | SR_RXR))
1102 return (0);
1103
1104 /*
1105 * XXX
1106 *
1107 * ixpcom cannot detect a break. It can only detect framing
1108 * errors. And, a sequence of "framing error, framing error"
1109 * meens a break. So I made this hack:
1110 *
1111 * 1. Set default magic is a sequence of "BREAK, BREAK".
1112 * 2. Tell cn_check_magic() a "framing error" as BREAK.
1113 *
1114 * see ixpcomcnattach() too.
1115 *
1116 */
1117 if (ISSET(sr, SR_FRE)) {
1118 cn_check_magic(sc->sc_tty->t_dev,
1119 CNC_BREAK, ixpcom_cnm_state);
1120 }
1121
1122 end = sc->sc_ebuf;
1123 put = sc->sc_rbput;
1124 cc = sc->sc_rbavail;
1125
1126 if (ISSET(sr, SR_RXR)) {
1127 if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1128 while (cc > 0) {
1129 if (!ISSET(sr, SR_RXR))
1130 break;
1131 c = bus_space_read_4(iot, ioh, IXPCOM_DR);
1132 if (ISSET(c, DR_FRE)) {
1133 cn_check_magic(sc->sc_tty->t_dev,
1134 CNC_BREAK,
1135 ixpcom_cnm_state);
1136 }
1137 put[0] = c & 0xff;
1138 put[1] = (c >> 8) & 0xff;
1139 cn_check_magic(sc->sc_tty->t_dev,
1140 put[0], ixpcom_cnm_state);
1141 put += 2;
1142 if (put >= end)
1143 put = sc->sc_rbuf;
1144 cc--;
1145
1146 sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
1147 }
1148
1149 /*
1150 * Current string of incoming characters ended because
1151 * no more data was available or we ran out of space.
1152 * Schedule a receive event if any data was received.
1153 * If we're out of space, turn off receive interrupts.
1154 */
1155 sc->sc_rbput = put;
1156 sc->sc_rbavail = cc;
1157 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1158 sc->sc_rx_ready = 1;
1159
1160 /*
1161 * See if we are in danger of overflowing a buffer. If
1162 * so, use hardware flow control to ease the pressure.
1163 */
1164
1165 /* but ixpcom cannot. X-( */
1166
1167 /*
1168 * If we're out of space, disable receive interrupts
1169 * until the queue has drained a bit.
1170 */
1171 if (!cc) {
1172 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1173 CLR(sc->sc_rie, CR_RIE);
1174 ixpcom_set_cr(sc);
1175 }
1176 } else {
1177 #ifdef DIAGNOSTIC
1178 panic("ixpcomintr: we shouldn't reach here");
1179 #endif
1180 CLR(sc->sc_rie, CR_RIE);
1181 ixpcom_set_cr(sc);
1182 }
1183 }
1184
1185 /*
1186 * Done handling any receive interrupts. See if data can be
1187 * transmitted as well. Schedule tx done event if no data left
1188 * and tty was marked busy.
1189 */
1190 sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
1191 if (ISSET(sr, SR_TXR)) {
1192 /*
1193 * If we've delayed a parameter change, do it now, and restart
1194 * output.
1195 */
1196 if (sc->sc_heldchange) {
1197 ixpcom_set_cr(sc);
1198 sc->sc_heldchange = 0;
1199 sc->sc_tbc = sc->sc_heldtbc;
1200 sc->sc_heldtbc = 0;
1201 }
1202
1203 /* Output the next chunk of the contiguous buffer, if any. */
1204 if (sc->sc_tbc > 0) {
1205 ixpcom_filltx(sc);
1206 } else {
1207 /* Disable transmit completion interrupts if necessary. */
1208 if (ISSET(sc->sc_xie, CR_XIE)) {
1209 CLR(sc->sc_xie, CR_XIE);
1210 ixpcom_set_cr(sc);
1211 }
1212 if (sc->sc_tx_busy) {
1213 sc->sc_tx_busy = 0;
1214 sc->sc_tx_done = 1;
1215 }
1216 }
1217 }
1218 COM_UNLOCK(sc);
1219
1220 /* Wake up the poller. */
1221 softintr_schedule(sc->sc_si);
1222
1223 #if NRND > 0 && defined(RND_COM)
1224 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1225 #endif
1226 return (1);
1227 }
1228