Home | History | Annotate | Line # | Download | only in nubus
grf_nubus.c revision 1.8
      1 /*	$NetBSD: grf_nubus.c,v 1.8 1996/05/05 06:16:35 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/cpu.h>
     46 #include <machine/grfioctl.h>
     47 #include <machine/viareg.h>
     48 
     49 #include "nubus.h"
     50 #include "grfvar.h"
     51 
     52 static void	load_image_data __P((caddr_t data, struct image_data *image));
     53 static void	grfmv_intr __P((void *vsc, int slot));
     54 static int	get_vrsrcid __P((nubus_slot *slot));
     55 
     56 static char zero = 0;
     57 
     58 static int	grfmv_mode __P((struct grf_softc *gp, int cmd, void *arg));
     59 static caddr_t	grfmv_phys __P((struct grf_softc *gp, vm_offset_t addr));
     60 static int	grfmv_match __P((struct device *, void *, void *));
     61 static void	grfmv_attach __P((struct device *, struct device *, void *));
     62 
     63 struct cfattach grf_mv_ca = {
     64 	sizeof(struct grf_softc), grfmv_match, grfmv_attach
     65 };
     66 
     67 static void
     68 load_image_data(data, image)
     69 	caddr_t	data;
     70 	struct	image_data *image;
     71 {
     72 	bcopy(data     , &image->size,       4);
     73 	bcopy(data +  4, &image->offset,     4);
     74 	bcopy(data +  8, &image->rowbytes,   2);
     75 	bcopy(data + 10, &image->top,        2);
     76 	bcopy(data + 12, &image->left,       2);
     77 	bcopy(data + 14, &image->bottom,     2);
     78 	bcopy(data + 16, &image->right,      2);
     79 	bcopy(data + 18, &image->version,    2);
     80 	bcopy(data + 20, &image->packType,   2);
     81 	bcopy(data + 22, &image->packSize,   4);
     82 	bcopy(data + 26, &image->hRes,       4);
     83 	bcopy(data + 30, &image->vRes,       4);
     84 	bcopy(data + 34, &image->pixelType,  2);
     85 	bcopy(data + 36, &image->pixelSize,  2);
     86 	bcopy(data + 38, &image->cmpCount,   2);
     87 	bcopy(data + 40, &image->cmpSize,    2);
     88 	bcopy(data + 42, &image->planeBytes, 4);
     89 }
     90 
     91 /*ARGSUSED*/
     92 static void
     93 grfmv_intr(vsc, slot)
     94 	void	*vsc;
     95 	int	slot;
     96 {
     97 	caddr_t		 slotbase;
     98 
     99 	slotbase = (caddr_t) NUBUS_SLOT_TO_BASE(slot);
    100 	slotbase[0xa0000] = zero;
    101 }
    102 
    103 static int
    104 get_vrsrcid(slot)
    105 	nubus_slot	*slot;
    106 {
    107 extern	u_short	mac68k_vrsrc_vec[];
    108 	int	i;
    109 
    110 	for (i = 0 ; i < 6 ; i++)
    111 		if ((mac68k_vrsrc_vec[i] & 0xff) == slot->slot)
    112 			return ((mac68k_vrsrc_vec[i] >> 8) & 0xff);
    113 	return 0x80;
    114 }
    115 
    116 static int
    117 grfmv_match(pdp, match, aux)
    118 	struct device *pdp;
    119 	void *match, *aux;
    120 {
    121 	struct grf_softc	*sc;
    122 	nubus_slot	*slot = (nubus_slot *) aux;
    123 	nubus_dir	dir, *dirp, *dirp2;
    124 	nubus_dirent	dirent, *direntp;
    125 	nubus_type	slottype;
    126 	int		vrsrc;
    127 
    128 	sc = (struct grf_softc *) match;
    129 	dirp = &dir;
    130 	direntp = &dirent;
    131 	nubus_get_main_dir(slot, dirp);
    132 
    133 	vrsrc = get_vrsrcid(slot);
    134 	if (nubus_find_rsrc(slot, dirp, vrsrc, direntp) <= 0) {
    135 		if (   (vrsrc != 128)
    136 		    || (nubus_find_rsrc(slot, dirp, 129, direntp) <= 0)) {
    137 			return 0;
    138 		}
    139 	}
    140 
    141 	dirp2 = (nubus_dir *) &sc->board_dir;
    142 	nubus_get_dir_from_rsrc(slot, direntp, dirp2);
    143 
    144 	if (nubus_find_rsrc(slot, dirp2, NUBUS_RSRC_TYPE, direntp) <= 0)
    145 		/* Type is a required entry...  This should never happen. */
    146 		return 0;
    147 
    148 	if (nubus_get_ind_data(slot, direntp,
    149 			(caddr_t) &slottype, sizeof(nubus_type)) <= 0)
    150 		return 0;
    151 
    152 	if (slottype.category != NUBUS_CATEGORY_DISPLAY)
    153 		return 0;
    154 
    155 	if (slottype.type != NUBUS_TYPE_VIDEO)
    156 		return 0;
    157 
    158 	if (slottype.drsw != NUBUS_DRSW_APPLE)
    159 		return 0;
    160 
    161 	/*
    162 	 * If we've gotten this far, then we're dealing with a real-live
    163 	 * Apple QuickDraw-compatible display card resource.  Now, how to
    164 	 * determine that this is an active resource???  Dunno.  But we'll
    165 	 * proceed like it is.
    166 	 */
    167 
    168 	sc->card_id = slottype.drhw;
    169 
    170 	/* Need to load display info (and driver?), etc... */
    171 
    172 	return 1;
    173 }
    174 
    175 static void
    176 grfmv_attach(parent, self, aux)
    177 	struct device *parent, *self;
    178 	void *aux;
    179 {
    180 	struct grf_softc	*sc;
    181 	struct image_data image_store, image;
    182 	nubus_dirent	dirent;
    183 	nubus_dir	mode_dir;
    184 	int		mode;
    185 	u_long		base;
    186 
    187 	sc = (struct grf_softc *) self;
    188 
    189 	sc->g_mode = grfmv_mode;
    190 	sc->g_phys = grfmv_phys;
    191 
    192 	mode = NUBUS_RSRC_FIRSTMODE;
    193 	if (nubus_find_rsrc(&sc->sc_slot, &sc->board_dir, mode, &dirent) <= 0) {
    194 		printf("grf probe failed to get board rsrc.\n");
    195 		return;
    196 	}
    197 
    198 	nubus_get_dir_from_rsrc(&sc->sc_slot, &dirent, &mode_dir);
    199 
    200 	if (nubus_find_rsrc(&sc->sc_slot, &mode_dir, VID_PARAMS, &dirent)
    201 	    <= 0) {
    202 		printf("grf probe failed to get mode dir.\n");
    203 		return;
    204 	}
    205 
    206 	if (nubus_get_ind_data(&sc->sc_slot, &dirent, (caddr_t) &image_store,
    207 				sizeof(struct image_data)) <= 0) {
    208 		printf("grf probe failed to get indirect mode data.\n");
    209 		return;
    210 	}
    211 
    212 	load_image_data((caddr_t) &image_store, &image);
    213 
    214 	base = NUBUS_SLOT_TO_BASE(sc->sc_slot.slot);
    215 
    216 	sc->curr_mode.mode_id = mode;
    217 	sc->curr_mode.fbbase = (caddr_t) (base + image.offset);
    218 	sc->curr_mode.fboff = image.offset;
    219 	sc->curr_mode.rowbytes = image.rowbytes;
    220 	sc->curr_mode.width = image.right - image.left;
    221 	sc->curr_mode.height = image.bottom - image.top;
    222 	sc->curr_mode.fbsize = sc->curr_mode.height * sc->curr_mode.rowbytes;
    223 	sc->curr_mode.hres = image.hRes;
    224 	sc->curr_mode.vres = image.vRes;
    225 	sc->curr_mode.ptype = image.pixelType;
    226 	sc->curr_mode.psize = image.pixelSize;
    227 
    228 	strncpy(sc->card_name, nubus_get_card_name(&sc->sc_slot),
    229 		CARD_NAME_LEN);
    230 
    231 	sc->card_name[CARD_NAME_LEN-1] = '\0';
    232 
    233 	add_nubus_intr(sc->sc_slot.slot, grfmv_intr, sc);
    234 
    235 	sc->g_flags = GF_ALIVE;
    236 
    237 	printf(": %d x %d ", sc->curr_mode.width, sc->curr_mode.height);
    238 
    239 	if (sc->curr_mode.psize == 1)
    240 		printf("monochrome");
    241 	else
    242 		printf("%d color", 1 << sc->curr_mode.psize);
    243 
    244 	printf(" %s display\n", sc->card_name);
    245 }
    246 
    247 static int
    248 grfmv_mode(gp, cmd, arg)
    249 	struct grf_softc *gp;
    250 	int cmd;
    251 	void *arg;
    252 {
    253 	switch (cmd) {
    254 	case GM_GRFON:
    255 	case GM_GRFOFF:
    256 		return 0;
    257 	case GM_CURRMODE:
    258 		break;
    259 	case GM_NEWMODE:
    260 		break;
    261 	case GM_LISTMODES:
    262 		break;
    263 	}
    264 	return EINVAL;
    265 }
    266 
    267 static caddr_t
    268 grfmv_phys(gp, addr)
    269 	struct grf_softc *gp;
    270 	vm_offset_t addr;
    271 {
    272 	return (caddr_t) (NUBUS_SLOT_TO_PADDR(gp->sc_slot.slot) +
    273 				(addr - gp->sc_slot.virtual_base));
    274 }
    275