zx.c revision 1.2 1 /* $NetBSD: zx.c,v 1.2 2002/09/18 18:42:06 ad Exp $ */
2
3 /*
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Driver for the Sun ZX display adapter. This would be called 'leo', but
41 * NetBSD/amiga already has a driver by that name. The XFree86 and Linux
42 * drivers were used as "living documentation" when writing this; thanks
43 * to the authors.
44 *
45 * Issues (which can be solved with wscons, happily enough):
46 *
47 * o There is lots of unnecessary mucking about rasops in here, primarily
48 * to appease the sparc fb code.
49 *
50 * o RASTERCONSOLE is required. X needs the board set up correctly, and
51 * that's difficult to reconcile with using the PROM for output.
52 */
53
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: zx.c,v 1.2 2002/09/18 18:42:06 ad Exp $");
56
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/device.h>
60 #include <sys/ioctl.h>
61 #include <sys/malloc.h>
62 #include <sys/mman.h>
63 #include <sys/tty.h>
64 #include <sys/conf.h>
65 #include <sys/syslog.h>
66 #include <sys/buf.h>
67
68 #include <machine/bus.h>
69 #include <machine/autoconf.h>
70
71 #include <uvm/uvm_extern.h>
72
73 #include <dev/sun/fbio.h>
74 #include <dev/sun/fbvar.h>
75
76 #include <dev/sbus/zxreg.h>
77 #include <dev/sbus/zxvar.h>
78 #include <dev/sbus/sbusvar.h>
79
80 #include <dev/wscons/wsconsio.h>
81
82 #ifndef RASTERCONSOLE
83 #error Sorry, this driver needs the RASTERCONSOLE option
84 #endif
85
86 /* Force 32-bit writes. */
87 #define SETREG(r, v) (*((volatile u_int32_t *)&r) = (v))
88
89 #define ZX_STD_ROP (ZX_ROP_NEW | ZX_ATTR_WE_ENABLE | \
90 ZX_ATTR_OE_ENABLE | ZX_ATTR_FORCE_WID)
91
92 void zx_attach(struct device *, struct device *, void *);
93 int zx_match(struct device *, struct cfdata *, void *);
94
95 void zx_blank(struct device *);
96 int zx_cmap_put(struct zx_softc *);
97 void zx_copyrect(struct rasops_info *, int, int, int, int, int, int);
98 int zx_cross_loadwid(struct zx_softc *, u_int, u_int, u_int);
99 int zx_cross_wait(struct zx_softc *);
100 void zx_fillrect(struct rasops_info *, int, int, int, int, long, int);
101 int zx_intr(void *);
102 void zx_reset(struct zx_softc *);
103 void zx_unblank(struct device *);
104
105 void zx_cursor_blank(struct zx_softc *);
106 void zx_cursor_color(struct zx_softc *);
107 void zx_cursor_move(struct zx_softc *);
108 void zx_cursor_set(struct zx_softc *);
109 void zx_cursor_unblank(struct zx_softc *);
110
111 void zx_copycols(void *, int, int, int, int);
112 void zx_copyrows(void *, int, int, int);
113 void zx_cursor(void *, int, int, int);
114 void zx_do_cursor(struct rasops_info *);
115 void zx_erasecols(void *, int, int, int, long);
116 void zx_eraserows(void *, int, int, long);
117 void zx_putchar(void *, int, int, u_int, long);
118
119 struct zx_mmo {
120 off_t mo_va;
121 off_t mo_pa;
122 off_t mo_size;
123 } static const zx_mmo[] = {
124 { ZX_FB0_VOFF, ZX_OFF_SS0, 0x00800000 },
125 { ZX_LC0_VOFF, ZX_OFF_LC_SS0_USR, 0x00001000 },
126 { ZX_LD0_VOFF, ZX_OFF_LD_SS0, 0x00001000 },
127 { ZX_LX0_CURSOR_VOFF, ZX_OFF_LX_CURSOR, 0x00001000 },
128 { ZX_FB1_VOFF, ZX_OFF_SS1, 0x00800000 },
129 { ZX_LC1_VOFF, ZX_OFF_LC_SS1_USR, 0x00001000 },
130 { ZX_LD1_VOFF, ZX_OFF_LD_SS1, 0x00001000 },
131 { ZX_LX_KRN_VOFF, ZX_OFF_LX_CROSS, 0x00001000 },
132 { ZX_LC0_KRN_VOFF, ZX_OFF_LC_SS0_KRN, 0x00001000 },
133 { ZX_LC1_KRN_VOFF, ZX_OFF_LC_SS1_KRN, 0x00001000 },
134 { ZX_LD_GBL_VOFF, ZX_OFF_LD_GBL, 0x00001000 },
135 };
136
137 struct cfattach zx_ca = {
138 sizeof(struct zx_softc), zx_match, zx_attach
139 };
140
141 extern struct cfdriver zx_cd;
142
143 dev_type_open(zxopen);
144 dev_type_close(zxclose);
145 dev_type_ioctl(zxioctl);
146 dev_type_mmap(zxmmap);
147
148 static struct fbdriver zx_fbdriver = {
149 zx_unblank, zxopen, zxclose, zxioctl, nopoll, zxmmap
150 };
151
152 int
153 zx_match(struct device *parent, struct cfdata *cf, void *aux)
154 {
155 struct sbus_attach_args *sa;
156
157 sa = (struct sbus_attach_args *)aux;
158
159 return (strcmp(sa->sa_name, "SUNW,leo") == 0);
160 }
161
162 void
163 zx_attach(struct device *parent, struct device *self, void *args)
164 {
165 struct zx_softc *sc;
166 struct sbus_attach_args *sa;
167 bus_space_handle_t bh;
168 bus_space_tag_t bt;
169 struct fbdevice *fb;
170 struct rasops_info *ri;
171 volatile struct zx_command *zc;
172 int isconsole;
173
174 sc = (struct zx_softc *)self;
175 sa = args;
176 fb = &sc->sc_fb;
177 ri = &fb->fb_rinfo;
178 bt = sa->sa_bustag;
179 sc->sc_bt = bt;
180
181 sc->sc_paddr = sbus_bus_addr(bt, sa->sa_slot, sa->sa_offset);
182
183 if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_SS0,
184 0x800000, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
185 printf("%s: can't map bits\n", self->dv_xname);
186 return;
187 }
188 fb->fb_pixels = (caddr_t)bus_space_vaddr(bt, bh);
189 sc->sc_pixels = (u_int32_t *)fb->fb_pixels;
190
191 if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LC_SS0_USR,
192 PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
193 printf("%s: can't map zc\n", self->dv_xname);
194 return;
195 }
196 sc->sc_zc = (struct zx_command *)bus_space_vaddr(bt, bh);
197
198 if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LD_SS0,
199 PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
200 printf("%s: can't map ld/ss0\n", self->dv_xname);
201 return;
202 }
203 sc->sc_zd_ss0 = (struct zx_draw *)bus_space_vaddr(bt, bh);
204
205 if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LD_SS1,
206 PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
207 printf("%s: can't map ld/ss1\n", self->dv_xname);
208 return;
209 }
210 sc->sc_zd_ss1 =
211 (struct zx_draw_ss1 *)bus_space_vaddr(bt, bh);
212
213 if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LX_CROSS,
214 PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
215 printf("%s: can't map zx\n", self->dv_xname);
216 return;
217 }
218 sc->sc_zx = (struct zx_cross *)bus_space_vaddr(bt, bh);
219
220 if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + ZX_OFF_LX_CURSOR,
221 PAGE_SIZE, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
222 printf("%s: can't map zcu\n", self->dv_xname);
223 return;
224 }
225 sc->sc_zcu = (struct zx_cursor *)bus_space_vaddr(bt, bh);
226
227 fb->fb_driver = &zx_fbdriver;
228 fb->fb_device = &sc->sc_dv;
229 fb->fb_flags = sc->sc_dv.dv_cfdata->cf_flags & FB_USERMASK;
230 fb->fb_pfour = NULL;
231 fb->fb_linebytes = 8192;
232
233 fb_setsize_obp(fb, 32, 1280, 1024, sa->sa_node);
234
235 fb->fb_type.fb_cmsize = 256;
236 fb->fb_type.fb_depth = 32;
237 fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
238 fb->fb_type.fb_type = FBTYPE_SUNLEO;
239
240 printf(": %d x %d", fb->fb_type.fb_width, fb->fb_type.fb_height);
241 isconsole = fb_is_console(sa->sa_node);
242 if (isconsole)
243 printf(" (console)");
244 printf("\n");
245
246 sbus_establish(&sc->sc_sd, &sc->sc_dv);
247 if (sa->sa_nintr != 0)
248 bus_intr_establish(bt, sa->sa_pri, IPL_NONE, 0, zx_intr, sc);
249
250 sc->sc_cmap = malloc(768, M_DEVBUF, M_NOWAIT);
251 fb_attach(&sc->sc_fb, isconsole);
252 zx_reset(sc);
253
254 /*
255 * Attach to rcons. XXX At this point, rasops_do_cursor() will be
256 * called before we get our hooks in place. So, we mask off access
257 * to the framebuffer until it's done.
258 */
259 zc = sc->sc_zc;
260 SETREG(zc->zc_fontt, 1);
261 SETREG(zc->zc_fontmsk, 0);
262
263 fbrcons_init(&sc->sc_fb);
264
265 SETREG(zc->zc_fontt, 0);
266 SETREG(zc->zc_fontmsk, 0xffffffff);
267
268 ri->ri_hw = sc;
269 ri->ri_do_cursor = zx_do_cursor;
270 ri->ri_ops.copycols = zx_copycols;
271 ri->ri_ops.copyrows = zx_copyrows;
272 ri->ri_ops.erasecols = zx_erasecols;
273 ri->ri_ops.eraserows = zx_eraserows;
274 ri->ri_ops.putchar = zx_putchar;
275
276 sc->sc_fontw = ri->ri_font->fontwidth;
277 sc->sc_fonth = ri->ri_font->fontheight;
278 }
279
280 int
281 zxopen(dev_t dev, int flags, int mode, struct proc *p)
282 {
283
284 if (device_lookup(&zx_cd, minor(dev)) == NULL)
285 return (ENXIO);
286 return (0);
287 }
288
289 int
290 zxclose(dev_t dev, int flags, int mode, struct proc *p)
291 {
292 struct zx_softc *sc;
293
294 sc = (struct zx_softc *)device_lookup(&zx_cd, minor(dev));
295
296 zx_reset(sc);
297 zx_cursor_blank(sc);
298 return (0);
299 }
300
301 int
302 zxioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
303 {
304 struct zx_softc *sc;
305 struct fbcmap *cm;
306 struct fbcursor *cu;
307 int rv, v, count, i;
308
309 sc = zx_cd.cd_devs[minor(dev)];
310
311 switch (cmd) {
312 case FBIOGTYPE:
313 *(struct fbtype *)data = sc->sc_fb.fb_type;
314 break;
315
316 case FBIOGATTR:
317 #define fba ((struct fbgattr *)data)
318 fba->real_type = sc->sc_fb.fb_type.fb_type;
319 fba->owner = 0; /* XXX ??? */
320 fba->fbtype = sc->sc_fb.fb_type;
321 fba->sattr.flags = 0;
322 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
323 fba->sattr.dev_specific[0] = -1;
324 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
325 fba->emu_types[1] = -1;
326 fba->emu_types[2] = -1;
327 #undef fba
328 break;
329
330 case FBIOGVIDEO:
331 *(int *)data = ((sc->sc_flags & ZX_BLANKED) != 0);
332 break;
333
334 case FBIOSVIDEO:
335 if (*(int *)data)
336 zx_unblank(&sc->sc_dv);
337 else
338 zx_blank(&sc->sc_dv);
339 break;
340
341 case FBIOGETCMAP:
342 cm = (struct fbcmap *)data;
343 if (cm->index > 256 || cm->count > 256 - cm->index)
344 return (EINVAL);
345 rv = copyout(sc->sc_cmap + cm->index, cm->red, cm->count);
346 if (rv == 0)
347 rv = copyout(sc->sc_cmap + 256 + cm->index, cm->green,
348 cm->count);
349 if (rv == 0)
350 rv = copyout(sc->sc_cmap + 512 + cm->index, cm->blue,
351 cm->count);
352 return (rv);
353
354 case FBIOPUTCMAP:
355 cm = (struct fbcmap *)data;
356 if (cm->index > 256 || cm->count > 256 - cm->index)
357 return (EINVAL);
358 rv = copyin(cm->red, sc->sc_cmap + cm->index, cm->count);
359 if (rv == 0)
360 rv = copyin(cm->green, sc->sc_cmap + 256 + cm->index,
361 cm->count);
362 if (rv == 0)
363 rv = copyin(cm->blue, sc->sc_cmap + 512 + cm->index,
364 cm->count);
365 zx_cmap_put(sc);
366 return (rv);
367
368 case FBIOGCURPOS:
369 *(struct fbcurpos *)data = sc->sc_curpos;
370 break;
371
372 case FBIOSCURPOS:
373 sc->sc_curpos = *(struct fbcurpos *)data;
374 zx_cursor_move(sc);
375 break;
376
377 case FBIOGCURMAX:
378 ((struct fbcurpos *)data)->x = 32;
379 ((struct fbcurpos *)data)->y = 32;
380 break;
381
382 case FBIOSCURSOR:
383 cu = (struct fbcursor *)data;
384 v = cu->set;
385
386 if ((v & FB_CUR_SETSHAPE) != 0) {
387 if ((u_int)cu->size.x > 32 || (u_int)cu->size.y > 32)
388 return (EINVAL);
389 count = cu->size.y * 4;
390 if (!uvm_useracc(cu->image, count, B_READ) ||
391 !uvm_useracc(cu->mask, count, B_READ))
392 return (EFAULT);
393 }
394 if ((v & FB_CUR_SETCUR) != 0) {
395 if (cu->enable)
396 zx_cursor_unblank(sc);
397 else
398 zx_cursor_blank(sc);
399 }
400 if ((v & (FB_CUR_SETPOS | FB_CUR_SETHOT)) != 0) {
401 if ((v & FB_CUR_SETPOS) != 0)
402 sc->sc_curpos = cu->pos;
403 if ((v & FB_CUR_SETHOT) != 0)
404 sc->sc_curhot = cu->hot;
405 zx_cursor_move(sc);
406 }
407 if ((v & FB_CUR_SETCMAP) != 0) {
408 if (cu->cmap.index > 2 ||
409 cu->cmap.count > 2 - cu->cmap.index)
410 return (EINVAL);
411 for (i = 0; i < cu->cmap.count; i++) {
412 if ((v = fubyte(&cu->cmap.red[i])) < 0)
413 return (EFAULT);
414 sc->sc_curcmap[i + cu->cmap.index + 0] = v;
415 if ((v = fubyte(&cu->cmap.green[i])) < 0)
416 return (EFAULT);
417 sc->sc_curcmap[i + cu->cmap.index + 2] = v;
418 if ((v = fubyte(&cu->cmap.blue[i])) < 0)
419 return (EFAULT);
420 sc->sc_curcmap[i + cu->cmap.index + 4] = v;
421 }
422 zx_cursor_color(sc);
423 }
424 if ((v & FB_CUR_SETSHAPE) != 0) {
425 sc->sc_cursize = cu->size;
426 count = cu->size.y * 4;
427 memset(sc->sc_curbits, 0, sizeof(sc->sc_curbits));
428 copyin(cu->mask, (caddr_t)sc->sc_curbits[0], count);
429 copyin(cu->image, (caddr_t)sc->sc_curbits[1], count);
430 zx_cursor_set(sc);
431 }
432 break;
433
434 case FBIOGCURSOR:
435 cu = (struct fbcursor *)data;
436
437 cu->set = FB_CUR_SETALL;
438 cu->enable = ((sc->sc_flags & ZX_CURSOR) != 0);
439 cu->pos = sc->sc_curpos;
440 cu->hot = sc->sc_curhot;
441 cu->size = sc->sc_cursize;
442
443 if (cu->image != NULL) {
444 count = sc->sc_cursize.y * 4;
445 rv = copyout((caddr_t)sc->sc_curbits[1],
446 (caddr_t)cu->image, count);
447 if (rv)
448 return (rv);
449 rv = copyout((caddr_t)sc->sc_curbits[0],
450 (caddr_t)cu->mask, count);
451 if (rv)
452 return (rv);
453 }
454 if (cu->cmap.red != NULL) {
455 if (cu->cmap.index > 2 ||
456 cu->cmap.count > 2 - cu->cmap.index)
457 return (EINVAL);
458 for (i = 0; i < cu->cmap.count; i++) {
459 v = sc->sc_curcmap[i + cu->cmap.index + 0];
460 if (subyte(&cu->cmap.red[i], v))
461 return (EFAULT);
462 v = sc->sc_curcmap[i + cu->cmap.index + 2];
463 if (subyte(&cu->cmap.green[i], v))
464 return (EFAULT);
465 v = sc->sc_curcmap[i + cu->cmap.index + 4];
466 if (subyte(&cu->cmap.blue[i], v))
467 return (EFAULT);
468 }
469 } else {
470 cu->cmap.index = 0;
471 cu->cmap.count = 2;
472 }
473 break;
474
475 default:
476 #ifdef DEBUG
477 log(LOG_NOTICE, "zxioctl(0x%lx) (%s[%d])\n", cmd,
478 p->p_comm, p->p_pid);
479 #endif
480 return (ENOTTY);
481 }
482
483 return (0);
484 }
485
486 int
487 zx_intr(void *cookie)
488 {
489
490 return (1);
491 }
492
493 void
494 zx_reset(struct zx_softc *sc)
495 {
496 volatile struct zx_draw *zd;
497 volatile struct zx_command *zc;
498 struct fbtype *fbt;
499 u_int i;
500
501 zd = sc->sc_zd_ss0;
502 zc = sc->sc_zc;
503 fbt = &sc->sc_fb.fb_type;
504
505 zx_cross_loadwid(sc, ZX_WID_DBL_8, 0, 0x2c0);
506 zx_cross_loadwid(sc, ZX_WID_DBL_8, 1, 0x30);
507 zx_cross_loadwid(sc, ZX_WID_DBL_8, 2, 0x20);
508 zx_cross_loadwid(sc, ZX_WID_DBL_24, 1, 0x30);
509
510 i = sc->sc_zd_ss1->zd_misc;
511 i |= ZX_SS1_MISC_ENABLE;
512 SETREG(sc->sc_zd_ss1->zd_misc, i);
513
514 SETREG(zd->zd_wid, 0xffffffff);
515 SETREG(zd->zd_widclip, 0);
516 SETREG(zd->zd_wmask, 0xffff);
517 SETREG(zd->zd_vclipmin, 0);
518 SETREG(zd->zd_vclipmax,
519 (fbt->fb_width - 1) | ((fbt->fb_height - 1) << 16));
520 SETREG(zd->zd_fg, 0);
521 SETREG(zd->zd_planemask, 0xff000000);
522 SETREG(zd->zd_rop, ZX_STD_ROP);
523
524 SETREG(zc->zc_extent,
525 (fbt->fb_width - 1) | ((fbt->fb_height - 1) << 11));
526 SETREG(zc->zc_addrspace, ZX_ADDRSPC_FONT_OBGR);
527 SETREG(zc->zc_fontt, 0);
528
529 for (i = 0; i < 256; i++) {
530 sc->sc_cmap[i] = rasops_cmap[i * 3];
531 sc->sc_cmap[i + 256] = rasops_cmap[i * 3 + 1];
532 sc->sc_cmap[i + 512] = rasops_cmap[i * 3 + 2];
533 }
534
535 zx_cmap_put(sc);
536 }
537
538 int
539 zx_cross_wait(struct zx_softc *sc)
540 {
541 volatile struct zx_cross *zx;
542 int i;
543
544 zx = sc->sc_zx;
545
546 for (i = 300000; i != 0; i--) {
547 if ((zx->zx_csr & ZX_CROSS_CSR_PROGRESS) == 0)
548 break;
549 DELAY(1);
550 }
551
552 if (i == 0)
553 printf("zx_cross_wait: timed out\n");
554
555 return (i);
556 }
557
558 int
559 zx_cross_loadwid(struct zx_softc *sc, u_int type, u_int index, u_int value)
560 {
561 volatile struct zx_cross *zx;
562 u_int tmp;
563
564 zx = sc->sc_zx;
565 SETREG(zx->zx_type, ZX_CROSS_TYPE_WID);
566
567 if (zx_cross_wait(sc))
568 return (1);
569
570 if (type == ZX_WID_DBL_8)
571 tmp = (index & 0x0f) + 0x40;
572 else if (type == ZX_WID_DBL_24)
573 tmp = index & 0x3f;
574
575 SETREG(zx->zx_type, 0x5800 + tmp);
576 SETREG(zx->zx_value, value);
577 SETREG(zx->zx_type, ZX_CROSS_TYPE_WID);
578 SETREG(zx->zx_csr, ZX_CROSS_CSR_UNK | ZX_CROSS_CSR_UNK2);
579
580 return (0);
581 }
582
583 int
584 zx_cmap_put(struct zx_softc *sc)
585 {
586 volatile struct zx_cross *zx;
587 const u_char *b;
588 u_int i, t;
589
590 zx = sc->sc_zx;
591
592 SETREG(zx->zx_type, ZX_CROSS_TYPE_CLUT0);
593 if (zx_cross_wait(sc))
594 return (1);
595
596 SETREG(zx->zx_type, ZX_CROSS_TYPE_CLUTDATA);
597
598 for (i = 0, b = sc->sc_cmap; i < 256; i++) {
599 t = b[i];
600 t |= b[i + 256] << 8;
601 t |= b[i + 512] << 16;
602 SETREG(zx->zx_value, t);
603 }
604
605 SETREG(zx->zx_type, ZX_CROSS_TYPE_CLUT0);
606 i = zx->zx_csr;
607 i = i | ZX_CROSS_CSR_UNK | ZX_CROSS_CSR_UNK2;
608 SETREG(zx->zx_csr, i);
609 return (0);
610 }
611
612 void
613 zx_cursor_move(struct zx_softc *sc)
614 {
615 volatile struct zx_cursor *zcu;
616 int sx, sy, x, y;
617
618 x = sc->sc_curpos.x - sc->sc_curhot.x;
619 y = sc->sc_curpos.y - sc->sc_curhot.y;
620 zcu = sc->sc_zcu;
621
622 if (x < 0) {
623 sx = min(-x, 32);
624 x = 0;
625 } else
626 sx = 0;
627
628 if (y < 0) {
629 sy = min(-y, 32);
630 y = 0;
631 } else
632 sy = 0;
633
634 if (sx != sc->sc_shiftx || sy != sc->sc_shifty) {
635 sc->sc_shiftx = sx;
636 sc->sc_shifty = sy;
637 zx_cursor_set(sc);
638 }
639
640 SETREG(zcu->zcu_sxy, ((y & 0x7ff) << 11) | (x & 0x7ff));
641 SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x30);
642
643 /* XXX Necessary? */
644 SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x80);
645 }
646
647 void
648 zx_cursor_set(struct zx_softc *sc)
649 {
650 volatile struct zx_cursor *zcu;
651 int i, j, data;
652
653 zcu = sc->sc_zcu;
654
655 if ((sc->sc_flags & ZX_CURSOR) != 0)
656 SETREG(zcu->zcu_misc, zcu->zcu_misc & ~0x80);
657
658 for (j = 0; j < 2; j++) {
659 SETREG(zcu->zcu_type, 0x20 << i);
660
661 for (i = sc->sc_shifty; i < 32; i++) {
662 data = sc->sc_curbits[j][i];
663 SETREG(zcu->zcu_data, data >> sc->sc_shiftx);
664 }
665 for (i = sc->sc_shifty; i != 0; i--)
666 SETREG(zcu->zcu_data, 0);
667 }
668
669 if ((sc->sc_flags & ZX_CURSOR) != 0)
670 SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x80);
671 }
672
673 void
674 zx_cursor_blank(struct zx_softc *sc)
675 {
676 volatile struct zx_cursor *zcu;
677
678 sc->sc_flags &= ~ZX_CURSOR;
679 zcu = sc->sc_zcu;
680 SETREG(zcu->zcu_misc, zcu->zcu_misc & ~0x80);
681 }
682
683 void
684 zx_cursor_unblank(struct zx_softc *sc)
685 {
686 volatile struct zx_cursor *zcu;
687
688 sc->sc_flags |= ZX_CURSOR;
689 zcu = sc->sc_zcu;
690 SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x80);
691 }
692
693 void
694 zx_cursor_color(struct zx_softc *sc)
695 {
696 volatile struct zx_cursor *zcu;
697 u_int8_t tmp;
698
699 zcu = sc->sc_zcu;
700
701 SETREG(zcu->zcu_type, 0x50);
702
703 tmp = sc->sc_curcmap[0] | (sc->sc_curcmap[2] << 8) |
704 (sc->sc_curcmap[4] << 16);
705 SETREG(zcu->zcu_data, tmp);
706
707 tmp = sc->sc_curcmap[1] | (sc->sc_curcmap[3] << 8) |
708 (sc->sc_curcmap[5] << 16);
709 SETREG(zcu->zcu_data, sc->sc_curcmap[1]);
710
711 SETREG(zcu->zcu_misc, zcu->zcu_misc | 0x03);
712 }
713
714 void
715 zx_blank(struct device *dv)
716 {
717 struct zx_softc *sc;
718 volatile struct zx_cross *zx;
719
720 sc = (struct zx_softc *)dv;
721
722 if ((sc->sc_flags & ZX_BLANKED) != 0)
723 return;
724 sc->sc_flags |= ZX_BLANKED;
725
726 zx = sc->sc_zx;
727 SETREG(zx->zx_type, ZX_CROSS_TYPE_VIDEO);
728 SETREG(zx->zx_csr, zx->zx_csr & ~ZX_CROSS_CSR_ENABLE);
729 }
730
731 void
732 zx_unblank(struct device *dv)
733 {
734 struct zx_softc *sc;
735 volatile struct zx_cross *zx;
736
737 sc = (struct zx_softc *)dv;
738
739 if ((sc->sc_flags & ZX_BLANKED) == 0)
740 return;
741 sc->sc_flags &= ~ZX_BLANKED;
742
743 zx = sc->sc_zx;
744 SETREG(zx->zx_type, ZX_CROSS_TYPE_VIDEO);
745 SETREG(zx->zx_csr, zx->zx_csr | ZX_CROSS_CSR_ENABLE);
746 }
747
748 paddr_t
749 zxmmap(dev_t dev, off_t off, int prot)
750 {
751 struct zx_softc *sc;
752 const struct zx_mmo *mm, *max;
753
754 sc = device_lookup(&zx_cd, minor(dev));
755 off = trunc_page(off);
756 mm = zx_mmo;
757 max = mm + sizeof(zx_mmo) / sizeof(zx_mmo[0]);
758
759 for (; mm < max; mm++)
760 if (off >= mm->mo_va && off < mm->mo_va + mm->mo_size) {
761 off = off - mm->mo_va + mm->mo_pa;
762 return (bus_space_mmap(sc->sc_bt, sc->sc_paddr,
763 off, prot, BUS_SPACE_MAP_LINEAR));
764 }
765
766 return (-1);
767 }
768
769 void
770 zx_fillrect(struct rasops_info *ri, int x, int y, int w, int h, long attr,
771 int rop)
772 {
773 struct zx_softc *sc;
774 volatile struct zx_command *zc;
775 volatile struct zx_draw *zd;
776 int fg, bg;
777
778 sc = ri->ri_hw;
779 zc = sc->sc_zc;
780 zd = sc->sc_zd_ss0;
781
782 rasops_unpack_attr(attr, &fg, &bg, NULL);
783 x = x * sc->sc_fontw + ri->ri_xorigin;
784 y = y * sc->sc_fonth + ri->ri_yorigin;
785 w = sc->sc_fontw * w - 1;
786 h = sc->sc_fonth * h - 1;
787
788 while ((zc->zc_csr & ZX_CSR_BLT_BUSY) != 0)
789 ;
790
791 SETREG(zd->zd_rop, rop);
792 SETREG(zd->zd_fg, (bg & 7) ? 0x00000000 : 0xff000000);
793 SETREG(zc->zc_extent, w | (h << 11));
794 SETREG(zc->zc_fill, x | (y << 11) | 0x80000000);
795 }
796
797 void
798 zx_copyrect(struct rasops_info *ri, int sx, int sy, int dx, int dy, int w,
799 int h)
800 {
801 struct zx_softc *sc;
802 volatile struct zx_command *zc;
803 volatile struct zx_draw *zd;
804 int dir;
805
806 sc = ri->ri_hw;
807 zc = sc->sc_zc;
808 zd = sc->sc_zd_ss0;
809
810 sx = sx * sc->sc_fontw + ri->ri_xorigin;
811 sy = sy * sc->sc_fonth + ri->ri_yorigin;
812 dx = dx * sc->sc_fontw + ri->ri_xorigin;
813 dy = dy * sc->sc_fonth + ri->ri_yorigin;
814 w = w * sc->sc_fontw - 1;
815 h = h * sc->sc_fonth - 1;
816
817 if (sy < dy || sx < dx) {
818 dir = 0x80000000;
819 sx += w;
820 sy += h;
821 dx += w;
822 dy += h;
823 } else
824 dir = 0;
825
826 while ((zc->zc_csr & ZX_CSR_BLT_BUSY) != 0)
827 ;
828
829 SETREG(zd->zd_rop, ZX_STD_ROP);
830 SETREG(zc->zc_extent, w | (h << 11) | dir);
831 SETREG(zc->zc_src, sx | (sy << 11));
832 SETREG(zc->zc_copy, dx | (dy << 11));
833 }
834
835 void
836 zx_do_cursor(struct rasops_info *ri)
837 {
838
839 zx_fillrect(ri, ri->ri_ccol, ri->ri_crow, 1, 1, 0,
840 ZX_ROP_NEW_XOR_OLD | ZX_ATTR_WE_ENABLE | ZX_ATTR_OE_ENABLE |
841 ZX_ATTR_FORCE_WID);
842 }
843
844 void
845 zx_erasecols(void *cookie, int row, int col, int num, long attr)
846 {
847 struct rasops_info *ri;
848
849 ri = (struct rasops_info *)cookie;
850
851 zx_fillrect(ri, col, row, num, 1, attr, ZX_STD_ROP);
852 }
853
854 void
855 zx_eraserows(void *cookie, int row, int num, long attr)
856 {
857 struct rasops_info *ri;
858
859 ri = (struct rasops_info *)cookie;
860
861 zx_fillrect(ri, 0, row, ri->ri_cols, num, attr, ZX_STD_ROP);
862 }
863
864 void
865 zx_copyrows(void *cookie, int src, int dst, int num)
866 {
867 struct rasops_info *ri;
868
869 ri = (struct rasops_info *)cookie;
870
871 zx_copyrect(ri, 0, src, 0, dst, ri->ri_cols, num);
872 }
873
874 void
875 zx_copycols(void *cookie, int row, int src, int dst, int num)
876 {
877 struct rasops_info *ri;
878
879 ri = (struct rasops_info *)cookie;
880
881 zx_copyrect(ri, src, row, dst, row, num, 1);
882 }
883
884 void
885 zx_putchar(void *cookie, int row, int col, u_int uc, long attr)
886 {
887 struct rasops_info *ri;
888 struct zx_softc *sc;
889 struct wsdisplay_font *font;
890 volatile struct zx_command *zc;
891 volatile struct zx_draw *zd;
892 volatile u_int32_t *dp;
893 u_int8_t *fb;
894 int fs, i, fg, bg, ul;
895
896 ri = (struct rasops_info *)cookie;
897
898 if (uc == ' ') {
899 zx_fillrect(ri, col, row, 1, 1, attr, ZX_STD_ROP);
900 return;
901 }
902
903 sc = (struct zx_softc *)ri->ri_hw;
904 font = ri->ri_font;
905 zc = sc->sc_zc;
906 zd = sc->sc_zd_ss0;
907
908 dp = (volatile u_int32_t *)sc->sc_pixels +
909 ((row * sc->sc_fonth + ri->ri_yorigin) << 11) +
910 (col * sc->sc_fontw + ri->ri_xorigin);
911 fb = (u_int8_t *)font->data + (uc - font->firstchar) *
912 ri->ri_fontscale;
913 fs = font->stride;
914 rasops_unpack_attr(attr, &fg, &bg, &ul);
915
916 while ((zc->zc_csr & ZX_CSR_BLT_BUSY) != 0)
917 ;
918
919 SETREG(zd->zd_rop, ZX_STD_ROP);
920 SETREG(zd->zd_fg, (fg & 7) ? 0x00000000 : 0xff000000);
921 SETREG(zd->zd_bg, (bg & 7) ? 0x00000000 : 0xff000000);
922 SETREG(zc->zc_fontmsk, 0xffffffff << (32 - sc->sc_fontw));
923
924 if (sc->sc_fontw <= 8) {
925 for (i = sc->sc_fonth; i != 0; i--, dp += 2048) {
926 *dp = *fb << 24;
927 fb += fs;
928 }
929 } else {
930 for (i = sc->sc_fonth; i != 0; i--, dp += 2048) {
931 *dp = *((u_int16_t *)fb) << 16;
932 fb += fs;
933 }
934 }
935
936 if (ul) {
937 dp -= 4096;
938 *dp = 0xffffffff;
939 }
940 }
941