1 /* $NetBSD: fb_elb.c,v 1.13 2011/07/01 19:02:32 dyoung Exp $ */ 2 3 /*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Juergen Hannken-Illjes. 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: fb_elb.c,v 1.13 2011/07/01 19:02:32 dyoung Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/conf.h> 37 #include <sys/device.h> 38 #include <sys/ioctl.h> 39 #include <sys/malloc.h> 40 #include <sys/systm.h> 41 42 #include <dev/wscons/wsconsio.h> 43 #include <dev/wscons/wsdisplayvar.h> 44 #include <dev/rasops/rasops.h> 45 46 #include <machine/explora.h> 47 #include <sys/bus.h> 48 49 #include <evbppc/explora/dev/elbvar.h> 50 51 #define FB_NPORTS 65536 52 53 struct fb_dev { 54 void *fb_vram; 55 bus_space_tag_t fb_iot; 56 bus_space_handle_t fb_ioh; 57 struct rasops_info fb_ri; 58 }; 59 60 struct fb_elb_softc { 61 device_t sc_dev; 62 struct fb_dev *sc_fb; 63 int sc_nscreens; 64 }; 65 66 static int fb_elb_probe(device_t, cfdata_t, void *); 67 static void fb_elb_attach(device_t, device_t, void *); 68 void fb_cnattach(bus_space_tag_t, bus_addr_t, void *); 69 static void fb_init(struct fb_dev *, int); 70 static int fb_ioctl(void *, void *, u_long, void *, int, struct lwp *); 71 static paddr_t fb_mmap(void *, void *, off_t, int); 72 static int fb_alloc_screen(void *, const struct wsscreen_descr *, void **, 73 int *, int *, long *); 74 static void fb_free_screen(void *, void *); 75 static int fb_show_screen(void *, void *, int, void (*)(void *, int, int), 76 void *); 77 78 static void fb_eraserows(void *, int, int, long); 79 static void fb_erasecols(void *, int, int, int, long); 80 static void fb_copyrows(void *, int, int, int); 81 static void fb_copycols(void *, int, int, int, int); 82 83 static void s3_init(struct fb_dev *, int *, int *); 84 static void s3_copy(struct fb_dev *, int, int, int, int, int, int, int); 85 static void s3_fill(struct fb_dev *, int, int, int, int, int, int); 86 87 static struct fb_dev console_dev; 88 89 static struct wsdisplay_accessops accessops = { 90 fb_ioctl, 91 fb_mmap, 92 fb_alloc_screen, 93 fb_free_screen, 94 fb_show_screen, 95 NULL 96 }; 97 98 static struct wsscreen_descr stdscreen = { 99 "std", 100 0, 0, 101 0, 102 0, 0, 103 0 104 }; 105 106 static const struct wsscreen_descr *scrlist[] = { 107 &stdscreen 108 }; 109 110 static struct wsscreen_list screenlist = { 111 __arraycount(scrlist), scrlist 112 }; 113 114 CFATTACH_DECL_NEW(fb_elb, sizeof(struct fb_elb_softc), 115 fb_elb_probe, fb_elb_attach, NULL, NULL); 116 117 static int 118 fb_elb_probe(device_t parent, cfdata_t cf, void *aux) 119 { 120 struct elb_attach_args *oaa = aux; 121 122 if (strcmp(oaa->elb_name, cf->cf_name) != 0) 123 return 0; 124 125 return (1); 126 } 127 128 static void 129 fb_elb_attach(device_t parent, device_t self, void *aux) 130 { 131 struct fb_elb_softc *sc = device_private(self); 132 struct elb_attach_args *eaa = aux; 133 struct wsemuldisplaydev_attach_args waa; 134 struct rasops_info *ri; 135 bus_space_handle_t ioh; 136 int is_console; 137 138 sc->sc_dev = self; 139 140 is_console = ((void *)eaa->elb_base == console_dev.fb_vram); 141 142 if (is_console) { 143 sc->sc_fb = &console_dev; 144 sc->sc_fb->fb_ri.ri_flg &= ~RI_NO_AUTO; 145 } else { 146 sc->sc_fb = malloc(sizeof(struct fb_dev), M_DEVBUF, M_WAITOK); 147 memset(sc->sc_fb, 0, sizeof(struct fb_dev)); 148 } 149 150 sc->sc_fb->fb_iot = eaa->elb_bt; 151 bus_space_map(sc->sc_fb->fb_iot, eaa->elb_base, SIZE_FB, 152 BUS_SPACE_MAP_LINEAR, &ioh); 153 sc->sc_fb->fb_vram = bus_space_vaddr(sc->sc_fb->fb_iot, ioh); 154 bus_space_map(sc->sc_fb->fb_iot, eaa->elb_base2, FB_NPORTS, 155 0, &sc->sc_fb->fb_ioh); 156 157 fb_init(sc->sc_fb, !is_console); 158 159 ri = &sc->sc_fb->fb_ri; 160 161 printf(": %d x %d\n", ri->ri_rows, ri->ri_cols); 162 163 waa.console = is_console; 164 waa.scrdata = &screenlist; 165 waa.accessops = &accessops; 166 waa.accesscookie = sc; 167 168 config_found(self, &waa, wsemuldisplaydevprint); 169 } 170 171 static void 172 fb_init(struct fb_dev *fb, int full) 173 { 174 struct rasops_info *ri = &fb->fb_ri; 175 176 if (full) { 177 s3_init(fb, &ri->ri_width, &ri->ri_height); 178 ri->ri_depth = 8; 179 ri->ri_stride = ri->ri_width; 180 ri->ri_bits = fb->fb_vram; 181 ri->ri_flg = RI_CENTER; 182 if (ri == &console_dev.fb_ri) 183 ri->ri_flg |= RI_NO_AUTO; 184 185 rasops_init(ri, 500, 500); 186 } else { 187 ri->ri_origbits = fb->fb_vram; /*XXX*/ 188 rasops_reconfig(ri, 500, 500); 189 } 190 191 /* Replace the copy/erase ops. */ 192 ri->ri_hw = fb; 193 ri->ri_ops.eraserows = fb_eraserows; 194 ri->ri_ops.erasecols = fb_erasecols; 195 ri->ri_ops.copyrows = fb_copyrows; 196 ri->ri_ops.copycols = fb_copycols; 197 198 stdscreen.nrows = ri->ri_rows; 199 stdscreen.ncols = ri->ri_cols; 200 stdscreen.textops = &ri->ri_ops; 201 stdscreen.capabilities = ri->ri_caps; 202 } 203 204 static int 205 fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l) 206 { 207 struct fb_elb_softc *sc = v; 208 struct rasops_info *ri = &sc->sc_fb->fb_ri; 209 struct wsdisplay_fbinfo *wdf; 210 211 switch (cmd) { 212 case WSDISPLAYIO_GTYPE: 213 *(int *)data = WSDISPLAY_TYPE_UNKNOWN; /* XXX */ 214 return(0); 215 216 case WSDISPLAYIO_GINFO: 217 wdf = (void *)data; 218 wdf->height = ri->ri_height; 219 wdf->width = ri->ri_width; 220 wdf->depth = ri->ri_depth; 221 wdf->cmsize = 16; /*XXX*/ 222 return(0); 223 224 case WSDISPLAYIO_SVIDEO: 225 case WSDISPLAYIO_GETCMAP: 226 case WSDISPLAYIO_PUTCMAP: 227 break; 228 } 229 230 return(EPASSTHROUGH); 231 } 232 233 static paddr_t 234 fb_mmap(void *v, void *vs, off_t offset, int prot) 235 { 236 struct fb_elb_softc *sc = v; 237 238 if (offset < 0 || offset >= SIZE_FB) 239 return -1; 240 241 return bus_space_mmap(sc->sc_fb->fb_iot, BASE_FB, offset, prot, 242 BUS_SPACE_MAP_LINEAR); 243 } 244 245 static int 246 fb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc, void **cookiep, 247 int *ccolp, int *crowp, long *attrp) 248 { 249 struct fb_elb_softc *sc = v; 250 struct rasops_info *ri = &sc->sc_fb->fb_ri; 251 252 if (sc->sc_nscreens > 0) 253 return ENOMEM; 254 255 *cookiep = ri; 256 *ccolp = *crowp = 0; 257 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, attrp); 258 sc->sc_nscreens++; 259 260 return(0); 261 } 262 263 static void 264 fb_free_screen(void *v, void *cookie) 265 { 266 struct fb_elb_softc *sc = v; 267 268 if (sc->sc_fb == &console_dev) 269 panic("fb_free_screen: freeing console"); 270 271 sc->sc_nscreens--; 272 } 273 274 static int 275 fb_show_screen(void *v, void *cookie, int waitok, void (*cb)(void *, int, int), 276 void *cbarg) 277 { 278 return(0); 279 } 280 281 void 282 fb_cnattach(bus_space_tag_t iot, bus_addr_t iobase, void *vram) 283 { 284 struct rasops_info *ri = &console_dev.fb_ri; 285 long defattr; 286 287 console_dev.fb_iot = iot; 288 console_dev.fb_ioh = iobase; 289 console_dev.fb_vram = vram; 290 291 fb_init(&console_dev, 1); 292 293 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); 294 295 wsdisplay_cnattach(&stdscreen, ri, 0, 0, defattr); 296 } 297 298 static void 299 fb_eraserows(void *v, int row, int nrows, long attr) 300 { 301 struct rasops_info *ri = v; 302 struct fb_dev *fb = ri->ri_hw; 303 304 row *= ri->ri_font->fontheight; 305 nrows *= ri->ri_font->fontheight; 306 307 s3_fill(fb, 0, row, ri->ri_stride, nrows, (attr >> 16)&0x0f, 0x0f); 308 } 309 310 static void 311 fb_erasecols(void *v, int row, int startcol, int ncols, long attr) 312 { 313 struct rasops_info *ri = v; 314 struct fb_dev *fb = ri->ri_hw; 315 316 row *= ri->ri_font->fontheight; 317 startcol *= ri->ri_font->fontwidth; 318 ncols *= ri->ri_font->fontwidth; 319 320 s3_fill(fb, startcol, row, ncols, ri->ri_font->fontheight, 321 (attr >> 16)&0x0f, 0x0f); 322 } 323 324 static void 325 fb_copyrows(void *v, int srcrow, int dstrow, int nrows) 326 { 327 struct rasops_info *ri = v; 328 struct fb_dev *fb = ri->ri_hw; 329 330 srcrow *= ri->ri_font->fontheight; 331 dstrow *= ri->ri_font->fontheight; 332 nrows *= ri->ri_font->fontheight; 333 334 s3_copy(fb, 0, srcrow, 0, dstrow, ri->ri_stride, nrows, 0x0f); 335 } 336 337 static void 338 fb_copycols(void *v, int row, int srccol, int dstcol, int ncols) 339 { 340 struct rasops_info *ri = v; 341 struct fb_dev *fb = ri->ri_hw; 342 343 row *= ri->ri_font->fontheight; 344 srccol *= ri->ri_font->fontwidth; 345 dstcol *= ri->ri_font->fontwidth; 346 ncols *= ri->ri_font->fontwidth; 347 348 s3_copy(fb, srccol, row, dstcol, row, 349 ncols, ri->ri_font->fontheight, 0x0f); 350 } 351 352 /* 353 * S3 support routines 354 */ 355 356 #define S3_CRTC_INDEX 0x83d4 357 #define S3_CRTC_DATA 0x83d5 358 359 #define S3_DAC_RD_INDEX 0x83c7 360 #define S3_DAC_WR_INDEX 0x83c8 361 #define S3_DAC_DATA 0x83c9 362 363 #define S3_CUR_Y 0x82e8 364 #define S3_CUR_X 0x86e8 365 #define S3_DESTY_AXSTP 0x8ae8 366 #define S3_DESTX_DIASTP 0x8ee8 367 #define S3_MAJ_AXIS_PCNT 0x96e8 368 #define S3_GP_STAT 0x9ae8 369 #define S3_CMD 0x9ae8 370 #define S3_BKGD_COLOR 0xa2e8 371 #define S3_FRGD_COLOR 0xa6e8 372 #define S3_WRT_MASK 0xaae8 373 #define S3_RD_MASK 0xaee8 374 #define S3_BKGD_MIX 0xb6e8 375 #define S3_FRGD_MIX 0xbae8 376 #define S3_MULTIFUNC_CNTL 0xbee8 377 378 #define S3_GP_STAT_FIFO_1 0x0080 379 #define S3_GP_STAT_BSY 0x0200 380 381 #define S3_CMD_BITBLT 0xc001 382 #define S3_CMD_RECT 0x4001 383 #define S3_INC_Y 0x0080 384 #define S3_INC_X 0x0020 385 #define S3_DRAW 0x0010 386 #define S3_MULTI 0x0002 387 388 #define S3_CSRC_BKGDCOL 0x0000 389 #define S3_CSRC_FRGDCOL 0x0020 390 #define S3_CSRC_DISPMEM 0x0060 391 #define S3_MIX_NEW 0x0007 392 393 #define CMAP_SIZE 256 394 395 static u_int8_t default_cmap[] = { 396 /* black */ 0, 0, 0, 397 /* blue */ 0, 0, 192, 398 /* green */ 0, 192, 0, 399 /* cyan */ 0, 192, 192, 400 /* red */ 192, 0, 0, 401 /* magenta */ 192, 0, 192, 402 /* brown */ 192, 192, 0, 403 /* lightgrey */ 212, 208, 200, 404 /* darkgrey */ 200, 192, 188, 405 /* lightblue */ 0, 0, 255, 406 /* lightgreen */ 0, 255, 0, 407 /* lightcyan */ 0, 255, 255, 408 /* lightred */ 255, 0, 0, 409 /* lightmagenta */ 255, 0, 255, 410 /* yellow */ 255, 255, 0, 411 /* white */ 255, 255, 255, 412 }; 413 414 static void 415 s3_init(struct fb_dev *fb, int *width, int *height) 416 { 417 int i, j, w, h; 418 bus_space_tag_t iot = fb->fb_iot; 419 bus_space_handle_t ioh = fb->fb_ioh; 420 421 /* Initialize colormap */ 422 423 bus_space_write_1(iot, ioh, S3_DAC_WR_INDEX, 0); 424 425 for (i = j = 0; i < CMAP_SIZE*3; i++) { 426 bus_space_write_1(iot, ioh, S3_DAC_DATA, default_cmap[j] >> 2); 427 j = (j+1) % sizeof(default_cmap)/sizeof(default_cmap[0]); 428 } 429 430 /* Retrieve frame buffer geometry */ 431 432 bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 1); 433 w = bus_space_read_1(iot, ioh, S3_CRTC_DATA); 434 435 bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 18); 436 h = bus_space_read_1(iot, ioh, S3_CRTC_DATA); 437 438 bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 7); 439 i = bus_space_read_1(iot, ioh, S3_CRTC_DATA); 440 441 h += (i << 7) & 0x100; 442 h += (i << 3) & 0x200; 443 444 *width = (w+1) << 3; 445 *height = h+1; 446 } 447 448 static void 449 s3_copy(struct fb_dev *fb, int src_x, int src_y, int dest_x, int dest_y, 450 int width, int height, int mask) 451 { 452 bus_space_tag_t iot = fb->fb_iot; 453 bus_space_handle_t ioh = fb->fb_ioh; 454 u_int16_t cmd = S3_CMD_BITBLT | S3_DRAW; 455 456 if (src_x > dest_x) 457 cmd |= S3_INC_X; 458 else { 459 src_x += width-1; 460 dest_x += width-1; 461 } 462 463 if (src_y > dest_y) 464 cmd |= S3_INC_Y; 465 else { 466 src_y += height-1; 467 dest_y += height-1; 468 } 469 470 while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_FIFO_1) 471 ; 472 473 bus_space_write_2(iot, ioh, S3_FRGD_MIX, S3_CSRC_DISPMEM | S3_MIX_NEW); 474 bus_space_write_2(iot, ioh, S3_WRT_MASK, mask); 475 bus_space_write_2(iot, ioh, S3_CUR_X, src_x); 476 bus_space_write_2(iot, ioh, S3_CUR_Y, src_y); 477 bus_space_write_2(iot, ioh, S3_DESTX_DIASTP, dest_x); 478 bus_space_write_2(iot, ioh, S3_DESTY_AXSTP, dest_y); 479 bus_space_write_2(iot, ioh, S3_MULTIFUNC_CNTL, height-1); 480 bus_space_write_2(iot, ioh, S3_MAJ_AXIS_PCNT, width-1); 481 bus_space_write_2(iot, ioh, S3_CMD, cmd); 482 483 while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_BSY) 484 ; 485 } 486 487 static void 488 s3_fill(struct fb_dev *fb, int x, int y, int width, int height, 489 int color, int mask) 490 { 491 bus_space_tag_t iot = fb->fb_iot; 492 bus_space_handle_t ioh = fb->fb_ioh; 493 u_int16_t cmd = S3_CMD_RECT | S3_INC_X | S3_INC_Y | S3_DRAW; 494 495 while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_FIFO_1) 496 ; 497 498 bus_space_write_2(iot, ioh, S3_FRGD_MIX, S3_CSRC_FRGDCOL | S3_MIX_NEW); 499 bus_space_write_2(iot, ioh, S3_FRGD_COLOR, color); 500 bus_space_write_2(iot, ioh, S3_WRT_MASK, mask); 501 bus_space_write_2(iot, ioh, S3_CUR_X, x); 502 bus_space_write_2(iot, ioh, S3_CUR_Y, y); 503 bus_space_write_2(iot, ioh, S3_MULTIFUNC_CNTL, height-1); 504 bus_space_write_2(iot, ioh, S3_MAJ_AXIS_PCNT, width-1); 505 bus_space_write_2(iot, ioh, S3_CMD, cmd); 506 507 while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_BSY) 508 ; 509 } 510