xref: /netbsd-src/sys/dev/isa/pcdisplay.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /* $NetBSD: pcdisplay.c,v 1.36 2009/03/14 21:04:20 dsl 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.36 2009/03/14 21:04:20 dsl 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 <sys/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(struct device *, struct cfdata *, void *);
75 void	pcdisplay_attach(struct device *, struct device *, void *);
76 
77 static int pcdisplay_is_console(bus_space_tag_t);
78 static int pcdisplay_probe_col(bus_space_tag_t, bus_space_tag_t);
79 static int pcdisplay_probe_mono(bus_space_tag_t, bus_space_tag_t);
80 static void pcdisplay_init(struct pcdisplay_config *,
81 			     bus_space_tag_t, bus_space_tag_t,
82 			     int);
83 static int pcdisplay_allocattr(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 	NULL,	/* replaceattr */
98 };
99 
100 const struct wsscreen_descr pcdisplay_scr = {
101 	"80x25", 80, 25,
102 	&pcdisplay_emulops,
103 	0, 0, /* no font support */
104 	WSSCREEN_REVERSE, /* that's minimal... */
105 	NULL, /* modecookie */
106 };
107 
108 const struct wsscreen_descr *_pcdisplay_scrlist[] = {
109 	&pcdisplay_scr,
110 };
111 
112 const struct wsscreen_list pcdisplay_screenlist = {
113 	sizeof(_pcdisplay_scrlist) / sizeof(struct wsscreen_descr *),
114 	_pcdisplay_scrlist
115 };
116 
117 static int pcdisplay_ioctl(void *, void *, u_long, void *, int, struct lwp *);
118 static paddr_t pcdisplay_mmap(void *, void *, off_t, int);
119 static int pcdisplay_alloc_screen(void *, const struct wsscreen_descr *,
120 				       void **, int *, int *, long *);
121 static void pcdisplay_free_screen(void *, void *);
122 static int pcdisplay_show_screen(void *, void *, int,
123 				      void (*) (void *, int, int), void *);
124 
125 const struct wsdisplay_accessops pcdisplay_accessops = {
126 	pcdisplay_ioctl,
127 	pcdisplay_mmap,
128 	pcdisplay_alloc_screen,
129 	pcdisplay_free_screen,
130 	pcdisplay_show_screen,
131 	NULL, /* load_font */
132 	NULL, /* pollc */
133 	NULL, /* scroll */
134 };
135 
136 static int
137 pcdisplay_probe_col(bus_space_tag_t iot, bus_space_tag_t memt)
138 {
139 	bus_space_handle_t memh, ioh_6845;
140 	u_int16_t oldval, val;
141 
142 	if (bus_space_map(memt, 0xb8000, 0x8000, 0, &memh))
143 		return (0);
144 	oldval = bus_space_read_2(memt, memh, 0);
145 	bus_space_write_2(memt, memh, 0, 0xa55a);
146 	val = bus_space_read_2(memt, memh, 0);
147 	bus_space_write_2(memt, memh, 0, oldval);
148 	bus_space_unmap(memt, memh, 0x8000);
149 	if (val != 0xa55a)
150 		return (0);
151 
152 	if (bus_space_map(iot, 0x3d0, 0x10, 0, &ioh_6845))
153 		return (0);
154 	bus_space_unmap(iot, ioh_6845, 0x10);
155 
156 	return (1);
157 }
158 
159 static int
160 pcdisplay_probe_mono(bus_space_tag_t iot, bus_space_tag_t memt)
161 {
162 	bus_space_handle_t memh, ioh_6845;
163 	u_int16_t oldval, val;
164 
165 	if (bus_space_map(memt, 0xb0000, 0x8000, 0, &memh))
166 		return (0);
167 	oldval = bus_space_read_2(memt, memh, 0);
168 	bus_space_write_2(memt, memh, 0, 0xa55a);
169 	val = bus_space_read_2(memt, memh, 0);
170 	bus_space_write_2(memt, memh, 0, oldval);
171 	bus_space_unmap(memt, memh, 0x8000);
172 	if (val != 0xa55a)
173 		return (0);
174 
175 	if (bus_space_map(iot, 0x3b0, 0x10, 0, &ioh_6845))
176 		return (0);
177 	bus_space_unmap(iot, ioh_6845, 0x10);
178 
179 	return (1);
180 }
181 
182 static void
183 pcdisplay_init(struct pcdisplay_config *dc, bus_space_tag_t iot, bus_space_tag_t memt, int mono)
184 {
185 	struct pcdisplay_handle *ph = &dc->dc_ph;
186 	int cpos;
187 
188         ph->ph_iot = iot;
189         ph->ph_memt = memt;
190 	dc->mono = mono;
191 
192 	if (bus_space_map(memt, mono ? 0xb0000 : 0xb8000, 0x8000,
193 			  0, &ph->ph_memh))
194 		panic("pcdisplay_init: cannot map memory");
195 	if (bus_space_map(iot, mono ? 0x3b0 : 0x3d0, 0x10,
196 			  0, &ph->ph_ioh_6845))
197 		panic("pcdisplay_init: cannot map io");
198 
199 	/*
200 	 * initialize the only screen
201 	 */
202 	dc->pcs.hdl = ph;
203 	dc->pcs.type = &pcdisplay_scr;
204 	dc->pcs.active = 1;
205 	dc->pcs.mem = NULL;
206 
207 	cpos = pcdisplay_6845_read(ph, cursorh) << 8;
208 	cpos |= pcdisplay_6845_read(ph, cursorl);
209 
210 	/* make sure we have a valid cursor position */
211 	if (cpos < 0 || cpos >= pcdisplay_scr.nrows * pcdisplay_scr.ncols)
212 		cpos = 0;
213 
214 	dc->pcs.dispoffset = 0;
215 
216 	dc->pcs.cursorrow = cpos / pcdisplay_scr.ncols;
217 	dc->pcs.cursorcol = cpos % pcdisplay_scr.ncols;
218 	pcdisplay_cursor_init(&dc->pcs, 1);
219 }
220 
221 int
222 pcdisplay_match(struct device *parent, struct cfdata *match,
223     void *aux)
224 {
225 	struct isa_attach_args *ia = aux;
226 	int mono;
227 
228 	if (ISA_DIRECT_CONFIG(ia))
229 		return (0);
230 
231 	/* If values are hardwired to something that they can't be, punt. */
232 	if (ia->ia_nio < 1 ||
233 	    (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
234 	     ia->ia_io[0].ir_addr != 0x3d0 &&
235 	     ia->ia_io[0].ir_addr != 0x3b0))
236 		return (0);
237 
238 	if (ia->ia_niomem < 1 ||
239 	    (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM &&
240 	     ia->ia_iomem[0].ir_addr != 0xb8000 &&
241 	     ia->ia_iomem[0].ir_addr != 0xb0000))
242 		return (0);
243 	if (ia->ia_iomem[0].ir_size != 0 &&
244 	    ia->ia_iomem[0].ir_size != 0x8000)
245 		return (0);
246 
247 	if (ia->ia_nirq > 0 &&
248 	    ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ)
249 		return (0);
250 
251 	if (ia->ia_ndrq > 0 &&
252 	    ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ)
253 		return (0);
254 
255 	if (pcdisplay_is_console(ia->ia_iot))
256 		mono = pcdisplay_console_dc.mono;
257 	else if (ia->ia_io[0].ir_addr != 0x3b0 &&
258 		 ia->ia_iomem[0].ir_addr != 0xb0000 &&
259 		 pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
260 		mono = 0;
261 	else if (ia->ia_io[0].ir_addr != 0x3d0 &&
262 		 ia->ia_iomem[0].ir_addr != 0xb8000 &&
263 		 pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
264 		mono = 1;
265 	else
266 		return (0);
267 
268 	ia->ia_nio = 1;
269 	ia->ia_io[0].ir_addr = mono ? 0x3b0 : 0x3d0;
270 	ia->ia_io[0].ir_size = 0x10;
271 
272 	ia->ia_niomem = 1;
273 	ia->ia_iomem[0].ir_size = mono ? 0xb0000 : 0xb8000;
274 	ia->ia_iomem[0].ir_size = 0x8000;
275 
276 	ia->ia_nirq = 0;
277 	ia->ia_ndrq = 0;
278 
279 	return (1);
280 }
281 
282 void
283 pcdisplay_attach(struct device *parent, struct device *self, void *aux)
284 {
285 	struct isa_attach_args *ia = aux;
286 	struct pcdisplay_softc *sc = (struct pcdisplay_softc *)self;
287 	int console;
288 	struct pcdisplay_config *dc;
289 	struct wsemuldisplaydev_attach_args aa;
290 
291 	printf("\n");
292 
293 	console = pcdisplay_is_console(ia->ia_iot);
294 
295 	if (console) {
296 		dc = &pcdisplay_console_dc;
297 		sc->nscreens = 1;
298 		pcdisplay_console_attached = 1;
299 	} else {
300 		dc = malloc(sizeof(struct pcdisplay_config),
301 			    M_DEVBUF, M_WAITOK);
302 		if (ia->ia_io[0].ir_addr != 0x3b0 &&
303 		    ia->ia_iomem[0].ir_addr != 0xb0000 &&
304 		    pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
305 			pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 0);
306 		else if (ia->ia_io[0].ir_addr != 0x3d0 &&
307 			 ia->ia_iomem[0].ir_addr != 0xb8000 &&
308 			 pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
309 			pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 1);
310 		else
311 			panic("pcdisplay_attach: display disappeared");
312 	}
313 	sc->sc_dc = dc;
314 
315 #if NPCWEASEL > 0
316 	/*
317 	 * If the display is monochrome, check to see if we have
318 	 * a PC-Weasel, and initialize its special features.
319 	 */
320 	if (dc->mono) {
321 		sc->sc_weasel.wh_st = dc->dc_ph.ph_memt;
322 		sc->sc_weasel.wh_sh = dc->dc_ph.ph_memh;
323 		sc->sc_weasel.wh_parent = &sc->sc_dev;
324 		weasel_isa_init(&sc->sc_weasel);
325 	}
326 #endif /* NPCWEASEL > 0 */
327 
328 	aa.console = console;
329 	aa.scrdata = &pcdisplay_screenlist;
330 	aa.accessops = &pcdisplay_accessops;
331 	aa.accesscookie = sc;
332 
333         config_found(self, &aa, wsemuldisplaydevprint);
334 }
335 
336 
337 int
338 pcdisplay_cnattach(bus_space_tag_t iot, bus_space_tag_t memt)
339 {
340 	int mono;
341 
342 	if (pcdisplay_probe_col(iot, memt))
343 		mono = 0;
344 	else if (pcdisplay_probe_mono(iot, memt))
345 		mono = 1;
346 	else
347 		return (ENXIO);
348 
349 	pcdisplay_init(&pcdisplay_console_dc, iot, memt, mono);
350 
351 	wsdisplay_cnattach(&pcdisplay_scr, &pcdisplay_console_dc,
352 			   pcdisplay_console_dc.pcs.cursorcol,
353 			   pcdisplay_console_dc.pcs.cursorrow,
354 			   FG_LIGHTGREY | BG_BLACK);
355 
356 	pcdisplayconsole = 1;
357 	return (0);
358 }
359 
360 static int
361 pcdisplay_is_console(bus_space_tag_t iot)
362 {
363 	if (pcdisplayconsole &&
364 	    !pcdisplay_console_attached &&
365 	    iot == pcdisplay_console_dc.dc_ph.ph_iot)
366 		return (1);
367 	return (0);
368 }
369 
370 static int
371 pcdisplay_ioctl(void *v, void *vs, u_long cmd,
372     void *data, int flag, struct lwp *l)
373 {
374 	/*
375 	 * XXX "do something!"
376 	 */
377 	return (EPASSTHROUGH);
378 }
379 
380 static paddr_t
381 pcdisplay_mmap(void *v, void *vs, off_t offset,
382     int prot)
383 {
384 	return (-1);
385 }
386 
387 static int
388 pcdisplay_alloc_screen(void *v, const struct wsscreen_descr *type,
389     void **cookiep, int *curxp, int *curyp, long *defattrp)
390 {
391 	struct pcdisplay_softc *sc = v;
392 
393 	if (sc->nscreens > 0)
394 		return (ENOMEM);
395 
396 	*cookiep = sc->sc_dc;
397 	*curxp = 0;
398 	*curyp = 0;
399 	*defattrp = FG_LIGHTGREY | BG_BLACK;
400 	sc->nscreens++;
401 	return (0);
402 }
403 
404 static void
405 pcdisplay_free_screen(void *v, void *cookie)
406 {
407 	struct pcdisplay_softc *sc = v;
408 
409 	if (sc->sc_dc == &pcdisplay_console_dc)
410 		panic("pcdisplay_free_screen: console");
411 
412 	sc->nscreens--;
413 }
414 
415 static int
416 pcdisplay_show_screen(void *v, void *cookie,
417     int waitok, void (*cb)(void *, int, int),
418     void *cbarg)
419 {
420 #ifdef DIAGNOSTIC
421 	struct pcdisplay_softc *sc = v;
422 
423 	if (cookie != sc->sc_dc)
424 		panic("pcdisplay_show_screen: bad screen");
425 #endif
426 	return (0);
427 }
428 
429 static int
430 pcdisplay_allocattr(void *id, int fg, int bg,
431     int flags, long *attrp)
432 {
433 	if (flags & WSATTR_REVERSE)
434 		*attrp = FG_BLACK | BG_LIGHTGREY;
435 	else
436 		*attrp = FG_LIGHTGREY | BG_BLACK;
437 	return (0);
438 }
439