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