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