Initial import from FreeBSD RELENG_4:
[games.git] / sys / net / i4b / layer1 / isic / i4b_elsa_pcc16.c
1 /*
2  * Copyright (c) 1999, 2001 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      isic - I4B driver for ELSA MicroLink ISDN/PCC-16 and ELSA PCFpro
28  *      ================================================================
29  *
30  * $FreeBSD: src/sys/i4b/layer1/isic/i4b_elsa_pcc16.c,v 1.5.2.2 2001/10/24 14:17:20 hm Exp $
31  *
32  *      last edit-date: [Wed Oct 24 16:15:26 2001]
33  *
34  *---------------------------------------------------------------------------*/
35
36 #include "isic.h"
37 #include "opt_i4b.h"
38
39 #if (NISIC > 0) && defined(ELSA_PCC16)
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/socket.h>
44 #include <net/if.h>
45
46 #include <machine/i4b_ioctl.h>
47
48 #include <i4b/layer1/isic/i4b_isic.h>
49 #include <i4b/layer1/isic/i4b_hscx.h>
50
51 static void i4b_epcc16_clrirq(struct l1_softc *sc);
52
53 /* masks for register encoded in base addr */
54
55 #define ELSA_BASE_MASK          0x0ffff
56 #define ELSA_OFF_MASK           0xf0000
57
58 /* register id's to be encoded in base addr */
59
60 #define ELSA_IDISAC             0x00000
61 #define ELSA_IDHSCXA            0x10000
62 #define ELSA_IDHSCXB            0x20000
63
64 /* offsets from base address */
65
66 #define ELSA_OFF_ISAC           0x00
67 #define ELSA_OFF_HSCX           0x02
68 #define ELSA_OFF_OFF            0x03
69 #define ELSA_OFF_CTRL           0x04
70 #define ELSA_OFF_CFG            0x05
71 #define ELSA_OFF_TIMR           0x06
72 #define ELSA_OFF_IRQ            0x07
73
74 /* control register (write access) */
75
76 #define ELSA_CTRL_LED_YELLOW    0x02
77 #define ELSA_CTRL_LED_GREEN     0x08
78 #define ELSA_CTRL_RESET         0x20
79 #define ELSA_CTRL_TIMEREN       0x80
80 #define ELSA_CTRL_SECRET        0x50
81
82 /*---------------------------------------------------------------------------*
83  *      ELSA MicroLink ISDN/PCC-16 clear IRQ routine
84  *---------------------------------------------------------------------------*/
85 static void
86 i4b_epcc16_clrirq(struct l1_softc *sc)
87 {
88         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
89         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
90         bus_space_write_1(t, h, ELSA_OFF_IRQ, 0);
91 }
92
93 /*---------------------------------------------------------------------------*
94  *      ELSA MicroLink ISDN/PCC-16 ISAC get fifo routine
95  *---------------------------------------------------------------------------*/
96 static void
97 epcc16_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
98 {
99         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
100         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
101
102         switch (what) {
103                 case ISIC_WHAT_ISAC:
104                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0);
105                         bus_space_read_multi_1(t, h, ELSA_OFF_ISAC, buf, size);
106                         break;
107                 case ISIC_WHAT_HSCXA:
108                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0);
109                         bus_space_read_multi_1(t, h, ELSA_OFF_HSCX, buf, size);
110                         break;
111                 case ISIC_WHAT_HSCXB:
112                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0x40);
113                         bus_space_read_multi_1(t, h, ELSA_OFF_HSCX, buf, size);
114                         break;
115         }
116 }
117
118 /*---------------------------------------------------------------------------*
119  *      ELSA MicroLink ISDN/PCC-16 ISAC put fifo routine
120  *---------------------------------------------------------------------------*/
121 static void
122 epcc16_write_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
123 {
124         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
125         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
126
127         switch (what) {
128                 case ISIC_WHAT_ISAC:
129                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0);
130                         bus_space_write_multi_1(t, h, ELSA_OFF_ISAC, buf, size);
131                         break;
132                 case ISIC_WHAT_HSCXA:
133                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0);
134                         bus_space_write_multi_1(t, h, ELSA_OFF_HSCX, buf, size);
135                         break;
136                 case ISIC_WHAT_HSCXB:
137                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0x40);
138                         bus_space_write_multi_1(t, h, ELSA_OFF_HSCX, buf, size);
139                         break;
140         }
141 }
142
143 /*---------------------------------------------------------------------------*
144  *      ELSA MicroLink ISDN/PCC-16 ISAC put register routine
145  *---------------------------------------------------------------------------*/
146 static void
147 epcc16_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
148 {
149         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
150         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
151
152         switch (what) {
153                 case ISIC_WHAT_ISAC:
154                         bus_space_write_1(t, h, ELSA_OFF_OFF, offs);
155                         bus_space_write_1(t, h, ELSA_OFF_ISAC, data);
156                         break;
157                 case ISIC_WHAT_HSCXA:
158                         bus_space_write_1(t, h, ELSA_OFF_OFF, offs);
159                         bus_space_write_1(t, h, ELSA_OFF_HSCX, data);
160                         break;
161                 case ISIC_WHAT_HSCXB:
162                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0x40+offs);
163                         bus_space_write_1(t, h, ELSA_OFF_HSCX, data);
164                         break;
165         }
166 }
167
168 /*---------------------------------------------------------------------------*
169  *      ELSA MicroLink ISDN/PCC-16 ISAC get register routine
170  *---------------------------------------------------------------------------*/
171 static u_int8_t
172 epcc16_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
173 {
174         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
175         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
176
177         switch (what) {
178                 case ISIC_WHAT_ISAC:
179                         bus_space_write_1(t, h, ELSA_OFF_OFF, offs);
180                         return bus_space_read_1(t, h, ELSA_OFF_ISAC);
181                 case ISIC_WHAT_HSCXA:
182                         bus_space_write_1(t, h, ELSA_OFF_OFF, offs);
183                         return bus_space_read_1(t, h, ELSA_OFF_HSCX);
184                 case ISIC_WHAT_HSCXB:
185                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0x40+offs);
186                         return bus_space_read_1(t, h, ELSA_OFF_HSCX);
187         }
188         return 0;
189 }
190
191 /*---------------------------------------------------------------------------*
192  *      isic_detach_Epcc16 - detach for ELSA MicroLink ISDN/PCC-16
193  *---------------------------------------------------------------------------*/
194 static void
195 isic_detach_Epcc16(device_t dev)
196 {
197         struct l1_softc *sc = &l1_sc[device_get_unit(dev)];
198
199         if ( sc->sc_resources.irq )
200         {
201                 bus_teardown_intr(dev,sc->sc_resources.irq,
202                         (void(*)(void *))isicintr);
203                 bus_release_resource(dev,SYS_RES_IRQ,
204                                         sc->sc_resources.irq_rid,
205                                         sc->sc_resources.irq);
206                 sc->sc_resources.irq = 0;
207         }
208         
209         if ( sc->sc_resources.io_base[0] ) {
210                 bus_release_resource(dev,SYS_RES_IOPORT,
211                                         sc->sc_resources.io_rid[0],
212                                         sc->sc_resources.io_base[0]);
213                 sc->sc_resources.io_base[0] = 0;
214         }
215 }
216
217 /*---------------------------------------------------------------------------*
218  *      isic_probe_Epcc16 - probe for ELSA MicroLink ISDN/PCC-16
219  *---------------------------------------------------------------------------*/
220 int
221 isic_probe_Epcc16(device_t dev)
222 {
223         size_t unit = device_get_unit(dev);     /* get unit */
224         struct l1_softc *sc = 0;                /* pointer to softc */
225         void *ih = 0;                           /* dummy */
226
227         /* check max unit range */
228         if(unit >= ISIC_MAXUNIT)
229         {
230                 printf("isic%d: Error, unit %d >= ISIC_MAXUNIT for ELSA PCC-16!\n",
231                                 unit, unit);
232                 return(ENXIO);  
233         }
234
235         sc = &l1_sc[unit];              /* get pointer to softc */
236         sc->sc_unit = unit;             /* set unit */
237
238         /* see if an io base was supplied */
239         
240         if(!(sc->sc_resources.io_base[0] =
241                         bus_alloc_resource(dev, SYS_RES_IOPORT,
242                                            &sc->sc_resources.io_rid[0],
243                                            0ul, ~0ul, 1, RF_ACTIVE)))
244         {
245                 printf("isic%d: Could not get iobase for ELSA PCC-16.\n",
246                                 unit);
247                 return(ENXIO);
248         }
249
250         /* check if we got an iobase */
251
252         sc->sc_port = rman_get_start(sc->sc_resources.io_base[0]);
253
254         switch(sc->sc_port)
255         {
256                 case 0x160:
257                 case 0x170:
258                 case 0x260:
259                 case 0x360:
260                         break;
261                 default:
262                         printf("isic%d: Error, invalid iobase 0x%x specified for ELSA MicroLink ISDN/PCC-16!\n",
263                                 unit, sc->sc_port);
264                         isic_detach_Epcc16(dev);
265                         return(ENXIO);
266                         break;
267         }
268
269         /* setup access routines */
270
271         sc->clearirq = i4b_epcc16_clrirq;
272         sc->readreg = epcc16_read_reg;
273         sc->writereg = epcc16_write_reg;
274
275         sc->readfifo = epcc16_read_fifo;
276         sc->writefifo = epcc16_write_fifo;
277
278         /* setup card type */
279         
280         sc->sc_cardtyp = CARD_TYPEP_PCC16;
281
282         /* setup IOM bus type */
283         
284         sc->sc_bustyp = BUS_TYPE_IOM2;
285
286         sc->sc_ipac = 0;
287         sc->sc_bfifolen = HSCX_FIFO_LEN;        
288
289         /* 
290          * Read HSCX A/B VSTR.  Expected value for the ELSA PCC-16
291          * is 0x05 ( = version 2.1 ) in the least significant bits.
292          */
293
294         if( ((HSCX_READ(0, H_VSTR) & 0xf) != 0x5) ||
295             ((HSCX_READ(1, H_VSTR) & 0xf) != 0x5) )
296         {
297                 /* patch from "Doobee R . Tzeck" <drt@ailis.de>:
298                  * I own an ELSA PCFpro. To my knowledge, the ELSA PCC16 is
299                  * a stripped down Version on the PCFpro. By patching the
300                  * card detection routine for the PPC16 I was able to use
301                  * the PPC16 driver for the PCFpro.
302                  */
303                 if( ((HSCX_READ(0, H_VSTR) & 0xf) != 0x85) ||
304                     ((HSCX_READ(1, H_VSTR) & 0xf) != 0x85) )
305                 {
306                         printf("isic%d: HSCX VSTR test failed for ELSA MicroLink ISDN/PCC-16\n",
307                                 unit);
308                         isic_detach_Epcc16(dev);
309                         printf("isic%d: HSC0: VSTR: %#x\n",
310                                 unit, HSCX_READ(0, H_VSTR));
311                         printf("isic%d: HSC1: VSTR: %#x\n",
312                                 unit, HSCX_READ(1, H_VSTR));
313                         return (ENXIO);
314                 }
315                 else
316                 {
317                         printf("isic%d: ELSA MicroLink ISDN/PCFpro found, going to tread it as PCC-16\n",
318                                 unit);
319                 }
320         }
321
322         /* get our irq */
323
324         if(!(sc->sc_resources.irq =
325                         bus_alloc_resource(dev, SYS_RES_IRQ,
326                                         &sc->sc_resources.irq_rid,
327                                         0ul, ~0ul, 1, RF_ACTIVE)))
328         {
329                 printf("isic%d: Could not get an irq.\n",unit);
330                 isic_detach_Epcc16(dev);
331                 return ENXIO;
332         }
333
334         /* get the irq number */
335         sc->sc_irq = rman_get_start(sc->sc_resources.irq);
336
337         /* check IRQ validity */        
338         switch(sc->sc_irq)
339         {
340                 case 2:
341                 case 9:         
342                 case 3:         
343                 case 5:
344                 case 10:
345                 case 11:
346                 case 15:                
347                         break;
348                         
349                 default:
350                         printf("isic%d: Error, invalid IRQ [%d] specified for ELSA MicroLink ISDN/PCC-16!\n",
351                                 unit, sc->sc_irq);
352                         isic_detach_Epcc16(dev);
353                         return(ENXIO);
354                         break;
355         }
356
357         /* register interupt routine */
358         bus_setup_intr(dev,sc->sc_resources.irq,INTR_TYPE_NET,
359                         (void(*)(void *))(isicintr),
360                         sc,&ih);
361
362
363         return (0);
364 }
365
366 /*---------------------------------------------------------------------------*
367  * isic_attach_Epcc16 - attach for ELSA MicroLink ISDN/PCC-16
368  *---------------------------------------------------------------------------*/
369 int
370 isic_attach_Epcc16(device_t dev)
371 {
372         int unit = device_get_unit(dev);
373         struct l1_softc *sc = &l1_sc[unit];     
374         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
375         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
376
377         u_char byte = ELSA_CTRL_SECRET;
378
379         byte &= ~ELSA_CTRL_RESET;
380         bus_space_write_1(t, h, ELSA_OFF_CTRL, byte);
381         DELAY(20);
382         byte |= ELSA_CTRL_RESET;
383         bus_space_write_1(t, h, ELSA_OFF_CTRL, byte);
384
385         DELAY(20);
386         bus_space_write_1(t, h, ELSA_OFF_IRQ, 0xff);
387
388         return 0;
389 }
390
391 #endif /* (NISIC > 0) && defined(ELSA_PCC16) */