grf_nubus.c revision 1.37 1 /* $NetBSD: grf_nubus.c,v 1.37 1997/12/01 06:07:33 scottr Exp $ */
2
3 /*
4 * Copyright (c) 1995 Allen Briggs. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Allen Briggs.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*
32 * Device-specific routines for handling Nubus-based video cards.
33 */
34
35 #include <sys/param.h>
36
37 #include <sys/device.h>
38 #include <sys/ioctl.h>
39 #include <sys/file.h>
40 #include <sys/malloc.h>
41 #include <sys/mman.h>
42 #include <sys/proc.h>
43 #include <sys/systm.h>
44
45 #include <machine/bus.h>
46 #include <machine/cpu.h>
47 #include <machine/grfioctl.h>
48 #include <machine/viareg.h>
49
50 #include <mac68k/dev/nubus.h>
51 #include <mac68k/dev/grfvar.h>
52
53 static void load_image_data __P((caddr_t data, struct image_data *image));
54
55 static void grfmv_intr_generic_1 __P((void *vsc, int slot));
56 static void grfmv_intr_generic_4 __P((void *vsc, int slot));
57
58 static void grfmv_intr_cb264 __P((void *vsc, int slot));
59 static void grfmv_intr_cb364 __P((void *vsc, int slot));
60 static void grfmv_intr_cmax __P((void *vsc, int slot));
61 static void grfmv_intr_cti __P((void *vsc, int slot));
62 static void grfmv_intr_radius __P((void *vsc, int slot));
63 static void grfmv_intr_supermacgfx __P((void *vsc, int slot));
64 static void grfmv_intr_lapis __P((void *vsc, int slot));
65
66 static int grfmv_mode __P((struct grf_softc *gp, int cmd, void *arg));
67 static caddr_t grfmv_phys __P((struct grf_softc *gp));
68 static int grfmv_match __P((struct device *, struct cfdata *, void *));
69 static void grfmv_attach __P((struct device *, struct device *, void *));
70
71 struct cfdriver macvid_cd = {
72 NULL, "macvid", DV_DULL
73 };
74
75 struct cfattach macvid_ca = {
76 sizeof(struct grfbus_softc), grfmv_match, grfmv_attach
77 };
78
79 static void
80 load_image_data(data, image)
81 caddr_t data;
82 struct image_data *image;
83 {
84 bcopy(data , &image->size, 4);
85 bcopy(data + 4, &image->offset, 4);
86 bcopy(data + 8, &image->rowbytes, 2);
87 bcopy(data + 10, &image->top, 2);
88 bcopy(data + 12, &image->left, 2);
89 bcopy(data + 14, &image->bottom, 2);
90 bcopy(data + 16, &image->right, 2);
91 bcopy(data + 18, &image->version, 2);
92 bcopy(data + 20, &image->packType, 2);
93 bcopy(data + 22, &image->packSize, 4);
94 bcopy(data + 26, &image->hRes, 4);
95 bcopy(data + 30, &image->vRes, 4);
96 bcopy(data + 34, &image->pixelType, 2);
97 bcopy(data + 36, &image->pixelSize, 2);
98 bcopy(data + 38, &image->cmpCount, 2);
99 bcopy(data + 40, &image->cmpSize, 2);
100 bcopy(data + 42, &image->planeBytes, 4);
101 }
102
103
104 static int
105 grfmv_match(parent, cf, aux)
106 struct device *parent;
107 struct cfdata *cf;
108 void *aux;
109 {
110 struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
111
112 if (na->category != NUBUS_CATEGORY_DISPLAY)
113 return 0;
114
115 if (na->type != NUBUS_TYPE_VIDEO)
116 return 0;
117
118 if (na->drsw != NUBUS_DRSW_APPLE)
119 return 0;
120
121 /*
122 * If we've gotten this far, then we're dealing with a real-live
123 * Apple QuickDraw-compatible display card resource. Now, how to
124 * determine that this is an active resource??? Dunno. But we'll
125 * proceed like it is.
126 */
127
128 return 1;
129 }
130
131 static void
132 grfmv_attach(parent, self, aux)
133 struct device *parent, *self;
134 void *aux;
135 {
136 struct grfbus_softc *sc = (struct grfbus_softc *)self;
137 struct nubus_attach_args *na = (struct nubus_attach_args *)aux;
138 struct image_data image_store, image;
139 struct grfmode *gm;
140 char cardname[CARD_NAME_LEN];
141 nubus_dirent dirent;
142 nubus_dir dir, mode_dir;
143 int mode;
144
145 sc->sc_tag = na->na_tag;
146 sc->card_id = na->drhw;
147
148 bcopy(na->fmt, &sc->sc_slot, sizeof(nubus_slot));
149
150 if (bus_space_map(sc->sc_tag,
151 NUBUS_SLOT2PA(na->slot), NBMEMSIZE, 0, &sc->sc_handle)) {
152 printf(": grfmv_attach: failed to map slot %d\n", na->slot);
153 return;
154 }
155
156 nubus_get_main_dir(&sc->sc_slot, &dir);
157
158 if (nubus_find_rsrc(sc->sc_tag, sc->sc_handle,
159 &sc->sc_slot, &dir, na->rsrcid, &dirent) <= 0) {
160 bad:
161 bus_space_unmap(sc->sc_tag, sc->sc_handle, NBMEMSIZE);
162 return;
163 }
164
165 nubus_get_dir_from_rsrc(&sc->sc_slot, &dirent, &sc->board_dir);
166
167 if (nubus_find_rsrc(sc->sc_tag, sc->sc_handle,
168 &sc->sc_slot, &sc->board_dir, NUBUS_RSRC_TYPE, &dirent) <= 0)
169 if ((na->rsrcid != 128) ||
170 (nubus_find_rsrc(sc->sc_tag, sc->sc_handle,
171 &sc->sc_slot, &dir, 129, &dirent) <= 0))
172 goto bad;
173
174 mode = NUBUS_RSRC_FIRSTMODE;
175 if (nubus_find_rsrc(sc->sc_tag, sc->sc_handle,
176 &sc->sc_slot, &sc->board_dir, mode, &dirent) <= 0) {
177 printf(": probe failed to get board rsrc.\n");
178 goto bad;
179 }
180
181 nubus_get_dir_from_rsrc(&sc->sc_slot, &dirent, &mode_dir);
182
183 if (nubus_find_rsrc(sc->sc_tag, sc->sc_handle,
184 &sc->sc_slot, &mode_dir, VID_PARAMS, &dirent) <= 0) {
185 printf(": probe failed to get mode dir.\n");
186 goto bad;
187 }
188
189 if (nubus_get_ind_data(sc->sc_tag, sc->sc_handle, &sc->sc_slot,
190 &dirent, (caddr_t)&image_store, sizeof(struct image_data)) <= 0) {
191 printf(": probe failed to get indirect mode data.\n");
192 goto bad;
193 }
194
195 /* Need to load display info (and driver?), etc... (?) */
196
197 load_image_data((caddr_t)&image_store, &image);
198
199 gm = &sc->curr_mode;
200 gm->mode_id = mode;
201 gm->fbbase = (caddr_t)sc->sc_handle; /* XXX evil! */
202 gm->fboff = image.offset;
203 gm->rowbytes = image.rowbytes;
204 gm->width = image.right - image.left;
205 gm->height = image.bottom - image.top;
206 gm->fbsize = gm->height * gm->rowbytes;
207 gm->hres = image.hRes;
208 gm->vres = image.vRes;
209 gm->ptype = image.pixelType;
210 gm->psize = image.pixelSize;
211
212 strncpy(cardname, nubus_get_card_name(sc->sc_tag, sc->sc_handle,
213 &sc->sc_slot), CARD_NAME_LEN);
214 cardname[CARD_NAME_LEN-1] = '\0';
215 printf(": %s\n", cardname);
216
217 if (sc->card_id == NUBUS_DRHW_TFB) {
218 /*
219 * This is the Toby card, but apparently some manufacturers
220 * (like Cornerstone) didn't bother to get/use their own
221 * value here, even though the cards are different, so we
222 * so we try to differentiate here.
223 */
224 if (strncmp(cardname, "Samsung 768", 11) == 0)
225 sc->card_id = NUBUS_DRHW_SAM768;
226 else if (strncmp(cardname, "Toby frame", 10) != 0)
227 printf("%s: This display card pretends to be a TFB!\n",
228 sc->sc_dev.dv_xname);
229 }
230
231 switch (sc->card_id) {
232 case NUBUS_DRHW_TFB:
233 case NUBUS_DRHW_M2HRVC:
234 case NUBUS_DRHW_PVC:
235 sc->cli_offset = 0xa0000;
236 sc->cli_value = 0;
237 add_nubus_intr(na->slot, grfmv_intr_generic_1, sc);
238 break;
239 case NUBUS_DRHW_WVC:
240 sc->cli_offset = 0xa00000;
241 sc->cli_value = 0;
242 add_nubus_intr(na->slot, grfmv_intr_generic_1, sc);
243 break;
244 case NUBUS_DRHW_COLORMAX:
245 add_nubus_intr(na->slot, grfmv_intr_cmax, sc);
246 break;
247 case NUBUS_DRHW_SE30:
248 /* Do nothing--SE/30 interrupts are disabled */
249 break;
250 case NUBUS_DRHW_MDC:
251 sc->cli_offset = 0x200148;
252 sc->cli_value = 1;
253 add_nubus_intr(na->slot, grfmv_intr_generic_4, sc);
254
255 /* Enable interrupts; to disable, write 0x7 to this location */
256 bus_space_write_4(sc->sc_tag, sc->sc_handle, 0x20013C, 5);
257 break;
258 case NUBUS_DRHW_CB264:
259 add_nubus_intr(na->slot, grfmv_intr_cb264, sc);
260 break;
261 case NUBUS_DRHW_CB364:
262 add_nubus_intr(na->slot, grfmv_intr_cb364, sc);
263 break;
264 case NUBUS_DRHW_RPC8:
265 sc->cli_offset = 0xfdff8f;
266 sc->cli_value = 0xff;
267 add_nubus_intr(na->slot, grfmv_intr_generic_1, sc);
268 break;
269 case NUBUS_DRHW_RPC8XJ:
270 add_nubus_intr(na->slot, grfmv_intr_radius, sc);
271 break;
272 case NUBUS_DRHW_FIILX:
273 case NUBUS_DRHW_FIISXDSP:
274 case NUBUS_DRHW_FUTURASX:
275 sc->cli_offset = 0xf05000;
276 sc->cli_value = 0x80;
277 add_nubus_intr(na->slot, grfmv_intr_generic_1, sc);
278 break;
279 case NUBUS_DRHW_SAM768:
280 add_nubus_intr(na->slot, grfmv_intr_cti, sc);
281 break;
282 case NUBUS_DRHW_SUPRGFX:
283 add_nubus_intr(na->slot, grfmv_intr_supermacgfx, sc);
284 break;
285 case NUBUS_DRHW_LAPIS:
286 add_nubus_intr(na->slot, grfmv_intr_lapis, sc);
287 break;
288 case NUBUS_DRHW_MICRON:
289 /* What do we know about this one? */
290 default:
291 printf("%s: Unknown video card ID 0x%x --",
292 sc->sc_dev.dv_xname, sc->card_id);
293 printf(" Not installing interrupt routine.\n");
294 break;
295 }
296
297 /* Perform common video attachment. */
298 grf_establish(sc, &sc->sc_slot, grfmv_mode, grfmv_phys);
299 }
300
301 static int
302 grfmv_mode(gp, cmd, arg)
303 struct grf_softc *gp;
304 int cmd;
305 void *arg;
306 {
307 switch (cmd) {
308 case GM_GRFON:
309 case GM_GRFOFF:
310 return 0;
311 case GM_CURRMODE:
312 break;
313 case GM_NEWMODE:
314 break;
315 case GM_LISTMODES:
316 break;
317 }
318 return EINVAL;
319 }
320
321 static caddr_t
322 grfmv_phys(gp)
323 struct grf_softc *gp;
324 {
325 return (caddr_t)NUBUS_SLOT2PA(gp->sc_slot->slot);
326 }
327
328 /* Interrupt handlers... */
329 /*
330 * Generic routine to clear interrupts for cards where it simply takes
331 * a MOV.B to clear the interrupt. The offset and value of this byte
332 * varies between cards.
333 */
334 /*ARGSUSED*/
335 static void
336 grfmv_intr_generic_1(vsc, slot)
337 void *vsc;
338 int slot;
339 {
340 struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
341
342 bus_space_write_1(sc->sc_tag, sc->sc_handle,
343 sc->cli_offset, (u_int8_t)sc->cli_value);
344 }
345
346 /*
347 * Generic routine to clear interrupts for cards where it simply takes
348 * a MOV.L to clear the interrupt. The offset and value of this byte
349 * varies between cards.
350 */
351 /*ARGSUSED*/
352 static void
353 grfmv_intr_generic_4(vsc, slot)
354 void *vsc;
355 int slot;
356 {
357 struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
358
359 bus_space_write_4(sc->sc_tag, sc->sc_handle,
360 sc->cli_offset, sc->cli_value);
361 }
362
363 /*
364 * Routine to clear interrupts for the Radius PrecisionColor 8xj card.
365 */
366 /*ARGSUSED*/
367 static void
368 grfmv_intr_radius(vsc, slot)
369 void *vsc;
370 int slot;
371 {
372 struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
373 u_int8_t c;
374
375 /*
376 * The value 0x66 was the observed value on one card. It is read
377 * from the driver's information block, so this may not be sufficient.
378 * Then again, we're not setting up any other interrupts...
379 */
380 c = 0x66;
381
382 c |= 0x80;
383 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0xd00403, c);
384 c &= 0x7f;
385 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0xd00403, c);
386 }
387
388 /*
389 * Routine to clear interrupts on Samsung 768x1006 video controller.
390 * This controller was manufactured by Cornerstone Technology, Inc.,
391 * now known as Cornerstone Imaging.
392 *
393 * To clear this interrupt, we apparently have to set, then clear,
394 * bit 2 at byte offset 0x80000 from the card's base.
395 * Information for this provided by Brad Salai <bsalai (at) servtech.com>
396 */
397 /*ARGSUSED*/
398 static void
399 grfmv_intr_cti(vsc, slot)
400 void *vsc;
401 int slot;
402 {
403 struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
404 u_int8_t c;
405
406 c = bus_space_read_1(sc->sc_tag, sc->sc_handle, 0x80000);
407 c |= 0x02;
408 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x80000, c);
409 c &= 0xfd;
410 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x80000, c);
411 }
412
413 /*ARGSUSED*/
414 static void
415 grfmv_intr_cb264(vsc, slot)
416 void *vsc;
417 int slot;
418 {
419 struct grfbus_softc *sc;
420 volatile char *slotbase;
421
422 sc = (struct grfbus_softc *)vsc;
423 slotbase = (volatile char *)sc->sc_handle; /* XXX evil hack */
424 asm volatile(" movl %0,a0
425 movl a0@(0xff6028),d0
426 andl #0x2,d0
427 beq _mv_intr0
428 movql #0x3,d0
429 _mv_intr0:
430 movl a0@(0xff600c),d1
431 andl #0x3,d1
432 cmpl d1,d0
433 beq _mv_intr_fin
434 movl d0,a0@(0xff600c)
435 nop
436 tstb d0
437 beq _mv_intr1
438 movl #0x0002,a0@(0xff6040)
439 movl #0x0102,a0@(0xff6044)
440 movl #0x0105,a0@(0xff6048)
441 movl #0x000e,a0@(0xff604c)
442 movl #0x001c,a0@(0xff6050)
443 movl #0x00bc,a0@(0xff6054)
444 movl #0x00c3,a0@(0xff6058)
445 movl #0x0061,a0@(0xff605c)
446 movl #0x0012,a0@(0xff6060)
447 bra _mv_intr_fin
448 _mv_intr1:
449 movl #0x0002,a0@(0xff6040)
450 movl #0x0209,a0@(0xff6044)
451 movl #0x020c,a0@(0xff6048)
452 movl #0x000f,a0@(0xff604c)
453 movl #0x0027,a0@(0xff6050)
454 movl #0x00c7,a0@(0xff6054)
455 movl #0x00d7,a0@(0xff6058)
456 movl #0x006b,a0@(0xff605c)
457 movl #0x0029,a0@(0xff6060)
458 _mv_intr_fin:
459 movl #0x1,a0@(0xff6014)"
460 : : "g" (slotbase) : "a0","d0","d1");
461 }
462
463 /*
464 * Support for the Colorboard 364 might be more complex than it needs to
465 * be. If we can find more information about this card, this might be
466 * significantly simplified. Contributions welcome... :-)
467 */
468 /*ARGSUSED*/
469 static void
470 grfmv_intr_cb364(vsc, slot)
471 void *vsc;
472 int slot;
473 {
474 struct grfbus_softc *sc;
475 volatile char *slotbase;
476
477 sc = (struct grfbus_softc *)vsc;
478 slotbase = (volatile char *)sc->sc_handle; /* XXX evil hack */
479 asm volatile(" movl %0,a0
480 movl a0@(0xfe6028),d0
481 andl #0x2,d0
482 beq _cb364_intr4
483 movql #0x3,d0
484 movl a0@(0xfe6018),d1
485 movl #0x3,a0@(0xfe6018)
486 movw a0@(0xfe7010),d2
487 movl d1,a0@(0xfe6018)
488 movl a0@(0xfe6020),d1
489 btst #0x06,d2
490 beq _cb364_intr0
491 btst #0x00,d1
492 beq _cb364_intr5
493 bsr _cb364_intr1
494 bra _cb364_intr_out
495 _cb364_intr0:
496 btst #0x00,d1
497 bne _cb364_intr5
498 bsr _cb364_intr1
499 bra _cb364_intr_out
500 _cb364_intr1:
501 movl d0,a0@(0xfe600c)
502 nop
503 tstb d0
504 beq _cb364_intr3
505 movl #0x0002,a0@(0xfe6040)
506 movl #0x0105,a0@(0xfe6048)
507 movl #0x000e,a0@(0xfe604c)
508 movl #0x00c3,a0@(0xfe6058)
509 movl #0x0061,a0@(0xfe605c)
510 btst #0x06,d2
511 beq _cb364_intr2
512 movl #0x001c,a0@(0xfe6050)
513 movl #0x00bc,a0@(0xfe6054)
514 movl #0x0012,a0@(0xfe6060)
515 movl #0x000e,a0@(0xfe6044)
516 movl #0x00c3,a0@(0xfe6064)
517 movl #0x0061,a0@(0xfe6020)
518 rts
519 _cb364_intr2:
520 movl #0x0016,a0@(0xfe6050)
521 movl #0x00b6,a0@(0xfe6054)
522 movl #0x0011,a0@(0xfe6060)
523 movl #0x0101,a0@(0xfe6044)
524 movl #0x00bf,a0@(0xfe6064)
525 movl #0x0001,a0@(0xfe6020)
526 rts
527 _cb364_intr3:
528 movl #0x0002,a0@(0xfe6040)
529 movl #0x0209,a0@(0xfe6044)
530 movl #0x020c,a0@(0xfe6048)
531 movl #0x000f,a0@(0xfe604c)
532 movl #0x0027,a0@(0xfe6050)
533 movl #0x00c7,a0@(0xfe6054)
534 movl #0x00d7,a0@(0xfe6058)
535 movl #0x006b,a0@(0xfe605c)
536 movl #0x0029,a0@(0xfe6060)
537 oril #0x0040,a0@(0xfe6064)
538 movl #0x0000,a0@(0xfe6020)
539 rts
540 _cb364_intr4:
541 movq #0x00,d0
542 _cb364_intr5:
543 movl a0@(0xfe600c),d1
544 andl #0x3,d1
545 cmpl d1,d0
546 beq _cb364_intr_out
547 bsr _cb364_intr1
548 _cb364_intr_out:
549 movl #0x1,a0@(0xfe6014)
550 _cb364_intr_quit:
551 " : : "g" (slotbase) : "a0","d0","d1","d2");
552 }
553
554 /*
555 * Interrupt clearing routine for SuperMac GFX card.
556 */
557 /*ARGSUSED*/
558 static void
559 grfmv_intr_supermacgfx(vsc, slot)
560 void *vsc;
561 int slot;
562 {
563 struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
564 u_int8_t dummy;
565
566 dummy = bus_space_read_1(sc->sc_tag, sc->sc_handle, 0xE70D3);
567 }
568
569 /*
570 * Routine to clear interrupts for the Sigma Designs ColorMax card.
571 */
572 /*ARGSUSED*/
573 static void
574 grfmv_intr_cmax(vsc, slot)
575 void *vsc;
576 int slot;
577 {
578 struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
579 u_int32_t dummy;
580
581 dummy = bus_space_read_4(sc->sc_tag, sc->sc_handle, 0xf501c);
582 dummy = bus_space_read_4(sc->sc_tag, sc->sc_handle, 0xf5018);
583 }
584
585 /*
586 * Routine to clear interrupts for the Lapis ProColorServer 8 PDS card
587 * (for the SE/30).
588 */
589 /*ARGSUSED*/
590 static void
591 grfmv_intr_lapis(vsc, slot)
592 void *vsc;
593 int slot;
594 {
595 struct grfbus_softc *sc = (struct grfbus_softc *)vsc;
596
597 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0xff7000, 0x08);
598 bus_space_write_1(sc->sc_tag, sc->sc_handle, 0xff7000, 0x0C);
599 }
600