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