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