p9100.c revision 1.36.16.1 1 /* $NetBSD: p9100.c,v 1.36.16.1 2008/06/02 13:23:50 mjf Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2005, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas.
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 (p9100) 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: p9100.c,v 1.36.16.1 2008/06/02 13:23:50 mjf 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 #include <sys/bus.h>
54 #include <machine/autoconf.h>
55
56 #include <dev/sun/fbio.h>
57 #include <dev/sun/fbvar.h>
58 #include <dev/sun/btreg.h>
59 #include <dev/sun/btvar.h>
60
61 #include <dev/sbus/p9100reg.h>
62
63 #include <dev/sbus/sbusvar.h>
64
65 /*#include <dev/wscons/wsdisplayvar.h>*/
66 #include <dev/wscons/wsconsio.h>
67 #include <dev/wsfont/wsfont.h>
68 #include <dev/rasops/rasops.h>
69
70 #include <dev/wscons/wsdisplay_vconsvar.h>
71
72 #include "opt_wsemul.h"
73 #include "rasops_glue.h"
74
75 #include "tctrl.h"
76 #if NTCTRL > 0
77 #include <machine/tctrl.h>
78 #include <sparc/dev/tctrlvar.h>/*XXX*/
79 #endif
80
81 struct pnozz_cursor {
82 short pc_enable; /* cursor is enabled */
83 struct fbcurpos pc_pos; /* position */
84 struct fbcurpos pc_hot; /* hot-spot */
85 struct fbcurpos pc_size; /* size of mask & image fields */
86 uint32_t pc_bits[0x100]; /* space for mask & image bits */
87 unsigned char red[3], green[3];
88 unsigned char blue[3]; /* cursor palette */
89 };
90
91 /* per-display variables */
92 struct p9100_softc {
93 struct device sc_dev; /* base device */
94 struct sbusdev sc_sd; /* sbus device */
95 struct fbdevice sc_fb; /* frame buffer device */
96
97 bus_space_tag_t sc_bustag;
98
99 bus_addr_t sc_ctl_paddr; /* phys address description */
100 bus_size_t sc_ctl_psize; /* for device mmap() */
101 bus_space_handle_t sc_ctl_memh; /* bus space handle */
102
103 bus_addr_t sc_cmd_paddr; /* phys address description */
104 bus_size_t sc_cmd_psize; /* for device mmap() */
105 bus_space_handle_t sc_cmd_memh; /* bus space handle */
106
107 bus_addr_t sc_fb_paddr; /* phys address description */
108 bus_size_t sc_fb_psize; /* for device mmap() */
109 bus_space_handle_t sc_fb_memh; /* bus space handle */
110
111 volatile uint32_t sc_junk;
112 uint32_t sc_mono_width; /* for setup_mono */
113
114 uint32_t sc_width;
115 uint32_t sc_height; /* panel width / height */
116 uint32_t sc_stride;
117 uint32_t sc_depth;
118 union bt_cmap sc_cmap; /* Brooktree color map */
119
120 struct pnozz_cursor sc_cursor;
121
122 int sc_mode;
123 int sc_video, sc_powerstate;
124 uint32_t sc_bg;
125 volatile uint32_t sc_last_offset;
126 struct vcons_data vd;
127 };
128
129
130 static struct vcons_screen p9100_console_screen;
131
132 extern const u_char rasops_cmap[768];
133
134 struct wsscreen_descr p9100_defscreendesc = {
135 "default",
136 0, 0,
137 NULL,
138 8, 16,
139 WSSCREEN_WSCOLORS,
140 };
141
142 const struct wsscreen_descr *_p9100_scrlist[] = {
143 &p9100_defscreendesc,
144 /* XXX other formats, graphics screen? */
145 };
146
147 struct wsscreen_list p9100_screenlist = {
148 sizeof(_p9100_scrlist) / sizeof(struct wsscreen_descr *), _p9100_scrlist
149 };
150
151 /* autoconfiguration driver */
152 static int p9100_sbus_match(struct device *, struct cfdata *, void *);
153 static void p9100_sbus_attach(struct device *, struct device *, void *);
154
155 static void p9100unblank(struct device *);
156 static void p9100_shutdown(void *);
157
158 CFATTACH_DECL(pnozz, sizeof(struct p9100_softc),
159 p9100_sbus_match, p9100_sbus_attach, NULL, NULL);
160
161 extern struct cfdriver pnozz_cd;
162
163 static dev_type_open(p9100open);
164 static dev_type_ioctl(p9100ioctl);
165 static dev_type_mmap(p9100mmap);
166
167 const struct cdevsw pnozz_cdevsw = {
168 p9100open, nullclose, noread, nowrite, p9100ioctl,
169 nostop, notty, nopoll, p9100mmap, nokqfilter,
170 };
171
172 /* frame buffer generic driver */
173 static struct fbdriver p9100fbdriver = {
174 p9100unblank, p9100open, nullclose, p9100ioctl, nopoll,
175 p9100mmap, nokqfilter
176 };
177
178 static void p9100loadcmap(struct p9100_softc *, int, int);
179 static void p9100_set_video(struct p9100_softc *, int);
180 static int p9100_get_video(struct p9100_softc *);
181 static uint32_t p9100_ctl_read_4(struct p9100_softc *, bus_size_t);
182 static void p9100_ctl_write_4(struct p9100_softc *, bus_size_t, uint32_t);
183 static uint8_t p9100_ramdac_read(struct p9100_softc *, bus_size_t);
184 static void p9100_ramdac_write(struct p9100_softc *, bus_size_t, uint8_t);
185
186 static uint8_t p9100_ramdac_read_ctl(struct p9100_softc *, int);
187 static void p9100_ramdac_write_ctl(struct p9100_softc *, int, uint8_t);
188
189 static void p9100_init_engine(struct p9100_softc *);
190
191 #if NWSDISPLAY > 0
192 static void p9100_sync(struct p9100_softc *);
193 static void p9100_bitblt(void *, int, int, int, int, int, int, uint32_t);
194 static void p9100_rectfill(void *, int, int, int, int, uint32_t);
195 static void p9100_clearscreen(struct p9100_softc *);
196
197 static void p9100_setup_mono(struct p9100_softc *, int, int, int, int,
198 uint32_t, uint32_t);
199 static void p9100_feed_line(struct p9100_softc *, int, uint8_t *);
200 static void p9100_set_color_reg(struct p9100_softc *, int, int32_t);
201
202 static void p9100_copycols(void *, int, int, int, int);
203 static void p9100_erasecols(void *, int, int, int, long);
204 static void p9100_copyrows(void *, int, int, int);
205 static void p9100_eraserows(void *, int, int, long);
206 /*static int p9100_mapchar(void *, int, u_int *);*/
207 static void p9100_putchar(void *, int, int, u_int, long);
208 static void p9100_cursor(void *, int, int, int);
209 static int p9100_allocattr(void *, int, int, int, long *);
210
211 /*static void p9100_scroll(void *, void *, int);*/
212
213 static int p9100_putcmap(struct p9100_softc *, struct wsdisplay_cmap *);
214 static int p9100_getcmap(struct p9100_softc *, struct wsdisplay_cmap *);
215 static int p9100_ioctl(void *, void *, u_long, void *, int, struct lwp *);
216 static paddr_t p9100_mmap(void *, void *, off_t, int);
217
218 /*static int p9100_load_font(void *, void *, struct wsdisplay_font *);*/
219
220 static void p9100_init_screen(void *, struct vcons_screen *, int,
221 long *);
222 #endif
223
224 static void p9100_init_cursor(struct p9100_softc *);
225
226 static void p9100_set_fbcursor(struct p9100_softc *);
227 static void p9100_setcursorcmap(struct p9100_softc *);
228 static void p9100_loadcursor(struct p9100_softc *);
229
230 static int p9100_intr(void *);
231
232 /* power management stuff */
233 static void p9100_power_hook(int, void *);
234
235 static void p9100_set_extvga(void *, int);
236
237 #if NWSDISPLAY > 0
238 struct wsdisplay_accessops p9100_accessops = {
239 p9100_ioctl,
240 p9100_mmap,
241 NULL, /* vcons_alloc_screen */
242 NULL, /* vcons_free_screen */
243 NULL, /* vcons_show_screen */
244 NULL, /* load_font */
245 NULL, /* polls */
246 NULL, /* scroll */
247 };
248 #endif
249
250 #define PNOZZ_LATCH(sc, off) if(sc->sc_last_offset == (off & 0xffffff80)) { \
251 sc->sc_junk = bus_space_read_4(sc->sc_bustag, sc->sc_fb_memh, \
252 off); \
253 sc->sc_last_offset = off & 0xffffff80; }
254
255 /*
256 * Match a p9100.
257 */
258 static int
259 p9100_sbus_match(struct device *parent, struct cfdata *cf, void *aux)
260 {
261 struct sbus_attach_args *sa = aux;
262
263 return (strcmp("p9100", sa->sa_name) == 0);
264 }
265
266
267 /*
268 * Attach a display. We need to notice if it is the console, too.
269 */
270 static void
271 p9100_sbus_attach(struct device *parent, struct device *self, void *args)
272 {
273 struct p9100_softc *sc = (struct p9100_softc *)self;
274 struct sbus_attach_args *sa = args;
275 struct fbdevice *fb = &sc->sc_fb;
276 int isconsole;
277 int node;
278 int i, j;
279 uint8_t ver;
280
281 #if NWSDISPLAY > 0
282 struct wsemuldisplaydev_attach_args aa;
283 struct rasops_info *ri;
284 unsigned long defattr;
285 #endif
286
287 sc->sc_last_offset = 0xffffffff;
288
289 /* Remember cookies for p9100_mmap() */
290 sc->sc_bustag = sa->sa_bustag;
291 sc->sc_ctl_paddr = sbus_bus_addr(sa->sa_bustag,
292 sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base);
293 sc->sc_ctl_psize = 0x8000;/*(bus_size_t)sa->sa_reg[0].oa_size;*/
294
295 sc->sc_cmd_paddr = sbus_bus_addr(sa->sa_bustag,
296 sa->sa_reg[1].oa_space, sa->sa_reg[1].oa_base);
297 sc->sc_cmd_psize = (bus_size_t)sa->sa_reg[1].oa_size;
298
299 sc->sc_fb_paddr = sbus_bus_addr(sa->sa_bustag,
300 sa->sa_reg[2].oa_space, sa->sa_reg[2].oa_base);
301 sc->sc_fb_psize = (bus_size_t)sa->sa_reg[2].oa_size;
302
303 fb->fb_driver = &p9100fbdriver;
304 fb->fb_device = &sc->sc_dev;
305 fb->fb_flags = device_cfdata(&sc->sc_dev)->cf_flags & FB_USERMASK;
306 #ifdef PNOZZ_EMUL_CG3
307 fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
308 #else
309 fb->fb_type.fb_type = FBTYPE_P9100;
310 #endif
311 fb->fb_pixels = NULL;
312
313 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
314
315 node = sa->sa_node;
316 isconsole = fb_is_console(node);
317 if (!isconsole) {
318 aprint_normal("\n");
319 aprint_error_dev(self, "fatal error: PROM didn't configure device\n");
320 return;
321 }
322
323 /*
324 * When the ROM has mapped in a p9100 display, the address
325 * maps only the video RAM, so in any case we have to map the
326 * registers ourselves. We only need the video RAM if we are
327 * going to print characters via rconsole.
328 */
329 if (sbus_bus_map(sc->sc_bustag,
330 sa->sa_reg[0].oa_space,
331 sa->sa_reg[0].oa_base,
332 /*
333 * XXX for some reason the SBus resources don't cover
334 * all registers, so we just map what we need
335 */
336 /*sc->sc_ctl_psize*/ 0x8000,
337 /*BUS_SPACE_MAP_LINEAR*/0, &sc->sc_ctl_memh) != 0) {
338 aprint_error_dev(self, "cannot map control registers\n");
339 return;
340 }
341
342 if (sa->sa_npromvaddrs != 0)
343 fb->fb_pixels = (void *)sa->sa_promvaddrs[0];
344
345 if (fb->fb_pixels == NULL) {
346 if (sbus_bus_map(sc->sc_bustag,
347 sa->sa_reg[2].oa_space,
348 sa->sa_reg[2].oa_base,
349 sc->sc_fb_psize,
350 BUS_SPACE_MAP_LINEAR, &sc->sc_fb_memh) != 0) {
351 aprint_error_dev(self, "cannot map framebuffer\n");
352 return;
353 }
354 fb->fb_pixels = (char *)sc->sc_fb_memh;
355 } else {
356 sc->sc_fb_memh = (bus_space_handle_t) fb->fb_pixels;
357 }
358
359 i = p9100_ctl_read_4(sc, 0x0004);
360 switch ((i >> 26) & 7) {
361 case 5: fb->fb_type.fb_depth = 32; break;
362 case 7: fb->fb_type.fb_depth = 24; break;
363 case 3: fb->fb_type.fb_depth = 16; break;
364 case 2: fb->fb_type.fb_depth = 8; break;
365 default: {
366 panic("pnozz: can't determine screen depth (0x%02x)", i);
367 }
368 }
369 sc->sc_depth = (fb->fb_type.fb_depth >> 3);
370
371 /* XXX for some reason I get a kernel trap with this */
372 sc->sc_width = prom_getpropint(node, "width", 800);
373 sc->sc_height = prom_getpropint(node, "height", 600);
374
375 sc->sc_stride = prom_getpropint(node, "linebytes", sc->sc_width *
376 (fb->fb_type.fb_depth >> 3));
377
378 /* check the RAMDAC */
379 ver = p9100_ramdac_read_ctl(sc, DAC_VERSION);
380
381 p9100_init_engine(sc);
382
383 fb_setsize_obp(fb, fb->fb_type.fb_depth, sc->sc_width, sc->sc_height,
384 node);
385
386 sbus_establish(&sc->sc_sd, &sc->sc_dev);
387 bus_intr_establish(sc->sc_bustag, sa->sa_pri, IPL_BIO,
388 p9100_intr, sc);
389
390 fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
391 printf(": rev %d / %x, %dx%d, depth %d mem %x",
392 (i & 7), ver, fb->fb_type.fb_width, fb->fb_type.fb_height,
393 fb->fb_type.fb_depth, (unsigned int)sc->sc_fb_psize);
394
395 fb->fb_type.fb_cmsize = prom_getpropint(node, "cmsize", 256);
396 if ((1 << fb->fb_type.fb_depth) != fb->fb_type.fb_cmsize)
397 printf(", %d entry colormap", fb->fb_type.fb_cmsize);
398
399 /* Initialize the default color map. */
400 /*bt_initcmap(&sc->sc_cmap, 256);*/
401 j = 0;
402 for (i = 0; i < 256; i++) {
403 sc->sc_cmap.cm_map[i][0] = rasops_cmap[j];
404 j++;
405 sc->sc_cmap.cm_map[i][1] = rasops_cmap[j];
406 j++;
407 sc->sc_cmap.cm_map[i][2] = rasops_cmap[j];
408 j++;
409 }
410 p9100loadcmap(sc, 0, 256);
411
412 /* make sure we are not blanked */
413 if (isconsole)
414 p9100_set_video(sc, 1);
415
416 if (shutdownhook_establish(p9100_shutdown, sc) == NULL) {
417 panic("%s: could not establish shutdown hook",
418 device_xname(&sc->sc_dev));
419 }
420
421 if (isconsole) {
422 printf(" (console)\n");
423 #ifdef RASTERCONSOLE
424 /*p9100loadcmap(sc, 255, 1);*/
425 fbrcons_init(fb);
426 #endif
427 } else
428 printf("\n");
429
430 #if NWSDISPLAY > 0
431 wsfont_init();
432
433 vcons_init(&sc->vd, sc, &p9100_defscreendesc, &p9100_accessops);
434 sc->vd.init_screen = p9100_init_screen;
435
436 vcons_init_screen(&sc->vd, &p9100_console_screen, 1, &defattr);
437 p9100_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
438
439 sc->sc_bg = (defattr >> 16) & 0xff;
440 p9100_clearscreen(sc);
441
442 ri = &p9100_console_screen.scr_ri;
443
444 p9100_defscreendesc.nrows = ri->ri_rows;
445 p9100_defscreendesc.ncols = ri->ri_cols;
446 p9100_defscreendesc.textops = &ri->ri_ops;
447 p9100_defscreendesc.capabilities = ri->ri_caps;
448
449 if(isconsole) {
450 wsdisplay_cnattach(&p9100_defscreendesc, ri, 0, 0, defattr);
451 }
452
453 aa.console = isconsole;
454 aa.scrdata = &p9100_screenlist;
455 aa.accessops = &p9100_accessops;
456 aa.accesscookie = &sc->vd;
457
458 config_found(self, &aa, wsemuldisplaydevprint);
459 #endif
460 /* cursor sprite handling */
461 p9100_init_cursor(sc);
462
463 /* attach the fb */
464 fb_attach(fb, isconsole);
465
466 /* register with power management */
467 sc->sc_video = 1;
468 sc->sc_powerstate = PWR_RESUME;
469 powerhook_establish(device_xname(&sc->sc_dev), p9100_power_hook, sc);
470
471 #if NTCTRL > 0
472 /* register callback for external monitor status change */
473 tadpole_register_callback(p9100_set_extvga, sc);
474 #endif
475 }
476
477 static void
478 p9100_shutdown(arg)
479 void *arg;
480 {
481 struct p9100_softc *sc = arg;
482
483 #ifdef RASTERCONSOLE
484 sc->sc_cmap.cm_map[0][0] = 0xff;
485 sc->sc_cmap.cm_map[0][1] = 0xff;
486 sc->sc_cmap.cm_map[0][2] = 0xff;
487 sc->sc_cmap.cm_map[1][0] = 0;
488 sc->sc_cmap.cm_map[1][1] = 0;
489 sc->sc_cmap.cm_map[1][2] = 0x00;
490 p9100loadcmap(sc, 0, 2);
491 sc->sc_cmap.cm_map[255][0] = 0;
492 sc->sc_cmap.cm_map[255][1] = 0;
493 sc->sc_cmap.cm_map[255][2] = 0;
494 p9100loadcmap(sc, 255, 1);
495 #endif
496 p9100_set_video(sc, 1);
497 }
498
499 int
500 p9100open(dev_t dev, int flags, int mode, struct lwp *l)
501 {
502 int unit = minor(dev);
503
504 if (unit >= pnozz_cd.cd_ndevs || pnozz_cd.cd_devs[unit] == NULL)
505 return (ENXIO);
506 return (0);
507 }
508
509 int
510 p9100ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
511 {
512 struct p9100_softc *sc = pnozz_cd.cd_devs[minor(dev)];
513 struct fbgattr *fba;
514 int error, v;
515
516 switch (cmd) {
517
518 case FBIOGTYPE:
519 *(struct fbtype *)data = sc->sc_fb.fb_type;
520 break;
521
522 case FBIOGATTR:
523 fba = (struct fbgattr *)data;
524 fba->real_type = sc->sc_fb.fb_type.fb_type;
525 fba->owner = 0; /* XXX ??? */
526 fba->fbtype = sc->sc_fb.fb_type;
527 fba->sattr.flags = 0;
528 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
529 fba->sattr.dev_specific[0] = -1;
530 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
531 fba->emu_types[1] = -1;
532 break;
533
534 case FBIOGETCMAP:
535 #define p ((struct fbcmap *)data)
536 return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
537
538 case FBIOPUTCMAP:
539 /* copy to software map */
540 error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
541 if (error)
542 return (error);
543 /* now blast them into the chip */
544 /* XXX should use retrace interrupt */
545 p9100loadcmap(sc, p->index, p->count);
546 #undef p
547 break;
548
549 case FBIOGVIDEO:
550 *(int *)data = p9100_get_video(sc);
551 break;
552
553 case FBIOSVIDEO:
554 p9100_set_video(sc, *(int *)data);
555 break;
556
557 /* these are for both FBIOSCURSOR and FBIOGCURSOR */
558 #define p ((struct fbcursor *)data)
559 #define pc (&sc->sc_cursor)
560
561 case FBIOGCURSOR:
562 p->set = FB_CUR_SETALL; /* close enough, anyway */
563 p->enable = pc->pc_enable;
564 p->pos = pc->pc_pos;
565 p->hot = pc->pc_hot;
566 p->size = pc->pc_size;
567
568 if (p->image != NULL) {
569 error = copyout(pc->pc_bits, p->image, 0x200);
570 if (error)
571 return error;
572 error = copyout(&pc->pc_bits[0x80], p->mask, 0x200);
573 if (error)
574 return error;
575 }
576
577 p->cmap.index = 0;
578 p->cmap.count = 3;
579 if (p->cmap.red != NULL) {
580 copyout(pc->red, p->cmap.red, 3);
581 copyout(pc->green, p->cmap.green, 3);
582 copyout(pc->blue, p->cmap.blue, 3);
583 }
584 break;
585
586 case FBIOSCURSOR:
587 {
588 int count;
589 uint32_t image[0x80], mask[0x80];
590 uint8_t red[3], green[3], blue[3];
591
592 v = p->set;
593 if (v & FB_CUR_SETCMAP) {
594 error = copyin(p->cmap.red, red, 3);
595 error |= copyin(p->cmap.green, green, 3);
596 error |= copyin(p->cmap.blue, blue, 3);
597 if (error)
598 return error;
599 }
600 if (v & FB_CUR_SETSHAPE) {
601 if (p->size.x > 64 || p->size.y > 64)
602 return EINVAL;
603 memset(&mask, 0, 0x200);
604 memset(&image, 0, 0x200);
605 count = p->size.y * 8;
606 error = copyin(p->image, image, count);
607 if (error)
608 return error;
609 error = copyin(p->mask, mask, count);
610 if (error)
611 return error;
612 }
613
614 /* parameters are OK; do it */
615 if (v & (FB_CUR_SETCUR | FB_CUR_SETPOS | FB_CUR_SETHOT)) {
616 if (v & FB_CUR_SETCUR)
617 pc->pc_enable = p->enable;
618 if (v & FB_CUR_SETPOS)
619 pc->pc_pos = p->pos;
620 if (v & FB_CUR_SETHOT)
621 pc->pc_hot = p->hot;
622 p9100_set_fbcursor(sc);
623 }
624
625 if (v & FB_CUR_SETCMAP) {
626 memcpy(pc->red, red, 3);
627 memcpy(pc->green, green, 3);
628 memcpy(pc->blue, blue, 3);
629 p9100_setcursorcmap(sc);
630 }
631
632 if (v & FB_CUR_SETSHAPE) {
633 memcpy(pc->pc_bits, image, 0x200);
634 memcpy(&pc->pc_bits[0x80], mask, 0x200);
635 p9100_loadcursor(sc);
636 }
637 }
638 break;
639
640 #undef p
641 #undef cc
642
643 case FBIOGCURPOS:
644 *(struct fbcurpos *)data = sc->sc_cursor.pc_pos;
645 break;
646
647 case FBIOSCURPOS:
648 sc->sc_cursor.pc_pos = *(struct fbcurpos *)data;
649 p9100_set_fbcursor(sc);
650 break;
651
652 case FBIOGCURMAX:
653 /* max cursor size is 64x64 */
654 ((struct fbcurpos *)data)->x = 64;
655 ((struct fbcurpos *)data)->y = 64;
656 break;
657
658 default:
659 return (ENOTTY);
660 }
661 return (0);
662 }
663
664 static uint32_t
665 p9100_ctl_read_4(struct p9100_softc *sc, bus_size_t off)
666 {
667
668 PNOZZ_LATCH(sc, off);
669 return bus_space_read_4(sc->sc_bustag, sc->sc_ctl_memh, off);
670 }
671
672 static void
673 p9100_ctl_write_4(struct p9100_softc *sc, bus_size_t off, uint32_t v)
674 {
675
676 PNOZZ_LATCH(sc, off);
677 bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh, off, v);
678 }
679
680 /* initialize the drawing engine */
681 static void
682 p9100_init_engine(struct p9100_softc *sc)
683 {
684 /* reset clipping rectangles */
685 uint32_t rmax = ((sc->sc_width & 0x3fff) << 16) |
686 (sc->sc_height & 0x3fff);
687
688 sc->sc_last_offset = 0xffffffff;
689
690 p9100_ctl_write_4(sc, WINDOW_OFFSET, 0);
691 p9100_ctl_write_4(sc, WINDOW_MIN, 0);
692 p9100_ctl_write_4(sc, WINDOW_MAX, rmax);
693 p9100_ctl_write_4(sc, BYTE_CLIP_MIN, 0);
694 p9100_ctl_write_4(sc, BYTE_CLIP_MAX, rmax);
695 p9100_ctl_write_4(sc, DRAW_MODE, 0);
696 p9100_ctl_write_4(sc, PLANE_MASK, 0xffffffff);
697 p9100_ctl_write_4(sc, PATTERN0, 0xffffffff);
698 p9100_ctl_write_4(sc, PATTERN1, 0xffffffff);
699 p9100_ctl_write_4(sc, PATTERN2, 0xffffffff);
700 p9100_ctl_write_4(sc, PATTERN3, 0xffffffff);
701 }
702
703 /* we only need these in the wsdisplay case */
704 #if NWSDISPLAY > 0
705
706 /* wait until the engine is idle */
707 static void
708 p9100_sync(struct p9100_softc *sc)
709 {
710 while((p9100_ctl_read_4(sc, ENGINE_STATUS) &
711 (ENGINE_BUSY | BLITTER_BUSY)) != 0);
712 }
713
714 static void
715 p9100_set_color_reg(struct p9100_softc *sc, int reg, int32_t col)
716 {
717 uint32_t out;
718
719 switch(sc->sc_depth)
720 {
721 case 1: /* 8 bit */
722 out = (col << 8) | col;
723 out |= out << 16;
724 break;
725 case 2: /* 16 bit */
726 out = col | (col << 16);
727 break;
728 default:
729 out = col;
730 }
731 p9100_ctl_write_4(sc, reg, out);
732 }
733
734 /* screen-to-screen blit */
735 static void
736 p9100_bitblt(void *cookie, int xs, int ys, int xd, int yd, int wi,
737 int he, uint32_t rop)
738 {
739 struct p9100_softc *sc = cookie;
740 uint32_t src, dst, srcw, dstw;
741
742 sc->sc_last_offset = 0xffffffff;
743
744 src = ((xs & 0x3fff) << 16) | (ys & 0x3fff);
745 dst = ((xd & 0x3fff) << 16) | (yd & 0x3fff);
746 srcw = (((xs + wi - 1) & 0x3fff) << 16) | ((ys + he - 1) & 0x3fff);
747 dstw = (((xd + wi - 1) & 0x3fff) << 16) | ((yd + he - 1) & 0x3fff);
748 p9100_sync(sc);
749 p9100_ctl_write_4(sc, RASTER_OP, rop);
750
751 p9100_ctl_write_4(sc, ABS_XY0, src);
752
753 p9100_ctl_write_4(sc, ABS_XY1, srcw);
754 p9100_ctl_write_4(sc, ABS_XY2, dst);
755 p9100_ctl_write_4(sc, ABS_XY3, dstw);
756 sc->sc_junk = p9100_ctl_read_4(sc, COMMAND_BLIT);
757 }
758
759 /* solid rectangle fill */
760 static void
761 p9100_rectfill(void *cookie, int xs, int ys, int wi, int he, uint32_t col)
762 {
763 struct p9100_softc *sc = cookie;
764 uint32_t src, srcw;
765
766 sc->sc_last_offset = 0xffffffff;
767
768 src = ((xs & 0x3fff) << 16) | (ys & 0x3fff);
769 srcw = (((xs + wi) & 0x3fff) << 16) | ((ys + he) & 0x3fff);
770 p9100_sync(sc);
771 p9100_set_color_reg(sc, FOREGROUND_COLOR, col);
772 p9100_set_color_reg(sc, BACKGROUND_COLOR, col);
773 p9100_ctl_write_4(sc, RASTER_OP, ROP_PAT);
774 p9100_ctl_write_4(sc, COORD_INDEX, 0);
775 p9100_ctl_write_4(sc, RECT_RTW_XY, src);
776 p9100_ctl_write_4(sc, RECT_RTW_XY, srcw);
777 sc->sc_junk = p9100_ctl_read_4(sc, COMMAND_QUAD);
778 }
779
780 /* setup for mono->colour expansion */
781 static void
782 p9100_setup_mono(struct p9100_softc *sc, int x, int y, int wi, int he,
783 uint32_t fg, uint32_t bg)
784 {
785
786 sc->sc_last_offset = 0xffffffff;
787
788 p9100_sync(sc);
789 /*
790 * this doesn't make any sense to me either, but for some reason the
791 * chip applies the foreground colour to 0 pixels
792 */
793
794 p9100_set_color_reg(sc,FOREGROUND_COLOR,bg);
795 p9100_set_color_reg(sc,BACKGROUND_COLOR,fg);
796
797 p9100_ctl_write_4(sc, RASTER_OP, ROP_SRC);
798 p9100_ctl_write_4(sc, ABS_X0, x);
799 p9100_ctl_write_4(sc, ABS_XY1, (x << 16) | (y & 0xFFFFL));
800 p9100_ctl_write_4(sc, ABS_X2, (x + wi));
801 p9100_ctl_write_4(sc, ABS_Y3, he);
802 /* now feed the data into the chip */
803 sc->sc_mono_width = wi;
804 }
805
806 /* write monochrome data to the screen through the blitter */
807 static void
808 p9100_feed_line(struct p9100_softc *sc, int count, uint8_t *data)
809 {
810 int i;
811 uint32_t latch = 0, bork;
812 int shift = 24;
813 int to_go = sc->sc_mono_width;
814
815 PNOZZ_LATCH(sc, PIXEL_1);
816
817 for (i = 0; i < count; i++) {
818 bork = data[i];
819 latch |= (bork << shift);
820 if (shift == 0) {
821 /* check how many bits are significant */
822 if (to_go > 31) {
823 bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh,
824 (PIXEL_1 + (31 << 2)), latch);
825 /*p9100_ctl_write_4(sc, (PIXEL_1 +
826 (31 << 2)), latch);*/
827 to_go -= 32;
828 } else
829 {
830 bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh,
831 (PIXEL_1 + ((to_go - 1) << 2)), latch);
832 /*p9100_ctl_write_4(sc, (PIXEL_1 +
833 ((to_go - 1) << 2)), latch);*/
834 to_go = 0;
835 }
836 latch = 0;
837 shift = 24;
838 } else
839 shift -= 8;
840 }
841 if (shift != 24)
842 p9100_ctl_write_4(sc, (PIXEL_1 + ((to_go - 1) << 2)), latch);
843 }
844
845 static void
846 p9100_clearscreen(struct p9100_softc *sc)
847 {
848
849 p9100_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, sc->sc_bg);
850 }
851 #endif /* NWSDISPLAY > 0 */
852
853 static uint8_t
854 p9100_ramdac_read(struct p9100_softc *sc, bus_size_t off)
855 {
856
857 sc->sc_junk = p9100_ctl_read_4(sc, PWRUP_CNFG);
858 return ((bus_space_read_4(sc->sc_bustag,
859 sc->sc_ctl_memh, off) >> 16) & 0xff);
860 }
861
862 static void
863 p9100_ramdac_write(struct p9100_softc *sc, bus_size_t off, uint8_t v)
864 {
865
866 sc->sc_junk = p9100_ctl_read_4(sc, PWRUP_CNFG);
867 bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh, off,
868 ((uint32_t)v) << 16);
869 }
870
871 static uint8_t
872 p9100_ramdac_read_ctl(struct p9100_softc *sc, int off)
873 {
874 p9100_ramdac_write(sc, DAC_INDX_LO, off & 0xff);
875 p9100_ramdac_write(sc, DAC_INDX_HI, (off & 0xff00) >> 8);
876 return p9100_ramdac_read(sc, DAC_INDX_DATA);
877 }
878
879 static void
880 p9100_ramdac_write_ctl(struct p9100_softc *sc, int off, uint8_t val)
881 {
882 p9100_ramdac_write(sc, DAC_INDX_LO, off & 0xff);
883 p9100_ramdac_write(sc, DAC_INDX_HI, (off & 0xff00) >> 8);
884 p9100_ramdac_write(sc, DAC_INDX_DATA, val);
885 }
886
887 /*
888 * Undo the effect of an FBIOSVIDEO that turns the video off.
889 */
890 static void
891 p9100unblank(struct device *dev)
892 {
893 struct p9100_softc *sc = (struct p9100_softc *)dev;
894
895 p9100_set_video((struct p9100_softc *)dev, 1);
896
897 /*
898 * Check if we're in terminal mode. If not force the console screen
899 * to front so we can see ddb, panic messages and so on
900 */
901 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL) {
902 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
903 if (sc->vd.active != &p9100_console_screen) {
904 SCREEN_INVISIBLE(sc->vd.active);
905 sc->vd.active = &p9100_console_screen;
906 SCREEN_VISIBLE(&p9100_console_screen);
907 }
908 vcons_redraw_screen(&p9100_console_screen);
909 }
910 }
911
912 static void
913 p9100_set_video(struct p9100_softc *sc, int enable)
914 {
915 u_int32_t v = p9100_ctl_read_4(sc, SCRN_RPNT_CTL_1);
916
917 if (enable)
918 v |= VIDEO_ENABLED;
919 else
920 v &= ~VIDEO_ENABLED;
921 p9100_ctl_write_4(sc, SCRN_RPNT_CTL_1, v);
922 #if NTCTRL > 0
923 /* Turn On/Off the TFT if we know how.
924 */
925 tadpole_set_video(enable);
926 #endif
927 }
928
929 static int
930 p9100_get_video(struct p9100_softc *sc)
931 {
932 return (p9100_ctl_read_4(sc, SCRN_RPNT_CTL_1) & VIDEO_ENABLED) != 0;
933 }
934
935 static void
936 p9100_power_hook(int why, void *cookie)
937 {
938 struct p9100_softc *sc = cookie;
939
940 if (why == sc->sc_powerstate)
941 return;
942
943 switch(why)
944 {
945 case PWR_SUSPEND:
946 case PWR_STANDBY:
947 sc->sc_video = p9100_get_video(sc);
948 p9100_set_video(sc, 0);
949 sc->sc_powerstate = why;
950 break;
951 case PWR_RESUME:
952 p9100_set_video(sc, sc->sc_video);
953 sc->sc_powerstate = why;
954 break;
955 }
956 }
957
958 /*
959 * Load a subset of the current (new) colormap into the IBM RAMDAC.
960 */
961 static void
962 p9100loadcmap(struct p9100_softc *sc, int start, int ncolors)
963 {
964 int i;
965
966 sc->sc_last_offset = 0xffffffff;
967
968 p9100_ramdac_write(sc, DAC_CMAP_WRIDX, start);
969
970 for (i=0;i<ncolors;i++) {
971 p9100_ramdac_write(sc, DAC_CMAP_DATA,
972 sc->sc_cmap.cm_map[i + start][0]);
973 p9100_ramdac_write(sc, DAC_CMAP_DATA,
974 sc->sc_cmap.cm_map[i + start][1]);
975 p9100_ramdac_write(sc, DAC_CMAP_DATA,
976 sc->sc_cmap.cm_map[i + start][2]);
977 }
978 }
979
980 /*
981 * Return the address that would map the given device at the given
982 * offset, allowing for the given protection, or return -1 for error.
983 */
984 static paddr_t
985 p9100mmap(dev_t dev, off_t off, int prot)
986 {
987 struct p9100_softc *sc = pnozz_cd.cd_devs[minor(dev)];
988
989 if (off & PGOFSET)
990 panic("p9100mmap");
991 if (off < 0)
992 return (-1);
993
994 #ifdef PNOZZ_EMUL_CG3
995 #define CG3_MMAP_OFFSET 0x04000000
996 /* Make Xsun think we are a CG3 (SUN3COLOR)
997 */
998 if (off >= CG3_MMAP_OFFSET && off < CG3_MMAP_OFFSET + sc->sc_fb_psize) {
999 off -= CG3_MMAP_OFFSET;
1000 return (bus_space_mmap(sc->sc_bustag,
1001 sc->sc_fb_paddr,
1002 off,
1003 prot,
1004 BUS_SPACE_MAP_LINEAR));
1005 }
1006 #endif
1007
1008 if (off >= sc->sc_fb_psize + sc->sc_ctl_psize + sc->sc_cmd_psize)
1009 return (-1);
1010
1011 if (off < sc->sc_fb_psize) {
1012 return (bus_space_mmap(sc->sc_bustag,
1013 sc->sc_fb_paddr,
1014 off,
1015 prot,
1016 BUS_SPACE_MAP_LINEAR));
1017 }
1018 off -= sc->sc_fb_psize;
1019 if (off < sc->sc_ctl_psize) {
1020 return (bus_space_mmap(sc->sc_bustag,
1021 sc->sc_ctl_paddr,
1022 off,
1023 prot,
1024 BUS_SPACE_MAP_LINEAR));
1025 }
1026 off -= sc->sc_ctl_psize;
1027
1028 return (bus_space_mmap(sc->sc_bustag,
1029 sc->sc_cmd_paddr,
1030 off,
1031 prot,
1032 BUS_SPACE_MAP_LINEAR));
1033 }
1034
1035 /* wscons stuff */
1036 #if NWSDISPLAY > 0
1037
1038 static void
1039 p9100_cursor(void *cookie, int on, int row, int col)
1040 {
1041 struct rasops_info *ri = cookie;
1042 struct vcons_screen *scr = ri->ri_hw;
1043 struct p9100_softc *sc = scr->scr_cookie;
1044 int x, y, wi,he;
1045
1046 wi = ri->ri_font->fontwidth;
1047 he = ri->ri_font->fontheight;
1048
1049 if (ri->ri_flg & RI_CURSOR) {
1050 x = ri->ri_ccol * wi + ri->ri_xorigin;
1051 y = ri->ri_crow * he + ri->ri_yorigin;
1052 p9100_bitblt(sc, x, y, x, y, wi, he, ROP_SRC ^ 0xff);
1053 ri->ri_flg &= ~RI_CURSOR;
1054 }
1055 ri->ri_crow = row;
1056 ri->ri_ccol = col;
1057 if (on)
1058 {
1059 x = ri->ri_ccol * wi + ri->ri_xorigin;
1060 y = ri->ri_crow * he + ri->ri_yorigin;
1061 p9100_bitblt(sc, x, y, x, y, wi, he, ROP_SRC ^ 0xff);
1062 ri->ri_flg |= RI_CURSOR;
1063 }
1064 }
1065
1066 #if 0
1067 static int
1068 p9100_mapchar(void *cookie, int uni, u_int *index)
1069 {
1070 return 0;
1071 }
1072 #endif
1073
1074 static void
1075 p9100_putchar(void *cookie, int row, int col, u_int c, long attr)
1076 {
1077 struct rasops_info *ri = cookie;
1078 struct vcons_screen *scr = ri->ri_hw;
1079 struct p9100_softc *sc = scr->scr_cookie;
1080
1081 int fg, bg, uc, i;
1082 uint8_t *data;
1083 int x, y, wi,he;
1084
1085 wi = ri->ri_font->fontwidth;
1086 he = ri->ri_font->fontheight;
1087
1088 if (!CHAR_IN_FONT(c, ri->ri_font))
1089 return;
1090 bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xff];
1091 fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xff];
1092 x = ri->ri_xorigin + col * wi;
1093 y = ri->ri_yorigin + row * he;
1094 if (c == 0x20) {
1095 p9100_rectfill(sc, x, y, wi, he, bg);
1096 } else {
1097 uc = c-ri->ri_font->firstchar;
1098 data = (uint8_t *)ri->ri_font->data + uc *
1099 ri->ri_fontscale;
1100
1101 p9100_setup_mono(sc, x, y, wi, 1, fg, bg);
1102 for (i = 0; i < he; i++) {
1103 p9100_feed_line(sc, ri->ri_font->stride,
1104 data);
1105 data += ri->ri_font->stride;
1106 }
1107 /*p9100_sync(sc);*/
1108 }
1109 }
1110
1111 /*
1112 * wsdisplay_accessops
1113 */
1114
1115 int
1116 p9100_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
1117 struct lwp *l)
1118 {
1119 struct vcons_data *vd = v;
1120 struct p9100_softc *sc = vd->cookie;
1121 struct wsdisplay_fbinfo *wdf;
1122 struct vcons_screen *ms = vd->active;
1123
1124 switch (cmd) {
1125 case WSDISPLAYIO_GTYPE:
1126 *(u_int *)data = WSDISPLAY_TYPE_SB_P9100;
1127 return 0;
1128
1129 case FBIOGVIDEO:
1130 case WSDISPLAYIO_GVIDEO:
1131 *(int *)data = p9100_get_video(sc);
1132 return 0;
1133
1134 case WSDISPLAYIO_SVIDEO:
1135 case FBIOSVIDEO:
1136 p9100_set_video(sc, *(int *)data);
1137 return 0;
1138
1139 case WSDISPLAYIO_GINFO:
1140 wdf = (void *)data;
1141 wdf->height = ms->scr_ri.ri_height;
1142 wdf->width = ms->scr_ri.ri_width;
1143 wdf->depth = ms->scr_ri.ri_depth;
1144 wdf->cmsize = 256;
1145 return 0;
1146
1147 case WSDISPLAYIO_GETCMAP:
1148 return p9100_getcmap(sc, (struct wsdisplay_cmap *)data);
1149
1150 case WSDISPLAYIO_PUTCMAP:
1151 return p9100_putcmap(sc, (struct wsdisplay_cmap *)data);
1152
1153 case WSDISPLAYIO_SMODE:
1154 {
1155 int new_mode = *(int*)data;
1156 if (new_mode != sc->sc_mode)
1157 {
1158 sc->sc_mode = new_mode;
1159 if (new_mode == WSDISPLAYIO_MODE_EMUL)
1160 {
1161 p9100_init_engine(sc);
1162 p9100loadcmap(sc, 0, 256);
1163 p9100_clearscreen(sc);
1164 vcons_redraw_screen(ms);
1165 }
1166 }
1167 }
1168 }
1169 return EPASSTHROUGH;
1170 }
1171
1172 static paddr_t
1173 p9100_mmap(void *v, void *vs, off_t offset, int prot)
1174 {
1175 struct vcons_data *vd = v;
1176 struct p9100_softc *sc = vd->cookie;
1177 paddr_t pa;
1178
1179 /* 'regular' framebuffer mmap()ing */
1180 if (offset < sc->sc_fb_psize) {
1181 pa = bus_space_mmap(sc->sc_bustag, sc->sc_fb_paddr + offset, 0,
1182 prot, BUS_SPACE_MAP_LINEAR);
1183 return pa;
1184 }
1185
1186 if ((offset >= sc->sc_fb_paddr) && (offset < (sc->sc_fb_paddr +
1187 sc->sc_fb_psize))) {
1188 pa = bus_space_mmap(sc->sc_bustag, offset, 0, prot,
1189 BUS_SPACE_MAP_LINEAR);
1190 return pa;
1191 }
1192
1193 if ((offset >= sc->sc_ctl_paddr) && (offset < (sc->sc_ctl_paddr +
1194 sc->sc_ctl_psize))) {
1195 pa = bus_space_mmap(sc->sc_bustag, offset, 0, prot,
1196 BUS_SPACE_MAP_LINEAR);
1197 return pa;
1198 }
1199
1200 return -1;
1201 }
1202
1203 static void
1204 p9100_init_screen(void *cookie, struct vcons_screen *scr,
1205 int existing, long *defattr)
1206 {
1207 struct p9100_softc *sc = cookie;
1208 struct rasops_info *ri = &scr->scr_ri;
1209
1210 ri->ri_depth = sc->sc_depth << 3;
1211 ri->ri_width = sc->sc_width;
1212 ri->ri_height = sc->sc_height;
1213 ri->ri_stride = sc->sc_stride;
1214 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
1215
1216 ri->ri_bits = bus_space_vaddr(sc->sc_bustag, sc->sc_fb_memh);
1217
1218 #ifdef DEBUG_P9100
1219 printf("addr: %08lx\n",(ulong)ri->ri_bits);
1220 #endif
1221 rasops_init(ri, sc->sc_height/8, sc->sc_width/8);
1222 ri->ri_caps = WSSCREEN_WSCOLORS;
1223 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
1224 sc->sc_width / ri->ri_font->fontwidth);
1225
1226 /* enable acceleration */
1227 ri->ri_ops.cursor = p9100_cursor;
1228 ri->ri_ops.copyrows = p9100_copyrows;
1229 ri->ri_ops.eraserows = p9100_eraserows;
1230 ri->ri_ops.copycols = p9100_copycols;
1231 ri->ri_ops.erasecols = p9100_erasecols;
1232 ri->ri_ops.putchar = p9100_putchar;
1233 ri->ri_ops.allocattr = p9100_allocattr;
1234 }
1235
1236 static int
1237 p9100_putcmap(struct p9100_softc *sc, struct wsdisplay_cmap *cm)
1238 {
1239 u_int index = cm->index;
1240 u_int count = cm->count;
1241 int i, error;
1242 u_char rbuf[256], gbuf[256], bbuf[256];
1243 u_char *r, *g, *b;
1244
1245 if (cm->index >= 256 || cm->count > 256 ||
1246 (cm->index + cm->count) > 256)
1247 return EINVAL;
1248 error = copyin(cm->red, &rbuf[index], count);
1249 if (error)
1250 return error;
1251 error = copyin(cm->green, &gbuf[index], count);
1252 if (error)
1253 return error;
1254 error = copyin(cm->blue, &bbuf[index], count);
1255 if (error)
1256 return error;
1257
1258 r = &rbuf[index];
1259 g = &gbuf[index];
1260 b = &bbuf[index];
1261
1262 for (i = 0; i < count; i++) {
1263 sc->sc_cmap.cm_map[index][0] = *r;
1264 sc->sc_cmap.cm_map[index][1] = *g;
1265 sc->sc_cmap.cm_map[index][2] = *b;
1266 index++;
1267 r++, g++, b++;
1268 }
1269 p9100loadcmap(sc, 0, 256);
1270 return 0;
1271 }
1272
1273 static int
1274 p9100_getcmap(struct p9100_softc *sc, struct wsdisplay_cmap *cm)
1275 {
1276 u_int index = cm->index;
1277 u_int count = cm->count;
1278 int error, i;
1279 uint8_t red[256], green[256], blue[256];
1280
1281 if (index >= 255 || count > 256 || index + count > 256)
1282 return EINVAL;
1283
1284 i = index;
1285 while (i < (index + count)) {
1286 red[i] = sc->sc_cmap.cm_map[i][0];
1287 green[i] = sc->sc_cmap.cm_map[i][1];
1288 blue[i] = sc->sc_cmap.cm_map[i][2];
1289 i++;
1290 }
1291 error = copyout(&red[index], cm->red, count);
1292 if (error)
1293 return error;
1294 error = copyout(&green[index], cm->green, count);
1295 if (error)
1296 return error;
1297 error = copyout(&blue[index], cm->blue, count);
1298 if (error)
1299 return error;
1300
1301 return 0;
1302 }
1303
1304 static void
1305 p9100_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1306 {
1307 struct rasops_info *ri = cookie;
1308 struct vcons_screen *scr = ri->ri_hw;
1309 int32_t xs, xd, y, width, height;
1310
1311 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1312 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1313 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1314 width = ri->ri_font->fontwidth * ncols;
1315 height = ri->ri_font->fontheight;
1316 p9100_bitblt(scr->scr_cookie, xs, y, xd, y, width, height, ROP_SRC);
1317 }
1318
1319 static void
1320 p9100_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1321 {
1322 struct rasops_info *ri = cookie;
1323 struct vcons_screen *scr = ri->ri_hw;
1324 int32_t x, y, width, height, bg;
1325
1326 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1327 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1328 width = ri->ri_font->fontwidth * ncols;
1329 height = ri->ri_font->fontheight;
1330 bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
1331 p9100_rectfill(scr->scr_cookie, x, y, width, height, bg);
1332 }
1333
1334 static void
1335 p9100_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1336 {
1337 struct rasops_info *ri = cookie;
1338 struct vcons_screen *scr = ri->ri_hw;
1339 int32_t x, ys, yd, width, height;
1340
1341 x = ri->ri_xorigin;
1342 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1343 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1344 width = ri->ri_emuwidth;
1345 height = ri->ri_font->fontheight * nrows;
1346 p9100_bitblt(scr->scr_cookie, x, ys, x, yd, width, height, ROP_SRC);
1347 }
1348
1349 static void
1350 p9100_eraserows(void *cookie, int row, int nrows, long fillattr)
1351 {
1352 struct rasops_info *ri = cookie;
1353 struct vcons_screen *scr = ri->ri_hw;
1354 int32_t x, y, width, height, bg;
1355
1356 if ((row == 0) && (nrows == ri->ri_rows)) {
1357 x = y = 0;
1358 width = ri->ri_width;
1359 height = ri->ri_height;
1360 } else {
1361 x = ri->ri_xorigin;
1362 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1363 width = ri->ri_emuwidth;
1364 height = ri->ri_font->fontheight * nrows;
1365 }
1366 bg = (uint32_t)ri->ri_devcmap[(fillattr >> 16) & 0xff];
1367 p9100_rectfill(scr->scr_cookie, x, y, width, height, bg);
1368 }
1369
1370
1371 static int
1372 p9100_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
1373 {
1374 if ((fg == 0) && (bg == 0))
1375 {
1376 fg = WS_DEFAULT_FG;
1377 bg = WS_DEFAULT_BG;
1378 }
1379 if (flags & WSATTR_REVERSE) {
1380 *attrp = (bg & 0xff) << 24 | (fg & 0xff) << 16 |
1381 (flags & 0xff) << 8;
1382 } else
1383 *attrp = (fg & 0xff) << 24 | (bg & 0xff) << 16 |
1384 (flags & 0xff) << 8;
1385 return 0;
1386 }
1387
1388 #if 0
1389 static int
1390 p9100_load_font(void *v, void *cookie, struct wsdisplay_font *data)
1391 {
1392
1393 return 0;
1394 }
1395 #endif
1396
1397 #endif /* NWSDISPLAY > 0 */
1398
1399 static int
1400 p9100_intr(void *arg)
1401 {
1402 /*p9100_softc *sc=arg;
1403 printf(".");*/
1404 return 1;
1405 }
1406
1407 static void
1408 p9100_init_cursor(struct p9100_softc *sc)
1409 {
1410
1411 memset(&sc->sc_cursor, 0, sizeof(struct pnozz_cursor));
1412 sc->sc_cursor.pc_size.x = 64;
1413 sc->sc_cursor.pc_size.y = 64;
1414
1415 }
1416
1417 static void
1418 p9100_set_fbcursor(struct p9100_softc *sc)
1419 {
1420 #ifdef PNOZZ_PARANOID
1421 int s;
1422
1423 s = splhigh(); /* just in case... */
1424 #endif
1425 sc->sc_last_offset = 0xffffffff;
1426
1427 /* set position and hotspot */
1428 p9100_ramdac_write(sc, DAC_INDX_CTL, DAC_INDX_AUTOINCR);
1429 p9100_ramdac_write(sc, DAC_INDX_HI, 0);
1430 p9100_ramdac_write(sc, DAC_INDX_LO, DAC_CURSOR_CTL);
1431 if (sc->sc_cursor.pc_enable) {
1432 p9100_ramdac_write(sc, DAC_INDX_DATA, DAC_CURSOR_X11 |
1433 DAC_CURSOR_64);
1434 } else
1435 p9100_ramdac_write(sc, DAC_INDX_DATA, DAC_CURSOR_OFF);
1436 /* next two registers - x low, high, y low, high */
1437 p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_pos.x & 0xff);
1438 p9100_ramdac_write(sc, DAC_INDX_DATA, (sc->sc_cursor.pc_pos.x >> 8) &
1439 0xff);
1440 p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_pos.y & 0xff);
1441 p9100_ramdac_write(sc, DAC_INDX_DATA, (sc->sc_cursor.pc_pos.y >> 8) &
1442 0xff);
1443 /* hotspot */
1444 p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_hot.x & 0xff);
1445 p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.pc_hot.y & 0xff);
1446
1447 #ifdef PNOZZ_PARANOID
1448 splx(s);
1449 #endif
1450
1451 }
1452
1453 static void
1454 p9100_setcursorcmap(struct p9100_softc *sc)
1455 {
1456 int i;
1457
1458 #ifdef PNOZZ_PARANOID
1459 int s;
1460 s = splhigh(); /* just in case... */
1461 #endif
1462 sc->sc_last_offset = 0xffffffff;
1463
1464 /* set cursor colours */
1465 p9100_ramdac_write(sc, DAC_INDX_CTL, DAC_INDX_AUTOINCR);
1466 p9100_ramdac_write(sc, DAC_INDX_HI, 0);
1467 p9100_ramdac_write(sc, DAC_INDX_LO, DAC_CURSOR_COL_1);
1468
1469 for (i = 0; i < 3; i++) {
1470 p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.red[i]);
1471 p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.green[i]);
1472 p9100_ramdac_write(sc, DAC_INDX_DATA, sc->sc_cursor.blue[i]);
1473 }
1474
1475 #ifdef PNOZZ_PARANOID
1476 splx(s);
1477 #endif
1478 }
1479
1480 static void
1481 p9100_loadcursor(struct p9100_softc *sc)
1482 {
1483 uint32_t *image, *mask;
1484 uint32_t bit, bbit, im, ma;
1485 int i, j, k;
1486 uint8_t latch1, latch2;
1487
1488 #ifdef PNOZZ_PARANOID
1489 int s;
1490 s = splhigh(); /* just in case... */
1491 #endif
1492 sc->sc_last_offset = 0xffffffff;
1493
1494 /* set cursor shape */
1495 p9100_ramdac_write(sc, DAC_INDX_CTL, DAC_INDX_AUTOINCR);
1496 p9100_ramdac_write(sc, DAC_INDX_HI, 1);
1497 p9100_ramdac_write(sc, DAC_INDX_LO, 0);
1498
1499 image = sc->sc_cursor.pc_bits;
1500 mask = &sc->sc_cursor.pc_bits[0x80];
1501
1502 for (i = 0; i < 0x80; i++) {
1503 bit = 0x80000000;
1504 im = image[i];
1505 ma = mask[i];
1506 for (k = 0; k < 4; k++) {
1507 bbit = 0x1;
1508 latch1 = 0;
1509 for (j = 0; j < 4; j++) {
1510 if (im & bit)
1511 latch1 |= bbit;
1512 bbit <<= 1;
1513 if (ma & bit)
1514 latch1 |= bbit;
1515 bbit <<= 1;
1516 bit >>= 1;
1517 }
1518 bbit = 0x1;
1519 latch2 = 0;
1520 for (j = 0; j < 4; j++) {
1521 if (im & bit)
1522 latch2 |= bbit;
1523 bbit <<= 1;
1524 if (ma & bit)
1525 latch2 |= bbit;
1526 bbit <<= 1;
1527 bit >>= 1;
1528 }
1529 p9100_ramdac_write(sc, DAC_INDX_DATA, latch1);
1530 p9100_ramdac_write(sc, DAC_INDX_DATA, latch2);
1531 }
1532 }
1533 #ifdef DEBUG_CURSOR
1534 printf("image:\n");
1535 for (i=0;i<0x80;i+=2)
1536 printf("%08x %08x\n", image[i], image[i+1]);
1537 printf("mask:\n");
1538 for (i=0;i<0x80;i+=2)
1539 printf("%08x %08x\n", mask[i], mask[i+1]);
1540 #endif
1541 #ifdef PNOZZ_PARANOID
1542 splx(s);
1543 #endif
1544 }
1545
1546 static void
1547 p9100_set_extvga(void *cookie, int status)
1548 {
1549 struct p9100_softc *sc = cookie;
1550 #ifdef PNOZZ_PARANOID
1551 int s;
1552
1553 s = splhigh();
1554 #endif
1555 #ifdef DEBUG
1556 printf("%s: external VGA %s\n", device_xname(&sc->sc_dev),
1557 status ? "on" : "off");
1558 #endif
1559 sc->sc_last_offset = 0xffffffff;
1560
1561 if (status) {
1562 p9100_ramdac_write_ctl(sc, DAC_POWER,
1563 p9100_ramdac_read_ctl(sc, DAC_POWER) &
1564 ~DAC_POWER_IPWR_DISABLE);
1565 } else {
1566 p9100_ramdac_write_ctl(sc, DAC_POWER,
1567 p9100_ramdac_read_ctl(sc, DAC_POWER) |
1568 DAC_POWER_IPWR_DISABLE);
1569 }
1570 #ifdef PNOZZ_PARANOID
1571 splx(s);
1572 #endif
1573 }
1574