1 /* $NetBSD: grf_obio.c,v 1.57 2007/10/17 19:55:16 garbled Exp $ */ 2 3 /* 4 * Copyright (C) 1998 Scott Reynolds 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 /* 30 * Copyright (c) 1995 Allen Briggs. All rights reserved. 31 * 32 * Redistribution and use in source and binary forms, with or without 33 * modification, are permitted provided that the following conditions 34 * are met: 35 * 1. Redistributions of source code must retain the above copyright 36 * notice, this list of conditions and the following disclaimer. 37 * 2. Redistributions in binary form must reproduce the above copyright 38 * notice, this list of conditions and the following disclaimer in the 39 * documentation and/or other materials provided with the distribution. 40 * 3. All advertising materials mentioning features or use of this software 41 * must display the following acknowledgement: 42 * This product includes software developed by Allen Briggs. 43 * 4. The name of the author may not be used to endorse or promote products 44 * derived from this software without specific prior written permission. 45 * 46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 49 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 50 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 51 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 52 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 53 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 54 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 55 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 */ 57 /* 58 * Graphics display driver for the Macintosh internal video for machines 59 * that don't map it into a fake nubus card. 60 */ 61 62 #include <sys/cdefs.h> 63 __KERNEL_RCSID(0, "$NetBSD: grf_obio.c,v 1.57 2007/10/17 19:55:16 garbled Exp $"); 64 65 #include <sys/param.h> 66 #include <sys/device.h> 67 #include <sys/ioctl.h> 68 #include <sys/file.h> 69 #include <sys/malloc.h> 70 #include <sys/mman.h> 71 #include <sys/proc.h> 72 #include <sys/systm.h> 73 74 #include <machine/autoconf.h> 75 #include <machine/bus.h> 76 #include <machine/cpu.h> 77 #include <machine/grfioctl.h> 78 #include <machine/viareg.h> 79 #include <machine/video.h> 80 81 #include <mac68k/nubus/nubus.h> 82 #include <mac68k/obio/obiovar.h> 83 #include <mac68k/dev/grfvar.h> 84 85 static int grfiv_mode(struct grf_softc *, int, void *); 86 static int grfiv_match(struct device *, struct cfdata *, void *); 87 static void grfiv_attach(struct device *, struct device *, void *); 88 89 CFATTACH_DECL(intvid, sizeof(struct grfbus_softc), 90 grfiv_match, grfiv_attach, NULL, NULL); 91 92 #define DAFB_BASE 0xf9000000 93 #define DAFB_CONTROL_BASE 0xf9800000 94 #define CIVIC_BASE 0x50100000 95 #define CIVIC_CONTROL_BASE 0x50036000 96 #define VALKYRIE_BASE 0xf9000000 97 #define VALKYRIE_CONTROL_BASE 0x50f2a000 98 99 static int 100 grfiv_match(struct device *parent, struct cfdata *cf, void *aux) 101 { 102 struct obio_attach_args *oa = (struct obio_attach_args *)aux; 103 bus_space_handle_t bsh; 104 int found; 105 u_int base; 106 107 found = 1; 108 109 switch (current_mac_model->class) { 110 case MACH_CLASSQ2: 111 if (current_mac_model->machineid != MACH_MACLC575) { 112 base = VALKYRIE_CONTROL_BASE; 113 114 if (bus_space_map(oa->oa_tag, base, 0x40, 0, &bsh)) 115 return 0; 116 117 /* Disable interrupts */ 118 bus_space_write_1(oa->oa_tag, bsh, 0x18, 0x1); 119 120 bus_space_unmap(oa->oa_tag, bsh, 0x40); 121 break; 122 } 123 /* 124 * Note: the only system in this class that does not have 125 * the Valkyrie chip -- at least, that we know of -- is 126 * the Performa/LC 57x series. This system has a version 127 * of the DAFB controller, instead. 128 * 129 * If this assumption proves false, we'll have to be more 130 * intelligent here. 131 */ 132 /*FALLTHROUGH*/ 133 case MACH_CLASSQ: 134 /* 135 * Assume DAFB for all of these, unless we can't 136 * access the memory. 137 */ 138 base = DAFB_CONTROL_BASE; 139 140 if (bus_space_map(oa->oa_tag, base, 0x20, 0, &bsh)) 141 return 0; 142 143 if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0x1c, 4) == 0) { 144 bus_space_unmap(oa->oa_tag, bsh, 0x20); 145 return 0; 146 } 147 148 bus_space_unmap(oa->oa_tag, bsh, 0x20); 149 150 if (bus_space_map(oa->oa_tag, base + 0x100, 0x20, 0, &bsh)) 151 return 0; 152 153 if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0x04, 4) == 0) { 154 bus_space_unmap(oa->oa_tag, bsh, 0x20); 155 return 0; 156 } 157 158 /* Disable interrupts */ 159 bus_space_write_4(oa->oa_tag, bsh, 0x04, 0); 160 161 /* Clear any interrupts */ 162 bus_space_write_4(oa->oa_tag, bsh, 0x0C, 0); 163 bus_space_write_4(oa->oa_tag, bsh, 0x10, 0); 164 bus_space_write_4(oa->oa_tag, bsh, 0x14, 0); 165 166 bus_space_unmap(oa->oa_tag, bsh, 0x20); 167 break; 168 case MACH_CLASSAV: 169 base = CIVIC_CONTROL_BASE; 170 171 if (bus_space_map(oa->oa_tag, base, 0x1000, 0, &bsh)) 172 return 0; 173 174 /* Disable interrupts */ 175 bus_space_write_1(oa->oa_tag, bsh, 0x120, 0); 176 177 bus_space_unmap(oa->oa_tag, bsh, 0x1000); 178 break; 179 case MACH_CLASSIIci: 180 case MACH_CLASSIIsi: 181 if (mac68k_video.mv_len == 0 || 182 (via2_reg(rMonitor) & RBVMonitorMask) == RBVMonIDNone) 183 found = 0; 184 break; 185 default: 186 if (mac68k_video.mv_len == 0) 187 found = 0; 188 break; 189 } 190 191 return found; 192 } 193 194 static void 195 grfiv_attach(struct device *parent, struct device *self, void *aux) 196 { 197 struct obio_attach_args *oa = (struct obio_attach_args *)aux; 198 struct grfbus_softc *sc; 199 struct grfmode *gm; 200 u_long base, length; 201 u_int32_t vbase1, vbase2; 202 203 sc = (struct grfbus_softc *)self; 204 205 sc->card_id = 0; 206 207 switch (current_mac_model->class) { 208 case MACH_CLASSQ2: 209 if (current_mac_model->machineid != MACH_MACLC575) { 210 sc->sc_basepa = VALKYRIE_BASE; 211 length = 0x00100000; /* 1MB */ 212 213 if (sc->sc_basepa <= mac68k_video.mv_phys && 214 mac68k_video.mv_phys < (sc->sc_basepa + length)) { 215 sc->sc_fbofs = 216 mac68k_video.mv_phys - sc->sc_basepa; 217 } else { 218 sc->sc_basepa = 219 m68k_trunc_page(mac68k_video.mv_phys); 220 sc->sc_fbofs = 221 m68k_page_offset(mac68k_video.mv_phys); 222 length = mac68k_video.mv_len + sc->sc_fbofs; 223 } 224 225 printf(" @ %lx: Valkyrie video subsystem\n", 226 sc->sc_basepa + sc->sc_fbofs); 227 break; 228 } 229 /* See note in grfiv_match() */ 230 /*FALLTHROUGH*/ 231 case MACH_CLASSQ: 232 base = DAFB_CONTROL_BASE; 233 sc->sc_tag = oa->oa_tag; 234 if (bus_space_map(sc->sc_tag, base, 0x20, 0, &sc->sc_regh)) { 235 printf(": failed to map DAFB register space\n"); 236 return; 237 } 238 239 sc->sc_basepa = DAFB_BASE; 240 length = 0x00100000; /* 1MB */ 241 242 /* Compute the current frame buffer offset */ 243 vbase1 = bus_space_read_4(sc->sc_tag, sc->sc_regh, 0x0) & 0xfff; 244 #if 1 245 /* 246 * XXX The following exists because the DAFB v7 in these 247 * systems doesn't return reasonable values to use for fbofs. 248 * Ken'ichi Ishizaka gets credit for this hack. (sar 19990426) 249 * (Does this get us the correct result for _all_ DAFB- 250 * equipped systems and monitor combinations? It seems 251 * possible, if not likely...) 252 */ 253 switch (current_mac_model->machineid) { 254 case MACH_MACLC475: 255 case MACH_MACLC475_33: 256 case MACH_MACLC575: 257 case MACH_MACQ605: 258 case MACH_MACQ605_33: 259 vbase1 &= 0x3f; 260 break; 261 } 262 #endif 263 vbase2 = bus_space_read_4(sc->sc_tag, sc->sc_regh, 0x4) & 0xf; 264 sc->sc_fbofs = (vbase1 << 9) | (vbase2 << 5); 265 266 printf(" @ %lx: DAFB video subsystem, monitor sense %x\n", 267 sc->sc_basepa + sc->sc_fbofs, 268 (bus_space_read_4(sc->sc_tag, sc->sc_regh, 0x1c) & 0x7)); 269 270 bus_space_unmap(sc->sc_tag, sc->sc_regh, 0x20); 271 break; 272 case MACH_CLASSAV: 273 sc->sc_basepa = CIVIC_BASE; 274 length = 0x00200000; /* 2MB */ 275 if (mac68k_video.mv_phys >= sc->sc_basepa && 276 mac68k_video.mv_phys < (sc->sc_basepa + length)) { 277 sc->sc_fbofs = mac68k_video.mv_phys - sc->sc_basepa; 278 } else { 279 sc->sc_basepa = m68k_trunc_page(mac68k_video.mv_phys); 280 sc->sc_fbofs = m68k_page_offset(mac68k_video.mv_phys); 281 length = mac68k_video.mv_len + sc->sc_fbofs; 282 } 283 284 printf(" @ %lx: CIVIC video subsystem\n", 285 sc->sc_basepa + sc->sc_fbofs); 286 break; 287 case MACH_CLASSIIci: 288 case MACH_CLASSIIsi: 289 sc->sc_basepa = m68k_trunc_page(mac68k_video.mv_phys); 290 sc->sc_fbofs = m68k_page_offset(mac68k_video.mv_phys); 291 length = mac68k_video.mv_len + sc->sc_fbofs; 292 293 printf(" @ %lx: RBV video subsystem, ", 294 sc->sc_basepa + sc->sc_fbofs); 295 switch (via2_reg(rMonitor) & RBVMonitorMask) { 296 case RBVMonIDBWP: 297 printf("15\" monochrome portrait"); 298 break; 299 case RBVMonIDRGB12: 300 printf("12\" color"); 301 break; 302 case RBVMonIDRGB15: 303 printf("15\" color"); 304 break; 305 case RBVMonIDStd: 306 printf("Macintosh II"); 307 break; 308 default: 309 printf("unrecognized"); 310 break; 311 } 312 printf(" display\n"); 313 314 break; 315 default: 316 sc->sc_basepa = m68k_trunc_page(mac68k_video.mv_phys); 317 sc->sc_fbofs = m68k_page_offset(mac68k_video.mv_phys); 318 length = mac68k_video.mv_len + sc->sc_fbofs; 319 320 printf(" @ %lx: On-board video\n", 321 sc->sc_basepa + sc->sc_fbofs); 322 break; 323 } 324 325 if (bus_space_map(sc->sc_tag, sc->sc_basepa, length, 0, 326 &sc->sc_handle)) { 327 printf("%s: failed to map video RAM\n", sc->sc_dev.dv_xname); 328 return; 329 } 330 331 if (sc->sc_basepa <= mac68k_video.mv_phys && 332 mac68k_video.mv_phys < (sc->sc_basepa + length)) { 333 /* XXX Hack */ 334 mac68k_video.mv_kvaddr = sc->sc_handle.base + sc->sc_fbofs; 335 } 336 337 gm = &(sc->curr_mode); 338 gm->mode_id = 0; 339 gm->psize = mac68k_video.mv_depth; 340 gm->ptype = 0; 341 gm->width = mac68k_video.mv_width; 342 gm->height = mac68k_video.mv_height; 343 gm->rowbytes = mac68k_video.mv_stride; 344 gm->hres = 80; /* XXX hack */ 345 gm->vres = 80; /* XXX hack */ 346 gm->fbsize = gm->height * gm->rowbytes; 347 gm->fbbase = (void *)sc->sc_handle.base; /* XXX yet another hack */ 348 gm->fboff = sc->sc_fbofs; 349 350 /* Perform common video attachment. */ 351 grf_establish(sc, NULL, grfiv_mode); 352 } 353 354 static int 355 grfiv_mode(struct grf_softc *sc, int cmd, void *arg) 356 { 357 switch (cmd) { 358 case GM_GRFON: 359 case GM_GRFOFF: 360 return 0; 361 case GM_CURRMODE: 362 break; 363 case GM_NEWMODE: 364 break; 365 case GM_LISTMODES: 366 break; 367 } 368 return EINVAL; 369 } 370