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