Home | History | Annotate | Line # | Download | only in ic
      1  1.15   thorpej /*	$NetBSD: ct65550.c,v 1.15 2021/08/07 16:19:12 thorpej Exp $	*/
      2   1.1  macallan 
      3   1.1  macallan /*
      4   1.1  macallan  * Copyright (c) 2006 Michael Lorenz
      5   1.1  macallan  * All rights reserved.
      6   1.1  macallan  *
      7   1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8   1.1  macallan  * modification, are permitted provided that the following conditions
      9   1.1  macallan  * are met:
     10   1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11   1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12   1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15   1.1  macallan  *
     16   1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  macallan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  macallan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  macallan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  macallan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1  macallan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1  macallan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1  macallan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1  macallan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1  macallan  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1  macallan  */
     27   1.1  macallan 
     28   1.1  macallan /*
     29   1.1  macallan  * A console driver for Chips & Technologies 65550 graphics controllers
     30   1.1  macallan  */
     31   1.1  macallan 
     32   1.1  macallan #include <sys/cdefs.h>
     33  1.15   thorpej __KERNEL_RCSID(0, "$NetBSD: ct65550.c,v 1.15 2021/08/07 16:19:12 thorpej Exp $");
     34   1.1  macallan 
     35   1.1  macallan #include <sys/param.h>
     36   1.1  macallan #include <sys/systm.h>
     37   1.1  macallan #include <sys/kernel.h>
     38   1.1  macallan #include <sys/device.h>
     39   1.1  macallan #include <sys/kauth.h>
     40   1.1  macallan #include <sys/bus.h>
     41   1.1  macallan #include <dev/videomode/videomode.h>
     42   1.1  macallan 
     43   1.1  macallan #include <dev/ic/ct65550reg.h>
     44   1.1  macallan #include <dev/ic/ct65550var.h>
     45   1.1  macallan 
     46   1.1  macallan #include "opt_wsemul.h"
     47   1.1  macallan #include "opt_chipsfb.h"
     48   1.1  macallan 
     49   1.1  macallan static struct vcons_screen chipsfb_console_screen;
     50   1.1  macallan 
     51   1.1  macallan extern const u_char rasops_cmap[768];
     52   1.1  macallan 
     53   1.1  macallan static void 	chipsfb_init(struct chipsfb_softc *);
     54   1.1  macallan 
     55   1.1  macallan static void	chipsfb_cursor(void *, int, int, int);
     56   1.1  macallan static void	chipsfb_copycols(void *, int, int, int, int);
     57   1.1  macallan static void	chipsfb_erasecols(void *, int, int, int, long);
     58   1.1  macallan static void	chipsfb_copyrows(void *, int, int, int);
     59   1.1  macallan static void	chipsfb_eraserows(void *, int, int, long);
     60   1.1  macallan 
     61   1.1  macallan #if 0
     62   1.1  macallan static int	chipsfb_allocattr(void *, int, int, int, long *);
     63   1.1  macallan static void	chipsfb_scroll(void *, void *, int);
     64   1.1  macallan static int	chipsfb_load_font(void *, void *, struct wsdisplay_font *);
     65   1.1  macallan #endif
     66   1.1  macallan 
     67   1.1  macallan static int	chipsfb_putcmap(struct chipsfb_softc *,
     68   1.1  macallan 			    struct wsdisplay_cmap *);
     69   1.1  macallan static int 	chipsfb_getcmap(struct chipsfb_softc *,
     70   1.1  macallan 			    struct wsdisplay_cmap *);
     71   1.1  macallan static int 	chipsfb_putpalreg(struct chipsfb_softc *, uint8_t, uint8_t,
     72   1.1  macallan 			    uint8_t, uint8_t);
     73   1.1  macallan 
     74  1.12  macallan static void	chipsfb_bitblt(void *, int, int, int, int,
     75  1.12  macallan 			    int, int, int);
     76   1.1  macallan static void	chipsfb_rectfill(struct chipsfb_softc *, int, int, int, int,
     77   1.1  macallan 			    int);
     78   1.1  macallan static void	chipsfb_putchar(void *, int, int, u_int, long);
     79  1.12  macallan static void	chipsfb_putchar_aa(void *, int, int, u_int, long);
     80   1.1  macallan static void	chipsfb_setup_mono(struct chipsfb_softc *, int, int, int,
     81   1.1  macallan 			    int, uint32_t, uint32_t);
     82   1.1  macallan static void	chipsfb_feed(struct chipsfb_softc *, int, uint8_t *);
     83   1.1  macallan 
     84   1.1  macallan #if 0
     85   1.1  macallan static void	chipsfb_showpal(struct chipsfb_softc *);
     86   1.1  macallan #endif
     87   1.1  macallan static void	chipsfb_restore_palette(struct chipsfb_softc *);
     88   1.1  macallan 
     89   1.1  macallan static void	chipsfb_wait_idle(struct chipsfb_softc *);
     90   1.1  macallan 
     91   1.1  macallan struct wsscreen_descr chipsfb_defaultscreen = {
     92   1.1  macallan 	"default",	/* name */
     93   1.1  macallan 	0, 0,		/* ncols, nrows */
     94   1.1  macallan 	NULL,		/* textops */
     95   1.1  macallan 	8, 16,		/* fontwidth, fontheight */
     96   1.1  macallan 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT, /* capabilities */
     97   1.1  macallan 	NULL,		/* modecookie */
     98   1.1  macallan };
     99   1.1  macallan 
    100   1.1  macallan const struct wsscreen_descr *_chipsfb_scrlist[] = {
    101   1.1  macallan 	&chipsfb_defaultscreen,
    102   1.1  macallan 	/* XXX other formats, graphics screen? */
    103   1.1  macallan };
    104   1.1  macallan 
    105   1.1  macallan struct wsscreen_list chipsfb_screenlist = {
    106   1.1  macallan 	sizeof(_chipsfb_scrlist) / sizeof(struct wsscreen_descr *), _chipsfb_scrlist
    107   1.1  macallan };
    108   1.1  macallan 
    109   1.1  macallan static int	chipsfb_ioctl(void *, void *, u_long, void *, int,
    110   1.1  macallan 		    struct lwp *);
    111   1.1  macallan static paddr_t	chipsfb_mmap(void *, void *, off_t, int);
    112   1.1  macallan static void	chipsfb_clearscreen(struct chipsfb_softc *);
    113   1.1  macallan static void	chipsfb_init_screen(void *, struct vcons_screen *, int,
    114   1.1  macallan 			    long *);
    115   1.1  macallan 
    116   1.1  macallan 
    117   1.1  macallan struct wsdisplay_accessops chipsfb_accessops = {
    118   1.1  macallan 	chipsfb_ioctl,
    119   1.1  macallan 	chipsfb_mmap,
    120   1.1  macallan 	NULL,	/* vcons_alloc_screen */
    121   1.1  macallan 	NULL,	/* vcons_free_screen */
    122   1.1  macallan 	NULL,	/* vcons_show_screen */
    123   1.1  macallan 	NULL,	/* load_font */
    124   1.1  macallan 	NULL,	/* polls */
    125   1.1  macallan 	NULL,	/* scroll */
    126   1.1  macallan };
    127   1.1  macallan 
    128   1.1  macallan /*
    129   1.1  macallan  * Inline functions for getting access to register aperture.
    130   1.1  macallan  */
    131   1.1  macallan static inline void
    132   1.1  macallan chipsfb_write32(struct chipsfb_softc *sc, uint32_t reg, uint32_t val)
    133   1.1  macallan {
    134   1.2  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_mmregh, reg, val);
    135   1.1  macallan }
    136   1.1  macallan 
    137   1.1  macallan static inline uint32_t
    138   1.1  macallan chipsfb_read32(struct chipsfb_softc *sc, uint32_t reg)
    139   1.1  macallan {
    140   1.2  macallan 	return bus_space_read_4(sc->sc_memt, sc->sc_mmregh, reg);
    141   1.1  macallan }
    142   1.1  macallan 
    143   1.1  macallan static inline void
    144   1.1  macallan chipsfb_write_vga(struct chipsfb_softc *sc, uint32_t reg,  uint8_t val)
    145   1.1  macallan {
    146   1.1  macallan 	bus_space_write_1(sc->sc_iot, sc->sc_ioregh, reg, val);
    147   1.1  macallan }
    148   1.1  macallan 
    149   1.1  macallan static inline uint8_t
    150   1.1  macallan chipsfb_read_vga(struct chipsfb_softc *sc, uint32_t reg)
    151   1.1  macallan {
    152   1.1  macallan 	return bus_space_read_1(sc->sc_iot, sc->sc_ioregh, reg);
    153   1.1  macallan }
    154   1.1  macallan 
    155  1.10  macallan static inline uint8_t
    156   1.1  macallan chipsfb_read_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index)
    157   1.1  macallan {
    158   1.1  macallan 
    159   1.1  macallan 	chipsfb_write_vga(sc, reg & 0xfffe, index);
    160   1.1  macallan 	return chipsfb_read_vga(sc, reg | 0x0001);
    161   1.1  macallan }
    162   1.1  macallan 
    163  1.12  macallan static inline void
    164   1.1  macallan chipsfb_write_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index,
    165   1.1  macallan     uint8_t val)
    166   1.1  macallan {
    167   1.1  macallan 
    168   1.1  macallan 	chipsfb_write_vga(sc, reg & 0xfffe, index);
    169   1.1  macallan 	chipsfb_write_vga(sc, reg | 0x0001, val);
    170   1.1  macallan }
    171   1.1  macallan 
    172   1.1  macallan static void
    173   1.1  macallan chipsfb_wait_idle(struct chipsfb_softc *sc)
    174   1.1  macallan {
    175   1.1  macallan 
    176   1.1  macallan #ifdef CHIPSFB_DEBUG
    177   1.1  macallan 	chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0);
    178   1.1  macallan #endif
    179   1.1  macallan 
    180   1.1  macallan 	/* spin until the blitter is idle */
    181   1.1  macallan 	while ((chipsfb_read32(sc, CT_BLT_CONTROL) & BLT_IS_BUSY) != 0) {
    182   1.1  macallan 	}
    183   1.1  macallan 
    184   1.1  macallan #ifdef CHIPSFB_DEBUG
    185   1.1  macallan 	chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0xffffffff);
    186   1.1  macallan #endif
    187   1.1  macallan }
    188   1.1  macallan 
    189   1.1  macallan void
    190   1.1  macallan chipsfb_do_attach(struct chipsfb_softc *sc)
    191   1.1  macallan {
    192   1.1  macallan 	struct wsemuldisplaydev_attach_args aa;
    193   1.1  macallan 	struct rasops_info *ri;
    194   1.1  macallan 	prop_dictionary_t dict;
    195   1.1  macallan 	ulong defattr;
    196   1.1  macallan 	bool console = false;
    197   1.1  macallan 	int width, height, i, j;
    198   1.1  macallan 	uint32_t bg, fg, ul;
    199   1.6  macallan 	uint8_t cmap[768];
    200   1.1  macallan 
    201   1.1  macallan 	dict = device_properties(sc->sc_dev);
    202   1.1  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    203   1.1  macallan 	sc->sc_dacw = -1;
    204   1.1  macallan 
    205   1.1  macallan #ifdef CHIPSFB_DEBUG
    206   1.1  macallan 	printf(prop_dictionary_externalize(dict));
    207   1.1  macallan #endif
    208   1.1  macallan 	chipsfb_init(sc);
    209   1.1  macallan 
    210   1.1  macallan 	width = height = -1;
    211   1.1  macallan 
    212   1.1  macallan 	/* detect panel size */
    213   1.1  macallan 	width = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HSIZE_LSB);
    214   1.1  macallan 	width |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HORZ_OVERFLOW_1)
    215   1.1  macallan 	    & 0x0f) << 8;
    216   1.1  macallan 	width = (width + 1) * 8;
    217   1.1  macallan 	height = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VSIZE_LSB);
    218   1.1  macallan 	height |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VERT_OVERFLOW_1)
    219   1.1  macallan 	    & 0x0f) << 8;
    220   1.1  macallan 	height++;
    221   1.2  macallan 	if ((width < 640) || ( width > 1280) || (height < 480) ||
    222   1.2  macallan 	    (height > 1024)) {
    223   1.2  macallan 		/* no sane values in the panel registers */
    224   1.2  macallan 		width = height = -1;
    225   1.2  macallan 	} else
    226   1.2  macallan 		aprint_verbose("Panel size: %d x %d\n", width, height);
    227   1.1  macallan 
    228  1.12  macallan 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width))
    229  1.12  macallan 		sc->sc_width = width;
    230  1.12  macallan 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height))
    231  1.12  macallan 		sc->sc_height = height;
    232  1.12  macallan 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_bits_per_pixel))
    233  1.12  macallan 		sc->sc_bits_per_pixel = 8;
    234  1.12  macallan 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_linebytes))
    235  1.12  macallan 		sc->sc_linebytes = (sc->sc_width * sc->sc_bits_per_pixel) >> 3;
    236   1.1  macallan 
    237   1.1  macallan 	prop_dictionary_get_bool(dict, "is_console", &console);
    238   1.1  macallan 
    239   1.1  macallan #ifdef notyet
    240   1.1  macallan 	/* XXX this should at least be configurable via kernel config */
    241   1.1  macallan 	chipsfb_set_videomode(sc, &videomode_list[16]);
    242   1.1  macallan #endif
    243   1.1  macallan 
    244   1.1  macallan 	vcons_init(&sc->vd, sc, &chipsfb_defaultscreen, &chipsfb_accessops);
    245   1.1  macallan 	sc->vd.init_screen = chipsfb_init_screen;
    246   1.1  macallan 
    247  1.12  macallan 	sc->sc_gc.gc_bitblt = chipsfb_bitblt;
    248  1.12  macallan 	sc->sc_gc.gc_blitcookie = sc;
    249  1.12  macallan 	sc->sc_gc.gc_rop = ROP_COPY;
    250  1.12  macallan 
    251   1.1  macallan 	ri = &chipsfb_console_screen.scr_ri;
    252   1.1  macallan 	if (console) {
    253   1.1  macallan 		vcons_init_screen(&sc->vd, &chipsfb_console_screen, 1,
    254   1.1  macallan 		    &defattr);
    255   1.1  macallan 		chipsfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    256   1.1  macallan 
    257   1.1  macallan 		chipsfb_defaultscreen.textops = &ri->ri_ops;
    258   1.1  macallan 		chipsfb_defaultscreen.capabilities = ri->ri_caps;
    259   1.1  macallan 		chipsfb_defaultscreen.nrows = ri->ri_rows;
    260   1.1  macallan 		chipsfb_defaultscreen.ncols = ri->ri_cols;
    261  1.12  macallan 		glyphcache_init(&sc->sc_gc, sc->sc_height + 1,
    262  1.12  macallan 				(sc->sc_fbsize / sc->sc_linebytes) - sc->sc_height - 1,
    263  1.12  macallan 				sc->sc_width,
    264  1.12  macallan 				ri->ri_font->fontwidth,
    265  1.12  macallan 				ri->ri_font->fontheight,
    266  1.12  macallan 				defattr);
    267   1.1  macallan 		wsdisplay_cnattach(&chipsfb_defaultscreen, ri, 0, 0, defattr);
    268   1.1  macallan 	} else {
    269   1.5  macallan 		if (chipsfb_console_screen.scr_ri.ri_rows == 0) {
    270   1.5  macallan 			/* do some minimal setup to avoid weirdnesses later */
    271   1.5  macallan 			vcons_init_screen(&sc->vd, &chipsfb_console_screen, 1,
    272   1.5  macallan 			    &defattr);
    273   1.7  macallan 		} else
    274   1.7  macallan 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    275   1.7  macallan 
    276  1.12  macallan 		glyphcache_init(&sc->sc_gc, sc->sc_height + 1,
    277  1.12  macallan 				(sc->sc_fbsize / sc->sc_linebytes) - sc->sc_height - 1,
    278  1.12  macallan 				sc->sc_width,
    279  1.12  macallan 				ri->ri_font->fontwidth,
    280  1.12  macallan 				ri->ri_font->fontheight,
    281  1.12  macallan 				defattr);
    282   1.1  macallan 	}
    283   1.1  macallan 
    284   1.1  macallan 	rasops_unpack_attr(defattr, &fg, &bg, &ul);
    285   1.1  macallan 	sc->sc_bg = ri->ri_devcmap[bg];
    286   1.1  macallan 	chipsfb_clearscreen(sc);
    287   1.1  macallan 
    288   1.1  macallan 	if (console)
    289   1.1  macallan 		vcons_replay_msgbuf(&chipsfb_console_screen);
    290   1.1  macallan 
    291   1.1  macallan 	aprint_normal_dev(sc->sc_dev, "%d MB aperture, %d MB VRAM at 0x%08x\n",
    292   1.1  macallan 	    (u_int)(sc->sc_fbsize >> 20),
    293   1.2  macallan 	    (int)sc->memsize >> 20, (u_int)sc->sc_fb);
    294   1.1  macallan #ifdef CHIPSFB_DEBUG
    295   1.1  macallan 	aprint_debug("fb: %08lx\n", (ulong)ri->ri_bits);
    296   1.1  macallan #endif
    297   1.1  macallan 
    298   1.1  macallan 	j = 0;
    299   1.6  macallan 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    300   1.1  macallan 	for (i = 0; i < 256; i++) {
    301   1.6  macallan 		chipsfb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    302   1.1  macallan 		j += 3;
    303   1.1  macallan 	}
    304   1.6  macallan 
    305   1.1  macallan 	aa.console = console;
    306   1.1  macallan 	aa.scrdata = &chipsfb_screenlist;
    307   1.1  macallan 	aa.accessops = &chipsfb_accessops;
    308   1.1  macallan 	aa.accesscookie = &sc->vd;
    309   1.1  macallan 
    310  1.15   thorpej 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARGS_NONE);
    311   1.1  macallan }
    312   1.1  macallan 
    313   1.1  macallan static int
    314   1.1  macallan chipsfb_putpalreg(struct chipsfb_softc *sc, uint8_t index, uint8_t r,
    315   1.1  macallan     uint8_t g, uint8_t b)
    316   1.1  macallan {
    317   1.1  macallan 
    318   1.1  macallan 	sc->sc_cmap_red[index] = r;
    319   1.1  macallan 	sc->sc_cmap_green[index] = g;
    320   1.1  macallan 	sc->sc_cmap_blue[index] = b;
    321   1.1  macallan 
    322   1.1  macallan 	chipsfb_write_vga(sc, CT_DACMASK, 0xff);
    323   1.1  macallan 	chipsfb_write_vga(sc, CT_WRITEINDEX, index);
    324   1.1  macallan 	chipsfb_write_vga(sc, CT_DACDATA, r);
    325   1.1  macallan 	chipsfb_write_vga(sc, CT_DACDATA, g);
    326   1.1  macallan 	chipsfb_write_vga(sc, CT_DACDATA, b);
    327   1.1  macallan 
    328   1.1  macallan 	return 0;
    329   1.1  macallan }
    330   1.1  macallan 
    331   1.1  macallan static int
    332   1.1  macallan chipsfb_putcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
    333   1.1  macallan {
    334   1.1  macallan 	u_char *r, *g, *b;
    335   1.1  macallan 	u_int index = cm->index;
    336   1.1  macallan 	u_int count = cm->count;
    337   1.1  macallan 	int i, error;
    338   1.1  macallan 	u_char rbuf[256], gbuf[256], bbuf[256];
    339   1.1  macallan 
    340   1.1  macallan #ifdef CHIPSFB_DEBUG
    341   1.1  macallan 	aprint_debug("putcmap: %d %d\n",index, count);
    342   1.1  macallan #endif
    343   1.1  macallan 	if (cm->index >= 256 || cm->count > 256 ||
    344   1.1  macallan 	    (cm->index + cm->count) > 256)
    345   1.1  macallan 		return EINVAL;
    346   1.1  macallan 	error = copyin(cm->red, &rbuf[index], count);
    347   1.1  macallan 	if (error)
    348   1.1  macallan 		return error;
    349   1.1  macallan 	error = copyin(cm->green, &gbuf[index], count);
    350   1.1  macallan 	if (error)
    351   1.1  macallan 		return error;
    352   1.1  macallan 	error = copyin(cm->blue, &bbuf[index], count);
    353   1.1  macallan 	if (error)
    354   1.1  macallan 		return error;
    355   1.1  macallan 
    356   1.1  macallan 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    357   1.1  macallan 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    358   1.1  macallan 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    359   1.1  macallan 
    360   1.1  macallan 	r = &sc->sc_cmap_red[index];
    361   1.1  macallan 	g = &sc->sc_cmap_green[index];
    362   1.1  macallan 	b = &sc->sc_cmap_blue[index];
    363   1.1  macallan 
    364   1.1  macallan 	for (i = 0; i < count; i++) {
    365   1.1  macallan 		chipsfb_putpalreg(sc, index, *r, *g, *b);
    366   1.1  macallan 		index++;
    367   1.1  macallan 		r++, g++, b++;
    368   1.1  macallan 	}
    369   1.1  macallan 	return 0;
    370   1.1  macallan }
    371   1.1  macallan 
    372   1.1  macallan static int
    373   1.1  macallan chipsfb_getcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
    374   1.1  macallan {
    375   1.1  macallan 	u_int index = cm->index;
    376   1.1  macallan 	u_int count = cm->count;
    377   1.1  macallan 	int error;
    378   1.1  macallan 
    379   1.1  macallan 	if (index >= 255 || count > 256 || index + count > 256)
    380   1.1  macallan 		return EINVAL;
    381   1.1  macallan 
    382   1.1  macallan 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    383   1.1  macallan 	if (error)
    384   1.1  macallan 		return error;
    385   1.1  macallan 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    386   1.1  macallan 	if (error)
    387   1.1  macallan 		return error;
    388   1.1  macallan 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    389   1.1  macallan 	if (error)
    390   1.1  macallan 		return error;
    391   1.1  macallan 
    392   1.1  macallan 	return 0;
    393   1.1  macallan }
    394   1.1  macallan 
    395   1.1  macallan static void
    396   1.1  macallan chipsfb_clearscreen(struct chipsfb_softc *sc)
    397   1.1  macallan {
    398  1.12  macallan 	chipsfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, sc->sc_bg);
    399   1.1  macallan }
    400   1.1  macallan 
    401   1.1  macallan /*
    402   1.1  macallan  * wsdisplay_emulops
    403   1.1  macallan  */
    404   1.1  macallan 
    405   1.1  macallan static void
    406   1.1  macallan chipsfb_cursor(void *cookie, int on, int row, int col)
    407   1.1  macallan {
    408   1.1  macallan 	struct rasops_info *ri = cookie;
    409   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    410   1.1  macallan 	struct chipsfb_softc *sc = scr->scr_cookie;
    411   1.1  macallan 	int x, y, wi, he;
    412   1.1  macallan 
    413   1.1  macallan 	wi = ri->ri_font->fontwidth;
    414   1.1  macallan 	he = ri->ri_font->fontheight;
    415   1.1  macallan 
    416   1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    417   1.1  macallan 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    418   1.1  macallan 		y = ri->ri_crow * he + ri->ri_yorigin;
    419   1.1  macallan 		if (ri->ri_flg & RI_CURSOR) {
    420   1.1  macallan 			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
    421   1.1  macallan 			ri->ri_flg &= ~RI_CURSOR;
    422   1.1  macallan 		}
    423   1.1  macallan 		ri->ri_crow = row;
    424   1.1  macallan 		ri->ri_ccol = col;
    425   1.1  macallan 		if (on) {
    426   1.1  macallan 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    427   1.1  macallan 			y = ri->ri_crow * he + ri->ri_yorigin;
    428   1.1  macallan 			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
    429   1.1  macallan 			ri->ri_flg |= RI_CURSOR;
    430   1.1  macallan 		}
    431   1.1  macallan 	} else {
    432   1.1  macallan 		ri->ri_flg &= ~RI_CURSOR;
    433   1.1  macallan 		ri->ri_crow = row;
    434   1.1  macallan 		ri->ri_ccol = col;
    435   1.1  macallan 	}
    436   1.1  macallan }
    437   1.1  macallan 
    438   1.1  macallan #if 0
    439   1.1  macallan int
    440   1.1  macallan chipsfb_mapchar(void *cookie, int uni, u_int *index)
    441   1.1  macallan {
    442   1.1  macallan 	return 0;
    443   1.1  macallan }
    444   1.1  macallan #endif
    445   1.1  macallan 
    446   1.1  macallan static void
    447   1.1  macallan chipsfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    448   1.1  macallan {
    449   1.1  macallan 	struct rasops_info *ri = cookie;
    450   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    451   1.1  macallan 	struct chipsfb_softc *sc = scr->scr_cookie;
    452   1.1  macallan 	int32_t xs, xd, y, width, height;
    453   1.1  macallan 
    454   1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    455   1.1  macallan 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    456   1.1  macallan 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    457   1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    458   1.1  macallan 		width = ri->ri_font->fontwidth * ncols;
    459   1.1  macallan 		height = ri->ri_font->fontheight;
    460   1.1  macallan 		chipsfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
    461   1.1  macallan 	}
    462   1.1  macallan }
    463   1.1  macallan 
    464   1.1  macallan static void
    465   1.1  macallan chipsfb_erasecols(void *cookie, int row, int startcol, int ncols,
    466   1.1  macallan     long fillattr)
    467   1.1  macallan {
    468   1.1  macallan 	struct rasops_info *ri = cookie;
    469   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    470   1.1  macallan 	struct chipsfb_softc *sc = scr->scr_cookie;
    471   1.1  macallan 	int32_t x, y, width, height, fg, bg, ul;
    472   1.1  macallan 
    473   1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    474   1.1  macallan 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    475   1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    476   1.1  macallan 		width = ri->ri_font->fontwidth * ncols;
    477   1.1  macallan 		height = ri->ri_font->fontheight;
    478   1.1  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    479   1.1  macallan 
    480   1.1  macallan 		chipsfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    481   1.1  macallan 	}
    482   1.1  macallan }
    483   1.1  macallan 
    484   1.1  macallan static void
    485   1.1  macallan chipsfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    486   1.1  macallan {
    487   1.1  macallan 	struct rasops_info *ri = cookie;
    488   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    489   1.1  macallan 	struct chipsfb_softc *sc = scr->scr_cookie;
    490   1.1  macallan 	int32_t x, ys, yd, width, height;
    491   1.1  macallan 
    492   1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    493   1.1  macallan 		x = ri->ri_xorigin;
    494   1.1  macallan 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    495   1.1  macallan 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    496   1.1  macallan 		width = ri->ri_emuwidth;
    497   1.1  macallan 		height = ri->ri_font->fontheight * nrows;
    498   1.1  macallan 		chipsfb_bitblt(sc, x, ys, x, yd, width, height, ROP_COPY);
    499   1.1  macallan 	}
    500   1.1  macallan }
    501   1.1  macallan 
    502   1.1  macallan static void
    503   1.1  macallan chipsfb_eraserows(void *cookie, int row, int nrows, long fillattr)
    504   1.1  macallan {
    505   1.1  macallan 	struct rasops_info *ri = cookie;
    506   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    507   1.1  macallan 	struct chipsfb_softc *sc = scr->scr_cookie;
    508   1.1  macallan 	int32_t x, y, width, height, fg, bg, ul;
    509   1.1  macallan 
    510   1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    511   1.1  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    512   1.1  macallan 		if ((row == 0) && (nrows == ri->ri_rows)) {
    513   1.1  macallan 			/* clear the whole screen */
    514   1.1  macallan 			chipsfb_rectfill(sc, 0, 0, ri->ri_width,
    515   1.1  macallan 			    ri->ri_height, ri->ri_devcmap[bg]);
    516   1.1  macallan 		} else {
    517   1.1  macallan 			x = ri->ri_xorigin;
    518   1.1  macallan 			y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    519   1.1  macallan 			width = ri->ri_emuwidth;
    520   1.1  macallan 			height = ri->ri_font->fontheight * nrows;
    521   1.1  macallan 			chipsfb_rectfill(sc, x, y, width, height,
    522   1.1  macallan 			    ri->ri_devcmap[bg]);
    523   1.1  macallan 		}
    524   1.1  macallan 	}
    525   1.1  macallan }
    526   1.1  macallan 
    527   1.1  macallan static void
    528  1.12  macallan chipsfb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
    529  1.12  macallan     int width, int height, int rop)
    530   1.1  macallan {
    531  1.12  macallan 	struct chipsfb_softc *sc = cookie;
    532   1.1  macallan 	uint32_t src, dst, cmd = rop, stride, size;
    533   1.1  macallan 
    534   1.1  macallan 	cmd |= BLT_PAT_IS_SOLID;
    535   1.1  macallan 
    536   1.1  macallan 	/* we assume 8 bit for now */
    537  1.12  macallan 	src = xs + ys * sc->sc_linebytes;
    538  1.12  macallan 	dst = xd + yd * sc->sc_linebytes;
    539   1.1  macallan 
    540   1.1  macallan 	if (xs < xd) {
    541   1.1  macallan 		/* right-to-left operation */
    542   1.1  macallan 		cmd |= BLT_START_RIGHT;
    543   1.1  macallan 		src += width - 1;
    544   1.1  macallan 		dst += width - 1;
    545   1.1  macallan 	}
    546   1.1  macallan 
    547   1.1  macallan 	if (ys < yd) {
    548   1.1  macallan 		/* bottom-to-top operation */
    549   1.1  macallan 		cmd |= BLT_START_BOTTOM;
    550  1.12  macallan 		src += (height - 1) * sc->sc_linebytes;
    551  1.12  macallan 		dst += (height - 1) * sc->sc_linebytes;
    552   1.1  macallan 	}
    553   1.1  macallan 
    554  1.12  macallan 	stride = (sc->sc_linebytes << 16) | sc->sc_linebytes;
    555   1.1  macallan 	size = (height << 16) | width;
    556   1.1  macallan 
    557   1.1  macallan 	chipsfb_wait_idle(sc);
    558   1.1  macallan 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    559   1.1  macallan 	chipsfb_write32(sc, CT_BLT_SRCADDR, src);
    560   1.1  macallan 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    561   1.1  macallan 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    562   1.1  macallan 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    563   1.1  macallan #ifdef CHIPSFB_WAIT
    564   1.1  macallan 	chipsfb_wait_idle(sc);
    565   1.1  macallan #endif
    566   1.1  macallan }
    567   1.1  macallan 
    568   1.1  macallan static void
    569   1.1  macallan chipsfb_rectfill(struct chipsfb_softc *sc, int x, int y, int width,
    570   1.1  macallan     int height, int colour)
    571   1.1  macallan {
    572   1.1  macallan 	uint32_t dst, cmd, stride, size;
    573   1.1  macallan 
    574   1.1  macallan 	cmd = BLT_PAT_IS_SOLID | BLT_PAT_IS_MONO | ROP_PAT;
    575   1.1  macallan 
    576   1.1  macallan 	/* we assume 8 bit for now */
    577  1.12  macallan 	dst = x + y * sc->sc_linebytes;
    578   1.1  macallan 
    579  1.12  macallan 	stride = (sc->sc_linebytes << 16) | sc->sc_linebytes;
    580   1.1  macallan 	size = (height << 16) | width;
    581   1.1  macallan 
    582   1.1  macallan 	chipsfb_wait_idle(sc);
    583   1.1  macallan 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    584   1.1  macallan 	chipsfb_write32(sc, CT_BLT_SRCADDR, dst);
    585   1.1  macallan 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    586   1.1  macallan 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    587   1.1  macallan 	chipsfb_write32(sc, CT_BLT_BG, colour);
    588   1.1  macallan 	chipsfb_write32(sc, CT_BLT_FG, colour);
    589   1.1  macallan 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    590   1.1  macallan #ifdef CHIPSFB_WAIT
    591   1.1  macallan 	chipsfb_wait_idle(sc);
    592   1.1  macallan #endif
    593   1.1  macallan }
    594   1.1  macallan 
    595   1.1  macallan static void
    596  1.12  macallan chipsfb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
    597  1.12  macallan {
    598  1.12  macallan 	struct rasops_info *ri = cookie;
    599  1.12  macallan 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    600  1.12  macallan 	struct vcons_screen *scr = ri->ri_hw;
    601  1.12  macallan 	struct chipsfb_softc *sc = scr->scr_cookie;
    602  1.12  macallan 	uint32_t bg, latch = 0, bg8, fg8, pixel, dst, stride, size;
    603  1.12  macallan 	int i, l, x, y, wi, he, r, g, b, aval;
    604  1.13  macallan 	int r1, g1, b1, r0, g0, b0, fgo, bgo, off, pad;
    605  1.12  macallan 	uint8_t *data8;
    606  1.12  macallan 	int rv;
    607  1.12  macallan 
    608  1.12  macallan 	if (__predict_false((unsigned int)row > ri->ri_rows ||
    609  1.12  macallan 	    (unsigned int)col > ri->ri_cols))
    610  1.12  macallan 		return;
    611  1.12  macallan 
    612  1.12  macallan 	if (__predict_false((sc->sc_mode != WSDISPLAYIO_MODE_EMUL)))
    613  1.12  macallan 		return;
    614  1.12  macallan 
    615  1.12  macallan 	if (__predict_false((!CHAR_IN_FONT(c, font))))
    616  1.12  macallan 		return;
    617  1.12  macallan 
    618  1.12  macallan 	wi = font->fontwidth;
    619  1.12  macallan 	he = font->fontheight;
    620  1.12  macallan 
    621  1.12  macallan 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    622  1.12  macallan 	x = ri->ri_xorigin + col * wi;
    623  1.12  macallan 	y = ri->ri_yorigin + row * he;
    624  1.12  macallan 
    625  1.12  macallan 	if (c == 0x20) {
    626  1.12  macallan 		chipsfb_rectfill(sc, x, y, wi, he, bg);
    627  1.12  macallan 		return;
    628  1.12  macallan 	}
    629  1.12  macallan 
    630  1.12  macallan 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
    631  1.12  macallan 	if (rv == GC_OK)
    632  1.12  macallan 		return;
    633  1.12  macallan 
    634  1.12  macallan 	data8 = WSFONT_GLYPH(c, font);
    635  1.12  macallan 
    636  1.12  macallan 	/* we assume 8 bit for now */
    637  1.12  macallan 	dst = x + y * sc->sc_linebytes;
    638  1.12  macallan 
    639  1.12  macallan 	stride = sc->sc_linebytes << 16;
    640  1.12  macallan 	size = (he << 16) | wi;
    641  1.12  macallan 
    642  1.12  macallan 	/* set up for host blit */
    643  1.12  macallan 	chipsfb_wait_idle(sc);
    644  1.12  macallan 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    645  1.12  macallan 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    646  1.12  macallan 	chipsfb_write32(sc, CT_BLT_SRCADDR, 0);
    647  1.12  macallan 	chipsfb_write32(sc, CT_BLT_CONTROL,
    648  1.12  macallan 	    BLT_PAT_IS_SOLID | BLT_SRC_IS_CPU | ROP_COPY);
    649  1.12  macallan 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    650  1.12  macallan 
    651  1.12  macallan 	/*
    652  1.12  macallan 	 * we need the RGB colours here, so get offsets into rasops_cmap
    653  1.12  macallan 	 */
    654  1.12  macallan 	fgo = ((attr >> 24) & 0xf) * 3;
    655  1.12  macallan 	bgo = ((attr >> 16) & 0xf) * 3;
    656  1.12  macallan 
    657  1.12  macallan 	r0 = rasops_cmap[bgo];
    658  1.12  macallan 	r1 = rasops_cmap[fgo];
    659  1.12  macallan 	g0 = rasops_cmap[bgo + 1];
    660  1.12  macallan 	g1 = rasops_cmap[fgo + 1];
    661  1.12  macallan 	b0 = rasops_cmap[bgo + 2];
    662  1.12  macallan 	b1 = rasops_cmap[fgo + 2];
    663  1.12  macallan #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
    664  1.12  macallan 	bg8 = R3G3B2(r0, g0, b0);
    665  1.12  macallan 	fg8 = R3G3B2(r1, g1, b1);
    666  1.12  macallan 
    667  1.13  macallan 	/* see if we need to pad lines to 64bit */
    668  1.13  macallan 	pad = (wi + 3) & 4;
    669  1.12  macallan 	for (l = 0; l < he; l++) {
    670  1.12  macallan 		off = 0;
    671  1.13  macallan 		latch = 0;
    672  1.12  macallan 		for (i = 0; i < wi; i++) {
    673  1.12  macallan 			aval = *data8;
    674  1.12  macallan 			if (aval == 0) {
    675  1.12  macallan 				pixel = bg8;
    676  1.12  macallan 			} else if (aval == 255) {
    677  1.12  macallan 				pixel = fg8;
    678  1.12  macallan 			} else {
    679  1.12  macallan 				r = aval * r1 + (255 - aval) * r0;
    680  1.12  macallan 				g = aval * g1 + (255 - aval) * g0;
    681  1.12  macallan 				b = aval * b1 + (255 - aval) * b0;
    682  1.12  macallan 				pixel = ((r & 0xe000) >> 8) |
    683  1.12  macallan 					((g & 0xe000) >> 11) |
    684  1.12  macallan 					((b & 0xc000) >> 14);
    685  1.12  macallan 			}
    686  1.12  macallan 			latch |= pixel << off;
    687  1.12  macallan 			off += 8;
    688  1.12  macallan 			/* write in 32bit chunks */
    689  1.12  macallan 			if ((i & 3) == 3) {
    690  1.13  macallan 				chipsfb_write32(sc,
    691  1.13  macallan 				    CT_OFF_DATA - CT_OFF_BITBLT, latch);
    692  1.12  macallan 				latch = 0;
    693  1.12  macallan 				off = 0;
    694  1.12  macallan 			}
    695  1.12  macallan 			data8++;
    696  1.12  macallan 		}
    697  1.12  macallan 		/* if we have pixels left in latch write them out */
    698  1.12  macallan 		if ((i & 3) != 0) {
    699  1.12  macallan 			chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, latch);
    700  1.12  macallan 		}
    701  1.12  macallan 		/* this chip needs scanlines 64bit aligned */
    702  1.13  macallan 		if (pad) chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, 0);
    703  1.12  macallan 	}
    704  1.12  macallan 
    705  1.12  macallan 	if (rv == GC_ADD) {
    706  1.12  macallan 		glyphcache_add(&sc->sc_gc, c, x, y);
    707  1.12  macallan 	}
    708  1.12  macallan }
    709  1.12  macallan 
    710  1.12  macallan static void
    711   1.1  macallan chipsfb_putchar(void *cookie, int row, int col, u_int c, long attr)
    712   1.1  macallan {
    713   1.1  macallan 	struct rasops_info *ri = cookie;
    714   1.1  macallan 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    715   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    716   1.1  macallan 	struct chipsfb_softc *sc = scr->scr_cookie;
    717   1.1  macallan 
    718   1.1  macallan 	if (__predict_false((unsigned int)row > ri->ri_rows ||
    719   1.1  macallan 	    (unsigned int)col > ri->ri_cols))
    720   1.1  macallan 		return;
    721   1.1  macallan 
    722   1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    723   1.1  macallan 		uint8_t *data;
    724   1.1  macallan 		int fg, bg, uc;
    725   1.1  macallan 		int x, y, wi, he;
    726   1.1  macallan 
    727   1.1  macallan 		wi = font->fontwidth;
    728   1.1  macallan 		he = font->fontheight;
    729   1.1  macallan 
    730   1.1  macallan 		if (!CHAR_IN_FONT(c, font))
    731   1.1  macallan 			return;
    732   1.1  macallan 		bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
    733   1.1  macallan 		fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
    734   1.1  macallan 		x = ri->ri_xorigin + col * wi;
    735   1.1  macallan 		y = ri->ri_yorigin + row * he;
    736   1.1  macallan 		if (c == 0x20) {
    737   1.1  macallan 			chipsfb_rectfill(sc, x, y, wi, he, bg);
    738   1.1  macallan 		} else {
    739   1.1  macallan 			uc = c - font->firstchar;
    740   1.1  macallan 			data = (uint8_t *)font->data + uc *
    741   1.1  macallan 			    ri->ri_fontscale;
    742   1.1  macallan 			chipsfb_setup_mono(sc, x, y, wi, he, fg, bg);
    743   1.1  macallan 			chipsfb_feed(sc, font->stride * he, data);
    744   1.1  macallan 		}
    745   1.1  macallan 	}
    746   1.1  macallan }
    747   1.1  macallan 
    748   1.1  macallan static void
    749   1.1  macallan chipsfb_setup_mono(struct chipsfb_softc *sc, int xd, int yd, int width,
    750   1.1  macallan     int height, uint32_t fg, uint32_t bg)
    751   1.1  macallan {
    752   1.1  macallan 	uint32_t dst, cmd, stride, size;
    753   1.1  macallan 
    754   1.1  macallan 	cmd = BLT_PAT_IS_SOLID | BLT_SRC_IS_CPU | BLT_SRC_IS_MONO | ROP_COPY;
    755   1.1  macallan 
    756   1.1  macallan 	/* we assume 8 bit for now */
    757  1.12  macallan 	dst = xd + yd * sc->sc_linebytes;
    758   1.1  macallan 
    759  1.12  macallan 	stride = (sc->sc_linebytes << 16);
    760   1.1  macallan 	size = (height << 16) | width;
    761   1.1  macallan 
    762   1.1  macallan 	chipsfb_wait_idle(sc);
    763   1.1  macallan 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    764   1.1  macallan 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    765   1.1  macallan 	chipsfb_write32(sc, CT_BLT_SRCADDR, 0);
    766   1.1  macallan 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    767   1.1  macallan 	chipsfb_write32(sc, CT_BLT_BG, bg);
    768   1.1  macallan 	chipsfb_write32(sc, CT_BLT_FG, fg);
    769   1.1  macallan 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    770   1.1  macallan }
    771   1.1  macallan 
    772   1.1  macallan static void
    773   1.1  macallan chipsfb_feed(struct chipsfb_softc *sc, int count, uint8_t *data)
    774   1.1  macallan {
    775   1.1  macallan 	int i;
    776   1.1  macallan 	uint32_t latch = 0, bork;
    777   1.1  macallan 	int shift = 0;
    778   1.1  macallan 
    779   1.1  macallan 	for (i = 0; i < count; i++) {
    780   1.1  macallan 		bork = data[i];
    781   1.1  macallan 		latch |= (bork << shift);
    782   1.1  macallan 		if (shift == 24) {
    783   1.2  macallan 			chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, latch);
    784   1.1  macallan 			latch = 0;
    785   1.1  macallan 			shift = 0;
    786   1.1  macallan 		} else
    787   1.1  macallan 			shift += 8;
    788   1.1  macallan 	}
    789   1.1  macallan 
    790   1.1  macallan 	if (shift != 0) {
    791   1.2  macallan 		chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, latch);
    792   1.1  macallan 	}
    793   1.1  macallan 
    794   1.1  macallan 	/* apparently the chip wants 64bit-aligned data or it won't go idle */
    795   1.1  macallan 	if ((count + 3) & 0x04) {
    796   1.2  macallan 		chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, 0);
    797   1.1  macallan 	}
    798   1.1  macallan #ifdef CHIPSFB_WAIT
    799   1.1  macallan 	chipsfb_wait_idle(sc);
    800   1.1  macallan #endif
    801   1.1  macallan }
    802   1.1  macallan 
    803   1.1  macallan #if 0
    804   1.1  macallan static void
    805   1.1  macallan chipsfb_showpal(struct chipsfb_softc *sc)
    806   1.1  macallan {
    807   1.1  macallan 	int i, x = 0;
    808   1.1  macallan 
    809   1.1  macallan 	for (i = 0; i < 16; i++) {
    810   1.1  macallan 		chipsfb_rectfill(sc, x, 0, 64, 64, i);
    811   1.1  macallan 		x += 64;
    812   1.1  macallan 	}
    813   1.1  macallan }
    814   1.1  macallan #endif
    815   1.1  macallan 
    816   1.1  macallan #if 0
    817   1.1  macallan static int
    818   1.1  macallan chipsfb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
    819   1.1  macallan {
    820   1.1  macallan 
    821   1.1  macallan 	return 0;
    822   1.1  macallan }
    823   1.1  macallan #endif
    824   1.1  macallan 
    825   1.1  macallan static void
    826   1.1  macallan chipsfb_restore_palette(struct chipsfb_softc *sc)
    827   1.1  macallan {
    828   1.1  macallan 	int i;
    829   1.1  macallan 
    830   1.1  macallan 	for (i = 0; i < 256; i++) {
    831   1.1  macallan 		chipsfb_putpalreg(sc,
    832   1.1  macallan 		   i,
    833   1.1  macallan 		   sc->sc_cmap_red[i],
    834   1.1  macallan 		   sc->sc_cmap_green[i],
    835   1.1  macallan 		   sc->sc_cmap_blue[i]);
    836   1.1  macallan 	}
    837   1.1  macallan }
    838   1.1  macallan 
    839   1.1  macallan /*
    840   1.1  macallan  * wsdisplay_accessops
    841   1.1  macallan  */
    842   1.1  macallan 
    843   1.1  macallan static int
    844   1.1  macallan chipsfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    845   1.1  macallan 	struct lwp *l)
    846   1.1  macallan {
    847   1.1  macallan 	struct vcons_data *vd = v;
    848   1.1  macallan 	struct chipsfb_softc *sc = vd->cookie;
    849   1.1  macallan 	struct wsdisplay_fbinfo *wdf;
    850   1.1  macallan 	struct vcons_screen *ms = vd->active;
    851   1.1  macallan 
    852   1.1  macallan 	switch (cmd) {
    853   1.1  macallan 	case WSDISPLAYIO_GTYPE:
    854   1.1  macallan 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    855   1.1  macallan 		return 0;
    856   1.1  macallan 
    857   1.1  macallan 	case WSDISPLAYIO_GINFO:
    858   1.1  macallan 		wdf = (void *)data;
    859   1.1  macallan 		wdf->height = ms->scr_ri.ri_height;
    860   1.1  macallan 		wdf->width = ms->scr_ri.ri_width;
    861   1.1  macallan 		wdf->depth = ms->scr_ri.ri_depth;
    862   1.1  macallan 		wdf->cmsize = 256;
    863   1.1  macallan 		return 0;
    864   1.1  macallan 
    865   1.1  macallan 	case WSDISPLAYIO_GETCMAP:
    866   1.1  macallan 		return chipsfb_getcmap(sc,
    867   1.1  macallan 		    (struct wsdisplay_cmap *)data);
    868   1.1  macallan 
    869   1.1  macallan 	case WSDISPLAYIO_PUTCMAP:
    870   1.1  macallan 		return chipsfb_putcmap(sc,
    871   1.1  macallan 		    (struct wsdisplay_cmap *)data);
    872   1.1  macallan 
    873   1.1  macallan 
    874   1.1  macallan 	case WSDISPLAYIO_SMODE: {
    875   1.1  macallan 		int new_mode = *(int*)data;
    876   1.1  macallan 		if (new_mode != sc->sc_mode) {
    877   1.1  macallan 			sc->sc_mode = new_mode;
    878   1.1  macallan 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    879  1.12  macallan 				chipsfb_init(sc);
    880   1.1  macallan 				chipsfb_restore_palette(sc);
    881  1.12  macallan 				glyphcache_wipe(&sc->sc_gc);
    882   1.1  macallan 				vcons_redraw_screen(ms);
    883   1.1  macallan 			}
    884   1.1  macallan 		}
    885   1.1  macallan 		}
    886   1.1  macallan 		return 0;
    887   1.8  macallan 
    888   1.8  macallan 	case WSDISPLAYIO_GET_FBINFO: {
    889   1.8  macallan 		struct wsdisplayio_fbinfo *fbi = data;
    890   1.8  macallan 		return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    891   1.8  macallan 	}
    892   1.8  macallan 
    893   1.1  macallan 	default:
    894   1.1  macallan 		if (sc->sc_ioctl != NULL)
    895   1.1  macallan 			return sc->sc_ioctl(v, vs, cmd, data, flag, l);
    896   1.1  macallan 	}
    897   1.1  macallan 	return EPASSTHROUGH;
    898   1.1  macallan }
    899   1.1  macallan 
    900   1.1  macallan static paddr_t
    901   1.1  macallan chipsfb_mmap(void *v, void *vs, off_t offset, int prot)
    902   1.1  macallan {
    903   1.1  macallan 	struct vcons_data *vd = v;
    904   1.1  macallan 	struct chipsfb_softc *sc = vd->cookie;
    905   1.1  macallan 	paddr_t pa;
    906   1.1  macallan 
    907  1.11  macallan 	if (sc->sc_mmap != NULL) {
    908  1.11  macallan 		pa = sc->sc_mmap(v, vs, offset, prot);
    909  1.11  macallan 		if (pa != -1) return pa;
    910  1.11  macallan 	}
    911   1.2  macallan 
    912   1.1  macallan 	/* 'regular' framebuffer mmap()ing */
    913   1.1  macallan 	if (offset < sc->memsize) {
    914  1.11  macallan 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb, offset, prot,
    915   1.2  macallan 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    916   1.1  macallan 		return pa;
    917   1.1  macallan 	}
    918   1.1  macallan 
    919   1.1  macallan 	/*
    920   1.1  macallan 	 * restrict all other mappings to processes with superuser privileges
    921   1.1  macallan 	 * or the kernel itself
    922   1.1  macallan 	 */
    923   1.4      elad 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
    924   1.4      elad 	    NULL, NULL, NULL, NULL) != 0) {
    925   1.1  macallan 		aprint_normal_dev(sc->sc_dev, "mmap() rejected.\n");
    926   1.1  macallan 		return -1;
    927   1.1  macallan 	}
    928   1.1  macallan 
    929   1.1  macallan 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    930   1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    931  1.11  macallan 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    932   1.1  macallan 		return pa;
    933   1.1  macallan 	}
    934   1.1  macallan 
    935   1.1  macallan #ifdef PCI_MAGIC_IO_RANGE
    936   1.1  macallan 	/* allow mapping of IO space */
    937   1.1  macallan 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    938   1.1  macallan 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    939   1.1  macallan 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    940   1.1  macallan 		    0, prot, BUS_SPACE_MAP_LINEAR);
    941   1.1  macallan 		return pa;
    942   1.1  macallan 	}
    943   1.1  macallan #endif
    944   1.1  macallan 
    945   1.1  macallan 	return -1;
    946   1.1  macallan }
    947   1.1  macallan 
    948   1.1  macallan static void
    949   1.1  macallan chipsfb_init_screen(void *cookie, struct vcons_screen *scr,
    950   1.1  macallan     int existing, long *defattr)
    951   1.1  macallan {
    952   1.1  macallan 	struct chipsfb_softc *sc = cookie;
    953   1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    954   1.1  macallan 
    955  1.12  macallan 	ri->ri_depth = sc->sc_bits_per_pixel;
    956  1.12  macallan 	ri->ri_width = sc->sc_width;
    957  1.12  macallan 	ri->ri_height = sc->sc_height;
    958  1.12  macallan 	ri->ri_stride = sc->sc_width;
    959  1.12  macallan 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR |
    960  1.12  macallan 		     RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
    961   1.1  macallan 
    962   1.2  macallan 	ri->ri_bits = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
    963   1.1  macallan 
    964   1.1  macallan #ifdef CHIPSFB_DEBUG
    965   1.1  macallan 	aprint_debug("addr: %08lx\n", (ulong)ri->ri_bits);
    966   1.1  macallan #endif
    967   1.1  macallan 	if (existing) {
    968   1.1  macallan 		ri->ri_flg |= RI_CLEAR;
    969   1.1  macallan 	}
    970   1.1  macallan 
    971   1.3  macallan 	rasops_init(ri, 0, 0);
    972   1.1  macallan 	ri->ri_caps = WSSCREEN_WSCOLORS;
    973   1.1  macallan 
    974  1.12  macallan 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    975  1.12  macallan 		    sc->sc_width / ri->ri_font->fontwidth);
    976   1.1  macallan 
    977   1.1  macallan 	ri->ri_hw = scr;
    978   1.1  macallan 	ri->ri_ops.copyrows = chipsfb_copyrows;
    979   1.1  macallan 	ri->ri_ops.copycols = chipsfb_copycols;
    980   1.1  macallan 	ri->ri_ops.eraserows = chipsfb_eraserows;
    981   1.1  macallan 	ri->ri_ops.erasecols = chipsfb_erasecols;
    982   1.1  macallan 	ri->ri_ops.cursor = chipsfb_cursor;
    983  1.12  macallan 	if (FONT_IS_ALPHA(ri->ri_font)) {
    984  1.12  macallan 		ri->ri_ops.putchar = chipsfb_putchar_aa;
    985  1.12  macallan 	} else
    986  1.12  macallan 		ri->ri_ops.putchar = chipsfb_putchar;
    987   1.1  macallan }
    988   1.1  macallan 
    989   1.1  macallan #if 0
    990   1.1  macallan int
    991   1.1  macallan chipsfb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
    992   1.1  macallan {
    993   1.1  macallan 
    994   1.1  macallan 	return 0;
    995   1.1  macallan }
    996   1.1  macallan #endif
    997   1.1  macallan 
    998   1.1  macallan static void
    999   1.1  macallan chipsfb_init(struct chipsfb_softc *sc)
   1000   1.1  macallan {
   1001  1.12  macallan 	uint8_t reg;
   1002   1.1  macallan 
   1003   1.1  macallan 	chipsfb_wait_idle(sc);
   1004   1.1  macallan 
   1005   1.1  macallan 	/* setup the blitter */
   1006  1.12  macallan 	chipsfb_write32(sc, CT_BLT_EXPCTL, MONO_SRC_ALIGN_BYTE);
   1007  1.12  macallan 
   1008  1.12  macallan 	/* put DAC into 8bit mode */
   1009  1.12  macallan 	reg = chipsfb_read_indexed(sc, CT_CONF_INDEX, XR_PIXEL_PIPELINE_CTL_0);
   1010  1.12  macallan 	reg |= ENABLE_8BIT_DAC;
   1011  1.12  macallan 	chipsfb_write_indexed(sc, CT_CONF_INDEX, XR_PIXEL_PIPELINE_CTL_0, reg);
   1012   1.1  macallan }
   1013   1.1  macallan 
   1014   1.1  macallan uint32_t
   1015   1.1  macallan chipsfb_probe_vram(struct chipsfb_softc *sc)
   1016   1.1  macallan {
   1017   1.1  macallan 	uint32_t ofs = 0x00080000;	/* 512kB */
   1018   1.1  macallan 
   1019   1.1  macallan 	/*
   1020   1.1  macallan 	 * advance in 0.5MB steps, see if we can read back what we wrote and
   1021   1.1  macallan 	 * if what we wrote to 0 is left untouched. Max. fb size is 4MB so
   1022   1.1  macallan 	 * we voluntarily stop there.
   1023   1.1  macallan 	 */
   1024   1.2  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_fbh, 0, 0xf0f0f0f0);
   1025   1.2  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_fbh, ofs, 0x0f0f0f0f);
   1026   1.2  macallan 	while ((bus_space_read_4(sc->sc_memt, sc->sc_fbh, 0) == 0xf0f0f0f0) &&
   1027   1.2  macallan 	    (bus_space_read_4(sc->sc_memt, sc->sc_fbh, ofs) == 0x0f0f0f0f) &&
   1028   1.1  macallan 	    (ofs < 0x00400000)) {
   1029   1.1  macallan 
   1030   1.1  macallan 		ofs += 0x00080000;
   1031   1.2  macallan 		bus_space_write_4(sc->sc_memt, sc->sc_fbh, ofs, 0x0f0f0f0f);
   1032   1.1  macallan 	}
   1033   1.1  macallan 
   1034   1.1  macallan 	return ofs;
   1035   1.1  macallan }
   1036