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
32 /*---------------------------------------------------------------------------
34 * i4b_ifpnp_avm.c: AVM Fritz!Card PnP hardware driver
35 * ---------------------------------------------------
36 * last edit-date: [Fri Jan 12 17:05:28 2001]
38 *---------------------------------------------------------------------------*/
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/systm.h>
50 #include <machine/bus.h>
54 #include <isa/isavar.h>
56 #include <sys/socket.h>
59 #include <i4b/include/i4b_debug.h>
60 #include <i4b/include/i4b_ioctl.h>
61 #include <i4b/include/i4b_trace.h>
63 #include <i4b/include/i4b_global.h>
64 #include <i4b/include/i4b_mbuf.h>
66 #include <i4b/layer1/i4b_l1.h>
67 #include <i4b/layer1/isic/i4b_isic.h>
68 #include <i4b/layer1/isic/i4b_isac.h>
69 #include <i4b/layer1/isic/i4b_hscx.h>
71 #include <i4b/layer1/ifpnp/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, 0, 0);
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 *---------------------------------------------------------------------------*/
425 struct ifpnp_ids *ids; /* pnp id's */
426 char *string = NULL; /* the name */
427 u_int32_t vend_id = isa_get_vendorid(dev); /* vendor id */
429 /* search table of knowd id's */
431 for(ids = ifpnp_ids; ids->vend_id != 0; ids++)
433 if(vend_id == ids->vend_id)
435 string = ids->id_str;
440 if(string) /* set name if we have one */
442 device_set_desc(dev, string); /* set description */
451 /*---------------------------------------------------------------------------*
452 * avm_pnp_attach - attach Fritz!Card PnP
453 *---------------------------------------------------------------------------*/
455 avm_pnp_attach(device_t dev)
463 bus_space_handle_t bhandle;
464 bus_space_tag_t btag;
468 vid = isa_get_vendorid(dev);
469 sc = device_get_softc(dev);
470 unit = device_get_unit(dev);
471 bzero(sc, sizeof(struct l1_softc));
473 /* probably not really required */
474 if(unit >= IFPNP_MAXUNIT) {
475 printf("avm_pnp%d: Error, unit >= IFPNP_MAXUNIT!\n", unit);
480 ifpnp_scp[unit] = sc;
484 if(!(sc->sc_resources.io_base[0] =
485 bus_alloc_resource_any(dev, SYS_RES_IOPORT,
486 &sc->sc_resources.io_rid[0],
489 printf("avm_pnp_attach: Couldn't get my io_base.\n");
492 if (sc->sc_resources.io_base[0] == NULL) {
493 printf("avm_pnp%d: couldn't map IO port\n", unit);
498 bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
499 btag = rman_get_bustag(sc->sc_resources.io_base[0]);
501 /* will not be used for pnp devices */
502 sc->sc_port = rman_get_start(sc->sc_resources.io_base[0]);
504 /* get irq, release io_base if we don't get it */
506 if(!(sc->sc_resources.irq =
507 bus_alloc_resource_any(dev, SYS_RES_IRQ,
508 &sc->sc_resources.irq_rid,
511 printf("avm_pnp%d: Could not get irq.\n",unit);
517 sc->sc_irq = rman_get_start(sc->sc_resources.irq);
518 error = bus_setup_intr(dev,sc->sc_resources.irq,INTR_TYPE_NET,
519 NULL, (void(*)(void*))avm_pnp_intr, sc,&ih);
522 printf("avm_pnp%d: Could not setup irq.\n",unit);
528 /* end of new-bus stuff */
530 ISAC_BASE = (caddr_t)ISIC_WHAT_ISAC;
532 HSCX_A_BASE = (caddr_t)ISIC_WHAT_HSCXA;
533 HSCX_B_BASE = (caddr_t)ISIC_WHAT_HSCXB;
535 /* setup access routines */
538 sc->readreg = avm_pnp_read_reg;
539 sc->writereg = avm_pnp_write_reg;
541 sc->readfifo = avm_pnp_read_fifo;
542 sc->writefifo = avm_pnp_write_fifo;
544 /* setup card type */
546 sc->sc_cardtyp = CARD_TYPEP_AVM_PNP;
548 /* setup IOM bus type */
550 sc->sc_bustyp = BUS_TYPE_IOM2;
552 /* set up some other miscellaneous things */
554 sc->sc_bfifolen = HSCX_FIFO_LEN;
557 /* the Linux driver does this to clear any pending ISAC interrupts */
559 v = ISAC_READ(I_STAR);
560 v = ISAC_READ(I_MODE);
561 v = ISAC_READ(I_ADF2);
562 v = ISAC_READ(I_ISTA);
563 if (v & ISAC_ISTA_EXI)
565 v = ISAC_READ(I_EXIR);
567 v = ISAC_READ(I_CIRR);
568 ISAC_WRITE(I_MASK, 0xff);
569 /* the Linux driver does this to clear any pending HSCX interrupts */
570 v = hscx_read_reg(0, HSCX_STAT, sc);
571 v = hscx_read_reg(1, HSCX_STAT, sc);
573 bus_space_write_1(btag, bhandle, STAT0_OFFSET, ASL_RESET_ALL|ASL_TIMERDISABLE);
574 DELAY(SEC_DELAY/100); /* 10 ms */
575 bus_space_write_1(btag, bhandle, STAT1_OFFSET, ASL1_ENABLE_IOM|sc->sc_irq);
576 DELAY(SEC_DELAY/100); /* 10 ms */
577 bus_space_write_1(btag, bhandle, STAT0_OFFSET, ASL_TIMERRESET|ASL_ENABLE_INT|ASL_TIMERDISABLE);
578 DELAY(SEC_DELAY/100); /* 10 ms */
580 printf("ifpnp%d: AVM Fritz!Card PnP Class %#x Revision %d \n", unit,
581 bus_space_read_1(btag, bhandle, CLASS_OFFSET),
582 bus_space_read_1(btag, bhandle, REVISION_OFFSET));
584 printf("ifpnp%d: ISAC %s (IOM-%c)\n", unit,
585 "2085 Version A1/A2 or 2086/2186 Version 1.1",
586 sc->sc_bustyp == BUS_TYPE_IOM1 ? '1' : '2');
592 /* init the "HSCX" */
593 avm_pnp_bchannel_setup(sc->sc_unit, HSCX_CH_A, BPROT_NONE, 0);
595 avm_pnp_bchannel_setup(sc->sc_unit, HSCX_CH_B, BPROT_NONE, 0);
597 /* can't use the normal B-Channel stuff */
598 avm_pnp_init_linktab(sc);
600 /* set trace level */
602 sc->sc_trace = TRACE_OFF;
604 sc->sc_state = ISAC_IDLE;
616 sc->sc_freeflag2 = 0;
618 callout_handle_init(&sc->sc_T3_callout);
619 callout_handle_init(&sc->sc_T4_callout);
621 /* init higher protocol layers */
623 i4b_l1_mph_status_ind(L0IFPNPUNIT(sc->sc_unit), STI_ATTACH, sc->sc_cardtyp, &avm_pnp_l1mux_func);
631 * this is the real interrupt routine
634 avm_pnp_hscx_intr(int h_chan, u_int stat, u_int cnt, struct l1_softc *sc)
636 register l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
639 NDBGL1(L1_H_IRQ, "%#x", stat);
641 if((stat & HSCX_INT_XDU) && (chan->bprot != BPROT_NONE))/* xmit data underrun */
644 NDBGL1(L1_H_XFRERR, "xmit data underrun");
645 /* abort the transmission */
647 sc->avma1pp_cmd |= HSCX_CMD_XRS;
648 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
649 sc->avma1pp_cmd &= ~HSCX_CMD_XRS;
650 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
652 if (chan->out_mbuf_head != NULL) /* don't continue to transmit this buffer */
654 i4b_Bfreembuf(chan->out_mbuf_head);
655 chan->out_mbuf_cur = chan->out_mbuf_head = NULL;
660 * The following is based on examination of the Linux driver.
662 * The logic here is different than with a "real" HSCX; all kinds
663 * of information (interrupt/status bits) are in stat.
664 * HSCX_INT_RPR indicates a receive interrupt
665 * HSCX_STAT_RDO indicates an overrun condition, abort -
666 * otherwise read the bytes ((stat & HSCX_STZT_RML_MASK) >> 8)
667 * HSCX_STAT_RME indicates end-of-frame and apparently any
668 * CRC/framing errors are only reported in this state.
669 * if ((stat & HSCX_STAT_CRCVFRRAB) != HSCX_STAT_CRCVFR)
673 if(stat & HSCX_INT_RPR)
675 register int fifo_data_len;
677 /* always have to read the FIFO, so use a scratch buffer */
678 u_char scrbuf[HSCX_FIFO_LEN];
680 if(stat & HSCX_STAT_RDO)
683 NDBGL1(L1_H_XFRERR, "receive data overflow");
688 * check whether we're receiving data for an inactive B-channel
689 * and discard it. This appears to happen for telephony when
690 * both B-channels are active and one is deactivated. Since
691 * it is not really possible to deactivate the channel in that
692 * case (the ASIC seems to deactivate _both_ channels), the
693 * "deactivated" channel keeps receiving data which can lead
694 * to exhaustion of mbufs and a kernel panic.
696 * This is a hack, but it's the only solution I can think of
697 * without having the documentation for the ASIC.
700 if (chan->state == HSCX_IDLE)
702 NDBGL1(L1_H_XFRERR, "toss data from %d", h_chan);
708 if(fifo_data_len == 0)
709 fifo_data_len = sc->sc_bfifolen;
711 /* ALWAYS read data from HSCX fifo */
713 HSCX_RDFIFO(h_chan, scrbuf, fifo_data_len);
714 chan->rxcount += fifo_data_len;
716 /* all error conditions checked, now decide and take action */
720 if(chan->in_mbuf == NULL)
722 if((chan->in_mbuf = i4b_Bgetmbuf(BCH_MAX_DATALEN)) == NULL)
723 panic("L1 avm_pnp_hscx_intr: RME, cannot allocate mbuf!\n");
724 chan->in_cbptr = chan->in_mbuf->m_data;
728 if((chan->in_len + fifo_data_len) <= BCH_MAX_DATALEN)
730 /* OK to copy the data */
731 bcopy(scrbuf, chan->in_cbptr, fifo_data_len);
732 chan->in_cbptr += fifo_data_len;
733 chan->in_len += fifo_data_len;
735 /* setup mbuf data length */
737 chan->in_mbuf->m_len = chan->in_len;
738 chan->in_mbuf->m_pkthdr.len = chan->in_len;
740 if(sc->sc_trace & TRACE_B_RX)
743 hdr.unit = L0IFPNPUNIT(sc->sc_unit);
744 hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
746 hdr.count = ++sc->sc_trace_bcount;
748 i4b_l1_trace_ind(&hdr, chan->in_mbuf->m_len, chan->in_mbuf->m_data);
751 if (stat & HSCX_STAT_RME)
753 if((stat & HSCX_STAT_CRCVFRRAB) == HSCX_STAT_CRCVFR)
755 (*chan->isic_drvr_linktab->bch_rx_data_ready)(chan->isic_drvr_linktab->unit);
758 /* mark buffer ptr as unused */
760 chan->in_mbuf = NULL;
761 chan->in_cbptr = NULL;
767 NDBGL1(L1_H_XFRERR, "CRC/RAB");
768 if (chan->in_mbuf != NULL)
770 i4b_Bfreembuf(chan->in_mbuf);
771 chan->in_mbuf = NULL;
772 chan->in_cbptr = NULL;
777 } /* END enough space in mbuf */
780 if(chan->bprot == BPROT_NONE)
782 /* setup mbuf data length */
784 chan->in_mbuf->m_len = chan->in_len;
785 chan->in_mbuf->m_pkthdr.len = chan->in_len;
787 if(sc->sc_trace & TRACE_B_RX)
790 hdr.unit = L0IFPNPUNIT(sc->sc_unit);
791 hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
793 hdr.count = ++sc->sc_trace_bcount;
795 i4b_l1_trace_ind(&hdr, chan->in_mbuf->m_len, chan->in_mbuf->m_data);
798 if(!(i4b_l1_bchan_tel_silence(chan->in_mbuf->m_data, chan->in_mbuf->m_len)))
801 /* move rx'd data to rx queue */
802 #if defined (__FreeBSD__) && __FreeBSD__ > 4
803 (void) IF_HANDOFF(&chan->rx_queue, chan->in_mbuf, NULL);
805 if(!(IF_QFULL(&chan->rx_queue)))
807 IF_ENQUEUE(&chan->rx_queue, chan->in_mbuf);
811 i4b_Bfreembuf(chan->in_mbuf);
814 /* signal upper layer that data are available */
815 (*chan->isic_drvr_linktab->bch_rx_data_ready)(chan->isic_drvr_linktab->unit);
817 /* alloc new buffer */
819 if((chan->in_mbuf = i4b_Bgetmbuf(BCH_MAX_DATALEN)) == NULL)
820 panic("L1 avm_pnp_hscx_intr: RPF, cannot allocate new mbuf!\n");
822 /* setup new data ptr */
824 chan->in_cbptr = chan->in_mbuf->m_data;
826 /* OK to copy the data */
827 bcopy(scrbuf, chan->in_cbptr, fifo_data_len);
829 chan->in_cbptr += fifo_data_len;
830 chan->in_len = fifo_data_len;
832 chan->rxcount += fifo_data_len;
836 NDBGL1(L1_H_XFRERR, "RAWHDLC rx buffer overflow in RPF, in_len=%d", chan->in_len);
837 chan->in_cbptr = chan->in_mbuf->m_data;
841 } /* if(error == 0) */
844 /* land here for RDO */
845 if (chan->in_mbuf != NULL)
847 i4b_Bfreembuf(chan->in_mbuf);
848 chan->in_mbuf = NULL;
849 chan->in_cbptr = NULL;
853 sc->avma1pp_cmd |= HSCX_CMD_RRS;
854 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
855 sc->avma1pp_cmd &= ~HSCX_CMD_RRS;
856 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
861 /* transmit fifo empty, new data can be written to fifo */
863 if(stat & HSCX_INT_XPR)
866 * for a description what is going on here, please have
867 * a look at isic_bchannel_start() in i4b_bchan.c !
870 NDBGL1(L1_H_IRQ, "unit %d, chan %d - XPR, Tx Fifo Empty!", sc->sc_unit, h_chan);
872 if(chan->out_mbuf_cur == NULL) /* last frame is transmitted */
874 IF_DEQUEUE(&chan->tx_queue, chan->out_mbuf_head);
876 if(chan->out_mbuf_head == NULL)
878 chan->state &= ~HSCX_TX_ACTIVE;
879 (*chan->isic_drvr_linktab->bch_tx_queue_empty)(chan->isic_drvr_linktab->unit);
883 chan->state |= HSCX_TX_ACTIVE;
884 chan->out_mbuf_cur = chan->out_mbuf_head;
885 chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
886 chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
888 if(sc->sc_trace & TRACE_B_TX)
891 hdr.unit = L0IFPNPUNIT(sc->sc_unit);
892 hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
894 hdr.count = ++sc->sc_trace_bcount;
896 i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
899 if(chan->bprot == BPROT_NONE)
901 if(!(i4b_l1_bchan_tel_silence(chan->out_mbuf_cur->m_data, chan->out_mbuf_cur->m_len)))
911 avm_pnp_hscx_fifo(chan, sc);
914 /* call timeout handling routine */
916 if(activity == ACT_RX || activity == ACT_TX)
917 (*chan->isic_drvr_linktab->bch_activity)(chan->isic_drvr_linktab->unit, activity);
921 * this is the main routine which checks each channel and then calls
922 * the real interrupt routine as appropriate
925 avm_pnp_hscx_int_handler(struct l1_softc *sc)
930 stat = hscx_read_reg(0, HSCX_STAT, sc);
931 if (stat & HSCX_INT_RPR)
932 cnt = hscx_read_reg(0, HSCX_STAT+1, sc);
933 if (stat & HSCX_INT_MASK)
934 avm_pnp_hscx_intr(0, stat, cnt, sc);
937 stat = hscx_read_reg(1, HSCX_STAT, sc);
938 if (stat & HSCX_INT_RPR)
939 cnt = hscx_read_reg(1, HSCX_STAT+1, sc);
940 if (stat & HSCX_INT_MASK)
941 avm_pnp_hscx_intr(1, stat, cnt, sc);
945 avm_pnp_intr(void *xsc)
949 bus_space_handle_t bhandle;
950 bus_space_tag_t btag;
953 bhandle = rman_get_bushandle(sc->sc_resources.io_base[0]);
954 btag = rman_get_bustag(sc->sc_resources.io_base[0]);
956 stat = bus_space_read_1(btag, bhandle, STAT0_OFFSET);
957 NDBGL1(L1_H_IRQ, "stat %x", stat);
958 /* was there an interrupt from this card ? */
959 if ((stat & ASL_IRQ_Pending) == ASL_IRQ_Pending)
961 /* interrupts are low active */
962 if (!(stat & ASL_IRQ_TIMER))
963 NDBGL1(L1_H_IRQ, "timer interrupt ???");
964 if (!(stat & ASL_IRQ_HSCX))
966 NDBGL1(L1_H_IRQ, "HSCX");
967 avm_pnp_hscx_int_handler(sc);
969 if (!(stat & ASL_IRQ_ISAC))
971 NDBGL1(L1_H_IRQ, "ISAC");
977 avm_pnp_hscx_init(struct l1_softc *sc, int h_chan, int activate)
979 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
981 NDBGL1(L1_BCHAN, "unit=%d, channel=%d, %s",
982 sc->sc_unit, h_chan, activate ? "activate" : "deactivate");
986 /* only deactivate if both channels are idle */
987 if (sc->sc_chan[HSCX_CH_A].state != HSCX_IDLE ||
988 sc->sc_chan[HSCX_CH_B].state != HSCX_IDLE)
992 sc->avma1pp_cmd = HSCX_CMD_XRS|HSCX_CMD_RRS;
993 sc->avma1pp_prot = HSCX_MODE_TRANS;
994 hscx_write_reg(h_chan, HSCX_STAT, sc, 5);
997 if(chan->bprot == BPROT_RHDLC)
999 NDBGL1(L1_BCHAN, "BPROT_RHDLC");
1001 /* HDLC Frames, transparent mode 0 */
1002 sc->avma1pp_cmd = HSCX_CMD_XRS|HSCX_CMD_RRS;
1003 sc->avma1pp_prot = HSCX_MODE_ITF_FLG;
1004 hscx_write_reg(h_chan, HSCX_STAT, sc, 5);
1005 sc->avma1pp_cmd = HSCX_CMD_XRS;
1006 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
1007 sc->avma1pp_cmd = 0;
1011 NDBGL1(L1_BCHAN, "BPROT_NONE??");
1013 /* Raw Telephony, extended transparent mode 1 */
1014 sc->avma1pp_cmd = HSCX_CMD_XRS|HSCX_CMD_RRS;
1015 sc->avma1pp_prot = HSCX_MODE_TRANS;
1016 hscx_write_reg(h_chan, HSCX_STAT, sc, 5);
1017 sc->avma1pp_cmd = HSCX_CMD_XRS;
1018 hscx_write_reg(h_chan, HSCX_STAT, sc, 1);
1019 sc->avma1pp_cmd = 0;
1024 avm_pnp_bchannel_setup(int unit, int h_chan, int bprot, int activate)
1026 struct l1_softc *sc = ifpnp_scp[unit];
1027 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1034 chan->state = HSCX_IDLE;
1035 avm_pnp_hscx_init(sc, h_chan, activate);
1038 NDBGL1(L1_BCHAN, "unit=%d, channel=%d, %s",
1039 sc->sc_unit, h_chan, activate ? "activate" : "deactivate");
1043 chan->unit = sc->sc_unit; /* unit number */
1044 chan->channel = h_chan; /* B channel */
1045 chan->bprot = bprot; /* B channel protocol */
1046 chan->state = HSCX_IDLE; /* B channel state */
1050 chan->rx_queue.ifq_maxlen = IFQ_MAXLEN;
1052 #if defined (__FreeBSD__) && __FreeBSD__ > 4
1053 if(!mtx_initialized(&chan->rx_queue.ifq_mtx))
1054 mtx_init(&chan->rx_queue.ifq_mtx, "i4b_avm_pnp_rx", NULL, MTX_DEF);
1057 i4b_Bcleanifq(&chan->rx_queue); /* clean rx queue */
1059 chan->rxcount = 0; /* reset rx counter */
1061 i4b_Bfreembuf(chan->in_mbuf); /* clean rx mbuf */
1063 chan->in_mbuf = NULL; /* reset mbuf ptr */
1064 chan->in_cbptr = NULL; /* reset mbuf curr ptr */
1065 chan->in_len = 0; /* reset mbuf data len */
1067 /* transmitter part */
1069 chan->tx_queue.ifq_maxlen = IFQ_MAXLEN;
1071 #if defined (__FreeBSD__) && __FreeBSD__ > 4
1072 if(!mtx_initialized(&chan->tx_queue.ifq_mtx))
1073 mtx_init(&chan->tx_queue.ifq_mtx, "i4b_avm_pnp_tx", NULL, MTX_DEF);
1075 i4b_Bcleanifq(&chan->tx_queue); /* clean tx queue */
1077 chan->txcount = 0; /* reset tx counter */
1079 i4b_Bfreembuf(chan->out_mbuf_head); /* clean tx mbuf */
1081 chan->out_mbuf_head = NULL; /* reset head mbuf ptr */
1082 chan->out_mbuf_cur = NULL; /* reset current mbuf ptr */
1083 chan->out_mbuf_cur_ptr = NULL; /* reset current mbuf data ptr */
1084 chan->out_mbuf_cur_len = 0; /* reset current mbuf data cnt */
1089 avm_pnp_hscx_init(sc, h_chan, activate);
1090 chan->state |= HSCX_AVMA1PP_ACTIVE;
1097 avm_pnp_bchannel_start(int unit, int h_chan)
1099 struct l1_softc *sc = ifpnp_scp[unit];
1100 register l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1104 s = SPLI4B(); /* enter critical section */
1105 if(chan->state & HSCX_TX_ACTIVE) /* already running ? */
1108 return; /* yes, leave */
1111 /* get next mbuf from queue */
1113 IF_DEQUEUE(&chan->tx_queue, chan->out_mbuf_head);
1115 if(chan->out_mbuf_head == NULL) /* queue empty ? */
1117 splx(s); /* leave critical section */
1118 return; /* yes, exit */
1121 /* init current mbuf values */
1123 chan->out_mbuf_cur = chan->out_mbuf_head;
1124 chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
1125 chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
1127 /* activity indicator for timeout handling */
1129 if(chan->bprot == BPROT_NONE)
1131 if(!(i4b_l1_bchan_tel_silence(chan->out_mbuf_cur->m_data, chan->out_mbuf_cur->m_len)))
1139 chan->state |= HSCX_TX_ACTIVE; /* we start transmitting */
1141 if(sc->sc_trace & TRACE_B_TX) /* if trace, send mbuf to trace dev */
1143 i4b_trace_hdr_t hdr;
1144 hdr.unit = L0IFPNPUNIT(sc->sc_unit);
1145 hdr.type = (h_chan == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
1147 hdr.count = ++sc->sc_trace_bcount;
1148 MICROTIME(hdr.time);
1149 i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
1152 avm_pnp_hscx_fifo(chan, sc);
1154 /* call timeout handling routine */
1156 if(activity == ACT_RX || activity == ACT_TX)
1157 (*chan->isic_drvr_linktab->bch_activity)(chan->isic_drvr_linktab->unit, activity);
1162 /*---------------------------------------------------------------------------*
1163 * return the address of isic drivers linktab
1164 *---------------------------------------------------------------------------*/
1165 static isdn_link_t *
1166 avm_pnp_ret_linktab(int unit, int channel)
1168 struct l1_softc *sc = ifpnp_scp[unit];
1169 l1_bchan_state_t *chan = &sc->sc_chan[channel];
1171 return(&chan->isic_isdn_linktab);
1174 /*---------------------------------------------------------------------------*
1175 * set the driver linktab in the b channel softc
1176 *---------------------------------------------------------------------------*/
1178 avm_pnp_set_linktab(int unit, int channel, drvr_link_t *dlt)
1180 struct l1_softc *sc = ifpnp_scp[unit];
1181 l1_bchan_state_t *chan = &sc->sc_chan[channel];
1183 chan->isic_drvr_linktab = dlt;
1187 /*---------------------------------------------------------------------------*
1188 * initialize our local linktab
1189 *---------------------------------------------------------------------------*/
1191 avm_pnp_init_linktab(struct l1_softc *sc)
1193 l1_bchan_state_t *chan = &sc->sc_chan[HSCX_CH_A];
1194 isdn_link_t *lt = &chan->isic_isdn_linktab;
1196 /* make sure the hardware driver is known to layer 4 */
1197 /* avoid overwriting if already set */
1198 if (ctrl_types[CTRL_PASSIVE].set_linktab == NULL)
1200 ctrl_types[CTRL_PASSIVE].set_linktab = avm_pnp_set_linktab;
1201 ctrl_types[CTRL_PASSIVE].get_linktab = avm_pnp_ret_linktab;
1205 lt->unit = sc->sc_unit;
1206 lt->channel = HSCX_CH_A;
1207 lt->bch_config = avm_pnp_bchannel_setup;
1208 lt->bch_tx_start = avm_pnp_bchannel_start;
1209 lt->bch_stat = avm_pnp_bchannel_stat;
1210 lt->tx_queue = &chan->tx_queue;
1212 /* used by non-HDLC data transfers, i.e. telephony drivers */
1213 lt->rx_queue = &chan->rx_queue;
1215 /* used by HDLC data transfers, i.e. ipr and isp drivers */
1216 lt->rx_mbuf = &chan->in_mbuf;
1218 chan = &sc->sc_chan[HSCX_CH_B];
1219 lt = &chan->isic_isdn_linktab;
1221 lt->unit = sc->sc_unit;
1222 lt->channel = HSCX_CH_B;
1223 lt->bch_config = avm_pnp_bchannel_setup;
1224 lt->bch_tx_start = avm_pnp_bchannel_start;
1225 lt->bch_stat = avm_pnp_bchannel_stat;
1226 lt->tx_queue = &chan->tx_queue;
1228 /* used by non-HDLC data transfers, i.e. telephony drivers */
1229 lt->rx_queue = &chan->rx_queue;
1231 /* used by HDLC data transfers, i.e. ipr and isp drivers */
1232 lt->rx_mbuf = &chan->in_mbuf;
1236 * use this instead of isic_bchannel_stat in i4b_bchan.c because it's static
1239 avm_pnp_bchannel_stat(int unit, int h_chan, bchan_statistics_t *bsp)
1241 struct l1_softc *sc = ifpnp_scp[unit];
1242 l1_bchan_state_t *chan = &sc->sc_chan[h_chan];
1247 bsp->outbytes = chan->txcount;
1248 bsp->inbytes = chan->rxcount;
1256 /*---------------------------------------------------------------------------*
1257 * fill HSCX fifo with data from the current mbuf
1258 * Put this here until it can go into i4b_hscx.c
1259 *---------------------------------------------------------------------------*/
1261 avm_pnp_hscx_fifo(l1_bchan_state_t *chan, struct l1_softc *sc)
1267 /* using a scratch buffer simplifies writing to the FIFO */
1268 u_char scrbuf[HSCX_FIFO_LEN];
1273 * fill the HSCX tx fifo with data from the current mbuf. if
1274 * current mbuf holds less data than HSCX fifo length, try to
1275 * get the next mbuf from (a possible) mbuf chain. if there is
1276 * not enough data in a single mbuf or in a chain, then this
1277 * is the last mbuf and we tell the HSCX that it has to send
1278 * CRC and closing flag
1281 while(chan->out_mbuf_cur && len != sc->sc_bfifolen)
1283 nextlen = min(chan->out_mbuf_cur_len, sc->sc_bfifolen - len);
1286 printf("i:mh=%p, mc=%p, mcp=%p, mcl=%d l=%d nl=%d # ",
1287 chan->out_mbuf_head,
1289 chan->out_mbuf_cur_ptr,
1290 chan->out_mbuf_cur_len,
1295 cmd |= HSCX_CMDR_XTF;
1296 /* collect the data in the scratch buffer */
1297 for (i = 0; i < nextlen; i++)
1298 scrbuf[i + len] = chan->out_mbuf_cur_ptr[i];
1301 chan->txcount += nextlen;
1303 chan->out_mbuf_cur_ptr += nextlen;
1304 chan->out_mbuf_cur_len -= nextlen;
1306 if(chan->out_mbuf_cur_len == 0)
1308 if((chan->out_mbuf_cur = chan->out_mbuf_cur->m_next) != NULL)
1310 chan->out_mbuf_cur_ptr = chan->out_mbuf_cur->m_data;
1311 chan->out_mbuf_cur_len = chan->out_mbuf_cur->m_len;
1313 if(sc->sc_trace & TRACE_B_TX)
1315 i4b_trace_hdr_t hdr;
1316 hdr.unit = L0IFPNPUNIT(sc->sc_unit);
1317 hdr.type = (chan->channel == HSCX_CH_A ? TRC_CH_B1 : TRC_CH_B2);
1319 hdr.count = ++sc->sc_trace_bcount;
1320 MICROTIME(hdr.time);
1321 i4b_l1_trace_ind(&hdr, chan->out_mbuf_cur->m_len, chan->out_mbuf_cur->m_data);
1326 if (chan->bprot != BPROT_NONE)
1327 cmd |= HSCX_CMDR_XME;
1328 i4b_Bfreembuf(chan->out_mbuf_head);
1329 chan->out_mbuf_head = NULL;
1333 /* write what we have from the scratch buf to the HSCX fifo */
1335 HSCX_WRFIFO(chan->channel, scrbuf, len);
1339 /*---------------------------------------------------------------------------*
1340 * ifpnp - ISAC interrupt routine
1341 *---------------------------------------------------------------------------*/
1343 ifpnp_isac_intr(struct l1_softc *sc)
1345 register u_char isac_irq_stat;
1349 /* get isac irq status */
1350 isac_irq_stat = ISAC_READ(I_ISTA);
1353 ifpnp_isac_irq(sc, isac_irq_stat); /* isac handler */
1358 ISAC_WRITE(I_MASK, 0xff);
1362 ISAC_WRITE(I_MASK, ISAC_IMASK);
1365 /*---------------------------------------------------------------------------*
1366 * ifpnp_recover - try to recover from irq lockup
1367 *---------------------------------------------------------------------------*/
1369 ifpnp_recover(struct l1_softc *sc)
1373 /* get isac irq status */
1375 byte = ISAC_READ(I_ISTA);
1377 NDBGL1(L1_ERROR, " ISAC: ISTA = 0x%x", byte);
1379 if(byte & ISAC_ISTA_EXI)
1380 NDBGL1(L1_ERROR, " ISAC: EXIR = 0x%x", (u_char)ISAC_READ(I_EXIR));
1382 if(byte & ISAC_ISTA_CISQ)
1384 byte = ISAC_READ(I_CIRR);
1386 NDBGL1(L1_ERROR, " ISAC: CISQ = 0x%x", byte);
1388 if(byte & ISAC_CIRR_SQC)
1389 NDBGL1(L1_ERROR, " ISAC: SQRR = 0x%x", (u_char)ISAC_READ(I_SQRR));
1392 NDBGL1(L1_ERROR, " ISAC: IMASK = 0x%x", ISAC_IMASK);
1394 ISAC_WRITE(I_MASK, 0xff);
1396 ISAC_WRITE(I_MASK, ISAC_IMASK);