kernel: Use NULL for pointers.
[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 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  *      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  *
33  *---------------------------------------------------------------------------
34  *
35  * The ITK ix1 micro ISDN card is an ISA card with one region
36  * of four io ports mapped and a fixed irq all jumpered on the card.
37  * Access to the board is straight forward and simmilar to
38  * the ELSA and DYNALINK cards. If a PCI version of this card
39  * exists all we need is probably a pci-bus attachment, all
40  * this low level routines should work imediately.
41  *
42  * To reset the card:
43  *   - write 0x01 to ITK_CONFIG
44  *   - wait >= 10 ms
45  *   - write 0x00 to ITK_CONFIG
46  *
47  * To read or write data:
48  *  - write address to ITK_ALE port
49  *  - read data from or write data to ITK_ISAC_DATA port or ITK_HSCX_DATA port
50  * The two HSCX channel registers are offset by HSCXA (0x00) and HSCXB (0x40).
51  *
52  * The probe routine was derived by trial and error from a representative
53  * sample of two cards ;-) The standard way (checking HSCX versions)
54  * was extended by reading a zero from a non existant HSCX register (register
55  * 0xff). Reading the config register gives varying results, so this doesn't
56  * seem to be used as an id register (like the Teles S0/16.3).
57  *
58  * If the probe fails for your card use "options ITK_PROBE_DEBUG" to get
59  * additional debug output.
60  *
61  *---------------------------------------------------------------------------*/
62
63 #include "use_isic.h"
64 #include "opt_i4b.h"
65
66 #if NISIC > 0 && 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 <net/i4b/include/machine/i4b_ioctl.h>
74 #include <net/i4b/include/machine/i4b_trace.h>
75 #include "../i4b_l1.h"
76 #include "i4b_isic.h"
77 #include "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 = NULL;             /* softc */
186         void *ih = NULL;                                /* 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         kprintf("Checking unit %u\n", unit);
194         #endif
195
196         /* check max unit range */
197         if(unit >= ISIC_MAXUNIT)
198         {
199                 kprintf("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         kprintf("Allocating io base...");
209         #endif
210         
211         if(!(sc->sc_resources.io_base[0] =
212                 bus_alloc_resource(dev, SYS_RES_IOPORT,
213                                 &sc->sc_resources.io_rid[0],
214                                 0ul, ~0ul, 1, RF_ACTIVE)))
215         {
216                 kprintf("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         kprintf("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         kprintf("Allocating irq...");
230         #endif
231
232         /* get our irq */
233         if(!(sc->sc_resources.irq =
234                         bus_alloc_resource(dev, SYS_RES_IRQ,
235                                                 &sc->sc_resources.irq_rid,
236                                                 0ul, ~0ul, 1, RF_ACTIVE)))
237         {
238                 kprintf("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         kprintf("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         kprintf("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         kprintf("done.\n");
274         #endif
275
276         /* register interupt routine */
277
278         #if defined(ITK_PROBE_DEBUG)
279         kprintf("Setting up access interrupt...");
280         #endif
281
282         bus_setup_intr(dev, sc->sc_resources.irq,
283                        0, (void(*)(void *))(isicintr),
284                        sc, &ih, NULL);
285
286         #if defined(ITK_PROBE_DEBUG)
287         kprintf("done.\n");
288
289         kprintf("Doing probe stuff...");
290         #endif
291         
292         /* save old value of this port, we're stomping over it */
293         saveale = bus_space_read_1(t, h, ITK_ALE);
294
295         /* select invalid register */
296         bus_space_write_1(t, h, ITK_ALE, 0xff);
297         /* get HSCX data for this non existent register */
298         hd = bus_space_read_1(t, h, ITK_HSCX_DATA);
299         /* get HSCX version info */
300         bus_space_write_1(t, h, ITK_ALE, HSCXA + H_VSTR);
301         hv1 = bus_space_read_1(t, h, ITK_HSCX_DATA);
302         bus_space_write_1(t, h, ITK_ALE, HSCXB + H_VSTR);
303         hv2 = bus_space_read_1(t, h, ITK_HSCX_DATA);
304
305         ret =  (hd == 0) && ((hv1 & 0x0f) == 0x05) && ((hv2 & 0x0f) == 0x05);
306         /* succeed if version bits are OK and we got a zero from the
307          * non existent register. we found verison 0x05 and 0x04
308          * out there... */
309         ret =  (hd == 0)
310                 && (((hv1 & 0x0f) == 0x05) || ((hv1 & 0x0f) == 0x04))
311                 && (((hv2 & 0x0f) == 0x05) || ((hv2 & 0x0f) == 0x04));
312
313         /* retstore save value if we fail (if we succeed the old value
314          * has no meaning) */
315         if (!ret)
316                 bus_space_write_1(t, h, ITK_ALE, saveale);
317
318         #if defined(ITK_PROBE_DEBUG)
319         kprintf("done.\n");
320
321         kprintf("Doing second probe stuff...");
322         #endif
323
324         hv1 = HSCX_READ(0, H_VSTR) & 0xf;
325         hv2 = HSCX_READ(1, H_VSTR) & 0xf;
326         /* Read HSCX A/B VSTR.  Expected value is 0x05 (V2.1) or 0x04 (V2.0). */
327         if((hv1 != 0x05 && hv1 != 0x04) || (hv2 != 0x05 && hv2 != 0x04))
328         {
329                 kprintf("isic%d: HSCX VSTR test failed for ITK ix1 micro\n",
330                         unit);
331                 kprintf("isic%d: HSC0: VSTR: %#x\n",
332                         unit, HSCX_READ(0, H_VSTR));
333                 kprintf("isic%d: HSC1: VSTR: %#x\n",
334                         unit, HSCX_READ(1, H_VSTR));
335                 isic_detach_common(dev);
336                 return ENXIO;
337         }  
338
339         #if defined(ITK_PROBE_DEBUG)
340         kprintf("done.\n");
341         #endif
342
343 #if defined(ITK_PROBE_DEBUG)
344         kprintf("\nITK ix1 micro probe: hscx = 0x%02x, v1 = 0x%02x, v2 = 0x%02x, would have %s\n",
345                 hd, hv1, hv2, ret ? "succeeded" : "failed");
346         isic_detach_common(dev);
347         return ENXIO;
348 #else
349         if ( ret )
350         {
351                 return 0;
352         }
353         else
354         {
355                 isic_detach_common(dev);
356                 return ENXIO;
357         }
358 #endif
359 }
360
361 /*
362  *  Attach card
363  */
364 int
365 isic_attach_itkix1(device_t dev)
366 {
367         size_t unit = device_get_unit(dev);     /* get unit */
368         struct l1_softc *sc = &l1_sc[unit];
369         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]); 
370         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
371
372         /* setup access routines */
373         sc->clearirq = NULL;
374         sc->readreg = itkix1_read_reg;
375         sc->writereg = itkix1_write_reg;
376         sc->readfifo = itkix1_read_fifo;
377         sc->writefifo = itkix1_write_fifo;
378
379         /* setup card type */   
380         sc->sc_cardtyp = CARD_TYPEP_ITKIX1;
381
382         /* setup IOM bus type */
383         sc->sc_bustyp = BUS_TYPE_IOM2;
384
385         sc->sc_ipac = 0;
386         sc->sc_bfifolen = HSCX_FIFO_LEN;
387
388         bus_space_write_1(t, h, ITK_CONFIG, 1);
389         DELAY(SEC_DELAY / 10);
390         bus_space_write_1(t, h, ITK_CONFIG, 0);
391         DELAY(SEC_DELAY / 10);
392
393         return 0;
394 }
395
396 #endif /* ITKIX1 */