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