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