Home | History | Annotate | Line # | Download | only in ic
igsfb.c revision 1.11
      1  1.11     uwe /*	$NetBSD: igsfb.c,v 1.11 2003/05/11 03:20:09 uwe Exp $ */
      2   1.1     uwe 
      3   1.1     uwe /*
      4   1.8     uwe  * Copyright (c) 2002, 2003 Valeriy E. Ushakov
      5   1.1     uwe  * All rights reserved.
      6   1.1     uwe  *
      7   1.1     uwe  * Redistribution and use in source and binary forms, with or without
      8   1.1     uwe  * modification, are permitted provided that the following conditions
      9   1.1     uwe  * are met:
     10   1.1     uwe  * 1. Redistributions of source code must retain the above copyright
     11   1.1     uwe  *    notice, this list of conditions and the following disclaimer.
     12   1.1     uwe  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1     uwe  *    notice, this list of conditions and the following disclaimer in the
     14   1.1     uwe  *    documentation and/or other materials provided with the distribution.
     15   1.1     uwe  * 3. The name of the author may not be used to endorse or promote products
     16   1.1     uwe  *    derived from this software without specific prior written permission
     17   1.1     uwe  *
     18   1.1     uwe  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1     uwe  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1     uwe  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1     uwe  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1     uwe  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23   1.1     uwe  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24   1.1     uwe  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25   1.1     uwe  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26   1.1     uwe  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27   1.1     uwe  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28   1.1     uwe  */
     29   1.1     uwe 
     30   1.1     uwe /*
     31   1.4     uwe  * Integraphics Systems IGA 168x and CyberPro series.
     32   1.1     uwe  */
     33   1.1     uwe #include <sys/cdefs.h>
     34  1.11     uwe __KERNEL_RCSID(0, "$NetBSD: igsfb.c,v 1.11 2003/05/11 03:20:09 uwe Exp $");
     35   1.1     uwe 
     36   1.1     uwe #include <sys/param.h>
     37   1.1     uwe #include <sys/systm.h>
     38   1.1     uwe #include <sys/kernel.h>
     39   1.1     uwe #include <sys/device.h>
     40   1.1     uwe #include <sys/malloc.h>
     41   1.2     uwe #include <sys/ioctl.h>
     42   1.1     uwe #include <sys/buf.h>
     43   1.2     uwe #include <uvm/uvm_extern.h>
     44   1.1     uwe 
     45   1.1     uwe #include <machine/bus.h>
     46   1.1     uwe 
     47   1.1     uwe #include <dev/wscons/wsdisplayvar.h>
     48   1.8     uwe #include <dev/wscons/wsconsio.h>
     49   1.8     uwe #include <dev/wsfont/wsfont.h>
     50   1.1     uwe #include <dev/rasops/rasops.h>
     51   1.1     uwe 
     52   1.1     uwe #include <dev/ic/igsfbreg.h>
     53   1.1     uwe #include <dev/ic/igsfbvar.h>
     54   1.1     uwe 
     55   1.1     uwe 
     56   1.8     uwe struct igsfb_devconfig igsfb_console_dc;
     57   1.8     uwe 
     58   1.1     uwe /*
     59   1.1     uwe  * wsscreen
     60   1.1     uwe  */
     61   1.1     uwe 
     62   1.1     uwe /* filled from rasops_info in igsfb_common_init */
     63   1.1     uwe static struct wsscreen_descr igsfb_stdscreen = {
     64   1.1     uwe 	"std",			/* name */
     65   1.1     uwe 	0, 0,			/* ncols, nrows */
     66   1.1     uwe 	NULL,			/* textops */
     67   1.1     uwe 	0, 0,			/* fontwidth, fontheight */
     68   1.1     uwe 	0			/* capabilities */
     69   1.1     uwe };
     70   1.1     uwe 
     71   1.1     uwe static const struct wsscreen_descr *_igsfb_scrlist[] = {
     72   1.1     uwe 	&igsfb_stdscreen,
     73   1.1     uwe };
     74   1.1     uwe 
     75   1.1     uwe static const struct wsscreen_list igsfb_screenlist = {
     76   1.1     uwe 	sizeof(_igsfb_scrlist) / sizeof(struct wsscreen_descr *),
     77   1.1     uwe 	_igsfb_scrlist
     78   1.1     uwe };
     79   1.1     uwe 
     80   1.1     uwe 
     81   1.1     uwe /*
     82   1.1     uwe  * wsdisplay_accessops
     83   1.1     uwe  */
     84   1.1     uwe 
     85   1.1     uwe static int	igsfb_ioctl(void *, u_long, caddr_t, int, struct proc *);
     86   1.1     uwe static paddr_t	igsfb_mmap(void *, off_t, int);
     87   1.1     uwe 
     88   1.1     uwe static int	igsfb_alloc_screen(void *, const struct wsscreen_descr *,
     89   1.1     uwe 				   void **, int *, int *, long *);
     90   1.1     uwe static void	igsfb_free_screen(void *, void *);
     91   1.1     uwe static int	igsfb_show_screen(void *, void *, int,
     92   1.1     uwe 				  void (*) (void *, int, int), void *);
     93   1.1     uwe 
     94   1.1     uwe static const struct wsdisplay_accessops igsfb_accessops = {
     95   1.1     uwe 	igsfb_ioctl,
     96   1.1     uwe 	igsfb_mmap,
     97   1.1     uwe 	igsfb_alloc_screen,
     98   1.1     uwe 	igsfb_free_screen,
     99   1.1     uwe 	igsfb_show_screen,
    100  1.11     uwe 	NULL,			/* load_font */
    101  1.11     uwe 	NULL,			/* pollc */
    102  1.11     uwe 	NULL,			/* getwschar */
    103  1.11     uwe 	NULL			/* putwschar */
    104   1.1     uwe };
    105   1.1     uwe 
    106   1.1     uwe 
    107   1.1     uwe /*
    108  1.11     uwe  * acceleration
    109  1.11     uwe  */
    110  1.11     uwe static int	igsfb_make_text_cursor(struct igsfb_devconfig *);
    111  1.11     uwe static void	igsfb_accel_cursor(void *, int, int, int);
    112  1.11     uwe 
    113  1.11     uwe 
    114  1.11     uwe /*
    115   1.1     uwe  * internal functions
    116   1.1     uwe  */
    117   1.8     uwe static int	igsfb_init_video(struct igsfb_devconfig *);
    118   1.8     uwe static void	igsfb_init_cmap(struct igsfb_devconfig *);
    119   1.8     uwe static u_int16_t igsfb_spread_bits_8(u_int8_t);
    120  1.10     uwe static void	igsfb_init_bit_table(struct igsfb_devconfig *);
    121   1.8     uwe static void	igsfb_init_wsdisplay(struct igsfb_devconfig *);
    122   1.8     uwe 
    123   1.8     uwe static void	igsfb_blank_screen(struct igsfb_devconfig *, int);
    124   1.8     uwe static int	igsfb_get_cmap(struct igsfb_devconfig *,
    125   1.8     uwe 			       struct wsdisplay_cmap *);
    126   1.8     uwe static int	igsfb_set_cmap(struct igsfb_devconfig *,
    127   1.9     uwe 			       const struct wsdisplay_cmap *);
    128   1.8     uwe static void	igsfb_update_cmap(struct igsfb_devconfig *, u_int, u_int);
    129   1.8     uwe static void	igsfb_set_curpos(struct igsfb_devconfig *,
    130   1.9     uwe 				 const struct wsdisplay_curpos *);
    131   1.8     uwe static void	igsfb_update_curpos(struct igsfb_devconfig *);
    132   1.8     uwe static int	igsfb_get_cursor(struct igsfb_devconfig *,
    133   1.1     uwe 				 struct wsdisplay_cursor *);
    134   1.8     uwe static int	igsfb_set_cursor(struct igsfb_devconfig *,
    135   1.9     uwe 				 const struct wsdisplay_cursor *);
    136   1.8     uwe static void	igsfb_update_cursor(struct igsfb_devconfig *, u_int);
    137   1.8     uwe static void	igsfb_convert_cursor_data(struct igsfb_devconfig *,
    138   1.8     uwe 					  u_int, u_int);
    139   1.8     uwe 
    140   1.8     uwe 
    141   1.8     uwe int
    142   1.8     uwe igsfb_cnattach_subr(dc)
    143   1.8     uwe 	struct igsfb_devconfig *dc;
    144   1.8     uwe {
    145   1.8     uwe 	struct rasops_info *ri;
    146   1.8     uwe 	long defattr;
    147   1.1     uwe 
    148   1.8     uwe 	KASSERT(dc == &igsfb_console_dc);
    149   1.8     uwe 
    150   1.8     uwe 	igsfb_init_video(dc);
    151   1.8     uwe 	igsfb_init_wsdisplay(dc);
    152   1.1     uwe 
    153  1.11     uwe 	dc->dc_nscreens = 1;
    154  1.11     uwe 
    155   1.8     uwe 	ri = &dc->dc_ri;
    156   1.8     uwe 	(*ri->ri_ops.allocattr)(ri,
    157   1.8     uwe 				WSCOL_BLACK, /* fg */
    158   1.8     uwe 				WSCOL_BLACK, /* bg */
    159   1.8     uwe 				0,           /* wsattrs */
    160   1.8     uwe 				&defattr);
    161   1.8     uwe 
    162   1.8     uwe 	wsdisplay_cnattach(&igsfb_stdscreen,
    163   1.8     uwe 			   ri,   /* emulcookie */
    164   1.8     uwe 			   0, 0, /* cursor position */
    165   1.8     uwe 			   defattr);
    166   1.8     uwe 	return (0);
    167   1.8     uwe }
    168   1.1     uwe 
    169   1.1     uwe 
    170   1.1     uwe /*
    171   1.1     uwe  * Finish off the attach.  Bus specific attach method should have
    172   1.8     uwe  * enabled io and memory accesses and mapped io (and cop?) registers.
    173   1.1     uwe  */
    174   1.1     uwe void
    175   1.8     uwe igsfb_attach_subr(sc, isconsole)
    176   1.1     uwe 	struct igsfb_softc *sc;
    177   1.1     uwe 	int isconsole;
    178   1.1     uwe {
    179   1.8     uwe 	struct igsfb_devconfig *dc = sc->sc_dc;
    180   1.8     uwe 	struct wsemuldisplaydev_attach_args waa;
    181   1.8     uwe 
    182   1.8     uwe 	KASSERT(dc != NULL);
    183   1.8     uwe 
    184   1.8     uwe 	if (!isconsole) {
    185   1.8     uwe 		igsfb_init_video(dc);
    186   1.8     uwe 		igsfb_init_wsdisplay(dc);
    187   1.8     uwe 	}
    188   1.8     uwe 
    189   1.8     uwe 	printf("%s: %dMB, %s%dx%d, %dbpp\n",
    190   1.8     uwe 	       sc->sc_dev.dv_xname,
    191   1.8     uwe 	       (u_int32_t)(dc->dc_vmemsz >> 20),
    192   1.8     uwe 	       (dc->dc_hwflags & IGSFB_HW_BSWAP)
    193   1.8     uwe 		   ? (dc->dc_hwflags & IGSFB_HW_BE_SELECT)
    194   1.8     uwe 		       ? "hardware bswap, " : "software bswap, "
    195   1.8     uwe 		   : "",
    196   1.8     uwe 	       dc->dc_width, dc->dc_height, dc->dc_depth);
    197   1.8     uwe 
    198   1.8     uwe 	/* attach wsdisplay */
    199   1.8     uwe 	waa.console = isconsole;
    200   1.8     uwe 	waa.scrdata = &igsfb_screenlist;
    201   1.8     uwe 	waa.accessops = &igsfb_accessops;
    202   1.8     uwe 	waa.accesscookie = dc;
    203   1.8     uwe 
    204   1.8     uwe 	config_found(&sc->sc_dev, &waa, wsemuldisplaydevprint);
    205   1.8     uwe }
    206   1.8     uwe 
    207   1.8     uwe 
    208   1.8     uwe static int
    209   1.8     uwe igsfb_init_video(dc)
    210   1.8     uwe 	struct igsfb_devconfig *dc;
    211   1.8     uwe {
    212   1.4     uwe 	bus_space_handle_t tmph;
    213   1.4     uwe 	u_int8_t *p;
    214   1.4     uwe 	int need_bswap;
    215   1.8     uwe 	bus_addr_t fbaddr, craddr;
    216   1.1     uwe 	off_t croffset;
    217   1.1     uwe 	u_int8_t busctl, curctl;
    218   1.1     uwe 
    219   1.8     uwe 	/* Total amount of video memory. */
    220   1.8     uwe 	busctl = igs_ext_read(dc->dc_iot, dc->dc_ioh, IGS_EXT_BUS_CTL);
    221   1.1     uwe 	if (busctl & 0x2)
    222   1.8     uwe 		dc->dc_vmemsz = 4;
    223   1.1     uwe 	else if (busctl & 0x1)
    224   1.8     uwe 		dc->dc_vmemsz = 2;
    225   1.1     uwe 	else
    226   1.8     uwe 		dc->dc_vmemsz = 1;
    227   1.8     uwe 	dc->dc_vmemsz <<= 20;	/* megabytes -> bytes */
    228   1.4     uwe 
    229   1.4     uwe 	/*
    230   1.8     uwe 	 * Check for endianness mismatch by writing a word at the end of
    231   1.8     uwe 	 * the video memory (off-screen) and reading it back byte-by-byte.
    232   1.4     uwe 	 */
    233   1.8     uwe 	if (bus_space_map(dc->dc_memt,
    234   1.8     uwe 			  dc->dc_memaddr + dc->dc_vmemsz - sizeof(u_int32_t),
    235   1.4     uwe 			  sizeof(u_int32_t),
    236   1.8     uwe 			  dc->dc_memflags | BUS_SPACE_MAP_LINEAR,
    237   1.4     uwe 			  &tmph) != 0)
    238   1.4     uwe 	{
    239   1.4     uwe 		printf("unable to map video memory for endianness test\n");
    240   1.8     uwe 		return (1);
    241   1.4     uwe 	}
    242   1.4     uwe 
    243   1.8     uwe 	p = bus_space_vaddr(dc->dc_memt, tmph);
    244   1.4     uwe #if BYTE_ORDER == BIG_ENDIAN
    245   1.4     uwe 	*((u_int32_t *)p) = 0x12345678;
    246   1.4     uwe #else
    247   1.4     uwe 	*((u_int32_t *)p) = 0x78563412;
    248   1.4     uwe #endif
    249   1.4     uwe 	if (p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)
    250   1.4     uwe 		need_bswap = 0;
    251   1.4     uwe 	else
    252   1.4     uwe 		need_bswap = 1;
    253   1.4     uwe 
    254   1.8     uwe 	bus_space_unmap(dc->dc_memt, tmph, sizeof(u_int32_t));
    255   1.4     uwe 
    256   1.4     uwe 	/*
    257   1.4     uwe 	 * On CyberPro we can use magic bswap bit in linear address.
    258   1.4     uwe 	 */
    259   1.8     uwe 	fbaddr = dc->dc_memaddr;
    260   1.8     uwe 	if (need_bswap) {
    261   1.8     uwe 		dc->dc_hwflags |= IGSFB_HW_BSWAP;
    262   1.8     uwe 		if (dc->dc_id >= 0x2000) {
    263   1.8     uwe 			dc->dc_hwflags |= IGSFB_HW_BE_SELECT;
    264   1.4     uwe 			fbaddr |= IGS_MEM_BE_SELECT;
    265   1.4     uwe 		}
    266   1.8     uwe 	}
    267   1.8     uwe 
    268   1.8     uwe 	/*
    269   1.8     uwe 	 * XXX: TODO: make it possible to select the desired video mode.
    270   1.8     uwe 	 * For now - hardcode to 1024x768/8bpp.  This is what Krups OFW uses.
    271   1.8     uwe 	 */
    272   1.8     uwe 	igsfb_hw_setup(dc);
    273   1.8     uwe 
    274   1.8     uwe 	dc->dc_width = 1024;
    275   1.8     uwe 	dc->dc_height = 768;
    276   1.8     uwe 	dc->dc_depth = 8;
    277   1.1     uwe 
    278   1.1     uwe 	/*
    279   1.8     uwe 	 * Don't map in all N megs, just the amount we need for the wsscreen.
    280   1.1     uwe 	 */
    281   1.8     uwe 	dc->dc_fbsz = dc->dc_width * dc->dc_height; /* XXX: 8bpp specific */
    282   1.8     uwe 	if (bus_space_map(dc->dc_memt, fbaddr, dc->dc_fbsz,
    283   1.8     uwe 			  dc->dc_memflags | BUS_SPACE_MAP_LINEAR,
    284   1.8     uwe 			  &dc->dc_fbh) != 0)
    285   1.1     uwe 	{
    286   1.8     uwe 		bus_space_unmap(dc->dc_iot, dc->dc_ioh, IGS_REG_SIZE);
    287   1.1     uwe 		printf("unable to map framebuffer\n");
    288   1.8     uwe 		return (1);
    289   1.1     uwe 	}
    290   1.1     uwe 
    291   1.8     uwe 	igsfb_init_cmap(dc);
    292   1.8     uwe 
    293   1.1     uwe 	/*
    294   1.8     uwe 	 * 1KB for cursor sprite data at the very end of the video memory.
    295   1.1     uwe 	 */
    296   1.8     uwe 	croffset = dc->dc_vmemsz - IGS_CURSOR_DATA_SIZE;
    297   1.4     uwe 	craddr = fbaddr + croffset;
    298   1.8     uwe 	if (bus_space_map(dc->dc_memt, craddr, IGS_CURSOR_DATA_SIZE,
    299   1.8     uwe 			  dc->dc_memflags | BUS_SPACE_MAP_LINEAR,
    300   1.8     uwe 			  &dc->dc_crh) != 0)
    301   1.1     uwe 	{
    302   1.8     uwe 		bus_space_unmap(dc->dc_iot, dc->dc_ioh, IGS_REG_SIZE);
    303   1.8     uwe 		bus_space_unmap(dc->dc_memt, dc->dc_fbh, dc->dc_fbsz);
    304   1.1     uwe 		printf("unable to map cursor sprite region\n");
    305   1.8     uwe 		return (1);
    306   1.1     uwe 	}
    307   1.1     uwe 
    308   1.1     uwe 	/*
    309   1.8     uwe 	 * Tell the device where cursor sprite data are located in the
    310   1.8     uwe 	 * linear space (it takes data offset in 1KB units).
    311   1.1     uwe 	 */
    312   1.8     uwe 	croffset >>= 10;	/* bytes -> kilobytes */
    313   1.8     uwe 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
    314   1.1     uwe 		      IGS_EXT_SPRITE_DATA_LO, croffset & 0xff);
    315   1.8     uwe 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
    316   1.1     uwe 		      IGS_EXT_SPRITE_DATA_HI, (croffset >> 8) & 0xf);
    317   1.1     uwe 
    318  1.10     uwe 	/* init the bit expansion table for cursor sprite data conversion */
    319  1.10     uwe 	igsfb_init_bit_table(dc);
    320   1.8     uwe 
    321   1.8     uwe 	/* XXX: fill dc_cursor and use igsfb_update_cursor() instead? */
    322   1.8     uwe 	memset(&dc->dc_cursor, 0, sizeof(struct igs_hwcursor));
    323   1.8     uwe 	memset(bus_space_vaddr(dc->dc_memt, dc->dc_crh),
    324   1.8     uwe 	       /* transparent */ 0xaa, IGS_CURSOR_DATA_SIZE);
    325   1.1     uwe 
    326   1.8     uwe 	curctl = igs_ext_read(dc->dc_iot, dc->dc_ioh, IGS_EXT_SPRITE_CTL);
    327   1.1     uwe 	curctl |= IGS_EXT_SPRITE_64x64;
    328   1.1     uwe 	curctl &= ~IGS_EXT_SPRITE_VISIBLE;
    329   1.8     uwe 	igs_ext_write(dc->dc_iot, dc->dc_ioh, IGS_EXT_SPRITE_CTL, curctl);
    330   1.8     uwe 	dc->dc_curenb = 0;
    331   1.1     uwe 
    332   1.8     uwe 	/* make sure screen is not blanked */
    333   1.8     uwe 	dc->dc_blanked = 0;
    334   1.8     uwe 	igsfb_blank_screen(dc, dc->dc_blanked);
    335   1.1     uwe 
    336   1.8     uwe 	return (0);
    337   1.1     uwe }
    338   1.1     uwe 
    339   1.1     uwe 
    340   1.8     uwe static void
    341   1.8     uwe igsfb_init_cmap(dc)
    342   1.8     uwe 	struct igsfb_devconfig *dc;
    343   1.4     uwe {
    344   1.8     uwe 	bus_space_tag_t iot = dc->dc_iot;
    345   1.8     uwe 	bus_space_handle_t ioh = dc->dc_ioh;
    346   1.8     uwe 	const u_int8_t *p;
    347   1.8     uwe 	int i;
    348   1.4     uwe 
    349   1.8     uwe 	p = rasops_cmap;	/* "ANSI" color map */
    350   1.4     uwe 
    351   1.8     uwe 	/* init software copy */
    352   1.8     uwe 	for (i = 0; i < IGS_CMAP_SIZE; ++i, p += 3) {
    353   1.8     uwe 		dc->dc_cmap.r[i] = p[0];
    354   1.8     uwe 		dc->dc_cmap.g[i] = p[1];
    355   1.8     uwe 		dc->dc_cmap.b[i] = p[2];
    356   1.8     uwe 	}
    357   1.4     uwe 
    358   1.8     uwe 	/* propagate to the device */
    359   1.8     uwe 	igsfb_update_cmap(dc, 0, IGS_CMAP_SIZE);
    360   1.1     uwe 
    361  1.11     uwe 	/* set overscan color (XXX: use defattr's background?) */
    362   1.8     uwe 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_RED,   0);
    363   1.8     uwe 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_GREEN, 0);
    364   1.8     uwe 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_BLUE,  0);
    365   1.1     uwe }
    366   1.1     uwe 
    367   1.8     uwe 
    368   1.1     uwe static void
    369   1.8     uwe igsfb_init_wsdisplay(dc)
    370   1.8     uwe 	struct igsfb_devconfig *dc;
    371   1.1     uwe {
    372   1.8     uwe 	struct rasops_info *ri;
    373   1.1     uwe 	int wsfcookie;
    374   1.1     uwe 
    375   1.8     uwe 	ri = &dc->dc_ri;
    376   1.8     uwe 	ri->ri_hw = dc;
    377   1.1     uwe 
    378   1.1     uwe 	ri->ri_flg = RI_CENTER | RI_CLEAR;
    379   1.8     uwe 	if (IGSFB_HW_SOFT_BSWAP(dc))
    380   1.1     uwe 	    ri->ri_flg |= RI_BSWAP;
    381   1.1     uwe 
    382   1.8     uwe 	ri->ri_depth = dc->dc_depth;
    383   1.8     uwe 	ri->ri_width = dc->dc_width;
    384   1.8     uwe 	ri->ri_height = dc->dc_height;
    385   1.6     uwe 
    386   1.8     uwe 	ri->ri_stride = dc->dc_width; /* XXX: 8bpp specific */
    387   1.8     uwe 	ri->ri_bits = (u_char *)dc->dc_fbh;
    388   1.1     uwe 
    389   1.1     uwe 	/*
    390   1.1     uwe 	 * Initialize wsfont related stuff.
    391   1.1     uwe 	 */
    392   1.1     uwe 	wsfont_init();
    393   1.1     uwe 
    394   1.1     uwe 	/* prefer gallant that is identical to the one the prom uses */
    395   1.1     uwe 	wsfcookie = wsfont_find("Gallant", 12, 22, 0,
    396   1.1     uwe 				WSDISPLAY_FONTORDER_L2R,
    397   1.1     uwe 				WSDISPLAY_FONTORDER_L2R);
    398   1.1     uwe 	if (wsfcookie <= 0) {
    399   1.1     uwe #ifdef DIAGNOSTIC
    400   1.8     uwe 		printf("unable to find font Gallant 12x22\n");
    401   1.1     uwe #endif
    402   1.8     uwe 		wsfcookie = wsfont_find(NULL, 0, 0, 0, /* any font at all? */
    403   1.1     uwe 					WSDISPLAY_FONTORDER_L2R,
    404   1.1     uwe 					WSDISPLAY_FONTORDER_L2R);
    405   1.1     uwe 	}
    406   1.1     uwe 
    407   1.1     uwe 	if (wsfcookie <= 0) {
    408   1.8     uwe 		printf("unable to find any fonts\n");
    409   1.1     uwe 		return;
    410   1.1     uwe 	}
    411   1.1     uwe 
    412   1.1     uwe 	if (wsfont_lock(wsfcookie, &ri->ri_font) != 0) {
    413   1.8     uwe 		printf("unable to lock font\n");
    414   1.1     uwe 		return;
    415   1.1     uwe 	}
    416   1.1     uwe 	ri->ri_wsfcookie = wsfcookie;
    417   1.1     uwe 
    418   1.1     uwe 
    419  1.11     uwe 	/* XXX: TODO: compute term size based on font dimensions? */
    420   1.1     uwe 	rasops_init(ri, 34, 80);
    421   1.1     uwe 
    422  1.11     uwe 	/* use the sprite for the text mode cursor */
    423  1.11     uwe 	igsfb_make_text_cursor(dc);
    424  1.11     uwe 
    425  1.11     uwe 	/* the cursor is "busy" while we are in the text mode */
    426  1.11     uwe 	dc->dc_hwflags |= IGSFB_HW_TEXT_CURSOR;
    427  1.11     uwe 
    428  1.11     uwe 	/* propagate sprite data to the device */
    429  1.11     uwe 	igsfb_update_cursor(dc, WSDISPLAY_CURSOR_DOSHAPE);
    430  1.11     uwe 
    431  1.11     uwe 	/* override rasops default method */
    432  1.11     uwe 	ri->ri_ops.cursor = igsfb_accel_cursor;
    433  1.11     uwe 
    434   1.1     uwe 	igsfb_stdscreen.nrows = ri->ri_rows;
    435   1.1     uwe 	igsfb_stdscreen.ncols = ri->ri_cols;
    436   1.1     uwe 	igsfb_stdscreen.textops = &ri->ri_ops;
    437   1.1     uwe 	igsfb_stdscreen.capabilities = ri->ri_caps;
    438   1.1     uwe }
    439   1.1     uwe 
    440   1.1     uwe 
    441   1.1     uwe /*
    442  1.11     uwe  * Init cursor data in dc_cursor for the accelerated text cursor.
    443  1.11     uwe  */
    444  1.11     uwe static int
    445  1.11     uwe igsfb_make_text_cursor(dc)
    446  1.11     uwe 	struct igsfb_devconfig *dc;
    447  1.11     uwe {
    448  1.11     uwe 	struct wsdisplay_font *f = dc->dc_ri.ri_font;
    449  1.11     uwe 	u_int16_t cc_scan[8];	/* one sprite scanline */
    450  1.11     uwe 	u_int16_t s;
    451  1.11     uwe 	int w, i;
    452  1.11     uwe 
    453  1.11     uwe 	KASSERT(f->fontwidth <= IGS_CURSOR_MAX_SIZE);
    454  1.11     uwe 	KASSERT(f->fontheight <= IGS_CURSOR_MAX_SIZE);
    455  1.11     uwe 
    456  1.11     uwe 	w = f->fontwidth;
    457  1.11     uwe 	for (i = 0; i < f->stride; ++i) {
    458  1.11     uwe 		if (w >= 8) {
    459  1.11     uwe 			s = 0xffff; /* all inverted */
    460  1.11     uwe 			w -= 8;
    461  1.11     uwe 		} else {
    462  1.11     uwe 			/* first w pixels inverted, the rest is transparent */
    463  1.11     uwe 			s = ~(0x5555 << (w * 2));
    464  1.11     uwe 			if (IGSFB_HW_SOFT_BSWAP(dc))
    465  1.11     uwe 				s = bswap16(s);
    466  1.11     uwe 			w = 0;
    467  1.11     uwe 		}
    468  1.11     uwe 		cc_scan[i] = s;
    469  1.11     uwe 	}
    470  1.11     uwe 
    471  1.11     uwe 	dc->dc_cursor.cc_size.x = f->fontwidth;
    472  1.11     uwe 	dc->dc_cursor.cc_size.y = f->fontheight;
    473  1.11     uwe 
    474  1.11     uwe 	dc->dc_cursor.cc_hot.x = 0;
    475  1.11     uwe 	dc->dc_cursor.cc_hot.y = 0;
    476  1.11     uwe 
    477  1.11     uwe 	/* init sprite array to be all transparent */
    478  1.11     uwe 	memset(dc->dc_cursor.cc_sprite, 0xaa, IGS_CURSOR_DATA_SIZE);
    479  1.11     uwe 
    480  1.11     uwe 	for (i = 0; i < f->fontheight; ++i)
    481  1.11     uwe 		memcpy(&dc->dc_cursor.cc_sprite[i * 8],
    482  1.11     uwe 		       cc_scan, f->stride * sizeof(u_int16_t));
    483  1.11     uwe 
    484  1.11     uwe 	return (0);
    485  1.11     uwe }
    486  1.11     uwe 
    487  1.11     uwe 
    488  1.11     uwe /*
    489   1.8     uwe  * Spread a byte (abcd.efgh) into two (0a0b.0c0d 0e0f.0g0h).
    490  1.10     uwe  * Helper function for igsfb_init_bit_table().
    491   1.8     uwe  */
    492   1.8     uwe static u_int16_t
    493   1.8     uwe igsfb_spread_bits_8(b)
    494   1.8     uwe 	u_int8_t b;
    495   1.8     uwe {
    496   1.8     uwe 	u_int16_t s = b;
    497   1.8     uwe 
    498   1.8     uwe 	s = ((s & 0x00f0) << 4) | (s & 0x000f);
    499   1.8     uwe 	s = ((s & 0x0c0c) << 2) | (s & 0x0303);
    500   1.8     uwe 	s = ((s & 0x2222) << 1) | (s & 0x1111);
    501   1.8     uwe 	return (s);
    502   1.8     uwe }
    503   1.8     uwe 
    504   1.8     uwe 
    505   1.8     uwe /*
    506   1.8     uwe  * Cursor sprite data are in 2bpp.  Incoming image/mask are in 1bpp.
    507  1.10     uwe  * Prebuild the table to expand 1bpp->2bpp, with bswapping if neccessary.
    508   1.8     uwe  */
    509   1.8     uwe static void
    510  1.10     uwe igsfb_init_bit_table(dc)
    511   1.8     uwe 	struct igsfb_devconfig *dc;
    512   1.8     uwe {
    513  1.10     uwe 	u_int16_t *expand = dc->dc_bexpand;
    514   1.8     uwe 	int need_bswap = IGSFB_HW_SOFT_BSWAP(dc);
    515  1.10     uwe 	u_int16_t s;
    516   1.8     uwe 	u_int i;
    517   1.8     uwe 
    518   1.8     uwe 	for (i = 0; i < 256; ++i) {
    519  1.10     uwe 		s = igsfb_spread_bits_8(i);
    520  1.10     uwe 		expand[i] = need_bswap ? bswap16(s) : s;
    521   1.8     uwe 	}
    522   1.8     uwe }
    523   1.8     uwe 
    524   1.8     uwe 
    525   1.8     uwe /*
    526   1.1     uwe  * wsdisplay_accessops: mmap()
    527   1.4     uwe  *   XXX: allow mmapping i/o mapped i/o regs if INSECURE???
    528   1.1     uwe  */
    529   1.1     uwe static paddr_t
    530   1.1     uwe igsfb_mmap(v, offset, prot)
    531   1.1     uwe 	void *v;
    532   1.1     uwe 	off_t offset;
    533   1.1     uwe 	int prot;
    534   1.1     uwe {
    535   1.8     uwe 	struct igsfb_devconfig *dc = v;
    536   1.1     uwe 
    537   1.8     uwe 	if (offset >= dc->dc_memsz || offset < 0)
    538   1.1     uwe 		return (-1);
    539   1.1     uwe 
    540   1.8     uwe 	return (bus_space_mmap(dc->dc_memt, dc->dc_memaddr, offset, prot,
    541   1.8     uwe 			       dc->dc_memflags | BUS_SPACE_MAP_LINEAR));
    542   1.1     uwe }
    543   1.1     uwe 
    544   1.1     uwe 
    545   1.1     uwe /*
    546   1.1     uwe  * wsdisplay_accessops: ioctl()
    547   1.1     uwe  */
    548   1.1     uwe static int
    549   1.1     uwe igsfb_ioctl(v, cmd, data, flag, p)
    550   1.1     uwe 	void *v;
    551   1.1     uwe 	u_long cmd;
    552   1.1     uwe 	caddr_t data;
    553   1.1     uwe 	int flag;
    554   1.1     uwe 	struct proc *p;
    555   1.1     uwe {
    556   1.8     uwe 	struct igsfb_devconfig *dc = v;
    557   1.8     uwe 	struct rasops_info *ri;
    558  1.11     uwe 	int cursor_busy;
    559   1.1     uwe 	int turnoff;
    560   1.1     uwe 
    561  1.11     uwe 	/* don't permit cursor ioctls if we use sprite for text cursor */
    562  1.11     uwe 	cursor_busy = !dc->dc_mapped
    563  1.11     uwe 		&& (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR);
    564  1.11     uwe 
    565   1.1     uwe 	switch (cmd) {
    566   1.1     uwe 
    567   1.1     uwe 	case WSDISPLAYIO_GTYPE:
    568   1.1     uwe 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    569   1.1     uwe 		return (0);
    570   1.1     uwe 
    571   1.1     uwe 	case WSDISPLAYIO_GINFO:
    572   1.8     uwe 		ri = &dc->dc_ri;
    573   1.1     uwe #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    574   1.1     uwe 		wsd_fbip->height = ri->ri_height;
    575   1.1     uwe 		wsd_fbip->width = ri->ri_width;
    576   1.1     uwe 		wsd_fbip->depth = ri->ri_depth;
    577   1.1     uwe 		wsd_fbip->cmsize = IGS_CMAP_SIZE;
    578   1.1     uwe #undef wsd_fbip
    579   1.1     uwe 		return (0);
    580   1.1     uwe 
    581  1.11     uwe 	case WSDISPLAYIO_SMODE:
    582  1.11     uwe #define d (*(int *)data)
    583  1.11     uwe 		if (d == WSDISPLAYIO_MODE_MAPPED)
    584  1.11     uwe 			dc->dc_mapped = 1;
    585  1.11     uwe 		else {
    586  1.11     uwe 			dc->dc_mapped = 0;
    587  1.11     uwe 			/* reinit sprite for text cursor */
    588  1.11     uwe 			if (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR) {
    589  1.11     uwe 				igsfb_make_text_cursor(dc);
    590  1.11     uwe 				dc->dc_curenb = 0;
    591  1.11     uwe 				igsfb_update_cursor(dc,
    592  1.11     uwe 					  WSDISPLAY_CURSOR_DOSHAPE
    593  1.11     uwe 					| WSDISPLAY_CURSOR_DOCUR);
    594  1.11     uwe 			}
    595  1.11     uwe 		}
    596  1.11     uwe 		return (0);
    597  1.11     uwe 
    598   1.1     uwe 	case WSDISPLAYIO_GVIDEO:
    599   1.8     uwe 		*(u_int *)data = dc->dc_blanked ?
    600   1.1     uwe 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    601   1.1     uwe 		return (0);
    602   1.1     uwe 
    603   1.1     uwe 	case WSDISPLAYIO_SVIDEO:
    604   1.1     uwe 		turnoff = (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF);
    605   1.8     uwe 		if (dc->dc_blanked != turnoff) {
    606   1.8     uwe 			dc->dc_blanked = turnoff;
    607   1.8     uwe 			igsfb_blank_screen(dc, dc->dc_blanked);
    608   1.1     uwe 		}
    609   1.1     uwe 		return (0);
    610   1.1     uwe 
    611   1.1     uwe 	case WSDISPLAYIO_GETCMAP:
    612   1.8     uwe 		return (igsfb_get_cmap(dc, (struct wsdisplay_cmap *)data));
    613   1.1     uwe 
    614   1.1     uwe 	case WSDISPLAYIO_PUTCMAP:
    615   1.8     uwe 		return (igsfb_set_cmap(dc, (struct wsdisplay_cmap *)data));
    616   1.1     uwe 
    617   1.1     uwe 	case WSDISPLAYIO_GCURMAX:
    618   1.1     uwe 		((struct wsdisplay_curpos *)data)->x = IGS_CURSOR_MAX_SIZE;
    619   1.1     uwe 		((struct wsdisplay_curpos *)data)->y = IGS_CURSOR_MAX_SIZE;
    620   1.1     uwe 		return (0);
    621   1.1     uwe 
    622   1.1     uwe 	case WSDISPLAYIO_GCURPOS:
    623  1.11     uwe 		if (cursor_busy)
    624  1.11     uwe 			return (EBUSY);
    625   1.8     uwe 		*(struct wsdisplay_curpos *)data = dc->dc_cursor.cc_pos;
    626   1.1     uwe 		return (0);
    627   1.1     uwe 
    628   1.1     uwe 	case WSDISPLAYIO_SCURPOS:
    629  1.11     uwe 		if (cursor_busy)
    630  1.11     uwe 			return (EBUSY);
    631   1.8     uwe 		igsfb_set_curpos(dc, (struct wsdisplay_curpos *)data);
    632   1.1     uwe 		return (0);
    633   1.1     uwe 
    634   1.1     uwe 	case WSDISPLAYIO_GCURSOR:
    635  1.11     uwe 		if (cursor_busy)
    636  1.11     uwe 			return (EBUSY);
    637   1.8     uwe 		return (igsfb_get_cursor(dc, (struct wsdisplay_cursor *)data));
    638   1.1     uwe 
    639   1.1     uwe 	case WSDISPLAYIO_SCURSOR:
    640  1.11     uwe 		if (cursor_busy)
    641  1.11     uwe 			return (EBUSY);
    642   1.8     uwe 		return (igsfb_set_cursor(dc, (struct wsdisplay_cursor *)data));
    643   1.1     uwe 	}
    644   1.1     uwe 
    645   1.1     uwe 	return (EPASSTHROUGH);
    646   1.1     uwe }
    647   1.1     uwe 
    648   1.1     uwe 
    649   1.1     uwe /*
    650   1.1     uwe  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SVIDEO)
    651   1.1     uwe  */
    652   1.1     uwe static void
    653   1.8     uwe igsfb_blank_screen(dc, blank)
    654   1.8     uwe 	struct igsfb_devconfig *dc;
    655   1.1     uwe 	int blank;
    656   1.1     uwe {
    657   1.1     uwe 
    658   1.8     uwe 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
    659   1.1     uwe 		      IGS_EXT_SYNC_CTL,
    660   1.1     uwe 		      blank ? IGS_EXT_SYNC_H0 | IGS_EXT_SYNC_V0
    661   1.1     uwe 			    : 0);
    662   1.1     uwe }
    663   1.1     uwe 
    664   1.1     uwe 
    665   1.1     uwe /*
    666   1.1     uwe  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GETCMAP)
    667   1.8     uwe  *   Served from the software cmap copy.
    668   1.1     uwe  */
    669   1.1     uwe static int
    670   1.8     uwe igsfb_get_cmap(dc, p)
    671   1.8     uwe 	struct igsfb_devconfig *dc;
    672   1.1     uwe 	struct wsdisplay_cmap *p;
    673   1.1     uwe {
    674  1.11     uwe 	u_int index, count;
    675  1.11     uwe 	int err;
    676  1.11     uwe 
    677  1.11     uwe 	index = p->index;
    678  1.11     uwe 	count = p->count;
    679   1.1     uwe 
    680   1.5  itojun 	if (index >= IGS_CMAP_SIZE || count > IGS_CMAP_SIZE - index)
    681   1.1     uwe 		return (EINVAL);
    682   1.1     uwe 
    683  1.11     uwe 	err = copyout(&dc->dc_cmap.r[index], p->red, count);
    684  1.11     uwe 	if (err)
    685  1.11     uwe 		return (err);
    686  1.11     uwe 	err = copyout(&dc->dc_cmap.g[index], p->green, count);
    687  1.11     uwe 	if (err)
    688  1.11     uwe 		return (err);
    689  1.11     uwe 	err = copyout(&dc->dc_cmap.b[index], p->blue, count);
    690  1.11     uwe 	if (err)
    691  1.11     uwe 		return (err);
    692   1.1     uwe 
    693   1.1     uwe 	return (0);
    694   1.1     uwe }
    695   1.1     uwe 
    696   1.1     uwe 
    697   1.1     uwe /*
    698   1.1     uwe  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SETCMAP)
    699   1.8     uwe  *   Set the software cmap copy and propagate changed range to the device.
    700   1.1     uwe  */
    701   1.1     uwe static int
    702   1.8     uwe igsfb_set_cmap(dc, p)
    703   1.8     uwe 	struct igsfb_devconfig *dc;
    704   1.9     uwe 	const struct wsdisplay_cmap *p;
    705   1.1     uwe {
    706  1.11     uwe 	u_int index, count;
    707  1.11     uwe 
    708  1.11     uwe 	index = p->index;
    709  1.11     uwe 	count = p->count;
    710   1.1     uwe 
    711   1.5  itojun 	if (index >= IGS_CMAP_SIZE || count > IGS_CMAP_SIZE - index)
    712   1.1     uwe 		return (EINVAL);
    713   1.1     uwe 
    714   1.1     uwe 	if (!uvm_useracc(p->red, count, B_READ) ||
    715   1.1     uwe 	    !uvm_useracc(p->green, count, B_READ) ||
    716   1.1     uwe 	    !uvm_useracc(p->blue, count, B_READ))
    717   1.1     uwe 		return (EFAULT);
    718   1.1     uwe 
    719   1.8     uwe 	copyin(p->red, &dc->dc_cmap.r[index], count);
    720   1.8     uwe 	copyin(p->green, &dc->dc_cmap.g[index], count);
    721   1.8     uwe 	copyin(p->blue, &dc->dc_cmap.b[index], count);
    722   1.1     uwe 
    723  1.11     uwe 	/* propagate changes to the device */
    724  1.11     uwe 	igsfb_update_cmap(dc, index, count);
    725   1.1     uwe 
    726   1.1     uwe 	return (0);
    727   1.1     uwe }
    728   1.1     uwe 
    729   1.1     uwe 
    730   1.1     uwe /*
    731   1.8     uwe  * Propagate specified part of the software cmap copy to the device.
    732   1.1     uwe  */
    733   1.1     uwe static void
    734   1.8     uwe igsfb_update_cmap(dc, index, count)
    735   1.8     uwe 	struct igsfb_devconfig *dc;
    736   1.1     uwe 	u_int index, count;
    737   1.1     uwe {
    738   1.1     uwe 	bus_space_tag_t t;
    739   1.1     uwe 	bus_space_handle_t h;
    740   1.1     uwe 	u_int last, i;
    741   1.1     uwe 
    742   1.1     uwe 	if (index >= IGS_CMAP_SIZE)
    743   1.1     uwe 		return;
    744   1.1     uwe 
    745   1.1     uwe 	last = index + count;
    746   1.1     uwe 	if (last > IGS_CMAP_SIZE)
    747   1.1     uwe 		last = IGS_CMAP_SIZE;
    748   1.1     uwe 
    749   1.8     uwe 	t = dc->dc_iot;
    750   1.8     uwe 	h = dc->dc_ioh;
    751   1.1     uwe 
    752   1.1     uwe 	/* start palette writing, index is autoincremented by hardware */
    753   1.1     uwe 	bus_space_write_1(t, h, IGS_DAC_PEL_WRITE_IDX, index);
    754   1.1     uwe 
    755   1.1     uwe 	for (i = index; i < last; ++i) {
    756   1.8     uwe 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.r[i]);
    757   1.8     uwe 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.g[i]);
    758   1.8     uwe 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.b[i]);
    759   1.1     uwe 	}
    760   1.1     uwe }
    761   1.1     uwe 
    762   1.1     uwe 
    763   1.1     uwe /*
    764   1.1     uwe  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURPOS)
    765   1.1     uwe  */
    766   1.1     uwe static void
    767   1.8     uwe igsfb_set_curpos(dc, curpos)
    768   1.8     uwe 	struct igsfb_devconfig *dc;
    769   1.9     uwe 	const struct wsdisplay_curpos *curpos;
    770   1.1     uwe {
    771   1.8     uwe 	struct rasops_info *ri = &dc->dc_ri;
    772   1.9     uwe 	u_int x = curpos->x, y = curpos->y;
    773   1.9     uwe 
    774   1.9     uwe 	if (x >= ri->ri_width)
    775   1.9     uwe 		x = ri->ri_width - 1;
    776   1.9     uwe 	if (y >= ri->ri_height)
    777   1.9     uwe 		y = ri->ri_height - 1;
    778   1.1     uwe 
    779   1.8     uwe 	dc->dc_cursor.cc_pos.x = x;
    780   1.8     uwe 	dc->dc_cursor.cc_pos.y = y;
    781   1.1     uwe 
    782  1.11     uwe 	/* propagate changes to the device */
    783   1.8     uwe 	igsfb_update_curpos(dc);
    784   1.1     uwe }
    785   1.1     uwe 
    786   1.1     uwe 
    787   1.1     uwe static void
    788   1.8     uwe igsfb_update_curpos(dc)
    789   1.8     uwe 	struct igsfb_devconfig *dc;
    790   1.1     uwe {
    791   1.1     uwe 	bus_space_tag_t t;
    792   1.1     uwe 	bus_space_handle_t h;
    793   1.1     uwe 	int x, xoff, y, yoff;
    794   1.1     uwe 
    795   1.1     uwe 	xoff = 0;
    796   1.8     uwe 	x = dc->dc_cursor.cc_pos.x - dc->dc_cursor.cc_hot.x;
    797   1.1     uwe 	if (x < 0) {
    798   1.8     uwe 		xoff = -x;
    799   1.1     uwe 		x = 0;
    800   1.1     uwe 	}
    801   1.1     uwe 
    802   1.1     uwe 	yoff = 0;
    803   1.8     uwe 	y = dc->dc_cursor.cc_pos.y - dc->dc_cursor.cc_hot.y;
    804   1.1     uwe 	if (y < 0) {
    805   1.8     uwe 		yoff = -y;
    806   1.1     uwe 		y = 0;
    807   1.1     uwe 	}
    808   1.1     uwe 
    809   1.8     uwe 	t = dc->dc_iot;
    810   1.8     uwe 	h = dc->dc_ioh;
    811   1.1     uwe 
    812   1.1     uwe 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_LO, x & 0xff);
    813   1.1     uwe 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_HI, (x >> 8) & 0x07);
    814   1.1     uwe 	igs_ext_write(t, h, IGS_EXT_SPRITE_HPRESET, xoff & 0x3f);
    815   1.1     uwe 
    816   1.1     uwe 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_LO, y & 0xff);
    817   1.1     uwe 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_HI, (y >> 8) & 0x07);
    818   1.1     uwe 	igs_ext_write(t, h, IGS_EXT_SPRITE_VPRESET, yoff & 0x3f);
    819   1.1     uwe }
    820   1.1     uwe 
    821   1.1     uwe 
    822   1.1     uwe /*
    823   1.1     uwe  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GCURSOR)
    824   1.1     uwe  */
    825   1.1     uwe static int
    826   1.8     uwe igsfb_get_cursor(dc, p)
    827   1.8     uwe 	struct igsfb_devconfig *dc;
    828   1.1     uwe 	struct wsdisplay_cursor *p;
    829   1.1     uwe {
    830   1.1     uwe 
    831   1.1     uwe 	/* XXX: TODO */
    832   1.1     uwe 	return (0);
    833   1.1     uwe }
    834   1.1     uwe 
    835   1.1     uwe 
    836   1.1     uwe /*
    837   1.1     uwe  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURSOR)
    838   1.1     uwe  */
    839   1.1     uwe static int
    840   1.8     uwe igsfb_set_cursor(dc, p)
    841   1.8     uwe 	struct igsfb_devconfig *dc;
    842   1.9     uwe 	const struct wsdisplay_cursor *p;
    843   1.1     uwe {
    844   1.1     uwe 	struct igs_hwcursor *cc;
    845   1.9     uwe 	struct wsdisplay_curpos pos, hot;
    846   1.1     uwe 	u_int v, index, count, icount, iwidth;
    847   1.1     uwe 
    848   1.8     uwe 	cc = &dc->dc_cursor;
    849   1.1     uwe 	v = p->which;
    850   1.1     uwe 
    851   1.9     uwe 	/* verify that the new cursor colormap is valid */
    852   1.1     uwe 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    853   1.1     uwe 		index = p->cmap.index;
    854   1.1     uwe 		count = p->cmap.count;
    855   1.1     uwe 		if (index >= 2 || (index + count) > 2)
    856   1.1     uwe 			return (EINVAL);
    857   1.9     uwe 
    858   1.1     uwe 		if (!uvm_useracc(p->cmap.red, count, B_READ)
    859   1.1     uwe 		    || !uvm_useracc(p->cmap.green, count, B_READ)
    860   1.1     uwe 		    || !uvm_useracc(p->cmap.blue, count, B_READ))
    861   1.1     uwe 			return (EFAULT);
    862   1.1     uwe 	}
    863   1.1     uwe 
    864   1.9     uwe 	/* verify that the new cursor data are valid */
    865   1.1     uwe 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    866   1.1     uwe 		if (p->size.x > IGS_CURSOR_MAX_SIZE
    867   1.1     uwe 		    || p->size.y > IGS_CURSOR_MAX_SIZE)
    868   1.1     uwe 			return (EINVAL);
    869   1.1     uwe 
    870   1.1     uwe 		iwidth = (p->size.x + 7) >> 3; /* bytes per scan line */
    871   1.1     uwe 		icount = iwidth * p->size.y;
    872   1.1     uwe 		if (!uvm_useracc(p->image, icount, B_READ)
    873   1.1     uwe 		    || !uvm_useracc(p->mask, icount, B_READ))
    874   1.1     uwe 			return (EFAULT);
    875   1.1     uwe 	}
    876   1.1     uwe 
    877   1.9     uwe 	/* enforce that the position is within screen bounds */
    878   1.9     uwe 	if (v & WSDISPLAY_CURSOR_DOPOS) {
    879   1.9     uwe 		struct rasops_info *ri = &dc->dc_ri;
    880   1.9     uwe 
    881   1.9     uwe 		pos = p->pos;	/* local copy we can write to */
    882   1.9     uwe 		if (pos.x >= ri->ri_width)
    883   1.9     uwe 			pos.x = ri->ri_width - 1;
    884   1.9     uwe 		if (pos.y >= ri->ri_height)
    885   1.9     uwe 			pos.y = ri->ri_height - 1;
    886   1.9     uwe 	}
    887   1.9     uwe 
    888   1.9     uwe 	/* enforce that the hot spot is within sprite bounds */
    889   1.9     uwe 	if (v & WSDISPLAY_CURSOR_DOHOT)
    890   1.9     uwe 		hot = p->hot;	/* local copy we can write to */
    891   1.9     uwe 
    892   1.9     uwe 	if (v & (WSDISPLAY_CURSOR_DOHOT | WSDISPLAY_CURSOR_DOSHAPE)) {
    893   1.9     uwe 		const struct wsdisplay_curpos *nsize;
    894   1.9     uwe 		struct wsdisplay_curpos *nhot;
    895   1.1     uwe 
    896   1.9     uwe 		nsize = (v & WSDISPLAY_CURSOR_DOSHAPE) ?
    897   1.9     uwe 			    &p->size : &cc->cc_size;
    898   1.9     uwe 		nhot = (v & WSDISPLAY_CURSOR_DOHOT) ? &hot : &cc->cc_hot;
    899   1.1     uwe 
    900   1.9     uwe 		if (nhot->x >= nsize->x)
    901   1.9     uwe 			nhot->x = nsize->x - 1;
    902   1.9     uwe 		if (nhot->y >= nsize->y)
    903   1.9     uwe 			nhot->y = nsize->y - 1;
    904   1.9     uwe 	}
    905   1.9     uwe 
    906   1.9     uwe 
    907   1.9     uwe 	/* arguments verified, copy data to the driver's cursor info */
    908   1.1     uwe 	if (v & WSDISPLAY_CURSOR_DOCUR)
    909   1.8     uwe 		dc->dc_curenb = p->enable;
    910   1.1     uwe 
    911   1.1     uwe 	if (v & WSDISPLAY_CURSOR_DOPOS)
    912   1.9     uwe 		cc->cc_pos = pos; /* local copy, possibly corrected */
    913   1.1     uwe 
    914   1.1     uwe 	if (v & WSDISPLAY_CURSOR_DOHOT)
    915   1.9     uwe 		cc->cc_hot = hot; /* local copy, possibly corrected */
    916   1.1     uwe 
    917   1.1     uwe 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    918   1.1     uwe 		copyin(p->cmap.red, &cc->cc_color[index], count);
    919   1.1     uwe 		copyin(p->cmap.green, &cc->cc_color[index + 2], count);
    920   1.1     uwe 		copyin(p->cmap.blue, &cc->cc_color[index + 4], count);
    921   1.1     uwe 	}
    922   1.1     uwe 
    923   1.1     uwe 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    924   1.1     uwe 		u_int trailing_bits;
    925   1.1     uwe 
    926   1.1     uwe 		copyin(p->image, cc->cc_image, icount);
    927   1.1     uwe 		copyin(p->mask, cc->cc_mask, icount);
    928   1.1     uwe 		cc->cc_size = p->size;
    929   1.1     uwe 
    930   1.1     uwe 		/* clear trailing bits in the "partial" mask bytes */
    931   1.1     uwe 		trailing_bits = p->size.x & 0x07;
    932   1.1     uwe 		if (trailing_bits != 0) {
    933   1.1     uwe 			const u_int cutmask = ~((~0) << trailing_bits);
    934   1.1     uwe 			u_char *mp;
    935   1.1     uwe 			u_int i;
    936   1.1     uwe 
    937   1.1     uwe 			mp = cc->cc_mask + iwidth - 1;
    938   1.1     uwe 			for (i = 0; i < p->size.y; ++i) {
    939   1.1     uwe 				*mp &= cutmask;
    940   1.1     uwe 				mp += iwidth;
    941   1.1     uwe 			}
    942   1.1     uwe 		}
    943   1.8     uwe 		igsfb_convert_cursor_data(dc, iwidth, p->size.y);
    944   1.1     uwe 	}
    945   1.1     uwe 
    946   1.9     uwe 	/* propagate changes to the device */
    947   1.8     uwe 	igsfb_update_cursor(dc, v);
    948   1.9     uwe 
    949   1.1     uwe 	return (0);
    950   1.1     uwe }
    951   1.1     uwe 
    952   1.4     uwe 
    953   1.1     uwe /*
    954   1.1     uwe  * Convert incoming 1bpp cursor image/mask into native 2bpp format.
    955   1.1     uwe  */
    956   1.1     uwe static void
    957  1.10     uwe igsfb_convert_cursor_data(dc, width, height)
    958   1.8     uwe 	struct igsfb_devconfig *dc;
    959  1.10     uwe 	u_int width, height;
    960   1.1     uwe {
    961   1.8     uwe 	struct igs_hwcursor *cc = &dc->dc_cursor;
    962  1.10     uwe 	u_int16_t *expand = dc->dc_bexpand;
    963   1.1     uwe 	u_int8_t *ip, *mp;
    964  1.10     uwe 	u_int16_t is, ms, *dp;
    965   1.1     uwe 	u_int line, i;
    966   1.1     uwe 
    967   1.1     uwe 	/* init sprite to be all transparent */
    968   1.1     uwe 	memset(cc->cc_sprite, 0xaa, IGS_CURSOR_DATA_SIZE);
    969   1.1     uwe 
    970   1.1     uwe 	/* first scanline */
    971   1.1     uwe 	ip = cc->cc_image;
    972   1.1     uwe 	mp = cc->cc_mask;
    973   1.1     uwe 	dp = cc->cc_sprite;
    974   1.1     uwe 
    975  1.10     uwe 	for (line = 0; line < height; ++line) {
    976  1.10     uwe 		for (i = 0; i < width; ++i) {
    977  1.10     uwe 			is = expand[ip[i]]; /* image: 0 -> 00, 1 -> 01 */
    978  1.10     uwe 			ms = expand[mp[i]]; /*  mask: 0 -> 00, 1 -> 11 */
    979   1.8     uwe 			ms |= (ms << 1);
    980   1.1     uwe 			dp[i] = (0xaaaa & ~ms) | (is & ms);
    981   1.1     uwe 		}
    982   1.1     uwe 
    983   1.1     uwe 		/* next scanline */
    984  1.10     uwe 		ip += width;
    985  1.10     uwe 		mp += width;
    986  1.10     uwe 		dp += 8; /* 64 pixels, 2bpp, 8 pixels per short = 8 shorts */
    987   1.1     uwe 	}
    988   1.1     uwe }
    989   1.1     uwe 
    990   1.1     uwe 
    991   1.1     uwe /*
    992   1.8     uwe  * Propagate cursor changes to the device.
    993   1.8     uwe  * "which" is composed of WSDISPLAY_CURSOR_DO* bits.
    994   1.1     uwe  */
    995   1.1     uwe static void
    996   1.8     uwe igsfb_update_cursor(dc, which)
    997   1.8     uwe 	struct igsfb_devconfig *dc;
    998   1.1     uwe 	u_int which;
    999   1.1     uwe {
   1000   1.8     uwe 	bus_space_tag_t iot = dc->dc_iot;
   1001   1.8     uwe 	bus_space_handle_t ioh = dc->dc_ioh;
   1002   1.1     uwe 	u_int8_t curctl;
   1003   1.1     uwe 
   1004   1.1     uwe 	/*
   1005   1.1     uwe 	 * We will need to tweak sprite control register for cursor
   1006   1.1     uwe 	 * visibility and cursor color map manipualtion.
   1007   1.1     uwe 	 */
   1008   1.1     uwe 	if (which & (WSDISPLAY_CURSOR_DOCUR | WSDISPLAY_CURSOR_DOCMAP)) {
   1009   1.1     uwe 		curctl = igs_ext_read(iot, ioh, IGS_EXT_SPRITE_CTL);
   1010   1.1     uwe 	}
   1011   1.1     uwe 
   1012   1.1     uwe 	if (which & WSDISPLAY_CURSOR_DOSHAPE) {
   1013   1.8     uwe 		u_int8_t *dst = bus_space_vaddr(dc->dc_memt, dc->dc_crh);
   1014   1.1     uwe 
   1015   1.1     uwe 		/*
   1016   1.1     uwe 		 * memcpy between spaces of different endianness would
   1017   1.8     uwe 		 * be ... special.  We cannot be sure if memcpy gonna
   1018   1.1     uwe 		 * push data in 4-byte chunks, we can't pre-bswap it,
   1019   1.1     uwe 		 * so do it byte-by-byte to preserve byte ordering.
   1020   1.1     uwe 		 */
   1021   1.8     uwe 		if (IGSFB_HW_SOFT_BSWAP(dc)) {
   1022   1.8     uwe 			u_int8_t *src = (u_int8_t *)dc->dc_cursor.cc_sprite;
   1023   1.1     uwe 			int i;
   1024   1.1     uwe 
   1025   1.8     uwe 			for (i = 0; i < IGS_CURSOR_DATA_SIZE; ++i)
   1026   1.1     uwe 				*dst++ = *src++;
   1027   1.1     uwe 		} else {
   1028   1.8     uwe 			memcpy(dst, dc->dc_cursor.cc_sprite,
   1029   1.8     uwe 			       IGS_CURSOR_DATA_SIZE);
   1030   1.1     uwe 		}
   1031   1.1     uwe 	}
   1032   1.1     uwe 
   1033   1.1     uwe 	if (which & (WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOHOT)) {
   1034   1.1     uwe 		/* code shared with WSDISPLAYIO_SCURPOS */
   1035   1.8     uwe 		igsfb_update_curpos(dc);
   1036   1.1     uwe 	}
   1037   1.1     uwe 
   1038   1.1     uwe 	if (which & WSDISPLAY_CURSOR_DOCMAP) {
   1039   1.1     uwe 		u_int8_t *p;
   1040   1.1     uwe 
   1041   1.1     uwe 		/* tell DAC we want access to the cursor palette */
   1042   1.1     uwe 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
   1043   1.4     uwe 			      curctl | IGS_EXT_SPRITE_DAC_PEL);
   1044   1.1     uwe 
   1045   1.8     uwe 		p = dc->dc_cursor.cc_color;
   1046   1.1     uwe 
   1047   1.1     uwe 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 0);
   1048   1.1     uwe 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[0]);
   1049   1.1     uwe 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[2]);
   1050   1.1     uwe 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[4]);
   1051   1.1     uwe 
   1052   1.1     uwe 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 1);
   1053   1.1     uwe 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[1]);
   1054   1.1     uwe 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[3]);
   1055   1.1     uwe 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[5]);
   1056   1.1     uwe 
   1057   1.8     uwe 		/* restore access to the normal palette */
   1058   1.1     uwe 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL, curctl);
   1059   1.1     uwe 	}
   1060   1.1     uwe 
   1061   1.1     uwe 	if (which & WSDISPLAY_CURSOR_DOCUR) {
   1062   1.1     uwe 		if ((curctl & IGS_EXT_SPRITE_VISIBLE) == 0
   1063   1.8     uwe 		    && dc->dc_curenb)
   1064   1.1     uwe 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
   1065   1.1     uwe 				      curctl | IGS_EXT_SPRITE_VISIBLE);
   1066   1.1     uwe 		else if ((curctl & IGS_EXT_SPRITE_VISIBLE) != 0
   1067   1.8     uwe 			 && !dc->dc_curenb)
   1068   1.1     uwe 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
   1069   1.1     uwe 				      curctl & ~IGS_EXT_SPRITE_VISIBLE);
   1070   1.1     uwe 	}
   1071   1.1     uwe }
   1072   1.1     uwe 
   1073   1.1     uwe 
   1074   1.1     uwe /*
   1075   1.1     uwe  * wsdisplay_accessops: alloc_screen()
   1076   1.1     uwe  */
   1077   1.1     uwe static int
   1078   1.1     uwe igsfb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
   1079   1.1     uwe 	void *v;
   1080   1.1     uwe 	const struct wsscreen_descr *type;
   1081   1.1     uwe 	void **cookiep;
   1082   1.1     uwe 	int *curxp, *curyp;
   1083   1.1     uwe 	long *attrp;
   1084   1.1     uwe {
   1085   1.8     uwe 	struct igsfb_devconfig *dc = v;
   1086   1.8     uwe 	struct rasops_info *ri = &dc->dc_ri;
   1087   1.7     uwe 
   1088   1.8     uwe 	if (dc->dc_nscreens > 0) /* only do single screen for now */
   1089   1.7     uwe 		return (ENOMEM);
   1090   1.1     uwe 
   1091   1.8     uwe 	dc->dc_nscreens = 1;
   1092   1.7     uwe 
   1093   1.8     uwe 	*cookiep = ri;		/* emulcookie for igsgfb_stdscreen.textops */
   1094   1.8     uwe 	*curxp = *curyp = 0;	/* cursor position */
   1095   1.8     uwe 	(*ri->ri_ops.allocattr)(ri,
   1096   1.8     uwe 				WSCOL_BLACK, /* fg */
   1097   1.8     uwe 				WSCOL_BLACK, /* bg */
   1098   1.8     uwe 				0,           /* wsattr */
   1099   1.8     uwe 				attrp);
   1100   1.7     uwe 	return (0);
   1101   1.1     uwe }
   1102   1.1     uwe 
   1103   1.1     uwe 
   1104   1.1     uwe /*
   1105   1.1     uwe  * wsdisplay_accessops: free_screen()
   1106   1.1     uwe  */
   1107   1.1     uwe static void
   1108   1.1     uwe igsfb_free_screen(v, cookie)
   1109   1.1     uwe 	void *v;
   1110   1.1     uwe 	void *cookie;
   1111   1.1     uwe {
   1112   1.1     uwe 
   1113   1.8     uwe 	/* XXX */
   1114   1.1     uwe 	return;
   1115   1.1     uwe }
   1116   1.1     uwe 
   1117   1.1     uwe 
   1118   1.1     uwe /*
   1119   1.1     uwe  * wsdisplay_accessops: show_screen()
   1120   1.1     uwe  */
   1121   1.1     uwe static int
   1122   1.1     uwe igsfb_show_screen(v, cookie, waitok, cb, cbarg)
   1123   1.1     uwe 	void *v;
   1124   1.1     uwe 	void *cookie;
   1125   1.1     uwe 	int waitok;
   1126   1.1     uwe 	void (*cb)(void *, int, int);
   1127   1.1     uwe 	void *cbarg;
   1128   1.1     uwe {
   1129   1.1     uwe 
   1130   1.8     uwe 	/* XXX */
   1131   1.1     uwe 	return (0);
   1132  1.11     uwe }
   1133  1.11     uwe 
   1134  1.11     uwe 
   1135  1.11     uwe 
   1136  1.11     uwe /*
   1137  1.11     uwe  * Accelerated text mode cursor that uses hardware sprite.
   1138  1.11     uwe  */
   1139  1.11     uwe static void
   1140  1.11     uwe igsfb_accel_cursor(cookie, on, row, col)
   1141  1.11     uwe 	void *cookie;
   1142  1.11     uwe 	int on, row, col;
   1143  1.11     uwe {
   1144  1.11     uwe 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1145  1.11     uwe 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)ri->ri_hw;
   1146  1.11     uwe 	struct igs_hwcursor *cc = &dc->dc_cursor;
   1147  1.11     uwe 	u_int which;
   1148  1.11     uwe 
   1149  1.11     uwe 	ri->ri_crow = row;
   1150  1.11     uwe 	ri->ri_ccol = col;
   1151  1.11     uwe 
   1152  1.11     uwe 	which = 0;
   1153  1.11     uwe 	if (on) {
   1154  1.11     uwe 		ri->ri_flg |= RI_CURSOR;
   1155  1.11     uwe 
   1156  1.11     uwe 		/* only bother to move the cursor if it's visibile */
   1157  1.11     uwe 		cc->cc_pos.x = ri->ri_xorigin
   1158  1.11     uwe 			+ ri->ri_ccol * ri->ri_font->fontwidth;
   1159  1.11     uwe 		cc->cc_pos.y = ri->ri_yorigin
   1160  1.11     uwe 			+ ri->ri_crow * ri->ri_font->fontheight;
   1161  1.11     uwe 		which |= WSDISPLAY_CURSOR_DOPOS;
   1162  1.11     uwe 
   1163  1.11     uwe 	} else
   1164  1.11     uwe 		ri->ri_flg &= ~RI_CURSOR;
   1165  1.11     uwe 
   1166  1.11     uwe 	if (dc->dc_curenb != on) {
   1167  1.11     uwe 		dc->dc_curenb = on;
   1168  1.11     uwe 		which |= WSDISPLAY_CURSOR_DOCUR;
   1169  1.11     uwe 	}
   1170  1.11     uwe 
   1171  1.11     uwe 	/* propagate changes to the device */
   1172  1.11     uwe 	igsfb_update_cursor(dc, which);
   1173   1.1     uwe }
   1174