kernel tree reorganization stage 1: Major cvs repository work (not logged as
[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.3 2003/08/07 21:17:24 dillon Exp $
30  */
31
32 #include "use_iavc.h"
33 #include "use_i4bcapi.h"
34 #include "use_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 "../../include/i4b_global.h"
57 #include "../../include/i4b_l3l4.h"
58 #include "../../include/i4b_mbuf.h"
59 #include "../capi.h"
60
61 #include "iavc.h"
62
63 /*
64 //  AVM B1 (active BRI, PIO mode)
65 */
66
67 int b1_detect(iavc_softc_t *sc)
68 {
69     if ((iavc_read_port(sc, B1_INSTAT) & 0xfc) ||
70         (iavc_read_port(sc, B1_OUTSTAT) & 0xfc))
71         return (1);
72
73     b1io_outp(sc, B1_INSTAT, 0x02);
74     b1io_outp(sc, B1_OUTSTAT, 0x02);
75     if ((iavc_read_port(sc, B1_INSTAT) & 0xfe) != 2 ||
76         (iavc_read_port(sc, B1_OUTSTAT) & 0xfe) != 2)
77         return (2);
78
79     b1io_outp(sc, B1_INSTAT, 0x00);
80     b1io_outp(sc, B1_OUTSTAT, 0x00);
81     if ((iavc_read_port(sc, B1_INSTAT) & 0xfe) ||
82         (iavc_read_port(sc, B1_OUTSTAT) & 0xfe))
83         return (3);
84
85     return (0); /* found */
86 }
87
88 void b1_disable_irq(iavc_softc_t *sc)
89 {
90     b1io_outp(sc, B1_INSTAT, 0x00);
91 }
92
93 void b1_reset(iavc_softc_t *sc)
94 {
95     b1io_outp(sc, B1_RESET, 0);
96     DELAY(55*2*1000);
97
98     b1io_outp(sc, B1_RESET, 1);
99     DELAY(55*2*1000);
100
101     b1io_outp(sc, B1_RESET, 0);
102     DELAY(55*2*1000);
103 }
104
105 /*
106 //  Newer PCI-based B1's, and T1's, supports DMA
107 */
108
109 int b1dma_detect(iavc_softc_t *sc)
110 {
111     AMCC_WRITE(sc, AMCC_MCSR, 0);
112     DELAY(10*1000);
113     AMCC_WRITE(sc, AMCC_MCSR, 0x0f000000);
114     DELAY(10*1000);
115     AMCC_WRITE(sc, AMCC_MCSR, 0);
116     DELAY(42*1000);
117
118     AMCC_WRITE(sc, AMCC_RXLEN, 0);
119     AMCC_WRITE(sc, AMCC_TXLEN, 0);
120     sc->sc_csr = 0;
121     AMCC_WRITE(sc, AMCC_INTCSR, sc->sc_csr);
122
123     if (AMCC_READ(sc, AMCC_INTCSR) != 0)
124         return 1;
125
126     AMCC_WRITE(sc, AMCC_RXPTR, 0xffffffff);
127     AMCC_WRITE(sc, AMCC_TXPTR, 0xffffffff);
128     if ((AMCC_READ(sc, AMCC_RXPTR) != 0xfffffffc) ||
129         (AMCC_READ(sc, AMCC_TXPTR) != 0xfffffffc))
130         return 2;
131
132     AMCC_WRITE(sc, AMCC_RXPTR, 0);
133     AMCC_WRITE(sc, AMCC_TXPTR, 0);
134     if ((AMCC_READ(sc, AMCC_RXPTR) != 0) ||
135         (AMCC_READ(sc, AMCC_TXPTR) != 0))
136         return 3;
137
138     iavc_write_port(sc, 0x10, 0x00);
139     iavc_write_port(sc, 0x07, 0x00);
140
141     iavc_write_port(sc, 0x02, 0x02);
142     iavc_write_port(sc, 0x03, 0x02);
143
144     if (((iavc_read_port(sc, 0x02) & 0xfe) != 0x02) ||
145         (iavc_read_port(sc, 0x03) != 0x03))
146         return 4;
147
148     iavc_write_port(sc, 0x02, 0x00);
149     iavc_write_port(sc, 0x03, 0x00);
150
151     if (((iavc_read_port(sc, 0x02) & 0xfe) != 0x00) ||
152         (iavc_read_port(sc, 0x03) != 0x01))
153         return 5;
154
155     return (0); /* found */
156 }
157
158 void b1dma_reset(iavc_softc_t *sc)
159 {
160     int s = SPLI4B();
161
162     sc->sc_csr = 0;
163     AMCC_WRITE(sc, AMCC_INTCSR, sc->sc_csr);
164     AMCC_WRITE(sc, AMCC_MCSR, 0);
165     AMCC_WRITE(sc, AMCC_RXLEN, 0);
166     AMCC_WRITE(sc, AMCC_TXLEN, 0);
167
168     iavc_write_port(sc, 0x10, 0x00); /* XXX magic numbers from */
169     iavc_write_port(sc, 0x07, 0x00); /* XXX the linux driver */
170
171     splx(s);
172
173     AMCC_WRITE(sc, AMCC_MCSR, 0);
174     DELAY(10 * 1000);
175     AMCC_WRITE(sc, AMCC_MCSR, 0x0f000000);
176     DELAY(10 * 1000);
177     AMCC_WRITE(sc, AMCC_MCSR, 0);
178     DELAY(42 * 1000);
179 }
180
181 /*
182 //  AVM T1 (active PRI)
183 */
184
185 /* XXX how do these differ from b1io_{read,write}_reg()? XXX */
186
187 static int b1dma_tx_empty(int iobase)
188 { return inb(iobase + 3) & 1; }
189
190 static int b1dma_rx_full(int iobase)
191 { return inb(iobase + 2) & 1; }
192
193 static int b1dma_tolink(iavc_softc_t *sc, void *buf, int len)
194 {
195     volatile int spin;
196     char *s = (char*) buf;
197     while (len--) {
198         spin = 0;
199         while (!b1dma_tx_empty(sc->sc_iobase) && spin < 100000)
200             spin++;
201         if (!b1dma_tx_empty(sc->sc_iobase))
202             return -1;
203         t1io_outp(sc, 1, *s++);
204     }
205     return 0;
206 }
207
208 static int b1dma_fromlink(iavc_softc_t *sc, void *buf, int len)
209 {
210     volatile int spin;
211     char *s = (char*) buf;
212     while (len--) {
213         spin = 0;
214         while (!b1dma_rx_full(sc->sc_iobase) && spin < 100000)
215             spin++;
216         if (!b1dma_rx_full(sc->sc_iobase))
217             return -1;
218         *s++ = t1io_inp(sc, 0);
219     }
220     return 0;
221 }
222
223 static int WriteReg(iavc_softc_t *sc, u_int32_t reg, u_int8_t val)
224 {
225     u_int8_t cmd = 0;
226     if (b1dma_tolink(sc, &cmd, 1) == 0 &&
227         b1dma_tolink(sc, &reg, 4) == 0) {
228         u_int32_t tmp = val;
229         return b1dma_tolink(sc, &tmp, 4);
230     }
231     return -1;
232 }
233
234 static u_int8_t ReadReg(iavc_softc_t *sc, u_int32_t reg)
235 {
236     u_int8_t cmd = 1;
237     if (b1dma_tolink(sc, &cmd, 1) == 0 &&
238         b1dma_tolink(sc, &reg, 4) == 0) {
239         u_int32_t tmp;
240         if (b1dma_fromlink(sc, &tmp, 4) == 0)
241             return (u_int8_t) tmp;
242     }
243     return 0xff;
244 }
245
246 int t1_detect(iavc_softc_t *sc)
247 {
248     int ret = b1dma_detect(sc);
249     if (ret) return ret;
250
251     if ((WriteReg(sc, 0x80001000, 0x11) != 0) ||
252         (WriteReg(sc, 0x80101000, 0x22) != 0) ||
253         (WriteReg(sc, 0x80201000, 0x33) != 0) ||
254         (WriteReg(sc, 0x80301000, 0x44) != 0))
255         return 6;
256
257     if ((ReadReg(sc, 0x80001000) != 0x11) ||
258         (ReadReg(sc, 0x80101000) != 0x22) ||
259         (ReadReg(sc, 0x80201000) != 0x33) ||
260         (ReadReg(sc, 0x80301000) != 0x44))
261         return 7;
262
263     if ((WriteReg(sc, 0x80001000, 0x55) != 0) ||
264         (WriteReg(sc, 0x80101000, 0x66) != 0) ||
265         (WriteReg(sc, 0x80201000, 0x77) != 0) ||
266         (WriteReg(sc, 0x80301000, 0x88) != 0))
267         return 8;
268
269     if ((ReadReg(sc, 0x80001000) != 0x55) ||
270         (ReadReg(sc, 0x80101000) != 0x66) ||
271         (ReadReg(sc, 0x80201000) != 0x77) ||
272         (ReadReg(sc, 0x80301000) != 0x88))
273         return 9;
274
275     return 0; /* found */
276 }
277
278 void t1_disable_irq(iavc_softc_t *sc)
279 {
280     iavc_write_port(sc, T1_IRQMASTER, 0x00);
281 }
282
283 void t1_reset(iavc_softc_t *sc)
284 {
285     b1_reset(sc);
286     iavc_write_port(sc, B1_INSTAT, 0x00);
287     iavc_write_port(sc, B1_OUTSTAT, 0x00);
288     iavc_write_port(sc, T1_IRQMASTER, 0x00);
289     iavc_write_port(sc, T1_RESETBOARD, 0x0f);
290 }
291
292 #endif