sbscn.c revision 1.18 1 /* $NetBSD: sbscn.c,v 1.18 2006/07/23 22:06:06 ad Exp $ */
2
3 /*
4 * Copyright 2000, 2001
5 * Broadcom Corporation. All rights reserved.
6 *
7 * This software is furnished under license and may be used and copied only
8 * in accordance with the following terms and conditions. Subject to these
9 * conditions, you may download, copy, install, use, modify and distribute
10 * modified or unmodified copies of this software in source and/or binary
11 * form. No title or ownership is transferred hereby.
12 *
13 * 1) Any source code used, modified or distributed must reproduce and
14 * retain this copyright notice and list of conditions as they appear in
15 * the source file.
16 *
17 * 2) No right is granted to use any trade name, trademark, or logo of
18 * Broadcom Corporation. The "Broadcom Corporation" name may not be
19 * used to endorse or promote products derived from this software
20 * without the prior written permission of Broadcom Corporation.
21 *
22 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
25 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
26 * FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
27 * LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32 * OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /* from: $NetBSD: com.c,v 1.172 2000/05/03 19:19:04 thorpej Exp */
36
37 /*-
38 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
39 * All rights reserved.
40 *
41 * This code is derived from software contributed to The NetBSD Foundation
42 * by Charles M. Hannum.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the NetBSD
55 * Foundation, Inc. and its contributors.
56 * 4. Neither the name of The NetBSD Foundation nor the names of its
57 * contributors may be used to endorse or promote products derived
58 * from this software without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
61 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
62 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
63 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
64 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
65 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
66 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
67 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
68 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
69 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
70 * POSSIBILITY OF SUCH DAMAGE.
71 */
72
73 /*
74 * Copyright (c) 1991 The Regents of the University of California.
75 * All rights reserved.
76 *
77 * Redistribution and use in source and binary forms, with or without
78 * modification, are permitted provided that the following conditions
79 * are met:
80 * 1. Redistributions of source code must retain the above copyright
81 * notice, this list of conditions and the following disclaimer.
82 * 2. Redistributions in binary form must reproduce the above copyright
83 * notice, this list of conditions and the following disclaimer in the
84 * documentation and/or other materials provided with the distribution.
85 * 3. Neither the name of the University nor the names of its contributors
86 * may be used to endorse or promote products derived from this software
87 * without specific prior written permission.
88 *
89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 * SUCH DAMAGE.
100 *
101 * @(#)com.c 7.5 (Berkeley) 5/16/91
102 */
103
104 /*
105 * `sbscn' driver, supports SiByte SB-1250 SOC DUART.
106 *
107 * This DUART is quite similar in programming model to the scn2681/68681
108 * DUARTs supported by the NetBSD/amiga `mfc' and NetBSD/pc532 `scn'
109 * driver, but substantial rework would have been necessary to make
110 * those drivers sane w.r.t. bus_space (which would then have been
111 * required on NetBSD/sbmips very early on), and to accommodate the
112 * different register mappings.
113 *
114 * So, another driver. Eventually there should be One True Driver,
115 * but we're not here to save the world.
116 */
117
118 #include <sys/cdefs.h>
119 __KERNEL_RCSID(0, "$NetBSD: sbscn.c,v 1.18 2006/07/23 22:06:06 ad Exp $");
120
121 #define SBSCN_DEBUG
122
123 #include "opt_ddb.h"
124
125 #include "rnd.h"
126 #if NRND > 0 && defined(RND_SBSCN)
127 #include <sys/rnd.h>
128 #endif
129
130 #include <sys/param.h>
131 #include <sys/systm.h>
132 #include <sys/ioctl.h>
133 #include <sys/select.h>
134 #include <sys/tty.h>
135 #include <sys/proc.h>
136 #include <sys/user.h>
137 #include <sys/conf.h>
138 #include <sys/file.h>
139 #include <sys/uio.h>
140 #include <sys/kernel.h>
141 #include <sys/syslog.h>
142 #include <sys/types.h>
143 #include <sys/device.h>
144 #include <sys/malloc.h>
145 #include <sys/vnode.h>
146 #include <sys/kauth.h>
147
148 #include <mips/sibyte/dev/sbobiovar.h>
149 #if 0
150 #include <mips/sibyte/dev/sbscnreg.h>
151 #endif
152 #include <mips/sibyte/dev/sbscnvar.h>
153 #include <dev/cons.h>
154 #include <machine/locore.h>
155
156 void sbscn_attach_channel(struct sbscn_softc *sc, int chan, int intr);
157 #if defined(DDB) || defined(KGDB)
158 static void sbscn_enable_debugport(struct sbscn_channel *ch);
159 #endif
160 void sbscn_config(struct sbscn_channel *ch);
161 void sbscn_shutdown(struct sbscn_channel *ch);
162 int sbscn_speed(long, long *);
163 static int cflag2modes(tcflag_t, u_char *, u_char *);
164 int sbscn_param(struct tty *, struct termios *);
165 void sbscn_start(struct tty *);
166 int sbscn_hwiflow(struct tty *, int);
167
168 void sbscn_loadchannelregs(struct sbscn_channel *);
169 void sbscn_dohwiflow(struct sbscn_channel *);
170 void sbscn_break(struct sbscn_channel *, int);
171 void sbscn_modem(struct sbscn_channel *, int);
172 void tiocm_to_sbscn(struct sbscn_channel *, int, int);
173 int sbscn_to_tiocm(struct sbscn_channel *);
174 void sbscn_iflush(struct sbscn_channel *);
175
176 int sbscn_init(u_long addr, int chan, int rate, tcflag_t cflag);
177 int sbscn_common_getc(u_long addr, int chan);
178 void sbscn_common_putc(u_long addr, int chan, int c);
179 void sbscn_intr(void *arg, uint32_t status, uint32_t pc);
180
181 int sbscn_cngetc(dev_t dev);
182 void sbscn_cnputc(dev_t dev, int c);
183 void sbscn_cnpollc(dev_t dev, int on);
184
185 extern struct cfdriver sbscn_cd;
186
187 dev_type_open(sbscnopen);
188 dev_type_close(sbscnclose);
189 dev_type_read(sbscnread);
190 dev_type_write(sbscnwrite);
191 dev_type_ioctl(sbscnioctl);
192 dev_type_stop(sbscnstop);
193 dev_type_tty(sbscntty);
194 dev_type_poll(sbscnpoll);
195
196 const struct cdevsw sbscn_cdevsw = {
197 sbscnopen, sbscnclose, sbscnread, sbscnwrite, sbscnioctl,
198 sbscnstop, sbscntty, sbscnpoll, nommap, ttykqfilter, D_TTY
199 };
200
201 #define integrate static inline
202 void sbscn_soft(void *);
203 integrate void sbscn_rxsoft(struct sbscn_channel *, struct tty *);
204 integrate void sbscn_txsoft(struct sbscn_channel *, struct tty *);
205 integrate void sbscn_stsoft(struct sbscn_channel *, struct tty *);
206 integrate void sbscn_schedrx(struct sbscn_channel *);
207 void sbscn_diag(void *);
208
209 /*
210 * Make this an option variable one can patch.
211 * But be warned: this must be a power of 2!
212 */
213 u_int sbscn_rbuf_size = SBSCN_RING_SIZE;
214
215 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
216 u_int sbscn_rbuf_hiwat = (SBSCN_RING_SIZE * 1) / 4;
217 u_int sbscn_rbuf_lowat = (SBSCN_RING_SIZE * 3) / 4;
218
219 static int sbscn_cons_present;
220 static int sbscn_cons_attached;
221 static u_long sbscn_cons_addr;
222 static int sbscn_cons_chan;
223 static int sbscn_cons_rate;
224 static tcflag_t sbscn_cons_cflag;
225
226 #ifdef KGDB
227 #include <sys/kgdb.h>
228
229 static int sbscn_kgdb_present;
230 static int sbscn_kgdb_attached;
231 static u_long sbscn_kgdb_addr;
232 static int sbscn_kgdb_chan;
233
234 int sbscn_kgdb_getc(void *);
235 void sbscn_kgdb_putc(void *, int);
236 #endif /* KGDB */
237
238 static int sbscn_match(struct device *, struct cfdata *, void *);
239 static void sbscn_attach(struct device *, struct device *, void *);
240
241 CFATTACH_DECL(sbscn, sizeof(struct sbscn_softc),
242 sbscn_match, sbscn_attach, NULL, NULL);
243
244 #define READ_REG(rp) (mips3_ld((int64_t *)__UNVOLATILE(rp)))
245 #define WRITE_REG(rp, val) (mips3_sd((uint64_t *)__UNVOLATILE(rp), (val)))
246
247 /*
248 * input and output signals are actually the _inverse_ of the bits in the
249 * input and output port registers!
250 */
251 #define GET_INPUT_SIGNALS(ch) \
252 ((~READ_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x280))) & (ch)->ch_i_mask)
253 #define SET_OUTPUT_SIGNALS(ch, val) \
254 do { \
255 int sigs_to_set, sigs_to_clr; \
256 \
257 sigs_to_set = (ch)->ch_o_mask & val; \
258 sigs_to_clr = (ch)->ch_o_mask & ~val; \
259 \
260 /* set signals by clearing op bits, and vice versa */ \
261 WRITE_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x2c0), \
262 sigs_to_set); \
263 WRITE_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x2b0), \
264 sigs_to_clr); \
265 } while (0)
266
267 static int
268 sbscn_match(struct device *parent, struct cfdata *match, void *aux)
269 {
270 struct sbobio_attach_args *sap = aux;
271
272 if (sap->sa_locs.sa_type != SBOBIO_DEVTYPE_DUART)
273 return (0);
274
275 return 1;
276 }
277
278 static void
279 sbscn_attach(struct device *parent, struct device *self, void *aux)
280 {
281 struct sbscn_softc *sc = (struct sbscn_softc *)self;
282 struct sbobio_attach_args *sap = aux;
283 int i;
284
285 sc->sc_addr = sap->sa_base + sap->sa_locs.sa_offset;
286
287 printf("\n");
288 for (i = 0; i < 2; i++)
289 sbscn_attach_channel(sc, i, sap->sa_locs.sa_intr[i]);
290
291 /* init duart_opcr */
292 WRITE_REG(MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x270), 0);
293 /* init duart_aux_ctrl */
294 WRITE_REG(MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x210), 0x0f); /* XXX */
295 }
296
297 void
298 sbscn_attach_channel(struct sbscn_softc *sc, int chan, int intr)
299 {
300 struct sbscn_channel *ch = &sc->sc_channels[chan];
301 u_long chan_addr;
302 struct tty *tp;
303
304 ch->ch_sc = sc;
305 ch->ch_num = chan;
306
307 chan_addr = sc->sc_addr + (0x100 * chan);
308 ch->ch_base = (void *)MIPS_PHYS_TO_KSEG1(chan_addr);
309 ch->ch_isr_base =
310 (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x220 + (0x20 * chan));
311 ch->ch_imr_base =
312 (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x230 + (0x20 * chan));
313 #ifdef XXXCGDnotyet
314 ch->ch_inchg_base =
315 (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x2d0 + (0x10 * chan));
316 #endif
317
318 ch->ch_i_dcd = ch->ch_i_dcd_pin = 0 /* XXXCGD */;
319 ch->ch_i_cts = ch->ch_i_cts_pin = 0 /* XXXCGD */;
320 ch->ch_i_dsr = ch->ch_i_dsr_pin = 0 /* XXXCGD */;
321 ch->ch_i_ri = ch->ch_i_ri_pin = 0 /* XXXCGD */;
322 ch->ch_i_mask =
323 ch->ch_i_dcd | ch->ch_i_cts | ch->ch_i_dsr | ch->ch_i_ri;
324 ch->ch_o_dtr = ch->ch_o_dtr_pin = 0 /* XXXCGD */;
325 ch->ch_o_rts = ch->ch_o_rts_pin = 0 /* XXXCGD */;
326 ch->ch_o_mask = ch->ch_o_dtr | ch->ch_o_rts;
327
328 ch->ch_intrhand = cpu_intr_establish(intr, IPL_SERIAL, sbscn_intr, ch);
329 callout_init(&ch->ch_diag_callout);
330
331 /* Disable interrupts before configuring the device. */
332 ch->ch_imr = 0;
333 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
334
335 if (sbscn_cons_present &&
336 sbscn_cons_addr == chan_addr && sbscn_cons_chan == chan) {
337 sbscn_cons_attached = 1;
338
339 /* Make sure the console is always "hardwired". */
340 delay(1000); /* wait for output to finish */
341 SET(ch->ch_hwflags, SBSCN_HW_CONSOLE);
342 SET(ch->ch_swflags, TIOCFLAG_SOFTCAR);
343 }
344
345 tp = ttymalloc();
346 tp->t_oproc = sbscn_start;
347 tp->t_param = sbscn_param;
348 tp->t_hwiflow = sbscn_hwiflow;
349
350 ch->ch_tty = tp;
351 ch->ch_rbuf = malloc(sbscn_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
352 if (ch->ch_rbuf == NULL) {
353 printf("%s: channel %d: unable to allocate ring buffer\n",
354 sc->sc_dev.dv_xname, chan);
355 return;
356 }
357 ch->ch_ebuf = ch->ch_rbuf + (sbscn_rbuf_size << 1);
358
359 tty_attach(tp);
360
361 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
362 int maj;
363
364 /* locate the major number */
365 maj = cdevsw_lookup_major(&sbscn_cdevsw);
366
367 cn_tab->cn_dev = makedev(maj,
368 (device_unit(&sc->sc_dev) << 1) + chan);
369
370 printf("%s: channel %d: console\n", sc->sc_dev.dv_xname, chan);
371 }
372
373 #ifdef KGDB
374 /*
375 * Allow kgdb to "take over" this port. If this is
376 * the kgdb device, it has exclusive use.
377 */
378 if (sbscn_kgdb_present &&
379 sbscn_kgdb_addr == chan_addr && sbscn_kgdb_chan == chan) {
380 sbscn_kgdb_attached = 1;
381
382 SET(sc->sc_hwflags, SBSCN_HW_KGDB);
383 printf("%s: channel %d: kgdb\n", sc->sc_dev.dv_xname, chan);
384 }
385 #endif
386
387 ch->ch_si = softintr_establish(IPL_SOFTSERIAL, sbscn_soft, ch);
388
389 #if NRND > 0 && defined(RND_SBSCN)
390 rnd_attach_source(&ch->ch_rnd_source, sc->sc_dev.dv_xname,
391 RND_TYPE_TTY, 0);
392 #endif
393
394 sbscn_config(ch);
395
396 SET(ch->ch_hwflags, SBSCN_HW_DEV_OK);
397 }
398
399 int
400 sbscn_speed(long speed, long *brcp)
401 {
402 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
403
404 int x, err;
405 int frequency = 100000000;
406
407 *brcp = divrnd(frequency / 20, speed) - 1;
408
409 if (speed <= 0)
410 return (-1);
411 x = divrnd(frequency / 20, speed);
412 if (x <= 0)
413 return (-1);
414 err = divrnd(((quad_t)frequency) * 1000 / 20, speed * x) - 1000;
415 if (err < 0)
416 err = -err;
417 if (err > SBSCN_TOLERANCE)
418 return (-1);
419 *brcp = x - 1;
420 return (0);
421
422 #undef divrnd
423 }
424
425 #ifdef SBSCN_DEBUG
426 void sbscn_status(struct sbscn_channel *, const char *);
427
428 int sbscn_debug = 0 /* XXXCGD */;
429
430 void
431 sbscn_status(struct sbscn_channel *ch, const char *str)
432 {
433 struct sbscn_softc *sc = ch->ch_sc;
434 struct tty *tp = ch->ch_tty;
435
436 printf("%s: chan %d: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n",
437 sc->sc_dev.dv_xname, ch->ch_num, str,
438 ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
439 ISSET(ch->ch_iports, ch->ch_i_dcd) ? "+" : "-",
440 ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
441 ISSET(ch->ch_oports, ch->ch_o_dtr) ? "+" : "-",
442 ch->ch_tx_stopped ? "+" : "-");
443
444 printf("%s: chan %d: %s %scrtscts %scts %sts_ttstop %srts %xrx_flags\n",
445 sc->sc_dev.dv_xname, ch->ch_num, str,
446 ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
447 ISSET(ch->ch_iports, ch->ch_i_cts) ? "+" : "-",
448 ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
449 ISSET(ch->ch_oports, ch->ch_o_rts) ? "+" : "-",
450 ch->ch_rx_flags);
451 }
452 #endif
453
454 #if defined(DDB) || defined(KGDB)
455 static void
456 sbscn_enable_debugport(struct sbscn_channel *ch)
457 {
458 int s;
459
460 /* Turn on line break interrupt, set carrier. */
461 s = splserial();
462
463 #if 0 /* DO NOT turn on break interrupt at this time. */
464 ch->ch_imr = 0x04;
465 #else
466 ch->ch_imr = 0x00;
467 #endif
468 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
469 SET(ch->ch_oports, ch->ch_o_dtr | ch->ch_o_rts);
470 SET_OUTPUT_SIGNALS(ch, ch->ch_oports);
471
472 splx(s);
473 }
474 #endif
475
476 void
477 sbscn_config(struct sbscn_channel *ch)
478 {
479
480 /* Disable interrupts before configuring the device. */
481 ch->ch_imr = 0x00;
482 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
483
484 #ifdef DDB
485 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE))
486 sbscn_enable_debugport(ch);
487 #endif
488
489 #ifdef KGDB
490 /*
491 * Allow kgdb to "take over" this port. If this is
492 * the kgdb device, it has exclusive use.
493 */
494 if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB))
495 sbscn_enable_debugport(ch);
496 #endif
497 }
498
499 void
500 sbscn_shutdown(struct sbscn_channel *ch)
501 {
502 struct tty *tp = ch->ch_tty;
503 int s;
504
505 s = splserial();
506
507 /* If we were asserting flow control, then deassert it. */
508 SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
509 sbscn_dohwiflow(ch);
510
511 /* Clear any break condition set with TIOCSBRK. */
512 sbscn_break(ch, 0);
513
514 /*
515 * Hang up if necessary. Wait a bit, so the other side has time to
516 * notice even if we immediately open the port again.
517 * Avoid tsleeping above splhigh().
518 */
519 if (ISSET(tp->t_cflag, HUPCL)) {
520 sbscn_modem(ch, 0);
521 splx(s);
522 /* XXX tsleep will only timeout */
523 (void) tsleep(ch, TTIPRI, ttclos, hz);
524 s = splserial();
525 }
526
527 /* Turn off interrupts. */
528 #ifdef DDB
529 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE))
530 #if 0 /* DO NOT turn on break interrupt at this time. */
531 ch->ch_imr = 0x04; /* interrupt on break */
532 #else
533 ch->ch_imr = 0x00;
534 #endif
535 else
536 #endif
537 ch->ch_imr = 0;
538 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
539
540 splx(s);
541 }
542
543 int
544 sbscnopen(dev_t dev, int flag, int mode, struct lwp *l)
545 {
546 int unit = SBSCN_UNIT(dev);
547 int chan = SBSCN_CHAN(dev);
548 struct sbscn_softc *sc;
549 struct sbscn_channel *ch;
550 struct tty *tp;
551 int s, s2;
552 int error;
553
554 if (unit >= sbscn_cd.cd_ndevs)
555 return (ENXIO);
556 sc = sbscn_cd.cd_devs[unit];
557 if (sc == 0)
558 return (ENXIO);
559 ch = &sc->sc_channels[chan];
560 if (!ISSET(ch->ch_hwflags, SBSCN_HW_DEV_OK) || ch->ch_rbuf == NULL)
561 return (ENXIO);
562
563 #ifdef KGDB
564 /*
565 * If this is the kgdb port, no other use is permitted.
566 */
567 if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB))
568 return (EBUSY);
569 #endif
570
571 tp = ch->ch_tty;
572
573 if (ISSET(tp->t_state, TS_ISOPEN) &&
574 ISSET(tp->t_state, TS_XCLUDE) &&
575 kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
576 &l->l_acflag) != 0)
577 return (EBUSY);
578
579 s = spltty();
580
581 /*
582 * Do the following iff this is a first open.
583 */
584 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
585 struct termios t;
586
587 tp->t_dev = dev;
588
589 s2 = splserial();
590
591 /* Turn on receive, break, and status change interrupts. */
592 #if 0 /* DO NOT turn on break or status change interrupt at this time. */
593 ch->ch_imr = 0xe;
594 #else
595 ch->ch_imr = 0x2;
596 #endif
597 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
598
599 /* Fetch the current modem control status, needed later. */
600 ch->ch_iports = GET_INPUT_SIGNALS(ch);
601 ch->ch_iports_delta = 0;
602 splx(s2);
603
604 /*
605 * Initialize the termios status to the defaults. Add in the
606 * sticky bits from TIOCSFLAGS.
607 */
608 t.c_ispeed = 0;
609 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
610 t.c_ospeed = sbscn_cons_rate;
611 t.c_cflag = sbscn_cons_cflag;
612 } else {
613 t.c_ospeed = TTYDEF_SPEED;
614 t.c_cflag = TTYDEF_CFLAG;
615 }
616 if (ISSET(ch->ch_swflags, TIOCFLAG_CLOCAL))
617 SET(t.c_cflag, CLOCAL);
618 if (ISSET(ch->ch_swflags, TIOCFLAG_CRTSCTS))
619 SET(t.c_cflag, CRTSCTS);
620 if (ISSET(ch->ch_swflags, TIOCFLAG_MDMBUF))
621 SET(t.c_cflag, MDMBUF);
622 /* Make sure sbscn_param() will do something. */
623 tp->t_ospeed = 0;
624 (void) sbscn_param(tp, &t);
625 tp->t_iflag = TTYDEF_IFLAG;
626 tp->t_oflag = TTYDEF_OFLAG;
627 tp->t_lflag = TTYDEF_LFLAG;
628 ttychars(tp);
629 ttsetwater(tp);
630
631 s2 = splserial();
632
633 /*
634 * Turn on DTR. We must always do this, even if carrier is not
635 * present, because otherwise we'd have to use TIOCSDTR
636 * immediately after setting CLOCAL, which applications do not
637 * expect. We always assert DTR while the device is open
638 * unless explicitly requested to deassert it.
639 */
640 sbscn_modem(ch, 1);
641
642 /* Clear the input ring, and unblock. */
643 ch->ch_rbput = ch->ch_rbget = ch->ch_rbuf;
644 ch->ch_rbavail = sbscn_rbuf_size;
645 sbscn_iflush(ch);
646 CLR(ch->ch_rx_flags, RX_ANY_BLOCK);
647 sbscn_dohwiflow(ch);
648
649 #ifdef SBSCN_DEBUG
650 if (sbscn_debug)
651 sbscn_status(ch, "sbscnopen ");
652 #endif
653
654 splx(s2);
655 }
656
657 splx(s);
658
659 error = ttyopen(tp, SBSCN_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
660 if (error)
661 goto bad;
662
663 error = (*tp->t_linesw->l_open)(dev, tp);
664 if (error)
665 goto bad;
666
667 return (0);
668
669 bad:
670 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
671 /*
672 * We failed to open the device, and nobody else had it opened.
673 * Clean up the state as appropriate.
674 */
675 sbscn_shutdown(ch);
676 }
677
678 return (error);
679 }
680
681 int
682 sbscnclose(dev_t dev, int flag, int mode, struct lwp *l)
683 {
684 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
685 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
686 struct tty *tp = ch->ch_tty;
687
688 /* XXX This is for cons.c. */
689 if (!ISSET(tp->t_state, TS_ISOPEN))
690 return (0);
691
692 (*tp->t_linesw->l_close)(tp, flag);
693 ttyclose(tp);
694
695 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
696 /*
697 * Although we got a last close, the device may still be in
698 * use; e.g. if this was the dialout node, and there are still
699 * processes waiting for carrier on the non-dialout node.
700 */
701 sbscn_shutdown(ch);
702 }
703
704 return (0);
705 }
706
707 int
708 sbscnread(dev_t dev, struct uio *uio, int flag)
709 {
710 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
711 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
712 struct tty *tp = ch->ch_tty;
713
714 return ((*tp->t_linesw->l_read)(tp, uio, flag));
715 }
716
717 int
718 sbscnwrite(dev_t dev, struct uio *uio, int flag)
719 {
720 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
721 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
722 struct tty *tp = ch->ch_tty;
723
724 return ((*tp->t_linesw->l_write)(tp, uio, flag));
725 }
726
727 int
728 sbscnpoll(dev_t dev, int events, struct lwp *l)
729 {
730 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
731 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
732 struct tty *tp = ch->ch_tty;
733
734 return ((*tp->t_linesw->l_poll)(tp, events, l));
735 }
736
737 struct tty *
738 sbscntty(dev_t dev)
739 {
740 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
741 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
742 struct tty *tp = ch->ch_tty;
743
744 return (tp);
745 }
746
747 int
748 sbscnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
749 {
750 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
751 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
752 struct tty *tp = ch->ch_tty;
753 int error;
754 int s;
755
756 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
757 if (error != EPASSTHROUGH)
758 return (error);
759
760 error = ttioctl(tp, cmd, data, flag, l);
761 if (error != EPASSTHROUGH)
762 return (error);
763
764 error = 0;
765
766 s = splserial();
767
768 switch (cmd) {
769 case TIOCSBRK:
770 sbscn_break(ch, 1);
771 break;
772
773 case TIOCCBRK:
774 sbscn_break(ch, 0);
775 break;
776
777 case TIOCSDTR:
778 sbscn_modem(ch, 1);
779 break;
780
781 case TIOCCDTR:
782 sbscn_modem(ch, 0);
783 break;
784
785 case TIOCGFLAGS:
786 *(int *)data = ch->ch_swflags;
787 break;
788
789 case TIOCSFLAGS:
790 error = kauth_authorize_generic(l->l_cred,
791 KAUTH_GENERIC_ISSUSER, &l->l_acflag);
792 if (error)
793 break;
794 ch->ch_swflags = *(int *)data;
795 break;
796
797 case TIOCMSET:
798 case TIOCMBIS:
799 case TIOCMBIC:
800 tiocm_to_sbscn(ch, cmd, *(int *)data);
801 break;
802
803 case TIOCMGET:
804 *(int *)data = sbscn_to_tiocm(ch);
805 break;
806
807 default:
808 error = EPASSTHROUGH;
809 break;
810 }
811
812 splx(s);
813
814 #ifdef SBSCN_DEBUG
815 if (sbscn_debug)
816 sbscn_status(ch, "sbscn_ioctl ");
817 #endif
818
819 return (error);
820 }
821
822 integrate void
823 sbscn_schedrx(struct sbscn_channel *ch)
824 {
825
826 ch->ch_rx_ready = 1;
827
828 /* Wake up the poller. */
829 softintr_schedule(ch->ch_si);
830 }
831
832 void
833 sbscn_break(struct sbscn_channel *ch, int onoff)
834 {
835
836 /* XXXCGD delay break until not busy */
837 if (onoff)
838 WRITE_REG(ch->ch_base + 0x50, 0x60);
839 else
840 WRITE_REG(ch->ch_base + 0x50, 0x70);
841
842 #if 0
843 if (!ch->ch_heldchange) {
844 if (ch->ch_tx_busy) {
845 ch->ch_heldtbc = ch->ch_tbc;
846 ch->ch_tbc = 0;
847 ch->ch_heldchange = 1;
848 } else
849 sbscn_loadchannelregs(ch);
850 }
851 #endif
852 }
853
854 void
855 sbscn_modem(struct sbscn_channel *ch, int onoff)
856 {
857
858 if (ch->ch_o_dtr == 0)
859 return;
860
861 if (onoff)
862 SET(ch->ch_oports, ch->ch_o_dtr);
863 else
864 CLR(ch->ch_oports, ch->ch_o_dtr);
865
866 if (!ch->ch_heldchange) {
867 if (ch->ch_tx_busy) {
868 ch->ch_heldtbc = ch->ch_tbc;
869 ch->ch_tbc = 0;
870 ch->ch_heldchange = 1;
871 } else
872 sbscn_loadchannelregs(ch);
873 }
874 }
875
876 void
877 tiocm_to_sbscn(struct sbscn_channel *ch, int how, int ttybits)
878 {
879 u_char bits;
880
881 bits = 0;
882 if (ISSET(ttybits, TIOCM_DTR))
883 SET(bits, ch->ch_o_dtr);
884 if (ISSET(ttybits, TIOCM_RTS))
885 SET(bits, ch->ch_o_rts);
886
887 switch (how) {
888 case TIOCMBIC:
889 CLR(ch->ch_oports, bits);
890 break;
891
892 case TIOCMBIS:
893 SET(ch->ch_oports, bits);
894 break;
895
896 case TIOCMSET:
897 ch->ch_oports = bits;
898 break;
899 }
900
901 if (!ch->ch_heldchange) {
902 if (ch->ch_tx_busy) {
903 ch->ch_heldtbc = ch->ch_tbc;
904 ch->ch_tbc = 0;
905 ch->ch_heldchange = 1;
906 } else
907 sbscn_loadchannelregs(ch);
908 }
909 }
910
911 int
912 sbscn_to_tiocm(struct sbscn_channel *ch)
913 {
914 u_char hwbits;
915 int ttybits = 0;
916
917 hwbits = ch->ch_oports;
918 if (ISSET(hwbits, ch->ch_o_dtr))
919 SET(ttybits, TIOCM_DTR);
920 if (ISSET(hwbits, ch->ch_o_rts))
921 SET(ttybits, TIOCM_RTS);
922
923 hwbits = ch->ch_iports;
924 if (ISSET(hwbits, ch->ch_i_dcd))
925 SET(ttybits, TIOCM_CD);
926 if (ISSET(hwbits, ch->ch_i_cts))
927 SET(ttybits, TIOCM_CTS);
928 if (ISSET(hwbits, ch->ch_i_dsr))
929 SET(ttybits, TIOCM_DSR);
930 if (ISSET(hwbits, ch->ch_i_ri))
931 SET(ttybits, TIOCM_RI);
932
933 if (ch->ch_imr != 0)
934 SET(ttybits, TIOCM_LE);
935
936 return (ttybits);
937 }
938
939 static int
940 cflag2modes(cflag, mode1p, mode2p)
941 tcflag_t cflag;
942 u_char *mode1p;
943 u_char *mode2p;
944 {
945 u_char mode1;
946 u_char mode2;
947 int err = 0;
948
949 mode1 = mode2 = 0;
950
951 switch (ISSET(cflag, CSIZE)) {
952 case CS7:
953 mode1 |= 2; /* XXX */
954 break;
955 default:
956 err = -1;
957 /* FALLTHRU for sanity */
958 case CS8:
959 mode1 |= 3; /* XXX */
960 break;
961 }
962 if (!ISSET(cflag, PARENB))
963 mode1 |= 2 << 3;
964 else {
965 mode1 |= 0 << 3;
966 if (ISSET(cflag, PARODD))
967 mode1 |= 1 << 2;
968 }
969
970 if (ISSET(cflag, CSTOPB))
971 mode2 |= 1 << 3; /* two stop bits XXX not std */
972
973 if (ISSET(cflag, CRTSCTS)) {
974 mode1 |= 1 << 7;
975 mode2 |= 1 << 4;
976 }
977
978 *mode1p = mode1;
979 *mode2p = mode2;
980 return (err);
981 }
982
983 int
984 sbscn_param(struct tty *tp, struct termios *t)
985 {
986 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)];
987 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
988 long brc;
989 u_char mode1, mode2;
990 int s;
991
992 /* XXX reset to console parameters if console? */
993 #if 0
994 XXX disable, enable.
995 #endif
996
997 /* Check requested parameters. */
998 if (sbscn_speed(t->c_ospeed, &brc) < 0)
999 return (EINVAL);
1000 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
1001 return (EINVAL);
1002
1003 /*
1004 * For the console, always force CLOCAL and !HUPCL, so that the port
1005 * is always active.
1006 */
1007 if (ISSET(ch->ch_swflags, TIOCFLAG_SOFTCAR) ||
1008 ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
1009 SET(t->c_cflag, CLOCAL);
1010 CLR(t->c_cflag, HUPCL);
1011 }
1012
1013 /*
1014 * If there were no changes, don't do anything. This avoids dropping
1015 * input and improves performance when all we did was frob things like
1016 * VMIN and VTIME.
1017 */
1018 if (tp->t_ospeed == t->c_ospeed &&
1019 tp->t_cflag == t->c_cflag)
1020 return (0);
1021
1022 if (cflag2modes(t->c_cflag, &mode1, &mode2) < 0)
1023 return (EINVAL);
1024
1025 s = splserial();
1026
1027 ch->ch_mode1 = mode1;
1028 ch->ch_mode2 = mode2;
1029
1030 /*
1031 * If we're not in a mode that assumes a connection is present, then
1032 * ignore carrier changes.
1033 */
1034 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1035 ch->ch_i_dcd = 0;
1036 else
1037 ch->ch_i_dcd = ch->ch_i_dcd_pin;
1038 /*
1039 * Set the flow control pins depending on the current flow control
1040 * mode.
1041 */
1042 if (ISSET(t->c_cflag, CRTSCTS)) {
1043 ch->ch_o_dtr = ch->ch_o_dtr_pin;
1044 ch->ch_o_rts = ch->ch_o_rts_pin;
1045 ch->ch_i_cts = ch->ch_i_cts_pin;
1046 /* hw controle enable bits in mod regs set by cflag2modes */
1047 } else if (ISSET(t->c_cflag, MDMBUF)) {
1048 /*
1049 * For DTR/DCD flow control, make sure we don't toggle DTR for
1050 * carrier detection.
1051 */
1052 ch->ch_o_dtr = 0;
1053 ch->ch_o_rts = ch->ch_o_dtr_pin;
1054 ch->ch_i_cts = ch->ch_i_dcd_pin;
1055 } else {
1056 /*
1057 * If no flow control, then always set RTS. This will make
1058 * the other side happy if it mistakenly thinks we're doing
1059 * RTS/CTS flow control.
1060 */
1061 ch->ch_o_dtr = ch->ch_o_dtr_pin | ch->ch_o_rts_pin;
1062 ch->ch_o_rts = 0;
1063 ch->ch_i_cts = 0;
1064 if (ISSET(ch->ch_oports, ch->ch_o_dtr_pin))
1065 SET(ch->ch_oports, ch->ch_o_rts_pin);
1066 else
1067 CLR(ch->ch_oports, ch->ch_o_rts_pin);
1068 }
1069 /* XXX maybe mask the ports which generate intrs? */
1070
1071 ch->ch_brc = brc;
1072
1073 /* XXX maybe set fifo-full receive mode if RTSCTS and high speed? */
1074
1075 /* And copy to tty. */
1076 tp->t_ispeed = 0;
1077 tp->t_ospeed = t->c_ospeed;
1078 tp->t_cflag = t->c_cflag;
1079
1080 if (!ch->ch_heldchange) {
1081 if (ch->ch_tx_busy) {
1082 ch->ch_heldtbc = ch->ch_tbc;
1083 ch->ch_tbc = 0;
1084 ch->ch_heldchange = 1;
1085 } else
1086 sbscn_loadchannelregs(ch);
1087 }
1088
1089 if (!ISSET(t->c_cflag, CHWFLOW)) {
1090 /* Disable the high water mark. */
1091 ch->ch_r_hiwat = 0;
1092 ch->ch_r_lowat = 0;
1093 if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
1094 CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1095 sbscn_schedrx(ch);
1096 }
1097 if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1098 CLR(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1099 sbscn_dohwiflow(ch);
1100 }
1101 } else {
1102 ch->ch_r_hiwat = sbscn_rbuf_hiwat;
1103 ch->ch_r_lowat = sbscn_rbuf_lowat;
1104 }
1105
1106 splx(s);
1107
1108 /*
1109 * Update the tty layer's idea of the carrier bit, in case we changed
1110 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1111 * explicit request.
1112 */
1113 (void) (*tp->t_linesw->l_modem)(tp,
1114 ISSET(ch->ch_iports, ch->ch_i_dcd));
1115
1116 #ifdef SBSCN_DEBUG
1117 if (sbscn_debug)
1118 sbscn_status(ch, "sbscnparam ");
1119 #endif
1120
1121 if (!ISSET(t->c_cflag, CHWFLOW)) {
1122 if (ch->ch_tx_stopped) {
1123 ch->ch_tx_stopped = 0;
1124 sbscn_start(tp);
1125 }
1126 }
1127
1128 return (0);
1129 }
1130
1131 void
1132 sbscn_iflush(struct sbscn_channel *ch)
1133 {
1134 #ifdef DIAGNOSTIC
1135 int reg;
1136 #endif
1137 int timo;
1138
1139 #ifdef DIAGNOSTIC
1140 reg = 0xffff;
1141 #endif
1142 timo = 50000;
1143 /* flush any pending I/O */
1144 while (ISSET(READ_REG(ch->ch_base + 0x20), 0x01)
1145 && --timo)
1146 #ifdef DIAGNOSTIC
1147 reg =
1148 #else
1149 (void)
1150 #endif
1151 READ_REG(ch->ch_base + 0x60);
1152 #ifdef DIAGNOSTIC
1153 if (!timo)
1154 printf("%s: sbscn_iflush timeout %02x\n",
1155 ch->ch_sc->sc_dev.dv_xname, reg & 0xff);
1156 #endif
1157 }
1158
1159 void
1160 sbscn_loadchannelregs(struct sbscn_channel *ch)
1161 {
1162
1163 /* XXXXX necessary? */
1164 sbscn_iflush(ch);
1165
1166 WRITE_REG(ch->ch_imr_base, 0);
1167
1168 // XXX disable?
1169 WRITE_REG(ch->ch_base + 0x00, ch->ch_mode1);
1170 WRITE_REG(ch->ch_base + 0x10, ch->ch_mode2);
1171 WRITE_REG(ch->ch_base + 0x30, ch->ch_brc);
1172
1173 ch->ch_oports_active = ch->ch_oports;
1174 SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active);
1175
1176 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1177 }
1178
1179 int
1180 sbscn_hwiflow(struct tty *tp, int block)
1181 {
1182 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)];
1183 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
1184 int s;
1185
1186 if (ch->ch_o_rts == 0)
1187 return (0);
1188
1189 s = splserial();
1190 if (block) {
1191 if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1192 SET(ch->ch_rx_flags, RX_TTY_BLOCKED);
1193 sbscn_dohwiflow(ch);
1194 }
1195 } else {
1196 if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
1197 CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1198 sbscn_schedrx(ch);
1199 }
1200 if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1201 CLR(ch->ch_rx_flags, RX_TTY_BLOCKED);
1202 sbscn_dohwiflow(ch);
1203 }
1204 }
1205 splx(s);
1206 return (1);
1207 }
1208
1209 /*
1210 * (un)block input via hw flowcontrol
1211 */
1212 void
1213 sbscn_dohwiflow(struct sbscn_channel *ch)
1214 {
1215
1216 if (ch->ch_o_rts == 0)
1217 return;
1218
1219 if (ISSET(ch->ch_rx_flags, RX_ANY_BLOCK)) {
1220 CLR(ch->ch_oports, ch->ch_o_rts);
1221 CLR(ch->ch_oports_active, ch->ch_o_rts);
1222 } else {
1223 SET(ch->ch_oports, ch->ch_o_rts);
1224 SET(ch->ch_oports_active, ch->ch_o_rts);
1225 }
1226 SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active);
1227 }
1228
1229 void
1230 sbscn_start(struct tty *tp)
1231 {
1232 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)];
1233 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
1234 int s;
1235
1236 s = spltty();
1237 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1238 goto out;
1239 if (ch->ch_tx_stopped)
1240 goto out;
1241
1242 if (tp->t_outq.c_cc <= tp->t_lowat) {
1243 if (ISSET(tp->t_state, TS_ASLEEP)) {
1244 CLR(tp->t_state, TS_ASLEEP);
1245 wakeup(&tp->t_outq);
1246 }
1247 selwakeup(&tp->t_wsel);
1248 if (tp->t_outq.c_cc == 0)
1249 goto out;
1250 }
1251
1252 /* Grab the first contiguous region of buffer space. */
1253 {
1254 u_char *tba;
1255 int tbc;
1256
1257 tba = tp->t_outq.c_cf;
1258 tbc = ndqb(&tp->t_outq, 0);
1259
1260 (void)splserial();
1261
1262 ch->ch_tba = tba;
1263 ch->ch_tbc = tbc;
1264 }
1265
1266 SET(tp->t_state, TS_BUSY);
1267 ch->ch_tx_busy = 1;
1268
1269 /* Enable transmit completion interrupts if necessary. */
1270 if (!ISSET(ch->ch_imr, 0x1)) {
1271 SET(ch->ch_imr, 0x1);
1272 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1273 }
1274
1275 /* Output the first chunk of the contiguous buffer. */
1276 {
1277 u_char c;
1278
1279 while (ch->ch_tbc && READ_REG(ch->ch_base + 0x20) & 0x04) {
1280 c = *ch->ch_tba++;
1281 ch->ch_tbc--;
1282 WRITE_REG(ch->ch_base + 0x70, c);
1283 }
1284 }
1285 out:
1286 splx(s);
1287 return;
1288 }
1289
1290 /*
1291 * Stop output on a line.
1292 */
1293 void
1294 sbscnstop(struct tty *tp, int flag)
1295 {
1296 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)];
1297 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
1298 int s;
1299
1300 s = splserial();
1301 if (ISSET(tp->t_state, TS_BUSY)) {
1302 /* Stop transmitting at the next chunk. */
1303 ch->ch_tbc = 0;
1304 ch->ch_heldtbc = 0;
1305 if (!ISSET(tp->t_state, TS_TTSTOP))
1306 SET(tp->t_state, TS_FLUSH);
1307 }
1308 splx(s);
1309 }
1310
1311 void
1312 sbscn_diag(arg)
1313 void *arg;
1314 {
1315 struct sbscn_channel *ch = arg;
1316 struct sbscn_softc *sc = ch->ch_sc;
1317 int overflows, floods;
1318 int s;
1319
1320 s = splserial();
1321 overflows = ch->ch_overflows;
1322 ch->ch_overflows = 0;
1323 floods = ch->ch_floods;
1324 ch->ch_floods = 0;
1325 ch->ch_errors = 0;
1326 splx(s);
1327
1328 log(LOG_WARNING, "%s: channel %d: %d fifo overflow%s, %d ibuf flood%s\n",
1329 sc->sc_dev.dv_xname, ch->ch_num,
1330 overflows, overflows == 1 ? "" : "s",
1331 floods, floods == 1 ? "" : "s");
1332 }
1333
1334 integrate void
1335 sbscn_rxsoft(struct sbscn_channel *ch, struct tty *tp)
1336 {
1337 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1338 u_char *get, *end;
1339 u_int cc, scc;
1340 u_char sr;
1341 int code;
1342 int s;
1343
1344 end = ch->ch_ebuf;
1345 get = ch->ch_rbget;
1346 scc = cc = sbscn_rbuf_size - ch->ch_rbavail;
1347
1348 if (cc == sbscn_rbuf_size) {
1349 ch->ch_floods++;
1350 if (ch->ch_errors++ == 0)
1351 callout_reset(&ch->ch_diag_callout, 60 * hz,
1352 sbscn_diag, ch);
1353 }
1354
1355 while (cc) {
1356 code = get[0];
1357 sr = get[1];
1358 if (ISSET(sr, 0xf0)) {
1359 if (ISSET(sr, 0x10)) {
1360 ch->ch_overflows++;
1361 if (ch->ch_errors++ == 0)
1362 callout_reset(&ch->ch_diag_callout,
1363 60 * hz, sbscn_diag, ch);
1364 }
1365 if (ISSET(sr, 0xc0))
1366 SET(code, TTY_FE);
1367 if (ISSET(sr, 0x20))
1368 SET(code, TTY_PE);
1369 }
1370 if ((*rint)(code, tp) == -1) {
1371 /*
1372 * The line discipline's buffer is out of space.
1373 */
1374 if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1375 /*
1376 * We're either not using flow control, or the
1377 * line discipline didn't tell us to block for
1378 * some reason. Either way, we have no way to
1379 * know when there's more space available, so
1380 * just drop the rest of the data.
1381 */
1382 get += cc << 1;
1383 if (get >= end)
1384 get -= sbscn_rbuf_size << 1;
1385 cc = 0;
1386 } else {
1387 /*
1388 * Don't schedule any more receive processing
1389 * until the line discipline tells us there's
1390 * space available (through comhwiflow()).
1391 * Leave the rest of the data in the input
1392 * buffer.
1393 */
1394 SET(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1395 }
1396 break;
1397 }
1398 get += 2;
1399 if (get >= end)
1400 get = ch->ch_rbuf;
1401 cc--;
1402 }
1403
1404 if (cc != scc) {
1405 ch->ch_rbget = get;
1406 s = splserial();
1407 cc = ch->ch_rbavail += scc - cc;
1408 /* Buffers should be ok again, release possible block. */
1409 if (cc >= ch->ch_r_lowat) {
1410 if (ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
1411 CLR(ch->ch_rx_flags, RX_IBUF_OVERFLOWED);
1412 SET(ch->ch_imr, 0x02);
1413 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1414 }
1415 if (ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED)) {
1416 CLR(ch->ch_rx_flags, RX_IBUF_BLOCKED);
1417 sbscn_dohwiflow(ch);
1418 }
1419 }
1420 splx(s);
1421 }
1422 }
1423
1424 integrate void
1425 sbscn_txsoft(struct sbscn_channel *ch, struct tty *tp)
1426 {
1427
1428 CLR(tp->t_state, TS_BUSY);
1429 if (ISSET(tp->t_state, TS_FLUSH))
1430 CLR(tp->t_state, TS_FLUSH);
1431 else
1432 ndflush(&tp->t_outq, (int)(ch->ch_tba - tp->t_outq.c_cf));
1433 (*tp->t_linesw->l_start)(tp);
1434 }
1435
1436 integrate void
1437 sbscn_stsoft(struct sbscn_channel *ch, struct tty *tp)
1438 {
1439 u_char iports, delta;
1440 int s;
1441
1442 s = splserial();
1443 iports = ch->ch_iports;
1444 delta = ch->ch_iports_delta;
1445 ch->ch_iports_delta = 0;
1446 splx(s);
1447
1448 if (ISSET(delta, ch->ch_i_dcd)) {
1449 /*
1450 * Inform the tty layer that carrier detect changed.
1451 */
1452 (void) (*tp->t_linesw->l_modem)(tp,
1453 ISSET(iports, ch->ch_i_dcd));
1454 }
1455
1456 if (ISSET(delta, ch->ch_i_cts)) {
1457 /* Block or unblock output according to flow control. */
1458 if (ISSET(iports, ch->ch_i_cts)) {
1459 ch->ch_tx_stopped = 0;
1460 (*tp->t_linesw->l_start)(tp);
1461 } else {
1462 ch->ch_tx_stopped = 1;
1463 }
1464 }
1465
1466 #ifdef SBSCN_DEBUG
1467 if (sbscn_debug)
1468 sbscn_status(ch, "sbscn_stsoft");
1469 #endif
1470 }
1471
1472 void
1473 sbscn_soft(void *arg)
1474 {
1475 struct sbscn_channel *ch = arg;
1476 struct tty *tp = ch->ch_tty;
1477
1478 if (ch->ch_rx_ready) {
1479 ch->ch_rx_ready = 0;
1480 sbscn_rxsoft(ch, tp);
1481 }
1482
1483 if (ch->ch_st_check) {
1484 ch->ch_st_check = 0;
1485 sbscn_stsoft(ch, tp);
1486 }
1487
1488 if (ch->ch_tx_done) {
1489 ch->ch_tx_done = 0;
1490 sbscn_txsoft(ch, tp);
1491 }
1492 }
1493
1494 void
1495 sbscn_intr(void *arg, uint32_t status, uint32_t pc)
1496 {
1497 struct sbscn_channel *ch = arg;
1498 u_char *put, *end;
1499 u_int cc;
1500 u_char isr, sr;
1501
1502 /* read ISR */
1503 isr = READ_REG(ch->ch_isr_base) & ch->ch_imr;
1504 if (isr == 0)
1505 return;
1506
1507 end = ch->ch_ebuf;
1508 put = ch->ch_rbput;
1509 cc = ch->ch_rbavail;
1510
1511 do {
1512 u_char iports, delta;
1513
1514 if (isr & 0x02) {
1515
1516 sr = READ_REG(ch->ch_base + 0x20);
1517 /* XXX sr 0x01 bit must be set at this point */
1518
1519 #if defined(DDB) || defined(KGDB)
1520 if ((sr & 0x80) == 0x80) {
1521 #ifdef DDB
1522 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
1523 (void)READ_REG(ch->ch_base + 0x60);
1524 console_debugger();
1525 continue;
1526 }
1527 #endif
1528 #ifdef KGDB
1529 if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB)) {
1530 (void)READ_REG(ch->ch_base + 0x60);
1531 kgdb_connect(1);
1532 continue;
1533 }
1534 #endif
1535 }
1536 #endif /* DDB || KGDB */
1537
1538 if (!ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
1539 while (cc > 0) {
1540 put[0] = READ_REG(ch->ch_base + 0x60);
1541 put[1] = sr;
1542 put += 2;
1543 if (put >= end)
1544 put = ch->ch_rbuf;
1545 cc--;
1546
1547 sr = READ_REG(ch->ch_base + 0x20);
1548 if (!ISSET(sr, 0x02))
1549 break;
1550 }
1551
1552 /*
1553 * Current string of incoming characters ended
1554 * because no more data was available or we
1555 * ran out of space. Schedule a receive event
1556 * if any data was received. If we're out of
1557 * space, turn off receive interrupts.
1558 */
1559 ch->ch_rbput = put;
1560 ch->ch_rbavail = cc;
1561 if (!ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED))
1562 ch->ch_rx_ready = 1;
1563
1564 /*
1565 * See if we are in danger of overflowing a
1566 * buffer. If so, use hardware flow control
1567 * to ease the pressure.
1568 */
1569 if (!ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED) &&
1570 cc < ch->ch_r_hiwat) {
1571 SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
1572 sbscn_dohwiflow(ch);
1573 }
1574
1575 /*
1576 * If we're out of space, disable receive
1577 * interrupts until the queue has drained
1578 * a bit.
1579 */
1580 if (!cc) {
1581 SET(ch->ch_rx_flags,
1582 RX_IBUF_OVERFLOWED);
1583 CLR(ch->ch_imr, 0x02);
1584 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1585 }
1586 } else {
1587 /* XXX panic? */
1588 CLR(ch->ch_imr, 0x02);
1589 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1590 continue;
1591 }
1592 }
1593
1594 if (isr & 0x01) {
1595 CLR(ch->ch_imr, 0x01);
1596 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1597 }
1598
1599 #if 0
1600 XXX
1601 if (isr & 0x08) {
1602 clear input signal change!
1603 }
1604 #endif
1605 iports = GET_INPUT_SIGNALS(ch);
1606 delta = iports ^ ch->ch_iports;
1607 ch->ch_iports = iports;
1608
1609 /*
1610 * Process normal status changes
1611 */
1612 if (ISSET(delta, ch->ch_i_mask)) {
1613 SET(ch->ch_iports_delta, delta);
1614
1615 /*
1616 * Stop output immediately if we lose the output
1617 * flow control signal or carrier detect.
1618 */
1619 if (ISSET(~iports, ch->ch_i_mask)) {
1620 ch->ch_tbc = 0;
1621 ch->ch_heldtbc = 0;
1622 #ifdef SBSCN_DEBUG
1623 if (sbscn_debug)
1624 sbscn_status(ch, "sbscn_intr ");
1625 #endif
1626 }
1627
1628 ch->ch_st_check = 1;
1629 }
1630 } while ((isr = (READ_REG(ch->ch_isr_base) & ch->ch_imr)) != 0);
1631
1632 /*
1633 * Done handling any receive interrupts and status changes, and
1634 * clearing the tx-ready interrupt if it was set. See if data can
1635 * be transmitted as well. Schedule tx done event if no data left
1636 * and tty was marked busy.
1637 */
1638 sr = READ_REG(ch->ch_base + 0x20);
1639 if (ISSET(sr, 0x4)) {
1640
1641 /*
1642 * If we've delayed a parameter change, do it now, and restart
1643 * output.
1644 */
1645 if (ch->ch_heldchange) {
1646 sbscn_loadchannelregs(ch);
1647 ch->ch_heldchange = 0;
1648 ch->ch_tbc = ch->ch_heldtbc;
1649 ch->ch_heldtbc = 0;
1650 }
1651
1652 /* Output the next chunk of the contiguous buffer, if any. */
1653 if (ch->ch_tbc > 0) {
1654 int wrote1;
1655 u_char c;
1656
1657 wrote1 = 0;
1658 while (ch->ch_tbc &&
1659 READ_REG(ch->ch_base + 0x20) & 0x04) {
1660 wrote1 = 1;
1661 c = *ch->ch_tba++;
1662 ch->ch_tbc--;
1663 WRITE_REG(ch->ch_base + 0x70, c);
1664 }
1665 if (wrote1) {
1666 SET(ch->ch_imr, 0x01);
1667 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1668 }
1669 } else {
1670 /*
1671 * transmit completion interrupts already disabled,
1672 * mark the channel tx state as done.
1673 */
1674 if (ch->ch_tx_busy) {
1675 ch->ch_tx_busy = 0;
1676 ch->ch_tx_done = 1;
1677 }
1678 }
1679 }
1680
1681 /* Wake up the poller. */
1682 softintr_schedule(ch->ch_si);
1683
1684 #if NRND > 0 && defined(RND_SBSCN)
1685 rnd_add_uint32(&ch->ch_rnd_source, isr | sr);
1686 #endif
1687 }
1688
1689 /*
1690 * The following functions are polled getc and putc routines, shared
1691 * by the console and kgdb glue.
1692 */
1693
1694 int
1695 sbscn_common_getc(u_long addr, int chan)
1696 {
1697 int s = splhigh();
1698 u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
1699 int c;
1700
1701 /* block until a character becomes available */
1702 while ((READ_REG(base + 0x20) & 0x01) == 0)
1703 continue;
1704
1705 c = READ_REG(base + 0x60) & 0xff;
1706 splx(s);
1707 return (c);
1708 }
1709
1710 void
1711 sbscn_common_putc(u_long addr, int chan, int c)
1712 {
1713 int s = splhigh();
1714 int timo;
1715 u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
1716
1717 /* wait for any pending transmission to finish */
1718 timo = 1500000;
1719 while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo)
1720 continue;
1721
1722 WRITE_REG(base + 0x70, c);
1723
1724 /* wait for this transmission to complete */
1725 timo = 15000000;
1726 while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo)
1727 continue;
1728
1729 splx(s);
1730 }
1731
1732 /*
1733 * Initialize UART for use as console or KGDB line.
1734 */
1735 int
1736 sbscn_init(u_long addr, int chan, int rate, tcflag_t cflag)
1737 {
1738 #if 1
1739 u_long chanregbase = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
1740 u_long imaskreg = MIPS_PHYS_TO_KSEG1(addr + 0x230 + (chan * 0x20));
1741 u_char mode1, mode2;
1742 u_long brc;
1743 volatile int timo;
1744
1745 WRITE_REG(imaskreg, 0); /* disable channel intrs */
1746
1747 /* XXX should we really do the following? how about only if enabled? */
1748 /* wait for any pending transmission to finish */
1749 timo = 1500000;
1750 while ((READ_REG(chanregbase + 0x20) & 0x08) == 0 && --timo)
1751 continue;
1752
1753 /* XXX: wait a little. THIS SHOULD NOT BE NECESSARY!!! (?) */
1754 timo = 1500000;
1755 while (--timo)
1756 ;
1757
1758 WRITE_REG(chanregbase + 0x50, 2 << 4); /* reset receiver */
1759 WRITE_REG(chanregbase + 0x50, 3 << 4); /* reset transmitter */
1760
1761 /* set up the line for use */
1762 (void)cflag2modes(cflag, &mode1, &mode2);
1763 (void)sbscn_speed(rate, &brc);
1764 WRITE_REG(chanregbase + 0x00, mode1);
1765 WRITE_REG(chanregbase + 0x10, mode2);
1766 WRITE_REG(chanregbase + 0x30, brc);
1767
1768 /* enable transmit and receive */
1769 #define M_DUART_RX_EN 0x01
1770 #define M_DUART_TX_EN 0x04
1771 WRITE_REG(chanregbase + 0x50,M_DUART_RX_EN | M_DUART_TX_EN);
1772 #endif
1773
1774 /* XXX: wait a little. THIS SHOULD NOT BE NECESSARY!!! (?) */
1775 timo = 1500000;
1776 while (--timo)
1777 ;
1778
1779 #if 0 /* XXXCGD */
1780 bus_space_handle_t ioh;
1781
1782 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
1783 return (ENOMEM); /* ??? */
1784
1785 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
1786 bus_space_write_1(iot, ioh, com_efr, 0);
1787 bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
1788 rate = comspeed(rate, frequency);
1789 bus_space_write_1(iot, ioh, com_dlbl, rate);
1790 bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
1791 bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
1792 bus_space_write_1(iot, ioh, com_mcr, MCR_DTR | MCR_RTS);
1793 bus_space_write_1(iot, ioh, com_fifo,
1794 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
1795 bus_space_write_1(iot, ioh, com_ier, 0);
1796
1797 *iohp = ioh;
1798 return (0);
1799 #endif /* XXXCGD */
1800 /* XXXCGD */
1801 return (0);
1802 }
1803
1804 /*
1805 * Following are all routines needed for sbscn to act as console
1806 */
1807 int
1808 sbscn_cnattach(u_long addr, int chan, int rate, tcflag_t cflag)
1809 {
1810 int res;
1811 static struct consdev sbscn_cons = {
1812 NULL, NULL, sbscn_cngetc, sbscn_cnputc, sbscn_cnpollc, NULL,
1813 NULL, NULL, NODEV, CN_NORMAL
1814 };
1815
1816 res = sbscn_init(addr, chan, rate, cflag);
1817 if (res)
1818 return (res);
1819
1820 cn_tab = &sbscn_cons;
1821
1822 sbscn_cons_present = 1;
1823 sbscn_cons_addr = addr;
1824 sbscn_cons_chan = chan;
1825 sbscn_cons_rate = rate;
1826 sbscn_cons_cflag = cflag;
1827
1828 return (0);
1829 }
1830
1831 int
1832 sbscn_cngetc(dev_t dev)
1833 {
1834
1835 return (sbscn_common_getc(sbscn_cons_addr, sbscn_cons_chan));
1836 }
1837
1838 /*
1839 * Console kernel output character routine.
1840 */
1841 void
1842 sbscn_cnputc(dev_t dev, int c)
1843 {
1844
1845 sbscn_common_putc(sbscn_cons_addr, sbscn_cons_chan, c);
1846 }
1847
1848 void
1849 sbscn_cnpollc(dev_t dev, int on)
1850 {
1851
1852 }
1853
1854 #ifdef KGDB
1855 int
1856 sbscn_kgdb_attach(u_long addr, int chan, int rate, tcflag_t cflag)
1857 {
1858 int res;
1859
1860 if (!sbscn_cons_present &&
1861 sbscn_cons_addr == addr && sbscn_cons_chan == chan)
1862 return (EBUSY); /* cannot share with console */
1863
1864 res = sbscn_init(addr, chan, rate, cflag);
1865 if (res)
1866 return (res);
1867
1868 kgdb_attach(sbscn_kgdb_getc, sbscn_kgdb_putc, NULL);
1869 kgdb_dev = 123; /* unneeded, only to satisfy some tests */
1870
1871 sbscn_kgdb_present = 1;
1872 sbscn_kgdb_addr = addr;
1873 sbscn_kgdb_chan = chan;
1874
1875 return (0);
1876 }
1877
1878 /* ARGSUSED */
1879 int
1880 sbscn_kgdb_getc(arg)
1881 void *arg;
1882 {
1883
1884 return (sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan));
1885 }
1886
1887 /* ARGSUSED */
1888 void
1889 sbscn_kgdb_putc(arg, c)
1890 void *arg;
1891 int c;
1892 {
1893
1894 sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan, c);
1895 }
1896 #endif /* KGDB */
1897
1898 /*
1899 * helper function to identify the sbscn channels used by
1900 * console or KGDB (and not yet autoconf attached)
1901 */
1902 int
1903 sbscn_is_console(u_long addr, int chan)
1904 {
1905
1906 if (sbscn_cons_present && !sbscn_cons_attached &&
1907 sbscn_cons_addr == addr && sbscn_cons_chan == chan)
1908 return (1);
1909 #ifdef KGDB
1910 if (sbscn_kgdb_present && !sbscn_kgdb_attached &&
1911 sbscn_kgdb_addr == addr && sbscn_kgdb_chan == chan)
1912 return (1);
1913 #endif
1914 return (0);
1915 }
1916