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