Home | History | Annotate | Line # | Download | only in dev
pvr.c revision 1.9
      1 /*	$NetBSD: pvr.c,v 1.9 2002/03/13 15:05:19 ad 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.9 2002/03/13 15:05:19 ad 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 	int	dc_tvsystem;		/* TV broadcast system */
    109 
    110 	struct rasops_info rinfo;
    111 };
    112 
    113 #define	PVR_RGBMODE	0x01		/* RGB or composite */
    114 #define	PVR_VGAMODE	0x02		/* VGA */
    115 
    116 struct pvr_softc {
    117 	struct device sc_dev;
    118 	struct fb_devconfig *sc_dc;	/* device configuration */
    119 	int nscreens;
    120 };
    121 
    122 int	pvr_match(struct device *, struct cfdata *, void *);
    123 void	pvr_attach(struct device *, struct device *, void *);
    124 
    125 struct cfattach pvr_ca = {
    126 	sizeof(struct pvr_softc), pvr_match, pvr_attach,
    127 };
    128 
    129 void	pvr_getdevconfig(struct fb_devconfig *);
    130 
    131 struct fb_devconfig pvr_console_dc;
    132 
    133 char pvr_stdscreen_textgeom[32] = { "std" };	/* XXX yuck */
    134 
    135 struct wsscreen_descr pvr_stdscreen = {
    136 	pvr_stdscreen_textgeom, 0, 0,
    137 	0, /* textops */
    138 	0, 0,
    139 	WSSCREEN_WSCOLORS,
    140 };
    141 
    142 const struct wsscreen_descr *_pvr_scrlist[] = {
    143 	&pvr_stdscreen,
    144 };
    145 
    146 const struct wsscreen_list pvr_screenlist = {
    147 	sizeof(_pvr_scrlist) / sizeof(struct wsscreen_descr *), _pvr_scrlist
    148 };
    149 
    150 int	pvrioctl(void *, u_long, caddr_t, int, struct proc *);
    151 paddr_t	pvrmmap(void *, off_t, int);
    152 
    153 int	pvr_alloc_screen(void *, const struct wsscreen_descr *,
    154 	    void **, int *, int *, long *);
    155 void	pvr_free_screen(void *, void *);
    156 int	pvr_show_screen(void *, void *, int,
    157 	    void (*)(void *, int, int), void *);
    158 
    159 const struct wsdisplay_accessops pvr_accessops = {
    160 	pvrioctl,
    161 	pvrmmap,
    162 	pvr_alloc_screen,
    163 	pvr_free_screen,
    164 	pvr_show_screen,
    165 	NULL, /* load_font */
    166 };
    167 
    168 void	pvrinit(struct fb_devconfig *);
    169 
    170 int	pvr_is_console;
    171 
    172 int
    173 pvr_match(struct device *parent, struct cfdata *match, void *aux)
    174 {
    175 	struct shb_attach_args *sa = aux;
    176 
    177 	if (strcmp("pvr", match->cf_driver->cd_name) != 0)
    178 		return (0);
    179 
    180 	sa->ia_iosize = 0; /* 0x1400 */;
    181 	return (1);
    182 }
    183 
    184 void
    185 pvr_getdevconfig(struct fb_devconfig *dc)
    186 {
    187 	int i, cookie;
    188 
    189 	dc->dc_paddr = 0x05000000;
    190 	dc->dc_vaddr = SH3_PHYS_TO_P2SEG(dc->dc_paddr);
    191 
    192 	dc->dc_wid = 640;
    193 	dc->dc_ht = 480;
    194 	dc->dc_depth = 16;
    195 	dc->dc_rowbytes = dc->dc_wid * (dc->dc_depth / 8);
    196 	dc->dc_videobase = dc->dc_vaddr;
    197 	dc->dc_blanked = 0;
    198 	dc->dc_dispflags = 0;
    199 
    200 	/* Clear the screen. */
    201 	for (i = 0; i < dc->dc_ht * dc->dc_rowbytes; i += sizeof(u_int32_t))
    202 		*(u_int32_t *)(dc->dc_videobase + i) = 0x0;
    203 
    204 	/* Initialize the device. */
    205 	pvrinit(dc);
    206 
    207 	dc->rinfo.ri_flg = 0;
    208 	dc->rinfo.ri_depth = dc->dc_depth;
    209 	dc->rinfo.ri_bits = (void *) dc->dc_videobase;
    210 	dc->rinfo.ri_width = dc->dc_wid;
    211 	dc->rinfo.ri_height = dc->dc_ht;
    212 	dc->rinfo.ri_stride = dc->dc_rowbytes;
    213 
    214 	wsfont_init();
    215 	/* prefer 8 pixel wide font */
    216 	cookie = wsfont_find(NULL, 8, 0, 0, WSDISPLAY_FONTORDER_L2R,
    217 	    WSDISPLAY_FONTORDER_L2R);
    218 	if (cookie <= 0)
    219 		cookie = wsfont_find(NULL, 0, 0, 0, WSDISPLAY_FONTORDER_L2R,
    220 		    WSDISPLAY_FONTORDER_L2R);
    221 	if (cookie <= 0) {
    222 		printf("pvr: font table is empty\n");
    223 		return;
    224 	}
    225 
    226 	if (wsfont_lock(cookie, &dc->rinfo.ri_font)) {
    227 		printf("pvr: unable to lock font\n");
    228 		return;
    229 	}
    230 	dc->rinfo.ri_wsfcookie = cookie;
    231 
    232 	rasops_init(&dc->rinfo, 500, 500);
    233 
    234 	/* XXX shouldn't be global */
    235 	pvr_stdscreen.nrows = dc->rinfo.ri_rows;
    236 	pvr_stdscreen.ncols = dc->rinfo.ri_cols;
    237 	pvr_stdscreen.textops = &dc->rinfo.ri_ops;
    238 	pvr_stdscreen.capabilities = dc->rinfo.ri_caps;
    239 
    240 	/* XXX yuck */
    241 	sprintf(pvr_stdscreen_textgeom, "%dx%d", pvr_stdscreen.ncols,
    242 	    pvr_stdscreen.nrows);
    243 }
    244 
    245 void
    246 pvr_attach(struct device *parent, struct device *self, void *aux)
    247 {
    248 	struct pvr_softc *sc = (void *) self;
    249 	struct wsemuldisplaydev_attach_args waa;
    250 	int console;
    251 	static const char *tvsystem_name[4] =
    252 		{ "NTSC", "PAL", "PAL-M", "PAL-N" };
    253 
    254 	console = pvr_is_console;
    255 	if (console) {
    256 		sc->sc_dc = &pvr_console_dc;
    257 		sc->nscreens = 1;
    258 	} else {
    259 		sc->sc_dc = malloc(sizeof(struct fb_devconfig), M_DEVBUF,
    260 		    M_WAITOK);
    261 		pvr_getdevconfig(sc->sc_dc);
    262 	}
    263 	printf(": %d x %d, %dbpp, %s, %s\n", sc->sc_dc->dc_wid,
    264 	    sc->sc_dc->dc_ht, sc->sc_dc->dc_depth,
    265 	    (sc->sc_dc->dc_dispflags & PVR_VGAMODE) ? "VGA" :
    266 	       tvsystem_name[sc->sc_dc->dc_tvsystem],
    267 	    (sc->sc_dc->dc_dispflags & PVR_RGBMODE) ? "RGB" : "composite");
    268 
    269 	/* XXX Colormap initialization? */
    270 
    271 	waa.console = console;
    272 	waa.scrdata = &pvr_screenlist;
    273 	waa.accessops = &pvr_accessops;
    274 	waa.accesscookie = sc;
    275 
    276 	(void) config_found(self, &waa, wsemuldisplaydevprint);
    277 }
    278 
    279 int
    280 pvrioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    281 {
    282 	struct pvr_softc *sc = v;
    283 	struct fb_devconfig *dc = sc->sc_dc;
    284 
    285 	switch (cmd) {
    286 	case WSDISPLAYIO_GTYPE:
    287 		*(u_int *)data = WSDISPLAY_TYPE_DCPVR;
    288 		return (0);
    289 
    290 	case WSDISPLAYIO_GINFO:
    291 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    292 		wsd_fbip->height = sc->sc_dc->dc_ht;
    293 		wsd_fbip->width = sc->sc_dc->dc_wid;
    294 		wsd_fbip->depth = sc->sc_dc->dc_depth;
    295 		wsd_fbip->cmsize = 0;	/* XXX Colormap */
    296 #undef wsd_fbip
    297 		return (0);
    298 
    299 	case WSDISPLAYIO_GETCMAP:
    300 	case WSDISPLAYIO_PUTCMAP:
    301 		return (ENOTTY);	/* XXX Colormap */
    302 
    303 	case WSDISPLAYIO_SVIDEO:
    304 		return (ENOTTY);	/* XXX */
    305 
    306 	case WSDISPLAYIO_GVIDEO:
    307 		*(u_int *)data = dc->dc_blanked ?
    308 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    309 		return (0);
    310 
    311 	case WSDISPLAYIO_GCURPOS:
    312 	case WSDISPLAYIO_SCURPOS:
    313 	case WSDISPLAYIO_GCURMAX:
    314 	case WSDISPLAYIO_GCURSOR:
    315 	case WSDISPLAYIO_SCURSOR:
    316 		return (ENOTTY);	/* XXX */
    317 	}
    318 
    319 	return (ENOTTY);
    320 }
    321 
    322 paddr_t
    323 pvrmmap(void *v, off_t offset, int prot)
    324 {
    325 
    326 	/*
    327 	 * XXX This should be easy to support -- just need to define
    328 	 * XXX offsets for the contol regs, etc.
    329 	 */
    330 
    331 	struct pvr_softc *sc = v;
    332 	struct fb_devconfig *dc = sc->sc_dc;
    333 	paddr_t addr;
    334 
    335 	if (offset >= 0 &&
    336 	    offset < sh3_round_page(dc->dc_rowbytes * dc->dc_ht))
    337 		addr = sh3_btop(dc->dc_paddr + offset);
    338 	else
    339 		addr = (-1);	/* XXX bogus */
    340 
    341 	return addr;
    342 }
    343 
    344 int
    345 pvr_alloc_screen(void *v, const struct wsscreen_descr *type,
    346     void **cookiep, int *curxp, int *curyp, long *attrp)
    347 {
    348 	struct pvr_softc *sc = v;
    349 	long defattr;
    350 
    351 	if (sc->nscreens > 0)
    352 		return (ENOMEM);
    353 
    354 	*cookiep = &sc->sc_dc->rinfo; /* one and only for now */
    355 	*curxp = 0;
    356 	*curyp = 0;
    357 	(*sc->sc_dc->rinfo.ri_ops.alloc_attr)(&sc->sc_dc->rinfo, 0, 0, 0,
    358 	    &defattr);
    359 	*attrp = defattr;
    360 	sc->nscreens++;
    361 	return (0);
    362 }
    363 
    364 void
    365 pvr_free_screen(void *v, void *cookie)
    366 {
    367 	struct pvr_softc *sc = v;
    368 
    369 	if (sc->sc_dc == &pvr_console_dc)
    370 		panic("pvr_free_screen: console");
    371 
    372 	sc->nscreens--;
    373 }
    374 
    375 int
    376 pvr_show_screen(void *v, void *cookie, int waitok,
    377     void (*cb)(void *, int, int), void *cbarg)
    378 {
    379 
    380 	return (0);
    381 }
    382 
    383 static void
    384 pvr_check_cable(struct fb_devconfig *dc)
    385 {
    386 	__volatile u_int32_t *porta =
    387 	    (__volatile u_int32_t *)0xff80002c;
    388 	u_int16_t v;
    389 
    390 	/* PORT8 and PORT9 is input */
    391 	*porta = (*porta & ~0xf0000) | 0xa0000;
    392 
    393 	/* Read PORT8 and PORT9 */
    394 	v = ((*(__volatile u_int16_t *)(porta + 1)) >> 8) & 3;
    395 
    396 	if ((v & 2) == 0)
    397 		dc->dc_dispflags |= PVR_VGAMODE|PVR_RGBMODE;
    398 	else if ((v & 1) == 0)
    399 		dc->dc_dispflags |= PVR_RGBMODE;
    400 }
    401 
    402 static void
    403 pvr_check_tvsys(struct fb_devconfig *dc)
    404 {
    405 	/* XXX should use flashmem device when one exists */
    406 	dc->dc_tvsystem = (*(__volatile u_int8_t *)0xa021a004) & 3;
    407 }
    408 
    409 void
    410 pvrinit(struct fb_devconfig *dc)
    411 {
    412 	__volatile u_int32_t *pvr = (__volatile u_int32_t *)
    413 	    SH3_PHYS_TO_P2SEG(0x005f8000);
    414 	int display_lines_per_field = 240;
    415 	int v_absolute_size = 525;
    416 	int h_absolute_size = 857;
    417 	int modulo = 1, voffset, hoffset = 164, border = (126 << 16) | 837;
    418 
    419 	pvr_check_cable(dc);
    420 	pvr_check_tvsys(dc);
    421 
    422 	pvr[8/4] = 0;		/* reset */
    423 	pvr[0x40/4] = 0;	/* black border */
    424 
    425 	if (dc->dc_dispflags & PVR_VGAMODE) {
    426 		pvr[0x44/4] = 0x800004;	/* 31kHz, RGB565 */
    427 		pvr[0xd0/4] = 0x100;	/* video output */
    428 		display_lines_per_field = 480;
    429 		voffset = 36;
    430 	} else {
    431 		if(dc->dc_tvsystem & 1) {
    432 			/* 50 Hz */
    433 			v_absolute_size = 625;
    434 			h_absolute_size = 863;
    435 			hoffset = 174;
    436 			border = (116 << 16) | 843;
    437 		}
    438 		pvr[0x44/4] = 0x000004;	/* 15kHz, RGB565 */
    439 		/* video output, PAL/NTSC, interlace */
    440 		pvr[0xd0/4] = 0x110|(dc->dc_tvsystem<<6);
    441 		modulo += 640 * 2 / 4;	/* interlace -> skip every other line */
    442 		voffset = 18;
    443 	}
    444 
    445 	pvr[0x50/4] = 0;	/* video base address, long field */
    446 	pvr[0x54/4] = 640 * 2;	/* video base address, short field */
    447 
    448 	pvr[0x5c/4] = (modulo << 20) | ((display_lines_per_field - 1) << 10) |
    449 	    (640 * 2 / 4 - 1);
    450 
    451 	voffset = (voffset << 16) | voffset;
    452 
    453 	pvr[0xf0/4] = voffset;				/* V start */
    454 	pvr[0xdc/4] = voffset + display_lines_per_field;/* V border */
    455 	pvr[0xec/4] = hoffset;				/* H start */
    456 	pvr[0xd8/4] = (v_absolute_size<<16) | h_absolute_size; /* HV counter */
    457 	pvr[0xd4/4] = border;				/* H border */
    458 	pvr[0xe8/4] = 22 << 16;
    459 
    460 	/* RGB / composite */
    461 	*(__volatile u_int32_t *)
    462 	    SH3_PHYS_TO_P2SEG(0x00702c00) =
    463 	    ((dc->dc_dispflags & PVR_RGBMODE) ? 0 : 3) << 8;
    464 
    465 	pvr[0x44/4] |= 1;	/* display on */
    466 }
    467 
    468 /* Console support. */
    469 
    470 void	pvrcnprobe(struct consdev *);
    471 void	pvrcninit(struct consdev *);
    472 
    473 void
    474 pvrcninit(struct consdev *cndev)
    475 {
    476 	struct fb_devconfig *dcp = &pvr_console_dc;
    477 	long defattr;
    478 
    479 	pvr_getdevconfig(dcp);
    480 	(*dcp->rinfo.ri_ops.alloc_attr)(&dcp->rinfo, 0, 0, 0, &defattr);
    481 	wsdisplay_cnattach(&pvr_stdscreen, &dcp->rinfo, 0, 0, defattr);
    482 
    483 	pvr_is_console = 1;
    484 
    485 	cn_tab->cn_pri = CN_INTERNAL;
    486 
    487 #if NMKBD > 0
    488 	mkbd_cnattach();	/* connect keyboard and screen together */
    489 #endif
    490 }
    491 
    492 void
    493 pvrcnprobe(struct consdev *cndev)
    494 {
    495 #if NWSDISPLAY > 0
    496 	int maj, unit;
    497 #endif
    498 	cndev->cn_dev = NODEV;
    499 	cndev->cn_pri = CN_NORMAL;
    500 
    501 #if NWSDISPLAY > 0
    502 	unit = 0;
    503 	for (maj = 0; maj < nchrdev; maj++) {
    504 		if (cdevsw[maj].d_open == wsdisplayopen)
    505 			break;
    506 	}
    507 	if (maj != nchrdev) {
    508 		cndev->cn_pri = CN_INTERNAL;
    509 		cndev->cn_dev = makedev(maj, unit);
    510 	}
    511 #endif
    512 }
    513