Home | History | Annotate | Line # | Download | only in dev
mq200.c revision 1.2.2.5
      1 /*	$NetBSD: mq200.c,v 1.2.2.5 2001/02/11 19:10:29 bouyer 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 	regval = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MQ200_PC08R);
    119 	printf(": MQ200 Rev.%02lx video controller\n", regval & 0xff);
    120 
    121 	/* Add a power hook to power saving */
    122 	sc->sc_powerstate = MQ200_POWERSTATE_D0;
    123 	sc->sc_powerhook = powerhook_establish(mq200_power, sc);
    124 	if (sc->sc_powerhook == NULL)
    125 		printf("%s: WARNING: unable to establish power hook\n",
    126 			sc->sc_dev.dv_xname);
    127 
    128 	/* Add a hard power hook to power saving */
    129 	sc->sc_hardpowerhook = config_hook(CONFIG_HOOK_PMEVENT,
    130 					   CONFIG_HOOK_PMEVENT_HARDPOWER,
    131 					   CONFIG_HOOK_SHARE,
    132 					   mq200_hardpower, sc);
    133 	if (sc->sc_hardpowerhook == NULL)
    134 		printf("%s: WARNING: unable to establish hard power hook\n",
    135 			sc->sc_dev.dv_xname);
    136 
    137 	if (mq200_fbinit(&sc->sc_fbconf) != 0) {
    138 		/* just return so that hpcfb will not be attached */
    139 		return;
    140 	}
    141 
    142 	sc->sc_fbconf.hf_baseaddr = (u_long)bootinfo->fb_addr;
    143 	sc->sc_fbconf.hf_offset	= (u_long)sc->sc_fbconf.hf_baseaddr -
    144 	    MIPS_PHYS_TO_KSEG1(mips_ptob(mips_btop(sc->sc_baseaddr)));
    145 	DPRINTF(("hf_baseaddr=%lx\n", sc->sc_fbconf.hf_baseaddr));
    146 	DPRINTF(("hf_offset=%lx\n", sc->sc_fbconf.hf_offset));
    147 
    148 	if (console && hpcfb_cnattach(&sc->sc_fbconf) != 0) {
    149 		panic("mq200_attach: can't init fb console");
    150 	}
    151 
    152 	ha.ha_console = console;
    153 	ha.ha_accessops = &mq200_ha;
    154 	ha.ha_accessctx = sc;
    155 	ha.ha_curfbconf = 0;
    156 	ha.ha_nfbconf = 1;
    157 	ha.ha_fbconflist = &sc->sc_fbconf;
    158 	ha.ha_curdspconf = 0;
    159 	ha.ha_ndspconf = 1;
    160 	ha.ha_dspconflist = &sc->sc_dspconf;
    161 
    162 	config_found(&sc->sc_dev, &ha, hpcfbprint);
    163 
    164 #if NBIVIDEO > 0
    165 	/*
    166 	 * bivideo is no longer need
    167 	 */
    168 	bivideo_dont_attach = 1;
    169 #endif /* NBIVIDEO > 0 */
    170 }
    171 
    172 static void
    173 mq200_power(why, arg)
    174 	int why;
    175 	void *arg;
    176 {
    177 #if 0
    178 	struct mq200_softc *sc = arg;
    179 
    180 	switch (why) {
    181 	case PWR_SUSPEND:
    182 		sc->sc_powerstate = MQ200_POWERSTATE_D2;
    183 		break;
    184 	case PWR_STANDBY:
    185 		sc->sc_powerstate = MQ200_POWERSTATE_D3;
    186 		break;
    187 	case PWR_RESUME:
    188 		sc->sc_powerstate = MQ200_POWERSTATE_D0;
    189 		break;
    190 	}
    191 
    192 	printf("MQ200_PMCSR=%08x\n", sc->sc_powerstate);
    193 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    194 			  MQ200_PMCSR, sc->sc_powerstate);
    195 #endif
    196 }
    197 
    198 static int
    199 mq200_hardpower(ctx, type, id, msg)
    200 	void *ctx;
    201 	int type;
    202 	long id;
    203 	void *msg;
    204 {
    205 	struct mq200_softc *sc = ctx;
    206 	int why = (int)msg;
    207 
    208 	switch (why) {
    209 	case PWR_SUSPEND:
    210 		sc->sc_powerstate = MQ200_POWERSTATE_D2;
    211 		break;
    212 	case PWR_STANDBY:
    213 		sc->sc_powerstate = MQ200_POWERSTATE_D3;
    214 		break;
    215 	case PWR_RESUME:
    216 		sc->sc_powerstate = MQ200_POWERSTATE_D0;
    217 		break;
    218 	}
    219 
    220 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    221 			  MQ200_PMCSR, sc->sc_powerstate);
    222 
    223 	/*
    224 	 * you should wait until the
    225 	 * power state transit sequence will end.
    226 	 */
    227 	{
    228 		unsigned long tmp;
    229 		do {
    230 			tmp = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    231 					       MQ200_PMCSR);
    232 		} while ((tmp & 0x3) != (sc->sc_powerstate & 0x3));
    233 		delay(100000); /* XXX */
    234 	}
    235 
    236 	return (0);
    237 }
    238 
    239 
    240 static int
    241 mq200_fbinit(fb)
    242 	struct hpcfb_fbconf *fb;
    243 {
    244 
    245 	/*
    246 	 * get fb settings from bootinfo
    247 	 */
    248 	if (bootinfo == NULL ||
    249 	    bootinfo->fb_addr == 0 ||
    250 	    bootinfo->fb_line_bytes == 0 ||
    251 	    bootinfo->fb_width == 0 ||
    252 	    bootinfo->fb_height == 0) {
    253 		printf("no frame buffer infomation.\n");
    254 		return (-1);
    255 	}
    256 
    257 	/* zero fill */
    258 	bzero(fb, sizeof(*fb));
    259 
    260 	fb->hf_conf_index	= 0;	/* configuration index		*/
    261 	fb->hf_nconfs		= 1;   	/* how many configurations	*/
    262 	strcpy(fb->hf_name, "built-in video");
    263 					/* frame buffer name		*/
    264 	strcpy(fb->hf_conf_name, "default");
    265 					/* configuration name		*/
    266 	fb->hf_height		= bootinfo->fb_height;
    267 	fb->hf_width		= bootinfo->fb_width;
    268 	fb->hf_baseaddr		= mips_ptob(mips_btop(bootinfo->fb_addr));
    269 	fb->hf_offset		= (u_long)bootinfo->fb_addr - fb->hf_baseaddr;
    270 					/* frame buffer start offset   	*/
    271 	fb->hf_bytes_per_line	= bootinfo->fb_line_bytes;
    272 	fb->hf_nplanes		= 1;
    273 	fb->hf_bytes_per_plane	= bootinfo->fb_height *
    274 					bootinfo->fb_line_bytes;
    275 
    276 	fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
    277 	fb->hf_access_flags |= HPCFB_ACCESS_WORD;
    278 	fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
    279 
    280 	switch (bootinfo->fb_type) {
    281 		/*
    282 		 * gray scale
    283 		 */
    284 	case BIFB_D2_M2L_3:
    285 	case BIFB_D2_M2L_3x2:
    286 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    287 		/* fall through */
    288 	case BIFB_D2_M2L_0:
    289 	case BIFB_D2_M2L_0x2:
    290 		fb->hf_class = HPCFB_CLASS_GRAYSCALE;
    291 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    292 		fb->hf_pack_width = 8;
    293 		fb->hf_pixels_per_pack = 4;
    294 		fb->hf_pixel_width = 2;
    295 		fb->hf_class_data_length = sizeof(struct hf_gray_tag);
    296 		fb->hf_u.hf_gray.hf_flags = 0;	/* reserved for future use */
    297 		break;
    298 
    299 	case BIFB_D4_M2L_F:
    300 	case BIFB_D4_M2L_Fx2:
    301 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    302 		/* fall through */
    303 	case BIFB_D4_M2L_0:
    304 	case BIFB_D4_M2L_0x2:
    305 		fb->hf_class = HPCFB_CLASS_GRAYSCALE;
    306 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    307 		fb->hf_pack_width = 8;
    308 		fb->hf_pixels_per_pack = 2;
    309 		fb->hf_pixel_width = 4;
    310 		fb->hf_class_data_length = sizeof(struct hf_gray_tag);
    311 		fb->hf_u.hf_gray.hf_flags = 0;	/* reserved for future use */
    312 		break;
    313 
    314 		/*
    315 		 * indexed color
    316 		 */
    317 	case BIFB_D8_FF:
    318 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    319 		/* fall through */
    320 	case BIFB_D8_00:
    321 		fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
    322 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    323 		fb->hf_pack_width = 8;
    324 		fb->hf_pixels_per_pack = 1;
    325 		fb->hf_pixel_width = 8;
    326 		fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
    327 		fb->hf_u.hf_indexed.hf_flags = 0; /* reserved for future use */
    328 		break;
    329 
    330 		/*
    331 		 * RGB color
    332 		 */
    333 	case BIFB_D16_FFFF:
    334 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    335 		/* fall through */
    336 	case BIFB_D16_0000:
    337 		fb->hf_class = HPCFB_CLASS_RGBCOLOR;
    338 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    339 #if BYTE_ORDER == LITTLE_ENDIAN
    340 		fb->hf_swap_flags = HPCFB_SWAP_BYTE;
    341 #endif
    342 		fb->hf_pack_width = 16;
    343 		fb->hf_pixels_per_pack = 1;
    344 		fb->hf_pixel_width = 16;
    345 
    346 		fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
    347 		fb->hf_u.hf_rgb.hf_flags = 0;	/* reserved for future use */
    348 
    349 		fb->hf_u.hf_rgb.hf_red_width = 5;
    350 		fb->hf_u.hf_rgb.hf_red_shift = 11;
    351 		fb->hf_u.hf_rgb.hf_green_width = 6;
    352 		fb->hf_u.hf_rgb.hf_green_shift = 5;
    353 		fb->hf_u.hf_rgb.hf_blue_width = 5;
    354 		fb->hf_u.hf_rgb.hf_blue_shift = 0;
    355 		fb->hf_u.hf_rgb.hf_alpha_width = 0;
    356 		fb->hf_u.hf_rgb.hf_alpha_shift = 0;
    357 		break;
    358 
    359 	default:
    360 		printf("unknown type (=%d).\n", bootinfo->fb_type);
    361 		return (-1);
    362 		break;
    363 	}
    364 
    365 	return (0); /* no error */
    366 }
    367 
    368 int
    369 mq200_ioctl(v, cmd, data, flag, p)
    370 	void *v;
    371 	u_long cmd;
    372 	caddr_t data;
    373 	int flag;
    374 	struct proc *p;
    375 {
    376 	struct mq200_softc *sc = (struct mq200_softc *)v;
    377 	struct hpcfb_fbconf *fbconf;
    378 	struct hpcfb_dspconf *dspconf;
    379 	struct wsdisplay_cmap *cmap;
    380 	struct wsdisplay_param *dispparam;
    381 
    382 	switch (cmd) {
    383 	case WSDISPLAYIO_GETCMAP:
    384 		cmap = (struct wsdisplay_cmap*)data;
    385 
    386 		if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
    387 		    sc->sc_fbconf.hf_pack_width != 8 ||
    388 		    256 <= cmap->index ||
    389 		    256 < (cmap->index + cmap->count))
    390 			return (EINVAL);
    391 
    392 #if 0
    393 		if (!uvm_useracc(cmap->red, cmap->count, B_WRITE) ||
    394 		    !uvm_useracc(cmap->green, cmap->count, B_WRITE) ||
    395 		    !uvm_useracc(cmap->blue, cmap->count, B_WRITE))
    396 			return (EFAULT);
    397 
    398 		copyout(&bivideo_cmap_r[cmap->index], cmap->red, cmap->count);
    399 		copyout(&bivideo_cmap_g[cmap->index], cmap->green,cmap->count);
    400 		copyout(&bivideo_cmap_b[cmap->index], cmap->blue, cmap->count);
    401 #endif
    402 
    403 		return (0);
    404 
    405 	case WSDISPLAYIO_PUTCMAP:
    406 		/*
    407 		 * This driver can't set color map.
    408 		 */
    409 		return (EINVAL);
    410 
    411 	case WSDISPLAYIO_GETPARAM:
    412 		dispparam = (struct wsdisplay_param*)data;
    413 		switch (dispparam->param) {
    414 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    415 			VPRINTF(("mq200_ioctl: GETPARAM:BACKLIGHT\n"));
    416 			return (EINVAL);
    417 		case WSDISPLAYIO_PARAM_CONTRAST:
    418 			VPRINTF(("mq200_ioctl: GETPARAM:CONTRAST\n"));
    419 			return (EINVAL);
    420 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    421 			VPRINTF(("mq200_ioctl: GETPARAM:BRIGHTNESS\n"));
    422 			return (EINVAL);
    423 		default:
    424 			return (EINVAL);
    425 		}
    426 		return (0);
    427 
    428 	case WSDISPLAYIO_SETPARAM:
    429 		dispparam = (struct wsdisplay_param*)data;
    430 		switch (dispparam->param) {
    431 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    432 			VPRINTF(("mq200_ioctl: GETPARAM:BACKLIGHT\n"));
    433 			return (EINVAL);
    434 		case WSDISPLAYIO_PARAM_CONTRAST:
    435 			VPRINTF(("mq200_ioctl: GETPARAM:CONTRAST\n"));
    436 			return (EINVAL);
    437 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    438 			VPRINTF(("mq200_ioctl: GETPARAM:BRIGHTNESS\n"));
    439 			return (EINVAL);
    440 		default:
    441 			return (EINVAL);
    442 		}
    443 		return (0);
    444 
    445 	case HPCFBIO_GCONF:
    446 		fbconf = (struct hpcfb_fbconf *)data;
    447 		if (fbconf->hf_conf_index != 0 &&
    448 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
    449 			return (EINVAL);
    450 		}
    451 		*fbconf = sc->sc_fbconf;	/* structure assignment */
    452 		return (0);
    453 	case HPCFBIO_SCONF:
    454 		fbconf = (struct hpcfb_fbconf *)data;
    455 		if (fbconf->hf_conf_index != 0 &&
    456 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
    457 			return (EINVAL);
    458 		}
    459 		/*
    460 		 * nothing to do because we have only one configration
    461 		 */
    462 		return (0);
    463 	case HPCFBIO_GDSPCONF:
    464 		dspconf = (struct hpcfb_dspconf *)data;
    465 		if ((dspconf->hd_unit_index != 0 &&
    466 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    467 		    (dspconf->hd_conf_index != 0 &&
    468 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    469 			return (EINVAL);
    470 		}
    471 		*dspconf = sc->sc_dspconf;	/* structure assignment */
    472 		return (0);
    473 	case HPCFBIO_SDSPCONF:
    474 		dspconf = (struct hpcfb_dspconf *)data;
    475 		if ((dspconf->hd_unit_index != 0 &&
    476 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    477 		    (dspconf->hd_conf_index != 0 &&
    478 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    479 			return (EINVAL);
    480 		}
    481 		/*
    482 		 * nothing to do
    483 		 * because we have only one unit and one configration
    484 		 */
    485 		return (0);
    486 	case HPCFBIO_GOP:
    487 	case HPCFBIO_SOP:
    488 		/*
    489 		 * curently not implemented...
    490 		 */
    491 		return (EINVAL);
    492 	}
    493 
    494 	return (ENOTTY);
    495 }
    496 
    497 paddr_t
    498 mq200_mmap(ctx, offset, prot)
    499 	void *ctx;
    500 	off_t offset;
    501 	int prot;
    502 {
    503 	struct mq200_softc *sc = (struct mq200_softc *)ctx;
    504 
    505 	if (offset < 0 || MQ200_MAPSIZE <= offset)
    506 		return -1;
    507 
    508 	return mips_btop(sc->sc_baseaddr + offset);
    509 }
    510