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