1 /* $NetBSD: cgthree.c,v 1.2 2000/08/20 20:01:45 pk 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * color display (cgthree) driver. 41 * 42 * Does not handle interrupts, even though they can occur. 43 * 44 * XXX should defer colormap updates to vertical retrace interrupts 45 */ 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/buf.h> 50 #include <sys/device.h> 51 #include <sys/ioctl.h> 52 #include <sys/malloc.h> 53 #include <sys/mman.h> 54 #include <sys/tty.h> 55 #include <sys/conf.h> 56 57 #include <machine/bus.h> 58 #include <machine/autoconf.h> 59 60 #include <dev/sun/fbio.h> 61 #include <dev/sun/fbvar.h> 62 63 #include <dev/sun/btreg.h> 64 #include <dev/sun/btvar.h> 65 #include <dev/sun/cgthreereg.h> 66 #include <dev/sun/cgthreevar.h> 67 68 #include <machine/conf.h> 69 70 static void cgthreeunblank(struct device *); 71 static void cgthreeloadcmap(struct cgthree_softc *, int, int); 72 static void cgthree_set_video(struct cgthree_softc *, int); 73 static int cgthree_get_video(struct cgthree_softc *); 74 75 /* cdevsw prototypes */ 76 cdev_decl(cgthree); 77 78 extern struct cfdriver cgthree_cd; 79 80 /* frame buffer generic driver */ 81 static struct fbdriver cgthreefbdriver = { 82 cgthreeunblank, cgthreeopen, cgthreeclose, cgthreeioctl, cgthreepoll, 83 cgthreemmap 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 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, p) 159 dev_t dev; 160 int flags, mode; 161 struct proc *p; 162 { 163 int unit = minor(dev); 164 165 if (unit >= cgthree_cd.cd_ndevs || cgthree_cd.cd_devs[unit] == NULL) 166 return (ENXIO); 167 return (0); 168 } 169 170 int 171 cgthreeclose(dev, flags, mode, p) 172 dev_t dev; 173 int flags, mode; 174 struct proc *p; 175 { 176 177 return (0); 178 } 179 180 int 181 cgthreeioctl(dev, cmd, data, flags, p) 182 dev_t dev; 183 u_long cmd; 184 caddr_t data; 185 int flags; 186 struct proc *p; 187 { 188 struct cgthree_softc *sc = cgthree_cd.cd_devs[minor(dev)]; 189 struct fbgattr *fba; 190 int error; 191 192 switch (cmd) { 193 194 case FBIOGTYPE: 195 *(struct fbtype *)data = sc->sc_fb.fb_type; 196 break; 197 198 case FBIOGATTR: 199 fba = (struct fbgattr *)data; 200 fba->real_type = sc->sc_fb.fb_type.fb_type; 201 fba->owner = 0; /* XXX ??? */ 202 fba->fbtype = sc->sc_fb.fb_type; 203 fba->sattr.flags = 0; 204 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type; 205 fba->sattr.dev_specific[0] = -1; 206 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type; 207 fba->emu_types[1] = -1; 208 break; 209 210 case FBIOGETCMAP: 211 #define p ((struct fbcmap *)data) 212 return (bt_getcmap(p, &sc->sc_cmap, 256, 1)); 213 214 case FBIOPUTCMAP: 215 /* copy to software map */ 216 error = bt_putcmap(p, &sc->sc_cmap, 256, 1); 217 if (error) 218 return (error); 219 /* now blast them into the chip */ 220 /* XXX should use retrace interrupt */ 221 cgthreeloadcmap(sc, p->index, p->count); 222 #undef p 223 break; 224 225 case FBIOGVIDEO: 226 *(int *)data = cgthree_get_video(sc); 227 break; 228 229 case FBIOSVIDEO: 230 cgthree_set_video(sc, *(int *)data); 231 break; 232 233 default: 234 return (ENOTTY); 235 } 236 return (0); 237 } 238 239 int 240 cgthreepoll(dev, events, p) 241 dev_t dev; 242 int events; 243 struct proc *p; 244 { 245 246 return (seltrue(dev, events, p)); 247 } 248 249 /* 250 * Undo the effect of an FBIOSVIDEO that turns the video off. 251 */ 252 static void 253 cgthreeunblank(dev) 254 struct device *dev; 255 { 256 257 cgthree_set_video((struct cgthree_softc *)dev, 1); 258 } 259 260 static void 261 cgthree_set_video(sc, enable) 262 struct cgthree_softc *sc; 263 int enable; 264 { 265 266 if (enable) 267 sc->sc_fbc->fbc_ctrl |= FBC_VENAB; 268 else 269 sc->sc_fbc->fbc_ctrl &= ~FBC_VENAB; 270 } 271 272 static int 273 cgthree_get_video(sc) 274 struct cgthree_softc *sc; 275 { 276 277 return ((sc->sc_fbc->fbc_ctrl & FBC_VENAB) != 0); 278 } 279 280 /* 281 * Load a subset of the current (new) colormap into the Brooktree DAC. 282 */ 283 static void 284 cgthreeloadcmap(sc, start, ncolors) 285 struct cgthree_softc *sc; 286 int start, ncolors; 287 { 288 volatile struct bt_regs *bt; 289 u_int *ip; 290 int count; 291 292 ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)]; /* start/4 * 3 */ 293 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3; 294 bt = &sc->sc_fbc->fbc_dac; 295 bt->bt_addr = BT_D4M4(start); 296 while (--count >= 0) 297 bt->bt_cmap = *ip++; 298 } 299 300 /* 301 * Return the address that would map the given device at the given 302 * offset, allowing for the given protection, or return -1 for error. 303 * 304 * The cg3 is mapped starting at 256KB, for pseudo-compatibility with 305 * the cg4 (which had an overlay plane in the first 128K and an enable 306 * plane in the next 128K). X11 uses only 256k+ region but tries to 307 * map the whole thing, so we repeatedly map the first 256K to the 308 * first page of the color screen. If someone tries to use the overlay 309 * and enable regions, they will get a surprise.... 310 * 311 * As well, mapping at an offset of 0x04000000 causes the cg3 to be 312 * mapped in flat mode without the cg4 emulation. 313 */ 314 paddr_t 315 cgthreemmap(dev, off, prot) 316 dev_t dev; 317 off_t off; 318 int prot; 319 { 320 struct cgthree_softc *sc = cgthree_cd.cd_devs[minor(dev)]; 321 bus_space_handle_t bh; 322 323 #define START (128*1024 + 128*1024) 324 #define NOOVERLAY (0x04000000) 325 326 if (off & PGOFSET) 327 panic("cgthreemmap"); 328 if (off < 0) 329 return (-1); 330 if ((u_int)off >= NOOVERLAY) 331 off -= NOOVERLAY; 332 else if ((u_int)off >= START) 333 off -= START; 334 else 335 off = 0; 336 337 if (off >= sc->sc_fb.fb_type.fb_size) 338 return (-1); 339 340 if (bus_space_mmap(sc->sc_bustag, 341 sc->sc_btype, 342 sc->sc_paddr + CG3REG_MEM + off, 343 BUS_SPACE_MAP_LINEAR, &bh)) 344 return (-1); 345 346 return ((paddr_t)bh); 347 } 348