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