ixp12x0_com.c revision 1.5 1 /* $NetBSD: ixp12x0_com.c,v 1.5 2002/09/27 15:35:48 provos 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(sc)
358 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 (tp->t_outq.c_cc <= tp->t_lowat) {
394 if (ISSET(tp->t_state, TS_ASLEEP)) {
395 CLR(tp->t_state, TS_ASLEEP);
396 wakeup(&tp->t_outq);
397 }
398 selwakeup(&tp->t_wsel);
399 if (tp->t_outq.c_cc == 0)
400 goto out;
401 }
402
403 /* Grab the first contiguous region of buffer space. */
404 {
405 u_char *tba;
406 int tbc;
407
408 tba = tp->t_outq.c_cf;
409 tbc = ndqb(&tp->t_outq, 0);
410
411 (void)splserial();
412 COM_LOCK(sc);
413
414 sc->sc_tba = tba;
415 sc->sc_tbc = tbc;
416 }
417
418 SET(tp->t_state, TS_BUSY);
419 sc->sc_tx_busy = 1;
420
421 /* Enable transmit completion interrupts if necessary. */
422 if (!ISSET(sc->sc_xie, CR_XIE)) {
423 SET(sc->sc_xie, CR_XIE);
424 ixpcom_set_cr(sc);
425 }
426
427 /* Output the first chunk of the contiguous buffer. */
428 ixpcom_filltx(sc);
429
430 COM_UNLOCK(sc);
431 out:
432 splx(s);
433 return;
434 }
435
436 static void
437 ixpcom_break(sc, onoff)
438 struct ixpcom_softc *sc;
439 int onoff;
440 {
441 if (onoff)
442 SET(ixpcom_cr, CR_BRK);
443 else
444 CLR(ixpcom_cr, CR_BRK);
445 if (!sc->sc_heldchange) {
446 if (sc->sc_tx_busy) {
447 sc->sc_heldtbc = sc->sc_tbc;
448 sc->sc_tbc = 0;
449 sc->sc_heldchange = 1;
450 } else
451 ixpcom_set_cr(sc);
452 }
453 }
454
455 static void
456 ixpcom_shutdown(sc)
457 struct ixpcom_softc *sc;
458 {
459 int s;
460
461 s = splserial();
462 COM_LOCK(sc);
463
464 /* Clear any break condition set with TIOCSBRK. */
465 ixpcom_break(sc, 0);
466
467 /* Turn off interrupts. */
468 sc->sc_rie = sc->sc_xie = 0;
469 ixpcom_set_cr(sc);
470
471 if (sc->disable) {
472 #ifdef DIAGNOSTIC
473 if (!sc->enabled)
474 panic("ixpcom_shutdown: not enabled?");
475 #endif
476 (*sc->disable)(sc);
477 sc->enabled = 0;
478 }
479 COM_UNLOCK(sc);
480 splx(s);
481 }
482
483 int
484 ixpcomopen(dev, flag, mode, p)
485 dev_t dev;
486 int flag, mode;
487 struct proc *p;
488 {
489 struct ixpcom_softc *sc;
490 struct tty *tp;
491 int s, s2;
492 int error;
493
494 sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
495 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
496 sc->sc_rbuf == NULL)
497 return (ENXIO);
498
499 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
500 return (ENXIO);
501
502 #ifdef KGDB
503 /*
504 * If this is the kgdb port, no other use is permitted.
505 */
506 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
507 return (EBUSY);
508 #endif
509
510 tp = sc->sc_tty;
511
512 if (ISSET(tp->t_state, TS_ISOPEN) &&
513 ISSET(tp->t_state, TS_XCLUDE) &&
514 p->p_ucred->cr_uid != 0)
515 return (EBUSY);
516
517 s = spltty();
518
519 /*
520 * Do the following iff this is a first open.
521 */
522 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
523 struct termios t;
524
525 tp->t_dev = dev;
526
527 s2 = splserial();
528 COM_LOCK(sc);
529
530 if (sc->enable) {
531 if ((*sc->enable)(sc)) {
532 COM_UNLOCK(sc);
533 splx(s2);
534 splx(s);
535 printf("%s: device enable failed\n",
536 sc->sc_dev.dv_xname);
537 return (EIO);
538 }
539 sc->enabled = 1;
540 #if 0
541 /* XXXXXXXXXXXXXXX */
542 com_config(sc);
543 #endif
544 }
545
546 /* Turn on interrupts. */
547 SET(sc->sc_rie, CR_RIE);
548 ixpcom_set_cr(sc);
549
550 #if 0
551 /* Fetch the current modem control status, needed later. */
552 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
553
554 /* Clear PPS capture state on first open. */
555 sc->sc_ppsmask = 0;
556 sc->ppsparam.mode = 0;
557 #endif
558
559 COM_UNLOCK(sc);
560 splx(s2);
561
562 /*
563 * Initialize the termios status to the defaults. Add in the
564 * sticky bits from TIOCSFLAGS.
565 */
566 t.c_ispeed = 0;
567 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
568 t.c_ospeed = ixpcomconsrate;
569 t.c_cflag = ixpcomconscflag;
570 } else {
571 t.c_ospeed = TTYDEF_SPEED;
572 t.c_cflag = TTYDEF_CFLAG;
573 }
574 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
575 SET(t.c_cflag, CLOCAL);
576 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
577 SET(t.c_cflag, CRTSCTS);
578 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
579 SET(t.c_cflag, MDMBUF);
580 /* Make sure ixpcomparam() will do something. */
581 tp->t_ospeed = 0;
582 (void) ixpcomparam(tp, &t);
583 tp->t_iflag = TTYDEF_IFLAG;
584 tp->t_oflag = TTYDEF_OFLAG;
585 tp->t_lflag = TTYDEF_LFLAG;
586 ttychars(tp);
587 ttsetwater(tp);
588
589 s2 = splserial();
590 COM_LOCK(sc);
591
592 /* Clear the input ring, and unblock. */
593 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
594 sc->sc_rbavail = IXPCOM_RING_SIZE;
595 ixpcom_iflush(sc);
596 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
597
598 #ifdef COM_DEBUG
599 if (ixpcom_debug)
600 comstatus(sc, "ixpcomopen ");
601 #endif
602
603 COM_UNLOCK(sc);
604 splx(s2);
605 }
606
607 splx(s);
608
609 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
610 if (error)
611 goto bad;
612
613 error = (*tp->t_linesw->l_open)(dev, tp);
614 if (error)
615 goto bad;
616
617 return (0);
618
619 bad:
620 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
621 /*
622 * We failed to open the device, and nobody else had it opened.
623 * Clean up the state as appropriate.
624 */
625 ixpcom_shutdown(sc);
626 }
627
628 return (error);
629 }
630
631 int
632 ixpcomclose(dev, flag, mode, p)
633 dev_t dev;
634 int flag, mode;
635 struct proc *p;
636 {
637 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
638 struct tty *tp = sc->sc_tty;
639
640 /* XXX This is for cons.c. */
641 if (!ISSET(tp->t_state, TS_ISOPEN))
642 return (0);
643
644 (*tp->t_linesw->l_close)(tp, flag);
645 ttyclose(tp);
646
647 if (COM_ISALIVE(sc) == 0)
648 return (0);
649
650 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
651 /*
652 * Although we got a last close, the device may still be in
653 * use; e.g. if this was the dialout node, and there are still
654 * processes waiting for carrier on the non-dialout node.
655 */
656 ixpcom_shutdown(sc);
657 }
658
659 return (0);
660 }
661
662 int
663 ixpcomread(dev, uio, flag)
664 dev_t dev;
665 struct uio *uio;
666 int flag;
667 {
668 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
669 struct tty *tp = sc->sc_tty;
670
671 if (COM_ISALIVE(sc) == 0)
672 return (EIO);
673
674 return ((*tp->t_linesw->l_read)(tp, uio, flag));
675 }
676
677 int
678 ixpcomwrite(dev, uio, flag)
679 dev_t dev;
680 struct uio *uio;
681 int flag;
682 {
683 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
684 struct tty *tp = sc->sc_tty;
685
686 if (COM_ISALIVE(sc) == 0)
687 return (EIO);
688
689 return ((*tp->t_linesw->l_write)(tp, uio, flag));
690 }
691
692 int
693 ixpcompoll(dev, events, p)
694 dev_t dev;
695 int events;
696 struct proc *p;
697 {
698 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
699 struct tty *tp = sc->sc_tty;
700
701 if (COM_ISALIVE(sc) == 0)
702 return (EIO);
703
704 return ((*tp->t_linesw->l_poll)(tp, events, p));
705 }
706
707 struct tty *
708 ixpcomtty(dev)
709 dev_t dev;
710 {
711 struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
712 struct tty *tp = sc->sc_tty;
713
714 return (tp);
715 }
716
717 /*
718 * Stop output on a line.
719 */
720 void
721 ixpcomstop(tp, flag)
722 struct tty *tp;
723 int flag;
724 {
725 struct ixpcom_softc *sc
726 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
727 int s;
728
729 s = splserial();
730 COM_LOCK(sc);
731 if (ISSET(tp->t_state, TS_BUSY)) {
732 /* Stop transmitting at the next chunk. */
733 sc->sc_tbc = 0;
734 sc->sc_heldtbc = 0;
735 if (!ISSET(tp->t_state, TS_TTSTOP))
736 SET(tp->t_state, TS_FLUSH);
737 }
738 COM_UNLOCK(sc);
739 splx(s);
740 }
741
742 static u_int
743 cflag2cr(cflag)
744 tcflag_t cflag;
745 {
746 u_int cr;
747
748 cr = (cflag & PARENB) ? CR_PE : 0;
749 cr |= (cflag & PARODD) ? CR_OES : 0;
750 cr |= (cflag & CSTOPB) ? CR_SBS : 0;
751 cr |= ((cflag & CSIZE) == CS8) ? DSS_8BIT : 0;
752
753 return (cr);
754 }
755
756 static void
757 ixpcom_iflush(sc)
758 struct ixpcom_softc *sc;
759 {
760 bus_space_tag_t iot = sc->sc_iot;
761 bus_space_handle_t ioh = sc->sc_ioh;
762 #ifdef DIAGNOSTIC
763 int reg;
764 #endif
765 int timo;
766
767 #ifdef DIAGNOSTIC
768 reg = 0xffff;
769 #endif
770 timo = 50000;
771 /* flush any pending I/O */
772 while (ISSET(bus_space_read_4(iot, ioh, IXPCOM_SR), SR_RXR)
773 && --timo)
774 #ifdef DIAGNOSTIC
775 reg =
776 #else
777 (void)
778 #endif
779 bus_space_read_4(iot, ioh, IXPCOM_DR);
780 #ifdef DIAGNOSTIC
781 if (!timo)
782 printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
783 reg);
784 #endif
785 }
786
787 static void
788 ixpcom_set_cr(sc)
789 struct ixpcom_softc *sc;
790 {
791 /* XXX */
792 ixpcom_cr &= ~(CR_RIE | CR_XIE);
793 ixpcom_cr |= sc->sc_rie | sc->sc_xie;
794 bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXPCOM_CR,
795 ixpcom_cr & ~ixpcom_imask);
796 }
797
798 /* Initialization for serial console */
799 int
800 ixpcominit(iot, iobase, baud, cflag, iohp)
801 bus_space_tag_t iot;
802 bus_addr_t iobase;
803 int baud;
804 tcflag_t cflag;
805 bus_space_handle_t *iohp;
806 {
807 int cr;
808
809 if (bus_space_map(iot, iobase, IXPCOM_UART_SIZE, 0, iohp))
810 printf("register map failed\n");
811
812 cr = cflag2cr(cflag);
813 cr |= IXPCOMSPEED2BRD(baud);
814 cr |= CR_UE;
815 ixpcom_cr = cr;
816
817 /* enable the UART */
818 bus_space_write_4(iot, *iohp, IXPCOM_CR, cr);
819
820 return (0);
821 }
822
823 int
824 ixpcomcnattach(iot, iobase, rate, cflag)
825 bus_space_tag_t iot;
826 bus_addr_t iobase;
827 int rate;
828 tcflag_t cflag;
829 {
830 int res;
831
832 if ((res = ixpcominit(iot, iobase, rate, cflag, &ixpcomconsioh)))
833 return (res);
834 cn_tab = &ixpcomcons;
835 cn_init_magic(&ixpcom_cnm_state);
836 /*
837 * XXX
838 *
839 * ixpcom cannot detect a break. It can only detect framing
840 * errors. And, a sequence of "framing error, framing error"
841 * meens a break. So I made this hack:
842 *
843 * 1. Set default magic is a sequence of "BREAK, BREAK".
844 * 2. Tell cn_check_magic() a "framing error" as BREAK.
845 *
846 * see ixpcom_intr() too.
847 *
848 */
849 /* default magic is a couple of BREAK. */
850 cn_set_magic("\047\001\047\001");
851
852 ixpcomconstag = iot;
853 ixpcomconsaddr = iobase;
854 ixpcomconsrate = rate;
855 ixpcomconscflag = cflag;
856
857 return (0);
858 }
859
860 void
861 ixpcomcninit(cp)
862 struct consdev *cp;
863 {
864 printf("ixpcomcninit called\n");
865 #if 0
866 if (cp == NULL) {
867 /* XXX cp == NULL means that MMU is disabled. */
868 ixpcomconsioh = IXPCOM_UART_BASE;
869 ixpcomconstag = &ixpcom_bs_tag;
870 cn_tab = &ixpcomcons;
871
872 IXPREG(IXPCOM_UART_BASE + IXPCOM_CR)
873 = IXPCOMSPEED2BRD(DEFAULT_COMSPEED)
874 | DSS_8BIT
875 | (CR_UE | CR_RIE | CR_XIE);
876 return;
877 }
878
879 if (ixpcominit(&ixpcom_bs_tag, CONADDR, CONSPEED,
880 CONMODE, &ixpcomconsioh))
881 panic("can't init serial console @%x", CONADDR);
882 cn_tab = &ixpcomcons;
883 ixpcomconstag = &ixpcom_bs_tag;
884 #endif
885 }
886
887 void
888 ixpcomcnprobe(cp)
889 struct consdev *cp;
890 {
891 cp->cn_pri = CN_REMOTE;
892 }
893
894 void
895 ixpcomcnpollc(dev, on)
896 dev_t dev;
897 int on;
898 {
899 }
900
901 void
902 ixpcomcnputc(dev, c)
903 dev_t dev;
904 int c;
905 {
906 int s;
907
908 s = spltty(); /* XXX do we need this? */
909
910 while(!(bus_space_read_4(ixpcomconstag, ixpcomconsioh, IXPCOM_SR)
911 & SR_TXR))
912 ;
913
914 bus_space_write_4(ixpcomconstag, ixpcomconsioh, IXPCOM_DR, c);
915
916 #ifdef DEBUG
917 if (c == '\r') {
918 while(!(bus_space_read_4(ixpcomconstag, ixpcomconsioh,
919 IXPCOM_SR)
920 & SR_TXE))
921 ;
922 }
923 #endif
924
925 splx(s);
926 }
927
928 int
929 ixpcomcngetc(dev)
930 dev_t dev;
931 {
932 int c, s;
933
934 s = spltty(); /* XXX do we need this? */
935
936 while(!(bus_space_read_4(ixpcomconstag, ixpcomconsioh, IXPCOM_SR)
937 & SR_RXR))
938 ;
939
940 c = bus_space_read_4(ixpcomconstag, ixpcomconsioh, 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 c, struct tty *tp)) = 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 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1222 softintr_schedule(sc->sc_si);
1223 #else
1224 setsoftserial();
1225 #endif
1226
1227 #if NRND > 0 && defined(RND_COM)
1228 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1229 #endif
1230 return (1);
1231 }
1232
1233