kernel: Use NULL for pointers.
[dragonfly.git] / sys / net / i4b / layer1 / isic / i4b_tel_s0163.c
1 /*
2  *   Copyright (c) 1996 Arne Helme. All rights reserved.
3  *
4  *   Copyright (c) 1996 Gary Jennejohn. All rights reserved. 
5  *
6  *   Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *   1. Redistributions of source code must retain the above copyright
13  *      notice, this list of conditions and the following disclaimer.
14  *   2. Redistributions in binary form must reproduce the above copyright
15  *      notice, this list of conditions and the following disclaimer in the
16  *      documentation and/or other materials provided with the distribution.
17  *   3. Neither the name of the author nor the names of any co-contributors
18  *      may be used to endorse or promote products derived from this software
19  *      without specific prior written permission.
20  *   4. Altered versions must be plainly marked as such, and must not be
21  *      misrepresented as being the original software and/or documentation.
22  *   
23  *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  *   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  *   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  *   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  *   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  *   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  *   SUCH DAMAGE.
34  *
35  *---------------------------------------------------------------------------
36  *
37  *      isic - I4B Siemens ISDN Chipset Driver for Teles S0/16.3
38  *      ========================================================
39  *
40  * $FreeBSD: src/sys/i4b/layer1/isic/i4b_tel_s0163.c,v 1.5.2.1 2001/08/10 14:08:38 obrien Exp $
41  *
42  *      last edit-date: [Wed Jan 24 09:27:40 2001]
43  *
44  *---------------------------------------------------------------------------*/
45
46 #include "use_isic.h"
47 #include "opt_i4b.h"
48
49 #if NISIC > 0 && defined(TEL_S0_16_3)
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/socket.h>
54 #include <net/if.h>
55
56 #include <net/i4b/include/machine/i4b_ioctl.h>
57 #include <net/i4b/include/machine/i4b_trace.h>
58
59 #include "../i4b_l1.h"
60 #include "i4b_isic.h"
61 #include "i4b_hscx.h"
62
63 static u_char intr_no[] = { 1, 1, 0, 2, 4, 6, 1, 1, 1, 0, 8, 10, 12, 1, 1, 14 };
64
65 #define ISAC_OFFS       0x400
66 #define HSCXA_OFFS      0xc00
67 #define HSCXB_OFFS      0x800
68
69 /*---------------------------------------------------------------------------*
70  *      Teles S0/16.3 read fifo routine
71  *---------------------------------------------------------------------------*/
72 static void
73 tels0163_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
74 {
75         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[what+1]);
76         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+1]);
77         bus_space_read_multi_1(t,h,0x1e,buf,size);
78 }
79
80 /*---------------------------------------------------------------------------*
81  *      Teles S0/16.3 write fifo routine
82  *---------------------------------------------------------------------------*/
83 static void
84 tels0163_write_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
85 {
86         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[what+1]);
87         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+1]);
88         bus_space_write_multi_1(t,h,0x1e,buf,size);
89 }
90
91 /*---------------------------------------------------------------------------*
92  *      Teles S0/16.3 ISAC put register routine
93  *---------------------------------------------------------------------------*/
94 static void
95 tels0163_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
96 {
97         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[what+1]);
98         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+1]);
99         bus_space_write_1(t,h,offs - 0x20,data);
100 }
101
102 /*---------------------------------------------------------------------------*
103  *      Teles S0/16.3 ISAC get register routine
104  *---------------------------------------------------------------------------*/
105 static u_int8_t
106 tels0163_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
107 {
108         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[what+1]);
109         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+1]);
110         return bus_space_read_1(t,h,offs - 0x20);
111 }
112
113 /*---------------------------------------------------------------------------*
114  *      isic_probe_s0163 - probe routine for Teles S0/16.3
115  *---------------------------------------------------------------------------*/
116 int
117 isic_probe_s0163(device_t dev)
118 {
119         size_t unit = device_get_unit(dev);     /* get unit */
120         struct l1_softc *sc = NULL;     /* pointer to softc */
121         void *ih = NULL;                        /* dummy */
122         bus_space_tag_t    t;           /* bus things */
123         bus_space_handle_t h;
124         u_int8_t b0,b1,b2;              /* signature */
125
126         /* check max unit range */
127
128         if(unit >= ISIC_MAXUNIT)
129         {
130                 kprintf("isic%d: Error, unit %d >= ISIC_MAXUNIT for Teles 16.3!\n",
131                                 unit, unit);
132                 return(ENXIO);  
133         }
134
135         sc = &l1_sc[unit];                      /* get pointer to softc */
136         sc->sc_unit = unit;                     /* set unit */
137
138         /* see if an io base was supplied */
139         
140         if(!(sc->sc_resources.io_base[0] =
141                         bus_alloc_resource(dev, SYS_RES_IOPORT,
142                                            &sc->sc_resources.io_rid[0],
143                                            0ul, ~0ul, 1, RF_ACTIVE)))
144         {
145                 kprintf("isic%d: Could not get iobase for Teles S0/16.3.\n",
146                                 unit);
147                 return(ENXIO);
148         }
149
150         /* set io base */
151
152         sc->sc_port = rman_get_start(sc->sc_resources.io_base[0]);
153         /* Release the resource -  re-allocate later with correct size  */
154         bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_resources.io_rid[0],
155                         sc->sc_resources.io_base[0]);
156         
157         switch(sc->sc_port)
158         {
159                 case 0xd80:
160                 case 0xe80:
161                 case 0xf80:
162                         break;
163                         
164                 case 0x180:
165                 case 0x280:
166                 case 0x380:
167                         kprintf("isic%d: Error, instead of using iobase 0x%x for your Teles S0/16.3,\n",
168                                 unit, sc->sc_port);
169                         kprintf("isic%d:        please use 0x%x in the kernel configuration file!\n",
170                                 unit, sc->sc_port+0xc00);                       
171                         isic_detach_common(dev);
172                         return(ENXIO);
173                         break;
174         
175                 default:
176                         kprintf("isic%d: Error, invalid iobase 0x%x specified for Teles S0/16.3!\n",
177                                 unit, sc->sc_port);
178                         isic_detach_common(dev);
179                         return(ENXIO);
180                         break;
181         }
182         
183         /* set io port resources */
184
185         sc->sc_resources.io_rid[0] = 0; 
186         bus_set_resource(dev, SYS_RES_IOPORT, 0, sc->sc_port, 0x20, -1);
187         sc->sc_resources.io_base[0] =
188                 bus_alloc_resource(dev, SYS_RES_IOPORT,
189                                    &sc->sc_resources.io_rid[0],
190                                    0ul, ~0ul, 1, RF_ACTIVE);
191         if(!sc->sc_resources.io_base[0])
192         {
193                 kprintf("isic%d: Error allocating io at 0x%x for Teles S0/16.3!\n",
194                         unit, sc->sc_port);
195                 isic_detach_common(dev);
196                 return ENXIO;
197         }
198         sc->sc_resources.io_rid[1] = 1; 
199         bus_set_resource(dev, SYS_RES_IOPORT, 1,
200                 sc->sc_port-ISAC_OFFS, 0x20, -1);
201         sc->sc_resources.io_base[1] =
202                 bus_alloc_resource(dev, SYS_RES_IOPORT,
203                                    &sc->sc_resources.io_rid[1],
204                                    0ul, ~0ul, 1, RF_ACTIVE);
205         if(!sc->sc_resources.io_base[1])
206         {
207                 kprintf("isic%d: Error allocating io at 0x%x for Teles S0/16.3!\n",
208                         unit, sc->sc_port-ISAC_OFFS);
209                 isic_detach_common(dev);
210                 return ENXIO;
211         }
212         
213         sc->sc_resources.io_rid[2] = 2;
214         bus_set_resource(dev, SYS_RES_IOPORT, 2,
215                 sc->sc_port-HSCXA_OFFS, 0x20, -1);
216         sc->sc_resources.io_base[2] =
217                 bus_alloc_resource(dev, SYS_RES_IOPORT,
218                                    &sc->sc_resources.io_rid[2],
219                                    0ul, ~0ul, 1, RF_ACTIVE);
220         if(!sc->sc_resources.io_base[2])
221         {
222                 kprintf("isic%d: Error allocating io at 0x%x for Teles S0/16.3!\n",
223                         unit, sc->sc_port-HSCXA_OFFS);
224                 isic_detach_common(dev);
225                 return ENXIO;
226         }
227
228         sc->sc_resources.io_rid[3] = 3;
229         bus_set_resource(dev, SYS_RES_IOPORT, 3,
230                 sc->sc_port-HSCXB_OFFS, 0x20, -1);
231         sc->sc_resources.io_base[3] =
232                 bus_alloc_resource(dev, SYS_RES_IOPORT,
233                                    &sc->sc_resources.io_rid[3],
234                                    0ul, ~0ul, 1, RF_ACTIVE);
235         if(!sc->sc_resources.io_base[3])
236         {
237                 kprintf("isic%d: Error allocating io at 0x%x for Teles S0/16.3!\n",
238                         unit, sc->sc_port-HSCXB_OFFS);
239                 isic_detach_common(dev);
240                 return ENXIO;
241         }
242
243         /* setup access routines */
244
245         sc->clearirq = NULL;
246         sc->readreg = tels0163_read_reg;
247         sc->writereg = tels0163_write_reg;
248
249         sc->readfifo = tels0163_read_fifo;
250         sc->writefifo = tels0163_write_fifo;
251
252         /* setup card type */
253         
254         sc->sc_cardtyp= CARD_TYPEP_16_3;
255
256         /* setup IOM bus type */
257         
258         sc->sc_bustyp = BUS_TYPE_IOM2;
259
260         sc->sc_ipac = 0;
261         sc->sc_bfifolen = HSCX_FIFO_LEN;
262
263         t = rman_get_bustag(sc->sc_resources.io_base[0]);
264         h = rman_get_bushandle(sc->sc_resources.io_base[0]);
265
266         b0 = bus_space_read_1(t, h, 0);
267         b1 = bus_space_read_1(t, h, 1);
268         b2 = bus_space_read_1(t, h, 2);
269         
270         if ( b0 != 0x51 && b0 != 0x10 ) {
271                 kprintf("isic%d: Error, signature 1 0x%x != 0x51 or 0x10 for Teles S0/16.3!\n",
272                         unit, b0);
273                 isic_detach_common(dev);
274                 return ENXIO;
275         }
276         
277         if ( b1 != 0x93 ) {
278                 kprintf("isic%d: Error, signature 2 0x%x != 0x93 for Teles S0/16.3!\n",
279                         unit, b1);
280                 isic_detach_common(dev);
281                 return ENXIO;
282         }
283         if (( b2 != 0x1c ) && ( b2 != 0x1f )) {
284                 kprintf("isic%d: Error, signature 3 0x%x != (0x1c || 0x1f) for Teles S0/16.3!\n",
285                         unit, b2);
286                 isic_detach_common(dev);
287                 return ENXIO;   
288         }
289         
290         /* 
291          * Read HSCX A/B VSTR.  Expected value for the S0/16.3 card is
292          * 0x05 or 0x04 (for older 16.3's) in the least significant bits.
293          */
294          
295         if( (((HSCX_READ(0, H_VSTR) & 0xf) != 0x5) &&
296              ((HSCX_READ(0, H_VSTR) & 0xf) != 0x4))     ||
297             (((HSCX_READ(1, H_VSTR) & 0xf) != 0x5) &&
298              ((HSCX_READ(1, H_VSTR) & 0xf) != 0x4)) )  
299         {
300                 kprintf("isic%d: HSCX VSTR test failed for Teles S0/16.3\n",
301                         unit);
302                 kprintf("isic%d: HSC0: VSTR: %#x\n",
303                         unit, HSCX_READ(0, H_VSTR));
304                 kprintf("isic%d: HSC1: VSTR: %#x\n",
305                         unit, HSCX_READ(1, H_VSTR));
306                 isic_detach_common(dev);
307                 return (ENXIO);
308         }                   
309
310         /* get our irq */
311
312         if(!(sc->sc_resources.irq =
313                 bus_alloc_resource(dev, SYS_RES_IRQ,
314                 &sc->sc_resources.irq_rid,
315                 0ul, ~0ul, 1, RF_ACTIVE)))
316         {
317                 kprintf("isic%d: Could not get IRQ for Teles S0/16.3.\n",unit);
318                 isic_detach_common(dev);
319                 return ENXIO;
320         }
321
322         /* get the irq number */
323         sc->sc_irq = rman_get_start(sc->sc_resources.irq);
324
325         switch(sc->sc_irq)
326         {
327                 case 2:
328                 case 9:
329                 case 5:
330                 case 10:
331                 case 12:
332                 case 15:
333                         break;
334
335                 default:
336                         kprintf("isic%d: Error, invalid IRQ [%d] specified for Teles S0/16.3!\n",
337                                 unit, sc->sc_irq);
338                         isic_detach_common(dev);
339                         return(ENXIO);
340                         break;
341         }
342
343         /* register interupt routine */
344         bus_setup_intr(dev, sc->sc_resources.irq,
345                        0, (void(*)(void *))(isicintr),
346                        sc, &ih, NULL);
347
348         return (0);
349 }
350
351 /*---------------------------------------------------------------------------*
352  *      isic_attach_s0163 - attach Teles S0/16.3 and compatibles
353  *---------------------------------------------------------------------------*/
354 int
355 isic_attach_s0163(device_t dev)
356 {
357         unsigned int unit = device_get_unit(dev);       /* get unit */
358         struct l1_softc *sc = &l1_sc[unit];
359         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
360         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
361
362         /* configure IRQ */
363         
364         DELAY(SEC_DELAY / 10);
365         bus_space_write_1(t, h, 4, intr_no[sc->sc_irq]);
366
367         DELAY(SEC_DELAY / 10);
368         bus_space_write_1(t, h, 4, intr_no[sc->sc_irq] | 0x01);
369
370         return (0);
371 }
372
373 #endif /* ISIC > 0 */
374