No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / dreamcast / dev / pvr.c
blobdbf848ee4b4276feb1cb08dfe0b52ebd3eefe47a
1 /* $NetBSD: pvr.c,v 1.24 2007/03/04 05:59:43 christos Exp $ */
3 /*-
4 * Copyright (c) 2001 Marcus Comstedt.
5 * Copyright (c) 2001 Jason R. Thorpe.
6 * All rights reserved.
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.
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.
36 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
38 __KERNEL_RCSID(0, "$NetBSD: pvr.c,v 1.24 2007/03/04 05:59:43 christos Exp $");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
45 #include <sys/buf.h>
46 #include <sys/ioctl.h>
48 #include <machine/vmparam.h>
49 #include <machine/cpu.h>
50 #include <machine/bus.h>
52 #include <dev/cons.h>
54 #include <dev/wscons/wsconsio.h>
55 #include <dev/wscons/wsdisplayvar.h>
57 #include <dev/wscons/wscons_callbacks.h>
59 #include <dev/rasops/rasops.h>
60 #include <dev/wsfont/wsfont.h>
62 #include <dreamcast/dev/pvrvar.h>
63 #include <dreamcast/dev/maple/mkbdvar.h>
65 #include "mkbd.h"
67 #define PVRREG_FBSTART 0x05000000
68 #define PVRREG_REGSTART 0x005f8000
70 #define PVRREG_BRDCOLR 0x40
71 #define BRDCOLR_BLUE(x) ((x) << 0)
72 #define BRDCOLR_GREEN(x) ((x) << 8)
73 #define BRDCOLR_RED(x) ((x) << 16)
75 #define PVRREG_DIWMODE 0x44
76 #define DIWMODE_DE (1U << 0) /* display enable */
77 #define DIWMODE_SD (1U << 1) /* scan double enable */
78 #define DIWMODE_COL(x) ((x) << 2)
79 #define DIWMODE_COL_RGB555 DIWMODE_COL(0) /* RGB555, 16-bit */
80 #define DIWMODE_COL_RGB565 DIWMODE_COL(1) /* RGB565, 16-bit */
81 #define DIWMODE_COL_RGB888 DIWMODE_COL(2) /* RGB888, 24-bit */
82 #define DIWMODE_COL_ARGB888 DIWMODE_COL(3) /* RGB888, 32-bit */
83 #define DIWMODE_C (1U << 23) /* 2x clock enable (VGA) */
85 #define PVRREG_DIWADDRL 0x50
87 #define PVRREG_DIWADDRS 0x54
89 #define PVRREG_DIWSIZE 0x5c
90 #define DIWSIZE_DPL(x) ((x) << 0) /* pixel data per line */
91 #define DIWSIZE_LPF(x) ((x) << 10) /* lines per field */
92 #define DIWSIZE_MODULO(x) ((x) << 20) /* words to skip + 1 */
94 #define PVRREG_RASEVTPOS 0xcc
95 #define RASEVTPOS_BOTTOM(x) ((x) << 0)
96 #define RASEVTPOS_TOP(x) ((x) << 16)
98 #define PVRREG_SYNCCONF 0xd0
99 #define SYNCCONF_VP (1U << 0) /* V-sync polarity */
100 #define SYNCCONF_HP (1U << 1) /* H-sync polarity */
101 #define SYNCCONF_I (1U << 4) /* interlace */
102 #define SYNCCONF_BC(x) (1U << 6) /* broadcast standard */
103 #define SYNCCONF_VO (1U << 8) /* video output enable */
105 #define PVRREG_BRDHORZ 0xd4
106 #define BRDHORZ_STOP(x) ((x) << 0)
107 #define BRDHORZ_START(x) ((x) << 16)
109 #define PVRREG_SYNCSIZE 0xd8
110 #define SYNCSIZE_H(x) ((x) << 0)
111 #define SYNCSIZE_V(x) ((x) << 16)
113 #define PVRREG_BRDVERT 0xdc
114 #define BRDVERT_STOP(x) ((x) << 0)
115 #define BRDVERT_START(x) ((x) << 16)
117 #define PVRREG_DIWCONF 0xe8
118 #define DIWCONF_LR (1U << 8) /* low-res */
119 #define DIWCONF_MAGIC (22 << 16)
121 #define PVRREG_DIWHSTRT 0xec
123 #define PVRREG_DIWVSTRT 0xf0
124 #define DIWVSTRT_V1(x) ((x) << 0)
125 #define DIWVSTRT_V2(x) ((x) << 16)
127 #define PVR_REG_READ(dc, reg) \
128 ((volatile uint32_t *)(dc)->dc_regvaddr)[(reg) >> 2]
129 #define PVR_REG_WRITE(dc, reg, val) \
130 ((volatile uint32_t *)(dc)->dc_regvaddr)[(reg) >> 2] = (val)
132 struct fb_devconfig {
133 vaddr_t dc_vaddr; /* framebuffer virtual address */
134 vaddr_t dc_paddr; /* framebuffer physical address */
135 vaddr_t dc_regvaddr; /* registers virtual address */
136 vaddr_t dc_regpaddr; /* registers physical address */
137 int dc_wid; /* width of frame buffer */
138 int dc_ht; /* height of frame buffer */
139 int dc_depth; /* depth, bits per pixel */
140 int dc_rowbytes; /* bytes in a FB scan line */
141 vaddr_t dc_videobase; /* base of flat frame buffer */
142 int dc_blanked; /* currently has video disabled */
143 int dc_dispflags; /* display flags */
144 int dc_tvsystem; /* TV broadcast system */
146 struct rasops_info rinfo;
149 #define PVR_RGBMODE 0x01 /* RGB or composite */
150 #define PVR_VGAMODE 0x02 /* VGA */
152 struct pvr_softc {
153 struct device sc_dev;
154 struct fb_devconfig *sc_dc; /* device configuration */
155 int nscreens;
158 int pvr_match(struct device *, struct cfdata *, void *);
159 void pvr_attach(struct device *, struct device *, void *);
161 CFATTACH_DECL(pvr, sizeof(struct pvr_softc),
162 pvr_match, pvr_attach, NULL, NULL);
164 void pvr_getdevconfig(struct fb_devconfig *);
166 struct fb_devconfig pvr_console_dc;
168 char pvr_stdscreen_textgeom[32] = { "std" }; /* XXX yuck */
170 struct wsscreen_descr pvr_stdscreen = {
171 pvr_stdscreen_textgeom, 0, 0,
172 0, /* textops */
173 0, 0,
174 WSSCREEN_WSCOLORS,
177 const struct wsscreen_descr *_pvr_scrlist[] = {
178 &pvr_stdscreen,
181 const struct wsscreen_list pvr_screenlist = {
182 sizeof(_pvr_scrlist) / sizeof(struct wsscreen_descr *), _pvr_scrlist
185 int pvrioctl(void *, void *, u_long, void *, int, struct lwp *);
186 paddr_t pvrmmap(void *, void *, off_t, int);
188 int pvr_alloc_screen(void *, const struct wsscreen_descr *,
189 void **, int *, int *, long *);
190 void pvr_free_screen(void *, void *);
191 int pvr_show_screen(void *, void *, int,
192 void (*)(void *, int, int), void *);
194 const struct wsdisplay_accessops pvr_accessops = {
195 pvrioctl,
196 pvrmmap,
197 pvr_alloc_screen,
198 pvr_free_screen,
199 pvr_show_screen,
200 NULL, /* load_font */
203 void pvrinit(struct fb_devconfig *);
205 int pvr_is_console;
208 pvr_match(struct device *parent, struct cfdata *match, void *aux)
211 return 1;
214 void
215 pvr_getdevconfig(struct fb_devconfig *dc)
217 int i, cookie;
219 dc->dc_paddr = PVRREG_FBSTART;
220 dc->dc_vaddr = SH3_PHYS_TO_P2SEG(dc->dc_paddr);
222 dc->dc_regpaddr = PVRREG_REGSTART;
223 dc->dc_regvaddr = SH3_PHYS_TO_P2SEG(dc->dc_regpaddr);
225 dc->dc_wid = 640;
226 dc->dc_ht = 480;
227 dc->dc_depth = 16;
228 dc->dc_rowbytes = dc->dc_wid * (dc->dc_depth / 8);
229 dc->dc_videobase = dc->dc_vaddr;
230 dc->dc_blanked = 0;
231 dc->dc_dispflags = 0;
233 /* Clear the screen. */
234 for (i = 0; i < dc->dc_ht * dc->dc_rowbytes; i += sizeof(uint32_t))
235 *(uint32_t *)(dc->dc_videobase + i) = 0x0;
237 /* Initialize the device. */
238 pvrinit(dc);
240 dc->rinfo.ri_flg = 0;
241 dc->rinfo.ri_depth = dc->dc_depth;
242 dc->rinfo.ri_bits = (void *) dc->dc_videobase;
243 dc->rinfo.ri_width = dc->dc_wid;
244 dc->rinfo.ri_height = dc->dc_ht;
245 dc->rinfo.ri_stride = dc->dc_rowbytes;
247 wsfont_init();
248 /* prefer 8 pixel wide font */
249 cookie = wsfont_find(NULL, 8, 0, 0, WSDISPLAY_FONTORDER_L2R,
250 WSDISPLAY_FONTORDER_L2R);
251 if (cookie <= 0)
252 cookie = wsfont_find(NULL, 0, 0, 0, WSDISPLAY_FONTORDER_L2R,
253 WSDISPLAY_FONTORDER_L2R);
254 if (cookie <= 0) {
255 printf("pvr: font table is empty\n");
256 return;
259 if (wsfont_lock(cookie, &dc->rinfo.ri_font)) {
260 printf("pvr: unable to lock font\n");
261 return;
263 dc->rinfo.ri_wsfcookie = cookie;
265 rasops_init(&dc->rinfo, 500, 500);
267 /* XXX shouldn't be global */
268 pvr_stdscreen.nrows = dc->rinfo.ri_rows;
269 pvr_stdscreen.ncols = dc->rinfo.ri_cols;
270 pvr_stdscreen.textops = &dc->rinfo.ri_ops;
271 pvr_stdscreen.capabilities = dc->rinfo.ri_caps;
273 /* XXX yuck */
274 sprintf(pvr_stdscreen_textgeom, "%dx%d", pvr_stdscreen.ncols,
275 pvr_stdscreen.nrows);
278 void
279 pvr_attach(struct device *parent, struct device *self, void *aux)
281 struct pvr_softc *sc = (void *) self;
282 struct wsemuldisplaydev_attach_args waa;
283 int console;
284 static const char *tvsystem_name[4] =
285 { "NTSC", "PAL", "PAL-M", "PAL-N" };
287 console = pvr_is_console;
288 if (console) {
289 sc->sc_dc = &pvr_console_dc;
290 sc->nscreens = 1;
291 } else {
292 sc->sc_dc = malloc(sizeof(struct fb_devconfig), M_DEVBUF,
293 M_WAITOK);
294 pvr_getdevconfig(sc->sc_dc);
296 printf(": %d x %d, %dbpp, %s, %s\n", sc->sc_dc->dc_wid,
297 sc->sc_dc->dc_ht, sc->sc_dc->dc_depth,
298 (sc->sc_dc->dc_dispflags & PVR_VGAMODE) ? "VGA" :
299 tvsystem_name[sc->sc_dc->dc_tvsystem],
300 (sc->sc_dc->dc_dispflags & PVR_RGBMODE) ? "RGB" : "composite");
302 /* XXX Colormap initialization? */
304 waa.console = console;
305 waa.scrdata = &pvr_screenlist;
306 waa.accessops = &pvr_accessops;
307 waa.accesscookie = sc;
309 (void) config_found(self, &waa, wsemuldisplaydevprint);
313 pvrioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
315 struct pvr_softc *sc = v;
316 struct fb_devconfig *dc = sc->sc_dc;
318 switch (cmd) {
319 case WSDISPLAYIO_GTYPE:
320 *(u_int *)data = WSDISPLAY_TYPE_DCPVR;
321 return 0;
323 case WSDISPLAYIO_GINFO:
324 #define wsd_fbip ((struct wsdisplay_fbinfo *)data)
325 wsd_fbip->height = sc->sc_dc->dc_ht;
326 wsd_fbip->width = sc->sc_dc->dc_wid;
327 wsd_fbip->depth = sc->sc_dc->dc_depth;
328 wsd_fbip->cmsize = 0; /* XXX Colormap */
329 #undef wsd_fbip
330 return 0;
332 case WSDISPLAYIO_GETCMAP:
333 case WSDISPLAYIO_PUTCMAP:
334 return EPASSTHROUGH; /* XXX Colormap */
336 case WSDISPLAYIO_SVIDEO:
337 switch (*(u_int *)data) {
338 case WSDISPLAYIO_VIDEO_OFF:
339 if (!dc->dc_blanked) {
340 dc->dc_blanked = 1;
341 PVR_REG_WRITE(dc, PVRREG_DIWMODE,
342 PVR_REG_READ(dc, PVRREG_DIWMODE) &
343 ~DIWMODE_DE);
345 break;
346 case WSDISPLAYIO_VIDEO_ON:
347 if (dc->dc_blanked) {
348 dc->dc_blanked = 0;
349 PVR_REG_WRITE(dc, PVRREG_DIWMODE,
350 PVR_REG_READ(dc, PVRREG_DIWMODE) |
351 DIWMODE_DE);
353 break;
354 default:
355 return EPASSTHROUGH; /* XXX */
357 return 0;
359 case WSDISPLAYIO_GVIDEO:
360 *(u_int *)data = dc->dc_blanked ?
361 WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
362 return 0;
364 case WSDISPLAYIO_GCURPOS:
365 case WSDISPLAYIO_SCURPOS:
366 case WSDISPLAYIO_GCURMAX:
367 case WSDISPLAYIO_GCURSOR:
368 case WSDISPLAYIO_SCURSOR:
369 return EPASSTHROUGH; /* XXX */
372 return EPASSTHROUGH;
375 paddr_t
376 pvrmmap(void *v, void *vs, off_t offset, int prot)
380 * XXX This should be easy to support -- just need to define
381 * XXX offsets for the contol regs, etc.
384 struct pvr_softc *sc = v;
385 struct fb_devconfig *dc = sc->sc_dc;
386 paddr_t addr;
388 if (offset >= 0 &&
389 offset < sh3_round_page(dc->dc_rowbytes * dc->dc_ht))
390 addr = sh3_btop(dc->dc_paddr + offset);
391 else
392 addr = (-1); /* XXX bogus */
394 return addr;
398 pvr_alloc_screen(void *v, const struct wsscreen_descr *type,
399 void **cookiep, int *curxp, int *curyp, long *attrp)
401 struct pvr_softc *sc = v;
402 long defattr;
404 if (sc->nscreens > 0)
405 return ENOMEM;
407 *cookiep = &sc->sc_dc->rinfo; /* one and only for now */
408 *curxp = 0;
409 *curyp = 0;
410 (*sc->sc_dc->rinfo.ri_ops.allocattr)(&sc->sc_dc->rinfo, 0, 0, 0,
411 &defattr);
412 *attrp = defattr;
413 sc->nscreens++;
414 return 0;
417 void
418 pvr_free_screen(void *v, void *cookie)
420 struct pvr_softc *sc = v;
422 if (sc->sc_dc == &pvr_console_dc)
423 panic("pvr_free_screen: console");
425 sc->nscreens--;
429 pvr_show_screen(void *v, void *cookie, int waitok,
430 void (*cb)(void *, int, int), void *cbarg)
433 return 0;
436 static void
437 pvr_check_cable(struct fb_devconfig *dc)
439 volatile uint32_t *porta =
440 (volatile uint32_t *)0xff80002c;
441 uint16_t v;
443 /* PORT8 and PORT9 is input */
444 *porta = (*porta & ~0xf0000) | 0xa0000;
446 /* Read PORT8 and PORT9 */
447 v = ((*(volatile uint16_t *)(porta + 1)) >> 8) & 3;
449 if ((v & 2) == 0)
450 dc->dc_dispflags |= PVR_VGAMODE|PVR_RGBMODE;
451 else if ((v & 1) == 0)
452 dc->dc_dispflags |= PVR_RGBMODE;
455 static void
456 pvr_check_tvsys(struct fb_devconfig *dc)
459 /* XXX should use flashmem device when one exists */
460 dc->dc_tvsystem = (*(volatile uint8_t *)0xa021a004) & 3;
463 void
464 pvrinit(struct fb_devconfig *dc)
466 int display_lines_per_field;
467 int v_absolute_size;
468 int h_absolute_size;
469 int vborder_start, vborder_stop;
470 int hborder_start, hborder_stop;
471 int modulo = 1, voffset, hoffset;
473 pvr_check_cable(dc);
474 pvr_check_tvsys(dc);
476 PVR_REG_WRITE(dc, 8, 0); /* reset */
477 PVR_REG_WRITE(dc, PVRREG_BRDCOLR, 0); /* black border */
479 if (dc->dc_dispflags & PVR_VGAMODE) {
480 v_absolute_size = 524;
481 h_absolute_size = 857;
483 display_lines_per_field = 480;
484 hoffset = 164;
485 voffset = 36;
487 hborder_start = 126;
488 hborder_stop = 837;
490 vborder_start = 40;
491 vborder_stop = 444; /* XXX */
493 /* 31kHz, RGB565 */
494 PVR_REG_WRITE(dc, PVRREG_DIWMODE,
495 DIWMODE_C | DIWMODE_COL_RGB565);
497 /* video output */
498 PVR_REG_WRITE(dc, PVRREG_SYNCCONF, SYNCCONF_VO);
499 } else {
500 if (dc->dc_tvsystem & 1) {
501 /* 50 Hz PAL */
502 v_absolute_size = 624;
503 h_absolute_size = 863;
505 display_lines_per_field = 240;
506 hoffset = 174;
507 voffset = 18;
509 hborder_start = 116;
510 hborder_stop = 843;
512 vborder_start = 44;
513 vborder_stop = 536; /* XXX */
514 } else {
515 /* 60 Hz NTSC */
516 v_absolute_size = 524;
517 h_absolute_size = 857;
519 display_lines_per_field = 240;
520 hoffset = 170;
521 voffset = 28;
523 hborder_start = 126;
524 hborder_stop = 837;
526 vborder_start = 18;
527 vborder_stop = 506; /* XXX */
530 modulo += 640 * 2 / 4; /* interlace -> skip every other line */
532 /* 15kHz, RGB565 */
533 PVR_REG_WRITE(dc, PVRREG_DIWMODE,
534 DIWMODE_COL_RGB565);
536 /* video output, PAL/NTSC, interlace */
537 PVR_REG_WRITE(dc, PVRREG_SYNCCONF,
538 SYNCCONF_VO | SYNCCONF_I | SYNCCONF_BC(dc->dc_tvsystem));
541 /* video base address, long field */
542 PVR_REG_WRITE(dc, PVRREG_DIWADDRL, 0);
544 /* video base address, short field */
545 PVR_REG_WRITE(dc, PVRREG_DIWADDRS, 640 * 2);
547 /* video size */
548 PVR_REG_WRITE(dc, PVRREG_DIWSIZE, DIWSIZE_MODULO(modulo) |
549 DIWSIZE_LPF(display_lines_per_field - 1) |
550 DIWSIZE_DPL(640 * 2 / 4 - 1));
552 PVR_REG_WRITE(dc, PVRREG_DIWVSTRT, /* V start */
553 DIWVSTRT_V1(voffset) | DIWVSTRT_V2(voffset));
554 PVR_REG_WRITE(dc, PVRREG_BRDVERT, /* V border */
555 BRDVERT_START(vborder_start) | BRDVERT_STOP(vborder_stop));
556 PVR_REG_WRITE(dc, PVRREG_DIWHSTRT, hoffset); /* H start */
557 PVR_REG_WRITE(dc, PVRREG_SYNCSIZE, /* HV counter */
558 SYNCSIZE_V(v_absolute_size) | SYNCSIZE_H(h_absolute_size));
559 PVR_REG_WRITE(dc, PVRREG_BRDHORZ, /* H border */
560 BRDHORZ_START(hborder_start) | BRDHORZ_STOP(hborder_stop));
561 PVR_REG_WRITE(dc, PVRREG_DIWCONF, DIWCONF_MAGIC);
563 /* RGB / composite */
564 *(volatile uint32_t *)
565 SH3_PHYS_TO_P2SEG(0x00702c00) =
566 ((dc->dc_dispflags & PVR_RGBMODE) ? 0 : 3) << 8;
568 /* display on */
569 PVR_REG_WRITE(dc, PVRREG_DIWMODE,
570 PVR_REG_READ(dc, PVRREG_DIWMODE) | DIWMODE_DE);
573 /* Console support. */
575 void pvrcnprobe(struct consdev *);
576 void pvrcninit(struct consdev *);
578 void
579 pvrcninit(struct consdev *cndev)
581 struct fb_devconfig *dcp = &pvr_console_dc;
582 long defattr;
584 pvr_getdevconfig(dcp);
585 (*dcp->rinfo.ri_ops.allocattr)(&dcp->rinfo, 0, 0, 0, &defattr);
586 wsdisplay_cnattach(&pvr_stdscreen, &dcp->rinfo, 0, 0, defattr);
588 pvr_is_console = 1;
590 cn_tab->cn_pri = CN_INTERNAL;
592 #if NMKBD > 0
593 mkbd_cnattach(); /* connect keyboard and screen together */
594 #endif
597 void
598 pvrcnprobe(struct consdev *cndev)
600 #if NWSDISPLAY > 0
601 int maj, unit;
602 extern const struct cdevsw wsdisplay_cdevsw;
603 #endif
604 cndev->cn_dev = NODEV;
605 cndev->cn_pri = CN_NORMAL;
607 #if NWSDISPLAY > 0
608 unit = 0;
609 maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
610 if (maj != -1) {
611 cndev->cn_pri = CN_INTERNAL;
612 cndev->cn_dev = makedev(maj, unit);
614 #endif