xref: /freebsd/sys/isa/vga_isa.c (revision bd001d86d679e10d179ef00b9866f0e65b6fa7fd)
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 #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 devclass_t	isavga_devclass;
135 
136 static void
137 isavga_identify(driver_t *driver, device_t parent)
138 {
139 	BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, VGA_DRIVER_NAME, 0);
140 }
141 
142 static int
143 isavga_probe(device_t dev)
144 {
145 	video_adapter_t adp;
146 	int error;
147 
148 	/* No pnp support */
149 	if (isa_get_vendorid(dev))
150 		return (ENXIO);
151 
152 	error = vga_probe_unit(device_get_unit(dev), &adp, device_get_flags(dev));
153 	if (error == 0) {
154 		device_set_desc(dev, "Generic ISA VGA");
155 		bus_set_resource(dev, SYS_RES_IOPORT, 0,
156 				 adp.va_io_base, adp.va_io_size);
157 		bus_set_resource(dev, SYS_RES_MEMORY, 0,
158 				 adp.va_mem_base, adp.va_mem_size);
159 		isa_set_vendorid(dev, VGA_ID);
160 		isa_set_logicalid(dev, VGA_ID);
161 #if 0
162 		isa_set_port(dev, adp.va_io_base);
163 		isa_set_portsize(dev, adp.va_io_size);
164 		isa_set_maddr(dev, adp.va_mem_base);
165 		isa_set_msize(dev, adp.va_mem_size);
166 #endif
167 	}
168 	return (error);
169 }
170 
171 static int
172 isavga_attach(device_t dev)
173 {
174 	vga_softc_t *sc;
175 	int unit;
176 	int rid;
177 	int error;
178 
179 	unit = device_get_unit(dev);
180 	sc = device_get_softc(dev);
181 
182 	rid = 0;
183 	bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
184 				  RF_ACTIVE | RF_SHAREABLE);
185 	rid = 0;
186 	bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
187 				 RF_ACTIVE | RF_SHAREABLE);
188 
189 	error = vga_attach_unit(unit, sc, device_get_flags(dev));
190 	if (error)
191 		return (error);
192 
193 	if (0 && bootverbose)
194 		vidd_diag(sc->adp, bootverbose);
195 
196 #if 0 /* experimental */
197 	device_add_child(dev, "fb", -1);
198 	bus_generic_attach(dev);
199 #endif
200 
201 	return (0);
202 }
203 
204 static int
205 isavga_suspend(device_t dev)
206 {
207 	int error;
208 
209 	error = bus_generic_suspend(dev);
210 	if (error != 0)
211 		return (error);
212 	vga_suspend(dev);
213 
214 	return (error);
215 }
216 
217 static int
218 isavga_resume(device_t dev)
219 {
220 
221 	vga_resume(dev);
222 
223 	return (bus_generic_resume(dev));
224 }
225 
226 static device_method_t isavga_methods[] = {
227 	DEVMETHOD(device_identify,	isavga_identify),
228 	DEVMETHOD(device_probe,		isavga_probe),
229 	DEVMETHOD(device_attach,	isavga_attach),
230 	DEVMETHOD(device_suspend,	isavga_suspend),
231 	DEVMETHOD(device_resume,	isavga_resume),
232 
233 	DEVMETHOD_END
234 };
235 
236 static driver_t isavga_driver = {
237 	VGA_DRIVER_NAME,
238 	isavga_methods,
239 	sizeof(vga_softc_t),
240 };
241 
242 DRIVER_MODULE(vga, isa, isavga_driver, isavga_devclass, 0, 0);
243 
244 static devclass_t	vgapm_devclass;
245 
246 static void
247 vgapm_identify(driver_t *driver, device_t parent)
248 {
249 
250 	if (device_get_flags(parent) != 0)
251 		device_add_child(parent, "vgapm", 0);
252 }
253 
254 static int
255 vgapm_probe(device_t dev)
256 {
257 
258 	device_set_desc(dev, "VGA suspend/resume");
259 	device_quiet(dev);
260 
261 	return (BUS_PROBE_DEFAULT);
262 }
263 
264 static int
265 vgapm_attach(device_t dev)
266 {
267 
268 	bus_generic_probe(dev);
269 	bus_generic_attach(dev);
270 
271 	return (0);
272 }
273 
274 static int
275 vgapm_suspend(device_t dev)
276 {
277 	device_t vga_dev;
278 	int error;
279 
280 	error = bus_generic_suspend(dev);
281 	if (error != 0)
282 		return (error);
283 	vga_dev = devclass_get_device(devclass_find(VGA_DRIVER_NAME), 0);
284 	if (vga_dev == NULL)
285 		return (0);
286 	vga_suspend(vga_dev);
287 
288 	return (0);
289 }
290 
291 static int
292 vgapm_resume(device_t dev)
293 {
294 	device_t vga_dev;
295 
296 	vga_dev = devclass_get_device(devclass_find(VGA_DRIVER_NAME), 0);
297 	if (vga_dev != NULL)
298 		vga_resume(vga_dev);
299 
300 	return (bus_generic_resume(dev));
301 }
302 
303 static device_method_t vgapm_methods[] = {
304 	DEVMETHOD(device_identify,	vgapm_identify),
305 	DEVMETHOD(device_probe,		vgapm_probe),
306 	DEVMETHOD(device_attach,	vgapm_attach),
307 	DEVMETHOD(device_suspend,	vgapm_suspend),
308 	DEVMETHOD(device_resume,	vgapm_resume),
309 	{ 0, 0 }
310 };
311 
312 static driver_t vgapm_driver = {
313 	"vgapm",
314 	vgapm_methods,
315 	0
316 };
317 
318 DRIVER_MODULE(vgapm, vgapci, vgapm_driver, vgapm_devclass, 0, 0);
319 ISA_PNP_INFO(vga_ids);
320