Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / net / i4b / layer1 / isic / i4b_avm_a1.c
1 /*
2  *   Copyright (c) 1996 Andrew Gordon. All rights reserved.
3  *
4  *   Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *   1. Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *   2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *   3. Neither the name of the author nor the names of any co-contributors
16  *      may be used to endorse or promote products derived from this software
17  *      without specific prior written permission.
18  *   4. Altered versions must be plainly marked as such, and must not be
19  *      misrepresented as being the original software and/or documentation.
20  *   
21  *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  *   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  *   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  *   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  *   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  *   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  *   SUCH DAMAGE.
32  *
33  *---------------------------------------------------------------------------
34  *
35  *      i4b_avm_a1.c - AVM A1/Fritz passive card driver for isdn4bsd
36  *      ------------------------------------------------------------
37  *
38  * $FreeBSD: src/sys/i4b/layer1/isic/i4b_avm_a1.c,v 1.5.2.1 2001/08/10 14:08:38 obrien Exp $
39  *
40  *      last edit-date: [Wed Jan 24 09:25:23 2001]
41  *
42  *---------------------------------------------------------------------------*/
43
44 #include "use_isic.h"
45 #include "opt_i4b.h"
46
47 #if NISIC > 0 && defined(AVM_A1)
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/socket.h>
52 #include <net/if.h>
53
54 #include <net/i4b/include/machine/i4b_ioctl.h>
55 #include <net/i4b/include/machine/i4b_trace.h>
56 #include <machine/clock.h>
57
58 #include "../i4b_l1.h"
59 #include "i4b_isic.h"
60 #include "i4b_hscx.h"
61
62 /*---------------------------------------------------------------------------*
63  *      AVM A1 and AVM Fritz! Card special registers
64  *---------------------------------------------------------------------------*/
65
66 #define AVM_CONF_REG    0x1800          /* base offset for config register */
67 #define AVM_CONF_IRQ    0x1801          /* base offset for IRQ register    */
68                                         /* config register write           */
69 #define  AVM_CONF_WR_RESET      0x01    /* 1 = RESET ISAC and HSCX         */
70 #define  AVM_CONF_WR_CCL        0x02    /* 1 = clear counter low nibble    */
71 #define  AVM_CONF_WR_CCH        0x04    /* 1 = clear counter high nibble   */
72 #define  AVM_CONF_WR_IRQEN      0x08    /* 1 = enable IRQ                  */
73 #define  AVM_CONF_WR_TEST       0x10    /* test bit                        */
74                                         /* config register read            */
75 #define  AVM_CONF_RD_IIRQ       0x01    /* 0 = ISAC IRQ active             */
76 #define  AVM_CONF_RD_HIRQ       0x02    /* 0 = HSCX IRQ active             */
77 #define  AVM_CONF_RD_CIRQ       0x04    /* 0 = counter IRQ active          */
78 #define  AVM_CONF_RD_ZER1       0x08    /* unused, always read 0           */
79 #define  AVM_CONF_RD_TEST       0x10    /* test bit read back              */
80 #define  AVM_CONF_RD_ZER2       0x20    /* unused, always read 0           */
81
82 #define AVM_ISAC_R_OFFS         (0x1400-0x20)
83 #define AVM_HSCXA_R_OFFS        (0x400-0x20)
84 #define AVM_HSCXB_R_OFFS        (0xc00-0x20)
85 #define AVM_ISAC_F_OFFS         (0x1400-0x20-0x3e0)
86 #define AVM_HSCXA_F_OFFS        (0x400-0x20-0x3e0)
87 #define AVM_HSCXB_F_OFFS        (0xc00-0x20-0x3e0)
88
89 /*---------------------------------------------------------------------------*
90  *      AVM read fifo routine
91  *---------------------------------------------------------------------------*/
92 static void
93 avma1_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
94 {
95         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.io_base[what+4]);
96         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+4]);
97         bus_space_read_multi_1(t, h, 0, buf, size);
98 }
99
100 /*---------------------------------------------------------------------------*
101  *      AVM write fifo routine
102  *---------------------------------------------------------------------------*/
103 static void
104 avma1_write_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
105 {
106         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.io_base[what+4]);
107         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+4]);
108         bus_space_write_multi_1(t, h, 0, (u_int8_t*)buf, size);
109 }
110
111 /*---------------------------------------------------------------------------*
112  *      AVM write register routine
113  *---------------------------------------------------------------------------*/
114 static void
115 avma1_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
116 {
117         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.io_base[what+1]);
118         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+1]);
119         bus_space_write_1(t, h, offs, data);
120 }
121
122 /*---------------------------------------------------------------------------*
123  *      AVM read register routine
124  *---------------------------------------------------------------------------*/
125 static u_int8_t
126 avma1_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
127 {
128         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.io_base[what+1]);
129         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+1]);
130         return bus_space_read_1(t, h, offs);
131 }
132
133 /*---------------------------------------------------------------------------*
134  *      allocate an io port
135  *---------------------------------------------------------------------------*/
136 static int
137 isic_alloc_port(device_t dev, int rid, u_int base, u_int len)
138
139         size_t unit = device_get_unit(dev);
140         struct l1_softc *sc = &l1_sc[unit];
141
142         sc->sc_resources.io_rid[rid] = rid;
143
144         bus_set_resource(dev, SYS_RES_IOPORT, rid, base, len, -1);
145
146         if(!(sc->sc_resources.io_base[rid] =
147                 bus_alloc_resource(dev, SYS_RES_IOPORT,
148                                    &sc->sc_resources.io_rid[rid],
149                                    0ul, ~0ul, 1, RF_ACTIVE)))
150         {
151                 kprintf("isic%d: Error, failed to reserve io #%d!\n", unit, rid);
152                 isic_detach_common(dev);
153                 return(ENXIO);
154         }
155         return(0);
156 }
157
158 /*---------------------------------------------------------------------------*
159  *      isic_probe_avma1 - probe for AVM A1 and compatibles
160  *---------------------------------------------------------------------------*/
161 int
162 isic_probe_avma1(device_t dev)
163 {
164         size_t unit = device_get_unit(dev);     /* get unit */
165         struct l1_softc *sc = NULL;     /* pointer to softc */
166         void *ih = NULL;                        /* dummy */
167         bus_space_tag_t    t;           /* bus things */
168         bus_space_handle_t h;
169         u_char savebyte;
170         u_char byte;
171
172         /* check max unit range */
173
174         if(unit >= ISIC_MAXUNIT)
175         {
176                 kprintf("isic%d: Error, unit %d >= ISIC_MAXUNIT for AVM A1/Fritz!\n",
177                                 unit, unit);
178                 return(ENXIO);  
179         }
180
181         sc = &l1_sc[unit];                      /* get pointer to softc */
182         sc->sc_unit = unit;                     /* set unit */
183
184         /* see if an io base was supplied */
185         
186         if(!(sc->sc_resources.io_base[0] =
187                         bus_alloc_resource(dev, SYS_RES_IOPORT,
188                                            &sc->sc_resources.io_rid[0],
189                                            0ul, ~0ul, 1, RF_ACTIVE)))
190         {
191                 kprintf("isic%d: Could not get iobase for AVM A1/Fritz!\n",
192                                 unit);
193                 return(ENXIO);
194         }
195
196         /* set io base */
197
198         sc->sc_port = rman_get_start(sc->sc_resources.io_base[0]);
199         
200         /* release io base */
201         
202         bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_resources.io_rid[0],
203                         sc->sc_resources.io_base[0]);
204
205         switch(sc->sc_port)
206         {
207                 case 0x200:
208                 case 0x240:
209                 case 0x300:
210                 case 0x340:             
211                         break;
212                         
213                 default:
214                         kprintf("isic%d: Error, invalid iobase 0x%x specified for AVM A1/Fritz!\n",
215                                 unit, sc->sc_port);
216                         return(ENXIO);
217                         break;
218         }
219
220         if(isic_alloc_port(dev, 0, sc->sc_port+AVM_CONF_REG, 0x20))
221                 return(ENXIO);
222
223         if(isic_alloc_port(dev, 1, sc->sc_port+AVM_ISAC_R_OFFS, 0x20))
224                 return(ENXIO);
225
226         if(isic_alloc_port(dev, 2, sc->sc_port+AVM_HSCXA_R_OFFS, 0x20))
227                 return(ENXIO);
228
229         if(isic_alloc_port(dev, 3, sc->sc_port+AVM_HSCXB_R_OFFS, 0x20))
230                 return(ENXIO);
231
232         if(isic_alloc_port(dev, 4, sc->sc_port+AVM_ISAC_F_OFFS, 0x20))
233                 return(ENXIO);
234
235         if(isic_alloc_port(dev, 5, sc->sc_port+AVM_HSCXA_F_OFFS, 0x20))
236                 return(ENXIO);
237
238         if(isic_alloc_port(dev, 6, sc->sc_port+AVM_HSCXB_F_OFFS, 0x20))
239                 return(ENXIO);
240
241         /* get our irq */
242
243         if(!(sc->sc_resources.irq =
244                 bus_alloc_resource(dev, SYS_RES_IRQ,
245                                    &sc->sc_resources.irq_rid,
246                                    0ul, ~0ul, 1, RF_ACTIVE)))
247         {
248                 kprintf("isic%d: Could not get an irq for AVM A1/Fritz!\n",unit);
249                 isic_detach_common(dev);
250                 return ENXIO;
251         }
252
253         /* get the irq number */
254         sc->sc_irq = rman_get_start(sc->sc_resources.irq);
255
256         /* register interupt routine */
257         bus_setup_intr(dev, sc->sc_resources.irq, 0,
258                         (void(*)(void *))(isicintr),
259                         sc, &ih, NULL);
260
261         /* check IRQ validity */
262
263         switch(sc->sc_irq)
264         {
265                 case 3:
266                 case 4:
267                 case 5:
268                 case 6:
269                 case 7:
270                 case 8:
271                 case 10:
272                 case 11:
273                 case 12:
274                 case 13:
275                 case 14:
276                 case 15:
277                         break;
278                         
279                 default:
280                         kprintf("isic%d: Error, invalid IRQ [%d] specified for AVM A1/Fritz!\n",
281                                 unit, sc->sc_irq);
282                         isic_detach_common(dev);
283                         return(ENXIO);
284                         break;
285         }               
286
287         sc->clearirq = NULL;
288         sc->readreg = avma1_read_reg;
289         sc->writereg = avma1_write_reg;
290
291         sc->readfifo = avma1_read_fifo;
292         sc->writefifo = avma1_write_fifo;
293
294         /* setup card type */
295
296         sc->sc_cardtyp = CARD_TYPEP_AVMA1;
297
298         /* setup IOM bus type */
299         
300         sc->sc_bustyp = BUS_TYPE_IOM2;
301
302         sc->sc_ipac = 0;
303         sc->sc_bfifolen = HSCX_FIFO_LEN;
304
305         /* 
306          * Read HSCX A/B VSTR.
307          * Expected value for AVM A1 is 0x04 or 0x05 and for the
308          * AVM Fritz!Card is 0x05 in the least significant bits.
309          */
310
311         if( (((HSCX_READ(0, H_VSTR) & 0xf) != 0x5) &&
312              ((HSCX_READ(0, H_VSTR) & 0xf) != 0x4))     ||
313             (((HSCX_READ(1, H_VSTR) & 0xf) != 0x5) &&
314              ((HSCX_READ(1, H_VSTR) & 0xf) != 0x4)) )  
315         {
316                 kprintf("isic%d: HSCX VSTR test failed for AVM A1/Fritz\n",
317                         unit);
318                 kprintf("isic%d: HSC0: VSTR: %#x\n",
319                         unit, HSCX_READ(0, H_VSTR));
320                 kprintf("isic%d: HSC1: VSTR: %#x\n",
321                         unit, HSCX_READ(1, H_VSTR));
322                 return(ENXIO);
323         }                   
324
325         /* AVM A1 or Fritz! control register bits:      */
326         /*        read                write             */
327         /* 0x01  hscx irq*           RESET              */
328         /* 0x02  isac irq*           clear counter1     */
329         /* 0x04  counter irq*        clear counter2     */
330         /* 0x08  always 0            irq enable         */
331         /* 0x10  read test bit       set test bit       */
332         /* 0x20  always 0            unused             */
333
334         /*
335          * XXX the following test may be destructive, to prevent the
336          * worst case, we save the byte first, and in case the test
337          * fails, we write back the saved byte .....
338          */
339
340         t = rman_get_bustag(sc->sc_resources.io_base[0]);
341         h = rman_get_bushandle(sc->sc_resources.io_base[0]);
342
343         savebyte = bus_space_read_1(t, h, 0);
344         
345         /* write low to test bit */
346
347         bus_space_write_1(t, h, 0, 0x00);
348         
349         /* test bit and next higher and lower bit must be 0 */
350
351         if((byte = bus_space_read_1(t, h, 0) & 0x38) != 0x00)
352         {
353                 kprintf("isic%d: Error, probe-1 failed, 0x%02x should be 0x00 for AVM A1/Fritz!\n",
354                                 unit, byte);
355                 bus_space_write_1(t, h, 0, savebyte);
356                 return(ENXIO);
357         }
358
359         /* write high to test bit */
360
361         bus_space_write_1(t, h, 0, 0x10);
362         
363         /* test bit must be high, next higher and lower bit must be 0 */
364
365         if((byte = bus_space_read_1(t, h, 0) & 0x38) != 0x10)
366         {
367                 kprintf("isic%d: Error, probe-2 failed, 0x%02x should be 0x10 for AVM A1/Fritz!\n",
368                                 unit, byte);
369                 bus_space_write_1(t, h, 0, savebyte);
370                 return(ENXIO);
371         }
372         return(0);
373 }
374
375 /*---------------------------------------------------------------------------*
376  *      isic_attach_avma1 - attach AVM A1 and compatibles
377  *---------------------------------------------------------------------------*/
378 int
379 isic_attach_avma1(device_t dev)
380 {
381         size_t unit = device_get_unit(dev);
382         struct l1_softc *sc = &l1_sc[unit];
383         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.io_base[0]);
384         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
385
386         /* reset ISAC/HSCX */
387
388         bus_space_write_1(t, h, 0, 0x00);
389         DELAY(SEC_DELAY / 10);
390
391         bus_space_write_1(t, h, 0, AVM_CONF_WR_RESET);
392         DELAY(SEC_DELAY / 10);
393
394         bus_space_write_1(t, h, 0, 0x00);
395         DELAY(SEC_DELAY / 10);
396
397         /* setup IRQ */
398
399         bus_space_write_1(t, h, 1, sc->sc_irq);
400         DELAY(SEC_DELAY / 10);
401
402         /* enable IRQ, disable counter IRQ */
403
404         bus_space_write_1(t, h, 0, AVM_CONF_WR_IRQEN |
405                                 AVM_CONF_WR_CCH | AVM_CONF_WR_CCL);
406         DELAY(SEC_DELAY / 10);
407
408         return(0);
409 }
410
411 #endif /* NISIC > 0 && defined(AVM_A1) */