Home | History | Annotate | Line # | Download | only in obio
grf_obio.c revision 1.54
      1 /*	$NetBSD: grf_obio.c,v 1.54 2007/08/29 12:39:32 jmmv Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1998 Scott Reynolds
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 /*
     30  * Copyright (c) 1995 Allen Briggs.  All rights reserved.
     31  *
     32  * Redistribution and use in source and binary forms, with or without
     33  * modification, are permitted provided that the following conditions
     34  * are met:
     35  * 1. Redistributions of source code must retain the above copyright
     36  *    notice, this list of conditions and the following disclaimer.
     37  * 2. Redistributions in binary form must reproduce the above copyright
     38  *    notice, this list of conditions and the following disclaimer in the
     39  *    documentation and/or other materials provided with the distribution.
     40  * 3. All advertising materials mentioning features or use of this software
     41  *    must display the following acknowledgement:
     42  *	This product includes software developed by Allen Briggs.
     43  * 4. The name of the author may not be used to endorse or promote products
     44  *    derived from this software without specific prior written permission.
     45  *
     46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     47  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     49  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     50  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     51  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     52  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     53  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     54  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     55  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     56  */
     57 /*
     58  * Graphics display driver for the Macintosh internal video for machines
     59  * that don't map it into a fake nubus card.
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: grf_obio.c,v 1.54 2007/08/29 12:39:32 jmmv Exp $");
     64 
     65 #include <sys/param.h>
     66 #include <sys/device.h>
     67 #include <sys/ioctl.h>
     68 #include <sys/file.h>
     69 #include <sys/malloc.h>
     70 #include <sys/mman.h>
     71 #include <sys/proc.h>
     72 #include <sys/systm.h>
     73 
     74 #include <machine/autoconf.h>
     75 #include <machine/bus.h>
     76 #include <machine/cpu.h>
     77 #include <machine/grfioctl.h>
     78 #include <machine/viareg.h>
     79 
     80 #include <mac68k/nubus/nubus.h>
     81 #include <mac68k/obio/obiovar.h>
     82 #include <mac68k/dev/grfvar.h>
     83 
     84 extern u_int32_t	mac68k_vidphys;
     85 extern u_int32_t	mac68k_vidlen;
     86 extern long		videoaddr;
     87 extern long		videorowbytes;
     88 extern long		videobitdepth;
     89 extern long		videowidth;
     90 extern long		videoheight;
     91 
     92 static int	grfiv_mode(struct grf_softc *, int, void *);
     93 static int	grfiv_match(struct device *, struct cfdata *, void *);
     94 static void	grfiv_attach(struct device *, struct device *, void *);
     95 
     96 CFATTACH_DECL(intvid, sizeof(struct grfbus_softc),
     97     grfiv_match, grfiv_attach, NULL, NULL);
     98 
     99 #define	DAFB_BASE		0xf9000000
    100 #define DAFB_CONTROL_BASE	0xf9800000
    101 #define	CIVIC_BASE		0x50100000
    102 #define CIVIC_CONTROL_BASE	0x50036000
    103 #define	VALKYRIE_BASE		0xf9000000
    104 #define VALKYRIE_CONTROL_BASE	0x50f2a000
    105 
    106 static int
    107 grfiv_match(struct device *parent, struct cfdata *cf, void *aux)
    108 {
    109 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    110 	bus_space_handle_t bsh;
    111 	int found;
    112 	u_int base;
    113 
    114 	found = 1;
    115 
    116 	switch (current_mac_model->class) {
    117 	case MACH_CLASSQ2:
    118 		if (current_mac_model->machineid != MACH_MACLC575) {
    119 			base = VALKYRIE_CONTROL_BASE;
    120 
    121 			if (bus_space_map(oa->oa_tag, base, 0x40, 0, &bsh))
    122 				return 0;
    123 
    124 			/* Disable interrupts */
    125 			bus_space_write_1(oa->oa_tag, bsh, 0x18, 0x1);
    126 
    127 			bus_space_unmap(oa->oa_tag, bsh, 0x40);
    128 			break;
    129 		}
    130 		/*
    131 		 * Note:  the only system in this class that does not have
    132 		 * the Valkyrie chip -- at least, that we know of -- is
    133 		 * the Performa/LC 57x series.  This system has a version
    134 		 * of the DAFB controller, instead.
    135 		 *
    136 		 * If this assumption proves false, we'll have to be more
    137 		 * intelligent here.
    138 		 */
    139 		/*FALLTHROUGH*/
    140 	case MACH_CLASSQ:
    141 		/*
    142 		 * Assume DAFB for all of these, unless we can't
    143 		 * access the memory.
    144 		 */
    145 		base = DAFB_CONTROL_BASE;
    146 
    147 		if (bus_space_map(oa->oa_tag, base, 0x20, 0, &bsh))
    148 			return 0;
    149 
    150 		if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0x1c, 4) == 0) {
    151 			bus_space_unmap(oa->oa_tag, bsh, 0x20);
    152 			return 0;
    153 		}
    154 
    155 		bus_space_unmap(oa->oa_tag, bsh, 0x20);
    156 
    157 		if (bus_space_map(oa->oa_tag, base + 0x100, 0x20, 0, &bsh))
    158 			return 0;
    159 
    160 		if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0x04, 4) == 0) {
    161 			bus_space_unmap(oa->oa_tag, bsh, 0x20);
    162 			return 0;
    163 		}
    164 
    165 		/* Disable interrupts */
    166 		bus_space_write_4(oa->oa_tag, bsh, 0x04, 0);
    167 
    168 		/* Clear any interrupts */
    169 		bus_space_write_4(oa->oa_tag, bsh, 0x0C, 0);
    170 		bus_space_write_4(oa->oa_tag, bsh, 0x10, 0);
    171 		bus_space_write_4(oa->oa_tag, bsh, 0x14, 0);
    172 
    173 		bus_space_unmap(oa->oa_tag, bsh, 0x20);
    174 		break;
    175 	case MACH_CLASSAV:
    176 		base = CIVIC_CONTROL_BASE;
    177 
    178 		if (bus_space_map(oa->oa_tag, base, 0x1000, 0, &bsh))
    179 			return 0;
    180 
    181 		/* Disable interrupts */
    182 		bus_space_write_1(oa->oa_tag, bsh, 0x120, 0);
    183 
    184 		bus_space_unmap(oa->oa_tag, bsh, 0x1000);
    185 		break;
    186 	case MACH_CLASSIIci:
    187 	case MACH_CLASSIIsi:
    188 		if (mac68k_vidlen == 0 ||
    189 		    (via2_reg(rMonitor) & RBVMonitorMask) == RBVMonIDNone)
    190 			found = 0;
    191 		break;
    192 	default:
    193 		if (mac68k_vidlen == 0)
    194 			found = 0;
    195 		break;
    196 	}
    197 
    198 	return found;
    199 }
    200 
    201 static void
    202 grfiv_attach(struct device *parent, struct device *self, void *aux)
    203 {
    204 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    205 	struct grfbus_softc *sc;
    206 	struct grfmode *gm;
    207 	u_long base, length;
    208 	u_int32_t vbase1, vbase2;
    209 
    210 	sc = (struct grfbus_softc *)self;
    211 
    212 	sc->card_id = 0;
    213 
    214 	switch (current_mac_model->class) {
    215 	case MACH_CLASSQ2:
    216 		if (current_mac_model->machineid != MACH_MACLC575) {
    217 			sc->sc_basepa = VALKYRIE_BASE;
    218 			length = 0x00100000;		/* 1MB */
    219 
    220 			if (sc->sc_basepa <= mac68k_vidphys &&
    221 			    mac68k_vidphys < (sc->sc_basepa + length)) {
    222 				sc->sc_fbofs = mac68k_vidphys - sc->sc_basepa;
    223 			} else {
    224 				sc->sc_basepa = m68k_trunc_page(mac68k_vidphys);
    225 				sc->sc_fbofs = m68k_page_offset(mac68k_vidphys);
    226 				length = mac68k_vidlen + sc->sc_fbofs;
    227 			}
    228 
    229 			printf(" @ %lx: Valkyrie video subsystem\n",
    230 			    sc->sc_basepa + sc->sc_fbofs);
    231 			break;
    232 		}
    233 		/* See note in grfiv_match() */
    234 		/*FALLTHROUGH*/
    235 	case MACH_CLASSQ:
    236 		base = DAFB_CONTROL_BASE;
    237 		sc->sc_tag = oa->oa_tag;
    238 		if (bus_space_map(sc->sc_tag, base, 0x20, 0, &sc->sc_regh)) {
    239 			printf(": failed to map DAFB register space\n");
    240 			return;
    241 		}
    242 
    243 		sc->sc_basepa = DAFB_BASE;
    244 		length = 0x00100000;		/* 1MB */
    245 
    246 		/* Compute the current frame buffer offset */
    247 		vbase1 = bus_space_read_4(sc->sc_tag, sc->sc_regh, 0x0) & 0xfff;
    248 #if 1
    249 		/*
    250 		 * XXX The following exists because the DAFB v7 in these
    251 		 * systems doesn't return reasonable values to use for fbofs.
    252 		 * Ken'ichi Ishizaka gets credit for this hack.  (sar 19990426)
    253 		 * (Does this get us the correct result for _all_ DAFB-
    254 		 * equipped systems and monitor combinations?  It seems
    255 		 * possible, if not likely...)
    256 		 */
    257 		switch (current_mac_model->machineid) {
    258 		case MACH_MACLC475:
    259 		case MACH_MACLC475_33:
    260 		case MACH_MACLC575:
    261 		case MACH_MACQ605:
    262 		case MACH_MACQ605_33:
    263 			vbase1 &= 0x3f;
    264 			break;
    265 		}
    266 #endif
    267 		vbase2 = bus_space_read_4(sc->sc_tag, sc->sc_regh, 0x4) & 0xf;
    268 		sc->sc_fbofs = (vbase1 << 9) | (vbase2 << 5);
    269 
    270 		printf(" @ %lx: DAFB video subsystem, monitor sense %x\n",
    271 		    sc->sc_basepa + sc->sc_fbofs,
    272 		    (bus_space_read_4(sc->sc_tag, sc->sc_regh, 0x1c) & 0x7));
    273 
    274 		bus_space_unmap(sc->sc_tag, sc->sc_regh, 0x20);
    275 		break;
    276 	case MACH_CLASSAV:
    277 		sc->sc_basepa = CIVIC_BASE;
    278 		length = 0x00200000;		/* 2MB */
    279 		if (mac68k_vidphys >= sc->sc_basepa &&
    280 		    mac68k_vidphys < (sc->sc_basepa + length)) {
    281 			sc->sc_fbofs = mac68k_vidphys - sc->sc_basepa;
    282 		} else {
    283 			sc->sc_basepa = m68k_trunc_page(mac68k_vidphys);
    284 			sc->sc_fbofs = m68k_page_offset(mac68k_vidphys);
    285 			length = mac68k_vidlen + sc->sc_fbofs;
    286 		}
    287 
    288 		printf(" @ %lx: CIVIC video subsystem\n",
    289 		    sc->sc_basepa + sc->sc_fbofs);
    290 		break;
    291 	case MACH_CLASSIIci:
    292 	case MACH_CLASSIIsi:
    293 		sc->sc_basepa = m68k_trunc_page(mac68k_vidphys);
    294 		sc->sc_fbofs = m68k_page_offset(mac68k_vidphys);
    295 		length = mac68k_vidlen + sc->sc_fbofs;
    296 
    297 		printf(" @ %lx: RBV video subsystem, ",
    298 		    sc->sc_basepa + sc->sc_fbofs);
    299 		switch (via2_reg(rMonitor) & RBVMonitorMask) {
    300 		case RBVMonIDBWP:
    301 			printf("15\" monochrome portrait");
    302 			break;
    303 		case RBVMonIDRGB12:
    304 			printf("12\" color");
    305 			break;
    306 		case RBVMonIDRGB15:
    307 			printf("15\" color");
    308 			break;
    309 		case RBVMonIDStd:
    310 			printf("Macintosh II");
    311 			break;
    312 		default:
    313 			printf("unrecognized");
    314 			break;
    315 		}
    316 		printf(" display\n");
    317 
    318 		break;
    319 	default:
    320 		sc->sc_basepa = m68k_trunc_page(mac68k_vidphys);
    321 		sc->sc_fbofs = m68k_page_offset(mac68k_vidphys);
    322 		length = mac68k_vidlen + sc->sc_fbofs;
    323 
    324 		printf(" @ %lx: On-board video\n",
    325 		    sc->sc_basepa + sc->sc_fbofs);
    326 		break;
    327 	}
    328 
    329 	if (bus_space_map(sc->sc_tag, sc->sc_basepa, length, 0,
    330 	    &sc->sc_handle)) {
    331 		printf("%s: failed to map video RAM\n", sc->sc_dev.dv_xname);
    332 		return;
    333 	}
    334 
    335 	if (sc->sc_basepa <= mac68k_vidphys &&
    336 	    mac68k_vidphys < (sc->sc_basepa + length))
    337 		videoaddr = sc->sc_handle.base + sc->sc_fbofs; /* XXX hack */
    338 
    339 	gm = &(sc->curr_mode);
    340 	gm->mode_id = 0;
    341 	gm->psize = videobitdepth;
    342 	gm->ptype = 0;
    343 	gm->width = videowidth;
    344 	gm->height = videoheight;
    345 	gm->rowbytes = videorowbytes;
    346 	gm->hres = 80;				/* XXX hack */
    347 	gm->vres = 80;				/* XXX hack */
    348 	gm->fbsize = gm->height * gm->rowbytes;
    349 	gm->fbbase = (void *)sc->sc_handle.base; /* XXX yet another hack */
    350 	gm->fboff = sc->sc_fbofs;
    351 
    352 	/* Perform common video attachment. */
    353 	grf_establish(sc, NULL, grfiv_mode);
    354 }
    355 
    356 static int
    357 grfiv_mode(struct grf_softc *sc, int cmd, void *arg)
    358 {
    359 	switch (cmd) {
    360 	case GM_GRFON:
    361 	case GM_GRFOFF:
    362 		return 0;
    363 	case GM_CURRMODE:
    364 		break;
    365 	case GM_NEWMODE:
    366 		break;
    367 	case GM_LISTMODES:
    368 		break;
    369 	}
    370 	return EINVAL;
    371 }
    372