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