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