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