Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / net / i4b / capi / iavc / iavc_card.c
1 /*
2  * Copyright (c) 2001 Cubical Solutions Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * capi/iavc/iavc_card.c
26  *              The AVM ISDN controllers' card specific support routines.
27  *
28  * $FreeBSD: src/sys/i4b/capi/iavc/iavc_card.c,v 1.1.2.1 2001/08/10 14:08:34 obrien Exp $
29  * $DragonFly: src/sys/net/i4b/capi/iavc/iavc_card.c,v 1.2 2003/06/17 04:28:39 dillon Exp $
30  */
31
32 #include "iavc.h"
33 #include "i4bcapi.h"
34 #include "pci.h"
35
36 #if (NIAVC > 0) && (NI4BCAPI > 0)
37
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/mbuf.h>
42 #include <sys/socket.h>
43 #include <net/if.h>
44
45 #include <machine/clock.h>
46
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 #include <sys/bus.h>
50 #include <sys/rman.h>
51
52 #include <machine/i4b_debug.h>
53 #include <machine/i4b_ioctl.h>
54 #include <machine/i4b_trace.h>
55
56 #include <i4b/include/i4b_global.h>
57 #include <i4b/include/i4b_l3l4.h>
58 #include <i4b/include/i4b_mbuf.h>
59
60 #include <i4b/capi/capi.h>
61
62 #include <i4b/capi/iavc/iavc.h>
63
64 /*
65 //  AVM B1 (active BRI, PIO mode)
66 */
67
68 int b1_detect(iavc_softc_t *sc)
69 {
70     if ((iavc_read_port(sc, B1_INSTAT) & 0xfc) ||
71         (iavc_read_port(sc, B1_OUTSTAT) & 0xfc))
72         return (1);
73
74     b1io_outp(sc, B1_INSTAT, 0x02);
75     b1io_outp(sc, B1_OUTSTAT, 0x02);
76     if ((iavc_read_port(sc, B1_INSTAT) & 0xfe) != 2 ||
77         (iavc_read_port(sc, B1_OUTSTAT) & 0xfe) != 2)
78         return (2);
79
80     b1io_outp(sc, B1_INSTAT, 0x00);
81     b1io_outp(sc, B1_OUTSTAT, 0x00);
82     if ((iavc_read_port(sc, B1_INSTAT) & 0xfe) ||
83         (iavc_read_port(sc, B1_OUTSTAT) & 0xfe))
84         return (3);
85
86     return (0); /* found */
87 }
88
89 void b1_disable_irq(iavc_softc_t *sc)
90 {
91     b1io_outp(sc, B1_INSTAT, 0x00);
92 }
93
94 void b1_reset(iavc_softc_t *sc)
95 {
96     b1io_outp(sc, B1_RESET, 0);
97     DELAY(55*2*1000);
98
99     b1io_outp(sc, B1_RESET, 1);
100     DELAY(55*2*1000);
101
102     b1io_outp(sc, B1_RESET, 0);
103     DELAY(55*2*1000);
104 }
105
106 /*
107 //  Newer PCI-based B1's, and T1's, supports DMA
108 */
109
110 int b1dma_detect(iavc_softc_t *sc)
111 {
112     AMCC_WRITE(sc, AMCC_MCSR, 0);
113     DELAY(10*1000);
114     AMCC_WRITE(sc, AMCC_MCSR, 0x0f000000);
115     DELAY(10*1000);
116     AMCC_WRITE(sc, AMCC_MCSR, 0);
117     DELAY(42*1000);
118
119     AMCC_WRITE(sc, AMCC_RXLEN, 0);
120     AMCC_WRITE(sc, AMCC_TXLEN, 0);
121     sc->sc_csr = 0;
122     AMCC_WRITE(sc, AMCC_INTCSR, sc->sc_csr);
123
124     if (AMCC_READ(sc, AMCC_INTCSR) != 0)
125         return 1;
126
127     AMCC_WRITE(sc, AMCC_RXPTR, 0xffffffff);
128     AMCC_WRITE(sc, AMCC_TXPTR, 0xffffffff);
129     if ((AMCC_READ(sc, AMCC_RXPTR) != 0xfffffffc) ||
130         (AMCC_READ(sc, AMCC_TXPTR) != 0xfffffffc))
131         return 2;
132
133     AMCC_WRITE(sc, AMCC_RXPTR, 0);
134     AMCC_WRITE(sc, AMCC_TXPTR, 0);
135     if ((AMCC_READ(sc, AMCC_RXPTR) != 0) ||
136         (AMCC_READ(sc, AMCC_TXPTR) != 0))
137         return 3;
138
139     iavc_write_port(sc, 0x10, 0x00);
140     iavc_write_port(sc, 0x07, 0x00);
141
142     iavc_write_port(sc, 0x02, 0x02);
143     iavc_write_port(sc, 0x03, 0x02);
144
145     if (((iavc_read_port(sc, 0x02) & 0xfe) != 0x02) ||
146         (iavc_read_port(sc, 0x03) != 0x03))
147         return 4;
148
149     iavc_write_port(sc, 0x02, 0x00);
150     iavc_write_port(sc, 0x03, 0x00);
151
152     if (((iavc_read_port(sc, 0x02) & 0xfe) != 0x00) ||
153         (iavc_read_port(sc, 0x03) != 0x01))
154         return 5;
155
156     return (0); /* found */
157 }
158
159 void b1dma_reset(iavc_softc_t *sc)
160 {
161     int s = SPLI4B();
162
163     sc->sc_csr = 0;
164     AMCC_WRITE(sc, AMCC_INTCSR, sc->sc_csr);
165     AMCC_WRITE(sc, AMCC_MCSR, 0);
166     AMCC_WRITE(sc, AMCC_RXLEN, 0);
167     AMCC_WRITE(sc, AMCC_TXLEN, 0);
168
169     iavc_write_port(sc, 0x10, 0x00); /* XXX magic numbers from */
170     iavc_write_port(sc, 0x07, 0x00); /* XXX the linux driver */
171
172     splx(s);
173
174     AMCC_WRITE(sc, AMCC_MCSR, 0);
175     DELAY(10 * 1000);
176     AMCC_WRITE(sc, AMCC_MCSR, 0x0f000000);
177     DELAY(10 * 1000);
178     AMCC_WRITE(sc, AMCC_MCSR, 0);
179     DELAY(42 * 1000);
180 }
181
182 /*
183 //  AVM T1 (active PRI)
184 */
185
186 /* XXX how do these differ from b1io_{read,write}_reg()? XXX */
187
188 static int b1dma_tx_empty(int iobase)
189 { return inb(iobase + 3) & 1; }
190
191 static int b1dma_rx_full(int iobase)
192 { return inb(iobase + 2) & 1; }
193
194 static int b1dma_tolink(iavc_softc_t *sc, void *buf, int len)
195 {
196     volatile int spin;
197     char *s = (char*) buf;
198     while (len--) {
199         spin = 0;
200         while (!b1dma_tx_empty(sc->sc_iobase) && spin < 100000)
201             spin++;
202         if (!b1dma_tx_empty(sc->sc_iobase))
203             return -1;
204         t1io_outp(sc, 1, *s++);
205     }
206     return 0;
207 }
208
209 static int b1dma_fromlink(iavc_softc_t *sc, void *buf, int len)
210 {
211     volatile int spin;
212     char *s = (char*) buf;
213     while (len--) {
214         spin = 0;
215         while (!b1dma_rx_full(sc->sc_iobase) && spin < 100000)
216             spin++;
217         if (!b1dma_rx_full(sc->sc_iobase))
218             return -1;
219         *s++ = t1io_inp(sc, 0);
220     }
221     return 0;
222 }
223
224 static int WriteReg(iavc_softc_t *sc, u_int32_t reg, u_int8_t val)
225 {
226     u_int8_t cmd = 0;
227     if (b1dma_tolink(sc, &cmd, 1) == 0 &&
228         b1dma_tolink(sc, &reg, 4) == 0) {
229         u_int32_t tmp = val;
230         return b1dma_tolink(sc, &tmp, 4);
231     }
232     return -1;
233 }
234
235 static u_int8_t ReadReg(iavc_softc_t *sc, u_int32_t reg)
236 {
237     u_int8_t cmd = 1;
238     if (b1dma_tolink(sc, &cmd, 1) == 0 &&
239         b1dma_tolink(sc, &reg, 4) == 0) {
240         u_int32_t tmp;
241         if (b1dma_fromlink(sc, &tmp, 4) == 0)
242             return (u_int8_t) tmp;
243     }
244     return 0xff;
245 }
246
247 int t1_detect(iavc_softc_t *sc)
248 {
249     int ret = b1dma_detect(sc);
250     if (ret) return ret;
251
252     if ((WriteReg(sc, 0x80001000, 0x11) != 0) ||
253         (WriteReg(sc, 0x80101000, 0x22) != 0) ||
254         (WriteReg(sc, 0x80201000, 0x33) != 0) ||
255         (WriteReg(sc, 0x80301000, 0x44) != 0))
256         return 6;
257
258     if ((ReadReg(sc, 0x80001000) != 0x11) ||
259         (ReadReg(sc, 0x80101000) != 0x22) ||
260         (ReadReg(sc, 0x80201000) != 0x33) ||
261         (ReadReg(sc, 0x80301000) != 0x44))
262         return 7;
263
264     if ((WriteReg(sc, 0x80001000, 0x55) != 0) ||
265         (WriteReg(sc, 0x80101000, 0x66) != 0) ||
266         (WriteReg(sc, 0x80201000, 0x77) != 0) ||
267         (WriteReg(sc, 0x80301000, 0x88) != 0))
268         return 8;
269
270     if ((ReadReg(sc, 0x80001000) != 0x55) ||
271         (ReadReg(sc, 0x80101000) != 0x66) ||
272         (ReadReg(sc, 0x80201000) != 0x77) ||
273         (ReadReg(sc, 0x80301000) != 0x88))
274         return 9;
275
276     return 0; /* found */
277 }
278
279 void t1_disable_irq(iavc_softc_t *sc)
280 {
281     iavc_write_port(sc, T1_IRQMASTER, 0x00);
282 }
283
284 void t1_reset(iavc_softc_t *sc)
285 {
286     b1_reset(sc);
287     iavc_write_port(sc, B1_INSTAT, 0x00);
288     iavc_write_port(sc, B1_OUTSTAT, 0x00);
289     iavc_write_port(sc, T1_IRQMASTER, 0x00);
290     iavc_write_port(sc, T1_RESETBOARD, 0x0f);
291 }
292
293 #endif