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