Home | History | Annotate | Line # | Download | only in dev
mq200.c revision 1.8
      1 /*	$NetBSD: mq200.c,v 1.8 2001/02/15 09:17:18 sato Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Takemura Shin
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  *
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/kernel.h>
     34 #include <sys/device.h>
     35 #include <sys/systm.h>
     36 #include <sys/reboot.h>
     37 
     38 #include <uvm/uvm_extern.h>
     39 
     40 #include <dev/wscons/wsconsio.h>
     41 
     42 #include <machine/bootinfo.h>
     43 #include <machine/bus.h>
     44 #include <machine/autoconf.h>
     45 #include <machine/config_hook.h>
     46 #include <machine/platid.h>
     47 #include <machine/platid_mask.h>
     48 
     49 #include <hpcmips/dev/mq200reg.h>
     50 #include <hpcmips/dev/mq200var.h>
     51 #include "bivideo.h"
     52 #if NBIVIDEO > 0
     53 #include <hpcmips/dev/bivideovar.h>
     54 #endif
     55 
     56 #define MQ200DEBUG
     57 #ifdef MQ200DEBUG
     58 #ifndef MQ200DEBUG_CONF
     59 #define MQ200DEBUG_CONF 0
     60 #endif
     61 int	mq200_debug = MQ200DEBUG_CONF;
     62 #define	DPRINTF(arg)     do { if (mq200_debug) printf arg; } while(0);
     63 #define	DPRINTFN(n, arg) do { if (mq200_debug > (n)) printf arg; } while (0);
     64 #define	VPRINTF(arg)     do { if (bootverbose || mq200_debug) printf arg; } while(0);
     65 #define	VPRINTFN(n, arg) do { if (bootverbose || mq200_debug > (n)) printf arg; } while (0);
     66 #else
     67 #define	DPRINTF(arg)     do { } while (0);
     68 #define DPRINTFN(n, arg) do { } while (0);
     69 #define	VPRINTF(arg)     do { if (bootverbose) printf arg; } while(0);
     70 #define	VPRINTFN(n, arg) do { if (bootverbose) printf arg; } while (0);
     71 #endif
     72 
     73 /*
     74  * function prototypes
     75  */
     76 static void	mq200_power __P((int, void *));
     77 static int	mq200_hardpower __P((void *, int, long, void *));
     78 static int	mq200_fbinit __P((struct hpcfb_fbconf *));
     79 static int	mq200_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     80 static paddr_t	mq200_mmap __P((void *, off_t offset, int));
     81 
     82 /*
     83  * static variables
     84  */
     85 struct hpcfb_accessops mq200_ha = {
     86 	mq200_ioctl, mq200_mmap
     87 };
     88 
     89 int
     90 mq200_probe(iot, ioh)
     91 	bus_space_tag_t iot;
     92 	bus_space_handle_t ioh;
     93 {
     94 	unsigned long regval;
     95 
     96 #if NBIVIDEO > 0
     97 	if (bivideo_dont_attach) /* some video driver already attached */
     98 		return (0);
     99 #endif /* NBIVIDEO > 0 */
    100 
    101 	regval = bus_space_read_4(iot, ioh, MQ200_PC00R);
    102 	VPRINTF(("mq200 probe: vendor id=%04lx product id=%04lx\n",
    103 		 regval & 0xffff, (regval >> 16) & 0xffff));
    104 	if (regval != ((MQ200_PRODUCT_ID << 16) | MQ200_VENDOR_ID))
    105 		return (0);
    106 
    107 	return (1);
    108 }
    109 
    110 void
    111 mq200_attach(sc)
    112 	struct mq200_softc *sc;
    113 {
    114 	unsigned long regval;
    115 	struct hpcfb_attach_args ha;
    116 	int console = (bootinfo->bi_cnuse & BI_CNUSE_SERIAL) ? 0 : 1;
    117 
    118 	printf(": ");
    119 	if (mq200_fbinit(&sc->sc_fbconf) != 0) {
    120 		/* just return so that hpcfb will not be attached */
    121 		return;
    122 	}
    123 
    124 	sc->sc_fbconf.hf_baseaddr = (u_long)bootinfo->fb_addr;
    125 	sc->sc_fbconf.hf_offset	= (u_long)sc->sc_fbconf.hf_baseaddr -
    126 	    MIPS_PHYS_TO_KSEG1(mips_ptob(mips_btop(sc->sc_baseaddr)));
    127 	DPRINTF(("hf_baseaddr=%lx\n", sc->sc_fbconf.hf_baseaddr));
    128 	DPRINTF(("hf_offset=%lx\n", sc->sc_fbconf.hf_offset));
    129 
    130 	regval = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MQ200_PC08R);
    131 	printf("MQ200 Rev.%02lx video controller", regval & 0xff);
    132 	if (console) {
    133 		printf(", console");
    134 	}
    135 	printf("\n");
    136         printf("%s: framebuffer address: 0x%08lx\n",
    137 		sc->sc_dev.dv_xname, (u_long)bootinfo->fb_addr);
    138 
    139 	/* Add a power hook to power saving */
    140 	sc->sc_powerstate = MQ200_POWERSTATE_D0;
    141 	sc->sc_powerhook = powerhook_establish(mq200_power, sc);
    142 	if (sc->sc_powerhook == NULL)
    143 		printf("%s: WARNING: unable to establish power hook\n",
    144 			sc->sc_dev.dv_xname);
    145 
    146 	/* Add a hard power hook to power saving */
    147 	sc->sc_hardpowerhook = config_hook(CONFIG_HOOK_PMEVENT,
    148 					   CONFIG_HOOK_PMEVENT_HARDPOWER,
    149 					   CONFIG_HOOK_SHARE,
    150 					   mq200_hardpower, sc);
    151 	if (sc->sc_hardpowerhook == NULL)
    152 		printf("%s: WARNING: unable to establish hard power hook\n",
    153 			sc->sc_dev.dv_xname);
    154 
    155 	if (console && hpcfb_cnattach(&sc->sc_fbconf) != 0) {
    156 		panic("mq200_attach: can't init fb console");
    157 	}
    158 
    159 	ha.ha_console = console;
    160 	ha.ha_accessops = &mq200_ha;
    161 	ha.ha_accessctx = sc;
    162 	ha.ha_curfbconf = 0;
    163 	ha.ha_nfbconf = 1;
    164 	ha.ha_fbconflist = &sc->sc_fbconf;
    165 	ha.ha_curdspconf = 0;
    166 	ha.ha_ndspconf = 1;
    167 	ha.ha_dspconflist = &sc->sc_dspconf;
    168 
    169 	config_found(&sc->sc_dev, &ha, hpcfbprint);
    170 
    171 #if NBIVIDEO > 0
    172 	/*
    173 	 * bivideo is no longer need
    174 	 */
    175 	bivideo_dont_attach = 1;
    176 #endif /* NBIVIDEO > 0 */
    177 }
    178 
    179 static void
    180 mq200_power(why, arg)
    181 	int why;
    182 	void *arg;
    183 {
    184 #if 0
    185 	struct mq200_softc *sc = arg;
    186 
    187 	switch (why) {
    188 	case PWR_SUSPEND:
    189 		sc->sc_powerstate = MQ200_POWERSTATE_D2;
    190 		break;
    191 	case PWR_STANDBY:
    192 		sc->sc_powerstate = MQ200_POWERSTATE_D3;
    193 		break;
    194 	case PWR_RESUME:
    195 		sc->sc_powerstate = MQ200_POWERSTATE_D0;
    196 		break;
    197 	}
    198 
    199 	printf("MQ200_PMCSR=%08x\n", sc->sc_powerstate);
    200 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    201 			  MQ200_PMCSR, sc->sc_powerstate);
    202 #endif
    203 }
    204 
    205 static int
    206 mq200_hardpower(ctx, type, id, msg)
    207 	void *ctx;
    208 	int type;
    209 	long id;
    210 	void *msg;
    211 {
    212 	struct mq200_softc *sc = ctx;
    213 	int why = (int)msg;
    214 
    215 	switch (why) {
    216 	case PWR_SUSPEND:
    217 		sc->sc_powerstate = MQ200_POWERSTATE_D2;
    218 		break;
    219 	case PWR_STANDBY:
    220 		sc->sc_powerstate = MQ200_POWERSTATE_D3;
    221 		break;
    222 	case PWR_RESUME:
    223 		sc->sc_powerstate = MQ200_POWERSTATE_D0;
    224 		break;
    225 	}
    226 
    227 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    228 			  MQ200_PMCSR, sc->sc_powerstate);
    229 
    230 	/*
    231 	 * you should wait until the
    232 	 * power state transit sequence will end.
    233 	 */
    234 	{
    235 		unsigned long tmp;
    236 		do {
    237 			tmp = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    238 					       MQ200_PMCSR);
    239 		} while ((tmp & 0x3) != (sc->sc_powerstate & 0x3));
    240 		delay(100000); /* XXX */
    241 	}
    242 
    243 	return (0);
    244 }
    245 
    246 
    247 static int
    248 mq200_fbinit(fb)
    249 	struct hpcfb_fbconf *fb;
    250 {
    251 
    252 	/*
    253 	 * get fb settings from bootinfo
    254 	 */
    255 	if (bootinfo == NULL ||
    256 	    bootinfo->fb_addr == 0 ||
    257 	    bootinfo->fb_line_bytes == 0 ||
    258 	    bootinfo->fb_width == 0 ||
    259 	    bootinfo->fb_height == 0) {
    260 		printf("no frame buffer infomation.\n");
    261 		return (-1);
    262 	}
    263 
    264 	/* zero fill */
    265 	bzero(fb, sizeof(*fb));
    266 
    267 	fb->hf_conf_index	= 0;	/* configuration index		*/
    268 	fb->hf_nconfs		= 1;   	/* how many configurations	*/
    269 	strcpy(fb->hf_name, "built-in video");
    270 					/* frame buffer name		*/
    271 	strcpy(fb->hf_conf_name, "default");
    272 					/* configuration name		*/
    273 	fb->hf_height		= bootinfo->fb_height;
    274 	fb->hf_width		= bootinfo->fb_width;
    275 	fb->hf_baseaddr		= mips_ptob(mips_btop(bootinfo->fb_addr));
    276 	fb->hf_offset		= (u_long)bootinfo->fb_addr - fb->hf_baseaddr;
    277 					/* frame buffer start offset   	*/
    278 	fb->hf_bytes_per_line	= bootinfo->fb_line_bytes;
    279 	fb->hf_nplanes		= 1;
    280 	fb->hf_bytes_per_plane	= bootinfo->fb_height *
    281 					bootinfo->fb_line_bytes;
    282 
    283 	fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
    284 	fb->hf_access_flags |= HPCFB_ACCESS_WORD;
    285 	fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
    286 
    287 	switch (bootinfo->fb_type) {
    288 		/*
    289 		 * gray scale
    290 		 */
    291 	case BIFB_D2_M2L_3:
    292 	case BIFB_D2_M2L_3x2:
    293 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    294 		/* fall through */
    295 	case BIFB_D2_M2L_0:
    296 	case BIFB_D2_M2L_0x2:
    297 		fb->hf_class = HPCFB_CLASS_GRAYSCALE;
    298 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    299 		fb->hf_pack_width = 8;
    300 		fb->hf_pixels_per_pack = 4;
    301 		fb->hf_pixel_width = 2;
    302 		fb->hf_class_data_length = sizeof(struct hf_gray_tag);
    303 		fb->hf_u.hf_gray.hf_flags = 0;	/* reserved for future use */
    304 		break;
    305 
    306 	case BIFB_D4_M2L_F:
    307 	case BIFB_D4_M2L_Fx2:
    308 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    309 		/* fall through */
    310 	case BIFB_D4_M2L_0:
    311 	case BIFB_D4_M2L_0x2:
    312 		fb->hf_class = HPCFB_CLASS_GRAYSCALE;
    313 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    314 		fb->hf_pack_width = 8;
    315 		fb->hf_pixels_per_pack = 2;
    316 		fb->hf_pixel_width = 4;
    317 		fb->hf_class_data_length = sizeof(struct hf_gray_tag);
    318 		fb->hf_u.hf_gray.hf_flags = 0;	/* reserved for future use */
    319 		break;
    320 
    321 		/*
    322 		 * indexed color
    323 		 */
    324 	case BIFB_D8_FF:
    325 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    326 		/* fall through */
    327 	case BIFB_D8_00:
    328 		fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
    329 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    330 		fb->hf_pack_width = 8;
    331 		fb->hf_pixels_per_pack = 1;
    332 		fb->hf_pixel_width = 8;
    333 		fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
    334 		fb->hf_u.hf_indexed.hf_flags = 0; /* reserved for future use */
    335 		break;
    336 
    337 		/*
    338 		 * RGB color
    339 		 */
    340 	case BIFB_D16_FFFF:
    341 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    342 		/* fall through */
    343 	case BIFB_D16_0000:
    344 		fb->hf_class = HPCFB_CLASS_RGBCOLOR;
    345 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    346 #if BYTE_ORDER == LITTLE_ENDIAN
    347 		fb->hf_swap_flags = HPCFB_SWAP_BYTE;
    348 #endif
    349 		fb->hf_pack_width = 16;
    350 		fb->hf_pixels_per_pack = 1;
    351 		fb->hf_pixel_width = 16;
    352 
    353 		fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
    354 		fb->hf_u.hf_rgb.hf_flags = 0;	/* reserved for future use */
    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 	default:
    367 		printf("unknown type (=%d).\n", bootinfo->fb_type);
    368 		return (-1);
    369 		break;
    370 	}
    371 
    372 	return (0); /* no error */
    373 }
    374 
    375 int
    376 mq200_ioctl(v, cmd, data, flag, p)
    377 	void *v;
    378 	u_long cmd;
    379 	caddr_t data;
    380 	int flag;
    381 	struct proc *p;
    382 {
    383 	struct mq200_softc *sc = (struct mq200_softc *)v;
    384 	struct hpcfb_fbconf *fbconf;
    385 	struct hpcfb_dspconf *dspconf;
    386 	struct wsdisplay_cmap *cmap;
    387 	struct wsdisplay_param *dispparam;
    388 
    389 	switch (cmd) {
    390 	case WSDISPLAYIO_GETCMAP:
    391 		cmap = (struct wsdisplay_cmap*)data;
    392 
    393 		if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
    394 		    sc->sc_fbconf.hf_pack_width != 8 ||
    395 		    256 <= cmap->index ||
    396 		    256 < (cmap->index + cmap->count))
    397 			return (EINVAL);
    398 
    399 #if 0
    400 		if (!uvm_useracc(cmap->red, cmap->count, B_WRITE) ||
    401 		    !uvm_useracc(cmap->green, cmap->count, B_WRITE) ||
    402 		    !uvm_useracc(cmap->blue, cmap->count, B_WRITE))
    403 			return (EFAULT);
    404 
    405 		copyout(&bivideo_cmap_r[cmap->index], cmap->red, cmap->count);
    406 		copyout(&bivideo_cmap_g[cmap->index], cmap->green,cmap->count);
    407 		copyout(&bivideo_cmap_b[cmap->index], cmap->blue, cmap->count);
    408 #endif
    409 
    410 		return (0);
    411 
    412 	case WSDISPLAYIO_PUTCMAP:
    413 		/*
    414 		 * This driver can't set color map.
    415 		 */
    416 		return (EINVAL);
    417 
    418 	case WSDISPLAYIO_GETPARAM:
    419 		dispparam = (struct wsdisplay_param*)data;
    420 		switch (dispparam->param) {
    421 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    422 			VPRINTF(("mq200_ioctl: GETPARAM:BACKLIGHT\n"));
    423 			return (EINVAL);
    424 		case WSDISPLAYIO_PARAM_CONTRAST:
    425 			VPRINTF(("mq200_ioctl: GETPARAM:CONTRAST\n"));
    426 			return (EINVAL);
    427 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    428 			VPRINTF(("mq200_ioctl: GETPARAM:BRIGHTNESS\n"));
    429 			return (EINVAL);
    430 		default:
    431 			return (EINVAL);
    432 		}
    433 		return (0);
    434 
    435 	case WSDISPLAYIO_SETPARAM:
    436 		dispparam = (struct wsdisplay_param*)data;
    437 		switch (dispparam->param) {
    438 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    439 			VPRINTF(("mq200_ioctl: GETPARAM:BACKLIGHT\n"));
    440 			return (EINVAL);
    441 		case WSDISPLAYIO_PARAM_CONTRAST:
    442 			VPRINTF(("mq200_ioctl: GETPARAM:CONTRAST\n"));
    443 			return (EINVAL);
    444 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    445 			VPRINTF(("mq200_ioctl: GETPARAM:BRIGHTNESS\n"));
    446 			return (EINVAL);
    447 		default:
    448 			return (EINVAL);
    449 		}
    450 		return (0);
    451 
    452 	case HPCFBIO_GCONF:
    453 		fbconf = (struct hpcfb_fbconf *)data;
    454 		if (fbconf->hf_conf_index != 0 &&
    455 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
    456 			return (EINVAL);
    457 		}
    458 		*fbconf = sc->sc_fbconf;	/* structure assignment */
    459 		return (0);
    460 	case HPCFBIO_SCONF:
    461 		fbconf = (struct hpcfb_fbconf *)data;
    462 		if (fbconf->hf_conf_index != 0 &&
    463 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
    464 			return (EINVAL);
    465 		}
    466 		/*
    467 		 * nothing to do because we have only one configration
    468 		 */
    469 		return (0);
    470 	case HPCFBIO_GDSPCONF:
    471 		dspconf = (struct hpcfb_dspconf *)data;
    472 		if ((dspconf->hd_unit_index != 0 &&
    473 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    474 		    (dspconf->hd_conf_index != 0 &&
    475 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    476 			return (EINVAL);
    477 		}
    478 		*dspconf = sc->sc_dspconf;	/* structure assignment */
    479 		return (0);
    480 	case HPCFBIO_SDSPCONF:
    481 		dspconf = (struct hpcfb_dspconf *)data;
    482 		if ((dspconf->hd_unit_index != 0 &&
    483 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    484 		    (dspconf->hd_conf_index != 0 &&
    485 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    486 			return (EINVAL);
    487 		}
    488 		/*
    489 		 * nothing to do
    490 		 * because we have only one unit and one configration
    491 		 */
    492 		return (0);
    493 	case HPCFBIO_GOP:
    494 	case HPCFBIO_SOP:
    495 		/*
    496 		 * curently not implemented...
    497 		 */
    498 		return (EINVAL);
    499 	}
    500 
    501 	return (ENOTTY);
    502 }
    503 
    504 paddr_t
    505 mq200_mmap(ctx, offset, prot)
    506 	void *ctx;
    507 	off_t offset;
    508 	int prot;
    509 {
    510 	struct mq200_softc *sc = (struct mq200_softc *)ctx;
    511 
    512 	if (offset < 0 || MQ200_MAPSIZE <= offset)
    513 		return -1;
    514 
    515 	return mips_btop(sc->sc_baseaddr + offset);
    516 }
    517