1 /* $NetBSD: cgthree.c,v 1.16 2008/06/11 21:25:31 drochner Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Paul Kranenburg. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * color display (cgthree) driver. 34 * 35 * Does not handle interrupts, even though they can occur. 36 * 37 * XXX should defer colormap updates to vertical retrace interrupts 38 */ 39 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: cgthree.c,v 1.16 2008/06/11 21:25:31 drochner Exp $"); 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/buf.h> 46 #include <sys/device.h> 47 #include <sys/ioctl.h> 48 #include <sys/malloc.h> 49 #include <sys/mman.h> 50 #include <sys/tty.h> 51 #include <sys/conf.h> 52 53 #include <sys/bus.h> 54 #include <machine/autoconf.h> 55 56 #include <dev/sun/fbio.h> 57 #include <dev/sun/fbvar.h> 58 59 #include <dev/sun/btreg.h> 60 #include <dev/sun/btvar.h> 61 #include <dev/sun/cgthreereg.h> 62 #include <dev/sun/cgthreevar.h> 63 64 static void cgthreeunblank(struct device *); 65 static void cgthreeloadcmap(struct cgthree_softc *, int, int); 66 static void cgthree_set_video(struct cgthree_softc *, int); 67 static int cgthree_get_video(struct cgthree_softc *); 68 69 extern struct cfdriver cgthree_cd; 70 71 dev_type_open(cgthreeopen); 72 dev_type_ioctl(cgthreeioctl); 73 dev_type_mmap(cgthreemmap); 74 75 const struct cdevsw cgthree_cdevsw = { 76 cgthreeopen, nullclose, noread, nowrite, cgthreeioctl, 77 nostop, notty, nopoll, cgthreemmap, nokqfilter 78 }; 79 80 /* frame buffer generic driver */ 81 static struct fbdriver cgthreefbdriver = { 82 cgthreeunblank, cgthreeopen, nullclose, cgthreeioctl, nopoll, 83 cgthreemmap, nokqfilter 84 }; 85 86 /* Video control parameters */ 87 struct cg3_videoctrl { 88 unsigned char sense; /* Monitor sense value */ 89 unsigned char vctrl[12]; 90 } cg3_videoctrl[] = { 91 /* Missing entries: sense 0x10, 0x30, 0x50 */ 92 { 0x40, /* this happens to be my 19'' 1152x900 gray-scale monitor */ 93 {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1} 94 }, 95 { 0x00, /* default? must be last */ 96 {0xbb, 0x2b, 0x3, 0xb, 0xb3, 0x3, 0xaf, 0x2b, 0x2, 0xa, 0xff, 0x1} 97 } 98 }; 99 100 101 void 102 cgthreeattach(sc, name, isconsole) 103 struct cgthree_softc *sc; 104 const char *name; 105 int isconsole; 106 { 107 int i; 108 struct fbdevice *fb = &sc->sc_fb; 109 volatile struct fbcontrol *fbc = sc->sc_fbc; 110 volatile struct bt_regs *bt = &fbc->fbc_dac; 111 112 fb->fb_driver = &cgthreefbdriver; 113 fb->fb_type.fb_cmsize = 256; 114 fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes; 115 printf(": %s, %d x %d", name, 116 fb->fb_type.fb_width, fb->fb_type.fb_height); 117 118 /* Transfer video magic to board, if it's not running */ 119 if ((fbc->fbc_ctrl & FBC_TIMING) == 0) { 120 int sense = (fbc->fbc_status & FBS_MSENSE); 121 /* Search table for video timings fitting this monitor */ 122 for (i = 0; i < sizeof(cg3_videoctrl)/sizeof(cg3_videoctrl[0]); 123 i++) { 124 int j; 125 if (sense != cg3_videoctrl[i].sense) 126 continue; 127 128 printf(" setting video ctrl"); 129 for (j = 0; j < 12; j++) 130 fbc->fbc_vcontrol[j] = 131 cg3_videoctrl[i].vctrl[j]; 132 fbc->fbc_ctrl |= FBC_TIMING; 133 break; 134 } 135 } 136 137 /* Initialize the default color map. */ 138 bt_initcmap(&sc->sc_cmap, 256); 139 cgthreeloadcmap(sc, 0, 256); 140 141 /* make sure we are not blanked */ 142 cgthree_set_video(sc, 1); 143 BT_INIT(bt, 0); 144 145 if (isconsole) { 146 printf(" (console)\n"); 147 #ifdef RASTERCONSOLE 148 fbrcons_init(fb); 149 #endif 150 } else 151 printf("\n"); 152 153 fb_attach(fb, isconsole); 154 } 155 156 157 int 158 cgthreeopen(dev, flags, mode, l) 159 dev_t dev; 160 int flags, mode; 161 struct lwp *l; 162 { 163 int unit = minor(dev); 164 165 if (device_lookup(&cgthree_cd, unit) == NULL) 166 return (ENXIO); 167 return (0); 168 } 169 170 int 171 cgthreeioctl(dev, cmd, data, flags, l) 172 dev_t dev; 173 u_long cmd; 174 void *data; 175 int flags; 176 struct lwp *l; 177 { 178 struct cgthree_softc *sc = device_lookup_private(&cgthree_cd, 179 minor(dev)); 180 struct fbgattr *fba; 181 int error; 182 183 switch (cmd) { 184 185 case FBIOGTYPE: 186 *(struct fbtype *)data = sc->sc_fb.fb_type; 187 break; 188 189 case FBIOGATTR: 190 fba = (struct fbgattr *)data; 191 fba->real_type = sc->sc_fb.fb_type.fb_type; 192 fba->owner = 0; /* XXX ??? */ 193 fba->fbtype = sc->sc_fb.fb_type; 194 fba->sattr.flags = 0; 195 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type; 196 fba->sattr.dev_specific[0] = -1; 197 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type; 198 fba->emu_types[1] = -1; 199 break; 200 201 case FBIOGETCMAP: 202 #define p ((struct fbcmap *)data) 203 return (bt_getcmap(p, &sc->sc_cmap, 256, 1)); 204 205 case FBIOPUTCMAP: 206 /* copy to software map */ 207 error = bt_putcmap(p, &sc->sc_cmap, 256, 1); 208 if (error) 209 return (error); 210 /* now blast them into the chip */ 211 /* XXX should use retrace interrupt */ 212 cgthreeloadcmap(sc, p->index, p->count); 213 #undef p 214 break; 215 216 case FBIOGVIDEO: 217 *(int *)data = cgthree_get_video(sc); 218 break; 219 220 case FBIOSVIDEO: 221 cgthree_set_video(sc, *(int *)data); 222 break; 223 224 default: 225 return (ENOTTY); 226 } 227 return (0); 228 } 229 230 /* 231 * Undo the effect of an FBIOSVIDEO that turns the video off. 232 */ 233 static void 234 cgthreeunblank(dev) 235 struct device *dev; 236 { 237 238 cgthree_set_video(device_private(dev), 1); 239 } 240 241 static void 242 cgthree_set_video(sc, enable) 243 struct cgthree_softc *sc; 244 int enable; 245 { 246 247 if (enable) 248 sc->sc_fbc->fbc_ctrl |= FBC_VENAB; 249 else 250 sc->sc_fbc->fbc_ctrl &= ~FBC_VENAB; 251 } 252 253 static int 254 cgthree_get_video(sc) 255 struct cgthree_softc *sc; 256 { 257 258 return ((sc->sc_fbc->fbc_ctrl & FBC_VENAB) != 0); 259 } 260 261 /* 262 * Load a subset of the current (new) colormap into the Brooktree DAC. 263 */ 264 static void 265 cgthreeloadcmap(sc, start, ncolors) 266 struct cgthree_softc *sc; 267 int start, ncolors; 268 { 269 volatile struct bt_regs *bt; 270 u_int *ip; 271 int count; 272 273 ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)]; /* start/4 * 3 */ 274 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3; 275 bt = &sc->sc_fbc->fbc_dac; 276 bt->bt_addr = BT_D4M4(start); 277 while (--count >= 0) 278 bt->bt_cmap = *ip++; 279 } 280 281 /* 282 * Return the address that would map the given device at the given 283 * offset, allowing for the given protection, or return -1 for error. 284 * 285 * The cg3 is mapped starting at 256KB, for pseudo-compatibility with 286 * the cg4 (which had an overlay plane in the first 128K and an enable 287 * plane in the next 128K). X11 uses only 256k+ region but tries to 288 * map the whole thing, so we repeatedly map the first 256K to the 289 * first page of the color screen. If someone tries to use the overlay 290 * and enable regions, they will get a surprise.... 291 * 292 * As well, mapping at an offset of 0x04000000 causes the cg3 to be 293 * mapped in flat mode without the cg4 emulation. 294 */ 295 paddr_t 296 cgthreemmap(dev, off, prot) 297 dev_t dev; 298 off_t off; 299 int prot; 300 { 301 struct cgthree_softc *sc = device_lookup_private(&cgthree_cd, 302 minor(dev)); 303 304 #define START (128*1024 + 128*1024) 305 #define NOOVERLAY (0x04000000) 306 307 if (off & PGOFSET) 308 panic("cgthreemmap"); 309 if (off < 0) 310 return (-1); 311 if ((u_int)off >= NOOVERLAY) 312 off -= NOOVERLAY; 313 else if ((u_int)off >= START) 314 off -= START; 315 else 316 off = 0; 317 318 if (off >= sc->sc_fb.fb_type.fb_size) 319 return (-1); 320 321 return (bus_space_mmap(sc->sc_bustag, 322 sc->sc_paddr, CG3REG_MEM + off, 323 prot, BUS_SPACE_MAP_LINEAR)); 324 } 325