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