sbscn.c revision 1.11 1 /* $NetBSD: sbscn.c,v 1.11 2003/07/15 02:43:40 lukem Exp $ */
2
3 /*
4 * Copyright 2000, 2001
5 * Broadcom Corporation. All rights reserved.
6 *
7 * This software is furnished under license and may be used and copied only
8 * in accordance with the following terms and conditions. Subject to these
9 * conditions, you may download, copy, install, use, modify and distribute
10 * modified or unmodified copies of this software in source and/or binary
11 * form. No title or ownership is transferred hereby.
12 *
13 * 1) Any source code used, modified or distributed must reproduce and
14 * retain this copyright notice and list of conditions as they appear in
15 * the source file.
16 *
17 * 2) No right is granted to use any trade name, trademark, or logo of
18 * Broadcom Corporation. The "Broadcom Corporation" name may not be
19 * used to endorse or promote products derived from this software
20 * without the prior written permission of Broadcom Corporation.
21 *
22 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
25 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
26 * FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
27 * LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32 * OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /* from: $NetBSD: com.c,v 1.172 2000/05/03 19:19:04 thorpej Exp */
36
37 /*-
38 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
39 * All rights reserved.
40 *
41 * This code is derived from software contributed to The NetBSD Foundation
42 * by Charles M. Hannum.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the NetBSD
55 * Foundation, Inc. and its contributors.
56 * 4. Neither the name of The NetBSD Foundation nor the names of its
57 * contributors may be used to endorse or promote products derived
58 * from this software without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
61 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
62 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
63 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
64 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
65 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
66 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
67 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
68 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
69 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
70 * POSSIBILITY OF SUCH DAMAGE.
71 */
72
73 /*
74 * Copyright (c) 1991 The Regents of the University of California.
75 * All rights reserved.
76 *
77 * Redistribution and use in source and binary forms, with or without
78 * modification, are permitted provided that the following conditions
79 * are met:
80 * 1. Redistributions of source code must retain the above copyright
81 * notice, this list of conditions and the following disclaimer.
82 * 2. Redistributions in binary form must reproduce the above copyright
83 * notice, this list of conditions and the following disclaimer in the
84 * documentation and/or other materials provided with the distribution.
85 * 3. All advertising materials mentioning features or use of this software
86 * must display the following acknowledgement:
87 * This product includes software developed by the University of
88 * California, Berkeley and its contributors.
89 * 4. Neither the name of the University nor the names of its contributors
90 * may be used to endorse or promote products derived from this software
91 * without specific prior written permission.
92 *
93 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
94 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
95 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
96 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
97 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
98 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
99 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
101 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
102 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
103 * SUCH DAMAGE.
104 *
105 * @(#)com.c 7.5 (Berkeley) 5/16/91
106 */
107
108 /*
109 * `sbscn' driver, supports SiByte SB-1250 SOC DUART.
110 *
111 * This DUART is quite similar in programming model to the scn2681/68681
112 * DUARTs supported by the NetBSD/amiga `mfc' and NetBSD/pc532 `scn'
113 * driver, but substantial rework would have been necessary to make
114 * those drivers sane w.r.t. bus_space (which would then have been
115 * required on NetBSD/sbmips very early on), and to accommodate the
116 * different register mappings.
117 *
118 * So, another driver. Eventually there should be One True Driver,
119 * but we're not here to save the world.
120 */
121
122 #include <sys/cdefs.h>
123 __KERNEL_RCSID(0, "$NetBSD: sbscn.c,v 1.11 2003/07/15 02:43:40 lukem Exp $");
124
125 #define SBSCN_DEBUG
126
127 #include "opt_ddb.h"
128
129 #include "rnd.h"
130 #if NRND > 0 && defined(RND_SBSCN)
131 #include <sys/rnd.h>
132 #endif
133
134 #include <sys/param.h>
135 #include <sys/systm.h>
136 #include <sys/ioctl.h>
137 #include <sys/select.h>
138 #include <sys/tty.h>
139 #include <sys/proc.h>
140 #include <sys/user.h>
141 #include <sys/conf.h>
142 #include <sys/file.h>
143 #include <sys/uio.h>
144 #include <sys/kernel.h>
145 #include <sys/syslog.h>
146 #include <sys/types.h>
147 #include <sys/device.h>
148 #include <sys/malloc.h>
149 #include <sys/vnode.h>
150
151 #include <mips/sibyte/dev/sbobiovar.h>
152 #if 0
153 #include <mips/sibyte/dev/sbscnreg.h>
154 #endif
155 #include <mips/sibyte/dev/sbscnvar.h>
156 #include <dev/cons.h>
157 #include <machine/locore.h>
158
159 void sbscn_attach_channel(struct sbscn_softc *sc, int chan, int intr);
160 #if defined(DDB) || defined(KGDB)
161 static void sbscn_enable_debugport(struct sbscn_channel *ch);
162 #endif
163 void sbscn_config(struct sbscn_channel *ch);
164 void sbscn_shutdown(struct sbscn_channel *ch);
165 int sbscn_speed(long, long *);
166 static int cflag2modes(tcflag_t, u_char *, u_char *);
167 int sbscn_param(struct tty *, struct termios *);
168 void sbscn_start(struct tty *);
169 int sbscn_hwiflow(struct tty *, int);
170
171 void sbscn_loadchannelregs(struct sbscn_channel *);
172 void sbscn_dohwiflow(struct sbscn_channel *);
173 void sbscn_break(struct sbscn_channel *, int);
174 void sbscn_modem(struct sbscn_channel *, int);
175 void tiocm_to_sbscn(struct sbscn_channel *, int, int);
176 int sbscn_to_tiocm(struct sbscn_channel *);
177 void sbscn_iflush(struct sbscn_channel *);
178
179 int sbscn_init(u_long addr, int chan, int rate, tcflag_t cflag);
180 int sbscn_common_getc(u_long addr, int chan);
181 void sbscn_common_putc(u_long addr, int chan, int c);
182 void sbscn_intr(void *arg, uint32_t status, uint32_t pc);
183
184 int sbscn_cngetc(dev_t dev);
185 void sbscn_cnputc(dev_t dev, int c);
186 void sbscn_cnpollc(dev_t dev, int on);
187
188 extern struct cfdriver sbscn_cd;
189
190 dev_type_open(sbscnopen);
191 dev_type_close(sbscnclose);
192 dev_type_read(sbscnread);
193 dev_type_write(sbscnwrite);
194 dev_type_ioctl(sbscnioctl);
195 dev_type_stop(sbscnstop);
196 dev_type_tty(sbscntty);
197 dev_type_poll(sbscnpoll);
198
199 const struct cdevsw sbscn_cdevsw = {
200 sbscnopen, sbscnclose, sbscnread, sbscnwrite, sbscnioctl,
201 sbscnstop, sbscntty, sbscnpoll, nommap, ttykqfilter, D_TTY
202 };
203
204 #define integrate static inline
205 void sbscn_soft(void *);
206 integrate void sbscn_rxsoft(struct sbscn_channel *, struct tty *);
207 integrate void sbscn_txsoft(struct sbscn_channel *, struct tty *);
208 integrate void sbscn_stsoft(struct sbscn_channel *, struct tty *);
209 integrate void sbscn_schedrx(struct sbscn_channel *);
210 void sbscn_diag(void *);
211
212 /*
213 * Make this an option variable one can patch.
214 * But be warned: this must be a power of 2!
215 */
216 u_int sbscn_rbuf_size = SBSCN_RING_SIZE;
217
218 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
219 u_int sbscn_rbuf_hiwat = (SBSCN_RING_SIZE * 1) / 4;
220 u_int sbscn_rbuf_lowat = (SBSCN_RING_SIZE * 3) / 4;
221
222 static int sbscn_cons_present;
223 static int sbscn_cons_attached;
224 static u_long sbscn_cons_addr;
225 static int sbscn_cons_chan;
226 static int sbscn_cons_rate;
227 static tcflag_t sbscn_cons_cflag;
228
229 #ifdef KGDB
230 #include <sys/kgdb.h>
231
232 static int sbscn_kgdb_present;
233 static int sbscn_kgdb_attached;
234 static u_long sbscn_kgdb_addr;
235 static int sbscn_kgdb_chan;
236
237 int sbscn_kgdb_getc(void *);
238 void sbscn_kgdb_putc(void *, int);
239 #endif /* KGDB */
240
241 static int sbscn_match(struct device *, struct cfdata *, void *);
242 static void sbscn_attach(struct device *, struct device *, void *);
243
244 CFATTACH_DECL(sbscn, sizeof(struct sbscn_softc),
245 sbscn_match, sbscn_attach, NULL, NULL);
246
247 #define READ_REG(rp) (mips3_ld((uint64_t *)(rp)))
248 #define WRITE_REG(rp, val) (mips3_sd((uint64_t *)(rp), (val)))
249
250 /*
251 * input and output signals are actually the _inverse_ of the bits in the
252 * input and output port registers!
253 */
254 #define GET_INPUT_SIGNALS(ch) \
255 ((~READ_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x280))) & (ch)->ch_i_mask)
256 #define SET_OUTPUT_SIGNALS(ch, val) \
257 do { \
258 int sigs_to_set, sigs_to_clr; \
259 \
260 sigs_to_set = (ch)->ch_o_mask & val; \
261 sigs_to_clr = (ch)->ch_o_mask & ~val; \
262 \
263 /* set signals by clearing op bits, and vice versa */ \
264 WRITE_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x2c0), \
265 sigs_to_set); \
266 WRITE_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x2b0), \
267 sigs_to_clr); \
268 } while (0)
269
270 static int
271 sbscn_match(struct device *parent, struct cfdata *match, void *aux)
272 {
273 struct sbobio_attach_args *sap = aux;
274
275 if (sap->sa_locs.sa_type != SBOBIO_DEVTYPE_DUART)
276 return (0);
277
278 return 1;
279 }
280
281 static void
282 sbscn_attach(struct device *parent, struct device *self, void *aux)
283 {
284 struct sbscn_softc *sc = (struct sbscn_softc *)self;
285 struct sbobio_attach_args *sap = aux;
286 int i;
287
288 sc->sc_addr = sap->sa_base + sap->sa_locs.sa_offset;
289
290 printf("\n");
291 for (i = 0; i < 2; i++)
292 sbscn_attach_channel(sc, i, sap->sa_locs.sa_intr[i]);
293
294 /* init duart_opcr */
295 WRITE_REG(MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x270), 0);
296 /* init duart_aux_ctrl */
297 WRITE_REG(MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x210), 0x0f); /* XXX */
298 }
299
300 void
301 sbscn_attach_channel(struct sbscn_softc *sc, int chan, int intr)
302 {
303 struct sbscn_channel *ch = &sc->sc_channels[chan];
304 u_long chan_addr;
305 struct tty *tp;
306
307 ch->ch_sc = sc;
308 ch->ch_num = chan;
309
310 chan_addr = sc->sc_addr + (0x100 * chan);
311 ch->ch_base = (void *)MIPS_PHYS_TO_KSEG1(chan_addr);
312 ch->ch_isr_base =
313 (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x220 + (0x20 * chan));
314 ch->ch_imr_base =
315 (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x230 + (0x20 * chan));
316 #ifdef XXXCGDnotyet
317 ch->ch_inchg_base =
318 (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x2d0 + (0x10 * chan));
319 #endif
320
321 ch->ch_i_dcd = ch->ch_i_dcd_pin = 0 /* XXXCGD */;
322 ch->ch_i_cts = ch->ch_i_cts_pin = 0 /* XXXCGD */;
323 ch->ch_i_dsr = ch->ch_i_dsr_pin = 0 /* XXXCGD */;
324 ch->ch_i_ri = ch->ch_i_ri_pin = 0 /* XXXCGD */;
325 ch->ch_i_mask =
326 ch->ch_i_dcd | ch->ch_i_cts | ch->ch_i_dsr | ch->ch_i_ri;
327 ch->ch_o_dtr = ch->ch_o_dtr_pin = 0 /* XXXCGD */;
328 ch->ch_o_rts = ch->ch_o_rts_pin = 0 /* XXXCGD */;
329 ch->ch_o_mask = ch->ch_o_dtr | ch->ch_o_rts;
330
331 ch->ch_intrhand = cpu_intr_establish(intr, IPL_SERIAL, sbscn_intr, ch);
332 callout_init(&ch->ch_diag_callout);
333
334 /* Disable interrupts before configuring the device. */
335 ch->ch_imr = 0;
336 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
337
338 if (sbscn_cons_present &&
339 sbscn_cons_addr == chan_addr && sbscn_cons_chan == chan) {
340 sbscn_cons_attached = 1;
341
342 /* Make sure the console is always "hardwired". */
343 delay(1000); /* wait for output to finish */
344 SET(ch->ch_hwflags, SBSCN_HW_CONSOLE);
345 SET(ch->ch_swflags, TIOCFLAG_SOFTCAR);
346 }
347
348 tp = ttymalloc();
349 tp->t_oproc = sbscn_start;
350 tp->t_param = sbscn_param;
351 tp->t_hwiflow = sbscn_hwiflow;
352
353 ch->ch_tty = tp;
354 ch->ch_rbuf = malloc(sbscn_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
355 if (ch->ch_rbuf == NULL) {
356 printf("%s: channel %d: unable to allocate ring buffer\n",
357 sc->sc_dev.dv_xname, chan);
358 return;
359 }
360 ch->ch_ebuf = ch->ch_rbuf + (sbscn_rbuf_size << 1);
361
362 tty_attach(tp);
363
364 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
365 int maj;
366
367 /* locate the major number */
368 maj = cdevsw_lookup_major(&sbscn_cdevsw);
369
370 cn_tab->cn_dev = makedev(maj, (sc->sc_dev.dv_unit << 1) + chan);
371
372 printf("%s: channel %d: console\n", sc->sc_dev.dv_xname, chan);
373 }
374
375 #ifdef KGDB
376 /*
377 * Allow kgdb to "take over" this port. If this is
378 * the kgdb device, it has exclusive use.
379 */
380 if (sbscn_kgdb_present &&
381 sbscn_kgdb_addr == chan_addr && sbscn_kgdb_chan == chan) {
382 sbscn_kgdb_attached = 1;
383
384 SET(sc->sc_hwflags, SBSCN_HW_KGDB);
385 printf("%s: channel %d: kgdb\n", sc->sc_dev.dv_xname, chan);
386 }
387 #endif
388
389 ch->ch_si = softintr_establish(IPL_SOFTSERIAL, sbscn_soft, ch);
390
391 #if NRND > 0 && defined(RND_SBSCN)
392 rnd_attach_source(&ch->ch_rnd_source, sc->sc_dev.dv_xname,
393 RND_TYPE_TTY, 0);
394 #endif
395
396 sbscn_config(ch);
397
398 SET(ch->ch_hwflags, SBSCN_HW_DEV_OK);
399 }
400
401 int
402 sbscn_speed(long speed, long *brcp)
403 {
404 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
405
406 int x, err;
407 int frequency = 100000000;
408
409 *brcp = divrnd(frequency / 20, speed) - 1;
410
411 if (speed <= 0)
412 return (-1);
413 x = divrnd(frequency / 20, speed);
414 if (x <= 0)
415 return (-1);
416 err = divrnd(((quad_t)frequency) * 1000 / 20, speed * x) - 1000;
417 if (err < 0)
418 err = -err;
419 if (err > SBSCN_TOLERANCE)
420 return (-1);
421 *brcp = x - 1;
422 return (0);
423
424 #undef divrnd
425 }
426
427 #ifdef SBSCN_DEBUG
428 void sbscn_status(struct sbscn_channel *, char *);
429
430 int sbscn_debug = 0 /* XXXCGD */;
431
432 void
433 sbscn_status(struct sbscn_channel *ch, char *str)
434 {
435 struct sbscn_softc *sc = ch->ch_sc;
436 struct tty *tp = ch->ch_tty;
437
438 printf("%s: chan %d: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n",
439 sc->sc_dev.dv_xname, ch->ch_num, str,
440 ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
441 ISSET(ch->ch_iports, ch->ch_i_dcd) ? "+" : "-",
442 ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
443 ISSET(ch->ch_oports, ch->ch_o_dtr) ? "+" : "-",
444 ch->ch_tx_stopped ? "+" : "-");
445
446 printf("%s: chan %d: %s %scrtscts %scts %sts_ttstop %srts %xrx_flags\n",
447 sc->sc_dev.dv_xname, ch->ch_num, str,
448 ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
449 ISSET(ch->ch_iports, ch->ch_i_cts) ? "+" : "-",
450 ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
451 ISSET(ch->ch_oports, ch->ch_o_rts) ? "+" : "-",
452 ch->ch_rx_flags);
453 }
454 #endif
455
456 #if defined(DDB) || defined(KGDB)
457 static void
458 sbscn_enable_debugport(struct sbscn_channel *ch)
459 {
460 int s;
461
462 /* Turn on line break interrupt, set carrier. */
463 s = splserial();
464
465 #if 0 /* DO NOT turn on break interrupt at this time. */
466 ch->ch_imr = 0x04;
467 #else
468 ch->ch_imr = 0x00;
469 #endif
470 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
471 SET(ch->ch_oports, ch->ch_o_dtr | ch->ch_o_rts);
472 SET_OUTPUT_SIGNALS(ch, ch->ch_oports);
473
474 splx(s);
475 }
476 #endif
477
478 void
479 sbscn_config(struct sbscn_channel *ch)
480 {
481
482 /* Disable interrupts before configuring the device. */
483 ch->ch_imr = 0x00;
484 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
485
486 #ifdef DDB
487 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE))
488 sbscn_enable_debugport(ch);
489 #endif
490
491 #ifdef KGDB
492 /*
493 * Allow kgdb to "take over" this port. If this is
494 * the kgdb device, it has exclusive use.
495 */
496 if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB))
497 sbscn_enable_debugport(ch);
498 #endif
499 }
500
501 void
502 sbscn_shutdown(struct sbscn_channel *ch)
503 {
504 struct tty *tp = ch->ch_tty;
505 int s;
506
507 s = splserial();
508
509 /* If we were asserting flow control, then deassert it. */
510 SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
511 sbscn_dohwiflow(ch);
512
513 /* Clear any break condition set with TIOCSBRK. */
514 sbscn_break(ch, 0);
515
516 /*
517 * Hang up if necessary. Wait a bit, so the other side has time to
518 * notice even if we immediately open the port again.
519 * Avoid tsleeping above splhigh().
520 */
521 if (ISSET(tp->t_cflag, HUPCL)) {
522 sbscn_modem(ch, 0);
523 splx(s);
524 /* XXX tsleep will only timeout */
525 (void) tsleep(ch, TTIPRI, ttclos, hz);
526 s = splserial();
527 }
528
529 /* Turn off interrupts. */
530 #ifdef DDB
531 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE))
532 #if 0 /* DO NOT turn on break interrupt at this time. */
533 ch->ch_imr = 0x04; /* interrupt on break */
534 #else
535 ch->ch_imr = 0x00;
536 #endif
537 else
538 #endif
539 ch->ch_imr = 0;
540 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
541
542 splx(s);
543 }
544
545 int
546 sbscnopen(dev_t dev, int flag, int mode, struct proc *p)
547 {
548 int unit = SBSCN_UNIT(dev);
549 int chan = SBSCN_CHAN(dev);
550 struct sbscn_softc *sc;
551 struct sbscn_channel *ch;
552 struct tty *tp;
553 int s, s2;
554 int error;
555
556 if (unit >= sbscn_cd.cd_ndevs)
557 return (ENXIO);
558 sc = sbscn_cd.cd_devs[unit];
559 if (sc == 0)
560 return (ENXIO);
561 ch = &sc->sc_channels[chan];
562 if (!ISSET(ch->ch_hwflags, SBSCN_HW_DEV_OK) || ch->ch_rbuf == NULL)
563 return (ENXIO);
564
565 #ifdef KGDB
566 /*
567 * If this is the kgdb port, no other use is permitted.
568 */
569 if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB))
570 return (EBUSY);
571 #endif
572
573 tp = ch->ch_tty;
574
575 if (ISSET(tp->t_state, TS_ISOPEN) &&
576 ISSET(tp->t_state, TS_XCLUDE) &&
577 p->p_ucred->cr_uid != 0)
578 return (EBUSY);
579
580 s = spltty();
581
582 /*
583 * Do the following iff this is a first open.
584 */
585 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
586 struct termios t;
587
588 tp->t_dev = dev;
589
590 s2 = splserial();
591
592 /* Turn on receive, break, and status change interrupts. */
593 #if 0 /* DO NOT turn on break or status change interrupt at this time. */
594 ch->ch_imr = 0xe;
595 #else
596 ch->ch_imr = 0x2;
597 #endif
598 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
599
600 /* Fetch the current modem control status, needed later. */
601 ch->ch_iports = GET_INPUT_SIGNALS(ch);
602 ch->ch_iports_delta = 0;
603 splx(s2);
604
605 /*
606 * Initialize the termios status to the defaults. Add in the
607 * sticky bits from TIOCSFLAGS.
608 */
609 t.c_ispeed = 0;
610 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
611 t.c_ospeed = sbscn_cons_rate;
612 t.c_cflag = sbscn_cons_cflag;
613 } else {
614 t.c_ospeed = TTYDEF_SPEED;
615 t.c_cflag = TTYDEF_CFLAG;
616 }
617 if (ISSET(ch->ch_swflags, TIOCFLAG_CLOCAL))
618 SET(t.c_cflag, CLOCAL);
619 if (ISSET(ch->ch_swflags, TIOCFLAG_CRTSCTS))
620 SET(t.c_cflag, CRTSCTS);
621 if (ISSET(ch->ch_swflags, TIOCFLAG_MDMBUF))
622 SET(t.c_cflag, MDMBUF);
623 /* Make sure sbscn_param() will do something. */
624 tp->t_ospeed = 0;
625 (void) sbscn_param(tp, &t);
626 tp->t_iflag = TTYDEF_IFLAG;
627 tp->t_oflag = TTYDEF_OFLAG;
628 tp->t_lflag = TTYDEF_LFLAG;
629 ttychars(tp);
630 ttsetwater(tp);
631
632 s2 = splserial();
633
634 /*
635 * Turn on DTR. We must always do this, even if carrier is not
636 * present, because otherwise we'd have to use TIOCSDTR
637 * immediately after setting CLOCAL, which applications do not
638 * expect. We always assert DTR while the device is open
639 * unless explicitly requested to deassert it.
640 */
641 sbscn_modem(ch, 1);
642
643 /* Clear the input ring, and unblock. */
644 ch->ch_rbput = ch->ch_rbget = ch->ch_rbuf;
645 ch->ch_rbavail = sbscn_rbuf_size;
646 sbscn_iflush(ch);
647 CLR(ch->ch_rx_flags, RX_ANY_BLOCK);
648 sbscn_dohwiflow(ch);
649
650 #ifdef SBSCN_DEBUG
651 if (sbscn_debug)
652 sbscn_status(ch, "sbscnopen ");
653 #endif
654
655 splx(s2);
656 }
657
658 splx(s);
659
660 error = ttyopen(tp, SBSCN_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
661 if (error)
662 goto bad;
663
664 error = (*tp->t_linesw->l_open)(dev, tp);
665 if (error)
666 goto bad;
667
668 return (0);
669
670 bad:
671 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
672 /*
673 * We failed to open the device, and nobody else had it opened.
674 * Clean up the state as appropriate.
675 */
676 sbscn_shutdown(ch);
677 }
678
679 return (error);
680 }
681
682 int
683 sbscnclose(dev_t dev, int flag, int mode, struct proc *p)
684 {
685 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
686 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
687 struct tty *tp = ch->ch_tty;
688
689 /* XXX This is for cons.c. */
690 if (!ISSET(tp->t_state, TS_ISOPEN))
691 return (0);
692
693 (*tp->t_linesw->l_close)(tp, flag);
694 ttyclose(tp);
695
696 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
697 /*
698 * Although we got a last close, the device may still be in
699 * use; e.g. if this was the dialout node, and there are still
700 * processes waiting for carrier on the non-dialout node.
701 */
702 sbscn_shutdown(ch);
703 }
704
705 return (0);
706 }
707
708 int
709 sbscnread(dev_t dev, struct uio *uio, int flag)
710 {
711 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
712 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
713 struct tty *tp = ch->ch_tty;
714
715 return ((*tp->t_linesw->l_read)(tp, uio, flag));
716 }
717
718 int
719 sbscnwrite(dev_t dev, struct uio *uio, int flag)
720 {
721 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
722 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
723 struct tty *tp = ch->ch_tty;
724
725 return ((*tp->t_linesw->l_write)(tp, uio, flag));
726 }
727
728 int
729 sbscnpoll(dev_t dev, int events, struct proc *p)
730 {
731 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
732 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
733 struct tty *tp = ch->ch_tty;
734
735 return ((*tp->t_linesw->l_poll)(tp, events, p));
736 }
737
738 struct tty *
739 sbscntty(dev_t dev)
740 {
741 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
742 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
743 struct tty *tp = ch->ch_tty;
744
745 return (tp);
746 }
747
748 int
749 sbscnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
750 {
751 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
752 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
753 struct tty *tp = ch->ch_tty;
754 int error;
755 int s;
756
757 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
758 if (error != EPASSTHROUGH)
759 return (error);
760
761 error = ttioctl(tp, cmd, data, flag, p);
762 if (error != EPASSTHROUGH)
763 return (error);
764
765 error = 0;
766
767 s = splserial();
768
769 switch (cmd) {
770 case TIOCSBRK:
771 sbscn_break(ch, 1);
772 break;
773
774 case TIOCCBRK:
775 sbscn_break(ch, 0);
776 break;
777
778 case TIOCSDTR:
779 sbscn_modem(ch, 1);
780 break;
781
782 case TIOCCDTR:
783 sbscn_modem(ch, 0);
784 break;
785
786 case TIOCGFLAGS:
787 *(int *)data = ch->ch_swflags;
788 break;
789
790 case TIOCSFLAGS:
791 error = suser(p->p_ucred, &p->p_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 c, struct tty *tp) = 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