r128fb.c revision 1.21 1 /* $NetBSD: r128fb.c,v 1.21 2011/02/15 04:06:43 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.21 2011/02/15 04:06:43 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
63 #ifdef R128FB_DEBUG
64 #define DPRINTF printf
65 #else
66 #define DPRINTF while(0) printf
67 #endif
68
69 struct r128fb_softc {
70 device_t sc_dev;
71
72 pci_chipset_tag_t sc_pc;
73 pcitag_t sc_pcitag;
74
75 bus_space_tag_t sc_memt;
76 bus_space_tag_t sc_iot;
77
78 bus_space_handle_t sc_regh;
79 bus_addr_t sc_fb, sc_reg;
80 bus_size_t sc_fbsize, sc_regsize;
81
82 int sc_width, sc_height, sc_depth, sc_stride;
83 int sc_locked, sc_have_backlight, sc_bl_level, sc_bl_on;
84 struct vcons_screen sc_console_screen;
85 struct wsscreen_descr sc_defaultscreen_descr;
86 const struct wsscreen_descr *sc_screens[1];
87 struct wsscreen_list sc_screenlist;
88 struct vcons_data vd;
89 int sc_mode;
90 u_char sc_cmap_red[256];
91 u_char sc_cmap_green[256];
92 u_char sc_cmap_blue[256];
93 /* engine stuff */
94 uint32_t sc_master_cntl;
95 };
96
97 static int r128fb_match(device_t, cfdata_t, void *);
98 static void r128fb_attach(device_t, device_t, void *);
99
100 CFATTACH_DECL_NEW(r128fb, sizeof(struct r128fb_softc),
101 r128fb_match, r128fb_attach, NULL, NULL);
102
103 extern const u_char rasops_cmap[768];
104
105 static int r128fb_ioctl(void *, void *, u_long, void *, int,
106 struct lwp *);
107 static paddr_t r128fb_mmap(void *, void *, off_t, int);
108 static void r128fb_init_screen(void *, struct vcons_screen *, int, long *);
109
110 static int r128fb_putcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
111 static int r128fb_getcmap(struct r128fb_softc *, struct wsdisplay_cmap *);
112 static void r128fb_restore_palette(struct r128fb_softc *);
113 static int r128fb_putpalreg(struct r128fb_softc *, uint8_t, uint8_t,
114 uint8_t, uint8_t);
115
116 static void r128fb_init(struct r128fb_softc *);
117 static void r128fb_flush_engine(struct r128fb_softc *);
118 static void r128fb_rectfill(struct r128fb_softc *, int, int, int, int,
119 uint32_t);
120 static void r128fb_bitblt(struct r128fb_softc *, int, int, int, int, int,
121 int, int);
122
123 static void r128fb_cursor(void *, int, int, int);
124 static void r128fb_putchar(void *, int, int, u_int, long);
125 static void r128fb_copycols(void *, int, int, int, int);
126 static void r128fb_erasecols(void *, int, int, int, long);
127 static void r128fb_copyrows(void *, int, int, int);
128 static void r128fb_eraserows(void *, int, int, long);
129
130 static void r128fb_brightness_up(device_t);
131 static void r128fb_brightness_down(device_t);
132 /* set backlight level */
133 static void r128fb_set_backlight(struct r128fb_softc *, int);
134 /* turn backlight on and off without messing with the level */
135 static void r128fb_switch_backlight(struct r128fb_softc *, int);
136
137 struct wsdisplay_accessops r128fb_accessops = {
138 r128fb_ioctl,
139 r128fb_mmap,
140 NULL, /* alloc_screen */
141 NULL, /* free_screen */
142 NULL, /* show_screen */
143 NULL, /* load_font */
144 NULL, /* pollc */
145 NULL /* scroll */
146 };
147
148 static inline void
149 r128fb_wait(struct r128fb_softc *sc, int slots)
150 {
151 uint32_t reg;
152
153 do {
154 reg = (bus_space_read_4(sc->sc_memt, sc->sc_regh,
155 R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK);
156 } while (reg <= slots);
157 }
158
159 static void
160 r128fb_flush_engine(struct r128fb_softc *sc)
161 {
162 uint32_t reg;
163
164 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT);
165 reg |= R128_PC_FLUSH_ALL;
166 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PC_NGUI_CTLSTAT, reg);
167 do {
168 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
169 R128_PC_NGUI_CTLSTAT);
170 } while (reg & R128_PC_BUSY);
171 }
172
173 static int
174 r128fb_match(device_t parent, cfdata_t match, void *aux)
175 {
176 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
177
178 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
179 return 0;
180 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_ATI)
181 return 0;
182
183 /* only cards tested on so far - likely need a list */
184 if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE1AGP4XT) ||
185 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE3AGP4XT) ||
186 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGEGLPCI) ||
187 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATI_RAGE_MOB_M3_AGP))
188 return 100;
189 return (0);
190 }
191
192 static void
193 r128fb_attach(device_t parent, device_t self, void *aux)
194 {
195 struct r128fb_softc *sc = device_private(self);
196 struct pci_attach_args *pa = aux;
197 struct rasops_info *ri;
198 bus_space_tag_t tag;
199 char devinfo[256];
200 struct wsemuldisplaydev_attach_args aa;
201 prop_dictionary_t dict;
202 unsigned long defattr;
203 bool is_console;
204 int i, j;
205 uint32_t reg, flags;
206
207 sc->sc_pc = pa->pa_pc;
208 sc->sc_pcitag = pa->pa_tag;
209 sc->sc_memt = pa->pa_memt;
210 sc->sc_iot = pa->pa_iot;
211 sc->sc_dev = self;
212
213 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
214 aprint_normal(": %s\n", devinfo);
215
216 /* fill in parameters from properties */
217 dict = device_properties(self);
218 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
219 aprint_error("%s: no width property\n", device_xname(self));
220 return;
221 }
222 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
223 aprint_error("%s: no height property\n", device_xname(self));
224 return;
225 }
226 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
227 aprint_error("%s: no depth property\n", device_xname(self));
228 return;
229 }
230 if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) {
231 aprint_error("%s: no linebytes property\n",
232 device_xname(self));
233 return;
234 }
235
236 prop_dictionary_get_bool(dict, "is_console", &is_console);
237
238 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x10, PCI_MAPREG_TYPE_MEM,
239 &sc->sc_fb, &sc->sc_fbsize, &flags)) {
240 aprint_error("%s: failed to map the frame buffer.\n",
241 device_xname(sc->sc_dev));
242 }
243
244 if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, 0,
245 &tag, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
246 aprint_error("%s: failed to map registers.\n",
247 device_xname(sc->sc_dev));
248 }
249
250 aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
251 (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
252
253 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
254 "default",
255 0, 0,
256 NULL,
257 8, 16,
258 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
259 NULL
260 };
261 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
262 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
263 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
264 sc->sc_locked = 0;
265
266 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
267 &r128fb_accessops);
268 sc->vd.init_screen = r128fb_init_screen;
269
270 /* init engine here */
271 r128fb_init(sc);
272
273 ri = &sc->sc_console_screen.scr_ri;
274
275 j = 0;
276 for (i = 0; i < (1 << sc->sc_depth); i++) {
277
278 sc->sc_cmap_red[i] = rasops_cmap[j];
279 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
280 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
281 r128fb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
282 rasops_cmap[j + 2]);
283 j += 3;
284 }
285
286 if (is_console) {
287 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
288 &defattr);
289 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
290
291 r128fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
292 ri->ri_devcmap[(defattr >> 16) & 0xff]);
293 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
294 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
295 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
296 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
297 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
298 defattr);
299 vcons_replay_msgbuf(&sc->sc_console_screen);
300 } else {
301 /*
302 * since we're not the console we can postpone the rest
303 * until someone actually allocates a screen for us
304 */
305 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
306 }
307
308 /* no suspend/resume support yet */
309 pmf_device_register(sc->sc_dev, NULL, NULL);
310
311 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
312 DPRINTF("R128_LVDS_GEN_CNTL: %08x\n", reg);
313 if (reg & R128_LVDS_ON) {
314 sc->sc_have_backlight = 1;
315 sc->sc_bl_on = 1;
316 sc->sc_bl_level = 255 -
317 ((reg & R128_LEVEL_MASK) >> R128_LEVEL_SHIFT);
318 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
319 r128fb_brightness_up, TRUE);
320 pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
321 r128fb_brightness_down, TRUE);
322 aprint_verbose("%s: LVDS output is active, enabling backlight"
323 " control\n", device_xname(self));
324 } else
325 sc->sc_have_backlight = 0;
326
327 aa.console = is_console;
328 aa.scrdata = &sc->sc_screenlist;
329 aa.accessops = &r128fb_accessops;
330 aa.accesscookie = &sc->vd;
331
332 config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
333 }
334
335 static int
336 r128fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
337 struct lwp *l)
338 {
339 struct vcons_data *vd = v;
340 struct r128fb_softc *sc = vd->cookie;
341 struct wsdisplay_fbinfo *wdf;
342 struct vcons_screen *ms = vd->active;
343 struct wsdisplay_param *param;
344
345 switch (cmd) {
346 case WSDISPLAYIO_GTYPE:
347 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
348 return 0;
349
350 /* PCI config read/write passthrough. */
351 case PCI_IOC_CFGREAD:
352 case PCI_IOC_CFGWRITE:
353 return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
354 cmd, data, flag, l);
355
356 case WSDISPLAYIO_GET_BUSID:
357 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc, sc->sc_pcitag, data);
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 int new_mode = *(int*)data;
383 if (new_mode != sc->sc_mode) {
384 sc->sc_mode = new_mode;
385 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
386 r128fb_init(sc);
387 r128fb_restore_palette(sc);
388 r128fb_rectfill(sc, 0, 0, sc->sc_width,
389 sc->sc_height, ms->scr_ri.ri_devcmap[
390 (ms->scr_defattr >> 16) & 0xff]);
391 vcons_redraw_screen(ms);
392 }
393 }
394 }
395 return 0;
396
397 case WSDISPLAYIO_GETPARAM:
398 param = (struct wsdisplay_param *)data;
399 if (sc->sc_have_backlight == 0)
400 return EPASSTHROUGH;
401 switch (param->param) {
402 case WSDISPLAYIO_PARAM_BRIGHTNESS:
403 param->min = 0;
404 param->max = 255;
405 param->curval = sc->sc_bl_level;
406 return 0;
407 case WSDISPLAYIO_PARAM_BACKLIGHT:
408 param->min = 0;
409 param->max = 1;
410 param->curval = sc->sc_bl_on;
411 return 0;
412 }
413 return EPASSTHROUGH;
414
415 case WSDISPLAYIO_SETPARAM:
416 param = (struct wsdisplay_param *)data;
417 if (sc->sc_have_backlight == 0)
418 return EPASSTHROUGH;
419 switch (param->param) {
420 case WSDISPLAYIO_PARAM_BRIGHTNESS:
421 r128fb_set_backlight(sc, param->curval);
422 return 0;
423 case WSDISPLAYIO_PARAM_BACKLIGHT:
424 r128fb_switch_backlight(sc, param->curval);
425 return 0;
426 }
427 return EPASSTHROUGH;
428 }
429 return EPASSTHROUGH;
430 }
431
432 static paddr_t
433 r128fb_mmap(void *v, void *vs, off_t offset, int prot)
434 {
435 struct vcons_data *vd = v;
436 struct r128fb_softc *sc = vd->cookie;
437 paddr_t pa;
438
439 /* 'regular' framebuffer mmap()ing */
440 if (offset < sc->sc_fbsize) {
441 pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
442 BUS_SPACE_MAP_LINEAR);
443 return pa;
444 }
445
446 /*
447 * restrict all other mappings to processes with superuser privileges
448 * or the kernel itself
449 */
450 if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
451 NULL) != 0) {
452 aprint_normal("%s: mmap() rejected.\n",
453 device_xname(sc->sc_dev));
454 return -1;
455 }
456
457 if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
458 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
459 BUS_SPACE_MAP_LINEAR);
460 return pa;
461 }
462
463 if ((offset >= sc->sc_reg) &&
464 (offset < (sc->sc_reg + sc->sc_regsize))) {
465 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
466 BUS_SPACE_MAP_LINEAR);
467 return pa;
468 }
469
470 #ifdef PCI_MAGIC_IO_RANGE
471 /* allow mapping of IO space */
472 if ((offset >= PCI_MAGIC_IO_RANGE) &&
473 (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
474 pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
475 0, prot, BUS_SPACE_MAP_LINEAR);
476 return pa;
477 }
478 #endif
479
480 #ifdef OFB_ALLOW_OTHERS
481 if (offset >= 0x80000000) {
482 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
483 BUS_SPACE_MAP_LINEAR);
484 return pa;
485 }
486 #endif
487 return -1;
488 }
489
490 static void
491 r128fb_init_screen(void *cookie, struct vcons_screen *scr,
492 int existing, long *defattr)
493 {
494 struct r128fb_softc *sc = cookie;
495 struct rasops_info *ri = &scr->scr_ri;
496
497 ri->ri_depth = sc->sc_depth;
498 ri->ri_width = sc->sc_width;
499 ri->ri_height = sc->sc_height;
500 ri->ri_stride = sc->sc_stride;
501 ri->ri_flg = RI_CENTER;
502
503 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
504 ri->ri_caps = WSSCREEN_WSCOLORS;
505
506 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
507 sc->sc_width / ri->ri_font->fontwidth);
508
509 ri->ri_hw = scr;
510 ri->ri_ops.copyrows = r128fb_copyrows;
511 ri->ri_ops.copycols = r128fb_copycols;
512 ri->ri_ops.eraserows = r128fb_eraserows;
513 ri->ri_ops.erasecols = r128fb_erasecols;
514 ri->ri_ops.cursor = r128fb_cursor;
515 ri->ri_ops.putchar = r128fb_putchar;
516 }
517
518 static int
519 r128fb_putcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
520 {
521 u_char *r, *g, *b;
522 u_int index = cm->index;
523 u_int count = cm->count;
524 int i, error;
525 u_char rbuf[256], gbuf[256], bbuf[256];
526
527 #ifdef R128FB_DEBUG
528 aprint_debug("putcmap: %d %d\n",index, count);
529 #endif
530 if (cm->index >= 256 || cm->count > 256 ||
531 (cm->index + cm->count) > 256)
532 return EINVAL;
533 error = copyin(cm->red, &rbuf[index], count);
534 if (error)
535 return error;
536 error = copyin(cm->green, &gbuf[index], count);
537 if (error)
538 return error;
539 error = copyin(cm->blue, &bbuf[index], count);
540 if (error)
541 return error;
542
543 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
544 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
545 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
546
547 r = &sc->sc_cmap_red[index];
548 g = &sc->sc_cmap_green[index];
549 b = &sc->sc_cmap_blue[index];
550
551 for (i = 0; i < count; i++) {
552 r128fb_putpalreg(sc, index, *r, *g, *b);
553 index++;
554 r++, g++, b++;
555 }
556 return 0;
557 }
558
559 static int
560 r128fb_getcmap(struct r128fb_softc *sc, struct wsdisplay_cmap *cm)
561 {
562 u_int index = cm->index;
563 u_int count = cm->count;
564 int error;
565
566 if (index >= 255 || count > 256 || index + count > 256)
567 return EINVAL;
568
569 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
570 if (error)
571 return error;
572 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
573 if (error)
574 return error;
575 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
576 if (error)
577 return error;
578
579 return 0;
580 }
581
582 static void
583 r128fb_restore_palette(struct r128fb_softc *sc)
584 {
585 int i;
586
587 for (i = 0; i < (1 << sc->sc_depth); i++) {
588 r128fb_putpalreg(sc, i, sc->sc_cmap_red[i],
589 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
590 }
591 }
592
593 static int
594 r128fb_putpalreg(struct r128fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
595 uint8_t b)
596 {
597 uint32_t reg;
598
599 /* whack the DAC */
600 reg = (r << 16) | (g << 8) | b;
601 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_INDEX, idx);
602 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_PALETTE_DATA, reg);
603 return 0;
604 }
605
606 static void
607 r128fb_init(struct r128fb_softc *sc)
608 {
609 uint32_t datatype;
610
611 r128fb_flush_engine(sc);
612
613 r128fb_wait(sc, 9);
614 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_CRTC_OFFSET, 0);
615 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_OFFSET, 0);
616 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DEFAULT_PITCH,
617 sc->sc_stride >> 3);
618 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_AUX_SC_CNTL, 0);
619 bus_space_write_4(sc->sc_memt, sc->sc_regh,
620 R128_DEFAULT_SC_BOTTOM_RIGHT,
621 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
622 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_TOP_LEFT, 0);
623 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SC_BOTTOM_RIGHT,
624 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
625 bus_space_write_4(sc->sc_memt, sc->sc_regh,
626 R128_DEFAULT_SC_BOTTOM_RIGHT,
627 R128_DEFAULT_SC_RIGHT_MAX | R128_DEFAULT_SC_BOTTOM_MAX);
628
629 #if BYTE_ORDER == BIG_ENDIAN
630 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE,
631 R128_HOST_BIG_ENDIAN_EN);
632 #else
633 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_DATATYPE, 0);
634 #endif
635
636 r128fb_wait(sc, 5);
637 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_PITCH,
638 sc->sc_stride >> 3);
639 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_PITCH,
640 sc->sc_stride >> 3);
641 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_OFFSET, 0);
642 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_OFFSET, 0);
643 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_WRITE_MASK,
644 0xffffffff);
645
646 switch (sc->sc_depth) {
647 case 8:
648 datatype = R128_GMC_DST_8BPP_CI;
649 break;
650 case 15:
651 datatype = R128_GMC_DST_15BPP;
652 break;
653 case 16:
654 datatype = R128_GMC_DST_16BPP;
655 break;
656 case 24:
657 datatype = R128_GMC_DST_24BPP;
658 break;
659 case 32:
660 datatype = R128_GMC_DST_32BPP;
661 break;
662 default:
663 aprint_error("%s: unsupported depth %d\n",
664 device_xname(sc->sc_dev), sc->sc_depth);
665 return;
666 }
667 sc->sc_master_cntl = R128_GMC_CLR_CMP_CNTL_DIS |
668 R128_GMC_AUX_CLIP_DIS | datatype;
669
670 r128fb_flush_engine(sc);
671 }
672
673 static void
674 r128fb_rectfill(struct r128fb_softc *sc, int x, int y, int wi, int he,
675 uint32_t colour)
676 {
677
678 r128fb_wait(sc, 5);
679 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
680 R128_GMC_BRUSH_SOLID_COLOR |
681 R128_GMC_SRC_DATATYPE_COLOR |
682 R128_ROP3_P |
683 sc->sc_master_cntl);
684
685 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_BRUSH_FRGD_CLR,
686 colour);
687 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL,
688 R128_DST_X_LEFT_TO_RIGHT | R128_DST_Y_TOP_TO_BOTTOM);
689 /* now feed it coordinates */
690 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
691 (x << 16) | y);
692 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_WIDTH_HEIGHT,
693 (wi << 16) | he);
694 }
695
696 static void
697 r128fb_bitblt(struct r128fb_softc *sc, int xs, int ys, int xd, int yd,
698 int wi, int he, int rop)
699 {
700 uint32_t dp_cntl = 0;
701
702 r128fb_wait(sc, 5);
703 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_GUI_MASTER_CNTL,
704 R128_GMC_BRUSH_SOLID_COLOR |
705 R128_GMC_SRC_DATATYPE_COLOR |
706 rop |
707 R128_DP_SRC_SOURCE_MEMORY |
708 sc->sc_master_cntl);
709
710 if (yd <= ys) {
711 dp_cntl = R128_DST_Y_TOP_TO_BOTTOM;
712 } else {
713 ys += he - 1;
714 yd += he - 1;
715 }
716 if (xd <= xs) {
717 dp_cntl |= R128_DST_X_LEFT_TO_RIGHT;
718 } else {
719 xs += wi - 1;
720 xd += wi - 1;
721 }
722 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DP_CNTL, dp_cntl);
723
724 /* now feed it coordinates */
725 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_SRC_X_Y,
726 (xs << 16) | ys);
727 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_DST_X_Y,
728 (xd << 16) | yd);
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_cursor(void *cookie, int on, int row, int col)
735 {
736 struct rasops_info *ri = cookie;
737 struct vcons_screen *scr = ri->ri_hw;
738 struct r128fb_softc *sc = scr->scr_cookie;
739 int x, y, wi, he;
740
741 wi = ri->ri_font->fontwidth;
742 he = ri->ri_font->fontheight;
743
744 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
745 x = ri->ri_ccol * wi + ri->ri_xorigin;
746 y = ri->ri_crow * he + ri->ri_yorigin;
747 if (ri->ri_flg & RI_CURSOR) {
748 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
749 ri->ri_flg &= ~RI_CURSOR;
750 }
751 ri->ri_crow = row;
752 ri->ri_ccol = col;
753 if (on) {
754 x = ri->ri_ccol * wi + ri->ri_xorigin;
755 y = ri->ri_crow * he + ri->ri_yorigin;
756 r128fb_bitblt(sc, x, y, x, y, wi, he, R128_ROP3_Dn);
757 ri->ri_flg |= RI_CURSOR;
758 }
759 } else {
760 scr->scr_ri.ri_crow = row;
761 scr->scr_ri.ri_ccol = col;
762 scr->scr_ri.ri_flg &= ~RI_CURSOR;
763 }
764
765 }
766
767 static void
768 r128fb_putchar(void *cookie, int row, int col, u_int c, long attr)
769 {
770 struct rasops_info *ri = cookie;
771 struct wsdisplay_font *font = PICK_FONT(ri, c);
772 struct vcons_screen *scr = ri->ri_hw;
773 struct r128fb_softc *sc = scr->scr_cookie;
774
775 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
776 void *data;
777 uint32_t fg, bg;
778 int uc, i;
779 int x, y, wi, he, offset;
780
781 wi = font->fontwidth;
782 he = font->fontheight;
783
784 if (!CHAR_IN_FONT(c, font))
785 return;
786 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
787 fg = ri->ri_devcmap[(attr >> 24) & 0xf];
788 x = ri->ri_xorigin + col * wi;
789 y = ri->ri_yorigin + row * he;
790 if (c == 0x20) {
791 r128fb_rectfill(sc, x, y, wi, he, bg);
792 } else {
793 uc = c - font->firstchar;
794 data = (uint8_t *)font->data + uc * ri->ri_fontscale;
795
796 r128fb_wait(sc, 8);
797
798 bus_space_write_4(sc->sc_memt, sc->sc_regh,
799 R128_DP_GUI_MASTER_CNTL,
800 R128_GMC_BRUSH_SOLID_COLOR |
801 R128_GMC_SRC_DATATYPE_MONO_FG_BG |
802 R128_ROP3_S |
803 R128_DP_SRC_SOURCE_HOST_DATA |
804 R128_GMC_DST_CLIPPING |
805 sc->sc_master_cntl);
806
807 bus_space_write_4(sc->sc_memt, sc->sc_regh,
808 R128_DP_CNTL,
809 R128_DST_Y_TOP_TO_BOTTOM |
810 R128_DST_X_LEFT_TO_RIGHT);
811
812 bus_space_write_4(sc->sc_memt, sc->sc_regh,
813 R128_DP_SRC_FRGD_CLR, fg);
814 bus_space_write_4(sc->sc_memt, sc->sc_regh,
815 R128_DP_SRC_BKGD_CLR, bg);
816
817 /*
818 * The Rage 128 doesn't have anything to skip pixels
819 * when colour expanding but all coordinates
820 * are signed so we just clip the leading bytes and
821 * trailing bits away
822 */
823 bus_space_write_4(sc->sc_memt, sc->sc_regh,
824 R128_SC_RIGHT, x + wi - 1);
825 bus_space_write_4(sc->sc_memt, sc->sc_regh,
826 R128_SC_LEFT, x);
827
828 /* needed? */
829 bus_space_write_4(sc->sc_memt, sc->sc_regh,
830 R128_SRC_X_Y, 0);
831
832 offset = 32 - (ri->ri_font->stride << 3);
833 bus_space_write_4(sc->sc_memt, sc->sc_regh,
834 R128_DST_X_Y, ((x - offset) << 16) | y);
835 bus_space_write_4(sc->sc_memt, sc->sc_regh,
836 R128_DST_WIDTH_HEIGHT, (32 << 16) | he);
837
838 r128fb_wait(sc, he);
839 switch (ri->ri_font->stride) {
840 case 1: {
841 uint8_t *data8 = data;
842 uint32_t reg;
843 for (i = 0; i < he; i++) {
844 reg = *data8;
845 bus_space_write_stream_4(sc->sc_memt,
846 sc->sc_regh,
847 R128_HOST_DATA0, reg);
848 data8++;
849 }
850 break;
851 }
852 case 2: {
853 uint16_t *data16 = data;
854 uint32_t reg;
855 for (i = 0; i < he; i++) {
856 reg = *data16;
857 bus_space_write_stream_4(sc->sc_memt,
858 sc->sc_regh,
859 R128_HOST_DATA0, reg);
860 data16++;
861 }
862 break;
863 }
864 }
865 }
866 }
867 }
868
869 static void
870 r128fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
871 {
872 struct rasops_info *ri = cookie;
873 struct vcons_screen *scr = ri->ri_hw;
874 struct r128fb_softc *sc = scr->scr_cookie;
875 int32_t xs, xd, y, width, height;
876
877 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
878 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
879 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
880 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
881 width = ri->ri_font->fontwidth * ncols;
882 height = ri->ri_font->fontheight;
883 r128fb_bitblt(sc, xs, y, xd, y, width, height, R128_ROP3_S);
884 }
885 }
886
887 static void
888 r128fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
889 {
890 struct rasops_info *ri = cookie;
891 struct vcons_screen *scr = ri->ri_hw;
892 struct r128fb_softc *sc = scr->scr_cookie;
893 int32_t x, y, width, height, fg, bg, ul;
894
895 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
896 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
897 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
898 width = ri->ri_font->fontwidth * ncols;
899 height = ri->ri_font->fontheight;
900 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
901
902 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
903 }
904 }
905
906 static void
907 r128fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
908 {
909 struct rasops_info *ri = cookie;
910 struct vcons_screen *scr = ri->ri_hw;
911 struct r128fb_softc *sc = scr->scr_cookie;
912 int32_t x, ys, yd, width, height;
913
914 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
915 x = ri->ri_xorigin;
916 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
917 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
918 width = ri->ri_emuwidth;
919 height = ri->ri_font->fontheight * nrows;
920 r128fb_bitblt(sc, x, ys, x, yd, width, height, R128_ROP3_S);
921 }
922 }
923
924 static void
925 r128fb_eraserows(void *cookie, int row, int nrows, long fillattr)
926 {
927 struct rasops_info *ri = cookie;
928 struct vcons_screen *scr = ri->ri_hw;
929 struct r128fb_softc *sc = scr->scr_cookie;
930 int32_t x, y, width, height, fg, bg, ul;
931
932 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
933 x = ri->ri_xorigin;
934 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
935 width = ri->ri_emuwidth;
936 height = ri->ri_font->fontheight * nrows;
937 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
938
939 r128fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
940 }
941 }
942
943 static void
944 r128fb_set_backlight(struct r128fb_softc *sc, int level)
945 {
946 uint32_t reg;
947
948 /*
949 * should we do nothing when backlight is off, should we just store the
950 * level and use it when turning back on or should we just flip sc_bl_on
951 * and turn the backlight on?
952 * For now turn it on so a crashed screensaver can't get the user stuck
953 * with a dark screen as long as hotkeys work
954 */
955 if (level > 255) level = 255;
956 if (level < 0) level = 0;
957 if (level == sc->sc_bl_level)
958 return;
959 sc->sc_bl_level = level;
960 if (sc->sc_bl_on == 0)
961 sc->sc_bl_on = 1;
962 level = 255 - level;
963 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
964 reg &= ~R128_LEVEL_MASK;
965 reg |= (level << R128_LEVEL_SHIFT);
966 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
967 DPRINTF("backlight level: %d reg %08x\n", level, reg);
968 }
969
970 static void
971 r128fb_switch_backlight(struct r128fb_softc *sc, int on)
972 {
973 uint32_t reg;
974 int level;
975
976 if (on == sc->sc_bl_on)
977 return;
978 sc->sc_bl_on = on;
979 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL);
980 reg &= ~R128_LEVEL_MASK;
981 level = on ? 255 - sc->sc_bl_level : 255;
982 reg |= level << R128_LEVEL_SHIFT;
983 bus_space_write_4(sc->sc_memt, sc->sc_regh, R128_LVDS_GEN_CNTL, reg);
984 DPRINTF("backlight state: %d reg %08x\n", on, reg);
985 }
986
987
988 static void
989 r128fb_brightness_up(device_t dev)
990 {
991 struct r128fb_softc *sc = device_private(dev);
992
993 r128fb_set_backlight(sc, sc->sc_bl_level + 8);
994 }
995
996 static void
997 r128fb_brightness_down(device_t dev)
998 {
999 struct r128fb_softc *sc = device_private(dev);
1000
1001 r128fb_set_backlight(sc, sc->sc_bl_level - 8);
1002 }
1003