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