1 /* $Id: hfc_2bs0.c,v 1.20.2.6 2004/02/11 13:21:33 keil Exp $
3 * specific routines for CCD's HFC 2BS0
6 * Copyright by Karsten Keil <keil@isdn4linux.de>
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
13 #include <linux/init.h>
18 #include <linux/interrupt.h>
21 WaitForBusy(struct IsdnCardState *cs)
26 while (!(cs->BC_Read_Reg(cs, HFC_STATUS, 0) & HFC_BUSY) && to) {
27 val = cs->BC_Read_Reg(cs, HFC_DATA, HFC_CIP | HFC_F2 |
28 (cs->hw.hfc.cip & 3));
33 printk(KERN_WARNING "HiSax: waitforBusy timeout\n");
40 WaitNoBusy(struct IsdnCardState *cs)
44 while ((cs->BC_Read_Reg(cs, HFC_STATUS, 0) & HFC_BUSY) && to) {
49 printk(KERN_WARNING "HiSax: waitforBusy timeout\n");
56 GetFreeFifoBytes(struct BCState *bcs)
60 if (bcs->hw.hfc.f1 == bcs->hw.hfc.f2)
61 return (bcs->cs->hw.hfc.fifosize);
62 s = bcs->hw.hfc.send[bcs->hw.hfc.f1] - bcs->hw.hfc.send[bcs->hw.hfc.f2];
64 s += bcs->cs->hw.hfc.fifosize;
65 s = bcs->cs->hw.hfc.fifosize - s;
70 ReadZReg(struct BCState *bcs, u_char reg)
75 val = 256 * bcs->cs->BC_Read_Reg(bcs->cs, HFC_DATA, reg | HFC_CIP | HFC_Z_HIGH);
77 val += bcs->cs->BC_Read_Reg(bcs->cs, HFC_DATA, reg | HFC_CIP | HFC_Z_LOW);
82 hfc_clear_fifo(struct BCState *bcs)
84 struct IsdnCardState *cs = bcs->cs;
89 if ((cs->debug & L1_DEB_HSCX) && !(cs->debug & L1_DEB_HSCX_FIFO))
90 debugl1(cs, "hfc_clear_fifo");
91 cip = HFC_CIP | HFC_F1 | HFC_REC | HFC_CHANNEL(bcs->channel);
92 if ((cip & 0xc3) != (cs->hw.hfc.cip & 0xc3)) {
93 cs->BC_Write_Reg(cs, HFC_STATUS, cip, cip);
97 f1 = cs->BC_Read_Reg(cs, HFC_DATA, cip);
98 cip = HFC_CIP | HFC_F2 | HFC_REC | HFC_CHANNEL(bcs->channel);
100 f2 = cs->BC_Read_Reg(cs, HFC_DATA, cip);
101 z1 = ReadZReg(bcs, HFC_Z1 | HFC_REC | HFC_CHANNEL(bcs->channel));
102 z2 = ReadZReg(bcs, HFC_Z2 | HFC_REC | HFC_CHANNEL(bcs->channel));
104 while (((f1 != f2) || (z1 != z2)) && cnt--) {
105 if (cs->debug & L1_DEB_HSCX)
106 debugl1(cs, "hfc clear %d f1(%d) f2(%d)",
107 bcs->channel, f1, f2);
110 rcnt += cs->hw.hfc.fifosize;
113 if (cs->debug & L1_DEB_HSCX)
114 debugl1(cs, "hfc clear %d z1(%x) z2(%x) cnt(%d)",
115 bcs->channel, z1, z2, rcnt);
116 cip = HFC_CIP | HFC_FIFO_OUT | HFC_REC | HFC_CHANNEL(bcs->channel);
118 while ((idx < rcnt) && WaitNoBusy(cs)) {
119 cs->BC_Read_Reg(cs, HFC_DATA_NODEB, cip);
124 cs->BC_Read_Reg(cs, HFC_DATA, HFC_CIP | HFC_F2_INC | HFC_REC |
125 HFC_CHANNEL(bcs->channel));
128 cip = HFC_CIP | HFC_F1 | HFC_REC | HFC_CHANNEL(bcs->channel);
130 f1 = cs->BC_Read_Reg(cs, HFC_DATA, cip);
131 cip = HFC_CIP | HFC_F2 | HFC_REC | HFC_CHANNEL(bcs->channel);
133 f2 = cs->BC_Read_Reg(cs, HFC_DATA, cip);
134 z1 = ReadZReg(bcs, HFC_Z1 | HFC_REC | HFC_CHANNEL(bcs->channel));
135 z2 = ReadZReg(bcs, HFC_Z2 | HFC_REC | HFC_CHANNEL(bcs->channel));
141 static struct sk_buff
143 hfc_empty_fifo(struct BCState *bcs, int count)
147 struct IsdnCardState *cs = bcs->cs;
152 if ((cs->debug & L1_DEB_HSCX) && !(cs->debug & L1_DEB_HSCX_FIFO))
153 debugl1(cs, "hfc_empty_fifo");
155 if (count > HSCX_BUFMAX + 3) {
156 if (cs->debug & L1_DEB_WARN)
157 debugl1(cs, "hfc_empty_fifo: incoming packet too large");
158 cip = HFC_CIP | HFC_FIFO_OUT | HFC_REC | HFC_CHANNEL(bcs->channel);
159 while ((idx++ < count) && WaitNoBusy(cs))
160 cs->BC_Read_Reg(cs, HFC_DATA_NODEB, cip);
162 stat = cs->BC_Read_Reg(cs, HFC_DATA, HFC_CIP | HFC_F2_INC | HFC_REC |
163 HFC_CHANNEL(bcs->channel));
167 if ((count < 4) && (bcs->mode != L1_MODE_TRANS)) {
168 if (cs->debug & L1_DEB_WARN)
169 debugl1(cs, "hfc_empty_fifo: incoming packet too small");
170 cip = HFC_CIP | HFC_FIFO_OUT | HFC_REC | HFC_CHANNEL(bcs->channel);
171 while ((idx++ < count) && WaitNoBusy(cs))
172 cs->BC_Read_Reg(cs, HFC_DATA_NODEB, cip);
174 stat = cs->BC_Read_Reg(cs, HFC_DATA, HFC_CIP | HFC_F2_INC | HFC_REC |
175 HFC_CHANNEL(bcs->channel));
177 #ifdef ERROR_STATISTIC
182 if (bcs->mode == L1_MODE_TRANS)
186 if (!(skb = dev_alloc_skb(count)))
187 printk(KERN_WARNING "HFC: receive out of memory\n");
189 ptr = skb_put(skb, count);
191 cip = HFC_CIP | HFC_FIFO_OUT | HFC_REC | HFC_CHANNEL(bcs->channel);
192 while ((idx < count) && WaitNoBusy(cs)) {
193 *ptr++ = cs->BC_Read_Reg(cs, HFC_DATA_NODEB, cip);
197 debugl1(cs, "RFIFO BUSY error");
198 printk(KERN_WARNING "HFC FIFO channel %d BUSY Error\n", bcs->channel);
199 dev_kfree_skb_any(skb);
200 if (bcs->mode != L1_MODE_TRANS) {
202 stat = cs->BC_Read_Reg(cs, HFC_DATA, HFC_CIP | HFC_F2_INC | HFC_REC |
203 HFC_CHANNEL(bcs->channel));
208 if (bcs->mode != L1_MODE_TRANS) {
210 chksum = (cs->BC_Read_Reg(cs, HFC_DATA, cip) << 8);
212 chksum += cs->BC_Read_Reg(cs, HFC_DATA, cip);
214 stat = cs->BC_Read_Reg(cs, HFC_DATA, cip);
215 if (cs->debug & L1_DEB_HSCX)
216 debugl1(cs, "hfc_empty_fifo %d chksum %x stat %x",
217 bcs->channel, chksum, stat);
219 debugl1(cs, "FIFO CRC error");
220 dev_kfree_skb_any(skb);
222 #ifdef ERROR_STATISTIC
227 stat = cs->BC_Read_Reg(cs, HFC_DATA, HFC_CIP | HFC_F2_INC | HFC_REC |
228 HFC_CHANNEL(bcs->channel));
236 hfc_fill_fifo(struct BCState *bcs)
238 struct IsdnCardState *cs = bcs->cs;
246 if (bcs->tx_skb->len <= 0)
249 cip = HFC_CIP | HFC_F1 | HFC_SEND | HFC_CHANNEL(bcs->channel);
250 if ((cip & 0xc3) != (cs->hw.hfc.cip & 0xc3)) {
251 cs->BC_Write_Reg(cs, HFC_STATUS, cip, cip);
255 if (bcs->mode != L1_MODE_TRANS) {
256 bcs->hw.hfc.f1 = cs->BC_Read_Reg(cs, HFC_DATA, cip);
257 cip = HFC_CIP | HFC_F2 | HFC_SEND | HFC_CHANNEL(bcs->channel);
259 bcs->hw.hfc.f2 = cs->BC_Read_Reg(cs, HFC_DATA, cip);
260 bcs->hw.hfc.send[bcs->hw.hfc.f1] = ReadZReg(bcs, HFC_Z1 | HFC_SEND | HFC_CHANNEL(bcs->channel));
261 if (cs->debug & L1_DEB_HSCX)
262 debugl1(cs, "hfc_fill_fifo %d f1(%d) f2(%d) z1(%x)",
263 bcs->channel, bcs->hw.hfc.f1, bcs->hw.hfc.f2,
264 bcs->hw.hfc.send[bcs->hw.hfc.f1]);
265 fcnt = bcs->hw.hfc.f1 - bcs->hw.hfc.f2;
269 if (cs->debug & L1_DEB_HSCX)
270 debugl1(cs, "hfc_fill_fifo more as 30 frames");
273 count = GetFreeFifoBytes(bcs);
277 z1 = ReadZReg(bcs, HFC_Z1 | HFC_REC | HFC_CHANNEL(bcs->channel));
278 z2 = ReadZReg(bcs, HFC_Z2 | HFC_REC | HFC_CHANNEL(bcs->channel));
281 count += cs->hw.hfc.fifosize;
282 } /* L1_MODE_TRANS */
283 if (cs->debug & L1_DEB_HSCX)
284 debugl1(cs, "hfc_fill_fifo %d count(%ld/%d)",
285 bcs->channel, bcs->tx_skb->len,
287 if (count < bcs->tx_skb->len) {
288 if (cs->debug & L1_DEB_HSCX)
289 debugl1(cs, "hfc_fill_fifo no fifo mem");
292 cip = HFC_CIP | HFC_FIFO_IN | HFC_SEND | HFC_CHANNEL(bcs->channel);
294 while ((idx < bcs->tx_skb->len) && WaitNoBusy(cs))
295 cs->BC_Write_Reg(cs, HFC_DATA_NODEB, cip, bcs->tx_skb->data[idx++]);
296 if (idx != bcs->tx_skb->len) {
297 debugl1(cs, "FIFO Send BUSY error");
298 printk(KERN_WARNING "HFC S FIFO channel %d BUSY Error\n", bcs->channel);
300 count = bcs->tx_skb->len;
301 bcs->tx_cnt -= count;
302 if (PACKET_NOACK == bcs->tx_skb->pkt_type)
304 dev_kfree_skb_any(bcs->tx_skb);
306 if (bcs->mode != L1_MODE_TRANS) {
309 cs->BC_Read_Reg(cs, HFC_DATA, HFC_CIP | HFC_F1_INC | HFC_SEND | HFC_CHANNEL(bcs->channel));
311 if (test_bit(FLG_LLI_L1WAKEUP,&bcs->st->lli.flag) &&
314 spin_lock_irqsave(&bcs->aclock, flags);
315 bcs->ackcnt += count;
316 spin_unlock_irqrestore(&bcs->aclock, flags);
317 schedule_event(bcs, B_ACKPENDING);
319 test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
325 main_irq_hfc(struct BCState *bcs)
327 struct IsdnCardState *cs = bcs->cs;
330 int receive, transmit, count = 5;
335 cip = HFC_CIP | HFC_F1 | HFC_REC | HFC_CHANNEL(bcs->channel);
336 if ((cip & 0xc3) != (cs->hw.hfc.cip & 0xc3)) {
337 cs->BC_Write_Reg(cs, HFC_STATUS, cip, cip);
342 if (bcs->mode == L1_MODE_HDLC) {
343 f1 = cs->BC_Read_Reg(cs, HFC_DATA, cip);
344 cip = HFC_CIP | HFC_F2 | HFC_REC | HFC_CHANNEL(bcs->channel);
346 f2 = cs->BC_Read_Reg(cs, HFC_DATA, cip);
348 if (cs->debug & L1_DEB_HSCX)
349 debugl1(cs, "hfc rec %d f1(%d) f2(%d)",
350 bcs->channel, f1, f2);
354 if (receive || (bcs->mode == L1_MODE_TRANS)) {
356 z1 = ReadZReg(bcs, HFC_Z1 | HFC_REC | HFC_CHANNEL(bcs->channel));
357 z2 = ReadZReg(bcs, HFC_Z2 | HFC_REC | HFC_CHANNEL(bcs->channel));
360 rcnt += cs->hw.hfc.fifosize;
361 if ((bcs->mode == L1_MODE_HDLC) || (rcnt)) {
363 if (cs->debug & L1_DEB_HSCX)
364 debugl1(cs, "hfc rec %d z1(%x) z2(%x) cnt(%d)",
365 bcs->channel, z1, z2, rcnt);
367 if ((skb = hfc_empty_fifo(bcs, rcnt))) {
368 skb_queue_tail(&bcs->rqueue, skb);
369 schedule_event(bcs, B_RCVBUFREADY);
376 test_and_set_bit(BC_FLG_BUSY, &bcs->Flag);
378 if (test_bit(BC_FLG_BUSY, &bcs->Flag))
381 if ((bcs->tx_skb = skb_dequeue(&bcs->squeue))) {
383 test_and_set_bit(BC_FLG_BUSY, &bcs->Flag);
385 if (test_bit(BC_FLG_BUSY, &bcs->Flag))
389 schedule_event(bcs, B_XMTBUFREADY);
392 if ((receive || transmit) && count)
398 mode_hfc(struct BCState *bcs, int mode, int bc)
400 struct IsdnCardState *cs = bcs->cs;
402 if (cs->debug & L1_DEB_HSCX)
403 debugl1(cs, "HFC 2BS0 mode %d bchan %d/%d",
404 mode, bc, bcs->channel);
411 cs->hw.hfc.ctmt &= ~1;
412 cs->hw.hfc.isac_spcr &= ~0x03;
415 cs->hw.hfc.ctmt &= ~2;
416 cs->hw.hfc.isac_spcr &= ~0x0c;
419 case (L1_MODE_TRANS):
420 cs->hw.hfc.ctmt &= ~(1 << bc); /* set HDLC mode */
421 cs->BC_Write_Reg(cs, HFC_STATUS, cs->hw.hfc.ctmt, cs->hw.hfc.ctmt);
422 hfc_clear_fifo(bcs); /* complete fifo clear */
424 cs->hw.hfc.ctmt |= 1;
425 cs->hw.hfc.isac_spcr &= ~0x03;
426 cs->hw.hfc.isac_spcr |= 0x02;
428 cs->hw.hfc.ctmt |= 2;
429 cs->hw.hfc.isac_spcr &= ~0x0c;
430 cs->hw.hfc.isac_spcr |= 0x08;
435 cs->hw.hfc.ctmt &= ~1;
436 cs->hw.hfc.isac_spcr &= ~0x03;
437 cs->hw.hfc.isac_spcr |= 0x02;
439 cs->hw.hfc.ctmt &= ~2;
440 cs->hw.hfc.isac_spcr &= ~0x0c;
441 cs->hw.hfc.isac_spcr |= 0x08;
445 cs->BC_Write_Reg(cs, HFC_STATUS, cs->hw.hfc.ctmt, cs->hw.hfc.ctmt);
446 cs->writeisac(cs, ISAC_SPCR, cs->hw.hfc.isac_spcr);
447 if (mode == L1_MODE_HDLC)
452 hfc_l2l1(struct PStack *st, int pr, void *arg)
454 struct BCState *bcs = st->l1.bcs;
455 struct sk_buff *skb = arg;
459 case (PH_DATA | REQUEST):
460 spin_lock_irqsave(&bcs->cs->lock, flags);
462 skb_queue_tail(&bcs->squeue, skb);
465 test_and_set_bit(BC_FLG_BUSY, &bcs->Flag);
466 bcs->cs->BC_Send_Data(bcs);
468 spin_unlock_irqrestore(&bcs->cs->lock, flags);
470 case (PH_PULL | INDICATION):
471 spin_lock_irqsave(&bcs->cs->lock, flags);
473 printk(KERN_WARNING "hfc_l2l1: this shouldn't happen\n");
475 test_and_set_bit(BC_FLG_BUSY, &bcs->Flag);
477 bcs->cs->BC_Send_Data(bcs);
479 spin_unlock_irqrestore(&bcs->cs->lock, flags);
481 case (PH_PULL | REQUEST):
483 test_and_clear_bit(FLG_L1_PULL_REQ, &st->l1.Flags);
484 st->l1.l1l2(st, PH_PULL | CONFIRM, NULL);
486 test_and_set_bit(FLG_L1_PULL_REQ, &st->l1.Flags);
488 case (PH_ACTIVATE | REQUEST):
489 spin_lock_irqsave(&bcs->cs->lock, flags);
490 test_and_set_bit(BC_FLG_ACTIV, &bcs->Flag);
491 mode_hfc(bcs, st->l1.mode, st->l1.bc);
492 spin_unlock_irqrestore(&bcs->cs->lock, flags);
493 l1_msg_b(st, pr, arg);
495 case (PH_DEACTIVATE | REQUEST):
496 l1_msg_b(st, pr, arg);
498 case (PH_DEACTIVATE | CONFIRM):
499 spin_lock_irqsave(&bcs->cs->lock, flags);
500 test_and_clear_bit(BC_FLG_ACTIV, &bcs->Flag);
501 test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
502 mode_hfc(bcs, 0, st->l1.bc);
503 spin_unlock_irqrestore(&bcs->cs->lock, flags);
504 st->l1.l1l2(st, PH_DEACTIVATE | CONFIRM, NULL);
511 close_hfcstate(struct BCState *bcs)
513 mode_hfc(bcs, 0, bcs->channel);
514 if (test_bit(BC_FLG_INIT, &bcs->Flag)) {
515 skb_queue_purge(&bcs->rqueue);
516 skb_queue_purge(&bcs->squeue);
518 dev_kfree_skb_any(bcs->tx_skb);
520 test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
523 test_and_clear_bit(BC_FLG_INIT, &bcs->Flag);
527 open_hfcstate(struct IsdnCardState *cs, struct BCState *bcs)
529 if (!test_and_set_bit(BC_FLG_INIT, &bcs->Flag)) {
530 skb_queue_head_init(&bcs->rqueue);
531 skb_queue_head_init(&bcs->squeue);
534 test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
541 setstack_hfc(struct PStack *st, struct BCState *bcs)
543 bcs->channel = st->l1.bc;
544 if (open_hfcstate(st->l1.hardware, bcs))
547 st->l2.l2l1 = hfc_l2l1;
548 setstack_manager(st);
555 init_send(struct BCState *bcs)
559 if (!(bcs->hw.hfc.send = kmalloc(32 * sizeof(unsigned int), GFP_ATOMIC))) {
561 "HiSax: No memory for hfc.send\n");
564 for (i = 0; i < 32; i++)
565 bcs->hw.hfc.send[i] = 0x1fff;
569 inithfc(struct IsdnCardState *cs)
571 init_send(&cs->bcs[0]);
572 init_send(&cs->bcs[1]);
573 cs->BC_Send_Data = &hfc_fill_fifo;
574 cs->bcs[0].BC_SetStack = setstack_hfc;
575 cs->bcs[1].BC_SetStack = setstack_hfc;
576 cs->bcs[0].BC_Close = close_hfcstate;
577 cs->bcs[1].BC_Close = close_hfcstate;
578 mode_hfc(cs->bcs, 0, 0);
579 mode_hfc(cs->bcs + 1, 0, 0);
583 releasehfc(struct IsdnCardState *cs)
585 if (cs->bcs[0].hw.hfc.send) {
586 kfree(cs->bcs[0].hw.hfc.send);
587 cs->bcs[0].hw.hfc.send = NULL;
589 if (cs->bcs[1].hw.hfc.send) {
590 kfree(cs->bcs[1].hw.hfc.send);
591 cs->bcs[1].hw.hfc.send = NULL;