Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / bus / isa / i386 / isa_compat.c
1 /*-
2  * Copyright (c) 1998 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/i386/isa/isa_compat.c,v 1.18.2.1 2001/05/17 23:05:06 imp Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/bus.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <machine/bus.h>
36 #include <sys/rman.h>
37
38 #include <machine/vmparam.h>
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 #include <machine/pmap.h>
42 #include <machine/md_var.h>
43
44 #include <machine/resource.h>
45 #include <isa/isavar.h>
46 #include <i386/isa/isa_compat.h>
47 #include <i386/isa/isa_device.h>
48
49 struct isa_compat_resources {
50     struct resource *ports;
51     struct resource *memory;
52     struct resource *drq;
53     struct resource *irq;
54 };
55
56 int
57 isa_compat_nextid(void)
58 {
59         static int id = 2;      /* id_id of -1, 0 and 1 are "used" */
60
61         return id++;
62 }
63
64 static void
65 isa_compat_alloc_resources(device_t dev, struct isa_compat_resources *res)
66 {
67         int rid;
68         u_long start, count;
69
70         if (bus_get_resource(dev, SYS_RES_IOPORT, 0,
71                              &start, &count) == 0) {
72                 rid = 0;
73                 res->ports = bus_alloc_resource(dev, SYS_RES_IOPORT,
74                                                 &rid, 0ul, ~0ul, 1,
75                                                 RF_ACTIVE);
76                 if (res->ports == NULL && bootverbose)
77                         printf("isa_compat: didn't get ports for %s\n",
78                                device_get_name(dev));
79         } else
80                 res->ports = 0;
81
82         if (bus_get_resource(dev, SYS_RES_MEMORY, 0,
83                              &start, &count) == 0
84             && start != 0) {
85                 rid = 0;
86                 res->memory = bus_alloc_resource(dev, SYS_RES_MEMORY,
87                                                  &rid, 0ul, ~0ul, 1,
88                                                  RF_ACTIVE);
89                 if (res->memory == NULL && bootverbose)
90                         printf("isa_compat: didn't get memory for %s\n",
91                                device_get_name(dev));
92         } else
93                 res->memory = 0;
94
95         if (bus_get_resource(dev, SYS_RES_DRQ, 0,
96                              &start, &count) == 0) {
97                 rid = 0;
98                 res->drq = bus_alloc_resource(dev, SYS_RES_DRQ,
99                                               &rid, 0ul, ~0ul, 1,
100                                               RF_ACTIVE);
101                 if (res->drq == NULL && bootverbose)
102                         printf("isa_compat: didn't get drq for %s\n",
103                                device_get_name(dev));
104         } else
105                 res->drq = 0;
106
107         if (bus_get_resource(dev, SYS_RES_IRQ, 0,
108                              &start, &count) == 0) {
109                 rid = 0;
110                 res->irq = bus_alloc_resource(dev, SYS_RES_IRQ,
111                                               &rid, 0ul, ~0ul, 1,
112                                               RF_SHAREABLE | RF_ACTIVE);
113                 if (res->irq == NULL && bootverbose)
114                         printf("isa_compat: didn't get irq for %s\n",
115                                device_get_name(dev));
116         } else
117                 res->irq = 0;
118 }
119
120 static void
121 isa_compat_release_resources(device_t dev, struct isa_compat_resources *res)
122 {
123         if (res->ports) {
124                 bus_release_resource(dev, SYS_RES_IOPORT, 0, res->ports);
125                 res->ports = 0;
126         }
127         if (res->memory) {
128                 bus_release_resource(dev, SYS_RES_MEMORY, 0, res->memory);
129                 res->memory = 0;
130         }
131         if (res->drq) {
132                 bus_release_resource(dev, SYS_RES_DRQ, 0, res->drq);
133                 res->drq = 0;
134         }
135         if (res->irq) {
136                 bus_release_resource(dev, SYS_RES_IRQ, 0, res->irq);
137                 res->irq = 0;
138         }
139 }
140
141 #define irqmask(x)      ((x) < 0 ? 0 : (1 << (x)))
142
143 static int
144 isa_compat_probe(device_t dev)
145 {
146         struct isa_device *dvp = device_get_softc(dev);
147         struct isa_compat_resources res;
148         struct old_isa_driver *op;
149         u_long start, count;
150
151         /* No pnp support */
152         if (isa_get_vendorid(dev))
153                 return (ENXIO);
154
155         bzero(&res, sizeof(res));
156         /*
157          * Fill in the isa_device fields.
158          */
159         op = device_get_driver(dev)->priv;
160         dvp->id_id = isa_compat_nextid();
161         dvp->id_driver = op->driver;
162         if (bus_get_resource(dev, SYS_RES_IOPORT, 0,
163                              &start, &count) == 0)
164                 dvp->id_iobase = start;
165         else
166                 dvp->id_iobase = -1;
167         if (bus_get_resource(dev, SYS_RES_IRQ, 0,
168                              &start, &count) == 0)
169                 dvp->id_irq = irqmask(start);
170         else
171                 dvp->id_irq = 0;
172         if (bus_get_resource(dev, SYS_RES_DRQ, 0,
173                              &start, &count) == 0)
174                 dvp->id_drq = start;
175         else
176                 dvp->id_drq = -1;
177         if (bus_get_resource(dev, SYS_RES_MEMORY,
178                              0, &start, &count) == 0) {
179                 dvp->id_maddr = (void *)(uintptr_t)start;
180                 dvp->id_msize = count;
181         } else {
182                 dvp->id_maddr = NULL;
183                 dvp->id_msize = 0;
184         }
185         dvp->id_unit = device_get_unit(dev);
186         dvp->id_flags = device_get_flags(dev);
187         dvp->id_enabled = device_is_enabled(dev);       /* XXX unused */
188         dvp->id_device = dev;
189
190         /*
191          * Do the wrapped probe.
192          */
193         if (dvp->id_driver->probe) {
194                 int portsize;
195                 void *maddr;
196                 struct isa_device old;
197
198                 isa_compat_alloc_resources(dev, &res);
199                 if (res.memory)
200                         maddr = rman_get_virtual(res.memory);
201                 else
202                         maddr = 0;
203                 dvp->id_maddr = maddr;
204                 old = *dvp;
205                 portsize = dvp->id_driver->probe(dvp);
206                 isa_compat_release_resources(dev, &res);
207                 if (portsize != 0) {
208                         if (portsize > 0 || dvp->id_iobase != old.id_iobase)
209                                 bus_set_resource(dev, SYS_RES_IOPORT,
210                                                  0, dvp->id_iobase, portsize);
211                         if (dvp->id_irq != old.id_irq)
212                                 bus_set_resource(dev, SYS_RES_IRQ, 0,
213                                                  ffs(dvp->id_irq) - 1, 1);
214                         if (dvp->id_drq != old.id_drq)
215                                 bus_set_resource(dev, SYS_RES_DRQ, 0,
216                                                  dvp->id_drq, 1);
217                         if (dvp->id_maddr != old.id_maddr
218                             || dvp->id_msize != old.id_msize) {
219                                 maddr = dvp->id_maddr;
220                                 if (maddr != NULL)
221                                         bus_set_resource(dev,
222                                                          SYS_RES_MEMORY,
223                                                          0,
224                                                          kvtop(maddr),
225                                                          dvp->id_msize);
226                                 else
227                                         bus_delete_resource(dev,
228                                                             SYS_RES_MEMORY,
229                                                             0);
230                         }
231                         return 0;
232                 }
233         }
234         return ENXIO;
235 }
236
237 static int
238 isa_compat_attach(device_t dev)
239 {
240         struct isa_device *dvp = device_get_softc(dev);
241         struct isa_compat_resources res;
242         int error;
243
244         bzero(&res, sizeof(res));
245         isa_compat_alloc_resources(dev, &res);
246         if (dvp->id_driver->attach)
247                 dvp->id_driver->attach(dvp);
248         if (res.irq && dvp->id_irq && dvp->id_intr) {
249                 struct old_isa_driver *op;
250                 void *ih;
251
252                 op = device_get_driver(dev)->priv;
253                 error = BUS_SETUP_INTR(device_get_parent(dev), dev,
254                                        res.irq, op->type,
255                                        dvp->id_intr,
256                                        (void *)(uintptr_t)dvp->id_unit,
257                                        &ih);
258                 if (error)
259                         printf("isa_compat_attach: failed to setup intr: %d\n",
260                                error);
261         }
262         device_printf(dev, "driver is using old-style compatability shims\n");
263         return 0;
264 }
265
266 static device_method_t isa_compat_methods[] = {
267         /* Device interface */
268         DEVMETHOD(device_probe,         isa_compat_probe),
269         DEVMETHOD(device_attach,        isa_compat_attach),
270
271         { 0, 0 }
272 };
273
274 /*
275  * Create a new style driver around each old isa driver.
276  */
277 void
278 isa_wrap_old_drivers(void)
279 {
280         int i;
281         struct old_isa_driver *op;
282         devclass_t isa_devclass = devclass_find("isa");
283
284         for (i = 0, op = &old_drivers[0]; i < old_drivers_count; i++, op++) {
285                 driver_t *driver;
286                 driver = malloc(sizeof(driver_t), M_DEVBUF, M_NOWAIT);
287                 if (!driver)
288                         continue;
289                 bzero(driver, sizeof(driver_t));
290                 driver->name = op->driver->name;
291                 driver->methods = isa_compat_methods;
292                 driver->softc = sizeof(struct isa_device);
293                 driver->priv = op;
294                 if (op->driver->sensitive_hw)
295                         resource_set_int(op->driver->name, -1, "sensitive", 1);
296                 devclass_add_driver(isa_devclass, driver);
297         }
298 }
299
300 int
301 haveseen_iobase(struct isa_device *dvp, int size)
302 {
303         int rid;
304         struct resource *res;
305         device_t dev = dvp->id_device;
306         int base = dvp->id_iobase;
307
308         /*
309          * Ask for resource 1 so that we don't hurt our hints.  In theory
310          * this should work, but....
311          */
312         rid = 1;
313         res = bus_alloc_resource(dev, SYS_RES_IOPORT,
314                                  &rid, base, base + size, size, RF_ACTIVE);
315         if (res)
316                 bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
317         return res ? 0 : 1;
318 }