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