Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / net / i4b / layer1 / isic / i4b_ctx_s0P.c
1 /*
2  * Copyright (c) 1997, 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 Siemens ISDN Chipset Driver for Creatix/Teles PnP
28  *      ============================================================
29  *
30  * $FreeBSD: src/sys/i4b/layer1/isic/i4b_ctx_s0P.c,v 1.5.2.2 2001/12/10 12:18:11 hm Exp $
31  *
32  *      last edit-date: [Wed Jan 24 09:07:22 2001]
33  *
34  *      Note: this driver works for the Creatix ISDN S0-16 P+P and
35  *            for the Teles S0/16.3 PnP card. Although they are not
36  *            the same hardware and don't share the same PnP config
37  *            information, once the base addresses are set, the
38  *            offsets are same and therefore they can use the same
39  *            driver.
40  *
41  *---------------------------------------------------------------------------*/
42
43 #include "isic.h"
44 #include "opt_i4b.h"
45
46 #if (NISIC > 0) && (defined(CRTX_S0_P) || defined(TEL_S0_16_3_P) || defined(COMPAQ_M610))
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/socket.h>
51 #include <net/if.h>
52
53 #include <machine/i4b_ioctl.h>
54 #include <machine/i4b_trace.h>
55
56 #include <i4b/layer1/i4b_l1.h>
57 #include <i4b/layer1/isic/i4b_isic.h>
58 #include <i4b/layer1/isic/i4b_hscx.h>
59
60 /*---------------------------------------------------------------------------*
61  *      Creatix / Teles PnP ISAC get fifo routine
62  *---------------------------------------------------------------------------*/
63 static void
64 ctxs0P_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
65 {
66         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[what+2]);
67         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+2]);
68         bus_space_read_multi_1(t,h,0x3e,buf,size);
69 }
70
71 /*---------------------------------------------------------------------------*
72  *      Creatix / Teles PnP ISAC put fifo routine
73  *---------------------------------------------------------------------------*/
74 static void
75 ctxs0P_write_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
76 {
77         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[what+2]);
78         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+2]);
79         bus_space_write_multi_1(t,h,0x3e,buf,size);
80 }
81
82 /*---------------------------------------------------------------------------*
83  *      Creatix / Teles PnP ISAC put register routine
84  *---------------------------------------------------------------------------*/
85 static void
86 ctxs0P_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
87 {
88         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[what+2]);
89         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+2]);
90         bus_space_write_1(t,h,offs,data);
91 }
92
93 /*---------------------------------------------------------------------------*
94  *      Creatix / Teles PnP ISAC get register routine
95  *---------------------------------------------------------------------------*/
96 static u_int8_t
97 ctxs0P_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
98 {
99         bus_space_tag_t    t = rman_get_bustag(sc->sc_resources.io_base[what+2]);
100         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.io_base[what+2]);
101         return bus_space_read_1(t,h,offs);
102 }
103
104 /*---------------------------------------------------------------------------*
105  *      isic_attach_Cs0P - attach Creatix / Teles PnP
106  *---------------------------------------------------------------------------*/
107 int
108 isic_attach_Cs0P(device_t dev)
109 {
110         u_int32_t iobase1;
111         u_int32_t iobase2;
112         u_int32_t iocfg;
113         int unit = device_get_unit(dev);
114         struct l1_softc *sc = &l1_sc[unit];     
115         bus_space_tag_t t;
116         bus_space_handle_t h;
117
118         /*
119          * this card needs a second io_base,
120          * free resources if we don't get it
121          */
122
123         sc->sc_resources.io_rid[1] = 1;
124         
125         if(!(sc->sc_resources.io_base[1] =
126                         bus_alloc_resource(dev, SYS_RES_IOPORT,
127                                         &sc->sc_resources.io_rid[1],
128                                         0UL, ~0UL, 1, RF_ACTIVE)))
129         {
130                 printf("isic%d: Could not get io area 1 for Creatix / Teles PnP!\n", unit);
131                 isic_detach_common(dev);
132                 return ENXIO;
133         }
134
135         /*
136          * Compaq M610 has a cfg io area,
137          * we need it
138          */
139
140         if (sc->sc_cardtyp == CARD_TYPEP_COMPAQ_M610)
141         {
142                 sc->sc_resources.io_rid[2] = 2;
143         
144                 if(!(sc->sc_resources.io_base[2] =
145                                 bus_alloc_resource(dev, SYS_RES_IOPORT,
146                                                 &sc->sc_resources.io_rid[2],
147                                                 0UL, ~0UL, 1, RF_ACTIVE)))
148                 {
149                         printf("isic%d: Could not get cfg io area for Compaq Microcom 610\n", unit);
150                         isic_detach_common(dev);
151                         return ENXIO;
152                 }
153
154                 iocfg = rman_get_start(sc->sc_resources.io_base[2]);
155
156                 bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_resources.io_rid[2],
157                                 sc->sc_resources.io_base[2]);
158         }
159
160         /* remember the io base addresses */
161         
162         if (sc->sc_cardtyp != CARD_TYPEP_COMPAQ_M610)
163         {
164                 iobase1 = rman_get_start(sc->sc_resources.io_base[0]);
165                 iobase2 = rman_get_start(sc->sc_resources.io_base[1]);
166         }
167         else
168         {
169                 iobase1 = rman_get_start(sc->sc_resources.io_base[1]);
170                 iobase2 = rman_get_start(sc->sc_resources.io_base[0]);
171         }
172
173         /*
174          * because overlapping resources are invalid,
175          * release the first and second io port resource
176          */
177         
178         bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_resources.io_rid[0],
179                         sc->sc_resources.io_base[0]);
180         
181         bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_resources.io_rid[1],
182                         sc->sc_resources.io_base[1]);
183
184         /* set and allocate a base io address for the ISAC chip */
185         
186         sc->sc_resources.io_rid[2] = 2;
187         
188         bus_set_resource(dev, SYS_RES_IOPORT, 2, iobase1-0x20, 0x40);
189
190         if(!(sc->sc_resources.io_base[2] =
191                 bus_alloc_resource(dev, SYS_RES_IOPORT,
192                                    &sc->sc_resources.io_rid[2],
193                                    0ul, ~0ul, 1, RF_ACTIVE)))
194         {
195                 printf("isic%d: Could not get io area 2 for Creatix / Teles PnP!\n", unit);
196                 isic_detach_common(dev);
197                 return ENXIO;
198         }
199
200
201         /* set and allocate a resource for the HSCX channel A */
202         
203         sc->sc_resources.io_rid[3] = 3;
204
205 /*XXX*/ /* FIXME !!!!
206          * the width of the resource is too small, there are accesses
207          * to it with an offset of 0x3e into the next resource. anyway,
208          * it seems to work and i have no idea how to do 2 resources
209          * overlapping each other.
210          */
211
212 #if 0
213         bus_set_resource(dev, SYS_RES_IOPORT, 3, iobase2-0x20, 0x20);
214 #else
215         bus_set_resource(dev, SYS_RES_IOPORT, 3, iobase2-0x20, 0x10);
216 #endif
217
218         if(!(sc->sc_resources.io_base[3] =
219                 bus_alloc_resource(dev,SYS_RES_IOPORT,
220                                    &sc->sc_resources.io_rid[3],
221                                    0ul,~0ul, 1, RF_ACTIVE)))
222         {
223                 printf("isic%d: Could not get io area 3 for Creatix / Teles PnP!\n", unit);
224                 isic_detach_common(dev);
225                 return ENXIO;
226         }
227
228         /* set and allocate a resources for the HSCX channel B */
229         
230         sc->sc_resources.io_rid[4] = 4;
231         
232         bus_set_resource(dev, SYS_RES_IOPORT, 4, iobase2, 0x40);
233
234         if(!(sc->sc_resources.io_base[4] =
235                 bus_alloc_resource(dev,SYS_RES_IOPORT,
236                                    &sc->sc_resources.io_rid[4],
237                                    0ul, ~0ul, 1, RF_ACTIVE)))
238         {
239                 printf("isic%d: Could not get io area 4 for Creatix / Teles PnP!\n", unit);
240                 isic_detach_common(dev);
241                 return ENXIO;
242         }
243
244         /*
245          * set and allocate a resource for the cfg io
246          * for compaq m610
247          */
248
249         if (sc->sc_cardtyp == CARD_TYPEP_COMPAQ_M610)
250         {
251                 sc->sc_resources.io_rid[5] = 5;
252
253                 bus_set_resource(dev, SYS_RES_IOPORT, 5, iocfg, 0x01);
254
255                 if(!(sc->sc_resources.io_base[5] =
256                         bus_alloc_resource(dev,SYS_RES_IOPORT,
257                                            &sc->sc_resources.io_rid[5],
258                                            0ul, ~0ul, 1, RF_ACTIVE)))
259                 {
260                         printf("isic%d: Could not get cfg io area for Compaq Microcom 610!\n", unit);
261                         isic_detach_common(dev);
262                         return ENXIO;
263                 }
264         }
265
266         /* setup access routines */
267
268         sc->clearirq = NULL;
269         sc->readreg = ctxs0P_read_reg;
270         sc->writereg = ctxs0P_write_reg;
271
272         sc->readfifo = ctxs0P_read_fifo;
273         sc->writefifo = ctxs0P_write_fifo;
274
275         /* setup card type */
276
277         if (sc->sc_cardtyp != CARD_TYPEP_COMPAQ_M610)
278                 sc->sc_cardtyp = CARD_TYPEP_CS0P;
279
280         /* setup IOM bus type */
281         
282         sc->sc_bustyp = BUS_TYPE_IOM2;
283
284         sc->sc_ipac = 0;
285         sc->sc_bfifolen = HSCX_FIFO_LEN;
286
287         /* enable the card */
288         
289         if (sc->sc_cardtyp != CARD_TYPEP_COMPAQ_M610)
290         {
291                 t = rman_get_bustag(sc->sc_resources.io_base[2]);
292                 h = rman_get_bushandle(sc->sc_resources.io_base[2]);
293         
294                 bus_space_write_1(t, h, 0x3c, 0);
295                 DELAY(SEC_DELAY / 10);
296
297                 bus_space_write_1(t, h, 0x3c, 1);
298                 DELAY(SEC_DELAY / 10);
299         }
300         else
301         {
302                 t = rman_get_bustag(sc->sc_resources.io_base[5]);
303                 h = rman_get_bushandle(sc->sc_resources.io_base[5]);
304
305                 bus_space_write_1(t, h, 0xff, 0);
306                 DELAY(SEC_DELAY / 10);
307
308                 bus_space_write_1(t, h, 0x00, 1);
309                 DELAY(SEC_DELAY / 10);
310         }
311
312         return 0;
313 }
314
315 #endif /* (NISIC > 0) && (defined(CRTX_S0_P) || defined(TEL_S0_16_3_P) || defined(COMPAQ_M610)) */
316