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