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