xref: /netbsd-src/sys/dev/isa/pcdisplay.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /* $NetBSD: pcdisplay.c,v 1.25 2004/03/24 17:26:53 drochner Exp $ */
2 
3 /*
4  * Copyright (c) 1998
5  *	Matthias Drochner.  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.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: pcdisplay.c,v 1.25 2004/03/24 17:26:53 drochner Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/malloc.h>
37 #include <machine/bus.h>
38 
39 #include <dev/isa/isavar.h>
40 
41 #include <dev/ic/mc6845reg.h>
42 #include <dev/ic/pcdisplayvar.h>
43 #include <dev/isa/pcdisplayvar.h>
44 
45 #include <dev/ic/pcdisplay.h>
46 
47 #include <dev/wscons/wsconsio.h>
48 #include <dev/wscons/wsdisplayvar.h>
49 
50 #include "pcweasel.h"
51 #if NPCWEASEL > 0
52 #include <dev/isa/weaselreg.h>
53 #include <dev/isa/weaselvar.h>
54 #endif
55 
56 struct pcdisplay_config {
57 	struct pcdisplayscreen pcs;
58 	struct pcdisplay_handle dc_ph;
59 	int mono;
60 };
61 
62 struct pcdisplay_softc {
63 	struct device sc_dev;
64 	struct pcdisplay_config *sc_dc;
65 	int nscreens;
66 #if NPCWEASEL > 0
67 	struct weasel_handle sc_weasel;
68 #endif
69 };
70 
71 static int pcdisplayconsole, pcdisplay_console_attached;
72 static struct pcdisplay_config pcdisplay_console_dc;
73 
74 int	pcdisplay_match __P((struct device *, struct cfdata *, void *));
75 void	pcdisplay_attach __P((struct device *, struct device *, void *));
76 
77 static int pcdisplay_is_console __P((bus_space_tag_t));
78 static int pcdisplay_probe_col __P((bus_space_tag_t, bus_space_tag_t));
79 static int pcdisplay_probe_mono __P((bus_space_tag_t, bus_space_tag_t));
80 static void pcdisplay_init __P((struct pcdisplay_config *,
81 			     bus_space_tag_t, bus_space_tag_t,
82 			     int));
83 static int pcdisplay_allocattr __P((void *, int, int, int, long *));
84 
85 CFATTACH_DECL(pcdisplay, sizeof(struct pcdisplay_softc),
86     pcdisplay_match, pcdisplay_attach, NULL, NULL);
87 
88 const struct wsdisplay_emulops pcdisplay_emulops = {
89 	pcdisplay_cursor,
90 	pcdisplay_mapchar,
91 	pcdisplay_putchar,
92 	pcdisplay_copycols,
93 	pcdisplay_erasecols,
94 	pcdisplay_copyrows,
95 	pcdisplay_eraserows,
96 	pcdisplay_allocattr
97 };
98 
99 const struct wsscreen_descr pcdisplay_scr = {
100 	"80x25", 80, 25,
101 	&pcdisplay_emulops,
102 	0, 0, /* no font support */
103 	WSSCREEN_REVERSE /* that's minimal... */
104 };
105 
106 const struct wsscreen_descr *_pcdisplay_scrlist[] = {
107 	&pcdisplay_scr,
108 };
109 
110 const struct wsscreen_list pcdisplay_screenlist = {
111 	sizeof(_pcdisplay_scrlist) / sizeof(struct wsscreen_descr *),
112 	_pcdisplay_scrlist
113 };
114 
115 static int pcdisplay_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
116 static paddr_t pcdisplay_mmap __P((void *, off_t, int));
117 static int pcdisplay_alloc_screen __P((void *, const struct wsscreen_descr *,
118 				       void **, int *, int *, long *));
119 static void pcdisplay_free_screen __P((void *, void *));
120 static int pcdisplay_show_screen __P((void *, void *, int,
121 				      void (*) (void *, int, int), void *));
122 
123 const struct wsdisplay_accessops pcdisplay_accessops = {
124 	pcdisplay_ioctl,
125 	pcdisplay_mmap,
126 	pcdisplay_alloc_screen,
127 	pcdisplay_free_screen,
128 	pcdisplay_show_screen,
129 	0 /* load_font */
130 };
131 
132 static int
133 pcdisplay_probe_col(iot, memt)
134 	bus_space_tag_t iot, memt;
135 {
136 	bus_space_handle_t memh, ioh_6845;
137 	u_int16_t oldval, val;
138 
139 	if (bus_space_map(memt, 0xb8000, 0x8000, 0, &memh))
140 		return (0);
141 	oldval = bus_space_read_2(memt, memh, 0);
142 	bus_space_write_2(memt, memh, 0, 0xa55a);
143 	val = bus_space_read_2(memt, memh, 0);
144 	bus_space_write_2(memt, memh, 0, oldval);
145 	bus_space_unmap(memt, memh, 0x8000);
146 	if (val != 0xa55a)
147 		return (0);
148 
149 	if (bus_space_map(iot, 0x3d0, 0x10, 0, &ioh_6845))
150 		return (0);
151 	bus_space_unmap(iot, ioh_6845, 0x10);
152 
153 	return (1);
154 }
155 
156 static int
157 pcdisplay_probe_mono(iot, memt)
158 	bus_space_tag_t iot, memt;
159 {
160 	bus_space_handle_t memh, ioh_6845;
161 	u_int16_t oldval, val;
162 
163 	if (bus_space_map(memt, 0xb0000, 0x8000, 0, &memh))
164 		return (0);
165 	oldval = bus_space_read_2(memt, memh, 0);
166 	bus_space_write_2(memt, memh, 0, 0xa55a);
167 	val = bus_space_read_2(memt, memh, 0);
168 	bus_space_write_2(memt, memh, 0, oldval);
169 	bus_space_unmap(memt, memh, 0x8000);
170 	if (val != 0xa55a)
171 		return (0);
172 
173 	if (bus_space_map(iot, 0x3b0, 0x10, 0, &ioh_6845))
174 		return (0);
175 	bus_space_unmap(iot, ioh_6845, 0x10);
176 
177 	return (1);
178 }
179 
180 static void
181 pcdisplay_init(dc, iot, memt, mono)
182 	struct pcdisplay_config *dc;
183 	bus_space_tag_t iot, memt;
184 	int mono;
185 {
186 	struct pcdisplay_handle *ph = &dc->dc_ph;
187 	int cpos;
188 
189         ph->ph_iot = iot;
190         ph->ph_memt = memt;
191 	dc->mono = mono;
192 
193 	if (bus_space_map(memt, mono ? 0xb0000 : 0xb8000, 0x8000,
194 			  0, &ph->ph_memh))
195 		panic("pcdisplay_init: cannot map memory");
196 	if (bus_space_map(iot, mono ? 0x3b0 : 0x3d0, 0x10,
197 			  0, &ph->ph_ioh_6845))
198 		panic("pcdisplay_init: cannot map io");
199 
200 	/*
201 	 * initialize the only screen
202 	 */
203 	dc->pcs.hdl = ph;
204 	dc->pcs.type = &pcdisplay_scr;
205 	dc->pcs.active = 1;
206 	dc->pcs.mem = NULL;
207 
208 	cpos = pcdisplay_6845_read(ph, cursorh) << 8;
209 	cpos |= pcdisplay_6845_read(ph, cursorl);
210 
211 	/* make sure we have a valid cursor position */
212 	if (cpos < 0 || cpos >= pcdisplay_scr.nrows * pcdisplay_scr.ncols)
213 		cpos = 0;
214 
215 	dc->pcs.dispoffset = 0;
216 
217 	dc->pcs.cursorrow = cpos / pcdisplay_scr.ncols;
218 	dc->pcs.cursorcol = cpos % pcdisplay_scr.ncols;
219 	pcdisplay_cursor_init(&dc->pcs, 1);
220 }
221 
222 int
223 pcdisplay_match(parent, match, aux)
224 	struct device *parent;
225 	struct cfdata *match;
226 	void *aux;
227 {
228 	struct isa_attach_args *ia = aux;
229 	int mono;
230 
231 	if (ISA_DIRECT_CONFIG(ia))
232 		return (0);
233 
234 	/* If values are hardwired to something that they can't be, punt. */
235 	if (ia->ia_nio < 1 ||
236 	    (ia->ia_io[0].ir_addr != ISACF_PORT_DEFAULT &&
237 	     ia->ia_io[0].ir_addr != 0x3d0 &&
238 	     ia->ia_io[0].ir_addr != 0x3b0))
239 		return (0);
240 
241 	if (ia->ia_niomem < 1 ||
242 	    (ia->ia_iomem[0].ir_addr != ISACF_IOMEM_DEFAULT &&
243 	     ia->ia_iomem[0].ir_addr != 0xb8000 &&
244 	     ia->ia_iomem[0].ir_addr != 0xb0000))
245 		return (0);
246 	if (ia->ia_iomem[0].ir_size != 0 &&
247 	    ia->ia_iomem[0].ir_size != 0x8000)
248 		return (0);
249 
250 	if (ia->ia_nirq > 0 &&
251 	    ia->ia_irq[0].ir_irq != ISACF_IRQ_DEFAULT)
252 		return (0);
253 
254 	if (ia->ia_ndrq > 0 &&
255 	    ia->ia_drq[0].ir_drq != ISACF_DRQ_DEFAULT)
256 		return (0);
257 
258 	if (pcdisplay_is_console(ia->ia_iot))
259 		mono = pcdisplay_console_dc.mono;
260 	else if (ia->ia_io[0].ir_addr != 0x3b0 &&
261 		 ia->ia_iomem[0].ir_addr != 0xb0000 &&
262 		 pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
263 		mono = 0;
264 	else if (ia->ia_io[0].ir_addr != 0x3d0 &&
265 		 ia->ia_iomem[0].ir_addr != 0xb8000 &&
266 		 pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
267 		mono = 1;
268 	else
269 		return (0);
270 
271 	ia->ia_nio = 1;
272 	ia->ia_io[0].ir_addr = mono ? 0x3b0 : 0x3d0;
273 	ia->ia_io[0].ir_size = 0x10;
274 
275 	ia->ia_niomem = 1;
276 	ia->ia_iomem[0].ir_size = mono ? 0xb0000 : 0xb8000;
277 	ia->ia_iomem[0].ir_size = 0x8000;
278 
279 	ia->ia_nirq = 0;
280 	ia->ia_ndrq = 0;
281 
282 	return (1);
283 }
284 
285 void
286 pcdisplay_attach(parent, self, aux)
287 	struct device *parent, *self;
288 	void *aux;
289 {
290 	struct isa_attach_args *ia = aux;
291 	struct pcdisplay_softc *sc = (struct pcdisplay_softc *)self;
292 	int console;
293 	struct pcdisplay_config *dc;
294 	struct wsemuldisplaydev_attach_args aa;
295 
296 	printf("\n");
297 
298 	console = pcdisplay_is_console(ia->ia_iot);
299 
300 	if (console) {
301 		dc = &pcdisplay_console_dc;
302 		sc->nscreens = 1;
303 		pcdisplay_console_attached = 1;
304 	} else {
305 		dc = malloc(sizeof(struct pcdisplay_config),
306 			    M_DEVBUF, M_WAITOK);
307 		if (ia->ia_io[0].ir_addr != 0x3b0 &&
308 		    ia->ia_iomem[0].ir_addr != 0xb0000 &&
309 		    pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
310 			pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 0);
311 		else if (ia->ia_io[0].ir_addr != 0x3d0 &&
312 			 ia->ia_iomem[0].ir_addr != 0xb8000 &&
313 			 pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
314 			pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 1);
315 		else
316 			panic("pcdisplay_attach: display disappeared");
317 	}
318 	sc->sc_dc = dc;
319 
320 #if NPCWEASEL > 0
321 	/*
322 	 * If the display is monochrome, check to see if we have
323 	 * a PC-Weasel, and initialize its special features.
324 	 */
325 	if (dc->mono) {
326 		sc->sc_weasel.wh_st = dc->dc_ph.ph_memt;
327 		sc->sc_weasel.wh_sh = dc->dc_ph.ph_memh;
328 		sc->sc_weasel.wh_parent = &sc->sc_dev;
329 		weasel_isa_init(&sc->sc_weasel);
330 	}
331 #endif /* NPCWEASEL > 0 */
332 
333 	aa.console = console;
334 	aa.scrdata = &pcdisplay_screenlist;
335 	aa.accessops = &pcdisplay_accessops;
336 	aa.accesscookie = sc;
337 
338         config_found(self, &aa, wsemuldisplaydevprint);
339 }
340 
341 
342 int
343 pcdisplay_cnattach(iot, memt)
344 	bus_space_tag_t iot, memt;
345 {
346 	int mono;
347 
348 	if (pcdisplay_probe_col(iot, memt))
349 		mono = 0;
350 	else if (pcdisplay_probe_mono(iot, memt))
351 		mono = 1;
352 	else
353 		return (ENXIO);
354 
355 	pcdisplay_init(&pcdisplay_console_dc, iot, memt, mono);
356 
357 	wsdisplay_cnattach(&pcdisplay_scr, &pcdisplay_console_dc,
358 			   pcdisplay_console_dc.pcs.cursorcol,
359 			   pcdisplay_console_dc.pcs.cursorrow,
360 			   FG_LIGHTGREY | BG_BLACK);
361 
362 	pcdisplayconsole = 1;
363 	return (0);
364 }
365 
366 static int
367 pcdisplay_is_console(iot)
368 	bus_space_tag_t iot;
369 {
370 	if (pcdisplayconsole &&
371 	    !pcdisplay_console_attached &&
372 	    iot == pcdisplay_console_dc.dc_ph.ph_iot)
373 		return (1);
374 	return (0);
375 }
376 
377 static int
378 pcdisplay_ioctl(v, cmd, data, flag, p)
379 	void *v;
380 	u_long cmd;
381 	caddr_t data;
382 	int flag;
383 	struct proc *p;
384 {
385 	/*
386 	 * XXX "do something!"
387 	 */
388 	return (EPASSTHROUGH);
389 }
390 
391 static paddr_t
392 pcdisplay_mmap(v, offset, prot)
393 	void *v;
394 	off_t offset;
395 	int prot;
396 {
397 	return (-1);
398 }
399 
400 static int
401 pcdisplay_alloc_screen(v, type, cookiep, curxp, curyp, defattrp)
402 	void *v;
403 	const struct wsscreen_descr *type;
404 	void **cookiep;
405 	int *curxp, *curyp;
406 	long *defattrp;
407 {
408 	struct pcdisplay_softc *sc = v;
409 
410 	if (sc->nscreens > 0)
411 		return (ENOMEM);
412 
413 	*cookiep = sc->sc_dc;
414 	*curxp = 0;
415 	*curyp = 0;
416 	*defattrp = FG_LIGHTGREY | BG_BLACK;
417 	sc->nscreens++;
418 	return (0);
419 }
420 
421 static void
422 pcdisplay_free_screen(v, cookie)
423 	void *v;
424 	void *cookie;
425 {
426 	struct pcdisplay_softc *sc = v;
427 
428 	if (sc->sc_dc == &pcdisplay_console_dc)
429 		panic("pcdisplay_free_screen: console");
430 
431 	sc->nscreens--;
432 }
433 
434 static int
435 pcdisplay_show_screen(v, cookie, waitok, cb, cbarg)
436 	void *v;
437 	void *cookie;
438 	int waitok;
439 	void (*cb) __P((void *, int, int));
440 	void *cbarg;
441 {
442 #ifdef DIAGNOSTIC
443 	struct pcdisplay_softc *sc = v;
444 
445 	if (cookie != sc->sc_dc)
446 		panic("pcdisplay_show_screen: bad screen");
447 #endif
448 	return (0);
449 }
450 
451 static int
452 pcdisplay_allocattr(id, fg, bg, flags, attrp)
453 	void *id;
454 	int fg, bg;
455 	int flags;
456 	long *attrp;
457 {
458 	if (flags & WSATTR_REVERSE)
459 		*attrp = FG_BLACK | BG_LIGHTGREY;
460 	else
461 		*attrp = FG_LIGHTGREY | BG_BLACK;
462 	return (0);
463 }
464