a9d66d09a861b211fbb867d9d7ae43f1ec124e94
[dragonfly.git] / sys / dev / disk / stg / tmc18c30_isa.c
1 /*      $FreeBSD: src/sys/dev/stg/tmc18c30_isa.c,v 1.2.2.4 2001/09/04 04:45:23 non Exp $        */
2 /*      $DragonFly: src/sys/dev/disk/stg/tmc18c30_isa.c,v 1.8 2005/10/12 17:35:50 dillon Exp $  */
3 /*      $NecBSD: tmc18c30_pisa.c,v 1.22 1998/11/26 01:59:21 honda Exp $ */
4 /*      $NetBSD$        */
5
6 /*
7 * [Ported for FreeBSD]
8 *  Copyright (c) 2000
9 *      Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
10 *      All rights reserved.
11 * [NetBSD for NEC PC-98 series]
12 *  Copyright (c) 1996, 1997, 1998
13 *       NetBSD/pc98 porting staff. All rights reserved.
14 *  Copyright (c) 1996, 1997, 1998
15 *       Naofumi HONDA. All rights reserved.
16 *  Copyright (c) 1996, 1997, 1998
17 *       Kouichi Matsuda. All rights reserved.
18
19 *  Redistribution and use in source and binary forms, with or without
20 *  modification, are permitted provided that the following conditions
21 *  are met:
22 *  1. Redistributions of source code must retain the above copyright
23 *     notice, this list of conditions and the following disclaimer.
24 *  2. Redistributions in binary form must reproduce the above copyright
25 *     notice, this list of conditions and the following disclaimer in the
26 *     documentation and/or other materials provided with the distribution.
27 *  3. The name of the author may not be used to endorse or promote products
28 *     derived from this software without specific prior written permission.
29
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
39 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/module.h>
47 #include <sys/bus.h>
48 #include <sys/disklabel.h>
49 #include <sys/buf.h>
50 #include <sys/queue.h>
51 #include <sys/malloc.h>
52 #include <sys/errno.h>
53 #include <sys/thread2.h>
54
55 #include <vm/vm.h>
56
57 #include <machine/bus_pio.h>
58 #include <machine/bus.h>
59 #include <machine/resource.h>
60 #include <sys/rman.h>
61
62 #include <bus/isa/isavar.h>
63
64 #include <machine/dvcfg.h>
65
66 #include <sys/device_port.h>
67
68 #include <bus/cam/scsi/scsi_low.h>
69 #include <bus/isa/isa_common.h>
70 #include <bus/cam/scsi/scsi_low_pisa.h>
71
72 #include "tmc18c30reg.h"
73 #include "tmc18c30var.h"
74
75 #define STG_HOSTID      7
76
77 #include        <sys/kernel.h>
78 #include        <sys/module.h>
79
80 static  int     stgprobe(device_t devi);
81 static  int     stgattach(device_t devi);
82
83 static  void    stg_isa_unload  (device_t);
84
85 static void
86 stg_isa_intr(void * arg)
87 {
88         stgintr(arg);
89 }
90
91 static void
92 stg_release_resource(device_t dev)
93 {
94         struct stg_softc        *sc = device_get_softc(dev);
95
96         if (sc->stg_intrhand) {
97                 bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
98         }
99
100         if (sc->port_res) {
101                 bus_release_resource(dev, SYS_RES_IOPORT,
102                                      sc->port_rid, sc->port_res);
103         }
104
105         if (sc->irq_res) {
106                 bus_release_resource(dev, SYS_RES_IRQ,
107                                      sc->irq_rid, sc->irq_res);
108         }
109
110         if (sc->mem_res) {
111                 bus_release_resource(dev, SYS_RES_MEMORY,
112                                      sc->mem_rid, sc->mem_res);
113         }
114 }
115
116 static int
117 stg_alloc_resource(device_t dev)
118 {
119         struct stg_softc        *sc = device_get_softc(dev);
120         u_long                  maddr, msize;
121         int                     error;
122
123         sc->port_rid = 0;
124         sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
125                                           0, ~0, STGIOSZ, RF_ACTIVE);
126         if (sc->port_res == NULL) {
127                 stg_release_resource(dev);
128                 return(ENOMEM);
129         }
130
131         sc->irq_rid = 0;
132         sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
133                                          0, ~0, 1, RF_ACTIVE);
134         if (sc->irq_res == NULL) {
135                 stg_release_resource(dev);
136                 return(ENOMEM);
137         }
138
139         error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
140         if (error) {
141                 return(0);      /* XXX */
142         }
143
144         /* no need to allocate memory if not configured */
145         if (maddr == 0 || msize == 0) {
146                 return(0);
147         }
148
149         sc->mem_rid = 0;
150         sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
151                                          0, ~0, 1, RF_ACTIVE);
152         if (sc->mem_res == NULL) {
153                 stg_release_resource(dev);
154                 return(ENOMEM);
155         }
156
157         return(0);
158 }
159
160 static int
161 stg_isa_probe(device_t dev)
162 {
163         struct stg_softc        *sc = device_get_softc(dev);
164         int                     error;
165
166         bzero(sc, sizeof(struct stg_softc));
167
168         error = stg_alloc_resource(dev);
169         if (error) {
170                 return(error);
171         }
172
173         if (stgprobe(dev) == 0) {
174                 stg_release_resource(dev);
175                 return(ENXIO);
176         }
177
178         stg_release_resource(dev);
179
180         return(0);
181 }
182
183 static int
184 stg_isa_attach(device_t dev)
185 {
186         struct stg_softc        *sc = device_get_softc(dev);
187         int                     error;
188
189         error = stg_alloc_resource(dev);
190         if (error) {
191                 return(error);
192         }
193
194         error = bus_setup_intr(dev, sc->irq_res, 0,
195                                stg_isa_intr, (void *)sc,
196                                &sc->stg_intrhand, NULL);
197         if (error) {
198                 stg_release_resource(dev);
199                 return(error);
200         }
201
202         if (stgattach(dev) == 0) {
203                 stg_release_resource(dev);
204                 return(ENXIO);
205         }
206
207         return(0);
208 }
209
210 static  void
211 stg_isa_detach(device_t dev)
212 {
213         stg_isa_unload(dev);
214         stg_release_resource(dev);
215 }
216
217 static device_method_t stg_isa_methods[] = {
218         /* Device interface */
219         DEVMETHOD(device_probe,         stg_isa_probe),
220         DEVMETHOD(device_attach,        stg_isa_attach),
221         DEVMETHOD(device_detach,        stg_isa_detach),
222
223         { 0, 0 }
224 };
225
226 static driver_t stg_isa_driver = {
227         "stg",
228         stg_isa_methods,
229         sizeof(struct stg_softc),
230 };
231
232 static devclass_t stg_devclass;
233
234 DRIVER_MODULE(stg, isa, stg_isa_driver, stg_devclass, 0, 0);
235
236 static  void
237 stg_isa_unload(device_t devi)
238 {
239         struct stg_softc *sc = device_get_softc(devi);
240
241         printf("%s: unload\n",sc->sc_sclow.sl_xname);
242         crit_enter();
243         scsi_low_deactivate((struct scsi_low_softc *)sc);
244         scsi_low_dettach(&sc->sc_sclow);
245         crit_exit();
246 }
247
248 static  int
249 stgprobe(device_t devi)
250 {
251         int rv;
252         struct stg_softc *sc = device_get_softc(devi);
253
254         rv = stgprobesubr(rman_get_bustag(sc->port_res),
255                           rman_get_bushandle(sc->port_res),
256                           device_get_flags(devi));
257
258         return rv;
259 }
260
261 static  int
262 stgattach(device_t devi)
263 {
264         struct stg_softc *sc;
265         struct scsi_low_softc *slp;
266         u_int32_t flags = device_get_flags(devi);
267         u_int iobase = bus_get_resource_start(devi, SYS_RES_IOPORT, 0);
268         char    dvname[16];
269
270         strcpy(dvname,"stg");
271
272
273         if (iobase == 0)
274         {
275                 printf("%s: no ioaddr is given\n", dvname);
276                 return (0);
277         }
278
279         sc = device_get_softc(devi);
280         if (sc == NULL) {
281                 return(0);
282         }
283
284         slp = &sc->sc_sclow;
285         slp->sl_dev = devi;
286         sc->sc_iot = rman_get_bustag(sc->port_res);
287         sc->sc_ioh = rman_get_bushandle(sc->port_res);
288
289         slp->sl_hostid = STG_HOSTID;
290         slp->sl_cfgflags = flags;
291
292         crit_enter();
293         stgattachsubr(sc);
294         crit_exit();
295
296         return(STGIOSZ);
297 }