1 /* $NetBSD: cy.c,v 1.56 2008/05/25 19:22:21 ad Exp $ */
6 * Driver for Cyclades Cyclom-8/16/32 multiport serial cards
7 * (currently not tested with Cyclom-32 cards)
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>
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>
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
= {
70 .d_kqfilter
= ttykqfilter
,
74 static int cy_open
= 0;
75 static int cy_events
= 0;
79 callout_t cy_poll_callout
;
82 * Common probe routine
85 cy_find(struct cy_softc
*sc
)
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);
99 printf("cy: card reset done\n");
103 for (cy_chip
= 0, chip
= 0; cy_chip
< CY_MAX_CD1400s
;
104 cy_chip
++, chip
+= (CY_CD1400_MEMSPACING
<< bustype
)) {
108 * the last 4 nchips are 'interleaved' with the first 4 on
112 chip
-= (CY32_ADDR_FIX
<< bustype
);
115 printf("sy: probe chip %d offset 0x%x ... ", cy_chip
, chip
);
118 /* wait until the chip is ready for command */
120 if (bus_space_read_1(tag
, bsh
, chip
+
121 ((CD1400_CCR
<< 1) << bustype
)) != 0) {
123 printf("not ready for command\n");
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.
138 bus_space_read_1(tag
, bsh
, /* off for chip 0 (0) + */
139 ((CD1400_GFRCR
<< 1) << bustype
)) == 0)
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
++) {
150 firmware_ver
= bus_space_read_1(tag
, bsh
, chip
+
151 ((CD1400_GFRCR
<< 1) << bustype
));
152 if ((firmware_ver
& 0xf0) == 0x40) /* found a CD1400 */
156 printf("firmware version 0x%x\n", firmware_ver
);
159 if ((firmware_ver
& 0xf0) != 0x40)
162 /* firmware version OK, CD1400 found */
166 if (sc
->sc_nchips
== 0) {
168 printf("no CD1400s found\n");
173 printf("found %d CD1400s\n", sc
->sc_nchips
);
180 cy_attach(struct cy_softc
*sc
)
182 int port
, cy_chip
, num_chips
, cdu
, chip
;
185 if (!cy_callout_init
) {
186 cy_callout_init
= true;
187 callout_init(&cy_poll_callout
, 0);
190 num_chips
= sc
->sc_nchips
;
194 memset(sc
->sc_ports
, 0, sizeof(sc
->sc_ports
));
197 for (cy_chip
= 0, chip
= 0; cy_chip
< num_chips
; cy_chip
++,
198 chip
+= (CY_CD1400_MEMSPACING
<< sc
->sc_bustype
)) {
201 chip
-= (CY32_ADDR_FIX
<< sc
->sc_bustype
);
204 aprint_debug("attach CD1400 #%d offset 0x%x\n", cy_chip
, chip
);
206 sc
->sc_cd1400_offs
[cy_chip
] = chip
;
209 * configure port 0 as serial port (should already be after
212 cd_write_reg(sc
, cy_chip
, CD1400_GCR
, 0);
214 if (cd_read_reg(sc
, cy_chip
, CD1400_GFRCR
) <= 0x46)
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? */
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
;
260 for (i
= 0, j
= 0; i
< cy_cd
.cd_ndevs
; i
++) {
262 sc
= device_lookup_private(&cy_cd
, i
);
265 if (sc
->sc_nchannels
== 0)
267 j
+= sc
->sc_nchannels
;
269 return (&sc
->sc_ports
[u
- k
]);
276 * open routine. returns zero if successful, else error code
279 cyopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
292 if (cy
->cy_tty
== NULL
) {
293 if ((cy
->cy_tty
= ttymalloc()) == NULL
) {
295 aprint_error_dev(sc
->sc_dev
,
296 "port %d: can't allocate tty\n",
300 tty_attach(cy
->cy_tty
);
305 tp
->t_oproc
= cystart
;
306 tp
->t_param
= cyparam
;
309 if (kauth_authorize_device_tty(l
->l_cred
, KAUTH_DEVICE_TTY_OPEN
, tp
))
312 if (!ISSET(tp
->t_state
, TS_ISOPEN
) && tp
->t_wopen
== 0) {
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
;
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",
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? */
360 callout_reset(&cy_poll_callout
, 1, cy_poll
, NULL
);
362 /* this sets parameters and raises DTR */
363 cyparam(tp
, &tp
->t_termios
);
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
);
383 CLR(tp
->t_state
, TS_CARR_ON
);
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
)) {
393 error
= ttysleep(tp
, &tp
->t_rawcv
, true, 0);
396 mutex_spin_exit(&tty_lock
);
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
)
421 (*tp
->t_linesw
->l_close
) (tp
, flag
);
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
);
448 cyread(dev_t dev
, struct uio
*uio
, int flag
)
456 return ((*tp
->t_linesw
->l_read
)(tp
, uio
, flag
));
463 cywrite(dev_t dev
, struct uio
*uio
, int flag
)
471 return ((*tp
->t_linesw
->l_write
)(tp
, uio
, flag
));
478 cypoll(dev_t dev
, int events
, struct lwp
*l
)
486 return ((*tp
->t_linesw
->l_poll
)(tp
, events
, l
));
506 cyioctl(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
517 error
= (*tp
->t_linesw
->l_ioctl
)(tp
, cmd
, data
, flag
, l
);
518 if (error
!= EPASSTHROUGH
)
521 error
= ttioctl(tp
, cmd
, data
, flag
, l
);
522 if (error
!= EPASSTHROUGH
)
525 /* XXX should not allow dropping DTR when dialin? */
528 case TIOCSBRK
: /* start break */
529 SET(cy
->cy_flags
, CY_F_START_BREAK
);
530 cy_enable_transmitter(sc
, cy
);
533 case TIOCCBRK
: /* stop break */
534 SET(cy
->cy_flags
, CY_F_END_BREAK
);
535 cy_enable_transmitter(sc
, cy
);
538 case TIOCSDTR
: /* DTR on */
539 cy_modem_control(sc
, cy
, TIOCM_DTR
, DMBIS
);
542 case TIOCCDTR
: /* DTR off */
543 cy_modem_control(sc
, cy
, TIOCM_DTR
, DMBIC
);
546 case TIOCMSET
: /* set new modem control line values */
547 cy_modem_control(sc
, cy
, *((int *) data
), DMSET
);
550 case TIOCMBIS
: /* turn modem control bits on */
551 cy_modem_control(sc
, cy
, *((int *) data
), DMBIS
);
554 case TIOCMBIC
: /* turn modem control bits off */
555 cy_modem_control(sc
, cy
, *((int *) data
), DMBIC
);
558 case TIOCMGET
: /* get modem control/status line state */
559 *((int *) data
) = cy_modem_control(sc
, cy
, 0, DMGET
);
563 *((int *) data
) = cy
->cy_openflags
|
564 (CY_DIALOUT(dev
) ? TIOCFLAG_SOFTCAR
: 0);
568 error
= kauth_authorize_device_tty(l
->l_cred
,
569 KAUTH_DEVICE_TTY_PRIVSET
, tp
);
573 cy
->cy_openflags
= *((int *) data
) &
574 (TIOCFLAG_SOFTCAR
| TIOCFLAG_CLOCAL
|
575 TIOCFLAG_CRTSCTS
| TIOCFLAG_MDMBUF
);
589 cystart(struct tty
*tp
)
595 cy
= CY_PORT(tp
->t_dev
);
601 cy
->cy_start_count
++;
604 if (!ISSET(tp
->t_state
, TS_TTSTOP
| TS_TIMEOUT
| TS_BUSY
)) {
607 SET(tp
->t_state
, TS_BUSY
);
608 cy_enable_transmitter(sc
, cy
);
619 cystop(struct tty
*tp
, int flag
)
624 cy
= CY_PORT(tp
->t_dev
);
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
);
641 * parameter setting routine.
642 * returns 0 if successful, else returns error code
645 cyparam(struct tty
*tp
, struct termios
*t
)
649 int ibpr
= 0, obpr
= 0, i_clk_opt
= 0, o_clk_opt
= 0; /* XXX: GCC */
652 cy
= CY_PORT(tp
->t_dev
);
655 if (t
->c_ospeed
!= 0 && cy_speed(t
->c_ospeed
, &o_clk_opt
, &obpr
, cy
->cy_clock
) < 0)
658 if (t
->c_ispeed
!= 0 && cy_speed(t
->c_ispeed
, &i_clk_opt
, &ibpr
, cy
->cy_clock
) < 0)
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() */
668 cd_write_reg(sc
, cy
->cy_chip
, CD1400_CAR
, port
& CD1400_CAR_CHAN
);
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 */
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
) {
703 opt
|= CD1400_COR1_CS5
;
707 opt
|= CD1400_COR1_CS6
;
711 opt
|= CD1400_COR1_CS7
;
715 opt
|= CD1400_COR1_CS8
;
719 cd_write_reg(sc
, cy
->cy_chip
, CD1400_COR1
, opt
);
722 printf("cor1 = 0x%x...", opt
);
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
,
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));
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?
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
;
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. */
796 if (cy
->cy_channel_control
& CD1400_CCR_RCVEN
)
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
) &
803 if (msvr
& CD1400_MSVR2_DTR
)
806 if (cd_read_reg(sc
, cy
->cy_chip
, CD1400_MSVR1
) &
809 if (msvr
& CD1400_MSVR2_DTR
)
812 if (msvr
& CD1400_MSVR2_CTS
)
814 if (msvr
& CD1400_MSVR2_CD
)
816 /* Not connected on some Cyclom-Y boards? */
817 if (msvr
& CD1400_MSVR2_DSR
)
819 /* Not connected on some Cyclom-8Y boards? */
820 if (msvr
& CD1400_MSVR2_RI
)
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));
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));
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
,
846 if (bits
& TIOCM_DTR
)
847 cd_write_reg(sc
, cy
->cy_chip
, CD1400_MSVR1
,
850 if (!ISSET(tp
->t_cflag
, CRTSCTS
) &&
851 (bits
& TIOCM_RTS
) != 0)
852 cd_write_reg(sc
, cy
->cy_chip
, CD1400_MSVR1
,
854 if (bits
& TIOCM_DTR
)
855 cd_write_reg(sc
, cy
->cy_chip
, CD1400_MSVR2
,
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);
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);
875 return ((howto
== DMGET
) ? bits
: 0);
879 * Upper-level handler loop (called from timer interrupt?)
880 * This routine is common for multiple cards
889 static int counter
= 0;
895 if (cy_events
== 0 && ++counter
< 200) {
902 for (card
= 0; card
< cy_cd
.cd_ndevs
; card
++) {
903 sc
= device_lookup_private(&cy_cd
, card
);
908 sc
->sc_poll_count1
++;
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))
919 * handle received data
921 while (cy
->cy_ibuf_rd_ptr
!= cy
->cy_ibuf_wr_ptr
) {
925 line_stat
= cy
->cy_ibuf_rd_ptr
[0];
926 chr
= cy
->cy_ibuf_rd_ptr
[1];
929 (CD1400_RDSR_BREAK
| CD1400_RDSR_FE
))
931 if (line_stat
& CD1400_RDSR_PE
)
935 * on an overrun error the data is treated as
936 * good just as it should be.
940 aprint_debug_dev(sc
->sc_dev
,
941 "port %d ttyinput 0x%x\n",
945 (*tp
->t_linesw
->l_rint
) (chr
, tp
);
947 s
= spltty(); /* really necessary? */
948 if ((cy
->cy_ibuf_rd_ptr
+= 2) ==
950 cy
->cy_ibuf_rd_ptr
= cy
->cy_ibuf
;
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
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
);
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
);
993 * handle carrier changes
996 if (ISSET(cy
->cy_flags
, CY_F_CARRIER_CHANGED
)) {
999 CLR(cy
->cy_flags
, CY_F_CARRIER_CHANGED
);
1002 carrier
= ((cy
->cy_carrier_stat
&
1003 CD1400_MSVR2_CD
) != 0);
1006 printf("cy_poll: carrier change "
1007 "(card %d, port %d, carrier %d)\n",
1008 card
, port
, carrier
);
1010 if (CY_DIALOUT(tp
->t_dev
) == 0 &&
1011 !(*tp
->t_linesw
->l_modem
)(tp
, carrier
))
1012 cy_modem_control(sc
, cy
,
1022 if (ISSET(cy
->cy_flags
, CY_F_START
)) {
1023 CLR(cy
->cy_flags
, CY_F_START
);
1026 (*tp
->t_linesw
->l_start
) (tp
);
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...) */
1051 if (did_something
&& counter
>= 200)
1052 sc
->sc_poll_count2
++;
1054 } /* for(card...) */
1059 callout_reset(&cy_poll_callout
, 1, cy_poll
, NULL
);
1063 * hardware interrupt routine
1068 struct cy_softc
*sc
= arg
;
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
);
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
;
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];
1097 cy
->cy_rx_int_count
++;
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
,
1105 recv_data
= cd_read_reg(sc
, cy
->cy_chip
,
1108 if (cy
->cy_tty
== NULL
||
1109 !ISSET(cy
->cy_tty
->t_state
, TS_ISOPEN
))
1115 "port %d recv exception, "
1116 "line_stat 0x%x, char 0x%x\n",
1117 cy
->cy_port_num
, line_stat
, recv_data
);
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
;
1131 cy
->cy_ibuf_overruns
++;
1134 } else {/* no exception, received data OK */
1135 n_chars
= cd_read_reg(sc
, cy
->cy_chip
,
1138 /* If no tty or not open, discard data */
1139 if (cy
->cy_tty
== NULL
||
1140 !ISSET(cy
->cy_tty
->t_state
, TS_ISOPEN
)) {
1142 (void)cd_read_reg(sc
,
1143 cy
->cy_chip
, CD1400_RDSR
);
1148 aprint_debug_dev(sc
->sc_dev
,
1149 "port %d receive ok %d chars\n",
1150 cy
->cy_port_num
, n_chars
);
1153 *buf_p
++ = 0; /* status: OK */
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
;
1163 cy
->cy_ibuf_overruns
++;
1170 cy
->cy_ibuf_wr_ptr
= buf_p
;
1172 /* RTS handshaking for incoming data */
1173 if (ISSET(cy
->cy_tty
->t_cflag
, CRTSCTS
)) {
1176 bf
= buf_p
- cy
->cy_ibuf_rd_ptr
;
1180 if (bf
> (CY_IBUF_SIZE
/ 2)) {
1182 if (cy
->cy_clock
== CY_CLOCK_60
)
1183 msvr
= CD1400_MSVR2
;
1185 msvr
= CD1400_MSVR1
;
1186 cd_write_reg(sc
, cy
->cy_chip
, msvr
, 0);
1191 /* terminate service context */
1192 cd_write_reg(sc
, cy
->cy_chip
, CD1400_RIR
,
1194 cd_write_reg(sc
, cy
->cy_chip
, CD1400_CAR
, save_car
);
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];
1209 cy
->cy_modem_int_count
++;
1212 modem_stat
= cd_read_reg(sc
, cy
->cy_chip
, CD1400_MSVR2
);
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
);
1219 if (ISSET((cy
->cy_carrier_stat
^ modem_stat
), CD1400_MSVR2_CD
)) {
1220 SET(cy
->cy_flags
, CY_F_CARRIER_CHANGED
);
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
);
1229 } /* if (modem_service...) */
1230 if (ISSET(stat
, CD1400_SVRR_TXRDY
)) {
1231 u_char save_car
, save_tir
, serv_type
,
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];
1244 cy
->cy_tx_int_count
++;
1247 aprint_debug_dev(sc
->sc_dev
, "port %d tx service\n",
1251 /* stop transmitting if no tty or CY_F_STOP set */
1253 if (tp
== NULL
|| ISSET(cy
->cy_flags
, CY_F_STOP
))
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);
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
1274 if (count
>= CD1400_TX_FIFO_SIZE
- 2) {
1275 SET(cy
->cy_flags
, CY_F_SEND_NUL
);
1278 cd_write_reg(sc
, cy
->cy_chip
,
1282 cd_write_reg(sc
, cy
->cy_chip
,
1288 * no data to send -- check if we should
1289 * start/stop a break
1292 * XXX does this cause too much delay before
1295 if (ISSET(cy
->cy_flags
, CY_F_START_BREAK
)) {
1296 cd_write_reg(sc
, cy
->cy_chip
,
1298 cd_write_reg(sc
, cy
->cy_chip
,
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
,
1305 cd_write_reg(sc
, cy
->cy_chip
,
1307 CLR(cy
->cy_flags
, CY_F_END_BREAK
);
1311 if (tp
->t_outq
.c_cc
== 0) {
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
);
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
);
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
1344 cy_enable_transmitter(struct cy_softc
*sc
, struct cy_port
*cy
)
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
);
1355 * Execute a CD1400 channel command
1358 cd1400_channel_cmd(struct cy_softc
*sc
, struct cy_port
*cy
, int cmd
)
1360 u_int waitcnt
= 5 * 8 * 1024; /* approx 5 ms */
1363 printf("c1400_channel_cmd cy %p command 0x%x\n", cy
, cmd
);
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);
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
)
1389 if (speed
< 50 || speed
> 150000)
1392 for (c
= 0, co
= 8; co
<= 2048; co
<<= 2, c
++) {
1393 br
= (cy_clock
+ (co
* speed
) / 2) / (co
* speed
);