zs.c revision 1.9 1 /* $NetBSD: zs.c,v 1.9 1995/09/23 20:29:17 leo Exp $ */
2
3 /*
4 * Copyright (c) 1995 L. Weppelman (Atari modifications)
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This software was developed by the Computer Systems Engineering group
9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 * contributed to Berkeley.
11 *
12 *
13 * All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Lawrence Berkeley Laboratory.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 *
46 * @(#)zs.c 8.1 (Berkeley) 7/19/93
47 */
48
49 /*
50 * Zilog Z8530 (ZSCC) driver.
51 *
52 * Runs two tty ports (modem2 and serial2) on zs0.
53 *
54 * This driver knows far too much about chip to usage mappings.
55 */
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/proc.h>
59 #include <sys/device.h>
60 #include <sys/conf.h>
61 #include <sys/file.h>
62 #include <sys/ioctl.h>
63 #include <sys/tty.h>
64 #include <sys/time.h>
65 #include <sys/kernel.h>
66 #include <sys/syslog.h>
67
68 #include <machine/cpu.h>
69 #include <machine/iomap.h>
70 #include <machine/scu.h>
71 #include <machine/mfp.h>
72
73 #include <dev/ic/z8530reg.h>
74 #include <atari/dev/zsvar.h>
75 #include "zs.h"
76 #if NZS > 1
77 #error "This driver supports only 1 85C30!"
78 #endif
79
80 #if NZS > 0
81
82 #define PCLK (8000000) /* PCLK pin input clock rate */
83
84 #define splzs spl5
85
86 /*
87 * Software state per found chip.
88 */
89 struct zs_softc {
90 struct device zi_dev; /* base device */
91 volatile struct zsdevice *zi_zs; /* chip registers */
92 struct zs_chanstate zi_cs[2]; /* chan A and B software state */
93 };
94
95 static u_char cb_scheduled = 0; /* Already asked for callback? */
96 /*
97 * Define the registers for a closed port
98 */
99 static u_char zs_init_regs[16] = {
100 /* 0 */ 0,
101 /* 1 */ 0,
102 /* 2 */ 0x60,
103 /* 3 */ 0,
104 /* 4 */ 0,
105 /* 5 */ 0,
106 /* 6 */ 0,
107 /* 7 */ 0,
108 /* 8 */ 0,
109 /* 9 */ ZSWR9_VECTOR_INCL_STAT,
110 /* 10 */ ZSWR10_NRZ,
111 /* 11 */ ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
112 /* 12 */ 0,
113 /* 13 */ 0,
114 /* 14 */ ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
115 /* 15 */ 0
116 };
117
118 /*
119 * Define the machine dependant clock frequencies
120 * If BRgen feeds sender/receiver we always use a
121 * divisor 16, therefor the division by 16 can as
122 * well be done here.
123 */
124 static u_long zs_freqs_tt[] = {
125 /*
126 * Atari TT, RTxCB is generated by TT-MFP timer C,
127 * which is set to 307.2KHz during initialisation
128 * and never changed afterwards.
129 */
130 PCLK/16, /* BRgen, PCLK, divisor 16 */
131 229500, /* BRgen, RTxCA, divisor 16 */
132 3672000, /* RTxCA, from PCLK4 */
133 0, /* TRxCA, external */
134
135 PCLK/16, /* BRgen, PCLK, divisor 16 */
136 19200, /* BRgen, RTxCB, divisor 16 */
137 307200, /* RTxCB, from TT-MFP TCO */
138 2457600 /* TRxCB, from BCLK */
139 };
140 static u_long zs_freqs_falcon[] = {
141 /*
142 * Atari Falcon, XXX no specs available, this might be wrong
143 */
144 PCLK/16, /* BRgen, PCLK, divisor 16 */
145 229500, /* BRgen, RTxCA, divisor 16 */
146 3672000, /* RTxCA, ??? */
147 0, /* TRxCA, external */
148
149 PCLK/16, /* BRgen, PCLK, divisor 16 */
150 229500, /* BRgen, RTxCB, divisor 16 */
151 3672000, /* RTxCB, ??? */
152 2457600 /* TRxCB, ??? */
153 };
154 static u_long zs_freqs_generic[] = {
155 /*
156 * other machines, assume only PCLK is available
157 */
158 PCLK/16, /* BRgen, PCLK, divisor 16 */
159 0, /* BRgen, RTxCA, divisor 16 */
160 0, /* RTxCA, unknown */
161 0, /* TRxCA, unknown */
162
163 PCLK/16, /* BRgen, PCLK, divisor 16 */
164 0, /* BRgen, RTxCB, divisor 16 */
165 0, /* RTxCB, unknown */
166 0 /* TRxCB, unknown */
167 };
168 static u_long *zs_frequencies;
169
170 /* Definition of the driver for autoconfig. */
171 static int zsmatch __P((struct device *, struct cfdata *, void *));
172 static void zsattach __P((struct device *, struct device *, void *));
173 struct cfdriver zscd = {
174 NULL, "zs", (cfmatch_t)zsmatch, zsattach, DV_TTY,
175 sizeof(struct zs_softc), NULL, 0 };
176
177 /* Interrupt handlers. */
178 int zshard __P((long));
179 static int zssoft __P((long));
180 static int zsrint __P((struct zs_chanstate *, volatile struct zschan *));
181 static int zsxint __P((struct zs_chanstate *, volatile struct zschan *));
182 static int zssint __P((struct zs_chanstate *, volatile struct zschan *));
183
184 static struct zs_chanstate *zslist;
185
186 /* Routines called from other code. */
187 static void zsstart __P((struct tty *));
188 void zsstop __P((struct tty *, int));
189 static int zsparam __P((struct tty *, struct termios *));
190 static int zsbaudrate __P((int, int, int *, int *, int *, int *));
191
192 /* Routines purely local to this driver. */
193 static void zs_reset __P((volatile struct zschan *, int, int));
194 static int zs_modem __P((struct zs_chanstate *, int, int));
195 static void zs_loadchannelregs __P((volatile struct zschan *, u_char *));
196
197 static int zsshortcuts; /* number of "shortcut" software interrupts */
198
199 static int
200 zsmatch(pdp, cfp, auxp)
201 struct device *pdp;
202 struct cfdata *cfp;
203 void *auxp;
204 {
205 if(strcmp("zs", auxp) || cfp->cf_unit != 0)
206 return(0);
207 return(1);
208 }
209
210 /*
211 * Attach a found zs.
212 */
213 static void
214 zsattach(parent, dev, aux)
215 struct device *parent;
216 struct device *dev;
217 void *aux;
218 {
219 register struct zs_softc *zi;
220 register struct zs_chanstate *cs;
221 register volatile struct zsdevice *addr;
222 register struct tty *tp;
223 char tmp;
224
225 addr = (struct zsdevice *)AD_SCC;
226 zi = (struct zs_softc *)dev;
227 zi->zi_zs = addr;
228 cs = zi->zi_cs;
229
230 /*
231 * Get the command register into a known state.
232 */
233 tmp = addr->zs_chan[ZS_CHAN_A].zc_csr;
234 tmp = addr->zs_chan[ZS_CHAN_A].zc_csr;
235 tmp = addr->zs_chan[ZS_CHAN_B].zc_csr;
236 tmp = addr->zs_chan[ZS_CHAN_B].zc_csr;
237
238 /*
239 * Do a hardware reset.
240 */
241 ZS_WRITE(&addr->zs_chan[ZS_CHAN_A], 9, ZSWR9_HARD_RESET);
242 delay(50000); /*enough ? */
243 ZS_WRITE(&addr->zs_chan[ZS_CHAN_A], 9, 0);
244
245 /*
246 * Initialize both channels
247 */
248 zs_loadchannelregs(&addr->zs_chan[ZS_CHAN_A], zs_init_regs);
249 zs_loadchannelregs(&addr->zs_chan[ZS_CHAN_B], zs_init_regs);
250
251 if(machineid & ATARI_TT) {
252 /*
253 * ininitialise TT-MFP timer C: 307200Hz
254 * timer C and D share one control register:
255 * bits 0-2 control timer D
256 * bits 4-6 control timer C
257 */
258 int cr = MFP2->mf_tcdcr & 7;
259 MFP2->mf_tcdcr = cr; /* stop timer C */
260 MFP2->mf_tcdr = 1; /* counter 1 */
261 cr |= T_Q004 << 4; /* divisor 4 */
262 MFP2->mf_tcdcr = cr; /* start timer C */
263 /*
264 * enable scc related interrupts
265 */
266 SCU->sys_mask |= SCU_SCC;
267
268 zs_frequencies = zs_freqs_tt;
269 } else if (machineid & ATARI_FALCON) {
270 zs_frequencies = zs_freqs_falcon;
271 } else {
272 zs_frequencies = zs_freqs_generic;
273 }
274
275 /* link into interrupt list with order (A,B) (B=A+1) */
276 cs[0].cs_next = &cs[1];
277 cs[1].cs_next = zslist;
278 zslist = cs;
279
280 cs->cs_unit = 0;
281 cs->cs_zc = &addr->zs_chan[ZS_CHAN_A];
282 cs++;
283 cs->cs_unit = 1;
284 cs->cs_zc = &addr->zs_chan[ZS_CHAN_B];
285
286 printf(": serial2 on channel a and modem2 on channel b\n");
287 }
288
289 /*
290 * Open a zs serial port.
291 */
292 int
293 zsopen(dev, flags, mode, p)
294 dev_t dev;
295 int flags;
296 int mode;
297 struct proc *p;
298 {
299 register struct tty *tp;
300 register struct zs_chanstate *cs;
301 struct zs_softc *zi;
302 int unit = ZS_UNIT(dev);
303 int zs = unit >> 1;
304 int error, s;
305
306 if(zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL)
307 return (ENXIO);
308 cs = &zi->zi_cs[unit & 1];
309 tp = cs->cs_ttyp;
310 if(tp == NULL) {
311 cs->cs_ttyp = tp = ttymalloc();
312 tp->t_dev = dev;
313 tp->t_oproc = zsstart;
314 tp->t_param = zsparam;
315 }
316
317 s = spltty();
318 if((tp->t_state & TS_ISOPEN) == 0) {
319 ttychars(tp);
320 if(tp->t_ispeed == 0) {
321 tp->t_iflag = TTYDEF_IFLAG;
322 tp->t_oflag = TTYDEF_OFLAG;
323 tp->t_cflag = TTYDEF_CFLAG;
324 tp->t_lflag = TTYDEF_LFLAG;
325 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
326 }
327 (void)zsparam(tp, &tp->t_termios);
328 ttsetwater(tp);
329 }
330 else if(tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
331 splx(s);
332 return (EBUSY);
333 }
334 error = 0;
335 for(;;) {
336 /* loop, turning on the device, until carrier present */
337 zs_modem(cs, ZSWR5_RTS|ZSWR5_DTR, DMSET);
338
339 /* May never get a status intr. if DCD already on. -gwr */
340 if(cs->cs_zc->zc_csr & ZSRR0_DCD)
341 tp->t_state |= TS_CARR_ON;
342 if(cs->cs_softcar)
343 tp->t_state |= TS_CARR_ON;
344 if(flags & O_NONBLOCK || tp->t_cflag & CLOCAL ||
345 tp->t_state & TS_CARR_ON)
346 break;
347 tp->t_state |= TS_WOPEN;
348 if(error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
349 ttopen, 0)) {
350 if(!(tp->t_state & TS_ISOPEN)) {
351 zs_modem(cs, 0, DMSET);
352 tp->t_state &= ~TS_WOPEN;
353 ttwakeup(tp);
354 }
355 splx(s);
356 return error;
357 }
358 }
359 splx(s);
360 if(error == 0)
361 error = linesw[tp->t_line].l_open(dev, tp);
362 if(error)
363 zs_modem(cs, 0, DMSET);
364 return(error);
365 }
366
367 /*
368 * Close a zs serial port.
369 */
370 int
371 zsclose(dev, flags, mode, p)
372 dev_t dev;
373 int flags;
374 int mode;
375 struct proc *p;
376 {
377 register struct zs_chanstate *cs;
378 register struct tty *tp;
379 struct zs_softc *zi;
380 int unit = ZS_UNIT(dev);
381 int s;
382
383 zi = zscd.cd_devs[unit >> 1];
384 cs = &zi->zi_cs[unit & 1];
385 tp = cs->cs_ttyp;
386 linesw[tp->t_line].l_close(tp, flags);
387 if(tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
388 (tp->t_state & TS_ISOPEN) == 0) {
389 zs_modem(cs, 0, DMSET);
390 /* hold low for 1 second */
391 (void)tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
392 }
393 if(cs->cs_creg[5] & ZSWR5_BREAK) {
394 s = splzs();
395 cs->cs_preg[5] &= ~ZSWR5_BREAK;
396 cs->cs_creg[5] &= ~ZSWR5_BREAK;
397 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
398 splx(s);
399 }
400 ttyclose(tp);
401
402 /*
403 * Drop all lines and cancel interrupts
404 */
405 zs_loadchannelregs(&zi->zi_zs->zs_chan[unit & 1], zs_init_regs);
406 return (0);
407 }
408
409 /*
410 * Read/write zs serial port.
411 */
412 int
413 zsread(dev, uio, flags)
414 dev_t dev;
415 struct uio *uio;
416 int flags;
417 {
418 register struct zs_chanstate *cs;
419 register struct zs_softc *zi;
420 register struct tty *tp;
421 int unit;
422
423 unit = ZS_UNIT(dev);
424 zi = zscd.cd_devs[unit >> 1];
425 cs = &zi->zi_cs[unit & 1];
426 tp = cs->cs_ttyp;
427
428 return(linesw[tp->t_line].l_read(tp, uio, flags));
429 }
430
431 int
432 zswrite(dev, uio, flags)
433 dev_t dev;
434 struct uio *uio;
435 int flags;
436 {
437 register struct zs_chanstate *cs;
438 register struct zs_softc *zi;
439 register struct tty *tp;
440 int unit;
441
442 unit = ZS_UNIT(dev);
443 zi = zscd.cd_devs[unit >> 1];
444 cs = &zi->zi_cs[unit & 1];
445 tp = cs->cs_ttyp;
446
447 return(linesw[tp->t_line].l_write(tp, uio, flags));
448 }
449
450 struct tty *
451 zstty(dev)
452 dev_t dev;
453 {
454 register struct zs_chanstate *cs;
455 register struct zs_softc *zi;
456 int unit;
457
458 unit = ZS_UNIT(dev);
459 zi = zscd.cd_devs[unit >> 1];
460 cs = &zi->zi_cs[unit & 1];
461 return(cs->cs_ttyp);
462 }
463
464 /*
465 * ZS hardware interrupt. Scan all ZS channels. NB: we know here that
466 * channels are kept in (A,B) pairs.
467 *
468 * Do just a little, then get out; set a software interrupt if more
469 * work is needed.
470 *
471 * We deliberately ignore the vectoring Zilog gives us, and match up
472 * only the number of `reset interrupt under service' operations, not
473 * the order.
474 */
475
476 int
477 zshard(sr)
478 long sr;
479 {
480 register struct zs_chanstate *a;
481 #define b (a + 1)
482 register volatile struct zschan *zc;
483 register int rr3, intflags = 0, v, i;
484
485 do {
486 intflags &= ~4;
487 for(a = zslist; a != NULL; a = b->cs_next) {
488 rr3 = ZS_READ(a->cs_zc, 3);
489 if(rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
490 intflags |= 4|2;
491 zc = a->cs_zc;
492 i = a->cs_rbput;
493 if(rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
494 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
495 intflags |= 1;
496 }
497 if(rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
498 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
499 intflags |= 1;
500 }
501 if(rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
502 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
503 intflags |= 1;
504 }
505 a->cs_rbput = i;
506 }
507 if(rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
508 intflags |= 4|2;
509 zc = b->cs_zc;
510 i = b->cs_rbput;
511 if(rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
512 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
513 intflags |= 1;
514 }
515 if(rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
516 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
517 intflags |= 1;
518 }
519 if(rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
520 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
521 intflags |= 1;
522 }
523 b->cs_rbput = i;
524 }
525 }
526 } while(intflags & 4);
527 #undef b
528
529 if(intflags & 1) {
530 if(BASEPRI(sr)) {
531 spl1();
532 zsshortcuts++;
533 return(zssoft(sr));
534 }
535 else if(!cb_scheduled) {
536 cb_scheduled++;
537 add_sicallback(zssoft, 0, 0);
538 }
539 }
540 return(intflags & 2);
541 }
542
543 static int
544 zsrint(cs, zc)
545 register struct zs_chanstate *cs;
546 register volatile struct zschan *zc;
547 {
548 register int c;
549
550 /*
551 * First read the status, because read of the received char
552 * destroy the status of this char.
553 */
554 c = ZS_READ(zc, 1);
555 c |= (zc->zc_data << 8);
556
557 /* clear receive error & interrupt condition */
558 zc->zc_csr = ZSWR0_RESET_ERRORS;
559 zc->zc_csr = ZSWR0_CLR_INTR;
560
561 return(ZRING_MAKE(ZRING_RINT, c));
562 }
563
564 static int
565 zsxint(cs, zc)
566 register struct zs_chanstate *cs;
567 register volatile struct zschan *zc;
568 {
569 register int i = cs->cs_tbc;
570
571 if(i == 0) {
572 zc->zc_csr = ZSWR0_RESET_TXINT;
573 zc->zc_csr = ZSWR0_CLR_INTR;
574 return(ZRING_MAKE(ZRING_XINT, 0));
575 }
576 cs->cs_tbc = i - 1;
577 zc->zc_data = *cs->cs_tba++;
578 zc->zc_csr = ZSWR0_CLR_INTR;
579 return (0);
580 }
581
582 static int
583 zssint(cs, zc)
584 register struct zs_chanstate *cs;
585 register volatile struct zschan *zc;
586 {
587 register int rr0;
588
589 rr0 = zc->zc_csr;
590 zc->zc_csr = ZSWR0_RESET_STATUS;
591 zc->zc_csr = ZSWR0_CLR_INTR;
592 /*
593 * The chip's hardware flow control is, as noted in zsreg.h,
594 * busted---if the DCD line goes low the chip shuts off the
595 * receiver (!). If we want hardware CTS flow control but do
596 * not have it, and carrier is now on, turn HFC on; if we have
597 * HFC now but carrier has gone low, turn it off.
598 */
599 if(rr0 & ZSRR0_DCD) {
600 if(cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
601 (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
602 cs->cs_creg[3] |= ZSWR3_HFC;
603 ZS_WRITE(zc, 3, cs->cs_creg[3]);
604 }
605 }
606 else {
607 if (cs->cs_creg[3] & ZSWR3_HFC) {
608 cs->cs_creg[3] &= ~ZSWR3_HFC;
609 ZS_WRITE(zc, 3, cs->cs_creg[3]);
610 }
611 }
612 return(ZRING_MAKE(ZRING_SINT, rr0));
613 }
614
615 /*
616 * Print out a ring or fifo overrun error message.
617 */
618 static void
619 zsoverrun(unit, ptime, what)
620 int unit;
621 long *ptime;
622 char *what;
623 {
624
625 if(*ptime != time.tv_sec) {
626 *ptime = time.tv_sec;
627 log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
628 (unit & 1) + 'a', what);
629 }
630 }
631
632 /*
633 * ZS software interrupt. Scan all channels for deferred interrupts.
634 */
635 int
636 zssoft(sr)
637 long sr;
638 {
639 register struct zs_chanstate *cs;
640 register volatile struct zschan *zc;
641 register struct linesw *line;
642 register struct tty *tp;
643 register int get, n, c, cc, unit, s;
644 int retval = 0;
645
646 cb_scheduled = 0;
647 s = spltty();
648 for(cs = zslist; cs != NULL; cs = cs->cs_next) {
649 get = cs->cs_rbget;
650 again:
651 n = cs->cs_rbput; /* atomic */
652 if(get == n) /* nothing more on this line */
653 continue;
654 retval = 1;
655 unit = cs->cs_unit; /* set up to handle interrupts */
656 zc = cs->cs_zc;
657 tp = cs->cs_ttyp;
658 line = &linesw[tp->t_line];
659 /*
660 * Compute the number of interrupts in the receive ring.
661 * If the count is overlarge, we lost some events, and
662 * must advance to the first valid one. It may get
663 * overwritten if more data are arriving, but this is
664 * too expensive to check and gains nothing (we already
665 * lost out; all we can do at this point is trade one
666 * kind of loss for another).
667 */
668 n -= get;
669 if(n > ZLRB_RING_SIZE) {
670 zsoverrun(unit, &cs->cs_rotime, "ring");
671 get += n - ZLRB_RING_SIZE;
672 n = ZLRB_RING_SIZE;
673 }
674 while(--n >= 0) {
675 /* race to keep ahead of incoming interrupts */
676 c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
677 switch (ZRING_TYPE(c)) {
678
679 case ZRING_RINT:
680 c = ZRING_VALUE(c);
681 if(c & ZSRR1_DO)
682 zsoverrun(unit, &cs->cs_fotime, "fifo");
683 cc = c >> 8;
684 if(c & ZSRR1_FE)
685 cc |= TTY_FE;
686 if(c & ZSRR1_PE)
687 cc |= TTY_PE;
688 line->l_rint(cc, tp);
689 break;
690
691 case ZRING_XINT:
692 /*
693 * Transmit done: change registers and resume,
694 * or clear BUSY.
695 */
696 if(cs->cs_heldchange) {
697 int sps;
698
699 sps = splzs();
700 c = zc->zc_csr;
701 if((c & ZSRR0_DCD) == 0)
702 cs->cs_preg[3] &= ~ZSWR3_HFC;
703 bcopy((caddr_t)cs->cs_preg,
704 (caddr_t)cs->cs_creg, 16);
705 zs_loadchannelregs(zc, cs->cs_creg);
706 splx(sps);
707 cs->cs_heldchange = 0;
708 if(cs->cs_heldtbc
709 && (tp->t_state & TS_TTSTOP) == 0) {
710 cs->cs_tbc = cs->cs_heldtbc - 1;
711 zc->zc_data = *cs->cs_tba++;
712 goto again;
713 }
714 }
715 tp->t_state &= ~TS_BUSY;
716 if(tp->t_state & TS_FLUSH)
717 tp->t_state &= ~TS_FLUSH;
718 else ndflush(&tp->t_outq,cs->cs_tba
719 - (caddr_t)tp->t_outq.c_cf);
720 line->l_start(tp);
721 break;
722
723 case ZRING_SINT:
724 /*
725 * Status line change. HFC bit is run in
726 * hardware interrupt, to avoid locking
727 * at splzs here.
728 */
729 c = ZRING_VALUE(c);
730 if((c ^ cs->cs_rr0) & ZSRR0_DCD) {
731 cc = (c & ZSRR0_DCD) != 0;
732 if(line->l_modem(tp, cc) == 0)
733 zs_modem(cs, ZSWR5_RTS|ZSWR5_DTR,
734 cc ? DMBIS : DMBIC);
735 }
736 cs->cs_rr0 = c;
737 break;
738
739 default:
740 log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
741 unit >> 1, (unit & 1) + 'a', c);
742 break;
743 }
744 }
745 cs->cs_rbget = get;
746 goto again;
747 }
748 splx(s);
749 return (retval);
750 }
751
752 int
753 zsioctl(dev, cmd, data, flag, p)
754 dev_t dev;
755 u_long cmd;
756 caddr_t data;
757 int flag;
758 struct proc *p;
759 {
760 int unit = ZS_UNIT(dev);
761 struct zs_softc *zi = zscd.cd_devs[unit >> 1];
762 register struct tty *tp = zi->zi_cs[unit & 1].cs_ttyp;
763 register int error, s;
764 register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
765
766 error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p);
767 if(error >= 0)
768 return(error);
769 error = ttioctl(tp, cmd, data, flag, p);
770 if(error >= 0)
771 return (error);
772
773 switch (cmd) {
774 case TIOCSBRK:
775 s = splzs();
776 cs->cs_preg[5] |= ZSWR5_BREAK;
777 cs->cs_creg[5] |= ZSWR5_BREAK;
778 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
779 splx(s);
780 break;
781 case TIOCCBRK:
782 s = splzs();
783 cs->cs_preg[5] &= ~ZSWR5_BREAK;
784 cs->cs_creg[5] &= ~ZSWR5_BREAK;
785 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
786 splx(s);
787 break;
788 case TIOCGFLAGS: {
789 int bits = 0;
790
791 if(cs->cs_softcar)
792 bits |= TIOCFLAG_SOFTCAR;
793 if(cs->cs_creg[15] & ZSWR15_DCD_IE)
794 bits |= TIOCFLAG_CLOCAL;
795 if(cs->cs_creg[3] & ZSWR3_HFC)
796 bits |= TIOCFLAG_CRTSCTS;
797 *(int *)data = bits;
798 break;
799 }
800 case TIOCSFLAGS: {
801 int userbits, driverbits = 0;
802
803 error = suser(p->p_ucred, &p->p_acflag);
804 if(error != 0)
805 return (EPERM);
806
807 userbits = *(int *)data;
808
809 /*
810 * can have `local' or `softcar', and `rtscts' or `mdmbuf'
811 # defaulting to software flow control.
812 */
813 if(userbits & TIOCFLAG_SOFTCAR && userbits & TIOCFLAG_CLOCAL)
814 return(EINVAL);
815 if(userbits & TIOCFLAG_MDMBUF) /* don't support this (yet?) */
816 return(ENXIO);
817
818 s = splzs();
819 if((userbits & TIOCFLAG_SOFTCAR)) {
820 cs->cs_softcar = 1; /* turn on softcar */
821 cs->cs_preg[15] &= ~ZSWR15_DCD_IE; /* turn off dcd */
822 cs->cs_creg[15] &= ~ZSWR15_DCD_IE;
823 ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
824 }
825 else if(userbits & TIOCFLAG_CLOCAL) {
826 cs->cs_softcar = 0; /* turn off softcar */
827 cs->cs_preg[15] |= ZSWR15_DCD_IE; /* turn on dcd */
828 cs->cs_creg[15] |= ZSWR15_DCD_IE;
829 ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
830 tp->t_termios.c_cflag |= CLOCAL;
831 }
832 if(userbits & TIOCFLAG_CRTSCTS) {
833 cs->cs_preg[15] |= ZSWR15_CTS_IE;
834 cs->cs_creg[15] |= ZSWR15_CTS_IE;
835 ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
836 cs->cs_preg[3] |= ZSWR3_HFC;
837 cs->cs_creg[3] |= ZSWR3_HFC;
838 ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
839 tp->t_termios.c_cflag |= CRTSCTS;
840 }
841 else {
842 /* no mdmbuf, so we must want software flow control */
843 cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
844 cs->cs_creg[15] &= ~ZSWR15_CTS_IE;
845 ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
846 cs->cs_preg[3] &= ~ZSWR3_HFC;
847 cs->cs_creg[3] &= ~ZSWR3_HFC;
848 ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
849 tp->t_termios.c_cflag &= ~CRTSCTS;
850 }
851 splx(s);
852 break;
853 }
854 case TIOCSDTR:
855 zs_modem(cs, ZSWR5_DTR, DMBIS);
856 break;
857 case TIOCCDTR:
858 zs_modem(cs, ZSWR5_DTR, DMBIC);
859 break;
860 case TIOCMGET:
861 zs_modem(cs, 0, DMGET);
862 break;
863 case TIOCMSET:
864 case TIOCMBIS:
865 case TIOCMBIC:
866 default:
867 return (ENOTTY);
868 }
869 return (0);
870 }
871
872 /*
873 * Start or restart transmission.
874 */
875 static void
876 zsstart(tp)
877 register struct tty *tp;
878 {
879 register struct zs_chanstate *cs;
880 register int s, nch;
881 int unit = ZS_UNIT(tp->t_dev);
882 struct zs_softc *zi = zscd.cd_devs[unit >> 1];
883
884 cs = &zi->zi_cs[unit & 1];
885 s = spltty();
886
887 /*
888 * If currently active or delaying, no need to do anything.
889 */
890 if(tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
891 goto out;
892
893 /*
894 * If there are sleepers, and output has drained below low
895 * water mark, awaken.
896 */
897 if(tp->t_outq.c_cc <= tp->t_lowat) {
898 if(tp->t_state & TS_ASLEEP) {
899 tp->t_state &= ~TS_ASLEEP;
900 wakeup((caddr_t)&tp->t_outq);
901 }
902 selwakeup(&tp->t_wsel);
903 }
904
905 nch = ndqb(&tp->t_outq, 0); /* XXX */
906 if(nch) {
907 register char *p = tp->t_outq.c_cf;
908
909 /* mark busy, enable tx done interrupts, & send first byte */
910 tp->t_state |= TS_BUSY;
911 (void) splzs();
912 cs->cs_preg[1] |= ZSWR1_TIE;
913 cs->cs_creg[1] |= ZSWR1_TIE;
914 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
915 cs->cs_zc->zc_data = *p;
916 cs->cs_tba = p + 1;
917 cs->cs_tbc = nch - 1;
918 } else {
919 /*
920 * Nothing to send, turn off transmit done interrupts.
921 * This is useful if something is doing polled output.
922 */
923 (void) splzs();
924 cs->cs_preg[1] &= ~ZSWR1_TIE;
925 cs->cs_creg[1] &= ~ZSWR1_TIE;
926 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
927 }
928 out:
929 splx(s);
930 }
931
932 /*
933 * Stop output, e.g., for ^S or output flush.
934 */
935 void
936 zsstop(tp, flag)
937 register struct tty *tp;
938 int flag;
939 {
940 register struct zs_chanstate *cs;
941 register int s, unit = ZS_UNIT(tp->t_dev);
942 struct zs_softc *zi = zscd.cd_devs[unit >> 1];
943
944 cs = &zi->zi_cs[unit & 1];
945 s = splzs();
946 if(tp->t_state & TS_BUSY) {
947 /*
948 * Device is transmitting; must stop it.
949 */
950 cs->cs_tbc = 0;
951 if ((tp->t_state & TS_TTSTOP) == 0)
952 tp->t_state |= TS_FLUSH;
953 }
954 splx(s);
955 }
956
957 /*
958 * Set ZS tty parameters from termios.
959 *
960 * This routine makes use of the fact that only registers
961 * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
962 */
963 static int
964 zsparam(tp, t)
965 register struct tty *tp;
966 register struct termios *t;
967 {
968 int unit = ZS_UNIT(tp->t_dev);
969 struct zs_softc *zi = zscd.cd_devs[unit >> 1];
970 register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
971 int cdiv, clkm, brgm, tcon;
972 register int tmp, tmp5, cflag, s;
973
974 tmp = t->c_ospeed;
975 tmp5 = t->c_ispeed;
976 if(tmp < 0 || (tmp5 && tmp5 != tmp))
977 return(EINVAL);
978 if(tmp == 0) {
979 /* stty 0 => drop DTR and RTS */
980 zs_modem(cs, 0, DMSET);
981 return(0);
982 }
983 tmp = zsbaudrate(unit, tmp, &cdiv, &clkm, &brgm, &tcon);
984 if (tmp < 0)
985 return(EINVAL);
986 tp->t_ispeed = tp->t_ospeed = tmp;
987
988 cflag = tp->t_cflag = t->c_cflag;
989 if (cflag & CSTOPB)
990 cdiv |= ZSWR4_TWOSB;
991 else
992 cdiv |= ZSWR4_ONESB;
993 if (!(cflag & PARODD))
994 cdiv |= ZSWR4_EVENP;
995 if (cflag & PARENB)
996 cdiv |= ZSWR4_PARENB;
997
998 switch(cflag & CSIZE) {
999 case CS5:
1000 tmp = ZSWR3_RX_5;
1001 tmp5 = ZSWR5_TX_5;
1002 break;
1003 case CS6:
1004 tmp = ZSWR3_RX_6;
1005 tmp5 = ZSWR5_TX_6;
1006 break;
1007 case CS7:
1008 tmp = ZSWR3_RX_7;
1009 tmp5 = ZSWR5_TX_7;
1010 break;
1011 case CS8:
1012 default:
1013 tmp = ZSWR3_RX_8;
1014 tmp5 = ZSWR5_TX_8;
1015 break;
1016 }
1017 tmp |= ZSWR3_RX_ENABLE;
1018 tmp5 |= ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
1019
1020 /*
1021 * Block interrupts so that state will not
1022 * be altered until we are done setting it up.
1023 */
1024 s = splzs();
1025 cs->cs_preg[4] = cdiv;
1026 cs->cs_preg[11] = clkm;
1027 cs->cs_preg[12] = tcon;
1028 cs->cs_preg[13] = tcon >> 8;
1029 cs->cs_preg[14] = brgm;
1030 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
1031 cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_VECTOR_INCL_STAT;
1032 cs->cs_preg[10] = ZSWR10_NRZ;
1033 cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
1034
1035 /*
1036 * Output hardware flow control on the chip is horrendous: if
1037 * carrier detect drops, the receiver is disabled. Hence we
1038 * can only do this when the carrier is on.
1039 */
1040 if(cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD)
1041 tmp |= ZSWR3_HFC;
1042 cs->cs_preg[3] = tmp;
1043 cs->cs_preg[5] = tmp5;
1044
1045 /*
1046 * If nothing is being transmitted, set up new current values,
1047 * else mark them as pending.
1048 */
1049 if(cs->cs_heldchange == 0) {
1050 if (cs->cs_ttyp->t_state & TS_BUSY) {
1051 cs->cs_heldtbc = cs->cs_tbc;
1052 cs->cs_tbc = 0;
1053 cs->cs_heldchange = 1;
1054 } else {
1055 bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
1056 zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
1057 }
1058 }
1059 splx(s);
1060 return (0);
1061 }
1062
1063 /*
1064 * search for the best matching baudrate
1065 */
1066 static int
1067 zsbaudrate(unit, wanted, divisor, clockmode, brgenmode, timeconst)
1068 int unit, wanted, *divisor, *clockmode, *brgenmode, *timeconst;
1069 {
1070 int bestdiff, bestbps, source;
1071
1072 unit = (unit & 1) << 2;
1073 for (source = 0; source < 4; ++source) {
1074 long freq = zs_frequencies[unit + source];
1075 int diff, bps, div, clkm, brgm, tcon;
1076 switch (source) {
1077 case 0: /* BRgen, PCLK */
1078 brgm = ZSWR14_BAUD_ENA|ZSWR14_BAUD_FROM_PCLK;
1079 break;
1080 case 1: /* BRgen, RTxC */
1081 brgm = ZSWR14_BAUD_ENA;
1082 break;
1083 case 2: /* RTxC */
1084 clkm = ZSWR11_RXCLK_RTXC|ZSWR11_TXCLK_RTXC;
1085 break;
1086 case 3: /* TRxC */
1087 clkm = ZSWR11_RXCLK_TRXC|ZSWR11_TXCLK_TRXC;
1088 break;
1089 }
1090 switch (source) {
1091 case 0:
1092 case 1:
1093 div = ZSWR4_CLK_X16;
1094 clkm = ZSWR11_RXCLK_BAUD|ZSWR11_TXCLK_BAUD;
1095 tcon = BPS_TO_TCONST(freq, wanted);
1096 if (tcon < 0)
1097 tcon = 0;
1098 bps = TCONST_TO_BPS(freq, tcon);
1099 break;
1100 case 2:
1101 case 3:
1102 { int b1 = freq / 16, d1 = abs(b1 - wanted);
1103 int b2 = freq / 32, d2 = abs(b2 - wanted);
1104 int b3 = freq / 64, d3 = abs(b3 - wanted);
1105
1106 if (d1 < d2 && d1 < d3) {
1107 div = ZSWR4_CLK_X16;
1108 bps = b1;
1109 } else if (d2 < d3 && d2 < d1) {
1110 div = ZSWR4_CLK_X32;
1111 bps = b2;
1112 } else {
1113 div = ZSWR4_CLK_X64;
1114 bps = b3;
1115 }
1116 brgm = tcon = 0;
1117 break;
1118 }
1119 }
1120 diff = abs(bps - wanted);
1121 if (!source || diff < bestdiff) {
1122 *divisor = div;
1123 *clockmode = clkm;
1124 *brgenmode = brgm;
1125 *timeconst = tcon;
1126 bestbps = bps;
1127 bestdiff = diff;
1128 if (diff == 0)
1129 break;
1130 }
1131 }
1132 /* Allow deviations upto 5% */
1133 if (20 * bestdiff > wanted)
1134 return -1;
1135 return bestbps;
1136 }
1137
1138 /*
1139 * Raise or lower modem control (DTR/RTS) signals. If a character is
1140 * in transmission, the change is deferred.
1141 */
1142 static int
1143 zs_modem(cs, bits, how)
1144 struct zs_chanstate *cs;
1145 int bits, how;
1146 {
1147 int s, mbits;
1148
1149 bits &= ZSWR5_DTR | ZSWR5_RTS;
1150
1151 s = splzs();
1152 mbits = cs->cs_preg[5] & (ZSWR5_DTR | ZSWR5_RTS);
1153
1154 switch(how) {
1155 case DMSET:
1156 mbits = bits;
1157 break;
1158 case DMBIS:
1159 mbits |= bits;
1160 break;
1161 case DMBIC:
1162 mbits &= ~bits;
1163 break;
1164 case DMGET:
1165 splx(s);
1166 return(mbits);
1167 }
1168
1169 cs->cs_preg[5] = (cs->cs_preg[5] & ~(ZSWR5_DTR | ZSWR5_RTS)) | mbits;
1170 if(cs->cs_heldchange == 0) {
1171 if(cs->cs_ttyp->t_state & TS_BUSY) {
1172 cs->cs_heldtbc = cs->cs_tbc;
1173 cs->cs_tbc = 0;
1174 cs->cs_heldchange = 1;
1175 }
1176 else {
1177 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1178 }
1179 }
1180 splx(s);
1181 return(0);
1182 }
1183
1184 /*
1185 * Write the given register set to the given zs channel in the proper order.
1186 * The channel must not be transmitting at the time. The receiver will
1187 * be disabled for the time it takes to write all the registers.
1188 */
1189 static void
1190 zs_loadchannelregs(zc, reg)
1191 volatile struct zschan *zc;
1192 u_char *reg;
1193 {
1194 int i;
1195
1196 zc->zc_csr = ZSM_RESET_ERR; /* reset error condition */
1197 i = zc->zc_data; /* drain fifo */
1198 i = zc->zc_data;
1199 i = zc->zc_data;
1200 ZS_WRITE(zc, 4, reg[4]);
1201 ZS_WRITE(zc, 10, reg[10]);
1202 ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
1203 ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
1204 ZS_WRITE(zc, 1, reg[1]);
1205 ZS_WRITE(zc, 9, reg[9]);
1206 ZS_WRITE(zc, 11, reg[11]);
1207 ZS_WRITE(zc, 12, reg[12]);
1208 ZS_WRITE(zc, 13, reg[13]);
1209 ZS_WRITE(zc, 14, reg[14]);
1210 ZS_WRITE(zc, 15, reg[15]);
1211 ZS_WRITE(zc, 3, reg[3]);
1212 ZS_WRITE(zc, 5, reg[5]);
1213 }
1214 #endif /* NZS > 1 */
1215