1 /* $NetBSD: grf.c,v 1.26 2002/03/17 19:40:35 atatat Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Leo Weppelman 5 * Copyright (c) 1988 University of Utah. 6 * Copyright (c) 1990 The Regents of the University of California. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * the Systems Programming Group of the University of Utah Computer 11 * Science Department. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * from: Utah $Hdr: grf.c 1.31 91/01/21$ 42 * 43 * @(#)grf.c 7.8 (Berkeley) 5/7/91 44 */ 45 46 /* 47 * Graphics display driver for the Atari 48 * This is the hardware-independent portion of the driver. 49 * Hardware access is through the grf_softc->g_mode routine. 50 */ 51 52 #include <sys/param.h> 53 #include <sys/proc.h> 54 #include <sys/ioctl.h> 55 #include <sys/device.h> 56 #include <sys/file.h> 57 #include <sys/malloc.h> 58 #include <sys/conf.h> 59 #include <sys/systm.h> 60 #include <sys/vnode.h> 61 #include <sys/mman.h> 62 #include <sys/poll.h> 63 64 #include <machine/cpu.h> 65 66 #include <uvm/uvm_extern.h> 67 68 #include <atari/atari/device.h> 69 #include <atari/dev/grfioctl.h> 70 #include <atari/dev/grfabs_reg.h> 71 #include <atari/dev/grfvar.h> 72 #include <atari/dev/itevar.h> 73 #include <atari/dev/viewioctl.h> 74 #include <atari/dev/viewvar.h> 75 76 #include "grfcc.h" 77 #include "grfet.h" 78 #define NGRF (NGRFCC + NGRFET) 79 80 #if NGRF > 0 81 82 #include "ite.h" 83 #if NITE == 0 84 #define ite_on(u,f) 85 #define ite_off(u,f) 86 #define ite_reinit(d) 87 #endif 88 89 int grfon __P((dev_t)); 90 int grfoff __P((dev_t)); 91 int grfsinfo __P((dev_t, struct grfdyninfo *)); 92 #ifdef BANKEDDEVPAGER 93 int grfbanked_get __P((dev_t, off_t, int)); 94 int grfbanked_cur __P((dev_t)); 95 int grfbanked_set __P((dev_t, int)); 96 #endif 97 98 int grfbusprint __P((void *auxp, const char *)); 99 int grfbusmatch __P((struct device *, struct cfdata *, void *)); 100 void grfbusattach __P((struct device *, struct device *, void *)); 101 102 /* 103 * pointers to grf drivers device structs 104 */ 105 struct grf_softc *grfsp[NGRF]; /* XXX */ 106 107 struct cfattach grfbus_ca = { 108 sizeof(struct device), grfbusmatch, grfbusattach 109 }; 110 111 extern struct cfdriver grfbus_cd; 112 113 /* 114 * only used in console init. 115 */ 116 static struct cfdata *cfdata_gbus = NULL; 117 118 int 119 grfbusmatch(pdp, cfp, auxp) 120 struct device *pdp; 121 struct cfdata *cfp; 122 void *auxp; 123 { 124 if(strcmp(auxp, grfbus_cd.cd_name)) 125 return(0); 126 127 if(atari_realconfig == 0) 128 cfdata_gbus = cfp; 129 return(1); /* Always there */ 130 } 131 132 void 133 grfbusattach(pdp, dp, auxp) 134 struct device *pdp, *dp; 135 void *auxp; 136 { 137 grf_auxp_t grf_auxp; 138 139 grf_auxp.busprint = grfbusprint; 140 grf_auxp.from_bus_match = 1; 141 142 if(dp == NULL) /* Console init */ 143 atari_config_found(cfdata_gbus, NULL, (void*)&grf_auxp, grfbusprint); 144 else { 145 printf("\n"); 146 config_found(dp, (void*)&grf_auxp, grfbusprint); 147 } 148 } 149 150 int 151 grfbusprint(auxp, name) 152 void *auxp; 153 const char *name; 154 { 155 if(name == NULL) 156 return(UNCONF); 157 return(QUIET); 158 } 159 160 /*ARGSUSED*/ 161 int 162 grfopen(dev, flags, devtype, p) 163 dev_t dev; 164 int flags, devtype; 165 struct proc *p; 166 { 167 struct grf_softc *gp; 168 169 if (GRFUNIT(dev) >= NGRF) 170 return(ENXIO); 171 172 gp = grfsp[GRFUNIT(dev)]; 173 if (gp == NULL) 174 return(ENXIO); 175 176 if ((gp->g_flags & GF_ALIVE) == 0) 177 return(ENXIO); 178 179 if ((gp->g_flags & (GF_OPEN|GF_EXCLUDE)) == (GF_OPEN|GF_EXCLUDE)) 180 return(EBUSY); 181 grf_viewsync(gp); 182 183 return(0); 184 } 185 186 /*ARGSUSED*/ 187 int 188 grfclose(dev, flags, mode, p) 189 dev_t dev; 190 int flags; 191 int mode; 192 struct proc *p; 193 { 194 struct grf_softc *gp; 195 196 gp = grfsp[GRFUNIT(dev)]; 197 (void)grfoff(dev); 198 gp->g_flags &= GF_ALIVE; 199 return(0); 200 } 201 202 /*ARGSUSED*/ 203 int 204 grfioctl(dev, cmd, data, flag, p) 205 dev_t dev; 206 u_long cmd; 207 int flag; 208 caddr_t data; 209 struct proc *p; 210 { 211 struct grf_softc *gp; 212 int error; 213 214 gp = grfsp[GRFUNIT(dev)]; 215 error = 0; 216 217 switch (cmd) { 218 case OGRFIOCGINFO: 219 /* argl.. no bank-member.. */ 220 bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo)-4); 221 break; 222 case GRFIOCGINFO: 223 bcopy((caddr_t)&gp->g_display, data, sizeof(struct grfinfo)); 224 break; 225 case GRFIOCON: 226 error = grfon(dev); 227 break; 228 case GRFIOCOFF: 229 error = grfoff(dev); 230 break; 231 case GRFIOCSINFO: 232 error = grfsinfo(dev, (struct grfdyninfo *) data); 233 break; 234 case GRFGETVMODE: 235 return(gp->g_mode(gp, GM_GRFGETVMODE, data, 0, 0)); 236 case GRFSETVMODE: 237 error = gp->g_mode(gp, GM_GRFSETVMODE, data, 0, 0); 238 if (error == 0 && gp->g_itedev) 239 ite_reinit(gp->g_itedev); 240 break; 241 case GRFGETNUMVM: 242 return(gp->g_mode(gp, GM_GRFGETNUMVM, data, 0, 0)); 243 /* 244 * these are all hardware dependant, and have to be resolved 245 * in the respective driver. 246 */ 247 case GRFIOCPUTCMAP: 248 case GRFIOCGETCMAP: 249 case GRFIOCSSPRITEPOS: 250 case GRFIOCGSPRITEPOS: 251 case GRFIOCSSPRITEINF: 252 case GRFIOCGSPRITEINF: 253 case GRFIOCGSPRITEMAX: 254 default: 255 /* 256 * check to see whether it's a command recognized by the 257 * view code. 258 */ 259 return(viewioctl(gp->g_viewdev, cmd, data, flag, p)); 260 error = EINVAL; 261 break; 262 263 } 264 return(error); 265 } 266 267 /*ARGSUSED*/ 268 int 269 grfpoll(dev, events, p) 270 dev_t dev; 271 int events; 272 struct proc *p; 273 { 274 int revents = 0; 275 276 if (events & (POLLOUT | POLLWRNORM)) 277 revents |= events & (POLLOUT | POLLWRNORM); 278 return (revents); 279 } 280 281 /* 282 * map the contents of a graphics display card into process' 283 * memory space. 284 */ 285 paddr_t 286 grfmmap(dev, off, prot) 287 dev_t dev; 288 off_t off; 289 int prot; 290 { 291 struct grf_softc *gp; 292 struct grfinfo *gi; 293 u_int vgabase, linbase; 294 295 gp = grfsp[GRFUNIT(dev)]; 296 gi = &gp->g_display; 297 298 vgabase = gi->gd_vgabase; 299 linbase = gi->gd_linbase; 300 301 /* 302 * control registers 303 */ 304 if (off >= 0 && off < gi->gd_regsize) 305 return(((paddr_t)gi->gd_regaddr + off) >> PGSHIFT); 306 307 /* 308 * VGA memory 309 */ 310 if (off >= vgabase && off < (vgabase + gi->gd_vgasize)) 311 return(((paddr_t)gi->gd_vgaaddr - vgabase + off) >> PGSHIFT); 312 313 /* 314 * frame buffer 315 */ 316 if (off >= linbase && off < (linbase + gi->gd_fbsize)) 317 return(((paddr_t)gi->gd_fbaddr - linbase + off) >> PGSHIFT); 318 return(-1); 319 } 320 321 int 322 grfon(dev) 323 dev_t dev; 324 { 325 struct grf_softc *gp; 326 327 gp = grfsp[GRFUNIT(dev)]; 328 329 if (gp->g_flags & GF_GRFON) 330 return(0); 331 332 gp->g_flags |= GF_GRFON; 333 if (gp->g_itedev != NODEV) 334 ite_off(gp->g_itedev, 3); 335 336 return(gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVON : GM_GRFON, 337 NULL, 0, 0)); 338 } 339 340 int 341 grfoff(dev) 342 dev_t dev; 343 { 344 struct grf_softc *gp; 345 int error; 346 347 gp = grfsp[GRFUNIT(dev)]; 348 349 if ((gp->g_flags & GF_GRFON) == 0) 350 return(0); 351 352 gp->g_flags &= ~GF_GRFON; 353 error = gp->g_mode(gp, (dev & GRFOVDEV) ? GM_GRFOVOFF : GM_GRFOFF, 354 NULL, 0, 0); 355 356 /* 357 * Closely tied together no X's 358 */ 359 if (gp->g_itedev != NODEV) 360 ite_on(gp->g_itedev, 2); 361 362 return(error); 363 } 364 365 int 366 grfsinfo(dev, dyninfo) 367 dev_t dev; 368 struct grfdyninfo *dyninfo; 369 { 370 struct grf_softc *gp; 371 int error; 372 373 gp = grfsp[GRFUNIT(dev)]; 374 error = gp->g_mode(gp, GM_GRFCONFIG, dyninfo, 0, 0); 375 376 /* 377 * Closely tied together no X's 378 */ 379 if (gp->g_itedev != NODEV) 380 ite_reinit(gp->g_itedev); 381 return(error); 382 } 383 384 /* 385 * Get the grf-info in sync with underlying view. 386 */ 387 void 388 grf_viewsync(gp) 389 struct grf_softc *gp; 390 { 391 struct view_size vs; 392 bmap_t bm; 393 struct grfinfo *gi; 394 395 gi = &gp->g_display; 396 397 viewioctl(gp->g_viewdev, VIOCGBMAP, (caddr_t)&bm, 0, NOPROC); 398 399 gp->g_data = (caddr_t) 0xDeadBeaf; /* not particularly clean.. */ 400 401 gi->gd_fbaddr = bm.hw_address; 402 gi->gd_fbsize = bm.phys_mappable; 403 gi->gd_linbase = bm.lin_base; 404 gi->gd_regaddr = bm.hw_regs; 405 gi->gd_regsize = bm.reg_size; 406 gi->gd_vgaaddr = bm.vga_address; 407 gi->gd_vgasize = bm.vga_mappable; 408 gi->gd_vgabase = bm.vga_base; 409 410 if(viewioctl(gp->g_viewdev, VIOCGSIZE, (caddr_t)&vs, 0, NOPROC)) { 411 /* 412 * fill in some default values... 413 * XXX: Should _never_ happen 414 */ 415 vs.width = 640; 416 vs.height = 400; 417 vs.depth = 1; 418 } 419 gi->gd_colors = 1 << vs.depth; 420 gi->gd_planes = vs.depth; 421 422 gi->gd_fbwidth = vs.width; 423 gi->gd_fbheight = vs.height; 424 gi->gd_dyn.gdi_fbx = 0; 425 gi->gd_dyn.gdi_fby = 0; 426 gi->gd_dyn.gdi_dwidth = vs.width; 427 gi->gd_dyn.gdi_dheight = vs.height; 428 gi->gd_dyn.gdi_dx = 0; 429 gi->gd_dyn.gdi_dy = 0; 430 } 431 432 /* 433 * Change the mode of the display. 434 * Right now all we can do is grfon/grfoff. 435 * Return a UNIX error number or 0 for success. 436 */ 437 /*ARGSUSED*/ 438 int 439 grf_mode(gp, cmd, arg, a2, a3) 440 struct grf_softc *gp; 441 int cmd, a2, a3; 442 void *arg; 443 { 444 switch (cmd) { 445 case GM_GRFON: 446 /* 447 * Get in sync with view, ite might have changed it. 448 */ 449 grf_viewsync(gp); 450 viewioctl(gp->g_viewdev, VIOCDISPLAY, NULL, 0, NOPROC); 451 return(0); 452 case GM_GRFOFF: 453 viewioctl(gp->g_viewdev, VIOCREMOVE, NULL, 0, NOPROC); 454 return(0); 455 case GM_GRFCONFIG: 456 default: 457 break; 458 } 459 return(EPASSTHROUGH); 460 } 461 #endif /* NGRF > 0 */ 462