]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i4b/layer1/i4b_elsa_pcc16.c
This commit was generated by cvs2svn to compensate for changes in r56067,
[FreeBSD/FreeBSD.git] / sys / i4b / layer1 / i4b_elsa_pcc16.c
1 /*
2  * Copyright (c) 1999 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 Siemens ISDN Chipset Driver for ELSA MicroLink ISDN/PCC-16
28  *      =====================================================================
29  *
30  *      $Id: i4b_elsa_pcc16.c,v 1.2 1999/12/13 21:25:26 hm Exp $
31  *
32  * $FreeBSD$
33  *
34  *      last edit-date: [Mon Dec 13 21:59:36 1999]
35  *
36  *---------------------------------------------------------------------------*/
37
38 #include "isic.h"
39 #include "opt_i4b.h"
40
41 #if (NISIC > 0) && defined(ELSA_PCC16)
42
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <net/if.h>
49
50 #include <sys/ioccom.h>
51 #include <machine/clock.h>
52
53 #include <machine/i4b_debug.h>
54 #include <machine/i4b_ioctl.h>
55
56 #include <i4b/include/i4b_global.h>
57 #include <i4b/include/i4b_l1l2.h>
58 #include <i4b/include/i4b_mbuf.h>
59
60 #include <i4b/layer1/i4b_l1.h>
61 #include <i4b/layer1/i4b_isac.h>
62 #include <i4b/layer1/i4b_hscx.h>
63
64 static void i4b_epcc16_clrirq(struct l1_softc *sc);
65
66 /* masks for register encoded in base addr */
67
68 #define ELSA_BASE_MASK          0x0ffff
69 #define ELSA_OFF_MASK           0xf0000
70
71 /* register id's to be encoded in base addr */
72
73 #define ELSA_IDISAC             0x00000
74 #define ELSA_IDHSCXA            0x10000
75 #define ELSA_IDHSCXB            0x20000
76
77 /* offsets from base address */
78
79 #define ELSA_OFF_ISAC           0x00
80 #define ELSA_OFF_HSCX           0x02
81 #define ELSA_OFF_OFF            0x03
82 #define ELSA_OFF_CTRL           0x04
83 #define ELSA_OFF_CFG            0x05
84 #define ELSA_OFF_TIMR           0x06
85 #define ELSA_OFF_IRQ            0x07
86
87 /* control register (write access) */
88
89 #define ELSA_CTRL_LED_YELLOW    0x02
90 #define ELSA_CTRL_LED_GREEN     0x08
91 #define ELSA_CTRL_RESET         0x20
92 #define ELSA_CTRL_TIMEREN       0x80
93 #define ELSA_CTRL_SECRET        0x50
94
95 /*---------------------------------------------------------------------------*
96  *      ELSA MicroLink ISDN/PCC-16 clear IRQ routine
97  *---------------------------------------------------------------------------*/
98 static void
99 i4b_epcc16_clrirq(struct l1_softc *sc)
100 {
101         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
102         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
103         bus_space_write_1(t, h, ELSA_OFF_IRQ, 0);
104 }
105
106 /*---------------------------------------------------------------------------*
107  *      ELSA MicroLink ISDN/PCC-16 ISAC get fifo routine
108  *---------------------------------------------------------------------------*/
109 static void
110 epcc16_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
111 {
112         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
113         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
114
115         switch (what) {
116                 case ISIC_WHAT_ISAC:
117                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0);
118                         bus_space_read_multi_1(t, h, ELSA_OFF_ISAC, buf, size);
119                         break;
120                 case ISIC_WHAT_HSCXA:
121                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0);
122                         bus_space_read_multi_1(t, h, ELSA_OFF_HSCX, buf, size);
123                         break;
124                 case ISIC_WHAT_HSCXB:
125                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0x40);
126                         bus_space_read_multi_1(t, h, ELSA_OFF_HSCX, buf, size);
127                         break;
128         }
129 }
130
131 /*---------------------------------------------------------------------------*
132  *      ELSA MicroLink ISDN/PCC-16 ISAC put fifo routine
133  *---------------------------------------------------------------------------*/
134 static void
135 epcc16_write_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
136 {
137         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
138         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
139
140         switch (what) {
141                 case ISIC_WHAT_ISAC:
142                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0);
143                         bus_space_write_multi_1(t, h, ELSA_OFF_ISAC, buf, size);
144                         break;
145                 case ISIC_WHAT_HSCXA:
146                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0);
147                         bus_space_write_multi_1(t, h, ELSA_OFF_HSCX, buf, size);
148                         break;
149                 case ISIC_WHAT_HSCXB:
150                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0x40);
151                         bus_space_write_multi_1(t, h, ELSA_OFF_HSCX, buf, size);
152                         break;
153         }
154 }
155
156 /*---------------------------------------------------------------------------*
157  *      ELSA MicroLink ISDN/PCC-16 ISAC put register routine
158  *---------------------------------------------------------------------------*/
159 static void
160 epcc16_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
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
165         switch (what) {
166                 case ISIC_WHAT_ISAC:
167                         bus_space_write_1(t, h, ELSA_OFF_OFF, offs);
168                         bus_space_write_1(t, h, ELSA_OFF_ISAC, data);
169                         break;
170                 case ISIC_WHAT_HSCXA:
171                         bus_space_write_1(t, h, ELSA_OFF_OFF, offs);
172                         bus_space_write_1(t, h, ELSA_OFF_HSCX, data);
173                         break;
174                 case ISIC_WHAT_HSCXB:
175                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0x40+offs);
176                         bus_space_write_1(t, h, ELSA_OFF_HSCX, data);
177                         break;
178         }
179 }
180
181 /*---------------------------------------------------------------------------*
182  *      ELSA MicroLink ISDN/PCC-16 ISAC get register routine
183  *---------------------------------------------------------------------------*/
184 static u_int8_t
185 epcc16_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
186 {
187         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[0]);
188         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[0]);
189
190         switch (what) {
191                 case ISIC_WHAT_ISAC:
192                         bus_space_write_1(t, h, ELSA_OFF_OFF, offs);
193                         return bus_space_read_1(t, h, ELSA_OFF_ISAC);
194                 case ISIC_WHAT_HSCXA:
195                         bus_space_write_1(t, h, ELSA_OFF_OFF, offs);
196                         return bus_space_read_1(t, h, ELSA_OFF_HSCX);
197                 case ISIC_WHAT_HSCXB:
198                         bus_space_write_1(t, h, ELSA_OFF_OFF, 0x40+offs);
199                         return bus_space_read_1(t, h, ELSA_OFF_HSCX);
200         }
201         return 0;
202 }
203
204 /*---------------------------------------------------------------------------*
205  *      isic_detach_Epcc16 - detach for ELSA MicroLink ISDN/PCC-16
206  *---------------------------------------------------------------------------*/
207 static void
208 isic_detach_Epcc16(device_t dev)
209 {
210         struct l1_softc *sc = &l1_sc[device_get_unit(dev)];
211
212         if ( sc->sc_resources.irq )
213         {
214                 bus_teardown_intr(dev,sc->sc_resources.irq,
215                         (void(*)(void *))isicintr);
216                 bus_release_resource(dev,SYS_RES_IRQ,
217                                         sc->sc_resources.irq_rid,
218                                         sc->sc_resources.irq);
219                 sc->sc_resources.irq = 0;
220         }
221         
222         if ( sc->sc_resources.io_base[0] ) {
223                 bus_release_resource(dev,SYS_RES_IOPORT,
224                                         sc->sc_resources.io_rid[0],
225                                         sc->sc_resources.io_base[0]);
226                 sc->sc_resources.io_base[0] = 0;
227         }
228 }
229
230 /*---------------------------------------------------------------------------*
231  *      isic_probe_Epcc16 - probe for ELSA MicroLink ISDN/PCC-16
232  *---------------------------------------------------------------------------*/
233 int
234 isic_probe_Epcc16(device_t dev)
235 {
236         size_t unit = device_get_unit(dev);     /* get unit */
237         struct l1_softc *sc = 0;                /* pointer to softc */
238         void *ih = 0;                           /* dummy */
239
240         /* check max unit range */
241         if(unit >= ISIC_MAXUNIT)
242         {
243                 printf("isic%d: Error, unit %d >= ISIC_MAXUNIT for ELSA PCC-16!\n",
244                                 unit, unit);
245                 return(ENXIO);  
246         }
247
248         sc = &l1_sc[unit];              /* get pointer to softc */
249
250         sc->sc_unit = unit;             /* set unit */
251
252         sc->sc_flags = FLAG_ELSA_PCC16; /* set flags */
253
254         /* see if an io base was supplied */
255         
256         if(!(sc->sc_resources.io_base[0] =
257                         bus_alloc_resource(dev, SYS_RES_IOPORT,
258                                            &sc->sc_resources.io_rid[0],
259                                            0ul, ~0ul, 1, RF_ACTIVE)))
260         {
261                 printf("isic%d: Could not get iobase for ELSA PCC-16.\n",
262                                 unit);
263                 return(ENXIO);
264         }
265
266         /* check if we got an iobase */
267
268         sc->sc_port = rman_get_start(sc->sc_resources.io_base[0]);
269
270         switch(sc->sc_port)
271         {
272                 case 0x160:
273                 case 0x170:
274                 case 0x260:
275                 case 0x360:
276                         break;
277                 default:
278                         printf("isic%d: Error, invalid iobase 0x%x specified for ELSA MicroLink ISDN/PCC-16!\n",
279                                 unit, sc->sc_port);
280                         isic_detach_Epcc16(dev);
281                         return(ENXIO);
282                         break;
283         }
284
285         /* setup access routines */
286
287         sc->clearirq = i4b_epcc16_clrirq;
288         sc->readreg = epcc16_read_reg;
289         sc->writereg = epcc16_write_reg;
290
291         sc->readfifo = epcc16_read_fifo;
292         sc->writefifo = epcc16_write_fifo;
293
294         /* setup card type */
295         
296         sc->sc_cardtyp = CARD_TYPEP_ELSAQS1ISA;
297
298         /* setup IOM bus type */
299         
300         sc->sc_bustyp = BUS_TYPE_IOM2;
301
302         sc->sc_ipac = 0;
303         sc->sc_bfifolen = HSCX_FIFO_LEN;        
304
305         /* 
306          * Read HSCX A/B VSTR.  Expected value for the ELSA PCC-16
307          * is 0x05 ( = version 2.1 ) in the least significant bits.
308          */
309
310         if( ((HSCX_READ(0, H_VSTR) & 0xf) != 0x5) ||
311             ((HSCX_READ(1, H_VSTR) & 0xf) != 0x5) )
312         {
313                 printf("isic%d: HSCX VSTR test failed for ELSA MicroLink ISDN/PCC-16\n",
314                         unit);
315                 printf("isic%d: HSC0: VSTR: %#x\n",
316                         unit, HSCX_READ(0, H_VSTR));
317                 printf("isic%d: HSC1: VSTR: %#x\n",
318                         unit, HSCX_READ(1, H_VSTR));
319                 isic_detach_Epcc16(dev);
320                 return (ENXIO);
321         }                   
322
323         /* get our irq */
324
325         if(!(sc->sc_resources.irq =
326                         bus_alloc_resource(dev, SYS_RES_IRQ,
327                                         &sc->sc_resources.irq_rid,
328                                         0ul, ~0ul, 1, RF_ACTIVE)))
329         {
330                 printf("isic%d: Could not get an irq.\n",unit);
331                 isic_detach_Epcc16(dev);
332                 return ENXIO;
333         }
334
335         /* get the irq number */
336         sc->sc_irq = rman_get_start(sc->sc_resources.irq);
337
338         /* check IRQ validity */        
339         switch(sc->sc_irq)
340         {
341                 case 2:
342                 case 9:         
343                 case 3:         
344                 case 5:
345                 case 10:
346                 case 11:
347                 case 15:                
348                         break;
349                         
350                 default:
351                         printf("isic%d: Error, invalid IRQ [%d] specified for ELSA MicroLink ISDN/PCC-16!\n",
352                                 unit, sc->sc_irq);
353                         isic_detach_Epcc16(dev);
354                         return(ENXIO);
355                         break;
356         }
357
358         /* register interupt routine */
359         bus_setup_intr(dev,sc->sc_resources.irq,INTR_TYPE_NET,
360                         (void(*)(void *))(isicintr),
361                         sc,&ih);
362
363
364         return (0);
365 }
366
367 /*---------------------------------------------------------------------------*
368  * isic_attach_Epcc16 - attach for ELSA MicroLink ISDN/PCC-16
369  *---------------------------------------------------------------------------*/
370 int
371 isic_attach_Epcc16(device_t dev)
372 {
373         int unit = device_get_unit(dev);
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         u_char byte = ELSA_CTRL_SECRET;
379
380         byte &= ~ELSA_CTRL_RESET;
381         bus_space_write_1(t, h, ELSA_OFF_CTRL, byte);
382         DELAY(20);
383         byte |= ELSA_CTRL_RESET;
384         bus_space_write_1(t, h, ELSA_OFF_CTRL, byte);
385
386         DELAY(20);
387         bus_space_write_1(t, h, ELSA_OFF_IRQ, 0xff);
388
389         return 0;
390 }
391
392 #endif /* (NISIC > 0) && defined(ELSA_PCC16) */