09c34e5b9fd58a4ce897641dcf534adacc68a7b7
[dragonfly.git] / sys / bus / isa / vga_isa.c
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
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 as
10  *    the first lines of this file unmodified.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/isa/vga_isa.c,v 1.17 2000/01/29 15:08:56 peter Exp $
27  * $DragonFly: src/sys/bus/isa/vga_isa.c,v 1.9 2005/07/10 13:06:18 swildner Exp $
28  */
29
30 #include "opt_vga.h"
31 #include "opt_fb.h"
32 #include "opt_syscons.h"        /* should be removed in the future, XXX */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/conf.h>
39 #include <sys/bus.h>
40 #include <sys/fbio.h>
41
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44
45 #include <sys/rman.h>
46
47 #include <vm/vm.h>
48 #include <vm/pmap.h>
49
50 #include <machine/md_var.h>
51 #include <machine/pc/bios.h>
52
53 #include <dev/video/fb/fbreg.h>
54 #include <dev/video/fb/vgareg.h>
55
56 #include "isareg.h"
57 #include "isavar.h"
58
59 #define VGA_SOFTC(unit)         \
60         ((vga_softc_t *)devclass_get_softc(isavga_devclass, unit))
61
62 static devclass_t       isavga_devclass;
63
64 static int              isavga_probe(device_t dev);
65 static int              isavga_attach(device_t dev);
66 static int              isavga_suspend(device_t dev);
67 static int              isavga_resume(device_t dev);
68
69 static device_method_t isavga_methods[] = {
70         DEVMETHOD(device_probe,         isavga_probe),
71         DEVMETHOD(device_attach,        isavga_attach),
72         DEVMETHOD(device_suspend,       isavga_suspend),
73         DEVMETHOD(device_resume,        isavga_resume),
74
75         DEVMETHOD(bus_print_child,      bus_generic_print_child),
76         { 0, 0 }
77 };
78
79 static driver_t isavga_driver = {
80         VGA_DRIVER_NAME,
81         isavga_methods,
82         sizeof(vga_softc_t),
83 };
84
85 DRIVER_MODULE(vga, isa, isavga_driver, isavga_devclass, 0, 0);
86
87 #ifdef FB_INSTALL_CDEV
88
89 static d_open_t         isavga_open;
90 static d_close_t        isavga_close;
91 static d_read_t         isavga_read;
92 static d_write_t        isavga_write;
93 static d_ioctl_t        isavga_ioctl;
94 static d_mmap_t         isavga_mmap;
95
96 static struct cdevsw isavga_cdevsw = {
97         /* name */      VGA_DRIVER_NAME,
98         /* maj */       -1,
99         /* flags */     0,
100         /* port */      NULL,
101         /* clone */     NULL,
102
103         /* open */      isavga_open,
104         /* close */     isavga_close,
105         /* read */      isavga_read,
106         /* write */     isavga_write,
107         /* ioctl */     isavga_ioctl,
108         /* poll */      nopoll,
109         /* mmap */      isavga_mmap,
110         /* strategy */  nostrategy,
111         /* dump */      nodump,
112         /* psize */     nopsize
113 };
114
115 #endif /* FB_INSTALL_CDEV */
116
117 static int
118 isavga_probe(device_t dev)
119 {
120         video_adapter_t adp;
121         device_t bus;
122         int error;
123
124         /* No pnp support */
125         if (isa_get_vendorid(dev))
126                 return (ENXIO);
127
128         device_set_desc(dev, "Generic ISA VGA");
129         error = vga_probe_unit(device_get_unit(dev), &adp, device_get_flags(dev));
130         if (error == 0) {
131                 bus = device_get_parent(dev);
132                 bus_set_resource(dev, SYS_RES_IOPORT, 0,
133                                  adp.va_io_base, adp.va_io_size);
134                 bus_set_resource(dev, SYS_RES_MEMORY, 0,
135                                  adp.va_mem_base, adp.va_mem_size);
136 #if 0
137                 isa_set_port(dev, adp.va_io_base);
138                 isa_set_portsize(dev, adp.va_io_size);
139                 isa_set_maddr(dev, adp.va_mem_base);
140                 isa_set_msize(dev, adp.va_mem_size);
141 #endif
142         }
143         return error;
144 }
145
146 static int
147 isavga_attach(device_t dev)
148 {
149         vga_softc_t *sc;
150         struct resource *port;
151         struct resource *mem;
152         int unit;
153         int rid;
154         int error;
155
156         unit = device_get_unit(dev);
157         sc = device_get_softc(dev);
158
159         rid = 0;
160         port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
161                                   0, ~0, 0, RF_ACTIVE | RF_SHAREABLE);
162         rid = 0;
163         mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
164                                  0, ~0, 0, RF_ACTIVE | RF_SHAREABLE);
165
166         error = vga_attach_unit(unit, sc, device_get_flags(dev));
167         if (error)
168                 return error;
169
170 #ifdef FB_INSTALL_CDEV
171         /* attach a virtual frame buffer device */
172         cdevsw_add(&isavga_cdevsw, VGA_MKMINOR(-1), VGA_MKMINOR(unit));
173         sc->devt = make_dev(&isavga_cdevsw, VGA_MKMINOR(unit), 0, 0, 02660, "vga%x", VGA_MKMINOR(unit));
174         reference_dev(sc->devt);
175         error = fb_attach(sc->devt, sc->adp);
176         if (error)
177                 return error;
178 #endif /* FB_INSTALL_CDEV */
179
180         if (bootverbose)
181                 (*vidsw[sc->adp->va_index]->diag)(sc->adp, bootverbose);
182
183 #if experimental
184         device_add_child(dev, "fb", -1);
185         bus_generic_attach(dev);
186 #endif
187
188         return 0;
189 }
190
191 static int
192 isavga_suspend(device_t dev)
193 {
194         vga_softc_t *sc;
195         int err, nbytes;
196
197         sc = device_get_softc(dev);
198         err = bus_generic_suspend(dev);
199         if (err)
200                 return (err);
201
202         /* Save the video state across the suspend. */
203         if (sc->state_buf != NULL) {
204                 free(sc->state_buf, M_TEMP);
205                 sc->state_buf = NULL;
206         }
207         nbytes = (*vidsw[sc->adp->va_index]->save_state)(sc->adp, NULL, 0);
208         if (nbytes <= 0)
209                 return (0);
210         sc->state_buf = malloc(nbytes, M_TEMP, M_NOWAIT | M_ZERO);
211         if (sc->state_buf == NULL)
212                 return (0);
213         if (bootverbose)
214                 device_printf(dev, "saving %d bytes of video state\n", nbytes);
215         if ((*vidsw[sc->adp->va_index]->save_state)(sc->adp, sc->state_buf,
216             nbytes) != 0) {
217                 device_printf(dev, "failed to save state (nbytes=%d)\n",
218                     nbytes);
219                 free(sc->state_buf, M_TEMP);
220                 sc->state_buf = NULL;
221         }
222         return (0);
223 }
224
225 static int
226 isavga_resume(device_t dev)
227 {
228         vga_softc_t *sc;
229
230         sc = device_get_softc(dev);
231         if (sc->state_buf != NULL) {
232                 if ((*vidsw[sc->adp->va_index]->load_state)(sc->adp,
233                     sc->state_buf) != 0)
234                         device_printf(dev, "failed to reload state\n");
235                 free(sc->state_buf, M_TEMP);
236                 sc->state_buf = NULL;
237         }
238
239         bus_generic_resume(dev);
240         return 0;
241 }
242
243 #ifdef FB_INSTALL_CDEV
244
245 static int
246 isavga_open(dev_t dev, int flag, int mode, struct thread *td)
247 {
248         return vga_open(dev, VGA_SOFTC(VGA_UNIT(dev)), flag, mode, td);
249 }
250
251 static int
252 isavga_close(dev_t dev, int flag, int mode, struct thread *td)
253 {
254         return vga_close(dev, VGA_SOFTC(VGA_UNIT(dev)), flag, mode, td);
255 }
256
257 static int
258 isavga_read(dev_t dev, struct uio *uio, int flag)
259 {
260         return vga_read(dev, VGA_SOFTC(VGA_UNIT(dev)), uio, flag);
261 }
262
263 static int
264 isavga_write(dev_t dev, struct uio *uio, int flag)
265 {
266         return vga_write(dev, VGA_SOFTC(VGA_UNIT(dev)), uio, flag);
267 }
268
269 static int
270 isavga_ioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
271 {
272         return vga_ioctl(dev, VGA_SOFTC(VGA_UNIT(dev)), cmd, arg, flag, td);
273 }
274
275 static int
276 isavga_mmap(dev_t dev, vm_offset_t offset, int prot)
277 {
278         return vga_mmap(dev, VGA_SOFTC(VGA_UNIT(dev)), offset, prot);
279 }
280
281 #endif /* FB_INSTALL_CDEV */