xref: /netbsd-src/sys/dev/isa/pcdisplay.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /* $NetBSD: pcdisplay.c,v 1.44 2019/12/01 14:18:51 ad 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.44 2019/12/01 14:18:51 ad 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 pcdisplay_config *sc_dc;
64 	int nscreens;
65 #if NPCWEASEL > 0
66 	struct weasel_handle sc_weasel;
67 #endif
68 };
69 
70 static int pcdisplayconsole, pcdisplay_console_attached;
71 static struct pcdisplay_config pcdisplay_console_dc;
72 
73 int	pcdisplay_match(device_t, cfdata_t, void *);
74 void	pcdisplay_attach(device_t, device_t, void *);
75 
76 static int pcdisplay_is_console(bus_space_tag_t);
77 static int pcdisplay_probe_col(bus_space_tag_t, bus_space_tag_t);
78 static int pcdisplay_probe_mono(bus_space_tag_t, bus_space_tag_t);
79 static void pcdisplay_init(struct pcdisplay_config *,
80 			     bus_space_tag_t, bus_space_tag_t,
81 			     int);
82 static int pcdisplay_allocattr(void *, int, int, int, long *);
83 
84 CFATTACH_DECL_NEW(pcdisplay, sizeof(struct pcdisplay_softc),
85     pcdisplay_match, pcdisplay_attach, NULL, NULL);
86 
87 const struct wsdisplay_emulops pcdisplay_emulops = {
88 	pcdisplay_cursor,
89 	pcdisplay_mapchar,
90 	pcdisplay_putchar,
91 	pcdisplay_copycols,
92 	pcdisplay_erasecols,
93 	pcdisplay_copyrows,
94 	pcdisplay_eraserows,
95 	pcdisplay_allocattr,
96 	NULL,	/* replaceattr */
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 	NULL, /* modecookie */
105 };
106 
107 const struct wsscreen_descr *_pcdisplay_scrlist[] = {
108 	&pcdisplay_scr,
109 };
110 
111 const struct wsscreen_list pcdisplay_screenlist = {
112 	sizeof(_pcdisplay_scrlist) / sizeof(struct wsscreen_descr *),
113 	_pcdisplay_scrlist
114 };
115 
116 static int pcdisplay_ioctl(void *, void *, u_long, void *, int, struct lwp *);
117 static paddr_t pcdisplay_mmap(void *, void *, off_t, int);
118 static int pcdisplay_alloc_screen(void *, const struct wsscreen_descr *,
119 				       void **, int *, int *, long *);
120 static void pcdisplay_free_screen(void *, void *);
121 static int pcdisplay_show_screen(void *, void *, int,
122 				      void (*) (void *, int, int), void *);
123 
124 const struct wsdisplay_accessops pcdisplay_accessops = {
125 	pcdisplay_ioctl,
126 	pcdisplay_mmap,
127 	pcdisplay_alloc_screen,
128 	pcdisplay_free_screen,
129 	pcdisplay_show_screen,
130 	NULL, /* load_font */
131 	NULL, /* pollc */
132 	NULL, /* scroll */
133 };
134 
135 static int
136 pcdisplay_probe_col(bus_space_tag_t iot, bus_space_tag_t memt)
137 {
138 	bus_space_handle_t memh, ioh_6845;
139 	u_int16_t oldval, val;
140 
141 	if (bus_space_map(memt, 0xb8000, 0x8000, 0, &memh))
142 		return (0);
143 	oldval = bus_space_read_2(memt, memh, 0);
144 	bus_space_write_2(memt, memh, 0, 0xa55a);
145 	val = bus_space_read_2(memt, memh, 0);
146 	bus_space_write_2(memt, memh, 0, oldval);
147 	bus_space_unmap(memt, memh, 0x8000);
148 	if (val != 0xa55a)
149 		return (0);
150 
151 	if (bus_space_map(iot, 0x3d0, 0x10, 0, &ioh_6845))
152 		return (0);
153 	bus_space_unmap(iot, ioh_6845, 0x10);
154 
155 	return (1);
156 }
157 
158 static int
159 pcdisplay_probe_mono(bus_space_tag_t iot, bus_space_tag_t memt)
160 {
161 	bus_space_handle_t memh, ioh_6845;
162 	u_int16_t oldval, val;
163 
164 	if (bus_space_map(memt, 0xb0000, 0x8000, 0, &memh))
165 		return (0);
166 	oldval = bus_space_read_2(memt, memh, 0);
167 	bus_space_write_2(memt, memh, 0, 0xa55a);
168 	val = bus_space_read_2(memt, memh, 0);
169 	bus_space_write_2(memt, memh, 0, oldval);
170 	bus_space_unmap(memt, memh, 0x8000);
171 	if (val != 0xa55a)
172 		return (0);
173 
174 	if (bus_space_map(iot, 0x3b0, 0x10, 0, &ioh_6845))
175 		return (0);
176 	bus_space_unmap(iot, ioh_6845, 0x10);
177 
178 	return (1);
179 }
180 
181 static void
182 pcdisplay_init(struct pcdisplay_config *dc, bus_space_tag_t iot,
183     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 	    BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_PREFETCHABLE,
194 	    &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(device_t parent, cfdata_t match, 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_addr = 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(device_t parent, device_t self, void *aux)
284 {
285 	struct isa_attach_args *ia = aux;
286 	struct pcdisplay_softc *sc = device_private(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 = self;
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 	    bus_space_is_equal(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, int prot)
382 {
383 	return (-1);
384 }
385 
386 static int
387 pcdisplay_alloc_screen(void *v, const struct wsscreen_descr *type,
388     void **cookiep, int *curxp, int *curyp, long *defattrp)
389 {
390 	struct pcdisplay_softc *sc = v;
391 
392 	if (sc->nscreens > 0)
393 		return (ENOMEM);
394 
395 	*cookiep = sc->sc_dc;
396 	*curxp = 0;
397 	*curyp = 0;
398 	*defattrp = FG_LIGHTGREY | BG_BLACK;
399 	sc->nscreens++;
400 	return (0);
401 }
402 
403 static void
404 pcdisplay_free_screen(void *v, void *cookie)
405 {
406 	struct pcdisplay_softc *sc = v;
407 
408 	if (sc->sc_dc == &pcdisplay_console_dc)
409 		panic("pcdisplay_free_screen: console");
410 
411 	sc->nscreens--;
412 }
413 
414 static int
415 pcdisplay_show_screen(void *v, void *cookie, int waitok,
416     void (*cb)(void *, int, int), void *cbarg)
417 {
418 #ifdef DIAGNOSTIC
419 	struct pcdisplay_softc *sc = v;
420 
421 	if (cookie != sc->sc_dc)
422 		panic("pcdisplay_show_screen: bad screen");
423 #endif
424 	return (0);
425 }
426 
427 static int
428 pcdisplay_allocattr(void *id, int fg, int bg, int flags, long *attrp)
429 {
430 	if (flags & WSATTR_REVERSE)
431 		*attrp = FG_BLACK | BG_LIGHTGREY;
432 	else
433 		*attrp = FG_LIGHTGREY | BG_BLACK;
434 	return (0);
435 }
436