xref: /netbsd-src/sys/arch/sparc64/dev/ffb.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: ffb.c,v 1.58 2016/11/04 16:35:32 macallan Exp $	*/
2 /*	$OpenBSD: creator.c,v 1.20 2002/07/30 19:48:15 jason Exp $	*/
3 
4 /*
5  * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
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 Jason L. Wright
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: ffb.c,v 1.58 2016/11/04 16:35:32 macallan Exp $");
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/conf.h>
44 #include <sys/ioctl.h>
45 #include <sys/malloc.h>
46 #include <sys/mman.h>
47 
48 #include <sys/bus.h>
49 #include <machine/autoconf.h>
50 #include <machine/openfirm.h>
51 #include <machine/vmparam.h>
52 
53 #include <dev/wscons/wsconsio.h>
54 #include <dev/sun/fbio.h>
55 #include <dev/sun/fbvar.h>
56 
57 #include <dev/wsfont/wsfont.h>
58 #include <dev/wscons/wsdisplay_vconsvar.h>
59 
60 #include <prop/proplib.h>
61 
62 #include <dev/i2c/i2cvar.h>
63 #include <dev/i2c/i2c_bitbang.h>
64 #include <dev/i2c/ddcvar.h>
65 
66 #include <sparc64/dev/ffbreg.h>
67 #include <sparc64/dev/ffbvar.h>
68 
69 #include "opt_wsdisplay_compat.h"
70 #include "opt_ffb.h"
71 
72 #ifndef WS_DEFAULT_BG
73 /* Sun -> background should be white */
74 #define WS_DEFAULT_BG 0xf
75 #endif
76 
77 #ifdef FFB_SYNC
78 #define SYNC ffb_ras_wait(sc)
79 #else
80 #define SYNC
81 #endif
82 
83 /* Debugging */
84 #if !defined FFB_DEBUG
85 #define FFB_DEBUG 0
86 #endif
87 #define DPRINTF(x)	if (ffb_debug) printf x
88 /* Patchable */
89 extern int ffb_debug;
90 #if FFB_DEBUG > 0
91 int ffb_debug = 1;
92 #else
93 int ffb_debug = 0;
94 #endif
95 
96 extern struct cfdriver ffb_cd;
97 
98 struct wsscreen_descr ffb_stdscreen = {
99 	"sunffb",
100 	0, 0,	/* will be filled in -- XXX shouldn't, it's global. */
101 	0,
102 	0, 0,
103 	WSSCREEN_REVERSE | WSSCREEN_WSCOLORS,
104 	NULL	/* modecookie */
105 };
106 
107 const struct wsscreen_descr *ffb_scrlist[] = {
108 	&ffb_stdscreen,
109 	/* XXX other formats? */
110 };
111 
112 struct wsscreen_list ffb_screenlist = {
113 	sizeof(ffb_scrlist) / sizeof(struct wsscreen_descr *),
114 	    ffb_scrlist
115 };
116 
117 static struct vcons_screen ffb_console_screen;
118 
119 int	ffb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
120 static int ffb_blank(struct ffb_softc *, u_long, u_int *);
121 paddr_t ffb_mmap(void *, void *, off_t, int);
122 void	ffb_ras_fifo_wait(struct ffb_softc *, int);
123 void	ffb_ras_wait(struct ffb_softc *);
124 void	ffb_ras_init(struct ffb_softc *);
125 void	ffb_ras_copyrows(void *, int, int, int);
126 void	ffb_ras_erasecols(void *, int, int, int, long int);
127 void	ffb_ras_eraserows(void *, int, int, long int);
128 void	ffb_ras_do_cursor(struct rasops_info *);
129 void	ffb_ras_fill(struct ffb_softc *);
130 void	ffb_ras_invert(struct ffb_softc *);
131 static void	ffb_ras_setfg(struct ffb_softc *, int32_t);
132 static void	ffb_ras_setbg(struct ffb_softc *, int32_t);
133 
134 void	ffb_clearscreen(struct ffb_softc *);
135 int	ffb_load_font(void *, void *, struct wsdisplay_font *);
136 void	ffb_init_screen(void *, struct vcons_screen *, int,
137 	    long *);
138 int	ffb_allocattr(void *, int, int, int, long *);
139 void	ffb_putchar_mono(void *, int, int, u_int, long);
140 void	ffb_putchar_aa(void *, int, int, u_int, long);
141 void	ffb_cursor(void *, int, int, int);
142 
143 /* frame buffer generic driver */
144 static void ffbfb_unblank(device_t);
145 dev_type_open(ffbfb_open);
146 dev_type_close(ffbfb_close);
147 dev_type_ioctl(ffbfb_ioctl);
148 dev_type_mmap(ffbfb_mmap);
149 
150 static struct fbdriver ffb_fbdriver = {
151         ffbfb_unblank, ffbfb_open, ffbfb_close, ffbfb_ioctl, nopoll,
152 	ffbfb_mmap, nokqfilter
153 };
154 
155 struct wsdisplay_accessops ffb_accessops = {
156 	.ioctl = ffb_ioctl,
157 	.mmap = ffb_mmap,
158 };
159 
160 /* I2C glue */
161 static int ffb_i2c_acquire_bus(void *, int);
162 static void ffb_i2c_release_bus(void *, int);
163 static int ffb_i2c_send_start(void *, int);
164 static int ffb_i2c_send_stop(void *, int);
165 static int ffb_i2c_initiate_xfer(void *, i2c_addr_t, int);
166 static int ffb_i2c_read_byte(void *, uint8_t *, int);
167 static int ffb_i2c_write_byte(void *, uint8_t, int);
168 
169 /* I2C bitbang glue */
170 static void ffb_i2cbb_set_bits(void *, uint32_t);
171 static void ffb_i2cbb_set_dir(void *, uint32_t);
172 static uint32_t ffb_i2cbb_read(void *);
173 
174 static const struct i2c_bitbang_ops ffb_i2cbb_ops = {
175 	ffb_i2cbb_set_bits,
176 	ffb_i2cbb_set_dir,
177 	ffb_i2cbb_read,
178 	{
179 		FFB_DAC_CFG_MPDATA_SDA,
180 		FFB_DAC_CFG_MPDATA_SCL,
181 		0,
182 		0
183 	}
184 };
185 
186 void ffb_attach_i2c(struct ffb_softc *);
187 
188 /* Video mode setting */
189 int ffb_tgc_disable(struct ffb_softc *);
190 void ffb_get_pclk(int, uint32_t *, int *);
191 int ffb_set_vmode(struct ffb_softc *, struct videomode *, int, int *, int *);
192 
193 
194 void
195 ffb_attach(device_t self)
196 {
197 	struct ffb_softc *sc = device_private(self);
198 	struct wsemuldisplaydev_attach_args waa;
199 	struct rasops_info *ri;
200 	long defattr;
201 	const char *model, *out_dev;
202 	int btype;
203 	uint32_t dac;
204 	int maxrow;
205 	u_int blank = WSDISPLAYIO_VIDEO_ON;
206 	char buf[6+1];
207 	int i, try_edid;
208 	prop_data_t data;
209 
210 	printf(":");
211 
212 	if (sc->sc_type == FFB_CREATOR) {
213 		btype = prom_getpropint(sc->sc_node, "board_type", 0);
214 		if ((btype & 7) == 3)
215 			printf(" Creator3D");
216 		else
217 			printf(" Creator");
218 	} else {
219 		printf(" Elite3D");
220 		btype = 0;
221 	}
222 
223 	model = prom_getpropstring(sc->sc_node, "model");
224 	if (model == NULL || strlen(model) == 0)
225 		model = "unknown";
226 
227 	sc->sc_depth = 24;
228 	sc->sc_linebytes = 8192;
229 	/* We might alter these during EDID mode setting */
230 	sc->sc_height = prom_getpropint(sc->sc_node, "height", 0);
231 	sc->sc_width = prom_getpropint(sc->sc_node, "width", 0);
232 
233 	sc->sc_locked = 0;
234 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
235 
236 	maxrow = (prom_getoption("screen-#rows", buf, sizeof buf) != 0)
237 		? strtoul(buf, NULL, 10)
238 		: 34;
239 
240 	/* collect DAC version, as Elite3D cursor enable bit is reversed */
241 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_DEVID);
242 	dac = DAC_READ(sc, FFB_DAC_VALUE);
243 	sc->sc_dacrev = (dac >> 28) & 0xf;
244 
245 	if (sc->sc_type == FFB_AFB) {
246 		sc->sc_dacrev = 10;
247 		sc->sc_needredraw = 0;
248 	} else {
249 		/* see what kind of DAC we have */
250 		int pnum = (dac & 0x0ffff000) >> 12;
251 		if (pnum == 0x236e) {
252 			sc->sc_needredraw = 0;
253 		} else {
254 			sc->sc_needredraw = 1;
255 		}
256 	}
257 	printf(", model %s, dac %u\n", model, sc->sc_dacrev);
258 	if (sc->sc_needredraw)
259 		printf("%s: found old DAC, enabling redraw on unblank\n",
260 		    device_xname(sc->sc_dev));
261 
262 	/* Check if a console resolution "<device>:r<res>" is set. */
263 	if (sc->sc_console) {
264 		out_dev = prom_getpropstring(sc->sc_node, "output_device");
265 		if (out_dev != NULL && strlen(out_dev) != 0 &&
266 		    strstr(out_dev, ":r") != NULL)
267 			try_edid = 0;
268 		else
269 			try_edid = 1;
270 	} else
271 		try_edid = 1;
272 
273 #if FFB_DEBUG > 0
274 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TGC);
275 	printf("tgc: %08x\n", DAC_READ(sc, FFB_DAC_VALUE));
276 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_DAC_CTRL);
277 	printf("dcl: %08x\n", DAC_READ(sc, FFB_DAC_VALUE));
278 #endif
279 	ffb_attach_i2c(sc);
280 
281 	/* Need to set asynchronous blank during DDC write/read */
282 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_USR_CTRL);
283 	dac = DAC_READ(sc, FFB_DAC_VALUE);
284 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_USR_CTRL);
285 	DAC_WRITE(sc, FFB_DAC_VALUE, dac | FFB_DAC_USR_CTRL_BLANK);
286 
287 	/* Some monitors don't respond first time */
288 	i = 0;
289 	while (sc->sc_edid_data[1] == 0 && i++ < 3)
290 		ddc_read_edid(&sc->sc_i2c, sc->sc_edid_data, EDID_DATA_LEN);
291 
292 	/* Remove asynchronous blank */
293 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_USR_CTRL);
294 	DAC_WRITE(sc, FFB_DAC_VALUE, dac);
295 
296 	if (edid_parse(&sc->sc_edid_data[0], &sc->sc_edid_info) != -1) {
297 		sort_modes(sc->sc_edid_info.edid_modes,
298 		    &sc->sc_edid_info.edid_preferred_mode,
299 		    sc->sc_edid_info.edid_nmodes);
300 		DPRINTF(("%s: EDID data:\n  ", device_xname(sc->sc_dev)));
301 		for (i = 0; i < EDID_DATA_LEN; i++) {
302 			if (i && !(i % 32))
303 				DPRINTF(("\n "));
304 			if (i && !(i % 4))
305 				DPRINTF((" "));
306 			DPRINTF(("%02x", sc->sc_edid_data[i]));
307 		}
308 		DPRINTF(("\n"));
309 		if (ffb_debug)
310 			edid_print(&sc->sc_edid_info);
311 
312 		data = prop_data_create_data(sc->sc_edid_data, EDID_DATA_LEN);
313 		prop_dictionary_set(device_properties(self), "EDID", data);
314 		prop_object_release(data);
315 
316 		if (try_edid)
317 			for (i = 0; i < sc->sc_edid_info.edid_nmodes; i++) {
318 				if (ffb_set_vmode(sc,
319 			    	    &(sc->sc_edid_info.edid_modes[i]), btype,
320 				    &(sc->sc_width), &(sc->sc_height)))
321 					break;
322 			}
323 	} else {
324 		DPRINTF(("%s: No EDID data.\n", device_xname(sc->sc_dev)));
325 	}
326 
327 	ffb_ras_init(sc);
328 
329 	ffb_blank(sc, WSDISPLAYIO_SVIDEO, &blank);
330 
331 	sc->sc_accel = ((device_cfdata(sc->sc_dev)->cf_flags &
332 	    FFB_CFFLAG_NOACCEL) == 0);
333 
334 	wsfont_init();
335 
336 	vcons_init(&sc->vd, sc, &ffb_stdscreen, &ffb_accessops);
337 	sc->vd.init_screen = ffb_init_screen;
338 	ri = &ffb_console_screen.scr_ri;
339 
340 	/* we mess with ffb_console_screen only once */
341 	if (sc->sc_console) {
342 		vcons_init_screen(&sc->vd, &ffb_console_screen, 1, &defattr);
343 		SCREEN_VISIBLE((&ffb_console_screen));
344 		/*
345 		 * XXX we shouldn't use a global variable for the console
346 		 * screen
347 		 */
348 		sc->vd.active = &ffb_console_screen;
349 		ffb_console_screen.scr_flags = VCONS_SCREEN_IS_STATIC;
350 	} else {
351 		if (ffb_console_screen.scr_ri.ri_rows == 0) {
352 			/* do some minimal setup to avoid weirdnesses later */
353 			vcons_init_screen(&sc->vd, &ffb_console_screen, 1, &defattr);
354 		} else
355 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
356 	}
357 
358 	ffb_stdscreen.nrows = ri->ri_rows;
359 	ffb_stdscreen.ncols = ri->ri_cols;
360 	ffb_stdscreen.textops = &ri->ri_ops;
361 	ffb_stdscreen.capabilities = ri->ri_caps;
362 
363 	sc->sc_fb.fb_driver = &ffb_fbdriver;
364 	sc->sc_fb.fb_type.fb_cmsize = 0;
365 	sc->sc_fb.fb_type.fb_size = maxrow * sc->sc_linebytes;
366 	sc->sc_fb.fb_type.fb_type = FBTYPE_CREATOR;
367 	sc->sc_fb.fb_type.fb_width = sc->sc_width;
368 	sc->sc_fb.fb_type.fb_depth = sc->sc_depth;
369 	sc->sc_fb.fb_type.fb_height = sc->sc_height;
370 	sc->sc_fb.fb_device = sc->sc_dev;
371 	fb_attach(&sc->sc_fb, sc->sc_console);
372 
373 	ffb_clearscreen(sc);
374 
375 	if (sc->sc_console) {
376 		wsdisplay_cnattach(&ffb_stdscreen, ri, 0, 0, defattr);
377 		vcons_replay_msgbuf(&ffb_console_screen);
378 	}
379 
380 	waa.console = sc->sc_console;
381 	waa.scrdata = &ffb_screenlist;
382 	waa.accessops = &ffb_accessops;
383 	waa.accesscookie = &sc->vd;
384 	config_found(sc->sc_dev, &waa, wsemuldisplaydevprint);
385 }
386 
387 void
388 ffb_attach_i2c(struct ffb_softc *sc)
389 {
390 
391 	/* Fill in the i2c tag */
392 	sc->sc_i2c.ic_cookie = sc;
393 	sc->sc_i2c.ic_acquire_bus = ffb_i2c_acquire_bus;
394 	sc->sc_i2c.ic_release_bus = ffb_i2c_release_bus;
395 	sc->sc_i2c.ic_send_start = ffb_i2c_send_start;
396 	sc->sc_i2c.ic_send_stop = ffb_i2c_send_stop;
397 	sc->sc_i2c.ic_initiate_xfer = ffb_i2c_initiate_xfer;
398 	sc->sc_i2c.ic_read_byte = ffb_i2c_read_byte;
399 	sc->sc_i2c.ic_write_byte = ffb_i2c_write_byte;
400 	sc->sc_i2c.ic_exec = NULL;
401 }
402 
403 int
404 ffb_ioctl(void *v, void *vs, u_long cmd, void *data, int flags, struct lwp *l)
405 {
406 	struct vcons_data *vd = v;
407 	struct ffb_softc *sc = vd->cookie;
408 	struct wsdisplay_fbinfo *wdf;
409 	struct vcons_screen *ms = vd->active;
410 
411 	DPRINTF(("ffb_ioctl: %s cmd _IO%s%s('%c', %lu)\n",
412 	       device_xname(sc->sc_dev),
413 	       (cmd & IOC_IN) ? "W" : "", (cmd & IOC_OUT) ? "R" : "",
414 	       (char)IOCGROUP(cmd), cmd & 0xff));
415 
416 	switch (cmd) {
417 	case FBIOGTYPE:
418 		*(struct fbtype *)data = sc->sc_fb.fb_type;
419 		break;
420 	case FBIOGATTR:
421 #define fba ((struct fbgattr *)data)
422 		fba->real_type = sc->sc_fb.fb_type.fb_type;
423 		fba->owner = 0; 	/* XXX ??? */
424 		fba->fbtype = sc->sc_fb.fb_type;
425 		fba->sattr.flags = 0;
426 		fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
427 		fba->sattr.dev_specific[0] = -1;
428 		fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
429 		fba->emu_types[1] = -1;
430 #undef fba
431 		break;
432 
433 	case FBIOGETCMAP:
434 	case FBIOPUTCMAP:
435 		return EIO;
436 
437 	case FBIOGVIDEO:
438 	case FBIOSVIDEO:
439 		return ffb_blank(sc, cmd == FBIOGVIDEO?
440 		    WSDISPLAYIO_GVIDEO : WSDISPLAYIO_SVIDEO,
441 		    (u_int *)data);
442 		break;
443 	case FBIOGCURSOR:
444 	case FBIOSCURSOR:
445 		/* the console driver is not using the hardware cursor */
446 		break;
447 	case FBIOGCURPOS:
448 		printf("%s: FBIOGCURPOS not implemented\n",
449 		    device_xname(sc->sc_dev));
450 		return EIO;
451 	case FBIOSCURPOS:
452 		printf("%s: FBIOSCURPOS not implemented\n",
453 		    device_xname(sc->sc_dev));
454 		return EIO;
455 	case FBIOGCURMAX:
456 		printf("%s: FBIOGCURMAX not implemented\n",
457 		    device_xname(sc->sc_dev));
458 		return EIO;
459 
460 	case WSDISPLAYIO_GTYPE:
461 		*(u_int *)data = WSDISPLAY_TYPE_SUNFFB;
462 		break;
463 	case WSDISPLAYIO_SMODE:
464 		{
465 			if (sc->sc_mode != *(u_int *)data) {
466 				sc->sc_mode = *(u_int *)data;
467 				if ((sc->sc_mode == WSDISPLAYIO_MODE_EMUL) &&
468 				    (sc->sc_locked == 0)) {
469 					ffb_ras_init(sc);
470 					vcons_redraw_screen(ms);
471 				} else {
472 					ffb_ras_wait(sc);
473 				}
474 			}
475 		}
476 		break;
477 	case WSDISPLAYIO_GINFO:
478 		wdf = (void *)data;
479 		wdf->height = sc->sc_height;
480 		wdf->width  = sc->sc_width;
481 		wdf->depth  = 32;
482 		wdf->cmsize = 256; /* XXX */
483 		break;
484 #ifdef WSDISPLAYIO_LINEBYTES
485 	case WSDISPLAYIO_LINEBYTES:
486 		*(u_int *)data = sc->sc_linebytes;
487 		break;
488 #endif
489 	case WSDISPLAYIO_GETCMAP:
490 		break;/* XXX */
491 
492 	case WSDISPLAYIO_PUTCMAP:
493 		break;/* XXX */
494 
495 	case WSDISPLAYIO_SVIDEO:
496 	case WSDISPLAYIO_GVIDEO:
497 		return(ffb_blank(sc, cmd, (u_int *)data));
498 		break;
499 
500 	case WSDISPLAYIO_GCURPOS:
501 	case WSDISPLAYIO_SCURPOS:
502 	case WSDISPLAYIO_GCURMAX:
503 	case WSDISPLAYIO_GCURSOR:
504 	case WSDISPLAYIO_SCURSOR:
505 		return EIO; /* not supported yet */
506 		break;
507 
508 	case WSDISPLAYIO_GET_EDID: {
509 		struct wsdisplayio_edid_info *d = data;
510 		return wsdisplayio_get_edid(sc->sc_dev, d);
511 	}
512 
513 	case WSDISPLAYIO_GET_FBINFO: {
514 		struct wsdisplayio_fbinfo *fbi = data;
515 		return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
516 	}
517 
518 	default:
519 		return EPASSTHROUGH;
520 	}
521 
522 	return (0);
523 }
524 
525 /* blank/unblank the screen */
526 static int
527 ffb_blank(struct ffb_softc *sc, u_long cmd, u_int *data)
528 {
529 	struct vcons_screen *ms = sc->vd.active;
530 	u_int val;
531 
532 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TGC);
533 	val = DAC_READ(sc, FFB_DAC_VALUE);
534 
535 	switch (cmd) {
536 	case WSDISPLAYIO_GVIDEO:
537 		*data = val & 1;
538 		return(0);
539 		break;
540 	case WSDISPLAYIO_SVIDEO:
541 		if (*data == WSDISPLAYIO_VIDEO_OFF)
542 			val &= ~1;
543 		else if (*data == WSDISPLAYIO_VIDEO_ON)
544 			val |= 1;
545 		else
546 			return(EINVAL);
547 		break;
548 	default:
549 		return(EINVAL);
550 	}
551 
552 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TGC);
553 	DAC_WRITE(sc, FFB_DAC_VALUE, val);
554 
555 	if ((val & 1) && sc->sc_needredraw) {
556 		if (ms != NULL) {
557 			if ((sc->sc_mode == WSDISPLAYIO_MODE_EMUL) &&
558 			    (sc->sc_locked == 0)) {
559 				ffb_ras_init(sc);
560 				vcons_redraw_screen(ms);
561 			}
562 		}
563 	}
564 
565 	return(0);
566 }
567 
568 paddr_t
569 ffb_mmap(void *vsc, void *vs, off_t off, int prot)
570 {
571 	struct vcons_data *vd = vsc;
572 	struct ffb_softc *sc = vd->cookie;
573 	int i;
574 
575 	switch (sc->sc_mode) {
576 	case WSDISPLAYIO_MODE_MAPPED:
577 		for (i = 0; i < sc->sc_nreg; i++) {
578 			/* Before this set? */
579 			if (off < sc->sc_addrs[i])
580 				continue;
581 			/* After this set? */
582 			if (off >= (sc->sc_addrs[i] + sc->sc_sizes[i]))
583 				continue;
584 
585 			return (bus_space_mmap(sc->sc_bt, sc->sc_addrs[i],
586 			    off - sc->sc_addrs[i], prot, BUS_SPACE_MAP_LINEAR));
587 		}
588 		break;
589 #ifdef WSDISPLAYIO_MODE_DUMBFB
590 	case WSDISPLAYIO_MODE_DUMBFB:
591 		if (sc->sc_nreg < FFB_REG_DFB24)
592 			break;
593 		if (off >= 0 && off < sc->sc_sizes[FFB_REG_DFB24])
594 			return (bus_space_mmap(sc->sc_bt,
595 			    sc->sc_addrs[FFB_REG_DFB24], off, prot,
596 			    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE));
597 		break;
598 #endif
599 	}
600 	return (-1);
601 }
602 
603 void
604 ffb_ras_fifo_wait(struct ffb_softc *sc, int n)
605 {
606 	int32_t cache = sc->sc_fifo_cache;
607 
608 	if (cache < n) {
609 		do {
610 			cache = FBC_READ(sc, FFB_FBC_UCSR);
611 			cache = (cache & FBC_UCSR_FIFO_MASK) - 8;
612 		} while (cache < n);
613 	}
614 	sc->sc_fifo_cache = cache - n;
615 }
616 
617 void
618 ffb_ras_wait(struct ffb_softc *sc)
619 {
620 	uint32_t ucsr, r;
621 
622 	while (1) {
623 		ucsr = FBC_READ(sc, FFB_FBC_UCSR);
624 		if ((ucsr & (FBC_UCSR_FB_BUSY|FBC_UCSR_RP_BUSY)) == 0)
625 			break;
626 		r = ucsr & (FBC_UCSR_READ_ERR | FBC_UCSR_FIFO_OVFL);
627 		if (r != 0)
628 			FBC_WRITE(sc, FFB_FBC_UCSR, r);
629 	}
630 }
631 
632 void
633 ffb_ras_init(struct ffb_softc *sc)
634 {
635 	uint32_t fbc;
636 
637 	if (sc->sc_width > 1280) {
638 	DPRINTF(("ffb_ras_init: high resolution.\n"));
639 		fbc = FFB_FBC_WM_COMBINED | FFB_FBC_WE_FORCEON |
640 		    FFB_FBC_ZE_OFF | FFB_FBC_YE_OFF | FFB_FBC_XE_ON;
641 	} else {
642 	DPRINTF(("ffb_ras_init: standard resolution.\n"));
643 		fbc = FFB_FBC_XE_OFF;
644 	}
645 	ffb_ras_fifo_wait(sc, 7);
646 	DPRINTF(("WID: %08x\n", FBC_READ(sc, FFB_FBC_WID)));
647 	FBC_WRITE(sc, FFB_FBC_WID, 0x0);
648 	FBC_WRITE(sc, FFB_FBC_PPC,
649 	    FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE | FBC_PPC_ACE_DIS |
650 	    FBC_PPC_APE_DIS | FBC_PPC_DCE_DIS | FBC_PPC_CS_CONST |
651 	    FBC_PPC_ABE_DIS | FBC_PPC_XS_WID);
652 
653 	fbc |= FFB_FBC_WB_A | FFB_FBC_RB_A | FFB_FBC_SB_BOTH |
654 	       FFB_FBC_RGBE_MASK;
655         DPRINTF(("%s: fbc is %08x\n", __func__, fbc));
656         FBC_WRITE(sc, FFB_FBC_FBC, fbc);
657 	FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW);
658 	FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE);
659 	FBC_WRITE(sc, FFB_FBC_PMASK, 0xffffffff);
660 	FBC_WRITE(sc, FFB_FBC_FONTINC, 0x10000);
661 	ffb_ras_fifo_wait(sc, 5);
662 	sc->sc_fg_cache = 0;
663 	FBC_WRITE(sc, FFB_FBC_FG, sc->sc_fg_cache);
664 	sc->sc_bg_cache = 0;
665 	FBC_WRITE(sc, FFB_FBC_BG, sc->sc_bg_cache);
666 	FBC_WRITE(sc, FFB_FBC_BLENDC, FFB_BLENDC_FORCE_ONE |
667 				      FFB_BLENDC_DF_ONE_M_A |
668 				      FFB_BLENDC_SF_A);
669 	FBC_WRITE(sc, FFB_FBC_BLENDC1, 0);
670 	FBC_WRITE(sc, FFB_FBC_BLENDC2, 0);
671 	ffb_ras_wait(sc);
672 }
673 
674 void
675 ffb_ras_eraserows(void *cookie, int row, int n, long attr)
676 {
677 	struct rasops_info *ri = cookie;
678 	struct vcons_screen *scr = ri->ri_hw;
679 	struct ffb_softc *sc = scr->scr_cookie;
680 
681 	if (row < 0) {
682 		n += row;
683 		row = 0;
684 	}
685 	if (row + n > ri->ri_rows)
686 		n = ri->ri_rows - row;
687 	if (n <= 0)
688 		return;
689 
690 	ffb_ras_fill(sc);
691 	ffb_ras_setfg(sc, ri->ri_devcmap[(attr >> 16) & 0xf]);
692 	ffb_ras_fifo_wait(sc, 4);
693 	if ((n == ri->ri_rows) && (ri->ri_flg & RI_FULLCLEAR)) {
694 		FBC_WRITE(sc, FFB_FBC_BY, 0);
695 		FBC_WRITE(sc, FFB_FBC_BX, 0);
696 		FBC_WRITE(sc, FFB_FBC_BH, ri->ri_height);
697 		FBC_WRITE(sc, FFB_FBC_BW, ri->ri_width);
698 	} else {
699 		row *= ri->ri_font->fontheight;
700 		FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + row);
701 		FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin);
702 		FBC_WRITE(sc, FFB_FBC_BH, n * ri->ri_font->fontheight);
703 		FBC_WRITE(sc, FFB_FBC_BW, ri->ri_emuwidth);
704 	}
705 	SYNC;
706 }
707 
708 void
709 ffb_ras_erasecols(void *cookie, int row, int col, int n, long attr)
710 {
711 	struct rasops_info *ri = cookie;
712 	struct vcons_screen *scr = ri->ri_hw;
713 	struct ffb_softc *sc = scr->scr_cookie;
714 
715 	if ((row < 0) || (row >= ri->ri_rows))
716 		return;
717 	if (col < 0) {
718 		n += col;
719 		col = 0;
720 	}
721 	if (col + n > ri->ri_cols)
722 		n = ri->ri_cols - col;
723 	if (n <= 0)
724 		return;
725 	n *= ri->ri_font->fontwidth;
726 	col *= ri->ri_font->fontwidth;
727 	row *= ri->ri_font->fontheight;
728 
729 	ffb_ras_fill(sc);
730 	ffb_ras_setfg(sc, ri->ri_devcmap[(attr >> 16) & 0xf]);
731 	ffb_ras_fifo_wait(sc, 4);
732 	FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + row);
733 	FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin + col);
734 	FBC_WRITE(sc, FFB_FBC_BH, ri->ri_font->fontheight);
735 	FBC_WRITE(sc, FFB_FBC_BW, n);
736 	SYNC;
737 }
738 
739 void
740 ffb_ras_fill(struct ffb_softc *sc)
741 {
742 	ffb_ras_fifo_wait(sc, 3);
743 	FBC_WRITE(sc, FFB_FBC_PPC,
744 	    FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE | FBC_PPC_ACE_DIS |
745 	    FBC_PPC_APE_DIS | FBC_PPC_DCE_DIS | FBC_PPC_CS_CONST |
746 	    FBC_PPC_ABE_DIS | FBC_PPC_XS_WID);
747 	FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW);
748 	FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE);
749 	SYNC;
750 }
751 
752 void
753 ffb_ras_invert(struct ffb_softc *sc)
754 {
755 	ffb_ras_fifo_wait(sc, 3);
756 	FBC_WRITE(sc, FFB_FBC_PPC,
757 	    FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE | FBC_PPC_ACE_DIS |
758 	    FBC_PPC_APE_DIS | FBC_PPC_DCE_DIS | FBC_PPC_CS_CONST |
759 	    FBC_PPC_ABE_DIS | FBC_PPC_XS_WID);
760 	FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_INVERT);
761 	FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_RECTANGLE);
762 	SYNC;
763 }
764 
765 void
766 ffb_ras_copyrows(void *cookie, int src, int dst, int n)
767 {
768 	struct rasops_info *ri = cookie;
769 	struct vcons_screen *scr = ri->ri_hw;
770 	struct ffb_softc *sc = scr->scr_cookie;
771 
772 	if (dst == src)
773 		return;
774 	if (src < 0) {
775 		n += src;
776 		src = 0;
777 	}
778 	if ((src + n) > ri->ri_rows)
779 		n = ri->ri_rows - src;
780 	if (dst < 0) {
781 		n += dst;
782 		dst = 0;
783 	}
784 	if ((dst + n) > ri->ri_rows)
785 		n = ri->ri_rows - dst;
786 	if (n <= 0)
787 		return;
788 	n *= ri->ri_font->fontheight;
789 	src *= ri->ri_font->fontheight;
790 	dst *= ri->ri_font->fontheight;
791 
792 	ffb_ras_fifo_wait(sc, 9);
793 	FBC_WRITE(sc, FFB_FBC_PPC,
794 	    FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE | FBC_PPC_ACE_DIS |
795 	    FBC_PPC_APE_DIS | FBC_PPC_DCE_DIS | FBC_PPC_CS_CONST |
796 	    FBC_PPC_ABE_DIS | FBC_PPC_XS_WID);
797 	FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_OLD | (FBC_ROP_OLD << 8));
798 	FBC_WRITE(sc, FFB_FBC_DRAWOP, FBC_DRAWOP_VSCROLL);
799 	FBC_WRITE(sc, FFB_FBC_BY, ri->ri_yorigin + src);
800 	FBC_WRITE(sc, FFB_FBC_BX, ri->ri_xorigin);
801 	FBC_WRITE(sc, FFB_FBC_DY, ri->ri_yorigin + dst);
802 	FBC_WRITE(sc, FFB_FBC_DX, ri->ri_xorigin);
803 	FBC_WRITE(sc, FFB_FBC_BH, n);
804 	FBC_WRITE(sc, FFB_FBC_BW, ri->ri_emuwidth);
805 	SYNC;
806 }
807 
808 static void
809 ffb_ras_setfg(struct ffb_softc *sc, int32_t fg)
810 {
811 	ffb_ras_fifo_wait(sc, 1);
812 	if (fg == sc->sc_fg_cache)
813 		return;
814 	sc->sc_fg_cache = fg;
815 	FBC_WRITE(sc, FFB_FBC_FG, fg);
816 	SYNC;
817 }
818 
819 static void
820 ffb_ras_setbg(struct ffb_softc *sc, int32_t bg)
821 {
822 	ffb_ras_fifo_wait(sc, 1);
823 	if (bg == sc->sc_bg_cache)
824 		return;
825 	sc->sc_bg_cache = bg;
826 	FBC_WRITE(sc, FFB_FBC_BG, bg);
827 	SYNC;
828 }
829 
830 /* frame buffer generic driver support functions */
831 static void
832 ffbfb_unblank(device_t dev)
833 {
834 	struct ffb_softc *sc = device_private(dev);
835 	struct vcons_screen *ms = sc->vd.active;
836 	u_int on = 1;
837 	int redraw = 0;
838 
839 	ffb_ras_init(sc);
840 	if (sc->sc_locked) {
841 		sc->sc_locked = 0;
842 		redraw = 1;
843 	}
844 
845 	ffb_blank(sc, WSDISPLAYIO_SVIDEO, &on);
846 #if 0
847 	if ((sc->vd.active != &ffb_console_screen) &&
848 	    (ffb_console_screen.scr_flags & VCONS_SCREEN_IS_STATIC)) {
849 		/*
850 		 * force-switch to the console screen.
851 		 * Caveat: the higher layer will think we're still on the
852 		 * other screen
853 		 */
854 
855 		SCREEN_INVISIBLE(sc->vd.active);
856 		sc->vd.active = &ffb_console_screen;
857 		SCREEN_VISIBLE(sc->vd.active);
858 		ms = sc->vd.active;
859 		redraw = 1;
860 	}
861 #endif
862 	if (redraw) {
863 		vcons_redraw_screen(ms);
864 	}
865 }
866 
867 int
868 ffbfb_open(dev_t dev, int flags, int mode, struct lwp *l)
869 {
870 	struct ffb_softc *sc;
871 
872 	sc = device_lookup_private(&ffb_cd, minor(dev));
873 	if (sc == NULL)
874 		return ENXIO;
875 
876 	sc->sc_locked = 1;
877 	return 0;
878 }
879 
880 int
881 ffbfb_close(dev_t dev, int flags, int mode, struct lwp *l)
882 {
883 	struct ffb_softc *sc = device_lookup_private(&ffb_cd, minor(dev));
884 	struct vcons_screen *ms = sc->vd.active;
885 
886 	sc->sc_locked = 0;
887 	if (ms != NULL) {
888 		if ((sc->sc_mode == WSDISPLAYIO_MODE_EMUL) &&
889 		    (sc->sc_locked == 0)) {
890 			ffb_ras_init(sc);
891 			vcons_redraw_screen(ms);
892 		}
893 	}
894 	return 0;
895 }
896 
897 int
898 ffbfb_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
899 {
900 	struct ffb_softc *sc = device_lookup_private(&ffb_cd, minor(dev));
901 
902 	return ffb_ioctl(&sc->vd, NULL, cmd, data, flags, l);
903 }
904 
905 paddr_t
906 ffbfb_mmap(dev_t dev, off_t off, int prot)
907 {
908 	struct ffb_softc *sc = device_lookup_private(&ffb_cd, minor(dev));
909 	uint64_t size;
910 	int i, reg;
911 	off_t o;
912 
913 	/*
914 	 * off is a magic cookie (see xfree86/drivers/sunffb/ffb.h),
915 	 * which we map to an index into the "reg" property, and use
916 	 * our copy of the firmware data as arguments for the real
917 	 * mapping.
918 	 */
919 	static struct { unsigned long voff; int reg; long flags; } map[] = {
920 		{ 0x00000000, FFB_REG_SFB8R, BUS_SPACE_MAP_PREFETCHABLE },
921 		{ 0x00400000, FFB_REG_SFB8G, BUS_SPACE_MAP_PREFETCHABLE },
922 		{ 0x00800000, FFB_REG_SFB8B, BUS_SPACE_MAP_PREFETCHABLE },
923 		{ 0x00c00000, FFB_REG_SFB8X, BUS_SPACE_MAP_PREFETCHABLE },
924 		{ 0x01000000, FFB_REG_SFB32, BUS_SPACE_MAP_PREFETCHABLE },
925 		{ 0x02000000, FFB_REG_SFB64, BUS_SPACE_MAP_PREFETCHABLE  },
926 		{ 0x04000000, FFB_REG_FBC, 0 },
927 		{ 0x04004000, FFB_REG_DFB8R, BUS_SPACE_MAP_PREFETCHABLE },
928 		{ 0x04404000, FFB_REG_DFB8G, BUS_SPACE_MAP_PREFETCHABLE },
929 		{ 0x04804000, FFB_REG_DFB8B, BUS_SPACE_MAP_PREFETCHABLE },
930 		{ 0x04c04000, FFB_REG_DFB8X, BUS_SPACE_MAP_PREFETCHABLE },
931 		{ 0x05004000, FFB_REG_DFB24, BUS_SPACE_MAP_PREFETCHABLE },
932 		{ 0x06004000, FFB_REG_DFB32, BUS_SPACE_MAP_PREFETCHABLE },
933 		{ 0x07004000, FFB_REG_DFB422A, BUS_SPACE_MAP_PREFETCHABLE },
934 		{ 0x0bc06000, FFB_REG_DAC, 0 },
935 		{ 0x0bc08000, FFB_REG_PROM, 0 },
936 		{ 0x0bc18000, 0, 0 }
937 	};
938 
939 	/* special value "FFB_EXP_VOFF" - not backed by any "reg" entry */
940 	if (off == 0x0bc18000)
941 		return bus_space_mmap(sc->sc_bt, sc->sc_addrs[FFB_REG_PROM],
942 		    0x00200000, prot, BUS_SPACE_MAP_LINEAR);
943 
944 	/*
945 	 * FFB_VOFF_FBC_KREGS - used by afbinit to upload firmware. We should
946 	 * probably mmap them only on afb boards
947 	 */
948 	if ((off >= 0x0bc04000) && (off < 0x0bc06000))
949 		return bus_space_mmap(sc->sc_bt, sc->sc_addrs[FFB_REG_PROM],
950 		    0x00610000 + (off - 0x0bc04000), prot,
951 		    BUS_SPACE_MAP_LINEAR);
952 
953 #define NELEMS(arr) (sizeof(arr)/sizeof((arr)[0]))
954 
955 	/* the map is ordered by voff */
956 	for (i = 0; i < NELEMS(map)-1; i++) {
957 		reg = map[i].reg;
958 		/* the number of entries in reg seems to vary */
959 		if (reg < sc->sc_nreg) {
960 			size = min((map[i + 1].voff - map[i].voff),
961 			    sc->sc_sizes[reg]);
962 			if ((off >= map[i].voff) &&
963 			    (off < (map[i].voff + size))) {
964 				o = off - map[i].voff;
965 				return bus_space_mmap(sc->sc_bt,
966 				    sc->sc_addrs[reg], o, prot,
967 				    BUS_SPACE_MAP_LINEAR | map[i].flags);
968 			}
969 		}
970 	}
971 
972 	return -1;
973 }
974 
975 void
976 ffb_clearscreen(struct ffb_softc *sc)
977 {
978 	struct rasops_info *ri = &ffb_console_screen.scr_ri;
979 	ffb_ras_fill(sc);
980 	ffb_ras_setfg(sc, ri->ri_devcmap[WS_DEFAULT_BG]);
981 	ffb_ras_fifo_wait(sc, 4);
982 	FBC_WRITE(sc, FFB_FBC_BY, 0);
983 	FBC_WRITE(sc, FFB_FBC_BX, 0);
984 	FBC_WRITE(sc, FFB_FBC_BH, sc->sc_height);
985 	FBC_WRITE(sc, FFB_FBC_BW, sc->sc_width);
986 }
987 
988 void
989 ffb_cursor(void *cookie, int on, int row, int col)
990 {
991 	struct rasops_info *ri = cookie;
992 	struct vcons_screen *scr;
993 	struct ffb_softc *sc;
994 	int x, y, wi, he;
995 
996 	if (cookie != NULL) {
997 		scr = ri->ri_hw;
998 		sc = scr->scr_cookie;
999 
1000 		wi = ri->ri_font->fontwidth;
1001 		he = ri->ri_font->fontheight;
1002 
1003 		if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1004 
1005 			if (ri->ri_flg & RI_CURSOR) {
1006 
1007 				/* remove cursor */
1008 				x = ri->ri_ccol * wi + ri->ri_xorigin;
1009 				y = ri->ri_crow * he + ri->ri_yorigin;
1010 
1011 				ffb_ras_invert(sc);
1012 				ffb_ras_fifo_wait(sc, 4);
1013 				FBC_WRITE(sc, FFB_FBC_BY, y);
1014 				FBC_WRITE(sc, FFB_FBC_BX, x);
1015 				FBC_WRITE(sc, FFB_FBC_BH, he);
1016 				FBC_WRITE(sc, FFB_FBC_BW, wi);
1017 
1018 				ri->ri_flg &= ~RI_CURSOR;
1019 			}
1020 			ri->ri_crow = row;
1021 			ri->ri_ccol = col;
1022 			if (on)
1023 			{
1024 				x = ri->ri_ccol * wi + ri->ri_xorigin;
1025 				y = ri->ri_crow * he + ri->ri_yorigin;
1026 
1027 				ffb_ras_invert(sc);
1028 				ffb_ras_fifo_wait(sc, 4);
1029 				FBC_WRITE(sc, FFB_FBC_BY, y);
1030 				FBC_WRITE(sc, FFB_FBC_BX, x);
1031 				FBC_WRITE(sc, FFB_FBC_BH, he);
1032 				FBC_WRITE(sc, FFB_FBC_BW, wi);
1033 
1034 				ri->ri_flg |= RI_CURSOR;
1035 			}
1036 		} else {
1037 			ri->ri_crow = row;
1038 			ri->ri_ccol = col;
1039 			ri->ri_flg &= ~RI_CURSOR;
1040 		}
1041 	}
1042 }
1043 
1044 /* mono bitmap font */
1045 void
1046 ffb_putchar_mono(void *cookie, int row, int col, u_int c, long attr)
1047 {
1048 	struct rasops_info *ri = cookie;
1049 	struct vcons_screen *scr = ri->ri_hw;
1050 	struct wsdisplay_font *font = PICK_FONT(ri, c);
1051 	struct ffb_softc *sc = scr->scr_cookie;
1052 	void *data;
1053 	uint32_t fg, bg;
1054 	int i;
1055 	int x, y, wi, he;
1056 
1057 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1058 		return;
1059 
1060 	wi = font->fontwidth;
1061 	he = font->fontheight;
1062 
1063 	if (!CHAR_IN_FONT(c, font))
1064 		return;
1065 
1066 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1067 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
1068 	x = ri->ri_xorigin + col * wi;
1069 	y = ri->ri_yorigin + row * he;
1070 
1071 	data = WSFONT_GLYPH(c, font);
1072 
1073 	ffb_ras_setbg(sc, bg);
1074 	ffb_ras_setfg(sc, fg);
1075 	ffb_ras_fifo_wait(sc, 4);
1076 	FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW);
1077 	FBC_WRITE(sc, FFB_FBC_FONTXY, (y << 16) | x);
1078 	FBC_WRITE(sc, FFB_FBC_FONTW, wi);
1079 	FBC_WRITE(sc, FFB_FBC_PPC,
1080 	    FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE | FBC_PPC_ACE_DIS |
1081 	    FBC_PPC_APE_DIS | FBC_PPC_DCE_DIS | FBC_PPC_CS_CONST |
1082 	    FBC_PPC_ABE_DIS | FBC_PPC_XS_WID);
1083 
1084 	switch (font->stride) {
1085 		case 1: {
1086 			uint8_t *data8 = data;
1087 			uint32_t reg;
1088 			for (i = 0; i < he; i++) {
1089 				reg = *data8;
1090 				FBC_WRITE(sc, FFB_FBC_FONT, reg << 24);
1091 				data8++;
1092 			}
1093 			break;
1094 		}
1095 		case 2: {
1096 			uint16_t *data16 = data;
1097 			uint32_t reg;
1098 			for (i = 0; i < he; i++) {
1099 				reg = *data16;
1100 				FBC_WRITE(sc, FFB_FBC_FONT, reg << 16);
1101 				data16++;
1102 			}
1103 			break;
1104 		}
1105 	}
1106 }
1107 
1108 /* alpha font */
1109 void
1110 ffb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
1111 {
1112 	struct rasops_info *ri = cookie;
1113 	struct vcons_screen *scr = ri->ri_hw;
1114 	struct wsdisplay_font *font = PICK_FONT(ri, c);
1115 	struct ffb_softc *sc = scr->scr_cookie;
1116 	volatile uint32_t *dest, *ddest;
1117 	uint8_t *data8;
1118 	uint32_t fg, bg;
1119 	int i;
1120 	int x, y, wi, he;
1121 	uint32_t alpha = 0x80;
1122 	int j;
1123 
1124 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1125 		return;
1126 
1127 	wi = font->fontwidth;
1128 	he = font->fontheight;
1129 
1130 	if (!CHAR_IN_FONT(c, font))
1131 		return;
1132 
1133 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1134 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
1135 	x = ri->ri_xorigin + col * wi;
1136 	y = ri->ri_yorigin + row * he;
1137 
1138 	data8 = WSFONT_GLYPH(c, font);
1139 
1140 	/* first we erase the background */
1141 	ffb_ras_fill(sc);
1142 	ffb_ras_setfg(sc, bg);
1143 	ffb_ras_fifo_wait(sc, 4);
1144 	FBC_WRITE(sc, FFB_FBC_BY, y);
1145 	FBC_WRITE(sc, FFB_FBC_BX, x);
1146 	FBC_WRITE(sc, FFB_FBC_BH, he);
1147 	FBC_WRITE(sc, FFB_FBC_BW, wi);
1148 
1149 	/* if we draw a space we're done */
1150 	if (c == ' ') return;
1151 
1152 	/* now enable alpha blending */
1153 	ffb_ras_setfg(sc, fg);
1154 	ffb_ras_fifo_wait(sc, 2);
1155 	FBC_WRITE(sc, FFB_FBC_ROP, FBC_ROP_NEW);
1156 
1157 	FBC_WRITE(sc, FFB_FBC_PPC,
1158 	    FBC_PPC_VCE_DIS | FBC_PPC_TBE_OPAQUE | FBC_PPC_ACE_DIS |
1159 	    FBC_PPC_APE_DIS | FBC_PPC_DCE_DIS | FBC_PPC_CS_CONST |
1160 	    FBC_PPC_ABE_ENA | FBC_PPC_XS_VAR);
1161 	/*
1162 	 * we have to wait for both the rectangle drawing op above and the
1163 	 * FFB_FBC_PPC write to finish before mucking around in the SFB aperture
1164 	 */
1165 	ffb_ras_wait(sc);
1166 
1167 	/* ... and draw the character */
1168 	dest = sc->sc_sfb32 + (y << 11) + x;
1169 	for (i = 0; i < he; i++) {
1170 		ddest = dest;
1171 		for (j = 0; j < wi; j++) {
1172 			alpha = *data8;
1173 			/*
1174 			 * We set the colour source to constant above so we only
1175 			 * have to write the alpha channel here and the colour
1176 			 * comes from the FG register. It would be nice if we
1177 			 * could just use the SFB8X aperture and memcpy() the
1178 			 * alpha map line by line but for some strange reason
1179 			 * that will take colour info from the framebuffer even
1180 			 * if we set the FBC_PPC_CS_CONST bit above.
1181 			 */
1182 			*ddest = alpha << 24;
1183 			data8++;
1184 			ddest++;
1185 		}
1186 		dest += 2048;
1187 	}
1188 }
1189 
1190 int
1191 ffb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
1192 {
1193 	if ((fg == 0) && (bg == 0))
1194 	{
1195 		fg = WS_DEFAULT_FG;
1196 		bg = WS_DEFAULT_BG;
1197 	}
1198 	if (flags & WSATTR_REVERSE) {
1199 		*attrp = (bg & 0xff) << 24 | (fg & 0xff) << 16 |
1200 		    (flags & 0xff);
1201 	} else
1202 		*attrp = (fg & 0xff) << 24 | (bg & 0xff) << 16 |
1203 		    (flags & 0xff);
1204 	return 0;
1205 }
1206 
1207 void
1208 ffb_init_screen(void *cookie, struct vcons_screen *scr,
1209     int existing, long *defattr)
1210 {
1211 	struct ffb_softc *sc = cookie;
1212 	struct rasops_info *ri = &scr->scr_ri;
1213 
1214 	ri->ri_depth = 32;
1215 	ri->ri_width = sc->sc_width;
1216 	ri->ri_height = sc->sc_height;
1217 	ri->ri_stride = sc->sc_linebytes;
1218 	ri->ri_flg = RI_CENTER | RI_ENABLE_ALPHA;
1219 
1220 	/*
1221 	 * we can't accelerate copycols() so instead of falling back to
1222 	 * software use vcons' putchar() based implementation
1223 	 */
1224 	scr->scr_flags |= VCONS_NO_COPYCOLS;
1225 #ifdef VCONS_DRAW_INTR
1226         scr->scr_flags |= VCONS_DONT_READ;
1227 #endif
1228 	DPRINTF(("ffb_init_screen: addr: %08lx\n",(ulong)ri->ri_bits));
1229 
1230 	/* explicitly request BGR in case the default changes */
1231 	ri->ri_rnum = 8;
1232 	ri->ri_gnum = 8;
1233 	ri->ri_bnum = 8;
1234 	ri->ri_rpos = 0;
1235 	ri->ri_gpos = 8;
1236 	ri->ri_bpos = 16;
1237 
1238 	rasops_init(ri, 0, 0);
1239 	ri->ri_caps = WSSCREEN_WSCOLORS;
1240 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
1241 		    sc->sc_width / ri->ri_font->fontwidth);
1242 
1243 	/* enable acceleration */
1244 	ri->ri_ops.copyrows = ffb_ras_copyrows;
1245 	ri->ri_ops.eraserows = ffb_ras_eraserows;
1246 	ri->ri_ops.erasecols = ffb_ras_erasecols;
1247 	ri->ri_ops.cursor = ffb_cursor;
1248 	ri->ri_ops.allocattr = ffb_allocattr;
1249 	if (FONT_IS_ALPHA(ri->ri_font)) {
1250 		ri->ri_ops.putchar = ffb_putchar_aa;
1251 	} else
1252 		ri->ri_ops.putchar = ffb_putchar_mono;
1253 }
1254 
1255 /* I2C bitbanging */
1256 static void ffb_i2cbb_set_bits(void *cookie, uint32_t bits)
1257 {
1258 	struct ffb_softc *sc = cookie;
1259 
1260 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_CFG_MPDATA);
1261 	DAC_WRITE(sc, FFB_DAC_VALUE, bits);
1262 }
1263 
1264 static void ffb_i2cbb_set_dir(void *cookie, uint32_t dir)
1265 {
1266 	/* Nothing to do */
1267 }
1268 
1269 static uint32_t ffb_i2cbb_read(void *cookie)
1270 {
1271 	struct ffb_softc *sc = cookie;
1272 	uint32_t bits;
1273 
1274 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_CFG_MPSENSE);
1275 	bits = DAC_READ(sc, FFB_DAC_VALUE);
1276 
1277 	return bits;
1278 }
1279 
1280 /* higher level I2C stuff */
1281 static int
1282 ffb_i2c_acquire_bus(void *cookie, int flags)
1283 {
1284 	/* private bus */
1285 	return (0);
1286 }
1287 
1288 static void
1289 ffb_i2c_release_bus(void *cookie, int flags)
1290 {
1291 	/* private bus */
1292 }
1293 
1294 static int
1295 ffb_i2c_send_start(void *cookie, int flags)
1296 {
1297 	return (i2c_bitbang_send_start(cookie, flags, &ffb_i2cbb_ops));
1298 }
1299 
1300 static int
1301 ffb_i2c_send_stop(void *cookie, int flags)
1302 {
1303 
1304 	return (i2c_bitbang_send_stop(cookie, flags, &ffb_i2cbb_ops));
1305 }
1306 
1307 static int
1308 ffb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
1309 {
1310 	/*
1311 	 * for some reason i2c_bitbang_initiate_xfer left-shifts
1312 	 * the I2C-address and then sets the direction bit
1313 	 */
1314 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
1315 	    &ffb_i2cbb_ops));
1316 }
1317 
1318 static int
1319 ffb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
1320 {
1321 	return (i2c_bitbang_read_byte(cookie, valp, flags, &ffb_i2cbb_ops));
1322 }
1323 
1324 static int
1325 ffb_i2c_write_byte(void *cookie, uint8_t val, int flags)
1326 {
1327 	return (i2c_bitbang_write_byte(cookie, val, flags, &ffb_i2cbb_ops));
1328 }
1329 
1330 
1331 #define TVC_READ_LIMIT	100000
1332 int
1333 ffb_tgc_disable(struct ffb_softc *sc)
1334 {
1335 	int i;
1336 
1337 	/* Is the timing generator disabled? */
1338 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TGC);
1339 	if (!(DAC_READ(sc, FFB_DAC_VALUE) & FFB_DAC_TGC_TIMING_ENABLE))
1340 		return 1;
1341 
1342 	/* If not, disable it when the vertical counter reaches 0 */
1343 	for (i = 0; i < TVC_READ_LIMIT; i++) {
1344 		DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TVC);
1345 		if (!DAC_READ(sc, FFB_DAC_VALUE)) {
1346 			DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TGC);
1347 			DAC_WRITE(sc, FFB_DAC_VALUE, 0);
1348 			return 1;
1349 		}
1350 	}
1351 	return 0;
1352 }
1353 
1354 /*
1355  * PLL Control Register values:
1356  *	M)ultiplier = bits 0:6 + 1
1357  *	D)ivisor = bits 7:10 + 1
1358  *	P)ost divisor = bits 11:13 (000 = 1, 001 = 2, 010 = 4, 011 = 8)
1359  *	Frequency = 13.5 * M / D / P
1360  */
1361 #define FFB_PLL_FREQ	13500000
1362 void
1363 ffb_get_pclk(int request, uint32_t *pll, int *diff)
1364 {
1365 	int m, d, p, f, hex = 0, curdiff;
1366 
1367 	*diff = 100000000;
1368 
1369 	for (m = 32; m <= 80; m++) {
1370 		for (d = 4; d <= 11; d++) {
1371 			for (p = 1; p <= 8; p = p << 1) {
1372 				switch (p) {
1373 				case 1:
1374 					hex = 0x4000 + (d << 7) + m;
1375 					break;
1376 				case 2:
1377 					hex = 0x4800 + (d << 7) + m;
1378 					break;
1379 				case 4:
1380 					hex = 0x5000 + (d << 7) + m;
1381 					break;
1382 				case 8:
1383 					hex = 0x6000 + (d << 7) + m;
1384 					break;
1385 				}
1386 				f = 13500000 * m / d / p;
1387 				if (f == request) {
1388 					*diff = 0;
1389 					*pll = hex;
1390 					return;
1391 				} else {
1392 					curdiff = abs(request - f);
1393 					if (curdiff < *diff) {
1394 						*diff = curdiff;
1395 						*pll = hex;
1396 					}
1397 				}
1398 			}
1399 		}
1400 	}
1401 }
1402 
1403 /*
1404  * Details of the FFB RAMDAC are contained in the Brooktree BT497/498
1405  * and in the Connexant BT497A/498A documentation.
1406  *
1407  * VESA timings to FFB register conversion:
1408  *	If interleave = 4/2:1 then x = 2, if interleave = 8/2:1 then x = 4
1409  *	VBE = VBS - vres = (sync pulse - 1) + back porch
1410  *	VBS = VSS - front porch = (sync pulse - 1) + back porch + vres
1411  *	VSE = sync pulse - 1
1412  *	VSS = (sync pulse - 1) + back porch + vres + front porch
1413  *	HRE = HSS - HSE - 1
1414  *	HBE = (sync pulse + back porch) / x - 1
1415  *	HBS = (sync pulse + back porch + hres) / x - 1
1416  *	HSE = sync pulse / x - 1
1417  *	HSS = (sync pulse + back porch + hres + front porch) / x - 1
1418  *	HCE = HBS - 4
1419  *	HCS = HBE - 4
1420  *	EPE = EIE = EIS = 0 (for all non-interlaced modes)
1421  *
1422  * Note, that 8/2:1 Single Buffered Interleaving is only supported by the
1423  * double-buffered FFB (Creator3D), and at higher resolutions than 1280x1024
1424  *
1425  * Note, that the timing generator should be disabled and re-enabled when the
1426  * the timing parameter registers are being programmed.  Stopping the timing
1427  * generator should only be done when the vertical counter is zero.
1428  */
1429 #define DIVIDE(x,y)	(((x) + ((y) / 2)) / (y))
1430 int
1431 ffb_set_vmode(struct ffb_softc *sc, struct videomode *mode, int btype,
1432     int *hres, int *vres)
1433 {
1434 	int diff;
1435 	uint32_t fp, sp, bp, x;
1436 	uint32_t pll, pfc, ucl, dcl, tgc;
1437 	uint32_t vbe, vbs, vse, vss, hre, hbe, hbs, hse, hss, hce, hcs;
1438 	uint32_t epe, eie, eis;
1439 	uint32_t fbcfg0;
1440 
1441 	DPRINTF(("ffb_set_vmode: %dx%d@%d", mode->hdisplay, mode->vdisplay,
1442 	    DIVIDE(DIVIDE(mode->dot_clock * 1000,
1443 	    mode->htotal), mode->vtotal)));
1444 	DPRINTF((" (%d %d %d %d %d %d %d",
1445 	    mode->dot_clock, mode->hsync_start, mode->hsync_end, mode->htotal,
1446 	    mode->vsync_start, mode->vsync_end, mode->vtotal));
1447 	DPRINTF((" %s%sH %s%sV)\n",
1448 	    mode->flags & VID_PHSYNC ? "+" : "",
1449 	    mode->flags & VID_NHSYNC ? "-" : "",
1450 	    mode->flags & VID_PVSYNC ? "+" : "",
1451 	    mode->flags & VID_NVSYNC ? "-" : ""));
1452 
1453 	/* We don't handle interlaced or doublescan (yet) */
1454 	if ((mode->flags & VID_INTERLACE) || (mode->flags & VID_DBLSCAN))
1455 		return 0;
1456 
1457 	/* Only Creator3D can be set to > 1280x1024 */
1458 	if(((sc->sc_type == FFB_CREATOR && !((btype & 7) == 3)) ||
1459 	    sc->sc_type == FFB_AFB)
1460 	    && (mode->hdisplay > 1280 || mode->vdisplay > 1024))
1461 		return 0;
1462 	/* Creator3D can be set to <= 1920x1360 */
1463 	if (mode->hdisplay > 1920 || mode->vdisplay > 1360)
1464 		return 0;
1465 
1466 	/*
1467 	 * Look for a matching pixel clock and set PLL Control.
1468 	 * XXX: 640x480@60 is 25175000 in modelines but 25125000 in the
1469 	 * FFB PROM, and the closest match to 25175000 (0x4da9/25159090)
1470 	 * does not work.  So, use the PROM value instead.
1471 	 */
1472 	if (mode->hdisplay == 640 && mode->vdisplay == 480 &&
1473 	    mode->dot_clock == 25175) {
1474 		DPRINTF(("ffb_set_vmode: 640x480@60: adjusted dot clock\n"));
1475 		mode->dot_clock = 25125;
1476 	}
1477 	ffb_get_pclk(mode->dot_clock * 1000, &pll, &diff);
1478 	if (diff > 250000)
1479 		return 0;
1480 
1481 	/* Pixel Format Control, User Control and FBC Configuration. */
1482 	if (mode->hdisplay > 1280) {
1483 		pfc = FFB_DAC_PIX_FMT_821;
1484 		ucl = FFB_DAC_USR_CTRL_OVERLAY | FFB_DAC_USR_CTRL_WMODE_C;
1485 		x = 4;
1486 		fbcfg0 = FBC_READ(sc, FFB_FBC_FBCFG0) | FBC_CFG0_DOUBLE_BUF;
1487 	} else {
1488 		pfc = FFB_DAC_PIX_FMT_421;
1489 		/* Only Creator3D and Elite3D can have double-buffer */
1490 		if ((sc->sc_type == FFB_CREATOR && !((btype & 7) == 3)))
1491 			ucl = 0;
1492 		else
1493 			ucl = FFB_DAC_USR_CTRL_DOUBLE;
1494 		ucl |= (FFB_DAC_USR_CTRL_OVERLAY | FFB_DAC_USR_CTRL_WMODE_S8);
1495 		x = 2;
1496 		fbcfg0 = FBC_READ(sc, FFB_FBC_FBCFG0) | FBC_CFG0_SINGLE_BUF;
1497 	}
1498 
1499 	/* DAC Control and Timing Generator Control */
1500 	if (mode->flags & VID_PVSYNC)
1501 		dcl = FFB_DAC_DAC_CTRL_POS_VSYNC;
1502 	else
1503 		dcl = 0;
1504 	tgc = 0;
1505 #define EDID_VID_INP	sc->sc_edid_info.edid_video_input
1506 	if ((EDID_VID_INP & EDID_VIDEO_INPUT_COMPOSITE_SYNC)) {
1507 		dcl |= FFB_DAC_DAC_CTRL_VSYNC_DIS;
1508 		tgc = FFB_DAC_TGC_EQUAL_DISABLE;
1509 	} else {
1510 		dcl |= FFB_DAC_DAC_CTRL_SYNC_G;
1511 		if (EDID_VID_INP & EDID_VIDEO_INPUT_SEPARATE_SYNCS)
1512 			tgc |= FFB_DAC_TGC_VSYNC_DISABLE;
1513 		else
1514 			tgc = FFB_DAC_TGC_EQUAL_DISABLE;
1515 	}
1516 	if (EDID_VID_INP & EDID_VIDEO_INPUT_BLANK_TO_BLACK)
1517 		dcl |= FFB_DAC_DAC_CTRL_PED_ENABLE;
1518 	tgc |= (FFB_DAC_TGC_VIDEO_ENABLE | FFB_DAC_TGC_TIMING_ENABLE |
1519 	    FFB_DAC_TGC_MASTER_ENABLE);
1520 
1521 	/* Vertical timing */
1522 	fp = mode->vsync_start - mode->vdisplay;
1523 	sp = mode->vsync_end - mode->vsync_start;
1524 	bp = mode->vtotal - mode->vsync_end;
1525 
1526 	vbe = sp - 1 + bp;
1527 	vbs = sp - 1 + bp + mode->vdisplay;
1528 	vse = sp - 1;
1529 	vss = sp  - 1 + bp + mode->vdisplay + fp;
1530 
1531 	/* Horizontal timing */
1532 	fp = mode->hsync_start - mode->hdisplay;
1533 	sp = mode->hsync_end - mode->hsync_start;
1534 	bp = mode->htotal - mode->hsync_end;
1535 
1536 	hbe = (sp + bp) / x - 1;
1537 	hbs = (sp + bp + mode->hdisplay) / x - 1;
1538 	hse = sp / x - 1;
1539 	hss = (sp + bp + mode->hdisplay + fp) / x -1;
1540 	hre = hss - hse - 1;
1541 	hce = hbs - 4;
1542 	hcs = hbe - 4;
1543 
1544 	/* Equalisation (interlaced modes) */
1545 	epe = 0;
1546 	eie = 0;
1547 	eis = 0;
1548 
1549 	DPRINTF(("ffb_set_vmode: 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",
1550 	    pll, pfc, ucl, dcl, tgc));
1551 	DPRINTF(("\t0x%04x 0x%04x 0x%04x 0x%04x\n", vbe, vbs, vse, vss));
1552 	DPRINTF(("\t0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",
1553 	    hre, hbe, hbs, hse, hss, hce, hcs));
1554 	DPRINTF(("\t0x%04x 0x%04x 0x%04x\n", epe, eie, eis));
1555 
1556 	if (!ffb_tgc_disable(sc)) {
1557 		DPRINTF(("ffb_set_vmode: failed to disable TGC register\n"));
1558 		return 0;
1559 	}
1560 
1561 	/*
1562 	 * Program the mode registers.
1563 	 * Program the timing generator last, as that re-enables output.
1564 	 * Note, that a read to/write from a register increments the
1565 	 * register address to the next register automatically.
1566 	 */
1567 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_PLL_CTRL);
1568 	DAC_WRITE(sc, FFB_DAC_VALUE, pll);
1569 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_PIX_FMT);
1570 	DAC_WRITE(sc, FFB_DAC_VALUE, pfc);
1571 	DAC_WRITE(sc, FFB_DAC_VALUE, ucl);
1572 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_DAC_CTRL);
1573 	DAC_WRITE(sc, FFB_DAC_VALUE, dcl);
1574 
1575 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_VBE);
1576 	DAC_WRITE(sc, FFB_DAC_VALUE, vbe);
1577 	DAC_WRITE(sc, FFB_DAC_VALUE, vbs);
1578 	DAC_WRITE(sc, FFB_DAC_VALUE, vse);
1579 	DAC_WRITE(sc, FFB_DAC_VALUE, vss);
1580 
1581 	DAC_WRITE(sc, FFB_DAC_VALUE, hre);
1582 	DAC_WRITE(sc, FFB_DAC_VALUE, hbe);
1583 	DAC_WRITE(sc, FFB_DAC_VALUE, hbs);
1584 	DAC_WRITE(sc, FFB_DAC_VALUE, hse);
1585 	DAC_WRITE(sc, FFB_DAC_VALUE, hss);
1586 	DAC_WRITE(sc, FFB_DAC_VALUE, hce);
1587 	DAC_WRITE(sc, FFB_DAC_VALUE, hcs);
1588 
1589 	DAC_WRITE(sc, FFB_DAC_VALUE, epe);
1590 	DAC_WRITE(sc, FFB_DAC_VALUE, eie);
1591 	DAC_WRITE(sc, FFB_DAC_VALUE, eis);
1592 
1593 	FBC_WRITE(sc, FFB_FBC_FBCFG0, fbcfg0);
1594 
1595 	DAC_WRITE(sc, FFB_DAC_TYPE, FFB_DAC_TGC);
1596 	DAC_WRITE(sc, FFB_DAC_VALUE, tgc);
1597 	DPRINTF(("new tgc: %08x\n", tgc));
1598 
1599 	*hres = mode->hdisplay;
1600 	*vres = mode->vdisplay;
1601 
1602 	printf("%s: video mode set to %d x %d @ %dHz\n",
1603 	    device_xname(sc->sc_dev),
1604 	    mode->hdisplay, mode->vdisplay,
1605 	    DIVIDE(DIVIDE(mode->dot_clock * 1000,
1606 	    mode->htotal), mode->vtotal));
1607 
1608 	return 1;
1609 }
1610