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