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