xref: /netbsd-src/sys/arch/hpcmips/dev/plumvideo.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: plumvideo.c,v 1.37 2005/12/11 12:17:33 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 1999-2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: plumvideo.c,v 1.37 2005/12/11 12:17:33 christos Exp $");
41 
42 #undef PLUMVIDEODEBUG
43 
44 #include "plumohci.h" /* Plum2 OHCI shared memory allocated on V-RAM */
45 #include "bivideo.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/device.h>
50 
51 #include <sys/ioctl.h>
52 #include <sys/buf.h>
53 #include <uvm/uvm_extern.h>
54 
55 #include <dev/cons.h> /* consdev */
56 
57 #include <mips/cache.h>
58 
59 #include <machine/bus.h>
60 #include <machine/intr.h>
61 #include <machine/config_hook.h>
62 
63 #include <hpcmips/tx/tx39var.h>
64 #include <hpcmips/dev/plumvar.h>
65 #include <hpcmips/dev/plumicuvar.h>
66 #include <hpcmips/dev/plumpowervar.h>
67 #include <hpcmips/dev/plumvideoreg.h>
68 
69 #include <machine/bootinfo.h>
70 
71 #include <dev/wscons/wsdisplayvar.h>
72 #include <dev/rasops/rasops.h>
73 #include <dev/hpc/video_subr.h>
74 
75 #include <dev/wscons/wsconsio.h>
76 #include <dev/hpc/hpcfbvar.h>
77 #include <dev/hpc/hpcfbio.h>
78 #if NBIVIDEO > 0
79 #include <dev/hpc/bivideovar.h>
80 #endif
81 
82 #ifdef PLUMVIDEODEBUG
83 int	plumvideo_debug = 1;
84 #define	DPRINTF(arg) if (plumvideo_debug) printf arg;
85 #define	DPRINTFN(n, arg) if (plumvideo_debug > (n)) printf arg;
86 #else
87 #define	DPRINTF(arg)
88 #define DPRINTFN(n, arg)
89 #endif
90 
91 struct plumvideo_softc {
92 	struct device sc_dev;
93 	tx_chipset_tag_t sc_tc;
94 	plum_chipset_tag_t sc_pc;
95 
96 	void *sc_powerhook;	/* power management hook */
97 	int sc_console;
98 
99 	int sc_backlight;
100 	int sc_brightness;
101 	int sc_max_brightness;
102 
103 	/* control register */
104 	bus_space_tag_t sc_regt;
105 	bus_space_handle_t sc_regh;
106 	/* frame buffer */
107 	bus_space_tag_t sc_fbiot;
108 	bus_space_handle_t sc_fbioh;
109 	/* clut buffer (8bpp only) */
110 	bus_space_tag_t sc_clutiot;
111 	bus_space_handle_t sc_clutioh;
112 	/* bitblt */
113 	bus_space_tag_t sc_bitbltt;
114 	bus_space_handle_t sc_bitblth;
115 
116 	struct video_chip sc_chip;
117 	struct hpcfb_fbconf sc_fbconf;
118 	struct hpcfb_dspconf sc_dspconf;
119 };
120 
121 int	plumvideo_match(struct device*, struct cfdata*, void*);
122 void	plumvideo_attach(struct device*, struct device*, void*);
123 
124 int	plumvideo_ioctl(void *, u_long, caddr_t, int, struct lwp *);
125 paddr_t	plumvideo_mmap(void *, off_t, int);
126 
127 CFATTACH_DECL(plumvideo, sizeof(struct plumvideo_softc),
128     plumvideo_match, plumvideo_attach, NULL, NULL);
129 
130 struct hpcfb_accessops plumvideo_ha = {
131 	plumvideo_ioctl, plumvideo_mmap
132 };
133 
134 int	plumvideo_power(void *, int, long, void *);
135 
136 int	plumvideo_init(struct plumvideo_softc *, int *);
137 void	plumvideo_hpcfbinit(struct plumvideo_softc *, int);
138 
139 void	plumvideo_clut_default(struct plumvideo_softc *);
140 void	plumvideo_clut_set(struct plumvideo_softc *, u_int32_t *, int, int);
141 void	plumvideo_clut_get(struct plumvideo_softc *, u_int32_t *, int, int);
142 void	__plumvideo_clut_access(struct plumvideo_softc *, u_int32_t *, int, int,
143 	    void (*)(bus_space_tag_t, bus_space_handle_t, u_int32_t *, int, int));
144 static void _flush_cache(void) __attribute__((__unused__)); /* !!! */
145 static void plumvideo_init_backlight(struct plumvideo_softc *);
146 static void plumvideo_backlight(struct plumvideo_softc *, int);
147 static void plumvideo_brightness(struct plumvideo_softc *, int);
148 
149 #ifdef PLUMVIDEODEBUG
150 void	plumvideo_dump(struct plumvideo_softc*);
151 #endif
152 
153 #define ON	1
154 #define OFF	0
155 
156 int
157 plumvideo_match(struct device *parent, struct cfdata *cf, void *aux)
158 {
159 	/*
160 	 * VRAM area also uses as UHOSTC shared RAM.
161 	 */
162 	return (2); /* 1st attach group */
163 }
164 
165 void
166 plumvideo_attach(struct device *parent, struct device *self, void *aux)
167 {
168 	struct plum_attach_args *pa = aux;
169 	struct plumvideo_softc *sc = (void*)self;
170 	struct hpcfb_attach_args ha;
171 	int console, reverse_flag;
172 
173 	sc->sc_console = console = cn_tab ? 0 : 1;
174 	sc->sc_pc	= pa->pa_pc;
175 	sc->sc_regt	= pa->pa_regt;
176 	sc->sc_fbiot = sc->sc_clutiot = sc->sc_bitbltt  = pa->pa_iot;
177 
178 	printf(": ");
179 
180 	/* map register area */
181 	if (bus_space_map(sc->sc_regt, PLUM_VIDEO_REGBASE,
182 	    PLUM_VIDEO_REGSIZE, 0, &sc->sc_regh)) {
183 		printf("register map failed\n");
184 		return;
185 	}
186 
187 	/* initialize backlight and brightness values */
188 	plumvideo_init_backlight(sc);
189 
190 	/* power control */
191 	plumvideo_power(sc, 0, 0,
192 	    (void *)(console ? PWR_RESUME : PWR_SUSPEND));
193 	/* Add a hard power hook to power saving */
194 	sc->sc_powerhook = config_hook(CONFIG_HOOK_PMEVENT,
195 	    CONFIG_HOOK_PMEVENT_HARDPOWER,
196 	    CONFIG_HOOK_SHARE,
197 	    plumvideo_power, sc);
198 	if (sc->sc_powerhook == 0)
199 		printf("WARNING unable to establish hard power hook");
200 
201 	/*
202 	 *  Initialize LCD controller
203 	 *	map V-RAM area.
204 	 *	reinstall bootinfo structure.
205 	 *	some OHCI shared-buffer hack. XXX
206 	 */
207 	if (plumvideo_init(sc, &reverse_flag) != 0)
208 		return;
209 
210 	printf("\n");
211 
212 	/* Attach frame buffer device */
213 	plumvideo_hpcfbinit(sc, reverse_flag);
214 
215 #ifdef PLUMVIDEODEBUG
216 	if (plumvideo_debug > 0)
217 		plumvideo_dump(sc);
218 	/* attach debug draw routine (debugging use) */
219 	video_attach_drawfunc(&sc->sc_chip);
220 	tx_conf_register_video(sc->sc_pc->pc_tc, &sc->sc_chip);
221 #endif /* PLUMVIDEODEBUG */
222 
223 	if(console && hpcfb_cnattach(&sc->sc_fbconf) != 0) {
224 		panic("plumvideo_attach: can't init fb console");
225 	}
226 
227 	ha.ha_console = console;
228 	ha.ha_accessops = &plumvideo_ha;
229 	ha.ha_accessctx = sc;
230 	ha.ha_curfbconf = 0;
231 	ha.ha_nfbconf = 1;
232 	ha.ha_fbconflist = &sc->sc_fbconf;
233 	ha.ha_curdspconf = 0;
234 	ha.ha_ndspconf = 1;
235 	ha.ha_dspconflist = &sc->sc_dspconf;
236 
237 	config_found(self, &ha, hpcfbprint);
238 #if NBIVIDEO > 0
239 	/* bivideo is no longer need */
240 	bivideo_dont_attach = 1;
241 #endif /* NBIVIDEO > 0 */
242 }
243 
244 void
245 plumvideo_hpcfbinit(struct plumvideo_softc *sc, int reverse_flag)
246 {
247 	struct hpcfb_fbconf *fb = &sc->sc_fbconf;
248 	struct video_chip *chip = &sc->sc_chip;
249 	vaddr_t fbvaddr = (vaddr_t)sc->sc_fbioh;
250 	int height = chip->vc_fbheight;
251 	int width = chip->vc_fbwidth;
252 	int depth = chip->vc_fbdepth;
253 
254 	memset(fb, 0, sizeof(struct hpcfb_fbconf));
255 
256 	fb->hf_conf_index	= 0;	/* configuration index		*/
257 	fb->hf_nconfs		= 1;   	/* how many configurations	*/
258 	strncpy(fb->hf_name, "PLUM built-in video", HPCFB_MAXNAMELEN);
259 	/* frame buffer name		*/
260 	strncpy(fb->hf_conf_name, "LCD", HPCFB_MAXNAMELEN);
261 	/* configuration name		*/
262 	fb->hf_height		= height;
263 	fb->hf_width		= width;
264 	fb->hf_baseaddr		= (u_long)fbvaddr;
265 	fb->hf_offset		= (u_long)fbvaddr - mips_ptob(mips_btop(fbvaddr));
266 	/* frame buffer start offset   	*/
267 	fb->hf_bytes_per_line	= (width * depth) / NBBY;
268 	fb->hf_nplanes		= 1;
269 	fb->hf_bytes_per_plane	= height * fb->hf_bytes_per_line;
270 
271 	fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
272 	fb->hf_access_flags |= HPCFB_ACCESS_WORD;
273 	fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
274 	if (reverse_flag)
275 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
276 
277 	switch (depth) {
278 	default:
279 		panic("plumvideo_hpcfbinit: not supported color depth");
280 		/* NOTREACHED */
281 	case 16:
282 		fb->hf_class = HPCFB_CLASS_RGBCOLOR;
283 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
284 		fb->hf_pack_width = 16;
285 		fb->hf_pixels_per_pack = 1;
286 		fb->hf_pixel_width = 16;
287 
288 		fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
289 		/* reserved for future use */
290 		fb->hf_u.hf_rgb.hf_flags = 0;
291 
292 		fb->hf_u.hf_rgb.hf_red_width = 5;
293 		fb->hf_u.hf_rgb.hf_red_shift = 11;
294 		fb->hf_u.hf_rgb.hf_green_width = 6;
295 		fb->hf_u.hf_rgb.hf_green_shift = 5;
296 		fb->hf_u.hf_rgb.hf_blue_width = 5;
297 		fb->hf_u.hf_rgb.hf_blue_shift = 0;
298 		fb->hf_u.hf_rgb.hf_alpha_width = 0;
299 		fb->hf_u.hf_rgb.hf_alpha_shift = 0;
300 		break;
301 
302 	case 8:
303 		fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
304 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
305 		fb->hf_pack_width = 8;
306 		fb->hf_pixels_per_pack = 1;
307 		fb->hf_pixel_width = 8;
308 		fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
309 		/* reserved for future use */
310 		fb->hf_u.hf_indexed.hf_flags = 0;
311 		break;
312 	}
313 }
314 
315 int
316 plumvideo_init(struct plumvideo_softc *sc, int *reverse)
317 {
318 	struct video_chip *chip = &sc->sc_chip;
319 	bus_space_tag_t regt = sc->sc_regt;
320 	bus_space_handle_t regh = sc->sc_regh;
321 	plumreg_t reg;
322 	size_t vram_size;
323 	int bpp, width, height, vram_pitch;
324 
325 	*reverse = video_reverse_color();
326 	chip->vc_v = sc->sc_pc->pc_tc;
327 #if notyet
328 	/* map BitBlt area */
329 	if (bus_space_map(sc->sc_bitbltt,
330 	    PLUM_VIDEO_BITBLT_IOBASE,
331 	    PLUM_VIDEO_BITBLT_IOSIZE, 0,
332 	    &sc->sc_bitblth)) {
333 		printf(": BitBlt map failed\n");
334 		return (1);
335 	}
336 #endif
337 	reg = plum_conf_read(regt, regh, PLUM_VIDEO_PLGMD_REG);
338 
339 	switch (reg & PLUM_VIDEO_PLGMD_GMODE_MASK) {
340 	case PLUM_VIDEO_PLGMD_16BPP:
341 #if NPLUMOHCI > 0 /* reserve V-RAM area for USB OHCI */
342 		/* FALLTHROUGH */
343 #else
344 		bpp = 16;
345 		break;
346 #endif
347 	default:
348 		bootinfo->fb_type = *reverse ? BIFB_D8_FF : BIFB_D8_00;
349 		reg &= ~PLUM_VIDEO_PLGMD_GMODE_MASK;
350 		plum_conf_write(regt, regh, PLUM_VIDEO_PLGMD_REG, reg);
351 		reg |= PLUM_VIDEO_PLGMD_8BPP;
352 		plum_conf_write(regt, regh, PLUM_VIDEO_PLGMD_REG, reg);
353 #if notyet
354 		/* change BitBlt color depth */
355 		plum_conf_write(sc->sc_bitbltt, sc->sc_bitblth, 0x8, 0);
356 #endif
357 		/* FALLTHROUGH */
358 	case PLUM_VIDEO_PLGMD_8BPP:
359 		bpp = 8;
360 		break;
361 	}
362 	chip->vc_fbdepth = bpp;
363 
364 	/*
365 	 * Get display size from WindowsCE setted.
366 	 */
367 	chip->vc_fbwidth = width = bootinfo->fb_width =
368 	    plum_conf_read(regt, regh, PLUM_VIDEO_PLHPX_REG) + 1;
369 	chip->vc_fbheight = height = bootinfo->fb_height =
370 	    plum_conf_read(regt, regh, PLUM_VIDEO_PLVT_REG) -
371 	    plum_conf_read(regt, regh, PLUM_VIDEO_PLVDS_REG);
372 
373 	/*
374 	 * set line byte length to bootinfo and LCD controller.
375 	 */
376 	vram_pitch = bootinfo->fb_line_bytes = (width * bpp) / NBBY;
377 	plum_conf_write(regt, regh, PLUM_VIDEO_PLPIT1_REG, vram_pitch);
378 	plum_conf_write(regt, regh, PLUM_VIDEO_PLPIT2_REG,
379 	    vram_pitch & PLUM_VIDEO_PLPIT2_MASK);
380 	plum_conf_write(regt, regh, PLUM_VIDEO_PLOFS_REG, vram_pitch);
381 
382 	/*
383 	 * boot messages and map CLUT(if any).
384 	 */
385 	printf("display mode: ");
386 	switch (bpp) {
387 	default:
388 		printf("disabled ");
389 		break;
390 	case 8:
391 		printf("8bpp ");
392 		/* map CLUT area */
393 		if (bus_space_map(sc->sc_clutiot,
394 		    PLUM_VIDEO_CLUT_LCD_IOBASE,
395 		    PLUM_VIDEO_CLUT_LCD_IOSIZE, 0,
396 		    &sc->sc_clutioh)) {
397 			printf(": CLUT map failed\n");
398 			return (1);
399 		}
400 		/* install default CLUT */
401 		plumvideo_clut_default(sc);
402 		break;
403 	case 16:
404 		printf("16bpp ");
405 		break;
406 	}
407 
408 	/*
409 	 * calcurate frame buffer size.
410 	 */
411 	reg = plum_conf_read(regt, regh, PLUM_VIDEO_PLGMD_REG);
412 	vram_size = (width * height * bpp) / NBBY;
413 	vram_size = mips_round_page(vram_size);
414 	chip->vc_fbsize = vram_size;
415 
416 	/*
417 	 * map V-RAM area.
418 	 */
419 	if (bus_space_map(sc->sc_fbiot, PLUM_VIDEO_VRAM_IOBASE,
420 	    vram_size, 0, &sc->sc_fbioh)) {
421 		printf(": V-RAM map failed\n");
422 		return (1);
423 	}
424 
425 	bootinfo->fb_addr = (unsigned char *)sc->sc_fbioh;
426 	chip->vc_fbvaddr = (vaddr_t)sc->sc_fbioh;
427 	chip->vc_fbpaddr = PLUM_VIDEO_VRAM_IOBASE_PHYSICAL;
428 
429 	return (0);
430 }
431 
432 int
433 plumvideo_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct lwp *l)
434 {
435 	struct plumvideo_softc *sc = (struct plumvideo_softc *)v;
436 	struct hpcfb_fbconf *fbconf;
437 	struct hpcfb_dspconf *dspconf;
438 	struct wsdisplay_cmap *cmap;
439 	struct wsdisplay_param *dispparam;
440 	u_int8_t *r, *g, *b;
441 	u_int32_t *rgb;
442 	int idx, error;
443 	size_t cnt;
444 
445 	switch (cmd) {
446 	case WSDISPLAYIO_GETCMAP:
447 		cmap = (struct wsdisplay_cmap *)data;
448 		cnt = cmap->count;
449 		idx = cmap->index;
450 
451 		if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
452 		    sc->sc_fbconf.hf_pack_width != 8 ||
453 		    !LEGAL_CLUT_INDEX(idx) ||
454 		    !LEGAL_CLUT_INDEX(idx + cnt - 1)) {
455 			return (EINVAL);
456 		}
457 
458 		error = cmap_work_alloc(&r, &g, &b, &rgb, cnt);
459 		if (error)
460 			goto out;
461 		plumvideo_clut_get(sc, rgb, idx, cnt);
462 		rgb24_decompose(rgb, r, g, b, cnt);
463 
464 		error = copyout(r, cmap->red, cnt);
465 		if (error)
466 			goto out;
467 		error = copyout(g, cmap->green, cnt);
468 		if (error)
469 			goto out;
470 		error = copyout(b, cmap->blue, cnt);
471 
472 out:
473 		cmap_work_free(r, g, b, rgb);
474 		return error;
475 
476 	case WSDISPLAYIO_PUTCMAP:
477 		cmap = (struct wsdisplay_cmap *)data;
478 		cnt = cmap->count;
479 		idx = cmap->index;
480 
481 		if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
482 		    sc->sc_fbconf.hf_pack_width != 8 ||
483 		    !LEGAL_CLUT_INDEX(idx) ||
484 		    !LEGAL_CLUT_INDEX(idx + cnt - 1)) {
485 			return (EINVAL);
486 		}
487 
488 		error = cmap_work_alloc(&r, &g, &b, &rgb, cnt);
489 		if (error)
490 			goto out;
491 		error = copyin(cmap->red, r, cnt);
492 		if (error)
493 			goto out;
494 		error = copyin(cmap->green, g, cnt);
495 		if (error)
496 			goto out;
497 		error = copyin(cmap->blue, b, cnt);
498 		if (error)
499 			goto out;
500 		rgb24_compose(rgb, r, g, b, cnt);
501 		plumvideo_clut_set(sc, rgb, idx, cnt);
502 		goto out;
503 
504 	case WSDISPLAYIO_SVIDEO:
505 		if (*(int *)data == WSDISPLAYIO_VIDEO_OFF)
506 			plumvideo_backlight(sc, 0);
507 		else
508 			plumvideo_backlight(sc, 1);
509 		return 0;
510 
511 	case WSDISPLAYIO_GVIDEO:
512 		*(int *)data = sc->sc_backlight ?
513 			WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
514 		return 0;
515 
516 	case WSDISPLAYIO_GETPARAM:
517 		dispparam = (struct wsdisplay_param *)data;
518 		switch (dispparam->param) {
519 		case WSDISPLAYIO_PARAM_BACKLIGHT:
520 			dispparam->min = 0;
521 			dispparam->max = 1;
522 			dispparam->curval = sc->sc_backlight;
523 			break;
524 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
525 			if (sc->sc_max_brightness <= 0)
526 				return EINVAL;
527 			dispparam->min = 0;
528 			dispparam->max = sc->sc_max_brightness;
529 			dispparam->curval = sc->sc_brightness;
530 			break;
531 		default:
532 			return EINVAL;
533 		}
534 		return 0;
535 
536 	case WSDISPLAYIO_SETPARAM:
537 		dispparam = (struct wsdisplay_param * )data;
538 		switch (dispparam->param) {
539 		case WSDISPLAYIO_PARAM_BACKLIGHT:
540 			if (dispparam->curval < 0 || 1 < dispparam->curval)
541 				return EINVAL;
542 			plumvideo_backlight(sc, dispparam->curval);
543 			break;
544 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
545 			if (sc->sc_max_brightness <= 0)
546 				return EINVAL;
547 			if (dispparam->curval < 0 ||
548 			    sc->sc_max_brightness < dispparam->curval)
549 				return EINVAL;
550 			plumvideo_brightness(sc, dispparam->curval);
551 			break;
552 		default:
553 			return EINVAL;
554 		}
555 		return 0;
556 
557 	case HPCFBIO_GCONF:
558 		fbconf = (struct hpcfb_fbconf *)data;
559 		if (fbconf->hf_conf_index != 0 &&
560 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
561 			return (EINVAL);
562 		}
563 		*fbconf = sc->sc_fbconf;	/* structure assignment */
564 		return (0);
565 
566 	case HPCFBIO_SCONF:
567 		fbconf = (struct hpcfb_fbconf *)data;
568 		if (fbconf->hf_conf_index != 0 &&
569 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
570 			return (EINVAL);
571 		}
572 		/*
573 		 * nothing to do because we have only one configuration
574 		 */
575 		return (0);
576 
577 	case HPCFBIO_GDSPCONF:
578 		dspconf = (struct hpcfb_dspconf *)data;
579 		if ((dspconf->hd_unit_index != 0 &&
580 		    dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
581 		    (dspconf->hd_conf_index != 0 &&
582 			dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
583 			return (EINVAL);
584 		}
585 		*dspconf = sc->sc_dspconf;	/* structure assignment */
586 		return (0);
587 
588 	case HPCFBIO_SDSPCONF:
589 		dspconf = (struct hpcfb_dspconf *)data;
590 		if ((dspconf->hd_unit_index != 0 &&
591 		    dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
592 		    (dspconf->hd_conf_index != 0 &&
593 			dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
594 			return (EINVAL);
595 		}
596 		/*
597 		 * nothing to do
598 		 * because we have only one unit and one configuration
599 		 */
600 		return (0);
601 
602 	case HPCFBIO_GOP:
603 	case HPCFBIO_SOP:
604 		/* XXX not implemented yet */
605 		return (EINVAL);
606 	}
607 
608 	return (EPASSTHROUGH);
609 }
610 
611 paddr_t
612 plumvideo_mmap(void *ctx, off_t offset, int prot)
613 {
614 	struct plumvideo_softc *sc = (struct plumvideo_softc *)ctx;
615 
616 	if (offset < 0 || (sc->sc_fbconf.hf_bytes_per_plane +
617 	    sc->sc_fbconf.hf_offset) <  offset) {
618 		return (-1);
619 	}
620 
621 	return (mips_btop(PLUM_VIDEO_VRAM_IOBASE_PHYSICAL + offset));
622 }
623 
624 static void __plumvideo_clut_get(bus_space_tag_t, bus_space_handle_t,
625     u_int32_t *, int, int);
626 static void __plumvideo_clut_get(bus_space_tag_t iot, bus_space_handle_t ioh,
627     u_int32_t *rgb, int beg, int cnt)
628 {
629 	int i;
630 
631 	for (i = 0, beg *= 4; i < cnt; i++, beg += 4) {
632 		*rgb++ = bus_space_read_4(iot, ioh, beg) &
633 		    0x00ffffff;
634 	}
635 }
636 
637 void
638 plumvideo_clut_get(struct plumvideo_softc *sc, u_int32_t *rgb, int beg,
639     int cnt)
640 {
641 	KASSERT(rgb);
642 	KASSERT(LEGAL_CLUT_INDEX(beg));
643 	KASSERT(LEGAL_CLUT_INDEX(beg + cnt - 1));
644 	__plumvideo_clut_access(sc, rgb, beg, cnt, __plumvideo_clut_get);
645 }
646 
647 static void __plumvideo_clut_set(bus_space_tag_t, bus_space_handle_t,
648     u_int32_t *, int, int);
649 static void __plumvideo_clut_set(bus_space_tag_t iot, bus_space_handle_t ioh,
650     u_int32_t *rgb, int beg, int cnt)
651 {
652 	int i;
653 
654 	for (i = 0, beg *= 4; i < cnt; i++, beg +=4) {
655 		bus_space_write_4(iot, ioh, beg,
656 		    *rgb++ & 0x00ffffff);
657 	}
658 }
659 
660 void
661 plumvideo_clut_set(struct plumvideo_softc *sc, u_int32_t *rgb, int beg,
662     int cnt)
663 {
664 	KASSERT(rgb);
665 	KASSERT(LEGAL_CLUT_INDEX(beg));
666 	KASSERT(LEGAL_CLUT_INDEX(beg + cnt - 1));
667 	__plumvideo_clut_access(sc, rgb, beg, cnt, __plumvideo_clut_set);
668 }
669 
670 static void __plumvideo_clut_default(bus_space_tag_t, bus_space_handle_t,
671     u_int32_t *, int, int);
672 static void __plumvideo_clut_default(bus_space_tag_t iot, bus_space_handle_t ioh,
673     u_int32_t *rgb, int beg, int cnt)
674 {
675 	static const u_int8_t compo6[6] = { 0, 51, 102, 153, 204, 255 };
676 	static const u_int32_t ansi_color[16] = {
677 		0x000000, 0xff0000, 0x00ff00, 0xffff00,
678 		0x0000ff, 0xff00ff, 0x00ffff, 0xffffff,
679 		0x000000, 0x800000, 0x008000, 0x808000,
680 		0x000080, 0x800080, 0x008080, 0x808080,
681 	};
682 	int i, r, g, b;
683 
684 	/* ANSI escape sequence */
685 	for (i = 0; i < 16; i++) {
686 		bus_space_write_4(iot, ioh, i << 2, ansi_color[i]);
687 	}
688 	/* 16 - 31, gray scale */
689 	for ( ; i < 32; i++) {
690 		int j = (i - 16) * 17;
691 		bus_space_write_4(iot, ioh, i << 2, RGB24(j, j, j));
692 	}
693 	/* 32 - 247, RGB color */
694 	for (r = 0; r < 6; r++) {
695 		for (g = 0; g < 6; g++) {
696 			for (b = 0; b < 6; b++) {
697 				bus_space_write_4(iot, ioh, i << 2,
698 				    RGB24(compo6[r],
699 					compo6[g],
700 					compo6[b]));
701 				i++;
702 			}
703 		}
704 	}
705 	/* 248 - 245, just white */
706 	for ( ; i < 256; i++) {
707 		bus_space_write_4(iot, ioh, i << 2, 0xffffff);
708 	}
709 }
710 
711 void
712 plumvideo_clut_default(struct plumvideo_softc *sc)
713 {
714 	__plumvideo_clut_access(sc, NULL, 0, 256, __plumvideo_clut_default);
715 }
716 
717 void
718 __plumvideo_clut_access(struct plumvideo_softc *sc, u_int32_t *rgb, int beg,
719     int cnt, void (*palette_func)(bus_space_tag_t, bus_space_handle_t,
720     u_int32_t *, int, int))
721 {
722 	bus_space_tag_t regt = sc->sc_regt;
723 	bus_space_handle_t regh = sc->sc_regh;
724 	plumreg_t val, gmode;
725 
726 	/* display off */
727 	val = bus_space_read_4(regt, regh, PLUM_VIDEO_PLGMD_REG);
728 	gmode = val & PLUM_VIDEO_PLGMD_GMODE_MASK;
729 	val &= ~PLUM_VIDEO_PLGMD_GMODE_MASK;
730 	bus_space_write_4(regt, regh, PLUM_VIDEO_PLGMD_REG, val);
731 
732 	/* palette access disable */
733 	val &= ~PLUM_VIDEO_PLGMD_PALETTE_ENABLE;
734 	bus_space_write_4(regt, regh, PLUM_VIDEO_PLGMD_REG, val);
735 
736 	/* change palette mode to CPU */
737 	val &= ~PLUM_VIDEO_PLGMD_MODE_DISPLAY;
738 	bus_space_write_4(regt, regh, PLUM_VIDEO_PLGMD_REG, val);
739 
740 	/* palette access */
741 	(*palette_func) (sc->sc_clutiot, sc->sc_clutioh, rgb, beg, cnt);
742 
743 	/* change palette mode to Display */
744 	val |= PLUM_VIDEO_PLGMD_MODE_DISPLAY;
745 	bus_space_write_4(regt, regh, PLUM_VIDEO_PLGMD_REG, val);
746 
747 	/* palette access enable */
748 	val |= PLUM_VIDEO_PLGMD_PALETTE_ENABLE;
749 	bus_space_write_4(regt, regh, PLUM_VIDEO_PLGMD_REG, val);
750 
751 	/* display on */
752 	val |= gmode;
753 	bus_space_write_4(regt, regh, PLUM_VIDEO_PLGMD_REG, val);
754 }
755 
756 /* !!! */
757 static void
758 _flush_cache()
759 {
760 	mips_dcache_wbinv_all();
761 	mips_icache_sync_all();
762 }
763 
764 int
765 plumvideo_power(void *ctx, int type, long id, void *msg)
766 {
767 	struct plumvideo_softc *sc = ctx;
768 	int why = (int)msg;
769 
770 	switch (why) {
771 	case PWR_RESUME:
772 		if (!sc->sc_console)
773 			return (0); /* serial console */
774 
775 		DPRINTF(("%s: ON\n", sc->sc_dev.dv_xname));
776 		/* power on */
777 		plumvideo_backlight(sc, 1);
778 		break;
779 	case PWR_SUSPEND:
780 		/* FALLTHROUGH */
781 	case PWR_STANDBY:
782 		DPRINTF(("%s: OFF\n", sc->sc_dev.dv_xname));
783 		/* power off */
784 		plumvideo_backlight(sc, 0);
785 		break;
786 	}
787 
788 	return (0);
789 }
790 
791 static void
792 plumvideo_init_backlight(struct plumvideo_softc *sc)
793 {
794 	int val;
795 
796 	val = -1;
797 	if (config_hook_call(CONFIG_HOOK_GET,
798 	    CONFIG_HOOK_POWER_LCDLIGHT, &val) != -1) {
799 		/* we can get real backlight state */
800 		sc->sc_backlight = val;
801 	}
802 
803 	val = -1;
804 	if (config_hook_call(CONFIG_HOOK_GET,
805 	    CONFIG_HOOK_BRIGHTNESS_MAX, &val) != -1) {
806 		/* we can get real brightness max */
807 		sc->sc_max_brightness = val;
808 
809 		val = -1;
810 		if (config_hook_call(CONFIG_HOOK_GET,
811 		    CONFIG_HOOK_BRIGHTNESS, &val) != -1) {
812 			/* we can get real brightness */
813 			sc->sc_brightness = val;
814 		} else {
815 			sc->sc_brightness = sc->sc_max_brightness;
816 		}
817 	}
818 }
819 
820 static void
821 plumvideo_backlight(struct plumvideo_softc *sc, int on)
822 {
823 	plum_chipset_tag_t pc = sc->sc_pc;
824 	bus_space_tag_t regt = sc->sc_regt;
825 	bus_space_handle_t regh = sc->sc_regh;
826 
827 	sc->sc_backlight = on;
828 	if (on) {
829 		/* LCD on */
830 		plum_power_establish(pc, PLUM_PWR_LCD);
831 		/* backlight on */
832 		plum_power_establish(pc, PLUM_PWR_BKL);
833 		plum_conf_write(regt, regh, PLUM_VIDEO_PLLUM_REG,
834 				PLUM_VIDEO_PLLUM_MAX);
835 	} else {
836 		/* backlight off */
837 		plum_conf_write(regt, regh, PLUM_VIDEO_PLLUM_REG,
838 				PLUM_VIDEO_PLLUM_MIN);
839 		plum_power_disestablish(pc, PLUM_PWR_BKL);
840 		/* LCD off */
841 		plum_power_disestablish(pc, PLUM_PWR_LCD);
842 	}
843 	/* call machine dependent backlight control */
844 	config_hook_call(CONFIG_HOOK_SET,
845 			 CONFIG_HOOK_POWER_LCDLIGHT, (void *)on);
846 }
847 
848 static void
849 plumvideo_brightness(struct plumvideo_softc *sc, int val)
850 {
851 
852 	sc->sc_brightness = val;
853 	/* call machine dependent brightness control */
854 	if (sc->sc_backlight)
855 		config_hook_call(CONFIG_HOOK_SET,
856 				 CONFIG_HOOK_BRIGHTNESS, &val);
857 }
858 
859 #ifdef PLUMVIDEODEBUG
860 void
861 plumvideo_dump(struct plumvideo_softc *sc)
862 {
863 	bus_space_tag_t regt = sc->sc_regt;
864 	bus_space_handle_t regh = sc->sc_regh;
865 
866 	plumreg_t reg;
867 	int i;
868 
869 	for (i = 0; i < 0x160; i += 4) {
870 		reg = plum_conf_read(regt, regh, i);
871 		printf("0x%03x %08x", i, reg);
872 		dbg_bit_print(reg);
873 	}
874 }
875 #endif /* PLUMVIDEODEBUG */
876