sbjcn.c revision 1.7 1 /* $NetBSD: sbjcn.c,v 1.7 2003/07/15 02:43:40 lukem Exp $ */
2
3 /*
4 * Copyright 2000, 2001
5 * Broadcom Corporation. All rights reserved.
6 *
7 * This software is furnished under license and may be used and copied only
8 * in accordance with the following terms and conditions. Subject to these
9 * conditions, you may download, copy, install, use, modify and distribute
10 * modified or unmodified copies of this software in source and/or binary
11 * form. No title or ownership is transferred hereby.
12 *
13 * 1) Any source code used, modified or distributed must reproduce and
14 * retain this copyright notice and list of conditions as they appear in
15 * the source file.
16 *
17 * 2) No right is granted to use any trade name, trademark, or logo of
18 * Broadcom Corporation. The "Broadcom Corporation" name may not be
19 * used to endorse or promote products derived from this software
20 * without the prior written permission of Broadcom Corporation.
21 *
22 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
25 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
26 * FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
27 * LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32 * OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /* from: $NetBSD: com.c,v 1.172 2000/05/03 19:19:04 thorpej Exp */
36
37 /*-
38 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
39 * All rights reserved.
40 *
41 * This code is derived from software contributed to The NetBSD Foundation
42 * by Charles M. Hannum.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the NetBSD
55 * Foundation, Inc. and its contributors.
56 * 4. Neither the name of The NetBSD Foundation nor the names of its
57 * contributors may be used to endorse or promote products derived
58 * from this software without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
61 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
62 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
63 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
64 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
65 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
66 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
67 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
68 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
69 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
70 * POSSIBILITY OF SUCH DAMAGE.
71 */
72
73 /*
74 * Copyright (c) 1991 The Regents of the University of California.
75 * All rights reserved.
76 *
77 * Redistribution and use in source and binary forms, with or without
78 * modification, are permitted provided that the following conditions
79 * are met:
80 * 1. Redistributions of source code must retain the above copyright
81 * notice, this list of conditions and the following disclaimer.
82 * 2. Redistributions in binary form must reproduce the above copyright
83 * notice, this list of conditions and the following disclaimer in the
84 * documentation and/or other materials provided with the distribution.
85 * 3. All advertising materials mentioning features or use of this software
86 * must display the following acknowledgement:
87 * This product includes software developed by the University of
88 * California, Berkeley and its contributors.
89 * 4. Neither the name of the University nor the names of its contributors
90 * may be used to endorse or promote products derived from this software
91 * without specific prior written permission.
92 *
93 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
94 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
95 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
96 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
97 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
98 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
99 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
101 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
102 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
103 * SUCH DAMAGE.
104 *
105 * @(#)com.c 7.5 (Berkeley) 5/16/91
106 */
107
108 /*
109 * `sbjcn' driver, supports console over SiByte SB-1250 JTAG.
110 *
111 * Accesses a section of JTAG memory space to mimic a console,
112 * if there's a matching program outside to communicate with.
113 * If nobody is there, things will be very quiet.
114 */
115
116 #include <sys/cdefs.h>
117 __KERNEL_RCSID(0, "$NetBSD: sbjcn.c,v 1.7 2003/07/15 02:43:40 lukem Exp $");
118
119 #define SBJCN_DEBUG
120
121 #include "opt_ddb.h"
122
123 #include <sys/param.h>
124 #include <sys/systm.h>
125 #include <sys/ioctl.h>
126 #include <sys/select.h>
127 #include <sys/tty.h>
128 #include <sys/proc.h>
129 #include <sys/user.h>
130 #include <sys/conf.h>
131 #include <sys/file.h>
132 #include <sys/uio.h>
133 #include <sys/kernel.h>
134 #include <sys/syslog.h>
135 #include <sys/types.h>
136 #include <sys/device.h>
137 #include <sys/malloc.h>
138 #include <sys/vnode.h>
139
140 #include <sbmips/dev/sbscd/sbscdvar.h>
141 #include <sbmips/dev/sbscd/sbjcnvar.h>
142 #include <dev/cons.h>
143 #include <machine/locore.h>
144
145 void sbjcn_attach_channel(struct sbjcn_softc *sc, int chan, int intr);
146 static void sbjcncn_grabdword(struct sbjcn_channel *ch);
147 static char sbjcncn_nextbyte(struct sbjcn_channel *ch);
148 static void sbjcn_cngrabdword(void);
149
150 #if defined(DDB) || defined(KGDB)
151 static void sbjcn_enable_debugport(struct sbjcn_channel *ch);
152 #endif
153 void sbjcn_config(struct sbjcn_channel *ch);
154 void sbjcn_shutdown(struct sbjcn_channel *ch);
155 int sbjcn_speed(long, long *);
156 static int cflag2modes(tcflag_t, u_char *, u_char *);
157 int sbjcn_param(struct tty *, struct termios *);
158 void sbjcn_start(struct tty *);
159 int sbjcn_hwiflow(struct tty *, int);
160
161 void sbjcn_loadchannelregs(struct sbjcn_channel *);
162 void sbjcn_dohwiflow(struct sbjcn_channel *);
163 void sbjcn_break(struct sbjcn_channel *, int);
164 void sbjcn_modem(struct sbjcn_channel *, int);
165 void tiocm_to_sbjcn(struct sbjcn_channel *, int, int);
166 int sbjcn_to_tiocm(struct sbjcn_channel *);
167 void sbjcn_iflush(struct sbjcn_channel *);
168
169 int sbjcn_init(u_long addr, int chan, int rate, tcflag_t cflag);
170 int sbjcn_common_getc(u_long addr, int chan);
171 void sbjcn_common_putc(u_long addr, int chan, int c);
172
173 int sbjcn_cngetc(dev_t dev);
174 void sbjcn_cnputc(dev_t dev, int c);
175 void sbjcn_cnpollc(dev_t dev, int on);
176
177 extern struct cfdriver sbjcn_cd;
178
179 dev_type_open(sbjcnopen);
180 dev_type_close(sbjcnclose);
181 dev_type_read(sbjcnread);
182 dev_type_write(sbjcnwrite);
183 dev_type_ioctl(sbjcnioctl);
184 dev_type_stop(sbjcnstop);
185 dev_type_tty(sbjcntty);
186
187 const struct cdevsw sbjcn_cdevsw = {
188 sbjcnopen, sbjcnclose, sbjcnread, sbjcnwrite, sbjcnioctl,
189 sbjcnstop, sbjcntty, nopoll, nommap, ttykqfilter, D_TTY
190 };
191
192 #define integrate static inline
193 integrate void sbjcn_rxsoft(struct sbjcn_channel *, struct tty *);
194 integrate void sbjcn_txsoft(struct sbjcn_channel *, struct tty *);
195 integrate void sbjcn_stsoft(struct sbjcn_channel *, struct tty *);
196 integrate void sbjcn_schedrx(struct sbjcn_channel *);
197 integrate void sbjcn_recv(struct sbjcn_channel *ch);
198 void sbjcn_diag(void *);
199 void sbjcn_callout(void *);
200
201 /*
202 * Make this an option variable one can patch.
203 * But be warned: this must be a power of 2!
204 */
205 u_int sbjcn_rbuf_size = SBJCN_RING_SIZE;
206
207 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
208 u_int sbjcn_rbuf_hiwat = (SBJCN_RING_SIZE * 1) / 4;
209 u_int sbjcn_rbuf_lowat = (SBJCN_RING_SIZE * 3) / 4;
210
211 static int sbjcn_cons_present;
212 static int sbjcn_cons_attached;
213 static u_long sbjcn_cons_addr;
214 static int sbjcn_cons_chan;
215 static int sbjcn_cons_rate;
216 static tcflag_t sbjcn_cons_cflag;
217 static int sbjcn_cons_waiting_input;
218 static uint64_t sbjcn_cons_input_buf;
219
220 #ifdef KGDB
221 #include <sys/kgdb.h>
222
223 static int sbjcn_kgdb_present;
224 static int sbjcn_kgdb_attached;
225 static u_long sbjcn_kgdb_addr;
226 static int sbjcn_kgdb_chan;
227
228 int sbjcn_kgdb_getc(void *);
229 void sbjcn_kgdb_putc(void *, int);
230 #endif /* KGDB */
231
232 static int sbjcn_match(struct device *, struct cfdata *, void *);
233 static void sbjcn_attach(struct device *, struct device *, void *);
234
235 CFATTACH_DECL(sbjcn, sizeof(struct sbjcn_softc),
236 sbjcn_match, sbjcn_attach, NULL, NULL);
237
238 #define READ_REG(rp) (mips3_ld((uint64_t *)(rp)))
239 #define WRITE_REG(rp, val) (mips3_sd((uint64_t *)(rp), (val)))
240
241 #define JTAG_CONS_CONTROL 0x00
242 #define JTAG_CONS_INPUT 0x20
243 #define JTAG_CONS_OUTPUT 0x40
244 #define JTAG_CONS_MAGICNUM 0x50FABEEF12349873
245
246 #define jtag_input_len(data) (((data) >> 56) & 0xFF)
247
248
249 static int
250 sbjcn_match(struct device *parent, struct cfdata *match, void *aux)
251 {
252 struct sbscd_attach_args *sap = aux;
253
254 if (sap->sa_locs.sa_type != SBSCD_DEVTYPE_JTAGCONS)
255 return (0);
256
257 return 1;
258 }
259
260 static void
261 sbjcn_attach(struct device *parent, struct device *self, void *aux)
262 {
263 struct sbjcn_softc *sc = (struct sbjcn_softc *)self;
264 struct sbscd_attach_args *sap = aux;
265
266 sc->sc_addr = sap->sa_base + sap->sa_locs.sa_offset;
267
268 printf("\n");
269 sbjcn_attach_channel(sc, 0, sap->sa_locs.sa_intr[0]);
270 }
271
272 void
273 sbjcn_attach_channel(struct sbjcn_softc *sc, int chan, int intr)
274 {
275 struct sbjcn_channel *ch = &sc->sc_channels[chan];
276 u_long chan_addr;
277 struct tty *tp;
278
279 ch->ch_sc = sc;
280 ch->ch_num = chan;
281
282 chan_addr = sc->sc_addr + (0x100 * chan);
283 ch->ch_base = (void *)MIPS_PHYS_TO_KSEG1(chan_addr);
284 ch->ch_input_reg =
285 (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + JTAG_CONS_INPUT);
286 ch->ch_output_reg =
287 (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + JTAG_CONS_OUTPUT);
288 ch->ch_control_reg =
289 (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + JTAG_CONS_CONTROL);
290 ch->ch_waiting_input = 0;
291
292 ch->ch_i_dcd = ch->ch_i_dcd_pin = 0 /* XXXCGD */;
293 ch->ch_i_cts = ch->ch_i_cts_pin = 0 /* XXXCGD */;
294 ch->ch_i_dsr = ch->ch_i_dsr_pin = 0 /* XXXCGD */;
295 ch->ch_i_ri = ch->ch_i_ri_pin = 0 /* XXXCGD */;
296 ch->ch_i_mask =
297 ch->ch_i_dcd | ch->ch_i_cts | ch->ch_i_dsr | ch->ch_i_ri;
298 ch->ch_o_dtr = ch->ch_o_dtr_pin = 0 /* XXXCGD */;
299 ch->ch_o_rts = ch->ch_o_rts_pin = 0 /* XXXCGD */;
300 ch->ch_o_mask = ch->ch_o_dtr | ch->ch_o_rts;
301
302 callout_init(&ch->ch_diag_callout);
303 callout_init(&ch->ch_callout);
304
305 /* Disable interrupts before configuring the device. */
306 ch->ch_imr = 0;
307
308 if (sbjcn_cons_present &&
309 sbjcn_cons_addr == chan_addr && sbjcn_cons_chan == chan) {
310 sbjcn_cons_attached = 1;
311
312 /* Make sure the console is always "hardwired". */
313 delay(1000); /* wait for output to finish */
314 SET(ch->ch_hwflags, SBJCN_HW_CONSOLE);
315 SET(ch->ch_swflags, TIOCFLAG_SOFTCAR);
316 }
317
318 tp = ttymalloc();
319 tp->t_oproc = sbjcn_start;
320 tp->t_param = sbjcn_param;
321 tp->t_hwiflow = sbjcn_hwiflow;
322
323 ch->ch_tty = tp;
324 ch->ch_rbuf = malloc(sbjcn_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
325 if (ch->ch_rbuf == NULL) {
326 printf("%s: channel %d: unable to allocate ring buffer\n",
327 sc->sc_dev.dv_xname, chan);
328 return;
329 }
330 ch->ch_ebuf = ch->ch_rbuf + (sbjcn_rbuf_size << 1);
331
332 tty_attach(tp);
333
334 if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE)) {
335 int maj;
336
337 /* locate the major number */
338 maj = cdevsw_lookup_major(&sbjcn_cdevsw);
339
340 cn_tab->cn_dev = makedev(maj, (sc->sc_dev.dv_unit << 1) + chan);
341
342 printf("%s: channel %d: console\n", sc->sc_dev.dv_xname, chan);
343 }
344
345 #ifdef KGDB
346 /*
347 * Allow kgdb to "take over" this port. If this is
348 * the kgdb device, it has exclusive use.
349 */
350 if (sbjcn_kgdb_present &&
351 sbjcn_kgdb_addr == chan_addr && sbjcn_kgdb_chan == chan) {
352 sbjcn_kgdb_attached = 1;
353
354 SET(ch->ch_hwflags, SBJCN_HW_KGDB);
355 printf("%s: channel %d: kgdb\n", sc->sc_dev.dv_xname, chan);
356 }
357 #endif
358
359 sbjcn_config(ch);
360
361 callout_reset(&ch->ch_callout, hz/10, sbjcn_callout, ch);
362
363 SET(ch->ch_hwflags, SBJCN_HW_DEV_OK);
364 }
365
366 int
367 sbjcn_speed(long speed, long *brcp)
368 {
369 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
370
371 int x, err;
372 int frequency = 100000000;
373
374 *brcp = divrnd(frequency / 20, speed) - 1;
375
376 if (speed <= 0)
377 return (-1);
378 x = divrnd(frequency / 20, speed);
379 if (x <= 0)
380 return (-1);
381 err = divrnd(((quad_t)frequency) * 1000 / 20, speed * x) - 1000;
382 if (err < 0)
383 err = -err;
384 if (err > SBJCN_TOLERANCE)
385 return (-1);
386 *brcp = x - 1;
387 return (0);
388
389 #undef divrnd
390 }
391
392 #ifdef SBJCN_DEBUG
393 void sbjcn_status(struct sbjcn_channel *, char *);
394
395 int sbjcn_debug = 1 /* XXXCGD */;
396
397 void
398 sbjcn_status(struct sbjcn_channel *ch, char *str)
399 {
400 struct sbjcn_softc *sc = ch->ch_sc;
401 struct tty *tp = ch->ch_tty;
402
403 printf("%s: chan %d: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n",
404 sc->sc_dev.dv_xname, ch->ch_num, str,
405 ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
406 ISSET(ch->ch_iports, ch->ch_i_dcd) ? "+" : "-",
407 ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
408 ISSET(ch->ch_oports, ch->ch_o_dtr) ? "+" : "-",
409 ch->ch_tx_stopped ? "+" : "-");
410
411 printf("%s: chan %d: %s %scrtscts %scts %sts_ttstop %srts %xrx_flags\n",
412 sc->sc_dev.dv_xname, ch->ch_num, str,
413 ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
414 ISSET(ch->ch_iports, ch->ch_i_cts) ? "+" : "-",
415 ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
416 ISSET(ch->ch_oports, ch->ch_o_rts) ? "+" : "-",
417 ch->ch_rx_flags);
418 }
419 #endif
420
421 #if defined(DDB) || defined(KGDB)
422 static void
423 sbjcn_enable_debugport(struct sbjcn_channel *ch)
424 {
425 int s;
426
427 /* Turn on line break interrupt, set carrier. */
428 s = splserial();
429
430 ch->ch_imr = 0x04;
431 SET(ch->ch_oports, ch->ch_o_dtr | ch->ch_o_rts);
432
433 splx(s);
434 }
435 #endif
436
437 void
438 sbjcn_config(struct sbjcn_channel *ch)
439 {
440
441 /* Disable interrupts before configuring the device. */
442 ch->ch_imr = 0x00;
443
444 #ifdef DDB
445 if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE))
446 sbjcn_enable_debugport(ch);
447 #endif
448
449 #ifdef KGDB
450 /*
451 * Allow kgdb to "take over" this port. If this is
452 * the kgdb device, it has exclusive use.
453 */
454 if (ISSET(ch->ch_hwflags, SBJCN_HW_KGDB))
455 sbjcn_enable_debugport(ch);
456 #endif
457 }
458
459 void
460 sbjcn_shutdown(struct sbjcn_channel *ch)
461 {
462 struct tty *tp = ch->ch_tty;
463 int s;
464
465 s = splserial();
466
467 /* If we were asserting flow control, then deassert it. */
468 SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
469 sbjcn_dohwiflow(ch);
470
471 /* Clear any break condition set with TIOCSBRK. */
472 sbjcn_break(ch, 0);
473
474 /*
475 * Hang up if necessary. Wait a bit, so the other side has time to
476 * notice even if we immediately open the port again.
477 */
478 if (ISSET(tp->t_cflag, HUPCL)) {
479 sbjcn_modem(ch, 0);
480 (void) tsleep(ch, TTIPRI, ttclos, hz);
481 }
482
483 /* Turn off interrupts. */
484 #ifdef DDB
485 if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE))
486 ch->ch_imr = 0x04; /* interrupt on break */
487 else
488 #endif
489 ch->ch_imr = 0;
490
491 splx(s);
492 }
493
494 int
495 sbjcnopen(dev_t dev, int flag, int mode, struct proc *p)
496 {
497 int unit = SBJCN_UNIT(dev);
498 int chan = SBJCN_CHAN(dev);
499 struct sbjcn_softc *sc;
500 struct sbjcn_channel *ch;
501 struct tty *tp;
502 int s, s2;
503 int error;
504
505 if (unit >= sbjcn_cd.cd_ndevs)
506 return (ENXIO);
507 sc = sbjcn_cd.cd_devs[unit];
508 if (sc == 0)
509 return (ENXIO);
510 ch = &sc->sc_channels[chan];
511 if (!ISSET(ch->ch_hwflags, SBJCN_HW_DEV_OK) || ch->ch_rbuf == NULL)
512 return (ENXIO);
513
514 #ifdef KGDB
515 /*
516 * If this is the kgdb port, no other use is permitted.
517 */
518 if (ISSET(ch->ch_hwflags, SBJCN_HW_KGDB))
519 return (EBUSY);
520 #endif
521
522 tp = ch->ch_tty;
523
524 if (ISSET(tp->t_state, TS_ISOPEN) &&
525 ISSET(tp->t_state, TS_XCLUDE) &&
526 p->p_ucred->cr_uid != 0)
527 return (EBUSY);
528
529 s = spltty();
530
531 /*
532 * Do the following iff this is a first open.
533 */
534 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
535 struct termios t;
536
537 tp->t_dev = dev;
538
539 s2 = splserial();
540
541 /* Turn on receive, break, and status change interrupts. */
542 ch->ch_imr = 0xe;
543
544 /* Fetch the current modem control status, needed later. */
545 ch->ch_iports = 0;
546 ch->ch_iports_delta = 0;
547 splx(s2);
548
549 /*
550 * Initialize the termios status to the defaults. Add in the
551 * sticky bits from TIOCSFLAGS.
552 */
553 t.c_ispeed = 0;
554 if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE)) {
555 t.c_ospeed = sbjcn_cons_rate;
556 t.c_cflag = sbjcn_cons_cflag;
557 } else {
558 t.c_ospeed = TTYDEF_SPEED;
559 t.c_cflag = TTYDEF_CFLAG;
560 }
561 if (ISSET(ch->ch_swflags, TIOCFLAG_CLOCAL))
562 SET(t.c_cflag, CLOCAL);
563 if (ISSET(ch->ch_swflags, TIOCFLAG_CRTSCTS))
564 SET(t.c_cflag, CRTSCTS);
565 if (ISSET(ch->ch_swflags, TIOCFLAG_MDMBUF))
566 SET(t.c_cflag, MDMBUF);
567 /* Make sure sbjcn_param() will do something. */
568 tp->t_ospeed = 0;
569 (void) sbjcn_param(tp, &t);
570 tp->t_iflag = TTYDEF_IFLAG;
571 tp->t_oflag = TTYDEF_OFLAG;
572 tp->t_lflag = TTYDEF_LFLAG;
573 ttychars(tp);
574 ttsetwater(tp);
575
576 s2 = splserial();
577
578 /*
579 * Turn on DTR. We must always do this, even if carrier is not
580 * present, because otherwise we'd have to use TIOCSDTR
581 * immediately after setting CLOCAL, which applications do not
582 * expect. We always assert DTR while the device is open
583 * unless explicitly requested to deassert it.
584 */
585 sbjcn_modem(ch, 1);
586
587 /* Clear the input ring, and unblock. */
588 ch->ch_rbput = ch->ch_rbget = ch->ch_rbuf;
589 ch->ch_rbavail = sbjcn_rbuf_size;
590 sbjcn_iflush(ch);
591 CLR(ch->ch_rx_flags, RX_ANY_BLOCK);
592 sbjcn_dohwiflow(ch);
593
594 #ifdef SBJCN_DEBUG
595 if (sbjcn_debug)
596 sbjcn_status(ch, "sbjcnopen ");
597 #endif
598
599 splx(s2);
600 }
601
602 splx(s);
603
604 error = ttyopen(tp, SBJCN_DIALOUT(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 sbjcn_shutdown(ch);
621 }
622
623 return (error);
624 }
625
626 int
627 sbjcnclose(dev_t dev, int flag, int mode, struct proc *p)
628 {
629 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(dev)];
630 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
631 struct tty *tp = ch->ch_tty;
632
633 /* XXX This is for cons.c. */
634 if (!ISSET(tp->t_state, TS_ISOPEN))
635 return (0);
636
637 (*tp->t_linesw->l_close)(tp, flag);
638 ttyclose(tp);
639
640 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
641 /*
642 * Although we got a last close, the device may still be in
643 * use; e.g. if this was the dialout node, and there are still
644 * processes waiting for carrier on the non-dialout node.
645 */
646 sbjcn_shutdown(ch);
647 }
648
649 return (0);
650 }
651
652 int
653 sbjcnread(dev_t dev, struct uio *uio, int flag)
654 {
655 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(dev)];
656 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
657 struct tty *tp = ch->ch_tty;
658
659 return ((*tp->t_linesw->l_read)(tp, uio, flag));
660 }
661
662 int
663 sbjcnwrite(dev_t dev, struct uio *uio, int flag)
664 {
665 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(dev)];
666 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
667 struct tty *tp = ch->ch_tty;
668
669 return ((*tp->t_linesw->l_write)(tp, uio, flag));
670 }
671
672 struct tty *
673 sbjcntty(dev_t dev)
674 {
675 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(dev)];
676 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
677 struct tty *tp = ch->ch_tty;
678
679 return (tp);
680 }
681
682 int
683 sbjcnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
684 {
685 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(dev)];
686 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
687 struct tty *tp = ch->ch_tty;
688 int error;
689 int s;
690
691 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
692 if (error >= 0)
693 return (error);
694
695 error = ttioctl(tp, cmd, data, flag, p);
696 if (error >= 0)
697 return (error);
698
699 error = 0;
700
701 s = splserial();
702
703 switch (cmd) {
704 case TIOCSBRK:
705 sbjcn_break(ch, 1);
706 break;
707
708 case TIOCCBRK:
709 sbjcn_break(ch, 0);
710 break;
711
712 case TIOCSDTR:
713 sbjcn_modem(ch, 1);
714 break;
715
716 case TIOCCDTR:
717 sbjcn_modem(ch, 0);
718 break;
719
720 case TIOCGFLAGS:
721 *(int *)data = ch->ch_swflags;
722 break;
723
724 case TIOCSFLAGS:
725 error = suser(p->p_ucred, &p->p_acflag);
726 if (error)
727 break;
728 ch->ch_swflags = *(int *)data;
729 break;
730
731 case TIOCMSET:
732 case TIOCMBIS:
733 case TIOCMBIC:
734 tiocm_to_sbjcn(ch, cmd, *(int *)data);
735 break;
736
737 case TIOCMGET:
738 *(int *)data = sbjcn_to_tiocm(ch);
739 break;
740
741 default:
742 error = ENOTTY;
743 break;
744 }
745
746 splx(s);
747
748 #ifdef SBJCN_DEBUG
749 if (sbjcn_debug)
750 sbjcn_status(ch, "sbjcn_ioctl ");
751 #endif
752
753 return (error);
754 }
755
756 integrate void
757 sbjcn_schedrx(struct sbjcn_channel *ch)
758 {
759
760 ch->ch_rx_ready = 1;
761
762 /* Next callout will detect this flag. */
763 }
764
765 void
766 sbjcn_break(struct sbjcn_channel *ch, int onoff)
767 {
768 /* XXXKW do something? */
769 }
770
771 void
772 sbjcn_modem(struct sbjcn_channel *ch, int onoff)
773 {
774
775 if (ch->ch_o_dtr == 0)
776 return;
777
778 if (onoff)
779 SET(ch->ch_oports, ch->ch_o_dtr);
780 else
781 CLR(ch->ch_oports, ch->ch_o_dtr);
782
783 if (!ch->ch_heldchange) {
784 if (ch->ch_tx_busy) {
785 ch->ch_heldtbc = ch->ch_tbc;
786 ch->ch_tbc = 0;
787 ch->ch_heldchange = 1;
788 } else
789 sbjcn_loadchannelregs(ch);
790 }
791 }
792
793 void
794 tiocm_to_sbjcn(struct sbjcn_channel *ch, int how, int ttybits)
795 {
796 u_char bits;
797
798 bits = 0;
799 if (ISSET(ttybits, TIOCM_DTR))
800 SET(bits, ch->ch_o_dtr);
801 if (ISSET(ttybits, TIOCM_RTS))
802 SET(bits, ch->ch_o_rts);
803
804 switch (how) {
805 case TIOCMBIC:
806 CLR(ch->ch_oports, bits);
807 break;
808
809 case TIOCMBIS:
810 SET(ch->ch_oports, bits);
811 break;
812
813 case TIOCMSET:
814 ch->ch_oports = bits;
815 break;
816 }
817
818 if (!ch->ch_heldchange) {
819 if (ch->ch_tx_busy) {
820 ch->ch_heldtbc = ch->ch_tbc;
821 ch->ch_tbc = 0;
822 ch->ch_heldchange = 1;
823 } else
824 sbjcn_loadchannelregs(ch);
825 }
826 }
827
828 int
829 sbjcn_to_tiocm(struct sbjcn_channel *ch)
830 {
831 u_char hwbits;
832 int ttybits = 0;
833
834 hwbits = ch->ch_oports;
835 if (ISSET(hwbits, ch->ch_o_dtr))
836 SET(ttybits, TIOCM_DTR);
837 if (ISSET(hwbits, ch->ch_o_rts))
838 SET(ttybits, TIOCM_RTS);
839
840 hwbits = ch->ch_iports;
841 if (ISSET(hwbits, ch->ch_i_dcd))
842 SET(ttybits, TIOCM_CD);
843 if (ISSET(hwbits, ch->ch_i_cts))
844 SET(ttybits, TIOCM_CTS);
845 if (ISSET(hwbits, ch->ch_i_dsr))
846 SET(ttybits, TIOCM_DSR);
847 if (ISSET(hwbits, ch->ch_i_ri))
848 SET(ttybits, TIOCM_RI);
849
850 if (ch->ch_imr != 0)
851 SET(ttybits, TIOCM_LE);
852
853 return (ttybits);
854 }
855
856 static int
857 cflag2modes(cflag, mode1p, mode2p)
858 tcflag_t cflag;
859 u_char *mode1p;
860 u_char *mode2p;
861 {
862 u_char mode1;
863 u_char mode2;
864 int err = 0;
865
866 mode1 = mode2 = 0;
867
868 switch (ISSET(cflag, CSIZE)) {
869 case CS7:
870 mode1 |= 2; /* XXX */
871 break;
872 default:
873 err = -1;
874 /* FALLTHRU for sanity */
875 case CS8:
876 mode1 |= 3; /* XXX */
877 break;
878 }
879 if (!ISSET(cflag, PARENB))
880 mode1 |= 2 << 3;
881 else {
882 mode1 |= 0 << 3;
883 if (ISSET(cflag, PARODD))
884 mode1 |= 1 << 2;
885 }
886
887 if (ISSET(cflag, CSTOPB))
888 mode2 |= 1 << 3; /* two stop bits XXX not std */
889
890 if (ISSET(cflag, CRTSCTS)) {
891 mode1 |= 1 << 7;
892 mode2 |= 1 << 4;
893 }
894
895 *mode1p = mode1;
896 *mode2p = mode2;
897 return (err);
898 }
899
900 int
901 sbjcn_param(struct tty *tp, struct termios *t)
902 {
903 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(tp->t_dev)];
904 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
905 long brc;
906 u_char mode1, mode2;
907 int s;
908
909 /* XXX reset to console parameters if console? */
910 #if 0
911 XXX disable, enable.
912 #endif
913
914 /* Check requested parameters. */
915 if (sbjcn_speed(t->c_ospeed, &brc) < 0)
916 return (EINVAL);
917 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
918 return (EINVAL);
919
920 /*
921 * For the console, always force CLOCAL and !HUPCL, so that the port
922 * is always active.
923 */
924 if (ISSET(ch->ch_swflags, TIOCFLAG_SOFTCAR) ||
925 ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE)) {
926 SET(t->c_cflag, CLOCAL);
927 CLR(t->c_cflag, HUPCL);
928 }
929
930 /*
931 * If there were no changes, don't do anything. This avoids dropping
932 * input and improves performance when all we did was frob things like
933 * VMIN and VTIME.
934 */
935 if (tp->t_ospeed == t->c_ospeed &&
936 tp->t_cflag == t->c_cflag)
937 return (0);
938
939 if (cflag2modes(t->c_cflag, &mode1, &mode2) < 0)
940 return (EINVAL);
941
942 s = splserial();
943
944 ch->ch_mode1 = mode1;
945 ch->ch_mode2 = mode2;
946
947 /*
948 * If we're not in a mode that assumes a connection is present, then
949 * ignore carrier changes.
950 */
951 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
952 ch->ch_i_dcd = 0;
953 else
954 ch->ch_i_dcd = ch->ch_i_dcd_pin;
955 /*
956 * Set the flow control pins depending on the current flow control
957 * mode.
958 */
959 if (ISSET(t->c_cflag, CRTSCTS)) {
960 ch->ch_o_dtr = ch->ch_o_dtr_pin;
961 ch->ch_o_rts = ch->ch_o_rts_pin;
962 ch->ch_i_cts = ch->ch_i_cts_pin;
963 /* hw controle enable bits in mod regs set by cflag2modes */
964 } else if (ISSET(t->c_cflag, MDMBUF)) {
965 /*
966 * For DTR/DCD flow control, make sure we don't toggle DTR for
967 * carrier detection.
968 */
969 ch->ch_o_dtr = 0;
970 ch->ch_o_rts = ch->ch_o_dtr_pin;
971 ch->ch_i_cts = ch->ch_i_dcd_pin;
972 } else {
973 /*
974 * If no flow control, then always set RTS. This will make
975 * the other side happy if it mistakenly thinks we're doing
976 * RTS/CTS flow control.
977 */
978 ch->ch_o_dtr = ch->ch_o_dtr_pin | ch->ch_o_rts_pin;
979 ch->ch_o_rts = 0;
980 ch->ch_i_cts = 0;
981 if (ISSET(ch->ch_oports, ch->ch_o_dtr_pin))
982 SET(ch->ch_oports, ch->ch_o_rts_pin);
983 else
984 CLR(ch->ch_oports, ch->ch_o_rts_pin);
985 }
986 /* XXX maybe mask the ports which generate intrs? */
987
988 ch->ch_brc = brc;
989
990 /* XXX maybe set fifo-full receive mode if RTSCTS and high speed? */
991
992 /* And copy to tty. */
993 tp->t_ispeed = 0;
994 tp->t_ospeed = t->c_ospeed;
995 tp->t_cflag = t->c_cflag;
996
997 if (!ch->ch_heldchange) {
998 if (ch->ch_tx_busy) {
999 ch->ch_heldtbc = ch->ch_tbc;
1000 ch->ch_tbc = 0;
1001 ch->ch_heldchange = 1;
1002 } else
1003 sbjcn_loadchannelregs(ch);
1004 }
1005
1006 if (!ISSET(t->c_cflag, CHWFLOW)) {
1007 /* Disable the high water mark. */
1008 ch->ch_r_hiwat = 0;
1009 ch->ch_r_lowat = 0;
1010 if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
1011 CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1012 sbjcn_schedrx(ch);
1013 }
1014 if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1015 CLR(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1016 sbjcn_dohwiflow(ch);
1017 }
1018 } else {
1019 ch->ch_r_hiwat = sbjcn_rbuf_hiwat;
1020 ch->ch_r_lowat = sbjcn_rbuf_lowat;
1021 }
1022
1023 splx(s);
1024
1025 /*
1026 * Update the tty layer's idea of the carrier bit, in case we changed
1027 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1028 * explicit request.
1029 */
1030 (void) (*tp->t_linesw->l_modem)(tp,
1031 ISSET(ch->ch_iports, ch->ch_i_dcd));
1032
1033 #ifdef SBJCN_DEBUG
1034 if (sbjcn_debug)
1035 sbjcn_status(ch, "sbjcnparam ");
1036 #endif
1037
1038 if (!ISSET(t->c_cflag, CHWFLOW)) {
1039 if (ch->ch_tx_stopped) {
1040 ch->ch_tx_stopped = 0;
1041 sbjcn_start(tp);
1042 }
1043 }
1044
1045 return (0);
1046 }
1047
1048 void
1049 sbjcn_iflush(struct sbjcn_channel *ch)
1050 {
1051 uint64_t reg;
1052 int timo;
1053
1054 timo = 50000;
1055 /* flush any pending I/O */
1056 while (((reg = READ_REG(ch->ch_input_reg)) != 0) && --timo)
1057 ;
1058
1059 #ifdef DIAGNOSTIC
1060 if (!timo)
1061 printf("%s: sbjcn_iflush timeout %02x\n",
1062 ch->ch_sc->sc_dev.dv_xname, reg);
1063 #endif
1064 }
1065
1066 void
1067 sbjcn_loadchannelregs(struct sbjcn_channel *ch)
1068 {
1069 }
1070
1071 int
1072 sbjcn_hwiflow(struct tty *tp, int block)
1073 {
1074 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(tp->t_dev)];
1075 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
1076 int s;
1077
1078 if (ch->ch_o_rts == 0)
1079 return (0);
1080
1081 s = splserial();
1082 if (block) {
1083 if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1084 SET(ch->ch_rx_flags, RX_TTY_BLOCKED);
1085 sbjcn_dohwiflow(ch);
1086 }
1087 } else {
1088 if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
1089 CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1090 sbjcn_schedrx(ch);
1091 }
1092 if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1093 CLR(ch->ch_rx_flags, RX_TTY_BLOCKED);
1094 sbjcn_dohwiflow(ch);
1095 }
1096 }
1097 splx(s);
1098 return (1);
1099 }
1100
1101 /*
1102 * (un)block input via hw flowcontrol
1103 */
1104 void
1105 sbjcn_dohwiflow(struct sbjcn_channel *ch)
1106 {
1107
1108 if (ch->ch_o_rts == 0)
1109 return;
1110
1111 if (ISSET(ch->ch_rx_flags, RX_ANY_BLOCK)) {
1112 CLR(ch->ch_oports, ch->ch_o_rts);
1113 CLR(ch->ch_oports_active, ch->ch_o_rts);
1114 } else {
1115 SET(ch->ch_oports, ch->ch_o_rts);
1116 SET(ch->ch_oports_active, ch->ch_o_rts);
1117 }
1118 }
1119
1120 void
1121 sbjcn_start(struct tty *tp)
1122 {
1123 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(tp->t_dev)];
1124 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
1125 int s;
1126
1127 s = spltty();
1128 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1129 goto out;
1130 if (ch->ch_tx_stopped)
1131 goto out;
1132
1133 if (tp->t_outq.c_cc <= tp->t_lowat) {
1134 if (ISSET(tp->t_state, TS_ASLEEP)) {
1135 CLR(tp->t_state, TS_ASLEEP);
1136 wakeup(&tp->t_outq);
1137 }
1138 selwakeup(&tp->t_wsel);
1139 if (tp->t_outq.c_cc == 0)
1140 goto out;
1141 }
1142
1143 /* Grab the first contiguous region of buffer space. */
1144 {
1145 u_char *tba;
1146 int tbc;
1147
1148 tba = tp->t_outq.c_cf;
1149 tbc = ndqb(&tp->t_outq, 0);
1150
1151 (void)splserial();
1152
1153 ch->ch_tba = tba;
1154 ch->ch_tbc = tbc;
1155 }
1156
1157 SET(tp->t_state, TS_BUSY);
1158 ch->ch_tx_busy = 1;
1159
1160 /* Output the first chunk of the contiguous buffer. */
1161 {
1162 while (ch->ch_tbc) {
1163 uint64_t data;
1164 int bytes, i;
1165
1166 bytes = (ch->ch_tbc > 7) ? 7 : ch->ch_tbc;
1167 data = bytes;
1168 for (i=0; i<bytes; i++) {
1169 data <<= 8;
1170 data |= *ch->ch_tba++;
1171 }
1172 if (bytes < 7)
1173 data <<= 56-(bytes<<3);
1174 ch->ch_tbc -= bytes;
1175 WRITE_REG(ch->ch_output_reg, data);
1176 }
1177 ch->ch_tx_busy = 0;
1178 ch->ch_tx_done = 1;
1179 }
1180 out:
1181 splx(s);
1182 return;
1183 }
1184
1185 /*
1186 * Stop output on a line.
1187 */
1188 void
1189 sbjcnstop(struct tty *tp, int flag)
1190 {
1191 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(tp->t_dev)];
1192 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
1193 int s;
1194
1195 s = splserial();
1196 if (ISSET(tp->t_state, TS_BUSY)) {
1197 /* Stop transmitting at the next chunk. */
1198 ch->ch_tbc = 0;
1199 ch->ch_heldtbc = 0;
1200 if (!ISSET(tp->t_state, TS_TTSTOP))
1201 SET(tp->t_state, TS_FLUSH);
1202 }
1203 splx(s);
1204 }
1205
1206 void
1207 sbjcn_diag(arg)
1208 void *arg;
1209 {
1210 struct sbjcn_channel *ch = arg;
1211 struct sbjcn_softc *sc = ch->ch_sc;
1212 int overflows, floods;
1213 int s;
1214
1215 s = splserial();
1216 overflows = ch->ch_overflows;
1217 ch->ch_overflows = 0;
1218 floods = ch->ch_floods;
1219 ch->ch_floods = 0;
1220 ch->ch_errors = 0;
1221 splx(s);
1222
1223 log(LOG_WARNING, "%s: channel %d: %d fifo overflow%s, %d ibuf flood%s\n",
1224 sc->sc_dev.dv_xname, ch->ch_num,
1225 overflows, overflows == 1 ? "" : "s",
1226 floods, floods == 1 ? "" : "s");
1227 }
1228
1229 integrate void
1230 sbjcn_rxsoft(struct sbjcn_channel *ch, struct tty *tp)
1231 {
1232 int (*rint)(int c, struct tty *tp) = tp->t_linesw->l_rint;
1233 u_char *get, *end;
1234 u_int cc, scc;
1235 u_char sr;
1236 int code;
1237 int s;
1238
1239 end = ch->ch_ebuf;
1240 get = ch->ch_rbget;
1241 scc = cc = sbjcn_rbuf_size - ch->ch_rbavail;
1242
1243 if (cc == sbjcn_rbuf_size) {
1244 ch->ch_floods++;
1245 if (ch->ch_errors++ == 0)
1246 callout_reset(&ch->ch_diag_callout, 60 * hz,
1247 sbjcn_diag, ch);
1248 }
1249
1250 while (cc) {
1251 code = get[0];
1252 sr = get[1];
1253 if (ISSET(sr, 0xf0)) {
1254 if (ISSET(sr, 0x10)) {
1255 ch->ch_overflows++;
1256 if (ch->ch_errors++ == 0)
1257 callout_reset(&ch->ch_diag_callout,
1258 60 * hz, sbjcn_diag, ch);
1259 }
1260 if (ISSET(sr, 0xc0))
1261 SET(code, TTY_FE);
1262 if (ISSET(sr, 0x20))
1263 SET(code, TTY_PE);
1264 }
1265 if ((*rint)(code, tp) == -1) {
1266 /*
1267 * The line discipline's buffer is out of space.
1268 */
1269 if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1270 /*
1271 * We're either not using flow control, or the
1272 * line discipline didn't tell us to block for
1273 * some reason. Either way, we have no way to
1274 * know when there's more space available, so
1275 * just drop the rest of the data.
1276 */
1277 get += cc << 1;
1278 if (get >= end)
1279 get -= sbjcn_rbuf_size << 1;
1280 cc = 0;
1281 } else {
1282 /*
1283 * Don't schedule any more receive processing
1284 * until the line discipline tells us there's
1285 * space available (through comhwiflow()).
1286 * Leave the rest of the data in the input
1287 * buffer.
1288 */
1289 SET(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1290 }
1291 break;
1292 }
1293 get += 2;
1294 if (get >= end)
1295 get = ch->ch_rbuf;
1296 cc--;
1297 }
1298
1299 if (cc != scc) {
1300 ch->ch_rbget = get;
1301 s = splserial();
1302 cc = ch->ch_rbavail += scc - cc;
1303 /* Buffers should be ok again, release possible block. */
1304 if (cc >= ch->ch_r_lowat) {
1305 if (ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
1306 CLR(ch->ch_rx_flags, RX_IBUF_OVERFLOWED);
1307 SET(ch->ch_imr, 0x02);
1308 }
1309 if (ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED)) {
1310 CLR(ch->ch_rx_flags, RX_IBUF_BLOCKED);
1311 sbjcn_dohwiflow(ch);
1312 }
1313 }
1314 splx(s);
1315 }
1316 }
1317
1318 integrate void
1319 sbjcn_txsoft(struct sbjcn_channel *ch, struct tty *tp)
1320 {
1321
1322 CLR(tp->t_state, TS_BUSY);
1323 if (ISSET(tp->t_state, TS_FLUSH))
1324 CLR(tp->t_state, TS_FLUSH);
1325 else
1326 ndflush(&tp->t_outq, (int)(ch->ch_tba - tp->t_outq.c_cf));
1327 (*tp->t_linesw->l_start)(tp);
1328 }
1329
1330 integrate void
1331 sbjcn_stsoft(struct sbjcn_channel *ch, struct tty *tp)
1332 {
1333 u_char iports, delta;
1334 int s;
1335
1336 s = splserial();
1337 iports = ch->ch_iports;
1338 delta = ch->ch_iports_delta;
1339 ch->ch_iports_delta = 0;
1340 splx(s);
1341
1342 if (ISSET(delta, ch->ch_i_dcd)) {
1343 /*
1344 * Inform the tty layer that carrier detect changed.
1345 */
1346 (void) (*tp->t_linesw->l_modem)(tp,
1347 ISSET(iports, ch->ch_i_dcd));
1348 }
1349
1350 if (ISSET(delta, ch->ch_i_cts)) {
1351 /* Block or unblock output according to flow control. */
1352 if (ISSET(iports, ch->ch_i_cts)) {
1353 ch->ch_tx_stopped = 0;
1354 (*tp->t_linesw->l_start)(tp);
1355 } else {
1356 ch->ch_tx_stopped = 1;
1357 }
1358 }
1359
1360 #ifdef SBJCN_DEBUG
1361 if (sbjcn_debug)
1362 sbjcn_status(ch, "sbjcn_stsoft");
1363 #endif
1364 }
1365
1366 integrate void
1367 sbjcn_recv(struct sbjcn_channel *ch)
1368 {
1369 u_char *put, *end;
1370 u_int cc;
1371
1372 end = ch->ch_ebuf;
1373 put = ch->ch_rbput;
1374 cc = ch->ch_rbavail;
1375
1376 /* XXX process break */
1377
1378 sbjcncn_grabdword(ch);
1379 if (ch->ch_waiting_input) {
1380 if (!ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
1381 while (cc > 0) {
1382 put[0] = sbjcncn_nextbyte(ch);
1383 put[1] = 1; /* XXXKW ? */
1384 put += 2;
1385 if (put >= end)
1386 put = ch->ch_rbuf;
1387 cc--;
1388
1389 if (!ch->ch_waiting_input)
1390 break;
1391 }
1392
1393 /*
1394 * Current string of incoming characters ended
1395 * because no more data was available or we
1396 * ran out of space. Schedule a receive event
1397 * if any data was received. If we're out of
1398 * space, turn off receive interrupts.
1399 */
1400 ch->ch_rbput = put;
1401 ch->ch_rbavail = cc;
1402 if (!ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED))
1403 ch->ch_rx_ready = 1;
1404
1405 /*
1406 * See if we are in danger of overflowing a
1407 * buffer. If so, use hardware flow control
1408 * to ease the pressure.
1409 */
1410 if (!ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED) &&
1411 cc < ch->ch_r_hiwat) {
1412 SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
1413 sbjcn_dohwiflow(ch);
1414 }
1415
1416 /*
1417 * If we're out of space, disable receive
1418 * interrupts until the queue has drained
1419 * a bit.
1420 */
1421 if (!cc) {
1422 SET(ch->ch_rx_flags,
1423 RX_IBUF_OVERFLOWED);
1424 CLR(ch->ch_imr, 0x02);
1425 }
1426 } else {
1427 /* XXX panic? */
1428 CLR(ch->ch_imr, 0x02);
1429 // continue;
1430 }
1431 }
1432
1433 /*
1434 * If we've delayed a parameter change, do it now, and restart
1435 * output.
1436 */
1437 if (ch->ch_heldchange) {
1438 sbjcn_loadchannelregs(ch);
1439 ch->ch_heldchange = 0;
1440 ch->ch_tbc = ch->ch_heldtbc;
1441 ch->ch_heldtbc = 0;
1442 }
1443 }
1444
1445 void
1446 sbjcn_callout(void *arg)
1447 {
1448 struct sbjcn_channel *ch = arg;
1449 struct tty *tp = ch->ch_tty;
1450
1451 /* XXX get stuff */
1452 sbjcn_recv(ch);
1453
1454 /* XXX check receive */
1455 if (ch->ch_rx_ready) {
1456 ch->ch_rx_ready = 0;
1457 sbjcn_rxsoft(ch, tp);
1458 }
1459
1460 /* XXX check transmit */
1461 if (ch->ch_tx_done) {
1462 ch->ch_tx_done = 0;
1463 sbjcn_txsoft(ch, tp);
1464 }
1465
1466 callout_reset(&ch->ch_callout, hz/10, sbjcn_callout, ch);
1467 }
1468
1469 static char sbjcncn_nextbyte(struct sbjcn_channel *ch)
1470 {
1471 char c;
1472
1473 sbjcncn_grabdword(ch);
1474 c = (ch->ch_input_buf >> 56) & 0xff;
1475 ch->ch_input_buf <<= 8;
1476 ch->ch_waiting_input--;
1477 return c;
1478 }
1479
1480 static void sbjcncn_grabdword(struct sbjcn_channel *ch)
1481 {
1482 uint64_t inbuf;
1483
1484 if (ch->ch_waiting_input)
1485 return;
1486
1487 inbuf = READ_REG(ch->ch_input_reg);
1488 ch->ch_waiting_input = jtag_input_len(inbuf);
1489 ch->ch_input_buf = inbuf << 8;
1490 }
1491
1492 /*
1493 * Initialize UART for use as console or KGDB line.
1494 */
1495 int
1496 sbjcn_init(u_long addr, int chan, int rate, tcflag_t cflag)
1497 {
1498 /* XXXKW Anything to do here? */
1499 return (0);
1500 }
1501
1502 /*
1503 * Following are all routines needed for sbjcn to act as console
1504 */
1505 int
1506 sbjcn_cnattach(u_long addr, int chan, int rate, tcflag_t cflag)
1507 {
1508 int res;
1509 uint64_t ctrl_val;
1510 static struct consdev sbjcn_cons = {
1511 NULL, NULL, sbjcn_cngetc, sbjcn_cnputc, sbjcn_cnpollc, NULL,
1512 NODEV, CN_NORMAL
1513 };
1514
1515 res = sbjcn_init(addr, chan, rate, cflag);
1516 if (res)
1517 return (res);
1518
1519 cn_tab = &sbjcn_cons;
1520
1521 sbjcn_cons_present = 1;
1522 sbjcn_cons_addr = addr;
1523 sbjcn_cons_waiting_input = 0;
1524 sbjcn_cons_chan = chan;
1525 sbjcn_cons_rate = rate;
1526 sbjcn_cons_cflag = cflag;
1527
1528 /* Wait for sign of life from the other end */
1529 while ((ctrl_val = READ_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_CONTROL))) == 0)
1530 ;
1531
1532 return (ctrl_val != JTAG_CONS_MAGICNUM);
1533 }
1534
1535 int
1536 sbjcn_cngetc(dev_t dev)
1537 {
1538 char c;
1539
1540 while (sbjcn_cons_waiting_input == 0)
1541 sbjcn_cngrabdword();
1542
1543 c = (sbjcn_cons_input_buf >> 56) & 0xff;
1544 sbjcn_cons_input_buf <<= 8;
1545 sbjcn_cons_waiting_input--;
1546
1547 return c;
1548 }
1549
1550 /*
1551 * Console kernel output character routine.
1552 */
1553 void
1554 sbjcn_cnputc(dev_t dev, int c)
1555 {
1556 uint64_t outbuf;
1557
1558 outbuf = (1LL << 56) | (((uint64_t)c) << 48);
1559 WRITE_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_OUTPUT), outbuf);
1560 }
1561
1562 void
1563 sbjcn_cnpollc(dev_t dev, int on)
1564 {
1565
1566 }
1567
1568 static void sbjcn_cngrabdword(void)
1569 {
1570 uint64_t inbuf;
1571
1572 if (sbjcn_cons_waiting_input)
1573 return;
1574
1575 inbuf = READ_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_INPUT));
1576 sbjcn_cons_waiting_input = jtag_input_len(inbuf);
1577 sbjcn_cons_input_buf = inbuf << 8;
1578 }
1579
1580 #ifdef KGDB
1581 int
1582 sbjcn_kgdb_attach(u_long addr, int chan, int rate, tcflag_t cflag)
1583 {
1584 int res;
1585
1586 if (!sbjcn_cons_present &&
1587 sbjcn_cons_addr == addr && sbjcn_cons_chan == chan)
1588 return (EBUSY); /* cannot share with console */
1589
1590 res = sbjcn_init(addr, chan, rate, cflag);
1591 if (res)
1592 return (res);
1593
1594 kgdb_attach(sbjcn_kgdb_getc, sbjcn_kgdb_putc, NULL);
1595 kgdb_dev = 123; /* unneeded, only to satisfy some tests */
1596 kgdb_rate = rate;
1597
1598 sbjcn_kgdb_present = 1;
1599 /* XXX sbjcn_init wants addr, but we need the offset addr */
1600 sbjcn_kgdb_addr = addr + (chan * 0x100);
1601 sbjcn_kgdb_chan = chan;
1602
1603 return (0);
1604 }
1605
1606 /* ARGSUSED */
1607 int
1608 sbjcn_kgdb_getc(arg)
1609 void *arg;
1610 {
1611
1612 return (sbjcn_common_getc(sbjcn_kgdb_addr, sbjcn_kgdb_chan));
1613 }
1614
1615 /* ARGSUSED */
1616 void
1617 sbjcn_kgdb_putc(arg, c)
1618 void *arg;
1619 int c;
1620 {
1621
1622 sbjcn_common_putc(sbjcn_kgdb_addr, sbjcn_kgdb_chan, c);
1623 }
1624 #endif /* KGDB */
1625
1626 /*
1627 * helper function to identify the sbjcn channels used by
1628 * console or KGDB (and not yet autoconf attached)
1629 */
1630 int
1631 sbjcn_is_console(u_long addr, int chan)
1632 {
1633
1634 if (sbjcn_cons_present && !sbjcn_cons_attached &&
1635 sbjcn_cons_addr == addr && sbjcn_cons_chan == chan)
1636 return (1);
1637 #ifdef KGDB
1638 if (sbjcn_kgdb_present && !sbjcn_kgdb_attached &&
1639 sbjcn_kgdb_addr == addr && sbjcn_kgdb_chan == chan)
1640 return (1);
1641 #endif
1642 return (0);
1643 }
1644