Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / ic / cy.c
blob8122090dd909398b33fc5b03063c67b835cfa716
1 /* $NetBSD: cy.c,v 1.56 2008/05/25 19:22:21 ad Exp $ */
3 /*
4 * cy.c
6 * Driver for Cyclades Cyclom-8/16/32 multiport serial cards
7 * (currently not tested with Cyclom-32 cards)
9 * Timo Rossi, 1996
11 * Supports both ISA and PCI Cyclom cards
13 * Lots of debug output can be enabled by defining CY_DEBUG
14 * Some debugging counters (number of receive/transmit interrupts etc.)
15 * can be enabled by defining CY_DEBUG1
18 #include <sys/cdefs.h>
19 __KERNEL_RCSID(0, "$NetBSD: cy.c,v 1.56 2008/05/25 19:22:21 ad Exp $");
21 #include <sys/param.h>
22 #include <sys/ioctl.h>
23 #include <sys/syslog.h>
24 #include <sys/fcntl.h>
25 #include <sys/tty.h>
26 #include <sys/proc.h>
27 #include <sys/conf.h>
28 #include <sys/select.h>
29 #include <sys/device.h>
30 #include <sys/malloc.h>
31 #include <sys/systm.h>
32 #include <sys/callout.h>
33 #include <sys/kauth.h>
35 #include <sys/bus.h>
37 #include <dev/ic/cd1400reg.h>
38 #include <dev/ic/cyreg.h>
39 #include <dev/ic/cyvar.h>
41 static int cyparam(struct tty *, struct termios *);
42 static void cystart(struct tty *);
43 static void cy_poll(void *);
44 static int cy_modem_control(struct cy_softc *, struct cy_port *, int, int);
45 static void cy_enable_transmitter(struct cy_softc *, struct cy_port *);
46 static void cd1400_channel_cmd(struct cy_softc *, struct cy_port *, int);
47 static int cy_speed(speed_t, int *, int *, int);
49 extern struct cfdriver cy_cd;
51 static dev_type_open(cyopen);
52 static dev_type_close(cyclose);
53 static dev_type_read(cyread);
54 static dev_type_write(cywrite);
55 static dev_type_ioctl(cyioctl);
56 static dev_type_stop(cystop);
57 static dev_type_tty(cytty);
58 static dev_type_poll(cypoll);
60 const struct cdevsw cy_cdevsw = {
61 .d_open = cyopen,
62 .d_close = cyclose,
63 .d_read = cyread,
64 .d_write = cywrite,
65 .d_ioctl = cyioctl,
66 .d_stop = cystop,
67 .d_tty = cytty,
68 .d_poll = cypoll,
69 .d_mmap = nommap,
70 .d_kqfilter = ttykqfilter,
71 .d_flag = D_TTY
74 static int cy_open = 0;
75 static int cy_events = 0;
77 int cy_attached_ttys;
78 bool cy_callout_init;
79 callout_t cy_poll_callout;
82 * Common probe routine
84 int
85 cy_find(struct cy_softc *sc)
87 int cy_chip, chip;
88 u_char firmware_ver;
89 bus_space_tag_t tag = sc->sc_memt;
90 bus_space_handle_t bsh = sc->sc_bsh;
91 int bustype = sc->sc_bustype;
93 /* Cyclom card hardware reset */
94 bus_space_write_1(tag, bsh, CY16_RESET << bustype, 0);
95 DELAY(500); /* wait for reset to complete */
96 bus_space_write_1(tag, bsh, CY_CLEAR_INTR << bustype, 0);
98 #ifdef CY_DEBUG
99 printf("cy: card reset done\n");
100 #endif
101 sc->sc_nchips = 0;
103 for (cy_chip = 0, chip = 0; cy_chip < CY_MAX_CD1400s;
104 cy_chip++, chip += (CY_CD1400_MEMSPACING << bustype)) {
105 int i;
108 * the last 4 nchips are 'interleaved' with the first 4 on
109 * 32-port boards
111 if (cy_chip == 4)
112 chip -= (CY32_ADDR_FIX << bustype);
114 #ifdef CY_DEBUG
115 printf("sy: probe chip %d offset 0x%x ... ", cy_chip, chip);
116 #endif
118 /* wait until the chip is ready for command */
119 DELAY(1000);
120 if (bus_space_read_1(tag, bsh, chip +
121 ((CD1400_CCR << 1) << bustype)) != 0) {
122 #ifdef CY_DEBUG
123 printf("not ready for command\n");
124 #endif
125 break;
127 /* clear the firmware version reg. */
128 bus_space_write_1(tag, bsh, chip +
129 ((CD1400_GFRCR << 1) << bustype), 0);
132 * On Cyclom-16 references to non-existent chip 4
133 * actually access chip 0 (address line 9 not decoded).
134 * Here we check if the clearing of chip 4 GFRCR actually
135 * cleared chip 0 GFRCR. In that case we have a 16 port card.
137 if (cy_chip == 4 &&
138 bus_space_read_1(tag, bsh, /* off for chip 0 (0) + */
139 ((CD1400_GFRCR << 1) << bustype)) == 0)
140 break;
142 /* reset the chip */
143 bus_space_write_1(tag, bsh, chip +
144 ((CD1400_CCR << 1) << bustype),
145 CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
147 /* wait for the chip to initialize itself */
148 for (i = 0; i < 200; i++) {
149 DELAY(50);
150 firmware_ver = bus_space_read_1(tag, bsh, chip +
151 ((CD1400_GFRCR << 1) << bustype));
152 if ((firmware_ver & 0xf0) == 0x40) /* found a CD1400 */
153 break;
155 #ifdef CY_DEBUG
156 printf("firmware version 0x%x\n", firmware_ver);
157 #endif
159 if ((firmware_ver & 0xf0) != 0x40)
160 break;
162 /* firmware version OK, CD1400 found */
163 sc->sc_nchips++;
166 if (sc->sc_nchips == 0) {
167 #ifdef CY_DEBUG
168 printf("no CD1400s found\n");
169 #endif
170 return 0;
172 #ifdef CY_DEBUG
173 printf("found %d CD1400s\n", sc->sc_nchips);
174 #endif
176 return 1;
179 void
180 cy_attach(struct cy_softc *sc)
182 int port, cy_chip, num_chips, cdu, chip;
183 int cy_clock;
185 if (!cy_callout_init) {
186 cy_callout_init = true;
187 callout_init(&cy_poll_callout, 0);
190 num_chips = sc->sc_nchips;
191 if (num_chips == 0)
192 return;
194 memset(sc->sc_ports, 0, sizeof(sc->sc_ports));
196 port = 0;
197 for (cy_chip = 0, chip = 0; cy_chip < num_chips; cy_chip++,
198 chip += (CY_CD1400_MEMSPACING << sc->sc_bustype)) {
200 if (cy_chip == 4)
201 chip -= (CY32_ADDR_FIX << sc->sc_bustype);
203 #ifdef CY_DEBUG
204 aprint_debug("attach CD1400 #%d offset 0x%x\n", cy_chip, chip);
205 #endif
206 sc->sc_cd1400_offs[cy_chip] = chip;
209 * configure port 0 as serial port (should already be after
210 * reset)
212 cd_write_reg(sc, cy_chip, CD1400_GCR, 0);
214 if (cd_read_reg(sc, cy_chip, CD1400_GFRCR) <= 0x46)
215 cy_clock = CY_CLOCK;
216 else
217 cy_clock = CY_CLOCK_60;
219 /* set up a receive timeout period (1ms) */
220 cd_write_reg(sc, cy_chip, CD1400_PPR,
221 (cy_clock / CD1400_PPR_PRESCALER / 1000) + 1);
223 for (cdu = 0; cdu < CD1400_NO_OF_CHANNELS; cdu++) {
224 sc->sc_ports[port].cy_softc = sc;
225 sc->sc_ports[port].cy_port_num = port;
226 sc->sc_ports[port].cy_chip = cy_chip;
227 sc->sc_ports[port].cy_clock = cy_clock;
229 /* should we initialize anything else here? */
230 port++;
231 } /* for(each port on one CD1400...) */
233 } /* for(each CD1400 on a card... ) */
235 sc->sc_nchannels = port;
237 aprint_normal_dev(sc->sc_dev, "%d channels (ttyCY%03d..ttyCY%03d)\n",
238 sc->sc_nchannels, cy_attached_ttys,
239 cy_attached_ttys + (sc->sc_nchannels - 1));
241 cy_attached_ttys += sc->sc_nchannels;
243 /* ensure an edge for the next interrupt */
244 bus_space_write_1(sc->sc_memt, sc->sc_bsh,
245 CY_CLEAR_INTR << sc->sc_bustype, 0);
248 #define CYDIALOUT_MASK 0x80000
249 #define CY_DIALOUT(dev) (minor(dev) & CYDIALOUT_MASK)
251 #define CY_PORT(dev) cy_getport((dev))
252 #define CY_BOARD(cy) ((cy)->cy_softc)
254 static struct cy_port *
255 cy_getport(dev_t dev)
257 int i, j, k, u = minor(dev) & ~CYDIALOUT_MASK;
258 struct cy_softc *sc;
260 for (i = 0, j = 0; i < cy_cd.cd_ndevs; i++) {
261 k = j;
262 sc = device_lookup_private(&cy_cd, i);
263 if (sc == NULL)
264 continue;
265 if (sc->sc_nchannels == 0)
266 continue;
267 j += sc->sc_nchannels;
268 if (j > u)
269 return (&sc->sc_ports[u - k]);
272 return (NULL);
276 * open routine. returns zero if successful, else error code
279 cyopen(dev_t dev, int flag, int mode, struct lwp *l)
281 struct cy_softc *sc;
282 struct cy_port *cy;
283 struct tty *tp;
284 int s, error;
286 cy = CY_PORT(dev);
287 if (cy == NULL)
288 return (ENXIO);
289 sc = CY_BOARD(cy);
291 s = spltty();
292 if (cy->cy_tty == NULL) {
293 if ((cy->cy_tty = ttymalloc()) == NULL) {
294 splx(s);
295 aprint_error_dev(sc->sc_dev,
296 "port %d: can't allocate tty\n",
297 cy->cy_port_num);
298 return (ENOMEM);
300 tty_attach(cy->cy_tty);
302 splx(s);
304 tp = cy->cy_tty;
305 tp->t_oproc = cystart;
306 tp->t_param = cyparam;
307 tp->t_dev = dev;
309 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
310 return (EBUSY);
312 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
313 ttychars(tp);
314 tp->t_iflag = TTYDEF_IFLAG;
315 tp->t_oflag = TTYDEF_OFLAG;
316 tp->t_cflag = TTYDEF_CFLAG;
317 if (ISSET(cy->cy_openflags, TIOCFLAG_CLOCAL))
318 SET(tp->t_cflag, CLOCAL);
319 if (ISSET(cy->cy_openflags, TIOCFLAG_CRTSCTS))
320 SET(tp->t_cflag, CRTSCTS);
321 if (ISSET(cy->cy_openflags, TIOCFLAG_MDMBUF))
322 SET(tp->t_cflag, MDMBUF);
323 tp->t_lflag = TTYDEF_LFLAG;
324 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
326 s = spltty();
329 * Allocate input ring buffer if we don't already have one
331 if (cy->cy_ibuf == NULL) {
332 cy->cy_ibuf = malloc(CY_IBUF_SIZE, M_DEVBUF, M_NOWAIT);
333 if (cy->cy_ibuf == NULL) {
334 aprint_error_dev(sc->sc_dev,
335 "port %d: can't allocate input buffer\n",
336 cy->cy_port_num);
337 splx(s);
338 return ENOMEM;
340 cy->cy_ibuf_end = cy->cy_ibuf + CY_IBUF_SIZE;
342 /* mark the ring buffer as empty */
343 cy->cy_ibuf_rd_ptr = cy->cy_ibuf_wr_ptr = cy->cy_ibuf;
345 /* select CD1400 channel */
346 cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
347 cy->cy_port_num & CD1400_CAR_CHAN);
348 /* reset the channel */
349 cd1400_channel_cmd(sc, cy, CD1400_CCR_CMDRESET);
350 /* encode unit (port) number in LIVR */
351 /* there is just enough space for 5 bits (32 ports) */
352 cd_write_reg(sc, cy->cy_chip, CD1400_LIVR,
353 cy->cy_port_num << 3);
355 cy->cy_channel_control = 0;
357 /* hmm... need spltty() here? */
358 if (cy_open == 0) {
359 cy_open = 1;
360 callout_reset(&cy_poll_callout, 1, cy_poll, NULL);
362 /* this sets parameters and raises DTR */
363 cyparam(tp, &tp->t_termios);
365 ttsetwater(tp);
367 /* raise RTS too */
368 cy_modem_control(sc, cy, TIOCM_RTS, DMBIS);
370 cy->cy_carrier_stat =
371 cd_read_reg(sc, cy->cy_chip, CD1400_MSVR2);
373 /* enable receiver and modem change interrupts */
374 cd_write_reg(sc, cy->cy_chip, CD1400_SRER,
375 CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
377 if (CY_DIALOUT(dev) ||
378 ISSET(cy->cy_openflags, TIOCFLAG_SOFTCAR) ||
379 ISSET(tp->t_cflag, MDMBUF) ||
380 ISSET(cy->cy_carrier_stat, CD1400_MSVR2_CD))
381 SET(tp->t_state, TS_CARR_ON);
382 else
383 CLR(tp->t_state, TS_CARR_ON);
384 splx(s);
387 /* wait for carrier if necessary */
388 if (!ISSET(flag, O_NONBLOCK)) {
389 mutex_spin_enter(&tty_lock);
390 while (!ISSET(tp->t_cflag, CLOCAL) &&
391 !ISSET(tp->t_state, TS_CARR_ON)) {
392 tp->t_wopen++;
393 error = ttysleep(tp, &tp->t_rawcv, true, 0);
394 tp->t_wopen--;
395 if (error != 0) {
396 mutex_spin_exit(&tty_lock);
397 return error;
400 mutex_spin_exit(&tty_lock);
403 return (*tp->t_linesw->l_open) (dev, tp);
407 * close routine. returns zero if successful, else error code
410 cyclose(dev_t dev, int flag, int mode, struct lwp *l)
412 struct cy_softc *sc;
413 struct cy_port *cy;
414 struct tty *tp;
415 int s;
417 cy = CY_PORT(dev);
418 sc = CY_BOARD(cy);
419 tp = cy->cy_tty;
421 (*tp->t_linesw->l_close) (tp, flag);
422 s = spltty();
424 if (ISSET(tp->t_cflag, HUPCL) &&
425 !ISSET(cy->cy_openflags, TIOCFLAG_SOFTCAR)) {
427 * drop DTR and RTS (should we wait for output buffer to
428 * become empty first?)
430 cy_modem_control(sc, cy, 0, DMSET);
433 * XXX should we disable modem change and
434 * receive interrupts here or somewhere ?
436 CLR(tp->t_state, TS_BUSY | TS_FLUSH);
438 splx(s);
439 ttyclose(tp);
441 return 0;
445 * Read routine
448 cyread(dev_t dev, struct uio *uio, int flag)
450 struct cy_port *cy;
451 struct tty *tp;
453 cy = CY_PORT(dev);
454 tp = cy->cy_tty;
456 return ((*tp->t_linesw->l_read)(tp, uio, flag));
460 * Write routine
463 cywrite(dev_t dev, struct uio *uio, int flag)
465 struct cy_port *cy;
466 struct tty *tp;
468 cy = CY_PORT(dev);
469 tp = cy->cy_tty;
471 return ((*tp->t_linesw->l_write)(tp, uio, flag));
475 * Poll routine
478 cypoll(dev_t dev, int events, struct lwp *l)
480 struct cy_port *cy;
481 struct tty *tp;
483 cy = CY_PORT(dev);
484 tp = cy->cy_tty;
486 return ((*tp->t_linesw->l_poll)(tp, events, l));
490 * return tty pointer
492 struct tty *
493 cytty(dev_t dev)
495 struct cy_port *cy;
497 cy = CY_PORT(dev);
499 return (cy->cy_tty);
503 * ioctl routine
506 cyioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
508 struct cy_softc *sc;
509 struct cy_port *cy;
510 struct tty *tp;
511 int error;
513 cy = CY_PORT(dev);
514 sc = CY_BOARD(cy);
515 tp = cy->cy_tty;
517 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
518 if (error != EPASSTHROUGH)
519 return error;
521 error = ttioctl(tp, cmd, data, flag, l);
522 if (error != EPASSTHROUGH)
523 return error;
525 /* XXX should not allow dropping DTR when dialin? */
527 switch (cmd) {
528 case TIOCSBRK: /* start break */
529 SET(cy->cy_flags, CY_F_START_BREAK);
530 cy_enable_transmitter(sc, cy);
531 break;
533 case TIOCCBRK: /* stop break */
534 SET(cy->cy_flags, CY_F_END_BREAK);
535 cy_enable_transmitter(sc, cy);
536 break;
538 case TIOCSDTR: /* DTR on */
539 cy_modem_control(sc, cy, TIOCM_DTR, DMBIS);
540 break;
542 case TIOCCDTR: /* DTR off */
543 cy_modem_control(sc, cy, TIOCM_DTR, DMBIC);
544 break;
546 case TIOCMSET: /* set new modem control line values */
547 cy_modem_control(sc, cy, *((int *) data), DMSET);
548 break;
550 case TIOCMBIS: /* turn modem control bits on */
551 cy_modem_control(sc, cy, *((int *) data), DMBIS);
552 break;
554 case TIOCMBIC: /* turn modem control bits off */
555 cy_modem_control(sc, cy, *((int *) data), DMBIC);
556 break;
558 case TIOCMGET: /* get modem control/status line state */
559 *((int *) data) = cy_modem_control(sc, cy, 0, DMGET);
560 break;
562 case TIOCGFLAGS:
563 *((int *) data) = cy->cy_openflags |
564 (CY_DIALOUT(dev) ? TIOCFLAG_SOFTCAR : 0);
565 break;
567 case TIOCSFLAGS:
568 error = kauth_authorize_device_tty(l->l_cred,
569 KAUTH_DEVICE_TTY_PRIVSET, tp);
570 if (error != 0)
571 return EPERM;
573 cy->cy_openflags = *((int *) data) &
574 (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
575 TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
576 break;
578 default:
579 return EPASSTHROUGH;
582 return 0;
586 * start output
588 void
589 cystart(struct tty *tp)
591 struct cy_softc *sc;
592 struct cy_port *cy;
593 int s;
595 cy = CY_PORT(tp->t_dev);
596 sc = cy->cy_softc;
598 s = spltty();
600 #ifdef CY_DEBUG1
601 cy->cy_start_count++;
602 #endif
604 if (!ISSET(tp->t_state, TS_TTSTOP | TS_TIMEOUT | TS_BUSY)) {
605 if (!ttypull(tp))
606 goto out;
607 SET(tp->t_state, TS_BUSY);
608 cy_enable_transmitter(sc, cy);
610 out:
612 splx(s);
616 * stop output
618 void
619 cystop(struct tty *tp, int flag)
621 struct cy_port *cy;
622 int s;
624 cy = CY_PORT(tp->t_dev);
626 s = spltty();
627 if (ISSET(tp->t_state, TS_BUSY)) {
628 if (!ISSET(tp->t_state, TS_TTSTOP))
629 SET(tp->t_state, TS_FLUSH);
632 * the transmit interrupt routine will disable transmit when it
633 * notices that CY_F_STOP has been set.
635 SET(cy->cy_flags, CY_F_STOP);
637 splx(s);
641 * parameter setting routine.
642 * returns 0 if successful, else returns error code
645 cyparam(struct tty *tp, struct termios *t)
647 struct cy_softc *sc;
648 struct cy_port *cy;
649 int ibpr = 0, obpr = 0, i_clk_opt = 0, o_clk_opt = 0; /* XXX: GCC */
650 int s, opt;
652 cy = CY_PORT(tp->t_dev);
653 sc = CY_BOARD(cy);
655 if (t->c_ospeed != 0 && cy_speed(t->c_ospeed, &o_clk_opt, &obpr, cy->cy_clock) < 0)
656 return EINVAL;
658 if (t->c_ispeed != 0 && cy_speed(t->c_ispeed, &i_clk_opt, &ibpr, cy->cy_clock) < 0)
659 return EINVAL;
661 s = spltty();
663 /* hang up the line is ospeed is zero, else turn DTR on */
664 cy_modem_control(sc, cy, TIOCM_DTR, (t->c_ospeed == 0 ? DMBIC : DMBIS));
666 /* channel was selected by the above call to cy_modem_control() */
667 #if 0
668 cd_write_reg(sc, cy->cy_chip, CD1400_CAR, port & CD1400_CAR_CHAN);
669 #endif
671 /* set transmit speed */
672 if (t->c_ospeed != 0) {
673 cd_write_reg(sc, cy->cy_chip, CD1400_TCOR, o_clk_opt);
674 cd_write_reg(sc, cy->cy_chip, CD1400_TBPR, obpr);
676 /* set receive speed */
677 if (t->c_ispeed != 0) {
678 cd_write_reg(sc, cy->cy_chip, CD1400_RCOR, i_clk_opt);
679 cd_write_reg(sc, cy->cy_chip, CD1400_RBPR, ibpr);
681 opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN
682 | (ISSET(t->c_cflag, CREAD) ? CD1400_CCR_RCVEN : CD1400_CCR_RCVDIS);
684 if (opt != cy->cy_channel_control) {
685 cy->cy_channel_control = opt;
686 cd1400_channel_cmd(sc, cy, opt);
688 /* compute COR1 contents */
689 opt = 0;
690 if (ISSET(t->c_cflag, PARENB)) {
691 if (ISSET(t->c_cflag, PARODD))
692 opt |= CD1400_COR1_PARODD;
693 opt |= CD1400_COR1_PARNORMAL;
695 if (!ISSET(t->c_iflag, INPCK))
696 opt |= CD1400_COR1_NOINPCK; /* no parity checking */
698 if (ISSET(t->c_cflag, CSTOPB))
699 opt |= CD1400_COR1_STOP2;
701 switch (t->c_cflag & CSIZE) {
702 case CS5:
703 opt |= CD1400_COR1_CS5;
704 break;
706 case CS6:
707 opt |= CD1400_COR1_CS6;
708 break;
710 case CS7:
711 opt |= CD1400_COR1_CS7;
712 break;
714 default:
715 opt |= CD1400_COR1_CS8;
716 break;
719 cd_write_reg(sc, cy->cy_chip, CD1400_COR1, opt);
721 #ifdef CY_DEBUG
722 printf("cor1 = 0x%x...", opt);
723 #endif
726 * use the CD1400 automatic CTS flow control if CRTSCTS is set
728 * CD1400_COR2_ETC is used because breaks are generated with
729 * embedded transmit commands
731 cd_write_reg(sc, cy->cy_chip, CD1400_COR2,
732 CD1400_COR2_ETC |
733 (ISSET(t->c_cflag, CRTSCTS) ? CD1400_COR2_CCTS_OFLOW : 0));
735 cd_write_reg(sc, cy->cy_chip, CD1400_COR3, CY_RX_FIFO_THRESHOLD);
737 cd1400_channel_cmd(sc, cy, CD1400_CCR_CMDCORCHG |
738 CD1400_CCR_COR1 | CD1400_CCR_COR2 | CD1400_CCR_COR3);
740 cd_write_reg(sc, cy->cy_chip, CD1400_COR4, CD1400_COR4_PFO_EXCEPTION);
741 cd_write_reg(sc, cy->cy_chip, CD1400_COR5, 0);
744 * set modem change option registers to generate interrupts
745 * on carrier detect changes.
747 * if hardware RTS handshaking is used
748 * also set the handshaking threshold.
750 if (cy->cy_clock == CY_CLOCK_60) {
751 cd_write_reg(sc, cy->cy_chip, CD1400_MCOR1, CD1400_MCOR1_CDzd |
752 (ISSET(t->c_cflag, CRTSCTS) ? CY_RX_DTR_THRESHOLD : 0));
753 } else {
754 cd_write_reg(sc, cy->cy_chip, CD1400_MCOR1, CD1400_MCOR1_CDzd);
757 cd_write_reg(sc, cy->cy_chip, CD1400_MCOR2, CD1400_MCOR2_CDod);
760 * set receive timeout to approx. 2ms
761 * could use more complex logic here...
762 * (but is it actually needed or even useful?)
764 cd_write_reg(sc, cy->cy_chip, CD1400_RTPR, 2);
767 * should do anything else here?
768 * XXX check MDMBUF handshaking like in com.c?
771 splx(s);
772 return 0;
776 * set/get modem line status
778 * bits can be: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, TIOCM_CD, TIOCM_RI, TIOCM_DSR
781 cy_modem_control(struct cy_softc *sc, struct cy_port *cy, int bits, int howto)
783 struct tty *tp = cy->cy_tty;
784 int s, msvr;
786 s = spltty();
788 /* select channel */
789 cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
790 cy->cy_port_num & CD1400_CAR_CHAN);
792 /* Does not manipulate RTS if it is used for flow control. */
793 switch (howto) {
794 case DMGET:
795 bits = 0;
796 if (cy->cy_channel_control & CD1400_CCR_RCVEN)
797 bits |= TIOCM_LE;
798 msvr = cd_read_reg(sc, cy->cy_chip, CD1400_MSVR2);
799 if (cy->cy_clock == CY_CLOCK_60) {
800 if (cd_read_reg(sc, cy->cy_chip, CD1400_MSVR1) &
801 CD1400_MSVR1_RTS)
802 bits |= TIOCM_DTR;
803 if (msvr & CD1400_MSVR2_DTR)
804 bits |= TIOCM_RTS;
805 } else {
806 if (cd_read_reg(sc, cy->cy_chip, CD1400_MSVR1) &
807 CD1400_MSVR1_RTS)
808 bits |= TIOCM_RTS;
809 if (msvr & CD1400_MSVR2_DTR)
810 bits |= TIOCM_DTR;
812 if (msvr & CD1400_MSVR2_CTS)
813 bits |= TIOCM_CTS;
814 if (msvr & CD1400_MSVR2_CD)
815 bits |= TIOCM_CD;
816 /* Not connected on some Cyclom-Y boards? */
817 if (msvr & CD1400_MSVR2_DSR)
818 bits |= TIOCM_DSR;
819 /* Not connected on some Cyclom-8Y boards? */
820 if (msvr & CD1400_MSVR2_RI)
821 bits |= TIOCM_RI;
822 break;
824 case DMSET: /* replace old values with new ones */
825 if (cy->cy_clock == CY_CLOCK_60) {
826 if (!ISSET(tp->t_cflag, CRTSCTS))
827 cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
828 ((bits & TIOCM_RTS) ? CD1400_MSVR2_DTR : 0));
829 cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
830 ((bits & TIOCM_DTR) ? CD1400_MSVR1_RTS : 0));
831 } else {
832 if (!ISSET(tp->t_cflag, CRTSCTS))
833 cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
834 ((bits & TIOCM_RTS) ? CD1400_MSVR1_RTS : 0));
835 cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
836 ((bits & TIOCM_DTR) ? CD1400_MSVR2_DTR : 0));
838 break;
840 case DMBIS: /* set bits */
841 if (cy->cy_clock == CY_CLOCK_60) {
842 if (!ISSET(tp->t_cflag, CRTSCTS) &&
843 (bits & TIOCM_RTS) != 0)
844 cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
845 CD1400_MSVR2_DTR);
846 if (bits & TIOCM_DTR)
847 cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
848 CD1400_MSVR1_RTS);
849 } else {
850 if (!ISSET(tp->t_cflag, CRTSCTS) &&
851 (bits & TIOCM_RTS) != 0)
852 cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
853 CD1400_MSVR1_RTS);
854 if (bits & TIOCM_DTR)
855 cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
856 CD1400_MSVR2_DTR);
858 break;
860 case DMBIC: /* clear bits */
861 if (cy->cy_clock == CY_CLOCK_60) {
862 if (!ISSET(tp->t_cflag, CRTSCTS) && (bits & TIOCM_RTS))
863 cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2, 0);
864 if (bits & TIOCM_DTR)
865 cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1, 0);
866 } else {
867 if (!ISSET(tp->t_cflag, CRTSCTS) && (bits & TIOCM_RTS))
868 cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1, 0);
869 if (bits & TIOCM_DTR)
870 cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2, 0);
872 break;
874 splx(s);
875 return ((howto == DMGET) ? bits : 0);
879 * Upper-level handler loop (called from timer interrupt?)
880 * This routine is common for multiple cards
882 void
883 cy_poll(void *arg)
885 int card, port;
886 struct cy_softc *sc;
887 struct cy_port *cy;
888 struct tty *tp;
889 static int counter = 0;
890 #ifdef CY_DEBUG1
891 int did_something;
892 #endif
893 int s = spltty();
895 if (cy_events == 0 && ++counter < 200) {
896 splx(s);
897 goto out;
899 cy_events = 0;
900 splx(s);
902 for (card = 0; card < cy_cd.cd_ndevs; card++) {
903 sc = device_lookup_private(&cy_cd, card);
904 if (sc == NULL)
905 continue;
907 #ifdef CY_DEBUG1
908 sc->sc_poll_count1++;
909 did_something = 0;
910 #endif
912 for (port = 0; port < sc->sc_nchannels; port++) {
913 cy = &sc->sc_ports[port];
914 if ((tp = cy->cy_tty) == NULL || cy->cy_ibuf == NULL ||
915 (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0))
916 continue;
919 * handle received data
921 while (cy->cy_ibuf_rd_ptr != cy->cy_ibuf_wr_ptr) {
922 u_char line_stat;
923 int chr;
925 line_stat = cy->cy_ibuf_rd_ptr[0];
926 chr = cy->cy_ibuf_rd_ptr[1];
928 if (line_stat &
929 (CD1400_RDSR_BREAK | CD1400_RDSR_FE))
930 chr |= TTY_FE;
931 if (line_stat & CD1400_RDSR_PE)
932 chr |= TTY_PE;
935 * on an overrun error the data is treated as
936 * good just as it should be.
939 #ifdef CY_DEBUG
940 aprint_debug_dev(sc->sc_dev,
941 "port %d ttyinput 0x%x\n",
942 port, chr);
943 #endif
945 (*tp->t_linesw->l_rint) (chr, tp);
947 s = spltty(); /* really necessary? */
948 if ((cy->cy_ibuf_rd_ptr += 2) ==
949 cy->cy_ibuf_end)
950 cy->cy_ibuf_rd_ptr = cy->cy_ibuf;
951 splx(s);
953 #ifdef CY_DEBUG1
954 did_something = 1;
955 #endif
959 * If we don't have any received data in ibuf and
960 * CRTSCTS is on and RTS is turned off, it is time to
961 * turn RTS back on
963 if (ISSET(tp->t_cflag, CRTSCTS)) {
965 * we can't use cy_modem_control() here as it
966 * doesn't change RTS if RTSCTS is on
968 cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
969 port & CD1400_CAR_CHAN);
971 if (cy->cy_clock == CY_CLOCK_60) {
972 if ((cd_read_reg(sc, cy->cy_chip,
973 CD1400_MSVR2) & CD1400_MSVR2_DTR) == 0) {
974 cd_write_reg(sc, cy->cy_chip,
975 CD1400_MSVR2,CD1400_MSVR2_DTR);
976 #ifdef CY_DEBUG1
977 did_something = 1;
978 #endif
980 } else {
981 if ((cd_read_reg(sc, cy->cy_chip,
982 CD1400_MSVR1) & CD1400_MSVR1_RTS) == 0) {
983 cd_write_reg(sc, cy->cy_chip,
984 CD1400_MSVR1,CD1400_MSVR1_RTS);
985 #ifdef CY_DEBUG1
986 did_something = 1;
987 #endif
993 * handle carrier changes
995 s = spltty();
996 if (ISSET(cy->cy_flags, CY_F_CARRIER_CHANGED)) {
997 int carrier;
999 CLR(cy->cy_flags, CY_F_CARRIER_CHANGED);
1000 splx(s);
1002 carrier = ((cy->cy_carrier_stat &
1003 CD1400_MSVR2_CD) != 0);
1005 #ifdef CY_DEBUG
1006 printf("cy_poll: carrier change "
1007 "(card %d, port %d, carrier %d)\n",
1008 card, port, carrier);
1009 #endif
1010 if (CY_DIALOUT(tp->t_dev) == 0 &&
1011 !(*tp->t_linesw->l_modem)(tp, carrier))
1012 cy_modem_control(sc, cy,
1013 TIOCM_DTR, DMBIC);
1015 #ifdef CY_DEBUG1
1016 did_something = 1;
1017 #endif
1018 } else
1019 splx(s);
1021 s = spltty();
1022 if (ISSET(cy->cy_flags, CY_F_START)) {
1023 CLR(cy->cy_flags, CY_F_START);
1024 splx(s);
1026 (*tp->t_linesw->l_start) (tp);
1028 #ifdef CY_DEBUG1
1029 did_something = 1;
1030 #endif
1031 } else
1032 splx(s);
1034 /* could move this to even upper level... */
1035 if (cy->cy_fifo_overruns) {
1036 cy->cy_fifo_overruns = 0;
1038 * doesn't report overrun count, but
1039 * shouldn't really matter
1041 log(LOG_WARNING, "%s: port %d fifo overrun\n",
1042 device_xname(sc->sc_dev), port);
1044 if (cy->cy_ibuf_overruns) {
1045 cy->cy_ibuf_overruns = 0;
1046 log(LOG_WARNING, "%s: port %d ibuf overrun\n",
1047 device_xname(sc->sc_dev), port);
1049 } /* for(port...) */
1050 #ifdef CY_DEBUG1
1051 if (did_something && counter >= 200)
1052 sc->sc_poll_count2++;
1053 #endif
1054 } /* for(card...) */
1056 counter = 0;
1058 out:
1059 callout_reset(&cy_poll_callout, 1, cy_poll, NULL);
1063 * hardware interrupt routine
1066 cy_intr(void *arg)
1068 struct cy_softc *sc = arg;
1069 struct cy_port *cy;
1070 int cy_chip, stat;
1071 int int_serviced = 0;
1074 * Check interrupt status of each CD1400 chip on this card
1075 * (multiple cards cannot share the same interrupt)
1077 for (cy_chip = 0; cy_chip < sc->sc_nchips; cy_chip++) {
1079 stat = cd_read_reg(sc, cy_chip, CD1400_SVRR);
1080 if (stat == 0)
1081 continue;
1083 if (ISSET(stat, CD1400_SVRR_RXRDY)) {
1084 u_char save_car, save_rir, serv_type;
1085 u_char line_stat, recv_data, n_chars;
1086 u_char *buf_p;
1088 save_rir = cd_read_reg(sc, cy_chip, CD1400_RIR);
1089 save_car = cd_read_reg(sc, cy_chip, CD1400_CAR);
1090 /* enter rx service */
1091 cd_write_reg(sc, cy_chip, CD1400_CAR, save_rir);
1093 serv_type = cd_read_reg(sc, cy_chip, CD1400_RIVR);
1094 cy = &sc->sc_ports[serv_type >> 3];
1096 #ifdef CY_DEBUG1
1097 cy->cy_rx_int_count++;
1098 #endif
1100 buf_p = cy->cy_ibuf_wr_ptr;
1102 if (ISSET(serv_type, CD1400_RIVR_EXCEPTION)) {
1103 line_stat = cd_read_reg(sc, cy->cy_chip,
1104 CD1400_RDSR);
1105 recv_data = cd_read_reg(sc, cy->cy_chip,
1106 CD1400_RDSR);
1108 if (cy->cy_tty == NULL ||
1109 !ISSET(cy->cy_tty->t_state, TS_ISOPEN))
1110 goto end_rx_serv;
1112 #ifdef CY_DEBUG
1113 aprint_debug_dev(
1114 sc->sc_dev,
1115 "port %d recv exception, "
1116 "line_stat 0x%x, char 0x%x\n",
1117 cy->cy_port_num, line_stat, recv_data);
1118 #endif
1119 if (ISSET(line_stat, CD1400_RDSR_OE))
1120 cy->cy_fifo_overruns++;
1122 *buf_p++ = line_stat;
1123 *buf_p++ = recv_data;
1124 if (buf_p == cy->cy_ibuf_end)
1125 buf_p = cy->cy_ibuf;
1127 if (buf_p == cy->cy_ibuf_rd_ptr) {
1128 if (buf_p == cy->cy_ibuf)
1129 buf_p = cy->cy_ibuf_end;
1130 buf_p -= 2;
1131 cy->cy_ibuf_overruns++;
1133 cy_events = 1;
1134 } else {/* no exception, received data OK */
1135 n_chars = cd_read_reg(sc, cy->cy_chip,
1136 CD1400_RDCR);
1138 /* If no tty or not open, discard data */
1139 if (cy->cy_tty == NULL ||
1140 !ISSET(cy->cy_tty->t_state, TS_ISOPEN)) {
1141 while (n_chars--)
1142 (void)cd_read_reg(sc,
1143 cy->cy_chip, CD1400_RDSR);
1144 goto end_rx_serv;
1147 #ifdef CY_DEBUG
1148 aprint_debug_dev(sc->sc_dev,
1149 "port %d receive ok %d chars\n",
1150 cy->cy_port_num, n_chars);
1151 #endif
1152 while (n_chars--) {
1153 *buf_p++ = 0; /* status: OK */
1154 /* data byte */
1155 *buf_p++ = cd_read_reg(sc,
1156 cy->cy_chip, CD1400_RDSR);
1157 if (buf_p == cy->cy_ibuf_end)
1158 buf_p = cy->cy_ibuf;
1159 if (buf_p == cy->cy_ibuf_rd_ptr) {
1160 if (buf_p == cy->cy_ibuf)
1161 buf_p = cy->cy_ibuf_end;
1162 buf_p -= 2;
1163 cy->cy_ibuf_overruns++;
1164 break;
1167 cy_events = 1;
1170 cy->cy_ibuf_wr_ptr = buf_p;
1172 /* RTS handshaking for incoming data */
1173 if (ISSET(cy->cy_tty->t_cflag, CRTSCTS)) {
1174 int bf, msvr;
1176 bf = buf_p - cy->cy_ibuf_rd_ptr;
1177 if (bf < 0)
1178 bf += CY_IBUF_SIZE;
1180 if (bf > (CY_IBUF_SIZE / 2)) {
1181 /* turn RTS off */
1182 if (cy->cy_clock == CY_CLOCK_60)
1183 msvr = CD1400_MSVR2;
1184 else
1185 msvr = CD1400_MSVR1;
1186 cd_write_reg(sc, cy->cy_chip, msvr, 0);
1190 end_rx_serv:
1191 /* terminate service context */
1192 cd_write_reg(sc, cy->cy_chip, CD1400_RIR,
1193 save_rir & 0x3f);
1194 cd_write_reg(sc, cy->cy_chip, CD1400_CAR, save_car);
1195 int_serviced = 1;
1196 } /* if (rx_service...) */
1197 if (ISSET(stat, CD1400_SVRR_MDMCH)) {
1198 u_char save_car, save_mir, serv_type, modem_stat;
1200 save_mir = cd_read_reg(sc, cy_chip, CD1400_MIR);
1201 save_car = cd_read_reg(sc, cy_chip, CD1400_CAR);
1202 /* enter modem service */
1203 cd_write_reg(sc, cy_chip, CD1400_CAR, save_mir);
1205 serv_type = cd_read_reg(sc, cy_chip, CD1400_MIVR);
1206 cy = &sc->sc_ports[serv_type >> 3];
1208 #ifdef CY_DEBUG1
1209 cy->cy_modem_int_count++;
1210 #endif
1212 modem_stat = cd_read_reg(sc, cy->cy_chip, CD1400_MSVR2);
1214 #ifdef CY_DEBUG
1215 aprint_debug_dev(sc->sc_dev,
1216 "port %d modem line change, new stat 0x%x\n",
1217 cy->cy_port_num, modem_stat);
1218 #endif
1219 if (ISSET((cy->cy_carrier_stat ^ modem_stat), CD1400_MSVR2_CD)) {
1220 SET(cy->cy_flags, CY_F_CARRIER_CHANGED);
1221 cy_events = 1;
1223 cy->cy_carrier_stat = modem_stat;
1225 /* terminate service context */
1226 cd_write_reg(sc, cy->cy_chip, CD1400_MIR, save_mir & 0x3f);
1227 cd_write_reg(sc, cy->cy_chip, CD1400_CAR, save_car);
1228 int_serviced = 1;
1229 } /* if (modem_service...) */
1230 if (ISSET(stat, CD1400_SVRR_TXRDY)) {
1231 u_char save_car, save_tir, serv_type,
1232 count, ch;
1233 struct tty *tp;
1235 save_tir = cd_read_reg(sc, cy_chip, CD1400_TIR);
1236 save_car = cd_read_reg(sc, cy_chip, CD1400_CAR);
1237 /* enter tx service */
1238 cd_write_reg(sc, cy_chip, CD1400_CAR, save_tir);
1240 serv_type = cd_read_reg(sc, cy_chip, CD1400_TIVR);
1241 cy = &sc->sc_ports[serv_type >> 3];
1243 #ifdef CY_DEBUG1
1244 cy->cy_tx_int_count++;
1245 #endif
1246 #ifdef CY_DEBUG
1247 aprint_debug_dev(sc->sc_dev, "port %d tx service\n",
1248 cy->cy_port_num);
1249 #endif
1251 /* stop transmitting if no tty or CY_F_STOP set */
1252 tp = cy->cy_tty;
1253 if (tp == NULL || ISSET(cy->cy_flags, CY_F_STOP))
1254 goto txdone;
1256 count = 0;
1257 if (ISSET(cy->cy_flags, CY_F_SEND_NUL)) {
1258 cd_write_reg(sc, cy->cy_chip, CD1400_TDR, 0);
1259 cd_write_reg(sc, cy->cy_chip, CD1400_TDR, 0);
1260 count += 2;
1261 CLR(cy->cy_flags, CY_F_SEND_NUL);
1263 if (tp->t_outq.c_cc > 0) {
1264 SET(tp->t_state, TS_BUSY);
1265 while (tp->t_outq.c_cc > 0 &&
1266 count < CD1400_TX_FIFO_SIZE) {
1267 ch = getc(&tp->t_outq);
1269 * remember to double NUL characters
1270 * because embedded transmit commands
1271 * are enabled
1273 if (ch == 0) {
1274 if (count >= CD1400_TX_FIFO_SIZE - 2) {
1275 SET(cy->cy_flags, CY_F_SEND_NUL);
1276 break;
1278 cd_write_reg(sc, cy->cy_chip,
1279 CD1400_TDR, ch);
1280 count++;
1282 cd_write_reg(sc, cy->cy_chip,
1283 CD1400_TDR, ch);
1284 count++;
1286 } else {
1288 * no data to send -- check if we should
1289 * start/stop a break
1292 * XXX does this cause too much delay before
1293 * breaks?
1295 if (ISSET(cy->cy_flags, CY_F_START_BREAK)) {
1296 cd_write_reg(sc, cy->cy_chip,
1297 CD1400_TDR, 0);
1298 cd_write_reg(sc, cy->cy_chip,
1299 CD1400_TDR, 0x81);
1300 CLR(cy->cy_flags, CY_F_START_BREAK);
1302 if (ISSET(cy->cy_flags, CY_F_END_BREAK)) {
1303 cd_write_reg(sc, cy->cy_chip,
1304 CD1400_TDR, 0);
1305 cd_write_reg(sc, cy->cy_chip,
1306 CD1400_TDR, 0x83);
1307 CLR(cy->cy_flags, CY_F_END_BREAK);
1311 if (tp->t_outq.c_cc == 0) {
1312 txdone:
1314 * No data to send or requested to stop.
1315 * Disable transmit interrupt
1317 cd_write_reg(sc, cy->cy_chip, CD1400_SRER,
1318 cd_read_reg(sc, cy->cy_chip, CD1400_SRER)
1319 & ~CD1400_SRER_TXRDY);
1320 CLR(cy->cy_flags, CY_F_STOP);
1321 CLR(tp->t_state, TS_BUSY);
1323 if (tp->t_outq.c_cc <= tp->t_lowat) {
1324 SET(cy->cy_flags, CY_F_START);
1325 cy_events = 1;
1327 /* terminate service context */
1328 cd_write_reg(sc, cy->cy_chip, CD1400_TIR, save_tir & 0x3f);
1329 cd_write_reg(sc, cy->cy_chip, CD1400_CAR, save_car);
1330 int_serviced = 1;
1331 } /* if (tx_service...) */
1332 } /* for(...all CD1400s on a card) */
1334 /* ensure an edge for next interrupt */
1335 bus_space_write_1(sc->sc_memt, sc->sc_bsh,
1336 CY_CLEAR_INTR << sc->sc_bustype, 0);
1337 return int_serviced;
1341 * subroutine to enable CD1400 transmitter
1343 void
1344 cy_enable_transmitter(struct cy_softc *sc, struct cy_port *cy)
1346 int s = spltty();
1347 cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
1348 cy->cy_port_num & CD1400_CAR_CHAN);
1349 cd_write_reg(sc, cy->cy_chip, CD1400_SRER,
1350 cd_read_reg(sc, cy->cy_chip, CD1400_SRER) | CD1400_SRER_TXRDY);
1351 splx(s);
1355 * Execute a CD1400 channel command
1357 void
1358 cd1400_channel_cmd(struct cy_softc *sc, struct cy_port *cy, int cmd)
1360 u_int waitcnt = 5 * 8 * 1024; /* approx 5 ms */
1362 #ifdef CY_DEBUG
1363 printf("c1400_channel_cmd cy %p command 0x%x\n", cy, cmd);
1364 #endif
1366 /* wait until cd1400 is ready to process a new command */
1367 while (cd_read_reg(sc, cy->cy_chip, CD1400_CCR) != 0 && waitcnt-- > 0);
1369 if (waitcnt == 0)
1370 log(LOG_ERR, "%s: channel command timeout\n",
1371 device_xname(sc->sc_dev));
1373 cd_write_reg(sc, cy->cy_chip, CD1400_CCR, cmd);
1377 * Compute clock option register and baud rate register values
1378 * for a given speed. Return 0 on success, -1 on failure.
1380 * The error between requested and actual speed seems
1381 * to be well within allowed limits (less than 3%)
1382 * with every speed value between 50 and 150000 bps.
1385 cy_speed(speed_t speed, int *cor, int *bpr, int cy_clock)
1387 int c, co, br;
1389 if (speed < 50 || speed > 150000)
1390 return -1;
1392 for (c = 0, co = 8; co <= 2048; co <<= 2, c++) {
1393 br = (cy_clock + (co * speed) / 2) / (co * speed);
1394 if (br < 0x100) {
1395 *bpr = br;
1396 *cor = c;
1397 return 0;
1401 return -1;