at91dbgu.c revision 1.2 1 /* $Id: at91dbgu.c,v 1.2 2008/07/03 01:15:38 matt Exp $ */
2 /* $NetBSD: at91dbgu.c,v 1.2 2008/07/03 01:15:38 matt Exp $ */
3
4 /*
5 *
6 * Copyright (c) 1998, 1999, 2001, 2002, 2004 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Jesse Off
11 *
12 * This code is derived from software contributed to The NetBSD Foundation
13 * by Ichiro FUKUHARA and Naoto Shimazaki.
14 *
15 * This code is derived from software contributed to The NetBSD Foundation
16 * by IWAMOTO Toshihiro.
17 *
18 * This code is derived from software contributed to The NetBSD Foundation
19 * by Charles M. Hannum.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * 3. All advertising materials mentioning features or use of this software
30 * must display the following acknowledgement:
31 * This product includes software developed by the NetBSD
32 * Foundation, Inc. and its contributors.
33 * 4. Neither the name of The NetBSD Foundation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
38 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
39 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
41 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 * POSSIBILITY OF SUCH DAMAGE.
48 */
49
50 /*
51 * Copyright (c) 1991 The Regents of the University of California.
52 * All rights reserved.
53 *
54 * Redistribution and use in source and binary forms, with or without
55 * modification, are permitted provided that the following conditions
56 * are met:
57 * 1. Redistributions of source code must retain the above copyright
58 * notice, this list of conditions and the following disclaimer.
59 * 2. Redistributions in binary form must reproduce the above copyright
60 * notice, this list of conditions and the following disclaimer in the
61 * documentation and/or other materials provided with the distribution.
62 * 3. Neither the name of the University nor the names of its contributors
63 * may be used to endorse or promote products derived from this software
64 * without specific prior written permission.
65 *
66 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 * SUCH DAMAGE.
77 *
78 * @(#)com.c 7.5 (Berkeley) 5/16/91
79 */
80
81 /*
82 * TODO: hardware flow control
83 */
84
85 #include <sys/cdefs.h>
86 __KERNEL_RCSID(0, "$NetBSD: at91dbgu.c,v 1.2 2008/07/03 01:15:38 matt Exp $");
87
88 #include "opt_ddb.h"
89 #include "opt_kgdb.h"
90
91 #include "rnd.h"
92 #if NRND > 0 && defined(RND_COM)
93 #include <sys/rnd.h>
94 #endif
95
96 /*
97 * Override cnmagic(9) macro before including <sys/systm.h>.
98 * We need to know if cn_check_magic triggered debugger, so set a flag.
99 * Callers of cn_check_magic must declare int cn_trapped = 0;
100 * XXX: this is *ugly*!
101 */
102 #define cn_trap() \
103 do { \
104 console_debugger(); \
105 cn_trapped = 1; \
106 } while (/* CONSTCOND */ 0)
107
108
109 #include <sys/param.h>
110 #include <sys/systm.h>
111 #include <sys/types.h>
112 #include <sys/conf.h>
113 #include <sys/file.h>
114 #include <sys/device.h>
115 #include <sys/kernel.h>
116 #include <sys/malloc.h>
117 #include <sys/tty.h>
118 #include <sys/uio.h>
119 #include <sys/vnode.h>
120 #include <sys/kauth.h>
121
122 #include <machine/intr.h>
123 #include <machine/bus.h>
124
125 #include <arm/at91/at91reg.h>
126 #include <arm/at91/at91var.h>
127 #include <arm/at91/at91dbguvar.h>
128 #include <arm/at91/at91dbgureg.h>
129
130 #include <dev/cons.h>
131
132 static int at91dbgu_match(device_t, cfdata_t, void *);
133 static void at91dbgu_attach(device_t, device_t, void *);
134
135 static int at91dbgu_param(struct tty *, struct termios *);
136 static void at91dbgu_start(struct tty *);
137 static int at91dbgu_hwiflow(struct tty *, int);
138
139 #if 0
140 static u_int cflag2lcrhi(tcflag_t);
141 #endif
142 static void at91dbgu_iflush(struct at91dbgu_softc *);
143 static void at91dbgu_set(struct at91dbgu_softc *);
144
145 int at91dbgu_cn_getc(dev_t);
146 void at91dbgu_cn_putc(dev_t, int);
147 void at91dbgu_cn_pollc(dev_t, int);
148
149 static void at91dbgu_soft(void* arg);
150 inline static void at91dbgu_txsoft(struct at91dbgu_softc *, struct tty *);
151 inline static void at91dbgu_rxsoft(struct at91dbgu_softc *, struct tty *);
152
153 void at91dbgu_cn_probe(struct consdev *);
154 void at91dbgu_cn_init(struct consdev *);
155
156 static struct at91dbgu_cons_softc {
157 bus_space_tag_t sc_iot;
158 bus_space_handle_t sc_ioh;
159 bus_addr_t sc_hwbase;
160 int sc_ospeed;
161 tcflag_t sc_cflag;
162 int sc_attached;
163
164 u_int8_t *sc_rx_ptr;
165 u_int8_t sc_rx_fifo[64];
166 } dbgu_cn_sc;
167
168 static struct cnm_state at91dbgu_cnm_state;
169
170 CFATTACH_DECL(at91dbgu, sizeof(struct at91dbgu_softc),
171 at91dbgu_match, at91dbgu_attach, NULL, NULL);
172
173 extern struct cfdriver at91dbgu_cd;
174
175 dev_type_open(at91dbgu_open);
176 dev_type_close(at91dbgu_close);
177 dev_type_read(at91dbgu_read);
178 dev_type_write(at91dbgu_write);
179 dev_type_ioctl(at91dbgu_ioctl);
180 dev_type_stop(at91dbgu_stop);
181 dev_type_tty(at91dbgu_tty);
182 dev_type_poll(at91dbgu_poll);
183
184 const struct cdevsw at91dbgu_cdevsw = {
185 at91dbgu_open, at91dbgu_close, at91dbgu_read, at91dbgu_write, at91dbgu_ioctl,
186 at91dbgu_stop, at91dbgu_tty, at91dbgu_poll, nommap, ttykqfilter, D_TTY
187 };
188
189 struct consdev at91dbgu_cons = {
190 at91dbgu_cn_probe, NULL, at91dbgu_cn_getc, at91dbgu_cn_putc, at91dbgu_cn_pollc, NULL,
191 NULL, NULL, NODEV, CN_REMOTE
192 };
193
194 #ifndef DEFAULT_COMSPEED
195 #define DEFAULT_COMSPEED 115200
196 #endif
197
198 #define COMUNIT_MASK 0x7ffff
199 #define COMDIALOUT_MASK 0x80000
200
201 #define COMUNIT(x) (minor(x) & COMUNIT_MASK)
202 #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK)
203
204 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && device_is_active((sc)->sc_dev))
205
206 static int
207 at91dbgu_match(parent, match, aux)
208 device_t parent;
209 cfdata_t match;
210 void *aux;
211 {
212 if (strcmp(match->cf_name, "at91dbgu") == 0)
213 return 2;
214 return 0;
215 }
216
217 static int
218 dbgu_intr(void* arg);
219
220 static void
221 at91dbgu_attach(parent, self, aux)
222 device_t parent;
223 device_t self;
224 void *aux;
225 {
226 struct at91dbgu_softc *sc = device_private(self);
227 struct at91bus_attach_args *sa = aux;
228 struct tty *tp;
229
230 printf("\n");
231
232 sc->sc_dev = self;
233 bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh);
234 sc->sc_iot = sa->sa_iot;
235 sc->sc_hwbase = sa->sa_addr;
236
237 DBGUREG(DBGU_IDR) = -1;
238 at91_intr_establish(sa->sa_pid, IPL_SERIAL, INTR_HIGH_LEVEL, dbgu_intr, sc);
239 DBGU_INIT(AT91_MSTCLK, 115200U);
240
241 if (sc->sc_iot == dbgu_cn_sc.sc_iot
242 && sc->sc_hwbase == dbgu_cn_sc.sc_hwbase) {
243 dbgu_cn_sc.sc_attached = 1;
244 /* Make sure the console is always "hardwired". */
245 delay(10000); /* wait for output to finish */
246 SET(sc->sc_hwflags, COM_HW_CONSOLE);
247 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
248 SET(sc->sc_ier, DBGU_INT_RXRDY);
249 DBGUREG(DBGU_IER) = DBGU_INT_RXRDY; // @@@@@
250 }
251
252 tp = ttymalloc();
253 tp->t_oproc = at91dbgu_start;
254 tp->t_param = at91dbgu_param;
255 tp->t_hwiflow = at91dbgu_hwiflow;
256
257 sc->sc_tty = tp;
258 sc->sc_rbuf = malloc(AT91DBGU_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
259 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
260 sc->sc_rbavail = AT91DBGU_RING_SIZE;
261 if (sc->sc_rbuf == NULL) {
262 printf("%s: unable to allocate ring buffer\n",
263 device_xname(sc->sc_dev));
264 return;
265 }
266 sc->sc_ebuf = sc->sc_rbuf + (AT91DBGU_RING_SIZE << 1);
267 sc->sc_tbc = 0;
268
269 tty_attach(tp);
270
271 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
272 int maj;
273
274 /* locate the major number */
275 maj = cdevsw_lookup_major(&at91dbgu_cdevsw);
276
277 cn_tab->cn_dev = makedev(maj, device_unit(sc->sc_dev));
278
279 aprint_normal("%s: console (maj %u min %u cn_dev %u)\n",
280 device_xname(sc->sc_dev), maj, device_unit(sc->sc_dev),
281 cn_tab->cn_dev);
282 }
283
284 sc->sc_si = softint_establish(SOFTINT_SERIAL, at91dbgu_soft, sc);
285
286 #if NRND > 0 && defined(RND_COM)
287 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
288 RND_TYPE_TTY, 0);
289 #endif
290
291 /* if there are no enable/disable functions, assume the device
292 is always enabled */
293 if (!sc->enable)
294 sc->enabled = 1;
295
296 /* XXX configure register */
297 /* xxx_config(sc) */
298
299 SET(sc->sc_hwflags, COM_HW_DEV_OK);
300 }
301
302 static int
303 at91dbgu_param(struct tty *tp, struct termios *t)
304 {
305 struct at91dbgu_softc *sc
306 = device_lookup_private(&at91dbgu_cd, COMUNIT(tp->t_dev));
307 int s;
308
309 if (COM_ISALIVE(sc) == 0)
310 return (EIO);
311
312 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
313 return (EINVAL);
314
315 /*
316 * For the console, always force CLOCAL and !HUPCL, so that the port
317 * is always active.
318 */
319 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
320 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
321 SET(t->c_cflag, CLOCAL);
322 CLR(t->c_cflag, HUPCL);
323 }
324
325 /*
326 * If there were no changes, don't do anything. This avoids dropping
327 * input and improves performance when all we did was frob things like
328 * VMIN and VTIME.
329 */
330 if (tp->t_ospeed == t->c_ospeed &&
331 tp->t_cflag == t->c_cflag)
332 return (0);
333
334 s = splserial();
335
336 sc->sc_brgr = (AT91_MSTCLK / 16 + t->c_ospeed / 2) / t->c_ospeed;
337
338 /* And copy to tty. */
339 tp->t_ispeed = 0;
340 tp->t_ospeed = t->c_ospeed;
341 tp->t_cflag = t->c_cflag;
342 at91dbgu_set(sc);
343
344 splx(s);
345
346 /*
347 * Update the tty layer's idea of the carrier bit.
348 * We tell tty the carrier is always on.
349 */
350 (void) (*tp->t_linesw->l_modem)(tp, 1);
351
352 #ifdef COM_DEBUG
353 if (com_debug)
354 comstatus(sc, "comparam ");
355 #endif
356
357 if (!ISSET(t->c_cflag, CHWFLOW)) {
358 if (sc->sc_tx_stopped) {
359 sc->sc_tx_stopped = 0;
360 at91dbgu_start(tp);
361 }
362 }
363
364 return (0);
365 }
366
367 static int
368 at91dbgu_hwiflow(struct tty *tp, int block)
369 {
370 return (0);
371 }
372
373 static void
374 at91dbgu_filltx(struct at91dbgu_softc *sc)
375 {
376 #if 0
377 bus_space_tag_t iot = sc->sc_iot;
378 bus_space_handle_t ioh = sc->sc_ioh;
379 #endif
380 int n;
381
382 n = 0;
383 while (DBGUREG(DBGU_SR) & DBGU_SR_TXRDY) {
384 if (n >= sc->sc_tbc)
385 break;
386 DBGUREG(DBGU_THR) = *(sc->sc_tba + n) & 0xff;
387 n++;
388 }
389 sc->sc_tbc -= n;
390 sc->sc_tba += n;
391 }
392
393 static void
394 at91dbgu_start(struct tty *tp)
395 {
396 struct at91dbgu_softc *sc
397 = device_lookup_private(&at91dbgu_cd, COMUNIT(tp->t_dev));
398 int s;
399
400 if (COM_ISALIVE(sc) == 0)
401 return;
402
403 s = spltty();
404 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
405 goto out;
406 if (sc->sc_tx_stopped)
407 goto out;
408 if (!ttypull(tp))
409 goto out;
410
411 /* Grab the first contiguous region of buffer space. */
412 {
413 u_char *tba;
414 int tbc;
415
416 tba = tp->t_outq.c_cf;
417 tbc = ndqb(&tp->t_outq, 0);
418
419 (void)splserial();
420
421 sc->sc_tba = tba;
422 sc->sc_tbc = tbc;
423 }
424
425 SET(tp->t_state, TS_BUSY);
426 sc->sc_tx_busy = 1;
427
428 /* Output the first chunk of the contiguous buffer. */
429 at91dbgu_filltx(sc);
430
431 SET(sc->sc_ier, DBGU_INT_TXRDY);
432 DBGUREG(DBGU_IER) = DBGU_INT_TXRDY;
433
434 out:
435 splx(s);
436 return;
437 }
438
439 static void
440 at91dbgu_break(struct at91dbgu_softc *sc, int onoff)
441 {
442 // @@@ we must disconnect the TX pin from the DBGU and control
443 // @@@ the pin directly to support this...
444 }
445
446 static void
447 at91dbgu_shutdown(struct at91dbgu_softc *sc)
448 {
449 int s;
450
451 s = splserial();
452
453 /* Turn off interrupts. */
454 DBGUREG(DBGU_IDR) = -1;
455
456 /* Clear any break condition set with TIOCSBRK. */
457 at91dbgu_break(sc, 0);
458 at91dbgu_set(sc);
459
460 if (sc->disable) {
461 #ifdef DIAGNOSTIC
462 if (!sc->enabled)
463 panic("at91dbgu_shutdown: not enabled?");
464 #endif
465 (*sc->disable)(sc);
466 sc->enabled = 0;
467 }
468 splx(s);
469 }
470
471 int
472 at91dbgu_open(dev_t dev, int flag, int mode, struct lwp *l)
473 {
474 struct at91dbgu_softc *sc;
475 struct tty *tp;
476 int s, s2;
477 int error;
478
479 sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
480 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
481 sc->sc_rbuf == NULL)
482 return (ENXIO);
483
484 if (!device_is_active(sc->sc_dev))
485 return (ENXIO);
486
487 #ifdef KGDB
488 /*
489 * If this is the kgdb port, no other use is permitted.
490 */
491 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
492 return (EBUSY);
493 #endif
494
495 tp = sc->sc_tty;
496
497 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
498 return (EBUSY);
499
500 s = spltty();
501
502 /*
503 * Do the following iff this is a first open.
504 */
505 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
506 struct termios t;
507
508 tp->t_dev = dev;
509
510 s2 = splserial();
511
512 if (sc->enable) {
513 if ((*sc->enable)(sc)) {
514 splx(s2);
515 splx(s);
516 printf("%s: device enable failed\n",
517 device_xname(sc->sc_dev));
518 return (EIO);
519 }
520 sc->enabled = 1;
521 #if 0
522 /* XXXXXXXXXXXXXXX */
523 com_config(sc);
524 #endif
525 }
526
527 /* Turn on interrupts. */
528 sc->sc_ier |= DBGU_INT_RXRDY;
529 DBGUREG(DBGU_IER) = DBGU_INT_RXRDY;
530
531 #if 0
532 /* Fetch the current modem control status, needed later. */
533 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
534
535 /* Clear PPS capture state on first open. */
536 sc->sc_ppsmask = 0;
537 sc->ppsparam.mode = 0;
538 #endif
539
540 splx(s2);
541
542 /*
543 * Initialize the termios status to the defaults. Add in the
544 * sticky bits from TIOCSFLAGS.
545 */
546 t.c_ispeed = 0;
547 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
548 t.c_ospeed = dbgu_cn_sc.sc_ospeed;
549 t.c_cflag = dbgu_cn_sc.sc_cflag;
550 } else {
551 t.c_ospeed = TTYDEF_SPEED;
552 t.c_cflag = TTYDEF_CFLAG;
553 }
554 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
555 SET(t.c_cflag, CLOCAL);
556 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
557 SET(t.c_cflag, CRTSCTS);
558 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
559 SET(t.c_cflag, MDMBUF);
560 /* Make sure at91dbgu_param() will do something. */
561 tp->t_ospeed = 0;
562 (void) at91dbgu_param(tp, &t);
563 tp->t_iflag = TTYDEF_IFLAG;
564 tp->t_oflag = TTYDEF_OFLAG;
565 tp->t_lflag = TTYDEF_LFLAG;
566 ttychars(tp);
567 ttsetwater(tp);
568
569 s2 = splserial();
570
571 /* Clear the input ring, and unblock. */
572 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
573 sc->sc_rbavail = AT91DBGU_RING_SIZE;
574 at91dbgu_iflush(sc);
575 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
576
577 #ifdef COM_DEBUG
578 if (at91dbgu_debug)
579 comstatus(sc, "at91dbgu_open ");
580 #endif
581
582 splx(s2);
583 }
584
585 splx(s);
586
587 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
588 if (error)
589 goto bad;
590
591 error = (*tp->t_linesw->l_open)(dev, tp);
592 if (error)
593 goto bad;
594
595 return (0);
596
597 bad:
598 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
599 /*
600 * We failed to open the device, and nobody else had it opened.
601 * Clean up the state as appropriate.
602 */
603 at91dbgu_shutdown(sc);
604 }
605
606 return (error);
607 }
608
609 int
610 at91dbgu_close(dev_t dev, int flag, int mode, struct lwp *l)
611 {
612 struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
613 struct tty *tp = sc->sc_tty;
614
615 /* XXX This is for cons.c. */
616 if (!ISSET(tp->t_state, TS_ISOPEN))
617 return (0);
618
619 (*tp->t_linesw->l_close)(tp, flag);
620 ttyclose(tp);
621
622 if (COM_ISALIVE(sc) == 0)
623 return (0);
624
625 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
626 /*
627 * Although we got a last close, the device may still be in
628 * use; e.g. if this was the dialout node, and there are still
629 * processes waiting for carrier on the non-dialout node.
630 */
631 at91dbgu_shutdown(sc);
632 }
633
634 return (0);
635 }
636
637 int
638 at91dbgu_read(dev_t dev, struct uio *uio, int flag)
639 {
640 struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
641 struct tty *tp = sc->sc_tty;
642
643 if (COM_ISALIVE(sc) == 0)
644 return (EIO);
645
646 return ((*tp->t_linesw->l_read)(tp, uio, flag));
647 }
648
649 int
650 at91dbgu_write(dev_t dev, struct uio *uio, int flag)
651 {
652 struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
653 struct tty *tp = sc->sc_tty;
654
655 if (COM_ISALIVE(sc) == 0)
656 return (EIO);
657
658 return ((*tp->t_linesw->l_write)(tp, uio, flag));
659 }
660
661 int
662 at91dbgu_poll(dev_t dev, int events, struct lwp *l)
663 {
664 struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
665 struct tty *tp = sc->sc_tty;
666
667 if (COM_ISALIVE(sc) == 0)
668 return (EIO);
669
670 return ((*tp->t_linesw->l_poll)(tp, events, l));
671 }
672
673 struct tty *
674 at91dbgu_tty(dev_t dev)
675 {
676 struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
677 struct tty *tp = sc->sc_tty;
678
679 return (tp);
680 }
681
682 int
683 at91dbgu_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
684 {
685 struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev));
686 struct tty *tp = sc->sc_tty;
687 int error;
688 int s;
689
690 if (COM_ISALIVE(sc) == 0)
691 return (EIO);
692
693 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
694 if (error != EPASSTHROUGH)
695 return (error);
696
697 error = ttioctl(tp, cmd, data, flag, l);
698 if (error != EPASSTHROUGH)
699 return (error);
700
701 error = 0;
702
703 s = splserial();
704
705 switch (cmd) {
706 case TIOCSBRK:
707 at91dbgu_break(sc, 1);
708 break;
709
710 case TIOCCBRK:
711 at91dbgu_break(sc, 0);
712 break;
713
714 case TIOCGFLAGS:
715 *(int *)data = sc->sc_swflags;
716 break;
717
718 case TIOCSFLAGS:
719 error = kauth_authorize_device_tty(l->l_cred,
720 KAUTH_DEVICE_TTY_PRIVSET, tp);
721 if (error)
722 break;
723 sc->sc_swflags = *(int *)data;
724 break;
725
726 default:
727 error = EPASSTHROUGH;
728 break;
729 }
730
731 splx(s);
732
733 return (error);
734 }
735
736 /*
737 * Stop output on a line.
738 */
739 void
740 at91dbgu_stop(struct tty *tp, int flag)
741 {
742 struct at91dbgu_softc *sc
743 = device_lookup_private(&at91dbgu_cd, COMUNIT(tp->t_dev));
744 int s;
745
746 s = splserial();
747 if (ISSET(tp->t_state, TS_BUSY)) {
748 /* Stop transmitting at the next chunk. */
749 sc->sc_tbc = 0;
750 if (!ISSET(tp->t_state, TS_TTSTOP))
751 SET(tp->t_state, TS_FLUSH);
752 }
753 splx(s);
754 }
755
756 #if 0
757 static u_int
758 cflag2lcrhi(tcflag_t cflag)
759 {
760 u_int32_t mr;
761
762 switch (cflag & CSIZE) {
763 default:
764 mr = 0x0;
765 break;
766 }
767 #if 0
768 mr |= (cflag & PARENB) ? LinCtrlHigh_PEN : 0;
769 mr |= (cflag & PARODD) ? 0 : LinCtrlHigh_EPS;
770 mr |= (cflag & CSTOPB) ? LinCtrlHigh_STP2 : 0;
771 mr |= LinCtrlHigh_FEN; /* FIFO always enabled */
772 #endif
773 mr |= DBGU_MR_PAR_NONE;
774 return (mr);
775 }
776 #endif
777
778 static void
779 at91dbgu_iflush(struct at91dbgu_softc *sc)
780 {
781 #if 0
782 bus_space_tag_t iot = sc->sc_iot;
783 bus_space_handle_t ioh = sc->sc_ioh;
784 #endif
785 #ifdef DIAGNOSTIC
786 int reg;
787 #endif
788 int timo;
789
790 #ifdef DIAGNOSTIC
791 reg = 0xffff;
792 #endif
793 timo = 50000;
794 /* flush any pending I/O */
795 while (DBGUREG(DBGU_SR) & DBGU_SR_RXRDY
796 && --timo)
797 #ifdef DIAGNOSTIC
798 reg =
799 #else
800 (void)
801 #endif
802 DBGUREG(DBGU_RHR);
803 #ifdef DIAGNOSTIC
804 if (!timo)
805 printf("%s: com_iflush timeout %02x\n", device_xname(sc->sc_dev),
806 reg);
807 #endif
808 }
809
810 static void
811 at91dbgu_set(struct at91dbgu_softc *sc)
812 {
813 DBGUREG(DBGU_MR) = DBGU_MR_PAR_NONE;
814 DBGUREG(DBGU_BRGR) = sc->sc_brgr;
815 DBGUREG(DBGU_CR) = DBGU_CR_TXEN | DBGU_CR_RXEN; // @@@ just in case
816 }
817
818 void
819 at91dbgu_cn_attach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh,
820 int ospeed, tcflag_t cflag);
821 void
822 at91dbgu_cn_attach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh,
823 int ospeed, tcflag_t cflag)
824 {
825 cn_tab = &at91dbgu_cons;
826 cn_init_magic(&at91dbgu_cnm_state);
827 cn_set_magic("\047\001");
828
829 dbgu_cn_sc.sc_iot = iot;
830 dbgu_cn_sc.sc_ioh = ioh;
831 dbgu_cn_sc.sc_hwbase = iobase;
832 dbgu_cn_sc.sc_ospeed = ospeed;
833 dbgu_cn_sc.sc_cflag = cflag;
834
835 DBGU_INIT(AT91_MSTCLK, ospeed);
836 }
837
838 void at91dbgu_attach_cn(bus_space_tag_t iot, int ospeed, int cflag)
839 {
840 bus_space_handle_t ioh;
841 bus_space_map(iot, AT91DBGU_BASE, AT91DBGU_SIZE, 0, &ioh);
842 at91dbgu_cn_attach(iot, AT91DBGU_BASE, ioh, ospeed, cflag);
843 }
844
845 void
846 at91dbgu_cn_probe(struct consdev *cp)
847 {
848 cp->cn_pri = CN_REMOTE;
849 }
850
851 void
852 at91dbgu_cn_pollc(dev_t dev, int on)
853 {
854 if (on) {
855 // enable polling mode
856 DBGUREG(DBGU_IDR) = DBGU_INT_RXRDY;
857 } else {
858 // disable polling mode
859 DBGUREG(DBGU_IER) = DBGU_INT_RXRDY;
860 }
861 }
862
863 void
864 at91dbgu_cn_putc(dev_t dev, int c)
865 {
866 #if 0
867 bus_space_tag_t iot = dbgu_cn_sc.sc_iot;
868 bus_space_handle_t ioh = dbgu_cn_sc.sc_ioh;
869 #endif
870 DBGU_PUTC(c);
871
872 #ifdef DEBUG
873 if (c == '\r') {
874 int s = splserial();
875 while((DBGUREG(DBGU_SR) & DBGU_SR_TXEMPTY) == 0) {
876 splx(s);
877 s = splserial();
878 }
879 splx(s);
880 }
881 #endif
882
883 }
884
885 int
886 at91dbgu_cn_getc(dev_t dev)
887 {
888 int c, sr;
889 int s;
890 #if 0
891 bus_space_tag_t iot = dbgu_cn_sc.sc_iot;
892 bus_space_handle_t ioh = dbgu_cn_sc.sc_ioh;
893 #endif
894
895 s = splserial();
896
897 while ((c = DBGU_PEEKC()) == -1) {
898 splx(s);
899 s = splserial();
900 }
901 ;
902 sr = DBGUREG(DBGU_SR);
903 if (ISSET(sr, DBGU_SR_FRAME) && c == 0) {
904 DBGUREG(DBGU_CR) = DBGU_CR_RSTSTA; // reset status bits
905 c = CNC_BREAK;
906 }
907 #ifdef DDB
908 extern int db_active;
909 if (!db_active)
910 #endif
911 {
912 int cn_trapped = 0; /* unused */
913
914 cn_check_magic(dev, c, at91dbgu_cnm_state);
915 }
916 splx(s);
917
918 c &= 0xff;
919
920 return (c);
921 }
922
923 inline static void
924 at91dbgu_txsoft(struct at91dbgu_softc *sc, struct tty *tp)
925 {
926 CLR(tp->t_state, TS_BUSY);
927 if (ISSET(tp->t_state, TS_FLUSH))
928 CLR(tp->t_state, TS_FLUSH);
929 else
930 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
931 (*tp->t_linesw->l_start)(tp);
932 }
933
934 inline static void
935 at91dbgu_rxsoft(struct at91dbgu_softc *sc, struct tty *tp)
936 {
937 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
938 u_char *get, *end;
939 u_int cc, scc;
940 u_char sts;
941 int code;
942 int s;
943
944 end = sc->sc_ebuf;
945 get = sc->sc_rbget;
946 scc = cc = AT91DBGU_RING_SIZE - sc->sc_rbavail;
947 #if 0
948 if (cc == AT91DBGU_RING_SIZE) {
949 sc->sc_floods++;
950 if (sc->sc_errors++ == 0)
951 callout_reset(&sc->sc_diag_callout, 60 * hz,
952 comdiag, sc);
953 }
954 #endif
955 while (cc) {
956 code = get[0];
957 sts = get[1];
958 if (ISSET(sts, DBGU_SR_PARE | DBGU_SR_FRAME | DBGU_SR_OVRE)) {
959 #if 0
960 if (ISSET(lsr, DR_ROR)) {
961 sc->sc_overflows++;
962 if (sc->sc_errors++ == 0)
963 callout_reset(&sc->sc_diag_callout,
964 60 * hz, comdiag, sc);
965 }
966 #endif
967 if (ISSET(sts, (DBGU_SR_FRAME | DBGU_SR_OVRE)))
968 SET(code, TTY_FE);
969 if (ISSET(sts, DBGU_SR_PARE))
970 SET(code, TTY_PE);
971 }
972 if ((*rint)(code, tp) == -1) {
973 /*
974 * The line discipline's buffer is out of space.
975 */
976 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
977 /*
978 * We're either not using flow control, or the
979 * line discipline didn't tell us to block for
980 * some reason. Either way, we have no way to
981 * know when there's more space available, so
982 * just drop the rest of the data.
983 */
984 get += cc << 1;
985 if (get >= end)
986 get -= AT91DBGU_RING_SIZE << 1;
987 cc = 0;
988 } else {
989 /*
990 * Don't schedule any more receive processing
991 * until the line discipline tells us there's
992 * space available (through comhwiflow()).
993 * Leave the rest of the data in the input
994 * buffer.
995 */
996 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
997 }
998 break;
999 }
1000 get += 2;
1001 if (get >= end)
1002 get = sc->sc_rbuf;
1003 cc--;
1004 }
1005
1006 if (cc != scc) {
1007 sc->sc_rbget = get;
1008 s = splserial();
1009
1010 cc = sc->sc_rbavail += scc - cc;
1011 /* Buffers should be ok again, release possible block. */
1012 if (cc >= 1) {
1013 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1014 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1015 DBGUREG(DBGU_IER) = DBGU_INT_RXRDY;
1016 }
1017 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1018 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1019 #if 0
1020 com_hwiflow(sc);
1021 #endif
1022 }
1023 }
1024 splx(s);
1025 }
1026 }
1027
1028 static void
1029 at91dbgu_soft(void* arg)
1030 {
1031 struct at91dbgu_softc *sc = arg;
1032
1033 if (COM_ISALIVE(sc) == 0)
1034 return;
1035
1036 if (sc->sc_rx_ready) {
1037 sc->sc_rx_ready = 0;
1038 at91dbgu_rxsoft(sc, sc->sc_tty);
1039 }
1040 if (sc->sc_tx_done) {
1041 sc->sc_tx_done = 0;
1042 at91dbgu_txsoft(sc, sc->sc_tty);
1043 }
1044 }
1045
1046
1047 static int
1048 dbgu_intr(void* arg)
1049 {
1050 struct at91dbgu_softc *sc = arg;
1051 #if 0
1052 bus_space_tag_t iot = sc->sc_iot;
1053 bus_space_handle_t ioh = sc->sc_ioh;
1054 #endif
1055 u_char *put, *end;
1056 u_int cc;
1057 u_int32_t imr, sr;
1058 int c = 0;
1059 imr = DBGUREG(DBGU_IMR);
1060 #if 0
1061 if (!imr)
1062 return 0;
1063 #endif
1064 sr = DBGUREG(DBGU_SR);
1065 if (!(sr & imr)) {
1066 if (sr & DBGU_SR_RXRDY) {
1067 // printf("sr=0x%08x imr=0x%08x\n", sr, imr);
1068 }
1069 return 0;
1070 }
1071
1072 end = sc->sc_ebuf;
1073 put = sc->sc_rbput;
1074 cc = sc->sc_rbavail;
1075
1076 // ok, we DO have some interrupts to serve!
1077 if (sr & DBGU_SR_RXRDY) {
1078 int cn_trapped = 0;
1079
1080 c = DBGUREG(DBGU_RHR);
1081 if (ISSET(sr, (DBGU_SR_OVRE | DBGU_SR_FRAME | DBGU_SR_PARE)))
1082 DBGUREG(DBGU_CR) = DBGU_CR_RSTSTA;
1083 if (ISSET(sr, DBGU_SR_FRAME) && c == 0) {
1084 c = CNC_BREAK;
1085 }
1086 #ifdef DDB
1087 extern int db_active;
1088 if (!db_active)
1089 #endif
1090 cn_check_magic(cn_tab->cn_dev, c, at91dbgu_cnm_state);
1091 if (!cn_trapped && cc) {
1092 put[0] = c & 0xff;
1093 put[1] = sr & 0xff;
1094 put += 2;
1095 if (put >= end)
1096 put = sc->sc_rbuf;
1097 cc--;
1098 /*
1099 * Current string of incoming characters ended because
1100 * no more data was available or we ran out of space.
1101 * Schedule a receive event if any data was received.
1102 * If we're out of space, turn off receive interrupts.
1103 */
1104 sc->sc_rbput = put;
1105 sc->sc_rbavail = cc;
1106 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1107 sc->sc_rx_ready = 1;
1108
1109 /*
1110 * See if we are in danger of overflowing a buffer. If
1111 * so, use hardware flow control to ease the pressure.
1112 */
1113
1114 /* but at91dbgu cannot (yet). X-( */
1115
1116 /*
1117 * If we're out of space, disable receive interrupts
1118 * until the queue has drained a bit.
1119 */
1120 if (!cc) {
1121 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1122 }
1123 }
1124 }
1125
1126 /*
1127 * Done handling any receive interrupts. See if data can be
1128 * transmitted as well. Schedule tx done event if no data left
1129 * and tty was marked busy.
1130 */
1131
1132 if (ISSET(sr, DBGU_SR_TXRDY) && sc->sc_tbc > 0) {
1133 /* Output the next chunk of the contiguous buffer, if any. */
1134 at91dbgu_filltx(sc);
1135 } else {
1136 /* Disable transmit completion interrupts if necessary. */
1137 DBGUREG(DBGU_IDR) = DBGU_INT_TXRDY;
1138 if (sc->sc_tx_busy) {
1139 sc->sc_tx_busy = 0;
1140 sc->sc_tx_done = 1;
1141 }
1142 }
1143
1144 /* Wake up the poller. */
1145 softint_schedule(sc->sc_si);
1146 #if 0
1147 #if NRND > 0 && defined(RND_COM)
1148 rnd_add_uint32(&sc->rnd_source, imr ^ sr ^ c);
1149 #endif
1150 #endif
1151
1152 return (1);
1153 }
1154