rename amd64 architecture to x86_64
[dragonfly.git] / sys / bus / isa / x86_64 / 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 <sys/rman.h>
36
37 #include <machine/vmparam.h>
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 #include <machine/pmap.h>
41 #include <machine/md_var.h>
42
43 #include "../isavar.h"
44 #include "isa_compat.h"
45 #include "isa_device.h"
46
47 struct isa_compat_resources {
48     struct resource *ports;
49     struct resource *memory;
50     struct resource *drq;
51     struct resource *irq;
52 };
53
54 struct isa_compat_driver {
55         driver_t driver;
56         struct old_isa_driver *op;
57 };
58
59 int
60 isa_compat_nextid(void)
61 {
62         static int id = 2;      /* id_id of -1, 0 and 1 are "used" */
63
64         return id++;
65 }
66
67 static void
68 isa_compat_alloc_resources(device_t dev, struct isa_compat_resources *res)
69 {
70         int rid;
71         u_long start, count;
72
73         if (bus_get_resource(dev, SYS_RES_IOPORT, 0,
74                              &start, &count) == 0) {
75                 rid = 0;
76                 res->ports = bus_alloc_resource(dev, SYS_RES_IOPORT,
77                                                 &rid, 0ul, ~0ul, 1,
78                                                 RF_ACTIVE);
79                 if (res->ports == NULL && bootverbose)
80                         kprintf("isa_compat: didn't get ports for %s\n",
81                                device_get_name(dev));
82         } else
83                 res->ports = 0;
84
85         if (bus_get_resource(dev, SYS_RES_MEMORY, 0,
86                              &start, &count) == 0
87             && start != 0) {
88                 rid = 0;
89                 res->memory = bus_alloc_resource(dev, SYS_RES_MEMORY,
90                                                  &rid, 0ul, ~0ul, 1,
91                                                  RF_ACTIVE);
92                 if (res->memory == NULL && bootverbose)
93                         kprintf("isa_compat: didn't get memory for %s\n",
94                                device_get_name(dev));
95         } else
96                 res->memory = 0;
97
98         if (bus_get_resource(dev, SYS_RES_DRQ, 0,
99                              &start, &count) == 0) {
100                 rid = 0;
101                 res->drq = bus_alloc_resource(dev, SYS_RES_DRQ,
102                                               &rid, 0ul, ~0ul, 1,
103                                               RF_ACTIVE);
104                 if (res->drq == NULL && bootverbose)
105                         kprintf("isa_compat: didn't get drq for %s\n",
106                                device_get_name(dev));
107         } else
108                 res->drq = 0;
109
110         if (bus_get_resource(dev, SYS_RES_IRQ, 0,
111                              &start, &count) == 0) {
112                 rid = 0;
113                 res->irq = bus_alloc_resource(dev, SYS_RES_IRQ,
114                                               &rid, 0ul, ~0ul, 1,
115                                               RF_SHAREABLE | RF_ACTIVE);
116                 if (res->irq == NULL && bootverbose)
117                         kprintf("isa_compat: didn't get irq for %s\n",
118                                device_get_name(dev));
119         } else
120                 res->irq = 0;
121 }
122
123 static void
124 isa_compat_release_resources(device_t dev, struct isa_compat_resources *res)
125 {
126         if (res->ports) {
127                 bus_release_resource(dev, SYS_RES_IOPORT, 0, res->ports);
128                 res->ports = 0;
129         }
130         if (res->memory) {
131                 bus_release_resource(dev, SYS_RES_MEMORY, 0, res->memory);
132                 res->memory = 0;
133         }
134         if (res->drq) {
135                 bus_release_resource(dev, SYS_RES_DRQ, 0, res->drq);
136                 res->drq = 0;
137         }
138         if (res->irq) {
139                 bus_release_resource(dev, SYS_RES_IRQ, 0, res->irq);
140                 res->irq = 0;
141         }
142 }
143
144 #define irqmask(x)      ((x) < 0 ? 0 : (1 << (x)))
145
146 static int
147 isa_compat_probe(device_t dev)
148 {
149         struct isa_device *dvp = device_get_softc(dev);
150         struct isa_compat_resources res;
151         struct old_isa_driver *op;
152         u_long start, count;
153
154         /* No pnp support */
155         if (isa_get_vendorid(dev))
156                 return (ENXIO);
157
158         bzero(&res, sizeof(res));
159         /*
160          * Fill in the isa_device fields.
161          */
162         op = ((struct isa_compat_driver *)device_get_driver(dev))->op;
163         dvp->id_id = isa_compat_nextid();
164         dvp->id_driver = op->driver;
165         if (bus_get_resource(dev, SYS_RES_IOPORT, 0,
166                              &start, &count) == 0)
167                 dvp->id_iobase = start;
168         else
169                 dvp->id_iobase = -1;
170         if (bus_get_resource(dev, SYS_RES_IRQ, 0,
171                              &start, &count) == 0)
172                 dvp->id_irq = irqmask(start);
173         else
174                 dvp->id_irq = 0;
175         if (bus_get_resource(dev, SYS_RES_DRQ, 0,
176                              &start, &count) == 0)
177                 dvp->id_drq = start;
178         else
179                 dvp->id_drq = -1;
180         if (bus_get_resource(dev, SYS_RES_MEMORY,
181                              0, &start, &count) == 0) {
182                 dvp->id_maddr = (void *)(uintptr_t)start;
183                 dvp->id_msize = count;
184         } else {
185                 dvp->id_maddr = NULL;
186                 dvp->id_msize = 0;
187         }
188         dvp->id_unit = device_get_unit(dev);
189         dvp->id_flags = device_get_flags(dev);
190         dvp->id_enabled = device_is_enabled(dev);       /* XXX unused */
191         dvp->id_device = dev;
192
193         /*
194          * Do the wrapped probe.
195          */
196         if (dvp->id_driver->probe) {
197                 int portsize;
198                 void *maddr;
199                 struct isa_device old;
200
201                 isa_compat_alloc_resources(dev, &res);
202                 if (res.memory)
203                         maddr = rman_get_virtual(res.memory);
204                 else
205                         maddr = 0;
206                 dvp->id_maddr = maddr;
207                 old = *dvp;
208                 portsize = dvp->id_driver->probe(dvp);
209                 isa_compat_release_resources(dev, &res);
210                 if (portsize != 0) {
211                         if (portsize > 0 || dvp->id_iobase != old.id_iobase)
212                                 bus_set_resource(dev, SYS_RES_IOPORT,
213                                                  0, dvp->id_iobase, portsize);
214                         if (dvp->id_irq != old.id_irq)
215                                 bus_set_resource(dev, SYS_RES_IRQ, 0,
216                                                  ffs(dvp->id_irq) - 1, 1);
217                         if (dvp->id_drq != old.id_drq)
218                                 bus_set_resource(dev, SYS_RES_DRQ, 0,
219                                                  dvp->id_drq, 1);
220                         if (dvp->id_maddr != old.id_maddr
221                             || dvp->id_msize != old.id_msize) {
222                                 maddr = dvp->id_maddr;
223                                 if (maddr != NULL)
224                                         bus_set_resource(dev,
225                                                          SYS_RES_MEMORY,
226                                                          0,
227                                                          kvtop(maddr),
228                                                          dvp->id_msize);
229                                 else
230                                         bus_delete_resource(dev,
231                                                             SYS_RES_MEMORY,
232                                                             0);
233                         }
234                         return 0;
235                 }
236         }
237         return ENXIO;
238 }
239
240 static int
241 isa_compat_attach(device_t dev)
242 {
243         struct isa_device *dvp = device_get_softc(dev);
244         struct isa_compat_resources res;
245         int error;
246
247         bzero(&res, sizeof(res));
248         isa_compat_alloc_resources(dev, &res);
249         if (dvp->id_driver->attach)
250                 dvp->id_driver->attach(dvp);
251         if (res.irq && dvp->id_irq && dvp->id_intr) {
252                 struct old_isa_driver *op;
253                 void *ih;
254
255                 op = ((struct isa_compat_driver *)device_get_driver(dev))->op;
256                 error = BUS_SETUP_INTR(device_get_parent(dev), dev,
257                                        res.irq, op->type,
258                                        dvp->id_intr,
259                                        (void *)(uintptr_t)dvp->id_unit,
260                                        &ih, NULL);
261                 if (error)
262                         kprintf("isa_compat_attach: failed to setup intr: %d\n",
263                                error);
264         }
265         device_printf(dev, "driver is using old-style compatibility shims\n");
266         return 0;
267 }
268
269 static device_method_t isa_compat_methods[] = {
270         /* Device interface */
271         DEVMETHOD(device_probe,         isa_compat_probe),
272         DEVMETHOD(device_attach,        isa_compat_attach),
273
274         { 0, 0 }
275 };
276
277 /*
278  * Create a new style driver around each old isa driver.
279  */
280 void
281 isa_wrap_old_drivers(void)
282 {
283         int i;
284         struct old_isa_driver *op;
285         devclass_t isa_devclass = devclass_find("isa");
286         struct isa_compat_driver *driver;
287
288         for (i = 0, op = &old_drivers[0]; i < old_drivers_count; i++, op++) {
289                 driver = kmalloc(sizeof(struct isa_compat_driver), M_DEVBUF, M_WAITOK | M_ZERO);
290                 driver->driver.name = op->driver->name;
291                 driver->driver.methods = isa_compat_methods;
292                 driver->driver.size = sizeof(struct isa_device);
293                 driver->op = op;
294                 if (op->driver->sensitive_hw)
295                         resource_set_int(op->driver->name, -1, "sensitive", 1);
296                 devclass_add_driver(isa_devclass, (driver_t *)driver);
297         }
298 }