pm2fb.c revision 1.6.4.1 1 /* $NetBSD: pm2fb.c,v 1.6.4.1 2011/02/08 16:19:53 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 2009 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * A console driver for Permedia 2 graphics controllers
30 * tested on sparc64 only so far
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: pm2fb.c,v 1.6.4.1 2011/02/08 16:19:53 bouyer Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41 #include <sys/lwp.h>
42 #include <sys/kauth.h>
43
44 #include <dev/videomode/videomode.h>
45
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcidevs.h>
49 #include <dev/pci/pciio.h>
50 #include <dev/pci/pm2reg.h>
51
52 #include <dev/wscons/wsdisplayvar.h>
53 #include <dev/wscons/wsconsio.h>
54 #include <dev/wsfont/wsfont.h>
55 #include <dev/rasops/rasops.h>
56 #include <dev/wscons/wsdisplay_vconsvar.h>
57 #include <dev/pci/wsdisplay_pci.h>
58
59 #include <dev/i2c/i2cvar.h>
60
61 struct pm2fb_softc {
62 device_t sc_dev;
63
64 pci_chipset_tag_t sc_pc;
65 pcitag_t sc_pcitag;
66
67 bus_space_tag_t sc_memt;
68 bus_space_tag_t sc_iot;
69
70 bus_space_handle_t sc_regh;
71 bus_addr_t sc_fb, sc_reg;
72 bus_size_t sc_fbsize, sc_regsize;
73
74 int sc_width, sc_height, sc_depth, sc_stride;
75 int sc_locked;
76 struct vcons_screen sc_console_screen;
77 struct wsscreen_descr sc_defaultscreen_descr;
78 const struct wsscreen_descr *sc_screens[1];
79 struct wsscreen_list sc_screenlist;
80 struct vcons_data vd;
81 int sc_mode;
82 u_char sc_cmap_red[256];
83 u_char sc_cmap_green[256];
84 u_char sc_cmap_blue[256];
85 /* engine stuff */
86 uint32_t sc_pprod;
87 };
88
89 static int pm2fb_match(device_t, cfdata_t, void *);
90 static void pm2fb_attach(device_t, device_t, void *);
91
92 CFATTACH_DECL_NEW(pm2fb, sizeof(struct pm2fb_softc),
93 pm2fb_match, pm2fb_attach, NULL, NULL);
94
95 extern const u_char rasops_cmap[768];
96
97 static int pm2fb_ioctl(void *, void *, u_long, void *, int,
98 struct lwp *);
99 static paddr_t pm2fb_mmap(void *, void *, off_t, int);
100 static void pm2fb_init_screen(void *, struct vcons_screen *, int, long *);
101
102 static int pm2fb_putcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
103 static int pm2fb_getcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
104 static void pm2fb_restore_palette(struct pm2fb_softc *);
105 static int pm2fb_putpalreg(struct pm2fb_softc *, uint8_t, uint8_t,
106 uint8_t, uint8_t);
107
108 static void pm2fb_init(struct pm2fb_softc *);
109 static void pm2fb_flush_engine(struct pm2fb_softc *);
110 static void pm2fb_rectfill(struct pm2fb_softc *, int, int, int, int,
111 uint32_t);
112 static void pm2fb_bitblt(struct pm2fb_softc *, int, int, int, int, int,
113 int, int);
114
115 static void pm2fb_cursor(void *, int, int, int);
116 static void pm2fb_putchar(void *, int, int, u_int, long);
117 static void pm2fb_copycols(void *, int, int, int, int);
118 static void pm2fb_erasecols(void *, int, int, int, long);
119 static void pm2fb_copyrows(void *, int, int, int);
120 static void pm2fb_eraserows(void *, int, int, long);
121
122 struct wsdisplay_accessops pm2fb_accessops = {
123 pm2fb_ioctl,
124 pm2fb_mmap,
125 NULL, /* alloc_screen */
126 NULL, /* free_screen */
127 NULL, /* show_screen */
128 NULL, /* load_font */
129 NULL, /* pollc */
130 NULL /* scroll */
131 };
132
133 static inline void
134 pm2fb_wait(struct pm2fb_softc *sc, int slots)
135 {
136 uint32_t reg;
137
138 do {
139 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
140 PM2_INPUT_FIFO_SPACE);
141 } while (reg <= slots);
142 }
143
144 static void
145 pm2fb_flush_engine(struct pm2fb_softc *sc)
146 {
147
148 pm2fb_wait(sc, 2);
149
150 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_FILTER_MODE,
151 PM2FLT_PASS_SYNC);
152 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SYNC, 0);
153 do {
154 while (bus_space_read_4(sc->sc_memt, sc->sc_regh,
155 PM2_OUTPUT_FIFO_WORDS) == 0);
156 } while (bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_OUTPUT_FIFO) !=
157 0x188);
158 }
159
160 static int
161 pm2fb_match(device_t parent, cfdata_t match, void *aux)
162 {
163 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
164
165 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
166 return 0;
167 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3DLABS)
168 return 0;
169
170 /* only cards tested on so far - likely need a list */
171 if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2) ||
172 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2V))
173 return 100;
174 return (0);
175 }
176
177 static void
178 pm2fb_attach(device_t parent, device_t self, void *aux)
179 {
180 struct pm2fb_softc *sc = device_private(self);
181 struct pci_attach_args *pa = aux;
182 struct rasops_info *ri;
183 char devinfo[256];
184 struct wsemuldisplaydev_attach_args aa;
185 prop_dictionary_t dict;
186 unsigned long defattr;
187 bool is_console;
188 int i, j;
189 uint32_t flags;
190
191 sc->sc_pc = pa->pa_pc;
192 sc->sc_pcitag = pa->pa_tag;
193 sc->sc_memt = pa->pa_memt;
194 sc->sc_iot = pa->pa_iot;
195 sc->sc_dev = self;
196
197 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
198 aprint_normal(": %s\n", devinfo);
199
200 /* fill in parameters from properties */
201 dict = device_properties(self);
202 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
203 aprint_error("%s: no width property\n", device_xname(self));
204 return;
205 }
206 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
207 aprint_error("%s: no height property\n", device_xname(self));
208 return;
209 }
210 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
211 aprint_error("%s: no depth property\n", device_xname(self));
212 return;
213 }
214 /*
215 * don't look at the linebytes property - The Raptor firmware lies
216 * about it. Get it from width * depth >> 3 instead.
217 */
218 sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
219
220 prop_dictionary_get_bool(dict, "is_console", &is_console);
221
222 pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x14, PCI_MAPREG_TYPE_MEM,
223 &sc->sc_fb, &sc->sc_fbsize, &flags);
224
225 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
226 &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
227 aprint_error("%s: failed to map registers.\n",
228 device_xname(sc->sc_dev));
229 }
230
231 /*
232 * XXX yeah, casting the fb address to uint32_t is formally wrong
233 * but as far as I know there are no PM2 with 64bit BARs
234 */
235 aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
236 (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
237
238 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
239 "default",
240 0, 0,
241 NULL,
242 8, 16,
243 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
244 NULL
245 };
246 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
247 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
248 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
249 sc->sc_locked = 0;
250
251 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
252 &pm2fb_accessops);
253 sc->vd.init_screen = pm2fb_init_screen;
254
255 /* init engine here */
256 pm2fb_init(sc);
257
258 ri = &sc->sc_console_screen.scr_ri;
259
260 j = 0;
261 for (i = 0; i < (1 << sc->sc_depth); i++) {
262
263 sc->sc_cmap_red[i] = rasops_cmap[j];
264 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
265 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
266 pm2fb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
267 rasops_cmap[j + 2]);
268 j += 3;
269 }
270
271 if (is_console) {
272 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
273 &defattr);
274 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
275
276 pm2fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
277 ri->ri_devcmap[(defattr >> 16) & 0xff]);
278 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
279 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
280 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
281 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
282 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
283 defattr);
284 vcons_replay_msgbuf(&sc->sc_console_screen);
285 } else {
286 /*
287 * since we're not the console we can postpone the rest
288 * until someone actually allocates a screen for us
289 */
290 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
291 }
292
293 aa.console = is_console;
294 aa.scrdata = &sc->sc_screenlist;
295 aa.accessops = &pm2fb_accessops;
296 aa.accesscookie = &sc->vd;
297
298 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
299 }
300
301 static int
302 pm2fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
303 struct lwp *l)
304 {
305 struct vcons_data *vd = v;
306 struct pm2fb_softc *sc = vd->cookie;
307 struct wsdisplay_fbinfo *wdf;
308 struct vcons_screen *ms = vd->active;
309
310 switch (cmd) {
311 case WSDISPLAYIO_GTYPE:
312 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
313 return 0;
314
315 /* PCI config read/write passthrough. */
316 case PCI_IOC_CFGREAD:
317 case PCI_IOC_CFGWRITE:
318 return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
319 cmd, data, flag, l);
320
321 case WSDISPLAYIO_GET_BUSID:
322 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
323 sc->sc_pcitag, data);
324
325 case WSDISPLAYIO_GINFO:
326 if (ms == NULL)
327 return ENODEV;
328 wdf = (void *)data;
329 wdf->height = ms->scr_ri.ri_height;
330 wdf->width = ms->scr_ri.ri_width;
331 wdf->depth = ms->scr_ri.ri_depth;
332 wdf->cmsize = 256;
333 return 0;
334
335 case WSDISPLAYIO_GETCMAP:
336 return pm2fb_getcmap(sc,
337 (struct wsdisplay_cmap *)data);
338
339 case WSDISPLAYIO_PUTCMAP:
340 return pm2fb_putcmap(sc,
341 (struct wsdisplay_cmap *)data);
342
343 case WSDISPLAYIO_LINEBYTES:
344 *(u_int *)data = sc->sc_stride;
345 return 0;
346
347 case WSDISPLAYIO_SMODE: {
348 int new_mode = *(int*)data;
349 if (new_mode != sc->sc_mode) {
350 sc->sc_mode = new_mode;
351 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
352 pm2fb_restore_palette(sc);
353 vcons_redraw_screen(ms);
354 } else
355 pm2fb_flush_engine(sc);
356 }
357 }
358 return 0;
359 }
360 return EPASSTHROUGH;
361 }
362
363 static paddr_t
364 pm2fb_mmap(void *v, void *vs, off_t offset, int prot)
365 {
366 struct vcons_data *vd = v;
367 struct pm2fb_softc *sc = vd->cookie;
368 paddr_t pa;
369
370 /* 'regular' framebuffer mmap()ing */
371 if (offset < sc->sc_fbsize) {
372 pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
373 BUS_SPACE_MAP_LINEAR);
374 return pa;
375 }
376
377 /*
378 * restrict all other mappings to processes with superuser privileges
379 * or the kernel itself
380 */
381 if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
382 NULL) != 0) {
383 aprint_normal("%s: mmap() rejected.\n",
384 device_xname(sc->sc_dev));
385 return -1;
386 }
387
388 if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
389 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
390 BUS_SPACE_MAP_LINEAR);
391 return pa;
392 }
393
394 if ((offset >= sc->sc_reg) &&
395 (offset < (sc->sc_reg + sc->sc_regsize))) {
396 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
397 BUS_SPACE_MAP_LINEAR);
398 return pa;
399 }
400
401 #ifdef PCI_MAGIC_IO_RANGE
402 /* allow mapping of IO space */
403 if ((offset >= PCI_MAGIC_IO_RANGE) &&
404 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
405 pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
406 0, prot, BUS_SPACE_MAP_LINEAR);
407 return pa;
408 }
409 #endif
410
411 return -1;
412 }
413
414 static void
415 pm2fb_init_screen(void *cookie, struct vcons_screen *scr,
416 int existing, long *defattr)
417 {
418 struct pm2fb_softc *sc = cookie;
419 struct rasops_info *ri = &scr->scr_ri;
420
421 ri->ri_depth = sc->sc_depth;
422 ri->ri_width = sc->sc_width;
423 ri->ri_height = sc->sc_height;
424 ri->ri_stride = sc->sc_stride;
425 ri->ri_flg = RI_CENTER;
426
427 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
428 ri->ri_caps = WSSCREEN_WSCOLORS;
429
430 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
431 sc->sc_width / ri->ri_font->fontwidth);
432
433 ri->ri_hw = scr;
434 ri->ri_ops.copyrows = pm2fb_copyrows;
435 ri->ri_ops.copycols = pm2fb_copycols;
436 ri->ri_ops.cursor = pm2fb_cursor;
437 ri->ri_ops.eraserows = pm2fb_eraserows;
438 ri->ri_ops.erasecols = pm2fb_erasecols;
439 ri->ri_ops.putchar = pm2fb_putchar;
440 }
441
442 static int
443 pm2fb_putcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
444 {
445 u_char *r, *g, *b;
446 u_int index = cm->index;
447 u_int count = cm->count;
448 int i, error;
449 u_char rbuf[256], gbuf[256], bbuf[256];
450
451 #ifdef PM2FB_DEBUG
452 aprint_debug("putcmap: %d %d\n",index, count);
453 #endif
454 if (cm->index >= 256 || cm->count > 256 ||
455 (cm->index + cm->count) > 256)
456 return EINVAL;
457 error = copyin(cm->red, &rbuf[index], count);
458 if (error)
459 return error;
460 error = copyin(cm->green, &gbuf[index], count);
461 if (error)
462 return error;
463 error = copyin(cm->blue, &bbuf[index], count);
464 if (error)
465 return error;
466
467 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
468 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
469 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
470
471 r = &sc->sc_cmap_red[index];
472 g = &sc->sc_cmap_green[index];
473 b = &sc->sc_cmap_blue[index];
474
475 for (i = 0; i < count; i++) {
476 pm2fb_putpalreg(sc, index, *r, *g, *b);
477 index++;
478 r++, g++, b++;
479 }
480 return 0;
481 }
482
483 static int
484 pm2fb_getcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
485 {
486 u_int index = cm->index;
487 u_int count = cm->count;
488 int error;
489
490 if (index >= 255 || count > 256 || index + count > 256)
491 return EINVAL;
492
493 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
494 if (error)
495 return error;
496 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
497 if (error)
498 return error;
499 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
500 if (error)
501 return error;
502
503 return 0;
504 }
505
506 static void
507 pm2fb_restore_palette(struct pm2fb_softc *sc)
508 {
509 int i;
510
511 for (i = 0; i < (1 << sc->sc_depth); i++) {
512 pm2fb_putpalreg(sc, i, sc->sc_cmap_red[i],
513 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
514 }
515 }
516
517 static int
518 pm2fb_putpalreg(struct pm2fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
519 uint8_t b)
520 {
521 bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_PAL_WRITE_IDX, idx);
522 bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, r);
523 bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, g);
524 bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, b);
525 return 0;
526 }
527
528 static void
529 pm2fb_init(struct pm2fb_softc *sc)
530 {
531 pm2fb_flush_engine(sc);
532
533 pm2fb_wait(sc, 8);
534 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_BASE, 0);
535 #if 0
536 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_BYPASS_MASK,
537 0xffffffff);
538 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_WRITE_MASK,
539 0xffffffff);
540 #endif
541 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HW_WRITEMASK,
542 0xffffffff);
543 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SW_WRITEMASK,
544 0xffffffff);
545 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WRITE_MODE,
546 PM2WM_WRITE_EN);
547 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCREENSIZE,
548 (sc->sc_height << 16) | sc->sc_width);
549 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MODE,
550 PM2SC_SCREEN_EN);
551 pm2fb_wait(sc, 8);
552 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DITHER_MODE, 0);
553 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ALPHA_MODE, 0);
554 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
555 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_COLOUR_MODE, 0);
556 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_ADDRESS_MODE, 0);
557 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_READ_MODE, 0);
558 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_LUT_MODE, 0);
559 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_YUV_MODE, 0);
560 pm2fb_wait(sc, 8);
561 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH_MODE, 0);
562 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH, 0);
563 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STENCIL_MODE, 0);
564 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STIPPLE_MODE, 0);
565 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ROP_MODE, 0);
566 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WINDOW_ORIGIN, 0);
567 sc->sc_pprod = bus_space_read_4(sc->sc_memt, sc->sc_regh,
568 PM2_FB_READMODE) &
569 (PM2FB_PP0_MASK | PM2FB_PP1_MASK | PM2FB_PP2_MASK);
570 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_READMODE,
571 sc->sc_pprod);
572 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEXMAP_FORMAT,
573 sc->sc_pprod);
574 pm2fb_wait(sc, 8);
575 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DY, 1 << 16);
576 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DXDOM, 0);
577 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXDOM, 0);
578 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXSUB, 0);
579 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTY, 0);
580 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_COUNT, 0);
581 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MINYX, 0);
582 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MAXYX,
583 0x0fff0fff);
584 pm2fb_flush_engine(sc);
585 }
586
587 static void
588 pm2fb_rectfill(struct pm2fb_softc *sc, int x, int y, int wi, int he,
589 uint32_t colour)
590 {
591
592 pm2fb_wait(sc, 7);
593 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
594 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
595 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
596 PM2RECFG_WRITE_EN);
597 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_BLOCK_COLOUR,
598 colour);
599 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
600 (y << 16) | x);
601 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
602 (he << 16) | wi);
603 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
604 PM2RE_RECTANGLE | PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
605 }
606
607 static void
608 pm2fb_bitblt(struct pm2fb_softc *sc, int xs, int ys, int xd, int yd,
609 int wi, int he, int rop)
610 {
611 uint32_t dir = 0;
612
613 if (yd <= ys) {
614 dir |= PM2RE_INC_Y;
615 }
616 if (xd <= xs) {
617 dir |= PM2RE_INC_X;
618 }
619 pm2fb_wait(sc, 7);
620 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
621 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
622 if (rop == 3) {
623 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
624 PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN | PM2RECFG_ROP_EN |
625 PM2RECFG_PACKED | (rop << 6));
626 } else {
627 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
628 PM2RECFG_READ_SRC | PM2RECFG_READ_DST | PM2RECFG_WRITE_EN |
629 PM2RECFG_PACKED | PM2RECFG_ROP_EN | (rop << 6));
630 }
631 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
632 (yd << 16) | xd);
633 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
634 (he << 16) | wi);
635 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SOURCE_DELTA,
636 (((ys - yd) & 0xfff) << 16) | ((xs - xd) & 0xfff));
637 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
638 PM2RE_RECTANGLE | dir);
639 }
640
641 static void
642 pm2fb_cursor(void *cookie, int on, int row, int col)
643 {
644 struct rasops_info *ri = cookie;
645 struct vcons_screen *scr = ri->ri_hw;
646 struct pm2fb_softc *sc = scr->scr_cookie;
647 int x, y, wi, he;
648
649 wi = ri->ri_font->fontwidth;
650 he = ri->ri_font->fontheight;
651
652 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
653 x = ri->ri_ccol * wi + ri->ri_xorigin;
654 y = ri->ri_crow * he + ri->ri_yorigin;
655 if (ri->ri_flg & RI_CURSOR) {
656 pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
657 ri->ri_flg &= ~RI_CURSOR;
658 }
659 ri->ri_crow = row;
660 ri->ri_ccol = col;
661 if (on) {
662 x = ri->ri_ccol * wi + ri->ri_xorigin;
663 y = ri->ri_crow * he + ri->ri_yorigin;
664 pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
665 ri->ri_flg |= RI_CURSOR;
666 }
667 } else {
668 scr->scr_ri.ri_crow = row;
669 scr->scr_ri.ri_ccol = col;
670 scr->scr_ri.ri_flg &= ~RI_CURSOR;
671 }
672
673 }
674
675 static void
676 pm2fb_putchar(void *cookie, int row, int col, u_int c, long attr)
677 {
678 struct rasops_info *ri = cookie;
679 struct wsdisplay_font *font = PICK_FONT(ri, c);
680 struct vcons_screen *scr = ri->ri_hw;
681 struct pm2fb_softc *sc = scr->scr_cookie;
682 uint32_t mode;
683
684 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
685 void *data;
686 uint32_t fg, bg;
687 int uc, i;
688 int x, y, wi, he;
689
690 wi = font->fontwidth;
691 he = font->fontheight;
692
693 if (!CHAR_IN_FONT(c, font))
694 return;
695 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
696 fg = ri->ri_devcmap[(attr >> 24) & 0xf];
697 x = ri->ri_xorigin + col * wi;
698 y = ri->ri_yorigin + row * he;
699 if (c == 0x20) {
700 pm2fb_rectfill(sc, x, y, wi, he, bg);
701 } else {
702 uc = c - font->firstchar;
703 data = (uint8_t *)font->data + uc * ri->ri_fontscale;
704
705 mode = PM2RM_MASK_MIRROR;
706 switch (ri->ri_font->stride) {
707 case 1:
708 mode |= 3 << 7;
709 break;
710 case 2:
711 mode |= 2 << 7;
712 break;
713 }
714
715 pm2fb_wait(sc, 8);
716
717 bus_space_write_4(sc->sc_memt, sc->sc_regh,
718 PM2_RE_MODE, mode);
719 bus_space_write_4(sc->sc_memt, sc->sc_regh,
720 PM2_RE_CONFIG, PM2RECFG_WRITE_EN);
721 bus_space_write_4(sc->sc_memt, sc->sc_regh,
722 PM2_RE_BLOCK_COLOUR, bg);
723 bus_space_write_4(sc->sc_memt, sc->sc_regh,
724 PM2_RE_RECT_START, (y << 16) | x);
725 bus_space_write_4(sc->sc_memt, sc->sc_regh,
726 PM2_RE_RECT_SIZE, (he << 16) | wi);
727 bus_space_write_4(sc->sc_memt, sc->sc_regh,
728 PM2_RE_RENDER,
729 PM2RE_RECTANGLE |
730 PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
731 bus_space_write_4(sc->sc_memt, sc->sc_regh,
732 PM2_RE_BLOCK_COLOUR, fg);
733 bus_space_write_4(sc->sc_memt, sc->sc_regh,
734 PM2_RE_RENDER,
735 PM2RE_RECTANGLE | PM2RE_SYNC_ON_MASK |
736 PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
737
738 pm2fb_wait(sc, he);
739 switch (ri->ri_font->stride) {
740 case 1: {
741 uint8_t *data8 = data;
742 uint32_t reg;
743 for (i = 0; i < he; i++) {
744 reg = *data8;
745 bus_space_write_4(sc->sc_memt,
746 sc->sc_regh,
747 PM2_RE_BITMASK, reg);
748 data8++;
749 }
750 break;
751 }
752 case 2: {
753 uint16_t *data16 = data;
754 uint32_t reg;
755 for (i = 0; i < he; i++) {
756 reg = *data16;
757 bus_space_write_4(sc->sc_memt,
758 sc->sc_regh,
759 PM2_RE_BITMASK, reg);
760 data16++;
761 }
762 break;
763 }
764 }
765 }
766 }
767 }
768
769 static void
770 pm2fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
771 {
772 struct rasops_info *ri = cookie;
773 struct vcons_screen *scr = ri->ri_hw;
774 struct pm2fb_softc *sc = scr->scr_cookie;
775 int32_t xs, xd, y, width, height;
776
777 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
778 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
779 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
780 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
781 width = ri->ri_font->fontwidth * ncols;
782 height = ri->ri_font->fontheight;
783 pm2fb_bitblt(sc, xs, y, xd, y, width, height, 3);
784 }
785 }
786
787 static void
788 pm2fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
789 {
790 struct rasops_info *ri = cookie;
791 struct vcons_screen *scr = ri->ri_hw;
792 struct pm2fb_softc *sc = scr->scr_cookie;
793 int32_t x, y, width, height, fg, bg, ul;
794
795 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
796 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
797 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
798 width = ri->ri_font->fontwidth * ncols;
799 height = ri->ri_font->fontheight;
800 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
801
802 pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
803 }
804 }
805
806 static void
807 pm2fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
808 {
809 struct rasops_info *ri = cookie;
810 struct vcons_screen *scr = ri->ri_hw;
811 struct pm2fb_softc *sc = scr->scr_cookie;
812 int32_t x, ys, yd, width, height;
813
814 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
815 x = ri->ri_xorigin;
816 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
817 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
818 width = ri->ri_emuwidth;
819 height = ri->ri_font->fontheight*nrows;
820 pm2fb_bitblt(sc, x, ys, x, yd, width, height, 3);
821 }
822 }
823
824 static void
825 pm2fb_eraserows(void *cookie, int row, int nrows, long fillattr)
826 {
827 struct rasops_info *ri = cookie;
828 struct vcons_screen *scr = ri->ri_hw;
829 struct pm2fb_softc *sc = scr->scr_cookie;
830 int32_t x, y, width, height, fg, bg, ul;
831
832 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
833 x = ri->ri_xorigin;
834 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
835 width = ri->ri_emuwidth;
836 height = ri->ri_font->fontheight * nrows;
837 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
838
839 pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
840 }
841 }
842
843