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