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