xref: /openbsd-src/sys/dev/ic/cy.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: cy.c,v 1.13 2001/04/23 10:55:46 art Exp $	*/
2 
3 /*
4  * cy.c
5  *
6  * Driver for Cyclades Cyclom-8/16/32 multiport serial cards
7  * (currently not tested with Cyclom-32 cards)
8  *
9  * Timo Rossi, 1996
10  *
11  * Supports both ISA and PCI Cyclom cards
12  *
13  * Uses CD1400 automatic CTS flow control, and
14  * if CY_HW_RTS is defined, uses CD1400 automatic input flow control.
15  * This requires a special cable that exchanges the RTS and DTR lines.
16  *
17  * Lots of debug output can be enabled by defining CY_DEBUG
18  * Some debugging counters (number of receive/transmit interrupts etc.)
19  * can be enabled by defining CY_DEBUG1
20  *
21  * This version uses the bus_space/io_??() stuff
22  *
23  */
24 
25 #undef CY_DEBUG
26 #undef CY_DEBUG1
27 
28 /* NCY is the number of Cyclom cards in the machine */
29 #include "cy.h"
30 #if NCY > 0
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/syslog.h>
36 #include <sys/fcntl.h>
37 #include <sys/tty.h>
38 #include <sys/proc.h>
39 #include <sys/conf.h>
40 #include <sys/user.h>
41 #include <sys/select.h>
42 #include <sys/device.h>
43 #include <sys/malloc.h>
44 #include <sys/systm.h>
45 
46 #include <machine/bus.h>
47 #include <machine/intr.h>
48 
49 #if NCY_ISA > 0
50 #include <dev/isa/isavar.h>
51 #include <dev/isa/isareg.h>
52 #endif /* NCY_ISA > 0 */
53 #if NCY_PCI > 0
54 #include <dev/pci/pcivar.h>
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcidevs.h>
57 #endif /* NCY_PCI > 0 */
58 
59 #include <dev/ic/cd1400reg.h>
60 #include <dev/ic/cyreg.h>
61 
62 
63 /* Macros to clear/set/test flags. */
64 #define	SET(t, f)	(t) |= (f)
65 #define	CLR(t, f)	(t) &= ~(f)
66 #define	ISSET(t, f)	((t) & (f))
67 
68 
69 void	cyattach __P((struct device *, struct device *, void *));
70 int	cy_probe_common __P((int, bus_space_tag_t, bus_space_handle_t, int));
71 int	cyintr __P((void *));
72 int	cyparam __P((struct tty *, struct termios *));
73 void	cystart __P((struct tty *));
74 void	cy_poll __P((void *));
75 int	cy_modem_control __P((struct cy_port *, int, int));
76 void	cy_enable_transmitter __P((struct cy_port *));
77 void	cd1400_channel_cmd __P((struct cy_port *, int));
78 int	cy_speed __P((speed_t, int *, int *, int));
79 
80 struct cfdriver cy_cd = {
81   NULL, "cy", DV_TTY
82 };
83 
84 static int cy_nr_cd1400s[NCY];
85 static int cy_bus_types[NCY];
86 static bus_space_handle_t cy_card_memh[NCY];
87 static int cy_open = 0;
88 static int cy_events = 0;
89 
90 struct timeout cy_poll_to;
91 
92 /*
93  * Common probe routine
94  */
95 int
96 cy_probe_common(card, memt, memh, bustype)
97      int card, bustype;
98      bus_space_tag_t memt;
99      bus_space_handle_t memh;
100 {
101   int cy_chip, chip_offs;
102   u_char firmware_ver;
103 
104   /* Cyclom card hardware reset */
105   bus_space_write_1(memt, memh, CY16_RESET<<bustype, 0);
106   DELAY(500); /* wait for reset to complete */
107   bus_space_write_1(memt, memh, CY_CLEAR_INTR<<bustype, 0);
108 
109 #ifdef CY_DEBUG
110   printf("cy: card reset done\n");
111 #endif
112 
113   cy_nr_cd1400s[card] = 0;
114 
115   for(cy_chip = 0, chip_offs = 0;
116       cy_chip < CY_MAX_CD1400s;
117       cy_chip++, chip_offs += (CY_CD1400_MEMSPACING<<bustype)) {
118     int i;
119 
120     /* the last 4 cd1400s are 'interleaved'
121        with the first 4 on 32-port boards */
122     if(cy_chip == 4)
123       chip_offs -= (CY32_ADDR_FIX<<bustype);
124 
125 #ifdef CY_DEBUG
126     printf("cy%d probe chip %d offset 0x%lx ... ",
127 	   card, cy_chip, chip_offs);
128 #endif
129 
130     /* wait until the chip is ready for command */
131     DELAY(1000);
132     if(bus_space_read_1(memt, memh, chip_offs +
133 		      ((CD1400_CCR<<1) << bustype)) != 0) {
134 #ifdef CY_DEBUG
135       printf("not ready for command\n");
136 #endif
137       break;
138     }
139 
140     /* clear the firmware version reg. */
141     bus_space_write_1(memt, memh, chip_offs +
142 		    ((CD1400_GFRCR<<1) << bustype), 0);
143 
144     /*
145      * On Cyclom-16 references to non-existent chip 4
146      * actually access chip 0 (address line 9 not decoded).
147      * Here we check if the clearing of chip 4 GFRCR actually
148      * cleared chip 0 GFRCR. In that case we have a 16 port card.
149      */
150     if(cy_chip == 4 &&
151        bus_space_read_1(memt, memh, chip_offs +
152 		      ((CD1400_GFRCR<<1) << bustype)) ==0)
153       break;
154 
155     /* reset the chip */
156     bus_space_write_1(memt, memh, chip_offs +
157 		    ((CD1400_CCR<<1) << bustype),
158 		    CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
159 
160     /* wait for the chip to initialize itself */
161     for(i = 0; i < 200; i++) {
162       DELAY(50);
163       firmware_ver =
164 	bus_space_read_1(memt, memh, chip_offs +
165 		       ((CD1400_GFRCR<<1) << bustype));
166       if((firmware_ver & 0xf0) == 0x40) /* found a CD1400 */
167 	break;
168     }
169 #ifdef CY_DEBUG
170     printf("firmware version 0x%x\n", firmware_ver);
171 #endif
172 
173     if((firmware_ver & 0xf0) != 0x40)
174       break;
175 
176     /* firmware version OK, CD1400 found */
177     cy_nr_cd1400s[card]++;
178   }
179 
180   if(cy_nr_cd1400s[card] == 0) {
181 #ifdef CY_DEBUG
182     printf("no CD1400s found\n");
183 #endif
184     return 0;
185   }
186 
187 #ifdef CY_DEBUG
188   printf("found %d CD1400s\n", cy_nr_cd1400s[card]);
189 #endif
190 
191   cy_card_memh[card] = memh;
192   cy_bus_types[card] = bustype;
193 
194   return 1;
195 }
196 
197 /*
198  * Attach (ISA/PCI)
199  */
200 void
201 cyattach(parent, self, aux)
202      struct device *parent, *self;
203      void *aux;
204 {
205   struct cy_softc *sc = (void *)self;
206   int card, port, cy_chip, num_chips, cdu, chip_offs, cy_clock;
207 
208   card = sc->sc_dev.dv_unit;
209   num_chips = cy_nr_cd1400s[card];
210   if(num_chips == 0)
211     return;
212 
213   sc->sc_bustype = cy_bus_types[card];
214   sc->sc_memh = cy_card_memh[card];
215   switch(sc->sc_bustype) {
216 #if NCY_ISA > 0
217     case CY_BUSTYPE_ISA:
218       sc->sc_memt = ((struct isa_attach_args *)(aux))->ia_memt;
219       break;
220 #endif
221 #if NCY_PCI > 0
222     case CY_BUSTYPE_PCI:
223       sc->sc_memt = ((struct pci_attach_args *)aux)->pa_memt;
224       break;
225 #endif
226   }
227 
228   if (!timeout_initialized(&cy_poll_to))
229     timeout_set(&cy_poll_to, cy_poll, NULL);
230   bzero(sc->sc_ports, sizeof(sc->sc_ports));
231   sc->sc_nports = num_chips * CD1400_NO_OF_CHANNELS;
232 
233   port = 0;
234   for(cy_chip = 0, chip_offs = 0;
235       cy_chip < num_chips; cy_chip++,
236       chip_offs += (CY_CD1400_MEMSPACING<<sc->sc_bustype)) {
237 
238     if(cy_chip == 4)
239       chip_offs -= (CY32_ADDR_FIX<<sc->sc_bustype);
240 
241 #ifdef CY_DEBUG
242     printf("attach CD1400 #%d offset 0x%x\n", cy_chip, chip_offs);
243 #endif
244     sc->sc_cd1400_offs[cy_chip] = chip_offs;
245 
246     /* configure port 0 as serial port (should already be after reset) */
247     cd_write_reg_sc(sc, cy_chip, CD1400_GCR, 0);
248 
249     /* Set cy_clock depending on firmware version */
250     if (cd_read_reg_sc(sc, cy_chip, CD1400_GFRCR) <= 0x46)
251         cy_clock = CY_CLOCK;
252     else
253         cy_clock = CY_CLOCK_60;
254 
255     /* set up a receive timeout period (1ms) */
256     cd_write_reg_sc(sc, cy_chip, CD1400_PPR,
257 		    (cy_clock / CD1400_PPR_PRESCALER / 1000) + 1);
258 
259     for(cdu = 0; cdu < CD1400_NO_OF_CHANNELS; cdu++) {
260       sc->sc_ports[port].cy_port_num = port;
261       sc->sc_ports[port].cy_memt = sc->sc_memt;
262       sc->sc_ports[port].cy_memh = sc->sc_memh;
263       sc->sc_ports[port].cy_chip_offs = chip_offs;
264       sc->sc_ports[port].cy_bustype = sc->sc_bustype;
265       sc->sc_ports[port].cy_clock = cy_clock;
266 
267       /* should we initialize anything else here? */
268       port++;
269     } /* for(each port on one CD1400...) */
270 
271   } /* for(each CD1400 on a card... ) */
272 
273   printf(" (%d ports)\n", port);
274 
275   /* ensure an edge for the next interrupt */
276   bus_space_write_1(sc->sc_memt, sc->sc_memh,
277 		  CY_CLEAR_INTR<<sc->sc_bustype, 0);
278 
279   switch(sc->sc_bustype) {
280 #if NCY_ISA > 0
281     case CY_BUSTYPE_ISA:
282       {
283 	struct isa_attach_args *ia = aux;
284 
285 	sc->sc_ih =
286 	  isa_intr_establish(ia->ia_ic, ia->ia_irq,
287 	    IST_EDGE, IPL_TTY, cyintr, sc, sc->sc_dev.dv_xname);
288       }
289       break;
290 #endif /* NCY_ISA > 0 */
291 #if NCY_PCI > 0
292     case CY_BUSTYPE_PCI:
293       {
294 	pci_intr_handle_t intrhandle;
295 	struct pci_attach_args *pa = aux;
296 
297 	if(pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
298 			pa->pa_intrline, &intrhandle) != 0)
299 	  panic("cy: couldn't map PCI interrupt");
300 
301 	sc->sc_ih = pci_intr_establish(pa->pa_pc, intrhandle,
302 	  IPL_TTY, cyintr, sc, sc->sc_dev.dv_xname);
303       }
304       break;
305 #endif /* NCY_PCI > 0 */
306   }
307 
308   if(sc->sc_ih == NULL)
309     panic("cy: couldn't establish interrupt");
310 }
311 
312 #undef CY_DEBUG /*!!*/
313 
314 /*
315  * open routine. returns zero if successfull, else error code
316  */
317 int cyopen __P((dev_t, int, int, struct proc *));
318 int cyclose __P((dev_t, int, int, struct proc *));
319 int cyread __P((dev_t, struct uio *, int));
320 int cywrite __P((dev_t, struct uio *, int));
321 struct tty *cytty __P((dev_t));
322 int cyioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
323 int cystop __P((struct tty *, int flag));
324 
325 int
326 cyopen(dev, flag, mode, p)
327      dev_t dev;
328      int flag, mode;
329      struct proc *p;
330 {
331     int card = CY_CARD(dev);
332     int port = CY_PORT(dev);
333     struct cy_softc *sc;
334     struct cy_port *cy;
335     struct tty *tp;
336     int s, error;
337 
338 #ifdef CY_DEBUG
339     printf("cy%d open port %d flag 0x%x mode 0x%x\n",
340 	   card, port, flag, mode);
341 #endif
342 
343     if(card >= cy_cd.cd_ndevs ||
344        (sc = cy_cd.cd_devs[card]) == NULL)
345       return ENXIO;
346 
347     cy = &sc->sc_ports[port];
348 
349     s = spltty();
350     if(cy->cy_tty == NULL) {
351 	if((cy->cy_tty = ttymalloc()) == NULL) {
352 	    splx(s);
353 	    printf("cy%d port %d open: can't allocate tty\n", card, port);
354 	    return ENOMEM;
355 	}
356     }
357     splx(s);
358 
359     tp = cy->cy_tty;
360     tty_attach(tp);
361     tp = cy->cy_tty;
362     tp->t_oproc = cystart;
363     tp->t_param = cyparam;
364     tp->t_dev = dev;
365 
366     if(!ISSET(tp->t_state, TS_ISOPEN)) {
367 	SET(tp->t_state, TS_WOPEN);
368 	ttychars(tp);
369 	tp->t_iflag = TTYDEF_IFLAG;
370 	tp->t_oflag = TTYDEF_OFLAG;
371 	tp->t_cflag = TTYDEF_CFLAG;
372 	if(ISSET(cy->cy_openflags, TIOCFLAG_CLOCAL))
373 	  SET(tp->t_cflag, CLOCAL);
374 	if(ISSET(cy->cy_openflags, TIOCFLAG_CRTSCTS))
375 	  SET(tp->t_cflag, CRTSCTS);
376 	if(ISSET(cy->cy_openflags, TIOCFLAG_MDMBUF))
377 	  SET(tp->t_cflag, MDMBUF);
378 	tp->t_lflag = TTYDEF_LFLAG;
379 	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
380 
381 	s = spltty();
382 
383 	/*
384 	 * Allocate input ring buffer if we don't already have one
385 	 */
386 	if(cy->cy_ibuf == NULL) {
387 	    cy->cy_ibuf = malloc(IBUF_SIZE, M_DEVBUF, M_NOWAIT);
388 	    if(cy->cy_ibuf == NULL) {
389 		printf("cy%d: (port %d) can't allocate input buffer\n",
390 		       card, port);
391 		splx(s);
392 		return ENOMEM;
393 	    }
394 	    cy->cy_ibuf_end = cy->cy_ibuf + IBUF_SIZE;
395 	}
396 
397 	/* mark the ring buffer as empty */
398 	cy->cy_ibuf_rd_ptr = cy->cy_ibuf_wr_ptr = cy->cy_ibuf;
399 
400 	/* select CD1400 channel */
401 	cd_write_reg(cy, CD1400_CAR, port & CD1400_CAR_CHAN);
402 	/* reset the channel */
403 	cd1400_channel_cmd(cy, CD1400_CCR_CMDRESET);
404 	/* encode unit (port) number in LIVR */
405 	/* there is just enough space for 5 bits (32 ports) */
406 	cd_write_reg(cy, CD1400_LIVR, port << 3);
407 
408 	cy->cy_channel_control = 0;
409 
410 	/* hmm... need spltty() here? */
411 	if(cy_open == 0)
412 	  {
413 	    cy_open = 1;
414 	    timeout_add(&cy_poll_to, 1);
415 	  }
416 
417 	/* this sets parameters and raises DTR */
418 	cyparam(tp, &tp->t_termios);
419 
420 	ttsetwater(tp);
421 
422 	/* raise RTS too */
423 	cy_modem_control(cy, TIOCM_RTS, DMBIS);
424 
425 	cy->cy_carrier_stat =
426 	  cd_read_reg(cy, CD1400_MSVR2);
427 
428 	/* enable receiver and modem change interrupts */
429 	cd_write_reg(cy, CD1400_SRER, CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
430 
431 	if(CY_DIALOUT(dev) ||
432 	   ISSET(cy->cy_openflags, TIOCFLAG_SOFTCAR) ||
433 	   ISSET(tp->t_cflag, MDMBUF) ||
434 	   ISSET(cy->cy_carrier_stat, CD1400_MSVR2_CD))
435 	  SET(tp->t_state, TS_CARR_ON);
436 	else
437 	  CLR(tp->t_state, TS_CARR_ON);
438     } else if(ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0) {
439 	return EBUSY;
440     } else {
441 	s = spltty();
442     }
443 
444     /* wait for carrier if necessary */
445     if(!ISSET(flag, O_NONBLOCK)) {
446 	while(!ISSET(tp->t_cflag, CLOCAL) &&
447 	    !ISSET(tp->t_state, TS_CARR_ON)) {
448 	    SET(tp->t_state, TS_WOPEN);
449 	    error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH, "cydcd", 0);
450 	    if(error != 0) {
451 		splx(s);
452 		CLR(tp->t_state, TS_WOPEN);
453 		return error;
454 	    }
455 	}
456     }
457 
458     splx(s);
459 
460     return (*linesw[tp->t_line].l_open)(dev, tp);
461 }
462 
463 /*
464  * close routine. returns zero if successfull, else error code
465  */
466 int
467 cyclose(dev, flag, mode, p)
468      dev_t dev;
469      int flag, mode;
470      struct proc *p;
471 {
472     int card = CY_CARD(dev);
473     int port = CY_PORT(dev);
474     struct cy_softc *sc = cy_cd.cd_devs[card];
475     struct cy_port *cy = &sc->sc_ports[port];
476     struct tty *tp = cy->cy_tty;
477     int s;
478 
479 #ifdef CY_DEBUG
480     printf("cy%d close port %d, flag 0x%x, mode 0x%x\n",
481 	 card, port, flag, mode);
482 #endif
483 
484     (*linesw[tp->t_line].l_close)(tp, flag);
485     s = spltty();
486 
487     if(ISSET(tp->t_cflag, HUPCL) &&
488        !ISSET(cy->cy_openflags, TIOCFLAG_SOFTCAR)) {
489 	/* drop DTR and RTS
490 	   (should we wait for output buffer to become empty first?) */
491 	cy_modem_control(cy, 0, DMSET);
492     }
493 
494 /*
495  * XXX should we disable modem change and
496  * receive interrupts here or somewhere ?
497  */
498     CLR(tp->t_state, TS_BUSY | TS_FLUSH);
499 
500     splx(s);
501     ttyclose(tp);
502 
503     return 0;
504 }
505 
506 /*
507  * Read routine
508  */
509 int
510 cyread(dev, uio, flag)
511      dev_t dev;
512      struct uio *uio;
513      int flag;
514 {
515     int card = CY_CARD(dev);
516     int port = CY_PORT(dev);
517     struct cy_softc *sc = cy_cd.cd_devs[card];
518     struct cy_port *cy = &sc->sc_ports[port];
519     struct tty *tp = cy->cy_tty;
520 
521 #ifdef CY_DEBUG
522     printf("cy%d read port %d uio 0x%x flag 0x%x\n",
523 	   card, port, uio, flag);
524 #endif
525 
526     return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
527 }
528 
529 /*
530  * Write routine
531  */
532 int
533 cywrite(dev, uio, flag)
534      dev_t dev;
535      struct uio *uio;
536      int flag;
537 {
538     int card = CY_CARD(dev);
539     int port = CY_PORT(dev);
540     struct cy_softc *sc = cy_cd.cd_devs[card];
541     struct cy_port *cy = &sc->sc_ports[port];
542     struct tty *tp = cy->cy_tty;
543 
544 #ifdef CY_DEBUG
545     printf("cy%d write port %d uio 0x%x flag 0x%x\n",
546 	   card, port, uio, flag);
547 #endif
548 
549     return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
550 }
551 
552 /*
553  * return tty pointer
554  */
555 struct tty *
556 cytty(dev)
557      dev_t dev;
558 {
559     int card = CY_CARD(dev);
560     int port = CY_PORT(dev);
561     struct cy_softc *sc = cy_cd.cd_devs[card];
562     struct cy_port *cy = &sc->sc_ports[port];
563     struct tty *tp = cy->cy_tty;
564 
565 #ifdef CY_DEBUG
566     printf("cy%d tty port %d tp 0x%x\n",
567 	   card, port, tp);
568 #endif
569 
570     return tp;
571 }
572 
573 /*
574  * ioctl routine
575  */
576 int
577 cyioctl(dev, cmd, data, flag, p)
578      dev_t dev;
579      u_long cmd;
580      caddr_t data;
581      int flag;
582      struct proc *p;
583 {
584     int card = CY_CARD(dev);
585     int port = CY_PORT(dev);
586     struct cy_softc *sc = cy_cd.cd_devs[card];
587     struct cy_port *cy = &sc->sc_ports[port];
588     struct tty *tp = cy->cy_tty;
589     int error;
590 
591 #ifdef CY_DEBUG
592     printf("cy%d port %d ioctl cmd 0x%x data 0x%x flag 0x%x\n",
593 	   card, port, cmd, data, flag);
594 #endif
595 
596     error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
597     if(error >= 0)
598       return error;
599 
600     error = ttioctl(tp, cmd, data, flag, p);
601     if(error >= 0)
602       return error;
603 
604 /* XXX should not allow dropping DTR when dialin? */
605 
606     switch(cmd) {
607       case TIOCSBRK: /* start break */
608         SET(cy->cy_flags, CYF_START_BREAK);
609 	cy_enable_transmitter(cy);
610         break;
611 
612       case TIOCCBRK: /* stop break */
613 	SET(cy->cy_flags, CYF_END_BREAK);
614 	cy_enable_transmitter(cy);
615 	break;
616 
617       case TIOCSDTR: /* DTR on */
618 	cy_modem_control(cy, TIOCM_DTR, DMBIS);
619 	break;
620 
621       case TIOCCDTR: /* DTR off */
622 	cy_modem_control(cy, TIOCM_DTR, DMBIC);
623 	break;
624 
625       case TIOCMSET: /* set new modem control line values */
626 	cy_modem_control(cy, *((int *)data), DMSET);
627 	break;
628 
629       case TIOCMBIS: /* turn modem control bits on */
630 	cy_modem_control(cy, *((int *)data), DMBIS);
631 	break;
632 
633       case TIOCMBIC: /* turn modem control bits off */
634 	cy_modem_control(cy, *((int *)data), DMBIC);
635 	break;
636 
637       case TIOCMGET: /* get modem control/status line state */
638 	*((int *)data) = cy_modem_control(cy, 0, DMGET);
639 	break;
640 
641       case TIOCGFLAGS:
642 	*((int *)data) = cy->cy_openflags |
643 	  (CY_DIALOUT(dev) ? TIOCFLAG_SOFTCAR : 0);
644 	break;
645 
646       case TIOCSFLAGS:
647 	error = suser(p->p_ucred, &p->p_acflag);
648 	if(error != 0)
649 	  return EPERM;
650 
651 	cy->cy_openflags = *((int *)data) &
652 	  (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
653 	   TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
654 	break;
655 
656       default:
657 	return ENOTTY;
658     }
659 
660     return 0;
661 }
662 
663 /*
664  * start output
665  */
666 void
667 cystart(tp)
668      struct tty *tp;
669 {
670     int card = CY_CARD(tp->t_dev);
671     int port = CY_PORT(tp->t_dev);
672     struct cy_softc *sc = cy_cd.cd_devs[card];
673     struct cy_port *cy = &sc->sc_ports[port];
674     int s;
675 
676 #ifdef CY_DEBUG
677     printf("cy%d port %d start, tty 0x%x\n", card, port, tp);
678 #endif
679 
680 
681     s = spltty();
682 
683 #ifdef CY_DEBUG1
684     cy->cy_start_count++;
685 #endif
686 
687   if(!ISSET(tp->t_state, TS_TTSTOP | TS_TIMEOUT | TS_BUSY)) {
688       if(tp->t_outq.c_cc <= tp->t_lowat) {
689 	  if(ISSET(tp->t_state, TS_ASLEEP)) {
690 	      CLR(tp->t_state, TS_ASLEEP);
691 	      wakeup(&tp->t_outq);
692 	  }
693 
694 	  selwakeup(&tp->t_wsel);
695 
696 	  if(tp->t_outq.c_cc == 0)
697 	    goto out;
698       }
699 
700       SET(tp->t_state, TS_BUSY);
701       cy_enable_transmitter(cy);
702   }
703 out:
704 
705     splx(s);
706 }
707 
708 /*
709  * stop output
710  */
711 int
712 cystop(tp, flag)
713      struct tty *tp;
714      int flag;
715 {
716     int card = CY_CARD(tp->t_dev);
717     int port = CY_PORT(tp->t_dev);
718     struct cy_softc *sc = cy_cd.cd_devs[card];
719     struct cy_port *cy = &sc->sc_ports[port];
720     int s;
721 
722 #ifdef CY_DEBUG
723     printf("cy%d port %d stop tty 0x%x flag 0x%x\n",
724 	   card, port, tp, flag);
725 #endif
726 
727     s = spltty();
728 
729     if(ISSET(tp->t_state, TS_BUSY)) {
730 	if(!ISSET(tp->t_state, TS_TTSTOP))
731 	  SET(tp->t_state, TS_FLUSH);
732 
733 	/*
734 	 * the transmit interrupt routine will disable transmit when it
735 	 * notices that CYF_STOP has been set.
736 	 */
737 	SET(cy->cy_flags, CYF_STOP);
738     }
739     splx(s);
740     return(0);
741 }
742 
743 /*
744  * parameter setting routine.
745  * returns 0 if successfull, else returns error code
746  */
747 int
748 cyparam(tp, t)
749      struct tty *tp;
750      struct termios *t;
751 {
752     int card = CY_CARD(tp->t_dev);
753     int port = CY_PORT(tp->t_dev);
754     struct cy_softc *sc = cy_cd.cd_devs[card];
755     struct cy_port *cy = &sc->sc_ports[port];
756     int ibpr, obpr, i_clk_opt, o_clk_opt;
757     int s, opt;
758 
759 #ifdef CY_DEBUG
760     printf("cy%d port %d param tty 0x%x termios 0x%x\n",
761 	   card, port, tp, t);
762     printf("ispeed %d ospeed %d\n", t->c_ispeed, t->c_ospeed);
763 #endif
764 
765     if(t->c_ospeed != 0 && cy_speed(t->c_ospeed, &o_clk_opt, &obpr, cy->cy_clock) < 0)
766       return EINVAL;
767 
768     if(t->c_ispeed != 0 && cy_speed(t->c_ispeed, &i_clk_opt, &ibpr, cy->cy_clock) < 0)
769       return EINVAL;
770 
771     s = spltty();
772 
773     /* hang up the line is ospeed is zero, else turn DTR on */
774     cy_modem_control(cy, TIOCM_DTR, (t->c_ospeed == 0 ? DMBIC : DMBIS));
775 
776     /* channel was selected by the above call to cy_modem_control() */
777     /* cd_write_reg(cy, CD1400_CAR, port & CD1400_CAR_CHAN); */
778 
779     /* set transmit speed */
780     if(t->c_ospeed != 0) {
781 	cd_write_reg(cy, CD1400_TCOR, o_clk_opt);
782 	cd_write_reg(cy, CD1400_TBPR, obpr);
783     }
784     /* set receive speed */
785     if(t->c_ispeed != 0) {
786 	cd_write_reg(cy, CD1400_RCOR, i_clk_opt);
787 	cd_write_reg(cy, CD1400_RBPR, ibpr);
788     }
789 
790     opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN
791       | (ISSET(t->c_cflag, CREAD) ? CD1400_CCR_RCVEN : CD1400_CCR_RCVDIS);
792 
793     if(opt != cy->cy_channel_control) {
794 	cy->cy_channel_control = opt;
795 	cd1400_channel_cmd(cy, opt);
796     }
797 
798     /* compute COR1 contents */
799     opt = 0;
800     if(ISSET(t->c_cflag, PARENB)) {
801 	if(ISSET(t->c_cflag, PARODD))
802 	  opt |= CD1400_COR1_PARODD;
803 	opt |= CD1400_COR1_PARNORMAL;
804     }
805 
806     if(!ISSET(t->c_iflag, INPCK))
807       opt |= CD1400_COR1_NOINPCK; /* no parity checking */
808 
809     if(ISSET(t->c_cflag, CSTOPB))
810       opt |= CD1400_COR1_STOP2;
811 
812     switch(t->c_cflag & CSIZE) {
813       case CS5:
814         opt |= CD1400_COR1_CS5;
815         break;
816 
817       case CS6:
818         opt |= CD1400_COR1_CS6;
819 	break;
820 
821       case CS7:
822         opt |= CD1400_COR1_CS7;
823 	break;
824 
825       default:
826         opt |= CD1400_COR1_CS8;
827 	break;
828     }
829 
830     cd_write_reg(cy, CD1400_COR1, opt);
831 
832 #ifdef CY_DEBUG
833     printf("cor1 = 0x%x...", opt);
834 #endif
835 
836 /*
837  * use the CD1400 automatic CTS flow control if CRTSCTS is set
838  *
839  * CD1400_COR2_ETC is used because breaks are generated with
840  * embedded transmit commands
841  */
842     cd_write_reg(cy, CD1400_COR2,
843 		 CD1400_COR2_ETC |
844 		 (ISSET(t->c_cflag, CRTSCTS) ? CD1400_COR2_CCTS_OFLOW : 0));
845 
846     cd_write_reg(cy, CD1400_COR3, RX_FIFO_THRESHOLD);
847 
848     cd1400_channel_cmd(cy,
849 		       CD1400_CCR_CMDCORCHG |
850 		       CD1400_CCR_COR1 | CD1400_CCR_COR2 | CD1400_CCR_COR3);
851 
852     cd_write_reg(cy, CD1400_COR4, CD1400_COR4_PFO_EXCEPTION);
853     cd_write_reg(cy, CD1400_COR5, 0);
854 
855   /*
856    * set modem change option registers to generate interrupts
857    * on carrier detect changes.
858    *
859    * if hardware RTS handshaking is used (CY_HW_RTS, DTR and RTS lines
860    * exchanged), also set the handshaking threshold.
861    */
862 #ifdef CY_HW_RTS
863     cd_write_reg(cy, CD1400_MCOR1, CD1400_MCOR1_CDzd |
864 		 (ISSET(t->c_cflag, CRTSCTS) ? RX_DTR_THRESHOLD : 0));
865 #else
866     cd_write_reg(cy, CD1400_MCOR1, CD1400_MCOR1_CDzd);
867 #endif /* CY_HW_RTS */
868 
869     cd_write_reg(cy, CD1400_MCOR2, CD1400_MCOR2_CDod);
870 
871     /*
872      * set receive timeout to approx. 2ms
873      * could use more complex logic here...
874      * (but is it actually needed or even useful?)
875      */
876     cd_write_reg(cy, CD1400_RTPR, 2);
877 
878     /*
879      * should do anything else here?
880      * XXX check MDMBUF handshaking like in com.c?
881      */
882 
883     splx(s);
884     return 0;
885 }
886 
887 /*
888  * set/get modem line status
889  *
890  * bits can be: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, TIOCM_CD, TIOCM_RI, TIOCM_DSR
891  *
892  * RTS and DTR are exchanged if CY_HW_RTS is set
893  *
894  */
895 int
896 cy_modem_control(cy, bits, howto)
897      struct cy_port *cy;
898      int bits;
899      int howto;
900 {
901     int s, msvr;
902 
903     s = spltty();
904 
905     /* select channel */
906     cd_write_reg(cy, CD1400_CAR, cy->cy_port_num & CD1400_CAR_CHAN);
907 
908 /* does not manipulate RTS if it is used for flow control */
909     switch(howto) {
910       case DMGET:
911 	bits = 0;
912 	if(cy->cy_channel_control & CD1400_CCR_RCVEN)
913 	  bits |= TIOCM_LE;
914 	msvr = cd_read_reg(cy, CD1400_MSVR2);
915 #ifdef CY_HW_RTS
916 	if(cd_read_reg(cy, CD1400_MSVR1) & CD1400_MSVR1_RTS)
917 	  bits |= TIOCM_DTR;
918 	if(msvr & CD1400_MSVR2_DTR)
919 	  bits |= TIOCM_RTS;
920 #else
921 	if(cd_read_reg(cy, CD1400_MSVR1) & CD1400_MSVR1_RTS)
922 	  bits |= TIOCM_RTS;
923 	if(msvr & CD1400_MSVR2_DTR)
924 	  bits |= TIOCM_DTR;
925 #endif /* CY_HW_RTS */
926 	if(msvr & CD1400_MSVR2_CTS)
927 	  bits |= TIOCM_CTS;
928 	if(msvr & CD1400_MSVR2_CD)
929 	  bits |= TIOCM_CD;
930 	if(msvr & CD1400_MSVR2_DSR) /* not connected on some Cyclom cards? */
931 	  bits |= TIOCM_DSR;
932 	if(msvr & CD1400_MSVR2_RI) /* not connected on Cyclom-8Y cards? */
933 	  bits |= TIOCM_RI;
934 	splx(s);
935 	return bits;
936 
937       case DMSET: /* replace old values with new ones */
938 #ifdef CY_HW_RTS
939 	if(!ISSET(cy->cy_tty->t_cflag, CRTSCTS))
940 	  cd_write_reg(cy, CD1400_MSVR2,
941 		       ((bits & TIOCM_RTS) ? CD1400_MSVR2_DTR : 0));
942 	cd_write_reg(cy, CD1400_MSVR1,
943 		     ((bits & TIOCM_DTR) ? CD1400_MSVR1_RTS : 0));
944 #else
945 	if(!ISSET(cy->cy_tty->t_cflag, CRTSCTS))
946 	  cd_write_reg(cy, CD1400_MSVR1,
947 		  ((bits & TIOCM_RTS) ? CD1400_MSVR1_RTS : 0));
948 	cd_write_reg(cy, CD1400_MSVR2,
949 		((bits & TIOCM_DTR) ? CD1400_MSVR2_DTR : 0));
950 #endif /* CY_HW_RTS */
951         break;
952 
953       case DMBIS: /* set bits */
954 #ifdef CY_HW_RTS
955 	if(!ISSET(cy->cy_tty->t_cflag, CRTSCTS) &&
956 	   (bits & TIOCM_RTS) != 0)
957 	  cd_write_reg(cy, CD1400_MSVR2, CD1400_MSVR2_DTR);
958 	if(bits & TIOCM_DTR)
959 	  cd_write_reg(cy, CD1400_MSVR1, CD1400_MSVR1_RTS);
960 #else
961 	if(!ISSET(cy->cy_tty->t_cflag, CRTSCTS) &&
962 	   (bits & TIOCM_RTS) != 0)
963 	  cd_write_reg(cy, CD1400_MSVR1, CD1400_MSVR1_RTS);
964 	if(bits & TIOCM_DTR)
965 	  cd_write_reg(cy, CD1400_MSVR2, CD1400_MSVR2_DTR);
966 #endif /* CY_HW_RTS */
967 	break;
968 
969       case DMBIC: /* clear bits */
970 #ifdef CY_HW_RTS
971 	if(!ISSET(cy->cy_tty->t_cflag, CRTSCTS) &&
972 	   (bits & TIOCM_RTS))
973 	  cd_write_reg(cy, CD1400_MSVR2, 0);
974 	if(bits & TIOCM_DTR)
975 	  cd_write_reg(cy, CD1400_MSVR1, 0);
976 #else
977 	if(!ISSET(cy->cy_tty->t_cflag, CRTSCTS) &&
978 	   (bits & TIOCM_RTS))
979 	  cd_write_reg(cy, CD1400_MSVR1, 0);
980 	if(bits & TIOCM_DTR)
981 	  cd_write_reg(cy, CD1400_MSVR2, 0);
982 #endif /* CY_HW_RTS */
983 	break;
984     }
985     splx(s);
986     return 0;
987 }
988 
989 /*
990  * Upper-level handler loop (called from timer interrupt?)
991  * This routine is common for multiple cards
992  */
993 void
994 cy_poll(arg)
995      void *arg;
996 {
997     int card, port;
998     struct cy_softc *sc;
999     struct cy_port *cy;
1000     struct tty *tp;
1001     static int counter = 0;
1002 #ifdef CY_DEBUG1
1003     int did_something;
1004 #endif
1005 
1006     int s;
1007 
1008     s = splhigh();
1009 
1010     if(cy_events == 0 && ++counter < 200) {
1011         splx(s);
1012 	goto out;
1013     }
1014 
1015     cy_events = 0;
1016     splx(s);
1017 
1018     for(card = 0; card < cy_cd.cd_ndevs; card++) {
1019 	sc = cy_cd.cd_devs[card];
1020 	if(sc == NULL)
1021 	  continue;
1022 
1023 #ifdef CY_DEBUG1
1024 	sc->sc_poll_count1++;
1025 	did_something = 0;
1026 #endif
1027 
1028 	for(port = 0; port < sc->sc_nports; port++) {
1029 	    cy = &sc->sc_ports[port];
1030 	    if((tp = cy->cy_tty) == NULL || cy->cy_ibuf == NULL ||
1031 	       !ISSET(tp->t_state, TS_ISOPEN | TS_WOPEN))
1032 	    continue;
1033 
1034 	    /*
1035 	     * handle received data
1036 	     */
1037 	    while(cy->cy_ibuf_rd_ptr != cy->cy_ibuf_wr_ptr) {
1038 	        u_char line_stat;
1039 		int chr;
1040 
1041 		line_stat = cy->cy_ibuf_rd_ptr[0];
1042 		chr = cy->cy_ibuf_rd_ptr[1];
1043 
1044 		if(line_stat & (CD1400_RDSR_BREAK|CD1400_RDSR_FE))
1045 		  chr |= TTY_FE;
1046 		if(line_stat & CD1400_RDSR_PE)
1047 		  chr |= TTY_PE;
1048 
1049 		/*
1050 		 * on an overrun error the data is treated as good
1051 		 * just as it should be.
1052 		 */
1053 
1054 #ifdef CY_DEBUG
1055 		printf("cy%d port %d ttyinput 0x%x\n",
1056 		       card, port, chr);
1057 #endif
1058 
1059 		(*linesw[tp->t_line].l_rint)(chr, tp);
1060 
1061                 s = splhigh(); /* really necessary? */
1062 		if((cy->cy_ibuf_rd_ptr += 2) == cy->cy_ibuf_end)
1063 		  cy->cy_ibuf_rd_ptr = cy->cy_ibuf;
1064 		splx(s);
1065 
1066 #ifdef CY_DEBUG1
1067 		did_something = 1;
1068 #endif
1069 	    }
1070 
1071 #ifndef CY_HW_RTS
1072 	    /* If we don't have any received data in ibuf and
1073 	     * CRTSCTS is on and RTS is turned off, it is time
1074 	     * to turn RTS back on
1075 	     */
1076 	    if(ISSET(tp->t_cflag, CRTSCTS)) {
1077 	      /* we can't use cy_modem_control() here as it doesn't
1078 		 change RTS if RTSCTS is on */
1079 	      cd_write_reg(cy, CD1400_CAR, port & CD1400_CAR_CHAN);
1080 
1081 	      if((cd_read_reg(cy, CD1400_MSVR1) & CD1400_MSVR1_RTS) == 0) {
1082 		cd_write_reg(cy, CD1400_MSVR1, CD1400_MSVR1_RTS);
1083 #ifdef CY_DEBUG1
1084 		did_something = 1;
1085 #endif
1086 	      }
1087 	    }
1088 #endif /* CY_HW_RTS */
1089 
1090 	    /*
1091 	     * handle carrier changes
1092 	     */
1093 	    s = splhigh();
1094 	    if(ISSET(cy->cy_flags, CYF_CARRIER_CHANGED)) {
1095 		int carrier;
1096 
1097 		CLR(cy->cy_flags, CYF_CARRIER_CHANGED);
1098 		splx(s);
1099 
1100 		carrier = ((cy->cy_carrier_stat & CD1400_MSVR2_CD) != 0);
1101 
1102 #ifdef CY_DEBUG
1103 		printf("cy_poll: carrier change "
1104 		       "(card %d, port %d, carrier %d)\n",
1105 		       card, port, carrier);
1106 #endif
1107 		if(CY_DIALIN(tp->t_dev) &&
1108 		   !(*linesw[tp->t_line].l_modem)(tp, carrier))
1109 		  cy_modem_control(cy, TIOCM_DTR, DMBIC);
1110 
1111 #ifdef CY_DEBUG1
1112 		did_something = 1;
1113 #endif
1114 	    } else {
1115 		splx(s);
1116 	    }
1117 
1118 	    s = splhigh();
1119 	    if(ISSET(cy->cy_flags, CYF_START)) {
1120 		CLR(cy->cy_flags, CYF_START);
1121               splx(s);
1122 
1123 		(*linesw[tp->t_line].l_start)(tp);
1124 
1125 #ifdef CY_DEBUG1
1126 		did_something = 1;
1127 #endif
1128 	    } else {
1129                 splx(s);
1130 	    }
1131 
1132 	    /* could move this to even upper level... */
1133 	    if(cy->cy_fifo_overruns) {
1134 		cy->cy_fifo_overruns = 0;
1135 		/* doesn't report overrun count,
1136 		   but shouldn't really matter */
1137 		log(LOG_WARNING, "cy%d port %d fifo overrun\n",
1138 		    card, port);
1139 	    }
1140 	    if(cy->cy_ibuf_overruns) {
1141 		cy->cy_ibuf_overruns = 0;
1142 		log(LOG_WARNING, "cy%d port %d ibuf overrun\n",
1143 		    card, port);
1144 	    }
1145 	} /* for(port...) */
1146 #ifdef CY_DEBUG1
1147 	if(did_something && counter >= 200)
1148 	  sc->sc_poll_count2++;
1149 #endif
1150     } /* for(card...) */
1151 
1152     counter = 0;
1153 
1154 out:
1155     timeout_add(&cy_poll_to, 1);
1156 }
1157 
1158 /*
1159  * hardware interrupt routine
1160  */
1161 int
1162 cyintr(arg)
1163      void *arg;
1164 {
1165     struct cy_softc *sc = arg;
1166     struct cy_port *cy;
1167     int card = sc->sc_dev.dv_unit;
1168     int cy_chip, stat;
1169     int int_serviced = 0;
1170 
1171 /*
1172  * Check interrupt status of each CD1400 chip on this card
1173  * (multiple cards cannot share the same interrupt)
1174  */
1175     for(cy_chip = 0; cy_chip < cy_nr_cd1400s[card]; cy_chip++) {
1176 
1177 	stat = cd_read_reg_sc(sc, cy_chip, CD1400_SVRR);
1178 	if(stat == 0)
1179 	  continue;
1180 
1181 	if(ISSET(stat, CD1400_SVRR_RXRDY)) {
1182 	    u_char save_car, save_rir, serv_type;
1183 	    u_char line_stat, recv_data, n_chars;
1184 	    u_char *buf_p;
1185 
1186 	    save_rir = cd_read_reg_sc(sc, cy_chip, CD1400_RIR);
1187 	    save_car = cd_read_reg_sc(sc, cy_chip, CD1400_CAR);
1188 	    /* enter rx service */
1189 	    cd_write_reg_sc(sc, cy_chip, CD1400_CAR, save_rir);
1190 
1191 	    serv_type = cd_read_reg_sc(sc, cy_chip, CD1400_RIVR);
1192 	    cy = &sc->sc_ports[serv_type >> 3];
1193 
1194 #ifdef CY_DEBUG1
1195 	    cy->cy_rx_int_count++;
1196 #endif
1197 
1198 	    if(cy->cy_tty == NULL ||
1199 	       !ISSET(cy->cy_tty->t_state, TS_ISOPEN))
1200 	      goto end_rx_serv;
1201 
1202 	    buf_p = cy->cy_ibuf_wr_ptr;
1203 
1204 	    if(ISSET(serv_type, CD1400_RIVR_EXCEPTION)) {
1205 		line_stat = cd_read_reg(cy, CD1400_RDSR);
1206 		recv_data = cd_read_reg(cy, CD1400_RDSR);
1207 
1208 #ifdef CY_DEBUG
1209 		printf("cy%d port %d recv exception, "
1210 		       "line_stat 0x%x, char 0x%x\n",
1211 		       card, cy->cy_port_num, line_stat, recv_data);
1212 #endif
1213 		if(ISSET(line_stat, CD1400_RDSR_OE))
1214 		  cy->cy_fifo_overruns++;
1215 
1216 		*buf_p++ = line_stat;
1217 		*buf_p++ = recv_data;
1218 		if(buf_p == cy->cy_ibuf_end)
1219 		  buf_p = cy->cy_ibuf;
1220 
1221 		if(buf_p == cy->cy_ibuf_rd_ptr) {
1222 		    if(buf_p == cy->cy_ibuf)
1223 		      buf_p = cy->cy_ibuf_end;
1224 		    buf_p -= 2;
1225 		    cy->cy_ibuf_overruns++;
1226 		}
1227 		cy_events = 1;
1228 	    } else { /* no exception, received data OK */
1229 		n_chars = cd_read_reg(cy, CD1400_RDCR);
1230 #ifdef CY_DEBUG
1231 		printf("cy%d port %d receive ok %d chars\n",
1232 		       card, cy->cy_port_num, n_chars);
1233 #endif
1234 		while(n_chars--) {
1235 		    *buf_p++ = 0; /* status: OK */
1236 		    *buf_p++ =
1237 		      cd_read_reg(cy, CD1400_RDSR); /* data byte */
1238 		    if(buf_p == cy->cy_ibuf_end)
1239 		      buf_p = cy->cy_ibuf;
1240 		    if(buf_p == cy->cy_ibuf_rd_ptr) {
1241 			if(buf_p == cy->cy_ibuf)
1242 			  buf_p = cy->cy_ibuf_end;
1243 			buf_p -= 2;
1244 			cy->cy_ibuf_overruns++;
1245 			break;
1246 		    }
1247 		}
1248 		cy_events = 1;
1249 	    }
1250 
1251 	    cy->cy_ibuf_wr_ptr = buf_p;
1252 
1253 #ifndef CY_HW_RTS
1254 	    /* RTS handshaking for incoming data */
1255 	    if(ISSET(cy->cy_tty->t_cflag, CRTSCTS)) {
1256 		int bf;
1257 
1258 		bf = buf_p - cy->cy_ibuf_rd_ptr;
1259 		if(bf < 0)
1260 		  bf += IBUF_SIZE;
1261 
1262 		if(bf > (IBUF_SIZE/2))  /* turn RTS off */
1263 		  cd_write_reg(cy, CD1400_MSVR1, 0);
1264 	    }
1265 #endif /* CY_HW_RTS */
1266 
1267 end_rx_serv:
1268 	    /* terminate service context */
1269 	    cd_write_reg(cy, CD1400_RIR, save_rir & 0x3f);
1270 	    cd_write_reg(cy, CD1400_CAR, save_car);
1271 	    int_serviced = 1;
1272 	} /* if(rx_service...) */
1273 
1274 	if(ISSET(stat, CD1400_SVRR_MDMCH)) {
1275 	    u_char save_car, save_mir, serv_type, modem_stat;
1276 
1277 	    save_mir = cd_read_reg_sc(sc, cy_chip, CD1400_MIR);
1278 	    save_car = cd_read_reg_sc(sc, cy_chip, CD1400_CAR);
1279 	    /* enter modem service */
1280 	    cd_write_reg_sc(sc, cy_chip, CD1400_CAR, save_mir);
1281 
1282 	    serv_type = cd_read_reg_sc(sc, cy_chip, CD1400_MIVR);
1283 	    cy = &sc->sc_ports[serv_type >> 3];
1284 
1285 #ifdef CY_DEBUG1
1286 	    cy->cy_modem_int_count++;
1287 #endif
1288 
1289 	    modem_stat = cd_read_reg(cy, CD1400_MSVR2);
1290 
1291 #ifdef CY_DEBUG
1292 	    printf("cy%d port %d modem line change, new stat 0x%x\n",
1293 		   card, cy->cy_port_num, modem_stat);
1294 #endif
1295 	    if(ISSET((cy->cy_carrier_stat ^ modem_stat), CD1400_MSVR2_CD)) {
1296 		SET(cy->cy_flags, CYF_CARRIER_CHANGED);
1297 		cy_events = 1;
1298 	    }
1299 
1300 	    cy->cy_carrier_stat = modem_stat;
1301 
1302 	  /* terminate service context */
1303 	    cd_write_reg(cy, CD1400_MIR, save_mir & 0x3f);
1304 	    cd_write_reg(cy, CD1400_CAR, save_car);
1305 	    int_serviced = 1;
1306 	} /* if(modem_service...) */
1307 
1308 	if(ISSET(stat, CD1400_SVRR_TXRDY)) {
1309 	    u_char save_car, save_tir, serv_type, count, ch;
1310 	    struct tty *tp;
1311 
1312 	    save_tir = cd_read_reg_sc(sc, cy_chip, CD1400_TIR);
1313 	    save_car = cd_read_reg_sc(sc, cy_chip, CD1400_CAR);
1314 	    /* enter tx service */
1315 	    cd_write_reg_sc(sc, cy_chip, CD1400_CAR, save_tir);
1316 
1317 	    serv_type = cd_read_reg_sc(sc, cy_chip, CD1400_TIVR);
1318 	    cy = &sc->sc_ports[serv_type >> 3];
1319 
1320 #ifdef CY_DEBUG1
1321 	    cy->cy_tx_int_count++;
1322 #endif
1323 #ifdef CY_DEBUG
1324 	    printf("cy%d port %d tx service\n", card, cy->cy_port_num);
1325 #endif
1326 
1327 	    /* stop transmitting if no tty or CYF_STOP set */
1328 	    tp = cy->cy_tty;
1329 	    if(tp == NULL || ISSET(cy->cy_flags, CYF_STOP))
1330 	      goto txdone;
1331 
1332 	    count = 0;
1333 	    if(ISSET(cy->cy_flags, CYF_SEND_NUL)) {
1334 		cd_write_reg(cy, CD1400_TDR, 0);
1335 		cd_write_reg(cy, CD1400_TDR, 0);
1336 		count += 2;
1337 		CLR(cy->cy_flags, CYF_SEND_NUL);
1338 	    }
1339 
1340 	    if(tp->t_outq.c_cc > 0) {
1341 		SET(tp->t_state, TS_BUSY);
1342 		while(tp->t_outq.c_cc > 0 && count < CD1400_TX_FIFO_SIZE) {
1343 		    ch = getc(&tp->t_outq);
1344 		    /* remember to double NUL characters because
1345 		       embedded transmit commands are enabled */
1346 		    if(ch == 0) {
1347 			if(count >= CD1400_TX_FIFO_SIZE-2) {
1348 			    SET(cy->cy_flags, CYF_SEND_NUL);
1349 			    break;
1350 			}
1351 
1352 			cd_write_reg(cy, CD1400_TDR, ch);
1353 			count++;
1354 		    }
1355 
1356 		    cd_write_reg(cy, CD1400_TDR, ch);
1357 		    count++;
1358 		}
1359 	    } else {
1360 		/* no data to send -- check if we should start/stop a break */
1361 		/* XXX does this cause too much delay before breaks? */
1362 		if(ISSET(cy->cy_flags, CYF_START_BREAK)) {
1363 		    cd_write_reg(cy, CD1400_TDR, 0);
1364 		    cd_write_reg(cy, CD1400_TDR, 0x81);
1365 		    CLR(cy->cy_flags, CYF_START_BREAK);
1366 		}
1367 		if(ISSET(cy->cy_flags, CYF_END_BREAK)) {
1368 		    cd_write_reg(cy, CD1400_TDR, 0);
1369 		    cd_write_reg(cy, CD1400_TDR, 0x83);
1370 		    CLR(cy->cy_flags, CYF_END_BREAK);
1371 		}
1372 	    }
1373 
1374 	    if(tp->t_outq.c_cc == 0) {
1375 txdone:
1376 		/*
1377 		 * No data to send or requested to stop.
1378 		 * Disable transmit interrupt
1379 		 */
1380 		cd_write_reg(cy, CD1400_SRER,
1381 			     cd_read_reg(cy, CD1400_SRER)
1382 			     & ~CD1400_SRER_TXRDY);
1383 		CLR(cy->cy_flags, CYF_STOP);
1384 		CLR(tp->t_state, TS_BUSY);
1385 	    }
1386 
1387 	    if(tp->t_outq.c_cc <= tp->t_lowat) {
1388 		SET(cy->cy_flags, CYF_START);
1389 		cy_events = 1;
1390 	    }
1391 
1392 	    /* terminate service context */
1393 	    cd_write_reg(cy, CD1400_TIR, save_tir & 0x3f);
1394 	    cd_write_reg(cy, CD1400_CAR, save_car);
1395 	    int_serviced = 1;
1396 	} /* if(tx_service...) */
1397     } /* for(...all CD1400s on a card) */
1398 
1399     /* ensure an edge for next interrupt */
1400     bus_space_write_1(sc->sc_memt, sc->sc_memh,
1401 		    CY_CLEAR_INTR<<sc->sc_bustype, 0);
1402     return int_serviced;
1403 }
1404 
1405 /*
1406  * subroutine to enable CD1400 transmitter
1407  */
1408 void
1409 cy_enable_transmitter(cy)
1410      struct cy_port *cy;
1411 {
1412     int s;
1413     s = splhigh();
1414     cd_write_reg(cy, CD1400_CAR, cy->cy_port_num & CD1400_CAR_CHAN);
1415     cd_write_reg(cy, CD1400_SRER, cd_read_reg(cy, CD1400_SRER)
1416 		 | CD1400_SRER_TXRDY);
1417     splx(s);
1418 }
1419 
1420 /*
1421  * Execute a CD1400 channel command
1422  */
1423 void
1424 cd1400_channel_cmd(cy, cmd)
1425      struct cy_port *cy;
1426      int cmd;
1427 {
1428     u_int waitcnt = 5 * 8 * 1024; /* approx 5 ms */
1429 
1430 #ifdef CY_DEBUG
1431     printf("c1400_channel_cmd cy 0x%x command 0x%x\n", cy, cmd);
1432 #endif
1433 
1434     /* wait until cd1400 is ready to process a new command */
1435     while(cd_read_reg(cy, CD1400_CCR) != 0 && waitcnt-- > 0)
1436       ;
1437 
1438     if(waitcnt == 0)
1439       log(LOG_ERR, "cy: channel command timeout\n");
1440 
1441     cd_write_reg(cy, CD1400_CCR, cmd);
1442 }
1443 
1444 /*
1445  * Compute clock option register and baud rate register values
1446  * for a given speed. Return 0 on success, -1 on failure.
1447  *
1448  * The error between requested and actual speed seems
1449  * to be well within allowed limits (less than 3%)
1450  * with every speed value between 50 and 150000 bps.
1451  */
1452 int
1453 cy_speed(speed_t speed, int *cor, int *bpr, int cy_clock)
1454 {
1455     int c, co, br;
1456 
1457     if(speed < 50 || speed > 150000)
1458       return -1;
1459 
1460     for(c = 0, co = 8; co <= 2048; co <<= 2, c++) {
1461 	br = (cy_clock + (co * speed) / 2) / (co * speed);
1462 	if(br < 0x100) {
1463 	    *bpr = br;
1464 	    *cor = c;
1465 	    return 0;
1466 	}
1467     }
1468 
1469     return -1;
1470 }
1471 
1472 #endif /* NCY > 0 */
1473