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