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