xref: /openbsd/sys/dev/isa/pcdisplay.c (revision 898184e3)
1 /* $OpenBSD: pcdisplay.c,v 1.11 2010/08/28 12:48:14 miod Exp $ */
2 /* $NetBSD: pcdisplay.c,v 1.9.4.1 2000/06/30 16:27:48 simonb Exp $ */
3 
4 /*
5  * Copyright (c) 1998
6  *	Matthias Drochner.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
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 AUTHOR ``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 AUTHOR 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 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/device.h>
34 #include <sys/malloc.h>
35 #include <machine/bus.h>
36 
37 #include <dev/isa/isavar.h>
38 #include <dev/isa/isareg.h>
39 
40 #include <dev/ic/mc6845reg.h>
41 #include <dev/ic/pcdisplayvar.h>
42 #include <dev/isa/pcdisplayvar.h>
43 
44 #include <dev/ic/pcdisplay.h>
45 
46 #include <dev/wscons/wsconsio.h>
47 #include <dev/wscons/wsdisplayvar.h>
48 
49 struct pcdisplay_config {
50 	struct pcdisplayscreen pcs;
51 	struct pcdisplay_handle dc_ph;
52 	int mono;
53 };
54 
55 struct pcdisplay_softc {
56 	struct device sc_dev;
57 	struct pcdisplay_config *sc_dc;
58 	int nscreens;
59 };
60 
61 static int pcdisplayconsole, pcdisplay_console_attached;
62 static struct pcdisplay_config pcdisplay_console_dc;
63 
64 int	pcdisplay_match(struct device *, void *, void *);
65 void	pcdisplay_attach(struct device *, struct device *, void *);
66 
67 static int pcdisplay_is_console(bus_space_tag_t);
68 static int pcdisplay_probe_col(bus_space_tag_t, bus_space_tag_t);
69 static int pcdisplay_probe_mono(bus_space_tag_t, bus_space_tag_t);
70 static void pcdisplay_init(struct pcdisplay_config *,
71 			     bus_space_tag_t, bus_space_tag_t,
72 			     int);
73 static int pcdisplay_alloc_attr(void *, int, int, int, long *);
74 static void pcdisplay_unpack_attr(void *, long, int *, int *, int *);
75 
76 struct cfattach pcdisplay_ca = {
77 	sizeof(struct pcdisplay_softc), pcdisplay_match, pcdisplay_attach,
78 };
79 
80 const struct wsdisplay_emulops pcdisplay_emulops = {
81 	pcdisplay_cursor,
82 	pcdisplay_mapchar,
83 	pcdisplay_putchar,
84 	pcdisplay_copycols,
85 	pcdisplay_erasecols,
86 	pcdisplay_copyrows,
87 	pcdisplay_eraserows,
88 	pcdisplay_alloc_attr,
89 	pcdisplay_unpack_attr
90 };
91 
92 const struct wsscreen_descr pcdisplay_scr = {
93 	"80x25", 80, 25,
94 	&pcdisplay_emulops,
95 	0, 0, /* no font support */
96 	WSSCREEN_REVERSE /* that's minimal... */
97 };
98 
99 const struct wsscreen_descr *_pcdisplay_scrlist[] = {
100 	&pcdisplay_scr,
101 };
102 
103 const struct wsscreen_list pcdisplay_screenlist = {
104 	sizeof(_pcdisplay_scrlist) / sizeof(struct wsscreen_descr *),
105 	_pcdisplay_scrlist
106 };
107 
108 static int pcdisplay_ioctl(void *, u_long, caddr_t, int, struct proc *);
109 static paddr_t pcdisplay_mmap(void *, off_t, int);
110 static int pcdisplay_alloc_screen(void *, const struct wsscreen_descr *,
111 				       void **, int *, int *, long *);
112 static void pcdisplay_free_screen(void *, void *);
113 static int pcdisplay_show_screen(void *, void *, int,
114 				 void (*) (void *, int, int), void *);
115 
116 const struct wsdisplay_accessops pcdisplay_accessops = {
117 	pcdisplay_ioctl,
118 	pcdisplay_mmap,
119 	pcdisplay_alloc_screen,
120 	pcdisplay_free_screen,
121 	pcdisplay_show_screen,
122 	0 /* load_font */
123 };
124 
125 static int
126 pcdisplay_probe_col(bus_space_tag_t iot, bus_space_tag_t memt)
127 {
128 	bus_space_handle_t memh, ioh_6845;
129 	u_int16_t oldval, val;
130 
131 	if (bus_space_map(memt, 0xb8000, 0x8000, 0, &memh))
132 		return (0);
133 	oldval = bus_space_read_2(memt, memh, 0);
134 	bus_space_write_2(memt, memh, 0, 0xa55a);
135 	val = bus_space_read_2(memt, memh, 0);
136 	bus_space_write_2(memt, memh, 0, oldval);
137 	bus_space_unmap(memt, memh, 0x8000);
138 	if (val != 0xa55a)
139 		return (0);
140 
141 	if (bus_space_map(iot, 0x3d0, 0x10, 0, &ioh_6845))
142 		return (0);
143 	bus_space_unmap(iot, ioh_6845, 0x10);
144 
145 	return (1);
146 }
147 
148 static int
149 pcdisplay_probe_mono(bus_space_tag_t iot, bus_space_tag_t memt)
150 {
151 	bus_space_handle_t memh, ioh_6845;
152 	u_int16_t oldval, val;
153 
154 	if (bus_space_map(memt, 0xb0000, 0x8000, 0, &memh))
155 		return (0);
156 	oldval = bus_space_read_2(memt, memh, 0);
157 	bus_space_write_2(memt, memh, 0, 0xa55a);
158 	val = bus_space_read_2(memt, memh, 0);
159 	bus_space_write_2(memt, memh, 0, oldval);
160 	bus_space_unmap(memt, memh, 0x8000);
161 	if (val != 0xa55a)
162 		return (0);
163 
164 	if (bus_space_map(iot, 0x3b0, 0x10, 0, &ioh_6845))
165 		return (0);
166 	bus_space_unmap(iot, ioh_6845, 0x10);
167 
168 	return (1);
169 }
170 
171 static void
172 pcdisplay_init(struct pcdisplay_config *dc, bus_space_tag_t iot,
173     bus_space_tag_t memt, int mono)
174 {
175 	struct pcdisplay_handle *ph = &dc->dc_ph;
176 	int cpos;
177 
178         ph->ph_iot = iot;
179         ph->ph_memt = memt;
180 	dc->mono = mono;
181 
182 	if (bus_space_map(memt, mono ? 0xb0000 : 0xb8000, 0x8000,
183 			  0, &ph->ph_memh))
184 		panic("pcdisplay_init: can't map mem space");
185 	if (bus_space_map(iot, mono ? 0x3b0 : 0x3d0, 0x10,
186 			  0, &ph->ph_ioh_6845))
187 		panic("pcdisplay_init: can't map i/o space");
188 
189 	/*
190 	 * initialize the only screen
191 	 */
192 	dc->pcs.hdl = ph;
193 	dc->pcs.type = &pcdisplay_scr;
194 	dc->pcs.active = 1;
195 	dc->pcs.mem = NULL;
196 
197 	cpos = pcdisplay_6845_read(ph, cursorh) << 8;
198 	cpos |= pcdisplay_6845_read(ph, cursorl);
199 
200 	/* make sure we have a valid cursor position */
201 	if (cpos < 0 || cpos >= pcdisplay_scr.nrows * pcdisplay_scr.ncols)
202 		cpos = 0;
203 
204 	dc->pcs.dispoffset = 0;
205 	dc->pcs.visibleoffset = 0;
206 
207 	dc->pcs.vc_crow = cpos / pcdisplay_scr.ncols;
208 	dc->pcs.vc_ccol = cpos % pcdisplay_scr.ncols;
209 	pcdisplay_cursor_init(&dc->pcs, 1);
210 }
211 
212 int
213 pcdisplay_match(struct device *parent, void *match, void *aux)
214 {
215 	struct isa_attach_args *ia = aux;
216 	int mono;
217 
218 	/* If values are hardwired to something that they can't be, punt. */
219 	if ((ia->ia_iobase != IOBASEUNK &&
220 	     ia->ia_iobase != 0x3d0 &&
221 	     ia->ia_iobase != 0x3b0) ||
222 	    /* ia->ia_iosize != 0 || XXX isa.c */
223 	    (ia->ia_maddr != MADDRUNK &&
224 	     ia->ia_maddr != 0xb8000 &&
225 	     ia->ia_maddr != 0xb0000) ||
226 	    (ia->ia_msize != 0 && ia->ia_msize != 0x8000) ||
227 	    ia->ia_irq != IRQUNK || ia->ia_drq != DRQUNK)
228 		return (0);
229 
230 	if (pcdisplay_is_console(ia->ia_iot))
231 		mono = pcdisplay_console_dc.mono;
232 	else if (ia->ia_iobase != 0x3b0 && ia->ia_maddr != 0xb0000 &&
233 		 pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
234 		mono = 0;
235 	else if (ia->ia_iobase != 0x3d0 && ia->ia_maddr != 0xb8000 &&
236 		 pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
237 		mono = 1;
238 	else
239 		return (0);
240 
241 	ia->ia_iobase = mono ? 0x3b0 : 0x3d0;
242 	ia->ia_iosize = 0x10;
243 	ia->ia_maddr = mono ? 0xb0000 : 0xb8000;
244 	ia->ia_msize = 0x8000;
245 	return (1);
246 }
247 
248 void
249 pcdisplay_attach(struct device *parent, struct device *self, void *aux)
250 {
251 	struct isa_attach_args *ia = aux;
252 	struct pcdisplay_softc *sc = (struct pcdisplay_softc *)self;
253 	int console;
254 	struct pcdisplay_config *dc;
255 	struct wsemuldisplaydev_attach_args aa;
256 
257 	printf("\n");
258 
259 	console = pcdisplay_is_console(ia->ia_iot);
260 
261 	if (console) {
262 		dc = &pcdisplay_console_dc;
263 		sc->nscreens = 1;
264 		pcdisplay_console_attached = 1;
265 	} else {
266 		dc = malloc(sizeof(struct pcdisplay_config),
267 			    M_DEVBUF, M_WAITOK);
268 		if (ia->ia_iobase != 0x3b0 && ia->ia_maddr != 0xb0000 &&
269 		    pcdisplay_probe_col(ia->ia_iot, ia->ia_memt))
270 			pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 0);
271 		else if (ia->ia_iobase != 0x3d0 && ia->ia_maddr != 0xb8000 &&
272 			 pcdisplay_probe_mono(ia->ia_iot, ia->ia_memt))
273 			pcdisplay_init(dc, ia->ia_iot, ia->ia_memt, 1);
274 		else
275 			panic("pcdisplay_attach: display disappeared");
276 	}
277 	sc->sc_dc = dc;
278 
279 	aa.console = console;
280 	aa.scrdata = &pcdisplay_screenlist;
281 	aa.accessops = &pcdisplay_accessops;
282 	aa.accesscookie = sc;
283 	aa.defaultscreens = 0;
284 
285         config_found(self, &aa, wsemuldisplaydevprint);
286 }
287 
288 
289 int
290 pcdisplay_cnattach(bus_space_tag_t iot, bus_space_tag_t memt)
291 {
292 	int mono;
293 
294 	if (pcdisplay_probe_col(iot, memt))
295 		mono = 0;
296 	else if (pcdisplay_probe_mono(iot, memt))
297 		mono = 1;
298 	else
299 		return (ENXIO);
300 
301 	pcdisplay_init(&pcdisplay_console_dc, iot, memt, mono);
302 
303 	wsdisplay_cnattach(&pcdisplay_scr, &pcdisplay_console_dc,
304 			   pcdisplay_console_dc.pcs.vc_ccol,
305 			   pcdisplay_console_dc.pcs.vc_crow,
306 			   FG_LIGHTGREY | BG_BLACK);
307 
308 	pcdisplayconsole = 1;
309 	return (0);
310 }
311 
312 static int
313 pcdisplay_is_console(bus_space_tag_t iot)
314 {
315 	if (pcdisplayconsole &&
316 	    !pcdisplay_console_attached &&
317 	    iot == pcdisplay_console_dc.dc_ph.ph_iot)
318 		return (1);
319 	return (0);
320 }
321 
322 static int
323 pcdisplay_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
324 {
325 	/*
326 	 * XXX "do something!"
327 	 */
328 	return (-1);
329 }
330 
331 static paddr_t
332 pcdisplay_mmap(void *v, off_t offset, int prot)
333 {
334 	return (-1);
335 }
336 
337 static int
338 pcdisplay_alloc_screen(void *v, const struct wsscreen_descr *type,
339     void **cookiep, int *curxp, int *curyp, long *defattrp)
340 {
341 	struct pcdisplay_softc *sc = v;
342 
343 	if (sc->nscreens > 0)
344 		return (ENOMEM);
345 
346 	*cookiep = sc->sc_dc;
347 	*curxp = 0;
348 	*curyp = 0;
349 	*defattrp = FG_LIGHTGREY | BG_BLACK;
350 	sc->nscreens++;
351 	return (0);
352 }
353 
354 static void
355 pcdisplay_free_screen(void *v, void *cookie)
356 {
357 	struct pcdisplay_softc *sc = v;
358 
359 	if (sc->sc_dc == &pcdisplay_console_dc)
360 		panic("pcdisplay_free_screen: console");
361 
362 	sc->nscreens--;
363 }
364 
365 static int
366 pcdisplay_show_screen(void *v, void *cookie, int waitok,
367     void (*cb)(void *, int, int), void *cbarg)
368 {
369 #ifdef DIAGNOSTIC
370 	struct pcdisplay_softc *sc = v;
371 
372 	if (cookie != sc->sc_dc)
373 		panic("pcdisplay_show_screen: bad screen");
374 #endif
375 	return (0);
376 }
377 
378 static int
379 pcdisplay_alloc_attr(void *id, int fg, int bg, int flags, long *attrp)
380 {
381 	if (flags & WSATTR_REVERSE)
382 		*attrp = FG_BLACK | BG_LIGHTGREY;
383 	else
384 		*attrp = FG_LIGHTGREY | BG_BLACK;
385 	return (0);
386 }
387 
388 static void
389 pcdisplay_unpack_attr(void *id, long attr, int *fg, int *bg, int *ul)
390 {
391 	if (attr == (FG_BLACK | BG_LIGHTGREY)) {
392 		*fg = WSCOL_BLACK;
393 		*bg = WSCOL_WHITE;
394 	} else {
395 		*fg = WSCOL_WHITE;
396 		*bg = WSCOL_BLACK;
397 	}
398 	if (ul != NULL)
399 		*ul = 0;
400 }
401 
402 struct cfdriver pcdisplay_cd = {
403 	NULL, "pcdisplay", DV_DULL
404 };
405