2 * Low-level subroutines for Cronyx-Tau adapter.
4 * Copyright (C) 1994-2001 Cronyx Engineering.
5 * Author: Serge Vakulenko, <vak@cronyx.ru>
7 * Copyright (C) 2003 Cronyx Engineering.
8 * Author: Roman Kurakin, <rik@cronyx.ru>
10 * This software is distributed with NO WARRANTIES, not even the implied
11 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * Authors grant any other persons or organisations permission to use
14 * or modify this software as long as this message is kept with the software,
15 * all derivative works or modified versions.
17 * Cronyx Id: ctau.c,v 1.1.2.4 2003/12/11 17:33:43 rik Exp $
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
22 #include <dev/cx/machdep.h>
23 #include <dev/ctau/ctddk.h>
24 #include <dev/ctau/ctaureg.h>
25 #include <dev/ctau/hdc64570.h>
26 #include <dev/ctau/ds2153.h>
27 #include <dev/ctau/am8530.h>
28 #include <dev/ctau/lxt318.h>
29 #include <dev/cx/cronyxfw.h>
31 #define DMA_MASK 0xd4 /* DMA mask register */
32 #define DMA_MASK_CLEAR 0x04 /* DMA clear mask */
33 #define DMA_MODE 0xd6 /* DMA mode register */
34 #define DMA_MODE_MASTER 0xc0 /* DMA master mode */
36 #define BYTE *(unsigned char*)&
38 static unsigned char irqmask [] = {
39 BCR0_IRQ_DIS, BCR0_IRQ_DIS, BCR0_IRQ_DIS, BCR0_IRQ_3,
40 BCR0_IRQ_DIS, BCR0_IRQ_5, BCR0_IRQ_DIS, BCR0_IRQ_7,
41 BCR0_IRQ_DIS, BCR0_IRQ_DIS, BCR0_IRQ_10, BCR0_IRQ_11,
42 BCR0_IRQ_12, BCR0_IRQ_DIS, BCR0_IRQ_DIS, BCR0_IRQ_15,
45 static unsigned char dmamask [] = {
46 BCR0_DMA_DIS, BCR0_DMA_DIS, BCR0_DMA_DIS, BCR0_DMA_DIS,
47 BCR0_DMA_DIS, BCR0_DMA_5, BCR0_DMA_6, BCR0_DMA_7,
50 static short porttab [] = { /* standard base port set */
51 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
52 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
55 static short irqtab [] = { 3, 5, 7, 10, 11, 12, 15, 0 };
56 static short dmatab [] = { 5, 6, 7, 0 };
58 static int valid (short value, short *list)
66 long ct_baud = 256000; /* default baud rate */
67 unsigned char ct_chan_mode = M_HDLC; /* default mode */
69 static void ct_init_chan (ct_board_t *b, int num);
70 static void ct_enable_loop (ct_chan_t *c);
71 static void ct_disable_loop (ct_chan_t *c);
73 int ct_download (port_t port, const unsigned char *firmware,
74 long bits, const cr_dat_tst_t *tst)
76 unsigned char cr1, sr2;
77 long i, n, maxn = (bits + 7) >> 3;
81 for (i=n=0; n<maxn; ++n) {
82 v = ((firmware[n] ^ ' ') << 1) | ((firmware[n] >> 7) & 1);
83 for (b=0; b<7; b+=2, i+=2) {
91 outb (BCR1(port), cr1);
92 sr2 = inb (BSR2(port));
93 outb (BCR0(port), BCR0_TCK);
97 if (i >= tst->start && (sr2 & BSR2_LERR))
105 * Firmware unpack algorithm.
108 const unsigned char *ptr;
113 static unsigned short unpack_init (unpack_t *t, const unsigned char *ptr)
125 static unsigned char unpack_getchar (unpack_t *t)
133 t->count = *t->ptr++;
138 * Firmware download signals.
140 #define nstatus(b) (inb(BSR3(b)) & BSR3_NSTATUS)
142 #define confdone(b) (inb(BSR3(b)) & BSR3_CONF_DN)
144 #define nconfig_set(b) outb (bcr1_port, (bcr1 &= ~BCR1_NCONFIGI))
145 #define nconfig_clr(b) outb (bcr1_port, (bcr1 |= BCR1_NCONFIGI))
147 #define dclk_tick(b) outb (BCR3(b), 0)
149 #define putbit(b,bit) { if (bit) bcr1 |= BCR1_1KDAT; \
150 else bcr1 &= ~BCR1_1KDAT; \
151 outb (bcr1_port, bcr1); \
154 #define CTAU_DEBUG(x) /*trace_str x*/
156 int ct_download2 (port_t port, const unsigned char *fwaddr)
158 unsigned short bytes;
159 unsigned char bcr1, val;
164 * Set NCONFIG and wait until NSTATUS is set.
166 bcr1_port = BCR1(port);
169 for (val=0; val<255; ++val)
174 * Clear NCONFIG, wait 2 usec and check that NSTATUS is cleared.
177 for (val=0; val<2*3; ++val)
180 CTAU_DEBUG (("Bad nstatus, downloading aborted (bsr3=0x%x).\n", inb(BSR3(port))));
186 * Set NCONFIG and wait 5 usec.
189 for (val=0; val<5*3; ++val) /* Delay 5 msec. */
193 * ó ÁÄÒÅÓÁ `fwaddr' × ÐÁÍÑÔÉ ÄÏÌÖÎÙ ÌÅÖÁÔØ ÕÐÁËÏ×ÁÎÎÙÅ ÄÁÎÎÙÅ
194 * ÄÌÑ ÚÁÇÒÕÚËÉ firmware. úÎÁÞÅÎÉÅ ÄÏÌÖÎÏ ÂÙÔØ ÓÏÇÌÁÓÏ×ÁÎÏ Ó ÐÁÒÁÍÅÔÒÏÍ
195 * ×ÙÚÏ×Á ÕÔÉÌÉÔÙ `megaprog' × ÓËÒÉÐÔÅ ÚÁÇÒÕÚËÉ (É Makefile).
197 bytes = unpack_init (&t, fwaddr);
198 for (; bytes>0; --bytes) {
199 val = unpack_getchar (&t);
201 if (nstatus(port) == 0) {
202 CTAU_DEBUG (("Bad nstatus, %d bytes remaining.\n", bytes));
206 if (confdone(port)) {
207 /* Ten extra clocks. Hope 50 is enough. */
208 for (val=0; val<50; ++val)
211 if (nstatus(port) == 0) {
212 CTAU_DEBUG (("Bad nstatus after confdone, %d bytes remaining (%d).\n",
213 bytes, t.ptr - fwaddr));
218 /*CTAU_DEBUG (("Download succeeded.\n"));*/
222 putbit (port, val & 0x01);
223 putbit (port, val & 0x02);
224 putbit (port, val & 0x04);
225 putbit (port, val & 0x08);
226 putbit (port, val & 0x10);
227 putbit (port, val & 0x20);
228 putbit (port, val & 0x40);
229 putbit (port, val & 0x80);
231 /* if ((bytes & 1023) == 0) putch ('.'); */
234 CTAU_DEBUG (("Bad confdone.\n"));
236 CTAU_DEBUG (("Downloading aborted.\n"));
241 * Detect Tau2 adapter.
243 static int ct_probe2_board (port_t port)
245 unsigned char sr3, osr3;
248 if (! valid (port, porttab))
251 osr3 = inb (BSR3(port));
252 if ((osr3 & (BSR3_IB | BSR3_IB_NEG)) != BSR3_IB &&
253 (osr3 & (BSR3_IB | BSR3_IB_NEG)) != BSR3_IB_NEG)
255 for (i=0; i<100; ++i) {
257 sr3 = inb (BSR3(port));
258 sr3 = inb (BSR3(port));
259 if (((sr3 ^ osr3) & (BSR3_IB | BSR3_IB_NEG | BSR3_ZERO)) !=
260 (BSR3_IB | BSR3_IB_NEG))
264 /* Reset the controller. */
265 outb (BCR0(port), 0);
270 * Check if the Tau board is present at the given base port.
271 * Read board status register 1 and check identification bits
272 * which should invert every next read.
273 * The "zero" bit should remain stable.
275 int ct_probe_board (port_t port, int irq, int dma)
277 unsigned char sr3, osr3;
280 if (! valid (port, porttab))
283 if ((irq > 0) && (!valid (irq, irqtab)))
286 if ((dma > 0) && (!valid (dma, dmatab)))
289 osr3 = inb (BSR3(port));
290 if ((osr3 & (BSR3_IB | BSR3_IB_NEG)) != BSR3_IB &&
291 (osr3 & (BSR3_IB | BSR3_IB_NEG)) != BSR3_IB_NEG)
293 for (i=0; i<100; ++i) {
294 sr3 = inb (BSR3(port));
295 if (((sr3 ^ osr3) & (BSR3_IB | BSR3_IB_NEG | BSR3_ZERO)) !=
296 (BSR3_IB | BSR3_IB_NEG))
297 return ct_probe2_board (port);
300 /* Reset the controller. */
301 outb (BCR0(port), 0);
306 * Check that the irq is functional.
307 * irq>0 - activate the interrupt from the adapter (irq=on)
308 * irq<0 - deactivate the interrupt (irq=off)
309 * irq==0 - free the interrupt line (irq=tri-state)
310 * Return the interrupt mask _before_ activating irq.
312 int ct_probe_irq (ct_board_t *b, int irq)
319 mask |= inb (0xa0) << 8;
322 outb (BCR0(b->port), BCR0_HDRUN | irqmask[irq]);
323 outb (R(b->port,HD_TEPR_0R), 0);
324 outw (R(b->port,HD_TCONR_0R), 1);
325 outw (R(b->port,HD_TCNT_0R), 0);
326 outb (R(b->port,HD_TCSR_0R), TCSR_ENABLE | TCSR_INTR);
327 outb (IER2(b->port), IER2_RX_TME_0);
328 } else if (irq < 0) {
329 outb (BCR0(b->port), BCR0_HDRUN | irqmask[-irq]);
330 outb (IER0(b->port), 0);
331 outb (IER1(b->port), 0);
332 outb (IER2(b->port), 0);
333 outb (R(b->port,HD_TCSR_0R), 0);
334 cte_out (E1CS0 (b->port), DS_IMR2, 0);
335 cte_out (E1CS1 (b->port), DS_IMR2, 0);
337 outb (0xa0, 0x60 | ((-irq) & 7));
340 outb (0x20, 0x60 | (-irq));
342 outb (BCR0(b->port), b->bcr0);
343 cte_out (E1CS0 (b->port), DS_IMR2, SR2_SEC);
344 cte_out (E1CS1 (b->port), DS_IMR2, SR2_SEC);
350 void ct_init_board (ct_board_t *b, int num, port_t port, int irq, int dma,
355 /* Initialize board structure. */
363 /* Get the board type. */
364 if (b->type == B_TAU) strcpy (b->name, "Tau");
365 else if (b->type == B_TAU_E1) strcpy (b->name, "Tau/E1");
366 else if (b->type == B_TAU_E1C) strcpy (b->name, "Tau/E1c");
367 else if (b->type == B_TAU_E1D) strcpy (b->name, "Tau/E1d");
368 else if (b->type == B_TAU_G703) strcpy (b->name, "Tau/G.703");
369 else if (b->type == B_TAU_G703C) strcpy (b->name, "Tau/G.703c");
370 else if (b->type == B_TAU2) strcpy (b->name, "Tau2");
371 else if (b->type == B_TAU2_E1) strcpy (b->name, "Tau2/E1");
372 else if (b->type == B_TAU2_E1D) strcpy (b->name, "Tau2/E1d");
373 else if (b->type == B_TAU2_G703) strcpy (b->name, "Tau2/G.703");
374 else strcpy (b->name, "Tau/???");
376 /* Set DMA and IRQ. */
377 b->bcr0 = BCR0_HDRUN | dmamask[b->dma] | irqmask[b->irq];
379 /* Clear DTR[0..1]. */
383 /* Initialize channel structures. */
384 for (i=0; i<NCHAN; ++i)
390 * Initialize the board structure.
392 void ct_init (ct_board_t *b, int num, port_t port, int irq, int dma,
393 const unsigned char *firmware, long bits, const cr_dat_tst_t *tst,
394 const unsigned char *firmware2)
396 static long tlen = 182;
397 static cr_dat_tst_t tvec [] = {{ 114, 178 }, { 182, 182 }};
398 static cr_dat_tst_t tvec2 [] = {{ 50, 178 }, { 182, 182 }};
399 static unsigned char tau [] = { 155,153,113,48,64,236,
400 48,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,183,};
401 static unsigned char e1 [] = { 155,153,113,48,64,236,
402 112,37,49,37,33,116,101,100,112,37,49,37,33,116,101,100,230,};
403 static unsigned char e1_2 [] = { 155,153,113,48,64,236,
404 112,37,49,37,33,116,101,100,96,97,53,49,49,96,97,100,230,};
405 static unsigned char e1_3 [] = { 155,153,113,48,64,236,
406 96,97,53,49,49,96,97,100,96,97,53,49,49,96,97,100,230,};
407 static unsigned char e1_4 [] = { 155,153,113,48,64,236,
408 96,97,53,49,49,96,97,100,112,37,49,37,33,116,101,100,230,};
409 static unsigned char g703 [] = { 155,153,113,48,64,236,
410 112,37,49,37,33,116,101,32,117,37,49,37,33,116,101,100,230,};
411 static unsigned char g703_2 [] = { 155,153,113,48,64,236,
412 112,37,49,37,33,116,101,32,101,97,53,49,49,96,97,100,230,};
413 static unsigned char g703_3 [] = { 155,153,113,48,64,236,
414 96,97,53,49,49,96,97,32,101,97,53,49,49,96,97,100,230,};
415 static unsigned char g703_4 [] = { 155,153,113,48,64,236,
416 96,97,53,49,49,96,97,32,117,37,49,37,33,116,101,100,230,};
419 long osc = (inb (BSR3(port)) & BSR3_ZERO) ? 8192000 : 10000000;
421 /* Get the board type. */
422 if (ct_probe2_board (port) && ct_download2 (port, firmware2)) {
423 /* Tau2, 1k30-based model */
424 unsigned char sr0 = inb (BSR0(port));
425 if (! (sr0 & BSR0_T703))
427 else if (sr0 & BSR0_TE1)
429 else if (inb(E1SR(port)) & E1SR_REV)
433 } else if (ct_download (port, tau, tlen, tvec)) {
434 if (! ct_download (port, firmware, bits, tst))
437 unsigned char sr0 = inb (BSR0(port));
438 if (! (sr0 & BSR0_T703))
440 else if (sr0 & BSR0_TE1)
442 else if (inb(E1SR(port)) & E1SR_REV)
447 } else if (ct_download (port, e1, tlen, tvec2) ||
448 ct_download (port, e1_2, tlen, tvec2) ||
449 ct_download (port, e1_3, tlen, tvec2) ||
450 ct_download (port, e1_4, tlen, tvec2))
452 else if (ct_download (port, g703, tlen, tvec2) ||
453 ct_download (port, g703_2, tlen, tvec2) ||
454 ct_download (port, g703_3, tlen, tvec2) ||
455 ct_download (port, g703_4, tlen, tvec2))
457 ct_init_board (b, num, port, irq, dma, type, osc);
461 * Initialize the channel structure.
463 static void ct_init_chan (ct_board_t *b, int i)
465 ct_chan_t *c = b->chan + i;
466 port_t port = b->port;
472 case B_TAU2: c->type = T_SERIAL; break;
477 case B_TAU2_E1D: c->type = T_E1; break;
480 case B_TAU2_G703: c->type = T_G703; break;
485 #define reg(X,N) HD_##X##_##N
486 #define set(X,N) c->X = R(port,reg(X,N))
487 #define srx(X,N) c->RX.X = R(port,reg(X,N##R))
488 #define stx(X,N) c->TX.X = R(port,reg(X,N##T))
490 set(MD0, 0); set(MD1, 0); set(MD2, 0); set(CTL, 0);
491 set(RXS, 0); set(TXS, 0); set(TMC, 0); set(CMD, 0);
492 set(ST0, 0); set(ST1, 0); set(ST2, 0); set(ST3, 0);
493 set(FST, 0); set(IE0, 0); set(IE1, 0); set(IE2, 0);
494 set(FST, 0); set(IE0, 0); set(IE1, 0); set(IE2, 0);
495 set(FIE, 0); set(SA0, 0); set(SA1, 0); set(IDL, 0);
496 set(TRB, 0); set(RRC, 0); set(TRC0,0); set(TRC1,0);
498 srx(DAR, 0); srx(DARB,0); srx(SAR, 0); srx(SARB,0);
499 srx(CDA, 0); srx(EDA, 0); srx(BFL, 0); srx(BCR, 0);
500 srx(DSR, 0); srx(DMR, 0); srx(FCT, 0); srx(DIR, 0);
502 srx(TCNT,0); srx(TCONR,0); srx(TCSR,0); srx(TEPR,0);
503 stx(DAR, 0); stx(DARB,0); stx(SAR, 0); stx(SARB,0);
504 stx(CDA, 0); stx(EDA, 0); stx(BCR, 0);
505 stx(DSR, 0); stx(DMR, 0); stx(FCT, 0); stx(DIR, 0);
507 stx(TCNT,0); stx(TCONR,0); stx(TCSR,0); stx(TEPR,0);
509 set(MD0, 1); set(MD1, 1); set(MD2, 1); set(CTL, 1);
510 set(RXS, 1); set(TXS, 1); set(TMC, 1); set(CMD, 1);
511 set(ST0, 1); set(ST1, 1); set(ST2, 1); set(ST3, 1);
512 set(FST, 1); set(IE0, 1); set(IE1, 1); set(IE2, 1);
513 set(FST, 1); set(IE0, 1); set(IE1, 1); set(IE2, 1);
514 set(FIE, 1); set(SA0, 1); set(SA1, 1); set(IDL, 1);
515 set(TRB, 1); set(RRC, 1); set(TRC0,1); set(TRC1,1);
517 srx(DAR, 1); srx(DARB,1); srx(SAR, 1); srx(SARB,1);
518 srx(CDA, 1); srx(EDA, 1); srx(BFL, 1); srx(BCR, 1);
519 srx(DSR, 1); srx(DMR, 1); srx(FCT, 1); srx(DIR, 1);
521 srx(TCNT,1); srx(TCONR,1); srx(TCSR,1); srx(TEPR,1);
522 stx(DAR, 1); stx(DARB,1); stx(SAR, 1); stx(SARB,1);
523 stx(CDA, 1); stx(EDA, 1); stx(BCR, 1);
524 stx(DSR, 1); stx(DMR, 1); stx(FCT, 1); stx(DIR, 1);
526 stx(TCNT,1); stx(TCONR,1); stx(TCSR,1); stx(TEPR,1);
535 * Reinitialize the channels, using new options.
537 void ct_reinit_chan (ct_chan_t *c)
539 ct_board_t *b = c->board;
543 if (c->hopt.txs == CLK_LINE) {
544 /* External clock mode -- set zero baud rate. */
545 if (c->mode != M_ASYNC)
547 } else if (c->baud == 0) {
548 /* No baud rate in internal clock mode -- set default values. */
549 if (c->mode == M_ASYNC)
551 else if (c->mode == M_HDLC)
556 if (b->opt.cfg == CFG_B)
558 /* Fall through... */
561 c->hopt.txs = CLK_LINE;
563 /* Compute the baud value. */
566 if (b->opt.cfg == CFG_C)
570 /* Skip timeslot 16 in CAS mode. */
578 c->gopt.rate = c->baud / 1000;
580 /* Set NRZ and clear INVCLK. */
581 c->opt.md2.encod = MD2_ENCOD_NRZ;
582 c->board->opt.bcr2 &= c->num ?
583 ~(BCR2_INVTXC1 | BCR2_INVRXC1) :
584 ~(BCR2_INVTXC0 | BCR2_INVRXC0);
588 if (b->opt.cfg == CFG_B)
590 /* Fall through... */
593 c->hopt.txs = CLK_LINE;
594 c->baud = c->gopt.rate * 1000L;
596 /* Set NRZ/NRZI and clear INVCLK. */
597 if (c->opt.md2.encod != MD2_ENCOD_NRZ &&
598 c->opt.md2.encod != MD2_ENCOD_NRZI)
599 c->opt.md2.encod = MD2_ENCOD_NRZ;
600 c->board->opt.bcr2 &= c->num ?
601 ~(BCR2_INVTXC1 | BCR2_INVRXC1) :
602 ~(BCR2_INVTXC0 | BCR2_INVRXC0);
608 * Reinitialize all channels, using new options and baud rate.
610 void ct_reinit_board (ct_board_t *b)
614 b->opt = ct_board_opt_dflt;
615 for (c=b->chan; c<b->chan+NCHAN; ++c) {
616 c->opt = ct_chan_opt_dflt;
617 c->hopt = ct_opt_hdlc_dflt;
618 c->gopt = ct_opt_g703_dflt;
619 c->mode = ct_chan_mode;
627 * Set up the E1 controller of the Tau/E1 board.
628 * Frame sync signals:
629 * Configuration Rsync0 Tsync0 Rsync1 Tsync1
630 * ---------------------------------------------------
631 * A (II) out out out out
632 * B,C,D (HI,K,D) in out in out
633 * ---------------------------------------------------
634 * BI out out in in -- not implemented
635 * old B,C,D (HI,K,D) out in out in -- old
637 static void ct_setup_ctlr (ct_chan_t *c)
639 ct_board_t *b = c->board;
640 port_t p = c->num ? E1CS1 (b->port) : E1CS0 (b->port);
641 unsigned char rcr1, rcr2, tcr1, tcr2, ccr1, licr;
642 unsigned long xcbr, tir;
646 tcr1 = TCR1_TSIS | TCR1_TSO;
651 if (b->opt.cfg != CFG_D) {
652 /* Enable monitoring channel, when not in telephony mode. */
656 if (b->opt.cfg == CFG_A) {
669 ccr1 |= CCR1_THDB3 | CCR1_RHDB3;
672 ccr1 |= CCR1_TCRC4 | CCR1_RCRC4;
678 if (inb (E1SR (b->port)) & (c->num ? E1SR_TP1 : E1SR_TP0))
683 cte_out (p, DS_RCR1, rcr1); /* receive control 1 */
684 cte_out (p, DS_RCR2, rcr2); /* receive control 2 */
685 cte_out (p, DS_TCR1, tcr1); /* transmit control 1 */
686 cte_out (p, DS_TCR2, tcr2); /* transmit control 2 */
687 cte_out (p, DS_CCR1, ccr1); /* common control 1 */
688 cte_out (p, DS_CCR2, CCR2_CNTCV | CCR2_AUTORA | CCR2_LOFA1); /* common control 2 */
689 cte_out (p, DS_CCR3, CCR3_TSCLKM); /* common control 3 */
690 cte_out (p, DS_LICR, licr); /* line interface control */
691 cte_out (p, DS_IMR1, 0); /* interrupt mask 1 */
692 cte_out (p, DS_IMR2, SR2_SEC); /* interrupt mask 2 */
693 cte_out (p, DS_TEST1, 0);
694 cte_out (p, DS_TEST2, 0);
695 cte_out (p, DS_TAF, 0x9b); /* transmit align frame */
696 cte_out (p, DS_TNAF, 0xdf); /* transmit non-align frame */
697 cte_out (p, DS_TIDR, 0xff); /* transmit idle definition */
699 cte_out (p, DS_TS, 0x0b); /* transmit signaling 1 */
701 /* transmit signaling 2..16 */
702 cte_out (p, (unsigned char) (DS_TS+i), 0xff);
705 * S0 == list of timeslots for channel 0
706 * S1 == list of timeslots for channel 1
707 * S2 == list of timeslots for E1 subchannel (pass through)
709 * Each channel uses the same timeslots for receive and transmit,
710 * i.e. RCBRi == TCBRi.
712 if (b->opt.cfg == CFG_B)
714 else if (b->opt.cfg == CFG_C)
715 b->opt.s1 &=~ b->opt.s0;
717 /* Skip timeslot 16 in CAS mode. */
718 b->opt.s0 &=~ (1L << 16);
719 b->opt.s1 &=~ (1L << 16);
721 b->opt.s2 &=~ b->opt.s0;
722 b->opt.s2 &=~ b->opt.s1;
733 * Configuration C: (S0 & S2 == 0)
739 * Configuration D: (Si & Sj == 0)
742 * TIR0 := ~S0 & ~S1 & ~S2
745 xcbr = c->num ? b->opt.s1 : b->opt.s0;
746 if (b->opt.cfg == CFG_A)
748 else if (b->opt.cfg == CFG_D)
750 else if (c->num == 0)
751 tir = ~(b->opt.s0 | b->opt.s1 | b->opt.s2);
755 /* Mark idle channels. */
756 cte_out (p, DS_TIR, (unsigned char) (tir & 0xfe));
757 cte_out (p, DS_TIR+1, (unsigned char) (tir >> 8));
758 cte_out (p, DS_TIR+2, (unsigned char) (tir >> 16));
759 cte_out (p, DS_TIR+3, (unsigned char) (tir >> 24));
761 /* Set up rx/tx timeslots. */
762 cte_out (p, DS_RCBR, (unsigned char) (xcbr & 0xfe));
763 cte_out (p, DS_RCBR+1, (unsigned char) (xcbr >> 8));
764 cte_out (p, DS_RCBR+2, (unsigned char) (xcbr >> 16));
765 cte_out (p, DS_RCBR+3, (unsigned char) (xcbr >> 24));
766 cte_out (p, DS_TCBR, (unsigned char) (xcbr & 0xfe));
767 cte_out (p, DS_TCBR+1, (unsigned char) (xcbr >> 8));
768 cte_out (p, DS_TCBR+2, (unsigned char) (xcbr >> 16));
769 cte_out (p, DS_TCBR+3, (unsigned char) (xcbr >> 24));
771 /* Reset the line interface. */
772 cte_out (p, DS_CCR3, CCR3_TSCLKM | CCR3_LIRESET);
773 cte_out (p, DS_CCR3, CCR3_TSCLKM);
775 /* Reset the elastic store. */
776 cte_out (p, DS_CCR3, CCR3_TSCLKM | CCR3_ESRESET);
777 cte_out (p, DS_CCR3, CCR3_TSCLKM);
779 /* Clear status registers. */
780 cte_ins (p, DS_SR1, 0xff);
781 cte_ins (p, DS_SR2, 0xff);
782 cte_ins (p, DS_RIR, 0xff);
786 * Set up the serial controller of the Tau/E1 board.
788 static void ct_setup_scc (port_t port)
790 #define SET(r,v) { cte_out2 (port, r, v); cte_out2 (port, AM_A | r, v); }
793 cte_out2 (port, AM_MICR, MICR_RESET_HW);
795 SET (AM_PMR, 0x0c); /* 2 stop bits */
796 SET (AM_IMR, 0); /* no W/REQ signal */
797 cte_out2 (port, AM_IVR, 0); /* interrupt vector */
798 SET (AM_RCR, 0xc0); /* rx 8 bits/char */
799 SET (AM_TCR, 0x60); /* tx 8 bits/char */
800 SET (AM_SAF, 0); /* sync address field */
801 SET (AM_SFR, 0x7e); /* sync flag register */
802 cte_out2 (port, AM_MICR, 0); /* master interrupt control */
803 SET (AM_MCR, 0); /* NRZ mode */
804 SET (AM_CMR, 0x08); /* rxclk=RTxC, txclk=TRxC */
805 SET (AM_TCL, 0); /* time constant low */
806 SET (AM_TCH, 0); /* time constant high */
807 SET (AM_BCR, 0); /* disable baud rate generator */
809 SET (AM_RCR, 0xc1); /* enable rx */
810 SET (AM_TCR, 0x68); /* enable tx */
812 SET (AM_SICR, 0); /* no status interrupt */
813 SET (AM_CR, CR_RST_EXTINT); /* reset external status */
814 SET (AM_CR, CR_RST_EXTINT); /* reset ext/status twice */
819 * Set up the Tau/E1 board.
821 void ct_setup_e1 (ct_board_t *b)
824 * Control register 0:
825 * 1) board configuration
828 b->e1cfg &= E1CFG_LED;
830 case CFG_C: b->e1cfg |= E1CFG_K; break;
831 case CFG_B: b->e1cfg |= E1CFG_HI; break;
832 case CFG_D: b->e1cfg |= E1CFG_D; break;
833 default: b->e1cfg |= E1CFG_II; break;
836 if (b->opt.clk0 == GCLK_RCV) b->e1cfg |= E1CFG_CLK0_RCV;
837 if (b->opt.clk0 == GCLK_RCLKO) b->e1cfg |= E1CFG_CLK0_RCLK1;
838 else b->e1cfg |= E1CFG_CLK0_INT;
839 if (b->opt.clk1 == GCLK_RCV) b->e1cfg |= E1CFG_CLK1_RCV;
840 if (b->opt.clk1 == GCLK_RCLKO) b->e1cfg |= E1CFG_CLK1_RCLK0;
841 else b->e1cfg |= E1CFG_CLK1_INT;
843 outb (E1CFG (b->port), b->e1cfg);
846 * Set up serial controller.
848 ct_setup_scc (b->port);
851 * Set up E1 controllers.
853 ct_setup_ctlr (b->chan + 0); /* channel 0 */
854 ct_setup_ctlr (b->chan + 1); /* channel 1 */
856 /* Start the board (GRUN). */
857 b->e1cfg |= E1CFG_GRUN;
858 outb (E1CFG (b->port), b->e1cfg);
862 * Set up the G.703 controller of the Tau/G.703 board.
864 static void ct_setup_lxt (ct_chan_t *c)
866 ctg_outx (c, LX_CCR1, LX_RESET); /* reset the chip */
868 ctg_inx (c, LX_CCR1);
869 c->lx = LX_LOS; /* disable loss of sync interrupt */
870 if (c->num && c->board->opt.cfg == CFG_B)
871 c->lx |= LX_TAOS; /* idle channel--transmit all ones */
873 c->lx |= LX_HDB3; /* enable HDB3 encoding */
874 ctg_outx (c, LX_CCR1, c->lx); /* setup the new mode */
875 ctg_outx (c, LX_CCR2, LX_CCR2_LH); /* setup Long Haul mode */
876 ctg_outx (c, LX_CCR3, LX_CCR3_E1_LH); /* setup Long Haul mode */
880 * Set up the Tau/G.703 board.
882 void ct_setup_g703 (ct_board_t *b)
885 if (b->chan[0].gopt.pce) {
886 if (b->chan[0].gopt.pce2) b->gmd0 |= GMD_PCE_PCM2;
887 else b->gmd0 |= GMD_PCE_PCM2D;
890 b->gmd0 |= GMD_RSYNC;
893 if (b->chan[1].gopt.pce) {
894 if (b->chan[1].gopt.pce2) b->gmd1 |= GMD_PCE_PCM2;
895 else b->gmd1 |= GMD_PCE_PCM2D;
898 b->gmd1 |= GMD_RSYNC;
900 switch (b->chan[0].gopt.rate) {
901 case 2048: b->gmd0 |= GMD_2048; break;
902 case 1024: b->gmd0 |= GMD_1024; break;
903 case 512: b->gmd0 |= GMD_512; break;
904 case 256: b->gmd0 |= GMD_256; break;
905 case 128: b->gmd0 |= GMD_128; break;
906 case 64: b->gmd0 |= GMD_64; break;
908 switch (b->chan[1].gopt.rate) {
909 case 2048: b->gmd1 |= GMD_2048; break;
910 case 1024: b->gmd1 |= GMD_1024; break;
911 case 512: b->gmd1 |= GMD_512; break;
912 case 256: b->gmd1 |= GMD_256; break;
914 case 128: b->gmd1 |= GMD_128; break;
915 case 64: b->gmd1 |= GMD_64; break;
918 outb (GMD0(b->port), b->gmd0);
919 outb (GMD1(b->port), b->gmd1 | GMD1_NCS0 | GMD1_NCS1);
922 if (b->opt.cfg == CFG_B) b->gmd2 |= GMD2_SERIAL;
923 outb (GMD2(b->port), b->gmd2);
925 /* Set up G.703 controllers. */
926 if ((b->chan + 0)->lx & LX_LLOOP) {
927 ct_setup_lxt (b->chan + 0); /* channel 0 */
928 ct_enable_loop (b->chan + 0);
930 ct_setup_lxt (b->chan + 0); /* channel 0 */
932 if ((b->chan + 1)->lx & LX_LLOOP) {
933 ct_setup_lxt (b->chan + 1); /* channel 1 */
934 ct_enable_loop (b->chan + 1);
936 ct_setup_lxt (b->chan + 1); /* channel 1 */
940 outb (GERR(b->port), 0xff);
941 outb (GLDR(b->port), 0xff);
947 int ct_setup_board (ct_board_t *b, const unsigned char *firmware,
948 long bits, const cr_dat_tst_t *tst)
952 /* Disable DMA channel. */
953 outb (DMA_MASK, (b->dma & 3) | DMA_MASK_CLEAR);
955 /* Reset the controller. */
956 outb (BCR0(b->port), 0);
958 /* Load the firmware. */
959 if (firmware && (b->type == B_TAU || b->type == B_TAU_E1 ||
960 b->type == B_TAU_G703) &&
961 ! ct_download (b->port, firmware, bits, tst))
963 if (firmware && (b->type == B_TAU2 || b->type == B_TAU2_E1 ||
964 b->type == B_TAU2_E1D || b->type == B_TAU2_G703) &&
965 ! ct_download2 (b->port, firmware))
968 /* Enable DMA and IRQ. */
969 outb (BCR0(b->port), BCR0_HDRUN);
970 outb (BCR0(b->port), b->bcr0);
972 /* Clear DTR[0..1]. */
973 outb (BCR1(b->port), b->bcr1);
975 /* Set bus timing. */
976 b->bcr2 = b->opt.bcr2;
977 outb (BCR2(b->port), b->bcr2);
980 * Initialize the controller.
982 /* Zero wait state mode. */
983 outb (WCRL(b->port), 0);
984 outb (WCRM(b->port), 0);
985 outb (WCRH(b->port), 0);
987 /* Clear interrupt modified vector register. */
988 outb (IMVR(b->port), 0);
989 outb (ITCR(b->port), ITCR_CYCLE_SINGLE | ITCR_VECT_MOD);
991 /* Disable all interrupts. */
992 outb (IER0(b->port), 0);
993 outb (IER1(b->port), 0);
994 outb (IER2(b->port), 0);
996 /* Set DMA parameters, enable master DMA mode. */
997 outb (PCR(b->port), BYTE b->opt.pcr);
998 outb (DMER(b->port), DME_ENABLE);
1000 /* Set up DMA channel to master mode. */
1001 outb (DMA_MODE, (b->dma & 3) | DMA_MODE_MASTER);
1003 /* Enable DMA channel. */
1004 outb (DMA_MASK, b->dma & 3);
1006 /* Disable byte-sync mode for Tau/G.703. */
1007 if (b->type == B_TAU_G703)
1008 outb (GMD2(b->port), 0);
1010 /* Initialize all channels. */
1011 for (c=b->chan; c<b->chan+NCHAN; ++c)
1032 * Update the channel mode options.
1034 void ct_update_chan (ct_chan_t *c)
1036 int txbr, rxbr, tmc, txcout;
1037 unsigned char rxs, txs, dmr = 0;
1038 ct_md0_async_t amd0;
1040 ct_md1_async_t amd1;
1042 switch (c->mode) { /* initialize the channel mode */
1043 case M_ASYNC: default:
1047 amd0.mode = MD0_MODE_ASYNC;
1048 amd0.stopb = MD0_STOPB_1;
1049 amd0.cts_rts_dcd = 0;
1051 amd1.clk = MD1_CLK_16;
1052 amd1.txclen = amd1.rxclen = MD1_CLEN_8;
1053 amd1.parmode = MD1_PAR_NO;
1055 outb (c->MD0, BYTE amd0);
1056 outb (c->MD1, BYTE amd1);
1057 outb (c->CTL, c->rts ? 0 : CTL_RTS_INV);
1066 if (c->mode == M_E1 && c->board->opt.cfg == CFG_D) {
1070 outb (c->MD0, BYTE hmd0);
1071 outb (c->MD1, BYTE c->hopt.md1);
1072 outb (c->CTL, c->hopt.ctl & ~CTL_IDLE_PTRN);
1073 outb (c->SA0, c->hopt.sa0);
1074 outb (c->SA1, c->hopt.sa1);
1075 outb (c->IDL, 0x7e); /* HDLC flag 01111110 */
1077 outb (c->MD0, BYTE c->hopt.md0);
1078 outb (c->MD1, BYTE c->hopt.md1);
1079 outb (c->SA0, c->hopt.sa0);
1080 outb (c->SA1, c->hopt.sa1);
1081 outb (c->IDL, 0x7e); /* HDLC flag 01111110 */
1084 outb (c->CTL, c->hopt.ctl & ~CTL_RTS_INV);
1086 outb (c->CTL, c->hopt.ctl | CTL_RTS_INV);
1089 /* Chained-block DMA mode with frame counter. */
1090 dmr |= DMR_CHAIN_CNTE | DMR_CHAIN_NF | DMR_TMOD;
1094 outb (c->RX.DMR, dmr);
1095 outb (c->TX.DMR, dmr);
1097 /* set mode-independent options */
1098 c->opt.md2.dpll_clk = MD2_DPLL_CLK_8;
1099 outb (c->MD2, BYTE c->opt.md2);
1101 /* set up receiver and transmitter clocks */
1102 if (c->baud > 1024000) {
1103 /* turn off DPLL if the baud rate is too high */
1104 if (rxs == CLK_RXS_LINE_NS) rxs = CLK_LINE;
1105 else if (rxs == CLK_RXS_DPLL_INT) rxs = CLK_INT;
1107 if (rxs == CLK_RXS_LINE_NS || rxs == CLK_RXS_DPLL_INT) {
1108 /* Using 1:8 sampling rate. */
1109 ct_compute_clock (c->board->osc, c->baud * 8, &rxbr, &tmc);
1111 } else if (c->mode == M_ASYNC) {
1112 /* Using 1:16 sampling rate. */
1113 ct_compute_clock (c->board->osc, c->baud * 8, &rxbr, &tmc);
1117 ct_compute_clock (c->board->osc, c->baud, &rxbr, &tmc);
1125 /* Disable TXCOUT before changing TXS
1126 * to avoid two transmitters on the same line.
1127 * Enable it after TXS is set, if needed. */
1128 txcout = c->num ? BCR1_TXCOUT1 : BCR1_TXCOUT0;
1129 c->board->bcr1 &= ~txcout;
1130 outb (BCR1(c->board->port), c->board->bcr1);
1132 if ((txs & CLK_MASK) != CLK_LINE) {
1133 c->board->bcr1 |= txcout;
1134 outb (BCR1(c->board->port), c->board->bcr1);
1136 if (c->board->type == B_TAU_E1D ||
1137 c->board->type == B_TAU2_E1D)
1138 ct_set_phony (c, c->gopt.phony);
1142 * Initialize the channel.
1144 void ct_setup_chan (ct_chan_t *c)
1146 /* reset the channel */
1147 outb (c->RX.DSR, DSR_DMA_DISABLE);
1148 outb (c->TX.DSR, DSR_DMA_DISABLE);
1149 outb (c->CMD, CMD_TX_RESET);
1150 outb (c->CMD, CMD_TX_ABORT);
1151 outb (c->CMD, CMD_CHAN_RESET);
1153 /* disable interrupts */
1159 /* clear DTR, RTS */
1167 unsigned long ct_get_ts (ct_chan_t *c)
1169 return c->num ? c->board->opt.s1 : c->board->opt.s0;
1173 * Data transfer speed
1175 unsigned long ct_get_baud (ct_chan_t *c)
1177 unsigned long speed;
1180 if (c->mode == M_G703) {
1181 speed = 1000 * c->gopt.rate;
1182 } else if (c->mode == M_E1) {
1184 for (speed=0; ts; ts >>= 1) /* Each timeslot is 64 Kbps */
1188 speed = (c->hopt.txs == CLK_INT) ? c->baud : 0;
1193 * Turn local loopback on
1195 static void ct_enable_loop (ct_chan_t *c)
1197 if (c->mode == M_E1) {
1198 unsigned short p = c->num ? E1CS1 (c->board->port) :
1199 E1CS0 (c->board->port);
1201 /* Local loopback. */
1202 cte_out (p, DS_CCR2, cte_in (p, DS_CCR2) | CCR2_LLOOP);
1204 /* Enable jitter attenuator at the transmit side. */
1205 cte_out (p, DS_LICR, cte_in (p, DS_LICR) | LICR_JA_TX);
1207 } else if (c->mode == M_G703) {
1208 c->lx = LX_LOS | LX_HDB3;
1209 ctg_outx (c, LX_CCR1, c->lx |= LX_LLOOP);
1211 } else if (c->mode == M_HDLC && ct_get_baud(c)) {
1212 unsigned char rxs = inb (c->RXS) & ~CLK_MASK;
1213 unsigned char txs = inb (c->TXS) & ~CLK_MASK;
1214 int txcout = c->num ? BCR1_TXCOUT1 : BCR1_TXCOUT0;
1215 c->opt.md2.loop = MD2_LLOOP;
1217 /* Disable TXCOUT before changing TXS */
1218 /* to avoid two transmitters on the same line. */
1219 /* Enable it after TXS is set. */
1220 outb (BCR1(c->board->port), c->board->bcr1 & ~txcout);
1222 outb (c->RXS, rxs | CLK_INT);
1223 outb (c->TXS, txs | CLK_INT);
1225 c->board->bcr1 |= txcout;
1226 outb (BCR1(c->board->port), c->board->bcr1);
1228 outb (c->MD2, *(unsigned char*)&c->opt.md2);
1234 * Turn local loopback off
1236 static void ct_disable_loop (ct_chan_t *c)
1238 if (c->mode == M_E1) {
1239 unsigned short p = c->num ? E1CS1 (c->board->port) :
1240 E1CS0 (c->board->port);
1242 /* Local loopback. */
1243 cte_out (p, DS_CCR2, cte_in (p, DS_CCR2) & ~CCR2_LLOOP);
1245 /* Disable jitter attenuator at the transmit side. */
1246 cte_out (p, DS_LICR, cte_in (p, DS_LICR) & ~LICR_JA_TX);
1248 } else if (c->mode == M_G703) {
1249 c->lx = LX_LOS | LX_HDB3;
1250 ctg_outx (c, LX_CCR1, c->lx);
1252 } else if (c->mode == M_HDLC && ct_get_baud(c)) {
1253 unsigned char rxs = inb (c->RXS) & ~CLK_MASK;
1254 unsigned char txs = inb (c->TXS) & ~CLK_MASK;
1255 int txcout = c->num ? BCR1_TXCOUT1 : BCR1_TXCOUT0;
1256 c->opt.md2.loop = MD2_FDX;
1258 outb (BCR1(c->board->port), c->board->bcr1 & ~txcout);
1260 outb (c->RXS, rxs | CLK_LINE);
1261 if (ct_get_baud (c))
1262 outb (c->TXS, txs | CLK_INT);
1264 outb (c->TXS, txs | CLK_LINE);
1266 c->board->bcr1 |= txcout;
1267 outb (BCR1(c->board->port), c->board->bcr1);
1269 outb (c->MD2, *(unsigned char*)&c->opt.md2);
1275 * Turn local loopback on/off
1277 void ct_set_loop (ct_chan_t *c, int on)
1282 ct_disable_loop (c);
1285 int ct_get_loop (ct_chan_t *c)
1287 if (c->mode == M_E1) {
1288 unsigned short p = c->num ? E1CS1 (c->board->port) :
1289 E1CS0 (c->board->port);
1291 return cte_in (p, DS_CCR2) & CCR2_LLOOP;
1293 if (c->mode == M_G703)
1294 return c->lx & LX_LLOOP;
1297 return (c->opt.md2.loop & MD2_LLOOP) != 0;
1300 void ct_set_phony (ct_chan_t *c, int on)
1302 /* Valid only for TauPCI-E1. */
1303 if (c->board->type != B_TAU_E1D &&
1304 c->board->type != B_TAU2_E1D)
1306 c->gopt.phony = (on != 0);
1307 if (c->gopt.phony) {
1308 c->board->e1syn |= c->num ? E1SYN_ENS1 : E1SYN_ENS0;
1309 /* No receive/transmit crc. */
1310 c->hopt.md0.crc = 0;
1312 c->board->e1syn &= ~(c->num ? E1SYN_ENS1 : E1SYN_ENS0);
1314 c->hopt.md0.crc = 1;
1316 outb (c->MD0, BYTE c->hopt.md0);
1317 outb (E1SYN(c->board->port), c->board->e1syn);
1320 void ct_start_receiver (ct_chan_t *c, int dma, unsigned long buf,
1321 unsigned len, unsigned long desc, unsigned long lim)
1323 int ier0 = inb (IER0(c->board->port));
1324 int ier1 = inb (IER1(c->board->port));
1325 int ier2 = inb (IER2(c->board->port));
1326 int ie0 = inb (c->IE0);
1327 int ie2 = inb (c->IE2);
1330 ier1 |= c->num ? (IER1_RX_DMERE_1 | IER1_RX_DME_1) :
1331 (IER1_RX_DMERE_0 | IER1_RX_DME_0);
1332 if (c->mode == M_ASYNC) {
1333 ier0 |= c->num ? IER0_RX_INTE_1 : IER0_RX_INTE_0;
1335 ie2 |= IE2_OVRNE | IE2_ASYNC_FRMEE | IE2_ASYNC_PEE;
1338 ier0 |= c->num ? (IER0_RX_INTE_1 | IER0_RX_RDYE_1) :
1339 (IER0_RX_INTE_0 | IER0_RX_RDYE_0);
1340 ie0 |= IE0_RX_INTE | IE0_RX_RDYE;
1345 outb (c->RX.TEPR, TEPR_64); /* prescale to 16 kHz */
1346 outw (c->RX.TCONR, 160); /* period is 10 msec */
1347 outw (c->RX.TCNT, 0);
1348 outb (c->RX.TCSR, TCSR_ENABLE | TCSR_INTR);
1349 ier2 |= c->num ? IER2_RX_TME_1 : IER2_RX_TME_0;
1352 /* Enable interrupts. */
1353 outb (IER0(c->board->port), ier0);
1354 outb (IER1(c->board->port), ier1);
1355 outb (IER2(c->board->port), ier2);
1359 /* RXRDY:=1 when the receive buffer has more than RRC chars */
1360 outb (c->RRC, dma ? c->opt.dma_rrc : c->opt.pio_rrc);
1362 /* Start receiver. */
1364 outb (c->RX.DCR, DCR_ABORT);
1365 if (c->mode == M_ASYNC) {
1366 /* Single-buffer DMA mode. */
1367 outb (c->RX.DARB, (unsigned char) (buf >> 16));
1368 outw (c->RX.DAR, (unsigned short) buf);
1369 outw (c->RX.BCR, len);
1370 outb (c->RX.DIR, DIR_EOTE);
1372 /* Chained-buffer DMA mode. */
1373 outb (c->RX.SARB, (unsigned char) (desc >> 16));
1374 outw (c->RX.EDA, (unsigned short) lim);
1375 outw (c->RX.CDA, (unsigned short) desc);
1376 outw (c->RX.BFL, len);
1377 outb (c->RX.DIR, DIR_CHAIN_EOME | DIR_CHAIN_BOFE |
1380 outb (c->RX.DSR, DSR_DMA_ENABLE);
1382 outb (c->CMD, CMD_RX_ENABLE);
1385 void ct_start_transmitter (ct_chan_t *c, int dma, unsigned long buf,
1386 unsigned len, unsigned long desc, unsigned long lim)
1388 int ier0 = inb (IER0(c->board->port));
1389 int ier1 = inb (IER1(c->board->port));
1390 int ie0 = inb (c->IE0);
1391 int ie1 = inb (c->IE1);
1393 /* Enable underrun interrupt in HDLC and raw modes. */
1394 if (c->mode != M_ASYNC) {
1395 ier0 |= c->num ? IER0_TX_INTE_1 : IER0_TX_INTE_0;
1397 ie1 |= IE1_HDLC_UDRNE;
1400 ier1 |= c->num ? (IER1_TX_DMERE_1 | IER1_TX_DME_1) :
1401 (IER1_TX_DMERE_0 | IER1_TX_DME_0);
1403 ier0 |= c->num ? IER0_TX_RDYE_1 : IER0_TX_RDYE_0;
1407 /* Enable interrupts. */
1408 outb (IER0(c->board->port), ier0);
1409 outb (IER1(c->board->port), ier1);
1413 /* TXRDY:=1 when the transmit buffer has TRC0 or less chars,
1414 * TXRDY:=0 when the transmit buffer has more than TRC1 chars */
1415 outb (c->TRC0, dma ? c->opt.dma_trc0 : c->opt.pio_trc0);
1416 outb (c->TRC1, dma ? c->opt.dma_trc1 : c->opt.pio_trc1);
1418 /* Start transmitter. */
1420 outb (c->TX.DCR, DCR_ABORT);
1421 if (c->mode == M_ASYNC) {
1422 /* Single-buffer DMA mode. */
1423 outb (c->TX.SARB, (unsigned char) (buf >> 16));
1424 outw (c->TX.SAR, (unsigned short) buf);
1425 outw (c->TX.BCR, len);
1426 outb (c->TX.DIR, DIR_EOTE);
1428 /* Chained-buffer DMA mode. */
1429 outb (c->TX.SARB, (unsigned char) (desc >> 16));
1430 outw (c->TX.EDA, (unsigned short) lim);
1431 outw (c->TX.CDA, (unsigned short) desc);
1432 outb (c->TX.DIR, /* DIR_CHAIN_EOME | */ DIR_CHAIN_BOFE |
1435 /* Set DSR_DMA_ENABLE to begin! */
1437 outb (c->CMD, CMD_TX_ENABLE);
1440 if (c->board->type == B_TAU_G703) {
1441 outb (GERR(c->board->port), 0xff);
1442 outb (GLDR(c->board->port), 0xff);
1447 * Control DTR signal for the channel.
1450 void ct_set_dtr (ct_chan_t *c, int on)
1454 c->board->bcr1 |= c->num ? BCR1_DTR1 : BCR1_DTR0;
1457 c->board->bcr1 &= ~(c->num ? BCR1_DTR1 : BCR1_DTR0);
1459 outb (BCR1(c->board->port), c->board->bcr1);
1463 * Control RTS signal for the channel.
1466 void ct_set_rts (ct_chan_t *c, int on)
1470 outb (c->CTL, inb (c->CTL) & ~CTL_RTS_INV);
1472 outb (c->CTL, inb (c->CTL) | CTL_RTS_INV);
1476 * Control BREAK state in asynchronous mode.
1479 void ct_set_brk (ct_chan_t *c, int on)
1481 if (c->mode != M_ASYNC)
1484 outb (c->CTL, inb (c->CTL) | CTL_BRK);
1486 outb (c->CTL, inb (c->CTL) & ~CTL_BRK);
1490 * Get the state of DSR signal of the channel.
1492 int ct_get_dsr (ct_chan_t *c)
1494 return (inb (BSR1(c->board->port)) &
1495 (c->num ? BSR1_DSR1 : BSR1_DSR0)) != 0;
1499 * Get the G.703 line signal level.
1501 int ct_get_lq (ct_chan_t *c)
1503 unsigned char q1, q2, q3;
1504 static int lq_to_santibells [] = { 0, 95, 195, 285 };
1507 if (! (c->type & T_G703))
1509 q1 = inb (GLQ (c->board->port));
1510 /* Repeat reading the register to produce a 10-usec delay. */
1511 for (i=0; i<20; ++i)
1512 q2 = inb (GLQ (c->board->port));
1513 for (i=0; i<20; ++i)
1514 q3 = inb (GLQ (c->board->port));
1523 if (q1 <= q2 && q2 <= q3) return lq_to_santibells [q2];
1524 if (q2 <= q3 && q3 <= q1) return lq_to_santibells [q3];
1525 if (q3 <= q1 && q1 <= q2) return lq_to_santibells [q1];
1526 if (q1 <= q3 && q3 <= q2) return lq_to_santibells [q3];
1527 if (q3 <= q2 && q2 <= q1) return lq_to_santibells [q2];
1528 /* if (q2 <= q1 && q1 <= q3) */ return lq_to_santibells [q1];
1532 * Get the state of CARRIER signal of the channel.
1534 int ct_get_cd (ct_chan_t *c)
1536 return (inb (c->ST3) & ST3_DCD_INV) == 0;
1540 * Get the state of CTS signal of the channel.
1542 int ct_get_cts (ct_chan_t *c)
1544 return (inb (c->ST3) & ST3_CTS_INV) == 0;
1550 void ct_led (ct_board_t *b, int on)
1556 if (on) b->gmd2 |= GMD2_LED;
1557 else b->gmd2 &= ~GMD2_LED;
1558 outb (GMD2(b->port), b->gmd2);
1561 if (on) b->e1cfg |= E1CFG_LED;
1562 else b->e1cfg &= ~E1CFG_LED;
1563 outb (E1CFG(b->port), b->e1cfg);
1568 void ct_disable_dma (ct_board_t *b)
1570 /* Disable DMA channel. */
1571 outb (DMA_MASK, (b->dma & 3) | DMA_MASK_CLEAR);
1574 void ct_compute_clock (long hz, long baud, int *txbr, int *tmc)
1582 while (((hz / baud) >> ++*txbr) > 256)
1584 *tmc = (((2 * hz / baud) >> *txbr) + 1) / 2;
1589 * Access to DS2153 chips on the Tau/E1 board.
1591 * val = cte_in (E1CSi (base), DS_RCR1)
1592 * cte_out (E1CSi (base), DS_CCR1, val)
1593 * val = cte_ins (E1CSi (base), DS_SSR)
1595 unsigned char cte_in (port_t base, unsigned char reg)
1598 return inb (E1DAT (base & 0x3e0));
1601 void cte_out (port_t base, unsigned char reg, unsigned char val)
1604 outb (E1DAT (base & 0x3e0), val);
1608 * Get the DS2153 status register, using write-read-write scheme.
1610 unsigned char cte_ins (port_t base, unsigned char reg,
1614 port_t rw = E1DAT (base & 0x3e0);
1616 outb (base, reg); outb (rw, mask); /* lock bits */
1617 outb (base, reg); val = inb (rw) & mask; /* get values */
1618 outb (base, reg); outb (rw, val); /* unlock bits */
1623 * Access to 8530 chip on the Tau/E1 board.
1625 * val = cte_in2 (base, AM_RSR | AM_A)
1626 * cte_out2 (base, AM_IMR, val)
1628 unsigned char cte_in2 (port_t base, unsigned char reg)
1630 outb (E1CS2(base), E1CS2_SCC | reg >> 4);
1631 outb (E1DAT(base), reg & 15);
1632 return inb (E1DAT(base));
1635 void cte_out2 (port_t base, unsigned char reg, unsigned char val)
1637 outb (E1CS2(base), E1CS2_SCC | reg >> 4);
1638 outb (E1DAT(base), reg & 15);
1639 outb (E1DAT(base), val);
1643 * Read the data from the 8530 receive fifo.
1645 unsigned char cte_in2d (ct_chan_t *c)
1647 outb (E1CS2(c->board->port), E1CS2_SCC | E1CS2_DC |
1648 (c->num ? 0 : E1CS2_AB));
1649 return inb (E1DAT(c->board->port));
1653 * Send the 8530 command.
1655 void cte_out2c (ct_chan_t *c, unsigned char val)
1657 outb (E1CS2(c->board->port), E1CS2_SCC | (c->num ? 0 : E1CS2_AB));
1658 outb (E1DAT(c->board->port), val);
1662 * Write the data to the 8530 transmit fifo.
1664 void cte_out2d (ct_chan_t *c, unsigned char val)
1666 outb (E1CS2(c->board->port), E1CS2_SCC | E1CS2_DC |
1667 (c->num ? 0 : E1CS2_AB));
1668 outb (E1DAT(c->board->port), val);
1672 * Access to LXT318 chip on the Tau/G.703 board.
1677 static void ctg_output (port_t port, unsigned char val, unsigned char v0)
1681 for (i=0; i<8; ++i) {
1682 unsigned char v = v0;
1689 outb (port, v | GMD0_SCLK);
1690 outb (port, v | GMD0_SCLK);
1691 outb (port, v | GMD0_SCLK);
1692 outb (port, v | GMD0_SCLK);
1697 void ctg_outx (ct_chan_t *c, unsigned char reg, unsigned char val)
1699 port_t port = GMD0(c->board->port);
1701 outb (GMD1(c->board->port), c->board->gmd1 | GMD1_NCS0 | GMD1_NCS1);
1702 outb (GMD1(c->board->port), c->board->gmd1 |
1703 (c->num ? GMD1_NCS0 : GMD1_NCS1));
1704 ctg_output (port, (reg << 1) | LX_WRITE, c->board->gmd0);
1705 ctg_output (port, val, c->board->gmd0);
1706 outb (GMD1(c->board->port), c->board->gmd1 | GMD1_NCS0 | GMD1_NCS1);
1709 unsigned char ctg_inx (ct_chan_t *c, unsigned char reg)
1711 port_t port = GMD0(c->board->port);
1712 port_t data = GLDR(c->board->port);
1713 unsigned char val = 0, mask = c->num ? GLDR_C1 : GLDR_C0;
1716 outb (GMD1(c->board->port), c->board->gmd1 | GMD1_NCS0 | GMD1_NCS1);
1717 outb (GMD1(c->board->port), c->board->gmd1 |
1718 (c->num ? GMD1_NCS0 : GMD1_NCS1));
1719 ctg_output (port, (reg << 1) | LX_READ, c->board->gmd0);
1720 for (i=0; i<8; ++i) {
1721 outb (port, c->board->gmd0 | GMD0_SCLK);
1722 if (inb (data) & mask)
1724 outb (port, c->board->gmd0);
1726 outb (GMD1(c->board->port), c->board->gmd1 | GMD1_NCS0 | GMD1_NCS1);
1733 ct_board_opt_t ct_board_opt_dflt = {
1734 0, /* board control register 2 */
1735 { /* DMA priority control register */
1737 0, /* all channels share the the bus hold */
1738 0, /* hold the bus until all transfers done */
1740 CFG_A, /* E1/G.703 config: two independent channels */
1741 GCLK_INT, /* E1/G.703 chan 0 internal tx clock source */
1742 GCLK_INT, /* E1/G.703 chan 1 internal tx clock source */
1743 ~0UL << 1, /* E1 channel 0 timeslots 1..31 */
1744 ~0UL << 1, /* E1 channel 1 timeslots 1..31 */
1745 0, /* no E1 subchannel timeslots */
1749 * Mode-independent channel options
1751 ct_chan_opt_t ct_chan_opt_dflt = {
1752 { /* mode register 2 */
1753 MD2_FDX, /* full duplex communication */
1754 0, /* empty ADPLL clock rate */
1755 MD2_ENCOD_NRZ, /* NRZ encoding */
1757 /* DMA mode FIFO marks */
1758 15, 24, 30, /* rx ready, tx empty, tx full */
1759 /* port i/o mode FIFO marks */
1760 15, 16, 30, /* rx ready, tx empty, tx full */
1764 * Default HDLC options
1766 ct_opt_hdlc_t ct_opt_hdlc_dflt = {
1767 { /* mode register 0 */
1768 1, /* CRC preset to all ones (V.41) */
1771 0, /* disable automatic CTS/DCD */
1772 MD0_MODE_HDLC, /* HDLC mode */
1774 { /* mode register 1 */
1775 MD1_ADDR_NOCHK, /* do not check address field */
1777 CTL_IDLE_PTRN | CTL_UDRN_ABORT | CTL_RTS_INV, /* control register */
1778 0, 0, /* empty sync/address registers 0,1 */
1779 CLK_LINE, /* receive clock source: RXC line input */
1780 CLK_LINE, /* transmit clock source: TXC line input */
1784 * Default E1/G.703 options
1786 ct_opt_g703_t ct_opt_g703_dflt = {
1787 1, /* HDB3 enable */
1788 0, /* precoder disable */
1789 GTEST_DIS, /* test disabled, normal operation */
1790 0, /* CRC4 disable */
1791 0, /* CCS signaling */
1793 0, /* no raw mode */
1794 0, /* no PCM2 precoder compatibility */
1795 2048, /* data rate 2048 kbit/sec */