]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/i4b/layer1/isic/i4b_itk_ix1.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / i4b / layer1 / isic / i4b_itk_ix1.c
1 /*-
2  * Copyright (c) 1998, 1999 Martin Husemann <martin@rumolt.teuto.de>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. The name of the author may not be used to endorse or promote products
11  *    derived from this software without specific prior written permission
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 /*---------------------------------------------------------------------------
26  *
27  *      i4b_itk_ix1.c - ITK ix1 micro passive card driver for isdn4bsd
28  *      --------------------------------------------------------------
29  *      last edit-date: [Wed Jan 24 09:27:06 2001]
30  *
31  *---------------------------------------------------------------------------
32  *
33  * The ITK ix1 micro ISDN card is an ISA card with one region
34  * of four io ports mapped and a fixed irq all jumpered on the card.
35  * Access to the board is straight forward and simmilar to
36  * the ELSA and DYNALINK cards. If a PCI version of this card
37  * exists all we need is probably a pci-bus attachment, all
38  * this low level routines should work imediately.
39  *
40  * To reset the card:
41  *   - write 0x01 to ITK_CONFIG
42  *   - wait >= 10 ms
43  *   - write 0x00 to ITK_CONFIG
44  *
45  * To read or write data:
46  *  - write address to ITK_ALE port
47  *  - read data from or write data to ITK_ISAC_DATA port or ITK_HSCX_DATA port
48  * The two HSCX channel registers are offset by HSCXA (0x00) and HSCXB (0x40).
49  *
50  * The probe routine was derived by trial and error from a representative
51  * sample of two cards ;-) The standard way (checking HSCX versions)
52  * was extended by reading a zero from a non existant HSCX register (register
53  * 0xff). Reading the config register gives varying results, so this doesn't
54  * seem to be used as an id register (like the Teles S0/16.3).
55  *
56  * If the probe fails for your card use "options ITK_PROBE_DEBUG" to get
57  * additional debug output.
58  *
59  *---------------------------------------------------------------------------*/
60
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
63
64 #include "opt_i4b.h"
65
66 #if defined(ITKIX1)
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/socket.h>
71 #include <net/if.h>
72
73 #include <i4b/include/i4b_ioctl.h>
74 #include <i4b/include/i4b_trace.h>
75 #include <i4b/layer1/i4b_l1.h>
76 #include <i4b/layer1/isic/i4b_isic.h>
77 #include <i4b/layer1/isic/i4b_hscx.h>
78
79 /* Register offsets */
80 #define ITK_ISAC_DATA   0
81 #define ITK_HSCX_DATA   1
82 #define ITK_ALE         2
83 #define ITK_CONFIG      3
84
85 /* Size of IO range to allocate for this card */
86 #define ITK_IO_SIZE     4
87
88 /* Register offsets for the two HSCX channels */
89 #define HSCXA   0
90 #define HSCXB   0x40
91
92 static void
93 itkix1_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
94 {
95         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
96         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
97         switch (what)
98         {
99                 case ISIC_WHAT_ISAC:
100                         bus_space_write_1(t, h, ITK_ALE, 0);
101                         bus_space_read_multi_1(t, h, ITK_ISAC_DATA, buf, size);
102                         break;
103                 case ISIC_WHAT_HSCXA:
104                         bus_space_write_1(t, h, ITK_ALE, HSCXA);
105                         bus_space_read_multi_1(t, h, ITK_HSCX_DATA, buf, size);
106                         break;
107                 case ISIC_WHAT_HSCXB:
108                         bus_space_write_1(t, h, ITK_ALE, HSCXB);
109                         bus_space_read_multi_1(t, h, ITK_HSCX_DATA, buf, size);
110                         break;
111         }
112 }
113
114 static void
115 itkix1_write_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
116 {
117         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
118         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
119         switch (what)
120         {
121                 case ISIC_WHAT_ISAC:
122                         bus_space_write_1(t, h, ITK_ALE, 0);
123                         bus_space_write_multi_1(t, h, ITK_ISAC_DATA, (u_int8_t*)buf, size);
124                         break;
125                 case ISIC_WHAT_HSCXA:
126                         bus_space_write_1(t, h, ITK_ALE, HSCXA);
127                         bus_space_write_multi_1(t, h, ITK_HSCX_DATA, (u_int8_t*)buf, size);
128                         break;
129                 case ISIC_WHAT_HSCXB:
130                         bus_space_write_1(t, h, ITK_ALE, HSCXB);
131                         bus_space_write_multi_1(t, h, ITK_HSCX_DATA, (u_int8_t*)buf, size);
132                         break;
133         }
134 }
135
136 static void
137 itkix1_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
138 {
139         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
140         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
141         switch (what)
142         {
143                 case ISIC_WHAT_ISAC:
144                         bus_space_write_1(t, h, ITK_ALE, offs);
145                         bus_space_write_1(t, h, ITK_ISAC_DATA, data);
146                         break;
147                 case ISIC_WHAT_HSCXA:
148                         bus_space_write_1(t, h, ITK_ALE, HSCXA+offs);
149                         bus_space_write_1(t, h, ITK_HSCX_DATA, data);
150                         break;
151                 case ISIC_WHAT_HSCXB:
152                         bus_space_write_1(t, h, ITK_ALE, HSCXB+offs);
153                         bus_space_write_1(t, h, ITK_HSCX_DATA, data);
154                         break;
155         }
156 }
157
158 static u_int8_t
159 itkix1_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
160 {
161         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
162         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
163         switch (what)
164         {
165                 case ISIC_WHAT_ISAC:
166                         bus_space_write_1(t, h, ITK_ALE, offs);
167                         return bus_space_read_1(t, h, ITK_ISAC_DATA);
168                 case ISIC_WHAT_HSCXA:
169                         bus_space_write_1(t, h, ITK_ALE, HSCXA+offs);
170                         return bus_space_read_1(t, h, ITK_HSCX_DATA);
171                 case ISIC_WHAT_HSCXB:
172                         bus_space_write_1(t, h, ITK_ALE, HSCXB+offs);
173                         return bus_space_read_1(t, h, ITK_HSCX_DATA);
174         }
175         return 0;
176 }
177
178 /*
179  * Probe for card
180  */
181 int
182 isic_probe_itkix1(device_t dev)
183 {       
184         size_t unit = device_get_unit(dev);     /* get unit */
185         struct l1_softc *sc = 0;                /* softc */
186         void *ih = 0;                           /* dummy */
187         bus_space_tag_t    t;                   /* bus things */
188         bus_space_handle_t h;
189         u_int8_t hd, hv1, hv2, saveale;
190         int ret;
191
192         #if defined(ITK_PROBE_DEBUG)
193         printf("Checking unit %u\n", unit);
194         #endif
195
196         /* check max unit range */
197         if(unit >= ISIC_MAXUNIT)
198         {
199                 printf("isic%d: Error, unit %d >= ISIC_MAXUNIT for ITK IX1!\n",
200                                 unit, unit);
201                 return ENXIO;   
202         }
203
204         sc = &l1_sc[unit];                      /* get pointer to softc */
205         sc->sc_unit = unit;                     /* set unit */
206         
207         #if defined(ITK_PROBE_DEBUG)
208         printf("Allocating io base...");
209         #endif
210         
211         if(!(sc->sc_resources.io_base[0] =
212                 bus_alloc_resource_any(dev, SYS_RES_IOPORT,
213                                 &sc->sc_resources.io_rid[0],
214                                 RF_ACTIVE)))
215         {
216                 printf("isic%d: Could not allocate i/o port for ITK IX1.\n", unit);
217                 return ENXIO;
218         }
219
220         #if defined(ITK_PROBE_DEBUG)
221         printf("done.\n");
222         #endif
223
224         sc->sc_port = rman_get_start(sc->sc_resources.io_base[0]);
225         t = rman_get_bustag(sc->sc_resources.io_base[0]);
226         h = rman_get_bushandle(sc->sc_resources.io_base[0]);
227
228         #if defined(ITK_PROBE_DEBUG)
229         printf("Allocating irq...");
230         #endif
231
232         /* get our irq */
233         if(!(sc->sc_resources.irq =
234                         bus_alloc_resource_any(dev, SYS_RES_IRQ,
235                                                 &sc->sc_resources.irq_rid,
236                                                 RF_ACTIVE)))
237         {
238                 printf("isic%d: Could not allocate irq for ITK IX1.\n", unit);
239                 bus_release_resource(dev,SYS_RES_IOPORT,
240                                 sc->sc_resources.io_rid[0],
241                                 sc->sc_resources.io_base[0]);
242                 return ENXIO;
243         }
244
245         #if defined(ITK_PROBE_DEBUG)
246         printf("done.\n");
247         #endif
248
249         /* get the irq number */
250         sc->sc_irq = rman_get_start(sc->sc_resources.irq);
251
252         #if defined(ITK_PROBE_DEBUG)
253         printf("Setting up access routines...");
254         #endif
255         
256         /* setup access routines */
257         sc->clearirq = NULL;
258         sc->readreg = itkix1_read_reg;
259         sc->writereg = itkix1_write_reg;
260         sc->readfifo = itkix1_read_fifo;
261         sc->writefifo = itkix1_write_fifo;
262
263         /* setup card type */   
264         sc->sc_cardtyp = CARD_TYPEP_ITKIX1;
265
266         /* setup IOM bus type */
267         sc->sc_bustyp = BUS_TYPE_IOM2;
268
269         sc->sc_ipac = 0;
270         sc->sc_bfifolen = HSCX_FIFO_LEN;
271
272         #if defined(ITK_PROBE_DEBUG)
273         printf("done.\n");
274         #endif
275
276         /* register interrupt routine */
277
278         #if defined(ITK_PROBE_DEBUG)
279         printf("Setting up access interrupt...");
280         #endif
281
282         if (bus_setup_intr(dev, sc->sc_resources.irq, INTR_TYPE_NET, NULL,
283                         (void(*)(void *))(isicintr), sc, &ih) != 0)
284         {
285                 printf("isic%d: Could not setup irq for ITK IX1.\n", unit);
286                 bus_release_resource(dev,SYS_RES_IOPORT,
287                                 sc->sc_resources.io_rid[0],
288                                 sc->sc_resources.io_base[0]);
289                 return ENXIO;
290         }
291
292         #if defined(ITK_PROBE_DEBUG)
293         printf("done.\n");
294
295         printf("Doing probe stuff...");
296         #endif
297         
298         /* save old value of this port, we're stomping over it */
299         saveale = bus_space_read_1(t, h, ITK_ALE);
300
301         /* select invalid register */
302         bus_space_write_1(t, h, ITK_ALE, 0xff);
303         /* get HSCX data for this non existent register */
304         hd = bus_space_read_1(t, h, ITK_HSCX_DATA);
305         /* get HSCX version info */
306         bus_space_write_1(t, h, ITK_ALE, HSCXA + H_VSTR);
307         hv1 = bus_space_read_1(t, h, ITK_HSCX_DATA);
308         bus_space_write_1(t, h, ITK_ALE, HSCXB + H_VSTR);
309         hv2 = bus_space_read_1(t, h, ITK_HSCX_DATA);
310
311         ret =  (hd == 0) && ((hv1 & 0x0f) == 0x05) && ((hv2 & 0x0f) == 0x05);
312         /* succeed if version bits are OK and we got a zero from the
313          * non existent register. we found verison 0x05 and 0x04
314          * out there... */
315         ret =  (hd == 0)
316                 && (((hv1 & 0x0f) == 0x05) || ((hv1 & 0x0f) == 0x04))
317                 && (((hv2 & 0x0f) == 0x05) || ((hv2 & 0x0f) == 0x04));
318
319         /* retstore save value if we fail (if we succeed the old value
320          * has no meaning) */
321         if (!ret)
322                 bus_space_write_1(t, h, ITK_ALE, saveale);
323
324         #if defined(ITK_PROBE_DEBUG)
325         printf("done.\n");
326
327         printf("Doing second probe stuff...");
328         #endif
329
330         hv1 = HSCX_READ(0, H_VSTR) & 0xf;
331         hv2 = HSCX_READ(1, H_VSTR) & 0xf;
332         /* Read HSCX A/B VSTR.  Expected value is 0x05 (V2.1) or 0x04 (V2.0). */
333         if((hv1 != 0x05 && hv1 != 0x04) || (hv2 != 0x05 && hv2 != 0x04))
334         {
335                 printf("isic%d: HSCX VSTR test failed for ITK ix1 micro\n",
336                         unit);
337                 printf("isic%d: HSC0: VSTR: %#x\n",
338                         unit, HSCX_READ(0, H_VSTR));
339                 printf("isic%d: HSC1: VSTR: %#x\n",
340                         unit, HSCX_READ(1, H_VSTR));
341                 isic_detach_common(dev);
342                 return ENXIO;
343         }  
344
345         #if defined(ITK_PROBE_DEBUG)
346         printf("done.\n");
347         #endif
348
349 #if defined(ITK_PROBE_DEBUG)
350         printf("\nITK ix1 micro probe: hscx = 0x%02x, v1 = 0x%02x, v2 = 0x%02x, would have %s\n",
351                 hd, hv1, hv2, ret ? "succeeded" : "failed");
352         isic_detach_common(dev);
353         return ENXIO;
354 #else
355         if ( ret )
356         {
357                 return 0;
358         }
359         else
360         {
361                 isic_detach_common(dev);
362                 return ENXIO;
363         }
364 #endif
365 }
366
367 /*
368  *  Attach card
369  */
370 int
371 isic_attach_itkix1(device_t dev)
372 {
373         size_t unit = device_get_unit(dev);     /* get unit */
374         struct l1_softc *sc = &l1_sc[unit];
375         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]); 
376         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
377
378         /* setup access routines */
379         sc->clearirq = NULL;
380         sc->readreg = itkix1_read_reg;
381         sc->writereg = itkix1_write_reg;
382         sc->readfifo = itkix1_read_fifo;
383         sc->writefifo = itkix1_write_fifo;
384
385         /* setup card type */   
386         sc->sc_cardtyp = CARD_TYPEP_ITKIX1;
387
388         /* setup IOM bus type */
389         sc->sc_bustyp = BUS_TYPE_IOM2;
390
391         sc->sc_ipac = 0;
392         sc->sc_bfifolen = HSCX_FIFO_LEN;
393
394         bus_space_write_1(t, h, ITK_CONFIG, 1);
395         DELAY(SEC_DELAY / 10);
396         bus_space_write_1(t, h, ITK_CONFIG, 0);
397         DELAY(SEC_DELAY / 10);
398
399         return 0;
400 }
401
402 #endif /* defined(ITKIX1) */