Home | History | Annotate | Line # | Download | only in nubus
grf_nubus.c revision 1.28
      1 /*	$NetBSD: grf_nubus.c,v 1.28 1997/07/26 08:21:15 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_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, vm_offset_t addr));
     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 + image.offset); /* 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, addr)
    304 	struct grf_softc *gp;
    305 	vm_offset_t addr;
    306 {
    307 	return (caddr_t)(NUBUS_SLOT2PA(gp->sc_slot->slot) +
    308 	    (addr - gp->sc_handle));	/* XXX evil hack */
    309 }
    310 
    311 /* Interrupt handlers... */
    312 /*
    313  * Generic routine to clear interrupts for cards where it simply takes
    314  * a MOV.B to clear the interrupt.  The offset and value of this byte
    315  * varies between cards.
    316  */
    317 /*ARGSUSED*/
    318 static void
    319 grfmv_intr_generic_1(vsc, slot)
    320 	void	*vsc;
    321 	int	slot;
    322 {
    323 	struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
    324 
    325 	bus_space_write_1(sc->sc_tag, sc->sc_handle,
    326 	    sc->cli_offset, (u_int8_t)sc->cli_value);
    327 }
    328 
    329 /*
    330  * Generic routine to clear interrupts for cards where it simply takes
    331  * a MOV.L to clear the interrupt.  The offset and value of this byte
    332  * varies between cards.
    333  */
    334 /*ARGSUSED*/
    335 static void
    336 grfmv_intr_generic_4(vsc, slot)
    337 	void	*vsc;
    338 	int	slot;
    339 {
    340 	struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
    341 
    342 	bus_space_write_4(sc->sc_tag, sc->sc_handle,
    343 	    sc->cli_offset, sc->cli_value);
    344 }
    345 
    346 /*
    347  * Routine to clear interrupts for the Radius PrecisionColor 8xj card.
    348  */
    349 /*ARGSUSED*/
    350 static void
    351 grfmv_intr_radius(vsc, slot)
    352 	void	*vsc;
    353 	int	slot;
    354 {
    355 	struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
    356 	u_int8_t c;
    357 
    358 	/*
    359 	 * The value 0x66 was the observed value on one	card.  It is read
    360 	 * from the driver's information block, so this may not be sufficient.
    361 	 * Then again, we're not setting up any other interrupts...
    362 	 */
    363 	c = 0x66;
    364 
    365 	c |= 0x80;
    366 	bus_space_write_1(sc->sc_tag, sc->sc_handle, 0xd00403, c);
    367 	c &= 0x7f;
    368 	bus_space_write_1(sc->sc_tag, sc->sc_handle, 0xd00403, c);
    369 }
    370 
    371 /*
    372  * Routine to clear interrupts on Samsung 768x1006 video controller.
    373  * This controller was manufactured by Cornerstone Technology, Inc.,
    374  * now known as Cornerstone Imaging.
    375  *
    376  * To clear this interrupt, we apparently have to set, then clear,
    377  * bit 2 at byte offset 0x80000 from the card's base.
    378  *	Information for this provided by Brad Salai <bsalai (at) servtech.com>
    379  */
    380 /*ARGSUSED*/
    381 static void
    382 grfmv_intr_cti(vsc, slot)
    383 	void	*vsc;
    384 	int	slot;
    385 {
    386 	struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
    387 	u_int8_t c;
    388 
    389 	c = bus_space_read_1(sc->sc_tag, sc->sc_handle, 0x80000);
    390 	c |= 0x02;
    391 	bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x80000, c);
    392 	c &= 0xfd;
    393 	bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x80000, c);
    394 }
    395 
    396 /*ARGSUSED*/
    397 static void
    398 grfmv_intr_cb264(vsc, slot)
    399 	void	*vsc;
    400 	int	slot;
    401 {
    402 	struct grfbus_softc *sc;
    403 	volatile char *slotbase;
    404 
    405 	sc = (struct grfbus_softc *)vsc;
    406 	slotbase = (volatile char *)sc->sc_handle;	/* XXX evil hack */
    407 	asm volatile("	movl	%0,a0
    408 			movl	a0@(0xff6028),d0
    409 			andl	#0x2,d0
    410 			beq	_mv_intr0
    411 			movql	#0x3,d0
    412 		_mv_intr0:
    413 			movl	a0@(0xff600c),d1
    414 			andl	#0x3,d1
    415 			cmpl	d1,d0
    416 			beq	_mv_intr_fin
    417 			movl	d0,a0@(0xff600c)
    418 			nop
    419 			tstb	d0
    420 			beq	_mv_intr1
    421 			movl	#0x0002,a0@(0xff6040)
    422 			movl	#0x0102,a0@(0xff6044)
    423 			movl	#0x0105,a0@(0xff6048)
    424 			movl	#0x000e,a0@(0xff604c)
    425 			movl	#0x001c,a0@(0xff6050)
    426 			movl	#0x00bc,a0@(0xff6054)
    427 			movl	#0x00c3,a0@(0xff6058)
    428 			movl	#0x0061,a0@(0xff605c)
    429 			movl	#0x0012,a0@(0xff6060)
    430 			bra	_mv_intr_fin
    431 		_mv_intr1:
    432 			movl	#0x0002,a0@(0xff6040)
    433 			movl	#0x0209,a0@(0xff6044)
    434 			movl	#0x020c,a0@(0xff6048)
    435 			movl	#0x000f,a0@(0xff604c)
    436 			movl	#0x0027,a0@(0xff6050)
    437 			movl	#0x00c7,a0@(0xff6054)
    438 			movl	#0x00d7,a0@(0xff6058)
    439 			movl	#0x006b,a0@(0xff605c)
    440 			movl	#0x0029,a0@(0xff6060)
    441 		_mv_intr_fin:
    442 			movl	#0x1,a0@(0xff6014)"
    443 		: : "g" (slotbase) : "a0","d0","d1");
    444 }
    445 
    446 /*
    447  * Support for the Colorboard 364 might be more complex than it needs to
    448  * be.  If we can find more information about this card, this might be
    449  * significantly simplified.  Contributions welcome...  :-)
    450  */
    451 /*ARGSUSED*/
    452 static void
    453 grfmv_intr_cb364(vsc, slot)
    454 	void	*vsc;
    455 	int	slot;
    456 {
    457 	struct grfbus_softc *sc;
    458 	volatile char *slotbase;
    459 
    460 	sc = (struct grfbus_softc *)vsc;
    461 	slotbase = (volatile char *)sc->sc_handle;	/* XXX evil hack */
    462 	asm volatile("	movl	%0,a0
    463 			movl	a0@(0xfe6028),d0
    464 			andl	#0x2,d0
    465 			beq	_cb364_intr4
    466 			movql	#0x3,d0
    467 			movl	a0@(0xfe6018),d1
    468 			movl	#0x3,a0@(0xfe6018)
    469 			movw	a0@(0xfe7010),d2
    470 			movl	d1,a0@(0xfe6018)
    471 			movl	a0@(0xfe6020),d1
    472 			btst	#0x06,d2
    473 			beq	_cb364_intr0
    474 			btst	#0x00,d1
    475 			beq	_cb364_intr5
    476 			bsr	_cb364_intr1
    477 			bra	_cb364_intr_out
    478 		_cb364_intr0:
    479 			btst	#0x00,d1
    480 			bne	_cb364_intr5
    481 			bsr	_cb364_intr1
    482 			bra	_cb364_intr_out
    483 		_cb364_intr1:
    484 			movl	d0,a0@(0xfe600c)
    485 			nop
    486 			tstb	d0
    487 			beq	_cb364_intr3
    488 			movl	#0x0002,a0@(0xfe6040)
    489 			movl	#0x0105,a0@(0xfe6048)
    490 			movl	#0x000e,a0@(0xfe604c)
    491 			movl	#0x00c3,a0@(0xfe6058)
    492 			movl	#0x0061,a0@(0xfe605c)
    493 			btst	#0x06,d2
    494 			beq	_cb364_intr2
    495 			movl	#0x001c,a0@(0xfe6050)
    496 			movl	#0x00bc,a0@(0xfe6054)
    497 			movl	#0x0012,a0@(0xfe6060)
    498 			movl	#0x000e,a0@(0xfe6044)
    499 			movl	#0x00c3,a0@(0xfe6064)
    500 			movl	#0x0061,a0@(0xfe6020)
    501 			rts
    502 		_cb364_intr2:
    503 			movl	#0x0016,a0@(0xfe6050)
    504 			movl	#0x00b6,a0@(0xfe6054)
    505 			movl	#0x0011,a0@(0xfe6060)
    506 			movl	#0x0101,a0@(0xfe6044)
    507 			movl	#0x00bf,a0@(0xfe6064)
    508 			movl	#0x0001,a0@(0xfe6020)
    509 			rts
    510 		_cb364_intr3:
    511 			movl	#0x0002,a0@(0xfe6040)
    512 			movl	#0x0209,a0@(0xfe6044)
    513 			movl	#0x020c,a0@(0xfe6048)
    514 			movl	#0x000f,a0@(0xfe604c)
    515 			movl	#0x0027,a0@(0xfe6050)
    516 			movl	#0x00c7,a0@(0xfe6054)
    517 			movl	#0x00d7,a0@(0xfe6058)
    518 			movl	#0x006b,a0@(0xfe605c)
    519 			movl	#0x0029,a0@(0xfe6060)
    520 			oril	#0x0040,a0@(0xfe6064)
    521 			movl	#0x0000,a0@(0xfe6020)
    522 			rts
    523 		_cb364_intr4:
    524 			movq	#0x00,d0
    525 		_cb364_intr5:
    526 			movl	a0@(0xfe600c),d1
    527 			andl	#0x3,d1
    528 			cmpl	d1,d0
    529 			beq	_cb364_intr_out
    530 			bsr	_cb364_intr1
    531 		_cb364_intr_out:
    532 			movl	#0x1,a0@(0xfe6014)
    533 		_cb364_intr_quit:
    534 		" : : "g" (slotbase) : "a0","d0","d1","d2");
    535 }
    536