Home | History | Annotate | Line # | Download | only in nubus
grf_nubus.c revision 1.26
      1 /*	$NetBSD: grf_nubus.c,v 1.26 1997/05/12 20:35:50 scottr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Allen Briggs.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Allen Briggs.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 /*
     32  * Device-specific routines for handling Nubus-based video cards.
     33  */
     34 
     35 #include <sys/param.h>
     36 
     37 #include <sys/device.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/file.h>
     40 #include <sys/malloc.h>
     41 #include <sys/mman.h>
     42 #include <sys/proc.h>
     43 #include <sys/systm.h>
     44 
     45 #include <machine/bus.h>
     46 #include <machine/cpu.h>
     47 #include <machine/grfioctl.h>
     48 #include <machine/viareg.h>
     49 
     50 #include "nubus.h"
     51 #include "grfvar.h"
     52 
     53 static void	load_image_data __P((caddr_t data, struct image_data *image));
     54 
     55 static void	grfmv_intr_generic __P((void *vsc, int slot));
     56 static void	grfmv_intr_radius __P((void *vsc, int slot));
     57 static void	grfmv_intr_cti __P((void *vsc, int slot));
     58 static void	grfmv_intr_cb264 __P((void *vsc, int slot));
     59 static void	grfmv_intr_cb364 __P((void *vsc, int slot));
     60 
     61 static int	grfmv_mode __P((struct grf_softc *gp, int cmd, void *arg));
     62 static caddr_t	grfmv_phys __P((struct grf_softc *gp, vm_offset_t addr));
     63 static int	grfmv_match __P((struct device *, struct cfdata *, void *));
     64 static void	grfmv_attach __P((struct device *, struct device *, void *));
     65 
     66 struct cfdriver macvid_cd = {
     67 	NULL, "macvid", DV_DULL
     68 };
     69 
     70 struct cfattach macvid_ca = {
     71 	sizeof(struct grfbus_softc), grfmv_match, grfmv_attach
     72 };
     73 
     74 static void
     75 load_image_data(data, image)
     76 	caddr_t	data;
     77 	struct	image_data *image;
     78 {
     79 	bcopy(data     , &image->size,       4);
     80 	bcopy(data +  4, &image->offset,     4);
     81 	bcopy(data +  8, &image->rowbytes,   2);
     82 	bcopy(data + 10, &image->top,        2);
     83 	bcopy(data + 12, &image->left,       2);
     84 	bcopy(data + 14, &image->bottom,     2);
     85 	bcopy(data + 16, &image->right,      2);
     86 	bcopy(data + 18, &image->version,    2);
     87 	bcopy(data + 20, &image->packType,   2);
     88 	bcopy(data + 22, &image->packSize,   4);
     89 	bcopy(data + 26, &image->hRes,       4);
     90 	bcopy(data + 30, &image->vRes,       4);
     91 	bcopy(data + 34, &image->pixelType,  2);
     92 	bcopy(data + 36, &image->pixelSize,  2);
     93 	bcopy(data + 38, &image->cmpCount,   2);
     94 	bcopy(data + 40, &image->cmpSize,    2);
     95 	bcopy(data + 42, &image->planeBytes, 4);
     96 }
     97 
     98 
     99 static int
    100 grfmv_match(parent, cf, aux)
    101 	struct device *parent;
    102 	struct cfdata *cf;
    103 	void *aux;
    104 {
    105 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
    106 
    107 	if (na->category != NUBUS_CATEGORY_DISPLAY)
    108 		return 0;
    109 
    110 	if (na->type != NUBUS_TYPE_VIDEO)
    111 		return 0;
    112 
    113 	if (na->drsw != NUBUS_DRSW_APPLE)
    114 		return 0;
    115 
    116 	/*
    117 	 * If we've gotten this far, then we're dealing with a real-live
    118 	 * Apple QuickDraw-compatible display card resource.  Now, how to
    119 	 * determine that this is an active resource???  Dunno.  But we'll
    120 	 * proceed like it is.
    121 	 */
    122 
    123 	return 1;
    124 }
    125 
    126 static void
    127 grfmv_attach(parent, self, aux)
    128 	struct device *parent, *self;
    129 	void *aux;
    130 {
    131 	struct grfbus_softc *sc = (struct grfbus_softc *)self;
    132 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
    133 	struct image_data image_store, image;
    134 	struct grfmode *gm;
    135 	char cardname[CARD_NAME_LEN];
    136 	nubus_dirent dirent;
    137 	nubus_dir dir, mode_dir;
    138 	int mode;
    139 
    140 	sc->sc_tag = na->na_tag;
    141 	sc->card_id = na->drhw;
    142 
    143 	bcopy(na->fmt, &sc->sc_slot, sizeof(nubus_slot));
    144 
    145 	if (bus_space_map(sc->sc_tag,
    146 	    NUBUS_SLOT2PA(na->slot), NBMEMSIZE, 0, &sc->sc_handle)) {
    147 		printf(": grfmv_attach: failed to map slot %d\n", na->slot);
    148 		return;
    149 	}
    150 
    151 	nubus_get_main_dir(&sc->sc_slot, &dir);
    152 
    153 	if (nubus_find_rsrc(sc->sc_tag, sc->sc_handle,
    154 	    &sc->sc_slot, &dir, na->rsrcid, &dirent) <= 0) {
    155 bad:
    156 		bus_space_unmap(sc->sc_tag, sc->sc_handle, NBMEMSIZE);
    157 		return;
    158 	}
    159 
    160 	nubus_get_dir_from_rsrc(&sc->sc_slot, &dirent, &sc->board_dir);
    161 
    162 	if (nubus_find_rsrc(sc->sc_tag, sc->sc_handle,
    163 	    &sc->sc_slot, &sc->board_dir, NUBUS_RSRC_TYPE, &dirent) <= 0)
    164 		if ((na->rsrcid != 128) ||
    165 		    (nubus_find_rsrc(sc->sc_tag, sc->sc_handle,
    166 		    &sc->sc_slot, &dir, 129, &dirent) <= 0))
    167 			goto bad;
    168 
    169 	mode = NUBUS_RSRC_FIRSTMODE;
    170 	if (nubus_find_rsrc(sc->sc_tag, sc->sc_handle,
    171 	    &sc->sc_slot, &sc->board_dir, mode, &dirent) <= 0) {
    172 		printf(": probe failed to get board rsrc.\n");
    173 		goto bad;
    174 	}
    175 
    176 	nubus_get_dir_from_rsrc(&sc->sc_slot, &dirent, &mode_dir);
    177 
    178 	if (nubus_find_rsrc(sc->sc_tag, sc->sc_handle,
    179 	    &sc->sc_slot, &mode_dir, VID_PARAMS, &dirent) <= 0) {
    180 		printf(": probe failed to get mode dir.\n");
    181 		goto bad;
    182 	}
    183 
    184 	if (nubus_get_ind_data(sc->sc_tag, sc->sc_handle, &sc->sc_slot,
    185 	    &dirent, (caddr_t)&image_store, sizeof(struct image_data)) <= 0) {
    186 		printf(": probe failed to get indirect mode data.\n");
    187 		goto bad;
    188 	}
    189 
    190 	/* Need to load display info (and driver?), etc... (?) */
    191 
    192 	load_image_data((caddr_t)&image_store, &image);
    193 
    194 	gm = &sc->curr_mode;
    195 	gm->mode_id = mode;
    196 	gm->fbbase = (caddr_t)(sc->sc_handle + image.offset); /* XXX evil! */
    197 	gm->fboff = image.offset;
    198 	gm->rowbytes = image.rowbytes;
    199 	gm->width = image.right - image.left;
    200 	gm->height = image.bottom - image.top;
    201 	gm->fbsize = sc->curr_mode.height * sc->curr_mode.rowbytes;
    202 	gm->hres = image.hRes;
    203 	gm->vres = image.vRes;
    204 	gm->ptype = image.pixelType;
    205 	gm->psize = image.pixelSize;
    206 
    207 	strncpy(cardname, nubus_get_card_name(sc->sc_tag, sc->sc_handle,
    208 	    &sc->sc_slot), CARD_NAME_LEN);
    209 	cardname[CARD_NAME_LEN-1] = '\0';
    210 	printf(": %s\n", cardname);
    211 
    212 	if (sc->card_id == NUBUS_DRHW_TFB) {
    213 		/*
    214 		 * This is the Toby card, but apparently some manufacturers
    215 		 * (like Cornerstone) didn't bother to get/use their own
    216 		 * value here, even though the cards are different, so we
    217 		 * so we try to differentiate here.
    218 		 */
    219 		if (strncmp(cardname, "Samsung 768", 11) == 0)
    220 			sc->card_id = NUBUS_DRHW_SAM768;
    221 		else if (strncmp(cardname, "Toby frame", 10) != 0)
    222 			printf("%s: This display card pretends to be a TFB!\n",
    223 			    sc->sc_dev.dv_xname);
    224 	}
    225 
    226 	switch (sc->card_id) {
    227 	case NUBUS_DRHW_M2HRVC:
    228 	case NUBUS_DRHW_TFB:
    229 		sc->cli_offset = 0xa0000;
    230 		sc->cli_value = 0;
    231 		add_nubus_intr(na->slot, grfmv_intr_generic, sc);
    232 		break;
    233 	case NUBUS_DRHW_WVC:
    234 		sc->cli_offset = 0xa00000;
    235 		sc->cli_value = 0;
    236 		add_nubus_intr(na->slot, grfmv_intr_generic, sc);
    237 		break;
    238 	case NUBUS_DRHW_RPC8XJ:
    239 		add_nubus_intr(na->slot, grfmv_intr_radius, sc);
    240 		break;
    241 	case NUBUS_DRHW_FIILX:
    242 	case NUBUS_DRHW_FIISXDSP:
    243 		sc->cli_offset = 0xf05000;
    244 		sc->cli_value = 0x80;
    245 		add_nubus_intr(na->slot, grfmv_intr_generic, sc);
    246 		break;
    247 	case NUBUS_DRHW_SAM768:
    248 		add_nubus_intr(na->slot, grfmv_intr_cti, sc);
    249 		break;
    250 	case NUBUS_DRHW_CB264:
    251 		add_nubus_intr(na->slot, grfmv_intr_cb264, sc);
    252 		break;
    253 	case NUBUS_DRHW_CB364:
    254 		add_nubus_intr(na->slot, grfmv_intr_cb364, sc);
    255 		break;
    256 	case NUBUS_DRHW_SE30:
    257 		/* Do nothing--SE/30 interrupts are disabled */
    258 		break;
    259 	case NUBUS_DRHW_MICRON:
    260 		/* What do we know about this one? */
    261 	default:
    262 		printf("%s: Unknown video card ID 0x%x --",
    263 		    sc->sc_dev.dv_xname, sc->card_id);
    264 		printf(" Not installing interrupt routine.\n");
    265 		break;
    266 	}
    267 
    268 	/* Perform common video attachment. */
    269 	grf_establish(sc, &sc->sc_slot, grfmv_mode, grfmv_phys);
    270 }
    271 
    272 static int
    273 grfmv_mode(gp, cmd, arg)
    274 	struct grf_softc *gp;
    275 	int cmd;
    276 	void *arg;
    277 {
    278 	switch (cmd) {
    279 	case GM_GRFON:
    280 	case GM_GRFOFF:
    281 		return 0;
    282 	case GM_CURRMODE:
    283 		break;
    284 	case GM_NEWMODE:
    285 		break;
    286 	case GM_LISTMODES:
    287 		break;
    288 	}
    289 	return EINVAL;
    290 }
    291 
    292 static caddr_t
    293 grfmv_phys(gp, addr)
    294 	struct grf_softc *gp;
    295 	vm_offset_t addr;
    296 {
    297 	return (caddr_t)(NUBUS_SLOT2PA(gp->sc_slot->slot) +
    298 	    (addr - gp->sc_handle));	/* XXX evil hack */
    299 }
    300 
    301 /* Interrupt handlers... */
    302 /*
    303  * Generic routine to clear interrupts for cards where it simply takes
    304  * a CLR.B to clear the interrupt.  The offset of this byte varies between
    305  * cards.
    306  */
    307 /*ARGSUSED*/
    308 static void
    309 grfmv_intr_generic(vsc, slot)
    310 	void	*vsc;
    311 	int	slot;
    312 {
    313 	struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
    314 
    315 	bus_space_write_1(sc->sc_tag, sc->sc_handle,
    316 	    sc->cli_offset, sc->cli_value);
    317 }
    318 
    319 /*
    320  * Routine to clear interrupts for the Radius PrecisionColor 8xj card.
    321  */
    322 /*ARGSUSED*/
    323 static void
    324 grfmv_intr_radius(vsc, slot)
    325 	void	*vsc;
    326 	int	slot;
    327 {
    328 	struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
    329 	u_int8_t c;
    330 
    331 	/*
    332 	 * The value 0x66 was the observed value on one	card.  It is read
    333 	 * from the driver's information block, so this may not be sufficient.
    334 	 * Then again, we're not setting up any other interrupts...
    335 	 */
    336 	c = 0x66;
    337 
    338 	c |= 0x80;
    339 	bus_space_write_1(sc->sc_tag, sc->sc_handle, 0xd00403, c);
    340 	c &= 0x7f;
    341 	bus_space_write_1(sc->sc_tag, sc->sc_handle, 0xd00403, c);
    342 }
    343 
    344 /*
    345  * Routine to clear interrupts on Samsung 768x1006 video controller.
    346  * This controller was manufactured by Cornerstone Technology, Inc.,
    347  * now known as Cornerstone Imaging.
    348  *
    349  * To clear this interrupt, we apparently have to set, then clear,
    350  * bit 2 at byte offset 0x80000 from the card's base.
    351  *	Information for this provided by Brad Salai <bsalai (at) servtech.com>
    352  */
    353 /*ARGSUSED*/
    354 static void
    355 grfmv_intr_cti(vsc, slot)
    356 	void	*vsc;
    357 	int	slot;
    358 {
    359 	struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
    360 	u_int8_t c;
    361 
    362 	c = bus_space_read_1(sc->sc_tag, sc->sc_handle, 0x80000);
    363 	c |= 0x02;
    364 	bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x80000, c);
    365 	c &= 0xfd;
    366 	bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x80000, c);
    367 }
    368 
    369 /*ARGSUSED*/
    370 static void
    371 grfmv_intr_cb264(vsc, slot)
    372 	void	*vsc;
    373 	int	slot;
    374 {
    375 	struct grfbus_softc *sc;
    376 	volatile char *slotbase;
    377 
    378 	sc = (struct grfbus_softc *)vsc;
    379 	slotbase = (volatile char *)sc->sc_handle;	/* XXX evil hack */
    380 	asm volatile("	movl	%0,a0
    381 			movl	a0@(0xff6028),d0
    382 			andl	#0x2,d0
    383 			beq	_mv_intr0
    384 			movql	#0x3,d0
    385 		_mv_intr0:
    386 			movl	a0@(0xff600c),d1
    387 			andl	#0x3,d1
    388 			cmpl	d1,d0
    389 			beq	_mv_intr_fin
    390 			movl	d0,a0@(0xff600c)
    391 			nop
    392 			tstb	d0
    393 			beq	_mv_intr1
    394 			movl	#0x0002,a0@(0xff6040)
    395 			movl	#0x0102,a0@(0xff6044)
    396 			movl	#0x0105,a0@(0xff6048)
    397 			movl	#0x000e,a0@(0xff604c)
    398 			movl	#0x001c,a0@(0xff6050)
    399 			movl	#0x00bc,a0@(0xff6054)
    400 			movl	#0x00c3,a0@(0xff6058)
    401 			movl	#0x0061,a0@(0xff605c)
    402 			movl	#0x0012,a0@(0xff6060)
    403 			bra	_mv_intr_fin
    404 		_mv_intr1:
    405 			movl	#0x0002,a0@(0xff6040)
    406 			movl	#0x0209,a0@(0xff6044)
    407 			movl	#0x020c,a0@(0xff6048)
    408 			movl	#0x000f,a0@(0xff604c)
    409 			movl	#0x0027,a0@(0xff6050)
    410 			movl	#0x00c7,a0@(0xff6054)
    411 			movl	#0x00d7,a0@(0xff6058)
    412 			movl	#0x006b,a0@(0xff605c)
    413 			movl	#0x0029,a0@(0xff6060)
    414 		_mv_intr_fin:
    415 			movl	#0x1,a0@(0xff6014)"
    416 		: : "g" (slotbase) : "a0","d0","d1");
    417 }
    418 
    419 /*
    420  * Support for the Colorboard 364 might be more complex than it needs to
    421  * be.  If we can find more information about this card, this might be
    422  * significantly simplified.  Contributions welcome...  :-)
    423  */
    424 /*ARGSUSED*/
    425 static void
    426 grfmv_intr_cb364(vsc, slot)
    427 	void	*vsc;
    428 	int	slot;
    429 {
    430 	struct grfbus_softc *sc;
    431 	volatile char *slotbase;
    432 
    433 	sc = (struct grfbus_softc *)vsc;
    434 	slotbase = (volatile char *)sc->sc_handle;	/* XXX evil hack */
    435 	asm volatile("	movl	%0,a0
    436 			movl	a0@(0xfe6028),d0
    437 			andl	#0x2,d0
    438 			beq	_cb364_intr4
    439 			movql	#0x3,d0
    440 			movl	a0@(0xfe6018),d1
    441 			movl	#0x3,a0@(0xfe6018)
    442 			movw	a0@(0xfe7010),d2
    443 			movl	d1,a0@(0xfe6018)
    444 			movl	a0@(0xfe6020),d1
    445 			btst	#0x06,d2
    446 			beq	_cb364_intr0
    447 			btst	#0x00,d1
    448 			beq	_cb364_intr5
    449 			bsr	_cb364_intr1
    450 			bra	_cb364_intr_out
    451 		_cb364_intr0:
    452 			btst	#0x00,d1
    453 			bne	_cb364_intr5
    454 			bsr	_cb364_intr1
    455 			bra	_cb364_intr_out
    456 		_cb364_intr1:
    457 			movl	d0,a0@(0xfe600c)
    458 			nop
    459 			tstb	d0
    460 			beq	_cb364_intr3
    461 			movl	#0x0002,a0@(0xfe6040)
    462 			movl	#0x0105,a0@(0xfe6048)
    463 			movl	#0x000e,a0@(0xfe604c)
    464 			movl	#0x00c3,a0@(0xfe6058)
    465 			movl	#0x0061,a0@(0xfe605c)
    466 			btst	#0x06,d2
    467 			beq	_cb364_intr2
    468 			movl	#0x001c,a0@(0xfe6050)
    469 			movl	#0x00bc,a0@(0xfe6054)
    470 			movl	#0x0012,a0@(0xfe6060)
    471 			movl	#0x000e,a0@(0xfe6044)
    472 			movl	#0x00c3,a0@(0xfe6064)
    473 			movl	#0x0061,a0@(0xfe6020)
    474 			rts
    475 		_cb364_intr2:
    476 			movl	#0x0016,a0@(0xfe6050)
    477 			movl	#0x00b6,a0@(0xfe6054)
    478 			movl	#0x0011,a0@(0xfe6060)
    479 			movl	#0x0101,a0@(0xfe6044)
    480 			movl	#0x00bf,a0@(0xfe6064)
    481 			movl	#0x0001,a0@(0xfe6020)
    482 			rts
    483 		_cb364_intr3:
    484 			movl	#0x0002,a0@(0xfe6040)
    485 			movl	#0x0209,a0@(0xfe6044)
    486 			movl	#0x020c,a0@(0xfe6048)
    487 			movl	#0x000f,a0@(0xfe604c)
    488 			movl	#0x0027,a0@(0xfe6050)
    489 			movl	#0x00c7,a0@(0xfe6054)
    490 			movl	#0x00d7,a0@(0xfe6058)
    491 			movl	#0x006b,a0@(0xfe605c)
    492 			movl	#0x0029,a0@(0xfe6060)
    493 			oril	#0x0040,a0@(0xfe6064)
    494 			movl	#0x0000,a0@(0xfe6020)
    495 			rts
    496 		_cb364_intr4:
    497 			movq	#0x00,d0
    498 		_cb364_intr5:
    499 			movl	a0@(0xfe600c),d1
    500 			andl	#0x3,d1
    501 			cmpl	d1,d0
    502 			beq	_cb364_intr_out
    503 			bsr	_cb364_intr1
    504 		_cb364_intr_out:
    505 			movl	#0x1,a0@(0xfe6014)
    506 		_cb364_intr_quit:
    507 		" : : "g" (slotbase) : "a0","d0","d1","d2");
    508 }
    509