xref: /freebsd/sys/isa/vga_isa.c (revision 97cb52fa9aefd90fad38790fded50905aeeb9b9e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
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 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_vga.h"
33 #include "opt_fb.h"
34 #include "opt_syscons.h"	/* should be removed in the future, XXX */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/conf.h>
42 #include <sys/bus.h>
43 #include <sys/fbio.h>
44 
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 
48 #include <sys/rman.h>
49 
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52 
53 #include <machine/md_var.h>
54 #ifdef __i386__
55 #include <machine/pc/bios.h>
56 #endif
57 
58 #include <dev/fb/fbreg.h>
59 #include <dev/fb/vgareg.h>
60 
61 #include <isa/isareg.h>
62 #include <isa/isavar.h>
63 
64 static void
65 vga_suspend(device_t dev)
66 {
67 	vga_softc_t *sc;
68 	int nbytes;
69 
70 	sc = device_get_softc(dev);
71 
72 	/* Save the video state across the suspend. */
73 	if (sc->state_buf != NULL)
74 		goto save_palette;
75 	nbytes = vidd_save_state(sc->adp, NULL, 0);
76 	if (nbytes <= 0)
77 		goto save_palette;
78 	sc->state_buf = malloc(nbytes, M_TEMP, M_NOWAIT);
79 	if (sc->state_buf == NULL)
80 		goto save_palette;
81 	if (bootverbose)
82 		device_printf(dev, "saving %d bytes of video state\n", nbytes);
83 	if (vidd_save_state(sc->adp, sc->state_buf, nbytes) != 0) {
84 		device_printf(dev, "failed to save state (nbytes=%d)\n",
85 		    nbytes);
86 		free(sc->state_buf, M_TEMP);
87 		sc->state_buf = NULL;
88 	}
89 
90 save_palette:
91 	/* Save the color palette across the suspend. */
92 	if (sc->pal_buf != NULL)
93 		return;
94 	sc->pal_buf = malloc(256 * 3, M_TEMP, M_NOWAIT);
95 	if (sc->pal_buf == NULL)
96 		return;
97 	if (bootverbose)
98 		device_printf(dev, "saving color palette\n");
99 	if (vidd_save_palette(sc->adp, sc->pal_buf) != 0) {
100 		device_printf(dev, "failed to save palette\n");
101 		free(sc->pal_buf, M_TEMP);
102 		sc->pal_buf = NULL;
103 	}
104 }
105 
106 static void
107 vga_resume(device_t dev)
108 {
109 	vga_softc_t *sc;
110 
111 	sc = device_get_softc(dev);
112 
113 	if (sc->state_buf != NULL) {
114 		if (vidd_load_state(sc->adp, sc->state_buf) != 0)
115 			device_printf(dev, "failed to reload state\n");
116 		free(sc->state_buf, M_TEMP);
117 		sc->state_buf = NULL;
118 	}
119 	if (sc->pal_buf != NULL) {
120 		if (vidd_load_palette(sc->adp, sc->pal_buf) != 0)
121 			device_printf(dev, "failed to reload palette\n");
122 		free(sc->pal_buf, M_TEMP);
123 		sc->pal_buf = NULL;
124 	}
125 }
126 
127 #define VGA_SOFTC(unit)		\
128 	((vga_softc_t *)devclass_get_softc(isavga_devclass, unit))
129 
130 static devclass_t	isavga_devclass;
131 
132 #ifdef FB_INSTALL_CDEV
133 
134 static d_open_t		isavga_open;
135 static d_close_t	isavga_close;
136 static d_read_t		isavga_read;
137 static d_write_t	isavga_write;
138 static d_ioctl_t	isavga_ioctl;
139 static d_mmap_t		isavga_mmap;
140 
141 static struct cdevsw isavga_cdevsw = {
142 	.d_version =	D_VERSION,
143 	.d_flags =	D_NEEDGIANT,
144 	.d_open =	isavga_open,
145 	.d_close =	isavga_close,
146 	.d_read =	isavga_read,
147 	.d_write =	isavga_write,
148 	.d_ioctl =	isavga_ioctl,
149 	.d_mmap =	isavga_mmap,
150 	.d_name =	VGA_DRIVER_NAME,
151 };
152 
153 #endif /* FB_INSTALL_CDEV */
154 
155 static void
156 isavga_identify(driver_t *driver, device_t parent)
157 {
158 	BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, VGA_DRIVER_NAME, 0);
159 }
160 
161 static int
162 isavga_probe(device_t dev)
163 {
164 	video_adapter_t adp;
165 	int error;
166 
167 	/* No pnp support */
168 	if (isa_get_vendorid(dev))
169 		return (ENXIO);
170 
171 	error = vga_probe_unit(device_get_unit(dev), &adp, device_get_flags(dev));
172 	if (error == 0) {
173 		device_set_desc(dev, "Generic ISA VGA");
174 		bus_set_resource(dev, SYS_RES_IOPORT, 0,
175 				 adp.va_io_base, adp.va_io_size);
176 		bus_set_resource(dev, SYS_RES_MEMORY, 0,
177 				 adp.va_mem_base, adp.va_mem_size);
178 #if 0
179 		isa_set_port(dev, adp.va_io_base);
180 		isa_set_portsize(dev, adp.va_io_size);
181 		isa_set_maddr(dev, adp.va_mem_base);
182 		isa_set_msize(dev, adp.va_mem_size);
183 #endif
184 	}
185 	return (error);
186 }
187 
188 static int
189 isavga_attach(device_t dev)
190 {
191 	vga_softc_t *sc;
192 	int unit;
193 	int rid;
194 	int error;
195 
196 	unit = device_get_unit(dev);
197 	sc = device_get_softc(dev);
198 
199 	rid = 0;
200 	bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
201 				  RF_ACTIVE | RF_SHAREABLE);
202 	rid = 0;
203 	bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
204 				 RF_ACTIVE | RF_SHAREABLE);
205 
206 	error = vga_attach_unit(unit, sc, device_get_flags(dev));
207 	if (error)
208 		return (error);
209 
210 #ifdef FB_INSTALL_CDEV
211 	/* attach a virtual frame buffer device */
212 	error = fb_attach(VGA_MKMINOR(unit), sc->adp, &isavga_cdevsw);
213 	if (error)
214 		return (error);
215 #endif /* FB_INSTALL_CDEV */
216 
217 	if (0 && bootverbose)
218 		vidd_diag(sc->adp, bootverbose);
219 
220 #if 0 /* experimental */
221 	device_add_child(dev, "fb", -1);
222 	bus_generic_attach(dev);
223 #endif
224 
225 	return (0);
226 }
227 
228 static int
229 isavga_suspend(device_t dev)
230 {
231 	int error;
232 
233 	error = bus_generic_suspend(dev);
234 	if (error != 0)
235 		return (error);
236 	vga_suspend(dev);
237 
238 	return (error);
239 }
240 
241 static int
242 isavga_resume(device_t dev)
243 {
244 
245 	vga_resume(dev);
246 
247 	return (bus_generic_resume(dev));
248 }
249 
250 #ifdef FB_INSTALL_CDEV
251 
252 static int
253 isavga_open(struct cdev *dev, int flag, int mode, struct thread *td)
254 {
255 	return (vga_open(dev, VGA_SOFTC(VGA_UNIT(dev)), flag, mode, td));
256 }
257 
258 static int
259 isavga_close(struct cdev *dev, int flag, int mode, struct thread *td)
260 {
261 	return (vga_close(dev, VGA_SOFTC(VGA_UNIT(dev)), flag, mode, td));
262 }
263 
264 static int
265 isavga_read(struct cdev *dev, struct uio *uio, int flag)
266 {
267 	return (vga_read(dev, VGA_SOFTC(VGA_UNIT(dev)), uio, flag));
268 }
269 
270 static int
271 isavga_write(struct cdev *dev, struct uio *uio, int flag)
272 {
273 	return (vga_write(dev, VGA_SOFTC(VGA_UNIT(dev)), uio, flag));
274 }
275 
276 static int
277 isavga_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
278 {
279 	return (vga_ioctl(dev, VGA_SOFTC(VGA_UNIT(dev)), cmd, arg, flag, td));
280 }
281 
282 static int
283 isavga_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
284     int prot, vm_memattr_t *memattr)
285 {
286 	return (vga_mmap(dev, VGA_SOFTC(VGA_UNIT(dev)), offset, paddr, prot,
287 	    memattr));
288 }
289 
290 #endif /* FB_INSTALL_CDEV */
291 
292 static device_method_t isavga_methods[] = {
293 	DEVMETHOD(device_identify,	isavga_identify),
294 	DEVMETHOD(device_probe,		isavga_probe),
295 	DEVMETHOD(device_attach,	isavga_attach),
296 	DEVMETHOD(device_suspend,	isavga_suspend),
297 	DEVMETHOD(device_resume,	isavga_resume),
298 
299 	DEVMETHOD_END
300 };
301 
302 static driver_t isavga_driver = {
303 	VGA_DRIVER_NAME,
304 	isavga_methods,
305 	sizeof(vga_softc_t),
306 };
307 
308 DRIVER_MODULE(vga, isa, isavga_driver, isavga_devclass, 0, 0);
309 
310 static devclass_t	vgapm_devclass;
311 
312 static void
313 vgapm_identify(driver_t *driver, device_t parent)
314 {
315 
316 	if (device_get_flags(parent) != 0)
317 		device_add_child(parent, "vgapm", 0);
318 }
319 
320 static int
321 vgapm_probe(device_t dev)
322 {
323 
324 	device_set_desc(dev, "VGA suspend/resume");
325 	device_quiet(dev);
326 
327 	return (BUS_PROBE_DEFAULT);
328 }
329 
330 static int
331 vgapm_attach(device_t dev)
332 {
333 
334 	bus_generic_probe(dev);
335 	bus_generic_attach(dev);
336 
337 	return (0);
338 }
339 
340 static int
341 vgapm_suspend(device_t dev)
342 {
343 	device_t vga_dev;
344 	int error;
345 
346 	error = bus_generic_suspend(dev);
347 	if (error != 0)
348 		return (error);
349 	vga_dev = devclass_get_device(isavga_devclass, 0);
350 	if (vga_dev == NULL)
351 		return (0);
352 	vga_suspend(vga_dev);
353 
354 	return (0);
355 }
356 
357 static int
358 vgapm_resume(device_t dev)
359 {
360 	device_t vga_dev;
361 
362 	vga_dev = devclass_get_device(isavga_devclass, 0);
363 	if (vga_dev != NULL)
364 		vga_resume(vga_dev);
365 
366 	return (bus_generic_resume(dev));
367 }
368 
369 static device_method_t vgapm_methods[] = {
370 	DEVMETHOD(device_identify,	vgapm_identify),
371 	DEVMETHOD(device_probe,		vgapm_probe),
372 	DEVMETHOD(device_attach,	vgapm_attach),
373 	DEVMETHOD(device_suspend,	vgapm_suspend),
374 	DEVMETHOD(device_resume,	vgapm_resume),
375 	{ 0, 0 }
376 };
377 
378 static driver_t vgapm_driver = {
379 	"vgapm",
380 	vgapm_methods,
381 	0
382 };
383 
384 DRIVER_MODULE(vgapm, vgapci, vgapm_driver, vgapm_devclass, 0, 0);
385