xref: /netbsd-src/sys/arch/hpcmips/dev/plumvideo.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: plumvideo.c,v 1.20 2001/08/05 18:07:52 jdolecek Exp $ */
2 
3 /*-
4  * Copyright (c) 1999, 2000 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 #undef PLUMVIDEODEBUG
40 #include "opt_tx39_debug.h"
41 #include "plumohci.h" /* Plum2 OHCI shared memory allocated on V-RAM */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 
47 #include <sys/ioctl.h>
48 #include <sys/buf.h>
49 #include <uvm/uvm_extern.h>
50 
51 #include <dev/cons.h> /* consdev */
52 
53 #include <machine/bus.h>
54 #include <machine/intr.h>
55 #include <machine/config_hook.h>
56 
57 #include <hpcmips/tx/tx39var.h>
58 #include <hpcmips/dev/plumvar.h>
59 #include <hpcmips/dev/plumicuvar.h>
60 #include <hpcmips/dev/plumpowervar.h>
61 #include <hpcmips/dev/plumvideoreg.h>
62 
63 #include <machine/bootinfo.h>
64 
65 #include <dev/wscons/wsdisplayvar.h>
66 #include <dev/rasops/rasops.h>
67 #include <dev/hpc/video_subr.h>
68 
69 #include <dev/wscons/wsconsio.h>
70 #include <dev/hpc/hpcfbvar.h>
71 #include <dev/hpc/hpcfbio.h>
72 
73 #ifdef PLUMVIDEODEBUG
74 int	plumvideo_debug = 1;
75 #define	DPRINTF(arg) if (plumvideo_debug) printf arg;
76 #define	DPRINTFN(n, arg) if (plumvideo_debug > (n)) printf arg;
77 #else
78 #define	DPRINTF(arg)
79 #define DPRINTFN(n, arg)
80 #endif
81 
82 struct plumvideo_softc {
83 	struct device sc_dev;
84 	tx_chipset_tag_t sc_tc;
85 	plum_chipset_tag_t sc_pc;
86 
87 	void *sc_powerhook;	/* power management hook */
88 	int sc_console;
89 
90 	/* control register */
91 	bus_space_tag_t sc_regt;
92 	bus_space_handle_t sc_regh;
93 	/* frame buffer */
94 	bus_space_tag_t sc_fbiot;
95 	bus_space_handle_t sc_fbioh;
96 	/* clut buffer (8bpp only) */
97 	bus_space_tag_t sc_clutiot;
98 	bus_space_handle_t sc_clutioh;
99 	/* bitblt */
100 	bus_space_tag_t sc_bitbltt;
101 	bus_space_handle_t sc_bitblth;
102 
103 	struct video_chip sc_chip;
104 	struct hpcfb_fbconf sc_fbconf;
105 	struct hpcfb_dspconf sc_dspconf;
106 };
107 
108 int	plumvideo_match(struct device*, struct cfdata*, void*);
109 void	plumvideo_attach(struct device*, struct device*, void*);
110 
111 int	plumvideo_ioctl(void *, u_long, caddr_t, int, struct proc *);
112 paddr_t	plumvideo_mmap(void *, off_t, int);
113 
114 struct cfattach plumvideo_ca = {
115 	sizeof(struct plumvideo_softc), plumvideo_match, plumvideo_attach
116 };
117 
118 struct hpcfb_accessops plumvideo_ha = {
119 	plumvideo_ioctl, plumvideo_mmap
120 };
121 
122 int	plumvideo_power(void *, int, long, void *);
123 
124 int	plumvideo_init(struct plumvideo_softc *, int *);
125 void	plumvideo_hpcfbinit(struct plumvideo_softc *, int);
126 
127 void	plumvideo_clut_default(struct plumvideo_softc *);
128 void	plumvideo_clut_set(struct plumvideo_softc *, u_int32_t *, int, int);
129 void	plumvideo_clut_get(struct plumvideo_softc *, u_int32_t *, int, int);
130 void	__plumvideo_clut_access(struct plumvideo_softc *,
131 				void (*)(bus_space_tag_t, bus_space_handle_t));
132 static void _flush_cache(void) __attribute__((__unused__)); /* !!! */
133 
134 #ifdef PLUMVIDEODEBUG
135 void	plumvideo_dump(struct plumvideo_softc*);
136 #endif
137 
138 #define ON	1
139 #define OFF	0
140 
141 int
142 plumvideo_match(struct device *parent, struct cfdata *cf, void *aux)
143 {
144 	/*
145 	 * VRAM area also uses as UHOSTC shared RAM.
146 	 */
147 	return (2); /* 1st attach group */
148 }
149 
150 void
151 plumvideo_attach(struct device *parent, struct device *self, void *aux)
152 {
153 	struct plum_attach_args *pa = aux;
154 	struct plumvideo_softc *sc = (void*)self;
155 	struct hpcfb_attach_args ha;
156 	int console, reverse_flag;
157 
158 	sc->sc_console = console = cn_tab ? 0 : 1;
159 	sc->sc_pc	= pa->pa_pc;
160 	sc->sc_regt	= pa->pa_regt;
161 	sc->sc_fbiot = sc->sc_clutiot = sc->sc_bitbltt  = pa->pa_iot;
162 
163 	printf(": ");
164 
165 	/* map register area */
166 	if (bus_space_map(sc->sc_regt, PLUM_VIDEO_REGBASE,
167 			  PLUM_VIDEO_REGSIZE, 0, &sc->sc_regh)) {
168 		printf("register map failed\n");
169 		return;
170 	}
171 
172 	/* power control */
173 	plumvideo_power(sc, 0, 0,
174 			(void *)(console ? PWR_RESUME : PWR_SUSPEND));
175 	/* Add a hard power hook to power saving */
176 	sc->sc_powerhook = config_hook(CONFIG_HOOK_PMEVENT,
177 				       CONFIG_HOOK_PMEVENT_HARDPOWER,
178 				       CONFIG_HOOK_SHARE,
179 				       plumvideo_power, sc);
180 	if (sc->sc_powerhook == 0)
181 		printf("WARNING unable to establish hard power hook");
182 
183 	/*
184 	 *  Initialize LCD controller
185 	 *	map V-RAM area.
186 	 *	reinstall bootinfo structure.
187 	 *	some OHCI shared-buffer hack. XXX
188 	 */
189 	if (plumvideo_init(sc, &reverse_flag) != 0)
190 		return;
191 
192 	printf("\n");
193 
194 	/* Attach frame buffer device */
195 	plumvideo_hpcfbinit(sc, reverse_flag);
196 
197 #ifdef PLUMVIDEODEBUG
198 	if (plumvideo_debug > 0)
199 		plumvideo_dump(sc);
200 	/* attach debug draw routine (debugging use) */
201 	video_attach_drawfunc(&sc->sc_chip);
202 	tx_conf_register_video(sc->sc_pc->pc_tc, &sc->sc_chip);
203 #endif /* PLUMVIDEODEBUG */
204 
205 	if(console && hpcfb_cnattach(&sc->sc_fbconf) != 0) {
206 		panic("plumvideo_attach: can't init fb console");
207 	}
208 
209 	ha.ha_console = console;
210 	ha.ha_accessops = &plumvideo_ha;
211 	ha.ha_accessctx = sc;
212 	ha.ha_curfbconf = 0;
213 	ha.ha_nfbconf = 1;
214 	ha.ha_fbconflist = &sc->sc_fbconf;
215 	ha.ha_curdspconf = 0;
216 	ha.ha_ndspconf = 1;
217 	ha.ha_dspconflist = &sc->sc_dspconf;
218 
219 	config_found(self, &ha, hpcfbprint);
220 }
221 
222 void
223 plumvideo_hpcfbinit(struct plumvideo_softc *sc, int reverse_flag)
224 {
225 	struct hpcfb_fbconf *fb = &sc->sc_fbconf;
226 	struct video_chip *chip = &sc->sc_chip;
227 	vaddr_t fbvaddr = (vaddr_t)sc->sc_fbioh;
228 	int height = chip->vc_fbheight;
229 	int width = chip->vc_fbwidth;
230 	int depth = chip->vc_fbdepth;
231 
232 	memset(fb, 0, sizeof(struct hpcfb_fbconf));
233 
234 	fb->hf_conf_index	= 0;	/* configuration index		*/
235 	fb->hf_nconfs		= 1;   	/* how many configurations	*/
236 	strncpy(fb->hf_name, "PLUM built-in video", HPCFB_MAXNAMELEN);
237 	/* frame buffer name		*/
238 	strncpy(fb->hf_conf_name, "LCD", HPCFB_MAXNAMELEN);
239 	/* configuration name		*/
240 	fb->hf_height		= height;
241 	fb->hf_width		= width;
242 	fb->hf_baseaddr		= (u_long)fbvaddr;
243 	fb->hf_offset		= (u_long)fbvaddr - mips_ptob(mips_btop(fbvaddr));
244 	/* frame buffer start offset   	*/
245 	fb->hf_bytes_per_line	= (width * depth) / NBBY;
246 	fb->hf_nplanes		= 1;
247 	fb->hf_bytes_per_plane	= height * fb->hf_bytes_per_line;
248 
249 	fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
250 	fb->hf_access_flags |= HPCFB_ACCESS_WORD;
251 	fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
252 	if (reverse_flag)
253 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
254 
255 	switch (depth) {
256 	default:
257 		panic("plumvideo_hpcfbinit: not supported color depth\n");
258 		/* NOTREACHED */
259 	case 16:
260 		fb->hf_class = HPCFB_CLASS_RGBCOLOR;
261 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
262 		fb->hf_order_flags = HPCFB_REVORDER_BYTE;
263 		fb->hf_pack_width = 16;
264 		fb->hf_pixels_per_pack = 1;
265 		fb->hf_pixel_width = 16;
266 
267 		fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
268 		/* reserved for future use */
269 		fb->hf_u.hf_rgb.hf_flags = 0;
270 
271 		fb->hf_u.hf_rgb.hf_red_width = 5;
272 		fb->hf_u.hf_rgb.hf_red_shift = 11;
273 		fb->hf_u.hf_rgb.hf_green_width = 6;
274 		fb->hf_u.hf_rgb.hf_green_shift = 5;
275 		fb->hf_u.hf_rgb.hf_blue_width = 5;
276 		fb->hf_u.hf_rgb.hf_blue_shift = 0;
277 		fb->hf_u.hf_rgb.hf_alpha_width = 0;
278 		fb->hf_u.hf_rgb.hf_alpha_shift = 0;
279 		break;
280 
281 	case 8:
282 		fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
283 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
284 		fb->hf_pack_width = 8;
285 		fb->hf_pixels_per_pack = 1;
286 		fb->hf_pixel_width = 8;
287 		fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
288 		/* reserved for future use */
289 		fb->hf_u.hf_indexed.hf_flags = 0;
290 		break;
291 	}
292 }
293 
294 int
295 plumvideo_init(struct plumvideo_softc *sc, int *reverse)
296 {
297 	struct video_chip *chip = &sc->sc_chip;
298 	bus_space_tag_t regt = sc->sc_regt;
299 	bus_space_handle_t regh = sc->sc_regh;
300 	plumreg_t reg;
301 	size_t vram_size;
302 	int bpp, width, height, vram_pitch;
303 
304 	*reverse = video_reverse_color();
305 	chip->vc_v = sc->sc_pc->pc_tc;
306 #if notyet
307 	/* map BitBlt area */
308 	if (bus_space_map(sc->sc_bitbltt,
309 			  PLUM_VIDEO_BITBLT_IOBASE,
310 			  PLUM_VIDEO_BITBLT_IOSIZE, 0,
311 			  &sc->sc_bitblth)) {
312 		printf(": BitBlt map failed\n");
313 		return (1);
314 	}
315 #endif
316 	reg = plum_conf_read(regt, regh, PLUM_VIDEO_PLGMD_REG);
317 
318 	switch (reg & PLUM_VIDEO_PLGMD_GMODE_MASK) {
319 	case PLUM_VIDEO_PLGMD_16BPP:
320 #if NPLUMOHCI > 0 /* reserve V-RAM area for USB OHCI */
321 		/* FALLTHROUGH */
322 #else
323 		bpp = 16;
324 		break;
325 #endif
326 	default:
327 		bootinfo->fb_type = *reverse ? BIFB_D8_FF : BIFB_D8_00;
328 		reg &= ~PLUM_VIDEO_PLGMD_GMODE_MASK;
329 		plum_conf_write(regt, regh, PLUM_VIDEO_PLGMD_REG, reg);
330 		reg |= PLUM_VIDEO_PLGMD_8BPP;
331 		plum_conf_write(regt, regh, PLUM_VIDEO_PLGMD_REG, reg);
332 #if notyet
333 		/* change BitBlt color depth */
334 		plum_conf_write(sc->sc_bitbltt, sc->sc_bitblth, 0x8, 0);
335 #endif
336 		/* FALLTHROUGH */
337 	case PLUM_VIDEO_PLGMD_8BPP:
338 		bpp = 8;
339 		break;
340 	}
341 	chip->vc_fbdepth = bpp;
342 
343 	/*
344 	 * Get display size from WindowsCE setted.
345 	 */
346 	chip->vc_fbwidth = width = bootinfo->fb_width =
347 		plum_conf_read(regt, regh, PLUM_VIDEO_PLHPX_REG) + 1;
348 	chip->vc_fbheight = height = bootinfo->fb_height =
349 		plum_conf_read(regt, regh, PLUM_VIDEO_PLVT_REG) -
350 		plum_conf_read(regt, regh, PLUM_VIDEO_PLVDS_REG);
351 
352 	/*
353 	 * set line byte length to bootinfo and LCD controller.
354 	 */
355 	vram_pitch = bootinfo->fb_line_bytes = (width * bpp) / NBBY;
356 	plum_conf_write(regt, regh, PLUM_VIDEO_PLPIT1_REG, vram_pitch);
357 	plum_conf_write(regt, regh, PLUM_VIDEO_PLPIT2_REG,
358 			vram_pitch & PLUM_VIDEO_PLPIT2_MASK);
359 	plum_conf_write(regt, regh, PLUM_VIDEO_PLOFS_REG, vram_pitch);
360 
361 	/*
362 	 * boot messages and map CLUT(if any).
363 	 */
364 	printf("display mode: ");
365 	switch (bpp) {
366 	default:
367 		printf("disabled ");
368 		break;
369 	case 8:
370 		printf("8bpp ");
371 		/* map CLUT area */
372 		if (bus_space_map(sc->sc_clutiot,
373 				  PLUM_VIDEO_CLUT_LCD_IOBASE,
374 				  PLUM_VIDEO_CLUT_LCD_IOSIZE, 0,
375 				  &sc->sc_clutioh)) {
376 			printf(": CLUT map failed\n");
377 			return (1);
378 		}
379 		/* install default CLUT */
380 		plumvideo_clut_default(sc);
381 		break;
382 	case 16:
383 		printf("16bpp ");
384 		break;
385 	}
386 
387 	/*
388 	 * calcurate frame buffer size.
389 	 */
390 	reg = plum_conf_read(regt, regh, PLUM_VIDEO_PLGMD_REG);
391 	vram_size = (width * height * bpp) / NBBY;
392 	vram_size = mips_round_page(vram_size);
393 	chip->vc_fbsize = vram_size;
394 
395 	/*
396 	 * map V-RAM area.
397 	 */
398 	if (bus_space_map(sc->sc_fbiot, PLUM_VIDEO_VRAM_IOBASE,
399 			  vram_size, 0, &sc->sc_fbioh)) {
400 		printf(": V-RAM map failed\n");
401 		return (1);
402 	}
403 
404 	bootinfo->fb_addr = (unsigned char *)sc->sc_fbioh;
405 	chip->vc_fbvaddr = (vaddr_t)sc->sc_fbioh;
406 	chip->vc_fbpaddr = PLUM_VIDEO_VRAM_IOBASE_PHYSICAL;
407 
408 	return (0);
409 }
410 
411 int
412 plumvideo_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
413 {
414 	struct plumvideo_softc *sc = (struct plumvideo_softc *)v;
415 	struct hpcfb_fbconf *fbconf;
416 	struct hpcfb_dspconf *dspconf;
417 	struct wsdisplay_cmap *cmap;
418 	u_int8_t *r, *g, *b;
419 	u_int32_t *rgb;
420 	int idx, error;
421 	size_t cnt;
422 
423 	switch (cmd) {
424 	case WSDISPLAYIO_GETCMAP:
425 		cmap = (struct wsdisplay_cmap*)data;
426 		cnt = cmap->count;
427 		idx = cmap->index;
428 
429 		if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
430 		    sc->sc_fbconf.hf_pack_width != 8 ||
431 		    !LEGAL_CLUT_INDEX(idx) ||
432 		    !LEGAL_CLUT_INDEX(idx + cnt -1)) {
433 			return (EINVAL);
434 		}
435 
436 		if (!uvm_useracc(cmap->red, cnt, B_WRITE) ||
437 		    !uvm_useracc(cmap->green, cnt, B_WRITE) ||
438 		    !uvm_useracc(cmap->blue, cnt, B_WRITE)) {
439 			return (EFAULT);
440 		}
441 
442 		error = cmap_work_alloc(&r, &g, &b, &rgb, cnt);
443 		if (error != 0) {
444 			cmap_work_free(r, g, b, rgb);
445 			return  (ENOMEM);
446 		}
447 		plumvideo_clut_get(sc, rgb, idx, cnt);
448 		rgb24_decompose(rgb, r, g, b, cnt);
449 
450 		copyout(r, cmap->red, cnt);
451 		copyout(g, cmap->green,cnt);
452 		copyout(b, cmap->blue, cnt);
453 
454 		cmap_work_free(r, g, b, rgb);
455 
456 		return (0);
457 
458 	case WSDISPLAYIO_PUTCMAP:
459 		cmap = (struct wsdisplay_cmap*)data;
460 		cnt = cmap->count;
461 		idx = cmap->index;
462 
463 		if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
464 		    sc->sc_fbconf.hf_pack_width != 8 ||
465 		    !LEGAL_CLUT_INDEX(idx) ||
466 		    !LEGAL_CLUT_INDEX(idx + cnt -1)) {
467 			return (EINVAL);
468 		}
469 
470 		if (!uvm_useracc(cmap->red, cnt, B_WRITE) ||
471 		    !uvm_useracc(cmap->green, cnt, B_WRITE) ||
472 		    !uvm_useracc(cmap->blue, cnt, B_WRITE)) {
473 			return (EFAULT);
474 		}
475 
476 		error = cmap_work_alloc(&r, &g, &b, &rgb, cnt);
477 		if (error != 0) {
478 			cmap_work_free(r, g, b, rgb);
479 			return  (ENOMEM);
480 		}
481 		copyin(cmap->red,   r, cnt);
482 		copyin(cmap->green, g, cnt);
483 		copyin(cmap->blue,  b, cnt);
484 		rgb24_compose(rgb, r, g, b, cnt);
485 		plumvideo_clut_set(sc, rgb, idx, cnt);
486 
487 		cmap_work_free(r, g, b, rgb);
488 
489 		return (0);
490 
491 	case HPCFBIO_GCONF:
492 		fbconf = (struct hpcfb_fbconf *)data;
493 		if (fbconf->hf_conf_index != 0 &&
494 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
495 			return (EINVAL);
496 		}
497 		*fbconf = sc->sc_fbconf;	/* structure assignment */
498 		return (0);
499 
500 	case HPCFBIO_SCONF:
501 		fbconf = (struct hpcfb_fbconf *)data;
502 		if (fbconf->hf_conf_index != 0 &&
503 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
504 			return (EINVAL);
505 		}
506 		/*
507 		 * nothing to do because we have only one configration
508 		 */
509 		return (0);
510 
511 	case HPCFBIO_GDSPCONF:
512 		dspconf = (struct hpcfb_dspconf *)data;
513 		if ((dspconf->hd_unit_index != 0 &&
514 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
515 		    (dspconf->hd_conf_index != 0 &&
516 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
517 			return (EINVAL);
518 		}
519 		*dspconf = sc->sc_dspconf;	/* structure assignment */
520 		return (0);
521 
522 	case HPCFBIO_SDSPCONF:
523 		dspconf = (struct hpcfb_dspconf *)data;
524 		if ((dspconf->hd_unit_index != 0 &&
525 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
526 		    (dspconf->hd_conf_index != 0 &&
527 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
528 			return (EINVAL);
529 		}
530 		/*
531 		 * nothing to do
532 		 * because we have only one unit and one configration
533 		 */
534 		return (0);
535 
536 	case HPCFBIO_GOP:
537 	case HPCFBIO_SOP:
538 		/* XXX not implemented yet */
539 		return (EINVAL);
540 	}
541 
542 	return (ENOTTY);
543 }
544 
545 paddr_t
546 plumvideo_mmap(void *ctx, off_t offset, int prot)
547 {
548 	struct plumvideo_softc *sc = (struct plumvideo_softc *)ctx;
549 
550 	if (offset < 0 || (sc->sc_fbconf.hf_bytes_per_plane +
551 			   sc->sc_fbconf.hf_offset) <  offset) {
552 		return (-1);
553 	}
554 
555 	return (mips_btop(PLUM_VIDEO_VRAM_IOBASE_PHYSICAL + offset));
556 }
557 
558 void
559 plumvideo_clut_get(struct plumvideo_softc *sc, u_int32_t *rgb, int beg,
560 		   int cnt)
561 {
562 	static void __plumvideo_clut_get(bus_space_tag_t,
563 					      bus_space_handle_t);
564 	static void __plumvideo_clut_get(iot, ioh)
565 		bus_space_tag_t iot;
566 		bus_space_handle_t ioh;
567 	{
568 		int i;
569 
570 		for (i = 0, beg *= 4; i < cnt; i++, beg += 4) {
571 			*rgb++ = bus_space_read_4(iot, ioh, beg) &
572 				0x00ffffff;
573 		}
574 	}
575 
576 	KASSERT(rgb);
577 	KASSERT(LEGAL_CLUT_INDEX(beg));
578 	KASSERT(LEGAL_CLUT_INDEX(beg + cnt - 1));
579 	__plumvideo_clut_access(sc, __plumvideo_clut_get);
580 }
581 
582 void
583 plumvideo_clut_set(struct plumvideo_softc *sc, u_int32_t *rgb, int beg,
584 		   int cnt)
585 {
586 	static void __plumvideo_clut_set(bus_space_tag_t,
587 					      bus_space_handle_t);
588 	static void __plumvideo_clut_set(iot, ioh)
589 		bus_space_tag_t iot;
590 		bus_space_handle_t ioh;
591 	{
592 		int i;
593 
594 		for (i = 0, beg *= 4; i < cnt; i++, beg +=4) {
595 			bus_space_write_4(iot, ioh, beg,
596 					  *rgb++ & 0x00ffffff);
597 		}
598 	}
599 
600 	KASSERT(rgb);
601 	KASSERT(LEGAL_CLUT_INDEX(beg));
602 	KASSERT(LEGAL_CLUT_INDEX(beg + cnt - 1));
603 	__plumvideo_clut_access(sc, __plumvideo_clut_set);
604 }
605 
606 void
607 plumvideo_clut_default(struct plumvideo_softc *sc)
608 {
609 	static void __plumvideo_clut_default(bus_space_tag_t,
610 						  bus_space_handle_t);
611 	static void __plumvideo_clut_default(iot, ioh)
612 		bus_space_tag_t iot;
613 		bus_space_handle_t ioh;
614 	{
615 		const u_int8_t compo6[6] = { 0,  51, 102, 153, 204, 255 };
616 		const u_int32_t ansi_color[16] = {
617 			0x000000, 0xff0000, 0x00ff00, 0xffff00,
618 			0x0000ff, 0xff00ff, 0x00ffff, 0xffffff,
619 			0x000000, 0x800000, 0x008000, 0x808000,
620 			0x000080, 0x800080, 0x008080, 0x808080,
621 		};
622 		int i, r, g, b;
623 
624 		/* ANSI escape sequence */
625 		for (i = 0; i < 16; i++) {
626 			bus_space_write_4(iot, ioh, i << 2, ansi_color[i]);
627 		}
628 		/* 16 - 31, gray scale */
629 		for ( ; i < 32; i++) {
630 			int j = (i - 16) * 17;
631 			bus_space_write_4(iot, ioh, i << 2, RGB24(j, j, j));
632 		}
633 		/* 32 - 247, RGB color */
634 		for (r = 0; r < 6; r++) {
635 			for (g = 0; g < 6; g++) {
636 				for (b = 0; b < 6; b++) {
637 					bus_space_write_4(iot, ioh, i << 2,
638 							  RGB24(compo6[r],
639 								compo6[g],
640 								compo6[b]));
641 					i++;
642 				}
643 			}
644 		}
645 		/* 248 - 245, just white */
646 		for ( ; i < 256; i++) {
647 			bus_space_write_4(iot, ioh, i << 2, 0xffffff);
648 		}
649 	}
650 
651 	__plumvideo_clut_access(sc, __plumvideo_clut_default);
652 }
653 
654 void
655 __plumvideo_clut_access(struct plumvideo_softc *sc, void (*palette_func)
656 			(bus_space_tag_t, bus_space_handle_t))
657 {
658 	bus_space_tag_t regt = sc->sc_regt;
659 	bus_space_handle_t regh = sc->sc_regh;
660 	plumreg_t val, gmode;
661 
662 	/* display off */
663 	val = bus_space_read_4(regt, regh, PLUM_VIDEO_PLGMD_REG);
664 	gmode = val & PLUM_VIDEO_PLGMD_GMODE_MASK;
665 	val &= ~PLUM_VIDEO_PLGMD_GMODE_MASK;
666 	bus_space_write_4(regt, regh, PLUM_VIDEO_PLGMD_REG, val);
667 
668 	/* palette access disable */
669 	val &= ~PLUM_VIDEO_PLGMD_PALETTE_ENABLE;
670 	bus_space_write_4(regt, regh, PLUM_VIDEO_PLGMD_REG, val);
671 
672 	/* change palette mode to CPU */
673 	val &= ~PLUM_VIDEO_PLGMD_MODE_DISPLAY;
674 	bus_space_write_4(regt, regh, PLUM_VIDEO_PLGMD_REG, val);
675 
676 	/* palette access */
677 	(*palette_func) (sc->sc_clutiot, sc->sc_clutioh);
678 
679 	/* change palette mode to Display */
680 	val |= PLUM_VIDEO_PLGMD_MODE_DISPLAY;
681 	bus_space_write_4(regt, regh, PLUM_VIDEO_PLGMD_REG, val);
682 
683 	/* palette access enable */
684 	val |= PLUM_VIDEO_PLGMD_PALETTE_ENABLE;
685 	bus_space_write_4(regt, regh, PLUM_VIDEO_PLGMD_REG, val);
686 
687 	/* display on */
688 	val |= gmode;
689 	bus_space_write_4(regt, regh, PLUM_VIDEO_PLGMD_REG, val);
690 }
691 
692 /* !!! */
693 static void
694 _flush_cache()
695 {
696 	MachFlushCache();
697 }
698 
699 int
700 plumvideo_power(void *ctx, int type, long id, void *msg)
701 {
702 	struct plumvideo_softc *sc = ctx;
703 	plum_chipset_tag_t pc = sc->sc_pc;
704 	bus_space_tag_t regt = sc->sc_regt;
705 	bus_space_handle_t regh = sc->sc_regh;
706 	int why = (int)msg;
707 
708 	switch (why) {
709 	case PWR_RESUME:
710 		if (!sc->sc_console)
711 			return 0; /* serial console */
712 
713 		DPRINTF(("%s: ON\n", sc->sc_dev.dv_xname));
714 		/* power on */
715 		/* LCD power on and display on */
716 		plum_power_establish(pc, PLUM_PWR_LCD);
717 		/* back-light on */
718 		plum_power_establish(pc, PLUM_PWR_BKL);
719 		plum_conf_write(regt, regh, PLUM_VIDEO_PLLUM_REG,
720 				PLUM_VIDEO_PLLUM_MAX);
721 		break;
722 	case PWR_SUSPEND:
723 		/* FALLTHROUGH */
724 	case PWR_STANDBY:
725 		DPRINTF(("%s: OFF\n", sc->sc_dev.dv_xname));
726 		/* back-light off */
727 		plum_conf_write(regt, regh, PLUM_VIDEO_PLLUM_REG,
728 				PLUM_VIDEO_PLLUM_MIN);
729 		plum_power_disestablish(pc, PLUM_PWR_BKL);
730 		/* power down */
731 		plum_power_disestablish(pc, PLUM_PWR_LCD);
732 		break;
733 	}
734 
735 	return 0;
736 }
737 
738 #ifdef PLUMVIDEODEBUG
739 void
740 plumvideo_dump(struct plumvideo_softc *sc)
741 {
742 	bus_space_tag_t regt = sc->sc_regt;
743 	bus_space_handle_t regh = sc->sc_regh;
744 
745 	plumreg_t reg;
746 	int i;
747 
748 	for (i = 0; i < 0x160; i += 4) {
749 		reg = plum_conf_read(regt, regh, i);
750 		printf("0x%03x %08x", i, reg);
751 		bitdisp(reg);
752 	}
753 }
754 #endif /* PLUMVIDEODEBUG */
755