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