2 * Copyright (c) 1999, 2000 Udo Schweigert. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 * 4. Altered versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software and/or documentation.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 *---------------------------------------------------------------------------
33 * i4b_ifpnp_avm.c: AVM Fritz!Card PnP hardware driver
34 * ---------------------------------------------------
36 * $FreeBSD: src/sys/i4b/layer1/ifpnp/i4b_ifpnp_avm.c,v 1.5.2.1 2001/08/10 14:08:37 obrien Exp $
38 * last edit-date: [Fri Jan 12 17:05:28 2001]
40 *---------------------------------------------------------------------------*/
42 #include "use_ifpnp.h"
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/systm.h>
53 #include <sys/socket.h>
54 #include <sys/thread2.h>
56 #include <bus/isa/isavar.h>
59 #include <net/i4b/include/machine/i4b_debug.h>
60 #include <net/i4b/include/machine/i4b_ioctl.h>
61 #include <net/i4b/include/machine/i4b_trace.h>
63 #include "../../include/i4b_global.h"
64 #include "../../include/i4b_mbuf.h"
66 #include "../i4b_l1.h"
67 #include "../isic/i4b_isic.h"
68 #include "../isic/i4b_isac.h"
69 #include "../isic/i4b_hscx.h"
71 #include "i4b_ifpnp_ext.h"
74 static void avm_pnp_intr(void *);
75 static void hscx_write_reg(int, u_int, struct l1_softc *, u_int);
76 static void hscx_write_reg_val(int, u_int, u_int8_t, struct l1_softc *);
77 static u_int hscx_read_reg(int, u_int, struct l1_softc *);
78 static void hscx_read_fifo(int, void *, size_t, struct l1_softc *);
79 static void hscx_write_fifo(int, void *, size_t, struct l1_softc *);
80 static void avm_pnp_hscx_int_handler(struct l1_softc *);
81 static void avm_pnp_hscx_intr(int, u_int, u_int, struct l1_softc *);
82 static void avm_pnp_init_linktab(struct l1_softc *);
83 static void avm_pnp_bchannel_setup(int, int, int, int);
84 static void avm_pnp_bchannel_start(int, int);
85 static void avm_pnp_hscx_init(struct l1_softc *, int, int);
86 static void avm_pnp_bchannel_stat(int, int, bchan_statistics_t *);
87 static void avm_pnp_set_linktab(int, int, drvr_link_t *);
88 static isdn_link_t * avm_pnp_ret_linktab(int, int);
89 static int avm_pnp_probe(device_t);
90 static int avm_pnp_hscx_fifo(l1_bchan_state_t *, struct l1_softc *);
91 int avm_pnp_attach(device_t);
92 static void ifpnp_isac_intr(struct l1_softc *sc);
94 static device_method_t avm_pnp_methods[] = {
95 /* Device interface */
96 DEVMETHOD(device_probe, avm_pnp_probe),
97 DEVMETHOD(device_attach, avm_pnp_attach),
101 static driver_t avm_pnp_driver = {
104 sizeof(struct l1_softc)
107 static devclass_t avm_pnp_devclass;
109 DRIVER_MODULE(avm_pnp, isa, avm_pnp_driver, avm_pnp_devclass, NULL, NULL);
111 /* jump table for multiplex routines */
113 struct i4b_l1mux_func avm_pnp_l1mux_func = {
116 ifpnp_mph_command_req,
118 ifpnp_ph_activate_req,
121 struct l1_softc *ifpnp_scp[IFPNP_MAXUNIT];
123 /*---------------------------------------------------------------------------*
124 * AVM PnP Fritz!Card special registers
125 *---------------------------------------------------------------------------*/
128 * register offsets from i/o base
130 #define CLASS_OFFSET 0x00
131 #define REVISION_OFFSET 0x01
132 #define STAT0_OFFSET 0x02
133 #define STAT1_OFFSET 0x03
134 #define ADDR_REG_OFFSET 0x04
135 /*#define MODREG_OFFSET 0x06
136 #define VERREG_OFFSET 0x07*/
138 /* these 2 are used to select an ISAC register set */
139 #define ISAC_LO_REG_OFFSET 0x04
140 #define ISAC_HI_REG_OFFSET 0x06
142 /* offset higher than this goes to the HI register set */
143 #define MAX_LO_REG_OFFSET 0x2f
145 /* mask for the offset */
146 #define ISAC_REGSET_MASK 0x0f
148 /* the offset from the base to the ISAC registers */
149 #define ISAC_REG_OFFSET 0x10
151 /* the offset from the base to the ISAC FIFO */
152 #define ISAC_FIFO 0x02
154 /* not really the HSCX, but sort of */
155 #define HSCX_FIFO 0x00
156 #define HSCX_STAT 0x04
159 * AVM PnP Status Latch 0 read only bits
161 #define ASL_IRQ_ISAC 0x01 /* ISAC interrupt, active low */
162 #define ASL_IRQ_HSCX 0x02 /* HSX interrupt, active low */
163 #define ASL_IRQ_TIMER 0x04 /* Timer interrupt, active low */
164 #define ASL_IRQ_BCHAN ASL_IRQ_HSCX
165 /* actually active LOW */
166 #define ASL_IRQ_Pending (ASL_IRQ_ISAC | ASL_IRQ_HSCX | ASL_IRQ_TIMER)
169 * AVM Status Latch 0 write only bits
171 #define ASL_RESET_ALL 0x01 /* reset siemens IC's, active 1 */
172 #define ASL_TIMERDISABLE 0x02 /* active high */
173 #define ASL_TIMERRESET 0x04 /* active high */
174 #define ASL_ENABLE_INT 0x08 /* active high */
175 #define ASL_TESTBIT 0x10 /* active high */
178 * AVM Status Latch 1 write only bits
180 #define ASL1_INTSEL 0x0f /* active high */
181 #define ASL1_ENABLE_IOM 0x80 /* active high */
186 #define HSCX_MODE_ITF_FLG 0x01
187 #define HSCX_MODE_TRANS 0x02
188 #define HSCX_MODE_CCR_7 0x04
189 #define HSCX_MODE_CCR_16 0x08
190 #define HSCX_MODE_TESTLOOP 0x80
195 #define HSCX_STAT_RME 0x01
196 #define HSCX_STAT_RDO 0x10
197 #define HSCX_STAT_CRCVFRRAB 0x0E
198 #define HSCX_STAT_CRCVFR 0x06
199 #define HSCX_STAT_RML_MASK 0x3f00
202 * "HSCX" interrupt bits
204 #define HSCX_INT_XPR 0x80
205 #define HSCX_INT_XDU 0x40
206 #define HSCX_INT_RPR 0x20
207 #define HSCX_INT_MASK 0xE0
210 * "HSCX" command bits
212 #define HSCX_CMD_XRS 0x80
213 #define HSCX_CMD_XME 0x01
214 #define HSCX_CMD_RRS 0x20
215 #define HSCX_CMD_XML_MASK 0x3f00
218 * to prevent deactivating the "HSCX" when both channels are active we
219 * define an HSCX_ACTIVE flag which is or'd into the channel's state
220 * flag in avm_pnp_bchannel_setup upon active and cleared upon deactivation.
221 * It is set high to allow room for new flags.
223 #define HSCX_AVMA1PP_ACTIVE 0x1000
225 /*---------------------------------------------------------------------------*
226 * AVM read fifo routines
227 *---------------------------------------------------------------------------*/
230 avm_pnp_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
232 bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
233 bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]);
237 bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, ISAC_FIFO);
238 bus_space_read_multi_1(btag, bhandle, ISAC_REG_OFFSET, buf, size);
240 case ISIC_WHAT_HSCXA:
241 hscx_read_fifo(0, buf, size, sc);
243 case ISIC_WHAT_HSCXB:
244 hscx_read_fifo(1, buf, size, sc);
250 hscx_read_fifo(int chan, void *buf, size_t len, struct l1_softc *sc)
254 bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
255 bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]);
257 bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, chan);
258 ip = (u_int8_t *)buf;
262 *ip++ = bus_space_read_1(btag, bhandle, ISAC_REG_OFFSET);
266 /*---------------------------------------------------------------------------*
267 * AVM write fifo routines
268 *---------------------------------------------------------------------------*/
270 avm_pnp_write_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
272 bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
273 bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]);
277 bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, ISAC_FIFO);
278 bus_space_write_multi_1(btag, bhandle, ISAC_REG_OFFSET, (u_int8_t*)buf, size);
280 case ISIC_WHAT_HSCXA:
281 hscx_write_fifo(0, buf, size, sc);
283 case ISIC_WHAT_HSCXB:
284 hscx_write_fifo(1, buf, size, sc);
290 hscx_write_fifo(int chan, void *buf, size_t len, struct l1_softc *sc)
294 l1_bchan_state_t *Bchan = &sc->sc_chan[chan];
295 bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
296 bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]);
298 sc->avma1pp_cmd &= ~HSCX_CMD_XME;
301 if (Bchan->out_mbuf_cur == NULL)
303 if (Bchan->bprot != BPROT_NONE)
304 sc->avma1pp_cmd |= HSCX_CMD_XME;
306 if (len != sc->sc_bfifolen)
307 sc->avma1pp_txl = len;
309 hscx_write_reg(chan, HSCX_STAT, sc, 3);
311 ip = (u_int8_t *)buf;
315 bus_space_write_1(btag, bhandle, ISAC_REG_OFFSET, *ip++);
319 /*---------------------------------------------------------------------------*
320 * AVM write register routines
321 *---------------------------------------------------------------------------*/
324 avm_pnp_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
327 bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
328 bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]);
332 reg_bank = (offs > MAX_LO_REG_OFFSET) ? ISAC_HI_REG_OFFSET:ISAC_LO_REG_OFFSET;
333 /* set the register bank */
334 bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, reg_bank);
335 bus_space_write_1(btag, bhandle, ISAC_REG_OFFSET + (offs & ISAC_REGSET_MASK), data);
337 case ISIC_WHAT_HSCXA:
338 hscx_write_reg_val(0, offs, data, sc);
340 case ISIC_WHAT_HSCXB:
341 hscx_write_reg_val(1, offs, data, sc);
347 hscx_write_reg(int chan, u_int off, struct l1_softc *sc, u_int which)
349 bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
350 bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]);
352 /* point at the correct channel */
353 bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, chan);
355 bus_space_write_1(btag, bhandle, ISAC_REG_OFFSET + off + 2, sc->avma1pp_prot);
357 bus_space_write_1(btag, bhandle, ISAC_REG_OFFSET + off + 1, sc->avma1pp_txl);
359 bus_space_write_1(btag, bhandle, ISAC_REG_OFFSET + off, sc->avma1pp_cmd);
363 hscx_write_reg_val(int chan, u_int off, u_int8_t val, struct l1_softc *sc)
365 bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
366 bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]);
368 /* point at the correct channel */
369 bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, chan);
370 bus_space_write_1(btag, bhandle, ISAC_REG_OFFSET + off, val);
373 /*---------------------------------------------------------------------------*
374 * AVM read register routines
375 *---------------------------------------------------------------------------*/
377 avm_pnp_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
380 bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
381 bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]);
385 reg_bank = (offs > MAX_LO_REG_OFFSET) ? ISAC_HI_REG_OFFSET:ISAC_LO_REG_OFFSET;
386 /* set the register bank */
387 bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, reg_bank);
388 return(bus_space_read_1(btag, bhandle, ISAC_REG_OFFSET +
389 (offs & ISAC_REGSET_MASK)));
390 case ISIC_WHAT_HSCXA:
391 return hscx_read_reg(0, offs, sc);
392 case ISIC_WHAT_HSCXB:
393 return hscx_read_reg(1, offs, sc);
399 hscx_read_reg(int chan, u_int off, struct l1_softc *sc)
401 bus_space_handle_t bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
402 bus_space_tag_t btag = rman_get_bustag(sc->sc_resources.io_base[0]);
404 /* point at the correct channel */
405 bus_space_write_1(btag, bhandle, ADDR_REG_OFFSET, chan);
406 return(bus_space_read_1(btag, bhandle, ISAC_REG_OFFSET + off));
410 static struct ifpnp_ids {
414 { 0x0009cd06, "AVM Fritz!Card PnP" },
418 /*---------------------------------------------------------------------------*
419 * avm_pnp_probe - probe for a card
420 *---------------------------------------------------------------------------*/
422 avm_pnp_probe(device_t dev)
424 struct ifpnp_ids *ids; /* pnp id's */
425 char *string = NULL; /* the name */
426 u_int32_t vend_id = isa_get_vendorid(dev); /* vendor id */
428 /* search table of knowd id's */
430 for(ids = ifpnp_ids; ids->vend_id != 0; ids++)
432 if(vend_id == ids->vend_id)
434 string = ids->id_str;
439 if(string) /* set name if we have one */
441 device_set_desc(dev, string); /* set description */
450 /*---------------------------------------------------------------------------*
451 * avm_pnp_attach - attach Fritz!Card PnP
452 *---------------------------------------------------------------------------*/
454 avm_pnp_attach(device_t dev)
461 bus_space_handle_t bhandle;
462 bus_space_tag_t btag;
466 vid = isa_get_vendorid(dev);
467 sc = device_get_softc(dev);
468 unit = device_get_unit(dev);
469 bzero(sc, sizeof(struct l1_softc));
471 /* probably not really required */
472 if(unit > IFPNP_MAXUNIT) {
473 kprintf("avm_pnp%d: Error, unit > IFPNP_MAXUNIT!\n", unit);
478 ifpnp_scp[unit] = sc;
482 if(!(sc->sc_resources.io_base[0] =
483 bus_alloc_resource(dev, SYS_RES_IOPORT,
484 &sc->sc_resources.io_rid[0],
485 0UL, ~0UL, 1, RF_ACTIVE ) ))
487 kprintf("avm_pnp_attach: Couldn't get my io_base.\n");
490 if (sc->sc_resources.io_base[0] == NULL) {
491 kprintf("avm_pnp%d: couldn't map IO port\n", unit);
496 bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
497 btag = rman_get_bustag(sc->sc_resources.io_base[0]);
499 /* will not be used for pnp devices */
500 sc->sc_port = rman_get_start(sc->sc_resources.io_base[0]);
502 /* get irq, release io_base if we don't get it */
504 if(!(sc->sc_resources.irq =
505 bus_alloc_resource(dev, SYS_RES_IRQ,
506 &sc->sc_resources.irq_rid,
507 0UL, ~0UL, 1, RF_ACTIVE)))
509 kprintf("avm_pnp%d: Could not get irq.\n",unit);
515 sc->sc_irq = rman_get_start(sc->sc_resources.irq);
516 bus_setup_intr(dev, sc->sc_resources.irq, 0,
517 (void(*)(void*))avm_pnp_intr, sc,
521 /* end of new-bus stuff */
523 ISAC_BASE = (caddr_t)ISIC_WHAT_ISAC;
525 HSCX_A_BASE = (caddr_t)ISIC_WHAT_HSCXA;
526 HSCX_B_BASE = (caddr_t)ISIC_WHAT_HSCXB;
528 /* setup access routines */
531 sc->readreg = avm_pnp_read_reg;
532 sc->writereg = avm_pnp_write_reg;
534 sc->readfifo = avm_pnp_read_fifo;
535 sc->writefifo = avm_pnp_write_fifo;
537 /* setup card type */
539 sc->sc_cardtyp = CARD_TYPEP_AVM_PNP;
541 /* setup IOM bus type */
543 sc->sc_bustyp = BUS_TYPE_IOM2;
545 /* set up some other miscellaneous things */
547 sc->sc_bfifolen = HSCX_FIFO_LEN;
550 /* the Linux driver does this to clear any pending ISAC interrupts */
552 v = ISAC_READ(I_STAR);
553 v = ISAC_READ(I_MODE);
554 v = ISAC_READ(I_ADF2);
555 v = ISAC_READ(I_ISTA);
556 if (v & ISAC_ISTA_EXI)
558 v = ISAC_READ(I_EXIR);
560 v = ISAC_READ(I_CIRR);
561 ISAC_WRITE(I_MASK, 0xff);
562 /* the Linux driver does this to clear any pending HSCX interrupts */
563 v = hscx_read_reg(0, HSCX_STAT, sc);
564 v = hscx_read_reg(1, HSCX_STAT, sc);
566 bus_space_write_1(btag, bhandle, STAT0_OFFSET, ASL_RESET_ALL|ASL_TIMERDISABLE);
567 DELAY(SEC_DELAY/100); /* 10 ms */
568 bus_space_write_1(btag, bhandle, STAT1_OFFSET, ASL1_ENABLE_IOM|sc->sc_irq);
569 DELAY(SEC_DELAY/100); /* 10 ms */
570 bus_space_write_1(btag, bhandle, STAT0_OFFSET, ASL_TIMERRESET|ASL_ENABLE_INT|ASL_TIMERDISABLE);
571 DELAY(SEC_DELAY/100); /* 10 ms */
573 kprintf("ifpnp%d: AVM Fritz!Card PnP Class %#x Revision %d \n", unit,
574 bus_space_read_1(btag, bhandle, CLASS_OFFSET),
575 bus_space_read_1(btag, bhandle, REVISION_OFFSET));
577 kprintf("ifpnp%d: ISAC %s (IOM-%c)\n", unit,
578 "2085 Version A1/A2 or 2086/2186 Version 1.1",
579 sc->sc_bustyp == BUS_TYPE_IOM1 ? '1' : '2');
585 /* init the "HSCX" */
586 avm_pnp_bchannel_setup(sc->sc_unit, HSCX_CH_A, BPROT_NONE, 0);
588 avm_pnp_bchannel_setup(sc->sc_unit, HSCX_CH_B, BPROT_NONE, 0);
590 /* can't use the normal B-Channel stuff */
591 avm_pnp_init_linktab(sc);
593 /* set trace level */
595 sc->sc_trace = TRACE_OFF;
597 sc->sc_state = ISAC_IDLE;
609 sc->sc_freeflag2 = 0;
611 callout_init(&sc->sc_T3_timeout);
612 callout_init(&sc->sc_T4_timeout);
614 /* init higher protocol layers */
616 i4b_l1_mph_status_ind(L0IFPNPUNIT(sc->sc_unit), STI_ATTACH, sc->sc_cardtyp, &avm_pnp_l1mux_func);
624 * this is the real interrupt routine
627 avm_pnp_hscx_intr(int h_chan, u_int stat, u_int cnt, struct l1_softc *sc)
629 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
632 NDBGL1(L1_H_IRQ, "%#x", stat);
634 if((stat & HSCX_INT_XDU) && (chan->bprot != BPROT_NONE))/* xmit data underrun */
637 NDBGL1(L1_H_XFRERR, "xmit data underrun");
638 /* abort the transmission */
640 sc->avma1pp_cmd |= HSCX_CMD_XRS;
641 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
642 sc->avma1pp_cmd &= ~HSCX_CMD_XRS;
643 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
645 if (chan->out_mbuf_head != NULL) /* don't continue to transmit this buffer */
647 i4b_Bfreembuf(chan->out_mbuf_head);
648 chan->out_mbuf_cur = chan->out_mbuf_head = NULL;
653 * The following is based on examination of the Linux driver.
655 * The logic here is different than with a "real" HSCX; all kinds
656 * of information (interrupt/status bits) are in stat.
657 * HSCX_INT_RPR indicates a receive interrupt
658 * HSCX_STAT_RDO indicates an overrun condition, abort -
659 * otherwise read the bytes ((stat & HSCX_STZT_RML_MASK) >> 8)
660 * HSCX_STAT_RME indicates end-of-frame and apparently any
661 * CRC/framing errors are only reported in this state.
662 * if ((stat & HSCX_STAT_CRCVFRRAB) != HSCX_STAT_CRCVFR)
666 if(stat & HSCX_INT_RPR)
670 /* always have to read the FIFO, so use a scratch buffer */
671 u_char scrbuf[HSCX_FIFO_LEN];
673 if(stat & HSCX_STAT_RDO)
676 NDBGL1(L1_H_XFRERR, "receive data overflow");
681 * check whether we're receiving data for an inactive B-channel
682 * and discard it. This appears to happen for telephony when
683 * both B-channels are active and one is deactivated. Since
684 * it is not really possible to deactivate the channel in that
685 * case (the ASIC seems to deactivate _both_ channels), the
686 * "deactivated" channel keeps receiving data which can lead
687 * to exhaustion of mbufs and a kernel panic.
689 * This is a hack, but it's the only solution I can think of
690 * without having the documentation for the ASIC.
693 if (chan->state == HSCX_IDLE)
695 NDBGL1(L1_H_XFRERR, "toss data from %d", h_chan);
701 if(fifo_data_len == 0)
702 fifo_data_len = sc->sc_bfifolen;
704 /* ALWAYS read data from HSCX fifo */
706 HSCX_RDFIFO(h_chan, scrbuf, fifo_data_len);
707 chan->rxcount += fifo_data_len;
709 /* all error conditions checked, now decide and take action */
713 if(chan->in_mbuf == NULL)
715 if((chan->in_mbuf = i4b_Bgetmbuf(BCH_MAX_DATALEN)) == NULL)
716 panic("L1 avm_pnp_hscx_intr: RME, cannot allocate mbuf!\n");
717 chan->in_cbptr = chan->in_mbuf->m_data;
721 if((chan->in_len + fifo_data_len) <= BCH_MAX_DATALEN)
723 /* OK to copy the data */
724 bcopy(scrbuf, chan->in_cbptr, fifo_data_len);
725 chan->in_cbptr += fifo_data_len;
726 chan->in_len += fifo_data_len;
728 /* setup mbuf data length */
730 chan->in_mbuf->m_len = chan->in_len;
731 chan->in_mbuf->m_pkthdr.len = chan->in_len;
733 if(sc->sc_trace & TRACE_B_RX)
736 hdr.unit = L0IFPNPUNIT(sc->sc_unit);
737 hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
739 hdr.count = ++sc->sc_trace_bcount;
741 i4b_l1_trace_ind(&hdr, chan->in_mbuf->m_len, chan->in_mbuf->m_data);
744 if (stat & HSCX_STAT_RME)
746 if((stat & HSCX_STAT_CRCVFRRAB) == HSCX_STAT_CRCVFR)
748 (*chan->isic_drvr_linktab->bch_rx_data_ready)(chan->isic_drvr_linktab->unit);
751 /* mark buffer ptr as unused */
753 chan->in_mbuf = NULL;
754 chan->in_cbptr = NULL;
760 NDBGL1(L1_H_XFRERR, "CRC/RAB");
761 if (chan->in_mbuf != NULL)
763 i4b_Bfreembuf(chan->in_mbuf);
764 chan->in_mbuf = NULL;
765 chan->in_cbptr = NULL;
770 } /* END enough space in mbuf */
773 if(chan->bprot == BPROT_NONE)
775 /* setup mbuf data length */
777 chan->in_mbuf->m_len = chan->in_len;
778 chan->in_mbuf->m_pkthdr.len = chan->in_len;
780 if(sc->sc_trace & TRACE_B_RX)
783 hdr.unit = L0IFPNPUNIT(sc->sc_unit);
784 hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
786 hdr.count = ++sc->sc_trace_bcount;
788 i4b_l1_trace_ind(&hdr, chan->in_mbuf->m_len, chan->in_mbuf->m_data);
791 if(!(i4b_l1_bchan_tel_silence(chan->in_mbuf->m_data, chan->in_mbuf->m_len)))
794 /* move rx'd data to rx queue */
795 if(!(IF_QFULL(&chan->rx_queue)))
797 IF_ENQUEUE(&chan->rx_queue, chan->in_mbuf);
801 i4b_Bfreembuf(chan->in_mbuf);
803 /* signal upper layer that data are available */
804 (*chan->isic_drvr_linktab->bch_rx_data_ready)(chan->isic_drvr_linktab->unit);
806 /* alloc new buffer */
808 if((chan->in_mbuf = i4b_Bgetmbuf(BCH_MAX_DATALEN)) == NULL)
809 panic("L1 avm_pnp_hscx_intr: RPF, cannot allocate new mbuf!\n");
811 /* setup new data ptr */
813 chan->in_cbptr = chan->in_mbuf->m_data;
815 /* OK to copy the data */
816 bcopy(scrbuf, chan->in_cbptr, fifo_data_len);
818 chan->in_cbptr += fifo_data_len;
819 chan->in_len = fifo_data_len;
821 chan->rxcount += fifo_data_len;
825 NDBGL1(L1_H_XFRERR, "RAWHDLC rx buffer overflow in RPF, in_len=%d", chan->in_len);
826 chan->in_cbptr = chan->in_mbuf->m_data;
830 } /* if(error == 0) */
833 /* land here for RDO */
834 if (chan->in_mbuf != NULL)
836 i4b_Bfreembuf(chan->in_mbuf);
837 chan->in_mbuf = NULL;
838 chan->in_cbptr = NULL;
842 sc->avma1pp_cmd |= HSCX_CMD_RRS;
843 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
844 sc->avma1pp_cmd &= ~HSCX_CMD_RRS;
845 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
850 /* transmit fifo empty, new data can be written to fifo */
852 if(stat & HSCX_INT_XPR)
855 * for a description what is going on here, please have
856 * a look at isic_bchannel_start() in i4b_bchan.c !
859 NDBGL1(L1_H_IRQ, "unit %d, chan %d - XPR, Tx Fifo Empty!", sc->sc_unit, h_chan);
861 if(chan->out_mbuf_cur == NULL) /* last frame is transmitted */
863 IF_DEQUEUE(&chan->tx_queue, chan->out_mbuf_head);
865 if(chan->out_mbuf_head == NULL)
867 chan->state &= ~HSCX_TX_ACTIVE;
868 (*chan->isic_drvr_linktab->bch_tx_queue_empty)(chan->isic_drvr_linktab->unit);
872 chan->state |= HSCX_TX_ACTIVE;
873 chan->out_mbuf_cur = chan->out_mbuf_head;
874 chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
875 chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
877 if(sc->sc_trace & TRACE_B_TX)
880 hdr.unit = L0IFPNPUNIT(sc->sc_unit);
881 hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
883 hdr.count = ++sc->sc_trace_bcount;
885 i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
888 if(chan->bprot == BPROT_NONE)
890 if(!(i4b_l1_bchan_tel_silence(chan->out_mbuf_cur->m_data, chan->out_mbuf_cur->m_len)))
900 avm_pnp_hscx_fifo(chan, sc);
903 /* call timeout handling routine */
905 if(activity == ACT_RX || activity == ACT_TX)
906 (*chan->isic_drvr_linktab->bch_activity)(chan->isic_drvr_linktab->unit, activity);
910 * this is the main routine which checks each channel and then calls
911 * the real interrupt routine as appropriate
914 avm_pnp_hscx_int_handler(struct l1_softc *sc)
919 stat = hscx_read_reg(0, HSCX_STAT, sc);
920 if (stat & HSCX_INT_RPR)
921 cnt = hscx_read_reg(0, HSCX_STAT+1, sc);
922 if (stat & HSCX_INT_MASK)
923 avm_pnp_hscx_intr(0, stat, cnt, sc);
926 stat = hscx_read_reg(1, HSCX_STAT, sc);
927 if (stat & HSCX_INT_RPR)
928 cnt = hscx_read_reg(1, HSCX_STAT+1, sc);
929 if (stat & HSCX_INT_MASK)
930 avm_pnp_hscx_intr(1, stat, cnt, sc);
934 avm_pnp_intr(void *xsc)
938 bus_space_handle_t bhandle;
939 bus_space_tag_t btag;
942 bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
943 btag = rman_get_bustag(sc->sc_resources.io_base[0]);
945 stat = bus_space_read_1(btag, bhandle, STAT0_OFFSET);
946 NDBGL1(L1_H_IRQ, "stat %x", stat);
947 /* was there an interrupt from this card ? */
948 if ((stat & ASL_IRQ_Pending) == ASL_IRQ_Pending)
950 /* interrupts are low active */
951 if (!(stat & ASL_IRQ_TIMER))
952 NDBGL1(L1_H_IRQ, "timer interrupt ???");
953 if (!(stat & ASL_IRQ_HSCX))
955 NDBGL1(L1_H_IRQ, "HSCX");
956 avm_pnp_hscx_int_handler(sc);
958 if (!(stat & ASL_IRQ_ISAC))
960 NDBGL1(L1_H_IRQ, "ISAC");
966 avm_pnp_hscx_init(struct l1_softc *sc, int h_chan, int activate)
968 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
970 NDBGL1(L1_BCHAN, "unit=%d, channel=%d, %s",
971 sc->sc_unit, h_chan, activate ? "activate" : "deactivate");
975 /* only deactivate if both channels are idle */
976 if (sc->sc_chan[HSCX_CH_A].state != HSCX_IDLE ||
977 sc->sc_chan[HSCX_CH_B].state != HSCX_IDLE)
981 sc->avma1pp_cmd = HSCX_CMD_XRS|HSCX_CMD_RRS;
982 sc->avma1pp_prot = HSCX_MODE_TRANS;
983 hscx_write_reg(h_chan, HSCX_STAT, sc, 5);
986 if(chan->bprot == BPROT_RHDLC)
988 NDBGL1(L1_BCHAN, "BPROT_RHDLC");
990 /* HDLC Frames, transparent mode 0 */
991 sc->avma1pp_cmd = HSCX_CMD_XRS|HSCX_CMD_RRS;
992 sc->avma1pp_prot = HSCX_MODE_ITF_FLG;
993 hscx_write_reg(h_chan, HSCX_STAT, sc, 5);
994 sc->avma1pp_cmd = HSCX_CMD_XRS;
995 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
1000 NDBGL1(L1_BCHAN, "BPROT_NONE??");
1002 /* Raw Telephony, extended transparent mode 1 */
1003 sc->avma1pp_cmd = HSCX_CMD_XRS|HSCX_CMD_RRS;
1004 sc->avma1pp_prot = HSCX_MODE_TRANS;
1005 hscx_write_reg(h_chan, HSCX_STAT, sc, 5);
1006 sc->avma1pp_cmd = HSCX_CMD_XRS;
1007 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
1008 sc->avma1pp_cmd = 0;
1013 avm_pnp_bchannel_setup(int unit, int h_chan, int bprot, int activate)
1015 struct l1_softc *sc = ifpnp_scp[unit];
1016 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1023 chan->state = HSCX_IDLE;
1024 avm_pnp_hscx_init(sc, h_chan, activate);
1027 NDBGL1(L1_BCHAN, "unit=%d, channel=%d, %s",
1028 sc->sc_unit, h_chan, activate ? "activate" : "deactivate");
1032 chan->unit = sc->sc_unit; /* unit number */
1033 chan->channel = h_chan; /* B channel */
1034 chan->bprot = bprot; /* B channel protocol */
1035 chan->state = HSCX_IDLE; /* B channel state */
1039 chan->rx_queue.ifq_maxlen = IFQ_MAXLEN;
1041 i4b_Bcleanifq(&chan->rx_queue); /* clean rx queue */
1043 chan->rxcount = 0; /* reset rx counter */
1045 i4b_Bfreembuf(chan->in_mbuf); /* clean rx mbuf */
1047 chan->in_mbuf = NULL; /* reset mbuf ptr */
1048 chan->in_cbptr = NULL; /* reset mbuf curr ptr */
1049 chan->in_len = 0; /* reset mbuf data len */
1051 /* transmitter part */
1053 chan->tx_queue.ifq_maxlen = IFQ_MAXLEN;
1055 i4b_Bcleanifq(&chan->tx_queue); /* clean tx queue */
1057 chan->txcount = 0; /* reset tx counter */
1059 i4b_Bfreembuf(chan->out_mbuf_head); /* clean tx mbuf */
1061 chan->out_mbuf_head = NULL; /* reset head mbuf ptr */
1062 chan->out_mbuf_cur = NULL; /* reset current mbuf ptr */
1063 chan->out_mbuf_cur_ptr = NULL; /* reset current mbuf data ptr */
1064 chan->out_mbuf_cur_len = 0; /* reset current mbuf data cnt */
1069 avm_pnp_hscx_init(sc, h_chan, activate);
1070 chan->state |= HSCX_AVMA1PP_ACTIVE;
1077 avm_pnp_bchannel_start(int unit, int h_chan)
1079 struct l1_softc *sc = ifpnp_scp[unit];
1080 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1084 if(chan->state & HSCX_TX_ACTIVE) /* already running ? */
1087 return; /* yes, leave */
1090 /* get next mbuf from queue */
1092 IF_DEQUEUE(&chan->tx_queue, chan->out_mbuf_head);
1094 if(chan->out_mbuf_head == NULL) /* queue empty ? */
1097 return; /* yes, exit */
1100 /* init current mbuf values */
1102 chan->out_mbuf_cur = chan->out_mbuf_head;
1103 chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
1104 chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
1106 /* activity indicator for timeout handling */
1108 if(chan->bprot == BPROT_NONE)
1110 if(!(i4b_l1_bchan_tel_silence(chan->out_mbuf_cur->m_data, chan->out_mbuf_cur->m_len)))
1118 chan->state |= HSCX_TX_ACTIVE; /* we start transmitting */
1120 if(sc->sc_trace & TRACE_B_TX) /* if trace, send mbuf to trace dev */
1122 i4b_trace_hdr_t hdr;
1123 hdr.unit = L0IFPNPUNIT(sc->sc_unit);
1124 hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
1126 hdr.count = ++sc->sc_trace_bcount;
1127 MICROTIME(hdr.time);
1128 i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
1131 avm_pnp_hscx_fifo(chan, sc);
1133 /* call timeout handling routine */
1135 if(activity == ACT_RX || activity == ACT_TX)
1136 (*chan->isic_drvr_linktab->bch_activity)(chan->isic_drvr_linktab->unit, activity);
1141 /*---------------------------------------------------------------------------*
1142 * return the address of isic drivers linktab
1143 *---------------------------------------------------------------------------*/
1144 static isdn_link_t *
1145 avm_pnp_ret_linktab(int unit, int channel)
1147 struct l1_softc *sc = ifpnp_scp[unit];
1148 l1_bchan_state_t *chan = &sc->sc_chan[channel];
1150 return(&chan->isic_isdn_linktab);
1153 /*---------------------------------------------------------------------------*
1154 * set the driver linktab in the b channel softc
1155 *---------------------------------------------------------------------------*/
1157 avm_pnp_set_linktab(int unit, int channel, drvr_link_t *dlt)
1159 struct l1_softc *sc = ifpnp_scp[unit];
1160 l1_bchan_state_t *chan = &sc->sc_chan[channel];
1162 chan->isic_drvr_linktab = dlt;
1166 /*---------------------------------------------------------------------------*
1167 * initialize our local linktab
1168 *---------------------------------------------------------------------------*/
1170 avm_pnp_init_linktab(struct l1_softc *sc)
1172 l1_bchan_state_t *chan = &sc->sc_chan[HSCX_CH_A];
1173 isdn_link_t *lt = &chan->isic_isdn_linktab;
1175 /* make sure the hardware driver is known to layer 4 */
1176 /* avoid overwriting if already set */
1177 if (ctrl_types[CTRL_PASSIVE].set_linktab == NULL)
1179 ctrl_types[CTRL_PASSIVE].set_linktab = avm_pnp_set_linktab;
1180 ctrl_types[CTRL_PASSIVE].get_linktab = avm_pnp_ret_linktab;
1184 lt->unit = sc->sc_unit;
1185 lt->channel = HSCX_CH_A;
1186 lt->bch_config = avm_pnp_bchannel_setup;
1187 lt->bch_tx_start = avm_pnp_bchannel_start;
1188 lt->bch_stat = avm_pnp_bchannel_stat;
1189 lt->tx_queue = &chan->tx_queue;
1191 /* used by non-HDLC data transfers, i.e. telephony drivers */
1192 lt->rx_queue = &chan->rx_queue;
1194 /* used by HDLC data transfers, i.e. ipr and isp drivers */
1195 lt->rx_mbuf = &chan->in_mbuf;
1197 chan = &sc->sc_chan[HSCX_CH_B];
1198 lt = &chan->isic_isdn_linktab;
1200 lt->unit = sc->sc_unit;
1201 lt->channel = HSCX_CH_B;
1202 lt->bch_config = avm_pnp_bchannel_setup;
1203 lt->bch_tx_start = avm_pnp_bchannel_start;
1204 lt->bch_stat = avm_pnp_bchannel_stat;
1205 lt->tx_queue = &chan->tx_queue;
1207 /* used by non-HDLC data transfers, i.e. telephony drivers */
1208 lt->rx_queue = &chan->rx_queue;
1210 /* used by HDLC data transfers, i.e. ipr and isp drivers */
1211 lt->rx_mbuf = &chan->in_mbuf;
1215 * use this instead of isic_bchannel_stat in i4b_bchan.c because it's static
1218 avm_pnp_bchannel_stat(int unit, int h_chan, bchan_statistics_t *bsp)
1220 struct l1_softc *sc = ifpnp_scp[unit];
1221 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1225 bsp->outbytes = chan->txcount;
1226 bsp->inbytes = chan->rxcount;
1234 /*---------------------------------------------------------------------------*
1235 * fill HSCX fifo with data from the current mbuf
1236 * Put this here until it can go into i4b_hscx.c
1237 *---------------------------------------------------------------------------*/
1239 avm_pnp_hscx_fifo(l1_bchan_state_t *chan, struct l1_softc *sc)
1245 /* using a scratch buffer simplifies writing to the FIFO */
1246 u_char scrbuf[HSCX_FIFO_LEN];
1251 * fill the HSCX tx fifo with data from the current mbuf. if
1252 * current mbuf holds less data than HSCX fifo length, try to
1253 * get the next mbuf from (a possible) mbuf chain. if there is
1254 * not enough data in a single mbuf or in a chain, then this
1255 * is the last mbuf and we tell the HSCX that it has to send
1256 * CRC and closing flag
1259 while(chan->out_mbuf_cur && len != sc->sc_bfifolen)
1261 nextlen = min(chan->out_mbuf_cur_len, sc->sc_bfifolen - len);
1264 kprintf("i:mh=%p, mc=%p, mcp=%p, mcl=%d l=%d nl=%d # ",
1265 chan->out_mbuf_head,
1267 chan->out_mbuf_cur_ptr,
1268 chan->out_mbuf_cur_len,
1273 cmd |= HSCX_CMDR_XTF;
1274 /* collect the data in the scratch buffer */
1275 for (i = 0; i < nextlen; i++)
1276 scrbuf[i + len] = chan->out_mbuf_cur_ptr[i];
1279 chan->txcount += nextlen;
1281 chan->out_mbuf_cur_ptr += nextlen;
1282 chan->out_mbuf_cur_len -= nextlen;
1284 if(chan->out_mbuf_cur_len == 0)
1286 if((chan->out_mbuf_cur = chan->out_mbuf_cur->m_next) != NULL)
1288 chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
1289 chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
1291 if(sc->sc_trace & TRACE_B_TX)
1293 i4b_trace_hdr_t hdr;
1294 hdr.unit = L0IFPNPUNIT(sc->sc_unit);
1295 hdr.type = (chan->channel == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
1297 hdr.count = ++sc->sc_trace_bcount;
1298 MICROTIME(hdr.time);
1299 i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
1304 if (chan->bprot != BPROT_NONE)
1305 cmd |= HSCX_CMDR_XME;
1306 i4b_Bfreembuf(chan->out_mbuf_head);
1307 chan->out_mbuf_head = NULL;
1311 /* write what we have from the scratch buf to the HSCX fifo */
1313 HSCX_WRFIFO(chan->channel, scrbuf, len);
1317 /*---------------------------------------------------------------------------*
1318 * ifpnp - ISAC interrupt routine
1319 *---------------------------------------------------------------------------*/
1321 ifpnp_isac_intr(struct l1_softc *sc)
1323 u_char isac_irq_stat;
1327 /* get isac irq status */
1328 isac_irq_stat = ISAC_READ(I_ISTA);
1331 ifpnp_isac_irq(sc, isac_irq_stat); /* isac handler */
1336 ISAC_WRITE(I_MASK, 0xff);
1340 ISAC_WRITE(I_MASK, ISAC_IMASK);
1343 /*---------------------------------------------------------------------------*
1344 * ifpnp_recover - try to recover from irq lockup
1345 *---------------------------------------------------------------------------*/
1347 ifpnp_recover(struct l1_softc *sc)
1351 /* get isac irq status */
1353 byte = ISAC_READ(I_ISTA);
1355 NDBGL1(L1_ERROR, " ISAC: ISTA = 0x%x", byte);
1357 if(byte & ISAC_ISTA_EXI)
1358 NDBGL1(L1_ERROR, " ISAC: EXIR = 0x%x", (u_char)ISAC_READ(I_EXIR));
1360 if(byte & ISAC_ISTA_CISQ)
1362 byte = ISAC_READ(I_CIRR);
1364 NDBGL1(L1_ERROR, " ISAC: CISQ = 0x%x", byte);
1366 if(byte & ISAC_CIRR_SQC)
1367 NDBGL1(L1_ERROR, " ISAC: SQRR = 0x%x", (u_char)ISAC_READ(I_SQRR));
1370 NDBGL1(L1_ERROR, " ISAC: IMASK = 0x%x", ISAC_IMASK);
1372 ISAC_WRITE(I_MASK, 0xff);
1374 ISAC_WRITE(I_MASK, ISAC_IMASK);
1377 #endif /* NIFPNP > 0 */