xref: /netbsd-src/sys/arch/dreamcast/dev/pvr.c (revision 9fbd88883c38d0c0fbfcbe66d76fe6b0fab3f9de)
1 /*	$NetBSD: pvr.c,v 1.8 2001/03/05 02:45:24 marcus 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.8 2001/03/05 02:45:24 marcus 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 	if ((cookie = wsfont_find(NULL, 8, 0, 0)) <= 0)
217 		cookie = wsfont_find(NULL, 0, 0, 0);
218 	if (cookie <= 0) {
219 		printf("pvr: font table is empty\n");
220 		return;
221 	}
222 
223 	if (wsfont_lock(cookie, &dc->rinfo.ri_font,
224 	    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R) <= 0) {
225 		printf("pvr: unable to lock font\n");
226 		return;
227 	}
228 	dc->rinfo.ri_wsfcookie = cookie;
229 
230 	rasops_init(&dc->rinfo, 500, 500);
231 
232 	/* XXX shouldn't be global */
233 	pvr_stdscreen.nrows = dc->rinfo.ri_rows;
234 	pvr_stdscreen.ncols = dc->rinfo.ri_cols;
235 	pvr_stdscreen.textops = &dc->rinfo.ri_ops;
236 	pvr_stdscreen.capabilities = dc->rinfo.ri_caps;
237 
238 	/* XXX yuck */
239 	sprintf(pvr_stdscreen_textgeom, "%dx%d", pvr_stdscreen.ncols,
240 	    pvr_stdscreen.nrows);
241 }
242 
243 void
244 pvr_attach(struct device *parent, struct device *self, void *aux)
245 {
246 	struct pvr_softc *sc = (void *) self;
247 	struct wsemuldisplaydev_attach_args waa;
248 	int console;
249 	static const char *tvsystem_name[4] =
250 		{ "NTSC", "PAL", "PAL-M", "PAL-N" };
251 
252 	console = pvr_is_console;
253 	if (console) {
254 		sc->sc_dc = &pvr_console_dc;
255 		sc->nscreens = 1;
256 	} else {
257 		sc->sc_dc = malloc(sizeof(struct fb_devconfig), M_DEVBUF,
258 		    M_WAITOK);
259 		pvr_getdevconfig(sc->sc_dc);
260 	}
261 	printf(": %d x %d, %dbpp, %s, %s\n", sc->sc_dc->dc_wid,
262 	    sc->sc_dc->dc_ht, sc->sc_dc->dc_depth,
263 	    (sc->sc_dc->dc_dispflags & PVR_VGAMODE) ? "VGA" :
264 	       tvsystem_name[sc->sc_dc->dc_tvsystem],
265 	    (sc->sc_dc->dc_dispflags & PVR_RGBMODE) ? "RGB" : "composite");
266 
267 	/* XXX Colormap initialization? */
268 
269 	waa.console = console;
270 	waa.scrdata = &pvr_screenlist;
271 	waa.accessops = &pvr_accessops;
272 	waa.accesscookie = sc;
273 
274 	(void) config_found(self, &waa, wsemuldisplaydevprint);
275 }
276 
277 int
278 pvrioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
279 {
280 	struct pvr_softc *sc = v;
281 	struct fb_devconfig *dc = sc->sc_dc;
282 
283 	switch (cmd) {
284 	case WSDISPLAYIO_GTYPE:
285 		*(u_int *)data = WSDISPLAY_TYPE_DCPVR;
286 		return (0);
287 
288 	case WSDISPLAYIO_GINFO:
289 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
290 		wsd_fbip->height = sc->sc_dc->dc_ht;
291 		wsd_fbip->width = sc->sc_dc->dc_wid;
292 		wsd_fbip->depth = sc->sc_dc->dc_depth;
293 		wsd_fbip->cmsize = 0;	/* XXX Colormap */
294 #undef wsd_fbip
295 		return (0);
296 
297 	case WSDISPLAYIO_GETCMAP:
298 	case WSDISPLAYIO_PUTCMAP:
299 		return (ENOTTY);	/* XXX Colormap */
300 
301 	case WSDISPLAYIO_SVIDEO:
302 		return (ENOTTY);	/* XXX */
303 
304 	case WSDISPLAYIO_GVIDEO:
305 		*(u_int *)data = dc->dc_blanked ?
306 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
307 		return (0);
308 
309 	case WSDISPLAYIO_GCURPOS:
310 	case WSDISPLAYIO_SCURPOS:
311 	case WSDISPLAYIO_GCURMAX:
312 	case WSDISPLAYIO_GCURSOR:
313 	case WSDISPLAYIO_SCURSOR:
314 		return (ENOTTY);	/* XXX */
315 	}
316 
317 	return (ENOTTY);
318 }
319 
320 paddr_t
321 pvrmmap(void *v, off_t offset, int prot)
322 {
323 
324 	/*
325 	 * XXX This should be easy to support -- just need to define
326 	 * XXX offsets for the contol regs, etc.
327 	 */
328 
329 	struct pvr_softc *sc = v;
330 	struct fb_devconfig *dc = sc->sc_dc;
331 	paddr_t addr;
332 
333 	if (offset >= 0 &&
334 	    offset < sh3_round_page(dc->dc_rowbytes * dc->dc_ht))
335 		addr = sh3_btop(dc->dc_paddr + offset);
336 	else
337 		addr = (-1);	/* XXX bogus */
338 
339 	return addr;
340 }
341 
342 int
343 pvr_alloc_screen(void *v, const struct wsscreen_descr *type,
344     void **cookiep, int *curxp, int *curyp, long *attrp)
345 {
346 	struct pvr_softc *sc = v;
347 	long defattr;
348 
349 	if (sc->nscreens > 0)
350 		return (ENOMEM);
351 
352 	*cookiep = &sc->sc_dc->rinfo; /* one and only for now */
353 	*curxp = 0;
354 	*curyp = 0;
355 	(*sc->sc_dc->rinfo.ri_ops.alloc_attr)(&sc->sc_dc->rinfo, 0, 0, 0,
356 	    &defattr);
357 	*attrp = defattr;
358 	sc->nscreens++;
359 	return (0);
360 }
361 
362 void
363 pvr_free_screen(void *v, void *cookie)
364 {
365 	struct pvr_softc *sc = v;
366 
367 	if (sc->sc_dc == &pvr_console_dc)
368 		panic("pvr_free_screen: console");
369 
370 	sc->nscreens--;
371 }
372 
373 int
374 pvr_show_screen(void *v, void *cookie, int waitok,
375     void (*cb)(void *, int, int), void *cbarg)
376 {
377 
378 	return (0);
379 }
380 
381 static void
382 pvr_check_cable(struct fb_devconfig *dc)
383 {
384 	__volatile u_int32_t *porta =
385 	    (__volatile u_int32_t *)0xff80002c;
386 	u_int16_t v;
387 
388 	/* PORT8 and PORT9 is input */
389 	*porta = (*porta & ~0xf0000) | 0xa0000;
390 
391 	/* Read PORT8 and PORT9 */
392 	v = ((*(__volatile u_int16_t *)(porta + 1)) >> 8) & 3;
393 
394 	if ((v & 2) == 0)
395 		dc->dc_dispflags |= PVR_VGAMODE|PVR_RGBMODE;
396 	else if ((v & 1) == 0)
397 		dc->dc_dispflags |= PVR_RGBMODE;
398 }
399 
400 static void
401 pvr_check_tvsys(struct fb_devconfig *dc)
402 {
403 	/* XXX should use flashmem device when one exists */
404 	dc->dc_tvsystem = (*(__volatile u_int8_t *)0xa021a004) & 3;
405 }
406 
407 void
408 pvrinit(struct fb_devconfig *dc)
409 {
410 	__volatile u_int32_t *pvr = (__volatile u_int32_t *)
411 	    SH3_PHYS_TO_P2SEG(0x005f8000);
412 	int display_lines_per_field = 240;
413 	int v_absolute_size = 525;
414 	int h_absolute_size = 857;
415 	int modulo = 1, voffset, hoffset = 164, border = (126 << 16) | 837;
416 
417 	pvr_check_cable(dc);
418 	pvr_check_tvsys(dc);
419 
420 	pvr[8/4] = 0;		/* reset */
421 	pvr[0x40/4] = 0;	/* black border */
422 
423 	if (dc->dc_dispflags & PVR_VGAMODE) {
424 		pvr[0x44/4] = 0x800004;	/* 31kHz, RGB565 */
425 		pvr[0xd0/4] = 0x100;	/* video output */
426 		display_lines_per_field = 480;
427 		voffset = 36;
428 	} else {
429 		if(dc->dc_tvsystem & 1) {
430 			/* 50 Hz */
431 			v_absolute_size = 625;
432 			h_absolute_size = 863;
433 			hoffset = 174;
434 			border = (116 << 16) | 843;
435 		}
436 		pvr[0x44/4] = 0x000004;	/* 15kHz, RGB565 */
437 		/* video output, PAL/NTSC, interlace */
438 		pvr[0xd0/4] = 0x110|(dc->dc_tvsystem<<6);
439 		modulo += 640 * 2 / 4;	/* interlace -> skip every other line */
440 		voffset = 18;
441 	}
442 
443 	pvr[0x50/4] = 0;	/* video base address, long field */
444 	pvr[0x54/4] = 640 * 2;	/* video base address, short field */
445 
446 	pvr[0x5c/4] = (modulo << 20) | ((display_lines_per_field - 1) << 10) |
447 	    (640 * 2 / 4 - 1);
448 
449 	voffset = (voffset << 16) | voffset;
450 
451 	pvr[0xf0/4] = voffset;				/* V start */
452 	pvr[0xdc/4] = voffset + display_lines_per_field;/* V border */
453 	pvr[0xec/4] = hoffset;				/* H start */
454 	pvr[0xd8/4] = (v_absolute_size<<16) | h_absolute_size; /* HV counter */
455 	pvr[0xd4/4] = border;				/* H border */
456 	pvr[0xe8/4] = 22 << 16;
457 
458 	/* RGB / composite */
459 	*(__volatile u_int32_t *)
460 	    SH3_PHYS_TO_P2SEG(0x00702c00) =
461 	    ((dc->dc_dispflags & PVR_RGBMODE) ? 0 : 3) << 8;
462 
463 	pvr[0x44/4] |= 1;	/* display on */
464 }
465 
466 /* Console support. */
467 
468 void	pvrcnprobe(struct consdev *);
469 void	pvrcninit(struct consdev *);
470 
471 void
472 pvrcninit(struct consdev *cndev)
473 {
474 	struct fb_devconfig *dcp = &pvr_console_dc;
475 	long defattr;
476 
477 	pvr_getdevconfig(dcp);
478 	(*dcp->rinfo.ri_ops.alloc_attr)(&dcp->rinfo, 0, 0, 0, &defattr);
479 	wsdisplay_cnattach(&pvr_stdscreen, &dcp->rinfo, 0, 0, defattr);
480 
481 	pvr_is_console = 1;
482 
483 	cn_tab->cn_pri = CN_INTERNAL;
484 
485 #if NMKBD > 0
486 	mkbd_cnattach();	/* connect keyboard and screen together */
487 #endif
488 }
489 
490 void
491 pvrcnprobe(struct consdev *cndev)
492 {
493 #if NWSDISPLAY > 0
494 	int maj, unit;
495 #endif
496 	cndev->cn_dev = NODEV;
497 	cndev->cn_pri = CN_NORMAL;
498 
499 #if NWSDISPLAY > 0
500 	unit = 0;
501 	for (maj = 0; maj < nchrdev; maj++) {
502 		if (cdevsw[maj].d_open == wsdisplayopen)
503 			break;
504 	}
505 	if (maj != nchrdev) {
506 		cndev->cn_pri = CN_INTERNAL;
507 		cndev->cn_dev = makedev(maj, unit);
508 	}
509 #endif
510 }
511