1 /* $NetBSD: lunafb.c,v 1.6 2000/06/29 08:17:25 mrg Exp $ */ 2 3 /*- 4 * Copyright (c) 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Tohru Nishimura. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 40 41 __KERNEL_RCSID(0, "$NetBSD: lunafb.c,v 1.6 2000/06/29 08:17:25 mrg Exp $"); 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/conf.h> 46 #include <sys/device.h> 47 #include <sys/ioctl.h> 48 #include <sys/malloc.h> 49 #include <sys/mman.h> 50 #include <sys/proc.h> 51 #include <sys/tty.h> 52 #include <sys/errno.h> 53 #include <sys/buf.h> 54 55 #include <uvm/uvm_extern.h> 56 57 #include <dev/rcons/raster.h> 58 #include <dev/wscons/wsconsio.h> 59 #include <dev/wscons/wscons_raster.h> 60 #include <dev/wscons/wsdisplayvar.h> 61 62 #include <machine/cpu.h> 63 #include <machine/autoconf.h> 64 65 struct bt454 { 66 u_int8_t bt_addr; /* map address register */ 67 u_int8_t bt_cmap; /* colormap data register */ 68 }; 69 70 struct bt458 { 71 u_int8_t bt_addr; /* map address register */ 72 unsigned :24; 73 u_int8_t bt_cmap; /* colormap data register */ 74 unsigned :24; 75 u_int8_t bt_ctrl; /* control register */ 76 unsigned :24; 77 u_int8_t bt_omap; /* overlay (cursor) map register */ 78 unsigned :24; 79 }; 80 81 #define OMFB_RFCNT 0xB1000000 /* video h-origin/v-origin */ 82 #define OMFB_PLANEMASK 0xB1040000 /* planemask register */ 83 #define OMFB_FB_WADDR 0xB1080008 /* common plane */ 84 #define OMFB_FB_RADDR 0xB10C0008 /* plane #0 */ 85 #define OMFB_ROPFUNC 0xB12C0000 /* ROP function code */ 86 #define OMFB_RAMDAC 0xC1100000 /* Bt454/Bt458 RAMDAC */ 87 #define OMFB_SIZE (0xB1300000 - 0xB1080000 + NBPG) 88 89 struct om_hwdevconfig { 90 int dc_wid; /* width of frame buffer */ 91 int dc_ht; /* height of frame buffer */ 92 int dc_depth; /* depth, bits per pixel */ 93 int dc_rowbytes; /* bytes in a FB scan line */ 94 int dc_cmsize; /* colormap size */ 95 vaddr_t dc_videobase; /* base of flat frame buffer */ 96 struct raster dc_raster; /* raster description */ 97 struct rcons dc_rcons; /* raster blitter control info */ 98 }; 99 100 struct hwcmap { 101 #define CMAP_SIZE 256 102 u_int8_t r[CMAP_SIZE]; 103 u_int8_t g[CMAP_SIZE]; 104 u_int8_t b[CMAP_SIZE]; 105 }; 106 107 struct omfb_softc { 108 struct device sc_dev; /* base device */ 109 struct om_hwdevconfig *sc_dc; /* device configuration */ 110 struct hwcmap sc_cmap; /* software copy of colormap */ 111 int nscreens; 112 }; 113 114 static int omgetcmap __P((struct omfb_softc *, struct wsdisplay_cmap *)); 115 static int omsetcmap __P((struct omfb_softc *, struct wsdisplay_cmap *)); 116 117 static struct om_hwdevconfig omfb_console_dc; 118 static void omfb_getdevconfig __P((paddr_t, struct om_hwdevconfig *)); 119 120 extern struct wsdisplay_emulops omfb_emulops; 121 122 static struct wsscreen_descr omfb_stdscreen = { 123 "std", 0, 0, 124 &omfb_emulops, 125 0, 0, 126 0 127 }; 128 129 static const struct wsscreen_descr *_omfb_scrlist[] = { 130 &omfb_stdscreen, 131 }; 132 133 static const struct wsscreen_list omfb_screenlist = { 134 sizeof(_omfb_scrlist) / sizeof(struct wsscreen_descr *), _omfb_scrlist 135 }; 136 137 static int omfbioctl __P((void *, u_long, caddr_t, int, struct proc *)); 138 static paddr_t omfbmmap __P((void *, off_t, int)); 139 static int omfb_alloc_screen __P((void *, const struct wsscreen_descr *, 140 void **, int *, int *, long *)); 141 static void omfb_free_screen __P((void *, void *)); 142 static int omfb_show_screen __P((void *, void *, int, 143 void (*) (void *, int, int), void *)); 144 145 static const struct wsdisplay_accessops omfb_accessops = { 146 omfbioctl, 147 omfbmmap, 148 omfb_alloc_screen, 149 omfb_free_screen, 150 omfb_show_screen, 151 0 /* load_font */ 152 }; 153 154 static int omfbmatch __P((struct device *, struct cfdata *, void *)); 155 static void omfbattach __P((struct device *, struct device *, void *)); 156 157 const struct cfattach fb_ca = { 158 sizeof(struct omfb_softc), omfbmatch, omfbattach 159 }; 160 extern struct cfdriver fb_cd; 161 162 extern int hwplanemask; /* hardware planemask; retrieved at boot */ 163 164 static int omfb_console; 165 int omfb_cnattach __P((void)); 166 167 static int 168 omfbmatch(parent, cf, aux) 169 struct device *parent; 170 struct cfdata *cf; 171 void *aux; 172 { 173 struct mainbus_attach_args *ma = aux; 174 175 if (strcmp(ma->ma_name, fb_cd.cd_name)) 176 return (0); 177 #if 0 /* XXX badaddr() bombs if no framebuffer is installed */ 178 if (badaddr((caddr_t)ma->ma_addr, 4)) 179 return (0); 180 #else 181 if (hwplanemask == 0) 182 return (0); 183 #endif 184 return (1); 185 } 186 187 static void 188 omfbattach(parent, self, args) 189 struct device *parent, *self; 190 void *args; 191 { 192 struct omfb_softc *sc = (struct omfb_softc *)self; 193 struct wsemuldisplaydev_attach_args waa; 194 195 if (omfb_console) { 196 sc->sc_dc = &omfb_console_dc; 197 sc->nscreens = 1; 198 } 199 else { 200 sc->sc_dc = (struct om_hwdevconfig *) 201 malloc(sizeof(struct om_hwdevconfig), M_DEVBUF, M_WAITOK); 202 omfb_getdevconfig(OMFB_FB_WADDR, sc->sc_dc); 203 } 204 printf(": %d x %d, %dbpp\n", sc->sc_dc->dc_wid, sc->sc_dc->dc_ht, 205 sc->sc_dc->dc_depth); 206 207 #if 0 /* WHITE on BLACK */ 208 cm = &sc->sc_cmap; 209 memset(cm, 255, sizeof(struct hwcmap)); 210 cm->r[0] = cm->g[0] = cm->b[0] = 0; 211 #endif 212 waa.console = omfb_console; 213 waa.scrdata = &omfb_screenlist; 214 waa.accessops = &omfb_accessops; 215 waa.accesscookie = sc; 216 217 config_found(self, &waa, wsemuldisplaydevprint); 218 } 219 220 /* EXPORT */ int 221 omfb_cnattach() 222 { 223 struct om_hwdevconfig *dc = &omfb_console_dc; 224 long defattr; 225 226 omfb_getdevconfig(OMFB_FB_WADDR, dc); 227 (*omfb_emulops.alloc_attr)(&dc->dc_rcons, 0, 0, 0, &defattr); 228 wsdisplay_cnattach(&omfb_stdscreen, &dc->dc_rcons, 0, 0, defattr); 229 omfb_console = 1; 230 return (0); 231 } 232 233 static int 234 omfbioctl(v, cmd, data, flag, p) 235 void *v; 236 u_long cmd; 237 caddr_t data; 238 int flag; 239 struct proc *p; 240 { 241 struct omfb_softc *sc = v; 242 struct om_hwdevconfig *dc = sc->sc_dc; 243 244 switch (cmd) { 245 case WSDISPLAYIO_GTYPE: 246 *(u_int *)data = 0x19990927; 247 return (0); 248 249 case WSDISPLAYIO_GINFO: 250 #define wsd_fbip ((struct wsdisplay_fbinfo *)data) 251 wsd_fbip->height = dc->dc_ht; 252 wsd_fbip->width = dc->dc_wid; 253 wsd_fbip->depth = dc->dc_depth; 254 wsd_fbip->cmsize = dc->dc_cmsize; 255 #undef fbt 256 return (0); 257 258 case WSDISPLAYIO_GETCMAP: 259 return omgetcmap(sc, (struct wsdisplay_cmap *)data); 260 261 case WSDISPLAYIO_PUTCMAP: 262 return omsetcmap(sc, (struct wsdisplay_cmap *)data); 263 264 case WSDISPLAYIO_SVIDEO: 265 case WSDISPLAYIO_GVIDEO: 266 case WSDISPLAYIO_GCURPOS: 267 case WSDISPLAYIO_SCURPOS: 268 case WSDISPLAYIO_GCURMAX: 269 case WSDISPLAYIO_GCURSOR: 270 case WSDISPLAYIO_SCURSOR: 271 break; 272 } 273 return (ENOTTY); 274 } 275 276 /* 277 * Return the address that would map the given device at the given 278 * offset, allowing for the given protection, or return -1 for error. 279 */ 280 static paddr_t 281 omfbmmap(v, offset, prot) 282 void *v; 283 off_t offset; 284 int prot; 285 { 286 struct omfb_softc *sc = v; 287 288 if (offset >= OMFB_SIZE || offset < 0) 289 return (-1); 290 return m68k_btop(m68k_trunc_page(sc->sc_dc->dc_videobase) + offset); 291 } 292 293 static int 294 omgetcmap(sc, p) 295 struct omfb_softc *sc; 296 struct wsdisplay_cmap *p; 297 { 298 u_int index = p->index, count = p->count; 299 int cmsize; 300 301 cmsize = sc->sc_dc->dc_cmsize; 302 if (index >= cmsize || (index + count) > cmsize) 303 return (EINVAL); 304 305 if (!uvm_useracc(p->red, count, B_WRITE) || 306 !uvm_useracc(p->green, count, B_WRITE) || 307 !uvm_useracc(p->blue, count, B_WRITE)) 308 return (EFAULT); 309 310 copyout(&sc->sc_cmap.r[index], p->red, count); 311 copyout(&sc->sc_cmap.g[index], p->green, count); 312 copyout(&sc->sc_cmap.b[index], p->blue, count); 313 314 return (0); 315 } 316 317 static int 318 omsetcmap(sc, p) 319 struct omfb_softc *sc; 320 struct wsdisplay_cmap *p; 321 { 322 u_int index = p->index, count = p->count; 323 int cmsize, i; 324 325 cmsize = sc->sc_dc->dc_cmsize; 326 if (index >= cmsize || (index + count) > cmsize) 327 return (EINVAL); 328 329 if (!uvm_useracc(p->red, count, B_READ) || 330 !uvm_useracc(p->green, count, B_READ) || 331 !uvm_useracc(p->blue, count, B_READ)) 332 return (EFAULT); 333 334 copyin(p->red, &sc->sc_cmap.r[index], count); 335 copyin(p->green, &sc->sc_cmap.g[index], count); 336 copyin(p->blue, &sc->sc_cmap.b[index], count); 337 338 if (hwplanemask == 0x0f) { 339 struct bt454 *odac = (struct bt454 *)OMFB_RAMDAC; 340 odac->bt_addr = index; 341 for (i = index; i < count; i++) { 342 odac->bt_cmap = sc->sc_cmap.r[i]; 343 odac->bt_cmap = sc->sc_cmap.g[i]; 344 odac->bt_cmap = sc->sc_cmap.b[i]; 345 } 346 } 347 else if (hwplanemask == 0xff) { 348 struct bt458 *ndac = (struct bt458 *)OMFB_RAMDAC; 349 ndac->bt_addr = index; 350 for (i = index; i < count; i++) { 351 ndac->bt_cmap = sc->sc_cmap.r[i]; 352 ndac->bt_cmap = sc->sc_cmap.g[i]; 353 ndac->bt_cmap = sc->sc_cmap.b[i]; 354 } 355 } 356 return (0); 357 } 358 359 static void 360 omfb_getdevconfig(paddr, dc) 361 paddr_t paddr; 362 struct om_hwdevconfig *dc; 363 { 364 int bpp, i; 365 struct raster *rap; 366 struct rcons *rcp; 367 union { 368 struct { short h, v; } p; 369 u_int32_t u; 370 } rfcnt; 371 372 switch (hwplanemask) { 373 case 0xff: 374 bpp = 8; /* XXX check monochrome bit in DIPSW */ 375 break; 376 default: 377 case 0x0f: 378 bpp = 4; /* XXX check monochrome bit in DIPSW */ 379 break; 380 case 1: 381 bpp = 1; 382 break; 383 } 384 dc->dc_wid = 1280; 385 dc->dc_ht = 1024; 386 dc->dc_depth = bpp; 387 dc->dc_rowbytes = 2048 / 8; 388 dc->dc_cmsize = (bpp == 1) ? 0 : 1 << bpp; 389 dc->dc_videobase = paddr; 390 391 #if 0 /* WHITE on BLACK XXX experiment resulted in WHITE on SKYBLUE... */ 392 if (hwplanemask == 0x0f) { 393 /* XXX Need Bt454 initialization */ 394 struct bt454 *odac = (struct bt454 *)OMFB_RAMDAC; 395 odac->bt_addr = 0; 396 odac->bt_cmap = 0; 397 odac->bt_cmap = 0; 398 odac->bt_cmap = 0; 399 for (i = 1; i < 16; i++) { 400 odac->bt_cmap = 255; 401 odac->bt_cmap = 255; 402 odac->bt_cmap = 255; 403 } 404 } 405 else if (hwplanemask == 0xff) { 406 struct bt458 *ndac = (struct bt458 *)OMFB_RAMDAC; 407 408 ndac->bt_addr = 0x04; 409 ndac->bt_ctrl = 0xff; /* all planes will be read */ 410 ndac->bt_ctrl = 0x00; /* all planes have non-blink */ 411 ndac->bt_ctrl = 0x43; /* pallete enabled, ovly plane */ 412 ndac->bt_ctrl = 0x00; /* no test mode */ 413 ndac->bt_addr = 0; 414 ndac->bt_cmap = 0; 415 ndac->bt_cmap = 0; 416 ndac->bt_cmap = 0; 417 for (i = 1; i < 256; i++) { 418 ndac->bt_cmap = 255; 419 ndac->bt_cmap = 255; 420 ndac->bt_cmap = 255; 421 } 422 } 423 #endif 424 425 /* adjust h/v orgin on screen */ 426 rfcnt.p.h = 7; 427 rfcnt.p.v = -27; 428 *(u_int32_t *)OMFB_RFCNT = rfcnt.u; /* single write of 0x007ffe6 */ 429 430 /* clear the screen */ 431 *(u_int32_t *)OMFB_PLANEMASK = 0xff; 432 ((u_int32_t *)OMFB_ROPFUNC)[5] = ~0; /* ROP copy */ 433 for (i = 0; i < dc->dc_ht * dc->dc_rowbytes/sizeof(u_int32_t); i++) 434 *((u_int32_t *)dc->dc_videobase + i) = 0; 435 *(u_int32_t *)OMFB_PLANEMASK = 0x01; 436 437 /* initialize the raster */ 438 rap = &dc->dc_raster; 439 rap->width = dc->dc_wid; 440 rap->height = dc->dc_ht; 441 rap->depth = dc->dc_depth; 442 rap->linelongs = dc->dc_rowbytes / sizeof(u_int32_t); 443 rap->pixels = (u_int32_t *)dc->dc_videobase; 444 445 /* initialize the raster console blitter */ 446 rcp = &dc->dc_rcons; 447 rcp->rc_sp = rap; 448 rcp->rc_crow = rcp->rc_ccol = -1; 449 rcp->rc_crowp = &rcp->rc_crow; 450 rcp->rc_ccolp = &rcp->rc_ccol; 451 rcons_init(rcp, 34, 80); 452 453 omfb_stdscreen.nrows = dc->dc_rcons.rc_maxrow; 454 omfb_stdscreen.ncols = dc->dc_rcons.rc_maxcol; 455 } 456 457 static int 458 omfb_alloc_screen(v, type, cookiep, curxp, curyp, attrp) 459 void *v; 460 const struct wsscreen_descr *type; 461 void **cookiep; 462 int *curxp, *curyp; 463 long *attrp; 464 { 465 struct omfb_softc *sc = v; 466 long defattr; 467 468 if (sc->nscreens > 0) 469 return (ENOMEM); 470 471 *cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */ 472 *curxp = 0; 473 *curyp = 0; 474 (*omfb_emulops.alloc_attr)(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr); 475 *attrp = defattr; 476 sc->nscreens++; 477 return (0); 478 } 479 480 static void 481 omfb_free_screen(v, cookie) 482 void *v; 483 void *cookie; 484 { 485 struct omfb_softc *sc = v; 486 487 if (sc->sc_dc == &omfb_console_dc) 488 panic("omfb_free_screen: console"); 489 490 sc->nscreens--; 491 } 492 493 static int 494 omfb_show_screen(v, cookie, waitok, cb, cbarg) 495 void *v; 496 void *cookie; 497 int waitok; 498 void (*cb) __P((void *, int, int)); 499 void *cbarg; 500 { 501 return 0; 502 } 503