2 * Low-level subroutines for Cronyx-Sigma adapter.
4 * Copyright (C) 1994-2000 Cronyx Engineering.
5 * Author: Serge Vakulenko, <vak@cronyx.ru>
7 * This software is distributed with NO WARRANTIES, not even the implied
8 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 * Authors grant any other persons or organisations permission to use
11 * or modify this software as long as this message is kept with the software,
12 * all derivative works or modified versions.
14 * Cronyx Id: csigma.c,v 1.1.2.1 2003/11/12 17:13:41 rik Exp $
17 #include <dev/cx/machdep.h>
18 #include <dev/cx/cxddk.h>
19 #include <dev/cx/cxreg.h>
20 #include <dev/cx/cronyxfw.h>
22 #define DMA_MASK 0xd4 /* DMA mask register */
23 #define DMA_MASK_CLEAR 0x04 /* DMA clear mask */
24 #define DMA_MODE 0xd6 /* DMA mode register */
25 #define DMA_MODE_MASTER 0xc0 /* DMA master mode */
27 #define BYTE *(unsigned char*)&
29 static unsigned char irqmask [] = {
30 BCR0_IRQ_DIS, BCR0_IRQ_DIS, BCR0_IRQ_DIS, BCR0_IRQ_3,
31 BCR0_IRQ_DIS, BCR0_IRQ_5, BCR0_IRQ_DIS, BCR0_IRQ_7,
32 BCR0_IRQ_DIS, BCR0_IRQ_DIS, BCR0_IRQ_10, BCR0_IRQ_11,
33 BCR0_IRQ_12, BCR0_IRQ_DIS, BCR0_IRQ_DIS, BCR0_IRQ_15,
36 static unsigned char dmamask [] = {
37 BCR0_DMA_DIS, BCR0_DMA_DIS, BCR0_DMA_DIS, BCR0_DMA_DIS,
38 BCR0_DMA_DIS, BCR0_DMA_5, BCR0_DMA_6, BCR0_DMA_7,
41 /* standard base port set */
42 static short porttab [] = {
43 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
44 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
47 /* valid IRQs and DRQs */
48 static short irqtab [] = { 3, 5, 7, 10, 11, 12, 15, 0 };
49 static short dmatab [] = { 5, 6, 7, 0 };
51 static int valid (short value, short *list)
59 long cx_rxbaud = 9600; /* receiver baud rate */
60 long cx_txbaud = 9600; /* transmitter baud rate */
62 int cx_univ_mode = M_HDLC; /* univ. chan. mode: async or sync */
63 int cx_sync_mode = M_HDLC; /* sync. chan. mode: HDLC, Bisync or X.21 */
64 int cx_iftype = 0; /* univ. chan. interface: upper/lower */
66 static int cx_probe_chip (port_t base);
67 static void cx_setup_chip (cx_chan_t *c);
70 * Wait for CCR to clear.
72 void cx_cmd (port_t base, int cmd)
74 port_t port = CCR(base);
77 /* Wait 10 msec for the previous command to complete. */
78 for (count=0; inb(port) && count<20000; ++count)
81 /* Issue the command. */
84 /* Wait 10 msec for the command to complete. */
85 for (count=0; inb(port) && count<20000; ++count)
92 static int cx_reset (port_t port)
96 /* Wait up to 10 msec for revision code to appear after reset. */
97 for (count=0; count<20000; ++count)
98 if (inb(GFRCR(port)) != 0)
101 cx_cmd (port, CCR_RSTALL);
103 /* Firmware revision code should clear imediately. */
104 /* Wait up to 10 msec for revision code to appear again. */
105 for (count=0; count<20000; ++count)
106 if (inb(GFRCR(port)) != 0)
113 int cx_download (port_t port, const unsigned char *firmware, long bits,
114 const cr_dat_tst_t *tst)
116 unsigned char cr2, sr;
117 long i, n, maxn = (bits + 7) / 8;
121 for (i=n=0; n<maxn; ++n) {
122 v = ((firmware[n] ^ ' ') << 1) | (firmware[n] >> 7 & 1);
123 for (b=0; b<7; b+=2, i+=2) {
127 if (v >> b & 1) cr2 |= BCR2_TMS;
128 if (v >> b & 2) cr2 |= BCR2_TDI;
129 outb (BCR2(port), cr2);
130 sr = inb (BSR(port));
131 outb (BCR0(port), BCR0800_TCK);
132 outb (BCR0(port), 0);
135 if (i >= tst->start && (sr & BSR800_LERR))
143 * Check if the Sigma-XXX board is present at the given base port.
145 static int cx_probe_chained_board (port_t port, int *c0, int *c1)
149 /* Read and check the board revision code. */
150 rev = inb (BSR(port));
152 switch (rev & BSR_VAR_MASK) {
153 case CRONYX_100: *c0 = 1; break;
154 case CRONYX_400: *c1 = 1; break;
155 case CRONYX_500: *c0 = *c1 = 1; break;
156 case CRONYX_410: *c0 = 1; break;
157 case CRONYX_810: *c0 = *c1 = 1; break;
158 case CRONYX_410s: *c0 = 1; break;
159 case CRONYX_810s: *c0 = *c1 = 1; break;
160 case CRONYX_440: *c0 = 1; break;
161 case CRONYX_840: *c0 = *c1 = 1; break;
162 case CRONYX_401: *c0 = 1; break;
163 case CRONYX_801: *c0 = *c1 = 1; break;
164 case CRONYX_401s: *c0 = 1; break;
165 case CRONYX_801s: *c0 = *c1 = 1; break;
166 case CRONYX_404: *c0 = 1; break;
167 case CRONYX_703: *c0 = *c1 = 1; break;
168 default: return (0); /* invalid variant code */
171 switch (rev & BSR_OSC_MASK) {
172 case BSR_OSC_20: /* 20 MHz */
173 case BSR_OSC_18432: /* 18.432 MHz */
176 return (0); /* oscillator frequency does not match */
179 for (i=2; i<0x10; i+=2)
180 if ((inb (BSR(port)+i) & BSR_REV_MASK) != (rev & BSR_REV_MASK))
181 return (0); /* status changed? */
186 * Check if the Sigma-800 board is present at the given base port.
187 * Read board status register 1 and check identification bits
188 * which should invert every next read.
190 static int cx_probe_800_chained_board (port_t port)
192 unsigned char det, odet;
195 odet = inb (BDET(port));
196 if ((odet & (BDET_IB | BDET_IB_NEG)) != BDET_IB &&
197 (odet & (BDET_IB | BDET_IB_NEG)) != BDET_IB_NEG)
199 for (i=0; i<100; ++i) {
200 det = inb (BDET(port));
201 if (((det ^ odet) & (BDET_IB | BDET_IB_NEG)) !=
202 (BDET_IB | BDET_IB_NEG))
206 /* Reset the controller. */
207 outb (BCR0(port), 0);
208 outb (BCR1(port), 0);
209 outb (BCR2(port), 0);
214 * Check if the Sigma-2x board is present at the given base port.
216 static int cx_probe_2x_board (port_t port)
220 /* Read and check the board revision code. */
221 rev = inb (BSR(port));
222 if ((rev & BSR2X_VAR_MASK) != CRONYX_22 &&
223 (rev & BSR2X_VAR_MASK) != CRONYX_24)
224 return (0); /* invalid variant code */
226 for (i=2; i<0x10; i+=2)
227 if ((inb (BSR(port)+i) & BSR2X_REV_MASK) !=
228 (rev & BSR2X_REV_MASK))
229 return (0); /* status changed? */
234 * Check if the Cronyx-Sigma board is present at the given base port.
236 int cx_probe_board (port_t port, int irq, int dma)
238 int c0, c1, c2=0, c3=0, result;
240 if (! valid (port, porttab))
243 if (irq > 0 && ! valid (irq, irqtab))
246 if (dma > 0 && ! valid (dma, dmatab))
249 if (cx_probe_800_chained_board (port)) {
250 /* Sigma-800 detected. */
251 if (! (inb (BSR(port)) & BSR_NOCHAIN)) {
252 /* chained board attached */
253 if (! cx_probe_800_chained_board (port+0x10))
254 /* invalid chained board? */
256 if (! (inb (BSR(port+0x10)) & BSR_NOCHAIN))
257 /* invalid chained board flag? */
262 if (cx_probe_chained_board (port, &c0, &c1)) {
263 /* Sigma-XXX detected. */
264 if (! (inb (BSR(port)) & BSR_NOCHAIN)) {
265 /* chained board attached */
266 if (! cx_probe_chained_board (port+0x10, &c2, &c3))
267 /* invalid chained board? */
269 if (! (inb (BSR(port+0x10)) & BSR_NOCHAIN))
270 /* invalid chained board flag? */
273 } else if (cx_probe_2x_board (port)) {
274 c0 = 1; /* Sigma-2x detected. */
277 return (0); /* no board detected */
279 /* Turn off the reset bit. */
280 outb (BCR0(port), BCR0_NORESET);
282 outb (BCR0(port + 0x10), BCR0_NORESET);
285 if (c0 && ! cx_probe_chip (CS0(port)))
286 result = 0; /* no CD2400 chip here */
287 else if (c1 && ! cx_probe_chip (CS1A(port)) &&
288 ! cx_probe_chip (CS1(port)))
289 result = 0; /* no second CD2400 chip */
290 else if (c2 && ! cx_probe_chip (CS0(port + 0x10)))
291 result = 0; /* no CD2400 chip on the slave board */
292 else if (c3 && ! cx_probe_chip (CS1(port + 0x10)))
293 result = 0; /* no second CD2400 chip on the slave board */
295 /* Reset the controller. */
296 outb (BCR0(port), 0);
298 outb (BCR0(port + 0x10), 0);
300 /* Yes, we really have valid Sigma board. */
305 * Check if the CD2400 chip is present at the given base port.
307 static int cx_probe_chip (port_t base)
309 int rev, newrev, count;
311 /* Wait up to 10 msec for revision code to appear after reset. */
313 for (count=0; rev==0; ++count) {
315 return (0); /* reset failed */
316 rev = inb (GFRCR(base));
319 /* Read and check the global firmware revision code. */
320 if (! (rev>=REVCL_MIN && rev<=REVCL_MAX) &&
321 ! (rev>=REVCL31_MIN && rev<=REVCL31_MAX))
322 return (0); /* CD2400/2431 revision does not match */
324 /* Reset the chip. */
325 if (! cx_reset (base))
328 /* Read and check the new global firmware revision code. */
329 newrev = inb (GFRCR(base));
331 return (0); /* revision changed */
333 /* Yes, we really have CD2400/2431 chip here. */
338 * Check that the irq is functional.
339 * irq>0 - activate the interrupt from the adapter (irq=on)
340 * irq<0 - deactivate the interrupt (irq=off)
341 * irq==0 - free the interrupt line (irq=tri-state)
342 * Return the interrupt mask _before_ activating irq.
344 int cx_probe_irq (cx_board_t *b, int irq)
349 rev = inb (BSR(b->port));
350 port = ((rev & BSR_VAR_MASK) != CRONYX_400) ? CS0(b->port) : CS1(b->port);
355 mask |= inb (0xa0) << 8;
358 outb (BCR0(b->port), BCR0_NORESET | irqmask[irq]);
360 cx_cmd (port, CCR_CLRCH);
361 outb (CMR(port), CMR_HDLC);
362 outb (TCOR(port), 0);
363 outb (TBPR(port), 1);
364 cx_cmd (port, CCR_INITCH | CCR_ENTX);
365 outb (IER(port), IER_TXMPTY);
366 } else if (irq < 0) {
369 outb (0xa0, 0x60 | ((-irq) & 7));
372 outb (0x20, 0x60 | (-irq));
374 outb (BCR0(b->port), 0);
378 static int cx_chip_revision (port_t port, int rev)
382 /* Model 400 has no first chip. */
383 port = ((rev & BSR_VAR_MASK) != CRONYX_400) ? CS0(port) : CS1(port);
385 /* Wait up to 10 msec for revision code to appear after reset. */
386 for (count=0; inb(GFRCR(port))==0; ++count)
388 return (0); /* reset failed */
390 return inb (GFRCR (port));
394 * Probe and initialize the board structure.
396 void cx_init (cx_board_t *b, int num, port_t port, int irq, int dma)
398 int gfrcr, rev, chain, mod = 0, rev2 = 0, mod2 = 0;
400 rev = inb (BSR(port));
401 chain = ! (rev & BSR_NOCHAIN);
402 if (cx_probe_800_chained_board (port)) {
403 cx_init_800 (b, num, port, irq, dma, chain);
406 if ((rev & BSR2X_VAR_MASK) == CRONYX_22 ||
407 (rev & BSR2X_VAR_MASK) == CRONYX_24) {
408 cx_init_2x (b, num, port, irq, dma,
409 (rev & BSR2X_VAR_MASK), (rev & BSR2X_OSC_33));
413 outb (BCR0(port), BCR0_NORESET);
415 outb (BCR0(port+0x10), BCR0_NORESET);
416 gfrcr = cx_chip_revision (port, rev);
417 if (gfrcr >= REVCL31_MIN && gfrcr <= REVCL31_MAX)
420 rev2 = inb (BSR(port+0x10));
421 gfrcr = cx_chip_revision (port+0x10, rev2);
422 if (gfrcr >= REVCL31_MIN && gfrcr <= REVCL31_MAX)
424 outb (BCR0(port+0x10), 0);
426 outb (BCR0(port), 0);
428 cx_init_board (b, num, port, irq, dma, chain,
429 (rev & BSR_VAR_MASK), (rev & BSR_OSC_MASK), mod,
430 (rev2 & BSR_VAR_MASK), (rev2 & BSR_OSC_MASK), mod2);
434 * Initialize the board structure, given the type of the board.
436 void cx_init_board (cx_board_t *b, int num, port_t port, int irq, int dma,
437 int chain, int rev, int osc, int mod, int rev2, int osc2, int mod2)
443 /* Initialize board structure. */
448 b->opt = board_opt_dflt;
450 b->type = B_SIGMA_XXX;
451 b->if0type = b->if8type = cx_iftype;
453 /* Set channels 0 and 8 mode, set DMA and IRQ. */
454 b->bcr0 = b->bcr0b = BCR0_NORESET | dmamask[b->dma] | irqmask[b->irq];
456 /* Clear DTR[0..3] and DTR[8..12]. */
457 b->bcr1 = b->bcr1b = 0;
459 /*------------------ Master board -------------------*/
461 /* Read and check the board revision code. */
462 strcpy (b->name, mod ? "m" : "");
464 default: type = ""; break;
465 case CRONYX_100: type = "100"; break;
466 case CRONYX_400: type = "400"; break;
467 case CRONYX_500: type = "500"; break;
468 case CRONYX_410: type = "410"; break;
469 case CRONYX_810: type = "810"; break;
470 case CRONYX_410s: type = "410s"; break;
471 case CRONYX_810s: type = "810s"; break;
472 case CRONYX_440: type = "440"; break;
473 case CRONYX_840: type = "840"; break;
474 case CRONYX_401: type = "401"; break;
475 case CRONYX_801: type = "801"; break;
476 case CRONYX_401s: type = "401s"; break;
477 case CRONYX_801s: type = "801s"; break;
478 case CRONYX_404: type = "404"; break;
479 case CRONYX_703: type = "703"; break;
481 strcat (b->name, type);
485 case BSR_OSC_20: /* 20 MHz */
486 b->chan[0].oscfreq = b->chan[1].oscfreq =
487 b->chan[2].oscfreq = b->chan[3].oscfreq =
488 b->chan[4].oscfreq = b->chan[5].oscfreq =
489 b->chan[6].oscfreq = b->chan[7].oscfreq =
490 mod ? 33000000L : 20000000L;
491 strcat (b->name, "a");
493 case BSR_OSC_18432: /* 18.432 MHz */
494 b->chan[0].oscfreq = b->chan[1].oscfreq =
495 b->chan[2].oscfreq = b->chan[3].oscfreq =
496 b->chan[4].oscfreq = b->chan[5].oscfreq =
497 b->chan[6].oscfreq = b->chan[7].oscfreq =
498 mod ? 20000000L : 18432000L;
499 strcat (b->name, "b");
503 /*------------------ Slave board -------------------*/
506 /* Read and check the board revision code. */
507 strcat (b->name, mod2 ? "/m" : "/");
509 default: type = ""; break;
510 case CRONYX_100: type = "100"; break;
511 case CRONYX_400: type = "400"; break;
512 case CRONYX_500: type = "500"; break;
513 case CRONYX_410: type = "410"; break;
514 case CRONYX_810: type = "810"; break;
515 case CRONYX_410s: type = "410s"; break;
516 case CRONYX_810s: type = "810s"; break;
517 case CRONYX_440: type = "440"; break;
518 case CRONYX_840: type = "840"; break;
519 case CRONYX_401: type = "401"; break;
520 case CRONYX_801: type = "801"; break;
521 case CRONYX_401s: type = "401s"; break;
522 case CRONYX_801s: type = "801s"; break;
523 case CRONYX_404: type = "404"; break;
524 case CRONYX_703: type = "703"; break;
526 strcat (b->name, type);
530 case BSR_OSC_20: /* 20 MHz */
531 b->chan[8].oscfreq = b->chan[9].oscfreq =
532 b->chan[10].oscfreq = b->chan[11].oscfreq =
533 b->chan[12].oscfreq = b->chan[13].oscfreq =
534 b->chan[14].oscfreq = b->chan[15].oscfreq =
535 mod2 ? 33000000L : 20000000L;
536 strcat (b->name, "a");
538 case BSR_OSC_18432: /* 18.432 MHz */
539 b->chan[8].oscfreq = b->chan[9].oscfreq =
540 b->chan[10].oscfreq = b->chan[11].oscfreq =
541 b->chan[12].oscfreq = b->chan[13].oscfreq =
542 b->chan[14].oscfreq = b->chan[15].oscfreq =
543 mod2 ? 20000000L : 18432000L;
544 strcat (b->name, "b");
549 /* Initialize channel structures. */
550 for (i=0; i<4; ++i) {
551 b->chan[i+0].port = CS0(port);
552 b->chan[i+4].port = cx_probe_chip (CS1A(port)) ?
553 CS1A(port) : CS1(port);
554 b->chan[i+8].port = CS0(port+0x10);
555 b->chan[i+12].port = CS1(port+0x10);
557 for (c=b->chan; c<b->chan+NCHAN; ++c) {
559 c->num = c - b->chan;
563 /*------------------ Master board -------------------*/
568 b->chan[i].type = T_UNIV_RS232;
571 b->chan[0].type = T_UNIV_RS232;
574 b->chan[0].type = T_UNIV_RS232;
576 b->chan[i].type = T_UNIV_RS232;
579 b->chan[0].type = T_UNIV_V35;
581 b->chan[i].type = T_UNIV_RS232;
584 b->chan[0].type = T_UNIV_V35;
586 b->chan[i].type = T_UNIV_RS232;
589 b->chan[0].type = T_UNIV_V35;
591 b->chan[i].type = T_SYNC_RS232;
594 b->chan[0].type = T_UNIV_V35;
596 b->chan[i].type = T_SYNC_RS232;
598 b->chan[i].type = T_UNIV_RS232;
601 b->chan[0].type = T_UNIV_V35;
603 b->chan[i].type = T_SYNC_V35;
606 b->chan[0].type = T_UNIV_V35;
608 b->chan[i].type = T_SYNC_V35;
610 b->chan[i].type = T_UNIV_RS232;
613 b->chan[0].type = T_UNIV_RS449;
615 b->chan[i].type = T_UNIV_RS232;
618 b->chan[0].type = T_UNIV_RS449;
620 b->chan[i].type = T_UNIV_RS232;
623 b->chan[0].type = T_UNIV_RS449;
625 b->chan[i].type = T_SYNC_RS232;
628 b->chan[0].type = T_UNIV_RS449;
630 b->chan[i].type = T_SYNC_RS232;
632 b->chan[i].type = T_UNIV_RS232;
635 b->chan[0].type = T_UNIV_RS449;
637 b->chan[i].type = T_SYNC_RS449;
640 b->chan[0].type = T_UNIV_RS449;
642 b->chan[i].type = T_SYNC_RS449;
644 b->chan[i].type = T_UNIV_RS232;
648 /*------------------ Slave board -------------------*/
655 b->chan[8].type = T_UNIV_RS232;
658 b->chan[8].type = T_UNIV_RS232;
659 for (i=12; i<16; ++i)
660 b->chan[i].type = T_UNIV_RS232;
663 b->chan[8].type = T_UNIV_V35;
665 b->chan[i].type = T_UNIV_RS232;
668 b->chan[8].type = T_UNIV_V35;
670 b->chan[i].type = T_UNIV_RS232;
673 b->chan[8].type = T_UNIV_V35;
675 b->chan[i].type = T_SYNC_RS232;
678 b->chan[8].type = T_UNIV_V35;
680 b->chan[i].type = T_SYNC_RS232;
681 for (i=12; i<16; ++i)
682 b->chan[i].type = T_UNIV_RS232;
685 b->chan[8].type = T_UNIV_V35;
687 b->chan[i].type = T_SYNC_V35;
690 b->chan[8].type = T_UNIV_V35;
692 b->chan[i].type = T_SYNC_V35;
693 for (i=12; i<16; ++i)
694 b->chan[i].type = T_UNIV_RS232;
697 b->chan[8].type = T_UNIV_RS449;
699 b->chan[i].type = T_UNIV_RS232;
702 b->chan[8].type = T_UNIV_RS449;
704 b->chan[i].type = T_UNIV_RS232;
707 b->chan[8].type = T_UNIV_RS449;
709 b->chan[i].type = T_UNIV_RS232;
712 b->chan[8].type = T_UNIV_RS449;
714 b->chan[i].type = T_SYNC_RS232;
715 for (i=12; i<16; ++i)
716 b->chan[i].type = T_UNIV_RS232;
719 b->chan[8].type = T_UNIV_RS449;
721 b->chan[i].type = T_SYNC_RS449;
724 b->chan[8].type = T_UNIV_RS449;
726 b->chan[i].type = T_SYNC_RS449;
727 for (i=12; i<16; ++i)
728 b->chan[i].type = T_UNIV_RS232;
733 b->nuniv = b->nsync = b->nasync = 0;
734 for (c=b->chan; c<b->chan+NCHAN; ++c)
736 case T_ASYNC: ++b->nasync; break;
740 case T_UNIV_V35: ++b->nuniv; break;
743 case T_SYNC_RS449: ++b->nsync; break;
750 * Initialize the Sigma-800 board structure.
752 void cx_init_800 (cx_board_t *b, int num, port_t port, int irq, int dma,
758 /* Initialize board structure. */
763 b->opt = board_opt_dflt;
764 b->type = B_SIGMA_800;
766 /* Set channels 0 and 8 mode, set DMA and IRQ. */
767 b->bcr0 = b->bcr0b = dmamask[b->dma] | irqmask[b->irq];
769 /* Clear DTR[0..7] and DTR[8..15]. */
770 b->bcr1 = b->bcr1b = 0;
772 strcpy (b->name, "800");
774 strcat (b->name, "/800");
776 /* Initialize channel structures. */
777 for (i=0; i<4; ++i) {
778 b->chan[i+0].port = CS0(port);
779 b->chan[i+4].port = cx_probe_chip (CS1A(port)) ?
780 CS1A(port) : CS1(port);
781 b->chan[i+8].port = CS0(port+0x10);
782 b->chan[i+12].port = CS1(port+0x10);
784 for (c=b->chan; c<b->chan+NCHAN; ++c) {
786 c->num = c - b->chan;
787 c->oscfreq = 33000000L;
788 c->type = (c->num < 8 || chain) ? T_UNIV_RS232 : T_NONE;
791 b->nuniv = b->nsync = b->nasync = 0;
792 for (c=b->chan; c<b->chan+NCHAN; ++c)
794 case T_ASYNC: ++b->nasync; break;
798 case T_UNIV_V35: ++b->nuniv; break;
801 case T_SYNC_RS449: ++b->nsync; break;
808 * Initialize the Sigma-2x board structure.
810 void cx_init_2x (cx_board_t *b, int num, port_t port, int irq, int dma,
816 /* Initialize board structure. */
821 b->opt = board_opt_dflt;
823 b->type = B_SIGMA_2X;
825 /* Set channels 0 and 8 mode, set DMA and IRQ. */
826 b->bcr0 = BCR0_NORESET | dmamask[b->dma] | irqmask[b->irq];
827 if (b->type == B_SIGMA_2X && b->opt.fast)
828 b->bcr0 |= BCR02X_FAST;
830 /* Clear DTR[0..3] and DTR[8..12]. */
833 /* Initialize channel structures. */
834 for (i=0; i<4; ++i) {
835 b->chan[i+0].port = CS0(port);
836 b->chan[i+4].port = CS1(port);
837 b->chan[i+8].port = CS0(port+0x10);
838 b->chan[i+12].port = CS1(port+0x10);
840 for (c=b->chan; c<b->chan+NCHAN; ++c) {
842 c->num = c - b->chan;
844 c->oscfreq = (osc & BSR2X_OSC_33) ? 33000000L : 20000000L;
847 /* Check the board revision code. */
848 strcpy (b->name, "22");
849 b->chan[0].type = T_UNIV;
850 b->chan[1].type = T_UNIV;
851 b->nsync = b->nasync = 0;
853 if (rev == CRONYX_24) {
854 strcpy (b->name, "24");
855 b->chan[2].type = T_UNIV;
856 b->chan[3].type = T_UNIV;
859 strcat (b->name, (osc & BSR2X_OSC_33) ? "c" : "a");
864 * Reinitialize all channels, using new options and baud rate.
866 void cx_reinit_board (cx_board_t *b)
870 b->opt = board_opt_dflt;
871 if (b->type == B_SIGMA_2X) {
872 b->bcr0 &= ~BCR02X_FAST;
874 b->bcr0 |= BCR02X_FAST;
876 b->if0type = b->if8type = cx_iftype;
877 for (c=b->chan; c<b->chan+NCHAN; ++c) {
886 c->mode = (cx_univ_mode == M_ASYNC) ?
887 M_ASYNC : cx_sync_mode;
892 c->mode = cx_sync_mode;
898 c->rxbaud = cx_rxbaud;
899 c->txbaud = cx_txbaud;
900 c->opt = chan_opt_dflt;
901 c->aopt = opt_async_dflt;
902 c->hopt = opt_hdlc_dflt;
909 int cx_setup_board (cx_board_t *b, const unsigned char *firmware,
910 long bits, const cr_dat_tst_t *tst)
913 #ifndef NDIS_MINIPORT_DRIVER
914 /* Disable DMA channel. */
915 outb (DMA_MASK, (b->dma & 3) | DMA_MASK_CLEAR);
917 /* Reset the controller. */
918 outb (BCR0(b->port), 0);
919 if (b->chan[8].type || b->chan[12].type)
920 outb (BCR0(b->port+0x10), 0);
922 /* Load the firmware. */
923 if (b->type == B_SIGMA_800) {
924 /* Reset the controllers. */
925 outb (BCR2(b->port), BCR2_TMS);
926 if (b->chan[8].type || b->chan[12].type)
927 outb (BCR2(b->port+0x10), BCR2_TMS);
928 outb (BCR2(b->port), 0);
929 if (b->chan[8].type || b->chan[12].type)
930 outb (BCR2(b->port+0x10), 0);
933 (! cx_download (b->port, firmware, bits, tst) ||
934 ((b->chan[8].type || b->chan[12].type) &&
935 ! cx_download (b->port+0x10, firmware, bits, tst))))
940 * Set channels 0 and 8 to RS232 async. mode.
941 * Enable DMA and IRQ.
943 outb (BCR0(b->port), b->bcr0);
944 if (b->chan[8].type || b->chan[12].type)
945 outb (BCR0(b->port+0x10), b->bcr0b);
947 /* Clear DTR[0..3] and DTR[8..12]. */
948 outw (BCR1(b->port), b->bcr1);
949 if (b->chan[8].type || b->chan[12].type)
950 outw (BCR1(b->port+0x10), b->bcr1b);
952 if (b->type == B_SIGMA_800)
953 outb (BCR2(b->port), b->opt.fast &
954 (BCR2_BUS0 | BCR2_BUS1));
956 /* Initialize all controllers. */
957 for (i=0; i<NCHAN; i+=4)
958 if (b->chan[i].type != T_NONE)
959 cx_setup_chip (b->chan + i);
960 #ifndef NDIS_MINIPORT_DRIVER
961 /* Set up DMA channel to master mode. */
962 outb (DMA_MODE, (b->dma & 3) | DMA_MODE_MASTER);
964 /* Enable DMA channel. */
965 outb (DMA_MASK, b->dma & 3);
967 /* Initialize all channels. */
968 for (i=0; i<NCHAN; ++i)
969 if (b->chan[i].type != T_NONE)
970 cx_setup_chan (b->chan + i);
975 * Initialize the board.
977 static void cx_setup_chip (cx_chan_t *c)
979 /* Reset the chip. */
983 * Set all interrupt level registers to the same value.
984 * This enables the internal CD2400 priority scheme.
986 outb (RPILR(c->port), BRD_INTR_LEVEL);
987 outb (TPILR(c->port), BRD_INTR_LEVEL);
988 outb (MPILR(c->port), BRD_INTR_LEVEL);
990 /* Set bus error count to zero. */
991 outb (BERCNT(c->port), 0);
993 /* Set 16-bit DMA mode. */
994 outb (DMR(c->port), 0);
996 /* Set timer period register to 1 msec (approximately). */
997 outb (TPR(c->port), 10);
1001 * Initialize the CD2400 channel.
1003 void cx_update_chan (cx_chan_t *c)
1007 if (c->board->type == B_SIGMA_XXX)
1010 c->board->bcr0 &= ~BCR0_UMASK;
1011 if (c->mode != M_ASYNC)
1012 c->board->bcr0 |= BCR0_UM_SYNC;
1013 if (c->board->if0type &&
1014 (c->type==T_UNIV_RS449 || c->type==T_UNIV_V35))
1015 c->board->bcr0 |= BCR0_UI_RS449;
1016 outb (BCR0(c->board->port), c->board->bcr0);
1019 c->board->bcr0b &= ~BCR0_UMASK;
1020 if (c->mode != M_ASYNC)
1021 c->board->bcr0b |= BCR0_UM_SYNC;
1022 if (c->board->if8type &&
1023 (c->type==T_UNIV_RS449 || c->type==T_UNIV_V35))
1024 c->board->bcr0b |= BCR0_UI_RS449;
1025 outb (BCR0(c->board->port+0x10), c->board->bcr0b);
1029 /* set current channel number */
1030 outb (CAR(c->port), c->num & 3);
1032 switch (c->mode) { /* initialize the channel mode */
1034 /* set receiver timeout register */
1035 outw (RTPR(c->port), 10); /* 10 msec, see TPR */
1036 c->opt.rcor.encod = ENCOD_NRZ;
1038 outb (CMR(c->port), CMR_RXDMA | CMR_TXDMA | CMR_ASYNC);
1039 outb (COR1(c->port), BYTE c->aopt.cor1);
1040 outb (COR2(c->port), BYTE c->aopt.cor2);
1041 outb (COR3(c->port), BYTE c->aopt.cor3);
1042 outb (COR6(c->port), BYTE c->aopt.cor6);
1043 outb (COR7(c->port), BYTE c->aopt.cor7);
1044 outb (SCHR1(c->port), c->aopt.schr1);
1045 outb (SCHR2(c->port), c->aopt.schr2);
1046 outb (SCHR3(c->port), c->aopt.schr3);
1047 outb (SCHR4(c->port), c->aopt.schr4);
1048 outb (SCRL(c->port), c->aopt.scrl);
1049 outb (SCRH(c->port), c->aopt.scrh);
1050 outb (LNXT(c->port), c->aopt.lnxt);
1053 outb (CMR(c->port), CMR_RXDMA | CMR_TXDMA | CMR_HDLC);
1054 outb (COR1(c->port), BYTE c->hopt.cor1);
1055 outb (COR2(c->port), BYTE c->hopt.cor2);
1056 outb (COR3(c->port), BYTE c->hopt.cor3);
1057 outb (RFAR1(c->port), c->hopt.rfar1);
1058 outb (RFAR2(c->port), c->hopt.rfar2);
1059 outb (RFAR3(c->port), c->hopt.rfar3);
1060 outb (RFAR4(c->port), c->hopt.rfar4);
1061 outb (CPSR(c->port), c->hopt.cpsr);
1065 /* set mode-independent options */
1066 outb (COR4(c->port), BYTE c->opt.cor4);
1067 outb (COR5(c->port), BYTE c->opt.cor5);
1069 /* set up receiver clock values */
1070 if (c->mode == M_ASYNC || c->opt.rcor.dpll || c->opt.tcor.llm) {
1071 cx_clock (c->oscfreq, c->rxbaud, &clock, &period);
1072 c->opt.rcor.clk = clock;
1074 c->opt.rcor.clk = CLK_EXT;
1077 outb (RCOR(c->port), BYTE c->opt.rcor);
1078 outb (RBPR(c->port), period);
1080 /* set up transmitter clock values */
1081 if (c->mode == M_ASYNC || !c->opt.tcor.ext1x) {
1082 unsigned ext1x = c->opt.tcor.ext1x;
1083 c->opt.tcor.ext1x = 0;
1084 cx_clock (c->oscfreq, c->txbaud, &clock, &period);
1085 c->opt.tcor.clk = clock;
1086 c->opt.tcor.ext1x = ext1x;
1088 c->opt.tcor.clk = CLK_EXT;
1091 outb (TCOR(c->port), BYTE c->opt.tcor);
1092 outb (TBPR(c->port), period);
1096 * Initialize the CD2400 channel.
1098 void cx_setup_chan (cx_chan_t *c)
1100 /* set current channel number */
1101 outb (CAR(c->port), c->num & 3);
1103 /* reset the channel */
1104 cx_cmd (c->port, CCR_CLRCH);
1106 /* set LIVR to contain the board and channel numbers */
1107 outb (LIVR(c->port), c->board->num << 6 | c->num << 2);
1109 /* clear DTR, RTS, set TXCout/DTR pin */
1110 outb (MSVR_RTS(c->port), 0);
1111 outb (MSVR_DTR(c->port), c->mode==M_ASYNC ? 0 : MSV_TXCOUT);
1113 /* set receiver A buffer physical address */
1114 outw (ARBADRU(c->port), (unsigned short) (c->arphys>>16));
1115 outw (ARBADRL(c->port), (unsigned short) c->arphys);
1117 /* set receiver B buffer physical address */
1118 outw (BRBADRU(c->port), (unsigned short) (c->brphys>>16));
1119 outw (BRBADRL(c->port), (unsigned short) c->brphys);
1121 /* set transmitter A buffer physical address */
1122 outw (ATBADRU(c->port), (unsigned short) (c->atphys>>16));
1123 outw (ATBADRL(c->port), (unsigned short) c->atphys);
1125 /* set transmitter B buffer physical address */
1126 outw (BTBADRU(c->port), (unsigned short) (c->btphys>>16));
1127 outw (BTBADRL(c->port), (unsigned short) c->btphys);
1136 * Control DTR signal for the channel.
1139 void cx_set_dtr (cx_chan_t *c, int on)
1141 cx_board_t *b = c->board;
1143 c->dtr = on ? 1 : 0;
1145 if (b->type == B_SIGMA_2X) {
1146 if (on) b->bcr1 |= BCR1_DTR(c->num);
1147 else b->bcr1 &= ~BCR1_DTR(c->num);
1148 outw (BCR1(b->port), b->bcr1);
1151 if (b->type == B_SIGMA_800) {
1153 if (on) b->bcr1b |= BCR1800_DTR(c->num);
1154 else b->bcr1b &= ~BCR1800_DTR(c->num);
1155 outb (BCR1(b->port+0x10), b->bcr1b);
1157 if (on) b->bcr1 |= BCR1800_DTR(c->num);
1158 else b->bcr1 &= ~BCR1800_DTR(c->num);
1159 outb (BCR1(b->port), b->bcr1);
1163 if (c->mode == M_ASYNC) {
1164 outb (CAR(c->port), c->num & 3);
1165 outb (MSVR_DTR(c->port), on ? MSV_DTR : 0);
1171 /* Channels 4..7 and 12..15 in synchronous mode
1172 * have no DTR signal. */
1175 case 1: case 2: case 3:
1176 if (c->type == T_UNIV_RS232)
1179 if (on) b->bcr1 |= BCR1_DTR(c->num);
1180 else b->bcr1 &= ~BCR1_DTR(c->num);
1181 outw (BCR1(b->port), b->bcr1);
1184 case 9: case 10: case 11:
1185 if (c->type == T_UNIV_RS232)
1188 if (on) b->bcr1b |= BCR1_DTR(c->num & 3);
1189 else b->bcr1b &= ~BCR1_DTR(c->num & 3);
1190 outw (BCR1(b->port+0x10), b->bcr1b);
1196 * Control RTS signal for the channel.
1199 void cx_set_rts (cx_chan_t *c, int on)
1201 c->rts = on ? 1 : 0;
1202 outb (CAR(c->port), c->num & 3);
1203 outb (MSVR_RTS(c->port), on ? MSV_RTS : 0);
1207 * Get the state of DSR signal of the channel.
1209 int cx_get_dsr (cx_chan_t *c)
1211 unsigned char sigval;
1213 if (c->board->type == B_SIGMA_2X ||
1214 c->board->type == B_SIGMA_800 ||
1215 c->mode == M_ASYNC) {
1216 outb (CAR(c->port), c->num & 3);
1217 return (inb (MSVR(c->port)) & MSV_DSR ? 1 : 0);
1221 * Channels 4..7 and 12..15 don't have DSR signal available.
1227 case 1: case 2: case 3:
1228 if (c->type == T_UNIV_RS232)
1231 sigval = inw (BSR(c->board->port)) >> 8;
1234 case 9: case 10: case 11:
1235 if (c->type == T_UNIV_RS232)
1238 sigval = inw (BSR(c->board->port+0x10)) >> 8;
1241 return (~sigval >> (c->num & 3) & 1);
1245 * Get the state of CARRIER signal of the channel.
1247 int cx_get_cd (cx_chan_t *c)
1249 unsigned char sigval;
1251 if (c->board->type == B_SIGMA_2X ||
1252 c->board->type == B_SIGMA_800 ||
1253 c->mode == M_ASYNC) {
1254 outb (CAR(c->port), c->num & 3);
1255 return (inb (MSVR(c->port)) & MSV_CD ? 1 : 0);
1259 * Channels 4..7 and 12..15 don't have CD signal available.
1265 case 1: case 2: case 3:
1266 if (c->type == T_UNIV_RS232)
1269 sigval = inw (BSR(c->board->port)) >> 8;
1272 case 9: case 10: case 11:
1273 if (c->type == T_UNIV_RS232)
1276 sigval = inw (BSR(c->board->port+0x10)) >> 8;
1279 return (~sigval >> 4 >> (c->num & 3) & 1);
1283 * Get the state of CTS signal of the channel.
1285 int cx_get_cts (cx_chan_t *c)
1287 outb (CAR(c->port), c->num & 3);
1288 return (inb (MSVR(c->port)) & MSV_CTS ? 1 : 0);
1292 * Compute CD2400 clock values.
1294 void cx_clock (long hz, long ba, int *clk, int *div)
1296 static short clocktab[] = { 8, 32, 128, 512, 2048, 0 };
1298 for (*clk=0; clocktab[*clk]; ++*clk) {
1299 long c = ba * clocktab[*clk];
1301 *div = (2 * hz + c) / (2 * c) - 1;
1305 /* Incorrect baud rate. Return some meaningful values. */
1313 void cx_led (cx_board_t *b, int on)
1317 if (on) b->bcr0 |= BCR02X_LED;
1318 else b->bcr0 &= ~BCR02X_LED;
1319 outb (BCR0(b->port), b->bcr0);
1324 void cx_disable_dma (cx_board_t *b)
1326 #ifndef NDIS_MINIPORT_DRIVER
1327 /* Disable DMA channel. */
1328 outb (DMA_MASK, (b->dma & 3) | DMA_MASK_CLEAR);
1332 cx_board_opt_t board_opt_dflt = { /* board options */
1333 BUS_NORMAL, /* normal bus master timing */
1336 cx_chan_opt_t chan_opt_dflt = { /* mode-independent options */
1338 7, /* FIFO threshold, odd is better */
1340 0, /* don't detect 1 to 0 on CTS */
1341 1, /* detect 1 to 0 on CD */
1342 0, /* detect 1 to 0 on DSR */
1345 0, /* receive flow control FIFO threshold */
1347 0, /* don't detect 0 to 1 on CTS */
1348 1, /* detect 0 to 1 on CD */
1349 0, /* detect 0 to 1 on DSR */
1352 0, /* dummy clock source */
1353 ENCOD_NRZ, /* NRZ mode */
1354 0, /* disable DPLL */
1356 0, /* transmit line value */
1360 0, /* local loopback mode */
1362 1, /* external 1x clock mode */
1364 0, /* dummy transmit clock source */
1368 cx_opt_async_t opt_async_dflt = { /* default async options */
1370 8-1, /* 8-bit char length */
1371 0, /* don't ignore parity */
1372 PARM_NOPAR, /* no parity */
1373 PAR_EVEN, /* even parity */
1376 0, /* disable automatic DSR */
1377 1, /* enable automatic CTS */
1378 0, /* disable automatic RTS */
1379 0, /* no remote loopback */
1381 0, /* disable embedded cmds */
1382 0, /* disable XON/XOFF */
1383 0, /* disable XANY */
1386 STOPB_1, /* 1 stop bit */
1388 0, /* disable special char detection */
1389 FLOWCC_PASS, /* pass flow ctl chars to the host */
1390 0, /* range detect disable */
1391 0, /* disable extended spec. char detect */
1394 PERR_INTR, /* generate exception on parity errors */
1395 BRK_INTR, /* generate exception on break condition */
1396 0, /* don't translate NL to CR on input */
1397 0, /* don't translate CR to NL on input */
1398 0, /* don't discard CR on input */
1401 0, /* don't translate CR to NL on output */
1402 0, /* don't translate NL to CR on output */
1404 0, /* don't process flow ctl err chars */
1405 0, /* disable LNext option */
1406 0, /* don't strip 8 bit on input */
1408 0, 0, 0, 0, 0, 0, 0, /* clear schr1-4, scrl, scrh, lnxt */
1411 cx_opt_hdlc_t opt_hdlc_dflt = { /* default hdlc options */
1413 2, /* 2 inter-frame flags */
1414 0, /* no-address mode */
1415 CLRDET_DISABLE, /* disable clear detect */
1416 AFLO_1OCT, /* 1-byte address field length */
1419 0, /* disable automatic DSR */
1420 0, /* disable automatic CTS */
1421 0, /* disable automatic RTS */
1423 CRC_INVERT, /* use CRC V.41 */
1425 FCS_NOTPASS, /* don't pass received CRC to the host */
1429 0, /* 0 pad characters sent */
1430 IDLE_FLAG, /* idle in flag */
1432 FCSP_ONES, /* FCS preset to all ones (V.41) */
1433 SYNC_AA, /* use AAh as sync char */
1434 0, /* disable pad characters */
1436 0, 0, 0, 0, /* clear rfar1-4 */
1437 POLY_V41, /* use V.41 CRC polynomial */