78e560cc3e173fee294db580968a6c6aabc97ec7
[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  * $DragonFly: src/sys/net/i4b/layer1/isic/i4b_tel_s0163.c,v 1.6 2006/12/22 23:44:56 swildner Exp $
42  *
43  *      last edit-date: [Wed Jan 24 09:27:40 2001]
44  *
45  *---------------------------------------------------------------------------*/
46
47 #include "use_isic.h"
48 #include "opt_i4b.h"
49
50 #if NISIC > 0 && defined(TEL_S0_16_3)
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/socket.h>
55 #include <net/if.h>
56
57 #include <net/i4b/include/machine/i4b_ioctl.h>
58 #include <net/i4b/include/machine/i4b_trace.h>
59
60 #include "../i4b_l1.h"
61 #include "i4b_isic.h"
62 #include "i4b_hscx.h"
63
64 static u_char intr_no[] = { 1, 1, 0, 2, 4, 6, 1, 1, 1, 0, 8, 10, 12, 1, 1, 14 };
65
66 #define ISAC_OFFS       0x400
67 #define HSCXA_OFFS      0xc00
68 #define HSCXB_OFFS      0x800
69
70 /*---------------------------------------------------------------------------*
71  *      Teles S0/16.3 read fifo routine
72  *---------------------------------------------------------------------------*/
73 static void
74 tels0163_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
75 {
76         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[what+1]);
77         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+1]);
78         bus_space_read_multi_1(t,h,0x1e,buf,size);
79 }
80
81 /*---------------------------------------------------------------------------*
82  *      Teles S0/16.3 write fifo routine
83  *---------------------------------------------------------------------------*/
84 static void
85 tels0163_write_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
86 {
87         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[what+1]);
88         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+1]);
89         bus_space_write_multi_1(t,h,0x1e,buf,size);
90 }
91
92 /*---------------------------------------------------------------------------*
93  *      Teles S0/16.3 ISAC put register routine
94  *---------------------------------------------------------------------------*/
95 static void
96 tels0163_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
97 {
98         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[what+1]);
99         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+1]);
100         bus_space_write_1(t,h,offs - 0x20,data);
101 }
102
103 /*---------------------------------------------------------------------------*
104  *      Teles S0/16.3 ISAC get register routine
105  *---------------------------------------------------------------------------*/
106 static u_int8_t
107 tels0163_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
108 {
109         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[what+1]);
110         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+1]);
111         return bus_space_read_1(t,h,offs - 0x20);
112 }
113
114 /*---------------------------------------------------------------------------*
115  *      isic_probe_s0163 - probe routine for Teles S0/16.3
116  *---------------------------------------------------------------------------*/
117 int
118 isic_probe_s0163(device_t dev)
119 {
120         size_t unit = device_get_unit(dev);     /* get unit */
121         struct l1_softc *sc = 0;        /* pointer to softc */
122         void *ih = 0;                   /* dummy */
123         bus_space_tag_t    t;           /* bus things */
124         bus_space_handle_t h;
125         u_int8_t b0,b1,b2;              /* signature */
126
127         /* check max unit range */
128
129         if(unit >= ISIC_MAXUNIT)
130         {
131                 kprintf("isic%d: Error, unit %d >= ISIC_MAXUNIT for Teles 16.3!\n",
132                                 unit, unit);
133                 return(ENXIO);  
134         }
135
136         sc = &l1_sc[unit];                      /* get pointer to softc */
137         sc->sc_unit = unit;                     /* set unit */
138
139         /* see if an io base was supplied */
140         
141         if(!(sc->sc_resources.io_base[0] =
142                         bus_alloc_resource(dev, SYS_RES_IOPORT,
143                                            &sc->sc_resources.io_rid[0],
144                                            0ul, ~0ul, 1, RF_ACTIVE)))
145         {
146                 kprintf("isic%d: Could not get iobase for Teles S0/16.3.\n",
147                                 unit);
148                 return(ENXIO);
149         }
150
151         /* set io base */
152
153         sc->sc_port = rman_get_start(sc->sc_resources.io_base[0]);
154         /* Release the resource -  re-allocate later with correct size  */
155         bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_resources.io_rid[0],
156                         sc->sc_resources.io_base[0]);
157         
158         switch(sc->sc_port)
159         {
160                 case 0xd80:
161                 case 0xe80:
162                 case 0xf80:
163                         break;
164                         
165                 case 0x180:
166                 case 0x280:
167                 case 0x380:
168                         kprintf("isic%d: Error, instead of using iobase 0x%x for your Teles S0/16.3,\n",
169                                 unit, sc->sc_port);
170                         kprintf("isic%d:        please use 0x%x in the kernel configuration file!\n",
171                                 unit, sc->sc_port+0xc00);                       
172                         isic_detach_common(dev);
173                         return(ENXIO);
174                         break;
175         
176                 default:
177                         kprintf("isic%d: Error, invalid iobase 0x%x specified for Teles S0/16.3!\n",
178                                 unit, sc->sc_port);
179                         isic_detach_common(dev);
180                         return(ENXIO);
181                         break;
182         }
183         
184         /* set io port resources */
185
186         sc->sc_resources.io_rid[0] = 0; 
187         bus_set_resource(dev, SYS_RES_IOPORT, 0, sc->sc_port, 0x20);
188         sc->sc_resources.io_base[0] =
189                 bus_alloc_resource(dev, SYS_RES_IOPORT,
190                                    &sc->sc_resources.io_rid[0],
191                                    0ul, ~0ul, 1, RF_ACTIVE);
192         if(!sc->sc_resources.io_base[0])
193         {
194                 kprintf("isic%d: Error allocating io at 0x%x for Teles S0/16.3!\n",
195                         unit, sc->sc_port);
196                 isic_detach_common(dev);
197                 return ENXIO;
198         }
199         sc->sc_resources.io_rid[1] = 1; 
200         bus_set_resource(dev, SYS_RES_IOPORT, 1,
201                 sc->sc_port-ISAC_OFFS, 0x20);
202         sc->sc_resources.io_base[1] =
203                 bus_alloc_resource(dev, SYS_RES_IOPORT,
204                                    &sc->sc_resources.io_rid[1],
205                                    0ul, ~0ul, 1, RF_ACTIVE);
206         if(!sc->sc_resources.io_base[1])
207         {
208                 kprintf("isic%d: Error allocating io at 0x%x for Teles S0/16.3!\n",
209                         unit, sc->sc_port-ISAC_OFFS);
210                 isic_detach_common(dev);
211                 return ENXIO;
212         }
213         
214         sc->sc_resources.io_rid[2] = 2;
215         bus_set_resource(dev, SYS_RES_IOPORT, 2,
216                 sc->sc_port-HSCXA_OFFS, 0x20);
217         sc->sc_resources.io_base[2] =
218                 bus_alloc_resource(dev, SYS_RES_IOPORT,
219                                    &sc->sc_resources.io_rid[2],
220                                    0ul, ~0ul, 1, RF_ACTIVE);
221         if(!sc->sc_resources.io_base[2])
222         {
223                 kprintf("isic%d: Error allocating io at 0x%x for Teles S0/16.3!\n",
224                         unit, sc->sc_port-HSCXA_OFFS);
225                 isic_detach_common(dev);
226                 return ENXIO;
227         }
228
229         sc->sc_resources.io_rid[3] = 3;
230         bus_set_resource(dev, SYS_RES_IOPORT, 3,
231                 sc->sc_port-HSCXB_OFFS, 0x20);
232         sc->sc_resources.io_base[3] =
233                 bus_alloc_resource(dev, SYS_RES_IOPORT,
234                                    &sc->sc_resources.io_rid[3],
235                                    0ul, ~0ul, 1, RF_ACTIVE);
236         if(!sc->sc_resources.io_base[3])
237         {
238                 kprintf("isic%d: Error allocating io at 0x%x for Teles S0/16.3!\n",
239                         unit, sc->sc_port-HSCXB_OFFS);
240                 isic_detach_common(dev);
241                 return ENXIO;
242         }
243
244         /* setup access routines */
245
246         sc->clearirq = NULL;
247         sc->readreg = tels0163_read_reg;
248         sc->writereg = tels0163_write_reg;
249
250         sc->readfifo = tels0163_read_fifo;
251         sc->writefifo = tels0163_write_fifo;
252
253         /* setup card type */
254         
255         sc->sc_cardtyp= CARD_TYPEP_16_3;
256
257         /* setup IOM bus type */
258         
259         sc->sc_bustyp = BUS_TYPE_IOM2;
260
261         sc->sc_ipac = 0;
262         sc->sc_bfifolen = HSCX_FIFO_LEN;
263
264         t = rman_get_bustag(sc->sc_resources.io_base[0]);
265         h = rman_get_bushandle(sc->sc_resources.io_base[0]);
266
267         b0 = bus_space_read_1(t, h, 0);
268         b1 = bus_space_read_1(t, h, 1);
269         b2 = bus_space_read_1(t, h, 2);
270         
271         if ( b0 != 0x51 && b0 != 0x10 ) {
272                 kprintf("isic%d: Error, signature 1 0x%x != 0x51 or 0x10 for Teles S0/16.3!\n",
273                         unit, b0);
274                 isic_detach_common(dev);
275                 return ENXIO;
276         }
277         
278         if ( b1 != 0x93 ) {
279                 kprintf("isic%d: Error, signature 2 0x%x != 0x93 for Teles S0/16.3!\n",
280                         unit, b1);
281                 isic_detach_common(dev);
282                 return ENXIO;
283         }
284         if (( b2 != 0x1c ) && ( b2 != 0x1f )) {
285                 kprintf("isic%d: Error, signature 3 0x%x != (0x1c || 0x1f) for Teles S0/16.3!\n",
286                         unit, b2);
287                 isic_detach_common(dev);
288                 return ENXIO;   
289         }
290         
291         /* 
292          * Read HSCX A/B VSTR.  Expected value for the S0/16.3 card is
293          * 0x05 or 0x04 (for older 16.3's) in the least significant bits.
294          */
295          
296         if( (((HSCX_READ(0, H_VSTR) & 0xf) != 0x5) &&
297              ((HSCX_READ(0, H_VSTR) & 0xf) != 0x4))     ||
298             (((HSCX_READ(1, H_VSTR) & 0xf) != 0x5) &&
299              ((HSCX_READ(1, H_VSTR) & 0xf) != 0x4)) )  
300         {
301                 kprintf("isic%d: HSCX VSTR test failed for Teles S0/16.3\n",
302                         unit);
303                 kprintf("isic%d: HSC0: VSTR: %#x\n",
304                         unit, HSCX_READ(0, H_VSTR));
305                 kprintf("isic%d: HSC1: VSTR: %#x\n",
306                         unit, HSCX_READ(1, H_VSTR));
307                 isic_detach_common(dev);
308                 return (ENXIO);
309         }                   
310
311         /* get our irq */
312
313         if(!(sc->sc_resources.irq =
314                 bus_alloc_resource(dev, SYS_RES_IRQ,
315                 &sc->sc_resources.irq_rid,
316                 0ul, ~0ul, 1, RF_ACTIVE)))
317         {
318                 kprintf("isic%d: Could not get IRQ for Teles S0/16.3.\n",unit);
319                 isic_detach_common(dev);
320                 return ENXIO;
321         }
322
323         /* get the irq number */
324         sc->sc_irq = rman_get_start(sc->sc_resources.irq);
325
326         switch(sc->sc_irq)
327         {
328                 case 2:
329                 case 9:
330                 case 5:
331                 case 10:
332                 case 12:
333                 case 15:
334                         break;
335
336                 default:
337                         kprintf("isic%d: Error, invalid IRQ [%d] specified for Teles S0/16.3!\n",
338                                 unit, sc->sc_irq);
339                         isic_detach_common(dev);
340                         return(ENXIO);
341                         break;
342         }
343
344         /* register interupt routine */
345         bus_setup_intr(dev, sc->sc_resources.irq,
346                        0, (void(*)(void *))(isicintr),
347                        sc, &ih, NULL);
348
349         return (0);
350 }
351
352 /*---------------------------------------------------------------------------*
353  *      isic_attach_s0163 - attach Teles S0/16.3 and compatibles
354  *---------------------------------------------------------------------------*/
355 int
356 isic_attach_s0163(device_t dev)
357 {
358         unsigned int unit = device_get_unit(dev);       /* get unit */
359         struct l1_softc *sc = &l1_sc[unit];
360         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
361         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
362
363         /* configure IRQ */
364         
365         DELAY(SEC_DELAY / 10);
366         bus_space_write_1(t, h, 4, intr_no[sc->sc_irq]);
367
368         DELAY(SEC_DELAY / 10);
369         bus_space_write_1(t, h, 4, intr_no[sc->sc_irq] | 0x01);
370
371         return (0);
372 }
373
374 #endif /* ISIC > 0 */
375