pm2fb.c revision 1.20 1 /* $NetBSD: pm2fb.c,v 1.20 2012/10/04 10:35:54 macallan Exp $ */
2
3 /*
4 * Copyright (c) 2009, 2012 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.20 2012/10/04 10:35:54 macallan 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 #include <sys/atomic.h>
44
45 #include <dev/videomode/videomode.h>
46
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcidevs.h>
50 #include <dev/pci/pciio.h>
51 #include <dev/pci/pm2reg.h>
52
53 #include <dev/wscons/wsdisplayvar.h>
54 #include <dev/wscons/wsconsio.h>
55 #include <dev/wsfont/wsfont.h>
56 #include <dev/rasops/rasops.h>
57 #include <dev/wscons/wsdisplay_vconsvar.h>
58 #include <dev/wscons/wsdisplay_glyphcachevar.h>
59 #include <dev/pci/wsdisplay_pci.h>
60
61 #include <dev/i2c/i2cvar.h>
62 #include <dev/i2c/i2c_bitbang.h>
63 #include <dev/i2c/ddcvar.h>
64 #include <dev/videomode/videomode.h>
65 #include <dev/videomode/edidvar.h>
66 #include <dev/videomode/edidreg.h>
67
68 #include "opt_pm2fb.h"
69
70 #ifdef PM2FB_DEBUG
71 #define DPRINTF aprint_error
72 #else
73 #define DPRINTF while (0) printf
74 #endif
75
76 struct pm2fb_softc {
77 device_t sc_dev;
78
79 pci_chipset_tag_t sc_pc;
80 pcitag_t sc_pcitag;
81
82 bus_space_tag_t sc_memt;
83 bus_space_tag_t sc_iot;
84
85 bus_space_handle_t sc_regh;
86 bus_addr_t sc_fb, sc_reg;
87 bus_size_t sc_fbsize, sc_regsize;
88
89 int sc_width, sc_height, sc_depth, sc_stride;
90 int sc_locked;
91 struct vcons_screen sc_console_screen;
92 struct wsscreen_descr sc_defaultscreen_descr;
93 const struct wsscreen_descr *sc_screens[1];
94 struct wsscreen_list sc_screenlist;
95 struct vcons_data vd;
96 int sc_mode;
97 u_char sc_cmap_red[256];
98 u_char sc_cmap_green[256];
99 u_char sc_cmap_blue[256];
100 /* engine stuff */
101 uint32_t sc_pprod;
102 int sc_is_pm2;
103 /* i2c stuff */
104 struct i2c_controller sc_i2c;
105 uint8_t sc_edid_data[128];
106 struct edid_info sc_ei;
107 const struct videomode *sc_videomode;
108 glyphcache sc_gc;
109 };
110
111 static int pm2fb_match(device_t, cfdata_t, void *);
112 static void pm2fb_attach(device_t, device_t, void *);
113
114 CFATTACH_DECL_NEW(pm2fb, sizeof(struct pm2fb_softc),
115 pm2fb_match, pm2fb_attach, NULL, NULL);
116
117 extern const u_char rasops_cmap[768];
118
119 static int pm2fb_ioctl(void *, void *, u_long, void *, int,
120 struct lwp *);
121 static paddr_t pm2fb_mmap(void *, void *, off_t, int);
122 static void pm2fb_init_screen(void *, struct vcons_screen *, int, long *);
123
124 static int pm2fb_putcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
125 static int pm2fb_getcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
126 static void pm2fb_restore_palette(struct pm2fb_softc *);
127 static int pm2fb_putpalreg(struct pm2fb_softc *, uint8_t, uint8_t,
128 uint8_t, uint8_t);
129
130 static void pm2fb_init(struct pm2fb_softc *);
131 static void pm2fb_flush_engine(struct pm2fb_softc *);
132 static void pm2fb_rectfill(struct pm2fb_softc *, int, int, int, int,
133 uint32_t);
134 static void pm2fb_rectfill_a(void *, int, int, int, int, long);
135 static void pm2fb_bitblt(void *, int, int, int, int, int, int, int);
136
137 static void pm2fb_cursor(void *, int, int, int);
138 static void pm2fb_putchar(void *, int, int, u_int, long);
139 static void pm2fb_putchar_aa(void *, int, int, u_int, long);
140 static void pm2fb_copycols(void *, int, int, int, int);
141 static void pm2fb_erasecols(void *, int, int, int, long);
142 static void pm2fb_copyrows(void *, int, int, int);
143 static void pm2fb_eraserows(void *, int, int, long);
144
145 struct wsdisplay_accessops pm2fb_accessops = {
146 pm2fb_ioctl,
147 pm2fb_mmap,
148 NULL, /* alloc_screen */
149 NULL, /* free_screen */
150 NULL, /* show_screen */
151 NULL, /* load_font */
152 NULL, /* pollc */
153 NULL /* scroll */
154 };
155
156 /* I2C glue */
157 static int pm2fb_i2c_acquire_bus(void *, int);
158 static void pm2fb_i2c_release_bus(void *, int);
159 static int pm2fb_i2c_send_start(void *, int);
160 static int pm2fb_i2c_send_stop(void *, int);
161 static int pm2fb_i2c_initiate_xfer(void *, i2c_addr_t, int);
162 static int pm2fb_i2c_read_byte(void *, uint8_t *, int);
163 static int pm2fb_i2c_write_byte(void *, uint8_t, int);
164
165 /* I2C bitbang glue */
166 static void pm2fb_i2cbb_set_bits(void *, uint32_t);
167 static void pm2fb_i2cbb_set_dir(void *, uint32_t);
168 static uint32_t pm2fb_i2cbb_read(void *);
169
170 static void pm2_setup_i2c(struct pm2fb_softc *);
171
172 static const struct i2c_bitbang_ops pm2fb_i2cbb_ops = {
173 pm2fb_i2cbb_set_bits,
174 pm2fb_i2cbb_set_dir,
175 pm2fb_i2cbb_read,
176 {
177 PM2_DD_SDA_IN,
178 PM2_DD_SCL_IN,
179 0,
180 0
181 }
182 };
183
184 /* mode setting stuff */
185 static int pm2fb_set_pll(struct pm2fb_softc *, int);
186 static uint8_t pm2fb_read_dac(struct pm2fb_softc *, int);
187 static void pm2fb_write_dac(struct pm2fb_softc *, int, uint8_t);
188 static void pm2fb_set_mode(struct pm2fb_softc *, const struct videomode *);
189
190 /* this table is from xf86-video-glint */
191 #define PARTPROD(a,b,c) (((a)<<6) | ((b)<<3) | (c))
192 int partprodPermedia[] = {
193 -1,
194 PARTPROD(0,0,1), PARTPROD(0,1,1), PARTPROD(1,1,1), PARTPROD(1,1,2),
195 PARTPROD(1,2,2), PARTPROD(2,2,2), PARTPROD(1,2,3), PARTPROD(2,2,3),
196 PARTPROD(1,3,3), PARTPROD(2,3,3), PARTPROD(1,2,4), PARTPROD(3,3,3),
197 PARTPROD(1,3,4), PARTPROD(2,3,4), -1, PARTPROD(3,3,4),
198 PARTPROD(1,4,4), PARTPROD(2,4,4), -1, PARTPROD(3,4,4),
199 -1, PARTPROD(2,3,5), -1, PARTPROD(4,4,4),
200 PARTPROD(1,4,5), PARTPROD(2,4,5), PARTPROD(3,4,5), -1,
201 -1, -1, -1, PARTPROD(4,4,5),
202 PARTPROD(1,5,5), PARTPROD(2,5,5), -1, PARTPROD(3,5,5),
203 -1, -1, -1, PARTPROD(4,5,5),
204 -1, -1, -1, PARTPROD(3,4,6),
205 -1, -1, -1, PARTPROD(5,5,5),
206 PARTPROD(1,5,6), PARTPROD(2,5,6), -1, PARTPROD(3,5,6),
207 -1, -1, -1, PARTPROD(4,5,6),
208 -1, -1, -1, -1,
209 -1, -1, -1, PARTPROD(5,5,6),
210 -1, -1, -1, -1,
211 -1, -1, -1, -1,
212 -1, -1, -1, -1,
213 -1, -1, -1, -1,
214 -1, -1, -1, -1,
215 -1, -1, -1, -1,
216 -1, -1, -1, -1,
217 -1, -1, -1, -1,
218 -1, -1, -1, -1,
219 -1, -1, -1, -1,
220 -1, -1, -1, -1,
221 -1, -1, -1, -1,
222 -1, -1, -1, -1,
223 -1, -1, -1, -1,
224 -1, -1, -1, -1,
225 -1, -1, -1, -1,
226 0};
227
228 static inline void
229 pm2fb_wait(struct pm2fb_softc *sc, int slots)
230 {
231 uint32_t reg;
232
233 do {
234 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
235 PM2_INPUT_FIFO_SPACE);
236 } while (reg <= slots);
237 }
238
239 static void
240 pm2fb_flush_engine(struct pm2fb_softc *sc)
241 {
242
243 pm2fb_wait(sc, 2);
244
245 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_FILTER_MODE,
246 PM2FLT_PASS_SYNC);
247 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SYNC, 0);
248 do {
249 while (bus_space_read_4(sc->sc_memt, sc->sc_regh,
250 PM2_OUTPUT_FIFO_WORDS) == 0);
251 } while (bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_OUTPUT_FIFO) !=
252 0x188);
253 }
254
255 static int
256 pm2fb_match(device_t parent, cfdata_t match, void *aux)
257 {
258 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
259
260 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
261 return 0;
262 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3DLABS)
263 return 0;
264
265 /*
266 * only card tested on so far is a TechSource Raptor GFX 8P /
267 * Sun PGX32, which happens to be a Permedia 2v
268 */
269 if (/*(PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2) ||*/
270 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2V))
271 return 100;
272 return (0);
273 }
274
275 static void
276 pm2fb_attach(device_t parent, device_t self, void *aux)
277 {
278 struct pm2fb_softc *sc = device_private(self);
279 struct pci_attach_args *pa = aux;
280 struct rasops_info *ri;
281 struct wsemuldisplaydev_attach_args aa;
282 prop_dictionary_t dict;
283 unsigned long defattr;
284 bool is_console;
285 int i, j;
286 uint32_t flags;
287 uint8_t cmap[768];
288
289 sc->sc_pc = pa->pa_pc;
290 sc->sc_pcitag = pa->pa_tag;
291 sc->sc_memt = pa->pa_memt;
292 sc->sc_iot = pa->pa_iot;
293 sc->sc_dev = self;
294 sc->sc_is_pm2 = (PCI_PRODUCT(pa->pa_id) ==
295 PCI_PRODUCT_3DLABS_PERMEDIA2);
296 pci_aprint_devinfo(pa, NULL);
297
298 /*
299 * fill in parameters from properties
300 * if we can't get a usable mode via DDC2 we'll use this to pick one,
301 * which is why we fill them in with some conservative values that
302 * hopefully work as a last resort
303 */
304 dict = device_properties(self);
305 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
306 aprint_error("%s: no width property\n", device_xname(self));
307 sc->sc_width = 1024;
308 }
309 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
310 aprint_error("%s: no height property\n", device_xname(self));
311 sc->sc_height = 768;
312 }
313 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
314 aprint_error("%s: no depth property\n", device_xname(self));
315 sc->sc_depth = 8;
316 }
317
318 /*
319 * don't look at the linebytes property - The Raptor firmware lies
320 * about it. Get it from width * depth >> 3 instead.
321 */
322
323 sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
324
325 prop_dictionary_get_bool(dict, "is_console", &is_console);
326
327 pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x14, PCI_MAPREG_TYPE_MEM,
328 &sc->sc_fb, &sc->sc_fbsize, &flags);
329
330 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
331 &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
332 aprint_error("%s: failed to map registers.\n",
333 device_xname(sc->sc_dev));
334 }
335
336 /*
337 * XXX yeah, casting the fb address to uint32_t is formally wrong
338 * but as far as I know there are no PM2 with 64bit BARs
339 */
340 aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
341 (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
342
343 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
344 "default",
345 0, 0,
346 NULL,
347 8, 16,
348 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
349 NULL
350 };
351 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
352 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
353 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
354 sc->sc_locked = 0;
355
356 pm2_setup_i2c(sc);
357
358 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
359 &pm2fb_accessops);
360 sc->vd.init_screen = pm2fb_init_screen;
361
362 /* init engine here */
363 pm2fb_init(sc);
364
365 ri = &sc->sc_console_screen.scr_ri;
366
367 sc->sc_gc.gc_bitblt = pm2fb_bitblt;
368 sc->sc_gc.gc_rectfill = pm2fb_rectfill_a;
369 sc->sc_gc.gc_blitcookie = sc;
370 sc->sc_gc.gc_rop = 3;
371
372 #ifdef PM2FB_DEBUG
373 /*
374 * leave some room at the bottom of the screen for various blitter
375 * tests and in order to make the glyph cache visible
376 */
377 sc->sc_height -= 200;
378 #endif
379
380 if (is_console) {
381 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
382 &defattr);
383 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
384
385 pm2fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
386 ri->ri_devcmap[(defattr >> 16) & 0xff]);
387 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
388 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
389 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
390 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
391
392 glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
393 min(2047, (sc->sc_fbsize / sc->sc_stride))
394 - sc->sc_height - 5,
395 sc->sc_width,
396 ri->ri_font->fontwidth,
397 ri->ri_font->fontheight,
398 defattr);
399 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
400 defattr);
401 vcons_replay_msgbuf(&sc->sc_console_screen);
402 } else {
403 if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
404 /* do some minimal setup to avoid weirdnesses later */
405 glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
406 min(2047, (sc->sc_fbsize / sc->sc_stride))
407 - sc->sc_height - 5,
408 sc->sc_width,
409 ri->ri_font->fontwidth,
410 ri->ri_font->fontheight,
411 defattr);
412 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
413 &defattr);
414 }
415 }
416
417 j = 0;
418 rasops_get_cmap(ri, cmap, sizeof(cmap));
419 for (i = 0; i < 256; i++) {
420 sc->sc_cmap_red[i] = cmap[j];
421 sc->sc_cmap_green[i] = cmap[j + 1];
422 sc->sc_cmap_blue[i] = cmap[j + 2];
423 pm2fb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
424 j += 3;
425 }
426
427 aa.console = is_console;
428 aa.scrdata = &sc->sc_screenlist;
429 aa.accessops = &pm2fb_accessops;
430 aa.accesscookie = &sc->vd;
431
432 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
433
434 #ifdef PM2FB_DEBUG
435 /*
436 * draw a pattern to check if pm2fb_bitblt() gets the alignment stuff
437 * right
438 */
439 pm2fb_rectfill(sc, 0, sc->sc_height, sc->sc_width, 200, 0xffffffff);
440 pm2fb_rectfill(sc, 0, sc->sc_height, 300, 10, 0);
441 pm2fb_rectfill(sc, 10, sc->sc_height, 200, 10, 0xe0e0e0e0);
442 for (i = 1; i < 20; i++) {
443 pm2fb_bitblt(sc, 0, sc->sc_height,
444 i, sc->sc_height + 10 * i,
445 300, 10, 3);
446 pm2fb_bitblt(sc, i, sc->sc_height,
447 400, sc->sc_height + 10 * i,
448 300, 10, 3);
449 }
450 #endif
451 }
452
453 static int
454 pm2fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
455 struct lwp *l)
456 {
457 struct vcons_data *vd = v;
458 struct pm2fb_softc *sc = vd->cookie;
459 struct wsdisplay_fbinfo *wdf;
460 struct vcons_screen *ms = vd->active;
461
462 switch (cmd) {
463 case WSDISPLAYIO_GTYPE:
464 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
465 return 0;
466
467 /* PCI config read/write passthrough. */
468 case PCI_IOC_CFGREAD:
469 case PCI_IOC_CFGWRITE:
470 return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
471 cmd, data, flag, l);
472
473 case WSDISPLAYIO_GET_BUSID:
474 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
475 sc->sc_pcitag, data);
476
477 case WSDISPLAYIO_GINFO:
478 if (ms == NULL)
479 return ENODEV;
480 wdf = (void *)data;
481 wdf->height = ms->scr_ri.ri_height;
482 wdf->width = ms->scr_ri.ri_width;
483 wdf->depth = ms->scr_ri.ri_depth;
484 wdf->cmsize = 256;
485 return 0;
486
487 case WSDISPLAYIO_GETCMAP:
488 return pm2fb_getcmap(sc,
489 (struct wsdisplay_cmap *)data);
490
491 case WSDISPLAYIO_PUTCMAP:
492 return pm2fb_putcmap(sc,
493 (struct wsdisplay_cmap *)data);
494
495 case WSDISPLAYIO_LINEBYTES:
496 *(u_int *)data = sc->sc_stride;
497 return 0;
498
499 case WSDISPLAYIO_SMODE: {
500 int new_mode = *(int*)data;
501 if (new_mode != sc->sc_mode) {
502 sc->sc_mode = new_mode;
503 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
504 pm2fb_init(sc);
505 pm2fb_restore_palette(sc);
506 glyphcache_wipe(&sc->sc_gc);
507 vcons_redraw_screen(ms);
508 } else
509 pm2fb_flush_engine(sc);
510 }
511 }
512 return 0;
513 case WSDISPLAYIO_GET_EDID: {
514 struct wsdisplayio_edid_info *d = data;
515 d->data_size = 128;
516 if (d->buffer_size < 128)
517 return EAGAIN;
518 return copyout(sc->sc_edid_data, d->edid_data, 128);
519 }
520 }
521 return EPASSTHROUGH;
522 }
523
524 static paddr_t
525 pm2fb_mmap(void *v, void *vs, off_t offset, int prot)
526 {
527 struct vcons_data *vd = v;
528 struct pm2fb_softc *sc = vd->cookie;
529 paddr_t pa;
530
531 /* 'regular' framebuffer mmap()ing */
532 if (offset < sc->sc_fbsize) {
533 pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
534 BUS_SPACE_MAP_LINEAR);
535 return pa;
536 }
537
538 /*
539 * restrict all other mappings to processes with superuser privileges
540 * or the kernel itself
541 */
542 if (kauth_authorize_machdep(kauth_cred_get(),
543 KAUTH_MACHDEP_UNMANAGEDMEM,
544 NULL, NULL, NULL, NULL) != 0) {
545 aprint_normal("%s: mmap() rejected.\n",
546 device_xname(sc->sc_dev));
547 return -1;
548 }
549
550 if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
551 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
552 BUS_SPACE_MAP_LINEAR);
553 return pa;
554 }
555
556 if ((offset >= sc->sc_reg) &&
557 (offset < (sc->sc_reg + sc->sc_regsize))) {
558 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
559 BUS_SPACE_MAP_LINEAR);
560 return pa;
561 }
562
563 #ifdef PCI_MAGIC_IO_RANGE
564 /* allow mapping of IO space */
565 if ((offset >= PCI_MAGIC_IO_RANGE) &&
566 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
567 pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
568 0, prot, BUS_SPACE_MAP_LINEAR);
569 return pa;
570 }
571 #endif
572
573 return -1;
574 }
575
576 static void
577 pm2fb_init_screen(void *cookie, struct vcons_screen *scr,
578 int existing, long *defattr)
579 {
580 struct pm2fb_softc *sc = cookie;
581 struct rasops_info *ri = &scr->scr_ri;
582
583 ri->ri_depth = sc->sc_depth;
584 ri->ri_width = sc->sc_width;
585 ri->ri_height = sc->sc_height;
586 ri->ri_stride = sc->sc_stride;
587 ri->ri_flg = RI_CENTER;
588 if (sc->sc_depth == 8)
589 ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
590
591 rasops_init(ri, 0, 0);
592 ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_UNDERLINE;
593
594 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
595 sc->sc_width / ri->ri_font->fontwidth);
596
597 ri->ri_hw = scr;
598 ri->ri_ops.copyrows = pm2fb_copyrows;
599 ri->ri_ops.copycols = pm2fb_copycols;
600 ri->ri_ops.cursor = pm2fb_cursor;
601 ri->ri_ops.eraserows = pm2fb_eraserows;
602 ri->ri_ops.erasecols = pm2fb_erasecols;
603 if (FONT_IS_ALPHA(ri->ri_font)) {
604 ri->ri_ops.putchar = pm2fb_putchar_aa;
605 } else
606 ri->ri_ops.putchar = pm2fb_putchar;
607 }
608
609 static int
610 pm2fb_putcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
611 {
612 u_char *r, *g, *b;
613 u_int index = cm->index;
614 u_int count = cm->count;
615 int i, error;
616 u_char rbuf[256], gbuf[256], bbuf[256];
617
618 #ifdef PM2FB_DEBUG
619 aprint_debug("putcmap: %d %d\n",index, count);
620 #endif
621 if (cm->index >= 256 || cm->count > 256 ||
622 (cm->index + cm->count) > 256)
623 return EINVAL;
624 error = copyin(cm->red, &rbuf[index], count);
625 if (error)
626 return error;
627 error = copyin(cm->green, &gbuf[index], count);
628 if (error)
629 return error;
630 error = copyin(cm->blue, &bbuf[index], count);
631 if (error)
632 return error;
633
634 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
635 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
636 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
637
638 r = &sc->sc_cmap_red[index];
639 g = &sc->sc_cmap_green[index];
640 b = &sc->sc_cmap_blue[index];
641
642 for (i = 0; i < count; i++) {
643 pm2fb_putpalreg(sc, index, *r, *g, *b);
644 index++;
645 r++, g++, b++;
646 }
647 return 0;
648 }
649
650 static int
651 pm2fb_getcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
652 {
653 u_int index = cm->index;
654 u_int count = cm->count;
655 int error;
656
657 if (index >= 255 || count > 256 || index + count > 256)
658 return EINVAL;
659
660 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
661 if (error)
662 return error;
663 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
664 if (error)
665 return error;
666 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
667 if (error)
668 return error;
669
670 return 0;
671 }
672
673 static void
674 pm2fb_restore_palette(struct pm2fb_softc *sc)
675 {
676 int i;
677
678 for (i = 0; i < (1 << sc->sc_depth); i++) {
679 pm2fb_putpalreg(sc, i, sc->sc_cmap_red[i],
680 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
681 }
682 }
683
684 static int
685 pm2fb_putpalreg(struct pm2fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
686 uint8_t b)
687 {
688 bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_PAL_WRITE_IDX, idx);
689 bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, r);
690 bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, g);
691 bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, b);
692 return 0;
693 }
694
695 static uint8_t
696 pm2fb_read_dac(struct pm2fb_softc *sc, int reg)
697 {
698 if (sc->sc_is_pm2) {
699 bus_space_write_1(sc->sc_memt, sc->sc_regh,
700 PM2_DAC_PAL_WRITE_IDX, reg);
701 return bus_space_read_1(sc->sc_memt, sc->sc_regh,
702 PM2_DAC_INDEX_DATA);
703 } else {
704 bus_space_write_1(sc->sc_memt, sc->sc_regh,
705 PM2V_DAC_INDEX_LOW, reg & 0xff);
706 bus_space_write_1(sc->sc_memt, sc->sc_regh,
707 PM2V_DAC_INDEX_HIGH, (reg >> 8) & 0xff);
708 return bus_space_read_1(sc->sc_memt, sc->sc_regh,
709 PM2V_DAC_INDEX_DATA);
710 }
711 }
712
713 static void
714 pm2fb_write_dac(struct pm2fb_softc *sc, int reg, uint8_t data)
715 {
716 pm2fb_wait(sc, 3);
717 if (sc->sc_is_pm2) {
718 bus_space_write_1(sc->sc_memt, sc->sc_regh,
719 PM2_DAC_PAL_WRITE_IDX, reg);
720 bus_space_write_1(sc->sc_memt, sc->sc_regh,
721 PM2_DAC_INDEX_DATA, data);
722 } else {
723 bus_space_write_1(sc->sc_memt, sc->sc_regh,
724 PM2V_DAC_INDEX_LOW, reg & 0xff);
725 bus_space_write_1(sc->sc_memt, sc->sc_regh,
726 PM2V_DAC_INDEX_HIGH, (reg >> 8) & 0xff);
727 bus_space_write_1(sc->sc_memt, sc->sc_regh,
728 PM2V_DAC_INDEX_DATA, data);
729 }
730 }
731
732 static void
733 pm2fb_init(struct pm2fb_softc *sc)
734 {
735 pm2fb_flush_engine(sc);
736
737 pm2fb_wait(sc, 8);
738 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_BASE, 0);
739 #if 0
740 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_BYPASS_MASK,
741 0xffffffff);
742 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_WRITE_MASK,
743 0xffffffff);
744 #endif
745 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HW_WRITEMASK,
746 0xffffffff);
747 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SW_WRITEMASK,
748 0xffffffff);
749 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WRITE_MODE,
750 PM2WM_WRITE_EN);
751 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCREENSIZE,
752 (sc->sc_height << 16) | sc->sc_width);
753 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MODE,
754 PM2SC_SCREEN_EN);
755 pm2fb_wait(sc, 8);
756 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DITHER_MODE, 0);
757 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ALPHA_MODE, 0);
758 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
759 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_COLOUR_MODE, 0);
760 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_ADDRESS_MODE, 0);
761 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_READ_MODE, 0);
762 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_LUT_MODE, 0);
763 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_YUV_MODE, 0);
764 pm2fb_wait(sc, 8);
765 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH_MODE, 0);
766 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH, 0);
767 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STENCIL_MODE, 0);
768 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STIPPLE_MODE, 0);
769 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ROP_MODE, 0);
770 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WINDOW_ORIGIN, 0);
771 #if 0
772 sc->sc_pprod = bus_space_read_4(sc->sc_memt, sc->sc_regh,
773 PM2_FB_READMODE) &
774 (PM2FB_PP0_MASK | PM2FB_PP1_MASK | PM2FB_PP2_MASK);
775 #endif
776 sc->sc_pprod = partprodPermedia[sc->sc_stride >> 5];
777
778 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_READMODE,
779 sc->sc_pprod);
780 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEXMAP_FORMAT,
781 sc->sc_pprod);
782 pm2fb_wait(sc, 9);
783 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DY, 1 << 16);
784 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DXDOM, 0);
785 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXDOM, 0);
786 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXSUB, 0);
787 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTY, 0);
788 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_COUNT, 0);
789 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MINYX, 0);
790 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MAXYX,
791 0x0fff0fff);
792 /*
793 * another scissor we need to disable in order to blit into off-screen
794 * memory
795 */
796 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCREENSIZE,
797 0x0fff0fff);
798
799 switch(sc->sc_depth) {
800 case 8:
801 bus_space_write_4(sc->sc_memt, sc->sc_regh,
802 PM2_RE_PIXEL_SIZE, PM2PS_8BIT);
803 break;
804 case 16:
805 bus_space_write_4(sc->sc_memt, sc->sc_regh,
806 PM2_RE_PIXEL_SIZE, PM2PS_16BIT);
807 break;
808 case 32:
809 bus_space_write_4(sc->sc_memt, sc->sc_regh,
810 PM2_RE_PIXEL_SIZE, PM2PS_32BIT);
811 break;
812 }
813 pm2fb_flush_engine(sc);
814 DPRINTF("pixel size: %08x\n",
815 bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_RE_PIXEL_SIZE));
816 }
817
818 static void
819 pm2fb_rectfill(struct pm2fb_softc *sc, int x, int y, int wi, int he,
820 uint32_t colour)
821 {
822
823 pm2fb_wait(sc, 7);
824 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
825 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
826 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
827 PM2RECFG_WRITE_EN);
828 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_BLOCK_COLOUR,
829 colour);
830 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
831 (y << 16) | x);
832 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
833 (he << 16) | wi);
834 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
835 PM2RE_RECTANGLE | PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
836 }
837
838 static void
839 pm2fb_rectfill_a(void *cookie, int x, int y, int wi, int he, long attr)
840 {
841 struct pm2fb_softc *sc = cookie;
842
843 pm2fb_rectfill(sc, x, y, wi, he,
844 sc->vd.active->scr_ri.ri_devcmap[(attr >> 24 & 0xf)]);
845 }
846
847 static void
848 pm2fb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
849 int wi, int he, int rop)
850 {
851 struct pm2fb_softc *sc = cookie;
852 uint32_t dir = 0;
853 int rxs, rxd, rwi, rxdelta;
854
855 if (yd <= ys) {
856 dir |= PM2RE_INC_Y;
857 }
858 if (xd <= xs) {
859 dir |= PM2RE_INC_X;
860 }
861 pm2fb_wait(sc, 8);
862 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
863 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
864 if (sc->sc_depth == 8) {
865 int adjust;
866 /*
867 * use packed mode for some extra speed
868 * this copies 32bit quantities even in 8 bit mode, so we need
869 * to adjust for cases where the lower two bits in source and
870 * destination X don't align, and/or where the width isn't a
871 * multiple of 4
872 */
873 if (rop == 3) {
874 bus_space_write_4(sc->sc_memt, sc->sc_regh,
875 PM2_RE_CONFIG,
876 PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN |
877 PM2RECFG_ROP_EN | PM2RECFG_PACKED | (rop << 6));
878 } else {
879 bus_space_write_4(sc->sc_memt, sc->sc_regh,
880 PM2_RE_CONFIG,
881 PM2RECFG_READ_SRC | PM2RECFG_READ_DST |
882 PM2RECFG_WRITE_EN | PM2RECFG_PACKED |
883 PM2RECFG_ROP_EN | (rop << 6));
884 }
885 rxs = xs >> 2;
886 rxd = xd >> 2;
887 rwi = (wi + 7) >> 2;
888 rxdelta = (xs & 0xffc) - (xd & 0xffc);
889 /* adjust for non-aligned x */
890 adjust = ((xd & 3) - (xs & 3));
891 bus_space_write_4(sc->sc_memt, sc->sc_regh,
892 PM2_RE_PACKEDDATA_LIMIT,
893 (xd << 16) | (xd + wi) | (adjust << 29));
894
895 } else {
896 /* we're in 16 or 32bit mode */
897 if (rop == 3) {
898 bus_space_write_4(sc->sc_memt, sc->sc_regh,
899 PM2_RE_CONFIG,
900 PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN |
901 PM2RECFG_ROP_EN | PM2RECFG_PACKED | (rop << 6));
902 } else {
903 bus_space_write_4(sc->sc_memt, sc->sc_regh,
904 PM2_RE_CONFIG,
905 PM2RECFG_READ_SRC | PM2RECFG_READ_DST |
906 PM2RECFG_WRITE_EN | PM2RECFG_PACKED |
907 PM2RECFG_ROP_EN | (rop << 6));
908 }
909 rxs = xs;
910 rxd = xd;
911 rwi = wi;
912 rxdelta = xs - xd;
913 }
914 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
915 (yd << 16) | rxd);
916 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
917 (he << 16) | rwi);
918 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SOURCE_DELTA,
919 (((ys - yd) & 0xfff) << 16) | (rxdelta & 0xfff));
920 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
921 PM2RE_RECTANGLE | dir);
922 }
923
924 static void
925 pm2fb_cursor(void *cookie, int on, int row, int col)
926 {
927 struct rasops_info *ri = cookie;
928 struct vcons_screen *scr = ri->ri_hw;
929 struct pm2fb_softc *sc = scr->scr_cookie;
930 int x, y, wi, he;
931
932 wi = ri->ri_font->fontwidth;
933 he = ri->ri_font->fontheight;
934
935 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
936 x = ri->ri_ccol * wi + ri->ri_xorigin;
937 y = ri->ri_crow * he + ri->ri_yorigin;
938 if (ri->ri_flg & RI_CURSOR) {
939 pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
940 ri->ri_flg &= ~RI_CURSOR;
941 }
942 ri->ri_crow = row;
943 ri->ri_ccol = col;
944 if (on) {
945 x = ri->ri_ccol * wi + ri->ri_xorigin;
946 y = ri->ri_crow * he + ri->ri_yorigin;
947 pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
948 ri->ri_flg |= RI_CURSOR;
949 }
950 } else {
951 scr->scr_ri.ri_crow = row;
952 scr->scr_ri.ri_ccol = col;
953 scr->scr_ri.ri_flg &= ~RI_CURSOR;
954 }
955
956 }
957
958 static void
959 pm2fb_putchar(void *cookie, int row, int col, u_int c, long attr)
960 {
961 struct rasops_info *ri = cookie;
962 struct wsdisplay_font *font = PICK_FONT(ri, c);
963 struct vcons_screen *scr = ri->ri_hw;
964 struct pm2fb_softc *sc = scr->scr_cookie;
965 uint32_t mode;
966
967 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
968 void *data;
969 uint32_t fg, bg;
970 int uc, i;
971 int x, y, wi, he;
972
973 wi = font->fontwidth;
974 he = font->fontheight;
975
976 if (!CHAR_IN_FONT(c, font))
977 return;
978 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
979 fg = ri->ri_devcmap[(attr >> 24) & 0xf];
980 x = ri->ri_xorigin + col * wi;
981 y = ri->ri_yorigin + row * he;
982 if (c == 0x20) {
983 pm2fb_rectfill(sc, x, y, wi, he, bg);
984 } else {
985 uc = c - font->firstchar;
986 data = (uint8_t *)font->data + uc * ri->ri_fontscale;
987
988 mode = PM2RM_MASK_MIRROR;
989 switch (ri->ri_font->stride) {
990 case 1:
991 mode |= 3 << 7;
992 break;
993 case 2:
994 mode |= 2 << 7;
995 break;
996 }
997
998 pm2fb_wait(sc, 8);
999
1000 bus_space_write_4(sc->sc_memt, sc->sc_regh,
1001 PM2_RE_MODE, mode);
1002 bus_space_write_4(sc->sc_memt, sc->sc_regh,
1003 PM2_RE_CONFIG, PM2RECFG_WRITE_EN);
1004 bus_space_write_4(sc->sc_memt, sc->sc_regh,
1005 PM2_RE_BLOCK_COLOUR, bg);
1006 bus_space_write_4(sc->sc_memt, sc->sc_regh,
1007 PM2_RE_RECT_START, (y << 16) | x);
1008 bus_space_write_4(sc->sc_memt, sc->sc_regh,
1009 PM2_RE_RECT_SIZE, (he << 16) | wi);
1010 bus_space_write_4(sc->sc_memt, sc->sc_regh,
1011 PM2_RE_RENDER,
1012 PM2RE_RECTANGLE |
1013 PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
1014 bus_space_write_4(sc->sc_memt, sc->sc_regh,
1015 PM2_RE_BLOCK_COLOUR, fg);
1016 bus_space_write_4(sc->sc_memt, sc->sc_regh,
1017 PM2_RE_RENDER,
1018 PM2RE_RECTANGLE | PM2RE_SYNC_ON_MASK |
1019 PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
1020
1021 pm2fb_wait(sc, he);
1022 switch (ri->ri_font->stride) {
1023 case 1: {
1024 uint8_t *data8 = data;
1025 uint32_t reg;
1026 for (i = 0; i < he; i++) {
1027 reg = *data8;
1028 bus_space_write_4(sc->sc_memt,
1029 sc->sc_regh,
1030 PM2_RE_BITMASK, reg);
1031 data8++;
1032 }
1033 break;
1034 }
1035 case 2: {
1036 uint16_t *data16 = data;
1037 uint32_t reg;
1038 for (i = 0; i < he; i++) {
1039 reg = *data16;
1040 bus_space_write_4(sc->sc_memt,
1041 sc->sc_regh,
1042 PM2_RE_BITMASK, reg);
1043 data16++;
1044 }
1045 break;
1046 }
1047 }
1048 }
1049 if (attr & 1)
1050 pm2fb_rectfill(sc, x, y + he - 2, wi, 1, fg);
1051 }
1052 }
1053
1054 static void
1055 pm2fb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
1056 {
1057 struct rasops_info *ri = cookie;
1058 struct wsdisplay_font *font = PICK_FONT(ri, c);
1059 struct vcons_screen *scr = ri->ri_hw;
1060 struct pm2fb_softc *sc = scr->scr_cookie;
1061 uint32_t bg, fg, /*latch = 0,*/ bg8, fg8, pixel;
1062 int i, x, y, wi, he, r, g, b, aval;
1063 int r1, g1, b1, r0, g0, b0, fgo, bgo;
1064 uint8_t *data8;
1065 int rv = GC_NOPE, cnt = 0;
1066
1067 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1068 return;
1069
1070 if (!CHAR_IN_FONT(c, font))
1071 return;
1072
1073 wi = font->fontwidth;
1074 he = font->fontheight;
1075
1076 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
1077 fg = ri->ri_devcmap[(attr >> 24) & 0xf];
1078 x = ri->ri_xorigin + col * wi;
1079 y = ri->ri_yorigin + row * he;
1080 if (c == 0x20) {
1081 pm2fb_rectfill(sc, x, y, wi, he, bg);
1082 if (attr & 1)
1083 pm2fb_rectfill(sc, x, y + he - 2, wi, 1, fg);
1084 return;
1085 }
1086
1087 rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1088 if (rv == GC_OK)
1089 return;
1090
1091 data8 = WSFONT_GLYPH(c, font);
1092
1093 pm2fb_wait(sc, 5);
1094 #if 0
1095 /*
1096 * TODO:
1097 * - use packed mode here as well, instead of writing each pixel separately
1098 * - see if we can trick the chip into doing the alpha blending for us
1099 */
1100 x = x >> 2;
1101 wi = (wi + 3) >> 2;
1102 #endif
1103 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
1104 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
1105 PM2RECFG_WRITE_EN /*| PM2RECFG_PACKED*/);
1106 bus_space_write_4(sc->sc_memt, sc->sc_regh,
1107 PM2_RE_RECT_START, (y << 16) | x);
1108 bus_space_write_4(sc->sc_memt, sc->sc_regh,
1109 PM2_RE_RECT_SIZE, (he << 16) | wi);
1110 bus_space_write_4(sc->sc_memt, sc->sc_regh,
1111 PM2_RE_RENDER,
1112 PM2RE_RECTANGLE | PM2RE_SYNC_ON_HOST |
1113 PM2RE_INC_X | PM2RE_INC_Y);
1114 /*
1115 * we need the RGB colours here, so get offsets into rasops_cmap
1116 */
1117 fgo = ((attr >> 24) & 0xf) * 3;
1118 bgo = ((attr >> 16) & 0xf) * 3;
1119
1120 r0 = rasops_cmap[bgo];
1121 r1 = rasops_cmap[fgo];
1122 g0 = rasops_cmap[bgo + 1];
1123 g1 = rasops_cmap[fgo + 1];
1124 b0 = rasops_cmap[bgo + 2];
1125 b1 = rasops_cmap[fgo + 2];
1126 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
1127 bg8 = R3G3B2(r0, g0, b0);
1128 fg8 = R3G3B2(r1, g1, b1);
1129
1130 pm2fb_wait(sc, 200);
1131
1132 for (i = 0; i < ri->ri_fontscale; i++) {
1133 aval = *data8;
1134 if (aval == 0) {
1135 pixel = bg8;
1136 } else if (aval == 255) {
1137 pixel = fg8;
1138 } else {
1139 r = aval * r1 + (255 - aval) * r0;
1140 g = aval * g1 + (255 - aval) * g0;
1141 b = aval * b1 + (255 - aval) * b0;
1142 pixel = ((r & 0xe000) >> 8) |
1143 ((g & 0xe000) >> 11) |
1144 ((b & 0xc000) >> 14);
1145 }
1146 #if 0
1147 latch = (latch << 8) | pixel;
1148 /* write in 32bit chunks */
1149 if ((i & 3) == 3) {
1150 bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
1151 PM2_RE_DATA, latch);
1152 /*
1153 * not strictly necessary, old data should be shifted
1154 * out
1155 */
1156 latch = 0;
1157 cnt++;
1158 if (cnt > 190) {
1159 pm2fb_wait(sc, 200);
1160 cnt = 0;
1161 }
1162 }
1163 #else
1164 bus_space_write_4(sc->sc_memt, sc->sc_regh,
1165 PM2_RE_COLOUR, pixel);
1166
1167 if (cnt > 190) {
1168 pm2fb_wait(sc, 200);
1169 cnt = 0;
1170 }
1171 #endif
1172 data8++;
1173 }
1174 #if 0
1175 /* if we have pixels left in latch write them out */
1176 if ((i & 3) != 0) {
1177 latch = latch << ((4 - (i & 3)) << 3);
1178 bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
1179 PM2_RE_DATA, latch);
1180 }
1181 #endif
1182 /*
1183 * XXX
1184 * occasionally characters end up in the cache only partially drawn
1185 * apparently the blitter might end up grabbing them before they're
1186 * completely flushed out into video memory
1187 * so we let the pipeline drain a little bit before continuing
1188 */
1189 pm2fb_wait(sc, 20);
1190
1191 if (rv == GC_ADD) {
1192 glyphcache_add(&sc->sc_gc, c, x, y);
1193 } else if (attr & 1)
1194 pm2fb_rectfill(sc, x, y + he - 2, wi, 1, fg);
1195 }
1196
1197 static void
1198 pm2fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1199 {
1200 struct rasops_info *ri = cookie;
1201 struct vcons_screen *scr = ri->ri_hw;
1202 struct pm2fb_softc *sc = scr->scr_cookie;
1203 int32_t xs, xd, y, width, height;
1204
1205 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1206 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1207 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1208 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1209 width = ri->ri_font->fontwidth * ncols;
1210 height = ri->ri_font->fontheight;
1211 pm2fb_bitblt(sc, xs, y, xd, y, width, height, 3);
1212 }
1213 }
1214
1215 static void
1216 pm2fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1217 {
1218 struct rasops_info *ri = cookie;
1219 struct vcons_screen *scr = ri->ri_hw;
1220 struct pm2fb_softc *sc = scr->scr_cookie;
1221 int32_t x, y, width, height, fg, bg, ul;
1222
1223 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1224 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1225 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1226 width = ri->ri_font->fontwidth * ncols;
1227 height = ri->ri_font->fontheight;
1228 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1229
1230 pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1231 }
1232 }
1233
1234 static void
1235 pm2fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1236 {
1237 struct rasops_info *ri = cookie;
1238 struct vcons_screen *scr = ri->ri_hw;
1239 struct pm2fb_softc *sc = scr->scr_cookie;
1240 int32_t x, ys, yd, width, height;
1241
1242 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1243 x = ri->ri_xorigin;
1244 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1245 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1246 width = ri->ri_emuwidth;
1247 height = ri->ri_font->fontheight*nrows;
1248 pm2fb_bitblt(sc, x, ys, x, yd, width, height, 3);
1249 }
1250 }
1251
1252 static void
1253 pm2fb_eraserows(void *cookie, int row, int nrows, long fillattr)
1254 {
1255 struct rasops_info *ri = cookie;
1256 struct vcons_screen *scr = ri->ri_hw;
1257 struct pm2fb_softc *sc = scr->scr_cookie;
1258 int32_t x, y, width, height, fg, bg, ul;
1259
1260 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1261 x = ri->ri_xorigin;
1262 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1263 width = ri->ri_emuwidth;
1264 height = ri->ri_font->fontheight * nrows;
1265 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1266
1267 pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1268 }
1269 }
1270
1271 /*
1272 * Permedia2 can't blit outside of 2048x2048, so reject anything higher
1273 * max. dot clock is probably too high
1274 */
1275
1276 #define MODE_IS_VALID(m) (((m)->hdisplay < 2048) && \
1277 ((m)->dot_clock < 230000))
1278
1279 static void
1280 pm2_setup_i2c(struct pm2fb_softc *sc)
1281 {
1282 int i;
1283 #ifdef PM2FB_DEBUG
1284 int j;
1285 #endif
1286
1287 /* Fill in the i2c tag */
1288 sc->sc_i2c.ic_cookie = sc;
1289 sc->sc_i2c.ic_acquire_bus = pm2fb_i2c_acquire_bus;
1290 sc->sc_i2c.ic_release_bus = pm2fb_i2c_release_bus;
1291 sc->sc_i2c.ic_send_start = pm2fb_i2c_send_start;
1292 sc->sc_i2c.ic_send_stop = pm2fb_i2c_send_stop;
1293 sc->sc_i2c.ic_initiate_xfer = pm2fb_i2c_initiate_xfer;
1294 sc->sc_i2c.ic_read_byte = pm2fb_i2c_read_byte;
1295 sc->sc_i2c.ic_write_byte = pm2fb_i2c_write_byte;
1296 sc->sc_i2c.ic_exec = NULL;
1297
1298 DPRINTF("data: %08x\n", bus_space_read_4(sc->sc_memt, sc->sc_regh,
1299 PM2_DISPLAY_DATA));
1300
1301 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA, 0);
1302
1303 /* zero out the EDID buffer */
1304 memset(sc->sc_edid_data, 0, 128);
1305
1306 /* Some monitors don't respond first time */
1307 i = 0;
1308 while (sc->sc_edid_data[1] == 0 && i < 10) {
1309 ddc_read_edid(&sc->sc_i2c, sc->sc_edid_data, 128);
1310 i++;
1311 }
1312 #ifdef PM2FB_DEBUG
1313 printf("i = %d\n", i);
1314 for (i = 0; i < 128; i += 16) {
1315 printf("%02x:", i);
1316 for (j = 0; j < 16; j++)
1317 printf(" %02x", sc->sc_edid_data[i + j]);
1318 printf("\n");
1319 }
1320 #endif
1321
1322 if (edid_parse(&sc->sc_edid_data[0], &sc->sc_ei) != -1) {
1323 #ifdef PM2FB_DEBUG
1324 edid_print(&sc->sc_ei);
1325 #endif
1326
1327 /*
1328 * Now pick a mode.
1329 */
1330 if ((sc->sc_ei.edid_preferred_mode != NULL)) {
1331 struct videomode *m = sc->sc_ei.edid_preferred_mode;
1332 if (MODE_IS_VALID(m)) {
1333 sc->sc_videomode = m;
1334 } else {
1335 aprint_error_dev(sc->sc_dev,
1336 "unable to use preferred mode\n");
1337 }
1338 }
1339 /*
1340 * if we can't use the preferred mode go look for the
1341 * best one we can support
1342 */
1343 if (sc->sc_videomode == NULL) {
1344 int n;
1345 struct videomode *m = sc->sc_ei.edid_modes;
1346
1347 sort_modes(sc->sc_ei.edid_modes,
1348 &sc->sc_ei.edid_preferred_mode,
1349 sc->sc_ei.edid_nmodes);
1350 while ((sc->sc_videomode == NULL) &&
1351 (n < sc->sc_ei.edid_nmodes)) {
1352 if (MODE_IS_VALID(&m[n])) {
1353 sc->sc_videomode = &m[n];
1354 }
1355 n++;
1356 }
1357 }
1358 }
1359 if (sc->sc_videomode == NULL) {
1360 /* no EDID data? */
1361 sc->sc_videomode = pick_mode_by_ref(sc->sc_width,
1362 sc->sc_height, 60);
1363 }
1364 if (sc->sc_videomode != NULL) {
1365 pm2fb_set_mode(sc, sc->sc_videomode);
1366 }
1367 }
1368
1369 /* I2C bitbanging */
1370 static void pm2fb_i2cbb_set_bits(void *cookie, uint32_t bits)
1371 {
1372 struct pm2fb_softc *sc = cookie;
1373 uint32_t out;
1374
1375 out = bits << 2; /* bitmasks match the IN bits */
1376
1377 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA, out);
1378 delay(100);
1379 }
1380
1381 static void pm2fb_i2cbb_set_dir(void *cookie, uint32_t dir)
1382 {
1383 /* Nothing to do */
1384 }
1385
1386 static uint32_t pm2fb_i2cbb_read(void *cookie)
1387 {
1388 struct pm2fb_softc *sc = cookie;
1389 uint32_t bits;
1390
1391 bits = bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA);
1392 return bits;
1393 }
1394
1395 /* higher level I2C stuff */
1396 static int
1397 pm2fb_i2c_acquire_bus(void *cookie, int flags)
1398 {
1399 /* private bus */
1400 return (0);
1401 }
1402
1403 static void
1404 pm2fb_i2c_release_bus(void *cookie, int flags)
1405 {
1406 /* private bus */
1407 }
1408
1409 static int
1410 pm2fb_i2c_send_start(void *cookie, int flags)
1411 {
1412 return (i2c_bitbang_send_start(cookie, flags, &pm2fb_i2cbb_ops));
1413 }
1414
1415 static int
1416 pm2fb_i2c_send_stop(void *cookie, int flags)
1417 {
1418
1419 return (i2c_bitbang_send_stop(cookie, flags, &pm2fb_i2cbb_ops));
1420 }
1421
1422 static int
1423 pm2fb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
1424 {
1425
1426 return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
1427 &pm2fb_i2cbb_ops));
1428 }
1429
1430 static int
1431 pm2fb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
1432 {
1433 return (i2c_bitbang_read_byte(cookie, valp, flags, &pm2fb_i2cbb_ops));
1434 }
1435
1436 static int
1437 pm2fb_i2c_write_byte(void *cookie, uint8_t val, int flags)
1438 {
1439 return (i2c_bitbang_write_byte(cookie, val, flags, &pm2fb_i2cbb_ops));
1440 }
1441
1442 #define RefClk 14318 /* all frequencies are in kHz */
1443 static int
1444 pm2fb_set_pll(struct pm2fb_softc *sc, int freq)
1445 {
1446 int m, n, p, diff, out_freq, bm, bn, bp, bdiff = 1000000, bfreq;
1447 int fi;
1448 uint8_t temp;
1449
1450 /*
1451 * this should work on PM2V, PM2 needs something slightly different
1452 */
1453 for (m = 1; m < 128; m++) {
1454 for (n = 2 * m + 1; n < 256; n++) {
1455 fi = RefClk * n / m;
1456 for (p = 0; p < 2; p++) {
1457 out_freq = fi >> (p + 1);
1458 diff = abs(out_freq - freq);
1459 if (diff < bdiff) {
1460 bdiff = diff;
1461 bfreq = out_freq;
1462 bm = m;
1463 bn = n;
1464 bp = p;
1465 }
1466 }
1467 }
1468 }
1469 #if 0
1470 /*
1471 * XXX
1472 * output between switching modes and attaching a wsdisplay will
1473 * go through firmware calls on sparc64 and potentially mess up
1474 * our drawing engine state
1475 */
1476 DPRINTF("best: %d kHz ( %d off ), %d %d %d\n", bfreq, bdiff, bm, bn, bp);
1477 #endif
1478 temp = pm2fb_read_dac(sc, PM2V_DAC_CLOCK_CONTROL) & 0xfc;
1479 pm2fb_write_dac(sc, PM2V_DAC_CONTROL, 0);
1480 pm2fb_write_dac(sc, PM2V_DAC_CLOCK_A_M, bm);
1481 pm2fb_write_dac(sc, PM2V_DAC_CLOCK_A_N, bn);
1482 pm2fb_write_dac(sc, PM2V_DAC_CLOCK_A_P, bp);
1483 pm2fb_write_dac(sc, PM2V_DAC_CLOCK_CONTROL, temp | 3);
1484 return 0;
1485 }
1486
1487 /*
1488 * most of the following was adapted from the xf86-video-glint driver's
1489 * pm2v_dac.c
1490 */
1491 static void
1492 pm2fb_set_mode(struct pm2fb_softc *sc, const struct videomode *mode)
1493 {
1494 int t1, t2, t3, t4, stride;
1495 uint32_t vclk;
1496 uint8_t sync = 0;
1497
1498 t1 = mode->hsync_start - mode->hdisplay;
1499 t2 = mode->vsync_start - mode->vdisplay;
1500 t3 = mode->hsync_end - mode->hsync_start;
1501 t4 = mode->vsync_end - mode->vsync_start;
1502
1503 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HTOTAL,
1504 ((mode->htotal) >> 3) - 1);
1505 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HSYNC_END,
1506 (t1 + t3) >> 3);
1507 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HSYNC_START,
1508 (t1 >> 3) - 1);
1509 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HBLANK_END,
1510 (mode->htotal - mode->hdisplay) >> 3);
1511 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HGATE_END,
1512 (mode->htotal - mode->hdisplay) >> 3);
1513
1514 /* first round up to the next multiple of 32 */
1515 stride = (mode->hdisplay + 31) & ~31;
1516 /* then find the next bigger one that we have partial products for */
1517 while ((partprodPermedia[stride >> 5] == -1) && (stride < 2048)) {
1518 stride += 32;
1519 }
1520 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_STRIDE,
1521 stride >> 3);
1522
1523 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VTOTAL,
1524 mode->vtotal - 1);
1525 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VSYNC_END,
1526 t2 + t4);
1527 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VSYNC_START,
1528 t2);
1529 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VBLANK_END,
1530 mode->vtotal - mode->vdisplay);
1531
1532 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VIDEO_CONTROL,
1533 PM2_VC_VIDEO_ENABLE | PM2_VC_RAMDAC_64BIT |
1534 PM2_VC_HSYNC_ACT_HIGH | PM2_VC_VSYNC_ACT_HIGH);
1535
1536 vclk = bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_VCLKCTL);
1537 bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VCLKCTL,
1538 vclk & 0xfffffffc);
1539
1540 pm2fb_set_pll(sc, mode->dot_clock / 2);
1541 pm2fb_write_dac(sc, PM2V_DAC_MISC_CONTROL, PM2V_DAC_8BIT);
1542
1543 if (mode->flags & VID_PHSYNC)
1544 sync |= PM2V_DAC_HSYNC_INV;
1545 if (mode->flags & VID_PVSYNC)
1546 sync |= PM2V_DAC_VSYNC_INV;
1547 pm2fb_write_dac(sc, PM2V_DAC_SYNC_CONTROL, sync);
1548
1549 pm2fb_write_dac(sc, PM2V_DAC_COLOR_FORMAT, PM2V_DAC_PALETTE);
1550 pm2fb_write_dac(sc, PM2V_DAC_PIXEL_SIZE, PM2V_PS_8BIT);
1551 sc->sc_width = mode->hdisplay;
1552 sc->sc_height = mode->vdisplay;
1553 sc->sc_depth = 8;
1554 sc->sc_stride = stride;
1555 aprint_normal_dev(sc->sc_dev, "using %d x %d in 8 bit, stride %d\n",
1556 sc->sc_width, sc->sc_height, stride);
1557 }
1558