Module Name: src Committed By: macallan Date: Wed Aug 31 16:47:31 UTC 2011
Modified Files: src/sys/dev/pci: files.pci Added Files: src/sys/dev/pci: voyager.c voyagervar.h src/sys/dev/pci/voyager: files.voyager voyagerfb.c Log Message: split up voyagerfb into the framebuffer portion and a pseudo bus that we can attach all the other sub-devices to while there attach an i2c bus To generate a diff of this commit: cvs rdiff -u -r1.347 -r1.348 src/sys/dev/pci/files.pci cvs rdiff -u -r0 -r1.1 src/sys/dev/pci/voyager.c src/sys/dev/pci/voyagervar.h cvs rdiff -u -r0 -r1.1 src/sys/dev/pci/voyager/files.voyager \ src/sys/dev/pci/voyager/voyagerfb.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/dev/pci/files.pci diff -u src/sys/dev/pci/files.pci:1.347 src/sys/dev/pci/files.pci:1.348 --- src/sys/dev/pci/files.pci:1.347 Sat Aug 27 13:28:37 2011 +++ src/sys/dev/pci/files.pci Wed Aug 31 16:47:31 2011 @@ -1,4 +1,4 @@ -# $NetBSD: files.pci,v 1.347 2011/08/27 13:28:37 bouyer Exp $ +# $NetBSD: files.pci,v 1.348 2011/08/31 16:47:31 macallan Exp $ # # Config file and device description for machine-independent PCI code. # Included by ports that need it. Requires that the SCSI files be @@ -1029,11 +1029,12 @@ attach mvsata at pci with mvsata_pci file dev/pci/mvsata_pci.c mvsata_pci +include "dev/pci/voyager/files.voyager" # Silicon Motion SM502 / Voyager GX -device voyagerfb: wsemuldisplaydev, rasops8, rasops16, rasops32, vcons, videomode, iic, i2c_bitbang -attach voyagerfb at pci -file dev/pci/voyagerfb.c voyagerfb -defflag opt_voyagerfb.h VOYAGERFB_DEBUG +device voyager: iic, i2c_bitbang, voyagerbus, i2cbus +attach voyager at pci +file dev/pci/voyager.c voyager +defflag opt_voyager.h VOYAGER_DEBUG # High Definition Audio include "dev/pci/hdaudio/files.hdaudio" Added files: Index: src/sys/dev/pci/voyager.c diff -u /dev/null src/sys/dev/pci/voyager.c:1.1 --- /dev/null Wed Aug 31 16:47:31 2011 +++ src/sys/dev/pci/voyager.c Wed Aug 31 16:47:31 2011 @@ -0,0 +1,276 @@ +/* $NetBSD: voyager.c,v 1.1 2011/08/31 16:47:31 macallan Exp $ */ + +/* + * Copyright (c) 2009 Michael Lorenz + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__KERNEL_RCSID(0, "$NetBSD: voyager.c,v 1.1 2011/08/31 16:47:31 macallan Exp $"); + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/device.h> +#include <sys/malloc.h> +#include <sys/lwp.h> +#include <sys/kauth.h> + +#include <dev/pci/pcivar.h> +#include <dev/pci/pcireg.h> +#include <dev/pci/pcidevs.h> +#include <dev/pci/pciio.h> +#include <dev/ic/sm502reg.h> +#include <dev/i2c/i2cvar.h> +#include <dev/i2c/i2c_bitbang.h> + +#include <dev/pci/voyagervar.h> + +struct voyager_softc { + device_t sc_dev; + + pci_chipset_tag_t sc_pc; + pcitag_t sc_pcitag; + + bus_space_tag_t sc_memt; + bus_space_tag_t sc_iot; + + bus_space_handle_t sc_fbh, sc_regh; + bus_addr_t sc_fb, sc_reg; + bus_size_t sc_fbsize, sc_regsize; + + struct i2c_controller sc_i2c; + kmutex_t sc_i2c_lock; +}; + +static int voyager_match(device_t, cfdata_t, void *); +static void voyager_attach(device_t, device_t, void *); +static int voyager_print(void *, const char *); + +CFATTACH_DECL_NEW(voyager, sizeof(struct voyager_softc), + voyager_match, voyager_attach, NULL, NULL); + +/* I2C glue */ +static int voyager_i2c_acquire_bus(void *, int); +static void voyager_i2c_release_bus(void *, int); +static int voyager_i2c_send_start(void *, int); +static int voyager_i2c_send_stop(void *, int); +static int voyager_i2c_initiate_xfer(void *, i2c_addr_t, int); +static int voyager_i2c_read_byte(void *, uint8_t *, int); +static int voyager_i2c_write_byte(void *, uint8_t, int); + +/* I2C bitbang glue */ +static void voyager_i2cbb_set_bits(void *, uint32_t); +static void voyager_i2cbb_set_dir(void *, uint32_t); +static uint32_t voyager_i2cbb_read(void *); + +static const struct i2c_bitbang_ops voyager_i2cbb_ops = { + voyager_i2cbb_set_bits, + voyager_i2cbb_set_dir, + voyager_i2cbb_read, + { + 1 << 13, + 1 << 6, + 1 << 13, + 0 + } +}; + +static int +voyager_match(device_t parent, cfdata_t match, void *aux) +{ + struct pci_attach_args *pa = (struct pci_attach_args *)aux; + + if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY) + return 0; + if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_SILMOTION) + return 0; + + /* only chip tested on so far - may need a list */ + if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SILMOTION_SM502) + return 100; + return (0); +} + +static void +voyager_attach(device_t parent, device_t self, void *aux) +{ + struct voyager_softc *sc = device_private(self); + struct pci_attach_args *pa = aux; + char devinfo[256]; + struct voyager_attach_args vaa; + struct i2cbus_attach_args iba; + + sc->sc_pc = pa->pa_pc; + sc->sc_pcitag = pa->pa_tag; + sc->sc_memt = pa->pa_memt; + sc->sc_iot = pa->pa_iot; + sc->sc_dev = self; + + pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); + aprint_normal(": %s\n", devinfo); + + if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM, 0, + &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) { + aprint_error("%s: failed to map registers.\n", + device_xname(sc->sc_dev)); + } + + if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, + BUS_SPACE_MAP_LINEAR, + &sc->sc_memt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) { + aprint_error("%s: failed to map the frame buffer.\n", + device_xname(sc->sc_dev)); + } + + /* attach the framebuffer driver */ + vaa.vaa_memh = sc->sc_fbh; + vaa.vaa_mem_pa = sc->sc_fb; + vaa.vaa_regh = sc->sc_regh; + vaa.vaa_reg_pa = sc->sc_reg; + vaa.vaa_tag = sc->sc_memt; + vaa.vaa_pc = sc->sc_pc; + vaa.vaa_pcitag = sc->sc_pcitag; + strcpy(vaa.vaa_name, "voyagerfb"); + config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print); + + /* Fill in the i2c tag */ + sc->sc_i2c.ic_cookie = sc; + sc->sc_i2c.ic_acquire_bus = voyager_i2c_acquire_bus; + sc->sc_i2c.ic_release_bus = voyager_i2c_release_bus; + sc->sc_i2c.ic_send_start = voyager_i2c_send_start; + sc->sc_i2c.ic_send_stop = voyager_i2c_send_stop; + sc->sc_i2c.ic_initiate_xfer = voyager_i2c_initiate_xfer; + sc->sc_i2c.ic_read_byte = voyager_i2c_read_byte; + sc->sc_i2c.ic_write_byte = voyager_i2c_write_byte; + sc->sc_i2c.ic_exec = NULL; + mutex_init(&sc->sc_i2c_lock, MUTEX_DEFAULT, IPL_NONE); + iba.iba_tag = &sc->sc_i2c; + config_found_ia(self, "i2cbus", &iba, iicbus_print); +} + +static int +voyager_print(void *aux, const char *what) +{ + /*struct voyager_attach_args *vaa = aux;*/ + + if (what == NULL) + return 0; + + printf("%s:", what); + + return 0; +} + +static void +voyager_i2cbb_set_bits(void *cookie, uint32_t bits) +{ + struct voyager_softc *sc = cookie; + uint32_t reg; + + reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0); + //printf("%s: %08x ->", __func__, reg); + reg &= ~((1 << 6) | (1 << 13)); + reg |= bits; + //printf("%08x\n", reg); + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0, reg); +} + +static void +voyager_i2cbb_set_dir(void *cookie, uint32_t bits) +{ + struct voyager_softc *sc = cookie; + uint32_t reg; + + reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DIR0); + //printf("%s: %08x ->", __func__, reg); + reg &= ~(1 << 13); + reg |= bits; + //printf("%08x\n", reg); + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DIR0, reg); +} + +static uint32_t +voyager_i2cbb_read(void *cookie) +{ + struct voyager_softc *sc = cookie; + uint32_t reg; + + reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0); + //printf("%s: %08x\n", __func__, reg); + return reg; +} + +/* higher level I2C stuff */ +static int +voyager_i2c_acquire_bus(void *cookie, int flags) +{ + struct voyager_softc *sc = cookie; + + mutex_enter(&sc->sc_i2c_lock); + return 0; +} + +static void +voyager_i2c_release_bus(void *cookie, int flags) +{ + struct voyager_softc *sc = cookie; + + mutex_exit(&sc->sc_i2c_lock); +} + +static int +voyager_i2c_send_start(void *cookie, int flags) +{ + return (i2c_bitbang_send_start(cookie, flags, &voyager_i2cbb_ops)); +} + +static int +voyager_i2c_send_stop(void *cookie, int flags) +{ + + return (i2c_bitbang_send_stop(cookie, flags, &voyager_i2cbb_ops)); +} + +static int +voyager_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags) +{ + /* + * for some reason i2c_bitbang_initiate_xfer left-shifts + * the I2C-address and then sets the direction bit + */ + return (i2c_bitbang_initiate_xfer(cookie, addr, flags, + &voyager_i2cbb_ops)); +} + +static int +voyager_i2c_read_byte(void *cookie, uint8_t *valp, int flags) +{ + return (i2c_bitbang_read_byte(cookie, valp, flags, &voyager_i2cbb_ops)); +} + +static int +voyager_i2c_write_byte(void *cookie, uint8_t val, int flags) +{ + return (i2c_bitbang_write_byte(cookie, val, flags, &voyager_i2cbb_ops)); +} Index: src/sys/dev/pci/voyagervar.h diff -u /dev/null src/sys/dev/pci/voyagervar.h:1.1 --- /dev/null Wed Aug 31 16:47:31 2011 +++ src/sys/dev/pci/voyagervar.h Wed Aug 31 16:47:31 2011 @@ -0,0 +1,45 @@ +/* $NetBSD: voyagervar.h,v 1.1 2011/08/31 16:47:31 macallan Exp $ */ + +/* + * Copyright (c) 2009 Michael Lorenz + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__KERNEL_RCSID(0, "$NetBSD: voyagervar.h,v 1.1 2011/08/31 16:47:31 macallan Exp $"); + +#ifndef VOYAGERVAR_H +#define VOYAGERVAR_H + +struct voyager_attach_args { + pci_chipset_tag_t vaa_pc; + pcitag_t vaa_pcitag; + bus_space_tag_t vaa_tag; + bus_space_handle_t vaa_regh; + bus_space_handle_t vaa_memh; + bus_addr_t vaa_reg_pa; + bus_addr_t vaa_mem_pa; + char vaa_name[32]; +}; + +#endif \ No newline at end of file Index: src/sys/dev/pci/voyager/files.voyager diff -u /dev/null src/sys/dev/pci/voyager/files.voyager:1.1 --- /dev/null Wed Aug 31 16:47:31 2011 +++ src/sys/dev/pci/voyager/files.voyager Wed Aug 31 16:47:31 2011 @@ -0,0 +1,9 @@ +# Silicon Motion SM502 / Voyager GX + +define voyagerbus {} + +# the graphics part +device voyagerfb: wsemuldisplaydev, rasops8, rasops16, rasops32, vcons, videomode, iic, i2c_bitbang +attach voyagerfb at voyagerbus +file dev/pci/voyager/voyagerfb.c voyagerfb +defflag opt_voyagerfb.h VOYAGERFB_DEBUG Index: src/sys/dev/pci/voyager/voyagerfb.c diff -u /dev/null src/sys/dev/pci/voyager/voyagerfb.c:1.1 --- /dev/null Wed Aug 31 16:47:31 2011 +++ src/sys/dev/pci/voyager/voyagerfb.c Wed Aug 31 16:47:31 2011 @@ -0,0 +1,820 @@ +/* $NetBSD: voyagerfb.c,v 1.1 2011/08/31 16:47:31 macallan Exp $ */ + +/* + * Copyright (c) 2009 Michael Lorenz + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * A console driver for Silicon Motion SM502 / Voyager GX graphics controllers + * tested on GDIUM only so far + */ + +#include <sys/cdefs.h> +__KERNEL_RCSID(0, "$NetBSD: voyagerfb.c,v 1.1 2011/08/31 16:47:31 macallan Exp $"); + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/device.h> +#include <sys/malloc.h> +#include <sys/lwp.h> +#include <sys/kauth.h> + +#include <dev/videomode/videomode.h> + +#include <dev/pci/pcivar.h> +#include <dev/pci/pcireg.h> +#include <dev/pci/pcidevs.h> +#include <dev/pci/pciio.h> +#include <dev/ic/sm502reg.h> + +#include <dev/wscons/wsdisplayvar.h> +#include <dev/wscons/wsconsio.h> +#include <dev/wsfont/wsfont.h> +#include <dev/rasops/rasops.h> +#include <dev/wscons/wsdisplay_vconsvar.h> +#include <dev/pci/wsdisplay_pci.h> + +#include <dev/i2c/i2cvar.h> +#include <dev/pci/voyagervar.h> + +struct voyagerfb_softc { + device_t sc_dev; + + pci_chipset_tag_t sc_pc; + pcitag_t sc_pcitag; + bus_space_tag_t sc_memt; + + bus_space_handle_t sc_fbh; + bus_space_handle_t sc_regh; + bus_addr_t sc_fb, sc_reg; + bus_size_t sc_fbsize, sc_regsize; + + int sc_width, sc_height, sc_depth, sc_stride; + int sc_locked; + void *sc_fbaddr; + struct vcons_screen sc_console_screen; + struct wsscreen_descr sc_defaultscreen_descr; + const struct wsscreen_descr *sc_screens[1]; + struct wsscreen_list sc_screenlist; + struct vcons_data vd; + uint8_t *sc_dataport; + int sc_mode; + u_char sc_cmap_red[256]; + u_char sc_cmap_green[256]; + u_char sc_cmap_blue[256]; +}; + +static int voyagerfb_match(device_t, cfdata_t, void *); +static void voyagerfb_attach(device_t, device_t, void *); + +CFATTACH_DECL_NEW(voyagerfb, sizeof(struct voyagerfb_softc), + voyagerfb_match, voyagerfb_attach, NULL, NULL); + +extern const u_char rasops_cmap[768]; + +static int voyagerfb_ioctl(void *, void *, u_long, void *, int, + struct lwp *); +static paddr_t voyagerfb_mmap(void *, void *, off_t, int); +static void voyagerfb_init_screen(void *, struct vcons_screen *, int, + long *); + +static int voyagerfb_putcmap(struct voyagerfb_softc *, + struct wsdisplay_cmap *); +static int voyagerfb_getcmap(struct voyagerfb_softc *, + struct wsdisplay_cmap *); +static void voyagerfb_restore_palette(struct voyagerfb_softc *); +static int voyagerfb_putpalreg(struct voyagerfb_softc *, int, uint8_t, + uint8_t, uint8_t); + +static void voyagerfb_init(struct voyagerfb_softc *); + +static void voyagerfb_rectfill(struct voyagerfb_softc *, int, int, int, int, + uint32_t); +static void voyagerfb_bitblt(struct voyagerfb_softc *, int, int, int, int, + int, int, int); + +static void voyagerfb_cursor(void *, int, int, int); +static void voyagerfb_putchar(void *, int, int, u_int, long); +static void voyagerfb_copycols(void *, int, int, int, int); +static void voyagerfb_erasecols(void *, int, int, int, long); +static void voyagerfb_copyrows(void *, int, int, int); +static void voyagerfb_eraserows(void *, int, int, long); + +struct wsdisplay_accessops voyagerfb_accessops = { + voyagerfb_ioctl, + voyagerfb_mmap, + NULL, /* alloc_screen */ + NULL, /* free_screen */ + NULL, /* show_screen */ + NULL, /* load_font */ + NULL, /* pollc */ + NULL /* scroll */ +}; + +/* wait for FIFO empty so we can feed it another command */ +static inline void +voyagerfb_ready(struct voyagerfb_softc *sc) +{ + do {} while ((bus_space_read_4(sc->sc_memt, sc->sc_regh, + SM502_SYSTEM_CTRL) & SM502_SYSCTL_FIFO_EMPTY) == 0); +} + +/* wait for the drawing engine to be idle */ +static inline void +voyagerfb_wait(struct voyagerfb_softc *sc) +{ + do {} while ((bus_space_read_4(sc->sc_memt, sc->sc_regh, + SM502_SYSTEM_CTRL) & SM502_SYSCTL_ENGINE_BUSY) != 0); +} + +static int +voyagerfb_match(device_t parent, cfdata_t match, void *aux) +{ + struct voyager_attach_args *vaa = (struct voyager_attach_args *)aux; + + if (strcmp(vaa->vaa_name, "voyagerfb") == 0) return 100; + return 0; +} + +static void +voyagerfb_attach(device_t parent, device_t self, void *aux) +{ + struct voyagerfb_softc *sc = device_private(self); + struct voyager_attach_args *vaa = aux; + struct rasops_info *ri; + struct wsemuldisplaydev_attach_args aa; + prop_dictionary_t dict; + unsigned long defattr; + uint32_t reg; + bool is_console; + int i, j; + + sc->sc_pc = vaa->vaa_pc; + sc->sc_pcitag = vaa->vaa_pcitag; + sc->sc_memt = vaa->vaa_tag; + sc->sc_dev = self; + + aprint_normal("\n"); + + dict = device_properties(self); + prop_dictionary_get_bool(dict, "is_console", &is_console); + + sc->sc_fb = vaa->vaa_mem_pa; + sc->sc_fbh = vaa->vaa_memh; + sc->sc_fbsize = 16 * 1024 * 1024; + sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh); + + sc->sc_reg = vaa->vaa_reg_pa; + sc->sc_regh = vaa->vaa_regh; + sc->sc_regsize = 2 * 1024 * 1024; + sc->sc_dataport = bus_space_vaddr(sc->sc_memt, sc->sc_regh); + sc->sc_dataport += SM502_DATAPORT; + + reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_PANEL_DISP_CRTL); + switch (reg & SM502_PDC_DEPTH_MASK) { + case SM502_PDC_8BIT: + sc->sc_depth = 8; + break; + case SM502_PDC_16BIT: + sc->sc_depth = 16; + break; + case SM502_PDC_32BIT: + sc->sc_depth = 24; + break; + default: + panic("%s: unsupported depth", device_xname(self)); + } + sc->sc_stride = (bus_space_read_4(sc->sc_memt, sc->sc_regh, + SM502_PANEL_FB_OFFSET) & SM502_FBA_WIN_STRIDE_MASK) >> 16; + sc->sc_width = (bus_space_read_4(sc->sc_memt, sc->sc_regh, + SM502_PANEL_FB_WIDTH) & SM502_FBW_WIN_WIDTH_MASK) >> 16; + sc->sc_height = (bus_space_read_4(sc->sc_memt, sc->sc_regh, + SM502_PANEL_FB_HEIGHT) & SM502_FBH_WIN_HEIGHT_MASK) >> 16; + + printf("%s: %d x %d, %d bit, stride %d\n", device_xname(self), + sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride); + /* + * XXX yeah, casting the fb address to uint32_t is formally wrong + * but as far as I know there are no SM502 with 64bit BARs + */ + aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self), + (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb); + + sc->sc_defaultscreen_descr = (struct wsscreen_descr){ + "default", + 0, 0, + NULL, + 8, 16, + WSSCREEN_WSCOLORS | WSSCREEN_HILIT, + NULL + }; + sc->sc_screens[0] = &sc->sc_defaultscreen_descr; + sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens}; + sc->sc_mode = WSDISPLAYIO_MODE_EMUL; + sc->sc_locked = 0; + + vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr, + &voyagerfb_accessops); + sc->vd.init_screen = voyagerfb_init_screen; + + /* init engine here */ + voyagerfb_init(sc); + + ri = &sc->sc_console_screen.scr_ri; + + if (is_console) { + vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, + &defattr); + sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC; + + sc->sc_defaultscreen_descr.textops = &ri->ri_ops; + sc->sc_defaultscreen_descr.capabilities = ri->ri_caps; + sc->sc_defaultscreen_descr.nrows = ri->ri_rows; + sc->sc_defaultscreen_descr.ncols = ri->ri_cols; + wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0, + defattr); + } else { + /* + * since we're not the console we can postpone the rest + * until someone actually allocates a screen for us + */ + (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr); + } + + j = 0; + if (sc->sc_depth <= 8) { + for (i = 0; i < (1 << sc->sc_depth); i++) { + + sc->sc_cmap_red[i] = rasops_cmap[j]; + sc->sc_cmap_green[i] = rasops_cmap[j + 1]; + sc->sc_cmap_blue[i] = rasops_cmap[j + 2]; + voyagerfb_putpalreg(sc, i, rasops_cmap[j], + rasops_cmap[j + 1], rasops_cmap[j + 2]); + j += 3; + } + } + + voyagerfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, + ri->ri_devcmap[(defattr >> 16) & 0xff]); + + if (is_console) + vcons_replay_msgbuf(&sc->sc_console_screen); + + aa.console = is_console; + aa.scrdata = &sc->sc_screenlist; + aa.accessops = &voyagerfb_accessops; + aa.accesscookie = &sc->vd; + + config_found(sc->sc_dev, &aa, wsemuldisplaydevprint); +} + +static int +voyagerfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, + struct lwp *l) +{ + struct vcons_data *vd = v; + struct voyagerfb_softc *sc = vd->cookie; + struct wsdisplay_fbinfo *wdf; + struct vcons_screen *ms = vd->active; + + switch (cmd) { + case WSDISPLAYIO_GTYPE: + *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; + return 0; + + /* PCI config read/write passthrough. */ + case PCI_IOC_CFGREAD: + case PCI_IOC_CFGWRITE: + return pci_devioctl(sc->sc_pc, sc->sc_pcitag, + cmd, data, flag, l); + + case WSDISPLAYIO_GET_BUSID: + return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc, + sc->sc_pcitag, data); + + case WSDISPLAYIO_GINFO: + if (ms == NULL) + return ENODEV; + wdf = (void *)data; + wdf->height = ms->scr_ri.ri_height; + wdf->width = ms->scr_ri.ri_width; + wdf->depth = ms->scr_ri.ri_depth; + wdf->cmsize = 256; + return 0; + + case WSDISPLAYIO_GETCMAP: + return voyagerfb_getcmap(sc, + (struct wsdisplay_cmap *)data); + + case WSDISPLAYIO_PUTCMAP: + return voyagerfb_putcmap(sc, + (struct wsdisplay_cmap *)data); + + case WSDISPLAYIO_LINEBYTES: + *(u_int *)data = sc->sc_stride; + return 0; + + case WSDISPLAYIO_SMODE: { + int new_mode = *(int*)data; + if (new_mode != sc->sc_mode) { + sc->sc_mode = new_mode; + if(new_mode == WSDISPLAYIO_MODE_EMUL) { + voyagerfb_restore_palette(sc); + vcons_redraw_screen(ms); + } + } + } + return 0; + } + return EPASSTHROUGH; +} + +static paddr_t +voyagerfb_mmap(void *v, void *vs, off_t offset, int prot) +{ + struct vcons_data *vd = v; + struct voyagerfb_softc *sc = vd->cookie; + paddr_t pa; + + /* 'regular' framebuffer mmap()ing */ + if (offset < sc->sc_fbsize) { + pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot, + BUS_SPACE_MAP_LINEAR); + return pa; + } + + /* + * restrict all other mappings to processes with superuser privileges + * or the kernel itself + */ + if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER, + NULL) != 0) { + aprint_normal("%s: mmap() rejected.\n", + device_xname(sc->sc_dev)); + return -1; + } + + if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) { + pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, + BUS_SPACE_MAP_LINEAR); + return pa; + } + + if ((offset >= sc->sc_reg) && + (offset < (sc->sc_reg + sc->sc_regsize))) { + pa = bus_space_mmap(sc->sc_memt, offset, 0, prot, + BUS_SPACE_MAP_LINEAR); + return pa; + } + + return -1; +} + +static void +voyagerfb_init_screen(void *cookie, struct vcons_screen *scr, + int existing, long *defattr) +{ + struct voyagerfb_softc *sc = cookie; + struct rasops_info *ri = &scr->scr_ri; + + ri->ri_depth = sc->sc_depth; + ri->ri_width = sc->sc_width; + ri->ri_height = sc->sc_height; + ri->ri_stride = sc->sc_stride; + ri->ri_flg = RI_CENTER | RI_FULLCLEAR; + + ri->ri_bits = (char *)sc->sc_fbaddr; + + if (existing) { + ri->ri_flg |= RI_CLEAR; + } + + rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8); + ri->ri_caps = WSSCREEN_WSCOLORS; + + rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight, + sc->sc_width / ri->ri_font->fontwidth); + + ri->ri_hw = scr; + ri->ri_ops.copyrows = voyagerfb_copyrows; + ri->ri_ops.copycols = voyagerfb_copycols; + ri->ri_ops.eraserows = voyagerfb_eraserows; + ri->ri_ops.erasecols = voyagerfb_erasecols; + ri->ri_ops.cursor = voyagerfb_cursor; + ri->ri_ops.putchar = voyagerfb_putchar; +} + +static int +voyagerfb_putcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm) +{ + u_char *r, *g, *b; + u_int index = cm->index; + u_int count = cm->count; + int i, error; + u_char rbuf[256], gbuf[256], bbuf[256]; + +#ifdef VOYAGERFB_DEBUG + aprint_debug("putcmap: %d %d\n",index, count); +#endif + if (cm->index >= 256 || cm->count > 256 || + (cm->index + cm->count) > 256) + return EINVAL; + error = copyin(cm->red, &rbuf[index], count); + if (error) + return error; + error = copyin(cm->green, &gbuf[index], count); + if (error) + return error; + error = copyin(cm->blue, &bbuf[index], count); + if (error) + return error; + + memcpy(&sc->sc_cmap_red[index], &rbuf[index], count); + memcpy(&sc->sc_cmap_green[index], &gbuf[index], count); + memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count); + + r = &sc->sc_cmap_red[index]; + g = &sc->sc_cmap_green[index]; + b = &sc->sc_cmap_blue[index]; + + for (i = 0; i < count; i++) { + voyagerfb_putpalreg(sc, index, *r, *g, *b); + index++; + r++, g++, b++; + } + return 0; +} + +static int +voyagerfb_getcmap(struct voyagerfb_softc *sc, struct wsdisplay_cmap *cm) +{ + u_int index = cm->index; + u_int count = cm->count; + int error; + + if (index >= 255 || count > 256 || index + count > 256) + return EINVAL; + + error = copyout(&sc->sc_cmap_red[index], cm->red, count); + if (error) + return error; + error = copyout(&sc->sc_cmap_green[index], cm->green, count); + if (error) + return error; + error = copyout(&sc->sc_cmap_blue[index], cm->blue, count); + if (error) + return error; + + return 0; +} + +static void +voyagerfb_restore_palette(struct voyagerfb_softc *sc) +{ + int i; + + for (i = 0; i < (1 << sc->sc_depth); i++) { + voyagerfb_putpalreg(sc, i, sc->sc_cmap_red[i], + sc->sc_cmap_green[i], sc->sc_cmap_blue[i]); + } +} + +static int +voyagerfb_putpalreg(struct voyagerfb_softc *sc, int idx, uint8_t r, + uint8_t g, uint8_t b) +{ + uint32_t reg; + + reg = (r << 16) | (g << 8) | b; + /* XXX we should probably write the CRT palette too */ + bus_space_write_4(sc->sc_memt, sc->sc_regh, + SM502_PALETTE_PANEL + (idx << 2), reg); + return 0; +} + +static void +voyagerfb_init(struct voyagerfb_softc *sc) +{ + + voyagerfb_wait(sc); + /* disable colour compare */ + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_COLOR_COMP_MASK, 0); + /* allow writes to all planes */ + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PLANEMASK, + 0xffffffff); + /* disable clipping */ + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CLIP_TOP_LEFT, 0); + /* source and destination in local memory, no offset */ + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC_BASE, 0); + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST_BASE, 0); + /* pitch is screen stride */ + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PITCH, + sc->sc_width | (sc->sc_width << 16)); + /* window is screen width */ + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_WINDOW_WIDTH, + sc->sc_width | (sc->sc_width << 16)); + switch (sc->sc_depth) { + case 8: + bus_space_write_4(sc->sc_memt, sc->sc_regh, + SM502_STRETCH, SM502_STRETCH_8BIT); + break; + case 16: + bus_space_write_4(sc->sc_memt, sc->sc_regh, + SM502_STRETCH, SM502_STRETCH_16BIT); + break; + case 24: + case 32: + bus_space_write_4(sc->sc_memt, sc->sc_regh, + SM502_STRETCH, SM502_STRETCH_32BIT); + break; + } +} + +static void +voyagerfb_rectfill(struct voyagerfb_softc *sc, int x, int y, int wi, int he, + uint32_t colour) +{ + + voyagerfb_ready(sc); + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, + ROP_COPY | + SM502_CTRL_USE_ROP2 | + SM502_CTRL_CMD_RECTFILL | + SM502_CTRL_QUICKSTART_E); + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_FOREGROUND, + colour); + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, + (x << 16) | y); + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION, + (wi << 16) | he); +} + +static void +voyagerfb_bitblt(struct voyagerfb_softc *sc, int xs, int ys, int xd, int yd, + int wi, int he, int rop) +{ + uint32_t cmd; + + cmd = (rop & 0xf) | SM502_CTRL_USE_ROP2 | SM502_CTRL_CMD_BITBLT | + SM502_CTRL_QUICKSTART_E; + + voyagerfb_ready(sc); + + if (xd <= xs) { + /* left to right */ + } else { + /* + * According to the manual this flag reverses only the blitter's + * X direction. At least on my Gdium it also reverses the Y + * direction + */ + cmd |= SM502_CTRL_R_TO_L; + xs += wi - 1; + xd += wi - 1; + ys += he - 1; + yd += he - 1; + } + + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_CONTROL, cmd); + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_SRC, + (xs << 16) | ys); + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DST, + (xd << 16) | yd); + bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_DIMENSION, + (wi << 16) | he); +} + +static void +voyagerfb_cursor(void *cookie, int on, int row, int col) +{ + struct rasops_info *ri = cookie; + struct vcons_screen *scr = ri->ri_hw; + struct voyagerfb_softc *sc = scr->scr_cookie; + int x, y, wi, he; + + wi = ri->ri_font->fontwidth; + he = ri->ri_font->fontheight; + + if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { + x = ri->ri_ccol * wi + ri->ri_xorigin; + y = ri->ri_crow * he + ri->ri_yorigin; + if (ri->ri_flg & RI_CURSOR) { + voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT); + ri->ri_flg &= ~RI_CURSOR; + } + ri->ri_crow = row; + ri->ri_ccol = col; + if (on) { + x = ri->ri_ccol * wi + ri->ri_xorigin; + y = ri->ri_crow * he + ri->ri_yorigin; + voyagerfb_bitblt(sc, x, y, x, y, wi, he, ROP_INVERT); + ri->ri_flg |= RI_CURSOR; + } + } else { + scr->scr_ri.ri_crow = row; + scr->scr_ri.ri_ccol = col; + scr->scr_ri.ri_flg &= ~RI_CURSOR; + } + +} + +static inline void +voyagerfb_feed8(struct voyagerfb_softc *sc, uint8_t *data, int len) +{ + uint32_t *port = (uint32_t *)sc->sc_dataport; + int i; + + for (i = 0; i < ((len + 3) & 0xfffc); i++) { + *port = *data; + data++; + } +} + +static inline void +voyagerfb_feed16(struct voyagerfb_softc *sc, uint16_t *data, int len) +{ + uint32_t *port = (uint32_t *)sc->sc_dataport; + int i; + + len = len << 1; + for (i = 0; i < ((len + 1) & 0xfffe); i++) { + *port = *data; + data++; + } +} + + +static void +voyagerfb_putchar(void *cookie, int row, int col, u_int c, long attr) +{ + struct rasops_info *ri = cookie; + struct wsdisplay_font *font = PICK_FONT(ri, c); + struct vcons_screen *scr = ri->ri_hw; + struct voyagerfb_softc *sc = scr->scr_cookie; + uint32_t cmd; + + if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) { + int fg, bg, uc; + uint8_t *data; + int x, y, wi, he; + wi = font->fontwidth; + he = font->fontheight; + + if (!CHAR_IN_FONT(c, font)) + return; + bg = ri->ri_devcmap[(attr >> 16) & 0x0f]; + fg = ri->ri_devcmap[(attr >> 24) & 0x0f]; + x = ri->ri_xorigin + col * wi; + y = ri->ri_yorigin + row * he; + if (c == 0x20) { + voyagerfb_rectfill(sc, x, y, wi, he, bg); + } else { + uc = c - font->firstchar; + data = (uint8_t *)font->data + uc * ri->ri_fontscale; + cmd = ROP_COPY | + SM502_CTRL_USE_ROP2 | + SM502_CTRL_CMD_HOSTWRT | + SM502_CTRL_HOSTBLT_MONO | + SM502_CTRL_QUICKSTART_E | + SM502_CTRL_MONO_PACK_32BIT; + voyagerfb_ready(sc); + bus_space_write_4(sc->sc_memt, sc->sc_regh, + SM502_CONTROL, cmd); + bus_space_write_4(sc->sc_memt, sc->sc_regh, + SM502_FOREGROUND, fg); + bus_space_write_4(sc->sc_memt, sc->sc_regh, + SM502_BACKGROUND, bg); + bus_space_write_4(sc->sc_memt, sc->sc_regh, + SM502_SRC, 0); + bus_space_write_4(sc->sc_memt, sc->sc_regh, + SM502_DST, (x << 16) | y); + bus_space_write_4(sc->sc_memt, sc->sc_regh, + SM502_DIMENSION, (wi << 16) | he); + /* now feed the data, padded to 32bit */ + switch (ri->ri_font->stride) { + case 1: + voyagerfb_feed8(sc, data, ri->ri_fontscale); + break; + case 2: + voyagerfb_feed16(sc, (uint16_t *)data, + ri->ri_fontscale); + break; + + } + } + } +} + +static void +voyagerfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols) +{ + struct rasops_info *ri = cookie; + struct vcons_screen *scr = ri->ri_hw; + struct voyagerfb_softc *sc = scr->scr_cookie; + int32_t xs, xd, y, width, height; + + if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { + xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol; + xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol; + y = ri->ri_yorigin + ri->ri_font->fontheight * row; + width = ri->ri_font->fontwidth * ncols; + height = ri->ri_font->fontheight; + voyagerfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY); + } +} + +static void +voyagerfb_erasecols(void *cookie, int row, int startcol, int ncols, + long fillattr) +{ + struct rasops_info *ri = cookie; + struct vcons_screen *scr = ri->ri_hw; + struct voyagerfb_softc *sc = scr->scr_cookie; + int32_t x, y, width, height, fg, bg, ul; + + if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { + x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol; + y = ri->ri_yorigin + ri->ri_font->fontheight * row; + width = ri->ri_font->fontwidth * ncols; + height = ri->ri_font->fontheight; + rasops_unpack_attr(fillattr, &fg, &bg, &ul); + + voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]); + } +} + +static void +voyagerfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows) +{ + struct rasops_info *ri = cookie; + struct vcons_screen *scr = ri->ri_hw; + struct voyagerfb_softc *sc = scr->scr_cookie; + int32_t x, ys, yd, width, height; + int i; + + if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { + x = ri->ri_xorigin; + ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow; + yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow; + width = ri->ri_emuwidth; + height = ri->ri_font->fontheight * nrows; + if ((nrows > 1) && (dstrow > srcrow)) { + /* + * the blitter can't do bottom-up copies so we have + * to copy line by line here + * should probably use a command sequence + */ + ys += (height - ri->ri_font->fontheight); + yd += (height - ri->ri_font->fontheight); + for (i = 0; i < nrows; i++) { + voyagerfb_bitblt(sc, x, ys, x, yd, width, + ri->ri_font->fontheight, ROP_COPY); + ys -= ri->ri_font->fontheight; + yd -= ri->ri_font->fontheight; + } + } else + voyagerfb_bitblt(sc, x, ys, x, yd, width, height, + ROP_COPY); + } +} + +static void +voyagerfb_eraserows(void *cookie, int row, int nrows, long fillattr) +{ + struct rasops_info *ri = cookie; + struct vcons_screen *scr = ri->ri_hw; + struct voyagerfb_softc *sc = scr->scr_cookie; + int32_t x, y, width, height, fg, bg, ul; + + if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) { + x = ri->ri_xorigin; + y = ri->ri_yorigin + ri->ri_font->fontheight * row; + width = ri->ri_emuwidth; + height = ri->ri_font->fontheight * nrows; + rasops_unpack_attr(fillattr, &fg, &bg, &ul); + + voyagerfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]); + } +}