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