xref: /freebsd/sys/isa/vga_isa.c (revision 76238846ad3e9e271a3d1f792f72beab727fd153)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #define VGA_ID		0x0009d041	/* PNP0900 */
65 
66 static struct isa_pnp_id vga_ids[] = {
67 	{ VGA_ID,	NULL },		/* PNP0900 */
68 	{ 0,		NULL },
69 };
70 
71 static void
72 vga_suspend(device_t dev)
73 {
74 	vga_softc_t *sc;
75 	int nbytes;
76 
77 	sc = device_get_softc(dev);
78 
79 	/* Save the video state across the suspend. */
80 	if (sc->state_buf != NULL)
81 		goto save_palette;
82 	nbytes = vidd_save_state(sc->adp, NULL, 0);
83 	if (nbytes <= 0)
84 		goto save_palette;
85 	sc->state_buf = malloc(nbytes, M_TEMP, M_NOWAIT);
86 	if (sc->state_buf == NULL)
87 		goto save_palette;
88 	if (bootverbose)
89 		device_printf(dev, "saving %d bytes of video state\n", nbytes);
90 	if (vidd_save_state(sc->adp, sc->state_buf, nbytes) != 0) {
91 		device_printf(dev, "failed to save state (nbytes=%d)\n",
92 		    nbytes);
93 		free(sc->state_buf, M_TEMP);
94 		sc->state_buf = NULL;
95 	}
96 
97 save_palette:
98 	/* Save the color palette across the suspend. */
99 	if (sc->pal_buf != NULL)
100 		return;
101 	sc->pal_buf = malloc(256 * 3, M_TEMP, M_NOWAIT);
102 	if (sc->pal_buf == NULL)
103 		return;
104 	if (bootverbose)
105 		device_printf(dev, "saving color palette\n");
106 	if (vidd_save_palette(sc->adp, sc->pal_buf) != 0) {
107 		device_printf(dev, "failed to save palette\n");
108 		free(sc->pal_buf, M_TEMP);
109 		sc->pal_buf = NULL;
110 	}
111 }
112 
113 static void
114 vga_resume(device_t dev)
115 {
116 	vga_softc_t *sc;
117 
118 	sc = device_get_softc(dev);
119 
120 	if (sc->state_buf != NULL) {
121 		if (vidd_load_state(sc->adp, sc->state_buf) != 0)
122 			device_printf(dev, "failed to reload state\n");
123 		free(sc->state_buf, M_TEMP);
124 		sc->state_buf = NULL;
125 	}
126 	if (sc->pal_buf != NULL) {
127 		if (vidd_load_palette(sc->adp, sc->pal_buf) != 0)
128 			device_printf(dev, "failed to reload palette\n");
129 		free(sc->pal_buf, M_TEMP);
130 		sc->pal_buf = NULL;
131 	}
132 }
133 
134 static void
135 isavga_identify(driver_t *driver, device_t parent)
136 {
137 	BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, VGA_DRIVER_NAME, 0);
138 }
139 
140 static int
141 isavga_probe(device_t dev)
142 {
143 	video_adapter_t adp;
144 	int error;
145 
146 	/* No pnp support */
147 	if (isa_get_vendorid(dev))
148 		return (ENXIO);
149 
150 	error = vga_probe_unit(device_get_unit(dev), &adp, device_get_flags(dev));
151 	if (error == 0) {
152 		device_set_desc(dev, "Generic ISA VGA");
153 		bus_set_resource(dev, SYS_RES_IOPORT, 0,
154 				 adp.va_io_base, adp.va_io_size);
155 		bus_set_resource(dev, SYS_RES_MEMORY, 0,
156 				 adp.va_mem_base, adp.va_mem_size);
157 		isa_set_vendorid(dev, VGA_ID);
158 		isa_set_logicalid(dev, VGA_ID);
159 #if 0
160 		isa_set_port(dev, adp.va_io_base);
161 		isa_set_portsize(dev, adp.va_io_size);
162 		isa_set_maddr(dev, adp.va_mem_base);
163 		isa_set_msize(dev, adp.va_mem_size);
164 #endif
165 	}
166 	return (error);
167 }
168 
169 static int
170 isavga_attach(device_t dev)
171 {
172 	vga_softc_t *sc;
173 	int unit;
174 	int rid;
175 	int error;
176 
177 	unit = device_get_unit(dev);
178 	sc = device_get_softc(dev);
179 
180 	rid = 0;
181 	bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
182 				  RF_ACTIVE | RF_SHAREABLE);
183 	rid = 0;
184 	bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
185 				 RF_ACTIVE | RF_SHAREABLE);
186 
187 	error = vga_attach_unit(unit, sc, device_get_flags(dev));
188 	if (error)
189 		return (error);
190 
191 	if (0 && bootverbose)
192 		vidd_diag(sc->adp, bootverbose);
193 
194 #if 0 /* experimental */
195 	device_add_child(dev, "fb", -1);
196 	bus_generic_attach(dev);
197 #endif
198 
199 	return (0);
200 }
201 
202 static int
203 isavga_suspend(device_t dev)
204 {
205 	int error;
206 
207 	error = bus_generic_suspend(dev);
208 	if (error != 0)
209 		return (error);
210 	vga_suspend(dev);
211 
212 	return (error);
213 }
214 
215 static int
216 isavga_resume(device_t dev)
217 {
218 
219 	vga_resume(dev);
220 
221 	return (bus_generic_resume(dev));
222 }
223 
224 static device_method_t isavga_methods[] = {
225 	DEVMETHOD(device_identify,	isavga_identify),
226 	DEVMETHOD(device_probe,		isavga_probe),
227 	DEVMETHOD(device_attach,	isavga_attach),
228 	DEVMETHOD(device_suspend,	isavga_suspend),
229 	DEVMETHOD(device_resume,	isavga_resume),
230 
231 	DEVMETHOD_END
232 };
233 
234 static driver_t isavga_driver = {
235 	VGA_DRIVER_NAME,
236 	isavga_methods,
237 	sizeof(vga_softc_t),
238 };
239 
240 DRIVER_MODULE(vga, isa, isavga_driver, 0, 0);
241 
242 static void
243 vgapm_identify(driver_t *driver, device_t parent)
244 {
245 
246 	if (device_get_flags(parent) != 0)
247 		device_add_child(parent, "vgapm", 0);
248 }
249 
250 static int
251 vgapm_probe(device_t dev)
252 {
253 
254 	device_set_desc(dev, "VGA suspend/resume");
255 	device_quiet(dev);
256 
257 	return (BUS_PROBE_DEFAULT);
258 }
259 
260 static int
261 vgapm_attach(device_t dev)
262 {
263 
264 	bus_generic_probe(dev);
265 	bus_generic_attach(dev);
266 
267 	return (0);
268 }
269 
270 static int
271 vgapm_suspend(device_t dev)
272 {
273 	device_t vga_dev;
274 	int error;
275 
276 	error = bus_generic_suspend(dev);
277 	if (error != 0)
278 		return (error);
279 	vga_dev = devclass_get_device(devclass_find(VGA_DRIVER_NAME), 0);
280 	if (vga_dev == NULL)
281 		return (0);
282 	vga_suspend(vga_dev);
283 
284 	return (0);
285 }
286 
287 static int
288 vgapm_resume(device_t dev)
289 {
290 	device_t vga_dev;
291 
292 	vga_dev = devclass_get_device(devclass_find(VGA_DRIVER_NAME), 0);
293 	if (vga_dev != NULL)
294 		vga_resume(vga_dev);
295 
296 	return (bus_generic_resume(dev));
297 }
298 
299 static device_method_t vgapm_methods[] = {
300 	DEVMETHOD(device_identify,	vgapm_identify),
301 	DEVMETHOD(device_probe,		vgapm_probe),
302 	DEVMETHOD(device_attach,	vgapm_attach),
303 	DEVMETHOD(device_suspend,	vgapm_suspend),
304 	DEVMETHOD(device_resume,	vgapm_resume),
305 	{ 0, 0 }
306 };
307 
308 static driver_t vgapm_driver = {
309 	"vgapm",
310 	vgapm_methods,
311 	0
312 };
313 
314 DRIVER_MODULE(vgapm, vgapci, vgapm_driver, 0, 0);
315 ISA_PNP_INFO(vga_ids);
316