sbscn.c revision 1.19 1 /* $NetBSD: sbscn.c,v 1.19 2006/10/01 18:56:22 elad 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.19 2006/10/01 18:56:22 elad 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 (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
574 return (EBUSY);
575
576 s = spltty();
577
578 /*
579 * Do the following iff this is a first open.
580 */
581 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
582 struct termios t;
583
584 tp->t_dev = dev;
585
586 s2 = splserial();
587
588 /* Turn on receive, break, and status change interrupts. */
589 #if 0 /* DO NOT turn on break or status change interrupt at this time. */
590 ch->ch_imr = 0xe;
591 #else
592 ch->ch_imr = 0x2;
593 #endif
594 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
595
596 /* Fetch the current modem control status, needed later. */
597 ch->ch_iports = GET_INPUT_SIGNALS(ch);
598 ch->ch_iports_delta = 0;
599 splx(s2);
600
601 /*
602 * Initialize the termios status to the defaults. Add in the
603 * sticky bits from TIOCSFLAGS.
604 */
605 t.c_ispeed = 0;
606 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
607 t.c_ospeed = sbscn_cons_rate;
608 t.c_cflag = sbscn_cons_cflag;
609 } else {
610 t.c_ospeed = TTYDEF_SPEED;
611 t.c_cflag = TTYDEF_CFLAG;
612 }
613 if (ISSET(ch->ch_swflags, TIOCFLAG_CLOCAL))
614 SET(t.c_cflag, CLOCAL);
615 if (ISSET(ch->ch_swflags, TIOCFLAG_CRTSCTS))
616 SET(t.c_cflag, CRTSCTS);
617 if (ISSET(ch->ch_swflags, TIOCFLAG_MDMBUF))
618 SET(t.c_cflag, MDMBUF);
619 /* Make sure sbscn_param() will do something. */
620 tp->t_ospeed = 0;
621 (void) sbscn_param(tp, &t);
622 tp->t_iflag = TTYDEF_IFLAG;
623 tp->t_oflag = TTYDEF_OFLAG;
624 tp->t_lflag = TTYDEF_LFLAG;
625 ttychars(tp);
626 ttsetwater(tp);
627
628 s2 = splserial();
629
630 /*
631 * Turn on DTR. We must always do this, even if carrier is not
632 * present, because otherwise we'd have to use TIOCSDTR
633 * immediately after setting CLOCAL, which applications do not
634 * expect. We always assert DTR while the device is open
635 * unless explicitly requested to deassert it.
636 */
637 sbscn_modem(ch, 1);
638
639 /* Clear the input ring, and unblock. */
640 ch->ch_rbput = ch->ch_rbget = ch->ch_rbuf;
641 ch->ch_rbavail = sbscn_rbuf_size;
642 sbscn_iflush(ch);
643 CLR(ch->ch_rx_flags, RX_ANY_BLOCK);
644 sbscn_dohwiflow(ch);
645
646 #ifdef SBSCN_DEBUG
647 if (sbscn_debug)
648 sbscn_status(ch, "sbscnopen ");
649 #endif
650
651 splx(s2);
652 }
653
654 splx(s);
655
656 error = ttyopen(tp, SBSCN_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
657 if (error)
658 goto bad;
659
660 error = (*tp->t_linesw->l_open)(dev, tp);
661 if (error)
662 goto bad;
663
664 return (0);
665
666 bad:
667 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
668 /*
669 * We failed to open the device, and nobody else had it opened.
670 * Clean up the state as appropriate.
671 */
672 sbscn_shutdown(ch);
673 }
674
675 return (error);
676 }
677
678 int
679 sbscnclose(dev_t dev, int flag, int mode, struct lwp *l)
680 {
681 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
682 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
683 struct tty *tp = ch->ch_tty;
684
685 /* XXX This is for cons.c. */
686 if (!ISSET(tp->t_state, TS_ISOPEN))
687 return (0);
688
689 (*tp->t_linesw->l_close)(tp, flag);
690 ttyclose(tp);
691
692 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
693 /*
694 * Although we got a last close, the device may still be in
695 * use; e.g. if this was the dialout node, and there are still
696 * processes waiting for carrier on the non-dialout node.
697 */
698 sbscn_shutdown(ch);
699 }
700
701 return (0);
702 }
703
704 int
705 sbscnread(dev_t dev, struct uio *uio, int flag)
706 {
707 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
708 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
709 struct tty *tp = ch->ch_tty;
710
711 return ((*tp->t_linesw->l_read)(tp, uio, flag));
712 }
713
714 int
715 sbscnwrite(dev_t dev, struct uio *uio, int flag)
716 {
717 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
718 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
719 struct tty *tp = ch->ch_tty;
720
721 return ((*tp->t_linesw->l_write)(tp, uio, flag));
722 }
723
724 int
725 sbscnpoll(dev_t dev, int events, struct lwp *l)
726 {
727 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
728 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
729 struct tty *tp = ch->ch_tty;
730
731 return ((*tp->t_linesw->l_poll)(tp, events, l));
732 }
733
734 struct tty *
735 sbscntty(dev_t dev)
736 {
737 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
738 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
739 struct tty *tp = ch->ch_tty;
740
741 return (tp);
742 }
743
744 int
745 sbscnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
746 {
747 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)];
748 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
749 struct tty *tp = ch->ch_tty;
750 int error;
751 int s;
752
753 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
754 if (error != EPASSTHROUGH)
755 return (error);
756
757 error = ttioctl(tp, cmd, data, flag, l);
758 if (error != EPASSTHROUGH)
759 return (error);
760
761 error = 0;
762
763 s = splserial();
764
765 switch (cmd) {
766 case TIOCSBRK:
767 sbscn_break(ch, 1);
768 break;
769
770 case TIOCCBRK:
771 sbscn_break(ch, 0);
772 break;
773
774 case TIOCSDTR:
775 sbscn_modem(ch, 1);
776 break;
777
778 case TIOCCDTR:
779 sbscn_modem(ch, 0);
780 break;
781
782 case TIOCGFLAGS:
783 *(int *)data = ch->ch_swflags;
784 break;
785
786 case TIOCSFLAGS:
787 error = kauth_authorize_generic(l->l_cred,
788 KAUTH_GENERIC_ISSUSER, &l->l_acflag);
789 if (error)
790 break;
791 ch->ch_swflags = *(int *)data;
792 break;
793
794 case TIOCMSET:
795 case TIOCMBIS:
796 case TIOCMBIC:
797 tiocm_to_sbscn(ch, cmd, *(int *)data);
798 break;
799
800 case TIOCMGET:
801 *(int *)data = sbscn_to_tiocm(ch);
802 break;
803
804 default:
805 error = EPASSTHROUGH;
806 break;
807 }
808
809 splx(s);
810
811 #ifdef SBSCN_DEBUG
812 if (sbscn_debug)
813 sbscn_status(ch, "sbscn_ioctl ");
814 #endif
815
816 return (error);
817 }
818
819 integrate void
820 sbscn_schedrx(struct sbscn_channel *ch)
821 {
822
823 ch->ch_rx_ready = 1;
824
825 /* Wake up the poller. */
826 softintr_schedule(ch->ch_si);
827 }
828
829 void
830 sbscn_break(struct sbscn_channel *ch, int onoff)
831 {
832
833 /* XXXCGD delay break until not busy */
834 if (onoff)
835 WRITE_REG(ch->ch_base + 0x50, 0x60);
836 else
837 WRITE_REG(ch->ch_base + 0x50, 0x70);
838
839 #if 0
840 if (!ch->ch_heldchange) {
841 if (ch->ch_tx_busy) {
842 ch->ch_heldtbc = ch->ch_tbc;
843 ch->ch_tbc = 0;
844 ch->ch_heldchange = 1;
845 } else
846 sbscn_loadchannelregs(ch);
847 }
848 #endif
849 }
850
851 void
852 sbscn_modem(struct sbscn_channel *ch, int onoff)
853 {
854
855 if (ch->ch_o_dtr == 0)
856 return;
857
858 if (onoff)
859 SET(ch->ch_oports, ch->ch_o_dtr);
860 else
861 CLR(ch->ch_oports, ch->ch_o_dtr);
862
863 if (!ch->ch_heldchange) {
864 if (ch->ch_tx_busy) {
865 ch->ch_heldtbc = ch->ch_tbc;
866 ch->ch_tbc = 0;
867 ch->ch_heldchange = 1;
868 } else
869 sbscn_loadchannelregs(ch);
870 }
871 }
872
873 void
874 tiocm_to_sbscn(struct sbscn_channel *ch, int how, int ttybits)
875 {
876 u_char bits;
877
878 bits = 0;
879 if (ISSET(ttybits, TIOCM_DTR))
880 SET(bits, ch->ch_o_dtr);
881 if (ISSET(ttybits, TIOCM_RTS))
882 SET(bits, ch->ch_o_rts);
883
884 switch (how) {
885 case TIOCMBIC:
886 CLR(ch->ch_oports, bits);
887 break;
888
889 case TIOCMBIS:
890 SET(ch->ch_oports, bits);
891 break;
892
893 case TIOCMSET:
894 ch->ch_oports = bits;
895 break;
896 }
897
898 if (!ch->ch_heldchange) {
899 if (ch->ch_tx_busy) {
900 ch->ch_heldtbc = ch->ch_tbc;
901 ch->ch_tbc = 0;
902 ch->ch_heldchange = 1;
903 } else
904 sbscn_loadchannelregs(ch);
905 }
906 }
907
908 int
909 sbscn_to_tiocm(struct sbscn_channel *ch)
910 {
911 u_char hwbits;
912 int ttybits = 0;
913
914 hwbits = ch->ch_oports;
915 if (ISSET(hwbits, ch->ch_o_dtr))
916 SET(ttybits, TIOCM_DTR);
917 if (ISSET(hwbits, ch->ch_o_rts))
918 SET(ttybits, TIOCM_RTS);
919
920 hwbits = ch->ch_iports;
921 if (ISSET(hwbits, ch->ch_i_dcd))
922 SET(ttybits, TIOCM_CD);
923 if (ISSET(hwbits, ch->ch_i_cts))
924 SET(ttybits, TIOCM_CTS);
925 if (ISSET(hwbits, ch->ch_i_dsr))
926 SET(ttybits, TIOCM_DSR);
927 if (ISSET(hwbits, ch->ch_i_ri))
928 SET(ttybits, TIOCM_RI);
929
930 if (ch->ch_imr != 0)
931 SET(ttybits, TIOCM_LE);
932
933 return (ttybits);
934 }
935
936 static int
937 cflag2modes(cflag, mode1p, mode2p)
938 tcflag_t cflag;
939 u_char *mode1p;
940 u_char *mode2p;
941 {
942 u_char mode1;
943 u_char mode2;
944 int err = 0;
945
946 mode1 = mode2 = 0;
947
948 switch (ISSET(cflag, CSIZE)) {
949 case CS7:
950 mode1 |= 2; /* XXX */
951 break;
952 default:
953 err = -1;
954 /* FALLTHRU for sanity */
955 case CS8:
956 mode1 |= 3; /* XXX */
957 break;
958 }
959 if (!ISSET(cflag, PARENB))
960 mode1 |= 2 << 3;
961 else {
962 mode1 |= 0 << 3;
963 if (ISSET(cflag, PARODD))
964 mode1 |= 1 << 2;
965 }
966
967 if (ISSET(cflag, CSTOPB))
968 mode2 |= 1 << 3; /* two stop bits XXX not std */
969
970 if (ISSET(cflag, CRTSCTS)) {
971 mode1 |= 1 << 7;
972 mode2 |= 1 << 4;
973 }
974
975 *mode1p = mode1;
976 *mode2p = mode2;
977 return (err);
978 }
979
980 int
981 sbscn_param(struct tty *tp, struct termios *t)
982 {
983 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)];
984 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
985 long brc;
986 u_char mode1, mode2;
987 int s;
988
989 /* XXX reset to console parameters if console? */
990 #if 0
991 XXX disable, enable.
992 #endif
993
994 /* Check requested parameters. */
995 if (sbscn_speed(t->c_ospeed, &brc) < 0)
996 return (EINVAL);
997 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
998 return (EINVAL);
999
1000 /*
1001 * For the console, always force CLOCAL and !HUPCL, so that the port
1002 * is always active.
1003 */
1004 if (ISSET(ch->ch_swflags, TIOCFLAG_SOFTCAR) ||
1005 ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
1006 SET(t->c_cflag, CLOCAL);
1007 CLR(t->c_cflag, HUPCL);
1008 }
1009
1010 /*
1011 * If there were no changes, don't do anything. This avoids dropping
1012 * input and improves performance when all we did was frob things like
1013 * VMIN and VTIME.
1014 */
1015 if (tp->t_ospeed == t->c_ospeed &&
1016 tp->t_cflag == t->c_cflag)
1017 return (0);
1018
1019 if (cflag2modes(t->c_cflag, &mode1, &mode2) < 0)
1020 return (EINVAL);
1021
1022 s = splserial();
1023
1024 ch->ch_mode1 = mode1;
1025 ch->ch_mode2 = mode2;
1026
1027 /*
1028 * If we're not in a mode that assumes a connection is present, then
1029 * ignore carrier changes.
1030 */
1031 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1032 ch->ch_i_dcd = 0;
1033 else
1034 ch->ch_i_dcd = ch->ch_i_dcd_pin;
1035 /*
1036 * Set the flow control pins depending on the current flow control
1037 * mode.
1038 */
1039 if (ISSET(t->c_cflag, CRTSCTS)) {
1040 ch->ch_o_dtr = ch->ch_o_dtr_pin;
1041 ch->ch_o_rts = ch->ch_o_rts_pin;
1042 ch->ch_i_cts = ch->ch_i_cts_pin;
1043 /* hw controle enable bits in mod regs set by cflag2modes */
1044 } else if (ISSET(t->c_cflag, MDMBUF)) {
1045 /*
1046 * For DTR/DCD flow control, make sure we don't toggle DTR for
1047 * carrier detection.
1048 */
1049 ch->ch_o_dtr = 0;
1050 ch->ch_o_rts = ch->ch_o_dtr_pin;
1051 ch->ch_i_cts = ch->ch_i_dcd_pin;
1052 } else {
1053 /*
1054 * If no flow control, then always set RTS. This will make
1055 * the other side happy if it mistakenly thinks we're doing
1056 * RTS/CTS flow control.
1057 */
1058 ch->ch_o_dtr = ch->ch_o_dtr_pin | ch->ch_o_rts_pin;
1059 ch->ch_o_rts = 0;
1060 ch->ch_i_cts = 0;
1061 if (ISSET(ch->ch_oports, ch->ch_o_dtr_pin))
1062 SET(ch->ch_oports, ch->ch_o_rts_pin);
1063 else
1064 CLR(ch->ch_oports, ch->ch_o_rts_pin);
1065 }
1066 /* XXX maybe mask the ports which generate intrs? */
1067
1068 ch->ch_brc = brc;
1069
1070 /* XXX maybe set fifo-full receive mode if RTSCTS and high speed? */
1071
1072 /* And copy to tty. */
1073 tp->t_ispeed = 0;
1074 tp->t_ospeed = t->c_ospeed;
1075 tp->t_cflag = t->c_cflag;
1076
1077 if (!ch->ch_heldchange) {
1078 if (ch->ch_tx_busy) {
1079 ch->ch_heldtbc = ch->ch_tbc;
1080 ch->ch_tbc = 0;
1081 ch->ch_heldchange = 1;
1082 } else
1083 sbscn_loadchannelregs(ch);
1084 }
1085
1086 if (!ISSET(t->c_cflag, CHWFLOW)) {
1087 /* Disable the high water mark. */
1088 ch->ch_r_hiwat = 0;
1089 ch->ch_r_lowat = 0;
1090 if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
1091 CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1092 sbscn_schedrx(ch);
1093 }
1094 if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1095 CLR(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1096 sbscn_dohwiflow(ch);
1097 }
1098 } else {
1099 ch->ch_r_hiwat = sbscn_rbuf_hiwat;
1100 ch->ch_r_lowat = sbscn_rbuf_lowat;
1101 }
1102
1103 splx(s);
1104
1105 /*
1106 * Update the tty layer's idea of the carrier bit, in case we changed
1107 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1108 * explicit request.
1109 */
1110 (void) (*tp->t_linesw->l_modem)(tp,
1111 ISSET(ch->ch_iports, ch->ch_i_dcd));
1112
1113 #ifdef SBSCN_DEBUG
1114 if (sbscn_debug)
1115 sbscn_status(ch, "sbscnparam ");
1116 #endif
1117
1118 if (!ISSET(t->c_cflag, CHWFLOW)) {
1119 if (ch->ch_tx_stopped) {
1120 ch->ch_tx_stopped = 0;
1121 sbscn_start(tp);
1122 }
1123 }
1124
1125 return (0);
1126 }
1127
1128 void
1129 sbscn_iflush(struct sbscn_channel *ch)
1130 {
1131 #ifdef DIAGNOSTIC
1132 int reg;
1133 #endif
1134 int timo;
1135
1136 #ifdef DIAGNOSTIC
1137 reg = 0xffff;
1138 #endif
1139 timo = 50000;
1140 /* flush any pending I/O */
1141 while (ISSET(READ_REG(ch->ch_base + 0x20), 0x01)
1142 && --timo)
1143 #ifdef DIAGNOSTIC
1144 reg =
1145 #else
1146 (void)
1147 #endif
1148 READ_REG(ch->ch_base + 0x60);
1149 #ifdef DIAGNOSTIC
1150 if (!timo)
1151 printf("%s: sbscn_iflush timeout %02x\n",
1152 ch->ch_sc->sc_dev.dv_xname, reg & 0xff);
1153 #endif
1154 }
1155
1156 void
1157 sbscn_loadchannelregs(struct sbscn_channel *ch)
1158 {
1159
1160 /* XXXXX necessary? */
1161 sbscn_iflush(ch);
1162
1163 WRITE_REG(ch->ch_imr_base, 0);
1164
1165 // XXX disable?
1166 WRITE_REG(ch->ch_base + 0x00, ch->ch_mode1);
1167 WRITE_REG(ch->ch_base + 0x10, ch->ch_mode2);
1168 WRITE_REG(ch->ch_base + 0x30, ch->ch_brc);
1169
1170 ch->ch_oports_active = ch->ch_oports;
1171 SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active);
1172
1173 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1174 }
1175
1176 int
1177 sbscn_hwiflow(struct tty *tp, int block)
1178 {
1179 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)];
1180 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
1181 int s;
1182
1183 if (ch->ch_o_rts == 0)
1184 return (0);
1185
1186 s = splserial();
1187 if (block) {
1188 if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1189 SET(ch->ch_rx_flags, RX_TTY_BLOCKED);
1190 sbscn_dohwiflow(ch);
1191 }
1192 } else {
1193 if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
1194 CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1195 sbscn_schedrx(ch);
1196 }
1197 if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1198 CLR(ch->ch_rx_flags, RX_TTY_BLOCKED);
1199 sbscn_dohwiflow(ch);
1200 }
1201 }
1202 splx(s);
1203 return (1);
1204 }
1205
1206 /*
1207 * (un)block input via hw flowcontrol
1208 */
1209 void
1210 sbscn_dohwiflow(struct sbscn_channel *ch)
1211 {
1212
1213 if (ch->ch_o_rts == 0)
1214 return;
1215
1216 if (ISSET(ch->ch_rx_flags, RX_ANY_BLOCK)) {
1217 CLR(ch->ch_oports, ch->ch_o_rts);
1218 CLR(ch->ch_oports_active, ch->ch_o_rts);
1219 } else {
1220 SET(ch->ch_oports, ch->ch_o_rts);
1221 SET(ch->ch_oports_active, ch->ch_o_rts);
1222 }
1223 SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active);
1224 }
1225
1226 void
1227 sbscn_start(struct tty *tp)
1228 {
1229 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)];
1230 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
1231 int s;
1232
1233 s = spltty();
1234 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1235 goto out;
1236 if (ch->ch_tx_stopped)
1237 goto out;
1238
1239 if (tp->t_outq.c_cc <= tp->t_lowat) {
1240 if (ISSET(tp->t_state, TS_ASLEEP)) {
1241 CLR(tp->t_state, TS_ASLEEP);
1242 wakeup(&tp->t_outq);
1243 }
1244 selwakeup(&tp->t_wsel);
1245 if (tp->t_outq.c_cc == 0)
1246 goto out;
1247 }
1248
1249 /* Grab the first contiguous region of buffer space. */
1250 {
1251 u_char *tba;
1252 int tbc;
1253
1254 tba = tp->t_outq.c_cf;
1255 tbc = ndqb(&tp->t_outq, 0);
1256
1257 (void)splserial();
1258
1259 ch->ch_tba = tba;
1260 ch->ch_tbc = tbc;
1261 }
1262
1263 SET(tp->t_state, TS_BUSY);
1264 ch->ch_tx_busy = 1;
1265
1266 /* Enable transmit completion interrupts if necessary. */
1267 if (!ISSET(ch->ch_imr, 0x1)) {
1268 SET(ch->ch_imr, 0x1);
1269 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1270 }
1271
1272 /* Output the first chunk of the contiguous buffer. */
1273 {
1274 u_char c;
1275
1276 while (ch->ch_tbc && READ_REG(ch->ch_base + 0x20) & 0x04) {
1277 c = *ch->ch_tba++;
1278 ch->ch_tbc--;
1279 WRITE_REG(ch->ch_base + 0x70, c);
1280 }
1281 }
1282 out:
1283 splx(s);
1284 return;
1285 }
1286
1287 /*
1288 * Stop output on a line.
1289 */
1290 void
1291 sbscnstop(struct tty *tp, int flag)
1292 {
1293 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)];
1294 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
1295 int s;
1296
1297 s = splserial();
1298 if (ISSET(tp->t_state, TS_BUSY)) {
1299 /* Stop transmitting at the next chunk. */
1300 ch->ch_tbc = 0;
1301 ch->ch_heldtbc = 0;
1302 if (!ISSET(tp->t_state, TS_TTSTOP))
1303 SET(tp->t_state, TS_FLUSH);
1304 }
1305 splx(s);
1306 }
1307
1308 void
1309 sbscn_diag(arg)
1310 void *arg;
1311 {
1312 struct sbscn_channel *ch = arg;
1313 struct sbscn_softc *sc = ch->ch_sc;
1314 int overflows, floods;
1315 int s;
1316
1317 s = splserial();
1318 overflows = ch->ch_overflows;
1319 ch->ch_overflows = 0;
1320 floods = ch->ch_floods;
1321 ch->ch_floods = 0;
1322 ch->ch_errors = 0;
1323 splx(s);
1324
1325 log(LOG_WARNING, "%s: channel %d: %d fifo overflow%s, %d ibuf flood%s\n",
1326 sc->sc_dev.dv_xname, ch->ch_num,
1327 overflows, overflows == 1 ? "" : "s",
1328 floods, floods == 1 ? "" : "s");
1329 }
1330
1331 integrate void
1332 sbscn_rxsoft(struct sbscn_channel *ch, struct tty *tp)
1333 {
1334 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1335 u_char *get, *end;
1336 u_int cc, scc;
1337 u_char sr;
1338 int code;
1339 int s;
1340
1341 end = ch->ch_ebuf;
1342 get = ch->ch_rbget;
1343 scc = cc = sbscn_rbuf_size - ch->ch_rbavail;
1344
1345 if (cc == sbscn_rbuf_size) {
1346 ch->ch_floods++;
1347 if (ch->ch_errors++ == 0)
1348 callout_reset(&ch->ch_diag_callout, 60 * hz,
1349 sbscn_diag, ch);
1350 }
1351
1352 while (cc) {
1353 code = get[0];
1354 sr = get[1];
1355 if (ISSET(sr, 0xf0)) {
1356 if (ISSET(sr, 0x10)) {
1357 ch->ch_overflows++;
1358 if (ch->ch_errors++ == 0)
1359 callout_reset(&ch->ch_diag_callout,
1360 60 * hz, sbscn_diag, ch);
1361 }
1362 if (ISSET(sr, 0xc0))
1363 SET(code, TTY_FE);
1364 if (ISSET(sr, 0x20))
1365 SET(code, TTY_PE);
1366 }
1367 if ((*rint)(code, tp) == -1) {
1368 /*
1369 * The line discipline's buffer is out of space.
1370 */
1371 if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1372 /*
1373 * We're either not using flow control, or the
1374 * line discipline didn't tell us to block for
1375 * some reason. Either way, we have no way to
1376 * know when there's more space available, so
1377 * just drop the rest of the data.
1378 */
1379 get += cc << 1;
1380 if (get >= end)
1381 get -= sbscn_rbuf_size << 1;
1382 cc = 0;
1383 } else {
1384 /*
1385 * Don't schedule any more receive processing
1386 * until the line discipline tells us there's
1387 * space available (through comhwiflow()).
1388 * Leave the rest of the data in the input
1389 * buffer.
1390 */
1391 SET(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1392 }
1393 break;
1394 }
1395 get += 2;
1396 if (get >= end)
1397 get = ch->ch_rbuf;
1398 cc--;
1399 }
1400
1401 if (cc != scc) {
1402 ch->ch_rbget = get;
1403 s = splserial();
1404 cc = ch->ch_rbavail += scc - cc;
1405 /* Buffers should be ok again, release possible block. */
1406 if (cc >= ch->ch_r_lowat) {
1407 if (ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
1408 CLR(ch->ch_rx_flags, RX_IBUF_OVERFLOWED);
1409 SET(ch->ch_imr, 0x02);
1410 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1411 }
1412 if (ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED)) {
1413 CLR(ch->ch_rx_flags, RX_IBUF_BLOCKED);
1414 sbscn_dohwiflow(ch);
1415 }
1416 }
1417 splx(s);
1418 }
1419 }
1420
1421 integrate void
1422 sbscn_txsoft(struct sbscn_channel *ch, struct tty *tp)
1423 {
1424
1425 CLR(tp->t_state, TS_BUSY);
1426 if (ISSET(tp->t_state, TS_FLUSH))
1427 CLR(tp->t_state, TS_FLUSH);
1428 else
1429 ndflush(&tp->t_outq, (int)(ch->ch_tba - tp->t_outq.c_cf));
1430 (*tp->t_linesw->l_start)(tp);
1431 }
1432
1433 integrate void
1434 sbscn_stsoft(struct sbscn_channel *ch, struct tty *tp)
1435 {
1436 u_char iports, delta;
1437 int s;
1438
1439 s = splserial();
1440 iports = ch->ch_iports;
1441 delta = ch->ch_iports_delta;
1442 ch->ch_iports_delta = 0;
1443 splx(s);
1444
1445 if (ISSET(delta, ch->ch_i_dcd)) {
1446 /*
1447 * Inform the tty layer that carrier detect changed.
1448 */
1449 (void) (*tp->t_linesw->l_modem)(tp,
1450 ISSET(iports, ch->ch_i_dcd));
1451 }
1452
1453 if (ISSET(delta, ch->ch_i_cts)) {
1454 /* Block or unblock output according to flow control. */
1455 if (ISSET(iports, ch->ch_i_cts)) {
1456 ch->ch_tx_stopped = 0;
1457 (*tp->t_linesw->l_start)(tp);
1458 } else {
1459 ch->ch_tx_stopped = 1;
1460 }
1461 }
1462
1463 #ifdef SBSCN_DEBUG
1464 if (sbscn_debug)
1465 sbscn_status(ch, "sbscn_stsoft");
1466 #endif
1467 }
1468
1469 void
1470 sbscn_soft(void *arg)
1471 {
1472 struct sbscn_channel *ch = arg;
1473 struct tty *tp = ch->ch_tty;
1474
1475 if (ch->ch_rx_ready) {
1476 ch->ch_rx_ready = 0;
1477 sbscn_rxsoft(ch, tp);
1478 }
1479
1480 if (ch->ch_st_check) {
1481 ch->ch_st_check = 0;
1482 sbscn_stsoft(ch, tp);
1483 }
1484
1485 if (ch->ch_tx_done) {
1486 ch->ch_tx_done = 0;
1487 sbscn_txsoft(ch, tp);
1488 }
1489 }
1490
1491 void
1492 sbscn_intr(void *arg, uint32_t status, uint32_t pc)
1493 {
1494 struct sbscn_channel *ch = arg;
1495 u_char *put, *end;
1496 u_int cc;
1497 u_char isr, sr;
1498
1499 /* read ISR */
1500 isr = READ_REG(ch->ch_isr_base) & ch->ch_imr;
1501 if (isr == 0)
1502 return;
1503
1504 end = ch->ch_ebuf;
1505 put = ch->ch_rbput;
1506 cc = ch->ch_rbavail;
1507
1508 do {
1509 u_char iports, delta;
1510
1511 if (isr & 0x02) {
1512
1513 sr = READ_REG(ch->ch_base + 0x20);
1514 /* XXX sr 0x01 bit must be set at this point */
1515
1516 #if defined(DDB) || defined(KGDB)
1517 if ((sr & 0x80) == 0x80) {
1518 #ifdef DDB
1519 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
1520 (void)READ_REG(ch->ch_base + 0x60);
1521 console_debugger();
1522 continue;
1523 }
1524 #endif
1525 #ifdef KGDB
1526 if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB)) {
1527 (void)READ_REG(ch->ch_base + 0x60);
1528 kgdb_connect(1);
1529 continue;
1530 }
1531 #endif
1532 }
1533 #endif /* DDB || KGDB */
1534
1535 if (!ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
1536 while (cc > 0) {
1537 put[0] = READ_REG(ch->ch_base + 0x60);
1538 put[1] = sr;
1539 put += 2;
1540 if (put >= end)
1541 put = ch->ch_rbuf;
1542 cc--;
1543
1544 sr = READ_REG(ch->ch_base + 0x20);
1545 if (!ISSET(sr, 0x02))
1546 break;
1547 }
1548
1549 /*
1550 * Current string of incoming characters ended
1551 * because no more data was available or we
1552 * ran out of space. Schedule a receive event
1553 * if any data was received. If we're out of
1554 * space, turn off receive interrupts.
1555 */
1556 ch->ch_rbput = put;
1557 ch->ch_rbavail = cc;
1558 if (!ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED))
1559 ch->ch_rx_ready = 1;
1560
1561 /*
1562 * See if we are in danger of overflowing a
1563 * buffer. If so, use hardware flow control
1564 * to ease the pressure.
1565 */
1566 if (!ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED) &&
1567 cc < ch->ch_r_hiwat) {
1568 SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
1569 sbscn_dohwiflow(ch);
1570 }
1571
1572 /*
1573 * If we're out of space, disable receive
1574 * interrupts until the queue has drained
1575 * a bit.
1576 */
1577 if (!cc) {
1578 SET(ch->ch_rx_flags,
1579 RX_IBUF_OVERFLOWED);
1580 CLR(ch->ch_imr, 0x02);
1581 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1582 }
1583 } else {
1584 /* XXX panic? */
1585 CLR(ch->ch_imr, 0x02);
1586 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1587 continue;
1588 }
1589 }
1590
1591 if (isr & 0x01) {
1592 CLR(ch->ch_imr, 0x01);
1593 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1594 }
1595
1596 #if 0
1597 XXX
1598 if (isr & 0x08) {
1599 clear input signal change!
1600 }
1601 #endif
1602 iports = GET_INPUT_SIGNALS(ch);
1603 delta = iports ^ ch->ch_iports;
1604 ch->ch_iports = iports;
1605
1606 /*
1607 * Process normal status changes
1608 */
1609 if (ISSET(delta, ch->ch_i_mask)) {
1610 SET(ch->ch_iports_delta, delta);
1611
1612 /*
1613 * Stop output immediately if we lose the output
1614 * flow control signal or carrier detect.
1615 */
1616 if (ISSET(~iports, ch->ch_i_mask)) {
1617 ch->ch_tbc = 0;
1618 ch->ch_heldtbc = 0;
1619 #ifdef SBSCN_DEBUG
1620 if (sbscn_debug)
1621 sbscn_status(ch, "sbscn_intr ");
1622 #endif
1623 }
1624
1625 ch->ch_st_check = 1;
1626 }
1627 } while ((isr = (READ_REG(ch->ch_isr_base) & ch->ch_imr)) != 0);
1628
1629 /*
1630 * Done handling any receive interrupts and status changes, and
1631 * clearing the tx-ready interrupt if it was set. See if data can
1632 * be transmitted as well. Schedule tx done event if no data left
1633 * and tty was marked busy.
1634 */
1635 sr = READ_REG(ch->ch_base + 0x20);
1636 if (ISSET(sr, 0x4)) {
1637
1638 /*
1639 * If we've delayed a parameter change, do it now, and restart
1640 * output.
1641 */
1642 if (ch->ch_heldchange) {
1643 sbscn_loadchannelregs(ch);
1644 ch->ch_heldchange = 0;
1645 ch->ch_tbc = ch->ch_heldtbc;
1646 ch->ch_heldtbc = 0;
1647 }
1648
1649 /* Output the next chunk of the contiguous buffer, if any. */
1650 if (ch->ch_tbc > 0) {
1651 int wrote1;
1652 u_char c;
1653
1654 wrote1 = 0;
1655 while (ch->ch_tbc &&
1656 READ_REG(ch->ch_base + 0x20) & 0x04) {
1657 wrote1 = 1;
1658 c = *ch->ch_tba++;
1659 ch->ch_tbc--;
1660 WRITE_REG(ch->ch_base + 0x70, c);
1661 }
1662 if (wrote1) {
1663 SET(ch->ch_imr, 0x01);
1664 WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1665 }
1666 } else {
1667 /*
1668 * transmit completion interrupts already disabled,
1669 * mark the channel tx state as done.
1670 */
1671 if (ch->ch_tx_busy) {
1672 ch->ch_tx_busy = 0;
1673 ch->ch_tx_done = 1;
1674 }
1675 }
1676 }
1677
1678 /* Wake up the poller. */
1679 softintr_schedule(ch->ch_si);
1680
1681 #if NRND > 0 && defined(RND_SBSCN)
1682 rnd_add_uint32(&ch->ch_rnd_source, isr | sr);
1683 #endif
1684 }
1685
1686 /*
1687 * The following functions are polled getc and putc routines, shared
1688 * by the console and kgdb glue.
1689 */
1690
1691 int
1692 sbscn_common_getc(u_long addr, int chan)
1693 {
1694 int s = splhigh();
1695 u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
1696 int c;
1697
1698 /* block until a character becomes available */
1699 while ((READ_REG(base + 0x20) & 0x01) == 0)
1700 continue;
1701
1702 c = READ_REG(base + 0x60) & 0xff;
1703 splx(s);
1704 return (c);
1705 }
1706
1707 void
1708 sbscn_common_putc(u_long addr, int chan, int c)
1709 {
1710 int s = splhigh();
1711 int timo;
1712 u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
1713
1714 /* wait for any pending transmission to finish */
1715 timo = 1500000;
1716 while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo)
1717 continue;
1718
1719 WRITE_REG(base + 0x70, c);
1720
1721 /* wait for this transmission to complete */
1722 timo = 15000000;
1723 while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo)
1724 continue;
1725
1726 splx(s);
1727 }
1728
1729 /*
1730 * Initialize UART for use as console or KGDB line.
1731 */
1732 int
1733 sbscn_init(u_long addr, int chan, int rate, tcflag_t cflag)
1734 {
1735 #if 1
1736 u_long chanregbase = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
1737 u_long imaskreg = MIPS_PHYS_TO_KSEG1(addr + 0x230 + (chan * 0x20));
1738 u_char mode1, mode2;
1739 u_long brc;
1740 volatile int timo;
1741
1742 WRITE_REG(imaskreg, 0); /* disable channel intrs */
1743
1744 /* XXX should we really do the following? how about only if enabled? */
1745 /* wait for any pending transmission to finish */
1746 timo = 1500000;
1747 while ((READ_REG(chanregbase + 0x20) & 0x08) == 0 && --timo)
1748 continue;
1749
1750 /* XXX: wait a little. THIS SHOULD NOT BE NECESSARY!!! (?) */
1751 timo = 1500000;
1752 while (--timo)
1753 ;
1754
1755 WRITE_REG(chanregbase + 0x50, 2 << 4); /* reset receiver */
1756 WRITE_REG(chanregbase + 0x50, 3 << 4); /* reset transmitter */
1757
1758 /* set up the line for use */
1759 (void)cflag2modes(cflag, &mode1, &mode2);
1760 (void)sbscn_speed(rate, &brc);
1761 WRITE_REG(chanregbase + 0x00, mode1);
1762 WRITE_REG(chanregbase + 0x10, mode2);
1763 WRITE_REG(chanregbase + 0x30, brc);
1764
1765 /* enable transmit and receive */
1766 #define M_DUART_RX_EN 0x01
1767 #define M_DUART_TX_EN 0x04
1768 WRITE_REG(chanregbase + 0x50,M_DUART_RX_EN | M_DUART_TX_EN);
1769 #endif
1770
1771 /* XXX: wait a little. THIS SHOULD NOT BE NECESSARY!!! (?) */
1772 timo = 1500000;
1773 while (--timo)
1774 ;
1775
1776 #if 0 /* XXXCGD */
1777 bus_space_handle_t ioh;
1778
1779 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
1780 return (ENOMEM); /* ??? */
1781
1782 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
1783 bus_space_write_1(iot, ioh, com_efr, 0);
1784 bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
1785 rate = comspeed(rate, frequency);
1786 bus_space_write_1(iot, ioh, com_dlbl, rate);
1787 bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
1788 bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
1789 bus_space_write_1(iot, ioh, com_mcr, MCR_DTR | MCR_RTS);
1790 bus_space_write_1(iot, ioh, com_fifo,
1791 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
1792 bus_space_write_1(iot, ioh, com_ier, 0);
1793
1794 *iohp = ioh;
1795 return (0);
1796 #endif /* XXXCGD */
1797 /* XXXCGD */
1798 return (0);
1799 }
1800
1801 /*
1802 * Following are all routines needed for sbscn to act as console
1803 */
1804 int
1805 sbscn_cnattach(u_long addr, int chan, int rate, tcflag_t cflag)
1806 {
1807 int res;
1808 static struct consdev sbscn_cons = {
1809 NULL, NULL, sbscn_cngetc, sbscn_cnputc, sbscn_cnpollc, NULL,
1810 NULL, NULL, NODEV, CN_NORMAL
1811 };
1812
1813 res = sbscn_init(addr, chan, rate, cflag);
1814 if (res)
1815 return (res);
1816
1817 cn_tab = &sbscn_cons;
1818
1819 sbscn_cons_present = 1;
1820 sbscn_cons_addr = addr;
1821 sbscn_cons_chan = chan;
1822 sbscn_cons_rate = rate;
1823 sbscn_cons_cflag = cflag;
1824
1825 return (0);
1826 }
1827
1828 int
1829 sbscn_cngetc(dev_t dev)
1830 {
1831
1832 return (sbscn_common_getc(sbscn_cons_addr, sbscn_cons_chan));
1833 }
1834
1835 /*
1836 * Console kernel output character routine.
1837 */
1838 void
1839 sbscn_cnputc(dev_t dev, int c)
1840 {
1841
1842 sbscn_common_putc(sbscn_cons_addr, sbscn_cons_chan, c);
1843 }
1844
1845 void
1846 sbscn_cnpollc(dev_t dev, int on)
1847 {
1848
1849 }
1850
1851 #ifdef KGDB
1852 int
1853 sbscn_kgdb_attach(u_long addr, int chan, int rate, tcflag_t cflag)
1854 {
1855 int res;
1856
1857 if (!sbscn_cons_present &&
1858 sbscn_cons_addr == addr && sbscn_cons_chan == chan)
1859 return (EBUSY); /* cannot share with console */
1860
1861 res = sbscn_init(addr, chan, rate, cflag);
1862 if (res)
1863 return (res);
1864
1865 kgdb_attach(sbscn_kgdb_getc, sbscn_kgdb_putc, NULL);
1866 kgdb_dev = 123; /* unneeded, only to satisfy some tests */
1867
1868 sbscn_kgdb_present = 1;
1869 sbscn_kgdb_addr = addr;
1870 sbscn_kgdb_chan = chan;
1871
1872 return (0);
1873 }
1874
1875 /* ARGSUSED */
1876 int
1877 sbscn_kgdb_getc(arg)
1878 void *arg;
1879 {
1880
1881 return (sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan));
1882 }
1883
1884 /* ARGSUSED */
1885 void
1886 sbscn_kgdb_putc(arg, c)
1887 void *arg;
1888 int c;
1889 {
1890
1891 sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan, c);
1892 }
1893 #endif /* KGDB */
1894
1895 /*
1896 * helper function to identify the sbscn channels used by
1897 * console or KGDB (and not yet autoconf attached)
1898 */
1899 int
1900 sbscn_is_console(u_long addr, int chan)
1901 {
1902
1903 if (sbscn_cons_present && !sbscn_cons_attached &&
1904 sbscn_cons_addr == addr && sbscn_cons_chan == chan)
1905 return (1);
1906 #ifdef KGDB
1907 if (sbscn_kgdb_present && !sbscn_kgdb_attached &&
1908 sbscn_kgdb_addr == addr && sbscn_kgdb_chan == chan)
1909 return (1);
1910 #endif
1911 return (0);
1912 }
1913