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