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