1 /* $OpenBSD: fb.c,v 1.25 2013/10/21 10:36:19 miod Exp $ */ 2 /* $NetBSD: fb.c,v 1.23 1997/07/07 23:30:22 pk Exp $ */ 3 4 /* 5 * Copyright (c) 2002, 2004, 2008 Miodrag Vallat. 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 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * 30 * Copyright (c) 1992, 1993 31 * The Regents of the University of California. All rights reserved. 32 * 33 * This software was developed by the Computer Systems Engineering group 34 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 35 * contributed to Berkeley. 36 * 37 * All advertising materials mentioning features or use of this software 38 * must display the following acknowledgement: 39 * This product includes software developed by the University of 40 * California, Lawrence Berkeley Laboratory. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)fb.c 8.1 (Berkeley) 6/11/93 67 */ 68 69 /* 70 * Common wsdisplay framebuffer drivers helpers. 71 */ 72 73 #include <sys/param.h> 74 #include <sys/systm.h> 75 #include <sys/device.h> 76 #include <sys/proc.h> 77 #include <sys/conf.h> 78 79 #include <machine/autoconf.h> 80 #include <machine/conf.h> 81 #include <machine/openfirm.h> 82 83 #include <dev/wscons/wsdisplayvar.h> 84 #include <dev/rasops/rasops.h> 85 #include <machine/fbvar.h> 86 87 #include "wsdisplay.h" 88 89 /* 90 * Sun specific color indexes. 91 * Black is not really 7, but rather ~0; to fit within the 8 ANSI color 92 * palette we are using on console, we pick (~0) & 0x07 instead. 93 * This essentially swaps WSCOL_BLACK and WSCOL_WHITE. 94 */ 95 #define WSCOL_SUN_WHITE 0 96 #define WSCOL_SUN_BLACK 7 97 98 /* 99 * emergency unblank code 100 * XXX should be somewhat moved to wscons MI code 101 */ 102 103 void (*fb_burner)(void *, u_int, u_int); 104 void *fb_cookie; 105 106 void 107 fb_unblank() 108 { 109 if (fb_burner != NULL) 110 (*fb_burner)(fb_cookie, 1, 0); 111 } 112 113 #if NWSDISPLAY > 0 114 115 static int a2int(char *, int); 116 int fb_get_console_metrics(int *, int *, int *, int *); 117 void fb_initwsd(struct sunfb *); 118 void fb_updatecursor(struct rasops_info *); 119 120 int fb_alloc_screen(void *, const struct wsscreen_descr *, void **, 121 int *, int *, long *); 122 void fb_free_screen(void *, void *); 123 int fb_show_screen(void *, void *, int, void (*)(void *, int, int), 124 void *); 125 int fb_load_font(void *, void *, struct wsdisplay_font *); 126 int fb_list_font(void *, struct wsdisplay_font *); 127 128 void 129 fb_setsize(struct sunfb *sf, int def_depth, int def_width, int def_height, 130 int node, int unused) 131 { 132 int def_linebytes; 133 134 sf->sf_depth = getpropint(node, "depth", def_depth); 135 sf->sf_width = getpropint(node, "width", def_width); 136 sf->sf_height = getpropint(node, "height", def_height); 137 138 def_linebytes = 139 roundup(sf->sf_width, sf->sf_depth) * sf->sf_depth / 8; 140 sf->sf_linebytes = getpropint(node, "linebytes", def_linebytes); 141 142 /* 143 * XXX If we are configuring a board in a wider depth level 144 * than the mode it is currently operating in, the PROM will 145 * return a linebytes property tied to the current depth value, 146 * which is NOT what we are relying upon! 147 */ 148 if (sf->sf_linebytes < (sf->sf_width * sf->sf_depth) / 8) 149 sf->sf_linebytes = def_linebytes; 150 151 sf->sf_fbsize = sf->sf_height * sf->sf_linebytes; 152 } 153 154 static int 155 a2int(char *cp, int deflt) 156 { 157 int i = 0; 158 159 if (*cp == '\0') 160 return (deflt); 161 while (*cp != '\0') 162 i = i * 10 + *cp++ - '0'; 163 return (i); 164 } 165 166 /* setup the embedded wsscreen_descr structure from rasops settings */ 167 void 168 fb_initwsd(struct sunfb *sf) 169 { 170 strlcpy(sf->sf_wsd.name, "std", sizeof(sf->sf_wsd.name)); 171 sf->sf_wsd.capabilities = sf->sf_ro.ri_caps; 172 sf->sf_wsd.nrows = sf->sf_ro.ri_rows; 173 sf->sf_wsd.ncols = sf->sf_ro.ri_cols; 174 sf->sf_wsd.textops = &sf->sf_ro.ri_ops; 175 } 176 177 void 178 fb_updatecursor(struct rasops_info *ri) 179 { 180 struct sunfb *sf = (struct sunfb *)ri->ri_hw; 181 182 if (sf->sf_crowp != NULL) 183 *sf->sf_crowp = ri->ri_crow; 184 if (sf->sf_ccolp != NULL) 185 *sf->sf_ccolp = ri->ri_ccol; 186 } 187 188 void 189 fbwscons_init(struct sunfb *sf, int flags, int isconsole) 190 { 191 struct rasops_info *ri = &sf->sf_ro; 192 int cols, rows, fw, fh, wt, wl; 193 194 /* ri_hw and ri_bits must have already been setup by caller */ 195 ri->ri_flg = RI_FULLCLEAR | flags; 196 ri->ri_depth = sf->sf_depth; 197 ri->ri_stride = sf->sf_linebytes; 198 ri->ri_width = sf->sf_width; 199 ri->ri_height = sf->sf_height; 200 201 rows = a2int(getpropstring(optionsnode, "screen-#rows"), 34); 202 cols = a2int(getpropstring(optionsnode, "screen-#columns"), 80); 203 204 /* 205 * If the framebuffer width is under 960 pixels, rasops will 206 * switch from the 12x22 font to the more adequate 8x16 font 207 * here. 208 * If we are the console device, we need to adjust two things: 209 * - the display row should be overrided from the current PROM 210 * metrics, since it will not match the PROM reality anymore. 211 * - the screen needs to be cleared. 212 * 213 * However, to accomodate laptops with specific small fonts, 214 * it is necessary to compare the resolution with the actual 215 * font metrics. 216 */ 217 218 if (isconsole) { 219 if (fb_get_console_metrics(&fw, &fh, &wt, &wl) != 0) { 220 /* 221 * Assume a 12x22 prom font and a centered 222 * 80x34 console window. 223 */ 224 fw = 12; fh = 22; 225 wt = wl = 0; 226 } else { 227 /* 228 * Make sure window-top and window-left 229 * values are consistent with the font metrics. 230 */ 231 if (wt <= 0 || wt > sf->sf_height - rows * fh || 232 wl <= 0 || wl > sf->sf_width - cols * fw) 233 wt = wl = 0; 234 } 235 if (wt == 0 /* || wl == 0 */) { 236 ri->ri_flg |= RI_CENTER; 237 238 /* 239 * Since the console window might not be 240 * centered (e.g. on a 1280x1024 vigra 241 * VS-12 frame buffer), have rasops 242 * clear the margins even if the screen is 243 * not cleared. 244 */ 245 ri->ri_flg |= RI_CLEARMARGINS; 246 } 247 248 if (ri->ri_wsfcookie != 0) { 249 /* driver handles font issues. do nothing. */ 250 } else { 251 /* 252 * If the PROM uses a different font than the 253 * one we are expecting it to use, or if the 254 * display is shorter than 960 pixels wide, 255 * we'll force a screen clear. 256 */ 257 if (fw != 12 || sf->sf_width < 12 * 80) 258 ri->ri_flg |= RI_CLEAR | RI_CENTER; 259 } 260 } else { 261 ri->ri_flg |= RI_CLEAR | RI_CENTER; 262 } 263 264 /* ifb(4) doesn't set ri_bits at the moment */ 265 if (ri->ri_bits == NULL) 266 ri->ri_flg &= ~(RI_CLEAR | RI_CLEARMARGINS); 267 268 rasops_init(ri, rows, cols); 269 270 /* 271 * If this is the console display and there is no font change, 272 * adjust our terminal window to the position of the PROM 273 * window - in case it is not exactly centered. 274 */ 275 if ((ri->ri_flg & RI_CENTER) == 0) { 276 /* code above made sure wt and wl are initialized */ 277 ri->ri_bits += wt * ri->ri_stride; 278 if (ri->ri_depth >= 8) /* for 15bpp to compute ok */ 279 ri->ri_bits += wl * ri->ri_pelbytes; 280 else 281 ri->ri_bits += (wl * ri->ri_depth) >> 3; 282 283 ri->ri_xorigin = wl; 284 ri->ri_yorigin = wt; 285 } 286 287 if (sf->sf_depth == 8) { 288 /* 289 * If we are running with an indexed palette, compensate 290 * the swap of black and white through ri_devcmap. 291 */ 292 ri->ri_devcmap[WSCOL_SUN_BLACK] = 0; 293 ri->ri_devcmap[WSCOL_SUN_WHITE] = 0xffffffff; 294 } else if (sf->sf_depth > 8) { 295 /* 296 * If we are running on a direct color frame buffer, 297 * make the ``normal'' white the same as the highlighted 298 * white. 299 */ 300 ri->ri_devcmap[WSCOL_WHITE] = ri->ri_devcmap[WSCOL_WHITE + 8]; 301 } 302 } 303 304 void 305 fbwscons_console_init(struct sunfb *sf, int row) 306 { 307 struct rasops_info *ri = &sf->sf_ro; 308 void *cookie; 309 long defattr; 310 311 if (romgetcursoraddr(&sf->sf_crowp, &sf->sf_ccolp)) 312 sf->sf_ccolp = sf->sf_crowp = NULL; 313 if (sf->sf_ccolp != NULL) 314 ri->ri_ccol = *sf->sf_ccolp; 315 316 if (ri->ri_flg & RI_CLEAR) { 317 /* 318 * If we have cleared the screen, this is because either 319 * we are not the console display, or the font has been 320 * changed. 321 * In this case, choose not to keep pointers to the PROM 322 * cursor position, as the values are likely to be inaccurate 323 * upon shutdown... 324 */ 325 sf->sf_crowp = sf->sf_ccolp = NULL; 326 row = 0; 327 } 328 329 if (row < 0) /* no override */ { 330 if (sf->sf_crowp != NULL) 331 ri->ri_crow = *sf->sf_crowp; 332 else 333 /* assume last row */ 334 ri->ri_crow = ri->ri_rows - 1; 335 } else { 336 ri->ri_crow = row; 337 } 338 339 /* 340 * Scale back rows and columns if the font would not otherwise 341 * fit on this display. Without this we would panic later. 342 */ 343 if (ri->ri_crow >= ri->ri_rows) 344 ri->ri_crow = ri->ri_rows - 1; 345 if (ri->ri_ccol >= ri->ri_cols) 346 ri->ri_ccol = ri->ri_cols - 1; 347 348 /* 349 * Take care of updating the PROM cursor position as well if we can. 350 */ 351 if (ri->ri_updatecursor != NULL && 352 (sf->sf_ccolp != NULL || sf->sf_crowp != NULL)) 353 ri->ri_updatecursor = fb_updatecursor; 354 355 if (ri->ri_flg & RI_VCONS) 356 cookie = ri->ri_active; 357 else 358 cookie = ri; 359 360 if (ISSET(ri->ri_caps, WSSCREEN_WSCOLORS)) 361 ri->ri_ops.alloc_attr(cookie, 362 WSCOL_BLACK, WSCOL_WHITE, WSATTR_WSCOLORS, &defattr); 363 else 364 ri->ri_ops.alloc_attr(cookie, 0, 0, 0, &defattr); 365 366 fb_initwsd(sf); 367 wsdisplay_cnattach(&sf->sf_wsd, cookie, 368 ri->ri_ccol, ri->ri_crow, defattr); 369 } 370 371 void 372 fbwscons_setcolormap(struct sunfb *sf, 373 void (*setcolor)(void *, u_int, u_int8_t, u_int8_t, u_int8_t)) 374 { 375 int i; 376 const u_char *color; 377 378 if (sf->sf_depth <= 8 && setcolor != NULL) { 379 for (i = 0; i < 16; i++) { 380 color = &rasops_cmap[i * 3]; 381 setcolor(sf, i, color[0], color[1], color[2]); 382 } 383 for (i = 240; i < 256; i++) { 384 color = &rasops_cmap[i * 3]; 385 setcolor(sf, i, color[0], color[1], color[2]); 386 } 387 /* 388 * Compensate for BoW default hardware palette: existing 389 * output (which we do not want to affect) is black on 390 * white with color index 0 being white and 0xff being 391 * black. 392 */ 393 setcolor(sf, WSCOL_SUN_WHITE, 0xff, 0xff, 0xff); 394 setcolor(sf, 0xff ^ WSCOL_SUN_WHITE, 0, 0, 0); 395 setcolor(sf, WSCOL_SUN_BLACK, 0, 0, 0); 396 setcolor(sf, 0xff ^ (WSCOL_SUN_BLACK), 0xff, 0xff, 0xff); 397 } 398 } 399 400 void 401 fbwscons_attach(struct sunfb *sf, struct wsdisplay_accessops *op, int isconsole) 402 { 403 struct wsemuldisplaydev_attach_args waa; 404 405 if (isconsole == 0) { 406 /* done in wsdisplay_cnattach() earlier if console */ 407 fb_initwsd(sf); 408 } else { 409 /* remember screen burner routine */ 410 fb_burner = op->burn_screen; 411 fb_cookie = sf; 412 } 413 414 /* plug common wsdisplay_accessops if necessary */ 415 if (op->alloc_screen == NULL) { 416 op->alloc_screen = fb_alloc_screen; 417 op->free_screen = fb_free_screen; 418 op->show_screen = fb_show_screen; 419 } 420 if (op->load_font == NULL) { 421 op->load_font = fb_load_font; 422 op->list_font = fb_list_font; 423 } 424 425 sf->sf_scrlist[0] = &sf->sf_wsd; 426 sf->sf_wsl.nscreens = 1; 427 sf->sf_wsl.screens = (const struct wsscreen_descr **)sf->sf_scrlist; 428 429 waa.console = isconsole; 430 waa.scrdata = &sf->sf_wsl; 431 waa.accessops = op; 432 waa.accesscookie = sf; 433 waa.defaultscreens = 0; 434 config_found(&sf->sf_dev, &waa, wsemuldisplaydevprint); 435 } 436 437 /* 438 * Common wsdisplay_accessops routines. 439 */ 440 int 441 fb_alloc_screen(void *v, const struct wsscreen_descr *type, 442 void **cookiep, int *curxp, int *curyp, long *attrp) 443 { 444 struct sunfb *sf = v; 445 struct rasops_info *ri = &sf->sf_ro; 446 void *cookie; 447 448 if (sf->sf_nscreens > 0) 449 return (ENOMEM); 450 451 if (ri->ri_flg & RI_VCONS) 452 cookie = ri->ri_active; 453 else 454 cookie = ri; 455 456 *cookiep = cookie; 457 *curyp = 0; 458 *curxp = 0; 459 if (ISSET(ri->ri_caps, WSSCREEN_WSCOLORS)) 460 ri->ri_ops.alloc_attr(cookie, 461 WSCOL_BLACK, WSCOL_WHITE, WSATTR_WSCOLORS, attrp); 462 else 463 ri->ri_ops.alloc_attr(cookie, 0, 0, 0, attrp); 464 sf->sf_nscreens++; 465 return (0); 466 } 467 468 void 469 fb_free_screen(void *v, void *cookie) 470 { 471 struct sunfb *sf = v; 472 473 sf->sf_nscreens--; 474 } 475 476 int 477 fb_show_screen(void *v, void *cookie, int waitok, void (*cb)(void *, int, int), 478 void *cbarg) 479 { 480 return (0); 481 } 482 483 int 484 fb_load_font(void *v, void *emulcookie, struct wsdisplay_font *font) 485 { 486 struct sunfb *sf = v; 487 struct rasops_info *ri = &sf->sf_ro; 488 489 return rasops_load_font(ri, emulcookie, font); 490 } 491 492 int 493 fb_list_font(void *v, struct wsdisplay_font *font) 494 { 495 struct sunfb *sf = v; 496 struct rasops_info *ri = &sf->sf_ro; 497 498 return rasops_list_font(ri, font); 499 } 500 501 int 502 fb_get_console_metrics(int *fontwidth, int *fontheight, int *wtop, int *wleft) 503 { 504 cell_t romheight, romwidth, windowtop, windowleft; 505 506 /* 507 * Get the PROM font metrics and address 508 */ 509 OF_interpret("stdout @ is my-self " 510 "addr char-height addr char-width " 511 "addr window-top addr window-left", 512 4, &windowleft, &windowtop, &romwidth, &romheight); 513 514 if (romheight == 0 || romwidth == 0 || 515 windowtop == 0 || windowleft == 0) 516 return (1); 517 518 *fontwidth = (int)*(uint64_t *)romwidth; 519 *fontheight = (int)*(uint64_t *)romheight; 520 *wtop = (int)*(uint64_t *)windowtop; 521 *wleft = (int)*(uint64_t *)windowleft; 522 523 return (0); 524 } 525 526 #endif /* NWSDISPLAY */ 527