2 * Copyright (c) 2001 Cubical Solutions Ltd. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
25 * capi/iavc/iavc_lli.c
26 * The AVM ISDN controllers' Low Level Interface.
28 * $FreeBSD: src/sys/i4b/capi/iavc/iavc_lli.c,v 1.2.2.1 2001/08/10 14:08:34 obrien Exp $
29 * $DragonFly: src/sys/net/i4b/capi/iavc/iavc_lli.c,v 1.3 2003/08/07 21:17:24 dillon Exp $
33 #include "use_i4bcapi.h"
36 #if (NIAVC > 0) && (NI4BCAPI > 0)
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
42 #include <sys/socket.h>
43 #include <sys/malloc.h>
46 #include <machine/clock.h>
48 #include <machine/bus.h>
54 #include <machine/i4b_debug.h>
55 #include <machine/i4b_ioctl.h>
56 #include <machine/i4b_trace.h>
58 #include "../../include/i4b_global.h"
59 #include "../../include/i4b_l3l4.h"
60 #include "../../include/i4b_mbuf.h"
62 #include "../capi_msgs.h"
66 /* Forward declarations of local subroutines... */
68 static int iavc_send_init(iavc_softc_t *);
70 static void iavc_handle_rx(iavc_softc_t *);
71 static void iavc_start_tx(iavc_softc_t *);
74 // Callbacks from the upper (capi) layer:
75 // --------------------------------------
78 // Resets the board and loads the firmware, then initiates
82 // Registers a CAPI application id.
85 // Releases a CAPI application id.
88 // Sends a capi message.
91 int iavc_load(capi_softc_t *capi_sc, int len, u_int8_t *cp)
93 iavc_softc_t *sc = (iavc_softc_t*) capi_sc->ctx;
97 printf("iavc%d: reset card ....\n", sc->sc_unit);
100 b1dma_reset(sc); /* PCI cards */
102 t1_reset(sc); /* ISA attachment T1 */
104 b1_reset(sc); /* ISA attachment B1 */
109 printf("iavc%d: start loading %d bytes firmware ....\n", sc->sc_unit, len);
111 while (len && b1io_save_put_byte(sc, *cp++) == 0)
115 printf("iavc%d: loading failed, can't write to card, len = %d\n",
121 printf("iavc%d: firmware loaded, wait for ACK ....\n", sc->sc_unit);
123 if(sc->sc_capi.card_type == CARD_TYPEC_AVM_B1_ISA)
124 iavc_put_byte(sc, SEND_POLL);
126 iavc_put_byte(sc, SEND_POLLACK);
128 for (len = 0; len < 1000 && !iavc_rx_full(sc); len++)
131 if (!iavc_rx_full(sc)) {
132 printf("iavc%d: loading failed, no ack\n", sc->sc_unit);
136 val = iavc_get_byte(sc);
138 if ((sc->sc_dma && val != RECEIVE_POLLDWORD) ||
139 (!sc->sc_dma && val != RECEIVE_POLL)) {
140 printf("iavc%d: loading failed, bad ack = %02x\n", sc->sc_unit, val);
145 printf("iavc%d: got ACK = 0x%02x\n", sc->sc_unit, val);
148 /* Start the DMA engine */
152 sc->sc_csr = AVM_FLAG;
153 AMCC_WRITE(sc, AMCC_INTCSR, sc->sc_csr);
154 AMCC_WRITE(sc, AMCC_MCSR, (EN_A2P_TRANSFERS|EN_P2A_TRANSFERS|
155 A2P_HI_PRIORITY|P2A_HI_PRIORITY|
156 RESET_A2P_FLAGS|RESET_P2A_FLAGS));
158 iavc_write_port(sc, 0x07, 0x30); /* XXX magic numbers from */
159 iavc_write_port(sc, 0x10, 0xf0); /* XXX the linux driver */
162 AMCC_WRITE(sc, AMCC_RXPTR, vtophys(&sc->sc_recvbuf[0]));
163 AMCC_WRITE(sc, AMCC_RXLEN, 4);
164 sc->sc_csr |= EN_RX_TC_INT|EN_TX_TC_INT;
165 AMCC_WRITE(sc, AMCC_INTCSR, sc->sc_csr);
170 if(sc->sc_capi.card_type == CARD_TYPEC_AVM_B1_ISA)
178 int iavc_register(capi_softc_t *capi_sc, int applid, int nchan)
180 iavc_softc_t *sc = (iavc_softc_t*) capi_sc->ctx;
181 struct mbuf *m = i4b_Dgetmbuf(23);
185 printf("iavc%d: can't get memory\n", sc->sc_unit);
190 * byte 0x12 = SEND_REGISTER
193 * dword NumB3Connections 0..nbch
198 p = amcc_put_byte(mtod(m, u_int8_t*), 0);
199 p = amcc_put_byte(p, 0);
200 p = amcc_put_byte(p, SEND_REGISTER);
201 p = amcc_put_word(p, applid);
203 p = amcc_put_word(p, 1024 + (nchan + 1));
205 p = amcc_put_word(p, 1024 * (nchan + 1));
207 p = amcc_put_word(p, nchan);
208 p = amcc_put_word(p, 8);
209 p = amcc_put_word(p, 2048);
211 _IF_ENQUEUE(&sc->sc_txq, m);
218 int iavc_release(capi_softc_t *capi_sc, int applid)
220 iavc_softc_t *sc = (iavc_softc_t*) capi_sc->ctx;
221 struct mbuf *m = i4b_Dgetmbuf(7);
225 printf("iavc%d: can't get memory\n", sc->sc_unit);
230 * byte 0x14 = SEND_RELEASE
234 p = amcc_put_byte(mtod(m, u_int8_t*), 0);
235 p = amcc_put_byte(p, 0);
236 p = amcc_put_byte(p, SEND_RELEASE);
237 p = amcc_put_word(p, applid);
239 _IF_ENQUEUE(&sc->sc_txq, m);
245 int iavc_send(capi_softc_t *capi_sc, struct mbuf *m)
247 iavc_softc_t *sc = (iavc_softc_t*) capi_sc->ctx;
249 if (sc->sc_state != IAVC_UP) {
250 printf("iavc%d: attempt to send before device up\n", sc->sc_unit);
252 if (m->m_next) i4b_Bfreembuf(m->m_next);
258 if (_IF_QFULL(&sc->sc_txq)) {
259 #if defined (__FreeBSD__) && __FreeBSD__ > 4
260 _IF_DROP(&sc->sc_txq);
262 IF_DROP(&sc->sc_txq);
265 printf("iavc%d: tx overflow, message dropped\n", sc->sc_unit);
267 if (m->m_next) i4b_Bfreembuf(m->m_next);
271 _IF_ENQUEUE(&sc->sc_txq, m);
280 // Functions called by ourself during the initialization sequence:
281 // ---------------------------------------------------------------
284 // Sends the system initialization message to a newly loaded
285 // board, and sets state to INIT.
288 static int iavc_send_init(iavc_softc_t *sc)
290 struct mbuf *m = i4b_Dgetmbuf(15);
295 printf("iavc%d: can't get memory\n", sc->sc_unit);
300 * byte 0x11 = SEND_INIT
301 * dword NumApplications
306 p = amcc_put_byte(mtod(m, u_int8_t*), 0);
307 p = amcc_put_byte(p, 0);
308 p = amcc_put_byte(p, SEND_INIT);
309 p = amcc_put_word(p, 1); /* XXX MaxAppl XXX */
310 p = amcc_put_word(p, sc->sc_capi.sc_nbch);
311 p = amcc_put_word(p, sc->sc_unit);
314 _IF_ENQUEUE(&sc->sc_txq, m);
318 sc->sc_state = IAVC_INIT;
324 // Functions called during normal operation:
325 // -----------------------------------------
328 // Reads the initialization reply and calls capi_ll_control().
330 // iavc_receive_new_ncci
331 // Reads a new NCCI notification and calls capi_ll_control().
333 // iavc_receive_free_ncci
334 // Reads a freed NCCI notification and calls capi_ll_control().
336 // iavc_receive_task_ready
337 // Reads a task ready message -- which should not occur XXX.
339 // iavc_receive_debugmsg
340 // Reads a debug message -- which should not occur XXX.
342 // iavc_receive_start
343 // Reads a START TRANSMIT message and unblocks device.
346 // Reads a STOP TRANSMIT message and blocks device.
349 // Reads an incoming message and calls capi_ll_receive().
352 static int iavc_receive_init(iavc_softc_t *sc, u_int8_t *dmabuf)
356 u_int8_t *cardtype, *serial, *profile, *version, *caps, *prot;
359 p = amcc_get_word(dmabuf, &Length);
361 Length = iavc_get_slice(sc, sc->sc_recvbuf);
368 printf("iavc%d: rx_init: ", sc->sc_unit);
369 while (len < Length) {
370 printf(" %02x", p[len]);
371 if (len && (len % 16) == 0) printf("\n");
374 if (len % 16) printf("\n");
379 p += (*p + 1); /* driver version */
381 p += (*p + 1); /* card type */
382 p += (*p + 1); /* hardware ID */
384 p += (*p + 1); /* serial number */
386 p += (*p + 1); /* supported options */
388 p += (*p + 1); /* supported protocols */
391 if (cardtype && serial && profile) {
392 int nbch = ((profile[3]<<8) | profile[2]);
394 printf("iavc%d: AVM %s, s/n %s, %d chans, f/w rev %s, prot %s\n",
395 sc->sc_unit, cardtype, serial, nbch, version, prot);
398 printf("iavc%d: %s\n", sc->sc_unit, caps);
400 capi_ll_control(&sc->sc_capi, CAPI_CTRL_PROFILE, (int) profile);
403 printf("iavc%d: no profile data in info response?\n", sc->sc_unit);
406 sc->sc_blocked = TRUE; /* controller will send START when ready */
410 static int iavc_receive_start(iavc_softc_t *sc, u_int8_t *dmabuf)
412 struct mbuf *m = i4b_Dgetmbuf(3);
415 if (sc->sc_blocked && sc->sc_state == IAVC_UP)
416 printf("iavc%d: receive_start\n", sc->sc_unit);
419 printf("iavc%d: can't get memory\n", sc->sc_unit);
424 * byte 0x73 = SEND_POLLACK
427 p = amcc_put_byte(mtod(m, u_int8_t*), 0);
428 p = amcc_put_byte(p, 0);
429 p = amcc_put_byte(p, SEND_POLLACK);
431 _IF_PREPEND(&sc->sc_txq, m);
433 NDBGL4(L4_IAVCDBG, "iavc%d: blocked = %d, state = %d",
434 sc->sc_unit, sc->sc_blocked, sc->sc_state);
436 sc->sc_blocked = FALSE;
439 /* If this was our first START, register our readiness */
441 if (sc->sc_state != IAVC_UP) {
442 sc->sc_state = IAVC_UP;
443 capi_ll_control(&sc->sc_capi, CAPI_CTRL_READY, TRUE);
449 static int iavc_receive_stop(iavc_softc_t *sc, u_int8_t *dmabuf)
451 printf("iavc%d: receive_stop\n", sc->sc_unit);
452 sc->sc_blocked = TRUE;
456 static int iavc_receive_new_ncci(iavc_softc_t *sc, u_int8_t *dmabuf)
458 u_int32_t ApplId, NCCI, WindowSize;
461 dmabuf = amcc_get_word(dmabuf, &ApplId);
462 dmabuf = amcc_get_word(dmabuf, &NCCI);
463 dmabuf = amcc_get_word(dmabuf, &WindowSize);
465 ApplId = iavc_get_word(sc);
466 NCCI = iavc_get_word(sc);
467 WindowSize = iavc_get_word(sc);
470 capi_ll_control(&sc->sc_capi, CAPI_CTRL_NEW_NCCI, NCCI);
474 static int iavc_receive_free_ncci(iavc_softc_t *sc, u_int8_t *dmabuf)
476 u_int32_t ApplId, NCCI;
479 dmabuf = amcc_get_word(dmabuf, &ApplId);
480 dmabuf = amcc_get_word(dmabuf, &NCCI);
482 ApplId = iavc_get_word(sc);
483 NCCI = iavc_get_word(sc);
486 capi_ll_control(&sc->sc_capi, CAPI_CTRL_FREE_NCCI, NCCI);
490 static int iavc_receive_task_ready(iavc_softc_t *sc, u_int8_t *dmabuf)
492 u_int32_t TaskId, Length;
494 printf("iavc%d: receive_task_ready\n", sc->sc_unit);
497 p = amcc_get_word(dmabuf, &TaskId);
498 p = amcc_get_word(p, &Length);
500 TaskId = iavc_get_word(sc);
501 Length = iavc_get_slice(sc, sc->sc_recvbuf);
505 /* XXX could show the message if trace enabled? XXX */
509 static int iavc_receive_debugmsg(iavc_softc_t *sc, u_int8_t *dmabuf)
513 printf("iavc%d: receive_debugmsg\n", sc->sc_unit);
516 p = amcc_get_word(dmabuf, &Length);
518 Length = iavc_get_slice(sc, sc->sc_recvbuf);
522 /* XXX could show the message if trace enabled? XXX */
526 static int iavc_receive(iavc_softc_t *sc, u_int8_t *dmabuf, int b3data)
529 u_int32_t ApplId, Length;
532 * byte 0x21 = RECEIVE_MESSAGE
539 * byte 0x22 = RECEIVE_DATA_B3_IND
548 dmabuf = amcc_get_word(dmabuf, &ApplId);
549 dmabuf = amcc_get_word(dmabuf, &Length);
551 ApplId = iavc_get_word(sc);
552 Length = iavc_get_slice(sc, sc->sc_recvbuf);
553 dmabuf = sc->sc_recvbuf;
556 m = i4b_Dgetmbuf(Length);
558 printf("iavc%d: can't get memory for receive\n", sc->sc_unit);
562 bcopy(dmabuf, mtod(m, u_int8_t*), Length);
566 u_int8_t *p = mtod(m, u_int8_t*);
568 printf("iavc%d: applid=%d, len=%d\n", sc->sc_unit, ApplId, Length);
569 while (len < m->m_len) {
570 printf(" %02x", p[len]);
571 if (len && (len % 16) == 0) printf("\n");
574 if (len % 16) printf("\n");
580 dmabuf = amcc_get_word(dmabuf + Length, &Length);
582 Length = iavc_get_slice(sc, sc->sc_recvbuf);
583 dmabuf = sc->sc_recvbuf;
586 m->m_next = i4b_Bgetmbuf(Length);
588 printf("iavc%d: can't get memory for receive\n", sc->sc_unit);
593 bcopy(dmabuf, mtod(m->m_next, u_int8_t*), Length);
596 capi_ll_receive(&sc->sc_capi, m);
602 // Checks device interrupt status and calls iavc_handle_{rx,tx}()
606 // Reads in the command byte and calls the subroutines above.
609 // Initiates DMA on the next queued message if possible.
612 void iavc_handle_intr(iavc_softc_t *sc)
618 while (iavc_rx_full(sc))
623 status = AMCC_READ(sc, AMCC_INTCSR);
624 if ((status & ANY_S5933_INT) == 0)
627 newcsr = sc->sc_csr | (status & ALL_INT);
628 if (status & TX_TC_INT) newcsr &= ~EN_TX_TC_INT;
629 if (status & RX_TC_INT) newcsr &= ~EN_RX_TC_INT;
630 AMCC_WRITE(sc, AMCC_INTCSR, newcsr);
633 if (status & RX_TC_INT) {
636 if (sc->sc_recvlen == 0) {
637 sc->sc_recvlen = *((u_int32_t*)(&sc->sc_recvbuf[0]));
638 rxlen = (sc->sc_recvlen + 3) & ~3;
639 AMCC_WRITE(sc, AMCC_RXPTR, vtophys(&sc->sc_recvbuf[4]));
640 AMCC_WRITE(sc, AMCC_RXLEN, rxlen);
644 AMCC_WRITE(sc, AMCC_RXPTR, vtophys(&sc->sc_recvbuf[0]));
645 AMCC_WRITE(sc, AMCC_RXLEN, 4);
649 if (status & TX_TC_INT) {
650 sc->sc_csr &= ~EN_TX_TC_INT;
654 AMCC_WRITE(sc, AMCC_INTCSR, sc->sc_csr);
658 static void iavc_handle_rx(iavc_softc_t *sc)
660 u_int8_t *dmabuf = 0, cmd;
663 dmabuf = amcc_get_byte(&sc->sc_recvbuf[4], &cmd);
665 cmd = iavc_get_byte(sc);
668 NDBGL4(L4_IAVCDBG, "iavc%d: command = 0x%02x", sc->sc_unit, cmd);
671 case RECEIVE_DATA_B3_IND:
672 iavc_receive(sc, dmabuf, TRUE);
675 case RECEIVE_MESSAGE:
676 iavc_receive(sc, dmabuf, FALSE);
679 case RECEIVE_NEW_NCCI:
680 iavc_receive_new_ncci(sc, dmabuf);
683 case RECEIVE_FREE_NCCI:
684 iavc_receive_free_ncci(sc, dmabuf);
688 iavc_receive_start(sc, dmabuf);
692 iavc_receive_stop(sc, dmabuf);
696 iavc_receive_init(sc, dmabuf);
699 case RECEIVE_TASK_READY:
700 iavc_receive_task_ready(sc, dmabuf);
703 case RECEIVE_DEBUGMSG:
704 iavc_receive_debugmsg(sc, dmabuf);
708 printf("iavc%d: unknown msg %02x\n", sc->sc_unit, cmd);
712 static void iavc_start_tx(iavc_softc_t *sc)
718 /* If device has put us on hold, punt. */
720 if (sc->sc_blocked) {
724 /* If using DMA and transmitter busy, punt. */
726 if (sc->sc_dma && (sc->sc_csr & EN_TX_TC_INT)) {
730 /* Else, see if we have messages to send. */
732 _IF_DEQUEUE(&sc->sc_txq, m);
737 /* Have message, will send. */
739 if (CAPIMSG_LEN(m->m_data)) {
740 /* A proper CAPI message, possibly with B3 data */
743 /* Copy message to DMA buffer. */
746 dmabuf = amcc_put_byte(&sc->sc_sendbuf[0], SEND_DATA_B3_REQ);
748 dmabuf = amcc_put_byte(&sc->sc_sendbuf[0], SEND_MESSAGE);
751 dmabuf = amcc_put_word(dmabuf, m->m_len);
752 bcopy(m->m_data, dmabuf, m->m_len);
754 txlen = 5 + m->m_len;
757 dmabuf = amcc_put_word(dmabuf, m->m_next->m_len);
758 bcopy(m->m_next->m_data, dmabuf, m->m_next->m_len);
759 txlen += 4 + m->m_next->m_len;
766 iavc_put_byte(sc, SEND_DATA_B3_REQ);
767 NDBGL4(L4_IAVCDBG, "iavc%d: tx SDB3R msg, len = %d", sc->sc_unit, m->m_len);
769 iavc_put_byte(sc, SEND_MESSAGE);
770 NDBGL4(L4_IAVCDBG, "iavc%d: tx SM msg, len = %d", sc->sc_unit, m->m_len);
774 u_int8_t *p = mtod(m, u_int8_t*);
776 for (len = 0; len < m->m_len; len++) {
777 printf(" %02x", *p++);
778 if (len && (len % 16) == 0) printf("\n");
780 if (len % 16) printf("\n");
784 iavc_put_slice(sc, m->m_data, m->m_len);
787 iavc_put_slice(sc, m->m_next->m_data, m->m_next->m_len);
792 /* A board control message to be sent as is */
795 bcopy(m->m_data + 2, &sc->sc_sendbuf[0], m->m_len - 2);
796 txlen = m->m_len - 2;
801 u_int8_t *p = mtod(m, u_int8_t*) + 2;
803 printf("iavc%d: tx BDC msg, len = %d, msg =", sc->sc_unit, m->m_len-2);
804 for (len = 0; len < m->m_len-2; len++) {
805 printf(" %02x", *p++);
806 if (len && (len % 16) == 0) printf("\n");
808 if (len % 16) printf("\n");
812 txlen = m->m_len - 2;
813 dmabuf = mtod(m, char*) + 2;
815 b1io_put_byte(sc, *dmabuf++);
820 i4b_Bfreembuf(m->m_next);
826 /* Start transmitter */
828 txlen = (txlen + 3) & ~3;
829 AMCC_WRITE(sc, AMCC_TXPTR, vtophys(&sc->sc_sendbuf[0]));
830 AMCC_WRITE(sc, AMCC_TXLEN, txlen);
831 sc->sc_csr |= EN_TX_TC_INT;
834 AMCC_WRITE(sc, AMCC_INTCSR, sc->sc_csr);