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