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