1 /* $NetBSD: grf.c,v 1.42 2002/09/06 13:18:43 gehenna 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.42 2002/09/06 13:18:43 gehenna 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 76 #include "view.h" 77 #include "grf.h" 78 79 #if NGRF > 0 80 #include "ite.h" 81 #if NITE == 0 82 #define ite_on(u,f) 83 #define ite_off(u,f) 84 #define ite_reinit(d) 85 #endif 86 87 int grfon(dev_t); 88 int grfoff(dev_t); 89 int grfsinfo(dev_t, struct grfdyninfo *); 90 91 void grfattach(struct device *, struct device *, void *); 92 int grfmatch(struct device *, struct cfdata *, void *); 93 int grfprint(void *, const char *); 94 /* 95 * pointers to grf drivers device structs 96 */ 97 struct grf_softc *grfsp[NGRF]; 98 99 struct cfattach grf_ca = { 100 sizeof(struct device), grfmatch, grfattach 101 }; 102 103 dev_type_open(grfopen); 104 dev_type_close(grfclose); 105 dev_type_ioctl(grfioctl); 106 dev_type_poll(grfpoll); 107 dev_type_mmap(grfmmap); 108 109 const struct cdevsw grf_cdevsw = { 110 grfopen, grfclose, nullread, nullwrite, grfioctl, 111 nostop, notty, grfpoll, grfmmap, 112 }; 113 114 /* 115 * only used in console init. 116 */ 117 static struct cfdata *cfdata; 118 119 /* 120 * match if the unit of grf matches its perspective 121 * low level board driver. 122 */ 123 int 124 grfmatch(struct device *pdp, struct cfdata *cfp, void *auxp) 125 { 126 127 if (cfp->cf_unit != ((struct grf_softc *)pdp)->g_unit) 128 return(0); 129 cfdata = cfp; 130 return(1); 131 } 132 133 /* 134 * attach.. plug pointer in and print some info. 135 * then try and attach an ite to us. note: dp is NULL 136 * durring console init. 137 */ 138 void 139 grfattach(struct device *pdp, struct device *dp, void *auxp) 140 { 141 struct grf_softc *gp; 142 int maj; 143 144 gp = (struct grf_softc *)pdp; 145 grfsp[gp->g_unit] = (struct grf_softc *)pdp; 146 147 /* 148 * find our major device number 149 */ 150 maj = cdevsw_lookup_major(&grf_cdevsw); 151 152 gp->g_grfdev = makedev(maj, gp->g_unit); 153 if (dp != NULL) { 154 printf(": width %d height %d", gp->g_display.gd_dwidth, 155 gp->g_display.gd_dheight); 156 if (gp->g_display.gd_colors == 2) 157 printf(" monochrome\n"); 158 else 159 printf(" colors %d\n", gp->g_display.gd_colors); 160 } 161 162 /* 163 * try and attach an ite 164 */ 165 amiga_config_found(cfdata, dp, gp, grfprint); 166 } 167 168 int 169 grfprint(void *auxp, const char *pnp) 170 { 171 if (pnp) 172 printf("ite at %s", pnp); 173 return(UNCONF); 174 } 175 176 /*ARGSUSED*/ 177 int 178 grfopen(dev_t dev, int flags, int devtype, struct proc *p) 179 { 180 struct grf_softc *gp; 181 182 if (GRFUNIT(dev) >= NGRF || (gp = grfsp[GRFUNIT(dev)]) == NULL) 183 return(ENXIO); 184 185 if ((gp->g_flags & GF_ALIVE) == 0) 186 return(ENXIO); 187 188 if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE)) 189 return(EBUSY); 190 191 return(0); 192 } 193 194 /*ARGSUSED*/ 195 int 196 grfclose(dev_t dev, int flags, int mode, struct proc *p) 197 { 198 struct grf_softc *gp; 199 200 gp = grfsp[GRFUNIT(dev)]; 201 (void)grfoff(dev); 202 gp->g_flags &= GF_ALIVE; 203 return(0); 204 } 205 206 /*ARGSUSED*/ 207 int 208 grfioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 209 { 210 struct grf_softc *gp; 211 int error; 212 213 gp = grfsp[GRFUNIT(dev)]; 214 error = 0; 215 216 switch (cmd) { 217 case OGRFIOCGINFO: 218 /* argl.. no bank-member.. */ 219 bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo)-4); 220 break; 221 case GRFIOCGINFO: 222 bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo)); 223 break; 224 case GRFIOCON: 225 error = grfon(dev); 226 break; 227 case GRFIOCOFF: 228 error = grfoff(dev); 229 break; 230 case GRFIOCSINFO: 231 error = grfsinfo(dev, (struct grfdyninfo *) data); 232 break; 233 case GRFGETVMODE: 234 return(gp->g_mode(gp, GM_GRFGETVMODE, data, 0, 0)); 235 case GRFSETVMODE: 236 error = gp->g_mode(gp, GM_GRFSETVMODE, data, 0, 0); 237 if (error == 0 && gp->g_itedev && !(gp->g_flags & GF_GRFON)) 238 ite_reinit(gp->g_itedev); 239 break; 240 case GRFGETNUMVM: 241 return(gp->g_mode(gp, GM_GRFGETNUMVM, data, 0, 0)); 242 /* 243 * these are all hardware dependant, and have to be resolved 244 * in the respective driver. 245 */ 246 case GRFIOCPUTCMAP: 247 case GRFIOCGETCMAP: 248 case GRFIOCSSPRITEPOS: 249 case GRFIOCGSPRITEPOS: 250 case GRFIOCSSPRITEINF: 251 case GRFIOCGSPRITEINF: 252 case GRFIOCGSPRITEMAX: 253 case GRFIOCBITBLT: 254 case GRFIOCSETMON: 255 case GRFTOGGLE: /* Toggles between Cirrus boards and native ECS on 256 Amiga. 15/11/94 ill */ 257 /* 258 * We need the minor dev number to get the overlay/image 259 * information for grf_ul. 260 */ 261 return(gp->g_mode(gp, GM_GRFIOCTL, data, cmd, dev)); 262 263 case GRFIOCBLANK: /* blank ioctl, IOCON/OFF will turn ite on */ 264 case FBIOSVIDEO: 265 error = gp->g_mode(gp, GM_GRFIOCTL, data, GRFIOCBLANK, dev); 266 if (!error) 267 gp->g_blank = *(int *)data; 268 return (error); 269 270 case FBIOGVIDEO: 271 *(int *)data = gp->g_blank; 272 return (0); 273 274 default: 275 #if NVIEW > 0 276 /* 277 * check to see whether it's a command recognized by the 278 * view code if the unit is 0 279 * XXX 280 */ 281 if (GRFUNIT(dev) == 0) { 282 extern const struct cdevsw view_cdevsw; 283 284 return((*view_cdevsw.d_ioctl)(dev, cmd, data, flag, p)); 285 } 286 #endif 287 error = EPASSTHROUGH; 288 break; 289 290 } 291 return(error); 292 } 293 294 /*ARGSUSED*/ 295 int 296 grfpoll(dev_t dev, int events, struct proc *p) 297 { 298 return(events & (POLLOUT | POLLWRNORM)); 299 } 300 301 /* 302 * map the contents of a graphics display card into process' 303 * memory space. 304 */ 305 paddr_t 306 grfmmap(dev_t dev, off_t off, int prot) 307 { 308 struct grf_softc *gp; 309 struct grfinfo *gi; 310 311 gp = grfsp[GRFUNIT(dev)]; 312 gi = &gp->g_display; 313 314 /* 315 * control registers 316 */ 317 if (off >= 0 && off < gi->gd_regsize) 318 return(((paddr_t)gi->gd_regaddr + off) >> PGSHIFT); 319 320 /* 321 * frame buffer 322 */ 323 if (off >= gi->gd_regsize && off < gi->gd_regsize+gi->gd_fbsize) { 324 off -= gi->gd_regsize; 325 return(((paddr_t)gi->gd_fbaddr + off) >> PGSHIFT); 326 } 327 /* bogus */ 328 return(-1); 329 } 330 331 int 332 grfon(dev_t dev) 333 { 334 struct grf_softc *gp; 335 336 gp = grfsp[GRFUNIT(dev)]; 337 338 if (gp->g_flags & GF_GRFON) 339 return(0); 340 341 gp->g_flags |= GF_GRFON; 342 if (gp->g_itedev != NODEV) 343 ite_off(gp->g_itedev, 3); 344 345 return(gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON, 346 NULL, 0, 0)); 347 } 348 349 int 350 grfoff(dev_t dev) 351 { 352 struct grf_softc *gp; 353 int error; 354 355 gp = grfsp[GRFUNIT(dev)]; 356 357 if ((gp->g_flags & GF_GRFON) == 0) 358 return(0); 359 360 gp->g_flags &= ~GF_GRFON; 361 error = gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF, 362 NULL, 0, 0); 363 364 /* 365 * Closely tied together no X's 366 */ 367 if (gp->g_itedev != NODEV) 368 ite_on(gp->g_itedev, 2); 369 370 return(error); 371 } 372 373 int 374 grfsinfo(dev_t dev, struct grfdyninfo *dyninfo) 375 { 376 struct grf_softc *gp; 377 int error; 378 379 gp = grfsp[GRFUNIT(dev)]; 380 error = gp->g_mode(gp, GM_GRFCONFIG, dyninfo, 0, 0); 381 382 /* 383 * Closely tied together no X's 384 */ 385 if (gp->g_itedev != NODEV) 386 ite_reinit(gp->g_itedev); 387 return(error); 388 } 389 390 #endif /* NGRF > 0 */ 391