Home | History | Annotate | Line # | Download | only in dev
pvr.c revision 1.5
      1 /*	$NetBSD: pvr.c,v 1.5 2001/02/02 03:07:29 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 Marcus Comstedt.
      5  * Copyright (c) 2001 Jason R. Thorpe.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Marcus Comstedt.
     19  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  *    contributors may be used to endorse or promote products derived
     21  *    from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * Copyright (c) 1998, 1999 Tohru Nishimura.  All rights reserved.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *      This product includes software developed by Tohru Nishimura
     50  *	for the NetBSD Project.
     51  * 4. The name of the author may not be used to endorse or promote products
     52  *    derived from this software without specific prior written permission
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     55  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     56  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     57  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     58  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     59  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     60  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     61  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     63  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     64  */
     65 
     66 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     67 
     68 __KERNEL_RCSID(0, "$NetBSD: pvr.c,v 1.5 2001/02/02 03:07:29 thorpej Exp $");
     69 
     70 #include <sys/param.h>
     71 #include <sys/systm.h>
     72 #include <sys/kernel.h>
     73 #include <sys/device.h>
     74 #include <sys/malloc.h>
     75 #include <sys/buf.h>
     76 #include <sys/ioctl.h>
     77 
     78 #include <machine/cpu.h>
     79 #include <machine/bus.h>
     80 
     81 #include <dev/cons.h>
     82 
     83 #include <dev/wscons/wsconsio.h>
     84 #include <dev/wscons/wsdisplayvar.h>
     85 
     86 #include <dev/wscons/wscons_callbacks.h>
     87 
     88 #include <dev/rasops/rasops.h>
     89 #include <dev/wsfont/wsfont.h>
     90 
     91 #include <dreamcast/dev/pvrvar.h>
     92 #include <dreamcast/dev/maple/mkbdvar.h>
     93 
     94 #include <sh3/shbvar.h>
     95 
     96 #include "mkbd.h"
     97 
     98 struct fb_devconfig {
     99 	vaddr_t dc_vaddr;		/* framebuffer virtual address */
    100 	vaddr_t dc_paddr;		/* framebuffer physical address */
    101 	int	dc_wid;			/* width of frame buffer */
    102 	int	dc_ht;			/* height of frame buffer */
    103 	int	dc_depth;		/* depth, bits per pixel */
    104 	int	dc_rowbytes;		/* bytes in a FB scan line */
    105 	vaddr_t	dc_videobase;		/* base of flat frame buffer */
    106 	int	dc_blanked;		/* currently has video disabled */
    107 	int	dc_dispflags;		/* display flags */
    108 
    109 	struct rasops_info rinfo;
    110 };
    111 
    112 #define	PVR_RGBMODE	0x01		/* RGB or composite */
    113 #define	PVR_VGAMODE	0x02		/* VGA */
    114 
    115 struct pvr_softc {
    116 	struct device sc_dev;
    117 	struct fb_devconfig *sc_dc;	/* device configuration */
    118 	int nscreens;
    119 };
    120 
    121 int	pvr_match(struct device *, struct cfdata *, void *);
    122 void	pvr_attach(struct device *, struct device *, void *);
    123 
    124 struct cfattach pvr_ca = {
    125 	sizeof(struct pvr_softc), pvr_match, pvr_attach,
    126 };
    127 
    128 void	pvr_getdevconfig(struct fb_devconfig *);
    129 
    130 struct fb_devconfig pvr_console_dc;
    131 
    132 char pvr_stdscreen_textgeom[32] = { "std" };	/* XXX yuck */
    133 
    134 struct wsscreen_descr pvr_stdscreen = {
    135 	pvr_stdscreen_textgeom, 0, 0,
    136 	0, /* textops */
    137 	0, 0,
    138 	WSSCREEN_WSCOLORS,
    139 };
    140 
    141 const struct wsscreen_descr *_pvr_scrlist[] = {
    142 	&pvr_stdscreen,
    143 };
    144 
    145 const struct wsscreen_list pvr_screenlist = {
    146 	sizeof(_pvr_scrlist) / sizeof(struct wsscreen_descr *), _pvr_scrlist
    147 };
    148 
    149 int	pvrioctl(void *, u_long, caddr_t, int, struct proc *);
    150 paddr_t	pvrmmap(void *, off_t, int);
    151 
    152 int	pvr_alloc_screen(void *, const struct wsscreen_descr *,
    153 	    void **, int *, int *, long *);
    154 void	pvr_free_screen(void *, void *);
    155 int	pvr_show_screen(void *, void *, int,
    156 	    void (*)(void *, int, int), void *);
    157 
    158 const struct wsdisplay_accessops pvr_accessops = {
    159 	pvrioctl,
    160 	pvrmmap,
    161 	pvr_alloc_screen,
    162 	pvr_free_screen,
    163 	pvr_show_screen,
    164 	NULL, /* load_font */
    165 };
    166 
    167 void	pvrinit(struct fb_devconfig *);
    168 
    169 int	pvr_is_console;
    170 
    171 int
    172 pvr_match(struct device *parent, struct cfdata *match, void *aux)
    173 {
    174 	struct shb_attach_args *sa = aux;
    175 
    176 	if (strcmp("pvr", match->cf_driver->cd_name) != 0)
    177 		return (0);
    178 
    179 	sa->ia_iosize = 0; /* 0x1400 */;
    180 	return (1);
    181 }
    182 
    183 void
    184 pvr_getdevconfig(struct fb_devconfig *dc)
    185 {
    186 	int i, cookie;
    187 
    188 	dc->dc_paddr = 0x05000000;
    189 	dc->dc_vaddr = SH3_PHYS_TO_P2SEG(dc->dc_paddr);
    190 
    191 	dc->dc_wid = 640;
    192 	dc->dc_ht = 480;
    193 	dc->dc_depth = 16;
    194 	dc->dc_rowbytes = dc->dc_wid * (dc->dc_depth / 8);
    195 	dc->dc_videobase = dc->dc_vaddr;
    196 	dc->dc_blanked = 0;
    197 	dc->dc_dispflags = 0;
    198 
    199 	/* Clear the screen. */
    200 	for (i = 0; i < dc->dc_ht * dc->dc_rowbytes; i += sizeof(u_int32_t))
    201 		*(u_int32_t *)(dc->dc_videobase + i) = 0x0;
    202 
    203 	/* Initialize the device. */
    204 	pvrinit(dc);
    205 
    206 	dc->rinfo.ri_flg = 0;
    207 	dc->rinfo.ri_depth = dc->dc_depth;
    208 	dc->rinfo.ri_bits = (void *) dc->dc_videobase;
    209 	dc->rinfo.ri_width = dc->dc_wid;
    210 	dc->rinfo.ri_height = dc->dc_ht;
    211 	dc->rinfo.ri_stride = dc->dc_rowbytes;
    212 
    213 	wsfont_init();
    214 	/* prefer 8 pixel wide font */
    215 	if ((cookie = wsfont_find(NULL, 8, 0, 0)) <= 0)
    216 		cookie = wsfont_find(NULL, 0, 0, 0);
    217 	if (cookie <= 0) {
    218 		printf("pvr: font table is empty\n");
    219 		return;
    220 	}
    221 
    222 	if (wsfont_lock(cookie, &dc->rinfo.ri_font,
    223 	    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R) <= 0) {
    224 		printf("pvr: unable to lock font\n");
    225 		return;
    226 	}
    227 	dc->rinfo.ri_wsfcookie = cookie;
    228 
    229 	rasops_init(&dc->rinfo, 500, 500);
    230 
    231 	/* XXX shouldn't be global */
    232 	pvr_stdscreen.nrows = dc->rinfo.ri_rows;
    233 	pvr_stdscreen.ncols = dc->rinfo.ri_cols;
    234 	pvr_stdscreen.textops = &dc->rinfo.ri_ops;
    235 	pvr_stdscreen.capabilities = dc->rinfo.ri_caps;
    236 
    237 	/* XXX yuck */
    238 	sprintf(pvr_stdscreen_textgeom, "%dx%d", pvr_stdscreen.ncols,
    239 	    pvr_stdscreen.nrows);
    240 }
    241 
    242 void
    243 pvr_attach(struct device *parent, struct device *self, void *aux)
    244 {
    245 	struct pvr_softc *sc = (void *) self;
    246 	struct wsemuldisplaydev_attach_args waa;
    247 	int console;
    248 
    249 	console = pvr_is_console;
    250 	if (console) {
    251 		sc->sc_dc = &pvr_console_dc;
    252 		sc->nscreens = 1;
    253 	} else {
    254 		sc->sc_dc = malloc(sizeof(struct fb_devconfig), M_DEVBUF,
    255 		    M_WAITOK);
    256 		pvr_getdevconfig(sc->sc_dc);
    257 	}
    258 	printf(": %d x %d, %dbpp, %s, %s\n", sc->sc_dc->dc_wid,
    259 	    sc->sc_dc->dc_ht, sc->sc_dc->dc_depth,
    260 							/* XXX PAL? */
    261 	    (sc->sc_dc->dc_dispflags & PVR_VGAMODE) ? "VGA" : "NTSC",
    262 	    (sc->sc_dc->dc_dispflags & PVR_RGBMODE) ? "RGB" : "composite");
    263 
    264 	/* XXX Colormap initialization? */
    265 
    266 	waa.console = console;
    267 	waa.scrdata = &pvr_screenlist;
    268 	waa.accessops = &pvr_accessops;
    269 	waa.accesscookie = sc;
    270 
    271 	(void) config_found(self, &waa, wsemuldisplaydevprint);
    272 }
    273 
    274 int
    275 pvrioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    276 {
    277 	struct pvr_softc *sc = v;
    278 	struct fb_devconfig *dc = sc->sc_dc;
    279 
    280 	switch (cmd) {
    281 	case WSDISPLAYIO_GTYPE:
    282 		*(u_int *)data = WSDISPLAY_TYPE_DCPVR;
    283 		return (0);
    284 
    285 	case WSDISPLAYIO_GINFO:
    286 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    287 		wsd_fbip->height = sc->sc_dc->dc_ht;
    288 		wsd_fbip->width = sc->sc_dc->dc_wid;
    289 		wsd_fbip->depth = sc->sc_dc->dc_depth;
    290 		wsd_fbip->cmsize = 0;	/* XXX Colormap */
    291 #undef wsd_fbip
    292 		return (0);
    293 
    294 	case WSDISPLAYIO_GETCMAP:
    295 	case WSDISPLAYIO_PUTCMAP:
    296 		return (ENOTTY);	/* XXX Colormap */
    297 
    298 	case WSDISPLAYIO_SVIDEO:
    299 		return (ENOTTY);	/* XXX */
    300 
    301 	case WSDISPLAYIO_GVIDEO:
    302 		*(u_int *)data = dc->dc_blanked ?
    303 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    304 		return (0);
    305 
    306 	case WSDISPLAYIO_GCURPOS:
    307 	case WSDISPLAYIO_SCURPOS:
    308 	case WSDISPLAYIO_GCURMAX:
    309 	case WSDISPLAYIO_GCURSOR:
    310 	case WSDISPLAYIO_SCURSOR:
    311 		return (ENOTTY);	/* XXX */
    312 	}
    313 
    314 	return (ENOTTY);
    315 }
    316 
    317 paddr_t
    318 pvrmmap(void *v, off_t offset, int prot)
    319 {
    320 
    321 	/*
    322 	 * XXX This should be easy to support -- just need to define
    323 	 * XXX offsets for the contol regs, etc.
    324 	 */
    325 	return (-1);
    326 }
    327 
    328 int
    329 pvr_alloc_screen(void *v, const struct wsscreen_descr *type,
    330     void **cookiep, int *curxp, int *curyp, long *attrp)
    331 {
    332 	struct pvr_softc *sc = v;
    333 	long defattr;
    334 
    335 	if (sc->nscreens > 0)
    336 		return (ENOMEM);
    337 
    338 	*cookiep = &sc->sc_dc->rinfo; /* one and only for now */
    339 	*curxp = 0;
    340 	*curyp = 0;
    341 	(*sc->sc_dc->rinfo.ri_ops.alloc_attr)(&sc->sc_dc->rinfo, 0, 0, 0,
    342 	    &defattr);
    343 	*attrp = defattr;
    344 	sc->nscreens++;
    345 	return (0);
    346 }
    347 
    348 void
    349 pvr_free_screen(void *v, void *cookie)
    350 {
    351 	struct pvr_softc *sc = v;
    352 
    353 	if (sc->sc_dc == &pvr_console_dc)
    354 		panic("pvr_free_screen: console");
    355 
    356 	sc->nscreens--;
    357 }
    358 
    359 int
    360 pvr_show_screen(void *v, void *cookie, int waitok,
    361     void (*cb)(void *, int, int), void *cbarg)
    362 {
    363 
    364 	return (0);
    365 }
    366 
    367 static void
    368 pvr_check_cable(struct fb_devconfig *dc)
    369 {
    370 	__volatile u_int32_t *porta =
    371 	    (__volatile u_int32_t *)0xff80002c;
    372 	u_int16_t v;
    373 
    374 	/* PORT8 and PORT9 is input */
    375 	*porta = (*porta & ~0xf0000) | 0xa0000;
    376 
    377 	/* Read PORT8 and PORT9 */
    378 	v = ((*(__volatile u_int16_t *)(porta + 1)) >> 8) & 3;
    379 
    380 	if ((v & 2) == 0)
    381 		dc->dc_dispflags |= PVR_VGAMODE|PVR_RGBMODE;
    382 	else if ((v & 1) == 0)
    383 		dc->dc_dispflags |= PVR_RGBMODE;
    384 }
    385 
    386 void
    387 pvrinit(struct fb_devconfig *dc)
    388 {
    389 	__volatile u_int32_t *pvr = (__volatile u_int32_t *)
    390 	    SH3_PHYS_TO_P2SEG(0x005f8000);
    391 	int display_lines_per_field = 240;
    392 	int modulo = 1, voffset;
    393 
    394 	pvr_check_cable(dc);
    395 
    396 	pvr[8/4] = 0;		/* reset */
    397 	pvr[0x40/4] = 0;	/* black border */
    398 
    399 	if (dc->dc_dispflags & PVR_VGAMODE) {
    400 		pvr[0x44/4] = 0x800004;	/* 31kHz, RGB565 */
    401 		pvr[0xd0/4] = 0x100;	/* video output */
    402 		display_lines_per_field = 480;
    403 		voffset = 36;
    404 	} else {
    405 		pvr[0x44/4] = 0x000004;	/* 15kHz, RGB565 */
    406 		pvr[0xd0/4] = 0x110;	/* video output, NTSC, interlace */
    407 		modulo += 640 * 2 / 4;	/* interlace -> skip every other line */
    408 		voffset = 18;
    409 	}
    410 
    411 	pvr[0x50/4] = 0;	/* video base address, long field */
    412 	pvr[0x54/4] = 640 * 2;	/* video base address, short field */
    413 
    414 	pvr[0x5c/4] = (modulo << 20) | ((display_lines_per_field - 1) << 10) |
    415 	    (640 * 2 / 4 - 1);
    416 
    417 	voffset = (voffset << 16) | voffset;
    418 
    419 	pvr[0xf0/4] = voffset;				/* V start */
    420 	pvr[0xdc/4] = voffset + display_lines_per_field;/* V border */
    421 	pvr[0xec/4] = 164;				/* H start */
    422 	pvr[0xd8/4] = (524 << 16) | 857;		/* HV counter */
    423 	pvr[0xd4/4] = (126 << 16) | 837;		/* H border */
    424 	pvr[0xe8/4] = 22 << 16;
    425 
    426 	/* RGB / composite */
    427 	*(__volatile u_int32_t *)
    428 	    SH3_PHYS_TO_P2SEG(0x00702c00) =
    429 	    ((dc->dc_dispflags & PVR_RGBMODE) ? 0 : 3) << 8;
    430 
    431 	pvr[0x44/4] |= 1;	/* display on */
    432 }
    433 
    434 /* Console support. */
    435 
    436 void	pvrcnprobe(struct consdev *);
    437 void	pvrcninit(struct consdev *);
    438 
    439 void
    440 pvrcninit(struct consdev *cndev)
    441 {
    442 	struct fb_devconfig *dcp = &pvr_console_dc;
    443 	long defattr;
    444 
    445 	pvr_getdevconfig(dcp);
    446 	(*dcp->rinfo.ri_ops.alloc_attr)(&dcp->rinfo, 0, 0, 0, &defattr);
    447 	wsdisplay_cnattach(&pvr_stdscreen, &dcp->rinfo, 0, 0, defattr);
    448 
    449 	pvr_is_console = 1;
    450 
    451 	cn_tab->cn_pri = CN_INTERNAL;
    452 
    453 #if NMKBD > 0
    454 	mkbd_cnattach();	/* connect keyboard and screen together */
    455 #endif
    456 }
    457 
    458 void
    459 pvrcnprobe(struct consdev *cndev)
    460 {
    461 #if NWSDISPLAY > 0
    462 	int maj, unit;
    463 #endif
    464 	cndev->cn_dev = NODEV;
    465 	cndev->cn_pri = CN_NORMAL;
    466 
    467 #if NWSDISPLAY > 0
    468 	unit = 0;
    469 	for (maj = 0; maj < nchrdev; maj++) {
    470 		if (cdevsw[maj].d_open == wsdisplayopen)
    471 			break;
    472 	}
    473 	if (maj != nchrdev) {
    474 		cndev->cn_pri = CN_INTERNAL;
    475 		cndev->cn_dev = makedev(maj, unit);
    476 	}
    477 #endif
    478 }
    479