1 /* $NetBSD: cg2.c,v 1.22 2002/10/23 09:12:24 jdolecek Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * All advertising materials mentioning features or use of this software 12 * must display the following acknowledgement: 13 * This product includes software developed by the University of 14 * California, Lawrence Berkeley Laboratory. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. All advertising materials mentioning features or use of this software 25 * must display the following acknowledgement: 26 * This product includes software developed by the University of 27 * California, Berkeley and its contributors. 28 * 4. Neither the name of the University nor the names of its contributors 29 * may be used to endorse or promote products derived from this software 30 * without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 * 44 * from: @(#)cgthree.c 8.2 (Berkeley) 10/30/93 45 */ 46 47 /* 48 * color display (cg2) driver. 49 * 50 * Does not handle interrupts, even though they can occur. 51 * 52 * XXX should defer colormap updates to vertical retrace interrupts 53 */ 54 55 #include <sys/param.h> 56 #include <sys/systm.h> 57 #include <sys/conf.h> 58 #include <sys/device.h> 59 #include <sys/ioctl.h> 60 #include <sys/malloc.h> 61 #include <sys/mman.h> 62 #include <sys/proc.h> 63 #include <sys/tty.h> 64 65 #include <uvm/uvm_extern.h> 66 67 #include <dev/sun/fbio.h> 68 #include <machine/autoconf.h> 69 #include <machine/pmap.h> 70 #include <machine/cg2reg.h> 71 72 #include "fbvar.h" 73 74 #define CMSIZE 256 75 76 /* offset to and size of mapped part of frame buffer */ 77 #define PLANEMAP_SIZE 0x100000 78 #define PIXELMAP_SIZE 0x100000 79 #define ROWOPMAP_SIZE 0x100000 80 81 #define CTLREGS_OFF 0x300000 82 #define CTLREGS_SIZE 0x10600 83 84 #define CG2_MAPPED_OFFSET (PLANEMAP_SIZE + PIXELMAP_SIZE) 85 #define CG2_MAPPED_SIZE (CTLREGS_OFF + CTLREGS_SIZE) 86 87 /* per-display variables */ 88 struct cg2_softc { 89 struct device sc_dev; /* base device */ 90 struct fbdevice sc_fb; /* frame buffer device */ 91 int sc_phys; /* display RAM (phys addr) */ 92 int sc_pmtype; /* pmap type bits */ 93 struct cg2fb *sc_ctlreg; /* control registers */ 94 }; 95 96 /* autoconfiguration driver */ 97 static void cg2attach __P((struct device *, struct device *, void *)); 98 static int cg2match __P((struct device *, struct cfdata *, void *)); 99 100 CFATTACH_DECL(cgtwo, sizeof(struct cg2_softc), 101 cg2match, cg2attach, NULL, NULL); 102 103 extern struct cfdriver cgtwo_cd; 104 105 dev_type_open(cg2open); 106 dev_type_ioctl(cg2ioctl); 107 dev_type_mmap(cg2mmap); 108 109 const struct cdevsw cgtwo_cdevsw = { 110 cg2open, nullclose, noread, nowrite, cg2ioctl, 111 nostop, notty, nopoll, cg2mmap, nokqfilter, 112 }; 113 114 static int cg2gattr __P((struct fbdevice *, void *)); 115 static int cg2gvideo __P((struct fbdevice *, void *)); 116 static int cg2svideo __P((struct fbdevice *, void *)); 117 static int cg2getcmap __P((struct fbdevice *, void *)); 118 static int cg2putcmap __P((struct fbdevice *, void *)); 119 120 static struct fbdriver cg2fbdriver = { 121 cg2open, nullclose, cg2mmap, nokqfilter, cg2gattr, 122 cg2gvideo, cg2svideo, 123 cg2getcmap, cg2putcmap }; 124 125 static int cg2intr __P((void*)); 126 127 /* 128 * Match a cg2. 129 */ 130 static int 131 cg2match(parent, cf, aux) 132 struct device *parent; 133 struct cfdata *cf; 134 void *aux; 135 { 136 struct confargs *ca = aux; 137 int probe_addr; 138 139 /* No default VME address. */ 140 if (ca->ca_paddr == -1) 141 return (0); 142 143 /* Make sure something is there... */ 144 probe_addr = ca->ca_paddr + CTLREGS_OFF; 145 if (bus_peek(ca->ca_bustype, probe_addr, 1) == -1) 146 return (0); 147 148 /* XXX: look at the ID reg? */ 149 /* printf("cg2: id=0x%x\n", x); */ 150 151 /* Default interrupt priority. */ 152 if (ca->ca_intpri == -1) 153 ca->ca_intpri = 4; 154 155 return (1); 156 } 157 158 /* 159 * Attach a display. We need to notice if it is the console, too. 160 */ 161 static void 162 cg2attach(parent, self, args) 163 struct device *parent, *self; 164 void *args; 165 { 166 struct cg2_softc *sc = (struct cg2_softc *)self; 167 struct fbdevice *fb = &sc->sc_fb; 168 struct confargs *ca = args; 169 struct fbtype *fbt; 170 171 sc->sc_phys = ca->ca_paddr; 172 sc->sc_pmtype = PMAP_NC | PMAP_VME16; 173 174 sc->sc_ctlreg = (struct cg2fb *) bus_mapin(ca->ca_bustype, 175 ca->ca_paddr + CTLREGS_OFF, CTLREGS_SIZE); 176 177 isr_add_vectored(cg2intr, (void*)sc, 178 ca->ca_intpri, ca->ca_intvec); 179 180 /* 181 * XXX - Initialize? Determine type? 182 */ 183 sc->sc_ctlreg->intrptvec.reg = ca->ca_intvec; 184 sc->sc_ctlreg->status.word = 1; 185 186 fb->fb_driver = &cg2fbdriver; 187 fb->fb_private = sc; 188 fb->fb_name = sc->sc_dev.dv_xname; 189 190 fbt = &fb->fb_fbtype; 191 fbt->fb_type = FBTYPE_SUN2COLOR; 192 fbt->fb_depth = 8; 193 fbt->fb_cmsize = CMSIZE; 194 195 fbt->fb_width = 1152; 196 fbt->fb_height = 900; 197 fbt->fb_size = CG2_MAPPED_SIZE; 198 199 printf(" (%dx%d)\n", fbt->fb_width, fbt->fb_height); 200 fb_attach(fb, 2); 201 } 202 203 int 204 cg2open(dev, flags, mode, p) 205 dev_t dev; 206 int flags, mode; 207 struct proc *p; 208 { 209 int unit = minor(dev); 210 211 if (unit >= cgtwo_cd.cd_ndevs || cgtwo_cd.cd_devs[unit] == NULL) 212 return (ENXIO); 213 return (0); 214 } 215 216 int 217 cg2ioctl(dev, cmd, data, flags, p) 218 dev_t dev; 219 u_long cmd; 220 caddr_t data; 221 int flags; 222 struct proc *p; 223 { 224 struct cg2_softc *sc = cgtwo_cd.cd_devs[minor(dev)]; 225 226 return (fbioctlfb(&sc->sc_fb, cmd, data)); 227 } 228 229 /* 230 * Return the address that would map the given device at the given 231 * offset, allowing for the given protection, or return -1 for error. 232 */ 233 paddr_t 234 cg2mmap(dev, off, prot) 235 dev_t dev; 236 off_t off; 237 int prot; 238 { 239 struct cg2_softc *sc = cgtwo_cd.cd_devs[minor(dev)]; 240 241 if (off & PGOFSET) 242 panic("cg2mmap"); 243 244 if (off >= CG2_MAPPED_SIZE) 245 return (-1); 246 247 /* 248 * I turned on PMAP_NC here to disable the cache as I was 249 * getting horribly broken behaviour with it on. 250 */ 251 return ((sc->sc_phys + off) | sc->sc_pmtype); 252 } 253 254 /* 255 * Internal ioctl functions. 256 */ 257 258 /* FBIOGATTR: */ 259 static int cg2gattr(fb, data) 260 struct fbdevice *fb; 261 void *data; 262 { 263 struct fbgattr *fba = data; 264 265 fba->real_type = fb->fb_fbtype.fb_type; 266 fba->owner = 0; /* XXX - TIOCCONS stuff? */ 267 fba->fbtype = fb->fb_fbtype; 268 fba->sattr.flags = 0; 269 fba->sattr.emu_type = fb->fb_fbtype.fb_type; 270 fba->sattr.dev_specific[0] = -1; 271 fba->emu_types[0] = fb->fb_fbtype.fb_type; 272 fba->emu_types[1] = -1; 273 return (0); 274 } 275 276 /* FBIOGVIDEO: */ 277 static int cg2gvideo(fb, data) 278 struct fbdevice *fb; 279 void *data; 280 { 281 int *on = data; 282 struct cg2_softc *sc = fb->fb_private; 283 284 *on = sc->sc_ctlreg->status.reg.video_enab; 285 return (0); 286 } 287 288 /* FBIOSVIDEO: */ 289 static int cg2svideo(fb, data) 290 struct fbdevice *fb; 291 void *data; 292 { 293 int *on = data; 294 struct cg2_softc *sc = fb->fb_private; 295 296 sc->sc_ctlreg->status.reg.video_enab = (*on) & 1; 297 298 return (0); 299 } 300 301 /* FBIOGETCMAP: */ 302 static int cg2getcmap(fb, data) 303 struct fbdevice *fb; 304 void *data; 305 { 306 struct fbcmap *cmap = data; 307 struct cg2_softc *sc = fb->fb_private; 308 u_char red[CMSIZE], green[CMSIZE], blue[CMSIZE]; 309 int error, start, count, ecount; 310 u_int i; 311 u_short *p; 312 313 start = cmap->index; 314 count = cmap->count; 315 ecount = start + count; 316 if (start >= CMSIZE || count > CMSIZE - start) 317 return (EINVAL); 318 319 /* XXX - Wait for retrace? */ 320 321 /* Copy hardware to local arrays. */ 322 p = &sc->sc_ctlreg->redmap[start]; 323 for (i = start; i < ecount; i++) 324 red[i] = *p++; 325 p = &sc->sc_ctlreg->greenmap[start]; 326 for (i = start; i < ecount; i++) 327 green[i] = *p++; 328 p = &sc->sc_ctlreg->bluemap[start]; 329 for (i = start; i < ecount; i++) 330 blue[i] = *p++; 331 332 /* Copy local arrays to user space. */ 333 if ((error = copyout(red + start, cmap->red, count)) != 0) 334 return (error); 335 if ((error = copyout(green + start, cmap->green, count)) != 0) 336 return (error); 337 if ((error = copyout(blue + start, cmap->blue, count)) != 0) 338 return (error); 339 340 return (0); 341 } 342 343 /* FBIOPUTCMAP: */ 344 static int cg2putcmap(fb, data) 345 struct fbdevice *fb; 346 void *data; 347 { 348 struct fbcmap *cmap = data; 349 struct cg2_softc *sc = fb->fb_private; 350 u_char red[CMSIZE], green[CMSIZE], blue[CMSIZE]; 351 int error; 352 u_int start, count, ecount; 353 u_int i; 354 u_short *p; 355 356 start = cmap->index; 357 count = cmap->count; 358 ecount = start + count; 359 if (start >= CMSIZE || count > CMSIZE - start) 360 return (EINVAL); 361 362 /* Copy from user space to local arrays. */ 363 if ((error = copyin(cmap->red, red + start, count)) != 0) 364 return (error); 365 if ((error = copyin(cmap->green, green + start, count)) != 0) 366 return (error); 367 if ((error = copyin(cmap->blue, blue + start, count)) != 0) 368 return (error); 369 370 /* XXX - Wait for retrace? */ 371 372 /* Copy from local arrays to hardware. */ 373 p = &sc->sc_ctlreg->redmap[start]; 374 for (i = start; i < ecount; i++) 375 *p++ = red[i]; 376 p = &sc->sc_ctlreg->greenmap[start]; 377 for (i = start; i < ecount; i++) 378 *p++ = green[i]; 379 p = &sc->sc_ctlreg->bluemap[start]; 380 for (i = start; i < ecount; i++) 381 *p++ = blue[i]; 382 383 return (0); 384 385 } 386 387 static int 388 cg2intr(vsc) 389 void *vsc; 390 { 391 struct cg2_softc *sc = vsc; 392 393 /* XXX - Just disable interrupts for now. */ 394 sc->sc_ctlreg->status.reg.inten = 0; 395 396 printf("cg2intr\n"); 397 return (1); 398 } 399