xref: /openbsd/sys/arch/sparc64/dev/fb.c (revision 898184e3)
1 /*	$OpenBSD: fb.c,v 1.23 2011/04/07 15:30:16 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 
126 void
127 fb_setsize(struct sunfb *sf, int def_depth, int def_width, int def_height,
128     int node, int unused)
129 {
130 	int def_linebytes;
131 
132 	sf->sf_depth = getpropint(node, "depth", def_depth);
133 	sf->sf_width = getpropint(node, "width", def_width);
134 	sf->sf_height = getpropint(node, "height", def_height);
135 
136 	def_linebytes =
137 	    roundup(sf->sf_width, sf->sf_depth) * sf->sf_depth / 8;
138 	sf->sf_linebytes = getpropint(node, "linebytes", def_linebytes);
139 
140 	/*
141 	 * XXX If we are configuring a board in a wider depth level
142 	 * than the mode it is currently operating in, the PROM will
143 	 * return a linebytes property tied to the current depth value,
144 	 * which is NOT what we are relying upon!
145 	 */
146 	if (sf->sf_linebytes < (sf->sf_width * sf->sf_depth) / 8)
147 		sf->sf_linebytes = def_linebytes;
148 
149 	sf->sf_fbsize = sf->sf_height * sf->sf_linebytes;
150 }
151 
152 static int
153 a2int(char *cp, int deflt)
154 {
155 	int i = 0;
156 
157 	if (*cp == '\0')
158 		return (deflt);
159 	while (*cp != '\0')
160 		i = i * 10 + *cp++ - '0';
161 	return (i);
162 }
163 
164 /* setup the embedded wsscreen_descr structure from rasops settings */
165 void
166 fb_initwsd(struct sunfb *sf)
167 {
168 	strlcpy(sf->sf_wsd.name, "std", sizeof(sf->sf_wsd.name));
169 	sf->sf_wsd.capabilities = sf->sf_ro.ri_caps;
170 	sf->sf_wsd.nrows = sf->sf_ro.ri_rows;
171 	sf->sf_wsd.ncols = sf->sf_ro.ri_cols;
172 	sf->sf_wsd.textops = &sf->sf_ro.ri_ops;
173 }
174 
175 void
176 fb_updatecursor(struct rasops_info *ri)
177 {
178 	struct sunfb *sf = (struct sunfb *)ri->ri_hw;
179 
180 	if (sf->sf_crowp != NULL)
181 		*sf->sf_crowp = ri->ri_crow;
182 	if (sf->sf_ccolp != NULL)
183 		*sf->sf_ccolp = ri->ri_ccol;
184 }
185 
186 void
187 fbwscons_init(struct sunfb *sf, int flags, int isconsole)
188 {
189 	struct rasops_info *ri = &sf->sf_ro;
190 	int cols, rows, fw, fh, wt, wl;
191 
192 	/* ri_hw and ri_bits must have already been setup by caller */
193 	ri->ri_flg = RI_FULLCLEAR | flags;
194 	ri->ri_depth = sf->sf_depth;
195 	ri->ri_stride = sf->sf_linebytes;
196 	ri->ri_width = sf->sf_width;
197 	ri->ri_height = sf->sf_height;
198 
199 	rows = a2int(getpropstring(optionsnode, "screen-#rows"), 34);
200 	cols = a2int(getpropstring(optionsnode, "screen-#columns"), 80);
201 
202 	/*
203 	 * If the framebuffer width is under 960 pixels, rasops will
204 	 * switch from the 12x22 font to the more adequate 8x16 font
205 	 * here.
206 	 * If we are the console device, we need to adjust two things:
207 	 * - the display row should be overrided from the current PROM
208 	 *   metrics, since it will not match the PROM reality anymore.
209 	 * - the screen needs to be cleared.
210 	 *
211 	 * However, to accomodate laptops with specific small fonts,
212 	 * it is necessary to compare the resolution with the actual
213 	 * font metrics.
214 	 */
215 
216 	if (isconsole) {
217 		if (fb_get_console_metrics(&fw, &fh, &wt, &wl) != 0) {
218 			/*
219 			 * Assume a 12x22 prom font and a centered
220 			 * 80x34 console window.
221 			 */
222 			fw = 12; fh = 22;
223 			wt = wl = 0;
224 		} else {
225 			/*
226 			 * Make sure window-top and window-left
227 			 * values are consistent with the font metrics.
228 			 */
229 			if (wt <= 0 || wt > sf->sf_height - rows * fh ||
230 			    wl <= 0 || wl > sf->sf_width - cols * fw)
231 				wt = wl = 0;
232 		}
233 		if (wt == 0 /* || wl == 0 */) {
234 			ri->ri_flg |= RI_CENTER;
235 
236 			/*
237 			 * Since the console window might not be
238 			 * centered (e.g. on a 1280x1024 vigra
239 			 * VS-12 frame buffer), have rasops
240 			 * clear the margins even if the screen is
241 			 * not cleared.
242 			 */
243 			ri->ri_flg |= RI_CLEARMARGINS;
244 		}
245 
246 		if (ri->ri_wsfcookie != 0) {
247 			/* driver handles font issues. do nothing. */
248 		} else {
249 			/*
250 			 * If the PROM uses a different font than the
251 			 * one we are expecting it to use, or if the
252 			 * display is shorter than 960 pixels wide,
253 			 * we'll force a screen clear.
254 			 */
255 			if (fw != 12 || sf->sf_width < 12 * 80)
256 				ri->ri_flg |= RI_CLEAR | RI_CENTER;
257 		}
258 	} else {
259 		ri->ri_flg |= RI_CLEAR | RI_CENTER;
260 	}
261 
262 	/* ifb(4) doesn't set ri_bits at the moment */
263 	if (ri->ri_bits == NULL)
264 		ri->ri_flg &= ~(RI_CLEAR | RI_CLEARMARGINS);
265 
266 	rasops_init(ri, rows, cols);
267 
268 	/*
269 	 * If this is the console display and there is no font change,
270 	 * adjust our terminal window to the position of the PROM
271 	 * window - in case it is not exactly centered.
272 	 */
273 	if ((ri->ri_flg & RI_CENTER) == 0) {
274 		/* code above made sure wt and wl are initialized */
275 		ri->ri_bits += wt * ri->ri_stride;
276 		if (ri->ri_depth >= 8)	/* for 15bpp to compute ok */
277 			ri->ri_bits += wl * ri->ri_pelbytes;
278 		else
279 			ri->ri_bits += (wl * ri->ri_depth) >> 3;
280 
281 		ri->ri_xorigin = wl;
282 		ri->ri_yorigin = wt;
283 	}
284 
285 	if (sf->sf_depth == 8) {
286 		/*
287 		 * If we are running with an indexed palette, compensate
288 		 * the swap of black and white through ri_devcmap.
289 		 */
290 		ri->ri_devcmap[WSCOL_SUN_BLACK] = 0;
291 		ri->ri_devcmap[WSCOL_SUN_WHITE] = 0xffffffff;
292 	} else if (sf->sf_depth > 8) {
293 		/*
294 		 * If we are running on a direct color frame buffer,
295 		 * make the ``normal'' white the same as the highlighted
296 		 * white.
297 		 */
298 		ri->ri_devcmap[WSCOL_WHITE] = ri->ri_devcmap[WSCOL_WHITE + 8];
299 	}
300 }
301 
302 void
303 fbwscons_console_init(struct sunfb *sf, int row)
304 {
305 	struct rasops_info *ri = &sf->sf_ro;
306 	long defattr;
307 
308 	if (romgetcursoraddr(&sf->sf_crowp, &sf->sf_ccolp))
309 		sf->sf_ccolp = sf->sf_crowp = NULL;
310 	if (sf->sf_ccolp != NULL)
311 		ri->ri_ccol = *sf->sf_ccolp;
312 
313 	if (ri->ri_flg & RI_CLEAR) {
314 		/*
315 		 * If we have cleared the screen, this is because either
316 		 * we are not the console display, or the font has been
317 		 * changed.
318 		 * In this case, choose not to keep pointers to the PROM
319 		 * cursor position, as the values are likely to be inaccurate
320 		 * upon shutdown...
321 		 */
322 		sf->sf_crowp = sf->sf_ccolp = NULL;
323 		row = 0;
324 	}
325 
326 	if (row < 0) /* no override */ {
327 		if (sf->sf_crowp != NULL)
328 			ri->ri_crow = *sf->sf_crowp;
329 		else
330 			/* assume last row */
331 			ri->ri_crow = ri->ri_rows - 1;
332 	} else {
333 		ri->ri_crow = row;
334 	}
335 
336 	/*
337 	 * Scale back rows and columns if the font would not otherwise
338 	 * fit on this display. Without this we would panic later.
339 	 */
340 	if (ri->ri_crow >= ri->ri_rows)
341 		ri->ri_crow = ri->ri_rows - 1;
342 	if (ri->ri_ccol >= ri->ri_cols)
343 		ri->ri_ccol = ri->ri_cols - 1;
344 
345 	/*
346 	 * Take care of updating the PROM cursor position as well if we can.
347 	 */
348 	if (ri->ri_updatecursor != NULL &&
349 	    (sf->sf_ccolp != NULL || sf->sf_crowp != NULL))
350 		ri->ri_updatecursor = fb_updatecursor;
351 
352 	if (ISSET(ri->ri_caps, WSSCREEN_WSCOLORS))
353 		ri->ri_ops.alloc_attr(ri,
354 		    WSCOL_BLACK, WSCOL_WHITE, WSATTR_WSCOLORS, &defattr);
355 	else
356 		ri->ri_ops.alloc_attr(ri, 0, 0, 0, &defattr);
357 
358 	fb_initwsd(sf);
359 	wsdisplay_cnattach(&sf->sf_wsd, ri, ri->ri_ccol, ri->ri_crow, defattr);
360 }
361 
362 void
363 fbwscons_setcolormap(struct sunfb *sf,
364     void (*setcolor)(void *, u_int, u_int8_t, u_int8_t, u_int8_t))
365 {
366 	int i;
367 	const u_char *color;
368 
369 	if (sf->sf_depth <= 8 && setcolor != NULL) {
370 		for (i = 0; i < 16; i++) {
371 			color = &rasops_cmap[i * 3];
372 			setcolor(sf, i, color[0], color[1], color[2]);
373 		}
374 		for (i = 240; i < 256; i++) {
375 			color = &rasops_cmap[i * 3];
376 			setcolor(sf, i, color[0], color[1], color[2]);
377 		}
378 		/*
379 		 * Compensate for BoW default hardware palette: existing
380 		 * output (which we do not want to affect) is black on
381 		 * white with color index 0 being white and 0xff being
382 		 * black.
383 		 */
384 		setcolor(sf, WSCOL_SUN_WHITE, 0xff, 0xff, 0xff);
385 		setcolor(sf, 0xff ^ WSCOL_SUN_WHITE, 0, 0, 0);
386 		setcolor(sf, WSCOL_SUN_BLACK, 0, 0, 0);
387 		setcolor(sf, 0xff ^ (WSCOL_SUN_BLACK), 0xff, 0xff, 0xff);
388 	}
389 }
390 
391 void
392 fbwscons_attach(struct sunfb *sf, struct wsdisplay_accessops *op, int isconsole)
393 {
394 	struct wsemuldisplaydev_attach_args waa;
395 
396 	if (isconsole == 0) {
397 		/* done in wsdisplay_cnattach() earlier if console */
398 		fb_initwsd(sf);
399 	} else {
400 		/* remember screen burner routine */
401 		fb_burner = op->burn_screen;
402 		fb_cookie = sf;
403 	}
404 
405 	/* plug common wsdisplay_accessops if necessary */
406 	if (op->alloc_screen == NULL) {
407 		op->alloc_screen = fb_alloc_screen;
408 		op->free_screen = fb_free_screen;
409 		op->show_screen = fb_show_screen;
410 	}
411 
412 	sf->sf_scrlist[0] = &sf->sf_wsd;
413 	sf->sf_wsl.nscreens = 1;
414 	sf->sf_wsl.screens = (const struct wsscreen_descr **)sf->sf_scrlist;
415 
416 	waa.console = isconsole;
417 	waa.scrdata = &sf->sf_wsl;
418 	waa.accessops = op;
419 	waa.accesscookie = sf;
420 	waa.defaultscreens = 0;
421 	config_found(&sf->sf_dev, &waa, wsemuldisplaydevprint);
422 }
423 
424 /*
425  * Common wsdisplay_accessops routines.
426  */
427 int
428 fb_alloc_screen(void *v, const struct wsscreen_descr *type,
429     void **cookiep, int *curxp, int *curyp, long *attrp)
430 {
431 	struct sunfb *sf = v;
432 	struct rasops_info *ri = &sf->sf_ro;
433 
434 	if (sf->sf_nscreens > 0)
435 		return (ENOMEM);
436 
437 	*cookiep = ri;
438 	*curyp = 0;
439 	*curxp = 0;
440 	if (ISSET(ri->ri_caps, WSSCREEN_WSCOLORS))
441 		ri->ri_ops.alloc_attr(ri,
442 		    WSCOL_BLACK, WSCOL_WHITE, WSATTR_WSCOLORS, attrp);
443 	else
444 		ri->ri_ops.alloc_attr(ri, 0, 0, 0, attrp);
445 	sf->sf_nscreens++;
446 	return (0);
447 }
448 
449 void
450 fb_free_screen(void *v, void *cookie)
451 {
452 	struct sunfb *sf = v;
453 
454 	sf->sf_nscreens--;
455 }
456 
457 int
458 fb_show_screen(void *v, void *cookie, int waitok, void (*cb)(void *, int, int),
459     void *cbarg)
460 {
461 	return (0);
462 }
463 
464 int
465 fb_get_console_metrics(int *fontwidth, int *fontheight, int *wtop, int *wleft)
466 {
467 	cell_t romheight, romwidth, windowtop, windowleft;
468 
469 	/*
470 	 * Get the PROM font metrics and address
471 	 */
472 	OF_interpret("stdout @ is my-self "
473 	    "addr char-height addr char-width "
474 	    "addr window-top addr window-left",
475 	    4, &windowleft, &windowtop, &romwidth, &romheight);
476 
477 	if (romheight == 0 || romwidth == 0 ||
478 	    windowtop == 0 || windowleft == 0)
479 		return (1);
480 
481 	*fontwidth = (int)*(uint64_t *)romwidth;
482 	*fontheight = (int)*(uint64_t *)romheight;
483 	*wtop = (int)*(uint64_t *)windowtop;
484 	*wleft = (int)*(uint64_t *)windowleft;
485 
486 	return (0);
487 }
488 
489 #endif	/* NWSDISPLAY */
490