xref: /freebsd/sys/isa/vga_isa.c (revision a0409676120c1e558d0ade943019934e0f15118d)
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 #define VGA_SOFTC(unit)		\
135 	((vga_softc_t *)devclass_get_softc(isavga_devclass, unit))
136 
137 static devclass_t	isavga_devclass;
138 
139 #ifdef FB_INSTALL_CDEV
140 
141 static d_open_t		isavga_open;
142 static d_close_t	isavga_close;
143 static d_read_t		isavga_read;
144 static d_write_t	isavga_write;
145 static d_ioctl_t	isavga_ioctl;
146 static d_mmap_t		isavga_mmap;
147 
148 static struct cdevsw isavga_cdevsw = {
149 	.d_version =	D_VERSION,
150 	.d_flags =	D_NEEDGIANT,
151 	.d_open =	isavga_open,
152 	.d_close =	isavga_close,
153 	.d_read =	isavga_read,
154 	.d_write =	isavga_write,
155 	.d_ioctl =	isavga_ioctl,
156 	.d_mmap =	isavga_mmap,
157 	.d_name =	VGA_DRIVER_NAME,
158 };
159 
160 #endif /* FB_INSTALL_CDEV */
161 
162 static void
163 isavga_identify(driver_t *driver, device_t parent)
164 {
165 	BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, VGA_DRIVER_NAME, 0);
166 }
167 
168 static int
169 isavga_probe(device_t dev)
170 {
171 	video_adapter_t adp;
172 	int error;
173 
174 	/* No pnp support */
175 	if (isa_get_vendorid(dev))
176 		return (ENXIO);
177 
178 	error = vga_probe_unit(device_get_unit(dev), &adp, device_get_flags(dev));
179 	if (error == 0) {
180 		device_set_desc(dev, "Generic ISA VGA");
181 		bus_set_resource(dev, SYS_RES_IOPORT, 0,
182 				 adp.va_io_base, adp.va_io_size);
183 		bus_set_resource(dev, SYS_RES_MEMORY, 0,
184 				 adp.va_mem_base, adp.va_mem_size);
185 		isa_set_vendorid(dev, VGA_ID);
186 		isa_set_logicalid(dev, VGA_ID);
187 #if 0
188 		isa_set_port(dev, adp.va_io_base);
189 		isa_set_portsize(dev, adp.va_io_size);
190 		isa_set_maddr(dev, adp.va_mem_base);
191 		isa_set_msize(dev, adp.va_mem_size);
192 #endif
193 	}
194 	return (error);
195 }
196 
197 static int
198 isavga_attach(device_t dev)
199 {
200 	vga_softc_t *sc;
201 	int unit;
202 	int rid;
203 	int error;
204 
205 	unit = device_get_unit(dev);
206 	sc = device_get_softc(dev);
207 
208 	rid = 0;
209 	bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
210 				  RF_ACTIVE | RF_SHAREABLE);
211 	rid = 0;
212 	bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
213 				 RF_ACTIVE | RF_SHAREABLE);
214 
215 	error = vga_attach_unit(unit, sc, device_get_flags(dev));
216 	if (error)
217 		return (error);
218 
219 #ifdef FB_INSTALL_CDEV
220 	/* attach a virtual frame buffer device */
221 	error = fb_attach(VGA_MKMINOR(unit), sc->adp, &isavga_cdevsw);
222 	if (error)
223 		return (error);
224 #endif /* FB_INSTALL_CDEV */
225 
226 	if (0 && bootverbose)
227 		vidd_diag(sc->adp, bootverbose);
228 
229 #if 0 /* experimental */
230 	device_add_child(dev, "fb", -1);
231 	bus_generic_attach(dev);
232 #endif
233 
234 	return (0);
235 }
236 
237 static int
238 isavga_suspend(device_t dev)
239 {
240 	int error;
241 
242 	error = bus_generic_suspend(dev);
243 	if (error != 0)
244 		return (error);
245 	vga_suspend(dev);
246 
247 	return (error);
248 }
249 
250 static int
251 isavga_resume(device_t dev)
252 {
253 
254 	vga_resume(dev);
255 
256 	return (bus_generic_resume(dev));
257 }
258 
259 #ifdef FB_INSTALL_CDEV
260 
261 static int
262 isavga_open(struct cdev *dev, int flag, int mode, struct thread *td)
263 {
264 	return (vga_open(dev, VGA_SOFTC(VGA_UNIT(dev)), flag, mode, td));
265 }
266 
267 static int
268 isavga_close(struct cdev *dev, int flag, int mode, struct thread *td)
269 {
270 	return (vga_close(dev, VGA_SOFTC(VGA_UNIT(dev)), flag, mode, td));
271 }
272 
273 static int
274 isavga_read(struct cdev *dev, struct uio *uio, int flag)
275 {
276 	return (vga_read(dev, VGA_SOFTC(VGA_UNIT(dev)), uio, flag));
277 }
278 
279 static int
280 isavga_write(struct cdev *dev, struct uio *uio, int flag)
281 {
282 	return (vga_write(dev, VGA_SOFTC(VGA_UNIT(dev)), uio, flag));
283 }
284 
285 static int
286 isavga_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
287 {
288 	return (vga_ioctl(dev, VGA_SOFTC(VGA_UNIT(dev)), cmd, arg, flag, td));
289 }
290 
291 static int
292 isavga_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
293     int prot, vm_memattr_t *memattr)
294 {
295 	return (vga_mmap(dev, VGA_SOFTC(VGA_UNIT(dev)), offset, paddr, prot,
296 	    memattr));
297 }
298 
299 #endif /* FB_INSTALL_CDEV */
300 
301 static device_method_t isavga_methods[] = {
302 	DEVMETHOD(device_identify,	isavga_identify),
303 	DEVMETHOD(device_probe,		isavga_probe),
304 	DEVMETHOD(device_attach,	isavga_attach),
305 	DEVMETHOD(device_suspend,	isavga_suspend),
306 	DEVMETHOD(device_resume,	isavga_resume),
307 
308 	DEVMETHOD_END
309 };
310 
311 static driver_t isavga_driver = {
312 	VGA_DRIVER_NAME,
313 	isavga_methods,
314 	sizeof(vga_softc_t),
315 };
316 
317 DRIVER_MODULE(vga, isa, isavga_driver, isavga_devclass, 0, 0);
318 
319 static devclass_t	vgapm_devclass;
320 
321 static void
322 vgapm_identify(driver_t *driver, device_t parent)
323 {
324 
325 	if (device_get_flags(parent) != 0)
326 		device_add_child(parent, "vgapm", 0);
327 }
328 
329 static int
330 vgapm_probe(device_t dev)
331 {
332 
333 	device_set_desc(dev, "VGA suspend/resume");
334 	device_quiet(dev);
335 
336 	return (BUS_PROBE_DEFAULT);
337 }
338 
339 static int
340 vgapm_attach(device_t dev)
341 {
342 
343 	bus_generic_probe(dev);
344 	bus_generic_attach(dev);
345 
346 	return (0);
347 }
348 
349 static int
350 vgapm_suspend(device_t dev)
351 {
352 	device_t vga_dev;
353 	int error;
354 
355 	error = bus_generic_suspend(dev);
356 	if (error != 0)
357 		return (error);
358 	vga_dev = devclass_get_device(isavga_devclass, 0);
359 	if (vga_dev == NULL)
360 		return (0);
361 	vga_suspend(vga_dev);
362 
363 	return (0);
364 }
365 
366 static int
367 vgapm_resume(device_t dev)
368 {
369 	device_t vga_dev;
370 
371 	vga_dev = devclass_get_device(isavga_devclass, 0);
372 	if (vga_dev != NULL)
373 		vga_resume(vga_dev);
374 
375 	return (bus_generic_resume(dev));
376 }
377 
378 static device_method_t vgapm_methods[] = {
379 	DEVMETHOD(device_identify,	vgapm_identify),
380 	DEVMETHOD(device_probe,		vgapm_probe),
381 	DEVMETHOD(device_attach,	vgapm_attach),
382 	DEVMETHOD(device_suspend,	vgapm_suspend),
383 	DEVMETHOD(device_resume,	vgapm_resume),
384 	{ 0, 0 }
385 };
386 
387 static driver_t vgapm_driver = {
388 	"vgapm",
389 	vgapm_methods,
390 	0
391 };
392 
393 DRIVER_MODULE(vgapm, vgapci, vgapm_driver, vgapm_devclass, 0, 0);
394 ISA_PNP_INFO(vga_ids);
395