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