Home | History | Annotate | Line # | Download | only in pci
chipsfb.c revision 1.25
      1  1.25    cegger /*	$NetBSD: chipsfb.c,v 1.25 2011/01/22 15:14:27 cegger 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.8  macallan /*
     29   1.1  macallan  * A console driver for Chips & Technologies 65550 graphics controllers
     30   1.8  macallan  * tested on macppc only so far
     31   1.1  macallan  */
     32   1.1  macallan 
     33   1.1  macallan #include <sys/cdefs.h>
     34  1.25    cegger __KERNEL_RCSID(0, "$NetBSD: chipsfb.c,v 1.25 2011/01/22 15:14:27 cegger Exp $");
     35   1.1  macallan 
     36   1.1  macallan #include <sys/param.h>
     37   1.1  macallan #include <sys/systm.h>
     38   1.1  macallan #include <sys/kernel.h>
     39   1.1  macallan #include <sys/device.h>
     40   1.1  macallan #include <sys/malloc.h>
     41   1.1  macallan #include <sys/callout.h>
     42   1.1  macallan #include <sys/lwp.h>
     43   1.1  macallan #include <sys/kauth.h>
     44   1.1  macallan 
     45   1.1  macallan #include <dev/videomode/videomode.h>
     46   1.1  macallan 
     47   1.1  macallan #include <dev/pci/pcivar.h>
     48   1.1  macallan #include <dev/pci/pcireg.h>
     49   1.1  macallan #include <dev/pci/pcidevs.h>
     50   1.1  macallan #include <dev/pci/pciio.h>
     51   1.1  macallan #include <dev/pci/chipsfbreg.h>
     52   1.1  macallan 
     53   1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     54   1.1  macallan #include <dev/wscons/wsconsio.h>
     55   1.1  macallan #include <dev/wsfont/wsfont.h>
     56   1.1  macallan #include <dev/rasops/rasops.h>
     57   1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     58  1.25    cegger #include <dev/pci/wsdisplay_pci.h>
     59   1.1  macallan 
     60   1.1  macallan #include <dev/i2c/i2cvar.h>
     61   1.1  macallan 
     62   1.1  macallan #include "opt_wsemul.h"
     63   1.8  macallan #include "opt_chipsfb.h"
     64   1.1  macallan 
     65   1.1  macallan struct chipsfb_softc {
     66   1.1  macallan 	struct device sc_dev;
     67   1.1  macallan 	pci_chipset_tag_t sc_pc;
     68   1.1  macallan 	pcitag_t sc_pcitag;
     69   1.1  macallan 
     70   1.1  macallan 	bus_space_tag_t sc_memt;
     71   1.1  macallan 	bus_space_tag_t sc_iot;
     72   1.8  macallan 	bus_space_handle_t sc_memh;
     73   1.1  macallan 
     74   1.1  macallan 	bus_space_tag_t sc_fbt;
     75   1.1  macallan 	bus_space_tag_t sc_ioregt;
     76   1.8  macallan 	bus_space_handle_t sc_fbh;
     77   1.8  macallan 	bus_space_handle_t sc_ioregh;
     78   1.1  macallan 	bus_addr_t sc_fb, sc_ioreg;
     79   1.1  macallan 	bus_size_t sc_fbsize, sc_ioregsize;
     80   1.1  macallan 
     81   1.1  macallan 	void *sc_ih;
     82   1.8  macallan 
     83   1.1  macallan 	size_t memsize;
     84   1.1  macallan 
     85   1.1  macallan 	int bits_per_pixel;
     86   1.1  macallan 	int width, height, linebytes;
     87   1.1  macallan 
     88   1.1  macallan 	int sc_mode;
     89   1.1  macallan 	uint32_t sc_bg;
     90   1.8  macallan 
     91   1.1  macallan 	u_char sc_cmap_red[256];
     92   1.1  macallan 	u_char sc_cmap_green[256];
     93   1.8  macallan 	u_char sc_cmap_blue[256];
     94   1.1  macallan 	int sc_dacw;
     95   1.1  macallan 
     96   1.8  macallan 	/*
     97   1.8  macallan 	 * I2C stuff
     98   1.8  macallan 	 * DDC2 clock is on GPIO1, data on GPIO0
     99   1.8  macallan 	 */
    100   1.1  macallan 	struct i2c_controller sc_i2c;
    101   1.1  macallan 	uint8_t sc_edid[1024];
    102   1.1  macallan 	int sc_edidbytes;	/* number of bytes read from the monitor */
    103   1.1  macallan 
    104   1.1  macallan 	struct vcons_data vd;
    105   1.1  macallan };
    106   1.1  macallan 
    107   1.1  macallan static struct vcons_screen chipsfb_console_screen;
    108   1.1  macallan 
    109   1.1  macallan extern const u_char rasops_cmap[768];
    110   1.1  macallan 
    111  1.17    cegger static int	chipsfb_match(device_t, cfdata_t, void *);
    112  1.17    cegger static void	chipsfb_attach(device_t, device_t, void *);
    113   1.1  macallan 
    114   1.8  macallan CFATTACH_DECL(chipsfb, sizeof(struct chipsfb_softc), chipsfb_match,
    115   1.1  macallan     chipsfb_attach, NULL, NULL);
    116   1.1  macallan 
    117   1.8  macallan static void 	chipsfb_init(struct chipsfb_softc *);
    118   1.1  macallan 
    119   1.1  macallan static void	chipsfb_cursor(void *, int, int, int);
    120   1.1  macallan static void	chipsfb_copycols(void *, int, int, int, int);
    121   1.1  macallan static void	chipsfb_erasecols(void *, int, int, int, long);
    122   1.1  macallan static void	chipsfb_copyrows(void *, int, int, int);
    123   1.1  macallan static void	chipsfb_eraserows(void *, int, int, long);
    124   1.1  macallan 
    125   1.1  macallan #if 0
    126   1.1  macallan static int	chipsfb_allocattr(void *, int, int, int, long *);
    127   1.1  macallan static void	chipsfb_scroll(void *, void *, int);
    128   1.1  macallan static int	chipsfb_load_font(void *, void *, struct wsdisplay_font *);
    129   1.1  macallan #endif
    130   1.1  macallan 
    131   1.1  macallan static int	chipsfb_putcmap(struct chipsfb_softc *,
    132   1.1  macallan 			    struct wsdisplay_cmap *);
    133   1.1  macallan static int 	chipsfb_getcmap(struct chipsfb_softc *,
    134   1.1  macallan 			    struct wsdisplay_cmap *);
    135   1.1  macallan static int 	chipsfb_putpalreg(struct chipsfb_softc *, uint8_t, uint8_t,
    136   1.1  macallan 			    uint8_t, uint8_t);
    137   1.1  macallan 
    138   1.1  macallan static void	chipsfb_bitblt(struct chipsfb_softc *, int, int, int, int,
    139   1.1  macallan 			    int, int, uint8_t);
    140   1.1  macallan static void	chipsfb_rectfill(struct chipsfb_softc *, int, int, int, int,
    141   1.1  macallan 			    int);
    142   1.1  macallan static void	chipsfb_putchar(void *, int, int, u_int, long);
    143   1.1  macallan static void	chipsfb_setup_mono(struct chipsfb_softc *, int, int, int,
    144   1.8  macallan 			    int, uint32_t, uint32_t);
    145   1.2  macallan static void	chipsfb_feed(struct chipsfb_softc *, int, uint8_t *);
    146   1.2  macallan 
    147   1.8  macallan #if 0
    148   1.1  macallan static void	chipsfb_showpal(struct chipsfb_softc *);
    149   1.1  macallan #endif
    150   1.1  macallan static void	chipsfb_restore_palette(struct chipsfb_softc *);
    151   1.1  macallan 
    152   1.1  macallan static void	chipsfb_wait_idle(struct chipsfb_softc *);
    153   1.1  macallan 
    154   1.4  macallan static uint32_t chipsfb_probe_vram(struct chipsfb_softc *);
    155   1.4  macallan 
    156   1.1  macallan struct wsscreen_descr chipsfb_defaultscreen = {
    157  1.24    cegger 	"default",	/* name */
    158  1.24    cegger 	0, 0,		/* ncols, nrows */
    159  1.24    cegger 	NULL,		/* textops */
    160  1.24    cegger 	8, 16,		/* fontwidth, fontheight */
    161  1.24    cegger 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT, /* capabilities */
    162  1.24    cegger 	NULL,		/* modecookie */
    163   1.1  macallan };
    164   1.1  macallan 
    165   1.1  macallan const struct wsscreen_descr *_chipsfb_scrlist[] = {
    166   1.1  macallan 	&chipsfb_defaultscreen,
    167   1.1  macallan 	/* XXX other formats, graphics screen? */
    168   1.1  macallan };
    169   1.1  macallan 
    170   1.1  macallan struct wsscreen_list chipsfb_screenlist = {
    171   1.1  macallan 	sizeof(_chipsfb_scrlist) / sizeof(struct wsscreen_descr *), _chipsfb_scrlist
    172   1.1  macallan };
    173   1.1  macallan 
    174   1.9  christos static int	chipsfb_ioctl(void *, void *, u_long, void *, int,
    175   1.1  macallan 		    struct lwp *);
    176   1.1  macallan static paddr_t	chipsfb_mmap(void *, void *, off_t, int);
    177   1.1  macallan static void	chipsfb_clearscreen(struct chipsfb_softc *);
    178   1.1  macallan static void	chipsfb_init_screen(void *, struct vcons_screen *, int,
    179   1.1  macallan 			    long *);
    180   1.1  macallan 
    181   1.1  macallan 
    182   1.1  macallan struct wsdisplay_accessops chipsfb_accessops = {
    183   1.1  macallan 	chipsfb_ioctl,
    184   1.1  macallan 	chipsfb_mmap,
    185  1.24    cegger 	NULL,	/* vcons_alloc_screen */
    186  1.24    cegger 	NULL,	/* vcons_free_screen */
    187  1.24    cegger 	NULL,	/* vcons_show_screen */
    188   1.1  macallan 	NULL,	/* load_font */
    189   1.1  macallan 	NULL,	/* polls */
    190   1.1  macallan 	NULL,	/* scroll */
    191   1.1  macallan };
    192   1.1  macallan 
    193   1.1  macallan /*
    194   1.1  macallan  * Inline functions for getting access to register aperture.
    195   1.1  macallan  */
    196   1.1  macallan static inline void
    197   1.1  macallan chipsfb_write32(struct chipsfb_softc *sc, uint32_t reg, uint32_t val)
    198   1.1  macallan {
    199   1.1  macallan 	bus_space_write_4(sc->sc_fbt, sc->sc_fbh, reg, val);
    200   1.1  macallan }
    201   1.1  macallan 
    202   1.1  macallan static inline uint32_t
    203   1.8  macallan chipsfb_read32(struct chipsfb_softc *sc, uint32_t reg)
    204   1.1  macallan {
    205   1.1  macallan 	return bus_space_read_4(sc->sc_fbt, sc->sc_fbh, reg);
    206   1.1  macallan }
    207   1.1  macallan 
    208   1.1  macallan static inline void
    209   1.1  macallan chipsfb_write_vga(struct chipsfb_softc *sc, uint32_t reg,  uint8_t val)
    210   1.8  macallan {
    211   1.8  macallan 	bus_space_write_1(sc->sc_iot, sc->sc_ioregh, reg, val);
    212   1.1  macallan }
    213   1.1  macallan 
    214   1.1  macallan static inline uint8_t
    215   1.1  macallan chipsfb_read_vga(struct chipsfb_softc *sc, uint32_t reg)
    216   1.8  macallan {
    217   1.8  macallan 	return bus_space_read_1(sc->sc_iot, sc->sc_ioregh, reg);
    218   1.1  macallan }
    219   1.1  macallan 
    220   1.1  macallan static inline uint8_t
    221   1.1  macallan chipsfb_read_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index)
    222   1.1  macallan {
    223   1.1  macallan 
    224   1.1  macallan 	chipsfb_write_vga(sc, reg & 0xfffe, index);
    225   1.1  macallan 	return chipsfb_read_vga(sc, reg | 0x0001);
    226   1.1  macallan }
    227   1.1  macallan 
    228   1.1  macallan static inline void
    229   1.8  macallan chipsfb_write_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index,
    230   1.1  macallan     uint8_t val)
    231   1.1  macallan {
    232   1.1  macallan 
    233   1.1  macallan 	chipsfb_write_vga(sc, reg & 0xfffe, index);
    234   1.1  macallan 	chipsfb_write_vga(sc, reg | 0x0001, val);
    235   1.1  macallan }
    236   1.1  macallan 
    237   1.1  macallan static void
    238   1.1  macallan chipsfb_wait_idle(struct chipsfb_softc *sc)
    239   1.1  macallan {
    240   1.1  macallan 
    241   1.3  macallan #ifdef CHIPSFB_DEBUG
    242   1.3  macallan 	chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0);
    243   1.3  macallan #endif
    244   1.3  macallan 
    245   1.1  macallan 	/* spin until the blitter is idle */
    246   1.1  macallan 	while ((chipsfb_read32(sc, CT_BLT_CONTROL) & BLT_IS_BUSY) != 0) {
    247   1.1  macallan 	}
    248   1.3  macallan 
    249   1.3  macallan #ifdef CHIPSFB_DEBUG
    250   1.3  macallan 	chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0xffffffff);
    251   1.3  macallan #endif
    252   1.1  macallan }
    253   1.1  macallan 
    254   1.1  macallan static int
    255  1.17    cegger chipsfb_match(device_t parent, cfdata_t match, void *aux)
    256   1.1  macallan {
    257   1.1  macallan 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    258   1.1  macallan 
    259   1.1  macallan 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    260   1.1  macallan 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    261   1.1  macallan 		return 0;
    262   1.8  macallan 	if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CHIPS) &&
    263   1.1  macallan 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CHIPS_65550))
    264   1.1  macallan 		return 100;
    265   1.8  macallan 	if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CHIPS) &&
    266   1.8  macallan 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CHIPS_65554))
    267   1.8  macallan 		return 100;
    268   1.1  macallan 	return 0;
    269   1.1  macallan }
    270   1.1  macallan 
    271   1.1  macallan static void
    272  1.17    cegger chipsfb_attach(device_t parent, device_t self, void *aux)
    273   1.1  macallan {
    274  1.19    cegger 	struct chipsfb_softc *sc = device_private(self);
    275   1.1  macallan 	struct pci_attach_args *pa = aux;
    276   1.1  macallan 	char devinfo[256];
    277   1.1  macallan 	struct wsemuldisplaydev_attach_args aa;
    278   1.1  macallan 	struct rasops_info *ri;
    279   1.8  macallan 	prop_dictionary_t dict;
    280   1.1  macallan 	pcireg_t screg;
    281   1.1  macallan 	ulong defattr;
    282  1.12        he 	bool console = false;
    283  1.12        he 	int width, height, i, j;
    284   1.1  macallan 	uint32_t bg, fg, ul;
    285   1.8  macallan 
    286   1.8  macallan 	dict = device_properties(self);
    287   1.1  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    288   1.1  macallan 	sc->sc_pc = pa->pa_pc;
    289   1.1  macallan 	sc->sc_pcitag = pa->pa_tag;
    290   1.1  macallan 	sc->sc_dacw = -1;
    291   1.1  macallan 
    292   1.1  macallan 	screg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
    293   1.1  macallan 	screg |= PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
    294   1.1  macallan 	pci_conf_write(sc->sc_pc, sc->sc_pcitag,PCI_COMMAND_STATUS_REG,screg);
    295   1.8  macallan 
    296   1.1  macallan 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    297   1.8  macallan 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    298   1.8  macallan #ifdef CHIPSFB_DEBUG
    299   1.8  macallan 	printf(prop_dictionary_externalize(dict));
    300   1.8  macallan #endif
    301   1.1  macallan 
    302   1.1  macallan 	sc->sc_memt = pa->pa_memt;
    303   1.1  macallan 	sc->sc_iot = pa->pa_iot;
    304   1.1  macallan 
    305   1.1  macallan 	/* the framebuffer */
    306   1.1  macallan 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM,
    307   1.8  macallan 	    BUS_SPACE_MAP_LINEAR,
    308   1.1  macallan 	    &sc->sc_fbt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
    309  1.14    cegger 		aprint_error_dev(&sc->sc_dev, "failed to map the frame buffer.\n");
    310   1.1  macallan 	}
    311   1.1  macallan 
    312   1.1  macallan 	/* IO-mapped registers */
    313  1.11  macallan 	if (bus_space_map(sc->sc_iot, 0x0, 0x400, 0, &sc->sc_ioregh) != 0) {
    314  1.14    cegger 		aprint_error_dev(&sc->sc_dev, "failed to map IO registers.\n");
    315   1.1  macallan 	}
    316   1.1  macallan 
    317   1.4  macallan 	sc->memsize = chipsfb_probe_vram(sc);
    318   1.1  macallan 	chipsfb_init(sc);
    319   1.8  macallan 
    320   1.1  macallan 	/* we should read these from the chip instead of depending on OF */
    321   1.1  macallan 	width = height = -1;
    322   1.1  macallan 
    323   1.1  macallan 	/* detect panel size */
    324   1.1  macallan 	width = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HSIZE_LSB);
    325   1.1  macallan 	width |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HORZ_OVERFLOW_1)
    326   1.1  macallan 	    & 0x0f) << 8;
    327   1.1  macallan 	width = (width + 1) * 8;
    328   1.1  macallan 	height = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VSIZE_LSB);
    329   1.1  macallan 	height |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VERT_OVERFLOW_1)
    330   1.1  macallan 	    & 0x0f) << 8;
    331   1.1  macallan 	height++;
    332   1.8  macallan 	aprint_verbose("Panel size: %d x %d\n", width, height);
    333   1.8  macallan 
    334   1.8  macallan 	if (!prop_dictionary_get_uint32(dict, "width", &sc->width))
    335   1.8  macallan 		sc->width = width;
    336   1.8  macallan 	if (!prop_dictionary_get_uint32(dict, "height", &sc->height))
    337   1.8  macallan 		sc->height = height;
    338   1.8  macallan 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->bits_per_pixel))
    339   1.8  macallan 		sc->bits_per_pixel = 8;
    340   1.8  macallan 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->linebytes))
    341   1.8  macallan 		sc->linebytes = (sc->width * sc->bits_per_pixel) >> 3;
    342   1.1  macallan 
    343   1.8  macallan 	prop_dictionary_get_bool(dict, "is_console", &console);
    344   1.1  macallan 
    345   1.2  macallan #ifdef notyet
    346   1.1  macallan 	/* XXX this should at least be configurable via kernel config */
    347   1.2  macallan 	chipsfb_set_videomode(sc, &videomode_list[16]);
    348   1.2  macallan #endif
    349   1.1  macallan 
    350   1.1  macallan 	vcons_init(&sc->vd, sc, &chipsfb_defaultscreen, &chipsfb_accessops);
    351   1.1  macallan 	sc->vd.init_screen = chipsfb_init_screen;
    352   1.1  macallan 
    353   1.1  macallan 	ri = &chipsfb_console_screen.scr_ri;
    354   1.1  macallan 	if (console) {
    355   1.1  macallan 		vcons_init_screen(&sc->vd, &chipsfb_console_screen, 1,
    356   1.1  macallan 		    &defattr);
    357   1.1  macallan 		chipsfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    358   1.1  macallan 
    359   1.1  macallan 		chipsfb_defaultscreen.textops = &ri->ri_ops;
    360   1.1  macallan 		chipsfb_defaultscreen.capabilities = ri->ri_caps;
    361   1.1  macallan 		chipsfb_defaultscreen.nrows = ri->ri_rows;
    362   1.1  macallan 		chipsfb_defaultscreen.ncols = ri->ri_cols;
    363   1.1  macallan 		wsdisplay_cnattach(&chipsfb_defaultscreen, ri, 0, 0, defattr);
    364   1.1  macallan 	} else {
    365   1.1  macallan 		/*
    366   1.1  macallan 		 * since we're not the console we can postpone the rest
    367   1.1  macallan 		 * until someone actually allocates a screen for us
    368   1.1  macallan 		 */
    369   1.2  macallan #ifdef notyet
    370   1.2  macallan 		chipsfb_set_videomode(sc, &videomode_list[0]);
    371   1.2  macallan #endif
    372   1.1  macallan 	}
    373   1.1  macallan 
    374   1.1  macallan 	rasops_unpack_attr(defattr, &fg, &bg, &ul);
    375   1.1  macallan 	sc->sc_bg = ri->ri_devcmap[bg];
    376   1.1  macallan 	chipsfb_clearscreen(sc);
    377   1.1  macallan 
    378  1.20  macallan 	if (console)
    379  1.20  macallan 		vcons_replay_msgbuf(&chipsfb_console_screen);
    380  1.20  macallan 
    381  1.14    cegger 	aprint_normal_dev(&sc->sc_dev, "%d MB aperture, %d MB VRAM at 0x%08x\n",
    382  1.14    cegger 	    (u_int)(sc->sc_fbsize >> 20),
    383   1.4  macallan 	    sc->memsize >> 20, (u_int)sc->sc_fb);
    384   1.8  macallan #ifdef CHIPSFB_DEBUG
    385   1.8  macallan 	aprint_debug("fb: %08lx\n", (ulong)ri->ri_bits);
    386   1.1  macallan #endif
    387   1.8  macallan 
    388   1.1  macallan 	j = 0;
    389   1.1  macallan 	for (i = 0; i < 256; i++) {
    390   1.8  macallan 		chipsfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    391   1.1  macallan 		    rasops_cmap[j + 2]);
    392   1.1  macallan 		j += 3;
    393   1.1  macallan 	}
    394   1.1  macallan 
    395   1.1  macallan 	aa.console = console;
    396   1.1  macallan 	aa.scrdata = &chipsfb_screenlist;
    397   1.1  macallan 	aa.accessops = &chipsfb_accessops;
    398   1.1  macallan 	aa.accesscookie = &sc->vd;
    399   1.1  macallan 
    400   1.1  macallan 	config_found(self, &aa, wsemuldisplaydevprint);
    401   1.1  macallan }
    402   1.1  macallan 
    403   1.1  macallan static int
    404   1.8  macallan chipsfb_putpalreg(struct chipsfb_softc *sc, uint8_t index, uint8_t r,
    405   1.1  macallan     uint8_t g, uint8_t b)
    406   1.1  macallan {
    407   1.8  macallan 
    408   1.1  macallan 	sc->sc_cmap_red[index] = r;
    409   1.1  macallan 	sc->sc_cmap_green[index] = g;
    410   1.1  macallan 	sc->sc_cmap_blue[index] = b;
    411   1.1  macallan 
    412   1.1  macallan 	chipsfb_write_vga(sc, CT_DACMASK, 0xff);
    413   1.1  macallan 	chipsfb_write_vga(sc, CT_WRITEINDEX, index);
    414   1.1  macallan 	chipsfb_write_vga(sc, CT_DACDATA, r);
    415   1.1  macallan 	chipsfb_write_vga(sc, CT_DACDATA, g);
    416   1.1  macallan 	chipsfb_write_vga(sc, CT_DACDATA, b);
    417   1.1  macallan 
    418   1.1  macallan 	return 0;
    419   1.1  macallan }
    420   1.1  macallan 
    421   1.1  macallan static int
    422   1.1  macallan chipsfb_putcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
    423   1.1  macallan {
    424   1.1  macallan 	u_char *r, *g, *b;
    425   1.1  macallan 	u_int index = cm->index;
    426   1.1  macallan 	u_int count = cm->count;
    427   1.1  macallan 	int i, error;
    428   1.1  macallan 	u_char rbuf[256], gbuf[256], bbuf[256];
    429   1.1  macallan 
    430   1.8  macallan #ifdef CHIPSFB_DEBUG
    431   1.8  macallan 	aprint_debug("putcmap: %d %d\n",index, count);
    432   1.8  macallan #endif
    433   1.1  macallan 	if (cm->index >= 256 || cm->count > 256 ||
    434   1.1  macallan 	    (cm->index + cm->count) > 256)
    435   1.1  macallan 		return EINVAL;
    436   1.1  macallan 	error = copyin(cm->red, &rbuf[index], count);
    437   1.1  macallan 	if (error)
    438   1.1  macallan 		return error;
    439   1.1  macallan 	error = copyin(cm->green, &gbuf[index], count);
    440   1.1  macallan 	if (error)
    441   1.1  macallan 		return error;
    442   1.1  macallan 	error = copyin(cm->blue, &bbuf[index], count);
    443   1.1  macallan 	if (error)
    444   1.1  macallan 		return error;
    445   1.1  macallan 
    446   1.1  macallan 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    447   1.1  macallan 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    448   1.1  macallan 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    449   1.1  macallan 
    450   1.1  macallan 	r = &sc->sc_cmap_red[index];
    451   1.1  macallan 	g = &sc->sc_cmap_green[index];
    452   1.1  macallan 	b = &sc->sc_cmap_blue[index];
    453   1.8  macallan 
    454   1.1  macallan 	for (i = 0; i < count; i++) {
    455   1.1  macallan 		chipsfb_putpalreg(sc, index, *r, *g, *b);
    456   1.1  macallan 		index++;
    457   1.1  macallan 		r++, g++, b++;
    458   1.1  macallan 	}
    459   1.1  macallan 	return 0;
    460   1.1  macallan }
    461   1.1  macallan 
    462   1.1  macallan static int
    463   1.1  macallan chipsfb_getcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
    464   1.1  macallan {
    465   1.1  macallan 	u_int index = cm->index;
    466   1.1  macallan 	u_int count = cm->count;
    467   1.1  macallan 	int error;
    468   1.1  macallan 
    469   1.1  macallan 	if (index >= 255 || count > 256 || index + count > 256)
    470   1.1  macallan 		return EINVAL;
    471   1.8  macallan 
    472   1.1  macallan 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    473   1.1  macallan 	if (error)
    474   1.1  macallan 		return error;
    475   1.1  macallan 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    476   1.1  macallan 	if (error)
    477   1.1  macallan 		return error;
    478   1.1  macallan 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    479   1.1  macallan 	if (error)
    480   1.1  macallan 		return error;
    481   1.1  macallan 
    482   1.1  macallan 	return 0;
    483   1.1  macallan }
    484   1.1  macallan 
    485   1.1  macallan static void
    486   1.1  macallan chipsfb_clearscreen(struct chipsfb_softc *sc)
    487   1.1  macallan {
    488   1.1  macallan 	chipsfb_rectfill(sc, 0, 0, sc->width, sc->height, sc->sc_bg);
    489   1.1  macallan }
    490   1.1  macallan 
    491   1.1  macallan /*
    492   1.1  macallan  * wsdisplay_emulops
    493   1.1  macallan  */
    494   1.1  macallan 
    495   1.1  macallan static void
    496   1.1  macallan chipsfb_cursor(void *cookie, int on, int row, int col)
    497   1.1  macallan {
    498   1.1  macallan 	struct rasops_info *ri = cookie;
    499   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    500   1.1  macallan 	struct chipsfb_softc *sc = scr->scr_cookie;
    501   1.1  macallan 	int x, y, wi, he;
    502   1.8  macallan 
    503   1.1  macallan 	wi = ri->ri_font->fontwidth;
    504   1.1  macallan 	he = ri->ri_font->fontheight;
    505   1.8  macallan 
    506   1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    507   1.1  macallan 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    508   1.1  macallan 		y = ri->ri_crow * he + ri->ri_yorigin;
    509   1.1  macallan 		if (ri->ri_flg & RI_CURSOR) {
    510   1.1  macallan 			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
    511   1.1  macallan 			ri->ri_flg &= ~RI_CURSOR;
    512   1.1  macallan 		}
    513   1.1  macallan 		ri->ri_crow = row;
    514   1.1  macallan 		ri->ri_ccol = col;
    515   1.1  macallan 		if (on) {
    516   1.1  macallan 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    517   1.1  macallan 			y = ri->ri_crow * he + ri->ri_yorigin;
    518   1.1  macallan 			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
    519   1.1  macallan 			ri->ri_flg |= RI_CURSOR;
    520   1.1  macallan 		}
    521   1.1  macallan 	} else {
    522   1.1  macallan 		ri->ri_flg &= ~RI_CURSOR;
    523   1.1  macallan 		ri->ri_crow = row;
    524   1.1  macallan 		ri->ri_ccol = col;
    525   1.1  macallan 	}
    526   1.1  macallan }
    527   1.1  macallan 
    528   1.1  macallan #if 0
    529   1.1  macallan int
    530   1.1  macallan chipsfb_mapchar(void *cookie, int uni, u_int *index)
    531   1.1  macallan {
    532   1.1  macallan 	return 0;
    533   1.1  macallan }
    534   1.1  macallan #endif
    535   1.1  macallan 
    536   1.1  macallan static void
    537   1.1  macallan chipsfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    538   1.1  macallan {
    539   1.1  macallan 	struct rasops_info *ri = cookie;
    540   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    541   1.1  macallan 	struct chipsfb_softc *sc = scr->scr_cookie;
    542   1.1  macallan 	int32_t xs, xd, y, width, height;
    543   1.8  macallan 
    544   1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    545   1.1  macallan 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    546   1.1  macallan 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    547   1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    548   1.1  macallan 		width = ri->ri_font->fontwidth * ncols;
    549   1.8  macallan 		height = ri->ri_font->fontheight;
    550   1.1  macallan 		chipsfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
    551   1.1  macallan 	}
    552   1.1  macallan }
    553   1.1  macallan 
    554   1.1  macallan static void
    555   1.8  macallan chipsfb_erasecols(void *cookie, int row, int startcol, int ncols,
    556   1.1  macallan     long fillattr)
    557   1.1  macallan {
    558   1.1  macallan 	struct rasops_info *ri = cookie;
    559   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    560   1.1  macallan 	struct chipsfb_softc *sc = scr->scr_cookie;
    561   1.1  macallan 	int32_t x, y, width, height, fg, bg, ul;
    562   1.8  macallan 
    563   1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    564   1.1  macallan 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    565   1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    566   1.1  macallan 		width = ri->ri_font->fontwidth * ncols;
    567   1.8  macallan 		height = ri->ri_font->fontheight;
    568   1.1  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    569   1.8  macallan 
    570   1.1  macallan 		chipsfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    571   1.1  macallan 	}
    572   1.1  macallan }
    573   1.1  macallan 
    574   1.1  macallan static void
    575   1.1  macallan chipsfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    576   1.1  macallan {
    577   1.1  macallan 	struct rasops_info *ri = cookie;
    578   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    579   1.1  macallan 	struct chipsfb_softc *sc = scr->scr_cookie;
    580   1.1  macallan 	int32_t x, ys, yd, width, height;
    581   1.1  macallan 
    582   1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    583   1.1  macallan 		x = ri->ri_xorigin;
    584   1.1  macallan 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    585   1.1  macallan 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    586   1.1  macallan 		width = ri->ri_emuwidth;
    587   1.8  macallan 		height = ri->ri_font->fontheight * nrows;
    588   1.1  macallan 		chipsfb_bitblt(sc, x, ys, x, yd, width, height, ROP_COPY);
    589   1.1  macallan 	}
    590   1.1  macallan }
    591   1.1  macallan 
    592   1.1  macallan static void
    593   1.1  macallan chipsfb_eraserows(void *cookie, int row, int nrows, long fillattr)
    594   1.1  macallan {
    595   1.1  macallan 	struct rasops_info *ri = cookie;
    596   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    597   1.1  macallan 	struct chipsfb_softc *sc = scr->scr_cookie;
    598   1.1  macallan 	int32_t x, y, width, height, fg, bg, ul;
    599   1.1  macallan 
    600   1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    601   1.1  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    602   1.1  macallan 		if ((row == 0) && (nrows == ri->ri_rows)) {
    603   1.1  macallan 			/* clear the whole screen */
    604   1.1  macallan 			chipsfb_rectfill(sc, 0, 0, ri->ri_width,
    605   1.1  macallan 			    ri->ri_height, ri->ri_devcmap[bg]);
    606   1.1  macallan 		} else {
    607   1.1  macallan 			x = ri->ri_xorigin;
    608   1.1  macallan 			y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    609   1.1  macallan 			width = ri->ri_emuwidth;
    610   1.8  macallan 			height = ri->ri_font->fontheight * nrows;
    611   1.1  macallan 			chipsfb_rectfill(sc, x, y, width, height,
    612   1.1  macallan 			    ri->ri_devcmap[bg]);
    613   1.1  macallan 		}
    614   1.1  macallan 	}
    615   1.1  macallan }
    616   1.1  macallan 
    617   1.1  macallan static void
    618   1.1  macallan chipsfb_bitblt(struct chipsfb_softc *sc, int xs, int ys, int xd, int yd,
    619   1.8  macallan     int width, int height, uint8_t rop)
    620   1.1  macallan {
    621   1.1  macallan 	uint32_t src, dst, cmd = rop, stride, size;
    622   1.1  macallan 
    623   1.1  macallan 	cmd |= BLT_PAT_IS_SOLID;
    624   1.1  macallan 
    625   1.1  macallan 	/* we assume 8 bit for now */
    626   1.1  macallan 	src = xs + ys * sc->linebytes;
    627   1.1  macallan 	dst = xd + yd * sc->linebytes;
    628   1.1  macallan 
    629   1.1  macallan 	if (xs < xd) {
    630   1.1  macallan 		/* right-to-left operation */
    631   1.1  macallan 		cmd |= BLT_START_RIGHT;
    632   1.5  macallan 		src += width - 1;
    633   1.5  macallan 		dst += width - 1;
    634   1.1  macallan 	}
    635   1.1  macallan 
    636   1.1  macallan 	if (ys < yd) {
    637   1.1  macallan 		/* bottom-to-top operation */
    638   1.1  macallan 		cmd |= BLT_START_BOTTOM;
    639   1.1  macallan 		src += (height - 1) * sc->linebytes;
    640   1.1  macallan 		dst += (height - 1) * sc->linebytes;
    641   1.1  macallan 	}
    642   1.8  macallan 
    643   1.1  macallan 	stride = (sc->linebytes << 16) | sc->linebytes;
    644   1.1  macallan 	size = (height << 16) | width;
    645   1.8  macallan 
    646   1.2  macallan 	chipsfb_wait_idle(sc);
    647   1.1  macallan 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    648   1.1  macallan 	chipsfb_write32(sc, CT_BLT_SRCADDR, src);
    649   1.1  macallan 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    650   1.1  macallan 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    651   1.1  macallan 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    652   1.2  macallan #ifdef CHIPSFB_WAIT
    653   1.1  macallan 	chipsfb_wait_idle(sc);
    654   1.2  macallan #endif
    655   1.1  macallan }
    656   1.8  macallan 
    657   1.1  macallan static void
    658   1.8  macallan chipsfb_rectfill(struct chipsfb_softc *sc, int x, int y, int width,
    659   1.8  macallan     int height, int colour)
    660   1.1  macallan {
    661   1.1  macallan 	uint32_t dst, cmd, stride, size;
    662   1.1  macallan 
    663   1.1  macallan 	cmd = BLT_PAT_IS_SOLID | BLT_PAT_IS_MONO | ROP_PAT;
    664   1.1  macallan 
    665   1.1  macallan 	/* we assume 8 bit for now */
    666   1.1  macallan 	dst = x + y * sc->linebytes;
    667   1.1  macallan 
    668   1.1  macallan 	stride = (sc->linebytes << 16) | sc->linebytes;
    669   1.1  macallan 	size = (height << 16) | width;
    670   1.8  macallan 
    671   1.2  macallan 	chipsfb_wait_idle(sc);
    672   1.1  macallan 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    673   1.1  macallan 	chipsfb_write32(sc, CT_BLT_SRCADDR, dst);
    674   1.1  macallan 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    675   1.1  macallan 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    676   1.1  macallan 	chipsfb_write32(sc, CT_BLT_BG, colour);
    677   1.1  macallan 	chipsfb_write32(sc, CT_BLT_FG, colour);
    678   1.1  macallan 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    679   1.2  macallan #ifdef CHIPSFB_WAIT
    680   1.1  macallan 	chipsfb_wait_idle(sc);
    681   1.2  macallan #endif
    682   1.1  macallan }
    683   1.1  macallan 
    684   1.1  macallan static void
    685   1.1  macallan chipsfb_putchar(void *cookie, int row, int col, u_int c, long attr)
    686   1.1  macallan {
    687   1.1  macallan 	struct rasops_info *ri = cookie;
    688  1.21  macallan 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    689   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    690   1.1  macallan 	struct chipsfb_softc *sc = scr->scr_cookie;
    691   1.1  macallan 
    692  1.10       mjf 	if (__predict_false((unsigned int)row > ri->ri_rows ||
    693  1.10       mjf 	    (unsigned int)col > ri->ri_cols))
    694  1.10       mjf 		return;
    695  1.10       mjf 
    696   1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    697   1.1  macallan 		uint8_t *data;
    698   1.2  macallan 		int fg, bg, uc;
    699   1.1  macallan 		int x, y, wi, he;
    700   1.1  macallan 
    701  1.21  macallan 		wi = font->fontwidth;
    702  1.21  macallan 		he = font->fontheight;
    703   1.1  macallan 
    704  1.21  macallan 		if (!CHAR_IN_FONT(c, font))
    705   1.1  macallan 			return;
    706   1.1  macallan 		bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
    707   1.1  macallan 		fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
    708   1.1  macallan 		x = ri->ri_xorigin + col * wi;
    709   1.1  macallan 		y = ri->ri_yorigin + row * he;
    710   1.1  macallan 		if (c == 0x20) {
    711   1.1  macallan 			chipsfb_rectfill(sc, x, y, wi, he, bg);
    712   1.1  macallan 		} else {
    713  1.21  macallan 			uc = c - font->firstchar;
    714  1.21  macallan 			data = (uint8_t *)font->data + uc *
    715   1.1  macallan 			    ri->ri_fontscale;
    716   1.2  macallan 			chipsfb_setup_mono(sc, x, y, wi, he, fg, bg);
    717  1.21  macallan 			chipsfb_feed(sc, font->stride * he, data);
    718   1.1  macallan 		}
    719   1.1  macallan 	}
    720   1.1  macallan }
    721   1.1  macallan 
    722   1.8  macallan static void
    723   1.1  macallan chipsfb_setup_mono(struct chipsfb_softc *sc, int xd, int yd, int width,
    724   1.8  macallan     int height, uint32_t fg, uint32_t bg)
    725   1.1  macallan {
    726   1.2  macallan 	uint32_t dst, cmd, stride, size;
    727   1.2  macallan 
    728   1.2  macallan 	cmd = BLT_PAT_IS_SOLID | BLT_SRC_IS_CPU | BLT_SRC_IS_MONO | ROP_COPY;
    729   1.2  macallan 
    730   1.2  macallan 	/* we assume 8 bit for now */
    731   1.2  macallan 	dst = xd + yd * sc->linebytes;
    732   1.8  macallan 
    733   1.2  macallan 	stride = (sc->linebytes << 16);
    734   1.2  macallan 	size = (height << 16) | width;
    735   1.8  macallan 
    736   1.2  macallan 	chipsfb_wait_idle(sc);
    737   1.2  macallan 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    738   1.2  macallan 	chipsfb_write32(sc, CT_BLT_EXPCTL, MONO_SRC_ALIGN_BYTE);
    739   1.2  macallan 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    740   1.2  macallan 	chipsfb_write32(sc, CT_BLT_SRCADDR, 0);
    741   1.2  macallan 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    742   1.2  macallan 	chipsfb_write32(sc, CT_BLT_BG, bg);
    743   1.2  macallan 	chipsfb_write32(sc, CT_BLT_FG, fg);
    744   1.2  macallan 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    745   1.1  macallan }
    746   1.1  macallan 
    747   1.8  macallan static void
    748   1.2  macallan chipsfb_feed(struct chipsfb_softc *sc, int count, uint8_t *data)
    749   1.1  macallan {
    750   1.2  macallan 	int i;
    751   1.2  macallan 	uint32_t latch = 0, bork;
    752   1.2  macallan 	int shift = 0;
    753   1.8  macallan 
    754   1.2  macallan 	for (i = 0; i < count; i++) {
    755   1.2  macallan 		bork = data[i];
    756   1.2  macallan 		latch |= (bork << shift);
    757   1.2  macallan 		if (shift == 24) {
    758   1.2  macallan 			chipsfb_write32(sc, CT_OFF_DATA, latch);
    759   1.2  macallan 			latch = 0;
    760   1.2  macallan 			shift = 0;
    761   1.2  macallan 		} else
    762   1.2  macallan 			shift += 8;
    763   1.2  macallan 	}
    764   1.8  macallan 
    765   1.2  macallan 	if (shift != 0) {
    766   1.2  macallan 		chipsfb_write32(sc, CT_OFF_DATA, latch);
    767   1.3  macallan 	}
    768   1.8  macallan 
    769   1.3  macallan 	/* apparently the chip wants 64bit-aligned data or it won't go idle */
    770   1.3  macallan 	if ((count + 3) & 0x04) {
    771   1.2  macallan 		chipsfb_write32(sc, CT_OFF_DATA, 0);
    772   1.8  macallan 	}
    773   1.2  macallan #ifdef CHIPSFB_WAIT
    774   1.2  macallan 	chipsfb_wait_idle(sc);
    775   1.2  macallan #endif
    776   1.8  macallan }
    777   1.1  macallan 
    778   1.8  macallan #if 0
    779   1.1  macallan static void
    780   1.8  macallan chipsfb_showpal(struct chipsfb_softc *sc)
    781   1.1  macallan {
    782   1.1  macallan 	int i, x = 0;
    783   1.8  macallan 
    784   1.1  macallan 	for (i = 0; i < 16; i++) {
    785   1.1  macallan 		chipsfb_rectfill(sc, x, 0, 64, 64, i);
    786   1.1  macallan 		x += 64;
    787   1.1  macallan 	}
    788   1.1  macallan }
    789   1.1  macallan #endif
    790   1.1  macallan 
    791   1.1  macallan #if 0
    792   1.1  macallan static int
    793   1.1  macallan chipsfb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
    794   1.1  macallan {
    795   1.1  macallan 
    796   1.1  macallan 	return 0;
    797   1.1  macallan }
    798   1.1  macallan #endif
    799   1.1  macallan 
    800   1.1  macallan static void
    801   1.1  macallan chipsfb_restore_palette(struct chipsfb_softc *sc)
    802   1.1  macallan {
    803   1.1  macallan 	int i;
    804   1.8  macallan 
    805   1.1  macallan 	for (i = 0; i < 256; i++) {
    806   1.8  macallan 		chipsfb_putpalreg(sc,
    807   1.8  macallan 		   i,
    808   1.8  macallan 		   sc->sc_cmap_red[i],
    809   1.1  macallan 		   sc->sc_cmap_green[i],
    810   1.1  macallan 		   sc->sc_cmap_blue[i]);
    811   1.1  macallan 	}
    812   1.1  macallan }
    813   1.1  macallan 
    814   1.1  macallan /*
    815   1.1  macallan  * wsdisplay_accessops
    816   1.1  macallan  */
    817   1.1  macallan 
    818   1.1  macallan static int
    819   1.9  christos chipsfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    820   1.1  macallan 	struct lwp *l)
    821   1.1  macallan {
    822   1.1  macallan 	struct vcons_data *vd = v;
    823   1.1  macallan 	struct chipsfb_softc *sc = vd->cookie;
    824   1.1  macallan 	struct wsdisplay_fbinfo *wdf;
    825   1.1  macallan 	struct vcons_screen *ms = vd->active;
    826   1.1  macallan 
    827   1.1  macallan 	switch (cmd) {
    828  1.23    cegger 	case WSDISPLAYIO_GTYPE:
    829  1.23    cegger 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    830  1.23    cegger 		return 0;
    831  1.23    cegger 
    832  1.23    cegger 	case WSDISPLAYIO_GINFO:
    833  1.23    cegger 		wdf = (void *)data;
    834  1.23    cegger 		wdf->height = ms->scr_ri.ri_height;
    835  1.23    cegger 		wdf->width = ms->scr_ri.ri_width;
    836  1.23    cegger 		wdf->depth = ms->scr_ri.ri_depth;
    837  1.23    cegger 		wdf->cmsize = 256;
    838  1.23    cegger 		return 0;
    839  1.23    cegger 
    840  1.23    cegger 	case WSDISPLAYIO_GETCMAP:
    841  1.23    cegger 		return chipsfb_getcmap(sc,
    842  1.23    cegger 		    (struct wsdisplay_cmap *)data);
    843  1.23    cegger 
    844  1.23    cegger 	case WSDISPLAYIO_PUTCMAP:
    845  1.23    cegger 		return chipsfb_putcmap(sc,
    846  1.23    cegger 		    (struct wsdisplay_cmap *)data);
    847  1.23    cegger 
    848  1.23    cegger 	/* PCI config read/write passthrough. */
    849  1.23    cegger 	case PCI_IOC_CFGREAD:
    850  1.23    cegger 	case PCI_IOC_CFGWRITE:
    851  1.23    cegger 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    852  1.23    cegger 		    cmd, data, flag, l);
    853  1.23    cegger 
    854  1.25    cegger 	case WSDISPLAYIO_GET_BUSID:
    855  1.25    cegger 		return wsdisplayio_busid_pci(&sc->sc_dev, sc->sc_pc,
    856  1.25    cegger 		    sc->sc_pcitag, data);
    857  1.25    cegger 
    858  1.23    cegger 	case WSDISPLAYIO_SMODE: {
    859  1.23    cegger 		int new_mode = *(int*)data;
    860  1.23    cegger 		if (new_mode != sc->sc_mode) {
    861  1.23    cegger 			sc->sc_mode = new_mode;
    862  1.23    cegger 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    863  1.23    cegger 				chipsfb_restore_palette(sc);
    864  1.23    cegger 				vcons_redraw_screen(ms);
    865   1.1  macallan 			}
    866  1.23    cegger 		}
    867  1.23    cegger 		}
    868  1.23    cegger 		return 0;
    869   1.1  macallan 	}
    870   1.1  macallan 	return EPASSTHROUGH;
    871   1.1  macallan }
    872   1.1  macallan 
    873   1.1  macallan static paddr_t
    874   1.1  macallan chipsfb_mmap(void *v, void *vs, off_t offset, int prot)
    875   1.1  macallan {
    876   1.1  macallan 	struct vcons_data *vd = v;
    877   1.1  macallan 	struct chipsfb_softc *sc = vd->cookie;
    878   1.1  macallan 	paddr_t pa;
    879   1.8  macallan 
    880   1.1  macallan 	/* 'regular' framebuffer mmap()ing */
    881   1.4  macallan 	if (offset < sc->memsize) {
    882   1.8  macallan 		pa = bus_space_mmap(sc->sc_fbt, offset, 0, prot,
    883   1.8  macallan 		    BUS_SPACE_MAP_LINEAR);
    884   1.1  macallan 		return pa;
    885   1.1  macallan 	}
    886   1.1  macallan 
    887   1.1  macallan 	/*
    888   1.1  macallan 	 * restrict all other mappings to processes with superuser privileges
    889   1.1  macallan 	 * or the kernel itself
    890   1.1  macallan 	 */
    891  1.18      elad 	if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
    892  1.18      elad 	    NULL) != 0) {
    893  1.18      elad 		aprint_normal_dev(&sc->sc_dev, "mmap() rejected.\n");
    894  1.18      elad 		return -1;
    895   1.1  macallan 	}
    896   1.1  macallan 
    897   1.1  macallan 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    898   1.8  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    899   1.8  macallan 		    BUS_SPACE_MAP_LINEAR);
    900   1.1  macallan 		return pa;
    901   1.1  macallan 	}
    902   1.1  macallan 
    903  1.13  macallan #ifdef PCI_MAGIC_IO_RANGE
    904   1.1  macallan 	/* allow mapping of IO space */
    905  1.13  macallan 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    906  1.13  macallan 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    907  1.13  macallan 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    908  1.13  macallan 		    0, prot, BUS_SPACE_MAP_LINEAR);
    909   1.1  macallan 		return pa;
    910   1.8  macallan 	}
    911   1.1  macallan #endif
    912   1.8  macallan 
    913   1.1  macallan #ifdef OFB_ALLOW_OTHERS
    914   1.1  macallan 	if (offset >= 0x80000000) {
    915   1.8  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    916   1.8  macallan 		    BUS_SPACE_MAP_LINEAR);
    917   1.1  macallan 		return pa;
    918   1.8  macallan 	}
    919   1.1  macallan #endif
    920   1.1  macallan 	return -1;
    921   1.1  macallan }
    922   1.1  macallan 
    923   1.1  macallan static void
    924   1.1  macallan chipsfb_init_screen(void *cookie, struct vcons_screen *scr,
    925   1.1  macallan     int existing, long *defattr)
    926   1.1  macallan {
    927   1.1  macallan 	struct chipsfb_softc *sc = cookie;
    928   1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    929   1.8  macallan 
    930   1.1  macallan 	ri->ri_depth = sc->bits_per_pixel;
    931   1.1  macallan 	ri->ri_width = sc->width;
    932   1.1  macallan 	ri->ri_height = sc->height;
    933   1.1  macallan 	ri->ri_stride = sc->width;
    934   1.1  macallan 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    935   1.1  macallan 
    936   1.1  macallan 	ri->ri_bits = bus_space_vaddr(sc->sc_fbt, sc->sc_fbh);
    937   1.1  macallan 
    938   1.1  macallan #ifdef CHIPSFB_DEBUG
    939   1.8  macallan 	aprint_debug("addr: %08lx\n", (ulong)ri->ri_bits);
    940   1.1  macallan #endif
    941   1.1  macallan 	if (existing) {
    942   1.1  macallan 		ri->ri_flg |= RI_CLEAR;
    943   1.1  macallan 	}
    944   1.8  macallan 
    945   1.1  macallan 	rasops_init(ri, sc->height/8, sc->width/8);
    946   1.1  macallan 	ri->ri_caps = WSSCREEN_WSCOLORS;
    947   1.1  macallan 
    948   1.1  macallan 	rasops_reconfig(ri, sc->height / ri->ri_font->fontheight,
    949   1.1  macallan 		    sc->width / ri->ri_font->fontwidth);
    950   1.1  macallan 
    951   1.1  macallan 	ri->ri_hw = scr;
    952   1.1  macallan 	ri->ri_ops.copyrows = chipsfb_copyrows;
    953   1.1  macallan 	ri->ri_ops.copycols = chipsfb_copycols;
    954   1.1  macallan 	ri->ri_ops.eraserows = chipsfb_eraserows;
    955   1.1  macallan 	ri->ri_ops.erasecols = chipsfb_erasecols;
    956   1.1  macallan 	ri->ri_ops.cursor = chipsfb_cursor;
    957   1.1  macallan 	ri->ri_ops.putchar = chipsfb_putchar;
    958   1.1  macallan }
    959   1.1  macallan 
    960   1.1  macallan #if 0
    961   1.1  macallan int
    962   1.1  macallan chipsfb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
    963   1.1  macallan {
    964   1.1  macallan 
    965   1.1  macallan 	return 0;
    966   1.1  macallan }
    967   1.1  macallan #endif
    968   1.1  macallan 
    969   1.1  macallan static void
    970   1.1  macallan chipsfb_init(struct chipsfb_softc *sc)
    971   1.1  macallan {
    972   1.1  macallan 
    973   1.1  macallan 	chipsfb_wait_idle(sc);
    974   1.8  macallan 
    975   1.1  macallan 	chipsfb_write_indexed(sc, CT_CONF_INDEX, XR_IO_CONTROL,
    976   1.1  macallan 	    ENABLE_CRTC_EXT | ENABLE_ATTR_EXT);
    977   1.1  macallan 	chipsfb_write_indexed(sc, CT_CONF_INDEX, XR_ADDR_MAPPING,
    978   1.1  macallan 	    ENABLE_LINEAR);
    979   1.1  macallan 
    980   1.1  macallan 	/* setup the blitter */
    981   1.1  macallan }
    982   1.4  macallan 
    983   1.4  macallan static uint32_t
    984   1.4  macallan chipsfb_probe_vram(struct chipsfb_softc *sc)
    985   1.4  macallan {
    986   1.4  macallan 	uint32_t ofs = 0x00080000;	/* 512kB */
    987   1.8  macallan 
    988   1.4  macallan 	/*
    989   1.4  macallan 	 * advance in 0.5MB steps, see if we can read back what we wrote and
    990   1.4  macallan 	 * if what we wrote to 0 is left untouched. Max. fb size is 4MB so
    991   1.4  macallan 	 * we voluntarily stop there.
    992   1.4  macallan 	 */
    993   1.4  macallan 	chipsfb_write32(sc, 0, 0xf0f0f0f0);
    994   1.4  macallan 	chipsfb_write32(sc, ofs, 0x0f0f0f0f);
    995   1.8  macallan 	while ((chipsfb_read32(sc, 0) == 0xf0f0f0f0) &&
    996   1.4  macallan 	    (chipsfb_read32(sc, ofs) == 0x0f0f0f0f) &&
    997   1.4  macallan 	    (ofs < 0x00400000)) {
    998   1.8  macallan 
    999   1.4  macallan 		ofs += 0x00080000;
   1000   1.4  macallan 		chipsfb_write32(sc, ofs, 0x0f0f0f0f);
   1001   1.4  macallan 	}
   1002   1.8  macallan 
   1003   1.4  macallan 	return ofs;
   1004   1.4  macallan }
   1005