r128fb.c revision 1.23 1 /* $NetBSD: r128fb.c,v 1.23 2011/12/28 09:29:03 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.23 2011/12/28 09:29:03 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_copycols(void *, int, int, int, int);
127 static void r128fb_erasecols(void *, int, int, int, long);
128 static void r128fb_copyrows(void *, int, int, int);
129 static void r128fb_eraserows(void *, int, int, long);
130
131 static void r128fb_brightness_up(device_t);
132 static void r128fb_brightness_down(device_t);
133 /* set backlight level */
134 static void r128fb_set_backlight(struct r128fb_softc *, int);
135 /* turn backlight on and off without messing with the level */
136 static void r128fb_switch_backlight(struct r128fb_softc *, int);
137
138 struct wsdisplay_accessops r128fb_accessops = {
139 r128fb_ioctl,
140 r128fb_mmap,
141 NULL, /* alloc_screen */
142 NULL, /* free_screen */
143 NULL, /* show_screen */
144 NULL, /* load_font */
145 NULL, /* pollc */
146 NULL /* scroll */
147 };
148
149 static inline void
150 r128fb_wait(struct r128fb_softc *sc, int slots)
151 {
152 uint32_t reg;
153
154 do {
155 reg = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
156 R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK);
157 } while (reg <= slots);
158 }
159
160 static void
161 r128fb_flush_engine(struct r128fb_softc *sc)
162 {
163 uint32_t reg;
164
165 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT);
166 reg |= R128_PC_FLUSH_ALL;
167 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT, reg);
168 do {
169 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
170 R128_PC_NGUI_CTLSTAT);
171 } while (reg & R128_PC_BUSY);
172 }
173
174 static int
175 r128fb_match(device_t parent, cfdata_t match, void *aux)
176 {
177 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
178
179 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
180 return 0;
181 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ATI)
182 return 0;
183
184 /* only cards tested on so far - likely need a list */
185 if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE1AGP4XT) ||
186 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE3AGP4XT) ||
187 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGEGLPCI) ||
188 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE_MOB_M3_AGP))
189 return 100;
190 return (0);
191 }
192
193 static void
194 r128fb_attach(device_t parent, device_t self, void *aux)
195 {
196 struct r128fb_softc *sc = device_private(self);
197 struct pci_attach_args *pa = aux;
198 struct rasops_info *ri;
199 bus_space_tag_t tag;
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, flags;
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_info(pa->pa_pc, pa->pa_tag, 0x10, PCI_MAPREG_TYPE_MEM,
240 &sc->sc_fb, &sc->sc_fbsize, &flags)) {
241 aprint_error("%s: failed to map the frame buffer.\n",
242 device_xname(sc->sc_dev));
243 }
244
245 if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, 0,
246 &tag, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
247 aprint_error("%s: failed to map registers.\n",
248 device_xname(sc->sc_dev));
249 }
250
251 aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
252 (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
253
254 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
255 "default",
256 0, 0,
257 NULL,
258 8, 16,
259 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
260 NULL
261 };
262 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
263 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
264 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
265 sc->sc_locked = 0;
266
267 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
268 &r128fb_accessops);
269 sc->vd.init_screen = r128fb_init_screen;
270
271 /* init engine here */
272 r128fb_init(sc);
273
274 ri = &sc->sc_console_screen.scr_ri;
275
276 j = 0;
277 if (sc->sc_depth == 8) {
278 /* generate an r3g3b2 colour map */
279 for (i = 0; i < 256; i++) {
280 sc->sc_cmap_red[i] = i & 0xe0;
281 sc->sc_cmap_green[i] = (i & 0x1c) << 3;
282 sc->sc_cmap_blue[i] = (i & 0x03) << 6;
283 r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
284 sc->sc_cmap_green[i],
285 sc->sc_cmap_blue[i]);
286 }
287 } else {
288 /* steal rasops' ANSI cmap */
289 for (i = 0; i < 256; i++) {
290 sc->sc_cmap_red[i] = rasops_cmap[j];
291 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
292 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
293 r128fb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
294 rasops_cmap[j + 2]);
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 ri->ri_ops.putchar = r128fb_putchar;
538 }
539
540 static int
541 r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
542 {
543 u_char *r, *g, *b;
544 u_int index = cm->index;
545 u_int count = cm->count;
546 int i, error;
547 u_char rbuf[256], gbuf[256], bbuf[256];
548
549 #ifdef R128FB_DEBUG
550 aprint_debug("putcmap: %d %d\n",index, count);
551 #endif
552 if (cm->index >= 256 || cm->count > 256 ||
553 (cm->index + cm->count) > 256)
554 return EINVAL;
555 error = copyin(cm->red, &rbuf[index], count);
556 if (error)
557 return error;
558 error = copyin(cm->green, &gbuf[index], count);
559 if (error)
560 return error;
561 error = copyin(cm->blue, &bbuf[index], count);
562 if (error)
563 return error;
564
565 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
566 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
567 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
568
569 r = &sc->sc_cmap_red[index];
570 g = &sc->sc_cmap_green[index];
571 b = &sc->sc_cmap_blue[index];
572
573 for (i = 0; i < count; i++) {
574 r128fb_putpalreg(sc, index, *r, *g, *b);
575 index++;
576 r++, g++, b++;
577 }
578 return 0;
579 }
580
581 static int
582 r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
583 {
584 u_int index = cm->index;
585 u_int count = cm->count;
586 int error;
587
588 if (index >= 255 || count > 256 || index + count > 256)
589 return EINVAL;
590
591 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
592 if (error)
593 return error;
594 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
595 if (error)
596 return error;
597 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
598 if (error)
599 return error;
600
601 return 0;
602 }
603
604 static void
605 r128fb_restore_palette(struct r128fb_softc *sc)
606 {
607 int i;
608
609 for (i = 0; i < (1 << sc->sc_depth); i++) {
610 r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
611 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
612 }
613 }
614
615 static int
616 r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
617 uint8_t b)
618 {
619 uint32_t reg;
620
621 /* whack the DAC */
622 reg = (r << 16) | (g << 8) | b;
623 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx);
624 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg);
625 return 0;
626 }
627
628 static void
629 r128fb_init(struct r128fb_softc *sc)
630 {
631 uint32_t datatype;
632
633 r128fb_flush_engine(sc);
634
635 r128fb_wait(sc, 9);
636 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0);
637 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0);
638 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH,
639 sc->sc_stride >> 3);
640 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0);
641 bus_space_write_4(sc->sc_memt, sc->sc_regh,
642 R128_DEFAULT_SC_BOTTOM_RIGHT,
643 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
644 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0);
645 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_BOTTOM_RIGHT,
646 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
647 bus_space_write_4(sc->sc_memt, sc->sc_regh,
648 R128_DEFAULT_SC_BOTTOM_RIGHT,
649 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
650
651 #if BYTE_ORDER == BIG_ENDIAN
652 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE,
653 R128_HOST_BIG_ENDIAN_EN);
654 #else
655 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE, 0);
656 #endif
657
658 r128fb_wait(sc, 5);
659 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH,
660 sc->sc_stride >> 3);
661 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH,
662 sc->sc_stride >> 3);
663 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0);
664 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0);
665 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK,
666 0xffffffff);
667
668 switch (sc->sc_depth) {
669 case 8:
670 datatype = R128_GMC_DST_8BPP_CI;
671 break;
672 case 15:
673 datatype = R128_GMC_DST_15BPP;
674 break;
675 case 16:
676 datatype = R128_GMC_DST_16BPP;
677 break;
678 case 24:
679 datatype = R128_GMC_DST_24BPP;
680 break;
681 case 32:
682 datatype = R128_GMC_DST_32BPP;
683 break;
684 default:
685 aprint_error("%s: unsupported depth %d\n",
686 device_xname(sc->sc_dev), sc->sc_depth);
687 return;
688 }
689 sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS |
690 R128_GMC_AUX_CLIP_DIS | datatype;
691
692 r128fb_flush_engine(sc);
693 }
694
695 static void
696 r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he,
697 uint32_t colour)
698 {
699
700 r128fb_wait(sc, 5);
701 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
702 R128_GMC_BRUSH_SOLID_COLOR |
703 R128_GMC_SRC_DATATYPE_COLOR |
704 R128_ROP3_P |
705 sc->sc_master_cntl);
706
707 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR,
708 colour);
709 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL,
710 R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
711 /* now feed it coordinates */
712 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
713 (x << 16) | y);
714 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
715 (wi << 16) | he);
716 }
717
718 static void
719 r128fb_bitblt(struct r128fb_softc *sc, int xs, int ys, int xd, int yd,
720 int wi, int he, int rop)
721 {
722 uint32_t dp_cntl = 0;
723
724 r128fb_wait(sc, 5);
725 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
726 R128_GMC_BRUSH_SOLID_COLOR |
727 R128_GMC_SRC_DATATYPE_COLOR |
728 rop |
729 R128_DP_SRC_SOURCE_MEMORY |
730 sc->sc_master_cntl);
731
732 if (yd <= ys) {
733 dp_cntl = R128_DST_Y_TOP_TO_BOTTOM;
734 } else {
735 ys += he - 1;
736 yd += he - 1;
737 }
738 if (xd <= xs) {
739 dp_cntl |= R128_DST_X_LEFT_TO_RIGHT;
740 } else {
741 xs += wi - 1;
742 xd += wi - 1;
743 }
744 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl);
745
746 /* now feed it coordinates */
747 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y,
748 (xs << 16) | ys);
749 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
750 (xd << 16) | yd);
751 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
752 (wi << 16) | he);
753 }
754
755 static void
756 r128fb_cursor(void *cookie, int on, int row, int col)
757 {
758 struct rasops_info *ri = cookie;
759 struct vcons_screen *scr = ri->ri_hw;
760 struct r128fb_softc *sc = scr->scr_cookie;
761 int x, y, wi, he;
762
763 wi = ri->ri_font->fontwidth;
764 he = ri->ri_font->fontheight;
765
766 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
767 x = ri->ri_ccol * wi + ri->ri_xorigin;
768 y = ri->ri_crow * he + ri->ri_yorigin;
769 if (ri->ri_flg & RI_CURSOR) {
770 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
771 ri->ri_flg &= ~RI_CURSOR;
772 }
773 ri->ri_crow = row;
774 ri->ri_ccol = col;
775 if (on) {
776 x = ri->ri_ccol * wi + ri->ri_xorigin;
777 y = ri->ri_crow * he + ri->ri_yorigin;
778 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
779 ri->ri_flg |= RI_CURSOR;
780 }
781 } else {
782 scr->scr_ri.ri_crow = row;
783 scr->scr_ri.ri_ccol = col;
784 scr->scr_ri.ri_flg &= ~RI_CURSOR;
785 }
786
787 }
788
789 static void
790 r128fb_putchar(void *cookie, int row, int col, u_int c, long attr)
791 {
792 struct rasops_info *ri = cookie;
793 struct wsdisplay_font *font = PICK_FONT(ri, c);
794 struct vcons_screen *scr = ri->ri_hw;
795 struct r128fb_softc *sc = scr->scr_cookie;
796 void *data;
797 uint32_t fg, bg;
798 int uc, i;
799 int x, y, wi, he, offset;
800
801 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
802 return;
803
804 if (!CHAR_IN_FONT(c, font))
805 return;
806
807 wi = font->fontwidth;
808 he = font->fontheight;
809
810 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
811 fg = ri->ri_devcmap[(attr >> 24) & 0xf];
812 x = ri->ri_xorigin + col * wi;
813 y = ri->ri_yorigin + row * he;
814 if (c == 0x20) {
815 r128fb_rectfill(sc, x, y, wi, he, bg);
816 return;
817 }
818
819 uc = c - font->firstchar;
820 data = (uint8_t *)font->data + uc * ri->ri_fontscale;
821 if (font->stride < wi) {
822 /* this is a mono bitmap font */
823
824 r128fb_wait(sc, 8);
825
826 bus_space_write_4(sc->sc_memt, sc->sc_regh,
827 R128_DP_GUI_MASTER_CNTL,
828 R128_GMC_BRUSH_SOLID_COLOR |
829 R128_GMC_SRC_DATATYPE_MONO_FG_BG |
830 R128_ROP3_S |
831 R128_DP_SRC_SOURCE_HOST_DATA |
832 R128_GMC_DST_CLIPPING |
833 sc->sc_master_cntl);
834
835 bus_space_write_4(sc->sc_memt, sc->sc_regh,
836 R128_DP_CNTL,
837 R128_DST_Y_TOP_TO_BOTTOM |
838 R128_DST_X_LEFT_TO_RIGHT);
839
840 bus_space_write_4(sc->sc_memt, sc->sc_regh,
841 R128_DP_SRC_FRGD_CLR, fg);
842 bus_space_write_4(sc->sc_memt, sc->sc_regh,
843 R128_DP_SRC_BKGD_CLR, bg);
844
845 /*
846 * The Rage 128 doesn't have anything to skip pixels
847 * when colour expanding but all coordinates
848 * are signed so we just clip the leading bytes and
849 * trailing bits away
850 */
851 bus_space_write_4(sc->sc_memt, sc->sc_regh,
852 R128_SC_RIGHT, x + wi - 1);
853 bus_space_write_4(sc->sc_memt, sc->sc_regh,
854 R128_SC_LEFT, x);
855
856 /* needed? */
857 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y, 0);
858
859 offset = 32 - (ri->ri_font->stride << 3);
860 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
861 ((x - offset) << 16) | y);
862 bus_space_write_4(sc->sc_memt, sc->sc_regh,
863 R128_DST_WIDTH_HEIGHT, (32 << 16) | he);
864
865 r128fb_wait(sc, he);
866 switch (ri->ri_font->stride) {
867 case 1: {
868 uint8_t *data8 = data;
869 uint32_t reg;
870 for (i = 0; i < he; i++) {
871 reg = *data8;
872 bus_space_write_stream_4(sc->sc_memt,
873 sc->sc_regh,
874 R128_HOST_DATA0, reg);
875 data8++;
876 }
877 break;
878 }
879 case 2: {
880 uint16_t *data16 = data;
881 uint32_t reg;
882 for (i = 0; i < he; i++) {
883 reg = *data16;
884 bus_space_write_stream_4(sc->sc_memt,
885 sc->sc_regh,
886 R128_HOST_DATA0, reg);
887 data16++;
888 }
889 break;
890 }
891 }
892 } else {
893 /*
894 * this is an alpha font
895 * for now we only support this in 8 bit r3g3b2 colour
896 */
897 uint32_t latch = 0, bg8, fg8, pixel;
898 int r, g, b, aval;
899 int r1, g1, b1, r0, g0, b0, fgo, bgo;
900 uint8_t *data8 = data;
901
902 r128fb_wait(sc, 5);
903 bus_space_write_4(sc->sc_memt, sc->sc_regh,
904 R128_DP_GUI_MASTER_CNTL,
905 R128_GMC_BRUSH_SOLID_COLOR |
906 R128_GMC_SRC_DATATYPE_COLOR |
907 R128_ROP3_S |
908 R128_DP_SRC_SOURCE_HOST_DATA |
909 sc->sc_master_cntl);
910
911 bus_space_write_4(sc->sc_memt, sc->sc_regh,
912 R128_DP_CNTL,
913 R128_DST_Y_TOP_TO_BOTTOM |
914 R128_DST_X_LEFT_TO_RIGHT);
915
916 /* needed? */
917 bus_space_write_4(sc->sc_memt, sc->sc_regh,
918 R128_SRC_X_Y, 0);
919 bus_space_write_4(sc->sc_memt, sc->sc_regh,
920 R128_DST_X_Y, (x << 16) | y);
921 bus_space_write_4(sc->sc_memt, sc->sc_regh,
922 R128_DST_WIDTH_HEIGHT, (wi << 16) | he);
923
924 /*
925 * we need the RGB colours here, so get offsets into
926 * rasops_cmap
927 */
928 fgo = ((attr >> 24) & 0xf) * 3;
929 bgo = ((attr >> 16) & 0xf) * 3;
930
931 r0 = rasops_cmap[bgo];
932 r1 = rasops_cmap[fgo];
933 g0 = rasops_cmap[bgo + 1];
934 g1 = rasops_cmap[fgo + 1];
935 b0 = rasops_cmap[bgo + 2];
936 b1 = rasops_cmap[fgo + 2];
937 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
938 bg8 = R3G3B2(r0, g0, b0);
939 fg8 = R3G3B2(r1, g1, b1);
940 for (i = 0; i < ri->ri_fontscale; i++) {
941 aval = *data8;
942 if (aval == 0) {
943 pixel = bg8;
944 } else if (aval == 255) {
945 pixel = fg8;
946 } else {
947 r = aval * r1 + (255 - aval) * r0;
948 g = aval * g1 + (255 - aval) * g0;
949 b = aval * b1 + (255 - aval) * b0;
950 pixel = ((r & 0xe000) >> 8) |
951 ((g & 0xe000) >> 11) |
952 ((b & 0xc000) >> 14);
953 }
954 latch = (latch << 8) | pixel;
955 /* write in 32bit chunks */
956 if ((i & 3) == 3) {
957 bus_space_write_stream_4(sc->sc_memt,
958 sc->sc_regh, R128_HOST_DATA0, latch);
959 /*
960 * not strictly necessary, old data
961 * should be shifted out
962 */
963 latch = 0;
964 }
965 data8++;
966 }
967 /* if we have pixels left in latch write them out */
968 if ((i & 3) != 0) {
969 latch = latch << ((4 - (i & 3)) << 3);
970 bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
971 R128_HOST_DATA0, latch);
972 }
973 }
974 }
975
976 static void
977 r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
978 {
979 struct rasops_info *ri = cookie;
980 struct vcons_screen *scr = ri->ri_hw;
981 struct r128fb_softc *sc = scr->scr_cookie;
982 int32_t xs, xd, y, width, height;
983
984 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
985 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
986 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
987 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
988 width = ri->ri_font->fontwidth * ncols;
989 height = ri->ri_font->fontheight;
990 r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S);
991 }
992 }
993
994 static void
995 r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
996 {
997 struct rasops_info *ri = cookie;
998 struct vcons_screen *scr = ri->ri_hw;
999 struct r128fb_softc *sc = scr->scr_cookie;
1000 int32_t x, y, width, height, fg, bg, ul;
1001
1002 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1003 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1004 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1005 width = ri->ri_font->fontwidth * ncols;
1006 height = ri->ri_font->fontheight;
1007 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1008
1009 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1010 }
1011 }
1012
1013 static void
1014 r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1015 {
1016 struct rasops_info *ri = cookie;
1017 struct vcons_screen *scr = ri->ri_hw;
1018 struct r128fb_softc *sc = scr->scr_cookie;
1019 int32_t x, ys, yd, width, height;
1020
1021 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1022 x = ri->ri_xorigin;
1023 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1024 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1025 width = ri->ri_emuwidth;
1026 height = ri->ri_font->fontheight * nrows;
1027 r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S);
1028 }
1029 }
1030
1031 static void
1032 r128fb_eraserows(void *cookie, int row, int nrows, long fillattr)
1033 {
1034 struct rasops_info *ri = cookie;
1035 struct vcons_screen *scr = ri->ri_hw;
1036 struct r128fb_softc *sc = scr->scr_cookie;
1037 int32_t x, y, width, height, fg, bg, ul;
1038
1039 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1040 x = ri->ri_xorigin;
1041 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1042 width = ri->ri_emuwidth;
1043 height = ri->ri_font->fontheight * nrows;
1044 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1045
1046 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1047 }
1048 }
1049
1050 static void
1051 r128fb_set_backlight(struct r128fb_softc *sc, int level)
1052 {
1053 uint32_t reg;
1054
1055 /*
1056 * should we do nothing when backlight is off, should we just store the
1057 * level and use it when turning back on or should we just flip sc_bl_on
1058 * and turn the backlight on?
1059 * For now turn it on so a crashed screensaver can't get the user stuck
1060 * with a dark screen as long as hotkeys work
1061 */
1062 if (level > 255) level = 255;
1063 if (level < 0) level = 0;
1064 if (level == sc->sc_bl_level)
1065 return;
1066 sc->sc_bl_level = level;
1067 if (sc->sc_bl_on == 0)
1068 sc->sc_bl_on = 1;
1069 level = 255 - level;
1070 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
1071 reg &= ~R128_LEVEL_MASK;
1072 reg |= (level << R128_LEVEL_SHIFT);
1073 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
1074 DPRINTF("backlight level: %d reg %08x\n", level, reg);
1075 }
1076
1077 static void
1078 r128fb_switch_backlight(struct r128fb_softc *sc, int on)
1079 {
1080 uint32_t reg;
1081 int level;
1082
1083 if (on == sc->sc_bl_on)
1084 return;
1085 sc->sc_bl_on = on;
1086 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
1087 reg &= ~R128_LEVEL_MASK;
1088 level = on ? 255 - sc->sc_bl_level : 255;
1089 reg |= level << R128_LEVEL_SHIFT;
1090 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
1091 DPRINTF("backlight state: %d reg %08x\n", on, reg);
1092 }
1093
1094
1095 static void
1096 r128fb_brightness_up(device_t dev)
1097 {
1098 struct r128fb_softc *sc = device_private(dev);
1099
1100 r128fb_set_backlight(sc, sc->sc_bl_level + 8);
1101 }
1102
1103 static void
1104 r128fb_brightness_down(device_t dev)
1105 {
1106 struct r128fb_softc *sc = device_private(dev);
1107
1108 r128fb_set_backlight(sc, sc->sc_bl_level - 8);
1109 }
1110