Home | History | Annotate | Line # | Download | only in nubus
grf_nubus.c revision 1.22
      1 /*	$NetBSD: grf_nubus.c,v 1.22 1997/05/01 03:34:07 briggs 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_cti __P((void *vsc, int slot));
     57 static void	grfmv_intr_cb264 __P((void *vsc, int slot));
     58 
     59 static int	grfmv_mode __P((struct grf_softc *gp, int cmd, void *arg));
     60 static caddr_t	grfmv_phys __P((struct grf_softc *gp, vm_offset_t addr));
     61 static int	grfmv_match __P((struct device *, struct cfdata *, void *));
     62 static void	grfmv_attach __P((struct device *, struct device *, void *));
     63 
     64 struct cfdriver macvid_cd = {
     65 	NULL, "macvid", DV_DULL
     66 };
     67 
     68 struct cfattach macvid_ca = {
     69 	sizeof(struct grfbus_softc), grfmv_match, grfmv_attach
     70 };
     71 
     72 static void
     73 load_image_data(data, image)
     74 	caddr_t	data;
     75 	struct	image_data *image;
     76 {
     77 	bcopy(data     , &image->size,       4);
     78 	bcopy(data +  4, &image->offset,     4);
     79 	bcopy(data +  8, &image->rowbytes,   2);
     80 	bcopy(data + 10, &image->top,        2);
     81 	bcopy(data + 12, &image->left,       2);
     82 	bcopy(data + 14, &image->bottom,     2);
     83 	bcopy(data + 16, &image->right,      2);
     84 	bcopy(data + 18, &image->version,    2);
     85 	bcopy(data + 20, &image->packType,   2);
     86 	bcopy(data + 22, &image->packSize,   4);
     87 	bcopy(data + 26, &image->hRes,       4);
     88 	bcopy(data + 30, &image->vRes,       4);
     89 	bcopy(data + 34, &image->pixelType,  2);
     90 	bcopy(data + 36, &image->pixelSize,  2);
     91 	bcopy(data + 38, &image->cmpCount,   2);
     92 	bcopy(data + 40, &image->cmpSize,    2);
     93 	bcopy(data + 42, &image->planeBytes, 4);
     94 }
     95 
     96 
     97 static int
     98 grfmv_match(parent, cf, aux)
     99 	struct device *parent;
    100 	struct cfdata *cf;
    101 	void *aux;
    102 {
    103 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
    104 
    105 	if (na->category != NUBUS_CATEGORY_DISPLAY)
    106 		return 0;
    107 
    108 	if (na->type != NUBUS_TYPE_VIDEO)
    109 		return 0;
    110 
    111 	if (na->drsw != NUBUS_DRSW_APPLE)
    112 		return 0;
    113 
    114 	/*
    115 	 * If we've gotten this far, then we're dealing with a real-live
    116 	 * Apple QuickDraw-compatible display card resource.  Now, how to
    117 	 * determine that this is an active resource???  Dunno.  But we'll
    118 	 * proceed like it is.
    119 	 */
    120 
    121 	return 1;
    122 }
    123 
    124 static void
    125 grfmv_attach(parent, self, aux)
    126 	struct device *parent, *self;
    127 	void *aux;
    128 {
    129 	struct grfbus_softc *sc = (struct grfbus_softc *)self;
    130 	struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
    131 	struct image_data image_store, image;
    132 	struct grfmode *gm;
    133 	char cardname[CARD_NAME_LEN];
    134 	nubus_dirent dirent;
    135 	nubus_dir dir, mode_dir;
    136 	int mode;
    137 
    138 	sc->card_id = na->drhw;
    139 
    140 	bcopy(na->fmt, &sc->sc_slot, sizeof(nubus_slot));
    141 
    142 	nubus_get_main_dir(&sc->sc_slot, &dir);
    143 
    144 	if (nubus_find_rsrc(&sc->sc_slot, &dir, na->rsrcid, &dirent) <= 0)
    145 		return;
    146 
    147 	nubus_get_dir_from_rsrc(&sc->sc_slot, &dirent, &sc->board_dir);
    148 
    149 	if (nubus_find_rsrc(&sc->sc_slot, &sc->board_dir,
    150 	    NUBUS_RSRC_TYPE, &dirent) <= 0)
    151 		if ((na->rsrcid != 128) ||
    152 		    (nubus_find_rsrc(&sc->sc_slot, &dir, 129, &dirent) <= 0))
    153 			return;
    154 
    155 	mode = NUBUS_RSRC_FIRSTMODE;
    156 	if (nubus_find_rsrc(&sc->sc_slot, &sc->board_dir, mode, &dirent) <= 0) {
    157 		printf("\n%s: probe failed to get board rsrc.\n",
    158 		    sc->sc_dev.dv_xname);
    159 		return;
    160 	}
    161 
    162 	nubus_get_dir_from_rsrc(&sc->sc_slot, &dirent, &mode_dir);
    163 
    164 	if (nubus_find_rsrc(&sc->sc_slot, &mode_dir, VID_PARAMS, &dirent)
    165 	    <= 0) {
    166 		printf("\n%s: probe failed to get mode dir.\n",
    167 		    sc->sc_dev.dv_xname);
    168 		return;
    169 	}
    170 
    171 	if (nubus_get_ind_data(&sc->sc_slot, &dirent, (caddr_t)&image_store,
    172 				sizeof(struct image_data)) <= 0) {
    173 		printf("\n%s: probe failed to get indirect mode data.\n",
    174 		    sc->sc_dev.dv_xname);
    175 		return;
    176 	}
    177 
    178 	/* Need to load display info (and driver?), etc... (?) */
    179 
    180 	load_image_data((caddr_t)&image_store, &image);
    181 
    182 	gm = &sc->curr_mode;
    183 	gm->mode_id = mode;
    184 	gm->fbbase = (caddr_t)(sc->sc_slot.virtual_base + image.offset);
    185 	gm->fboff = image.offset;
    186 	gm->rowbytes = image.rowbytes;
    187 	gm->width = image.right - image.left;
    188 	gm->height = image.bottom - image.top;
    189 	gm->fbsize = sc->curr_mode.height * sc->curr_mode.rowbytes;
    190 	gm->hres = image.hRes;
    191 	gm->vres = image.vRes;
    192 	gm->ptype = image.pixelType;
    193 	gm->psize = image.pixelSize;
    194 
    195 	strncpy(cardname, nubus_get_card_name(&sc->sc_slot),
    196 		CARD_NAME_LEN);
    197 	cardname[CARD_NAME_LEN-1] = '\0';
    198 	printf(": %s\n", cardname);
    199 
    200 	if (sc->card_id == NUBUS_DRHW_TFB) {
    201 		/*
    202 		 * This is the Toby card, but apparently some manufacturers
    203 		 * (like Cornerstone) didn't bother to get/use their own
    204 		 * value here, even though the cards are different, so we
    205 		 * so we try to differentiate here.
    206 		 */
    207 		if (strncmp(cardname, "Samsung 768", 11) == 0)
    208 			sc->card_id = NUBUS_DRHW_SAM768;
    209 		else if (strncmp(cardname, "Toby frame", 10) != 0)
    210 			printf("%s: This display card pretends to be a TFB!",
    211 			    sc->sc_dev.dv_xname);
    212 	}
    213 
    214 	switch (sc->card_id) {
    215 	case NUBUS_DRHW_M2HRVC:
    216 	case NUBUS_DRHW_TFB:
    217 		sc->cli_offset = 0xa0000;
    218 		sc->cli_value = 0;
    219 		add_nubus_intr(sc->sc_slot.slot, grfmv_intr_generic, sc);
    220 		break;
    221 	case NUBUS_DRHW_WVC:
    222 		sc->cli_offset = 0xa00000;
    223 		sc->cli_value = 0;
    224 		add_nubus_intr(sc->sc_slot.slot, grfmv_intr_generic, sc);
    225 		break;
    226 	case NUBUS_DRHW_FIILX:
    227 	case NUBUS_DRHW_FIISXDSP:
    228 		sc->cli_offset = 0xF05000;
    229 		sc->cli_value = 0x80;
    230 		add_nubus_intr(sc->sc_slot.slot, grfmv_intr_generic, sc);
    231 		break;
    232 	case NUBUS_DRHW_SAM768:
    233 		add_nubus_intr(sc->sc_slot.slot, grfmv_intr_cti, sc);
    234 		break;
    235 	case NUBUS_DRHW_CB264:
    236 	case NUBUS_DRHW_CB364:
    237 		add_nubus_intr(sc->sc_slot.slot, grfmv_intr_cb264, sc);
    238 		break;
    239 	case NUBUS_DRHW_SE30:
    240 		/* Do nothing--SE/30 interrupts are disabled */
    241 		break;
    242 	case NUBUS_DRHW_MICRON:
    243 		/* What do we know about this one? */
    244 	default:
    245 		printf("%s: Unknown video card ID 0x%x --",
    246 		    sc->sc_dev.dv_xname, sc->card_id);
    247 		printf(" Not installing interrupt routine.\n");
    248 		break;
    249 	}
    250 
    251 	/* Perform common video attachment. */
    252 	grf_establish(sc, &sc->sc_slot, grfmv_mode, grfmv_phys);
    253 }
    254 
    255 static int
    256 grfmv_mode(gp, cmd, arg)
    257 	struct grf_softc *gp;
    258 	int cmd;
    259 	void *arg;
    260 {
    261 	switch (cmd) {
    262 	case GM_GRFON:
    263 	case GM_GRFOFF:
    264 		return 0;
    265 	case GM_CURRMODE:
    266 		break;
    267 	case GM_NEWMODE:
    268 		break;
    269 	case GM_LISTMODES:
    270 		break;
    271 	}
    272 	return EINVAL;
    273 }
    274 
    275 static caddr_t
    276 grfmv_phys(gp, addr)
    277 	struct grf_softc *gp;
    278 	vm_offset_t addr;
    279 {
    280 	return (caddr_t)(NUBUS_SLOT2PA(gp->sc_slot->slot) +
    281 				(addr - gp->sc_slot->virtual_base));
    282 }
    283 
    284 /* Interrupt handlers... */
    285 /*
    286  * Generic routine to clear interrupts for cards where it simply takes
    287  * a CLR.B to clear the interrupt.  The offset of this byte varies between
    288  * cards.
    289  */
    290 /*ARGSUSED*/
    291 static void
    292 grfmv_intr_generic(vsc, slot)
    293 	void	*vsc;
    294 	int	slot;
    295 {
    296 	static char zero = 0;
    297 	struct grfbus_softc *sc;
    298 	volatile char *slotbase;
    299 
    300 	sc = (struct grfbus_softc *)vsc;
    301 	slotbase = (volatile char *)sc->sc_slot.virtual_base;
    302 	slotbase[sc->cli_offset] = zero;
    303 }
    304 
    305 /*
    306  * Routine to clear interrupts on Samsung 768x1006 video controller.
    307  * This controller was manufactured by Cornerstone Technology, Inc.,
    308  * now known as Cornerstone Imaging.
    309  *
    310  * To clear this interrupt, we apparently have to set, then clear,
    311  * bit 2 at byte offset 0x80000 from the card's base.
    312  *	Information for this provided by Brad Salai <bsalai (at) servtech.com>
    313  */
    314 /*ARGSUSED*/
    315 static void
    316 grfmv_intr_cti(vsc, slot)
    317 	void	*vsc;
    318 	int	slot;
    319 {
    320 	struct grfbus_softc *sc;
    321 	volatile char *slotbase;
    322 
    323 	sc = (struct grfbus_softc *)vsc;
    324 	slotbase = ((volatile char *)sc->sc_slot.virtual_base) + 0x00080000;
    325 	*slotbase = (*slotbase | 0x02);
    326 	*slotbase = (*slotbase & 0xFD);
    327 }
    328 
    329 /*ARGSUSED*/
    330 static void
    331 grfmv_intr_cb264(vsc, slot)
    332 	void	*vsc;
    333 	int	slot;
    334 {
    335 	struct grfbus_softc *sc;
    336 	volatile char *slotbase;
    337 
    338 	sc = (struct grfbus_softc *)vsc;
    339 	slotbase = (volatile char *)sc->sc_slot.virtual_base;
    340 	asm volatile("	movl	%0,a0
    341 			movl	a0@(0xff6028),d0
    342 			andl	#0x2,d0
    343 			beq	_mv_intr0
    344 			movql	#0x3,d0
    345 		_mv_intr0:
    346 			movl	a0@(0xff600c),d1
    347 			andl	#0x3,d1
    348 			cmpl	d1,d0
    349 			beq	_mv_intr_fin
    350 			movl	d0,a0@(0xff600c)
    351 			nop
    352 			tstb	d0
    353 			beq	_mv_intr1
    354 			movl	#0x0002,a0@(0xff6040)
    355 			movl	#0x0102,a0@(0xff6044)
    356 			movl	#0x0105,a0@(0xff6048)
    357 			movl	#0x000e,a0@(0xff604c)
    358 			movl	#0x001c,a0@(0xff6050)
    359 			movl	#0x00bc,a0@(0xff6054)
    360 			movl	#0x00c3,a0@(0xff6058)
    361 			movl	#0x0061,a0@(0xff605c)
    362 			movl	#0x0012,a0@(0xff6060)
    363 			bra	_mv_intr_fin
    364 		_mv_intr1:
    365 			movl	#0x0002,a0@(0xff6040)
    366 			movl	#0x0209,a0@(0xff6044)
    367 			movl	#0x020c,a0@(0xff6048)
    368 			movl	#0x000f,a0@(0xff604c)
    369 			movl	#0x0027,a0@(0xff6050)
    370 			movl	#0x00c7,a0@(0xff6054)
    371 			movl	#0x00d7,a0@(0xff6058)
    372 			movl	#0x006b,a0@(0xff605c)
    373 			movl	#0x0029,a0@(0xff6060)
    374 		_mv_intr_fin:
    375 			movl	#0x1,a0@(0xff6014)"
    376 		: : "g" (slotbase) : "a0","d0","d1");
    377 }
    378