Home | History | Annotate | Line # | Download | only in hd64461
hd64461video.c revision 1.29
      1 /*	$NetBSD: hd64461video.c,v 1.29 2005/02/28 18:04:54 uwe Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2002, 2004 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: hd64461video.c,v 1.29 2005/02/28 18:04:54 uwe Exp $");
     41 
     42 #include "debug_hpcsh.h"
     43 // #define HD64461VIDEO_HWACCEL
     44 
     45 #include <sys/param.h>
     46 #include <sys/kernel.h>
     47 #include <sys/systm.h>
     48 #include <sys/device.h>
     49 #include <sys/malloc.h>
     50 
     51 #include <sys/conf.h> /* cdev_decl */
     52 #include <dev/cons.h> /* consdev */
     53 
     54 /* ioctl */
     55 #include <sys/ioctl.h>
     56 #include <sys/buf.h>
     57 #include <uvm/uvm_extern.h>
     58 
     59 #include <machine/bus.h>
     60 #include <machine/intr.h>
     61 
     62 #include <hpcsh/dev/hd64461/hd64461var.h>
     63 #include <hpcsh/dev/hd64461/hd64461reg.h>
     64 #include <hpcsh/dev/hd64461/hd64461videoreg.h>
     65 
     66 #include <dev/wscons/wsdisplayvar.h>
     67 #include <dev/rasops/rasops.h>
     68 
     69 #include <dev/wscons/wsconsio.h>
     70 #include <dev/hpc/hpcfbvar.h>
     71 #include <dev/hpc/hpcfbio.h>
     72 #include <dev/hpc/video_subr.h>
     73 
     74 #include <machine/config_hook.h>
     75 #include <machine/bootinfo.h>
     76 
     77 #ifdef	HD64461VIDEO_DEBUG
     78 #define DPRINTF_ENABLE
     79 #define DPRINTF_DEBUG	hd64461video_debug
     80 #endif
     81 #include <machine/debug.h>
     82 
     83 struct hd64461video_chip;
     84 struct hd64461video_font {
     85 	struct wsdisplay_font wsfont;
     86 	int c, cw, cstep;
     87 	int loaded;
     88 };
     89 
     90 struct hd64461video_softc {
     91 	struct device sc_dev;
     92 	enum hd64461_module_id sc_module_id;
     93 	struct hd64461video_chip *sc_vc;
     94 
     95 	struct hd64461video_font sc_font;
     96 };
     97 
     98 enum hd64461video_display_mode {
     99 	LCD256_C,
    100 	LCD64K_C,
    101 	LCD64_MONO,
    102 	LCD16_MONO,
    103 	LCD4_MONO,
    104 	LCD2_MONO,
    105 	CRT256_C,
    106 	LCDCRT
    107 };
    108 
    109 STATIC struct hd64461video_chip {
    110 	struct video_chip vc;
    111 	enum hd64461video_display_mode mode;
    112 	struct hpcfb_dspconf hd;
    113 	struct hpcfb_fbconf hf;
    114 	u_int8_t *off_screen_addr;
    115 	size_t off_screen_size;
    116 
    117 	struct callout unblank_ch;
    118 	int blanked;
    119 
    120 	int console;
    121 } hd64461video_chip;
    122 
    123 void	hd64461video_cnprobe(struct consdev *);
    124 void	hd64461video_cninit(struct consdev *);
    125 
    126 STATIC int hd64461video_match(struct device *, struct cfdata *, void *);
    127 STATIC void hd64461video_attach(struct device *, struct device *, void *);
    128 
    129 STATIC void hd64461video_setup_hpcfbif(struct hd64461video_chip *);
    130 STATIC void hd64461video_update_videochip_status(struct hd64461video_chip *);
    131 STATIC size_t hd64461video_frame_buffer_size(struct hd64461video_chip *);
    132 STATIC void hd64461video_hwaccel_init(struct hd64461video_chip *);
    133 
    134 STATIC void hd64461video_set_clut(struct hd64461video_chip *, int, int,
    135     u_int8_t *, u_int8_t *, u_int8_t *);
    136 STATIC void hd64461video_get_clut(struct hd64461video_chip *, int, int,
    137     u_int8_t *, u_int8_t *, u_int8_t *);
    138 STATIC int hd64461video_power(void *, int, long, void *);
    139 STATIC void hd64461video_off(struct hd64461video_chip *);
    140 STATIC void hd64461video_on(struct hd64461video_chip *);
    141 STATIC void hd64461video_display_onoff(void *, boolean_t);
    142 STATIC void hd64461video_display_on(void *);
    143 
    144 #if notyet
    145 STATIC void hd64461video_set_display_mode(struct hd64461video_chip *);
    146 STATIC void hd64461video_set_display_mode_lcdc(struct hd64461video_chip *);
    147 STATIC void hd64461video_set_display_mode_crtc(struct hd64461video_chip *);
    148 #endif
    149 
    150 #ifdef HD64461VIDEO_DEBUG
    151 STATIC void hd64461video_info(struct hd64461video_softc *);
    152 STATIC void hd64461video_dump(void) __attribute__((__unused__));
    153 #endif
    154 
    155 CFATTACH_DECL(hd64461video, sizeof(struct hd64461video_softc),
    156     hd64461video_match, hd64461video_attach, NULL, NULL);
    157 
    158 int hd64461video_ioctl(void *, u_long, caddr_t, int, struct proc *);
    159 paddr_t hd64461video_mmap(void *, off_t, int);
    160 void hd64461video_cursor(void *, int, int, int, int, int);
    161 void hd64461video_bitblit(void *, int, int, int, int, int, int);
    162 void hd64461video_erase(void *, int, int, int, int, int);
    163 void hd64461video_putchar(void *, int, int, struct wsdisplay_font *, int, int,
    164     u_int, int);
    165 void hd64461video_setclut(void *, struct rasops_info *);
    166 void hd64461video_font(void *, struct wsdisplay_font *);
    167 void hd64461video_iodone(void *);
    168 
    169 struct hpcfb_accessops hd64461video_ha = {
    170 	.ioctl	= hd64461video_ioctl,
    171 	.mmap	= hd64461video_mmap,
    172 #ifdef HD64461VIDEO_HWACCEL
    173 	.cursor	= hd64461video_cursor,
    174 	.bitblit= hd64461video_bitblit,
    175 	.erase	= hd64461video_erase,
    176 	.putchar= hd64461video_putchar,
    177 	.setclut= hd64461video_setclut,
    178 	.font	= hd64461video_font,
    179 	.iodone	= hd64461video_iodone
    180 #endif /* HD64461VIDEO_HWACCEL */
    181 };
    182 
    183 /* font */
    184 STATIC void hd64461video_font_load_16bpp(u_int16_t *, u_int8_t *, int, int, int);
    185 STATIC void hd64461video_font_load_8bpp(u_int8_t *, u_int8_t *, int, int, int);
    186 STATIC void hd64461video_font_set_attr(struct hd64461video_softc *,
    187     struct wsdisplay_font *);
    188 STATIC void hd64461video_font_load(struct hd64461video_softc *);
    189 STATIC vaddr_t hd64461video_font_start_addr(struct hd64461video_softc *, int);
    190 
    191 int
    192 hd64461video_match(struct device *parent, struct cfdata *cf, void *aux)
    193 {
    194 	struct hd64461_attach_args *ha = aux;
    195 
    196 	return (ha->ha_module_id == HD64461_MODULE_VIDEO);
    197 }
    198 
    199 void
    200 hd64461video_attach(struct device *parent, struct device *self, void *aux)
    201 {
    202 	struct hd64461_attach_args *ha = aux;
    203 	struct hd64461video_softc *sc = (struct hd64461video_softc *)self;
    204 	struct hpcfb_attach_args hfa;
    205 	struct video_chip *vc = &hd64461video_chip.vc;
    206 	char pbuf[9];
    207 	size_t fbsize, on_screen_size;
    208 
    209 	sc->sc_module_id = ha->ha_module_id;
    210 	sc->sc_vc = &hd64461video_chip;
    211 	printf(": ");
    212 
    213 	/* detect frame buffer size */
    214 	fbsize = hd64461video_frame_buffer_size(&hd64461video_chip);
    215 	format_bytes(pbuf, sizeof(pbuf), fbsize);
    216 	printf("frame buffer = %s ", pbuf);
    217 
    218 	/* update chip status */
    219 	hd64461video_update_videochip_status(&hd64461video_chip);
    220 //	hd64461video_set_display_mode(&hd64461video_chip);
    221 
    222 	if (hd64461video_chip.console)
    223 		printf(", console");
    224 
    225 	printf("\n");
    226 #ifdef HD64461VIDEO_DEBUG
    227 	hd64461video_info(sc);
    228 	hd64461video_dump();
    229 #endif
    230 	/* Add a hard power hook to power saving */
    231 	config_hook(CONFIG_HOOK_PMEVENT, CONFIG_HOOK_PMEVENT_HARDPOWER,
    232 	    CONFIG_HOOK_SHARE, hd64461video_power, sc);
    233 
    234 	/* setup hpcfb interface */
    235 	hd64461video_setup_hpcfbif(&hd64461video_chip);
    236 
    237 	/* setup off-screen buffer */
    238 	on_screen_size = (vc->vc_fbwidth * vc->vc_fbheight * vc->vc_fbdepth) /
    239 	    NBBY;
    240 	hd64461video_chip.off_screen_addr = (u_int8_t *)vc->vc_fbvaddr +
    241 	    on_screen_size;
    242 	hd64461video_chip.off_screen_size = fbsize - on_screen_size;
    243 	/* clean up off-screen area */
    244 	{
    245 		u_int8_t *p = hd64461video_chip.off_screen_addr;
    246 		u_int8_t *end = p + hd64461video_chip.off_screen_size;
    247 		while (p < end)
    248 			*p++ = 0xff;
    249 	}
    250 
    251 	/* initialize hardware acceralation */
    252 	hd64461video_hwaccel_init(&hd64461video_chip);
    253 
    254 	/* register interface to hpcfb */
    255 	hfa.ha_console	   = hd64461video_chip.console;
    256 	hfa.ha_accessops   = &hd64461video_ha;
    257 	hfa.ha_accessctx   = sc;
    258 	hfa.ha_curfbconf   = 0;
    259 	hfa.ha_nfbconf	   = 1;
    260 	hfa.ha_fbconflist  = &hd64461video_chip.hf;
    261 	hfa.ha_curdspconf  = 0;
    262 	hfa.ha_ndspconf	   = 1;
    263 	hfa.ha_dspconflist = &hd64461video_chip.hd;
    264 
    265 	config_found(self, &hfa, hpcfbprint);
    266 }
    267 
    268 /* console support */
    269 void
    270 hd64461video_cninit(struct consdev *cndev)
    271 {
    272 	hd64461video_chip.console = 1;
    273 	hd64461video_chip.vc.vc_reverse = video_reverse_color();
    274 
    275 	hd64461video_update_videochip_status(&hd64461video_chip);
    276 	hd64461video_setup_hpcfbif(&hd64461video_chip);
    277 	hpcfb_cnattach(&hd64461video_chip.hf);
    278 
    279 	cn_tab->cn_pri = CN_INTERNAL;
    280 }
    281 
    282 void
    283 hd64461video_cnprobe(struct consdev *cndev)
    284 {
    285 #if NWSDISPLAY > 0
    286 	extern const struct cdevsw wsdisplay_cdevsw;
    287 	int maj, unit;
    288 #endif
    289 	cndev->cn_dev = NODEV;
    290 	cndev->cn_pri = CN_NORMAL;
    291 
    292 #if NWSDISPLAY > 0
    293 	unit = 0;
    294 	maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
    295 
    296 	if (maj != -1) {
    297 		cndev->cn_pri = CN_INTERNAL;
    298 		cndev->cn_dev = makedev(maj, unit);
    299 	}
    300 #endif /* NWSDISPLAY > 0 */
    301 }
    302 
    303 /* hpcfb support */
    304 void
    305 hd64461video_setup_hpcfbif(struct hd64461video_chip *hvc)
    306 {
    307 	struct video_chip *vc = &hvc->vc;
    308 	struct hpcfb_fbconf *fb = &hvc->hf;
    309 	vaddr_t fbvaddr = vc->vc_fbvaddr;
    310 	int height = vc->vc_fbheight;
    311 	int width = vc->vc_fbwidth;
    312 	int depth = vc->vc_fbdepth;
    313 
    314 	memset(fb, 0, sizeof(struct hpcfb_fbconf));
    315 
    316 	fb->hf_conf_index	= 0;	/* configuration index		*/
    317 	fb->hf_nconfs		= 1;   	/* how many configurations	*/
    318 	strncpy(fb->hf_name, "HD64461 video module", HPCFB_MAXNAMELEN);
    319 
    320 	/* frame buffer name */
    321 	strncpy(fb->hf_conf_name, "LCD", HPCFB_MAXNAMELEN);
    322 
    323 	/* configuration name */
    324 	fb->hf_height		= height;
    325 	fb->hf_width		= width;
    326 	fb->hf_baseaddr		= (u_long)fbvaddr;
    327 	fb->hf_offset		= (u_long)fbvaddr -
    328 	    sh3_ptob(sh3_btop(fbvaddr));
    329 
    330 	/* frame buffer start offset */
    331 	fb->hf_bytes_per_line	= (width * depth) / NBBY;
    332 	fb->hf_nplanes		= 1;
    333 	fb->hf_bytes_per_plane	= height * fb->hf_bytes_per_line;
    334 
    335 	fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
    336 	fb->hf_access_flags |= HPCFB_ACCESS_WORD;
    337 	fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
    338 	if (vc->vc_reverse)
    339 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    340 
    341 	switch (depth) {
    342 	default:
    343 		panic("%s: not supported color depth", __FUNCTION__);
    344 		/* NOTREACHED */
    345 	case 16:
    346 		fb->hf_class = HPCFB_CLASS_RGBCOLOR;
    347 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    348 		fb->hf_pack_width = 16;
    349 		fb->hf_pixels_per_pack = 1;
    350 		fb->hf_pixel_width = 16;
    351 
    352 		fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
    353 		/* reserved for future use */
    354 		fb->hf_u.hf_rgb.hf_flags = 0;
    355 
    356 		fb->hf_u.hf_rgb.hf_red_width = 5;
    357 		fb->hf_u.hf_rgb.hf_red_shift = 11;
    358 		fb->hf_u.hf_rgb.hf_green_width = 6;
    359 		fb->hf_u.hf_rgb.hf_green_shift = 5;
    360 		fb->hf_u.hf_rgb.hf_blue_width = 5;
    361 		fb->hf_u.hf_rgb.hf_blue_shift = 0;
    362 		fb->hf_u.hf_rgb.hf_alpha_width = 0;
    363 		fb->hf_u.hf_rgb.hf_alpha_shift = 0;
    364 		break;
    365 
    366 	case 8:
    367 		fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
    368 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    369 		fb->hf_pack_width = 8;
    370 		fb->hf_pixels_per_pack = 1;
    371 		fb->hf_pixel_width = 8;
    372 		fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
    373 		/* reserved for future use */
    374 		fb->hf_u.hf_indexed.hf_flags = 0;
    375 		break;
    376 	}
    377 }
    378 
    379 void
    380 hd64461video_hwaccel_init(struct hd64461video_chip *hvc)
    381 {
    382 	u_int16_t r;
    383 
    384 	r = HD64461_LCDGRCFGR_ACCRESET;
    385 	switch (hvc->vc.vc_fbdepth) {
    386 	default:
    387 		panic("no bitblit acceralation.");
    388 	case 16:
    389 		break;
    390 	case 8:
    391 		r |= HD64461_LCDGRCFGR_COLORDEPTH_8BPP;
    392 		break;
    393 	}
    394 	hd64461_reg_write_2(HD64461_LCDGRCFGR_REG16, r);
    395 
    396 	while ((hd64461_reg_read_2(HD64461_LCDGRCFGR_REG16) &
    397 	    HD64461_LCDGRCFGR_ACCSTATUS) != 0)
    398 		/* busy loop */;
    399 	r &= ~HD64461_LCDGRCFGR_ACCRESET;
    400 	hd64461_reg_write_2(HD64461_LCDGRCFGR_REG16, r);
    401 
    402 	while ((hd64461_reg_read_2(HD64461_LCDGRCFGR_REG16) &
    403 	    HD64461_LCDGRCFGR_ACCSTATUS) != 0)
    404 		/* busy loop */;
    405 
    406 	hd64461_reg_write_2(HD64461_LCDGRDOR_REG16,
    407 	    (hvc->vc.vc_fbwidth - 1) & HD64461_LCDGRDOR_MASK);
    408 }
    409 
    410 /* hpcfb ops */
    411 int
    412 hd64461video_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    413 {
    414 	struct hd64461video_softc *sc = (struct hd64461video_softc *)v;
    415 	struct hpcfb_fbconf *hf = &sc->sc_vc->hf;
    416 	struct hpcfb_fbconf *fbconf;
    417 	struct hpcfb_dspconf *dspconf;
    418 	struct wsdisplay_cmap *cmap;
    419 	struct wsdisplay_param *dispparam;
    420 	long id, idmax;
    421 	int turnoff;
    422 	u_int8_t *r, *g, *b;
    423 	int error;
    424 	size_t idx, cnt;
    425 
    426 	switch (cmd) {
    427 	case WSDISPLAYIO_GVIDEO:
    428 		*(u_int *)data = sc->sc_vc->blanked ?
    429 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    430 		return (0);
    431 
    432 	case WSDISPLAYIO_SVIDEO:
    433 		turnoff = (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF);
    434 		if (sc->sc_vc->blanked != turnoff) {
    435 			sc->sc_vc->blanked = turnoff;
    436 			if (turnoff)
    437 				hd64461video_off(sc->sc_vc);
    438 			else
    439 				hd64461video_on(sc->sc_vc);
    440 		}
    441 
    442 		return (0);
    443 
    444 	case WSDISPLAYIO_GETPARAM:
    445 		dispparam = (struct wsdisplay_param *)data;
    446 		dispparam->min = 0;
    447 		switch (dispparam->param) {
    448 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    449 			id = CONFIG_HOOK_POWER_LCDLIGHT;
    450 			idmax = -1;
    451 			dispparam->max = ~0;
    452 			break;
    453 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    454 			id = CONFIG_HOOK_BRIGHTNESS;
    455 			idmax = CONFIG_HOOK_BRIGHTNESS_MAX;
    456 			break;
    457 		case WSDISPLAYIO_PARAM_CONTRAST:
    458 			id = CONFIG_HOOK_CONTRAST;
    459 			idmax = CONFIG_HOOK_CONTRAST_MAX;
    460 			break;
    461 		default:
    462 			return (EINVAL);
    463 		}
    464 
    465 		if (idmax >= 0) {
    466 			error = config_hook_call(CONFIG_HOOK_GET, idmax,
    467 						 &dispparam->max);
    468 			if (error)
    469 				return (error);
    470 		}
    471 		return config_hook_call(CONFIG_HOOK_GET, id,
    472 					&dispparam->curval);
    473 
    474 	case WSDISPLAYIO_SETPARAM:
    475 		dispparam = (struct wsdisplay_param *)data;
    476 		switch (dispparam->param) {
    477 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    478 			id = CONFIG_HOOK_POWER_LCDLIGHT;
    479 			break;
    480 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    481 			id = CONFIG_HOOK_BRIGHTNESS;
    482 			break;
    483 		case WSDISPLAYIO_PARAM_CONTRAST:
    484 			id = CONFIG_HOOK_CONTRAST;
    485 			break;
    486 		default:
    487 			return (EINVAL);
    488 		}
    489 		return config_hook_call(CONFIG_HOOK_SET, id,
    490 					&dispparam->curval);
    491 
    492 	case WSDISPLAYIO_GETCMAP:
    493 		cmap = (struct wsdisplay_cmap *)data;
    494 		cnt = cmap->count;
    495 		idx = cmap->index;
    496 
    497 		if (hf->hf_class != HPCFB_CLASS_INDEXCOLOR ||
    498 		    hf->hf_pack_width != 8 ||
    499 		    !LEGAL_CLUT_INDEX(idx) ||
    500 		    !LEGAL_CLUT_INDEX(idx + cnt -1)) {
    501 			return (EINVAL);
    502 		}
    503 
    504 		error = cmap_work_alloc(&r, &g, &b, 0, cnt);
    505 		if (error)
    506 			goto out;
    507 		hd64461video_get_clut(sc->sc_vc, idx, cnt, r, g, b);
    508 		error = copyout(r, cmap->red, cnt);
    509 		if (error)
    510 			goto out;
    511 		error = copyout(g, cmap->green,cnt);
    512 		if (error)
    513 			goto out;
    514 		error = copyout(b, cmap->blue, cnt);
    515 
    516 out:
    517 		cmap_work_free(r, g, b, 0);
    518 		return error;
    519 
    520 	case WSDISPLAYIO_PUTCMAP:
    521 		cmap = (struct wsdisplay_cmap *)data;
    522 		cnt = cmap->count;
    523 		idx = cmap->index;
    524 
    525 		if (hf->hf_class != HPCFB_CLASS_INDEXCOLOR ||
    526 		    hf->hf_pack_width != 8 ||
    527 		    !LEGAL_CLUT_INDEX(idx) ||
    528 		    !LEGAL_CLUT_INDEX(idx + cnt -1)) {
    529 			return (EINVAL);
    530 		}
    531 
    532 		error = cmap_work_alloc(&r, &g, &b, 0, cnt);
    533 		if (error)
    534 			goto out;
    535 
    536 		error = copyin(cmap->red, r, cnt);
    537 		if (error)
    538 			goto out;
    539 		error = copyin(cmap->green,g, cnt);
    540 		if (error)
    541 			goto out;
    542 		error = copyin(cmap->blue, b, cnt);
    543 		if (error)
    544 			goto out;
    545 		hd64461video_set_clut(sc->sc_vc, idx, cnt, r, g, b);
    546 		goto out;
    547 
    548 	case HPCFBIO_GCONF:
    549 		fbconf = (struct hpcfb_fbconf *)data;
    550 		if (fbconf->hf_conf_index != 0 &&
    551 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
    552 			return (EINVAL);
    553 		}
    554 		*fbconf = *hf;	/* structure assignment */
    555 		return (0);
    556 
    557 	case HPCFBIO_SCONF:
    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 		/*
    564 		 * nothing to do because we have only one configuration
    565 		 */
    566 		return (0);
    567 
    568 	case HPCFBIO_GDSPCONF:
    569 		dspconf = (struct hpcfb_dspconf *)data;
    570 		if ((dspconf->hd_unit_index != 0 &&
    571 		    dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    572 		    (dspconf->hd_conf_index != 0 &&
    573 			dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    574 			return (EINVAL);
    575 		}
    576 		*dspconf = sc->sc_vc->hd;	/* structure assignment */
    577 		return (0);
    578 
    579 	case HPCFBIO_SDSPCONF:
    580 		dspconf = (struct hpcfb_dspconf *)data;
    581 		if ((dspconf->hd_unit_index != 0 &&
    582 		    dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    583 		    (dspconf->hd_conf_index != 0 &&
    584 			dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    585 			return (EINVAL);
    586 		}
    587 		/*
    588 		 * nothing to do
    589 		 * because we have only one unit and one configuration
    590 		 */
    591 		return (0);
    592 
    593 	case HPCFBIO_GOP:
    594 	case HPCFBIO_SOP:
    595 		/* XXX not implemented yet */
    596 		return (EINVAL);
    597 	}
    598 
    599 	return (EPASSTHROUGH);
    600 }
    601 
    602 paddr_t
    603 hd64461video_mmap(void *ctx, off_t offset, int prot)
    604 {
    605 	struct hd64461video_softc *sc = (struct hd64461video_softc *)ctx;
    606 	struct hpcfb_fbconf *hf = &sc->sc_vc->hf;
    607 
    608 	if (offset < 0 || (hf->hf_bytes_per_plane + hf->hf_offset) < offset)
    609 		return (-1);
    610 
    611 	return (sh3_btop(HD64461_FBBASE + offset));
    612 }
    613 
    614 void
    615 hd64461video_cursor(void *ctx, int on, int xd, int yd, int w, int h)
    616 {
    617 	struct hd64461video_softc *sc = (struct hd64461video_softc *)ctx;
    618 	int xw, yh, width, bpp, adr;
    619 	u_int16_t r;
    620 
    621 	width = sc->sc_vc->vc.vc_fbwidth;
    622 	bpp = sc->sc_vc->vc.vc_fbdepth;
    623 	xw = w - 1;
    624 	yh = h - 1;
    625 
    626 	/* Wait until previous command done. */
    627 	hd64461video_iodone(ctx);
    628 
    629 	/* Destination addr */
    630 	adr = width * yd + xd;
    631 	if (bpp == 16)
    632 		adr *= 2;
    633 	hd64461_reg_write_2(HD64461_LCDBBTDSARH_REG16,
    634 	    HD64461_LCDBBTDSARH(adr));
    635 	hd64461_reg_write_2(HD64461_LCDBBTDSARL_REG16,
    636 	    HD64461_LCDBBTDSARL(adr));
    637 
    638 	// Width
    639 	hd64461_reg_write_2(HD64461_LCDBBTDWR_REG16,
    640 	    xw & HD64461_LCDBBTDWR_MASK);
    641 
    642 	// Height
    643 	hd64461_reg_write_2(HD64461_LCDBBTDHR_REG16,
    644 	    yh & HD64461_LCDBBTDHR_MASK);
    645 
    646 	// Operation (Destination Invert)
    647 	hd64461_reg_write_2(HD64461_LCDBBTROPR_REG16,
    648 	    HD64461_LCDC_BITBLT_DSTINVERT);
    649 
    650 	// BitBLT mode (Destination Invert)
    651 	hd64461_reg_write_2(HD64461_LCDBBTMDR_REG16, 0);
    652 
    653 	// Kick.
    654 	r = hd64461_reg_read_2(HD64461_LCDGRCFGR_REG16);
    655 	r &= ~HD64461_LCDGRCFGR_ACCSTART_MASK;
    656 	r |= HD64461_LCDGRCFGR_ACCSTART_BITBLT;
    657 	hd64461_reg_write_2(HD64461_LCDGRCFGR_REG16, r);
    658 }
    659 
    660 void
    661 hd64461video_bitblit(void *ctx, int xs, int ys, int xd, int yd, int h, int w)
    662 {
    663 	struct hd64461video_softc *sc = (struct hd64461video_softc *)ctx;
    664 	int xw, yh, width, bpp, condition_a, adr;
    665 	u_int16_t r;
    666 
    667 	xw = w - 1;
    668 	yh = h - 1;
    669 	width = sc->sc_vc->vc.vc_fbwidth;
    670 	bpp = sc->sc_vc->vc.vc_fbdepth;
    671 	condition_a = ((ys == yd) && (xs <= xd)) || (ys < yd);
    672 
    673 	hd64461video_iodone(ctx);
    674 
    675 	// Source addr
    676 	if (condition_a)
    677 		adr = (width * (ys + yh)) + (xs + xw);
    678 	else
    679 		adr = width * ys + xs;
    680 	if (bpp == 16)
    681 		adr *= 2;
    682 
    683 	hd64461_reg_write_2(HD64461_LCDBBTSSARH_REG16,
    684 	    HD64461_LCDBBTSSARH(adr));
    685 	hd64461_reg_write_2(HD64461_LCDBBTSSARL_REG16,
    686 	    HD64461_LCDBBTSSARL(adr));
    687 
    688 	// Destination addr
    689 	if (condition_a)
    690 		adr = (width * (yd + yh)) + (xd + xw);
    691 	else
    692 		adr = width * yd + xd;
    693 	if (bpp == 16)
    694 		adr *= 2;
    695 
    696 	hd64461_reg_write_2(HD64461_LCDBBTDSARH_REG16,
    697 	    HD64461_LCDBBTDSARH(adr));
    698 	hd64461_reg_write_2(HD64461_LCDBBTDSARL_REG16,
    699 	    HD64461_LCDBBTDSARL(adr));
    700 
    701 	// Width
    702 	hd64461_reg_write_2(HD64461_LCDBBTDWR_REG16,
    703 	    xw & HD64461_LCDBBTDWR_MASK);
    704 
    705 	// Height
    706 	hd64461_reg_write_2(HD64461_LCDBBTDHR_REG16,
    707 	    yh & HD64461_LCDBBTDHR_MASK);
    708 
    709 	// Operation (source copy)
    710 	hd64461_reg_write_2(HD64461_LCDBBTROPR_REG16,
    711 	    HD64461_LCDC_BITBLT_SRCCOPY);
    712 
    713 	// BitBLT mode (on screen to on screen)
    714 	r = HD64461_LCDBBTMDR_SET(0,
    715 	    HD64461_LCDBBTMDR_ON_SCREEN_TO_ON_SCREEN);
    716 	if (condition_a)	/* reverse direction */
    717 	    r |= HD64461_LCDBBTMDR_SCANDRCT_RL_BT;
    718 	hd64461_reg_write_2(HD64461_LCDBBTMDR_REG16, r);
    719 
    720 	// Kick.
    721 	r = hd64461_reg_read_2(HD64461_LCDGRCFGR_REG16);
    722 	r &= ~HD64461_LCDGRCFGR_ACCSTART_MASK;
    723 	r |= HD64461_LCDGRCFGR_ACCSTART_BITBLT;
    724 	hd64461_reg_write_2(HD64461_LCDGRCFGR_REG16, r);
    725 }
    726 
    727 void
    728 hd64461video_erase(void *ctx, int xd, int yd, int h, int w, int attr)
    729 {
    730 	struct hd64461video_softc *sc = (struct hd64461video_softc *)ctx;
    731 	int xw, yh, width, bpp, adr;
    732 	u_int16_t r;
    733 
    734 	width = sc->sc_vc->vc.vc_fbwidth;
    735 	bpp = sc->sc_vc->vc.vc_fbdepth;
    736 	xw = w - 1;
    737 	yh = h - 1;
    738 
    739 	/* Wait until previous command done. */
    740 	hd64461video_iodone(ctx);
    741 
    742 	/* Destination addr */
    743 	adr = width * yd + xd;
    744 	if (bpp == 16)
    745 		adr *= 2;
    746 	hd64461_reg_write_2(HD64461_LCDBBTDSARH_REG16,
    747 	    HD64461_LCDBBTDSARH(adr));
    748 	hd64461_reg_write_2(HD64461_LCDBBTDSARL_REG16,
    749 	    HD64461_LCDBBTDSARL(adr));
    750 
    751 	// Width
    752 	hd64461_reg_write_2(HD64461_LCDBBTDWR_REG16,
    753 	    xw & HD64461_LCDBBTDWR_MASK);
    754 
    755 	// Height
    756 	hd64461_reg_write_2(HD64461_LCDBBTDHR_REG16,
    757 	    yh & HD64461_LCDBBTDHR_MASK);
    758 
    759 	// Color
    760 	hd64461_reg_write_2(HD64461_LCDGRSCR_REG16, 0); //XXX black only
    761 
    762 	// Operation (Solid Color Fill)
    763 	hd64461_reg_write_2(HD64461_LCDBBTROPR_REG16,
    764 	    HD64461_LCDC_BITBLT_PATCOPY);
    765 
    766 	// BitBLT mode (Solid Color)
    767 	hd64461_reg_write_2(HD64461_LCDBBTMDR_REG16,
    768 	    HD64461_LCDBBTMDR_PATSELECT_SOLIDCOLOR);
    769 
    770 	// Kick.
    771 	r = hd64461_reg_read_2(HD64461_LCDGRCFGR_REG16);
    772 	r &= ~HD64461_LCDGRCFGR_ACCSTART_MASK;
    773 	r |= HD64461_LCDGRCFGR_ACCSTART_BITBLT;
    774 	hd64461_reg_write_2(HD64461_LCDGRCFGR_REG16, r);
    775 }
    776 
    777 void
    778 hd64461video_putchar(void *ctx, int row, int col, struct wsdisplay_font *font,
    779     int fclr, int uclr, u_int uc, int attr)
    780 {
    781 	struct hd64461video_softc *sc = (struct hd64461video_softc *)ctx;
    782 	int w, h, cw;
    783 
    784 	w = font->fontwidth;
    785 	h = font->fontheight;
    786 	cw = sc->sc_font.cw;
    787 	hd64461video_bitblit(ctx, (uc % cw) * w,
    788 	    sc->sc_vc->vc.vc_fbheight + (uc / cw) * h, row, col, h, w);
    789 }
    790 
    791 void
    792 hd64461video_setclut(void *ctx, struct rasops_info *info)
    793 {
    794 	struct hd64461video_softc *sc = (struct hd64461video_softc *)ctx;
    795 
    796 	if (sc->sc_vc->vc.vc_fbdepth != 8)
    797 		return;
    798 }
    799 
    800 void
    801 hd64461video_font(void *ctx, struct wsdisplay_font *font)
    802 {
    803 	struct hd64461video_softc *sc = (struct hd64461video_softc *)ctx;
    804 
    805 	hd64461video_font_set_attr(sc, font);
    806 	hd64461video_font_load(sc);
    807 }
    808 
    809 void
    810 hd64461video_iodone(void *ctx)
    811 {
    812 	while ((hd64461_reg_read_2(HD64461_LCDGRCFGR_REG16) &
    813 	    HD64461_LCDGRCFGR_ACCSTATUS) != 0)
    814 		/* busy loop */;
    815 }
    816 
    817 /* internal */
    818 void
    819 hd64461video_font_load_16bpp(u_int16_t *d, u_int8_t *s, int w, int h, int step)
    820 {
    821 	int i, j, n;
    822 	n = step / sizeof(u_int16_t);
    823 
    824 	for (i = 0; i < h; i++, d += n) {
    825 		for (j = 0; j < w; j++) {
    826 			d[j] = *s & (1 << (w - j - 1)) ? 0xffff : 0x0000;
    827 		}
    828 		s++;
    829 	}
    830 }
    831 
    832 void
    833 hd64461video_font_load_8bpp(u_int8_t *d, u_int8_t *s, int w, int h, int step)
    834 {
    835 	int i, j, n;
    836 	n = step / sizeof(u_int8_t);
    837 
    838 	for (i = 0; i < h; i++, d += n) {
    839 		for (j = 0; j < w; j++) {
    840 			d[j] = *s & (1 << (w - j - 1)) ? 0xff : 0x00;
    841 		}
    842 		s++;
    843 	}
    844 }
    845 
    846 void
    847 hd64461video_font_set_attr(struct hd64461video_softc *sc,
    848     struct wsdisplay_font *f)
    849 {
    850 	struct hd64461video_chip *hvc = sc->sc_vc;
    851 	struct wsdisplay_font *font = (struct wsdisplay_font *)&sc->sc_font;
    852 	int w, h, bpp;
    853 
    854 	w	= f->fontwidth;
    855 	h	= f->fontheight;
    856 	bpp	= hvc->vc.vc_fbdepth;
    857 
    858 	*font = *f;
    859 	sc->sc_font.c = (w * bpp) / NBBY;
    860 	sc->sc_font.cw = hvc->hf.hf_width / w;
    861 	sc->sc_font.cstep = ((w * h * bpp) / NBBY) * sc->sc_font.cw;
    862 
    863 	DPRINTF("c = %d cw = %d cstep = %d\n", sc->sc_font.c,
    864 	    sc->sc_font.cw, sc->sc_font.cstep);
    865 
    866 }
    867 
    868 /* return frame buffer virtual address of charcter #n */
    869 vaddr_t
    870 hd64461video_font_start_addr(struct hd64461video_softc *sc, int n)
    871 {
    872 	struct hd64461video_chip *hvc = sc->sc_vc;
    873 	struct hd64461video_font *font = &sc->sc_font;
    874 	vaddr_t base;
    875 
    876 	base = (vaddr_t)hvc->off_screen_addr;
    877 	base += (n / font->cw) * font->cstep + font->c * (n % font->cw);
    878 
    879 	return base;
    880 }
    881 
    882 void
    883 hd64461video_font_load(struct hd64461video_softc *sc)
    884 {
    885 	struct hd64461video_chip *hvc = sc->sc_vc;
    886 	struct wsdisplay_font *font = (struct wsdisplay_font *)&sc->sc_font;
    887 	u_int8_t *q;
    888 	int w, h, step, i, n;
    889 
    890 	if (sc->sc_font.loaded) {
    891 		printf("reload font\n");
    892 	}
    893 
    894 	w	= font->fontwidth;
    895 	h	= font->fontheight;
    896 	step	= sc->sc_font.cw * sc->sc_font.c;
    897 	n	= (w * h) / NBBY;
    898 	q	= font->data;
    899 
    900 	DPRINTF("%s (%dx%d) %d+%d\n", font->name, w, h, font->firstchar,
    901 	    font->numchars);
    902 	DPRINTF("bitorder %d byteorder %d stride %d\n", font->bitorder,
    903 	    font->byteorder, font->stride);
    904 
    905 	switch (hvc->vc.vc_fbdepth) {
    906 	case 8:
    907 		for (i = font->firstchar; i < font->numchars; i++) {
    908 			hd64461video_font_load_8bpp
    909 			    ((u_int8_t *)hd64461video_font_start_addr(sc, i),
    910 				q, w, h, step);
    911 			q += n;
    912 		}
    913 		break;
    914 	case 16:
    915 		for (i = font->firstchar; i < font->numchars; i++) {
    916 			hd64461video_font_load_16bpp
    917 			    ((u_int16_t *)hd64461video_font_start_addr(sc, i),
    918 				q, w, h, step);
    919 			q += n;
    920 		}
    921 		break;
    922 	}
    923 
    924 	sc->sc_font.loaded = TRUE;
    925 }
    926 
    927 void
    928 hd64461video_update_videochip_status(struct hd64461video_chip *hvc)
    929 {
    930 	struct video_chip *vc = &hvc->vc;
    931 	u_int16_t r;
    932 	int i;
    933 	int depth, width, height;
    934 
    935 	depth = 0;		/* XXX: -Wuninitialized */
    936 
    937 	/* display mode */
    938 	r = hd64461_reg_read_2(HD64461_LCDLDR3_REG16);
    939 	i = HD64461_LCDLDR3_CG(r);
    940 	switch (i) {
    941 	case HD64461_LCDLDR3_CG_COLOR16:
    942 		depth = 16;
    943 		hvc->mode = LCD64K_C;
    944 		break;
    945 	case HD64461_LCDLDR3_CG_COLOR8:
    946 		depth = 8;
    947 		hvc->mode = LCD256_C;
    948 		break;
    949 	case HD64461_LCDLDR3_CG_GRAY6:
    950 		depth = 6;
    951 		hvc->mode = LCD64_MONO;
    952 		break;
    953 	case HD64461_LCDLDR3_CG_GRAY4:
    954 		depth = 4;
    955 		hvc->mode = LCD16_MONO;
    956 		break;
    957 	case HD64461_LCDLDR3_CG_GRAY2:
    958 		depth = 2;
    959 		hvc->mode = LCD4_MONO;
    960 		break;
    961 	case HD64461_LCDLDR3_CG_GRAY1:
    962 		depth = 1;
    963 		hvc->mode = LCD2_MONO;
    964 		break;
    965 	}
    966 
    967 	r = hd64461_reg_read_2(HD64461_LCDCCR_REG16);
    968 	i = HD64461_LCDCCR_DSPSEL(i);
    969 	switch (i) {
    970 	case HD64461_LCDCCR_DSPSEL_LCD_CRT:
    971 		depth = 8;
    972 		hvc->mode = LCDCRT;
    973 		break;
    974 	case HD64461_LCDCCR_DSPSEL_CRT:
    975 		depth = 8;
    976 		hvc->mode = CRT256_C;
    977 		break;
    978 	case HD64461_LCDCCR_DSPSEL_LCD:
    979 		/* nothing to do */
    980 		break;
    981 	}
    982 
    983 	callout_init(&hvc->unblank_ch);
    984 	hvc->blanked = 0;
    985 
    986 	width = bootinfo->fb_width;
    987 	height = bootinfo->fb_height;
    988 
    989 	vc->vc_fbvaddr	= HD64461_FBBASE;
    990 	vc->vc_fbpaddr	= HD64461_FBBASE;
    991 	vc->vc_fbdepth	= depth;
    992 	vc->vc_fbsize	= (width * height * depth) / NBBY;
    993 	vc->vc_fbwidth	= width;
    994 	vc->vc_fbheight	= height;
    995 }
    996 
    997 #if notyet
    998 void
    999 hd64461video_set_display_mode(struct hd64461video_chip *hvc)
   1000 {
   1001 
   1002 	if (hvc->mode == LCDCRT || hvc->mode == CRT256_C)
   1003 		hd64461video_set_display_mode_crtc(hvc);
   1004 
   1005 	hd64461video_set_display_mode_lcdc(hvc);
   1006 }
   1007 
   1008 void
   1009 hd64461video_set_display_mode_lcdc(struct hd64461video_chip *hvc)
   1010 {
   1011 	struct {
   1012 		u_int16_t clor;	/* display size 640 x 240 */
   1013 		u_int16_t ldr3;
   1014 		const char *name;
   1015 	} disp_conf[] = {
   1016 		[LCD256_C]	= { 0x280 , HD64461_LCDLDR3_CG_COLOR8 ,
   1017 				    "8bit color" },
   1018 		[LCD64K_C]	= { 0x500 , HD64461_LCDLDR3_CG_COLOR16 ,
   1019 				    "16bit color" },
   1020 		[LCD64_MONO]	= { 0x280 , HD64461_LCDLDR3_CG_GRAY6 ,
   1021 				    "6bit gray scale" },
   1022 		[LCD16_MONO]	= { 0x140 , HD64461_LCDLDR3_CG_GRAY4 ,
   1023 				    "4bit gray scale" },
   1024 		[LCD4_MONO]	= { 0x0a0 , HD64461_LCDLDR3_CG_GRAY2 ,
   1025 				    "2bit gray scale" },
   1026 		[LCD2_MONO]	= { 0x050 , HD64461_LCDLDR3_CG_GRAY1 ,
   1027 				    "mono chrome" },
   1028 	}, *conf;
   1029 	u_int16_t r;
   1030 	int omode;
   1031 
   1032 	conf = &disp_conf[hvc->mode];
   1033 
   1034 	hd64461_reg_write_2(HD64461_LCDCLOR_REG16, conf->clor);
   1035 	r = hd64461_reg_read_2(HD64461_LCDLDR3_REG16);
   1036 	omode = HD64461_LCDLDR3_CG(r);
   1037 	r = HD64461_LCDLDR3_CG_CLR(r);
   1038 	r = HD64461_LCDLDR3_CG_SET(r, conf->ldr3);
   1039 	hd64461_reg_write_2(HD64461_LCDLDR3_REG16, r);
   1040 
   1041 	printf("%s ", conf->name);
   1042 }
   1043 
   1044 void
   1045 hd64461video_set_display_mode_crtc(struct hd64461video_chip *hvc)
   1046 {
   1047 	/* not yet */
   1048 }
   1049 #endif /* notyet */
   1050 
   1051 size_t
   1052 hd64461video_frame_buffer_size(struct hd64461video_chip *hvc)
   1053 {
   1054 	vaddr_t page, startaddr, endaddr;
   1055 	int x;
   1056 
   1057 	startaddr = HD64461_FBBASE;
   1058 	endaddr = startaddr + HD64461_FBSIZE - 1;
   1059 
   1060 	page = startaddr;
   1061 
   1062 	x = random();
   1063 	*(volatile int *)(page + 0) = x;
   1064 	*(volatile int *)(page + 4) = ~x;
   1065 
   1066 	if (*(volatile int *)(page + 0) != x ||
   1067 	    *(volatile int *)(page + 4) != ~x)
   1068 		return (0);
   1069 
   1070 	for (page += HD64461_FBPAGESIZE; page < endaddr;
   1071 	    page += HD64461_FBPAGESIZE) {
   1072 		if (*(volatile int *)(page + 0) == x &&
   1073 		    *(volatile int *)(page + 4) == ~x)
   1074 			goto fbend_found;
   1075 	}
   1076 
   1077 	page -= HD64461_FBPAGESIZE;
   1078 	*(volatile int *)(page + 0) = x;
   1079 	*(volatile int *)(page + 4) = ~x;
   1080 
   1081 	if (*(volatile int *)(page + 0) != x ||
   1082 	    *(volatile int *)(page + 4) != ~x)
   1083 		return (0);
   1084 
   1085  fbend_found:
   1086 	return (page - startaddr);
   1087 }
   1088 
   1089 void
   1090 hd64461video_set_clut(struct hd64461video_chip *vc, int idx, int cnt,
   1091     u_int8_t *r, u_int8_t *g, u_int8_t *b)
   1092 {
   1093 	KASSERT(r && g && b);
   1094 
   1095 	/* index pallete */
   1096 	hd64461_reg_write_2(HD64461_LCDCPTWAR_REG16,
   1097 	    HD64461_LCDCPTWAR_SET(0, idx));
   1098 	/* set data */
   1099 	while (cnt && LEGAL_CLUT_INDEX(idx)) {
   1100 		u_int16_t v;
   1101 #define	HD64461VIDEO_SET_CLUT(x)					\
   1102 		v = (x >> 2) & 0x3f;					\
   1103 		hd64461_reg_write_2(HD64461_LCDCPTWDR_REG16, v)
   1104 		HD64461VIDEO_SET_CLUT(*r);
   1105 		HD64461VIDEO_SET_CLUT(*g);
   1106 		HD64461VIDEO_SET_CLUT(*b);
   1107 #undef	HD64461VIDEO_SET_CLUT
   1108 		r++, g++, b++;
   1109 		idx++, cnt--;
   1110 	}
   1111 }
   1112 
   1113 void
   1114 hd64461video_get_clut(struct hd64461video_chip *vc, int idx, int cnt,
   1115     u_int8_t *r, u_int8_t *g, u_int8_t *b)
   1116 {
   1117 	KASSERT(r && g && b);
   1118 
   1119 	/* index pallete */
   1120 	hd64461_reg_write_2(HD64461_LCDCPTRAR_REG16,
   1121 	    HD64461_LCDCPTRAR_SET(0, idx));
   1122 
   1123 	/* get data */
   1124 	while (cnt && LEGAL_CLUT_INDEX(idx)) {
   1125 		u_int16_t v;
   1126 #define	HD64461VIDEO_GET_CLUT(x)					\
   1127 	v = hd64461_reg_read_2(HD64461_LCDCPTRDR_REG16);		\
   1128 	x = HD64461_LCDCPTRDR(v);					\
   1129 	x <<= 2
   1130 		HD64461VIDEO_GET_CLUT(*r);
   1131 		HD64461VIDEO_GET_CLUT(*g);
   1132 		HD64461VIDEO_GET_CLUT(*b);
   1133 #undef	HD64461VIDEO_GET_CLUT
   1134 		r++, g++, b++;
   1135 		idx++, cnt--;
   1136 	}
   1137 }
   1138 
   1139 int
   1140 hd64461video_power(void *ctx, int type, long id, void *msg)
   1141 {
   1142 	struct hd64461video_softc *sc = ctx;
   1143 	struct hd64461video_chip *hvc = sc->sc_vc;
   1144 
   1145 	switch ((int)msg) {
   1146 	case PWR_RESUME:
   1147 		if (!hvc->console)
   1148 			break; /* serial console */
   1149 		DPRINTF("%s: ON\n", sc->sc_dev.dv_xname);
   1150 		hd64461video_on(hvc);
   1151 		break;
   1152 	case PWR_SUSPEND:
   1153 		/* FALLTHROUGH */
   1154 	case PWR_STANDBY:
   1155 		DPRINTF("%s: OFF\n", sc->sc_dev.dv_xname);
   1156 		hd64461video_off(hvc);
   1157 		break;
   1158 	}
   1159 
   1160 	return 0;
   1161 }
   1162 
   1163 void
   1164 hd64461video_off(struct hd64461video_chip *vc)
   1165 {
   1166 
   1167 	callout_stop(&vc->unblank_ch);
   1168 
   1169 	/* turn off display in LCDC */
   1170 	hd64461video_display_onoff(vc, FALSE);
   1171 
   1172 	/* turn off the LCD */
   1173 	config_hook_call(CONFIG_HOOK_POWERCONTROL,
   1174 			 CONFIG_HOOK_POWERCONTROL_LCD,
   1175 			 (void *)0);
   1176 }
   1177 
   1178 void
   1179 hd64461video_on(struct hd64461video_chip *vc)
   1180 {
   1181 	int err;
   1182 
   1183 	/* turn on the LCD */
   1184 	err = config_hook_call(CONFIG_HOOK_POWERCONTROL,
   1185 			       CONFIG_HOOK_POWERCONTROL_LCD,
   1186 			       (void *)1);
   1187 
   1188 	if (err == 0)
   1189 		/* let the LCD warm up before turning on the display */
   1190 		callout_reset(&vc->unblank_ch, hz/2,
   1191 		    hd64461video_display_on, vc);
   1192 	else
   1193 		hd64461video_display_onoff(vc, TRUE);
   1194 }
   1195 
   1196 void
   1197 hd64461video_display_on(void *arg)
   1198 {
   1199 
   1200 	hd64461video_display_onoff(arg, TRUE);
   1201 }
   1202 
   1203 void
   1204 hd64461video_display_onoff(void *arg, boolean_t on)
   1205 {
   1206 	/* struct hd64461video_chip *vc = arg; */
   1207 	u_int16_t r;
   1208 
   1209 	/* turn on/off display in LCDC */
   1210 	r = hd64461_reg_read_2(HD64461_LCDLDR1_REG16);
   1211 	if (on)
   1212 		r |= HD64461_LCDLDR1_DON;
   1213 	else
   1214 		r &= ~HD64461_LCDLDR1_DON;
   1215 	hd64461_reg_write_2(HD64461_LCDLDR1_REG16, r);
   1216 }
   1217 
   1218 #ifdef HD64461VIDEO_DEBUG
   1219 void
   1220 hd64461video_info(struct hd64461video_softc *sc)
   1221 {
   1222 	u_int16_t r;
   1223 	int color;
   1224 	int i;
   1225 
   1226 	dbg_banner_function();
   1227 	printf("---[LCD]---\n");
   1228 	/* Base Address Register */
   1229 	r = hd64461_reg_read_2(HD64461_LCDCBAR_REG16);
   1230 	printf("LCDCBAR Frame buffer base address (4k Byte align): 0x%08x\n",
   1231 	    HD64461_LCDCBAR_BASEADDR(r));
   1232 
   1233 	/* Line Address Offset Register */
   1234 	r = hd64461_reg_read_2(HD64461_LCDCLOR_REG16);
   1235 	printf("LCDCLOR Line address offset: %d\n", HD64461_LCDCLOR(r));
   1236 
   1237 	/* LCDC Control Register */
   1238 	r = hd64461_reg_read_2(HD64461_LCDCCR_REG16);
   1239 	i = HD64461_LCDCCR_DSPSEL(r);
   1240 #define	DBG_BITMASK_PRINT(r, m)	dbg_bitmask_print(r, HD64461_LCDCCR_##m, #m)
   1241 	printf("LCDCCR (LCD Control Register)\n");
   1242 	DBG_BITMASK_PRINT(r, STBAK);
   1243 	DBG_BITMASK_PRINT(r, STREQ);
   1244 	DBG_BITMASK_PRINT(r, MOFF);
   1245 	DBG_BITMASK_PRINT(r, REFSEL);
   1246 	DBG_BITMASK_PRINT(r, EPON);
   1247 	DBG_BITMASK_PRINT(r, SPON);
   1248 	printf("\n");
   1249 #undef	DBG_BITMASK_PRINT
   1250 	printf("LCDCCR Display selct LCD[%c] CRT[%c]\n",
   1251 	    i == HD64461_LCDCCR_DSPSEL_LCD_CRT ||
   1252 	    i == HD64461_LCDCCR_DSPSEL_LCD ? 'x' : '_',
   1253 	    i == HD64461_LCDCCR_DSPSEL_LCD_CRT ||
   1254 	    i == HD64461_LCDCCR_DSPSEL_CRT ? 'x' : '_');
   1255 
   1256 	/* LCD Display Register */
   1257 	/* 1 */
   1258 	r = hd64461_reg_read_2(HD64461_LCDLDR1_REG16);
   1259 	printf("(LCD Display Register)\n");
   1260 #define	DBG_BITMASK_PRINT(r, m)	dbg_bitmask_print(r, HD64461_LCDLDR1_##m, #m)
   1261 	printf("LCDLDR1: ");
   1262 	DBG_BITMASK_PRINT(r, DINV);
   1263 	DBG_BITMASK_PRINT(r, DON);
   1264 	printf("\n");
   1265 #undef	DBG_BITMASK_PRINT
   1266 	/* 2 */
   1267 	r = hd64461_reg_read_2(HD64461_LCDLDR2_REG16);
   1268 	i = HD64461_LCDLDR2_LM(r);
   1269 #define	DBG_BITMASK_PRINT(r, m)	dbg_bitmask_print(r, HD64461_LCDLDR2_##m, #m)
   1270 	printf("LCDLDR2: ");
   1271 	DBG_BITMASK_PRINT(r, CC1);
   1272 	DBG_BITMASK_PRINT(r, CC2);
   1273 #undef	DBG_BITMASK_PRINT
   1274 	color = 0;
   1275 	switch (i) {
   1276 	default:
   1277 		panic("unknown unknown LCD interface.");
   1278 		break;
   1279 	case HD64461_LCDLDR2_LM_COLOR:
   1280 		color = 1;
   1281 		printf("Color");
   1282 		break;
   1283 	case HD64461_LCDLDR2_LM_GRAY8:
   1284 		printf("8-bit grayscale");
   1285 		break;
   1286 	case HD64461_LCDLDR2_LM_GRAY4:
   1287 		printf("8-bit grayscale");
   1288 		break;
   1289 	}
   1290 	printf(" LCD interface\n");
   1291 	/* 3 */
   1292 	printf("LCDLDR3: ");
   1293 	r = hd64461_reg_read_2(HD64461_LCDLDR3_REG16);
   1294 	i = HD64461_LCDLDR3_CS(r);
   1295 	printf("CS ");
   1296 	switch (i) {
   1297 	case 0:
   1298 		printf("15");
   1299 		break;
   1300 	case 1:
   1301 		printf("2.5");
   1302 		break;
   1303 	case 2:
   1304 		printf("3.75");
   1305 		break;
   1306 	case 4:
   1307 		printf("5");
   1308 		break;
   1309 	case 8:
   1310 		printf("7.5");
   1311 		break;
   1312 	case 16:
   1313 		printf("10");
   1314 		break;
   1315 	}
   1316 	printf("%s MHz ", color ? "" : "/2");
   1317 	i = HD64461_LCDLDR3_CG(r);
   1318 	switch (i) {
   1319 	case HD64461_LCDLDR3_CG_COLOR16:
   1320 		printf("Color 64K colors\n");
   1321 		break;
   1322 	case HD64461_LCDLDR3_CG_COLOR8:
   1323 		printf("Color 256 colors\n");
   1324 		break;
   1325 	case HD64461_LCDLDR3_CG_GRAY6:
   1326 		printf("6-bit Grayscale\n");
   1327 		break;
   1328 	case HD64461_LCDLDR3_CG_GRAY4:
   1329 		printf("4-bit Grayscale\n");
   1330 		break;
   1331 	case HD64461_LCDLDR3_CG_GRAY2:
   1332 		printf("2-bit Grayscale\n");
   1333 		break;
   1334 	case HD64461_LCDLDR3_CG_GRAY1:
   1335 		printf("1-bit Grayscale\n");
   1336 		break;
   1337 	}
   1338 
   1339 	/* LCD Number of Characters in Horizontal Register */
   1340 	r = hd64461_reg_read_2(HD64461_LCDLDHNCR_REG16);
   1341 	printf("LDHNCR: NHD %d NHT %d (# of horizontal characters)\n",
   1342 	    HD64461_LCDLDHNCR_NHD(r), HD64461_LCDLDHNCR_NHT(r));
   1343 
   1344 	/* Start Position of Horizontal Register */
   1345 	r = hd64461_reg_read_2(HD64461_LCDLDHNSR_REG16);
   1346 	printf("LDHNSR: HSW %d HSP %d (start position of horizontal)\n",
   1347 	    HD64461_LCDLDHNSR_HSW(r), HD64461_LCDLDHNSR_HSP(r));
   1348 
   1349 	/* Total Vertical Lines Register */
   1350 	r = hd64461_reg_read_2(HD64461_LCDLDVNTR_REG16);
   1351 	printf("LDVNTR: %d (total vertical lines)\n",
   1352 	    HD64461_LCDLDVNTR_VTL(r));
   1353 
   1354 	/* Display Vertical Lines Register */
   1355 	r = hd64461_reg_read_2(HD64461_LCDLDVNDR_REG16);
   1356 	printf("LDVNDR: %d (display vertical lines)\n",
   1357 	    HD64461_LCDLDVSPR_VSP(r));
   1358 
   1359 	/* Vertical Synchronization Position Register */
   1360 	r = hd64461_reg_read_2(HD64461_LCDLDVSPR_REG16);
   1361 	printf("LDVSPR: %d (vertical synchronization position)\n",
   1362 	    HD64461_LCDLDVSPR_VSP(r));
   1363 
   1364 	/*
   1365 	 * CRT Control Register
   1366 	 */
   1367 	printf("---[CRT]---\n");
   1368 	r = hd64461_reg_read_2(HD64461_LCDCRTVTR_REG16);
   1369 	printf("CRTVTR: %d (CRTC total vertical lines)\n",
   1370 	    HD64461_LCDCRTVTR(r));
   1371 	r = hd64461_reg_read_2(HD64461_LCDCRTVRSR_REG16);
   1372 	printf("CRTVRSR: %d (CRTC vertical retrace start line)\n",
   1373 	    HD64461_LCDCRTVRSR(r));
   1374 	r = hd64461_reg_read_2(HD64461_LCDCRTVRER_REG16);
   1375 	printf("CRTVRER: %d (CRTC vertical retrace end line)\n",
   1376 	    HD64461_LCDCRTVRER(r));
   1377 
   1378 }
   1379 
   1380 void
   1381 hd64461video_dump()
   1382 {
   1383 	u_int16_t r;
   1384 	printf("---[Display Mode Setting]---\n");
   1385 #define	DUMPREG(x)							\
   1386 	r = hd64461_reg_read_2(HD64461_LCD ## x ## _REG16);		\
   1387 	__dbg_bit_print(r, sizeof(u_int16_t), 0, 0, #x, DBG_BIT_PRINT_COUNT)
   1388 	DUMPREG(CBAR);
   1389 	DUMPREG(CLOR);
   1390 	DUMPREG(CCR);
   1391 	DUMPREG(LDR1);
   1392 	DUMPREG(LDR2);
   1393 	DUMPREG(LDHNCR);
   1394 	DUMPREG(LDHNSR);
   1395 	DUMPREG(LDVNTR);
   1396 	DUMPREG(LDVNDR);
   1397 	DUMPREG(LDVSPR);
   1398 	DUMPREG(LDR3);
   1399 	DUMPREG(CRTVTR);
   1400 	DUMPREG(CRTVRSR);
   1401 	DUMPREG(CRTVRER);
   1402 #undef	DUMPREG
   1403 	dbg_banner_line();
   1404 }
   1405 
   1406 #endif /* HD64461VIDEO_DEBUG */
   1407