sbjcn.c revision 1.19.16.1 1 /* $NetBSD: sbjcn.c,v 1.19.16.1 2008/05/18 12:32:27 yamt 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.19.16.1 2008/05/18 12:32:27 yamt 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 unit = SBJCN_UNIT(dev);
489 int chan = SBJCN_CHAN(dev);
490 struct sbjcn_softc *sc;
491 struct sbjcn_channel *ch;
492 struct tty *tp;
493 int s, s2;
494 int error;
495
496 if (unit >= sbjcn_cd.cd_ndevs)
497 return (ENXIO);
498 sc = sbjcn_cd.cd_devs[unit];
499 if (sc == 0)
500 return (ENXIO);
501 ch = &sc->sc_channels[chan];
502 if (!ISSET(ch->ch_hwflags, SBJCN_HW_DEV_OK) || ch->ch_rbuf == NULL)
503 return (ENXIO);
504
505 #ifdef KGDB
506 /*
507 * If this is the kgdb port, no other use is permitted.
508 */
509 if (ISSET(ch->ch_hwflags, SBJCN_HW_KGDB))
510 return (EBUSY);
511 #endif
512
513 tp = ch->ch_tty;
514
515 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
516 return (EBUSY);
517
518 s = spltty();
519
520 /*
521 * Do the following iff this is a first open.
522 */
523 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
524 struct termios t;
525
526 tp->t_dev = dev;
527
528 s2 = splserial();
529
530 /* Turn on receive, break, and status change interrupts. */
531 ch->ch_imr = 0xe;
532
533 /* Fetch the current modem control status, needed later. */
534 ch->ch_iports = 0;
535 ch->ch_iports_delta = 0;
536 splx(s2);
537
538 /*
539 * Initialize the termios status to the defaults. Add in the
540 * sticky bits from TIOCSFLAGS.
541 */
542 t.c_ispeed = 0;
543 if (ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE)) {
544 t.c_ospeed = sbjcn_cons_rate;
545 t.c_cflag = sbjcn_cons_cflag;
546 } else {
547 t.c_ospeed = TTYDEF_SPEED;
548 t.c_cflag = TTYDEF_CFLAG;
549 }
550 if (ISSET(ch->ch_swflags, TIOCFLAG_CLOCAL))
551 SET(t.c_cflag, CLOCAL);
552 if (ISSET(ch->ch_swflags, TIOCFLAG_CRTSCTS))
553 SET(t.c_cflag, CRTSCTS);
554 if (ISSET(ch->ch_swflags, TIOCFLAG_MDMBUF))
555 SET(t.c_cflag, MDMBUF);
556 /* Make sure sbjcn_param() will do something. */
557 tp->t_ospeed = 0;
558 (void) sbjcn_param(tp, &t);
559 tp->t_iflag = TTYDEF_IFLAG;
560 tp->t_oflag = TTYDEF_OFLAG;
561 tp->t_lflag = TTYDEF_LFLAG;
562 ttychars(tp);
563 ttsetwater(tp);
564
565 s2 = splserial();
566
567 /*
568 * Turn on DTR. We must always do this, even if carrier is not
569 * present, because otherwise we'd have to use TIOCSDTR
570 * immediately after setting CLOCAL, which applications do not
571 * expect. We always assert DTR while the device is open
572 * unless explicitly requested to deassert it.
573 */
574 sbjcn_modem(ch, 1);
575
576 /* Clear the input ring, and unblock. */
577 ch->ch_rbput = ch->ch_rbget = ch->ch_rbuf;
578 ch->ch_rbavail = sbjcn_rbuf_size;
579 sbjcn_iflush(ch);
580 CLR(ch->ch_rx_flags, RX_ANY_BLOCK);
581 sbjcn_dohwiflow(ch);
582
583 #ifdef SBJCN_DEBUG
584 if (sbjcn_debug)
585 sbjcn_status(ch, "sbjcnopen ");
586 #endif
587
588 splx(s2);
589 }
590
591 splx(s);
592
593 error = ttyopen(tp, SBJCN_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
594 if (error)
595 goto bad;
596
597 error = (*tp->t_linesw->l_open)(dev, tp);
598 if (error)
599 goto bad;
600
601 return (0);
602
603 bad:
604 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
605 /*
606 * We failed to open the device, and nobody else had it opened.
607 * Clean up the state as appropriate.
608 */
609 sbjcn_shutdown(ch);
610 }
611
612 return (error);
613 }
614
615 int
616 sbjcnclose(dev_t dev, int flag, int mode, struct proc *p)
617 {
618 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(dev)];
619 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
620 struct tty *tp = ch->ch_tty;
621
622 /* XXX This is for cons.c. */
623 if (!ISSET(tp->t_state, TS_ISOPEN))
624 return (0);
625
626 (*tp->t_linesw->l_close)(tp, flag);
627 ttyclose(tp);
628
629 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
630 /*
631 * Although we got a last close, the device may still be in
632 * use; e.g. if this was the dialout node, and there are still
633 * processes waiting for carrier on the non-dialout node.
634 */
635 sbjcn_shutdown(ch);
636 }
637
638 return (0);
639 }
640
641 int
642 sbjcnread(dev_t dev, struct uio *uio, int flag)
643 {
644 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(dev)];
645 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(dev)];
646 struct tty *tp = ch->ch_tty;
647
648 return ((*tp->t_linesw->l_read)(tp, uio, flag));
649 }
650
651 int
652 sbjcnwrite(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_write)(tp, uio, flag));
659 }
660
661 struct tty *
662 sbjcntty(dev_t dev)
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);
669 }
670
671 int
672 sbjcnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
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 int error;
678 int s;
679
680 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
681 if (error >= 0)
682 return (error);
683
684 error = ttioctl(tp, cmd, data, flag, p);
685 if (error >= 0)
686 return (error);
687
688 error = 0;
689
690 s = splserial();
691
692 switch (cmd) {
693 case TIOCSBRK:
694 sbjcn_break(ch, 1);
695 break;
696
697 case TIOCCBRK:
698 sbjcn_break(ch, 0);
699 break;
700
701 case TIOCSDTR:
702 sbjcn_modem(ch, 1);
703 break;
704
705 case TIOCCDTR:
706 sbjcn_modem(ch, 0);
707 break;
708
709 case TIOCGFLAGS:
710 *(int *)data = ch->ch_swflags;
711 break;
712
713 case TIOCSFLAGS:
714 error = kauth_authorize_device_tty(l->l_cred,
715 KAUTH_DEVICE_TTY_PRIVSET, tp);
716 if (error)
717 break;
718 ch->ch_swflags = *(int *)data;
719 break;
720
721 case TIOCMSET:
722 case TIOCMBIS:
723 case TIOCMBIC:
724 tiocm_to_sbjcn(ch, cmd, *(int *)data);
725 break;
726
727 case TIOCMGET:
728 *(int *)data = sbjcn_to_tiocm(ch);
729 break;
730
731 default:
732 error = ENOTTY;
733 break;
734 }
735
736 splx(s);
737
738 #ifdef SBJCN_DEBUG
739 if (sbjcn_debug)
740 sbjcn_status(ch, "sbjcn_ioctl ");
741 #endif
742
743 return (error);
744 }
745
746 integrate void
747 sbjcn_schedrx(struct sbjcn_channel *ch)
748 {
749
750 ch->ch_rx_ready = 1;
751
752 /* Next callout will detect this flag. */
753 }
754
755 void
756 sbjcn_break(struct sbjcn_channel *ch, int onoff)
757 {
758 /* XXXKW do something? */
759 }
760
761 void
762 sbjcn_modem(struct sbjcn_channel *ch, int onoff)
763 {
764
765 if (ch->ch_o_dtr == 0)
766 return;
767
768 if (onoff)
769 SET(ch->ch_oports, ch->ch_o_dtr);
770 else
771 CLR(ch->ch_oports, ch->ch_o_dtr);
772
773 if (!ch->ch_heldchange) {
774 if (ch->ch_tx_busy) {
775 ch->ch_heldtbc = ch->ch_tbc;
776 ch->ch_tbc = 0;
777 ch->ch_heldchange = 1;
778 } else
779 sbjcn_loadchannelregs(ch);
780 }
781 }
782
783 void
784 tiocm_to_sbjcn(struct sbjcn_channel *ch, int how, int ttybits)
785 {
786 u_char bits;
787
788 bits = 0;
789 if (ISSET(ttybits, TIOCM_DTR))
790 SET(bits, ch->ch_o_dtr);
791 if (ISSET(ttybits, TIOCM_RTS))
792 SET(bits, ch->ch_o_rts);
793
794 switch (how) {
795 case TIOCMBIC:
796 CLR(ch->ch_oports, bits);
797 break;
798
799 case TIOCMBIS:
800 SET(ch->ch_oports, bits);
801 break;
802
803 case TIOCMSET:
804 ch->ch_oports = bits;
805 break;
806 }
807
808 if (!ch->ch_heldchange) {
809 if (ch->ch_tx_busy) {
810 ch->ch_heldtbc = ch->ch_tbc;
811 ch->ch_tbc = 0;
812 ch->ch_heldchange = 1;
813 } else
814 sbjcn_loadchannelregs(ch);
815 }
816 }
817
818 int
819 sbjcn_to_tiocm(struct sbjcn_channel *ch)
820 {
821 u_char hwbits;
822 int ttybits = 0;
823
824 hwbits = ch->ch_oports;
825 if (ISSET(hwbits, ch->ch_o_dtr))
826 SET(ttybits, TIOCM_DTR);
827 if (ISSET(hwbits, ch->ch_o_rts))
828 SET(ttybits, TIOCM_RTS);
829
830 hwbits = ch->ch_iports;
831 if (ISSET(hwbits, ch->ch_i_dcd))
832 SET(ttybits, TIOCM_CD);
833 if (ISSET(hwbits, ch->ch_i_cts))
834 SET(ttybits, TIOCM_CTS);
835 if (ISSET(hwbits, ch->ch_i_dsr))
836 SET(ttybits, TIOCM_DSR);
837 if (ISSET(hwbits, ch->ch_i_ri))
838 SET(ttybits, TIOCM_RI);
839
840 if (ch->ch_imr != 0)
841 SET(ttybits, TIOCM_LE);
842
843 return (ttybits);
844 }
845
846 static int
847 cflag2modes(cflag, mode1p, mode2p)
848 tcflag_t cflag;
849 u_char *mode1p;
850 u_char *mode2p;
851 {
852 u_char mode1;
853 u_char mode2;
854 int err = 0;
855
856 mode1 = mode2 = 0;
857
858 switch (ISSET(cflag, CSIZE)) {
859 case CS7:
860 mode1 |= 2; /* XXX */
861 break;
862 default:
863 err = -1;
864 /* FALLTHRU for sanity */
865 case CS8:
866 mode1 |= 3; /* XXX */
867 break;
868 }
869 if (!ISSET(cflag, PARENB))
870 mode1 |= 2 << 3;
871 else {
872 mode1 |= 0 << 3;
873 if (ISSET(cflag, PARODD))
874 mode1 |= 1 << 2;
875 }
876
877 if (ISSET(cflag, CSTOPB))
878 mode2 |= 1 << 3; /* two stop bits XXX not std */
879
880 if (ISSET(cflag, CRTSCTS)) {
881 mode1 |= 1 << 7;
882 mode2 |= 1 << 4;
883 }
884
885 *mode1p = mode1;
886 *mode2p = mode2;
887 return (err);
888 }
889
890 int
891 sbjcn_param(struct tty *tp, struct termios *t)
892 {
893 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(tp->t_dev)];
894 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
895 long brc;
896 u_char mode1, mode2;
897 int s;
898
899 /* XXX reset to console parameters if console? */
900 #if 0
901 XXX disable, enable.
902 #endif
903
904 /* Check requested parameters. */
905 if (sbjcn_speed(t->c_ospeed, &brc) < 0)
906 return (EINVAL);
907 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
908 return (EINVAL);
909
910 /*
911 * For the console, always force CLOCAL and !HUPCL, so that the port
912 * is always active.
913 */
914 if (ISSET(ch->ch_swflags, TIOCFLAG_SOFTCAR) ||
915 ISSET(ch->ch_hwflags, SBJCN_HW_CONSOLE)) {
916 SET(t->c_cflag, CLOCAL);
917 CLR(t->c_cflag, HUPCL);
918 }
919
920 /*
921 * If there were no changes, don't do anything. This avoids dropping
922 * input and improves performance when all we did was frob things like
923 * VMIN and VTIME.
924 */
925 if (tp->t_ospeed == t->c_ospeed &&
926 tp->t_cflag == t->c_cflag)
927 return (0);
928
929 if (cflag2modes(t->c_cflag, &mode1, &mode2) < 0)
930 return (EINVAL);
931
932 s = splserial();
933
934 ch->ch_mode1 = mode1;
935 ch->ch_mode2 = mode2;
936
937 /*
938 * If we're not in a mode that assumes a connection is present, then
939 * ignore carrier changes.
940 */
941 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
942 ch->ch_i_dcd = 0;
943 else
944 ch->ch_i_dcd = ch->ch_i_dcd_pin;
945 /*
946 * Set the flow control pins depending on the current flow control
947 * mode.
948 */
949 if (ISSET(t->c_cflag, CRTSCTS)) {
950 ch->ch_o_dtr = ch->ch_o_dtr_pin;
951 ch->ch_o_rts = ch->ch_o_rts_pin;
952 ch->ch_i_cts = ch->ch_i_cts_pin;
953 /* hw controle enable bits in mod regs set by cflag2modes */
954 } else if (ISSET(t->c_cflag, MDMBUF)) {
955 /*
956 * For DTR/DCD flow control, make sure we don't toggle DTR for
957 * carrier detection.
958 */
959 ch->ch_o_dtr = 0;
960 ch->ch_o_rts = ch->ch_o_dtr_pin;
961 ch->ch_i_cts = ch->ch_i_dcd_pin;
962 } else {
963 /*
964 * If no flow control, then always set RTS. This will make
965 * the other side happy if it mistakenly thinks we're doing
966 * RTS/CTS flow control.
967 */
968 ch->ch_o_dtr = ch->ch_o_dtr_pin | ch->ch_o_rts_pin;
969 ch->ch_o_rts = 0;
970 ch->ch_i_cts = 0;
971 if (ISSET(ch->ch_oports, ch->ch_o_dtr_pin))
972 SET(ch->ch_oports, ch->ch_o_rts_pin);
973 else
974 CLR(ch->ch_oports, ch->ch_o_rts_pin);
975 }
976 /* XXX maybe mask the ports which generate intrs? */
977
978 ch->ch_brc = brc;
979
980 /* XXX maybe set fifo-full receive mode if RTSCTS and high speed? */
981
982 /* And copy to tty. */
983 tp->t_ispeed = 0;
984 tp->t_ospeed = t->c_ospeed;
985 tp->t_cflag = t->c_cflag;
986
987 if (!ch->ch_heldchange) {
988 if (ch->ch_tx_busy) {
989 ch->ch_heldtbc = ch->ch_tbc;
990 ch->ch_tbc = 0;
991 ch->ch_heldchange = 1;
992 } else
993 sbjcn_loadchannelregs(ch);
994 }
995
996 if (!ISSET(t->c_cflag, CHWFLOW)) {
997 /* Disable the high water mark. */
998 ch->ch_r_hiwat = 0;
999 ch->ch_r_lowat = 0;
1000 if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
1001 CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1002 sbjcn_schedrx(ch);
1003 }
1004 if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1005 CLR(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1006 sbjcn_dohwiflow(ch);
1007 }
1008 } else {
1009 ch->ch_r_hiwat = sbjcn_rbuf_hiwat;
1010 ch->ch_r_lowat = sbjcn_rbuf_lowat;
1011 }
1012
1013 splx(s);
1014
1015 /*
1016 * Update the tty layer's idea of the carrier bit, in case we changed
1017 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1018 * explicit request.
1019 */
1020 (void) (*tp->t_linesw->l_modem)(tp,
1021 ISSET(ch->ch_iports, ch->ch_i_dcd));
1022
1023 #ifdef SBJCN_DEBUG
1024 if (sbjcn_debug)
1025 sbjcn_status(ch, "sbjcnparam ");
1026 #endif
1027
1028 if (!ISSET(t->c_cflag, CHWFLOW)) {
1029 if (ch->ch_tx_stopped) {
1030 ch->ch_tx_stopped = 0;
1031 sbjcn_start(tp);
1032 }
1033 }
1034
1035 return (0);
1036 }
1037
1038 void
1039 sbjcn_iflush(struct sbjcn_channel *ch)
1040 {
1041 uint64_t reg;
1042 int timo;
1043
1044 timo = 50000;
1045 /* flush any pending I/O */
1046 while (((reg = READ_REG(ch->ch_input_reg)) != 0) && --timo)
1047 ;
1048
1049 #ifdef DIAGNOSTIC
1050 if (!timo)
1051 printf("%s: sbjcn_iflush timeout %02x\n",
1052 ch->ch_sc->sc_dev.dv_xname, reg);
1053 #endif
1054 }
1055
1056 void
1057 sbjcn_loadchannelregs(struct sbjcn_channel *ch)
1058 {
1059 }
1060
1061 int
1062 sbjcn_hwiflow(struct tty *tp, int block)
1063 {
1064 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(tp->t_dev)];
1065 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
1066 int s;
1067
1068 if (ch->ch_o_rts == 0)
1069 return (0);
1070
1071 s = splserial();
1072 if (block) {
1073 if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1074 SET(ch->ch_rx_flags, RX_TTY_BLOCKED);
1075 sbjcn_dohwiflow(ch);
1076 }
1077 } else {
1078 if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
1079 CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1080 sbjcn_schedrx(ch);
1081 }
1082 if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1083 CLR(ch->ch_rx_flags, RX_TTY_BLOCKED);
1084 sbjcn_dohwiflow(ch);
1085 }
1086 }
1087 splx(s);
1088 return (1);
1089 }
1090
1091 /*
1092 * (un)block input via hw flowcontrol
1093 */
1094 void
1095 sbjcn_dohwiflow(struct sbjcn_channel *ch)
1096 {
1097
1098 if (ch->ch_o_rts == 0)
1099 return;
1100
1101 if (ISSET(ch->ch_rx_flags, RX_ANY_BLOCK)) {
1102 CLR(ch->ch_oports, ch->ch_o_rts);
1103 CLR(ch->ch_oports_active, ch->ch_o_rts);
1104 } else {
1105 SET(ch->ch_oports, ch->ch_o_rts);
1106 SET(ch->ch_oports_active, ch->ch_o_rts);
1107 }
1108 }
1109
1110 void
1111 sbjcn_start(struct tty *tp)
1112 {
1113 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(tp->t_dev)];
1114 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
1115 int s;
1116
1117 s = spltty();
1118 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1119 goto out;
1120 if (ch->ch_tx_stopped)
1121 goto out;
1122 if (!ttypull(tp))
1123 goto out;
1124
1125 /* Grab the first contiguous region of buffer space. */
1126 {
1127 u_char *tba;
1128 int tbc;
1129
1130 tba = tp->t_outq.c_cf;
1131 tbc = ndqb(&tp->t_outq, 0);
1132
1133 (void)splserial();
1134
1135 ch->ch_tba = tba;
1136 ch->ch_tbc = tbc;
1137 }
1138
1139 SET(tp->t_state, TS_BUSY);
1140 ch->ch_tx_busy = 1;
1141
1142 /* Output the first chunk of the contiguous buffer. */
1143 {
1144 while (ch->ch_tbc) {
1145 uint64_t data;
1146 int bytes, i;
1147
1148 bytes = (ch->ch_tbc > 7) ? 7 : ch->ch_tbc;
1149 data = bytes;
1150 for (i=0; i<bytes; i++) {
1151 data <<= 8;
1152 data |= *ch->ch_tba++;
1153 }
1154 if (bytes < 7)
1155 data <<= 56-(bytes<<3);
1156 ch->ch_tbc -= bytes;
1157 WRITE_REG(ch->ch_output_reg, data);
1158 }
1159 ch->ch_tx_busy = 0;
1160 ch->ch_tx_done = 1;
1161 }
1162 out:
1163 splx(s);
1164 return;
1165 }
1166
1167 /*
1168 * Stop output on a line.
1169 */
1170 void
1171 sbjcnstop(struct tty *tp, int flag)
1172 {
1173 struct sbjcn_softc *sc = sbjcn_cd.cd_devs[SBJCN_UNIT(tp->t_dev)];
1174 struct sbjcn_channel *ch = &sc->sc_channels[SBJCN_CHAN(tp->t_dev)];
1175 int s;
1176
1177 s = splserial();
1178 if (ISSET(tp->t_state, TS_BUSY)) {
1179 /* Stop transmitting at the next chunk. */
1180 ch->ch_tbc = 0;
1181 ch->ch_heldtbc = 0;
1182 if (!ISSET(tp->t_state, TS_TTSTOP))
1183 SET(tp->t_state, TS_FLUSH);
1184 }
1185 splx(s);
1186 }
1187
1188 void
1189 sbjcn_diag(arg)
1190 void *arg;
1191 {
1192 struct sbjcn_channel *ch = arg;
1193 struct sbjcn_softc *sc = ch->ch_sc;
1194 int overflows, floods;
1195 int s;
1196
1197 s = splserial();
1198 overflows = ch->ch_overflows;
1199 ch->ch_overflows = 0;
1200 floods = ch->ch_floods;
1201 ch->ch_floods = 0;
1202 ch->ch_errors = 0;
1203 splx(s);
1204
1205 log(LOG_WARNING, "%s: channel %d: %d fifo overflow%s, %d ibuf flood%s\n",
1206 sc->sc_dev.dv_xname, ch->ch_num,
1207 overflows, overflows == 1 ? "" : "s",
1208 floods, floods == 1 ? "" : "s");
1209 }
1210
1211 integrate void
1212 sbjcn_rxsoft(struct sbjcn_channel *ch, struct tty *tp)
1213 {
1214 int (*rint)(int c, struct tty *tp) = tp->t_linesw->l_rint;
1215 u_char *get, *end;
1216 u_int cc, scc;
1217 u_char sr;
1218 int code;
1219 int s;
1220
1221 end = ch->ch_ebuf;
1222 get = ch->ch_rbget;
1223 scc = cc = sbjcn_rbuf_size - ch->ch_rbavail;
1224
1225 if (cc == sbjcn_rbuf_size) {
1226 ch->ch_floods++;
1227 if (ch->ch_errors++ == 0)
1228 callout_reset(&ch->ch_diag_callout, 60 * hz,
1229 sbjcn_diag, ch);
1230 }
1231
1232 while (cc) {
1233 code = get[0];
1234 sr = get[1];
1235 if (ISSET(sr, 0xf0)) {
1236 if (ISSET(sr, 0x10)) {
1237 ch->ch_overflows++;
1238 if (ch->ch_errors++ == 0)
1239 callout_reset(&ch->ch_diag_callout,
1240 60 * hz, sbjcn_diag, ch);
1241 }
1242 if (ISSET(sr, 0xc0))
1243 SET(code, TTY_FE);
1244 if (ISSET(sr, 0x20))
1245 SET(code, TTY_PE);
1246 }
1247 if ((*rint)(code, tp) == -1) {
1248 /*
1249 * The line discipline's buffer is out of space.
1250 */
1251 if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1252 /*
1253 * We're either not using flow control, or the
1254 * line discipline didn't tell us to block for
1255 * some reason. Either way, we have no way to
1256 * know when there's more space available, so
1257 * just drop the rest of the data.
1258 */
1259 get += cc << 1;
1260 if (get >= end)
1261 get -= sbjcn_rbuf_size << 1;
1262 cc = 0;
1263 } else {
1264 /*
1265 * Don't schedule any more receive processing
1266 * until the line discipline tells us there's
1267 * space available (through comhwiflow()).
1268 * Leave the rest of the data in the input
1269 * buffer.
1270 */
1271 SET(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1272 }
1273 break;
1274 }
1275 get += 2;
1276 if (get >= end)
1277 get = ch->ch_rbuf;
1278 cc--;
1279 }
1280
1281 if (cc != scc) {
1282 ch->ch_rbget = get;
1283 s = splserial();
1284 cc = ch->ch_rbavail += scc - cc;
1285 /* Buffers should be ok again, release possible block. */
1286 if (cc >= ch->ch_r_lowat) {
1287 if (ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
1288 CLR(ch->ch_rx_flags, RX_IBUF_OVERFLOWED);
1289 SET(ch->ch_imr, 0x02);
1290 }
1291 if (ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED)) {
1292 CLR(ch->ch_rx_flags, RX_IBUF_BLOCKED);
1293 sbjcn_dohwiflow(ch);
1294 }
1295 }
1296 splx(s);
1297 }
1298 }
1299
1300 integrate void
1301 sbjcn_txsoft(struct sbjcn_channel *ch, struct tty *tp)
1302 {
1303
1304 CLR(tp->t_state, TS_BUSY);
1305 if (ISSET(tp->t_state, TS_FLUSH))
1306 CLR(tp->t_state, TS_FLUSH);
1307 else
1308 ndflush(&tp->t_outq, (int)(ch->ch_tba - tp->t_outq.c_cf));
1309 (*tp->t_linesw->l_start)(tp);
1310 }
1311
1312 integrate void
1313 sbjcn_stsoft(struct sbjcn_channel *ch, struct tty *tp)
1314 {
1315 u_char iports, delta;
1316 int s;
1317
1318 s = splserial();
1319 iports = ch->ch_iports;
1320 delta = ch->ch_iports_delta;
1321 ch->ch_iports_delta = 0;
1322 splx(s);
1323
1324 if (ISSET(delta, ch->ch_i_dcd)) {
1325 /*
1326 * Inform the tty layer that carrier detect changed.
1327 */
1328 (void) (*tp->t_linesw->l_modem)(tp,
1329 ISSET(iports, ch->ch_i_dcd));
1330 }
1331
1332 if (ISSET(delta, ch->ch_i_cts)) {
1333 /* Block or unblock output according to flow control. */
1334 if (ISSET(iports, ch->ch_i_cts)) {
1335 ch->ch_tx_stopped = 0;
1336 (*tp->t_linesw->l_start)(tp);
1337 } else {
1338 ch->ch_tx_stopped = 1;
1339 }
1340 }
1341
1342 #ifdef SBJCN_DEBUG
1343 if (sbjcn_debug)
1344 sbjcn_status(ch, "sbjcn_stsoft");
1345 #endif
1346 }
1347
1348 integrate void
1349 sbjcn_recv(struct sbjcn_channel *ch)
1350 {
1351 u_char *put, *end;
1352 u_int cc;
1353
1354 end = ch->ch_ebuf;
1355 put = ch->ch_rbput;
1356 cc = ch->ch_rbavail;
1357
1358 /* XXX process break */
1359
1360 sbjcncn_grabdword(ch);
1361 if (ch->ch_waiting_input) {
1362 if (!ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
1363 while (cc > 0) {
1364 put[0] = sbjcncn_nextbyte(ch);
1365 put[1] = 1; /* XXXKW ? */
1366 put += 2;
1367 if (put >= end)
1368 put = ch->ch_rbuf;
1369 cc--;
1370
1371 if (!ch->ch_waiting_input)
1372 break;
1373 }
1374
1375 /*
1376 * Current string of incoming characters ended
1377 * because no more data was available or we
1378 * ran out of space. Schedule a receive event
1379 * if any data was received. If we're out of
1380 * space, turn off receive interrupts.
1381 */
1382 ch->ch_rbput = put;
1383 ch->ch_rbavail = cc;
1384 if (!ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED))
1385 ch->ch_rx_ready = 1;
1386
1387 /*
1388 * See if we are in danger of overflowing a
1389 * buffer. If so, use hardware flow control
1390 * to ease the pressure.
1391 */
1392 if (!ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED) &&
1393 cc < ch->ch_r_hiwat) {
1394 SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
1395 sbjcn_dohwiflow(ch);
1396 }
1397
1398 /*
1399 * If we're out of space, disable receive
1400 * interrupts until the queue has drained
1401 * a bit.
1402 */
1403 if (!cc) {
1404 SET(ch->ch_rx_flags,
1405 RX_IBUF_OVERFLOWED);
1406 CLR(ch->ch_imr, 0x02);
1407 }
1408 } else {
1409 /* XXX panic? */
1410 CLR(ch->ch_imr, 0x02);
1411 // continue;
1412 }
1413 }
1414
1415 /*
1416 * If we've delayed a parameter change, do it now, and restart
1417 * output.
1418 */
1419 if (ch->ch_heldchange) {
1420 sbjcn_loadchannelregs(ch);
1421 ch->ch_heldchange = 0;
1422 ch->ch_tbc = ch->ch_heldtbc;
1423 ch->ch_heldtbc = 0;
1424 }
1425 }
1426
1427 void
1428 sbjcn_callout(void *arg)
1429 {
1430 struct sbjcn_channel *ch = arg;
1431 struct tty *tp = ch->ch_tty;
1432
1433 /* XXX get stuff */
1434 sbjcn_recv(ch);
1435
1436 /* XXX check receive */
1437 if (ch->ch_rx_ready) {
1438 ch->ch_rx_ready = 0;
1439 sbjcn_rxsoft(ch, tp);
1440 }
1441
1442 /* XXX check transmit */
1443 if (ch->ch_tx_done) {
1444 ch->ch_tx_done = 0;
1445 sbjcn_txsoft(ch, tp);
1446 }
1447
1448 callout_reset(&ch->ch_callout, hz/10, sbjcn_callout, ch);
1449 }
1450
1451 static char sbjcncn_nextbyte(struct sbjcn_channel *ch)
1452 {
1453 char c;
1454
1455 sbjcncn_grabdword(ch);
1456 c = (ch->ch_input_buf >> 56) & 0xff;
1457 ch->ch_input_buf <<= 8;
1458 ch->ch_waiting_input--;
1459 return c;
1460 }
1461
1462 static void sbjcncn_grabdword(struct sbjcn_channel *ch)
1463 {
1464 uint64_t inbuf;
1465
1466 if (ch->ch_waiting_input)
1467 return;
1468
1469 inbuf = READ_REG(ch->ch_input_reg);
1470 ch->ch_waiting_input = jtag_input_len(inbuf);
1471 ch->ch_input_buf = inbuf << 8;
1472 }
1473
1474 /*
1475 * Initialize UART for use as console or KGDB line.
1476 */
1477 int
1478 sbjcn_init(u_long addr, int chan, int rate, tcflag_t cflag)
1479 {
1480 /* XXXKW Anything to do here? */
1481 return (0);
1482 }
1483
1484 /*
1485 * Following are all routines needed for sbjcn to act as console
1486 */
1487 int
1488 sbjcn_cnattach(u_long addr, int chan, int rate, tcflag_t cflag)
1489 {
1490 int res;
1491 uint64_t ctrl_val;
1492 static struct consdev sbjcn_cons = {
1493 NULL, NULL, sbjcn_cngetc, sbjcn_cnputc, sbjcn_cnpollc, NULL,
1494 NODEV, CN_NORMAL
1495 };
1496
1497 res = sbjcn_init(addr, chan, rate, cflag);
1498 if (res)
1499 return (res);
1500
1501 cn_tab = &sbjcn_cons;
1502
1503 sbjcn_cons_present = 1;
1504 sbjcn_cons_addr = addr;
1505 sbjcn_cons_waiting_input = 0;
1506 sbjcn_cons_chan = chan;
1507 sbjcn_cons_rate = rate;
1508 sbjcn_cons_cflag = cflag;
1509
1510 /* Wait for sign of life from the other end */
1511 while ((ctrl_val = READ_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_CONTROL))) == 0)
1512 ;
1513
1514 return (ctrl_val != JTAG_CONS_MAGICNUM);
1515 }
1516
1517 int
1518 sbjcn_cngetc(dev_t dev)
1519 {
1520 char c;
1521
1522 while (sbjcn_cons_waiting_input == 0)
1523 sbjcn_cngrabdword();
1524
1525 c = (sbjcn_cons_input_buf >> 56) & 0xff;
1526 sbjcn_cons_input_buf <<= 8;
1527 sbjcn_cons_waiting_input--;
1528
1529 return c;
1530 }
1531
1532 /*
1533 * Console kernel output character routine.
1534 */
1535 void
1536 sbjcn_cnputc(dev_t dev, int c)
1537 {
1538 uint64_t outbuf;
1539
1540 outbuf = (1LL << 56) | (((uint64_t)c) << 48);
1541 WRITE_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_OUTPUT), outbuf);
1542 }
1543
1544 void
1545 sbjcn_cnpollc(dev_t dev, int on)
1546 {
1547
1548 }
1549
1550 static void sbjcn_cngrabdword(void)
1551 {
1552 uint64_t inbuf;
1553
1554 if (sbjcn_cons_waiting_input)
1555 return;
1556
1557 inbuf = READ_REG(MIPS_PHYS_TO_KSEG1(sbjcn_cons_addr + JTAG_CONS_INPUT));
1558 sbjcn_cons_waiting_input = jtag_input_len(inbuf);
1559 sbjcn_cons_input_buf = inbuf << 8;
1560 }
1561
1562 #ifdef KGDB
1563 int
1564 sbjcn_kgdb_attach(u_long addr, int chan, int rate, tcflag_t cflag)
1565 {
1566 int res;
1567
1568 if (!sbjcn_cons_present &&
1569 sbjcn_cons_addr == addr && sbjcn_cons_chan == chan)
1570 return (EBUSY); /* cannot share with console */
1571
1572 res = sbjcn_init(addr, chan, rate, cflag);
1573 if (res)
1574 return (res);
1575
1576 kgdb_attach(sbjcn_kgdb_getc, sbjcn_kgdb_putc, NULL);
1577 kgdb_dev = 123; /* unneeded, only to satisfy some tests */
1578 kgdb_rate = rate;
1579
1580 sbjcn_kgdb_present = 1;
1581 /* XXX sbjcn_init wants addr, but we need the offset addr */
1582 sbjcn_kgdb_addr = addr + (chan * 0x100);
1583 sbjcn_kgdb_chan = chan;
1584
1585 return (0);
1586 }
1587
1588 /* ARGSUSED */
1589 int
1590 sbjcn_kgdb_getc(arg)
1591 void *arg;
1592 {
1593
1594 return (sbjcn_common_getc(sbjcn_kgdb_addr, sbjcn_kgdb_chan));
1595 }
1596
1597 /* ARGSUSED */
1598 void
1599 sbjcn_kgdb_putc(arg, c)
1600 void *arg;
1601 int c;
1602 {
1603
1604 sbjcn_common_putc(sbjcn_kgdb_addr, sbjcn_kgdb_chan, c);
1605 }
1606 #endif /* KGDB */
1607
1608 /*
1609 * helper function to identify the sbjcn channels used by
1610 * console or KGDB (and not yet autoconf attached)
1611 */
1612 int
1613 sbjcn_is_console(u_long addr, int chan)
1614 {
1615
1616 if (sbjcn_cons_present && !sbjcn_cons_attached &&
1617 sbjcn_cons_addr == addr && sbjcn_cons_chan == chan)
1618 return (1);
1619 #ifdef KGDB
1620 if (sbjcn_kgdb_present && !sbjcn_kgdb_attached &&
1621 sbjcn_kgdb_addr == addr && sbjcn_kgdb_chan == chan)
1622 return (1);
1623 #endif
1624 return (0);
1625 }
1626