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