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