Get rid of bus_{disable,enable}_intr(), it wasn't generic enough for
[dragonfly.git] / sys / net / i4b / layer1 / isic / i4b_tel_s08.c
1 /*
2  *   Copyright (c) 1996 Arne Helme. All rights reserved.
3  *
4  *   Copyright (c) 1996 Gary Jennejohn. All rights reserved. 
5  *
6  *   Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *   1. Redistributions of source code must retain the above copyright
13  *      notice, this list of conditions and the following disclaimer.
14  *   2. Redistributions in binary form must reproduce the above copyright
15  *      notice, this list of conditions and the following disclaimer in the
16  *      documentation and/or other materials provided with the distribution.
17  *   3. Neither the name of the author nor the names of any co-contributors
18  *      may be used to endorse or promote products derived from this software
19  *      without specific prior written permission.
20  *   4. Altered versions must be plainly marked as such, and must not be
21  *      misrepresented as being the original software and/or documentation.
22  *   
23  *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  *   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  *   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  *   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  *   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  *   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  *   SUCH DAMAGE.
34  *
35  *---------------------------------------------------------------------------
36  *
37  *      isic - I4B Siemens ISDN Chipset Driver for Teles S0/8 and clones
38  *      ================================================================
39  *
40  * $FreeBSD: src/sys/i4b/layer1/isic/i4b_tel_s08.c,v 1.5.2.1 2001/08/10 14:08:39 obrien Exp $
41  * $DragonFly: src/sys/net/i4b/layer1/isic/i4b_tel_s08.c,v 1.5 2005/05/24 20:59:05 dillon Exp $
42  *
43  *      last edit-date: [Wed Jan 24 09:27:58 2001]
44  *
45  *---------------------------------------------------------------------------*/
46
47 #include "use_isic.h"
48 #include "opt_i4b.h"
49
50 #if NISIC > 0 && defined(TEL_S0_8)
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/socket.h>
55 #include <net/if.h>
56
57 #include <machine/md_var.h>
58 #include <net/i4b/include/machine/i4b_ioctl.h>
59 #include <net/i4b/include/machine/i4b_trace.h>
60
61 #include "../i4b_l1.h"
62 #include "i4b_isic.h"
63 #include "i4b_hscx.h"
64
65 #define TELES_S08_MEMSIZE 0x1000
66
67 static const bus_size_t offset[] = { 0x100, 0x180, 0x1c0 };
68
69 /*---------------------------------------------------------------------------*
70  *      Teles S0/8 write register routine
71  *---------------------------------------------------------------------------*/
72 static void
73 tels08_write_reg(struct l1_softc *sc, int what, bus_size_t offs, u_int8_t data)
74 {
75         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.mem);
76         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.mem);
77
78         offs += offset[what];
79
80         if (offs & 0x01)
81                 offs |= 0x200;
82
83         bus_space_write_1(t, h, offs, data);
84 }
85
86 /*---------------------------------------------------------------------------*
87  *      Teles S0/8 read register routine
88  *---------------------------------------------------------------------------*/
89 static u_int8_t
90 tels08_read_reg(struct l1_softc *sc, int what, bus_size_t offs)
91 {
92         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.mem);
93         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.mem);
94
95         offs += offset[what];
96
97         if (offs & 0x01)
98                 offs |= 0x200;
99
100         return bus_space_read_1(t, h, offs);
101 }
102
103 /*---------------------------------------------------------------------------*
104  *      Teles S0/8 fifo write access
105  *---------------------------------------------------------------------------*/
106 static void
107 tels08_write_fifo(struct l1_softc *sc, int what, void *data, size_t size)
108 {
109         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.mem);
110         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.mem);
111         bus_space_write_region_1(t, h, offset[what], data, size);
112 }
113
114 /*---------------------------------------------------------------------------*
115  *      Teles S0/8 fifo read access
116  *---------------------------------------------------------------------------*/
117 static void
118 tels08_read_fifo(struct l1_softc *sc, int what, void *buf, size_t size)
119 {
120         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.mem);
121         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.mem);
122         bus_space_read_region_1(t, h, offset[what], buf, size);
123 }
124
125 /*---------------------------------------------------------------------------*
126  *      isic_probe_s08 - probe for Teles S0/8 and compatibles
127  *---------------------------------------------------------------------------*/
128 int
129 isic_probe_s08(device_t dev)
130 {
131         size_t unit = device_get_unit(dev);     /* get unit */
132         struct l1_softc *sc = 0;                /* pointer to softc */
133         void *ih = 0;                           /* dummy */
134
135         /* check max unit range */
136
137         if(unit >= ISIC_MAXUNIT)
138         {
139                 printf("isic%d: Error, unit %d >= ISIC_MAXUNIT for Teles S0/8!\n",
140                                 unit, unit);
141                 return(ENXIO);  
142         }
143
144         sc = &l1_sc[unit];              /* get pointer to softc */
145         sc->sc_unit = unit;             /* set unit */
146
147         /* see if an io base was supplied */
148
149         if((sc->sc_resources.io_base[0] =
150                         bus_alloc_resource(dev, SYS_RES_IOPORT,
151                                            &sc->sc_resources.io_rid[0],
152                                            0ul, ~0ul, 1, RF_ACTIVE)))
153         {
154                 /* the S0/8 is completely memory mapped ! */
155                 
156                 bus_release_resource(dev,SYS_RES_IOPORT,
157                                      sc->sc_resources.io_rid[0],
158                                      sc->sc_resources.io_base[0]);
159                 printf("isic%d: Error, iobase specified for Teles S0/8!\n", unit);
160                 return(ENXIO);
161         }
162
163         /* allocate memory */
164
165         if(!(sc->sc_resources.mem =
166                 bus_alloc_resource(dev, SYS_RES_MEMORY,
167                                 &sc->sc_resources.mem_rid,
168                                 0ul, ~0ul, TELES_S08_MEMSIZE, RF_ACTIVE)))
169         {
170                 printf("isic%d: Could not allocate memory for Teles S0/8!\n", unit);
171                 return(ENXIO);
172         }
173
174         /* 
175          * get virtual addr. it's just needed to see if it is in
176          * the valid range
177          */
178
179         sc->sc_vmem_addr = rman_get_virtual(sc->sc_resources.mem);
180                 
181         /* check if inside memory range of 0xA0000 .. 0xDF000 */
182
183         if((kvtop(sc->sc_vmem_addr) < 0xa0000) ||
184            (kvtop(sc->sc_vmem_addr) > 0xdf000))
185         {
186                 printf("isic%d: Error, mem addr 0x%llx outside 0xA0000-0xDF000 for Teles S0/8!\n",
187                                 unit, kvtop(sc->sc_vmem_addr));
188                 bus_release_resource(dev,SYS_RES_MEMORY,
189                                      sc->sc_resources.mem_rid,
190                                      sc->sc_resources.mem);
191                 sc->sc_resources.mem = 0;
192                 return(ENXIO);
193         }
194         
195         /* setup ISAC access routines */
196
197         sc->clearirq = NULL;
198
199         sc->readreg = tels08_read_reg;
200         sc->writereg = tels08_write_reg;
201
202         sc->readfifo = tels08_read_fifo;
203         sc->writefifo = tels08_write_fifo;
204
205         sc->sc_cardtyp = CARD_TYPEP_8;          /* setup card type */
206         
207         sc->sc_bustyp = BUS_TYPE_IOM1;          /* setup IOM bus type */
208
209         sc->sc_ipac = 0;
210         sc->sc_bfifolen = HSCX_FIFO_LEN;
211
212         /* setup ISAC base addr, though we don't really need it */
213         
214         ISAC_BASE = (caddr_t)((sc->sc_vmem_addr) + 0x100);
215
216         /* setup HSCX base addr */
217         
218         HSCX_A_BASE = (caddr_t)((sc->sc_vmem_addr) + 0x180);
219         HSCX_B_BASE = (caddr_t)((sc->sc_vmem_addr) + 0x1c0);
220
221         /* allocate our irq */
222
223         if(!(sc->sc_resources.irq =
224                         bus_alloc_resource(dev, SYS_RES_IRQ,
225                                                 &sc->sc_resources.irq_rid,
226                                                 0ul, ~0ul, 1, RF_ACTIVE)))
227         {
228                 printf("isic%d: Could not allocate irq for Teles S0/8!\n",unit);
229
230                 bus_release_resource(dev,SYS_RES_MEMORY,
231                                      sc->sc_resources.mem_rid,
232                                      sc->sc_resources.mem);
233
234                 sc->sc_resources.mem = 0;
235                 return ENXIO;
236         }
237
238         /* get the irq number */
239
240         sc->sc_irq = rman_get_start(sc->sc_resources.irq);
241         
242         /* check IRQ validity */
243
244         switch(sc->sc_irq)
245         {
246                 case 2:
247                 case 9:         /* XXX */
248                 case 3:
249                 case 4:
250                 case 5:
251                 case 6:
252                 case 7:
253                         break;
254                         
255                 default:
256                         printf("isic%d: Error, invalid IRQ [%d] specified for Teles S0/8!\n",
257                                 unit, sc->sc_irq);
258                         bus_release_resource(dev,SYS_RES_IRQ,
259                                              sc->sc_resources.irq_rid,
260                                              sc->sc_resources.irq);
261                         sc->sc_resources.irq = 0;
262                         bus_release_resource(dev,SYS_RES_MEMORY,
263                                              sc->sc_resources.mem_rid,
264                                              sc->sc_resources.mem);
265                         sc->sc_resources.mem = 0;
266                         return(ENXIO);
267                         break;
268         }
269
270         /* register interupt routine */
271
272         bus_setup_intr(dev, sc->sc_resources.irq,
273                         INTR_TYPE_NET,
274                         (void(*)(void *))(isicintr),
275                         sc, &ih, NULL);
276
277         return (0);
278 }
279
280 /*---------------------------------------------------------------------------*
281  *      isic_attach_s08 - attach Teles S0/8 and compatibles
282  *---------------------------------------------------------------------------*/
283 int
284 isic_attach_s08(device_t dev)
285 {
286         struct l1_softc *sc = &l1_sc[device_get_unit(dev)];
287         bus_space_tag_t t = rman_get_bustag(sc->sc_resources.mem);
288         bus_space_handle_t h = rman_get_bushandle(sc->sc_resources.mem);
289
290         /* set card off */
291
292         bus_space_write_1(t, h, 0x80, 0);
293
294         DELAY(SEC_DELAY / 5);
295
296         /* set card on */
297
298         bus_space_write_1(t, h, 0x80, 1);
299
300         DELAY(SEC_DELAY / 5);
301
302         return 0;
303 }
304 #endif /* ISIC > 0 */