sa11x0_com.c revision 1.12 1 /* $NetBSD: sa11x0_com.c,v 1.12 2002/10/23 09:10:47 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by IWAMOTO Toshihiro.
9 *
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Charles M. Hannum.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 /*
43 * Copyright (c) 1991 The Regents of the University of California.
44 * All rights reserved.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Berkeley and its contributors.
58 * 4. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
73 *
74 * @(#)com.c 7.5 (Berkeley) 5/16/91
75 */
76
77 #include "opt_com.h"
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
98 #include <dev/cons.h>
99
100 #include <machine/bus.h>
101 #include <arm/sa11x0/sa11x0_reg.h>
102 #include <arm/sa11x0/sa11x0_var.h>
103 #include <arm/sa11x0/sa11x0_comreg.h>
104 #include <arm/sa11x0/sa11x0_comvar.h>
105 #include <arm/sa11x0/sa11x0_gpioreg.h>
106
107 #ifdef hpcarm
108 #include <hpc/include/platid.h>
109 #include <hpc/include/platid_mask.h>
110 #endif
111
112 #include "sacom.h"
113
114 dev_type_open(sacomopen);
115 dev_type_close(sacomclose);
116 dev_type_read(sacomread);
117 dev_type_write(sacomwrite);
118 dev_type_ioctl(sacomioctl);
119 dev_type_stop(sacomstop);
120 dev_type_tty(sacomtty);
121 dev_type_poll(sacompoll);
122
123 const struct cdevsw sacom_cdevsw = {
124 sacomopen, sacomclose, sacomread, sacomwrite, sacomioctl,
125 sacomstop, sacomtty, sacompoll, nommap, ttykqfilter, D_TTY
126 };
127
128 static int sacom_match(struct device *, struct cfdata *, void *);
129 static void sacom_attach(struct device *, struct device *, void *);
130 static void sacom_filltx(struct sacom_softc *);
131 static void sacom_attach_subr(struct sacom_softc *);
132 #if defined(DDB) || defined(KGDB)
133 static void sacom_enable_debugport(struct sacom_softc *);
134 #endif
135 int sacom_detach(struct device *, int);
136 void sacom_config(struct sacom_softc *);
137 int sacom_activate(struct device *, enum devact);
138 void sacom_shutdown(struct sacom_softc *);
139 static u_int cflag2cr0(tcflag_t);
140 int sacomparam(struct tty *, struct termios *);
141 void sacomstart(struct tty *);
142 int sacomhwiflow(struct tty *, int);
143
144 void sacom_loadchannelregs(struct sacom_softc *);
145 void sacom_hwiflow(struct sacom_softc *);
146 void sacom_break(struct sacom_softc *, int);
147 void sacom_modem(struct sacom_softc *, int);
148 void tiocm_to_sacom(struct sacom_softc *, u_long, int);
149 int sacom_to_tiocm(struct sacom_softc *);
150 void sacom_iflush(struct sacom_softc *);
151
152 int sacominit(bus_space_tag_t, bus_addr_t, int, tcflag_t,
153 bus_space_handle_t *);
154 int sacom_is_console(bus_space_tag_t, bus_addr_t,
155 bus_space_handle_t *);
156
157 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
158 void sacomsoft(void *);
159 #else
160 void sacomsoft(void);
161 #endif
162
163 static inline void sacom_rxsoft(struct sacom_softc *, struct tty *);
164 static inline void sacom_txsoft(struct sacom_softc *, struct tty *);
165 static inline void sacom_stsoft(struct sacom_softc *, struct tty *);
166 static inline void sacom_schedrx(struct sacom_softc *);
167
168 #ifdef hpcarm
169 /* HPCARM specific functions */
170 static void sacom_j720_init(struct sa11x0_softc *, struct sacom_softc *);
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 ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
181
182 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
183 #define COM_LOCK(sc)
184 #define COM_UNLOCK(sc)
185
186 #define SET(t, f) (t) |= (f)
187 #define CLR(t, f) (t) &= ~(f)
188 #define ISSET(t, f) ((t) & (f))
189
190 int sacomintr(void *);
191 int sacomcngetc(dev_t);
192 void sacomcnputc(dev_t, int);
193 void sacomcnpollc(dev_t, int);
194
195 void sacomcnprobe(struct consdev *);
196 void sacomcninit(struct consdev *);
197
198 extern struct bus_space sa11x0_bs_tag;
199
200 static bus_space_tag_t sacomconstag;
201 static bus_space_handle_t sacomconsioh;
202 static bus_addr_t sacomconsaddr = SACOM3_BASE; /* XXX */
203
204 static int sacomconsattached;
205 static int sacomconsrate;
206 static tcflag_t sacomconscflag;
207
208 CFATTACH_DECL(sacom, sizeof(struct sacom_softc),
209 sacom_match, sacom_attach, NULL, NULL);
210 extern struct cfdriver sacom_cd;
211
212 #ifdef hpcarm
213 struct platid_data sacom_platid_table[] = {
214 { &platid_mask_MACH_HP_JORNADA_720, sacom_j720_init },
215 { &platid_mask_MACH_HP_JORNADA_720JP, sacom_j720_init },
216 { NULL, NULL }
217 };
218 #endif
219
220 struct consdev sacomcons = {
221 NULL, NULL, sacomcngetc, sacomcnputc, sacomcnpollc, NULL,
222 NODEV, CN_NORMAL
223 };
224
225 #ifndef CONMODE
226 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
227 #endif
228 #ifndef CONSPEED
229 #define CONSPEED 9600
230 #endif
231 #ifndef CONADDR
232 #define CONADDR SACOM3_BASE
233 #endif
234
235 static int
236 sacom_match(parent, match, aux)
237 struct device *parent;
238 struct cfdata *match;
239 void *aux;
240 {
241 return (1);
242 }
243
244 void
245 sacom_attach(parent, self, aux)
246 struct device *parent;
247 struct device *self;
248 void *aux;
249 {
250 struct sacom_softc *sc = (struct sacom_softc*)self;
251 struct sa11x0_attach_args *sa = aux;
252
253 #ifdef hpcarm
254 struct platid_data *p;
255 void (*mdinit)(struct device *, struct sacom_softc *);
256 #endif
257
258 printf("\n");
259
260 sc->sc_iot = sa->sa_iot;
261 sc->sc_baseaddr = sa->sa_addr;
262
263 if(bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
264 &sc->sc_ioh)) {
265 printf("%s: unable to map registers\n", sc->sc_dev.dv_xname);
266 return;
267 }
268
269 printf("%s: ", sc->sc_dev.dv_xname);
270 switch(sc->sc_baseaddr) {
271 case 0x80050000:
272 printf("SA11x0 UART3\n");
273 break;
274 case 0x80010000:
275 printf("SA11x0 UART1\n");
276 break;
277 case 0x80030000:
278 printf("SA11x0 UART2 (IRDA)\n");
279 break;
280 default:
281 printf("unknown SA11x0 UART\n");
282 break;
283 }
284
285 sacom_attach_subr(sc);
286
287 #ifdef hpcarm
288 /* Do hpcarm specific initialization, if any */
289 if ((p = platid_search_data(&platid, sacom_platid_table)) != NULL) {
290 mdinit = p->data;
291 (mdinit)(parent, sc);
292 }
293 #endif
294
295 sa11x0_intr_establish(0, sa->sa_intr, 1, IPL_SERIAL, sacomintr, sc);
296 }
297
298 void
299 sacom_attach_subr(sc)
300 struct sacom_softc *sc;
301 {
302 bus_addr_t iobase = sc->sc_baseaddr;
303 bus_space_tag_t iot = sc->sc_iot;
304 struct tty *tp;
305
306 #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(COM_MPLOCK)
307 simple_lock_init(&sc->sc_lock);
308 #endif
309
310 /* XXX Do we need to disable interrupts here? */
311
312 if (iot == sacomconstag && iobase == sacomconsaddr) {
313 sacomconsattached = 1;
314 sc->sc_speed = SACOMSPEED(sacomconsrate);
315
316 /* Make sure the console is always "hardwired". */
317 delay(10000); /* wait for output to finish */
318 SET(sc->sc_hwflags, COM_HW_CONSOLE);
319 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
320 }
321
322 tp = ttymalloc();
323 tp->t_oproc = sacomstart;
324 tp->t_param = sacomparam;
325 tp->t_hwiflow = sacomhwiflow;
326
327 sc->sc_tty = tp;
328 sc->sc_rbuf = malloc(SACOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
329 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
330 sc->sc_rbavail = SACOM_RING_SIZE;
331 if (sc->sc_rbuf == NULL) {
332 printf("%s: unable to allocate ring buffer\n",
333 sc->sc_dev.dv_xname);
334 return;
335 }
336 sc->sc_ebuf = sc->sc_rbuf + (SACOM_RING_SIZE << 1);
337 sc->sc_tbc = 0;
338
339 tty_attach(tp);
340
341 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
342 int maj;
343
344 /* locate the major number */
345 maj = cdevsw_lookup_major(&sacom_cdevsw);
346
347 cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
348
349 delay(10000); /* XXX */
350 printf("%s: console\n", sc->sc_dev.dv_xname);
351 delay(10000); /* XXX */
352 }
353
354
355 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
356 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, sacomsoft, sc);
357 #endif
358
359 #if NRND > 0 && defined(RND_COM)
360 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
361 RND_TYPE_TTY, 0);
362 #endif
363
364 /* if there are no enable/disable functions, assume the device
365 is always enabled */
366 if (!sc->enable)
367 sc->enabled = 1;
368
369 sacom_config(sc);
370
371 SET(sc->sc_hwflags, COM_HW_DEV_OK);
372 }
373
374 /* This is necessary when dynamically changing SAIP configuration. */
375 int
376 sacom_detach(self, flags)
377 struct device *self;
378 int flags;
379 {
380 struct sacom_softc *sc = (struct sacom_softc *)self;
381 int maj, mn;
382
383 /* locate the major number */
384 maj = cdevsw_lookup_major(&sacom_cdevsw);
385
386 /* Nuke the vnodes for any open instances. */
387 mn = self->dv_unit;
388 vdevgone(maj, mn, mn, VCHR);
389
390 mn |= COMDIALOUT_MASK;
391 vdevgone(maj, mn, mn, VCHR);
392
393 /* Free the receive buffer. */
394 free(sc->sc_rbuf, M_DEVBUF);
395
396 /* Detach and free the tty. */
397 tty_detach(sc->sc_tty);
398 ttyfree(sc->sc_tty);
399
400 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
401 /* Unhook the soft interrupt handler. */
402 softintr_disestablish(sc->sc_si);
403 #endif
404
405 #if NRND > 0 && defined(RND_COM)
406 /* Unhook the entropy source. */
407 rnd_detach_source(&sc->rnd_source);
408 #endif
409
410 return (0);
411 }
412
413 void
414 sacom_config(sc)
415 struct sacom_softc *sc;
416 {
417 bus_space_tag_t iot = sc->sc_iot;
418 bus_space_handle_t ioh = sc->sc_ioh;
419
420 /* Disable engine before configuring the device. */
421 sc->sc_cr3 = 0;
422 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
423
424 #ifdef DDB
425 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
426 sacom_enable_debugport(sc);
427 #endif
428 }
429
430 #ifdef DDB
431 static void
432 sacom_enable_debugport(sc)
433 struct sacom_softc *sc;
434 {
435 bus_space_tag_t iot = sc->sc_iot;
436 bus_space_handle_t ioh = sc->sc_ioh;
437 int s;
438
439 s = splserial();
440 COM_LOCK(sc);
441 sc->sc_cr3 = CR3_RXE | CR3_TXE;
442 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
443 COM_UNLOCK(sc);
444 splx(s);
445 }
446 #endif
447
448 int
449 sacom_activate(self, act)
450 struct device *self;
451 enum devact act;
452 {
453 struct sacom_softc *sc = (struct sacom_softc *)self;
454 int s, rv = 0;
455
456 s = splserial();
457 COM_LOCK(sc);
458 switch (act) {
459 case DVACT_ACTIVATE:
460 rv = EOPNOTSUPP;
461 break;
462
463 case DVACT_DEACTIVATE:
464 if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB)) {
465 rv = EBUSY;
466 break;
467 }
468
469 if (sc->disable != NULL && sc->enabled != 0) {
470 (*sc->disable)(sc);
471 sc->enabled = 0;
472 }
473 break;
474 }
475
476 COM_UNLOCK(sc);
477 splx(s);
478 return (rv);
479 }
480
481 void
482 sacom_shutdown(sc)
483 struct sacom_softc *sc;
484 {
485 struct tty *tp = sc->sc_tty;
486 int s;
487
488 s = splserial();
489 COM_LOCK(sc);
490
491 /* Clear any break condition set with TIOCSBRK. */
492 sacom_break(sc, 0);
493
494 /*
495 * Hang up if necessary. Wait a bit, so the other side has time to
496 * notice even if we immediately open the port again.
497 * Avoid tsleeping above splhigh().
498 */
499 if (ISSET(tp->t_cflag, HUPCL)) {
500 sacom_modem(sc, 0);
501 COM_UNLOCK(sc);
502 splx(s);
503 /* XXX tsleep will only timeout */
504 (void) tsleep(sc, TTIPRI, ttclos, hz);
505 s = splserial();
506 COM_LOCK(sc);
507 }
508
509 /* Turn off interrupts. */
510 sc->sc_cr3 = 0;
511 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3, sc->sc_cr3);
512
513 if (sc->disable) {
514 #ifdef DIAGNOSTIC
515 if (!sc->enabled)
516 panic("sacom_shutdown: not enabled?");
517 #endif
518 (*sc->disable)(sc);
519 sc->enabled = 0;
520 }
521 COM_UNLOCK(sc);
522 splx(s);
523 }
524
525 int
526 sacomopen(dev, flag, mode, p)
527 dev_t dev;
528 int flag, mode;
529 struct proc *p;
530 {
531 struct sacom_softc *sc;
532 struct tty *tp;
533 int s, s2;
534 int error;
535
536 sc = device_lookup(&sacom_cd, COMUNIT(dev));
537 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
538 sc->sc_rbuf == NULL)
539 return (ENXIO);
540
541 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
542 return (ENXIO);
543
544 tp = sc->sc_tty;
545
546 if (ISSET(tp->t_state, TS_ISOPEN) &&
547 ISSET(tp->t_state, TS_XCLUDE) &&
548 p->p_ucred->cr_uid != 0)
549 return (EBUSY);
550
551 s = spltty();
552
553 /*
554 * Do the following iff this is a first open.
555 */
556 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
557 struct termios t;
558
559 tp->t_dev = dev;
560
561 s2 = splserial();
562 COM_LOCK(sc);
563
564 if (sc->enable) {
565 if ((*sc->enable)(sc)) {
566 COM_UNLOCK(sc);
567 splx(s2);
568 splx(s);
569 printf("%s: device enable failed\n",
570 sc->sc_dev.dv_xname);
571 return (EIO);
572 }
573 sc->enabled = 1;
574 sacom_config(sc);
575 }
576
577 /* Turn on interrupts. */
578 sc->sc_cr3 = CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE;
579 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3,
580 sc->sc_cr3);
581
582
583 COM_UNLOCK(sc);
584 splx(s2);
585
586 /*
587 * Initialize the termios status to the defaults. Add in the
588 * sticky bits from TIOCSFLAGS.
589 */
590 t.c_ispeed = 0;
591 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
592 t.c_ospeed = sacomconsrate;
593 t.c_cflag = sacomconscflag;
594 } else {
595 t.c_ospeed = TTYDEF_SPEED;
596 t.c_cflag = TTYDEF_CFLAG;
597 }
598 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
599 SET(t.c_cflag, CLOCAL);
600 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
601 SET(t.c_cflag, CRTSCTS);
602 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
603 SET(t.c_cflag, MDMBUF);
604 /* Make sure sacomparam() will do something. */
605 tp->t_ospeed = 0;
606 (void) sacomparam(tp, &t);
607 tp->t_iflag = TTYDEF_IFLAG;
608 tp->t_oflag = TTYDEF_OFLAG;
609 tp->t_lflag = TTYDEF_LFLAG;
610 ttychars(tp);
611 ttsetwater(tp);
612
613 s2 = splserial();
614 COM_LOCK(sc);
615
616 /*
617 * Turn on DTR. We must always do this, even if carrier is not
618 * present, because otherwise we'd have to use TIOCSDTR
619 * immediately after setting CLOCAL, which applications do not
620 * expect. We always assert DTR while the device is open
621 * unless explicitly requested to deassert it.
622 */
623 sacom_modem(sc, 1);
624
625 /* Clear the input ring, and unblock. */
626 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
627 sc->sc_rbavail = SACOM_RING_SIZE;
628 sacom_iflush(sc);
629 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
630 sacom_hwiflow(sc);
631
632 #ifdef COM_DEBUG
633 if (sacom_debug)
634 comstatus(sc, "sacomopen ");
635 #endif
636
637 COM_UNLOCK(sc);
638 splx(s2);
639 }
640
641 splx(s);
642
643 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
644 if (error)
645 goto bad;
646
647 error = (*tp->t_linesw->l_open)(dev, tp);
648 if (error)
649 goto bad;
650
651 return (0);
652
653 bad:
654 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
655 /*
656 * We failed to open the device, and nobody else had it opened.
657 * Clean up the state as appropriate.
658 */
659 sacom_shutdown(sc);
660 }
661
662 return (error);
663 }
664
665 int
666 sacomclose(dev, flag, mode, p)
667 dev_t dev;
668 int flag, mode;
669 struct proc *p;
670 {
671 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
672 struct tty *tp = sc->sc_tty;
673
674 /* XXX This is for cons.c. */
675 if (!ISSET(tp->t_state, TS_ISOPEN))
676 return (0);
677
678 (*tp->t_linesw->l_close)(tp, flag);
679 ttyclose(tp);
680
681 if (COM_ISALIVE(sc) == 0)
682 return (0);
683
684 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
685 /*
686 * Although we got a last close, the device may still be in
687 * use; e.g. if this was the dialout node, and there are still
688 * processes waiting for carrier on the non-dialout node.
689 */
690 sacom_shutdown(sc);
691 }
692
693 return (0);
694 }
695
696 int
697 sacomread(dev, uio, flag)
698 dev_t dev;
699 struct uio *uio;
700 int flag;
701 {
702 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
703 struct tty *tp = sc->sc_tty;
704
705 if (COM_ISALIVE(sc) == 0)
706 return (EIO);
707
708 return ((*tp->t_linesw->l_read)(tp, uio, flag));
709 }
710
711 int
712 sacomwrite(dev, uio, flag)
713 dev_t dev;
714 struct uio *uio;
715 int flag;
716 {
717 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
718 struct tty *tp = sc->sc_tty;
719
720 if (COM_ISALIVE(sc) == 0)
721 return (EIO);
722
723 return ((*tp->t_linesw->l_write)(tp, uio, flag));
724 }
725
726 int
727 sacompoll(dev, events, p)
728 dev_t dev;
729 int events;
730 struct proc *p;
731 {
732 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
733 struct tty *tp = sc->sc_tty;
734
735 if (COM_ISALIVE(sc) == 0)
736 return (EIO);
737
738 return ((*tp->t_linesw->l_poll)(tp, events, p));
739 }
740
741 struct tty *
742 sacomtty(dev)
743 dev_t dev;
744 {
745 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
746 struct tty *tp = sc->sc_tty;
747
748 return (tp);
749 }
750
751 int
752 sacomioctl(dev, cmd, data, flag, p)
753 dev_t dev;
754 u_long cmd;
755 caddr_t data;
756 int flag;
757 struct proc *p;
758 {
759 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(dev));
760 struct tty *tp = sc->sc_tty;
761 int error;
762 int s;
763
764 if (COM_ISALIVE(sc) == 0)
765 return (EIO);
766
767 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
768 if (error != EPASSTHROUGH)
769 return (error);
770
771 error = ttioctl(tp, cmd, data, flag, p);
772 if (error != EPASSTHROUGH)
773 return (error);
774
775 error = 0;
776
777 s = splserial();
778 COM_LOCK(sc);
779
780 switch (cmd) {
781 case TIOCSBRK:
782 sacom_break(sc, 1);
783 break;
784
785 case TIOCCBRK:
786 sacom_break(sc, 0);
787 break;
788
789 case TIOCSDTR:
790 sacom_modem(sc, 1);
791 break;
792
793 case TIOCCDTR:
794 sacom_modem(sc, 0);
795 break;
796
797 case TIOCGFLAGS:
798 *(int *)data = sc->sc_swflags;
799 break;
800
801 case TIOCSFLAGS:
802 error = suser(p->p_ucred, &p->p_acflag);
803 if (error)
804 break;
805 sc->sc_swflags = *(int *)data;
806 break;
807
808 case TIOCMSET:
809 case TIOCMBIS:
810 case TIOCMBIC:
811 tiocm_to_sacom(sc, cmd, *(int *)data);
812 break;
813
814 case TIOCMGET:
815 *(int *)data = sacom_to_tiocm(sc);
816 break;
817
818 default:
819 error = EPASSTHROUGH;
820 break;
821 }
822
823 COM_UNLOCK(sc);
824 splx(s);
825
826 #ifdef COM_DEBUG
827 if (sacom_debug)
828 comstatus(sc, "comioctl ");
829 #endif
830
831 return (error);
832 }
833
834 static inline void
835 sacom_schedrx(sc)
836 struct sacom_softc *sc;
837 {
838
839 sc->sc_rx_ready = 1;
840
841 /* Wake up the poller. */
842 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
843 softintr_schedule(sc->sc_si);
844 #else
845 setsoftserial();
846 #endif
847 }
848
849 void
850 sacom_break(sc, onoff)
851 struct sacom_softc *sc;
852 int onoff;
853 {
854
855 if (onoff)
856 SET(sc->sc_cr3, CR3_BRK);
857 else
858 CLR(sc->sc_cr3, CR3_BRK);
859
860 if (!sc->sc_heldchange) {
861 if (sc->sc_tx_busy) {
862 sc->sc_heldtbc = sc->sc_tbc;
863 sc->sc_tbc = 0;
864 sc->sc_heldchange = 1;
865 } else
866 sacom_loadchannelregs(sc);
867 }
868 }
869
870 void
871 sacom_modem(sc, onoff)
872 struct sacom_softc *sc;
873 int onoff;
874 {
875 if (!sc->sc_heldchange) {
876 if (sc->sc_tx_busy) {
877 sc->sc_heldtbc = sc->sc_tbc;
878 sc->sc_tbc = 0;
879 sc->sc_heldchange = 1;
880 } else
881 sacom_loadchannelregs(sc);
882 }
883 }
884
885 void
886 tiocm_to_sacom(sc, how, ttybits)
887 struct sacom_softc *sc;
888 u_long how;
889 int ttybits;
890 {
891 }
892
893 int
894 sacom_to_tiocm(sc)
895 struct sacom_softc *sc;
896 {
897 int ttybits = 0;
898
899 if (sc->sc_cr3 != 0)
900 SET(ttybits, TIOCM_LE);
901
902 return (ttybits);
903 }
904
905 static u_int
906 cflag2cr0(cflag)
907 tcflag_t cflag;
908 {
909 u_int cr0;
910
911 cr0 = (cflag & PARENB) ? CR0_PE : 0;
912 cr0 |= (cflag & PARODD) ? 0 : CR0_OES;
913 cr0 |= (cflag & CSTOPB) ? CR0_SBS : 0;
914 cr0 |= ((cflag & CSIZE) == CS8) ? CR0_DSS : 0;
915
916 return (cr0);
917 }
918
919 int
920 sacomparam(tp, t)
921 struct tty *tp;
922 struct termios *t;
923 {
924 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
925 int ospeed = SACOMSPEED(t->c_ospeed);
926 u_int cr0;
927 int s;
928
929 if (COM_ISALIVE(sc) == 0)
930 return (EIO);
931
932 /* Check requested parameters. */
933 if (ospeed < 0)
934 return (EINVAL);
935 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
936 return (EINVAL);
937
938 /*
939 * For the console, always force CLOCAL and !HUPCL, so that the port
940 * is always active.
941 */
942 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
943 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
944 SET(t->c_cflag, CLOCAL);
945 CLR(t->c_cflag, HUPCL);
946 }
947
948 /*
949 * If there were no changes, don't do anything. This avoids dropping
950 * input and improves performance when all we did was frob things like
951 * VMIN and VTIME.
952 */
953 if (tp->t_ospeed == t->c_ospeed &&
954 tp->t_cflag == t->c_cflag)
955 return (0);
956
957 cr0 = cflag2cr0(t->c_cflag);
958
959 s = splserial();
960 COM_LOCK(sc);
961
962 sc->sc_cr0 = cr0;
963
964 sc->sc_speed = ospeed;
965
966 /* And copy to tty. */
967 tp->t_ispeed = 0;
968 tp->t_ospeed = t->c_ospeed;
969 tp->t_cflag = t->c_cflag;
970
971 if (!sc->sc_heldchange) {
972 if (sc->sc_tx_busy) {
973 sc->sc_heldtbc = sc->sc_tbc;
974 sc->sc_tbc = 0;
975 sc->sc_heldchange = 1;
976 } else
977 sacom_loadchannelregs(sc);
978 }
979
980 if (!ISSET(t->c_cflag, CHWFLOW)) {
981 /* Disable the high water mark. */
982 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
983 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
984 sacom_schedrx(sc);
985 }
986 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
987 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
988 sacom_hwiflow(sc);
989 }
990 }
991
992 COM_UNLOCK(sc);
993 splx(s);
994
995 (void) (*tp->t_linesw->l_modem)(tp, 1);
996
997 #ifdef COM_DEBUG
998 if (sacom_debug)
999 comstatus(sc, "comparam ");
1000 #endif
1001
1002 return (0);
1003 }
1004
1005 void
1006 sacom_iflush(sc)
1007 struct sacom_softc *sc;
1008 {
1009 bus_space_tag_t iot = sc->sc_iot;
1010 bus_space_handle_t ioh = sc->sc_ioh;
1011 #ifdef DIAGNOSTIC
1012 int reg;
1013 #endif
1014 int timo;
1015
1016 #ifdef DIAGNOSTIC
1017 reg = 0xffff;
1018 #endif
1019 timo = 50;
1020 /* flush any pending I/O */
1021 if (sc->sc_cr3 & CR3_RXE) {
1022 while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_RNE)
1023 && --timo)
1024 #ifdef DIAGNOSTIC
1025 reg =
1026 #else
1027 (void)
1028 #endif
1029 bus_space_read_4(iot, ioh, SACOM_DR);
1030 }
1031 #if 0
1032 /* XXX is it good idea to wait TX finish? */
1033 if (sc->sc_cr3 & CR3_TXE) {
1034 timo = 500;
1035 while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_TBY)
1036 && --timo)
1037 delay(100);
1038 }
1039 #endif
1040 #ifdef DIAGNOSTIC
1041 if (!timo)
1042 printf("%s: sacom_iflush timeout %02x\n", sc->sc_dev.dv_xname,
1043 reg);
1044 #endif
1045 }
1046
1047 void
1048 sacom_loadchannelregs(sc)
1049 struct sacom_softc *sc;
1050 {
1051 bus_space_tag_t iot = sc->sc_iot;
1052 bus_space_handle_t ioh = sc->sc_ioh;
1053
1054 /* XXXXX necessary? */
1055 sacom_iflush(sc);
1056
1057 /* Need to stop engines first. */
1058 bus_space_write_4(iot, ioh, SACOM_CR3, 0);
1059
1060 bus_space_write_4(iot, ioh, SACOM_CR1, sc->sc_speed >> 8);
1061 bus_space_write_4(iot, ioh, SACOM_CR2, sc->sc_speed & 0xff);
1062
1063 bus_space_write_4(iot, ioh, SACOM_CR0, sc->sc_cr0);
1064 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
1065 }
1066
1067 int
1068 sacomhwiflow(tp, block)
1069 struct tty *tp;
1070 int block;
1071 {
1072 #if 0
1073 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
1074 int s;
1075
1076 if (COM_ISALIVE(sc) == 0)
1077 return (0);
1078
1079 if (sc->sc_mcr_rts == 0)
1080 return (0);
1081
1082 s = splserial();
1083 COM_LOCK(sc);
1084
1085 if (block) {
1086 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1087 SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
1088 sacom_hwiflow(sc);
1089 }
1090 } else {
1091 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1092 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1093 sacom_schedrx(sc);
1094 }
1095 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1096 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
1097 sacom_hwiflow(sc);
1098 }
1099 }
1100
1101 COM_UNLOCK(sc);
1102 splx(s);
1103 #endif
1104 return (1);
1105 }
1106
1107 /*
1108 * (un)block input via hw flowcontrol
1109 */
1110 void
1111 sacom_hwiflow(sc)
1112 struct sacom_softc *sc;
1113 {
1114 #if 0
1115 bus_space_tag_t iot = sc->sc_iot;
1116 bus_space_handle_t ioh = sc->sc_ioh;
1117
1118 /* XXX implement */
1119 #endif
1120 }
1121
1122
1123 void
1124 sacomstart(tp)
1125 struct tty *tp;
1126 {
1127 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
1128 bus_space_tag_t iot = sc->sc_iot;
1129 bus_space_handle_t ioh = sc->sc_ioh;
1130 int s;
1131
1132 if (COM_ISALIVE(sc) == 0)
1133 return;
1134
1135 s = spltty();
1136 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1137 goto out;
1138
1139 if (tp->t_outq.c_cc <= tp->t_lowat) {
1140 if (ISSET(tp->t_state, TS_ASLEEP)) {
1141 CLR(tp->t_state, TS_ASLEEP);
1142 wakeup(&tp->t_outq);
1143 }
1144 selwakeup(&tp->t_wsel);
1145 if (tp->t_outq.c_cc == 0)
1146 goto out;
1147 }
1148
1149 /* Grab the first contiguous region of buffer space. */
1150 {
1151 u_char *tba;
1152 int tbc;
1153
1154 tba = tp->t_outq.c_cf;
1155 tbc = ndqb(&tp->t_outq, 0);
1156
1157 (void)splserial();
1158 COM_LOCK(sc);
1159
1160 sc->sc_tba = tba;
1161 sc->sc_tbc = tbc;
1162 }
1163
1164 SET(tp->t_state, TS_BUSY);
1165 sc->sc_tx_busy = 1;
1166
1167 /* Enable transmit completion interrupts if necessary. */
1168 if (!ISSET(sc->sc_cr3, CR3_TIE)) {
1169 SET(sc->sc_cr3, CR3_TIE);
1170 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
1171 }
1172
1173 /* Output the first chunk of the contiguous buffer. */
1174 sacom_filltx(sc);
1175
1176 COM_UNLOCK(sc);
1177 out:
1178 splx(s);
1179 return;
1180 }
1181
1182 void
1183 sacom_filltx(sc)
1184 struct sacom_softc *sc;
1185 {
1186 bus_space_tag_t iot = sc->sc_iot;
1187 bus_space_handle_t ioh = sc->sc_ioh;
1188 int c, n;
1189
1190 n = 0;
1191 while(bus_space_read_4(iot, ioh, SACOM_SR1)
1192 & SR1_TNF) {
1193 if (n == SACOM_TXFIFOLEN || n == sc->sc_tbc)
1194 break;
1195 c = *(sc->sc_tba + n);
1196 c &= 0xff;
1197 bus_space_write_4(iot, ioh, SACOM_DR, c);
1198 n++;
1199 }
1200 sc->sc_tbc -= n;
1201 sc->sc_tba += n;
1202 }
1203
1204 /*
1205 * Stop output on a line.
1206 */
1207 void
1208 sacomstop(tp, flag)
1209 struct tty *tp;
1210 int flag;
1211 {
1212 struct sacom_softc *sc = device_lookup(&sacom_cd, COMUNIT(tp->t_dev));
1213 int s;
1214
1215 s = splserial();
1216 COM_LOCK(sc);
1217 if (ISSET(tp->t_state, TS_BUSY)) {
1218 /* Stop transmitting at the next chunk. */
1219 sc->sc_tbc = 0;
1220 sc->sc_heldtbc = 0;
1221 if (!ISSET(tp->t_state, TS_TTSTOP))
1222 SET(tp->t_state, TS_FLUSH);
1223 }
1224 COM_UNLOCK(sc);
1225 splx(s);
1226 }
1227
1228 static inline void
1229 sacom_rxsoft(sc, tp)
1230 struct sacom_softc *sc;
1231 struct tty *tp;
1232 {
1233 int (*rint)(int c, struct tty *tp) = tp->t_linesw->l_rint;
1234 u_char *get, *end;
1235 u_int cc, scc;
1236 u_char sr1;
1237 int code;
1238 int s;
1239
1240 end = sc->sc_ebuf;
1241 get = sc->sc_rbget;
1242 scc = cc = SACOM_RING_SIZE - sc->sc_rbavail;
1243
1244 while (cc) {
1245 code = get[0];
1246 sr1 = get[1];
1247 if (ISSET(sr1, SR1_FRE))
1248 SET(code, TTY_FE);
1249 if (ISSET(sr1, SR1_PRE))
1250 SET(code, TTY_PE);
1251 if ((*rint)(code, tp) == -1) {
1252 /*
1253 * The line discipline's buffer is out of space.
1254 */
1255 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1256 /*
1257 * We're either not using flow control, or the
1258 * line discipline didn't tell us to block for
1259 * some reason. Either way, we have no way to
1260 * know when there's more space available, so
1261 * just drop the rest of the data.
1262 */
1263 get += cc << 1;
1264 if (get >= end)
1265 get -= SACOM_RING_SIZE << 1;
1266 cc = 0;
1267 } else {
1268 /*
1269 * Don't schedule any more receive processing
1270 * until the line discipline tells us there's
1271 * space available (through comhwiflow()).
1272 * Leave the rest of the data in the input
1273 * buffer.
1274 */
1275 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1276 }
1277 break;
1278 }
1279 get += 2;
1280 if (get >= end)
1281 get = sc->sc_rbuf;
1282 cc--;
1283 }
1284
1285 if (cc != scc) {
1286 sc->sc_rbget = get;
1287 s = splserial();
1288 COM_LOCK(sc);
1289
1290 cc = sc->sc_rbavail += scc - cc;
1291 /* Buffers should be ok again, release possible block. */
1292 if (cc >= 1) {
1293 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1294 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1295 SET(sc->sc_cr3, CR3_RIE);
1296 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
1297 SACOM_CR3, sc->sc_cr3);
1298 }
1299 }
1300 COM_UNLOCK(sc);
1301 splx(s);
1302 }
1303 }
1304
1305 static inline void
1306 sacom_txsoft(sc, tp)
1307 struct sacom_softc *sc;
1308 struct tty *tp;
1309 {
1310
1311 CLR(tp->t_state, TS_BUSY);
1312 if (ISSET(tp->t_state, TS_FLUSH))
1313 CLR(tp->t_state, TS_FLUSH);
1314 else
1315 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1316 (*tp->t_linesw->l_start)(tp);
1317 }
1318
1319 static inline void
1320 sacom_stsoft(sc, tp)
1321 struct sacom_softc *sc;
1322 struct tty *tp;
1323 {
1324 }
1325
1326 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1327 void
1328 sacomsoft(arg)
1329 void *arg;
1330 {
1331 struct sacom_softc *sc = arg;
1332 struct tty *tp;
1333
1334 if (COM_ISALIVE(sc) == 0)
1335 return;
1336
1337 {
1338 #else
1339 void
1340 sacomsoft()
1341 {
1342 struct sacom_softc *sc;
1343 struct tty *tp;
1344 int unit;
1345
1346 for (unit = 0; unit < sacom_cd.cd_ndevs; unit++) {
1347 sc = device_lookup(&sacom_cd, unit);
1348 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
1349 continue;
1350
1351 if (COM_ISALIVE(sc) == 0)
1352 continue;
1353
1354 tp = sc->sc_tty;
1355 if (tp == NULL)
1356 continue;
1357 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
1358 continue;
1359 #endif
1360 tp = sc->sc_tty;
1361
1362 if (sc->sc_rx_ready) {
1363 sc->sc_rx_ready = 0;
1364 sacom_rxsoft(sc, tp);
1365 }
1366
1367 if (sc->sc_st_check) {
1368 sc->sc_st_check = 0;
1369 sacom_stsoft(sc, tp);
1370 }
1371
1372 if (sc->sc_tx_done) {
1373 sc->sc_tx_done = 0;
1374 sacom_txsoft(sc, tp);
1375 }
1376 }
1377
1378 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
1379 #endif
1380 }
1381
1382 #ifdef __ALIGN_BRACKET_LEVEL_FOR_CTAGS
1383 /* there has got to be a better way to do comsoft() */
1384 }}
1385 #endif
1386
1387 int
1388 sacomintr(arg)
1389 void *arg;
1390 {
1391 struct sacom_softc *sc = arg;
1392 bus_space_tag_t iot = sc->sc_iot;
1393 bus_space_handle_t ioh = sc->sc_ioh;
1394 u_char *put, *end;
1395 u_int cc;
1396 u_int sr0, sr1;
1397
1398 if (COM_ISALIVE(sc) == 0)
1399 return (0);
1400
1401 COM_LOCK(sc);
1402 sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
1403 if (! sr0) {
1404 COM_UNLOCK(sc);
1405 return (0);
1406 }
1407 if (ISSET(sr0, SR0_EIF))
1408 /* XXX silently discard error bits */
1409 bus_space_read_4(iot, ioh, SACOM_DR);
1410 if (ISSET(sr0, SR0_RBB))
1411 bus_space_write_4(iot, ioh, SACOM_SR0, SR0_RBB);
1412 if (ISSET(sr0, SR0_REB)) {
1413 bus_space_write_4(iot, ioh, SACOM_SR0, SR0_REB);
1414 #if defined(DDB) || defined(KGDB)
1415 #ifndef DDB_BREAK_CHAR
1416 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1417 console_debugger();
1418 }
1419 #endif
1420 #endif /* DDB || KGDB */
1421 }
1422
1423
1424 end = sc->sc_ebuf;
1425 put = sc->sc_rbput;
1426 cc = sc->sc_rbavail;
1427
1428 sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
1429 if (ISSET(sr0, SR0_RFS | SR0_RID)) {
1430 if (! ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1431 while (cc > 0) {
1432 if (!ISSET(sr1, SR1_RNE)) {
1433 bus_space_write_4(iot, ioh, SACOM_SR0,
1434 SR0_RID);
1435 break;
1436 }
1437 put[0] = bus_space_read_4(iot, ioh, SACOM_DR);
1438 put[1] = sr1;
1439 #if defined(DDB) && defined(DDB_BREAK_CHAR)
1440 if (put[0] == DDB_BREAK_CHAR &&
1441 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1442 console_debugger();
1443
1444 sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
1445 continue;
1446 }
1447 #endif
1448 put += 2;
1449 if (put >= end)
1450 put = sc->sc_rbuf;
1451 cc--;
1452
1453 sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
1454 }
1455
1456 /*
1457 * Current string of incoming characters ended because
1458 * no more data was available or we ran out of space.
1459 * Schedule a receive event if any data was received.
1460 * If we're out of space, turn off receive interrupts.
1461 */
1462 sc->sc_rbput = put;
1463 sc->sc_rbavail = cc;
1464 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1465 sc->sc_rx_ready = 1;
1466
1467 /* XXX do RX hardware flow control */
1468
1469 /*
1470 * If we're out of space, disable receive interrupts
1471 * until the queue has drained a bit.
1472 */
1473 if (!cc) {
1474 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1475 CLR(sc->sc_cr3, CR3_RIE);
1476 bus_space_write_4(iot, ioh, SACOM_CR3,
1477 sc->sc_cr3);
1478 }
1479 } else {
1480 #ifdef DIAGNOSTIC
1481 panic("sacomintr: we shouldn't reach here");
1482 #endif
1483 CLR(sc->sc_cr3, CR3_RIE);
1484 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
1485 }
1486 }
1487
1488 /*
1489 * Done handling any receive interrupts. See if data can be
1490 * transmitted as well. Schedule tx done event if no data left
1491 * and tty was marked busy.
1492 */
1493 sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
1494 if (ISSET(sr0, SR0_TFS)) {
1495 /*
1496 * If we've delayed a parameter change, do it now, and restart
1497 * output.
1498 * XXX sacom_loadchanelregs() waits TX completion,
1499 * XXX resulting in ~0.1s hang (300bps, 4 bytes) in worst case
1500 */
1501 if (sc->sc_heldchange) {
1502 sacom_loadchannelregs(sc);
1503 sc->sc_heldchange = 0;
1504 sc->sc_tbc = sc->sc_heldtbc;
1505 sc->sc_heldtbc = 0;
1506 }
1507
1508 /* Output the next chunk of the contiguous buffer, if any. */
1509 if (sc->sc_tbc > 0) {
1510 sacom_filltx(sc);
1511 } else {
1512 /* Disable transmit completion interrupts if necessary. */
1513 if (ISSET(sc->sc_cr3, CR3_TIE)) {
1514 CLR(sc->sc_cr3, CR3_TIE);
1515 bus_space_write_4(iot, ioh, SACOM_CR3,
1516 sc->sc_cr3);
1517 }
1518 if (sc->sc_tx_busy) {
1519 sc->sc_tx_busy = 0;
1520 sc->sc_tx_done = 1;
1521 }
1522 }
1523 }
1524 COM_UNLOCK(sc);
1525
1526 /* Wake up the poller. */
1527 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1528 softintr_schedule(sc->sc_si);
1529 #else
1530 setsoftserial();
1531 #endif
1532
1533 #if NRND > 0 && defined(RND_COM)
1534 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1535 #endif
1536 return (1);
1537 }
1538
1539 static void
1540 sacom_j720_init(struct sa11x0_softc *parent, struct sacom_softc *sc) {
1541
1542 /* XXX this should be done at sc->enable function */
1543 bus_space_write_4(parent->sc_iot, parent->sc_gpioh,
1544 SAGPIO_PCR, 0xa0000);
1545 bus_space_write_4(parent->sc_iot, parent->sc_gpioh,
1546 SAGPIO_PSR, 0x100);
1547 }
1548
1549 /* Initialization for serial console */
1550 int
1551 sacominit(iot, iobase, baud, cflag, iohp)
1552 bus_space_tag_t iot;
1553 bus_addr_t iobase;
1554 int baud;
1555 tcflag_t cflag;
1556 bus_space_handle_t *iohp;
1557 {
1558 int brd, cr0;
1559
1560 if (bus_space_map(iot, iobase, SACOM_NPORTS, 0, iohp))
1561 printf("register map failed\n");
1562
1563 /* wait for the Tx queue to drain and disable the UART */
1564 while(bus_space_read_4(iot, *iohp, SACOM_SR1) & SR1_TBY)
1565 ;
1566 bus_space_write_4(iot, *iohp, SACOM_CR3, 0);
1567
1568 cr0 = cflag2cr0(cflag);
1569 bus_space_write_4(iot, *iohp, SACOM_CR0, cr0);
1570
1571 brd = SACOMSPEED(baud);
1572 sacomconsrate = baud;
1573 sacomconsaddr = iobase;
1574 sacomconscflag = cflag;
1575 /* XXX assumes little endian */
1576 bus_space_write_4(iot, *iohp, SACOM_CR1, brd >> 8);
1577 bus_space_write_4(iot, *iohp, SACOM_CR2, brd & 0xff);
1578
1579 /* enable the UART */
1580 bus_space_write_4(iot, *iohp, SACOM_CR3, CR3_RXE | CR3_TXE);
1581
1582 return (0);
1583 }
1584
1585 void
1586 sacomcnprobe(cp)
1587 struct consdev *cp;
1588 {
1589 cp->cn_pri = CN_REMOTE;
1590 }
1591
1592 void
1593 sacomcninit(cp)
1594 struct consdev *cp;
1595 {
1596 if (cp == NULL) {
1597 /* XXX cp == NULL means that MMU is disabled. */
1598 sacomconsioh = SACOM3_HW_BASE;
1599 sacomconstag = &sa11x0_bs_tag;
1600 cn_tab = &sacomcons;
1601 return;
1602 }
1603
1604 if (sacominit(&sa11x0_bs_tag, CONADDR, CONSPEED,
1605 CONMODE, &sacomconsioh))
1606 panic("can't init serial console @%x", CONADDR);
1607 cn_tab = &sacomcons;
1608 sacomconstag = &sa11x0_bs_tag;
1609 }
1610
1611 int
1612 sacomcngetc(dev)
1613 dev_t dev;
1614 {
1615 int c, s;
1616
1617 s = spltty(); /* XXX do we need this? */
1618
1619 while(! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
1620 & SR1_RNE)) {
1621 #if defined(DDB) || defined(KGDB)
1622 #ifndef DDB_BREAK_CHAR
1623 u_int sr0;
1624 extern int db_active;
1625
1626 sr0 = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR0);
1627 if (ISSET(sr0, SR0_RBB))
1628 bus_space_write_4(sacomconstag, sacomconsioh,
1629 SACOM_SR0, SR0_RBB);
1630 if (ISSET(sr0, SR0_REB)) {
1631 bus_space_write_4(sacomconstag, sacomconsioh,
1632 SACOM_SR0, SR0_REB);
1633 if (db_active == 0)
1634 console_debugger();
1635 }
1636 #endif
1637 #endif /* DDB || KGDB */
1638 }
1639
1640 c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
1641 c &= 0xff;
1642 splx(s);
1643
1644 return (c);
1645 }
1646
1647 void
1648 sacomcnputc(dev, c)
1649 dev_t dev;
1650 int c;
1651 {
1652 int s;
1653
1654 s = spltty(); /* XXX do we need this? */
1655
1656 while(! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
1657 & SR1_TNF))
1658 ;
1659
1660 bus_space_write_4(sacomconstag, sacomconsioh, SACOM_DR, c);
1661 splx(s);
1662 }
1663
1664 void
1665 sacomcnpollc(dev, on)
1666 dev_t dev;
1667 int on;
1668 {
1669
1670 }
1671
1672 #ifdef DEBUG
1673 int
1674 sacomcncharpoll()
1675 {
1676 int c;
1677
1678 if (! (bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
1679 & SR1_RNE))
1680 return -1;
1681
1682 c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
1683 c &= 0xff;
1684
1685 return (c);
1686 }
1687
1688 #endif
1689
1690 /* helper function to identify the com ports used by
1691 console or KGDB (and not yet autoconf attached) */
1692 int
1693 sacom_is_console(iot, iobase, ioh)
1694 bus_space_tag_t iot;
1695 bus_addr_t iobase;
1696 bus_space_handle_t *ioh;
1697 {
1698 bus_space_handle_t help;
1699
1700 if (! sacomconsattached &&
1701 iot == sacomconstag && iobase == sacomconsaddr)
1702 help = sacomconsioh;
1703 else
1704 return (0);
1705
1706 if (ioh)
1707 *ioh = help;
1708 return (1);
1709 }
1710