tcx.c revision 1.53 1 /* $NetBSD: tcx.c,v 1.53 2014/07/07 15:22:07 macallan Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1998, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg and Michael Lorenz.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * color display (TCX) driver.
34 *
35 * Does not handle interrupts, even though they can occur.
36 *
37 * XXX should defer colormap updates to vertical retrace interrupts
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: tcx.c,v 1.53 2014/07/07 15:22:07 macallan Exp $");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/device.h>
47 #include <sys/ioctl.h>
48 #include <sys/malloc.h>
49 #include <sys/mman.h>
50 #include <sys/tty.h>
51 #include <sys/conf.h>
52
53 #ifdef DEBUG
54 #include <sys/proc.h>
55 #include <sys/syslog.h>
56 #endif
57
58 #include <sys/bus.h>
59 #include <machine/autoconf.h>
60
61 #include <dev/sun/fbio.h>
62 #include <dev/sun/fbvar.h>
63 #include <dev/sun/btreg.h>
64 #include <dev/sun/btvar.h>
65
66 #include <dev/sbus/sbusvar.h>
67 #include <dev/sbus/tcxreg.h>
68
69 #include <dev/wscons/wsdisplayvar.h>
70 #include <dev/wscons/wsconsio.h>
71 #include <dev/wsfont/wsfont.h>
72 #include <dev/rasops/rasops.h>
73
74 #include <dev/wscons/wsdisplay_vconsvar.h>
75
76 #include "opt_wsemul.h"
77
78 #include "ioconf.h"
79
80 /* per-display variables */
81 struct tcx_softc {
82 device_t sc_dev; /* base device */
83 struct fbdevice sc_fb; /* frame buffer device */
84 bus_space_tag_t sc_bustag;
85 struct openprom_addr sc_physaddr[TCX_NREG];/* phys addr of h/w */
86
87 bus_space_handle_t sc_bt; /* Brooktree registers */
88 bus_space_handle_t sc_thc; /* THC registers */
89 uint8_t *sc_fbaddr; /* framebuffer */
90 uint64_t *sc_rblit; /* blitspace */
91 uint64_t *sc_rstip; /* stipple space */
92
93 short sc_8bit; /* true if 8-bit hardware */
94 short sc_blanked; /* true if blanked */
95 uint32_t sc_fbsize; /* size of the 8bit fb */
96 u_char sc_cmap_red[256];
97 u_char sc_cmap_green[256];
98 u_char sc_cmap_blue[256];
99 int sc_mode, sc_bg;
100 int sc_cursor_x, sc_cursor_y;
101 int sc_hotspot_x, sc_hotspot_y;
102 struct vcons_data vd;
103 };
104
105 static struct vcons_screen tcx_console_screen;
106
107 extern const u_char rasops_cmap[768];
108
109 struct wsscreen_descr tcx_defscreendesc = {
110 "default",
111 0, 0,
112 NULL,
113 8, 16,
114 WSSCREEN_WSCOLORS,
115 };
116
117 const struct wsscreen_descr *_tcx_scrlist[] = {
118 &tcx_defscreendesc,
119 /* XXX other formats, graphics screen? */
120 };
121
122 struct wsscreen_list tcx_screenlist = {
123 sizeof(_tcx_scrlist) / sizeof(struct wsscreen_descr *),
124 _tcx_scrlist
125 };
126
127 /*
128 * The S24 provides the framebuffer RAM mapped in three ways:
129 * 26 bits per pixel, in 32-bit words; the low-order 24 bits are
130 * blue, green, and red values, and the other two bits select the
131 * display modes, per pixel);
132 * 24 bits per pixel, in 32-bit words; the high-order byte reads as
133 * zero, and is ignored on writes (so the mode bits cannot be altered);
134 * 8 bits per pixel, unpadded; writes to this space do not modify the
135 * other 18 bits.
136 */
137 #define TCX_CTL_8_MAPPED 0x00000000 /* 8 bits, uses color map */
138 #define TCX_CTL_24_MAPPED 0x01000000 /* 24 bits, uses color map */
139 #define TCX_CTL_24_LEVEL 0x03000000 /* 24 bits, ignores color map */
140 #define TCX_CTL_PIXELMASK 0x00FFFFFF /* mask for index/level */
141
142 /*
143 * differences between S24 and tcx, as far as this driver is concerned:
144 * - S24 has 4MB VRAM, 24bit + 2bit control planes, no expansion possible
145 * - tcx has 1MB VRAM, 8bit, no control planes, may have a VSIMM toat bumps
146 * VRAM to 2MB
147 * - tcx can apply ROPs to STIP operations, unlike S24
148 * - tcx has a Bt458 DAC, just like CG6. S24 has an AT&T 20C567
149 */
150
151 /* autoconfiguration driver */
152 static void tcxattach(device_t, device_t, void *);
153 static int tcxmatch(device_t, cfdata_t, void *);
154 static void tcx_unblank(device_t);
155
156 CFATTACH_DECL_NEW(tcx, sizeof(struct tcx_softc),
157 tcxmatch, tcxattach, NULL, NULL);
158
159 dev_type_open(tcxopen);
160 dev_type_close(tcxclose);
161 dev_type_ioctl(tcxioctl);
162 dev_type_mmap(tcxmmap);
163
164 const struct cdevsw tcx_cdevsw = {
165 .d_open = tcxopen,
166 .d_close = tcxclose,
167 .d_read = noread,
168 .d_write = nowrite,
169 .d_ioctl = tcxioctl,
170 .d_stop = nostop,
171 .d_tty = notty,
172 .d_poll = nopoll,
173 .d_mmap = tcxmmap,
174 .d_kqfilter = nokqfilter,
175 .d_flag = 0
176 };
177
178 /* frame buffer generic driver */
179 static struct fbdriver tcx_fbdriver = {
180 tcx_unblank, tcxopen, tcxclose, tcxioctl, nopoll, tcxmmap,
181 nokqfilter
182 };
183
184 static void tcx_reset(struct tcx_softc *);
185 static void tcx_loadcmap(struct tcx_softc *, int, int);
186
187 static int tcx_ioctl(void *, void *, u_long, void *, int, struct lwp *);
188 static paddr_t tcx_mmap(void *, void *, off_t, int);
189
190 static void tcx_init_screen(void *, struct vcons_screen *, int, long *);
191 static void tcx_clearscreen(struct tcx_softc *, int);
192 static void tcx_copyrows(void *, int, int, int);
193 static void tcx_eraserows(void *, int, int, long);
194 static void tcx_putchar(void *, int, int, u_int, long);
195 static void tcx_set_video(struct tcx_softc *, int);
196 static int tcx_do_cursor(struct tcx_softc *, struct wsdisplay_cursor *);
197 static void tcx_set_cursor(struct tcx_softc *);
198
199 struct wsdisplay_accessops tcx_accessops = {
200 tcx_ioctl,
201 tcx_mmap,
202 NULL, /* vcons_alloc_screen */
203 NULL, /* vcons_free_screen */
204 NULL, /* vcons_show_screen */
205 NULL, /* load_font */
206 NULL, /* polls */
207 NULL, /* scroll */
208 };
209
210 #define OBPNAME "SUNW,tcx"
211
212 /*
213 * Match a tcx.
214 */
215 int
216 tcxmatch(device_t parent, cfdata_t cf, void *aux)
217 {
218 struct sbus_attach_args *sa = aux;
219
220 if (strcmp(sa->sa_name, OBPNAME) == 0)
221 return 100; /* beat genfb */
222 return 0;
223 }
224
225 /*
226 * Attach a display.
227 */
228 void
229 tcxattach(device_t parent, device_t self, void *args)
230 {
231 struct tcx_softc *sc = device_private(self);
232 struct sbus_attach_args *sa = args;
233 struct wsemuldisplaydev_attach_args aa;
234 struct rasops_info *ri;
235 unsigned long defattr;
236 int node;
237 struct fbdevice *fb = &sc->sc_fb;
238 bus_space_handle_t bh;
239 int isconsole, i, j;
240 uint32_t confreg;
241
242 sc->sc_dev = self;
243 sc->sc_bustag = sa->sa_bustag;
244 node = sa->sa_node;
245
246 sc->sc_cursor_x = 0x7fff;
247 sc->sc_cursor_y = 0x7fff;
248 sc->sc_hotspot_x = 0;
249 sc->sc_hotspot_y = 0;
250
251 fb->fb_driver = &tcx_fbdriver;
252 fb->fb_device = sc->sc_dev;
253 /* Mask out invalid flags from the user. */
254 fb->fb_flags = device_cfdata(sc->sc_dev)->cf_flags & FB_USERMASK;
255 /*
256 * The onboard framebuffer on the SS4 supports only 8-bit mode;
257 * it can be distinguished from the S24 card for the SS5 by the
258 * presence of the "tcx-8-bit" attribute on the SS4 version.
259 */
260 sc->sc_8bit = node_has_property(node, "tcx-8-bit");
261 fb->fb_type.fb_depth = 8;
262 fb_setsize_obp(fb, fb->fb_type.fb_depth, 1152, 900, node);
263
264 /*
265 * actual FB size ( of the 8bit region )
266 * no need to restrict userland mappings to the visible VRAM
267 */
268 if (sc->sc_8bit) {
269 aprint_normal(" (8-bit only TCX)\n");
270 /* at least the SS4 can have 2MB with a VSIMM */
271 sc->sc_fbsize = 0x100000 * prom_getpropint(node, "vram", 1);
272 } else {
273 aprint_normal(" (S24)\n");
274 /* all S24 I know of have 4MB, non-expandable */
275 sc->sc_fbsize = 0x100000;
276 }
277
278 fb->fb_type.fb_cmsize = 256;
279 fb->fb_type.fb_size = sc->sc_fbsize; /* later code assumes 8bit */
280 aprint_normal_dev(self, "%s, %d x %d\n", OBPNAME,
281 fb->fb_type.fb_width,
282 fb->fb_type.fb_height);
283
284 fb->fb_type.fb_type = FBTYPE_TCXCOLOR;
285
286 if (sa->sa_nreg != TCX_NREG) {
287 aprint_error("\n");
288 aprint_error_dev(self, "only %d register sets\n",
289 sa->sa_nreg);
290 return;
291 }
292 if (sa->sa_reg[TCX_REG_STIP].oa_size < 0x1000) {
293 aprint_error("\n");
294 aprint_error_dev(self, "STIP register too small (0x%x)\n",
295 sa->sa_reg[TCX_REG_STIP].oa_size);
296 return;
297 }
298
299 memcpy(sc->sc_physaddr, sa->sa_reg,
300 sa->sa_nreg * sizeof(struct openprom_addr));
301
302 /* Map the register banks we care about */
303 if (sbus_bus_map(sa->sa_bustag,
304 sc->sc_physaddr[TCX_REG_THC].oa_space,
305 sc->sc_physaddr[TCX_REG_THC].oa_base,
306 0x1000,
307 BUS_SPACE_MAP_LINEAR, &sc->sc_thc) != 0) {
308 aprint_error_dev(self,
309 "tcxattach: cannot map thc registers\n");
310 return;
311 }
312
313 if (sbus_bus_map(sa->sa_bustag,
314 sc->sc_physaddr[TCX_REG_CMAP].oa_space,
315 sc->sc_physaddr[TCX_REG_CMAP].oa_base,
316 0x1000,
317 BUS_SPACE_MAP_LINEAR, &sc->sc_bt) != 0) {
318 aprint_error_dev(self, "tcxattach: cannot map DAC registers\n");
319 return;
320 }
321
322 /* map the 8bit dumb FB for the console */
323 if (sbus_bus_map(sa->sa_bustag,
324 sc->sc_physaddr[TCX_REG_DFB8].oa_space,
325 sc->sc_physaddr[TCX_REG_DFB8].oa_base,
326 sc->sc_fbsize,
327 BUS_SPACE_MAP_LINEAR,
328 &bh) != 0) {
329 aprint_error_dev(self, "tcxattach: cannot map framebuffer\n");
330 return;
331 }
332 sc->sc_fbaddr = bus_space_vaddr(sa->sa_bustag, bh);
333
334 /*
335 * 8bit tcx has the RSTIP and RBLIT ranges set to size 0.
336 * On Real Hardware they work anyway ( on my SS4 at least ) but
337 * emulators may not be so forgiving.
338 */
339 if (sc->sc_8bit) {
340 /* BLIT space */
341 if (sbus_bus_map(sa->sa_bustag,
342 sc->sc_physaddr[TCX_REG_BLIT].oa_space,
343 sc->sc_physaddr[TCX_REG_BLIT].oa_base,
344 sc->sc_fbsize << 3,
345 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
346 &bh) != 0) {
347 aprint_error_dev(self,
348 "tcxattach: cannot map BLIT space\n");
349 return;
350 }
351 sc->sc_rblit = bus_space_vaddr(sa->sa_bustag, bh);
352
353 /* STIP space */
354 if (sbus_bus_map(sa->sa_bustag,
355 sc->sc_physaddr[TCX_REG_STIP].oa_space,
356 sc->sc_physaddr[TCX_REG_STIP].oa_base,
357 sc->sc_fbsize << 3,
358 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
359 &bh) != 0) {
360 aprint_error_dev(self,
361 "tcxattach: cannot map STIP space\n");
362 return;
363 }
364 sc->sc_rstip = bus_space_vaddr(sa->sa_bustag, bh);
365 } else {
366 /* RBLIT space */
367 if (sbus_bus_map(sa->sa_bustag,
368 sc->sc_physaddr[TCX_REG_RBLIT].oa_space,
369 sc->sc_physaddr[TCX_REG_RBLIT].oa_base,
370 sc->sc_fbsize << 3,
371 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
372 &bh) != 0) {
373 aprint_error_dev(self,
374 "tcxattach: cannot map RBLIT space\n");
375 return;
376 }
377 sc->sc_rblit = bus_space_vaddr(sa->sa_bustag, bh);
378
379 /* RSTIP space */
380 if (sbus_bus_map(sa->sa_bustag,
381 sc->sc_physaddr[TCX_REG_RSTIP].oa_space,
382 sc->sc_physaddr[TCX_REG_RSTIP].oa_base,
383 sc->sc_fbsize << 3,
384 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_LARGE,
385 &bh) != 0) {
386 aprint_error_dev(self,
387 "tcxattach: cannot map RSTIP space\n");
388 return;
389 }
390 sc->sc_rstip = bus_space_vaddr(sa->sa_bustag, bh);
391 }
392 isconsole = fb_is_console(node);
393
394 confreg = bus_space_read_4(sa->sa_bustag, sc->sc_thc, THC_CONFIG);
395 aprint_normal_dev(self, "id %d, rev %d, sense %d\n",
396 (confreg & THC_CFG_FBID) >> THC_CFG_FBID_SHIFT,
397 (confreg & THC_CFG_REV) >> THC_CFG_REV_SHIFT,
398 (confreg & THC_CFG_SENSE) >> THC_CFG_SENSE_SHIFT
399 );
400
401 /* reset cursor & frame buffer controls */
402 tcx_reset(sc);
403
404 /* Initialize the default color map. */
405 j = 0;
406 for (i = 0; i < 256; i++) {
407
408 sc->sc_cmap_red[i] = rasops_cmap[j];
409 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
410 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
411 j += 3;
412 }
413 tcx_loadcmap(sc, 0, 256);
414
415 if (!sc->sc_8bit)
416 tcx_set_cursor(sc);
417
418 /* enable video */
419 confreg = bus_space_read_4(sa->sa_bustag, sc->sc_thc, THC_MISC);
420 confreg |= THC_MISC_VIDEN;
421 bus_space_write_4(sa->sa_bustag, sc->sc_thc, THC_MISC, confreg);
422
423 if (isconsole) {
424 aprint_error_dev(self, "(console)\n");
425 }
426
427 fb_attach(&sc->sc_fb, isconsole);
428
429 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
430 wsfont_init();
431
432 vcons_init(&sc->vd, sc, &tcx_defscreendesc, &tcx_accessops);
433 sc->vd.init_screen = tcx_init_screen;
434
435 vcons_init_screen(&sc->vd, &tcx_console_screen, 1, &defattr);
436 tcx_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
437
438 ri = &tcx_console_screen.scr_ri;
439
440 sc->sc_bg = ri->ri_devcmap[(defattr >> 16) & 0xff];
441 tcx_clearscreen(sc, 0);
442
443 tcx_defscreendesc.nrows = ri->ri_rows;
444 tcx_defscreendesc.ncols = ri->ri_cols;
445 tcx_defscreendesc.textops = &ri->ri_ops;
446 tcx_defscreendesc.capabilities = ri->ri_caps;
447
448 if(isconsole) {
449 wsdisplay_cnattach(&tcx_defscreendesc, ri, 0, 0, defattr);
450 vcons_replay_msgbuf(&tcx_console_screen);
451 }
452
453 aa.console = isconsole;
454 aa.scrdata = &tcx_screenlist;
455 aa.accessops = &tcx_accessops;
456 aa.accesscookie = &sc->vd;
457
458 config_found(self, &aa, wsemuldisplaydevprint);
459 /*
460 * we need to do this again - something overwrites a handful
461 * palette registers and we end up with white in reg. 0
462 */
463 tcx_loadcmap(sc, 0, 256);
464 }
465
466 int
467 tcxopen(dev_t dev, int flags, int mode, struct lwp *l)
468 {
469 return (0);
470 }
471
472 int
473 tcxclose(dev_t dev, int flags, int mode, struct lwp *l)
474 {
475 struct tcx_softc *sc = device_lookup_private(&tcx_cd, minor(dev));
476
477 tcx_reset(sc);
478 /* we may want to clear and redraw the console here */
479 return (0);
480 }
481
482 int
483 tcxioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
484 {
485 struct tcx_softc *sc = device_lookup_private(&tcx_cd, minor(dev));
486
487 switch (cmd) {
488
489 case FBIOGTYPE:
490 *(struct fbtype *)data = sc->sc_fb.fb_type;
491 break;
492
493 case FBIOGATTR:
494 #define fba ((struct fbgattr *)data)
495 fba->real_type = sc->sc_fb.fb_type.fb_type;
496 fba->owner = 0; /* XXX ??? */
497 fba->fbtype = sc->sc_fb.fb_type;
498 fba->sattr.flags = 0;
499 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
500 fba->sattr.dev_specific[0] = -1;
501 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
502 fba->emu_types[1] = FBTYPE_SUN3COLOR;
503 fba->emu_types[2] = -1;
504 #undef fba
505 break;
506
507 case FBIOGETCMAP:
508 #define p ((struct fbcmap *)data)
509 if (copyout(&sc->sc_cmap_red[p->index], p->red, p->count) != 0)
510 return EINVAL;
511 if (copyout(&sc->sc_cmap_green[p->index], p->green, p->count)
512 != 0)
513 return EINVAL;
514 if (copyout(&sc->sc_cmap_blue[p->index], p->blue, p->count)
515 != 0)
516 return EINVAL;
517 return 0;
518
519 case FBIOPUTCMAP:
520 /* copy to software map */
521 if (copyin(p->red, &sc->sc_cmap_red[p->index], p->count) != 0)
522 return EINVAL;
523 if (copyin(p->green, &sc->sc_cmap_green[p->index], p->count)
524 != 0)
525 return EINVAL;
526 if (copyin(p->blue, &sc->sc_cmap_blue[p->index], p->count) != 0)
527 return EINVAL;
528 tcx_loadcmap(sc, p->index, p->count);
529 #undef p
530 break;
531 case FBIOGVIDEO:
532 *(int *)data = sc->sc_blanked;
533 break;
534
535 case FBIOSVIDEO:
536 tcx_set_video(sc, *(int *)data);
537 break;
538
539 default:
540 #ifdef DEBUG
541 log(LOG_NOTICE, "tcxioctl(0x%lx) (%s[%d])\n", cmd,
542 l->l_proc->p_comm, l->l_proc->p_pid);
543 #endif
544 return (ENOTTY);
545 }
546 return (0);
547 }
548
549 /*
550 * Clean up hardware state (e.g., after bootup or after X crashes).
551 */
552 static void
553 tcx_reset(struct tcx_softc *sc)
554 {
555 uint32_t reg;
556
557 reg = bus_space_read_4(sc->sc_bustag, sc->sc_thc, THC_MISC);
558 reg |= THC_MISC_CURSRES;
559 bus_space_write_4(sc->sc_bustag, sc->sc_thc, THC_MISC, reg);
560 }
561
562 static void
563 tcx_loadcmap(struct tcx_softc *sc, int start, int ncolors)
564 {
565 int i;
566
567 for (i = 0; i < ncolors; i++) {
568 bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_ADDRESS,
569 (start + i) << 24);
570 bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_FB_LUT,
571 sc->sc_cmap_red[i + start] << 24);
572 bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_FB_LUT,
573 sc->sc_cmap_green[i + start] << 24);
574 bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_FB_LUT,
575 sc->sc_cmap_blue[i + start] << 24);
576 }
577 bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_ADDRESS, 0);
578 }
579
580 static void
581 tcx_unblank(device_t dev)
582 {
583 struct tcx_softc *sc = device_private(dev);
584
585 if (sc->sc_blanked) {
586 uint32_t reg;
587 sc->sc_blanked = 0;
588 reg = bus_space_read_4(sc->sc_bustag, sc->sc_thc, THC_MISC);
589 reg &= ~THC_MISC_VSYNC_DISABLE;
590 reg &= ~THC_MISC_HSYNC_DISABLE;
591 reg |= THC_MISC_VIDEN;
592 bus_space_write_4(sc->sc_bustag, sc->sc_thc, THC_MISC, reg);
593 }
594 }
595
596 static void
597 tcx_set_video(struct tcx_softc *sc, int unblank)
598 {
599 uint32_t reg;
600 if (unblank) {
601 sc->sc_blanked = 0;
602 reg = bus_space_read_4(sc->sc_bustag, sc->sc_thc, THC_MISC);
603 reg &= ~THC_MISC_VSYNC_DISABLE;
604 reg &= ~THC_MISC_HSYNC_DISABLE;
605 reg |= THC_MISC_VIDEN;
606 bus_space_write_4(sc->sc_bustag, sc->sc_thc, THC_MISC, reg);
607 } else {
608 sc->sc_blanked = 1;
609 reg = bus_space_read_4(sc->sc_bustag, sc->sc_thc, THC_MISC);
610 reg |= THC_MISC_VSYNC_DISABLE;
611 reg |= THC_MISC_HSYNC_DISABLE;
612 reg &= ~THC_MISC_VIDEN;
613 bus_space_write_4(sc->sc_bustag, sc->sc_thc, THC_MISC, reg);
614 }
615 }
616
617 /*
618 * Base addresses at which users can mmap() the various pieces of a tcx.
619 */
620 #define TCX_USER_RAM 0x00000000
621 #define TCX_USER_RAM24 0x01000000
622 #define TCX_USER_RAM_COMPAT 0x04000000 /* cg3 emulation */
623 #define TCX_USER_STIP 0x10000000
624 #define TCX_USER_BLIT 0x20000000
625 #define TCX_USER_RDFB32 0x28000000
626 #define TCX_USER_RSTIP 0x30000000
627 #define TCX_USER_RBLIT 0x38000000
628 #define TCX_USER_TEC 0x70001000
629 #define TCX_USER_BTREGS 0x70002000
630 #define TCX_USER_THC 0x70004000
631 #define TCX_USER_DHC 0x70008000
632 #define TCX_USER_ALT 0x7000a000
633 #define TCX_USER_UART 0x7000c000
634 #define TCX_USER_VRT 0x7000e000
635 #define TCX_USER_ROM 0x70010000
636
637 struct mmo {
638 u_int mo_uaddr; /* user (virtual) address */
639 u_int mo_size; /* size, or 0 for video ram size */
640 u_int mo_bank; /* register bank number */
641 };
642
643 /*
644 * Return the address that would map the given device at the given
645 * offset, allowing for the given protection, or return -1 for error.
646 *
647 * XXX needs testing against `demanding' applications (e.g., aviator)
648 */
649 paddr_t
650 tcxmmap(dev_t dev, off_t off, int prot)
651 {
652 struct tcx_softc *sc = device_lookup_private(&tcx_cd, minor(dev));
653 struct openprom_addr *rr = sc->sc_physaddr;
654 struct mmo *mo, *mo_end;
655 u_int u, sz;
656 static struct mmo mmo[] = {
657 { TCX_USER_RAM, 0, TCX_REG_DFB8 },
658 { TCX_USER_RAM24, 0, TCX_REG_DFB24 },
659 { TCX_USER_RAM_COMPAT, 0, TCX_REG_DFB8 },
660
661 { TCX_USER_STIP, 1, TCX_REG_STIP },
662 { TCX_USER_BLIT, 1, TCX_REG_BLIT },
663 { TCX_USER_RDFB32, 0, TCX_REG_RDFB32 },
664 { TCX_USER_RSTIP, 1, TCX_REG_RSTIP },
665 { TCX_USER_RBLIT, 1, TCX_REG_RBLIT },
666 { TCX_USER_TEC, 1, TCX_REG_TEC },
667 { TCX_USER_BTREGS, 8192 /* XXX */, TCX_REG_CMAP },
668 { TCX_USER_THC, 0x2000, TCX_REG_THC },
669 { TCX_USER_DHC, 1, TCX_REG_DHC },
670 { TCX_USER_ALT, 1, TCX_REG_ALT },
671 { TCX_USER_ROM, 65536, TCX_REG_ROM },
672 };
673 #define NMMO (sizeof mmo / sizeof *mmo)
674
675 if (off & PGOFSET)
676 panic("tcxmmap");
677
678 /*
679 * Entries with size 0 map video RAM (i.e., the size in fb data).
680 * Entries that map 32-bit deep regions are adjusted for their
681 * depth (fb_size gives the size of the 8-bit-deep region).
682 *
683 * Since we work in pages, the fact that the map offset table's
684 * sizes are sometimes bizarre (e.g., 1) is effectively ignored:
685 * one byte is as good as one page.
686 */
687
688 mo = mmo;
689 mo_end = &mmo[NMMO];
690
691 for (; mo < mo_end; mo++) {
692 if ((u_int)off < mo->mo_uaddr)
693 continue;
694
695 u = off - mo->mo_uaddr;
696 sz = mo->mo_size;
697
698 if (sz == 0) {
699 sz = sc->sc_fb.fb_type.fb_size;
700 /*
701 * check for the 32-bit-deep regions and adjust
702 * accordingly
703 */
704 if (mo->mo_uaddr == TCX_USER_RAM24 ||
705 mo->mo_uaddr == TCX_USER_RDFB32) {
706 if (sc->sc_8bit) {
707 /*
708 * not present on 8-bit hardware
709 */
710 continue;
711 }
712 sz *= 4;
713 }
714 }
715 if (sz == 1)
716 sz = rr[mo->mo_bank].oa_size;
717
718 if (u < sz) {
719 return (bus_space_mmap(sc->sc_bustag,
720 BUS_ADDR(rr[mo->mo_bank].oa_space,
721 rr[mo->mo_bank].oa_base),
722 u,
723 prot,
724 BUS_SPACE_MAP_LINEAR));
725 }
726 }
727 return (-1);
728 }
729
730 int
731 tcx_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
732 struct lwp *l)
733 {
734 struct vcons_data *vd = v;
735 struct tcx_softc *sc = vd->cookie;
736 struct wsdisplay_fbinfo *wdf;
737 struct vcons_screen *ms = vd->active;
738
739 switch (cmd) {
740 case WSDISPLAYIO_GTYPE:
741 *(u_int *)data = WSDISPLAY_TYPE_SUNTCX;
742 return 0;
743
744 case FBIOGVIDEO:
745 case WSDISPLAYIO_GVIDEO:
746 *(int *)data = !sc->sc_blanked;
747 return 0;
748
749 case WSDISPLAYIO_SVIDEO:
750 case FBIOSVIDEO:
751 tcx_set_video(sc, *(int *)data);
752 return 0;
753
754 case WSDISPLAYIO_GINFO:
755 wdf = (void *)data;
756 wdf->height = ms->scr_ri.ri_height;
757 wdf->width = ms->scr_ri.ri_width;
758 if (sc->sc_8bit) {
759 wdf->depth = 8;
760 } else {
761 wdf->depth = 32;
762 }
763 wdf->cmsize = 256;
764 return 0;
765 case WSDISPLAYIO_LINEBYTES:
766 {
767 int *ret = (int *)data;
768 *ret = sc->sc_8bit ? ms->scr_ri.ri_width :
769 ms->scr_ri.ri_width << 2;
770 }
771 return 0;
772 #if 0
773 case WSDISPLAYIO_GETCMAP:
774 return tcx_getcmap(sc, (struct wsdisplay_cmap *)data);
775
776 case WSDISPLAYIO_PUTCMAP:
777 return tcx_putcmap(sc, (struct wsdisplay_cmap *)data);
778 #endif
779 case WSDISPLAYIO_SMODE:
780 {
781 int new_mode = *(int*)data;
782 if (new_mode != sc->sc_mode)
783 {
784 sc->sc_mode = new_mode;
785 if (new_mode == WSDISPLAYIO_MODE_EMUL)
786 {
787 tcx_loadcmap(sc, 0, 256);
788 tcx_clearscreen(sc, 0);
789 vcons_redraw_screen(ms);
790 } else if (!sc->sc_8bit)
791 tcx_clearscreen(sc, 3);
792 }
793 }
794 return 0;
795
796 case WSDISPLAYIO_GCURPOS:
797 if (sc->sc_8bit) {
798 return EOPNOTSUPP;
799 } else {
800 struct wsdisplay_curpos *cp = (void *)data;
801
802 cp->x = sc->sc_cursor_x;
803 cp->y = sc->sc_cursor_y;
804 }
805 return 0;
806
807 case WSDISPLAYIO_SCURPOS:
808 if (sc->sc_8bit) {
809 return EOPNOTSUPP;
810 } else {
811 struct wsdisplay_curpos *cp = (void *)data;
812
813 sc->sc_cursor_x = cp->x;
814 sc->sc_cursor_y = cp->y;
815 tcx_set_cursor(sc);
816 }
817 return 0;
818
819 case WSDISPLAYIO_GCURMAX:
820 if (sc->sc_8bit) {
821 return EOPNOTSUPP;
822 } else {
823 struct wsdisplay_curpos *cp = (void *)data;
824
825 cp->x = 32;
826 cp->y = 32;
827 }
828 return 0;
829
830 case WSDISPLAYIO_SCURSOR:
831 if (sc->sc_8bit) {
832 return EOPNOTSUPP;
833 } else {
834 struct wsdisplay_cursor *cursor = (void *)data;
835
836 return tcx_do_cursor(sc, cursor);
837 }
838 }
839 return EPASSTHROUGH;
840 }
841
842 static paddr_t
843 tcx_mmap(void *v, void *vs, off_t offset, int prot)
844 {
845 struct vcons_data *vd = v;
846 struct tcx_softc *sc = vd->cookie;
847
848 /* 'regular' framebuffer mmap()ing */
849 if (sc->sc_8bit) {
850 /* on 8Bit boards hand over the 8 bit aperture */
851 if (offset > sc->sc_fbsize)
852 return -1;
853 return bus_space_mmap(sc->sc_bustag,
854 sc->sc_physaddr[TCX_REG_DFB8].oa_base + offset, 0, prot,
855 BUS_SPACE_MAP_LINEAR);
856 } else {
857 /* ... but if we have a 24bit aperture we use it */
858 if (offset > sc->sc_fbsize * 4)
859 return -1;
860 return bus_space_mmap(sc->sc_bustag,
861 sc->sc_physaddr[TCX_REG_DFB24].oa_base + offset, 0, prot,
862 BUS_SPACE_MAP_LINEAR);
863 }
864 return -1;
865 }
866
867 static void
868 tcx_init_screen(void *cookie, struct vcons_screen *scr,
869 int existing, long *defattr)
870 {
871 struct tcx_softc *sc = cookie;
872 struct rasops_info *ri = &scr->scr_ri;
873
874 ri->ri_depth = 8;
875 ri->ri_width = sc->sc_fb.fb_type.fb_width;
876 ri->ri_height = sc->sc_fb.fb_type.fb_height;
877 ri->ri_stride = sc->sc_fb.fb_linebytes;
878 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
879
880 ri->ri_bits = sc->sc_fbaddr;
881
882 rasops_init(ri, 0, 0);
883 ri->ri_caps = WSSCREEN_WSCOLORS;
884 rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
885 ri->ri_width / ri->ri_font->fontwidth);
886
887 /* enable acceleration */
888 ri->ri_ops.copyrows = tcx_copyrows;
889 ri->ri_ops.eraserows = tcx_eraserows;
890 ri->ri_ops.putchar = tcx_putchar;
891 #if 0
892 ri->ri_ops.cursor = tcx_cursor;
893 ri->ri_ops.copycols = tcx_copycols;
894 ri->ri_ops.erasecols = tcx_erasecols;
895 #endif
896 }
897
898 static void
899 tcx_clearscreen(struct tcx_softc *sc, int spc)
900 {
901 /* ROP in the upper 4bit is necessary, tcx actually uses it */
902 uint64_t bg = 0x30000000ffffffffLL;
903 uint64_t spc64;
904 int i, len;
905
906 spc64 = ((spc & 3) << 24) | sc->sc_bg;
907 bg |= (spc64 << 32);
908
909 len = sc->sc_fb.fb_type.fb_width * sc->sc_fb.fb_type.fb_height;
910 for (i = 0; i < len; i += 32)
911 sc->sc_rstip[i] = bg;
912 }
913
914 static void
915 tcx_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
916 {
917 struct rasops_info *ri = cookie;
918 struct vcons_screen *scr = ri->ri_hw;
919 struct tcx_softc *sc = scr->scr_cookie;
920 int i, last, first, len, dest, leftover;
921
922 i = ri->ri_width * ri->ri_font->fontheight * nrows;
923 len = i & 0xffffe0;
924 leftover = i & 0x1f;
925 if (srcrow < dstrow) {
926 /* we must go bottom to top */
927 first = ri->ri_width *
928 (ri->ri_font->fontheight * srcrow + ri->ri_yorigin);
929 last = first + len;
930 dest = ri->ri_width *
931 (ri->ri_font->fontheight * dstrow + ri->ri_yorigin) + len;
932 if (leftover > 0) {
933 sc->sc_rblit[dest + 32] =
934 (uint64_t)((leftover - 1) << 24) |
935 (uint64_t)(i + 32);
936 }
937 for (i = last; i >= first; i -= 32) {
938 sc->sc_rblit[dest] = 0x300000001f000000LL | (uint64_t)i;
939 dest -= 32;
940 }
941 } else {
942 /* top to bottom */
943 first = ri->ri_width *
944 (ri->ri_font->fontheight * srcrow + ri->ri_yorigin);
945 dest = ri->ri_width *
946 (ri->ri_font->fontheight * dstrow + ri->ri_yorigin);
947 last = first + len;
948 for (i = first; i <= last; i+= 32) {
949 sc->sc_rblit[dest] = 0x300000001f000000LL | (uint64_t)i;
950 dest += 32;
951 }
952 if (leftover > 0) {
953 sc->sc_rblit[dest] =
954 (uint64_t)((leftover - 1) << 24) | (uint64_t)i;
955 }
956 }
957 }
958
959 static void
960 tcx_eraserows(void *cookie, int start, int nrows, long attr)
961 {
962 struct rasops_info *ri = cookie;
963 struct vcons_screen *scr = ri->ri_hw;
964 struct tcx_softc *sc = scr->scr_cookie;
965 uint64_t temp;
966 int i, last, first, len, leftover;
967
968 i = ri->ri_width * ri->ri_font->fontheight * nrows;
969 len = i & 0xffffe0;
970 leftover = i & 0x1f;
971 first = ri->ri_width *
972 (ri->ri_font->fontheight * start + ri->ri_yorigin);
973 last = first + len;
974 temp = 0x30000000ffffffffLL |
975 ((uint64_t)ri->ri_devcmap[(attr >> 16) & 0xff] << 32);
976
977 for (i = first; i <= last; i+= 32)
978 sc->sc_rblit[i] = temp;
979
980 if (leftover > 0) {
981 temp &= 0xffffffffffffffffLL << (32 - leftover);
982 sc->sc_rblit[i] = temp;
983 }
984 }
985 /*
986 * The stipple engine is 100% retarded. All drawing operations have to start
987 * at 32 pixel boundaries so we'll have to deal with characters being split.
988 */
989
990 static void
991 tcx_putchar(void *cookie, int row, int col, u_int c, long attr)
992 {
993 struct rasops_info *ri = cookie;
994 struct wsdisplay_font *font = PICK_FONT(ri, c);
995 struct vcons_screen *scr = ri->ri_hw;
996 struct tcx_softc *sc = scr->scr_cookie;
997 uint64_t bg, fg, temp, mask;
998 int addr, i, uc, shift;
999 uint32_t fmask;
1000 uint8_t *cdata;
1001 uint16_t *wdata;
1002
1003 addr = ri->ri_xorigin + col * font->fontwidth +
1004 (ri->ri_yorigin + row * font->fontheight) * ri->ri_width;
1005
1006 /* check if the character is crossing a 32 pixel boundary */
1007 if ((addr & 0xffffe0) ==
1008 ((addr + font->fontwidth - 1) & 0xffffe0)) {
1009 /* phew, not split */
1010 shift = addr & 0x1f;
1011 addr &= 0xffffe0;
1012 fmask = 0xffffffff >> (32 - font->fontwidth);
1013 fmask = fmask << (32 - font->fontwidth - shift);
1014 mask = fmask;
1015 bg = 0x3000000000000000LL |
1016 ((uint64_t)ri->ri_devcmap[(attr >> 16) & 0xff] &
1017 0xff) << 32;
1018 bg |= mask;
1019 temp = 0x3000000000000000LL |
1020 ((uint64_t)ri->ri_devcmap[(attr >> 24) & 0xff] & 0xff) <<
1021 32;
1022 uc = c - font->firstchar;
1023 cdata = (uint8_t *)font->data + uc * ri->ri_fontscale;
1024
1025 if (font->fontwidth < 9) {
1026 /* byte by byte */
1027 for (i = 0; i < font->fontheight; i++) {
1028 sc->sc_rstip[addr] = bg;
1029 if (*cdata != 0) {
1030 if (shift > 24) {
1031 fg = (uint64_t)*cdata >>
1032 (shift - 24);
1033 } else {
1034 fg = (uint64_t)*cdata <<
1035 (24 - shift);
1036 }
1037 sc->sc_rstip[addr] = fg | temp;
1038 }
1039 cdata++;
1040 addr += ri->ri_width;
1041 }
1042 } else if (font->fontwidth < 17) {
1043 /* short by short */
1044 wdata = (uint16_t *)cdata;
1045 for (i = 0; i < font->fontheight; i++) {
1046 sc->sc_rstip[addr] = bg;
1047 if (*wdata != 0) {
1048 if (shift > 16) {
1049 fg = temp | (uint64_t)*wdata >>
1050 (shift - 16);
1051 } else {
1052 fg = temp | (uint64_t)*wdata <<
1053 (16 - shift);
1054 }
1055 sc->sc_rstip[addr] = fg;
1056 }
1057 wdata++;
1058 addr += ri->ri_width;
1059 }
1060 }
1061 } else {
1062 /* and now the split case ( man this hardware is dumb ) */
1063 uint64_t bgr, maskr, fgr;
1064 uint32_t bork;
1065
1066 shift = addr & 0x1f;
1067 addr &= 0xffffe0;
1068 mask = 0xffffffff >> shift;
1069 maskr = (uint64_t)(0xffffffffUL <<
1070 (32 - (font->fontwidth + shift - 32)));
1071 bg = 0x3000000000000000LL |
1072 ((uint64_t)ri->ri_devcmap[(attr >> 16) & 0xff] &
1073 0xff) << 32;
1074 bgr = bg | maskr;
1075 bg |= mask;
1076 temp = 0x3000000000000000LL |
1077 ((uint64_t)ri->ri_devcmap[(attr >> 24) & 0xff] & 0xff) <<
1078 32;
1079
1080 uc = c - font->firstchar;
1081 cdata = (uint8_t *)font->data + uc * ri->ri_fontscale;
1082
1083 if (font->fontwidth < 9) {
1084 /* byte by byte */
1085 for (i = 0; i < font->fontheight; i++) {
1086 sc->sc_rstip[addr] = bg;
1087 sc->sc_rstip[addr + 32] = bgr;
1088 bork = *cdata;
1089 if (bork != 0) {
1090 fg = (uint64_t)bork >> (shift - 24);
1091 sc->sc_rstip[addr] = fg | temp;
1092 fgr = (uint64_t)(bork << (52 - shift));
1093 sc->sc_rstip[addr] = fgr | temp;
1094 }
1095 cdata++;
1096 addr += ri->ri_width;
1097 }
1098 } else if (font->fontwidth < 17) {
1099 /* short by short */
1100 wdata = (uint16_t *)cdata;
1101 for (i = 0; i < font->fontheight; i++) {
1102 sc->sc_rstip[addr] = bg;
1103 sc->sc_rstip[addr + 32] = bgr;
1104 bork = *wdata;
1105 if (bork != 0) {
1106 fg = (uint64_t)bork >> (shift - 16);
1107 sc->sc_rstip[addr] = fg | temp;
1108 fgr = (uint64_t)(bork << (48 - shift));
1109 sc->sc_rstip[addr + 32] = fgr | temp;
1110 }
1111 wdata++;
1112 addr += ri->ri_width;
1113 }
1114 }
1115
1116 }
1117 }
1118
1119 static int
1120 tcx_do_cursor(struct tcx_softc *sc, struct wsdisplay_cursor *cur)
1121 {
1122 if (sc->sc_8bit) {
1123 /* hw cursor is not implemented on tcx */
1124 return -1;
1125 }
1126 if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
1127
1128 if (cur->enable) {
1129 tcx_set_cursor(sc);
1130 } else {
1131 /* move the cursor out of sight */
1132 bus_space_write_4(sc->sc_bustag, sc->sc_thc,
1133 THC_CURSOR_POS, 0x7fff7fff);
1134 }
1135 }
1136 if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
1137
1138 sc->sc_hotspot_x = cur->hot.x;
1139 sc->sc_hotspot_y = cur->hot.y;
1140 tcx_set_cursor(sc);
1141 }
1142 if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
1143
1144 sc->sc_cursor_x = cur->pos.x;
1145 sc->sc_cursor_y = cur->pos.y;
1146 tcx_set_cursor(sc);
1147 }
1148 if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
1149 #if 0
1150 /*
1151 * apparently we're not writing in the right register here - if we do
1152 * this the screen goes all funky
1153 */
1154 int i;
1155
1156 for (i = 0; i < cur->cmap.count; i++) {
1157 bus_space_write_4(sc->sc_bustag, sc->sc_bt, DAC_ADDRESS,
1158 (cur->cmap.index + i + 2) << 24);
1159 bus_space_write_4(sc->sc_bustag, sc->sc_bt,
1160 DAC_CURSOR_LUT, cur->cmap.red[i] << 24);
1161 bus_space_write_4(sc->sc_bustag, sc->sc_bt,
1162 DAC_CURSOR_LUT, cur->cmap.green[i] << 24);
1163 bus_space_write_4(sc->sc_bustag, sc->sc_bt,
1164 DAC_CURSOR_LUT, cur->cmap.blue[i] << 24);
1165 }
1166 #endif
1167 }
1168 if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
1169 int i;
1170 uint32_t temp, poof;
1171
1172 for (i = 0; i < 128; i += 4) {
1173 memcpy(&temp, &cur->mask[i], 4);
1174 printf("%08x -> ", temp);
1175 poof = ((temp & 0x80808080) >> 7) |
1176 ((temp & 0x40404040) >> 5) |
1177 ((temp & 0x20202020) >> 3) |
1178 ((temp & 0x10101010) >> 1) |
1179 ((temp & 0x08080808) << 1) |
1180 ((temp & 0x04040404) << 3) |
1181 ((temp & 0x02020202) << 5) |
1182 ((temp & 0x01010101) << 7);
1183 printf("%08x\n", poof);
1184 bus_space_write_4(sc->sc_bustag, sc->sc_thc,
1185 THC_CURSOR_1 + i, poof);
1186 memcpy(&temp, &cur->image[i], 4);
1187 poof = ((temp & 0x80808080) >> 7) |
1188 ((temp & 0x40404040) >> 5) |
1189 ((temp & 0x20202020) >> 3) |
1190 ((temp & 0x10101010) >> 1) |
1191 ((temp & 0x08080808) << 1) |
1192 ((temp & 0x04040404) << 3) |
1193 ((temp & 0x02020202) << 5) |
1194 ((temp & 0x01010101) << 7);
1195 bus_space_write_4(sc->sc_bustag, sc->sc_thc,
1196 THC_CURSOR_0 + i, poof);
1197 }
1198 }
1199 return 0;
1200 }
1201
1202 static void
1203 tcx_set_cursor(struct tcx_softc *sc)
1204 {
1205 uint32_t reg;
1206
1207 reg = (sc->sc_cursor_x - sc->sc_hotspot_x) << 16 |
1208 ((sc->sc_cursor_y - sc->sc_hotspot_y) & 0xffff);
1209 bus_space_write_4(sc->sc_bustag, sc->sc_thc, THC_CURSOR_POS, reg);
1210 }
1211
1212