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