]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i4b/layer1/i4b_avm_a1.c
This commit was generated by cvs2svn to compensate for changes in r63516,
[FreeBSD/FreeBSD.git] / sys / i4b / layer1 / i4b_avm_a1.c
1 /*
2  *   Copyright (c) 1996 Andrew Gordon. All rights reserved.
3  *
4  *   Copyright (c) 1997, 1999 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  *      $Id: i4b_avm_a1.c,v 1.2 1999/12/13 21:25:26 hm Exp $ 
39  *
40  * $FreeBSD$
41  *
42  *      last edit-date: [Mon Dec 13 21:58:36 1999]
43  *
44  *---------------------------------------------------------------------------*/
45
46 #include "isic.h"
47 #include "opt_i4b.h"
48
49 #if NISIC > 0 && defined(AVM_A1)
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/mbuf.h>
54 #include <sys/socket.h>
55
56 #include <net/if.h>
57
58 #include <machine/clock.h>
59
60 #include <machine/i4b_debug.h>
61 #include <machine/i4b_ioctl.h>
62
63 #include <i4b/include/i4b_global.h>
64
65 #include <i4b/layer1/i4b_l1.h>
66 #include <i4b/layer1/i4b_isac.h>
67 #include <i4b/layer1/i4b_hscx.h>
68
69 /*---------------------------------------------------------------------------*
70  *      AVM A1 and AVM Fritz! Card special registers
71  *---------------------------------------------------------------------------*/
72
73 #define AVM_CONF_REG    0x1800          /* base offset for config register */
74 #define AVM_CONF_IRQ    0x1801          /* base offset for IRQ register    */
75                                         /* config register write           */
76 #define  AVM_CONF_WR_RESET      0x01    /* 1 = RESET ISAC and HSCX         */
77 #define  AVM_CONF_WR_CCL        0x02    /* 1 = clear counter low nibble    */
78 #define  AVM_CONF_WR_CCH        0x04    /* 1 = clear counter high nibble   */
79 #define  AVM_CONF_WR_IRQEN      0x08    /* 1 = enable IRQ                  */
80 #define  AVM_CONF_WR_TEST       0x10    /* test bit                        */
81                                         /* config register read            */
82 #define  AVM_CONF_RD_IIRQ       0x01    /* 0 = ISAC IRQ active             */
83 #define  AVM_CONF_RD_HIRQ       0x02    /* 0 = HSCX IRQ active             */
84 #define  AVM_CONF_RD_CIRQ       0x04    /* 0 = counter IRQ active          */
85 #define  AVM_CONF_RD_ZER1       0x08    /* unused, always read 0           */
86 #define  AVM_CONF_RD_TEST       0x10    /* test bit read back              */
87 #define  AVM_CONF_RD_ZER2       0x20    /* unused, always read 0           */
88
89 #define AVM_ISAC_R_OFFS         (0x1400-0x20)
90 #define AVM_HSCXA_R_OFFS        (0x400-0x20)
91 #define AVM_HSCXB_R_OFFS        (0xc00-0x20)
92 #define AVM_ISAC_F_OFFS         (0x1400-0x20-0x3e0)
93 #define AVM_HSCXA_F_OFFS        (0x400-0x20-0x3e0)
94 #define AVM_HSCXB_F_OFFS        (0xc00-0x20-0x3e0)
95
96 /*---------------------------------------------------------------------------*
97  *      AVM read fifo routine
98  *---------------------------------------------------------------------------*/
99 static void
100 avma1_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
101 {
102         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.io_base[what+4]);
103         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+4]);
104         bus_space_read_multi_1(t, h, 0, buf, size);
105 }
106
107 /*---------------------------------------------------------------------------*
108  *      AVM write fifo routine
109  *---------------------------------------------------------------------------*/
110 static void
111 avma1_write_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
112 {
113         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.io_base[what+4]);
114         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+4]);
115         bus_space_write_multi_1(t, h, 0, (u_int8_t*)buf, size);
116 }
117
118 /*---------------------------------------------------------------------------*
119  *      AVM write register routine
120  *---------------------------------------------------------------------------*/
121 static void
122 avma1_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
123 {
124         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.io_base[what+1]);
125         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+1]);
126         bus_space_write_1(t, h, offs, data);
127 }
128
129 /*---------------------------------------------------------------------------*
130  *      AVM read register routine
131  *---------------------------------------------------------------------------*/
132 static u_int8_t
133 avma1_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
134 {
135         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.io_base[what+1]);
136         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+1]);
137         return bus_space_read_1(t, h, offs);
138 }
139
140 /*---------------------------------------------------------------------------*
141  *      allocate an io port
142  *---------------------------------------------------------------------------*/
143 static int
144 isic_alloc_port(device_t dev, int rid, u_int base, u_int len)
145
146         size_t unit = device_get_unit(dev);
147         struct l1_softc *sc = &l1_sc[unit];
148
149         sc->sc_resources.io_rid[rid] = rid;
150
151         bus_set_resource(dev, SYS_RES_IOPORT, rid, base, len);
152
153         if(!(sc->sc_resources.io_base[rid] =
154                 bus_alloc_resource(dev, SYS_RES_IOPORT,
155                                    &sc->sc_resources.io_rid[rid],
156                                    0ul, ~0ul, 1, RF_ACTIVE)))
157         {
158                 printf("isic%d: Error, failed to reserve io #%d!\n", unit, rid);
159                 isic_detach_common(dev);
160                 return(ENXIO);
161         }
162         return(0);
163 }
164
165 /*---------------------------------------------------------------------------*
166  *      isic_probe_avma1 - probe for AVM A1 and compatibles
167  *---------------------------------------------------------------------------*/
168 int
169 isic_probe_avma1(device_t dev)
170 {
171         size_t unit = device_get_unit(dev);     /* get unit */
172         struct l1_softc *sc = 0;        /* pointer to softc */
173         void *ih = 0;                   /* dummy */
174         bus_space_tag_t    t;           /* bus things */
175         bus_space_handle_t h;
176         u_char savebyte;
177         u_char byte;
178
179         /* check max unit range */
180
181         if(unit >= ISIC_MAXUNIT)
182         {
183                 printf("isic%d: Error, unit %d >= ISIC_MAXUNIT for AVM A1/Fritz!\n",
184                                 unit, unit);
185                 return(ENXIO);  
186         }
187
188         sc = &l1_sc[unit];                      /* get pointer to softc */
189         sc->sc_unit = unit;                     /* set unit */
190         sc->sc_flags = FLAG_AVM_A1;             /* set flags */
191
192         /* see if an io base was supplied */
193         
194         if(!(sc->sc_resources.io_base[0] =
195                         bus_alloc_resource(dev, SYS_RES_IOPORT,
196                                            &sc->sc_resources.io_rid[0],
197                                            0ul, ~0ul, 1, RF_ACTIVE)))
198         {
199                 printf("isic%d: Could not get iobase for AVM A1/Fritz!\n",
200                                 unit);
201                 return(ENXIO);
202         }
203
204         /* set io base */
205
206         sc->sc_port = rman_get_start(sc->sc_resources.io_base[0]);
207         
208         /* release io base */
209         
210         bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_resources.io_rid[0],
211                         sc->sc_resources.io_base[0]);
212
213         switch(sc->sc_port)
214         {
215                 case 0x200:
216                 case 0x240:
217                 case 0x300:
218                 case 0x340:             
219                         break;
220                         
221                 default:
222                         printf("isic%d: Error, invalid iobase 0x%x specified for AVM A1/Fritz!\n",
223                                 unit, sc->sc_port);
224                         return(ENXIO);
225                         break;
226         }
227
228         if(isic_alloc_port(dev, 0, sc->sc_port+AVM_CONF_REG, 0x20))
229                 return(ENXIO);
230
231         if(isic_alloc_port(dev, 1, sc->sc_port+AVM_ISAC_R_OFFS, 0x20))
232                 return(ENXIO);
233
234         if(isic_alloc_port(dev, 2, sc->sc_port+AVM_HSCXA_R_OFFS, 0x20))
235                 return(ENXIO);
236
237         if(isic_alloc_port(dev, 3, sc->sc_port+AVM_HSCXB_R_OFFS, 0x20))
238                 return(ENXIO);
239
240         if(isic_alloc_port(dev, 4, sc->sc_port+AVM_ISAC_F_OFFS, 0x20))
241                 return(ENXIO);
242
243         if(isic_alloc_port(dev, 5, sc->sc_port+AVM_HSCXA_F_OFFS, 0x20))
244                 return(ENXIO);
245
246         if(isic_alloc_port(dev, 6, sc->sc_port+AVM_HSCXB_F_OFFS, 0x20))
247                 return(ENXIO);
248
249         /* get our irq */
250
251         if(!(sc->sc_resources.irq =
252                 bus_alloc_resource(dev, SYS_RES_IRQ,
253                                    &sc->sc_resources.irq_rid,
254                                    0ul, ~0ul, 1, RF_ACTIVE)))
255         {
256                 printf("isic%d: Could not get an irq for AVM A1/Fritz!\n",unit);
257                 isic_detach_common(dev);
258                 return ENXIO;
259         }
260
261         /* get the irq number */
262         sc->sc_irq = rman_get_start(sc->sc_resources.irq);
263
264         /* register interupt routine */
265         bus_setup_intr(dev, sc->sc_resources.irq, INTR_TYPE_NET,
266                         (void(*)(void *))(isicintr),
267                         sc, &ih);
268
269         /* check IRQ validity */
270
271         switch(sc->sc_irq)
272         {
273                 case 3:
274                 case 4:
275                 case 5:
276                 case 6:
277                 case 7:
278                 case 8:
279                 case 10:
280                 case 11:
281                 case 12:
282                 case 13:
283                 case 14:
284                 case 15:
285                         break;
286                         
287                 default:
288                         printf("isic%d: Error, invalid IRQ [%d] specified for AVM A1/Fritz!\n",
289                                 unit, sc->sc_irq);
290                         isic_detach_common(dev);
291                         return(ENXIO);
292                         break;
293         }               
294
295         sc->clearirq = NULL;
296         sc->readreg = avma1_read_reg;
297         sc->writereg = avma1_write_reg;
298
299         sc->readfifo = avma1_read_fifo;
300         sc->writefifo = avma1_write_fifo;
301
302         /* setup card type */
303
304         sc->sc_cardtyp = CARD_TYPEP_AVMA1;
305
306         /* setup IOM bus type */
307         
308         sc->sc_bustyp = BUS_TYPE_IOM2;
309
310         sc->sc_ipac = 0;
311         sc->sc_bfifolen = HSCX_FIFO_LEN;
312
313         /* 
314          * Read HSCX A/B VSTR.
315          * Expected value for AVM A1 is 0x04 or 0x05 and for the
316          * AVM Fritz!Card is 0x05 in the least significant bits.
317          */
318
319         if( (((HSCX_READ(0, H_VSTR) & 0xf) != 0x5) &&
320              ((HSCX_READ(0, H_VSTR) & 0xf) != 0x4))     ||
321             (((HSCX_READ(1, H_VSTR) & 0xf) != 0x5) &&
322              ((HSCX_READ(1, H_VSTR) & 0xf) != 0x4)) )  
323         {
324                 printf("isic%d: HSCX VSTR test failed for AVM A1/Fritz\n",
325                         unit);
326                 printf("isic%d: HSC0: VSTR: %#x\n",
327                         unit, HSCX_READ(0, H_VSTR));
328                 printf("isic%d: HSC1: VSTR: %#x\n",
329                         unit, HSCX_READ(1, H_VSTR));
330                 return(ENXIO);
331         }                   
332
333         /* AVM A1 or Fritz! control register bits:      */
334         /*        read                write             */
335         /* 0x01  hscx irq*           RESET              */
336         /* 0x02  isac irq*           clear counter1     */
337         /* 0x04  counter irq*        clear counter2     */
338         /* 0x08  always 0            irq enable         */
339         /* 0x10  read test bit       set test bit       */
340         /* 0x20  always 0            unused             */
341
342         /*
343          * XXX the following test may be destructive, to prevent the
344          * worst case, we save the byte first, and in case the test
345          * fails, we write back the saved byte .....
346          */
347
348         t = rman_get_bustag(sc->sc_resources.io_base[0]);
349         h = rman_get_bushandle(sc->sc_resources.io_base[0]);
350
351         savebyte = bus_space_read_1(t, h, 0);
352         
353         /* write low to test bit */
354
355         bus_space_write_1(t, h, 0, 0x00);
356         
357         /* test bit and next higher and lower bit must be 0 */
358
359         if((byte = bus_space_read_1(t, h, 0) & 0x38) != 0x00)
360         {
361                 printf("isic%d: Error, probe-1 failed, 0x%02x should be 0x00 for AVM A1/Fritz!\n",
362                                 unit, byte);
363                 bus_space_write_1(t, h, 0, savebyte);
364                 return(ENXIO);
365         }
366
367         /* write high to test bit */
368
369         bus_space_write_1(t, h, 0, 0x10);
370         
371         /* test bit must be high, next higher and lower bit must be 0 */
372
373         if((byte = bus_space_read_1(t, h, 0) & 0x38) != 0x10)
374         {
375                 printf("isic%d: Error, probe-2 failed, 0x%02x should be 0x10 for AVM A1/Fritz!\n",
376                                 unit, byte);
377                 bus_space_write_1(t, h, 0, savebyte);
378                 return(ENXIO);
379         }
380         return(0);
381 }
382
383 /*---------------------------------------------------------------------------*
384  *      isic_attach_avma1 - attach AVM A1 and compatibles
385  *---------------------------------------------------------------------------*/
386 int
387 isic_attach_avma1(device_t dev)
388 {
389         size_t unit = device_get_unit(dev);
390         struct l1_softc *sc = &l1_sc[unit];
391         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.io_base[0]);
392         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
393
394         /* reset ISAC/HSCX */
395
396         bus_space_write_1(t, h, 0, 0x00);
397         DELAY(SEC_DELAY / 10);
398
399         bus_space_write_1(t, h, 0, AVM_CONF_WR_RESET);
400         DELAY(SEC_DELAY / 10);
401
402         bus_space_write_1(t, h, 0, 0x00);
403         DELAY(SEC_DELAY / 10);
404
405         /* setup IRQ */
406
407         bus_space_write_1(t, h, 1, sc->sc_irq);
408         DELAY(SEC_DELAY / 10);
409
410         /* enable IRQ, disable counter IRQ */
411
412         bus_space_write_1(t, h, 0, AVM_CONF_WR_IRQEN |
413                                 AVM_CONF_WR_CCH | AVM_CONF_WR_CCL);
414         DELAY(SEC_DELAY / 10);
415
416         return(0);
417 }
418
419 #endif /* NISIC > 0 && defined(AVM_A1) */