Merge from vendor branch OPENSSL:
[dragonfly.git] / sys / net / i4b / layer1 / isic / i4b_hscx.c
1 /*
2  * Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      i4b - Siemens HSCX chip (B-channel) handling
28  *      --------------------------------------------
29  *
30  * $FreeBSD: src/sys/i4b/layer1/isic/i4b_hscx.c,v 1.7.2.1 2001/08/10 14:08:38 obrien Exp $
31  * $DragonFly: src/sys/net/i4b/layer1/isic/i4b_hscx.c,v 1.6 2006/12/22 23:44:56 swildner Exp $
32  *
33  *      last edit-date: [Wed Jan 24 09:09:42 2001]
34  *
35  *---------------------------------------------------------------------------*/
36
37 #include "use_isic.h"
38
39 #if NISIC > 0
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45
46 #include <net/if.h>
47
48 #include <net/i4b/include/machine/i4b_debug.h>
49 #include <net/i4b/include/machine/i4b_ioctl.h>
50 #include <net/i4b/include/machine/i4b_trace.h>
51
52 #include "i4b_isic.h"
53 #include "i4b_hscx.h"
54
55 #include "../i4b_l1.h"
56
57 #include "../../include/i4b_global.h"
58 #include "../../include/i4b_mbuf.h"
59
60 /*---------------------------------------------------------------------------*
61  *      HSCX IRQ Handler
62  *---------------------------------------------------------------------------*/
63 void
64 isic_hscx_irq(struct l1_softc *sc, u_char ista, int h_chan, u_char ex_irq)
65 {
66         l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
67         u_char exir = 0;
68         int activity = -1;
69         u_char cmd = 0;
70
71         NDBGL1(L1_H_IRQ, "%#x", ista);
72
73         if(ex_irq)
74         {
75                 /* get channel extended irq reg */
76
77                 exir = HSCX_READ(h_chan, H_EXIR);
78
79                 if(exir & HSCX_EXIR_RFO)
80                 {
81                         chan->stat_RFO++;
82                         NDBGL1(L1_H_XFRERR, "ex_irq: receive data overflow");
83                 }
84
85                 if((exir & HSCX_EXIR_XDU) && (chan->bprot != BPROT_NONE))/* xmit data underrun */
86                 {
87                         chan->stat_XDU++;                       
88                         NDBGL1(L1_H_XFRERR, "ex_irq: xmit data underrun");
89                         isic_hscx_cmd(sc, h_chan, HSCX_CMDR_XRES);
90
91                         if (chan->out_mbuf_head != NULL)  /* don't continue to transmit this buffer */
92                         {
93                                 i4b_Bfreembuf(chan->out_mbuf_head);
94                                 chan->out_mbuf_cur = chan->out_mbuf_head = NULL;
95                         }
96                 }
97
98         }
99
100         /* rx message end, end of frame */
101         
102         if(ista & HSCX_ISTA_RME)
103         {
104                 int fifo_data_len;
105                 u_char rsta;            
106                 int error = 0;
107
108                 rsta = HSCX_READ(h_chan, H_RSTA);
109
110                 if((rsta & 0xf0) != 0xa0)
111                 {
112                         if((rsta & HSCX_RSTA_VFR) == 0)
113                         {
114                                 chan->stat_VFR++;
115                                 cmd |= (HSCX_CMDR_RHR);
116                                 NDBGL1(L1_H_XFRERR, "received invalid Frame");
117                                 error++;
118                         }
119         
120                         if(rsta & HSCX_RSTA_RDO)
121                         {
122                                 chan->stat_RDO++;
123                                 NDBGL1(L1_H_XFRERR, "receive data overflow");
124                                 error++;                                
125                         }
126                         
127                         if((rsta & HSCX_RSTA_CRC) == 0)
128                         {
129                                 chan->stat_CRC++;
130                                 cmd |= (HSCX_CMDR_RHR);
131                                 NDBGL1(L1_H_XFRERR, "CRC check failed");
132                                 error++;
133                         }
134                         
135                         if(rsta & HSCX_RSTA_RAB)
136                         {
137                                 chan->stat_RAB++;                               
138                                 NDBGL1(L1_H_XFRERR, "Receive message aborted");
139                                 error++;
140                         }
141                 }
142
143                 fifo_data_len = ((HSCX_READ(h_chan, H_RBCL)) &
144                                                 ((sc->sc_bfifolen)-1));
145                 
146                 if(fifo_data_len == 0)
147                         fifo_data_len = sc->sc_bfifolen;
148
149                 /* all error conditions checked, now decide and take action */
150                 
151                 if(error == 0)
152                 {
153                         if(chan->in_mbuf == NULL)
154                         {
155                                 if((chan->in_mbuf = i4b_Bgetmbuf(BCH_MAX_DATALEN)) == NULL)
156                                         panic("L1 isic_hscx_irq: RME, cannot allocate mbuf!\n");
157                                 chan->in_cbptr = chan->in_mbuf->m_data;
158                                 chan->in_len = 0;
159                         }
160
161                         fifo_data_len -= 1; /* last byte in fifo is RSTA ! */
162                         
163                         if((chan->in_len + fifo_data_len) <= BCH_MAX_DATALEN)
164                         {
165                                 /* read data from HSCX fifo */
166         
167                                 HSCX_RDFIFO(h_chan, chan->in_cbptr, fifo_data_len);
168
169                                 cmd |= (HSCX_CMDR_RMC);
170                                 isic_hscx_cmd(sc, h_chan, cmd);
171                                 cmd = 0;
172                                 
173                                 chan->in_len += fifo_data_len;
174                                 chan->rxcount += fifo_data_len;
175
176                                 /* setup mbuf data length */
177                                         
178                                 chan->in_mbuf->m_len = chan->in_len;
179                                 chan->in_mbuf->m_pkthdr.len = chan->in_len;
180                 
181                                 if(sc->sc_trace & TRACE_B_RX)
182                                 {
183                                         i4b_trace_hdr_t hdr;
184                                         hdr.unit = L0ISICUNIT(sc->sc_unit);
185                                         hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
186                                         hdr.dir = FROM_NT;
187                                         hdr.count = ++sc->sc_trace_bcount;
188                                         MICROTIME(hdr.time);
189                                         i4b_l1_trace_ind(&hdr, chan->in_mbuf->m_len, chan->in_mbuf->m_data);
190                                 }
191
192                                 (*chan->isic_drvr_linktab->bch_rx_data_ready)(chan->isic_drvr_linktab->unit);
193
194                                 activity = ACT_RX;
195                                 
196                                 /* mark buffer ptr as unused */
197                                         
198                                 chan->in_mbuf = NULL;
199                                 chan->in_cbptr = NULL;
200                                 chan->in_len = 0;
201                         }
202                         else
203                         {
204                                 NDBGL1(L1_H_XFRERR, "RAWHDLC rx buffer overflow in RME, in_len=%d, fifolen=%d", chan->in_len, fifo_data_len);
205                                 chan->in_cbptr = chan->in_mbuf->m_data;
206                                 chan->in_len = 0;
207                                 cmd |= (HSCX_CMDR_RHR | HSCX_CMDR_RMC); 
208                         }
209                 }
210                 else
211                 {
212                         if (chan->in_mbuf != NULL)
213                         {
214                                 i4b_Bfreembuf(chan->in_mbuf);
215                                 chan->in_mbuf = NULL;
216                                 chan->in_cbptr = NULL;
217                                 chan->in_len = 0;
218                         }
219                         cmd |= (HSCX_CMDR_RMC);
220                 }
221         }
222
223         /* rx fifo full */
224
225         if(ista & HSCX_ISTA_RPF)
226         {
227                 if(chan->in_mbuf == NULL)
228                 {
229                         if((chan->in_mbuf = i4b_Bgetmbuf(BCH_MAX_DATALEN)) == NULL)
230                                 panic("L1 isic_hscx_irq: RPF, cannot allocate mbuf!\n");
231                         chan->in_cbptr = chan->in_mbuf->m_data;
232                         chan->in_len = 0;
233                 }
234
235                 chan->rxcount += sc->sc_bfifolen;
236                 
237                 if((chan->in_len + sc->sc_bfifolen) <= BCH_MAX_DATALEN)
238                 {
239                         /* read data from HSCX fifo */
240
241                         HSCX_RDFIFO(h_chan, chan->in_cbptr, sc->sc_bfifolen);
242
243                         chan->in_cbptr += sc->sc_bfifolen;
244                         chan->in_len += sc->sc_bfifolen;
245                 }
246                 else
247                 {
248                         if(chan->bprot == BPROT_NONE)
249                         {
250                                 /* setup mbuf data length */
251                                 
252                                 chan->in_mbuf->m_len = chan->in_len;
253                                 chan->in_mbuf->m_pkthdr.len = chan->in_len;
254
255                                 if(sc->sc_trace & TRACE_B_RX)
256                                 {
257                                         i4b_trace_hdr_t hdr;
258                                         hdr.unit = L0ISICUNIT(sc->sc_unit);
259                                         hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
260                                         hdr.dir = FROM_NT;
261                                         hdr.count = ++sc->sc_trace_bcount;
262                                         MICROTIME(hdr.time);
263                                         i4b_l1_trace_ind(&hdr, chan->in_mbuf->m_len, chan->in_mbuf->m_data);
264                                 }
265
266                                 /* silence detection */
267                                 
268                                 if(!(i4b_l1_bchan_tel_silence(chan->in_mbuf->m_data, chan->in_mbuf->m_len)))
269                                         activity = ACT_RX;
270
271                                 if(!(IF_QFULL(&chan->rx_queue)))
272                                 {
273                                         IF_ENQUEUE(&chan->rx_queue, chan->in_mbuf);
274                                 }
275                                 else
276                                 {
277                                         i4b_Bfreembuf(chan->in_mbuf);
278                                 }
279                                 /* signal upper driver that data is available */
280
281                                 (*chan->isic_drvr_linktab->bch_rx_data_ready)(chan->isic_drvr_linktab->unit);
282                                 
283                                 /* alloc new buffer */
284                                 
285                                 if((chan->in_mbuf = i4b_Bgetmbuf(BCH_MAX_DATALEN)) == NULL)
286                                         panic("L1 isic_hscx_irq: RPF, cannot allocate new mbuf!\n");
287         
288                                 /* setup new data ptr */
289                                 
290                                 chan->in_cbptr = chan->in_mbuf->m_data;
291         
292                                 /* read data from HSCX fifo */
293         
294                                 HSCX_RDFIFO(h_chan, chan->in_cbptr, sc->sc_bfifolen);
295
296                                 chan->in_cbptr += sc->sc_bfifolen;
297                                 chan->in_len = sc->sc_bfifolen;
298
299                                 chan->rxcount += sc->sc_bfifolen;
300                         }
301                         else
302                         {
303                                 NDBGL1(L1_H_XFRERR, "RAWHDLC rx buffer overflow in RPF, in_len=%d", chan->in_len);
304                                 chan->in_cbptr = chan->in_mbuf->m_data;
305                                 chan->in_len = 0;
306                                 cmd |= (HSCX_CMDR_RHR);
307                         }
308                 }
309                 
310                 /* command to release fifo space */
311                 
312                 cmd |= HSCX_CMDR_RMC;
313         }
314
315         /* transmit fifo empty, new data can be written to fifo */
316         
317         if(ista & HSCX_ISTA_XPR)
318         {
319                 /*
320                  * for a description what is going on here, please have
321                  * a look at isic_bchannel_start() in i4b_bchan.c !
322                  */
323                  
324                 int activity = -1;
325                 int len;
326                 int nextlen;
327
328                 NDBGL1(L1_H_IRQ, "unit %d, chan %d - XPR, Tx Fifo Empty!", sc->sc_unit, h_chan);
329
330                 if(chan->out_mbuf_cur == NULL)  /* last frame is transmitted */
331                 {
332                         IF_DEQUEUE(&chan->tx_queue, chan->out_mbuf_head);
333
334                         if(chan->out_mbuf_head == NULL)
335                         {
336                                 chan->state &= ~HSCX_TX_ACTIVE;
337                                 (*chan->isic_drvr_linktab->bch_tx_queue_empty)(chan->isic_drvr_linktab->unit);
338                         }
339                         else
340                         {
341                                 chan->state |= HSCX_TX_ACTIVE;
342                                 chan->out_mbuf_cur = chan->out_mbuf_head;
343                                 chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
344                                 chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
345
346                                 if(sc->sc_trace & TRACE_B_TX)
347                                 {
348                                         i4b_trace_hdr_t hdr;
349                                         hdr.unit = L0ISICUNIT(sc->sc_unit);
350                                         hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
351                                         hdr.dir = FROM_TE;
352                                         hdr.count = ++sc->sc_trace_bcount;
353                                         MICROTIME(hdr.time);
354                                         i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
355                                 }
356                                 
357                                 if(chan->bprot == BPROT_NONE)
358                                 {
359                                         if(!(i4b_l1_bchan_tel_silence(chan->out_mbuf_cur->m_data, chan->out_mbuf_cur->m_len)))
360                                                 activity = ACT_TX;
361                                 }
362                                 else
363                                 {
364                                         activity = ACT_TX;
365                                 }
366                         }
367                 }
368                         
369                 len = 0;
370
371                 while(chan->out_mbuf_cur && len != sc->sc_bfifolen)
372                 {
373                         nextlen = min(chan->out_mbuf_cur_len, sc->sc_bfifolen - len);
374
375 #ifdef NOTDEF                   
376                         kprintf("i:mh=%x, mc=%x, mcp=%x, mcl=%d l=%d nl=%d # ",
377                                 chan->out_mbuf_head,
378                                 chan->out_mbuf_cur,                     
379                                 chan->out_mbuf_cur_ptr,
380                                 chan->out_mbuf_cur_len,
381                                 len,
382                                 next_len);
383 #endif
384
385                         isic_hscx_waitxfw(sc, h_chan);  /* necessary !!! */
386                         
387                         HSCX_WRFIFO(h_chan, chan->out_mbuf_cur_ptr, nextlen);
388                         cmd |= HSCX_CMDR_XTF;
389         
390                         len += nextlen;
391                         chan->txcount += nextlen;
392         
393                         chan->out_mbuf_cur_ptr += nextlen;
394                         chan->out_mbuf_cur_len -= nextlen;
395                         
396                         if(chan->out_mbuf_cur_len == 0) 
397                         {
398                                 if((chan->out_mbuf_cur = chan->out_mbuf_cur->m_next) != NULL)
399                                 {
400                                         chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
401                                         chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
402         
403                                         if(sc->sc_trace & TRACE_B_TX)
404                                         {
405                                                 i4b_trace_hdr_t hdr;
406                                                 hdr.unit = L0ISICUNIT(sc->sc_unit);
407                                                 hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
408                                                 hdr.dir = FROM_TE;
409                                                 hdr.count = ++sc->sc_trace_bcount;
410                                                 MICROTIME(hdr.time);
411                                                 i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
412                                         }
413                                 }
414                                 else
415                                 {
416                                         if (chan->bprot != BPROT_NONE)
417                                                 cmd |= HSCX_CMDR_XME;
418                                         i4b_Bfreembuf(chan->out_mbuf_head);
419                                         chan->out_mbuf_head = NULL;
420                                 }
421
422                         }
423                 }
424         }
425
426         if(cmd)         /* is there a command for the HSCX ? */
427         {
428                 isic_hscx_cmd(sc, h_chan, cmd); /* yes, to HSCX */
429         }
430
431         /* call timeout handling routine */
432         
433         if(activity == ACT_RX || activity == ACT_TX)
434                 (*chan->isic_drvr_linktab->bch_activity)(chan->isic_drvr_linktab->unit, activity);
435 }
436
437 /*---------------------------------------------------------------------------*
438  *      HSCX initialization
439  *
440  *      for telephony: extended transparent mode 1
441  *      for raw hdlc:  transparent mode 0
442  *---------------------------------------------------------------------------*/
443 void
444 isic_hscx_init(struct l1_softc *sc, int h_chan, int activate)
445 {       
446         l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
447
448         HSCX_WRITE(h_chan, H_MASK, 0xff);               /* mask irq's */
449
450         if(sc->sc_ipac)
451         {
452                 /* CCR1: Power Up, Clock Mode 5 */
453                 HSCX_WRITE(h_chan, H_CCR1, HSCX_CCR1_PU  |      /* power up */
454                               HSCX_CCR1_CM1);   /* IPAC clock mode 5 */
455         }
456         else
457         {
458                 /* CCR1: Power Up, Clock Mode 5 */
459                 HSCX_WRITE(h_chan, H_CCR1, HSCX_CCR1_PU  |      /* power up */
460                               HSCX_CCR1_CM2 |   /* HSCX clock mode 5 */
461                               HSCX_CCR1_CM0);
462         }
463                 
464         /* XAD1: Transmit Address Byte 1 */
465         HSCX_WRITE(h_chan, H_XAD1, 0xff);
466         
467         /* XAD2: Transmit Address Byte 2 */
468         HSCX_WRITE(h_chan, H_XAD2, 0xff);
469
470         /* RAH2: Receive Address Byte High Reg. 2 */
471         HSCX_WRITE(h_chan, H_RAH2, 0xff);
472         
473         /* XBCH: reset Transmit Byte Count High */
474         HSCX_WRITE(h_chan, H_XBCH, 0x00);
475         
476         /* RLCR: reset Receive Length Check Register */
477         HSCX_WRITE(h_chan, H_RLCR, 0x00);
478         
479         /* CCR2: set tx/rx clock shift bit 0    */
480         /*       disable CTS irq, disable RIE irq*/
481         HSCX_WRITE(h_chan, H_CCR2, HSCX_CCR2_XCS0|HSCX_CCR2_RCS0);
482
483         /* XCCR: tx bit count per time slot */
484         HSCX_WRITE(h_chan, H_XCCR, 0x07);
485
486         /* RCCR: rx bit count per time slot */
487         HSCX_WRITE(h_chan, H_RCCR, 0x07);
488         
489         if(sc->sc_bustyp == BUS_TYPE_IOM2)
490         {
491                 switch(h_chan) 
492                 {
493                         case HSCX_CH_A: /* Prepare HSCX channel A */
494                                 /* TSAX: tx clock shift bits 1 & 2      */
495                                 /*       tx time slot number            */
496                                 HSCX_WRITE(h_chan, H_TSAX, 0x2f);
497
498                                 /* TSAR: rx clock shift bits 1 & 2      */
499                                 /*       rx time slot number            */
500                                 HSCX_WRITE(h_chan, H_TSAR, 0x2f);
501                                 break;
502
503                         case HSCX_CH_B: /* Prepare HSCX channel B */
504                                 /* TSAX: tx clock shift bits 1 & 2      */
505                                 /*       tx time slot number            */
506                                 HSCX_WRITE(h_chan, H_TSAX, 0x03);
507
508                                 /* TSAR: rx clock shift bits 1 & 2      */
509                                 /*       rx time slot number            */
510                                 HSCX_WRITE(h_chan, H_TSAR, 0x03);
511                                 break;
512                 }
513         }
514         else    /* IOM 1 setup */
515         {
516                 /* TSAX: tx clock shift bits 1 & 2      */
517                 /*       tx time slot number            */
518                 HSCX_WRITE(h_chan, H_TSAX, 0x07);
519
520                 /* TSAR: rx clock shift bits 1 & 2      */
521                 /*       rx time slot number            */
522                 HSCX_WRITE(h_chan, H_TSAR, 0x07);
523         }
524
525         if(activate)
526         {
527                 if(chan->bprot == BPROT_RHDLC)
528                 {
529                   /* HDLC Frames, transparent mode 0 */
530                   HSCX_WRITE(h_chan, H_MODE,
531                      HSCX_MODE_MDS1|HSCX_MODE_RAC|HSCX_MODE_RTS);
532                 }
533                 else
534                 {
535                   /* Raw Telephony, extended transparent mode 1 */
536                   HSCX_WRITE(h_chan, H_MODE,
537                      HSCX_MODE_MDS1|HSCX_MODE_MDS0|HSCX_MODE_ADM|HSCX_MODE_RTS);
538                 }
539 #if 0
540                 isic_hscx_cmd(sc, h_chan, HSCX_CMDR_RHR|HSCX_CMDR_XRES);
541 #else
542                 isic_hscx_cmd(sc, h_chan, HSCX_CMDR_RHR);
543 #endif
544         }
545         else
546         {
547                 /* TSAX: tx time slot */
548                 HSCX_WRITE(h_chan, H_TSAX, 0xff);
549
550                 /* TSAR: rx time slot */
551                 HSCX_WRITE(h_chan, H_TSAR, 0xff);
552
553                 /* Raw Telephony, extended transparent mode 1 */
554                 HSCX_WRITE(h_chan, H_MODE,
555                    HSCX_MODE_MDS1|HSCX_MODE_MDS0|HSCX_MODE_ADM|HSCX_MODE_RTS);
556         }
557
558         /* don't touch ICA, EXA and EXB bits, this could be HSCX_CH_B */        
559         /* always disable RSC and TIN */
560
561         chan->hscx_mask |= HSCX_MASK_RSC | HSCX_MASK_TIN;
562
563         if(activate)
564         {
565                 /* enable */
566                 chan->hscx_mask &= ~(HSCX_MASK_RME | HSCX_MASK_RPF | HSCX_MASK_XPR);
567         }
568         else
569         {
570                 /* disable */
571                 chan->hscx_mask |= HSCX_MASK_RME | HSCX_MASK_RPF | HSCX_MASK_XPR;
572         }
573
574         /* handle ICA, EXA, and EXB via interrupt mask of channel b */
575
576         if (h_chan == HSCX_CH_A)
577         {
578                 if (activate) 
579                         HSCX_B_IMASK &= ~(HSCX_MASK_EXA | HSCX_MASK_ICA);
580                 else
581                         HSCX_B_IMASK |= HSCX_MASK_EXA | HSCX_MASK_ICA;
582                 HSCX_WRITE(HSCX_CH_A, H_MASK, HSCX_A_IMASK);
583                 HSCX_WRITE(HSCX_CH_B, H_MASK, HSCX_B_IMASK);
584         }
585         else
586         {
587                 if (activate)
588                         HSCX_B_IMASK &= ~HSCX_MASK_EXB;
589                 else
590                         HSCX_B_IMASK |= HSCX_MASK_EXB;
591                 HSCX_WRITE(HSCX_CH_B, H_MASK, HSCX_B_IMASK);
592         }
593
594         /* clear spurious interrupts left over */
595
596         if(h_chan == HSCX_CH_A)
597         {
598                 HSCX_READ(h_chan, H_EXIR);
599                 HSCX_READ(h_chan, H_ISTA);
600         }
601         else  /* mask ICA, because it must not be cleared by reading ISTA */
602         {
603                 HSCX_WRITE(HSCX_CH_B, H_MASK, HSCX_B_IMASK | HSCX_MASK_ICA);
604                 HSCX_READ(h_chan, H_EXIR);
605                 HSCX_READ(h_chan, H_ISTA);
606                 HSCX_WRITE(HSCX_CH_B, H_MASK, HSCX_B_IMASK);
607         }
608 }
609
610 /*---------------------------------------------------------------------------*
611  *      write command to HSCX command register
612  *---------------------------------------------------------------------------*/
613 void
614 isic_hscx_cmd(struct l1_softc *sc, int h_chan, unsigned char cmd)
615 {       
616         int timeout = 20;
617
618         while(((HSCX_READ(h_chan, H_STAR)) & HSCX_STAR_CEC) && timeout)
619         {
620                 DELAY(10);
621                 timeout--;
622         }
623
624         if(timeout == 0)
625         {
626                 NDBGL1(L1_H_ERR, "HSCX wait for CEC timeout!");
627         }
628
629         HSCX_WRITE(h_chan, H_CMDR, cmd);        
630 }
631
632 /*---------------------------------------------------------------------------*
633  *      wait for HSCX transmit FIFO write enable
634  *---------------------------------------------------------------------------*/
635 void
636 isic_hscx_waitxfw(struct l1_softc *sc, int h_chan)
637 {       
638 #define WAITVAL 50
639 #define WAITTO  200
640
641         int timeout = WAITTO;
642
643         while((!(((HSCX_READ(h_chan, H_STAR)) &
644                 (HSCX_STAR_CEC | HSCX_STAR_XFW)) == HSCX_STAR_XFW)) && timeout)
645         {
646                 DELAY(WAITVAL);
647                 timeout--;
648         }
649
650         if(timeout == 0)
651         {
652                 NDBGL1(L1_H_ERR, "HSCX wait for XFW timeout!");
653         }
654         else if (timeout != WAITTO)
655         {
656                 NDBGL1(L1_H_XFRERR, "HSCX wait for XFW time: %d uS", (WAITTO-timeout)*50);
657         }
658 }
659                 
660 #endif /* NISIC > 0 */