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