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