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