Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / net / i4b / layer1 / isic / i4b_tel_s016.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 and clones
38  *      =================================================================
39  *
40  * $FreeBSD: src/sys/i4b/layer1/isic/i4b_tel_s016.c,v 1.5.2.1 2001/08/10 14:08:38 obrien Exp $
41  *
42  *      last edit-date: [Wed Jan 24 09:27:24 2001]
43  *
44  *---------------------------------------------------------------------------*/
45
46 #include "isic.h"
47 #include "opt_i4b.h"
48
49 #if NISIC > 0 && defined(TEL_S0_16)
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/socket.h>
54 #include <net/if.h>
55
56 #include <machine/md_var.h>
57 #include <machine/i4b_ioctl.h>
58 #include <machine/i4b_trace.h>
59
60 #include <i4b/layer1/i4b_l1.h>
61 #include <i4b/layer1/isic/i4b_isic.h>
62 #include <i4b/layer1/isic/i4b_hscx.h>
63
64 #define TELES_S016_MEMSIZE 0x1000
65
66 static u_char intr_no[] = { 1, 1, 0, 2, 4, 6, 1, 1, 1, 0, 8, 10, 12, 1, 1, 14 };
67 static const bus_size_t offset[] = { 0x100, 0x180, 0x1c0 };
68
69 /*---------------------------------------------------------------------------*
70  *      Teles S0/16 write register routine
71  *---------------------------------------------------------------------------*/
72 static void
73 tels016_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
74 {
75         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.mem);
76         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.mem);
77
78         offs += offset[what];
79
80         if (offs & 0x01)
81                 offs |= 0x200;
82
83         bus_space_write_1(t, h, offs, data);
84 }
85
86 /*---------------------------------------------------------------------------*
87  *      Teles S0/16 read register routine
88  *---------------------------------------------------------------------------*/
89 static u_int8_t
90 tels016_read_reg(struct l1_softc *sc,   int what, bus_size_t offs)
91 {
92         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.mem);
93         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.mem);
94
95         offs += offset[what];
96
97         if(offs & 0x01)
98                 offs |= 0x200;
99
100         return bus_space_read_1(t, h, offs);
101 }
102
103 /*---------------------------------------------------------------------------*
104  *      Teles S0/16 fifo write routine
105  *---------------------------------------------------------------------------*/
106 static void
107 tels016_write_fifo(struct l1_softc *sc, int what, void *data, size_t size)
108 {
109         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.mem);
110         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.mem);
111         bus_space_write_region_1(t, h, offset[what], data, size);
112 }
113
114 /*---------------------------------------------------------------------------*
115  *      Teles S0/16 fifo read routine
116  *---------------------------------------------------------------------------*/
117 static void
118 tels016_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
119 {
120         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.mem);
121         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.mem);
122         bus_space_read_region_1(t, h, offset[what], buf, size);
123 }
124
125 /*---------------------------------------------------------------------------*
126  *      isic_probe_s016 - probe for Teles S0/16 and compatibles
127  *---------------------------------------------------------------------------*/
128 int
129 isic_probe_s016(device_t dev)
130 {
131         size_t unit = device_get_unit(dev);     /* get unit */
132         struct l1_softc *sc = 0;                /* softc */
133         void *ih = 0;                           /* dummy */
134         u_int8_t b0,b1,b2;                      /* for signature */
135         bus_space_tag_t    t;                   /* bus things */
136         bus_space_handle_t h;
137
138         /* check max unit range */
139
140         if(unit >= ISIC_MAXUNIT)
141         {
142                 printf("isic%d: Error, unit %d >= ISIC_MAXUNIT for Teles S0/16!\n",
143                                 unit, unit);
144                 return(ENXIO);  
145         }
146
147         sc = &l1_sc[unit];                      /* get pointer to softc */
148         sc->sc_unit = unit;                     /* set unit */
149
150         /* see if an io base was supplied */
151
152         if(!(sc->sc_resources.io_base[0] =
153                         bus_alloc_resource(dev, SYS_RES_IOPORT,
154                                            &sc->sc_resources.io_rid[0],
155                                            0ul, ~0ul, 1, RF_ACTIVE)))
156         {
157                 printf("isic%d: Could not allocate i/o port for Teles S0/16.\n", unit);
158                 return(ENXIO);
159         }
160
161         sc->sc_port = rman_get_start(sc->sc_resources.io_base[0]);
162
163         /*
164          * check if the provided io port is valid
165          */
166
167         switch(sc->sc_port)
168         {
169                 case 0xd80:
170                 case 0xe80:
171                 case 0xf80:
172                         break;
173
174                 default:
175                         printf("isic%d: Error, invalid iobase 0x%x specified for Teles S0/16!\n",
176                                 unit, sc->sc_port);
177                         isic_detach_common(dev);
178                         return(ENXIO);
179                         break;
180         }
181
182         /* allocate memory resource */
183
184         if(!(sc->sc_resources.mem =
185                         bus_alloc_resource(dev, SYS_RES_MEMORY,
186                                         &sc->sc_resources.mem_rid,
187                                         0ul, ~0ul, TELES_S016_MEMSIZE,
188                                         RF_ACTIVE)))
189         {
190                 printf("isic%d: Could not allocate memory for Teles S0/16.\n", unit);
191                 isic_detach_common(dev);
192                 return(ENXIO);
193         }
194
195         /* 
196          * get virtual addr.
197          */
198         sc->sc_vmem_addr = rman_get_virtual(sc->sc_resources.mem);
199
200         /*
201          * check for valid adresses
202          */
203         switch(kvtop(sc->sc_vmem_addr))
204         {
205                 case 0xc0000:
206                 case 0xc2000:
207                 case 0xc4000:
208                 case 0xc6000:
209                 case 0xc8000:
210                 case 0xca000:
211                 case 0xcc000:
212                 case 0xce000:
213                 case 0xd0000:
214                 case 0xd2000:
215                 case 0xd4000:
216                 case 0xd6000:
217                 case 0xd8000:
218                 case 0xda000:
219                 case 0xdc000:
220                 case 0xde000:
221                         break;
222
223                 default:
224                         printf("isic%d: Error, invalid memory address 0x%lx for Teles S0/16!\n",
225                                 unit, kvtop(sc->sc_vmem_addr));
226                         isic_detach_common(dev);
227                         return(ENXIO);
228                         break;
229         }               
230         
231         /* setup ISAC access routines */
232
233         sc->clearirq = NULL;
234
235         sc->readreg = tels016_read_reg;
236         sc->writereg = tels016_write_reg;
237
238         sc->readfifo = tels016_read_fifo;
239         sc->writefifo = tels016_write_fifo;
240
241         /* setup card type */
242         sc->sc_cardtyp = CARD_TYPEP_16;
243
244         /* setup IOM bus type */
245         
246         sc->sc_bustyp = BUS_TYPE_IOM1;
247
248         sc->sc_ipac = 0;
249         sc->sc_bfifolen = HSCX_FIFO_LEN;
250
251         /* setup ISAC base addr, though we don't really need it */
252         
253         ISAC_BASE = (caddr_t)((sc->sc_vmem_addr) + 0x100);
254
255         /* setup HSCX base addr */
256         
257         HSCX_A_BASE = (caddr_t)((sc->sc_vmem_addr) + 0x180);
258         HSCX_B_BASE = (caddr_t)((sc->sc_vmem_addr) + 0x1c0);
259
260         t = rman_get_bustag(sc->sc_resources.io_base[0]);
261         h = rman_get_bushandle(sc->sc_resources.io_base[0]);
262
263         /* get signature bytes */
264         b0 = bus_space_read_1(t, h, 0);
265         b1 = bus_space_read_1(t, h, 1);
266         b2 = bus_space_read_1(t, h, 2);
267
268         /* check signature bytes */
269         if(b0 != 0x51)
270         {
271                 printf("isic%d: Error, signature 1 0x%x != 0x51 for Teles S0/16!\n",
272                         unit, b0);
273                 isic_detach_common(dev);
274                 return(ENXIO);
275         }
276         
277         if(b1 != 0x93)
278         {
279                 printf("isic%d: Error, signature 2 0x%x != 0x93 for Teles S0/16!\n",
280                         unit, b1);
281                 isic_detach_common(dev);
282                 return(ENXIO);
283         }
284         
285         if((b2 != 0x1e) && (b2 != 0x1f))
286         {
287                 printf("isic%d: Error, signature 3 0x%x != 0x1e or 0x1f for Teles S0/16!\n",
288                         unit, b2);
289                 isic_detach_common(dev);
290                 return(ENXIO);
291         }
292
293         /* get our irq */
294
295         if(!(sc->sc_resources.irq =
296                         bus_alloc_resource(dev, SYS_RES_IRQ,
297                                                 &sc->sc_resources.irq_rid,
298                                                 0ul, ~0ul, 1, RF_ACTIVE)))
299         {
300                 printf("isic%d: Could not allocate irq for Teles S0/16.\n", unit);
301                 isic_detach_common(dev);
302                 return ENXIO;
303         }
304
305         /* register interupt routine */
306
307         bus_setup_intr(dev, sc->sc_resources.irq,
308                         INTR_TYPE_NET,
309                         (void(*)(void *))(isicintr),
310                         sc, &ih);
311
312         /* get the irq number */
313         
314         sc->sc_irq = rman_get_start(sc->sc_resources.irq);
315
316         /* check IRQ validity */
317
318         if((intr_no[sc->sc_irq]) == 1)
319         {
320                 printf("isic%d: Error, invalid IRQ [%d] specified for Teles S0/16!\n",
321                         unit, sc->sc_irq);
322                 isic_detach_common(dev);
323                 return(ENXIO);
324         }
325         
326         return (0);
327 }
328
329 /*---------------------------------------------------------------------------*
330  *      isic_attach_s016 - attach Teles S0/16 and compatibles
331  *---------------------------------------------------------------------------*/
332 int
333 isic_attach_s016(device_t dev)
334 {
335         struct l1_softc *sc = &l1_sc[device_get_unit(dev)];
336         u_long irq;
337
338         bus_space_tag_t    ta = rman_get_bustag(sc->sc_resources.mem);
339         bus_space_handle_t ha = rman_get_bushandle(sc->sc_resources.mem);
340         bus_space_tag_t    tb = rman_get_bustag(sc->sc_resources.io_base[0]);
341         bus_space_handle_t hb = rman_get_bushandle(sc->sc_resources.io_base[0]);
342
343         /* is this right for FreeBSD or off by one ? */
344         irq = intr_no[sc->sc_irq];
345
346         /* configure IRQ */
347
348         irq |= ((u_long) sc->sc_vmem_addr) >> 9;
349
350         DELAY(SEC_DELAY / 10);
351         bus_space_write_1(tb, hb, 4, irq);
352
353         DELAY(SEC_DELAY / 10);
354         bus_space_write_1(tb, hb, 4, irq | 0x01);
355
356         DELAY(SEC_DELAY / 5);
357
358         /* set card bit off */
359
360         bus_space_write_1(ta, ha, 0x80, 0);
361         DELAY(SEC_DELAY / 5);
362
363         /* set card bit on */
364         
365         bus_space_write_1(ta, ha, 0x80, 1);
366         DELAY(SEC_DELAY / 5);
367
368         return 0;
369 }
370 #endif /* ISIC > 0 */