r128fb.c revision 1.3 1 /* $NetBSD: r128fb.c,v 1.3 2008/02/27 23:59:37 macallan Exp $ */
2
3 /*
4 * Copyright (c) 2007 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 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * A console driver for ATI Rage 128 graphics controllers
32 * tested on macppc only so far
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: r128fb.c,v 1.3 2008/02/27 23:59:37 macallan Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43 #include <sys/lwp.h>
44 #include <sys/kauth.h>
45
46 #include <uvm/uvm_extern.h>
47
48 #include <dev/videomode/videomode.h>
49
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcidevs.h>
53 #include <dev/pci/pciio.h>
54 #include <dev/pci/r128fbreg.h>
55
56 #include <dev/wscons/wsdisplayvar.h>
57 #include <dev/wscons/wsconsio.h>
58 #include <dev/wsfont/wsfont.h>
59 #include <dev/rasops/rasops.h>
60 #include <dev/wscons/wsdisplay_vconsvar.h>
61
62 #include <dev/i2c/i2cvar.h>
63
64 struct r128fb_softc {
65 device_t sc_dev;
66
67 pci_chipset_tag_t sc_pc;
68 pcitag_t sc_pcitag;
69
70 bus_space_tag_t sc_memt;
71 bus_space_tag_t sc_iot;
72
73 bus_space_handle_t sc_fbh;
74 bus_space_handle_t sc_regh;
75 bus_addr_t sc_fb, sc_reg;
76 bus_size_t sc_fbsize, sc_regsize;
77
78 int sc_width, sc_height, sc_depth, sc_stride;
79 int sc_locked;
80 void *sc_fbaddr;
81 struct vcons_screen sc_console_screen;
82 struct wsscreen_descr sc_defaultscreen_descr;
83 const struct wsscreen_descr *sc_screens[1];
84 struct wsscreen_list sc_screenlist;
85 struct vcons_data vd;
86 int sc_mode;
87 u_char sc_cmap_red[256];
88 u_char sc_cmap_green[256];
89 u_char sc_cmap_blue[256];
90 /* engine stuff */
91 uint32_t sc_master_cntl;
92 };
93
94 static int r128fb_match(device_t, cfdata_t, void *);
95 static void r128fb_attach(device_t, device_t, void *);
96
97 CFATTACH_DECL_NEW(r128fb, sizeof(struct r128fb_softc),
98 r128fb_match, r128fb_attach, NULL, NULL);
99
100 extern const u_char rasops_cmap[768];
101
102 static int r128fb_ioctl(void *, void *, u_long, void *, int,
103 struct lwp *);
104 static paddr_t r128fb_mmap(void *, void *, off_t, int);
105 static void r128fb_init_screen(void *, struct vcons_screen *, int, long *);
106
107 static int r128fb_putcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
108 static int r128fb_getcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
109 static void r128fb_restore_palette(struct r128fb_softc *);
110 static int r128fb_putpalreg(struct r128fb_softc *, uint8_t, uint8_t,
111 uint8_t, uint8_t);
112
113 static void r128fb_init(struct r128fb_softc *);
114 static void r128fb_flush_engine(struct r128fb_softc *);
115 static void r128fb_rectfill(struct r128fb_softc *, int, int, int, int,
116 uint32_t);
117 static void r128fb_bitblt(struct r128fb_softc *, int, int, int, int, int,
118 int, int);
119
120 static void r128fb_cursor(void *, int, int, int);
121 #if 0
122 static void r128fb_putchar(void *, int, int, u_int, long);
123 #endif
124 static void r128fb_copycols(void *, int, int, int, int);
125 static void r128fb_erasecols(void *, int, int, int, long);
126 static void r128fb_copyrows(void *, int, int, int);
127 static void r128fb_eraserows(void *, int, int, long);
128
129 struct wsdisplay_accessops r128fb_accessops = {
130 r128fb_ioctl,
131 r128fb_mmap,
132 NULL, /* alloc_screen */
133 NULL, /* free_screen */
134 NULL, /* show_screen */
135 NULL, /* load_font */
136 NULL, /* pollc */
137 NULL /* scroll */
138 };
139
140 static inline void
141 r128fb_wait(struct r128fb_softc *sc, int slots)
142 {
143 uint32_t reg;
144
145 do {
146 reg = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
147 R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK);
148 } while (reg <= slots);
149 }
150
151 static void
152 r128fb_flush_engine(struct r128fb_softc *sc)
153 {
154 uint32_t reg;
155
156 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT);
157 reg |= R128_PC_FLUSH_ALL;
158 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT, reg);
159 do {
160 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
161 R128_PC_NGUI_CTLSTAT);
162 } while (reg & R128_PC_BUSY);
163 }
164
165 static int
166 r128fb_match(device_t parent, cfdata_t match, void *aux)
167 {
168 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
169
170 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
171 PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
172 return 0;
173 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ATI)
174 return 0;
175
176 /* only card tested on so far - likely need a list */
177 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE1AGP4XT)
178 return 100;
179 return (0);
180 }
181
182 static void
183 r128fb_attach(device_t parent, device_t self, void *aux)
184 {
185 struct r128fb_softc *sc = device_private(self);
186 struct pci_attach_args *pa = aux;
187 struct rasops_info *ri;
188 char devinfo[256];
189 struct wsemuldisplaydev_attach_args aa;
190 prop_dictionary_t dict;
191 unsigned long defattr;
192 bool is_console;
193 int i, j;
194
195 sc->sc_pc = pa->pa_pc;
196 sc->sc_pcitag = pa->pa_tag;
197 sc->sc_memt = pa->pa_memt;
198 sc->sc_iot = pa->pa_iot;
199 sc->sc_dev = self;
200
201 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
202 aprint_normal(": %s\n", devinfo);
203
204 /* fill in parameters from properties */
205 dict = device_properties(self);
206 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
207 aprint_error("%s: no width property\n", device_xname(self));
208 return;
209 }
210 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
211 aprint_error("%s: no height property\n", device_xname(self));
212 return;
213 }
214 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
215 aprint_error("%s: no depth property\n", device_xname(self));
216 return;
217 }
218 if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) {
219 aprint_error("%s: no linebytes property\n",
220 device_xname(self));
221 return;
222 }
223
224 prop_dictionary_get_bool(dict, "is_console", &is_console);
225
226 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM,
227 BUS_SPACE_MAP_LINEAR,
228 &sc->sc_memt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
229 aprint_error("%s: failed to map the frame buffer.\n",
230 device_xname(sc->sc_dev));
231 }
232 sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
233
234 if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, 0,
235 &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
236 aprint_error("%s: failed to map registers.\n",
237 device_xname(sc->sc_dev));
238 }
239
240 /*
241 * XXX yeah, casting the fb address to uint32_t is formally wrong
242 * but as far as I know there are no mach64 with 64bit BARs
243 */
244 aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
245 (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
246
247 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
248 "default",
249 0, 0,
250 NULL,
251 8, 16,
252 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
253 NULL
254 };
255 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
256 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
257 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
258 sc->sc_locked = 0;
259
260 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
261 &r128fb_accessops);
262 sc->vd.init_screen = r128fb_init_screen;
263
264 /* init engine here */
265 r128fb_init(sc);
266
267 ri = &sc->sc_console_screen.scr_ri;
268
269 if (is_console) {
270 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
271 &defattr);
272 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
273
274 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
275 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
276 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
277 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
278 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
279 defattr);
280 } else {
281 /*
282 * since we're not the console we can postpone the rest
283 * until someone actually allocates a screen for us
284 */
285 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
286 }
287
288 j = 0;
289 for (i = 0; i < (1 << sc->sc_depth); i++) {
290
291 sc->sc_cmap_red[i] = rasops_cmap[j];
292 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
293 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
294 r128fb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
295 rasops_cmap[j + 2]);
296 j += 3;
297 }
298
299 r128fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
300 ri->ri_devcmap[(defattr >> 16) & 0xff]);
301
302 aa.console = is_console;
303 aa.scrdata = &sc->sc_screenlist;
304 aa.accessops = &r128fb_accessops;
305 aa.accesscookie = &sc->vd;
306
307 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
308
309 }
310
311 static int
312 r128fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
313 struct lwp *l)
314 {
315 struct vcons_data *vd = v;
316 struct r128fb_softc *sc = vd->cookie;
317 struct wsdisplay_fbinfo *wdf;
318 struct vcons_screen *ms = vd->active;
319
320 switch (cmd) {
321
322 case WSDISPLAYIO_GTYPE:
323 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
324 return 0;
325
326 /* PCI config read/write passthrough. */
327 case PCI_IOC_CFGREAD:
328 case PCI_IOC_CFGWRITE:
329 return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
330 cmd, data, flag, l));
331
332 case WSDISPLAYIO_GINFO:
333 if (ms == NULL)
334 return ENODEV;
335 wdf = (void *)data;
336 wdf->height = ms->scr_ri.ri_height;
337 wdf->width = ms->scr_ri.ri_width;
338 wdf->depth = ms->scr_ri.ri_depth;
339 wdf->cmsize = 256;
340 return 0;
341
342 case WSDISPLAYIO_GETCMAP:
343 return r128fb_getcmap(sc,
344 (struct wsdisplay_cmap *)data);
345
346 case WSDISPLAYIO_PUTCMAP:
347 return r128fb_putcmap(sc,
348 (struct wsdisplay_cmap *)data);
349
350 case WSDISPLAYIO_LINEBYTES:
351 *(u_int *)data = sc->sc_stride;
352 return 0;
353
354 case WSDISPLAYIO_SMODE:
355 {
356 int new_mode = *(int*)data;
357
358 /* notify the bus backend */
359 if (new_mode != sc->sc_mode) {
360 sc->sc_mode = new_mode;
361 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
362 r128fb_restore_palette(sc);
363 vcons_redraw_screen(ms);
364 }
365 }
366 }
367 return 0;
368 }
369 return EPASSTHROUGH;
370 }
371
372 static paddr_t
373 r128fb_mmap(void *v, void *vs, off_t offset, int prot)
374 {
375 struct vcons_data *vd = v;
376 struct r128fb_softc *sc = vd->cookie;
377 struct lwp *me;
378 paddr_t pa;
379
380 /* 'regular' framebuffer mmap()ing */
381 if (offset < sc->sc_fbsize) {
382 pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
383 BUS_SPACE_MAP_LINEAR);
384 return pa;
385 }
386
387 /*
388 * restrict all other mappings to processes with superuser privileges
389 * or the kernel itself
390 */
391 me = curlwp;
392 if (me != NULL) {
393 if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER,
394 NULL) != 0) {
395 aprint_normal("%s: mmap() rejected.\n",
396 device_xname(sc->sc_dev));
397 return -1;
398 }
399 }
400
401 if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
402 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
403 BUS_SPACE_MAP_LINEAR);
404 return pa;
405 }
406
407 if ((offset >= sc->sc_reg) &&
408 (offset < (sc->sc_reg + sc->sc_regsize))) {
409 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
410 BUS_SPACE_MAP_LINEAR);
411 return pa;
412 }
413
414 #ifdef PCI_MAGIC_IO_RANGE
415 /* allow mapping of IO space */
416 if ((offset >= PCI_MAGIC_IO_RANGE) &&
417 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
418 pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
419 0, prot, BUS_SPACE_MAP_LINEAR);
420 return pa;
421 }
422 #endif
423
424 #ifdef OFB_ALLOW_OTHERS
425 if (offset >= 0x80000000) {
426 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
427 BUS_SPACE_MAP_LINEAR);
428 return pa;
429 }
430 #endif
431 return -1;
432 }
433
434 static void
435 r128fb_init_screen(void *cookie, struct vcons_screen *scr,
436 int existing, long *defattr)
437 {
438 struct r128fb_softc *sc = cookie;
439 struct rasops_info *ri = &scr->scr_ri;
440
441 ri->ri_depth = sc->sc_depth;
442 ri->ri_width = sc->sc_width;
443 ri->ri_height = sc->sc_height;
444 ri->ri_stride = sc->sc_stride;
445 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
446
447 ri->ri_bits = (char *)sc->sc_fbaddr;
448
449 if (existing) {
450 ri->ri_flg |= RI_CLEAR;
451 }
452
453 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
454 ri->ri_caps = WSSCREEN_WSCOLORS;
455
456 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
457 sc->sc_width / ri->ri_font->fontwidth);
458
459 ri->ri_hw = scr;
460 ri->ri_ops.copyrows = r128fb_copyrows;
461 ri->ri_ops.copycols = r128fb_copycols;
462 ri->ri_ops.eraserows = r128fb_eraserows;
463 ri->ri_ops.erasecols = r128fb_erasecols;
464 ri->ri_ops.cursor = r128fb_cursor;
465 #if 0
466 ri->ri_ops.putchar = r128fb_putchar;
467 #endif
468 }
469
470 static int
471 r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
472 {
473 u_char *r, *g, *b;
474 u_int index = cm->index;
475 u_int count = cm->count;
476 int i, error;
477 u_char rbuf[256], gbuf[256], bbuf[256];
478
479 #ifdef R128FB_DEBUG
480 aprint_debug("putcmap: %d %d\n",index, count);
481 #endif
482 if (cm->index >= 256 || cm->count > 256 ||
483 (cm->index + cm->count) > 256)
484 return EINVAL;
485 error = copyin(cm->red, &rbuf[index], count);
486 if (error)
487 return error;
488 error = copyin(cm->green, &gbuf[index], count);
489 if (error)
490 return error;
491 error = copyin(cm->blue, &bbuf[index], count);
492 if (error)
493 return error;
494
495 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
496 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
497 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
498
499 r = &sc->sc_cmap_red[index];
500 g = &sc->sc_cmap_green[index];
501 b = &sc->sc_cmap_blue[index];
502
503 for (i = 0; i < count; i++) {
504 r128fb_putpalreg(sc, index, *r, *g, *b);
505 index++;
506 r++, g++, b++;
507 }
508 return 0;
509 }
510
511 static int
512 r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
513 {
514 u_int index = cm->index;
515 u_int count = cm->count;
516 int error;
517
518 if (index >= 255 || count > 256 || index + count > 256)
519 return EINVAL;
520
521 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
522 if (error)
523 return error;
524 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
525 if (error)
526 return error;
527 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
528 if (error)
529 return error;
530
531 return 0;
532 }
533
534 static void
535 r128fb_restore_palette(struct r128fb_softc *sc)
536 {
537 int i;
538
539 for (i = 0; i < (1 << sc->sc_depth); i++) {
540 r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
541 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
542 }
543 }
544
545 static int
546 r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
547 uint8_t b)
548 {
549 uint32_t reg;
550
551 /* whack the DAC */
552 reg = (r << 16) | (g << 8) | b;
553 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx);
554 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg);
555 return 0;
556 }
557
558 static void
559 r128fb_init(struct r128fb_softc *sc)
560 {
561 uint32_t datatype;
562
563 r128fb_flush_engine(sc);
564
565 r128fb_wait(sc, 8);
566 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0);
567 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0);
568 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH,
569 sc->sc_stride >> 3);
570 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0);
571 bus_space_write_4(sc->sc_memt, sc->sc_regh,
572 R128_DEFAULT_SC_BOTTOM_RIGHT,
573 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
574 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0);
575 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_BOTTOM_RIGHT,
576 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
577 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE,
578 R128_HOST_BIG_ENDIAN_EN);
579
580 r128fb_wait(sc, 5);
581 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH,
582 sc->sc_stride >> 3);
583 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH,
584 sc->sc_stride >> 3);
585 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0);
586 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0);
587 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK,
588 0xffffffff);
589
590 switch (sc->sc_depth) {
591 case 8:
592 datatype = R128_GMC_DST_8BPP_CI;
593 break;
594 case 15:
595 datatype = R128_GMC_DST_15BPP;
596 break;
597 case 16:
598 datatype = R128_GMC_DST_16BPP;
599 break;
600 case 24:
601 datatype = R128_GMC_DST_24BPP;
602 break;
603 case 32:
604 datatype = R128_GMC_DST_32BPP;
605 break;
606 default:
607 aprint_error("%s: unsupported depth %d\n",
608 device_xname(sc->sc_dev), sc->sc_depth);
609 return;
610 }
611 sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS |
612 R128_GMC_AUX_CLIP_DIS | datatype;
613
614 r128fb_flush_engine(sc);
615 }
616
617 static void
618 r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he,
619 uint32_t colour)
620 {
621
622 r128fb_wait(sc, 6);
623 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
624 R128_GMC_BRUSH_SOLID_COLOR |
625 R128_GMC_SRC_DATATYPE_COLOR |
626 R128_ROP3_P |
627 sc->sc_master_cntl);
628
629 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR,
630 colour);
631 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL,
632 R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
633 /* now feed it coordinates */
634 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
635 (x << 16) | y);
636 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
637 (wi << 16) | he);
638 r128fb_flush_engine(sc);
639 }
640
641 static void
642 r128fb_bitblt(struct r128fb_softc *sc, int xs, int ys, int xd, int yd,
643 int wi, int he, int rop)
644 {
645 uint32_t dp_cntl = 0;
646
647 r128fb_wait(sc, 5);
648 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
649 R128_GMC_BRUSH_SOLID_COLOR |
650 R128_GMC_SRC_DATATYPE_COLOR |
651 rop |
652 R128_DP_SRC_SOURCE_MEMORY |
653 sc->sc_master_cntl);
654
655 if (yd <= ys) {
656 dp_cntl = R128_DST_Y_TOP_TO_BOTTOM;
657 } else {
658 ys += he - 1;
659 yd += he - 1;
660 }
661 if (xd <= xs) {
662 dp_cntl |= R128_DST_X_LEFT_TO_RIGHT;
663 } else {
664 xs += wi - 1;
665 xd += wi - 1;
666 }
667 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl);
668
669 /* now feed it coordinates */
670 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y,
671 (xs << 16) | ys);
672 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
673 (xd << 16) | yd);
674 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
675 (wi << 16) | he);
676 r128fb_flush_engine(sc);
677 }
678
679 static void
680 r128fb_cursor(void *cookie, int on, int row, int col)
681 {
682 struct rasops_info *ri = cookie;
683 struct vcons_screen *scr = ri->ri_hw;
684 struct r128fb_softc *sc = scr->scr_cookie;
685 int x, y, wi, he;
686
687 wi = ri->ri_font->fontwidth;
688 he = ri->ri_font->fontheight;
689
690 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
691 x = ri->ri_ccol * wi + ri->ri_xorigin;
692 y = ri->ri_crow * he + ri->ri_yorigin;
693 if (ri->ri_flg & RI_CURSOR) {
694 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
695 ri->ri_flg &= ~RI_CURSOR;
696 }
697 ri->ri_crow = row;
698 ri->ri_ccol = col;
699 if (on) {
700 x = ri->ri_ccol * wi + ri->ri_xorigin;
701 y = ri->ri_crow * he + ri->ri_yorigin;
702 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
703 ri->ri_flg |= RI_CURSOR;;
704 }
705 } else {
706 scr->scr_ri.ri_crow = row;
707 scr->scr_ri.ri_ccol = col;
708 scr->scr_ri.ri_flg &= ~RI_CURSOR;
709 }
710
711 }
712
713 #if 0
714 static void
715 r128fb_putchar(void *cookie, int row, int col, u_int c, long attr)
716 {
717 }
718 #endif
719
720 static void
721 r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
722 {
723 struct rasops_info *ri = cookie;
724 struct vcons_screen *scr = ri->ri_hw;
725 struct r128fb_softc *sc = scr->scr_cookie;
726 int32_t xs, xd, y, width, height;
727
728 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
729 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
730 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
731 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
732 width = ri->ri_font->fontwidth * ncols;
733 height = ri->ri_font->fontheight;
734 r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S);
735 }
736 }
737
738 static void
739 r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
740 {
741 struct rasops_info *ri = cookie;
742 struct vcons_screen *scr = ri->ri_hw;
743 struct r128fb_softc *sc = scr->scr_cookie;
744 int32_t x, y, width, height, fg, bg, ul;
745
746 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
747 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
748 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
749 width = ri->ri_font->fontwidth * ncols;
750 height = ri->ri_font->fontheight;
751 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
752
753 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
754 }
755 }
756
757 static void
758 r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
759 {
760 struct rasops_info *ri = cookie;
761 struct vcons_screen *scr = ri->ri_hw;
762 struct r128fb_softc *sc = scr->scr_cookie;
763 int32_t x, ys, yd, width, height;
764
765 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
766 x = ri->ri_xorigin;
767 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
768 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
769 width = ri->ri_emuwidth;
770 height = ri->ri_font->fontheight*nrows;
771 r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S);
772 }
773 }
774
775 static void
776 r128fb_eraserows(void *cookie, int row, int nrows, long fillattr)
777 {
778 struct rasops_info *ri = cookie;
779 struct vcons_screen *scr = ri->ri_hw;
780 struct r128fb_softc *sc = scr->scr_cookie;
781 int32_t x, y, width, height, fg, bg, ul;
782
783 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
784 x = ri->ri_xorigin;
785 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
786 width = ri->ri_emuwidth;
787 height = ri->ri_font->fontheight * nrows;
788 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
789
790 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
791 }
792 }
793
794