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