r128fb.c revision 1.24 1 /* $NetBSD: r128fb.c,v 1.24 2012/01/04 07:56:35 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.24 2012/01/04 07:56:35 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 <dev/videomode/videomode.h>
45
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcidevs.h>
49 #include <dev/pci/pciio.h>
50 #include <dev/pci/r128fbreg.h>
51
52 #include <dev/wscons/wsdisplayvar.h>
53 #include <dev/wscons/wsconsio.h>
54 #include <dev/wsfont/wsfont.h>
55 #include <dev/rasops/rasops.h>
56 #include <dev/wscons/wsdisplay_vconsvar.h>
57 #include <dev/pci/wsdisplay_pci.h>
58
59 #include <dev/i2c/i2cvar.h>
60
61 #include "opt_r128fb.h"
62 #include "opt_vcons.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_regh;
80 bus_addr_t sc_fb, sc_reg;
81 bus_size_t sc_fbsize, sc_regsize;
82
83 int sc_width, sc_height, sc_depth, sc_stride;
84 int sc_locked, sc_have_backlight, sc_bl_level, sc_bl_on;
85 struct vcons_screen sc_console_screen;
86 struct wsscreen_descr sc_defaultscreen_descr;
87 const struct wsscreen_descr *sc_screens[1];
88 struct wsscreen_list sc_screenlist;
89 struct vcons_data vd;
90 int sc_mode;
91 u_char sc_cmap_red[256];
92 u_char sc_cmap_green[256];
93 u_char sc_cmap_blue[256];
94 /* engine stuff */
95 uint32_t sc_master_cntl;
96 };
97
98 static int r128fb_match(device_t, cfdata_t, void *);
99 static void r128fb_attach(device_t, device_t, void *);
100
101 CFATTACH_DECL_NEW(r128fb, sizeof(struct r128fb_softc),
102 r128fb_match, r128fb_attach, NULL, NULL);
103
104 extern const u_char rasops_cmap[768];
105
106 static int r128fb_ioctl(void *, void *, u_long, void *, int,
107 struct lwp *);
108 static paddr_t r128fb_mmap(void *, void *, off_t, int);
109 static void r128fb_init_screen(void *, struct vcons_screen *, int, long *);
110
111 static int r128fb_putcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
112 static int r128fb_getcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
113 static void r128fb_restore_palette(struct r128fb_softc *);
114 static int r128fb_putpalreg(struct r128fb_softc *, uint8_t, uint8_t,
115 uint8_t, uint8_t);
116
117 static void r128fb_init(struct r128fb_softc *);
118 static void r128fb_flush_engine(struct r128fb_softc *);
119 static void r128fb_rectfill(struct r128fb_softc *, int, int, int, int,
120 uint32_t);
121 static void r128fb_bitblt(struct r128fb_softc *, int, int, int, int, int,
122 int, int);
123
124 static void r128fb_cursor(void *, int, int, int);
125 static void r128fb_putchar(void *, int, int, u_int, long);
126 static void r128fb_putchar_aa(void *, int, int, u_int, long);
127 static void r128fb_copycols(void *, int, int, int, int);
128 static void r128fb_erasecols(void *, int, int, int, long);
129 static void r128fb_copyrows(void *, int, int, int);
130 static void r128fb_eraserows(void *, int, int, long);
131
132 static void r128fb_brightness_up(device_t);
133 static void r128fb_brightness_down(device_t);
134 /* set backlight level */
135 static void r128fb_set_backlight(struct r128fb_softc *, int);
136 /* turn backlight on and off without messing with the level */
137 static void r128fb_switch_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 bus_space_tag_t tag;
201 char devinfo[256];
202 struct wsemuldisplaydev_attach_args aa;
203 prop_dictionary_t dict;
204 unsigned long defattr;
205 bool is_console;
206 int i, j;
207 uint32_t reg, flags;
208
209 sc->sc_pc = pa->pa_pc;
210 sc->sc_pcitag = pa->pa_tag;
211 sc->sc_memt = pa->pa_memt;
212 sc->sc_iot = pa->pa_iot;
213 sc->sc_dev = self;
214
215 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
216 aprint_normal(": %s\n", devinfo);
217
218 /* fill in parameters from properties */
219 dict = device_properties(self);
220 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
221 aprint_error("%s: no width property\n", device_xname(self));
222 return;
223 }
224 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
225 aprint_error("%s: no height property\n", device_xname(self));
226 return;
227 }
228 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
229 aprint_error("%s: no depth property\n", device_xname(self));
230 return;
231 }
232 if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) {
233 aprint_error("%s: no linebytes property\n",
234 device_xname(self));
235 return;
236 }
237
238 prop_dictionary_get_bool(dict, "is_console", &is_console);
239
240 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x10, PCI_MAPREG_TYPE_MEM,
241 &sc->sc_fb, &sc->sc_fbsize, &flags)) {
242 aprint_error("%s: failed to map the frame buffer.\n",
243 device_xname(sc->sc_dev));
244 }
245
246 if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, 0,
247 &tag, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
248 aprint_error("%s: failed to map registers.\n",
249 device_xname(sc->sc_dev));
250 }
251
252 aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
253 (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
254
255 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
256 "default",
257 0, 0,
258 NULL,
259 8, 16,
260 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
261 NULL
262 };
263 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
264 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
265 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
266 sc->sc_locked = 0;
267
268 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
269 &r128fb_accessops);
270 sc->vd.init_screen = r128fb_init_screen;
271
272 /* init engine here */
273 r128fb_init(sc);
274
275 ri = &sc->sc_console_screen.scr_ri;
276
277 j = 0;
278 if (sc->sc_depth == 8) {
279 /* generate an r3g3b2 colour map */
280 for (i = 0; i < 256; i++) {
281 sc->sc_cmap_red[i] = i & 0xe0;
282 sc->sc_cmap_green[i] = (i & 0x1c) << 3;
283 sc->sc_cmap_blue[i] = (i & 0x03) << 6;
284 r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
285 sc->sc_cmap_green[i],
286 sc->sc_cmap_blue[i]);
287 }
288 } else {
289 /* steal rasops' ANSI cmap */
290 for (i = 0; i < 256; i++) {
291 sc->sc_cmap_red[i] = i;
292 sc->sc_cmap_green[i] = i;
293 sc->sc_cmap_blue[i] = i;
294 r128fb_putpalreg(sc, i, i, i, i);
295 j += 3;
296 }
297 }
298
299 if (is_console) {
300 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
301 &defattr);
302 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
303
304 r128fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
305 ri->ri_devcmap[(defattr >> 16) & 0xff]);
306 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
307 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
308 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
309 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
310 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
311 defattr);
312 vcons_replay_msgbuf(&sc->sc_console_screen);
313 } else {
314 /*
315 * since we're not the console we can postpone the rest
316 * until someone actually allocates a screen for us
317 */
318 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
319 }
320
321 /* no suspend/resume support yet */
322 pmf_device_register(sc->sc_dev, NULL, NULL);
323
324 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
325 DPRINTF("R128_LVDS_GEN_CNTL: %08x\n", reg);
326 if (reg & R128_LVDS_ON) {
327 sc->sc_have_backlight = 1;
328 sc->sc_bl_on = 1;
329 sc->sc_bl_level = 255 -
330 ((reg & R128_LEVEL_MASK) >> R128_LEVEL_SHIFT);
331 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
332 r128fb_brightness_up, TRUE);
333 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
334 r128fb_brightness_down, TRUE);
335 aprint_verbose("%s: LVDS output is active, enabling backlight"
336 " control\n", device_xname(self));
337 } else
338 sc->sc_have_backlight = 0;
339
340 aa.console = is_console;
341 aa.scrdata = &sc->sc_screenlist;
342 aa.accessops = &r128fb_accessops;
343 aa.accesscookie = &sc->vd;
344
345 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
346 }
347
348 static int
349 r128fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
350 struct lwp *l)
351 {
352 struct vcons_data *vd = v;
353 struct r128fb_softc *sc = vd->cookie;
354 struct wsdisplay_fbinfo *wdf;
355 struct vcons_screen *ms = vd->active;
356 struct wsdisplay_param *param;
357
358 switch (cmd) {
359 case WSDISPLAYIO_GTYPE:
360 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
361 return 0;
362
363 /* PCI config read/write passthrough. */
364 case PCI_IOC_CFGREAD:
365 case PCI_IOC_CFGWRITE:
366 return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
367 cmd, data, flag, l);
368
369 case WSDISPLAYIO_GET_BUSID:
370 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc, sc->sc_pcitag, data);
371
372 case WSDISPLAYIO_GINFO:
373 if (ms == NULL)
374 return ENODEV;
375 wdf = (void *)data;
376 wdf->height = ms->scr_ri.ri_height;
377 wdf->width = ms->scr_ri.ri_width;
378 wdf->depth = ms->scr_ri.ri_depth;
379 wdf->cmsize = 256;
380 return 0;
381
382 case WSDISPLAYIO_GETCMAP:
383 return r128fb_getcmap(sc,
384 (struct wsdisplay_cmap *)data);
385
386 case WSDISPLAYIO_PUTCMAP:
387 return r128fb_putcmap(sc,
388 (struct wsdisplay_cmap *)data);
389
390 case WSDISPLAYIO_LINEBYTES:
391 *(u_int *)data = sc->sc_stride;
392 return 0;
393
394 case WSDISPLAYIO_SMODE: {
395 int new_mode = *(int*)data;
396 if (new_mode != sc->sc_mode) {
397 sc->sc_mode = new_mode;
398 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
399 r128fb_init(sc);
400 r128fb_restore_palette(sc);
401 r128fb_rectfill(sc, 0, 0, sc->sc_width,
402 sc->sc_height, ms->scr_ri.ri_devcmap[
403 (ms->scr_defattr >> 16) & 0xff]);
404 vcons_redraw_screen(ms);
405 }
406 }
407 }
408 return 0;
409
410 case WSDISPLAYIO_GETPARAM:
411 param = (struct wsdisplay_param *)data;
412 if (sc->sc_have_backlight == 0)
413 return EPASSTHROUGH;
414 switch (param->param) {
415 case WSDISPLAYIO_PARAM_BRIGHTNESS:
416 param->min = 0;
417 param->max = 255;
418 param->curval = sc->sc_bl_level;
419 return 0;
420 case WSDISPLAYIO_PARAM_BACKLIGHT:
421 param->min = 0;
422 param->max = 1;
423 param->curval = sc->sc_bl_on;
424 return 0;
425 }
426 return EPASSTHROUGH;
427
428 case WSDISPLAYIO_SETPARAM:
429 param = (struct wsdisplay_param *)data;
430 if (sc->sc_have_backlight == 0)
431 return EPASSTHROUGH;
432 switch (param->param) {
433 case WSDISPLAYIO_PARAM_BRIGHTNESS:
434 r128fb_set_backlight(sc, param->curval);
435 return 0;
436 case WSDISPLAYIO_PARAM_BACKLIGHT:
437 r128fb_switch_backlight(sc, param->curval);
438 return 0;
439 }
440 return EPASSTHROUGH;
441 case WSDISPLAYIO_GET_EDID: {
442 struct wsdisplayio_edid_info *d = data;
443 return wsdisplayio_get_edid(sc->sc_dev, d);
444 }
445 }
446 return EPASSTHROUGH;
447 }
448
449 static paddr_t
450 r128fb_mmap(void *v, void *vs, off_t offset, int prot)
451 {
452 struct vcons_data *vd = v;
453 struct r128fb_softc *sc = vd->cookie;
454 paddr_t pa;
455
456 /* 'regular' framebuffer mmap()ing */
457 if (offset < sc->sc_fbsize) {
458 pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
459 BUS_SPACE_MAP_LINEAR);
460 return pa;
461 }
462
463 /*
464 * restrict all other mappings to processes with superuser privileges
465 * or the kernel itself
466 */
467 if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
468 NULL) != 0) {
469 aprint_normal("%s: mmap() rejected.\n",
470 device_xname(sc->sc_dev));
471 return -1;
472 }
473
474 if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
475 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
476 BUS_SPACE_MAP_LINEAR);
477 return pa;
478 }
479
480 if ((offset >= sc->sc_reg) &&
481 (offset < (sc->sc_reg + sc->sc_regsize))) {
482 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
483 BUS_SPACE_MAP_LINEAR);
484 return pa;
485 }
486
487 #ifdef PCI_MAGIC_IO_RANGE
488 /* allow mapping of IO space */
489 if ((offset >= PCI_MAGIC_IO_RANGE) &&
490 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
491 pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
492 0, prot, BUS_SPACE_MAP_LINEAR);
493 return pa;
494 }
495 #endif
496
497 #ifdef OFB_ALLOW_OTHERS
498 if (offset >= 0x80000000) {
499 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
500 BUS_SPACE_MAP_LINEAR);
501 return pa;
502 }
503 #endif
504 return -1;
505 }
506
507 static void
508 r128fb_init_screen(void *cookie, struct vcons_screen *scr,
509 int existing, long *defattr)
510 {
511 struct r128fb_softc *sc = cookie;
512 struct rasops_info *ri = &scr->scr_ri;
513
514 ri->ri_depth = sc->sc_depth;
515 ri->ri_width = sc->sc_width;
516 ri->ri_height = sc->sc_height;
517 ri->ri_stride = sc->sc_stride;
518 ri->ri_flg = RI_CENTER;
519 if (sc->sc_depth == 8)
520 ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
521
522 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
523 ri->ri_caps = WSSCREEN_WSCOLORS;
524 #ifdef VCONS_DRAW_INTR
525 scr->scr_flags |= VCONS_DONT_READ;
526 #endif
527
528 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
529 sc->sc_width / ri->ri_font->fontwidth);
530
531 ri->ri_hw = scr;
532 ri->ri_ops.copyrows = r128fb_copyrows;
533 ri->ri_ops.copycols = r128fb_copycols;
534 ri->ri_ops.eraserows = r128fb_eraserows;
535 ri->ri_ops.erasecols = r128fb_erasecols;
536 ri->ri_ops.cursor = r128fb_cursor;
537 if (FONT_IS_ALPHA(ri->ri_font)) {
538 ri->ri_ops.putchar = r128fb_putchar_aa;
539 } else
540 ri->ri_ops.putchar = r128fb_putchar;
541 }
542
543 static int
544 r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
545 {
546 u_char *r, *g, *b;
547 u_int index = cm->index;
548 u_int count = cm->count;
549 int i, error;
550 u_char rbuf[256], gbuf[256], bbuf[256];
551
552 #ifdef R128FB_DEBUG
553 aprint_debug("putcmap: %d %d\n",index, count);
554 #endif
555 if (cm->index >= 256 || cm->count > 256 ||
556 (cm->index + cm->count) > 256)
557 return EINVAL;
558 error = copyin(cm->red, &rbuf[index], count);
559 if (error)
560 return error;
561 error = copyin(cm->green, &gbuf[index], count);
562 if (error)
563 return error;
564 error = copyin(cm->blue, &bbuf[index], count);
565 if (error)
566 return error;
567
568 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
569 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
570 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
571
572 r = &sc->sc_cmap_red[index];
573 g = &sc->sc_cmap_green[index];
574 b = &sc->sc_cmap_blue[index];
575
576 for (i = 0; i < count; i++) {
577 r128fb_putpalreg(sc, index, *r, *g, *b);
578 index++;
579 r++, g++, b++;
580 }
581 return 0;
582 }
583
584 static int
585 r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
586 {
587 u_int index = cm->index;
588 u_int count = cm->count;
589 int error;
590
591 if (index >= 255 || count > 256 || index + count > 256)
592 return EINVAL;
593
594 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
595 if (error)
596 return error;
597 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
598 if (error)
599 return error;
600 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
601 if (error)
602 return error;
603
604 return 0;
605 }
606
607 static void
608 r128fb_restore_palette(struct r128fb_softc *sc)
609 {
610 int i;
611
612 for (i = 0; i < (1 << sc->sc_depth); i++) {
613 r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
614 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
615 }
616 }
617
618 static int
619 r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
620 uint8_t b)
621 {
622 uint32_t reg;
623
624 /* whack the DAC */
625 reg = (r << 16) | (g << 8) | b;
626 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx);
627 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg);
628 return 0;
629 }
630
631 static void
632 r128fb_init(struct r128fb_softc *sc)
633 {
634 uint32_t datatype, d, reg;
635
636 r128fb_flush_engine(sc);
637
638 r128fb_wait(sc, 9);
639 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0);
640 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0);
641 /* pitch is in units of 8 pixels */
642 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH,
643 sc->sc_width >> 3);
644 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0);
645 bus_space_write_4(sc->sc_memt, sc->sc_regh,
646 R128_DEFAULT_SC_BOTTOM_RIGHT,
647 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
648 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0);
649 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_BOTTOM_RIGHT,
650 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
651 bus_space_write_4(sc->sc_memt, sc->sc_regh,
652 R128_DEFAULT_SC_BOTTOM_RIGHT,
653 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
654
655 #if 0
656 #if BYTE_ORDER == BIG_ENDIAN
657 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE,
658 R128_HOST_BIG_ENDIAN_EN);
659 #else
660 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE, 0);
661 #endif
662 #endif
663 r128fb_wait(sc, 7);
664 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH,
665 sc->sc_width >> 3);
666 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH,
667 sc->sc_width >> 3);
668 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0);
669 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0);
670 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK,
671 0xffffffff);
672
673 switch (sc->sc_depth) {
674 case 8:
675 datatype = R128_GMC_DST_8BPP_CI;
676 d = R128_CRTC_COLOR_8BIT;
677 break;
678 case 15:
679 datatype = R128_GMC_DST_15BPP;
680 d = R128_CRTC_COLOR_15BIT;
681 break;
682 case 16:
683 datatype = R128_GMC_DST_16BPP;
684 d = R128_CRTC_COLOR_16BIT;
685 break;
686 case 24:
687 datatype = R128_GMC_DST_24BPP;
688 d = R128_CRTC_COLOR_24BIT;
689 break;
690 case 32:
691 datatype = R128_GMC_DST_32BPP;
692 d = R128_CRTC_COLOR_32BIT;
693 break;
694 default:
695 aprint_error("%s: unsupported depth %d\n",
696 device_xname(sc->sc_dev), sc->sc_depth);
697 return;
698 }
699 sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS |
700 R128_GMC_AUX_CLIP_DIS | datatype;
701 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_CRTC_GEN_CNTL);
702 DPRINTF("depth: %d\n", reg & R128_CRTC_PIX_WIDTH);
703 reg &= ~R128_CRTC_PIX_WIDTH;
704 reg |= d;
705 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_GEN_CNTL, reg);
706 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_PITCH, sc->sc_width >> 3);
707 r128fb_flush_engine(sc);
708 }
709
710 static void
711 r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he,
712 uint32_t colour)
713 {
714
715 r128fb_wait(sc, 5);
716 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
717 R128_GMC_BRUSH_SOLID_COLOR |
718 R128_GMC_SRC_DATATYPE_COLOR |
719 R128_ROP3_P |
720 sc->sc_master_cntl);
721
722 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR,
723 colour);
724 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL,
725 R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
726 /* now feed it coordinates */
727 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
728 (x << 16) | y);
729 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
730 (wi << 16) | he);
731 }
732
733 static void
734 r128fb_bitblt(struct r128fb_softc *sc, int xs, int ys, int xd, int yd,
735 int wi, int he, int rop)
736 {
737 uint32_t dp_cntl = 0;
738
739 r128fb_wait(sc, 5);
740 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
741 R128_GMC_BRUSH_SOLID_COLOR |
742 R128_GMC_SRC_DATATYPE_COLOR |
743 rop |
744 R128_DP_SRC_SOURCE_MEMORY |
745 sc->sc_master_cntl);
746
747 if (yd <= ys) {
748 dp_cntl = R128_DST_Y_TOP_TO_BOTTOM;
749 } else {
750 ys += he - 1;
751 yd += he - 1;
752 }
753 if (xd <= xs) {
754 dp_cntl |= R128_DST_X_LEFT_TO_RIGHT;
755 } else {
756 xs += wi - 1;
757 xd += wi - 1;
758 }
759 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl);
760
761 /* now feed it coordinates */
762 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y,
763 (xs << 16) | ys);
764 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
765 (xd << 16) | yd);
766 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
767 (wi << 16) | he);
768 }
769
770 static void
771 r128fb_cursor(void *cookie, int on, int row, int col)
772 {
773 struct rasops_info *ri = cookie;
774 struct vcons_screen *scr = ri->ri_hw;
775 struct r128fb_softc *sc = scr->scr_cookie;
776 int x, y, wi, he;
777
778 wi = ri->ri_font->fontwidth;
779 he = ri->ri_font->fontheight;
780
781 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
782 x = ri->ri_ccol * wi + ri->ri_xorigin;
783 y = ri->ri_crow * he + ri->ri_yorigin;
784 if (ri->ri_flg & RI_CURSOR) {
785 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
786 ri->ri_flg &= ~RI_CURSOR;
787 }
788 ri->ri_crow = row;
789 ri->ri_ccol = col;
790 if (on) {
791 x = ri->ri_ccol * wi + ri->ri_xorigin;
792 y = ri->ri_crow * he + ri->ri_yorigin;
793 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
794 ri->ri_flg |= RI_CURSOR;
795 }
796 } else {
797 scr->scr_ri.ri_crow = row;
798 scr->scr_ri.ri_ccol = col;
799 scr->scr_ri.ri_flg &= ~RI_CURSOR;
800 }
801
802 }
803
804 static void
805 r128fb_putchar(void *cookie, int row, int col, u_int c, long attr)
806 {
807 struct rasops_info *ri = cookie;
808 struct wsdisplay_font *font = PICK_FONT(ri, c);
809 struct vcons_screen *scr = ri->ri_hw;
810 struct r128fb_softc *sc = scr->scr_cookie;
811 void *data;
812 uint32_t fg, bg;
813 int uc, i;
814 int x, y, wi, he, offset;
815
816 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
817 return;
818
819 if (!CHAR_IN_FONT(c, font))
820 return;
821
822 wi = font->fontwidth;
823 he = font->fontheight;
824
825 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
826 fg = ri->ri_devcmap[(attr >> 24) & 0xf];
827 x = ri->ri_xorigin + col * wi;
828 y = ri->ri_yorigin + row * he;
829
830 if (c == 0x20) {
831 r128fb_rectfill(sc, x, y, wi, he, bg);
832 return;
833 }
834
835 uc = c - font->firstchar;
836 data = (uint8_t *)font->data + uc * ri->ri_fontscale;
837
838 r128fb_wait(sc, 8);
839
840 bus_space_write_4(sc->sc_memt, sc->sc_regh,
841 R128_DP_GUI_MASTER_CNTL,
842 R128_GMC_BRUSH_SOLID_COLOR |
843 R128_GMC_SRC_DATATYPE_MONO_FG_BG |
844 R128_ROP3_S |
845 R128_DP_SRC_SOURCE_HOST_DATA |
846 R128_GMC_DST_CLIPPING |
847 sc->sc_master_cntl);
848
849 bus_space_write_4(sc->sc_memt, sc->sc_regh,
850 R128_DP_CNTL,
851 R128_DST_Y_TOP_TO_BOTTOM |
852 R128_DST_X_LEFT_TO_RIGHT);
853
854 bus_space_write_4(sc->sc_memt, sc->sc_regh,
855 R128_DP_SRC_FRGD_CLR, fg);
856 bus_space_write_4(sc->sc_memt, sc->sc_regh,
857 R128_DP_SRC_BKGD_CLR, bg);
858
859 /*
860 * The Rage 128 doesn't have anything to skip pixels
861 * when colour expanding but all coordinates
862 * are signed so we just clip the leading bytes and
863 * trailing bits away
864 */
865 bus_space_write_4(sc->sc_memt, sc->sc_regh,
866 R128_SC_RIGHT, x + wi - 1);
867 bus_space_write_4(sc->sc_memt, sc->sc_regh,
868 R128_SC_LEFT, x);
869
870 /* needed? */
871 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 0);
872
873 offset = 32 - (font->stride << 3);
874 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
875 ((x - offset) << 16) | y);
876 bus_space_write_4(sc->sc_memt, sc->sc_regh,
877 R128_DST_WIDTH_HEIGHT, (32 << 16) | he);
878
879 r128fb_wait(sc, he);
880 switch (font->stride) {
881 case 1: {
882 uint8_t *data8 = data;
883 uint32_t reg;
884 for (i = 0; i < he; i++) {
885 reg = *data8;
886 bus_space_write_stream_4(sc->sc_memt,
887 sc->sc_regh, R128_HOST_DATA0, reg);
888 data8++;
889 }
890 break;
891 }
892 case 2: {
893 uint16_t *data16 = data;
894 uint32_t reg;
895 for (i = 0; i < he; i++) {
896 reg = *data16;
897 bus_space_write_stream_4(sc->sc_memt,
898 sc->sc_regh, R128_HOST_DATA0, reg);
899 data16++;
900 }
901 break;
902 }
903 }
904 }
905
906 static void
907 r128fb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
908 {
909 struct rasops_info *ri = cookie;
910 struct wsdisplay_font *font = PICK_FONT(ri, c);
911 struct vcons_screen *scr = ri->ri_hw;
912 struct r128fb_softc *sc = scr->scr_cookie;
913 uint32_t bg, latch = 0, bg8, fg8, pixel;
914 int uc, i, x, y, wi, he, r, g, b, aval;
915 int r1, g1, b1, r0, g0, b0, fgo, bgo;
916 uint8_t *data8;
917
918 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
919 return;
920
921 if (!CHAR_IN_FONT(c, font))
922 return;
923
924 wi = font->fontwidth;
925 he = font->fontheight;
926
927 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
928 x = ri->ri_xorigin + col * wi;
929 y = ri->ri_yorigin + row * he;
930 if (c == 0x20) {
931 r128fb_rectfill(sc, x, y, wi, he, bg);
932 return;
933 }
934
935 uc = c - font->firstchar;
936 data8 = (uint8_t *)font->data + uc * ri->ri_fontscale;
937
938 r128fb_wait(sc, 5);
939 bus_space_write_4(sc->sc_memt, sc->sc_regh,
940 R128_DP_GUI_MASTER_CNTL,
941 R128_GMC_BRUSH_SOLID_COLOR |
942 R128_GMC_SRC_DATATYPE_COLOR |
943 R128_ROP3_S |
944 R128_DP_SRC_SOURCE_HOST_DATA |
945 sc->sc_master_cntl);
946
947 bus_space_write_4(sc->sc_memt, sc->sc_regh,
948 R128_DP_CNTL,
949 R128_DST_Y_TOP_TO_BOTTOM |
950 R128_DST_X_LEFT_TO_RIGHT);
951
952 /* needed? */
953 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 0);
954 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
955 (x << 16) | y);
956 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
957 (wi << 16) | he);
958
959 /*
960 * we need the RGB colours here, so get offsets into rasops_cmap
961 */
962 fgo = ((attr >> 24) & 0xf) * 3;
963 bgo = ((attr >> 16) & 0xf) * 3;
964
965 r0 = rasops_cmap[bgo];
966 r1 = rasops_cmap[fgo];
967 g0 = rasops_cmap[bgo + 1];
968 g1 = rasops_cmap[fgo + 1];
969 b0 = rasops_cmap[bgo + 2];
970 b1 = rasops_cmap[fgo + 2];
971 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
972 bg8 = R3G3B2(r0, g0, b0);
973 fg8 = R3G3B2(r1, g1, b1);
974 for (i = 0; i < ri->ri_fontscale; i++) {
975 aval = *data8;
976 if (aval == 0) {
977 pixel = bg8;
978 } else if (aval == 255) {
979 pixel = fg8;
980 } else {
981 r = aval * r1 + (255 - aval) * r0;
982 g = aval * g1 + (255 - aval) * g0;
983 b = aval * b1 + (255 - aval) * b0;
984 pixel = ((r & 0xe000) >> 8) |
985 ((g & 0xe000) >> 11) |
986 ((b & 0xc000) >> 14);
987 }
988 latch = (latch << 8) | pixel;
989 /* write in 32bit chunks */
990 if ((i & 3) == 3) {
991 bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
992 R128_HOST_DATA0, latch);
993 /*
994 * not strictly necessary, old data should be shifted
995 * out
996 */
997 latch = 0;
998 }
999 data8++;
1000 }
1001 /* if we have pixels left in latch write them out */
1002 if ((i & 3) != 0) {
1003 latch = latch << ((4 - (i & 3)) << 3);
1004 bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
1005 R128_HOST_DATA0, latch);
1006 }
1007 }
1008
1009 static void
1010 r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1011 {
1012 struct rasops_info *ri = cookie;
1013 struct vcons_screen *scr = ri->ri_hw;
1014 struct r128fb_softc *sc = scr->scr_cookie;
1015 int32_t xs, xd, y, width, height;
1016
1017 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1018 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1019 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1020 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1021 width = ri->ri_font->fontwidth * ncols;
1022 height = ri->ri_font->fontheight;
1023 r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S);
1024 }
1025 }
1026
1027 static void
1028 r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1029 {
1030 struct rasops_info *ri = cookie;
1031 struct vcons_screen *scr = ri->ri_hw;
1032 struct r128fb_softc *sc = scr->scr_cookie;
1033 int32_t x, y, width, height, fg, bg, ul;
1034
1035 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1036 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1037 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1038 width = ri->ri_font->fontwidth * ncols;
1039 height = ri->ri_font->fontheight;
1040 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1041
1042 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1043 }
1044 }
1045
1046 static void
1047 r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1048 {
1049 struct rasops_info *ri = cookie;
1050 struct vcons_screen *scr = ri->ri_hw;
1051 struct r128fb_softc *sc = scr->scr_cookie;
1052 int32_t x, ys, yd, width, height;
1053
1054 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1055 x = ri->ri_xorigin;
1056 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1057 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1058 width = ri->ri_emuwidth;
1059 height = ri->ri_font->fontheight * nrows;
1060 r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S);
1061 }
1062 }
1063
1064 static void
1065 r128fb_eraserows(void *cookie, int row, int nrows, long fillattr)
1066 {
1067 struct rasops_info *ri = cookie;
1068 struct vcons_screen *scr = ri->ri_hw;
1069 struct r128fb_softc *sc = scr->scr_cookie;
1070 int32_t x, y, width, height, fg, bg, ul;
1071
1072 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1073 x = ri->ri_xorigin;
1074 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1075 width = ri->ri_emuwidth;
1076 height = ri->ri_font->fontheight * nrows;
1077 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1078
1079 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1080 }
1081 }
1082
1083 static void
1084 r128fb_set_backlight(struct r128fb_softc *sc, int level)
1085 {
1086 uint32_t reg;
1087
1088 /*
1089 * should we do nothing when backlight is off, should we just store the
1090 * level and use it when turning back on or should we just flip sc_bl_on
1091 * and turn the backlight on?
1092 * For now turn it on so a crashed screensaver can't get the user stuck
1093 * with a dark screen as long as hotkeys work
1094 */
1095 if (level > 255) level = 255;
1096 if (level < 0) level = 0;
1097 if (level == sc->sc_bl_level)
1098 return;
1099 sc->sc_bl_level = level;
1100 if (sc->sc_bl_on == 0)
1101 sc->sc_bl_on = 1;
1102 level = 255 - level;
1103 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
1104 reg &= ~R128_LEVEL_MASK;
1105 reg |= (level << R128_LEVEL_SHIFT);
1106 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
1107 DPRINTF("backlight level: %d reg %08x\n", level, reg);
1108 }
1109
1110 static void
1111 r128fb_switch_backlight(struct r128fb_softc *sc, int on)
1112 {
1113 uint32_t reg;
1114 int level;
1115
1116 if (on == sc->sc_bl_on)
1117 return;
1118 sc->sc_bl_on = on;
1119 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
1120 reg &= ~R128_LEVEL_MASK;
1121 level = on ? 255 - sc->sc_bl_level : 255;
1122 reg |= level << R128_LEVEL_SHIFT;
1123 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
1124 DPRINTF("backlight state: %d reg %08x\n", on, reg);
1125 }
1126
1127
1128 static void
1129 r128fb_brightness_up(device_t dev)
1130 {
1131 struct r128fb_softc *sc = device_private(dev);
1132
1133 r128fb_set_backlight(sc, sc->sc_bl_level + 8);
1134 }
1135
1136 static void
1137 r128fb_brightness_down(device_t dev)
1138 {
1139 struct r128fb_softc *sc = device_private(dev);
1140
1141 r128fb_set_backlight(sc, sc->sc_bl_level - 8);
1142 }
1143