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