1 /* $NetBSD: grf.c,v 1.41 2002/03/17 19:40:28 atatat Exp $ */ 2 3 /* 4 * Copyright (c) 1988 University of Utah. 5 * Copyright (c) 1990 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: Utah $Hdr: grf.c 1.31 91/01/21$ 41 * 42 * @(#)grf.c 7.8 (Berkeley) 5/7/91 43 */ 44 45 #include <sys/cdefs.h> 46 __KERNEL_RCSID(0, "$NetBSD: grf.c,v 1.41 2002/03/17 19:40:28 atatat Exp $"); 47 48 /* 49 * Graphics display driver for the Amiga 50 * This is the hardware-independent portion of the driver. 51 * Hardware access is through the grf_softc->g_mode routine. 52 */ 53 54 #include <sys/param.h> 55 #include <sys/proc.h> 56 #include <sys/ioctl.h> 57 #include <sys/device.h> 58 #include <sys/file.h> 59 #include <sys/malloc.h> 60 #include <sys/systm.h> 61 #include <sys/vnode.h> 62 #include <sys/mman.h> 63 #include <sys/poll.h> 64 #include <uvm/uvm_extern.h> 65 #include <machine/cpu.h> 66 #include <dev/sun/fbio.h> 67 #include <amiga/amiga/color.h> /* DEBUG */ 68 #include <amiga/amiga/device.h> 69 #include <amiga/dev/grfioctl.h> 70 #include <amiga/dev/grfvar.h> 71 #include <amiga/dev/itevar.h> 72 #include <amiga/dev/viewioctl.h> 73 74 #include <sys/conf.h> 75 #include <machine/conf.h> 76 77 #include "view.h" 78 #include "grf.h" 79 80 #if NGRF > 0 81 #include "ite.h" 82 #if NITE == 0 83 #define ite_on(u,f) 84 #define ite_off(u,f) 85 #define ite_reinit(d) 86 #endif 87 88 int grfon(dev_t); 89 int grfoff(dev_t); 90 int grfsinfo(dev_t, struct grfdyninfo *); 91 #ifdef BANKEDDEVPAGER 92 int grfbanked_get(dev_t, off_t, int); 93 int grfbanked_cur(dev_t); 94 int grfbanked_set(dev_t, int); 95 #endif 96 97 void grfattach(struct device *, struct device *, void *); 98 int grfmatch(struct device *, struct cfdata *, void *); 99 int grfprint(void *, const char *); 100 /* 101 * pointers to grf drivers device structs 102 */ 103 struct grf_softc *grfsp[NGRF]; 104 105 struct cfattach grf_ca = { 106 sizeof(struct device), grfmatch, grfattach 107 }; 108 109 /* 110 * only used in console init. 111 */ 112 static struct cfdata *cfdata; 113 114 /* 115 * match if the unit of grf matches its perspective 116 * low level board driver. 117 */ 118 int 119 grfmatch(struct device *pdp, struct cfdata *cfp, void *auxp) 120 { 121 122 if (cfp->cf_unit != ((struct grf_softc *)pdp)->g_unit) 123 return(0); 124 cfdata = cfp; 125 return(1); 126 } 127 128 /* 129 * attach.. plug pointer in and print some info. 130 * then try and attach an ite to us. note: dp is NULL 131 * durring console init. 132 */ 133 void 134 grfattach(struct device *pdp, struct device *dp, void *auxp) 135 { 136 struct grf_softc *gp; 137 int maj; 138 139 gp = (struct grf_softc *)pdp; 140 grfsp[gp->g_unit] = (struct grf_softc *)pdp; 141 142 /* 143 * find our major device number 144 */ 145 for(maj = 0; maj < nchrdev; maj++) 146 if (cdevsw[maj].d_open == grfopen) 147 break; 148 149 gp->g_grfdev = makedev(maj, gp->g_unit); 150 if (dp != NULL) { 151 printf(": width %d height %d", gp->g_display.gd_dwidth, 152 gp->g_display.gd_dheight); 153 if (gp->g_display.gd_colors == 2) 154 printf(" monochrome\n"); 155 else 156 printf(" colors %d\n", gp->g_display.gd_colors); 157 } 158 159 /* 160 * try and attach an ite 161 */ 162 amiga_config_found(cfdata, dp, gp, grfprint); 163 } 164 165 int 166 grfprint(void *auxp, const char *pnp) 167 { 168 if (pnp) 169 printf("ite at %s", pnp); 170 return(UNCONF); 171 } 172 173 /*ARGSUSED*/ 174 int 175 grfopen(dev_t dev, int flags, int devtype, struct proc *p) 176 { 177 struct grf_softc *gp; 178 179 if (GRFUNIT(dev) >= NGRF || (gp = grfsp[GRFUNIT(dev)]) == NULL) 180 return(ENXIO); 181 182 if ((gp->g_flags & GF_ALIVE) == 0) 183 return(ENXIO); 184 185 if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE)) 186 return(EBUSY); 187 188 return(0); 189 } 190 191 /*ARGSUSED*/ 192 int 193 grfclose(dev_t dev, int flags, int mode, struct proc *p) 194 { 195 struct grf_softc *gp; 196 197 gp = grfsp[GRFUNIT(dev)]; 198 (void)grfoff(dev); 199 gp->g_flags &= GF_ALIVE; 200 return(0); 201 } 202 203 /*ARGSUSED*/ 204 int 205 grfioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 206 { 207 struct grf_softc *gp; 208 int error; 209 210 gp = grfsp[GRFUNIT(dev)]; 211 error = 0; 212 213 switch (cmd) { 214 case OGRFIOCGINFO: 215 /* argl.. no bank-member.. */ 216 bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo)-4); 217 break; 218 case GRFIOCGINFO: 219 bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo)); 220 break; 221 case GRFIOCON: 222 error = grfon(dev); 223 break; 224 case GRFIOCOFF: 225 error = grfoff(dev); 226 break; 227 case GRFIOCSINFO: 228 error = grfsinfo(dev, (struct grfdyninfo *) data); 229 break; 230 case GRFGETVMODE: 231 return(gp->g_mode(gp, GM_GRFGETVMODE, data, 0, 0)); 232 case GRFSETVMODE: 233 error = gp->g_mode(gp, GM_GRFSETVMODE, data, 0, 0); 234 if (error == 0 && gp->g_itedev && !(gp->g_flags & GF_GRFON)) 235 ite_reinit(gp->g_itedev); 236 break; 237 case GRFGETNUMVM: 238 return(gp->g_mode(gp, GM_GRFGETNUMVM, data, 0, 0)); 239 /* 240 * these are all hardware dependant, and have to be resolved 241 * in the respective driver. 242 */ 243 case GRFIOCPUTCMAP: 244 case GRFIOCGETCMAP: 245 case GRFIOCSSPRITEPOS: 246 case GRFIOCGSPRITEPOS: 247 case GRFIOCSSPRITEINF: 248 case GRFIOCGSPRITEINF: 249 case GRFIOCGSPRITEMAX: 250 case GRFIOCBITBLT: 251 case GRFIOCSETMON: 252 case GRFTOGGLE: /* Toggles between Cirrus boards and native ECS on 253 Amiga. 15/11/94 ill */ 254 /* 255 * We need the minor dev number to get the overlay/image 256 * information for grf_ul. 257 */ 258 return(gp->g_mode(gp, GM_GRFIOCTL, data, cmd, dev)); 259 260 case GRFIOCBLANK: /* blank ioctl, IOCON/OFF will turn ite on */ 261 case FBIOSVIDEO: 262 error = gp->g_mode(gp, GM_GRFIOCTL, data, GRFIOCBLANK, dev); 263 if (!error) 264 gp->g_blank = *(int *)data; 265 return (error); 266 267 case FBIOGVIDEO: 268 *(int *)data = gp->g_blank; 269 return (0); 270 271 default: 272 #if NVIEW > 0 273 /* 274 * check to see whether it's a command recognized by the 275 * view code if the unit is 0 276 * XXX 277 */ 278 if (GRFUNIT(dev) == 0) 279 return(viewioctl(dev, cmd, data, flag, p)); 280 #endif 281 error = EPASSTHROUGH; 282 break; 283 284 } 285 return(error); 286 } 287 288 /*ARGSUSED*/ 289 int 290 grfpoll(dev_t dev, int events, struct proc *p) 291 { 292 return(events & (POLLOUT | POLLWRNORM)); 293 } 294 295 /* 296 * map the contents of a graphics display card into process' 297 * memory space. 298 */ 299 paddr_t 300 grfmmap(dev_t dev, off_t off, int prot) 301 { 302 struct grf_softc *gp; 303 struct grfinfo *gi; 304 305 gp = grfsp[GRFUNIT(dev)]; 306 gi = &gp->g_display; 307 308 /* 309 * control registers 310 */ 311 if (off >= 0 && off < gi->gd_regsize) 312 return(((paddr_t)gi->gd_regaddr + off) >> PGSHIFT); 313 314 /* 315 * frame buffer 316 */ 317 if (off >= gi->gd_regsize && off < gi->gd_regsize+gi->gd_fbsize) { 318 off -= gi->gd_regsize; 319 #ifdef BANKEDDEVPAGER 320 if (gi->gd_bank_size) 321 off %= gi->gd_bank_size; 322 #endif 323 return(((paddr_t)gi->gd_fbaddr + off) >> PGSHIFT); 324 } 325 /* bogus */ 326 return(-1); 327 } 328 329 int 330 grfon(dev_t dev) 331 { 332 struct grf_softc *gp; 333 334 gp = grfsp[GRFUNIT(dev)]; 335 336 if (gp->g_flags & GF_GRFON) 337 return(0); 338 339 gp->g_flags |= GF_GRFON; 340 if (gp->g_itedev != NODEV) 341 ite_off(gp->g_itedev, 3); 342 343 return(gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON, 344 NULL, 0, 0)); 345 } 346 347 int 348 grfoff(dev_t dev) 349 { 350 struct grf_softc *gp; 351 int error; 352 353 gp = grfsp[GRFUNIT(dev)]; 354 355 if ((gp->g_flags & GF_GRFON) == 0) 356 return(0); 357 358 gp->g_flags &= ~GF_GRFON; 359 error = gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF, 360 NULL, 0, 0); 361 362 /* 363 * Closely tied together no X's 364 */ 365 if (gp->g_itedev != NODEV) 366 ite_on(gp->g_itedev, 2); 367 368 return(error); 369 } 370 371 int 372 grfsinfo(dev_t dev, struct grfdyninfo *dyninfo) 373 { 374 struct grf_softc *gp; 375 int error; 376 377 gp = grfsp[GRFUNIT(dev)]; 378 error = gp->g_mode(gp, GM_GRFCONFIG, dyninfo, 0, 0); 379 380 /* 381 * Closely tied together no X's 382 */ 383 if (gp->g_itedev != NODEV) 384 ite_reinit(gp->g_itedev); 385 return(error); 386 } 387 388 #ifdef BANKEDDEVPAGER 389 390 int 391 grfbanked_get(dev_t dev, off_t off, int prot) 392 { 393 struct grf_softc *gp; 394 struct grfinfo *gi; 395 int error, bank; 396 397 gp = grfsp[GRFUNIT(dev)]; 398 gi = &gp->g_display; 399 400 off -= gi->gd_regsize; 401 if (off < 0 || off >= gi->gd_fbsize) 402 return -1; 403 404 error = gp->g_mode(gp, GM_GRFGETBANK, &bank, off, prot); 405 return error ? -1 : bank; 406 } 407 408 int 409 grfbanked_cur(dev_t dev) 410 { 411 struct grf_softc *gp; 412 int error, bank; 413 414 gp = grfsp[GRFUNIT(dev)]; 415 416 error = gp->g_mode(gp, GM_GRFGETCURBANK, &bank, 0, 0); 417 return(error ? -1 : bank); 418 } 419 420 int 421 grfbanked_set(dev_t dev, int bank) 422 { 423 struct grf_softc *gp; 424 425 gp = grfsp[GRFUNIT(dev)]; 426 return(gp->g_mode(gp, GM_GRFSETBANK, &bank, 0, 0) ? -1 : 0); 427 } 428 429 #endif /* BANKEDDEVPAGER */ 430 #endif /* NGRF > 0 */ 431