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