xref: /titanic_51/usr/src/uts/intel/io/vgatext/vgatext.c (revision 945167b5f89d2827087e9de0dd3844260dc16242)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54ab75253Smrj  * Common Development and Distribution License (the "License").
64ab75253Smrj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
214ab75253Smrj 
227c478bd9Sstevel@tonic-gate /*
2393a18d6dSEnrico Perla - Sun Microsystems  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27f67ca41aSrugrat /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
28f67ca41aSrugrat /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
29f67ca41aSrugrat /*	  All Rights Reserved  	*/
30f67ca41aSrugrat 
317c478bd9Sstevel@tonic-gate #include <sys/errno.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/conf.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/visual_io.h>
367c478bd9Sstevel@tonic-gate #include <sys/font.h>
377c478bd9Sstevel@tonic-gate #include <sys/fbio.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/file.h>
437c478bd9Sstevel@tonic-gate #include <sys/open.h>
447c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
457c478bd9Sstevel@tonic-gate #include <sys/vgareg.h>
467c478bd9Sstevel@tonic-gate #include <sys/vgasubr.h>
477c478bd9Sstevel@tonic-gate #include <sys/pci.h>
487c478bd9Sstevel@tonic-gate #include <sys/kd.h>
497c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
507c478bd9Sstevel@tonic-gate #include <sys/sunldi.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	MYNAME	"vgatext"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
5560405de4Skz151634  * Each instance of this driver has 2 minor nodes:
5660405de4Skz151634  * 0: for common graphics operations
5760405de4Skz151634  * 1: for agpmaster operations
587c478bd9Sstevel@tonic-gate  */
5960405de4Skz151634 #define	GFX_MINOR		0
6060405de4Skz151634 #define	AGPMASTER_MINOR		1
61d6bb6a84Sms148562 
6260405de4Skz151634 #define	MY_NBITSMINOR		1
6360405de4Skz151634 #define	DEV2INST(dev)		(getminor(dev) >> MY_NBITSMINOR)
6460405de4Skz151634 #define	DEV2MINOR(dev)		(getminor(dev) & ((1 << MY_NBITSMINOR) - 1))
6560405de4Skz151634 #define	INST2NODE1(inst)	((inst) << MY_NBITSMINOR + GFX_MINOR)
6660405de4Skz151634 #define	INST2NODE2(inst)	(((inst) << MY_NBITSMINOR) + AGPMASTER_MINOR)
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* I don't know exactly where these should be defined, but this is a	*/
697c478bd9Sstevel@tonic-gate /* heck of a lot better than constants in the code.			*/
707c478bd9Sstevel@tonic-gate #define	TEXT_ROWS		25
717c478bd9Sstevel@tonic-gate #define	TEXT_COLS		80
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	VGA_BRIGHT_WHITE	0x0f
747c478bd9Sstevel@tonic-gate #define	VGA_BLACK		0x00
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	VGA_REG_ADDR		0x3c0
777c478bd9Sstevel@tonic-gate #define	VGA_REG_SIZE		0x20
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #define	VGA_MEM_ADDR		0xa0000
807c478bd9Sstevel@tonic-gate #define	VGA_MEM_SIZE		0x20000
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	VGA_MMAP_FB_BASE	VGA_MEM_ADDR
837c478bd9Sstevel@tonic-gate 
842df1fe9cSrandyf /*
852df1fe9cSrandyf  * This variable allows for this driver to suspend even if it
862df1fe9cSrandyf  * shouldn't.  Note that by setting it, the framebuffer will probably
872df1fe9cSrandyf  * not come back.  So use it with a serial console, or with serial
882df1fe9cSrandyf  * line debugging (say, for example, if this driver is being modified
892df1fe9cSrandyf  * to support _some_ hardware doing suspend and resume).
902df1fe9cSrandyf  */
912df1fe9cSrandyf int vgatext_force_suspend = 0;
922df1fe9cSrandyf 
937c478bd9Sstevel@tonic-gate static int vgatext_open(dev_t *, int, int, cred_t *);
947c478bd9Sstevel@tonic-gate static int vgatext_close(dev_t, int, int, cred_t *);
957c478bd9Sstevel@tonic-gate static int vgatext_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
967c478bd9Sstevel@tonic-gate static int vgatext_devmap(dev_t, devmap_cookie_t, offset_t, size_t,
977c478bd9Sstevel@tonic-gate 			    size_t *, uint_t);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static 	struct cb_ops cb_vgatext_ops = {
1007c478bd9Sstevel@tonic-gate 	vgatext_open,		/* cb_open */
1017c478bd9Sstevel@tonic-gate 	vgatext_close,		/* cb_close */
1027c478bd9Sstevel@tonic-gate 	nodev,			/* cb_strategy */
1037c478bd9Sstevel@tonic-gate 	nodev,			/* cb_print */
1047c478bd9Sstevel@tonic-gate 	nodev,			/* cb_dump */
1057c478bd9Sstevel@tonic-gate 	nodev,			/* cb_read */
1067c478bd9Sstevel@tonic-gate 	nodev,			/* cb_write */
1077c478bd9Sstevel@tonic-gate 	vgatext_ioctl,		/* cb_ioctl */
1087c478bd9Sstevel@tonic-gate 	vgatext_devmap,		/* cb_devmap */
1097c478bd9Sstevel@tonic-gate 	nodev,			/* cb_mmap */
1107c478bd9Sstevel@tonic-gate 	ddi_devmap_segmap,	/* cb_segmap */
1117c478bd9Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
1127c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
1137c478bd9Sstevel@tonic-gate 	0,			/* cb_stream */
1147c478bd9Sstevel@tonic-gate 	D_NEW | D_MTSAFE	/* cb_flag */
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static int vgatext_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1187c478bd9Sstevel@tonic-gate 		void **result);
1197c478bd9Sstevel@tonic-gate static int vgatext_attach(dev_info_t *, ddi_attach_cmd_t);
1207c478bd9Sstevel@tonic-gate static int vgatext_detach(dev_info_t *, ddi_detach_cmd_t);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static struct vis_identifier text_ident = { "SUNWtext" };
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static struct dev_ops vgatext_ops = {
1257c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1267c478bd9Sstevel@tonic-gate 	0,			/* devo_refcnt */
1277c478bd9Sstevel@tonic-gate 	vgatext_info,		/* devo_getinfo */
1287c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_identify */
1297c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_probe */
1307c478bd9Sstevel@tonic-gate 	vgatext_attach,		/* devo_attach */
1317c478bd9Sstevel@tonic-gate 	vgatext_detach,		/* devo_detach */
1327c478bd9Sstevel@tonic-gate 	nodev,			/* devo_reset */
1337c478bd9Sstevel@tonic-gate 	&cb_vgatext_ops,	/* devo_cb_ops */
1347c478bd9Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* devo_bus_ops */
13519397407SSherry Moore 	NULL,			/* power */
13619397407SSherry Moore 	ddi_quiesce_not_needed,	/* quiesce */
1377c478bd9Sstevel@tonic-gate };
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate struct vgatext_softc {
1407c478bd9Sstevel@tonic-gate 	struct vgaregmap 	regs;
1417c478bd9Sstevel@tonic-gate 	struct vgaregmap 	fb;
1427c478bd9Sstevel@tonic-gate 	off_t			fb_size;
1437c478bd9Sstevel@tonic-gate 	int			fb_regno;
1447c478bd9Sstevel@tonic-gate 	dev_info_t		*devi;
1457c478bd9Sstevel@tonic-gate 	int			mode;	/* KD_TEXT or KD_GRAPHICS */
1467c478bd9Sstevel@tonic-gate 	caddr_t			text_base;	/* hardware text base */
1477c478bd9Sstevel@tonic-gate 	char			shadow[TEXT_ROWS*TEXT_COLS*2];
1487c478bd9Sstevel@tonic-gate 	caddr_t			current_base;	/* hardware or shadow */
1497c478bd9Sstevel@tonic-gate 	struct {
1507c478bd9Sstevel@tonic-gate 		boolean_t visible;
1517c478bd9Sstevel@tonic-gate 		int row;
1527c478bd9Sstevel@tonic-gate 		int col;
1537c478bd9Sstevel@tonic-gate 	}			cursor;
1547c478bd9Sstevel@tonic-gate 	struct vis_polledio	polledio;
1557c478bd9Sstevel@tonic-gate 	struct {
1567c478bd9Sstevel@tonic-gate 		unsigned char red;
1577c478bd9Sstevel@tonic-gate 		unsigned char green;
1587c478bd9Sstevel@tonic-gate 		unsigned char blue;
1597c478bd9Sstevel@tonic-gate 	}			colormap[VGA8_CMAP_ENTRIES];
1607c478bd9Sstevel@tonic-gate 	unsigned char attrib_palette[VGA_ATR_NUM_PLT];
1614e93fb0fSrugrat 	unsigned int flags;
16248633f18SJan Setje-Eilers 	kmutex_t lock;
1637c478bd9Sstevel@tonic-gate };
1647c478bd9Sstevel@tonic-gate 
1654e93fb0fSrugrat #define	VGATEXT_FLAG_CONSOLE 0x00000001
1664e93fb0fSrugrat #define	VGATEXT_IS_CONSOLE(softc) ((softc)->flags & VGATEXT_FLAG_CONSOLE)
1674e93fb0fSrugrat 
1687c478bd9Sstevel@tonic-gate static int vgatext_devinit(struct vgatext_softc *, struct vis_devinit *data);
1697c478bd9Sstevel@tonic-gate static void	vgatext_cons_copy(struct vgatext_softc *,
1707c478bd9Sstevel@tonic-gate 			struct vis_conscopy *);
1717c478bd9Sstevel@tonic-gate static void	vgatext_cons_display(struct vgatext_softc *,
1727c478bd9Sstevel@tonic-gate 			struct vis_consdisplay *);
1737c478bd9Sstevel@tonic-gate static void	vgatext_cons_cursor(struct vgatext_softc *,
1747c478bd9Sstevel@tonic-gate 			struct vis_conscursor *);
1757c478bd9Sstevel@tonic-gate static void	vgatext_polled_copy(struct vis_polledio_arg *,
1767c478bd9Sstevel@tonic-gate 			struct vis_conscopy *);
1777c478bd9Sstevel@tonic-gate static void	vgatext_polled_display(struct vis_polledio_arg *,
1787c478bd9Sstevel@tonic-gate 			struct vis_consdisplay *);
1797c478bd9Sstevel@tonic-gate static void	vgatext_polled_cursor(struct vis_polledio_arg *,
1807c478bd9Sstevel@tonic-gate 			struct vis_conscursor *);
1817c478bd9Sstevel@tonic-gate static void	vgatext_init(struct vgatext_softc *);
1827c478bd9Sstevel@tonic-gate static void	vgatext_set_text(struct vgatext_softc *);
1837c478bd9Sstevel@tonic-gate #if	defined(USE_BORDERS)
1847c478bd9Sstevel@tonic-gate static void	vgatext_init_graphics(struct vgatext_softc *);
1857c478bd9Sstevel@tonic-gate #endif
1867c478bd9Sstevel@tonic-gate static int vgatext_kdsetmode(struct vgatext_softc *softc, int mode);
1877c478bd9Sstevel@tonic-gate static void vgatext_setfont(struct vgatext_softc *softc);
1887c478bd9Sstevel@tonic-gate static void vgatext_get_cursor(struct vgatext_softc *softc,
1897c478bd9Sstevel@tonic-gate 		screen_pos_t *row, screen_pos_t *col);
1907c478bd9Sstevel@tonic-gate static void vgatext_set_cursor(struct vgatext_softc *softc, int row, int col);
1917c478bd9Sstevel@tonic-gate static void vgatext_hide_cursor(struct vgatext_softc *softc);
1927c478bd9Sstevel@tonic-gate static void vgatext_save_colormap(struct vgatext_softc *softc);
1937c478bd9Sstevel@tonic-gate static void vgatext_restore_colormap(struct vgatext_softc *softc);
1947c478bd9Sstevel@tonic-gate static int vgatext_get_pci_reg_index(dev_info_t *const devi,
1957c478bd9Sstevel@tonic-gate 		unsigned long himask, unsigned long hival, unsigned long addr,
1967c478bd9Sstevel@tonic-gate 		off_t *offset);
1977c478bd9Sstevel@tonic-gate static int vgatext_get_isa_reg_index(dev_info_t *const devi,
1987c478bd9Sstevel@tonic-gate 		unsigned long hival, unsigned long addr, off_t *offset);
1997c478bd9Sstevel@tonic-gate static void	*vgatext_softc_head;
2007c478bd9Sstevel@tonic-gate static char	vgatext_silent;
2017c478bd9Sstevel@tonic-gate static char	happyface_boot;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /* Loadable Driver stuff */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
2067c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* Type of module.  This one is a driver */
207613b2871SRichard Bean 	"VGA text driver",	/* Name of the module. */
2087c478bd9Sstevel@tonic-gate 	&vgatext_ops,		/* driver ops */
2097c478bd9Sstevel@tonic-gate };
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2127c478bd9Sstevel@tonic-gate 	MODREV_1, (void *) &modldrv, NULL
2137c478bd9Sstevel@tonic-gate };
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate typedef enum pc_colors {
2167c478bd9Sstevel@tonic-gate 	pc_black	= 0,
2177c478bd9Sstevel@tonic-gate 	pc_blue		= 1,
2187c478bd9Sstevel@tonic-gate 	pc_green	= 2,
2197c478bd9Sstevel@tonic-gate 	pc_cyan		= 3,
2207c478bd9Sstevel@tonic-gate 	pc_red		= 4,
2217c478bd9Sstevel@tonic-gate 	pc_magenta	= 5,
2227c478bd9Sstevel@tonic-gate 	pc_brown	= 6,
2237c478bd9Sstevel@tonic-gate 	pc_white	= 7,
2247c478bd9Sstevel@tonic-gate 	pc_grey		= 8,
2257c478bd9Sstevel@tonic-gate 	pc_brt_blue	= 9,
2267c478bd9Sstevel@tonic-gate 	pc_brt_green	= 10,
2277c478bd9Sstevel@tonic-gate 	pc_brt_cyan	= 11,
2287c478bd9Sstevel@tonic-gate 	pc_brt_red	= 12,
2297c478bd9Sstevel@tonic-gate 	pc_brt_magenta	= 13,
2307c478bd9Sstevel@tonic-gate 	pc_yellow	= 14,
2317c478bd9Sstevel@tonic-gate 	pc_brt_white	= 15
2327c478bd9Sstevel@tonic-gate } pc_colors_t;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static const unsigned char solaris_color_to_pc_color[16] = {
2357c478bd9Sstevel@tonic-gate 	pc_brt_white,		/*  0 - brt_white	*/
2367c478bd9Sstevel@tonic-gate 	pc_black,		/*  1 - black		*/
2377c478bd9Sstevel@tonic-gate 	pc_blue,		/*  2 - blue		*/
2387c478bd9Sstevel@tonic-gate 	pc_green,		/*  3 - green		*/
2397c478bd9Sstevel@tonic-gate 	pc_cyan,		/*  4 - cyan		*/
2407c478bd9Sstevel@tonic-gate 	pc_red,			/*  5 - red		*/
2417c478bd9Sstevel@tonic-gate 	pc_magenta,		/*  6 - magenta		*/
2427c478bd9Sstevel@tonic-gate 	pc_brown,		/*  7 - brown		*/
2437c478bd9Sstevel@tonic-gate 	pc_white,		/*  8 - white		*/
2447c478bd9Sstevel@tonic-gate 	pc_grey,		/*  9 - gery		*/
2457c478bd9Sstevel@tonic-gate 	pc_brt_blue,		/* 10 - brt_blue	*/
2467c478bd9Sstevel@tonic-gate 	pc_brt_green,		/* 11 - brt_green	*/
2477c478bd9Sstevel@tonic-gate 	pc_brt_cyan,		/* 12 - brt_cyan	*/
2487c478bd9Sstevel@tonic-gate 	pc_brt_red,		/* 13 - brt_red		*/
2497c478bd9Sstevel@tonic-gate 	pc_brt_magenta,		/* 14 - brt_magenta	*/
2507c478bd9Sstevel@tonic-gate 	pc_yellow		/* 15 - yellow		*/
2517c478bd9Sstevel@tonic-gate };
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate static ddi_device_acc_attr_t i8xx_dev_access = {
2547c478bd9Sstevel@tonic-gate 	DDI_DEVICE_ATTR_V0,
2557c478bd9Sstevel@tonic-gate 	DDI_NEVERSWAP_ACC,
2567c478bd9Sstevel@tonic-gate 	DDI_STRICTORDER_ACC
2577c478bd9Sstevel@tonic-gate };
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate static ddi_device_acc_attr_t dev_attr = {
2607c478bd9Sstevel@tonic-gate 	DDI_DEVICE_ATTR_V0,
2617c478bd9Sstevel@tonic-gate 	DDI_NEVERSWAP_ACC,
2627c478bd9Sstevel@tonic-gate 	DDI_STRICTORDER_ACC,
2637c478bd9Sstevel@tonic-gate };
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate int
2667c478bd9Sstevel@tonic-gate _init(void)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	int e;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if ((e = ddi_soft_state_init(&vgatext_softc_head,
2717c478bd9Sstevel@tonic-gate 		    sizeof (struct vgatext_softc), 1)) != 0) {
2727c478bd9Sstevel@tonic-gate 	    return (e);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	e = mod_install(&modlinkage);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (e) {
2787c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&vgatext_softc_head);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	return (e);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate int
2847c478bd9Sstevel@tonic-gate _fini(void)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	int e;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0)
2897c478bd9Sstevel@tonic-gate 		return (e);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&vgatext_softc_head);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	return (0);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate int
2977c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /* default structure for FBIOGATTR ioctl */
3037c478bd9Sstevel@tonic-gate static struct fbgattr vgatext_attr =  {
3047c478bd9Sstevel@tonic-gate /*	real_type	owner */
3057c478bd9Sstevel@tonic-gate 	FBTYPE_SUNFAST_COLOR, 0,
3067c478bd9Sstevel@tonic-gate /* fbtype: type		h  w  depth cms  size */
3077c478bd9Sstevel@tonic-gate 	{ FBTYPE_SUNFAST_COLOR, TEXT_ROWS, TEXT_COLS, 1,    256,  0 },
3087c478bd9Sstevel@tonic-gate /* fbsattr: flags emu_type	dev_specific */
3097c478bd9Sstevel@tonic-gate 	{ 0, FBTYPE_SUN4COLOR, { 0 } },
3107c478bd9Sstevel@tonic-gate /*	emu_types */
3117c478bd9Sstevel@tonic-gate 	{ -1 }
3127c478bd9Sstevel@tonic-gate };
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate  * handy macros
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate #define	getsoftc(instance) ((struct vgatext_softc *)	\
3197c478bd9Sstevel@tonic-gate 			ddi_get_soft_state(vgatext_softc_head, (instance)))
3207c478bd9Sstevel@tonic-gate 
3214e93fb0fSrugrat #define	STREQ(a, b)	(strcmp((a), (b)) == 0)
3224e93fb0fSrugrat 
32393a18d6dSEnrico Perla - Sun Microsystems /*
32493a18d6dSEnrico Perla - Sun Microsystems  * NOTE: this function is duplicated here and in gfx_private/vgatext while
32593a18d6dSEnrico Perla - Sun Microsystems  *       we work on a set of commitable interfaces to sunpci.c.
32693a18d6dSEnrico Perla - Sun Microsystems  *
32793a18d6dSEnrico Perla - Sun Microsystems  * Use the class code to determine if the device is a PCI-to-PCI bridge.
32893a18d6dSEnrico Perla - Sun Microsystems  * Returns:  B_TRUE  if the device is a bridge.
32993a18d6dSEnrico Perla - Sun Microsystems  *           B_FALSE if the device is not a bridge or the property cannot be
33093a18d6dSEnrico Perla - Sun Microsystems  *		     retrieved.
33193a18d6dSEnrico Perla - Sun Microsystems  */
33293a18d6dSEnrico Perla - Sun Microsystems static boolean_t
33393a18d6dSEnrico Perla - Sun Microsystems is_pci_bridge(dev_info_t *dip)
33493a18d6dSEnrico Perla - Sun Microsystems {
33593a18d6dSEnrico Perla - Sun Microsystems 	uint32_t class_code;
33693a18d6dSEnrico Perla - Sun Microsystems 
33793a18d6dSEnrico Perla - Sun Microsystems 	class_code = (uint32_t)ddi_prop_get_int(DDI_DEV_T_ANY, dip,
33893a18d6dSEnrico Perla - Sun Microsystems 	    DDI_PROP_DONTPASS, "class-code", 0xffffffff);
33993a18d6dSEnrico Perla - Sun Microsystems 
34093a18d6dSEnrico Perla - Sun Microsystems 	if (class_code == 0xffffffff || class_code == DDI_PROP_NOT_FOUND)
34193a18d6dSEnrico Perla - Sun Microsystems 		return (B_FALSE);
34293a18d6dSEnrico Perla - Sun Microsystems 
34393a18d6dSEnrico Perla - Sun Microsystems 	class_code &= 0x00ffff00;
34493a18d6dSEnrico Perla - Sun Microsystems 	if (class_code == ((PCI_CLASS_BRIDGE << 16) | (PCI_BRIDGE_PCI << 8)))
34593a18d6dSEnrico Perla - Sun Microsystems 		return (B_TRUE);
34693a18d6dSEnrico Perla - Sun Microsystems 
34793a18d6dSEnrico Perla - Sun Microsystems 	return (B_FALSE);
34893a18d6dSEnrico Perla - Sun Microsystems }
34993a18d6dSEnrico Perla - Sun Microsystems 
3504e93fb0fSrugrat static void
3514e93fb0fSrugrat vgatext_check_for_console(dev_info_t *devi, struct vgatext_softc *softc,
3524e93fb0fSrugrat 	int pci_pcie_bus)
3534e93fb0fSrugrat {
3544e93fb0fSrugrat 	ddi_acc_handle_t pci_conf;
3554e93fb0fSrugrat 	dev_info_t *pdevi;
3564e93fb0fSrugrat 	uint16_t data16;
3574e93fb0fSrugrat 
3584e93fb0fSrugrat 	/*
3594e93fb0fSrugrat 	 * Based on Section 11.3, "PCI Display Subsystem Initialization",
3604e93fb0fSrugrat 	 * of the 1.1 PCI-to-PCI Bridge Architecture Specification
3614e93fb0fSrugrat 	 * determine if this is the boot console device.  First, see
3624e93fb0fSrugrat 	 * if the SBIOS has turned on PCI I/O for this device.  Then if
3634e93fb0fSrugrat 	 * this is PCI/PCI-E, verify the parent bridge has VGAEnable set.
3644e93fb0fSrugrat 	 */
3654e93fb0fSrugrat 
3664e93fb0fSrugrat 	if (pci_config_setup(devi, &pci_conf) != DDI_SUCCESS) {
3674e93fb0fSrugrat 		cmn_err(CE_WARN,
3684e93fb0fSrugrat 		    MYNAME ": can't get PCI conf handle");
3694e93fb0fSrugrat 		return;
3704e93fb0fSrugrat 	}
3714e93fb0fSrugrat 
3724e93fb0fSrugrat 	data16 = pci_config_get16(pci_conf, PCI_CONF_COMM);
3734e93fb0fSrugrat 	if (data16 & PCI_COMM_IO)
3744e93fb0fSrugrat 		softc->flags |= VGATEXT_FLAG_CONSOLE;
3754e93fb0fSrugrat 
3764e93fb0fSrugrat 	pci_config_teardown(&pci_conf);
3774e93fb0fSrugrat 
3784e93fb0fSrugrat 	/* If IO not enabled or ISA/EISA, just return */
3794e93fb0fSrugrat 	if (!(softc->flags & VGATEXT_FLAG_CONSOLE) || !pci_pcie_bus)
3804e93fb0fSrugrat 		return;
3814e93fb0fSrugrat 
3824e93fb0fSrugrat 	/*
3834e93fb0fSrugrat 	 * Check for VGA Enable in the Bridge Control register for all
3844e93fb0fSrugrat 	 * PCI/PCIEX parents.  If not set all the way up the chain,
3854e93fb0fSrugrat 	 * this cannot be the boot console.
3864e93fb0fSrugrat 	 */
3874e93fb0fSrugrat 
38893a18d6dSEnrico Perla - Sun Microsystems 	pdevi = devi;
38993a18d6dSEnrico Perla - Sun Microsystems 	while (pdevi = ddi_get_parent(pdevi)) {
3904e93fb0fSrugrat 		int	error;
3914e93fb0fSrugrat 		ddi_acc_handle_t ppci_conf;
3924e93fb0fSrugrat 		char	*parent_type = NULL;
3934e93fb0fSrugrat 
3944e93fb0fSrugrat 		error = ddi_prop_lookup_string(DDI_DEV_T_ANY, pdevi,
3954e93fb0fSrugrat 		    DDI_PROP_DONTPASS, "device_type", &parent_type);
3964e93fb0fSrugrat 		if (error != DDI_SUCCESS) {
3974e93fb0fSrugrat 			return;
3984e93fb0fSrugrat 		}
3994e93fb0fSrugrat 
4004e93fb0fSrugrat 		/* Verify still on the PCI/PCIEX parent tree */
4014e93fb0fSrugrat 		if (!STREQ(parent_type, "pci") &&
4024e93fb0fSrugrat 		    !STREQ(parent_type, "pciex")) {
4034e93fb0fSrugrat 			ddi_prop_free(parent_type);
4044e93fb0fSrugrat 			return;
4054e93fb0fSrugrat 		}
4064e93fb0fSrugrat 
4074e93fb0fSrugrat 		ddi_prop_free(parent_type);
4084e93fb0fSrugrat 		parent_type = NULL;
4094e93fb0fSrugrat 
41093a18d6dSEnrico Perla - Sun Microsystems 		/* VGAEnable is set only for PCI-to-PCI bridges. */
41193a18d6dSEnrico Perla - Sun Microsystems 		if (is_pci_bridge(pdevi) == B_FALSE)
41293a18d6dSEnrico Perla - Sun Microsystems 			continue;
41393a18d6dSEnrico Perla - Sun Microsystems 
41493a18d6dSEnrico Perla - Sun Microsystems 		if (pci_config_setup(pdevi, &ppci_conf) != DDI_SUCCESS)
41593a18d6dSEnrico Perla - Sun Microsystems 			continue;
4164e93fb0fSrugrat 
4174e93fb0fSrugrat 		data16 = pci_config_get16(ppci_conf, PCI_BCNF_BCNTRL);
4184e93fb0fSrugrat 		pci_config_teardown(&ppci_conf);
4194e93fb0fSrugrat 
4204e93fb0fSrugrat 		if (!(data16 & PCI_BCNF_BCNTRL_VGA_ENABLE)) {
4214e93fb0fSrugrat 			softc->flags &= ~VGATEXT_FLAG_CONSOLE;
4224e93fb0fSrugrat 			return;
4234e93fb0fSrugrat 		}
4244e93fb0fSrugrat 	}
4254e93fb0fSrugrat }
4264e93fb0fSrugrat 
4277c478bd9Sstevel@tonic-gate static int
4287c478bd9Sstevel@tonic-gate vgatext_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	struct vgatext_softc *softc;
4317c478bd9Sstevel@tonic-gate 	int	unit = ddi_get_instance(devi);
4327c478bd9Sstevel@tonic-gate 	int	error;
4337c478bd9Sstevel@tonic-gate 	char	*parent_type = NULL;
4347c478bd9Sstevel@tonic-gate 	int	reg_rnumber;
4357c478bd9Sstevel@tonic-gate 	off_t	reg_offset;
4367c478bd9Sstevel@tonic-gate 	off_t	mem_offset;
4377c478bd9Sstevel@tonic-gate 	char	buf[80], *cons;
4384e93fb0fSrugrat 	int pci_pcie_bus = 0;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	switch (cmd) {
4427c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
4437c478bd9Sstevel@tonic-gate 		break;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
4462df1fe9cSrandyf 		/*
4472df1fe9cSrandyf 		 * Though vgatext doesn't really know how to resume
4482df1fe9cSrandyf 		 * on a generic framebuffer, we should succeed, as
4492df1fe9cSrandyf 		 * it is far better to have no console, than potentiall
4502df1fe9cSrandyf 		 * have no machine.
4512df1fe9cSrandyf 		 */
4527c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4537c478bd9Sstevel@tonic-gate 	default:
4547c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/* DDI_ATTACH */
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/* Allocate softc struct */
4607c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(vgatext_softc_head, unit) != DDI_SUCCESS) {
4617c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 	softc = getsoftc(unit);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/* link it in */
4667c478bd9Sstevel@tonic-gate 	softc->devi = devi;
4677c478bd9Sstevel@tonic-gate 	ddi_set_driver_private(devi, softc);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	softc->polledio.arg = (struct vis_polledio_arg *)softc;
4707c478bd9Sstevel@tonic-gate 	softc->polledio.display = vgatext_polled_display;
4717c478bd9Sstevel@tonic-gate 	softc->polledio.copy = vgatext_polled_copy;
4727c478bd9Sstevel@tonic-gate 	softc->polledio.cursor = vgatext_polled_cursor;
4737c478bd9Sstevel@tonic-gate 
47448633f18SJan Setje-Eilers 	mutex_init(&(softc->lock), NULL, MUTEX_DRIVER, NULL);
47548633f18SJan Setje-Eilers 
4767c478bd9Sstevel@tonic-gate 	error = ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_get_parent(devi),
4777c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "device_type", &parent_type);
4787c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
4797c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, MYNAME ": can't determine parent type.");
4807c478bd9Sstevel@tonic-gate 		goto fail;
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (STREQ(parent_type, "isa") || STREQ(parent_type, "eisa")) {
4847c478bd9Sstevel@tonic-gate 		reg_rnumber = vgatext_get_isa_reg_index(devi, 1, VGA_REG_ADDR,
4857c478bd9Sstevel@tonic-gate 		    &reg_offset);
4867c478bd9Sstevel@tonic-gate 		if (reg_rnumber < 0) {
4877c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
4887c478bd9Sstevel@tonic-gate 			    MYNAME ": can't find reg entry for registers");
4897ae111d4Sms148562 			error = DDI_FAILURE;
4907c478bd9Sstevel@tonic-gate 			goto fail;
4917c478bd9Sstevel@tonic-gate 		}
4927c478bd9Sstevel@tonic-gate 		softc->fb_regno = vgatext_get_isa_reg_index(devi, 0,
4937c478bd9Sstevel@tonic-gate 		    VGA_MEM_ADDR, &mem_offset);
4947c478bd9Sstevel@tonic-gate 		if (softc->fb_regno < 0) {
4957c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
4967c478bd9Sstevel@tonic-gate 			    MYNAME ": can't find reg entry for memory");
4977ae111d4Sms148562 			error = DDI_FAILURE;
4987c478bd9Sstevel@tonic-gate 			goto fail;
4997c478bd9Sstevel@tonic-gate 		}
50070025d76Sjohnny 	} else if (STREQ(parent_type, "pci") || STREQ(parent_type, "pciex")) {
5014e93fb0fSrugrat 		pci_pcie_bus = 1;
5027c478bd9Sstevel@tonic-gate 		reg_rnumber = vgatext_get_pci_reg_index(devi,
5037c478bd9Sstevel@tonic-gate 		    PCI_REG_ADDR_M|PCI_REG_REL_M,
5047c478bd9Sstevel@tonic-gate 		    PCI_ADDR_IO|PCI_RELOCAT_B, VGA_REG_ADDR,
5057c478bd9Sstevel@tonic-gate 		    &reg_offset);
5067c478bd9Sstevel@tonic-gate 		if (reg_rnumber < 0) {
5077c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
5087c478bd9Sstevel@tonic-gate 			    MYNAME ": can't find reg entry for registers");
5097ae111d4Sms148562 			error = DDI_FAILURE;
5107c478bd9Sstevel@tonic-gate 			goto fail;
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 		softc->fb_regno = vgatext_get_pci_reg_index(devi,
5137c478bd9Sstevel@tonic-gate 		    PCI_REG_ADDR_M|PCI_REG_REL_M,
5147c478bd9Sstevel@tonic-gate 		    PCI_ADDR_MEM32|PCI_RELOCAT_B, VGA_MEM_ADDR,
5157c478bd9Sstevel@tonic-gate 		    &mem_offset);
5167c478bd9Sstevel@tonic-gate 		if (softc->fb_regno < 0) {
5177c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
5187c478bd9Sstevel@tonic-gate 			    MYNAME ": can't find reg entry for memory");
5197ae111d4Sms148562 			error = DDI_FAILURE;
5207c478bd9Sstevel@tonic-gate 			goto fail;
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 	} else {
5237c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, MYNAME ": unknown parent type \"%s\".",
5247c478bd9Sstevel@tonic-gate 		    parent_type);
5257ae111d4Sms148562 		error = DDI_FAILURE;
5267c478bd9Sstevel@tonic-gate 		goto fail;
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 	ddi_prop_free(parent_type);
5297c478bd9Sstevel@tonic-gate 	parent_type = NULL;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	error = ddi_regs_map_setup(devi, reg_rnumber,
5327c478bd9Sstevel@tonic-gate 	    (caddr_t *)&softc->regs.addr, reg_offset, VGA_REG_SIZE,
5337c478bd9Sstevel@tonic-gate 	    &dev_attr, &softc->regs.handle);
5347c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
5357c478bd9Sstevel@tonic-gate 		goto fail;
5367c478bd9Sstevel@tonic-gate 	softc->regs.mapped = B_TRUE;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	softc->fb_size = VGA_MEM_SIZE;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	error = ddi_regs_map_setup(devi, softc->fb_regno,
5417c478bd9Sstevel@tonic-gate 	    (caddr_t *)&softc->fb.addr,
5427c478bd9Sstevel@tonic-gate 	    mem_offset, softc->fb_size,
5437c478bd9Sstevel@tonic-gate 	    &dev_attr, &softc->fb.handle);
5447c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
5457c478bd9Sstevel@tonic-gate 		goto fail;
5467c478bd9Sstevel@tonic-gate 	softc->fb.mapped = B_TRUE;
5477c478bd9Sstevel@tonic-gate 
5484ab75253Smrj 	if (ddi_get8(softc->regs.handle,
5497c478bd9Sstevel@tonic-gate 	    softc->regs.addr + VGA_MISC_R) & VGA_MISC_IOA_SEL)
5507c478bd9Sstevel@tonic-gate 		softc->text_base = (caddr_t)softc->fb.addr + VGA_COLOR_BASE;
5517c478bd9Sstevel@tonic-gate 	else
5527c478bd9Sstevel@tonic-gate 		softc->text_base = (caddr_t)softc->fb.addr + VGA_MONO_BASE;
55348633f18SJan Setje-Eilers 
55448633f18SJan Setje-Eilers 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
55548633f18SJan Setje-Eilers 	    DDI_PROP_DONTPASS, "console", &cons) == DDI_SUCCESS) {
55648633f18SJan Setje-Eilers 		if (strcmp(cons, "graphics") == 0) {
55748633f18SJan Setje-Eilers 			happyface_boot = 1;
55848633f18SJan Setje-Eilers 			vgatext_silent = 1;
55948633f18SJan Setje-Eilers 			softc->current_base = softc->shadow;
56048633f18SJan Setje-Eilers 		} else {
5617c478bd9Sstevel@tonic-gate 			softc->current_base = softc->text_base;
56248633f18SJan Setje-Eilers 		}
56348633f18SJan Setje-Eilers 		ddi_prop_free(cons);
56448633f18SJan Setje-Eilers 	} else {
56548633f18SJan Setje-Eilers 		softc->current_base = softc->text_base;
56648633f18SJan Setje-Eilers 	}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "text-%d", unit);
5697c478bd9Sstevel@tonic-gate 	error = ddi_create_minor_node(devi, buf, S_IFCHR,
5707c478bd9Sstevel@tonic-gate 	    INST2NODE1(unit), DDI_NT_DISPLAY, NULL);
5717c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
5727c478bd9Sstevel@tonic-gate 		goto fail;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	error = ddi_prop_create(makedevice(DDI_MAJOR_T_UNKNOWN, unit),
5757c478bd9Sstevel@tonic-gate 	    devi, DDI_PROP_CANSLEEP, DDI_KERNEL_IOCTL, NULL, 0);
5767c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
5777c478bd9Sstevel@tonic-gate 		goto fail;
5787c478bd9Sstevel@tonic-gate 
5794e93fb0fSrugrat 	vgatext_check_for_console(devi, softc, pci_pcie_bus);
5804e93fb0fSrugrat 
5817c478bd9Sstevel@tonic-gate 	/* only do this if not in graphics mode */
5824e93fb0fSrugrat 	if ((vgatext_silent == 0) && (VGATEXT_IS_CONSOLE(softc))) {
5837c478bd9Sstevel@tonic-gate 		vgatext_init(softc);
5847c478bd9Sstevel@tonic-gate 		vgatext_save_colormap(softc);
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate fail:
5907c478bd9Sstevel@tonic-gate 	if (parent_type != NULL)
5917c478bd9Sstevel@tonic-gate 		ddi_prop_free(parent_type);
5927c478bd9Sstevel@tonic-gate 	(void) vgatext_detach(devi, DDI_DETACH);
5937c478bd9Sstevel@tonic-gate 	return (error);
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate static int
5977c478bd9Sstevel@tonic-gate vgatext_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
6007c478bd9Sstevel@tonic-gate 	struct vgatext_softc *softc = getsoftc(instance);
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	switch (cmd) {
6047c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
6057c478bd9Sstevel@tonic-gate 		if (softc->fb.mapped)
6067c478bd9Sstevel@tonic-gate 			ddi_regs_map_free(&softc->fb.handle);
6077c478bd9Sstevel@tonic-gate 		if (softc->regs.mapped)
6087c478bd9Sstevel@tonic-gate 			ddi_regs_map_free(&softc->regs.handle);
60948633f18SJan Setje-Eilers 		mutex_destroy(&(softc->lock));
6107c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
6117c478bd9Sstevel@tonic-gate 		(void) ddi_soft_state_free(vgatext_softc_head, instance);
6127c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6137c478bd9Sstevel@tonic-gate 
6142df1fe9cSrandyf 	case DDI_SUSPEND:
6152df1fe9cSrandyf 		/*
6162df1fe9cSrandyf 		 * This is a generic VGA file, and therefore, cannot
6172df1fe9cSrandyf 		 * understand how to deal with suspend and resume on
6182df1fe9cSrandyf 		 * a generic interface.  So we fail any attempt to
6192df1fe9cSrandyf 		 * suspend.  At some point in the future, we might use
6202df1fe9cSrandyf 		 * this as an entrypoint for display drivers and this
6212df1fe9cSrandyf 		 * assumption may change.
6222df1fe9cSrandyf 		 *
6232df1fe9cSrandyf 		 * However, from a platform development perspective,
6242df1fe9cSrandyf 		 * it is important that this driver suspend if a
6252df1fe9cSrandyf 		 * developer is using a serial console and/or working
6262df1fe9cSrandyf 		 * on a framebuffer driver that will support suspend
6272df1fe9cSrandyf 		 * and resume.  Therefore, we have this module tunable
6282df1fe9cSrandyf 		 * (purposely using a long name) that will allow for
6292df1fe9cSrandyf 		 * suspend it it is set.  Otherwise we fail.
6302df1fe9cSrandyf 		 */
6312df1fe9cSrandyf 		if (vgatext_force_suspend != 0)
6322df1fe9cSrandyf 			return (DDI_SUCCESS);
6332df1fe9cSrandyf 		else
6342df1fe9cSrandyf 			return (DDI_FAILURE);
6352df1fe9cSrandyf 
6367c478bd9Sstevel@tonic-gate 	default:
6377c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vgatext_detach: unknown cmd 0x%x\n", cmd);
6387c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6437c478bd9Sstevel@tonic-gate static int
6447c478bd9Sstevel@tonic-gate vgatext_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	dev_t dev;
6477c478bd9Sstevel@tonic-gate 	int error;
6487c478bd9Sstevel@tonic-gate 	int instance;
6497c478bd9Sstevel@tonic-gate 	struct vgatext_softc *softc;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	error = DDI_SUCCESS;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	dev = (dev_t)arg;
6547c478bd9Sstevel@tonic-gate 	instance = DEV2INST(dev);
6557c478bd9Sstevel@tonic-gate 	softc = getsoftc(instance);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	switch (infocmd) {
6587c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
6597c478bd9Sstevel@tonic-gate 		if (softc == NULL || softc->devi == NULL) {
6607c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
6617c478bd9Sstevel@tonic-gate 		} else {
6627c478bd9Sstevel@tonic-gate 			*result = (void *) softc->devi;
6637c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
6647c478bd9Sstevel@tonic-gate 		}
6657c478bd9Sstevel@tonic-gate 		break;
6667c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
6677c478bd9Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
6687c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
6697c478bd9Sstevel@tonic-gate 		break;
6707c478bd9Sstevel@tonic-gate 	default:
6717c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
6727c478bd9Sstevel@tonic-gate 		break;
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 	return (error);
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6797c478bd9Sstevel@tonic-gate static int
6807c478bd9Sstevel@tonic-gate vgatext_open(dev_t *devp, int flag, int otyp, cred_t *cred)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	struct vgatext_softc *softc = getsoftc(DEV2INST(*devp));
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (softc == NULL || otyp == OTYP_BLK)
6857c478bd9Sstevel@tonic-gate 		return (ENXIO);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	return (0);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6917c478bd9Sstevel@tonic-gate static int
6927c478bd9Sstevel@tonic-gate vgatext_close(dev_t devp, int flag, int otyp, cred_t *cred)
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	return (0);
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate static int
69860405de4Skz151634 do_gfx_ioctl(int cmd, intptr_t data, int mode, struct vgatext_softc *softc)
6997c478bd9Sstevel@tonic-gate {
70060405de4Skz151634 	static char kernel_only[] =
70160405de4Skz151634 	    "do_gfx_ioctl: %s is a kernel only ioctl";
7027c478bd9Sstevel@tonic-gate 	int err;
7037c478bd9Sstevel@tonic-gate 	int kd_mode;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	switch (cmd) {
7067c478bd9Sstevel@tonic-gate 	case KDSETMODE:
7077c478bd9Sstevel@tonic-gate 		return (vgatext_kdsetmode(softc, (int)data));
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	case KDGETMODE:
7107c478bd9Sstevel@tonic-gate 		kd_mode = softc->mode;
7117c478bd9Sstevel@tonic-gate 		if (ddi_copyout(&kd_mode, (void *)data, sizeof (int), mode))
7127c478bd9Sstevel@tonic-gate 			return (EFAULT);
7137c478bd9Sstevel@tonic-gate 		break;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	case VIS_GETIDENTIFIER:
7167c478bd9Sstevel@tonic-gate 		if (ddi_copyout(&text_ident, (void *)data,
7177c478bd9Sstevel@tonic-gate 		    sizeof (struct vis_identifier), mode))
7187c478bd9Sstevel@tonic-gate 			return (EFAULT);
7197c478bd9Sstevel@tonic-gate 		break;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	case VIS_DEVINIT:
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 		if (!(mode & FKIOCTL)) {
7247c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, kernel_only, "VIS_DEVINIT");
7257c478bd9Sstevel@tonic-gate 			return (ENXIO);
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 		err = vgatext_devinit(softc, (struct vis_devinit *)data);
7297c478bd9Sstevel@tonic-gate 		if (err != 0) {
7307c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
7317c478bd9Sstevel@tonic-gate 			    "vgatext_ioctl:  could not initialize console");
7327c478bd9Sstevel@tonic-gate 			return (err);
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 		break;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	case VIS_CONSCOPY:	/* move */
7377c478bd9Sstevel@tonic-gate 		{
7387c478bd9Sstevel@tonic-gate 		struct vis_conscopy pma;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)data, &pma,
7417c478bd9Sstevel@tonic-gate 		    sizeof (struct vis_conscopy), mode))
7427c478bd9Sstevel@tonic-gate 			return (EFAULT);
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		vgatext_cons_copy(softc, &pma);
7457c478bd9Sstevel@tonic-gate 		break;
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	case VIS_CONSDISPLAY:	/* display */
7497c478bd9Sstevel@tonic-gate 		{
7507c478bd9Sstevel@tonic-gate 		struct vis_consdisplay display_request;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)data, &display_request,
7537c478bd9Sstevel@tonic-gate 		    sizeof (display_request), mode))
7547c478bd9Sstevel@tonic-gate 			return (EFAULT);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		vgatext_cons_display(softc, &display_request);
7577c478bd9Sstevel@tonic-gate 		break;
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	case VIS_CONSCURSOR:
7617c478bd9Sstevel@tonic-gate 		{
7627c478bd9Sstevel@tonic-gate 		struct vis_conscursor cursor_request;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)data, &cursor_request,
7657c478bd9Sstevel@tonic-gate 		    sizeof (cursor_request), mode))
7667c478bd9Sstevel@tonic-gate 			return (EFAULT);
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		vgatext_cons_cursor(softc, &cursor_request);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 		if (cursor_request.action == VIS_GET_CURSOR &&
7717c478bd9Sstevel@tonic-gate 		    ddi_copyout(&cursor_request, (void *)data,
7727c478bd9Sstevel@tonic-gate 		    sizeof (cursor_request), mode))
7737c478bd9Sstevel@tonic-gate 			return (EFAULT);
7747c478bd9Sstevel@tonic-gate 		break;
7757c478bd9Sstevel@tonic-gate 		}
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	case VIS_GETCMAP:
7787c478bd9Sstevel@tonic-gate 	case VIS_PUTCMAP:
7797c478bd9Sstevel@tonic-gate 	case FBIOPUTCMAP:
7807c478bd9Sstevel@tonic-gate 	case FBIOGETCMAP:
7817c478bd9Sstevel@tonic-gate 		/*
7827c478bd9Sstevel@tonic-gate 		 * At the moment, text mode is not considered to have
7837c478bd9Sstevel@tonic-gate 		 * a color map.
7847c478bd9Sstevel@tonic-gate 		 */
7857c478bd9Sstevel@tonic-gate 		return (EINVAL);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	case FBIOGATTR:
7887c478bd9Sstevel@tonic-gate 		if (copyout(&vgatext_attr, (void *)data,
7897c478bd9Sstevel@tonic-gate 		    sizeof (struct fbgattr)))
7907c478bd9Sstevel@tonic-gate 			return (EFAULT);
7917c478bd9Sstevel@tonic-gate 		break;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	case FBIOGTYPE:
7947c478bd9Sstevel@tonic-gate 		if (copyout(&vgatext_attr.fbtype, (void *)data,
7957c478bd9Sstevel@tonic-gate 		    sizeof (struct fbtype)))
7967c478bd9Sstevel@tonic-gate 			return (EFAULT);
7977c478bd9Sstevel@tonic-gate 		break;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	default:
8007c478bd9Sstevel@tonic-gate 		return (ENXIO);
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 	return (0);
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
80560405de4Skz151634 
80660405de4Skz151634 /*ARGSUSED*/
80760405de4Skz151634 static int
80860405de4Skz151634 vgatext_ioctl(
80960405de4Skz151634     dev_t dev,
81060405de4Skz151634     int cmd,
81160405de4Skz151634     intptr_t data,
81260405de4Skz151634     int mode,
81360405de4Skz151634     cred_t *cred,
81460405de4Skz151634     int *rval)
81560405de4Skz151634 {
81660405de4Skz151634 	struct vgatext_softc *softc = getsoftc(DEV2INST(dev));
81760405de4Skz151634 	int err;
81860405de4Skz151634 
81960405de4Skz151634 	switch (DEV2MINOR(dev)) {
82060405de4Skz151634 	case GFX_MINOR:
82148633f18SJan Setje-Eilers 		mutex_enter(&(softc->lock));
82260405de4Skz151634 		err = do_gfx_ioctl(cmd, data, mode, softc);
82348633f18SJan Setje-Eilers 		mutex_exit(&(softc->lock));
82460405de4Skz151634 		break;
82560405de4Skz151634 
82660405de4Skz151634 	case AGPMASTER_MINOR:
827*945167b5SGordon Ross 		/*
828*945167b5SGordon Ross 		 * This is apparently not used anymore.  Let's log a
829*945167b5SGordon Ross 		 * message so we'll know if some consumer shows up.
830*945167b5SGordon Ross 		 * If it turns out that we actually do need to keep
831*945167b5SGordon Ross 		 * support for this pass-through to agpmaster, it
832*945167b5SGordon Ross 		 * would probably be better to use "layered" access
833*945167b5SGordon Ross 		 * to the AGP device (ldi_open, ldi_ioctl, ldi_close)
834*945167b5SGordon Ross 		 */
835*945167b5SGordon Ross 		cmn_err(CE_NOTE, "!vgatext wants agpmaster");
836*945167b5SGordon Ross 		return (EBADF);
83760405de4Skz151634 
83860405de4Skz151634 	default:
83960405de4Skz151634 		/* not a valid minor node */
84060405de4Skz151634 		return (EBADF);
84160405de4Skz151634 	}
84260405de4Skz151634 	return (err);
84360405de4Skz151634 }
84460405de4Skz151634 
84548633f18SJan Setje-Eilers static void
84648633f18SJan Setje-Eilers vgatext_save_text(struct vgatext_softc *softc)
84748633f18SJan Setje-Eilers {
84848633f18SJan Setje-Eilers 	unsigned i;
84948633f18SJan Setje-Eilers 
85048633f18SJan Setje-Eilers 	for (i = 0; i < sizeof (softc->shadow); i++)
85148633f18SJan Setje-Eilers 		softc->shadow[i] = softc->current_base[i];
85248633f18SJan Setje-Eilers }
85348633f18SJan Setje-Eilers 
85448633f18SJan Setje-Eilers static void
85548633f18SJan Setje-Eilers vgatext_progressbar_stop()
85648633f18SJan Setje-Eilers {
85748633f18SJan Setje-Eilers 	extern void progressbar_stop(void);
85848633f18SJan Setje-Eilers 
85948633f18SJan Setje-Eilers 	if (vgatext_silent == 1) {
86048633f18SJan Setje-Eilers 		vgatext_silent = 0;
86148633f18SJan Setje-Eilers 		progressbar_stop();
86248633f18SJan Setje-Eilers 	}
86348633f18SJan Setje-Eilers }
86448633f18SJan Setje-Eilers 
86548633f18SJan Setje-Eilers static void
86648633f18SJan Setje-Eilers vgatext_kdsettext(struct vgatext_softc *softc)
8677c478bd9Sstevel@tonic-gate {
8687c478bd9Sstevel@tonic-gate 	int i;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	vgatext_init(softc);
8717c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (softc->shadow); i++) {
8727c478bd9Sstevel@tonic-gate 		softc->text_base[i] = softc->shadow[i];
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 	softc->current_base = softc->text_base;
8757c478bd9Sstevel@tonic-gate 	if (softc->cursor.visible) {
8767c478bd9Sstevel@tonic-gate 		vgatext_set_cursor(softc,
8777c478bd9Sstevel@tonic-gate 		    softc->cursor.row, softc->cursor.col);
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 	vgatext_restore_colormap(softc);
8807c478bd9Sstevel@tonic-gate }
88148633f18SJan Setje-Eilers 
88248633f18SJan Setje-Eilers static void
88348633f18SJan Setje-Eilers vgatext_kdsetgraphics(struct vgatext_softc *softc)
88448633f18SJan Setje-Eilers {
88548633f18SJan Setje-Eilers 	vgatext_progressbar_stop();
88648633f18SJan Setje-Eilers 	vgatext_save_text(softc);
8877c478bd9Sstevel@tonic-gate 	softc->current_base = softc->shadow;
8887c478bd9Sstevel@tonic-gate #if	defined(USE_BORDERS)
8897c478bd9Sstevel@tonic-gate 	vgatext_init_graphics(softc);
8907c478bd9Sstevel@tonic-gate #endif
89148633f18SJan Setje-Eilers }
89248633f18SJan Setje-Eilers 
89348633f18SJan Setje-Eilers static int
89448633f18SJan Setje-Eilers vgatext_kdsetmode(struct vgatext_softc *softc, int mode)
89548633f18SJan Setje-Eilers {
89648633f18SJan Setje-Eilers 	if ((mode == softc->mode) || (!VGATEXT_IS_CONSOLE(softc)))
89748633f18SJan Setje-Eilers 		return (0);
89848633f18SJan Setje-Eilers 
89948633f18SJan Setje-Eilers 	switch (mode) {
90048633f18SJan Setje-Eilers 	case KD_TEXT:
90148633f18SJan Setje-Eilers 		vgatext_kdsettext(softc);
90248633f18SJan Setje-Eilers 		break;
90348633f18SJan Setje-Eilers 
90448633f18SJan Setje-Eilers 	case KD_GRAPHICS:
90548633f18SJan Setje-Eilers 		vgatext_kdsetgraphics(softc);
90648633f18SJan Setje-Eilers 		break;
90748633f18SJan Setje-Eilers 
90848633f18SJan Setje-Eilers 	case KD_RESETTEXT:
90948633f18SJan Setje-Eilers 		/*
91048633f18SJan Setje-Eilers 		 * In order to avoid racing with a starting X server,
91148633f18SJan Setje-Eilers 		 * this needs to be a test and set that is performed in
91248633f18SJan Setje-Eilers 		 * a single (softc->lock protected) ioctl into this driver.
91348633f18SJan Setje-Eilers 		 */
91448633f18SJan Setje-Eilers 		if (softc->mode == KD_TEXT && vgatext_silent == 1) {
91548633f18SJan Setje-Eilers 			vgatext_progressbar_stop();
91648633f18SJan Setje-Eilers 			vgatext_kdsettext(softc);
91748633f18SJan Setje-Eilers 		}
9187c478bd9Sstevel@tonic-gate 		break;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	default:
9217c478bd9Sstevel@tonic-gate 		return (EINVAL);
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 	softc->mode = mode;
9247c478bd9Sstevel@tonic-gate 	return (0);
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9287c478bd9Sstevel@tonic-gate static int
9297c478bd9Sstevel@tonic-gate vgatext_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len,
9307c478bd9Sstevel@tonic-gate 		size_t *maplen, uint_t model)
9317c478bd9Sstevel@tonic-gate {
9327c478bd9Sstevel@tonic-gate 	struct vgatext_softc *softc;
9337c478bd9Sstevel@tonic-gate 	int err;
9347c478bd9Sstevel@tonic-gate 	size_t length;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	softc = getsoftc(DEV2INST(dev));
9387c478bd9Sstevel@tonic-gate 	if (softc == NULL) {
9397c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vgatext: Can't find softstate");
9407c478bd9Sstevel@tonic-gate 		return (-1);
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	if (!(off >= VGA_MMAP_FB_BASE &&
9447c478bd9Sstevel@tonic-gate 	    off < VGA_MMAP_FB_BASE + softc->fb_size)) {
9457c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vgatext: Can't map offset 0x%llx", off);
9467c478bd9Sstevel@tonic-gate 		return (-1);
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	if (off + len > VGA_MMAP_FB_BASE + softc->fb_size)
9507c478bd9Sstevel@tonic-gate 		length = VGA_MMAP_FB_BASE + softc->fb_size - off;
9517c478bd9Sstevel@tonic-gate 	else
9527c478bd9Sstevel@tonic-gate 		length = len;
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	if ((err = devmap_devmem_setup(dhp, softc->devi, NULL, softc->fb_regno,
9557c478bd9Sstevel@tonic-gate 	    off - VGA_MMAP_FB_BASE,
9567c478bd9Sstevel@tonic-gate 	    length, PROT_ALL, 0, &dev_attr)) < 0) {
9577c478bd9Sstevel@tonic-gate 		return (err);
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	*maplen = length;
9627c478bd9Sstevel@tonic-gate 	return (0);
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate static int
9677c478bd9Sstevel@tonic-gate vgatext_devinit(struct vgatext_softc *softc, struct vis_devinit *data)
9687c478bd9Sstevel@tonic-gate {
9697c478bd9Sstevel@tonic-gate 	/* initialize console instance */
9707c478bd9Sstevel@tonic-gate 	data->version = VIS_CONS_REV;
9717c478bd9Sstevel@tonic-gate 	data->width = TEXT_COLS;
9727c478bd9Sstevel@tonic-gate 	data->height = TEXT_ROWS;
9737c478bd9Sstevel@tonic-gate 	data->linebytes = TEXT_COLS;
9747c478bd9Sstevel@tonic-gate 	data->depth = 4;
9757c478bd9Sstevel@tonic-gate 	data->mode = VIS_TEXT;
9767c478bd9Sstevel@tonic-gate 	data->polledio = &softc->polledio;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	return (0);
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate /*
9827c478bd9Sstevel@tonic-gate  * display a string on the screen at (row, col)
9837c478bd9Sstevel@tonic-gate  *	 assume it has been cropped to fit.
9847c478bd9Sstevel@tonic-gate  */
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate static void
9877c478bd9Sstevel@tonic-gate vgatext_cons_display(struct vgatext_softc *softc, struct vis_consdisplay *da)
9887c478bd9Sstevel@tonic-gate {
9897c478bd9Sstevel@tonic-gate 	unsigned char	*string;
9907c478bd9Sstevel@tonic-gate 	int	i;
9917c478bd9Sstevel@tonic-gate 	unsigned char	attr;
9927c478bd9Sstevel@tonic-gate 	struct cgatext {
9937c478bd9Sstevel@tonic-gate 		unsigned char ch;
9947c478bd9Sstevel@tonic-gate 		unsigned char attr;
9957c478bd9Sstevel@tonic-gate 	};
9967c478bd9Sstevel@tonic-gate 	struct cgatext *addr;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	/*
9997c478bd9Sstevel@tonic-gate 	 * Sanity checks.  This is a last-ditch effort to avoid damage
10007c478bd9Sstevel@tonic-gate 	 * from brokenness or maliciousness above.
10017c478bd9Sstevel@tonic-gate 	 */
10027c478bd9Sstevel@tonic-gate 	if (da->row < 0 || da->row >= TEXT_ROWS ||
10037c478bd9Sstevel@tonic-gate 	    da->col < 0 || da->col >= TEXT_COLS ||
10047c478bd9Sstevel@tonic-gate 	    da->col + da->width > TEXT_COLS)
10057c478bd9Sstevel@tonic-gate 		return;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	/*
10087c478bd9Sstevel@tonic-gate 	 * To be fully general, we should copyin the data.  This is not
10097c478bd9Sstevel@tonic-gate 	 * really relevant for this text-only driver, but a graphical driver
10107c478bd9Sstevel@tonic-gate 	 * should support these ioctls from userland to enable simple
10117c478bd9Sstevel@tonic-gate 	 * system startup graphics.
10127c478bd9Sstevel@tonic-gate 	 */
10137c478bd9Sstevel@tonic-gate 	attr = (solaris_color_to_pc_color[da->bg_color & 0xf] << 4)
10147c478bd9Sstevel@tonic-gate 	    | solaris_color_to_pc_color[da->fg_color & 0xf];
10157c478bd9Sstevel@tonic-gate 	string = da->data;
10167c478bd9Sstevel@tonic-gate 	addr = (struct cgatext *)softc->current_base
10177c478bd9Sstevel@tonic-gate 	    +  (da->row * TEXT_COLS + da->col);
10187c478bd9Sstevel@tonic-gate 	for (i = 0; i < da->width; i++) {
10197c478bd9Sstevel@tonic-gate 		addr->ch = string[i];
10207c478bd9Sstevel@tonic-gate 		addr->attr = attr;
10217c478bd9Sstevel@tonic-gate 		addr++;
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate static void
10267c478bd9Sstevel@tonic-gate vgatext_polled_display(
10277c478bd9Sstevel@tonic-gate 	struct vis_polledio_arg *arg,
10287c478bd9Sstevel@tonic-gate 	struct vis_consdisplay *da)
10297c478bd9Sstevel@tonic-gate {
10307c478bd9Sstevel@tonic-gate 	vgatext_cons_display((struct vgatext_softc *)arg, da);
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate /*
10347c478bd9Sstevel@tonic-gate  * screen-to-screen copy
10357c478bd9Sstevel@tonic-gate  */
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate static void
10387c478bd9Sstevel@tonic-gate vgatext_cons_copy(struct vgatext_softc *softc, struct vis_conscopy *ma)
10397c478bd9Sstevel@tonic-gate {
10407c478bd9Sstevel@tonic-gate 	unsigned short	*from;
10417c478bd9Sstevel@tonic-gate 	unsigned short	*to;
10427c478bd9Sstevel@tonic-gate 	int		cnt;
10437c478bd9Sstevel@tonic-gate 	screen_size_t chars_per_row;
10447c478bd9Sstevel@tonic-gate 	unsigned short	*to_row_start;
10457c478bd9Sstevel@tonic-gate 	unsigned short	*from_row_start;
10467c478bd9Sstevel@tonic-gate 	screen_size_t	rows_to_move;
10477c478bd9Sstevel@tonic-gate 	unsigned short	*base;
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	/*
10507c478bd9Sstevel@tonic-gate 	 * Sanity checks.  Note that this is a last-ditch effort to avoid
10517c478bd9Sstevel@tonic-gate 	 * damage caused by broken-ness or maliciousness above.
10527c478bd9Sstevel@tonic-gate 	 */
10537c478bd9Sstevel@tonic-gate 	if (ma->s_col < 0 || ma->s_col >= TEXT_COLS ||
10547c478bd9Sstevel@tonic-gate 	    ma->s_row < 0 || ma->s_row >= TEXT_ROWS ||
10557c478bd9Sstevel@tonic-gate 	    ma->e_col < 0 || ma->e_col >= TEXT_COLS ||
10567c478bd9Sstevel@tonic-gate 	    ma->e_row < 0 || ma->e_row >= TEXT_ROWS ||
10577c478bd9Sstevel@tonic-gate 	    ma->t_col < 0 || ma->t_col >= TEXT_COLS ||
10587c478bd9Sstevel@tonic-gate 	    ma->t_row < 0 || ma->t_row >= TEXT_ROWS ||
10597c478bd9Sstevel@tonic-gate 	    ma->s_col > ma->e_col ||
10607c478bd9Sstevel@tonic-gate 	    ma->s_row > ma->e_row)
10617c478bd9Sstevel@tonic-gate 		return;
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	/*
10647c478bd9Sstevel@tonic-gate 	 * Remember we're going to copy shorts because each
10657c478bd9Sstevel@tonic-gate 	 * character/attribute pair is 16 bits.
10667c478bd9Sstevel@tonic-gate 	 */
10677c478bd9Sstevel@tonic-gate 	chars_per_row = ma->e_col - ma->s_col + 1;
10687c478bd9Sstevel@tonic-gate 	rows_to_move = ma->e_row - ma->s_row + 1;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	/* More sanity checks. */
10717c478bd9Sstevel@tonic-gate 	if (ma->t_row + rows_to_move > TEXT_ROWS ||
10727c478bd9Sstevel@tonic-gate 	    ma->t_col + chars_per_row > TEXT_COLS)
10737c478bd9Sstevel@tonic-gate 		return;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	base = (unsigned short *)softc->current_base;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	to_row_start = base + ((ma->t_row * TEXT_COLS) + ma->t_col);
10787c478bd9Sstevel@tonic-gate 	from_row_start = base + ((ma->s_row * TEXT_COLS) + ma->s_col);
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	if (to_row_start < from_row_start) {
10817c478bd9Sstevel@tonic-gate 		while (rows_to_move-- > 0) {
10827c478bd9Sstevel@tonic-gate 			to = to_row_start;
10837c478bd9Sstevel@tonic-gate 			from = from_row_start;
10847c478bd9Sstevel@tonic-gate 			to_row_start += TEXT_COLS;
10857c478bd9Sstevel@tonic-gate 			from_row_start += TEXT_COLS;
10867c478bd9Sstevel@tonic-gate 			for (cnt = chars_per_row; cnt-- > 0; )
10877c478bd9Sstevel@tonic-gate 				*to++ = *from++;
10887c478bd9Sstevel@tonic-gate 		}
10897c478bd9Sstevel@tonic-gate 	} else {
10907c478bd9Sstevel@tonic-gate 		/*
10917c478bd9Sstevel@tonic-gate 		 * Offset to the end of the region and copy backwards.
10927c478bd9Sstevel@tonic-gate 		 */
10937c478bd9Sstevel@tonic-gate 		cnt = rows_to_move * TEXT_COLS + chars_per_row;
10947c478bd9Sstevel@tonic-gate 		to_row_start += cnt;
10957c478bd9Sstevel@tonic-gate 		from_row_start += cnt;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 		while (rows_to_move-- > 0) {
10987c478bd9Sstevel@tonic-gate 			to_row_start -= TEXT_COLS;
10997c478bd9Sstevel@tonic-gate 			from_row_start -= TEXT_COLS;
11007c478bd9Sstevel@tonic-gate 			to = to_row_start;
11017c478bd9Sstevel@tonic-gate 			from = from_row_start;
11027c478bd9Sstevel@tonic-gate 			for (cnt = chars_per_row; cnt-- > 0; )
11037c478bd9Sstevel@tonic-gate 				*--to = *--from;
11047c478bd9Sstevel@tonic-gate 		}
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate }
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate static void
11097c478bd9Sstevel@tonic-gate vgatext_polled_copy(
11107c478bd9Sstevel@tonic-gate 	struct vis_polledio_arg *arg,
11117c478bd9Sstevel@tonic-gate 	struct vis_conscopy *ca)
11127c478bd9Sstevel@tonic-gate {
11137c478bd9Sstevel@tonic-gate 	vgatext_cons_copy((struct vgatext_softc *)arg, ca);
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate static void
11187c478bd9Sstevel@tonic-gate vgatext_cons_cursor(struct vgatext_softc *softc, struct vis_conscursor *ca)
11197c478bd9Sstevel@tonic-gate {
11207c478bd9Sstevel@tonic-gate 	if (vgatext_silent)
11217c478bd9Sstevel@tonic-gate 		return;
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	switch (ca->action) {
11247c478bd9Sstevel@tonic-gate 	case VIS_HIDE_CURSOR:
11257c478bd9Sstevel@tonic-gate 		softc->cursor.visible = B_FALSE;
11267c478bd9Sstevel@tonic-gate 		if (softc->current_base == softc->text_base)
11277c478bd9Sstevel@tonic-gate 			vgatext_hide_cursor(softc);
11287c478bd9Sstevel@tonic-gate 		break;
11297c478bd9Sstevel@tonic-gate 	case VIS_DISPLAY_CURSOR:
11307c478bd9Sstevel@tonic-gate 		/*
11317c478bd9Sstevel@tonic-gate 		 * Sanity check.  This is a last-ditch effort to avoid
11327c478bd9Sstevel@tonic-gate 		 * damage from brokenness or maliciousness above.
11337c478bd9Sstevel@tonic-gate 		 */
11347c478bd9Sstevel@tonic-gate 		if (ca->col < 0 || ca->col >= TEXT_COLS ||
11357c478bd9Sstevel@tonic-gate 		    ca->row < 0 || ca->row >= TEXT_ROWS)
11367c478bd9Sstevel@tonic-gate 			return;
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 		softc->cursor.visible = B_TRUE;
11397c478bd9Sstevel@tonic-gate 		softc->cursor.col = ca->col;
11407c478bd9Sstevel@tonic-gate 		softc->cursor.row = ca->row;
11417c478bd9Sstevel@tonic-gate 		if (softc->current_base == softc->text_base)
11427c478bd9Sstevel@tonic-gate 			vgatext_set_cursor(softc, ca->row, ca->col);
11437c478bd9Sstevel@tonic-gate 		break;
11447c478bd9Sstevel@tonic-gate 	case VIS_GET_CURSOR:
11457c478bd9Sstevel@tonic-gate 		if (softc->current_base == softc->text_base) {
11467c478bd9Sstevel@tonic-gate 			vgatext_get_cursor(softc, &ca->row, &ca->col);
11477c478bd9Sstevel@tonic-gate 		}
11487c478bd9Sstevel@tonic-gate 		break;
11497c478bd9Sstevel@tonic-gate 	}
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate static void
11537c478bd9Sstevel@tonic-gate vgatext_polled_cursor(
11547c478bd9Sstevel@tonic-gate 	struct vis_polledio_arg *arg,
11557c478bd9Sstevel@tonic-gate 	struct vis_conscursor *ca)
11567c478bd9Sstevel@tonic-gate {
11577c478bd9Sstevel@tonic-gate 	vgatext_cons_cursor((struct vgatext_softc *)arg, ca);
11587c478bd9Sstevel@tonic-gate }
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11637c478bd9Sstevel@tonic-gate static void
11647c478bd9Sstevel@tonic-gate vgatext_hide_cursor(struct vgatext_softc *softc)
11657c478bd9Sstevel@tonic-gate {
11667c478bd9Sstevel@tonic-gate 	/* Nothing at present */
11677c478bd9Sstevel@tonic-gate }
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate static void
11707c478bd9Sstevel@tonic-gate vgatext_set_cursor(struct vgatext_softc *softc, int row, int col)
11717c478bd9Sstevel@tonic-gate {
11727c478bd9Sstevel@tonic-gate 	short	addr;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	if (vgatext_silent)
11757c478bd9Sstevel@tonic-gate 		return;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	addr = row * TEXT_COLS + col;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	vga_set_crtc(&softc->regs, VGA_CRTC_CLAH, addr >> 8);
11807c478bd9Sstevel@tonic-gate 	vga_set_crtc(&softc->regs, VGA_CRTC_CLAL, addr & 0xff);
11817c478bd9Sstevel@tonic-gate }
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate static int vga_row, vga_col;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate static void
11867c478bd9Sstevel@tonic-gate vgatext_get_cursor(struct vgatext_softc *softc,
11877c478bd9Sstevel@tonic-gate     screen_pos_t *row, screen_pos_t *col)
11887c478bd9Sstevel@tonic-gate {
11897c478bd9Sstevel@tonic-gate 	short   addr;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	addr = (vga_get_crtc(&softc->regs, VGA_CRTC_CLAH) << 8) +
11927c478bd9Sstevel@tonic-gate 	    vga_get_crtc(&softc->regs, VGA_CRTC_CLAL);
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	vga_row = *row = addr / TEXT_COLS;
11957c478bd9Sstevel@tonic-gate 	vga_col = *col = addr % TEXT_COLS;
11967c478bd9Sstevel@tonic-gate }
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate /*
11997c478bd9Sstevel@tonic-gate  * This code is experimental. It's only enabled if console is
12007c478bd9Sstevel@tonic-gate  * set to graphics, a preliminary implementation of happyface boot.
12017c478bd9Sstevel@tonic-gate  */
12027c478bd9Sstevel@tonic-gate static void
12037c478bd9Sstevel@tonic-gate vgatext_set_text(struct vgatext_softc *softc)
12047c478bd9Sstevel@tonic-gate {
12057c478bd9Sstevel@tonic-gate 	int i;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	if (happyface_boot == 0)
12087c478bd9Sstevel@tonic-gate 		return;
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	/* we are in graphics mode, set to text 80X25 mode */
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	/* set misc registers */
12137c478bd9Sstevel@tonic-gate 	vga_set_reg(&softc->regs, VGA_MISC_W, VGA_MISC_TEXT);
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	/* set sequencer registers */
12167c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, VGA_SEQ_RST_SYN,
12177c478bd9Sstevel@tonic-gate 	    (vga_get_seq(&softc->regs, VGA_SEQ_RST_SYN) &
12187c478bd9Sstevel@tonic-gate 	    ~VGA_SEQ_RST_SYN_NO_SYNC_RESET));
12197c478bd9Sstevel@tonic-gate 	for (i = 1; i < NUM_SEQ_REG; i++) {
12207c478bd9Sstevel@tonic-gate 		vga_set_seq(&softc->regs, i, VGA_SEQ_TEXT[i]);
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, VGA_SEQ_RST_SYN,
12237c478bd9Sstevel@tonic-gate 	    (vga_get_seq(&softc->regs, VGA_SEQ_RST_SYN) |
12247c478bd9Sstevel@tonic-gate 	    VGA_SEQ_RST_SYN_NO_ASYNC_RESET |
12257c478bd9Sstevel@tonic-gate 	    VGA_SEQ_RST_SYN_NO_SYNC_RESET));
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	/* set crt controller registers */
12287c478bd9Sstevel@tonic-gate 	vga_set_crtc(&softc->regs, VGA_CRTC_VRE,
12297c478bd9Sstevel@tonic-gate 	    (vga_get_crtc(&softc->regs, VGA_CRTC_VRE) &
12307c478bd9Sstevel@tonic-gate 	    ~VGA_CRTC_VRE_LOCK));
12317c478bd9Sstevel@tonic-gate 	for (i = 0; i < NUM_CRTC_REG; i++) {
12327c478bd9Sstevel@tonic-gate 		vga_set_crtc(&softc->regs, i, VGA_CRTC_TEXT[i]);
12337c478bd9Sstevel@tonic-gate 	}
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	/* set graphics controller registers */
12367c478bd9Sstevel@tonic-gate 	for (i = 0; i < NUM_GRC_REG; i++) {
12377c478bd9Sstevel@tonic-gate 		vga_set_grc(&softc->regs, i, VGA_GRC_TEXT[i]);
12387c478bd9Sstevel@tonic-gate 	}
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	/* set attribute registers */
12417c478bd9Sstevel@tonic-gate 	for (i = 0; i < NUM_ATR_REG; i++) {
12427c478bd9Sstevel@tonic-gate 		vga_set_atr(&softc->regs, i, VGA_ATR_TEXT[i]);
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	/* set palette */
12467c478bd9Sstevel@tonic-gate 	for (i = 0; i < VGA_TEXT_CMAP_ENTRIES; i++) {
12477c478bd9Sstevel@tonic-gate 		vga_put_cmap(&softc->regs, i, VGA_TEXT_PALETTES[i][0] << 2,
12487c478bd9Sstevel@tonic-gate 		    VGA_TEXT_PALETTES[i][1] << 2,
12497c478bd9Sstevel@tonic-gate 		    VGA_TEXT_PALETTES[i][2] << 2);
12507c478bd9Sstevel@tonic-gate 	}
12517c478bd9Sstevel@tonic-gate 	for (i = VGA_TEXT_CMAP_ENTRIES; i < VGA8_CMAP_ENTRIES; i++) {
12527c478bd9Sstevel@tonic-gate 		vga_put_cmap(&softc->regs, i, 0, 0, 0);
12537c478bd9Sstevel@tonic-gate 	}
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	vgatext_save_colormap(softc);
12567c478bd9Sstevel@tonic-gate }
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate static void
12597c478bd9Sstevel@tonic-gate vgatext_init(struct vgatext_softc *softc)
12607c478bd9Sstevel@tonic-gate {
12617c478bd9Sstevel@tonic-gate 	unsigned char atr_mode;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	atr_mode = vga_get_atr(&softc->regs, VGA_ATR_MODE);
12647c478bd9Sstevel@tonic-gate 	if (atr_mode & VGA_ATR_MODE_GRAPH)
12657c478bd9Sstevel@tonic-gate 		vgatext_set_text(softc);
12667c478bd9Sstevel@tonic-gate 	atr_mode = vga_get_atr(&softc->regs, VGA_ATR_MODE);
12677c478bd9Sstevel@tonic-gate 	atr_mode &= ~VGA_ATR_MODE_BLINK;
12687c478bd9Sstevel@tonic-gate 	atr_mode &= ~VGA_ATR_MODE_9WIDE;
12697c478bd9Sstevel@tonic-gate 	vga_set_atr(&softc->regs, VGA_ATR_MODE, atr_mode);
12707c478bd9Sstevel@tonic-gate #if	defined(USE_BORDERS)
12717c478bd9Sstevel@tonic-gate 	vga_set_atr(&softc->regs, VGA_ATR_BDR_CLR,
12727c478bd9Sstevel@tonic-gate 	    vga_get_atr(&softc->regs, VGA_BRIGHT_WHITE));
12737c478bd9Sstevel@tonic-gate #else
12747c478bd9Sstevel@tonic-gate 	vga_set_atr(&softc->regs, VGA_ATR_BDR_CLR,
12757c478bd9Sstevel@tonic-gate 	    vga_get_atr(&softc->regs, VGA_BLACK));
12767c478bd9Sstevel@tonic-gate #endif
12777c478bd9Sstevel@tonic-gate 	vgatext_setfont(softc);	/* need selectable font? */
12787c478bd9Sstevel@tonic-gate }
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate #if	defined(USE_BORDERS)
12817c478bd9Sstevel@tonic-gate static void
12827c478bd9Sstevel@tonic-gate vgatext_init_graphics(struct vgatext_softc *softc)
12837c478bd9Sstevel@tonic-gate {
12847c478bd9Sstevel@tonic-gate 	vga_set_atr(&softc->regs, VGA_ATR_BDR_CLR,
12857c478bd9Sstevel@tonic-gate 	    vga_get_atr(&softc->regs, VGA_BLACK));
12867c478bd9Sstevel@tonic-gate }
12877c478bd9Sstevel@tonic-gate #endif
12887c478bd9Sstevel@tonic-gate 
12892269adc8Sszhou static char vga_fontslot = 0;
12902269adc8Sszhou 
12917c478bd9Sstevel@tonic-gate static void
12927c478bd9Sstevel@tonic-gate vgatext_setfont(struct vgatext_softc *softc)
12937c478bd9Sstevel@tonic-gate {
12942269adc8Sszhou 	static uchar_t fsreg[8] = {0x0, 0x30, 0x5, 0x35, 0xa, 0x3a, 0xf, 0x3f};
12952269adc8Sszhou 
12967c478bd9Sstevel@tonic-gate 	extern unsigned char *ENCODINGS[];
12972269adc8Sszhou 	uchar_t *from;
12982269adc8Sszhou 	uchar_t volatile *to;
12992269adc8Sszhou 	int	i, j, s;
13002269adc8Sszhou 	int	bpc, f_offset;
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	/* Sync-reset the sequencer registers */
13037c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x00, 0x01);
13047c478bd9Sstevel@tonic-gate 	/*
13057c478bd9Sstevel@tonic-gate 	 *  enable write to plane2, since fonts
13067c478bd9Sstevel@tonic-gate 	 * could only be loaded into plane2
13077c478bd9Sstevel@tonic-gate 	 */
13087c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x02, 0x04);
13097c478bd9Sstevel@tonic-gate 	/*
13107c478bd9Sstevel@tonic-gate 	 *  sequentially access data in the bit map being
13117c478bd9Sstevel@tonic-gate 	 * selected by MapMask register (index 0x02)
13127c478bd9Sstevel@tonic-gate 	 */
13137c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x04, 0x07);
13147c478bd9Sstevel@tonic-gate 	/* Sync-reset ended, and allow the sequencer to operate */
13157c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x00, 0x03);
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	/*
13187c478bd9Sstevel@tonic-gate 	 *  select plane 2 on Read Mode 0
13197c478bd9Sstevel@tonic-gate 	 */
13207c478bd9Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x04, 0x02);
13217c478bd9Sstevel@tonic-gate 	/*
13227c478bd9Sstevel@tonic-gate 	 *  system addresses sequentially access data, follow
13237c478bd9Sstevel@tonic-gate 	 * Memory Mode register bit 2 in the sequencer
13247c478bd9Sstevel@tonic-gate 	 */
13257c478bd9Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x05, 0x00);
13267c478bd9Sstevel@tonic-gate 	/*
13277c478bd9Sstevel@tonic-gate 	 * set range of host memory addresses decoded by VGA
13287c478bd9Sstevel@tonic-gate 	 * hardware -- A0000h-BFFFFh (128K region)
13297c478bd9Sstevel@tonic-gate 	 */
13307c478bd9Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x06, 0x00);
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	/*
13337c478bd9Sstevel@tonic-gate 	 * This assumes 8x16 characters, which yield the traditional 80x25
13347c478bd9Sstevel@tonic-gate 	 * screen.  It really should support other character heights.
13357c478bd9Sstevel@tonic-gate 	 */
13367c478bd9Sstevel@tonic-gate 	bpc = 16;
13372269adc8Sszhou 	s = vga_fontslot;
13382269adc8Sszhou 	f_offset = s * 8 * 1024;
13397c478bd9Sstevel@tonic-gate 	for (i = 0; i < 256; i++) {
13407c478bd9Sstevel@tonic-gate 		from = ENCODINGS[i];
13412269adc8Sszhou 		to = (unsigned char *)softc->fb.addr + f_offset + i * 0x20;
13427c478bd9Sstevel@tonic-gate 		for (j = 0; j < bpc; j++)
13437c478bd9Sstevel@tonic-gate 			*to++ = *from++;
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	/* Sync-reset the sequencer registers */
13477c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x00, 0x01);
13487c478bd9Sstevel@tonic-gate 	/* enable write to plane 0 and 1 */
13497c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x02, 0x03);
13507c478bd9Sstevel@tonic-gate 	/*
13517c478bd9Sstevel@tonic-gate 	 * enable character map selection
13527c478bd9Sstevel@tonic-gate 	 * and odd/even addressing
13537c478bd9Sstevel@tonic-gate 	 */
13547c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x04, 0x03);
13557c478bd9Sstevel@tonic-gate 	/*
13562269adc8Sszhou 	 * select font map
13577c478bd9Sstevel@tonic-gate 	 */
13582269adc8Sszhou 	vga_set_seq(&softc->regs, 0x03, fsreg[s]);
13597c478bd9Sstevel@tonic-gate 	/* Sync-reset ended, and allow the sequencer to operate */
13607c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x00, 0x03);
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	/* restore graphic registers */
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	/* select plane 0 */
13657c478bd9Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x04, 0x00);
13667c478bd9Sstevel@tonic-gate 	/* enable odd/even addressing mode */
13677c478bd9Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x05, 0x10);
13687c478bd9Sstevel@tonic-gate 	/*
13697c478bd9Sstevel@tonic-gate 	 * range of host memory addresses decoded by VGA
13707c478bd9Sstevel@tonic-gate 	 * hardware -- B8000h-BFFFFh (32K region)
13717c478bd9Sstevel@tonic-gate 	 */
13727c478bd9Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x06, 0x0e);
13737c478bd9Sstevel@tonic-gate 	/* enable all color plane */
13747c478bd9Sstevel@tonic-gate 	vga_set_atr(&softc->regs, 0x12, 0x0f);
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate }
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate static void
13797c478bd9Sstevel@tonic-gate vgatext_save_colormap(struct vgatext_softc *softc)
13807c478bd9Sstevel@tonic-gate {
13817c478bd9Sstevel@tonic-gate 	int i;
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	for (i = 0; i < VGA_ATR_NUM_PLT; i++) {
13847c478bd9Sstevel@tonic-gate 		softc->attrib_palette[i] = vga_get_atr(&softc->regs, i);
13857c478bd9Sstevel@tonic-gate 	}
13867c478bd9Sstevel@tonic-gate 	for (i = 0; i < VGA8_CMAP_ENTRIES; i++) {
13877c478bd9Sstevel@tonic-gate 		vga_get_cmap(&softc->regs, i,
13887c478bd9Sstevel@tonic-gate 		    &softc->colormap[i].red,
13897c478bd9Sstevel@tonic-gate 		    &softc->colormap[i].green,
13907c478bd9Sstevel@tonic-gate 		    &softc->colormap[i].blue);
13917c478bd9Sstevel@tonic-gate 	}
13927c478bd9Sstevel@tonic-gate }
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate static void
13957c478bd9Sstevel@tonic-gate vgatext_restore_colormap(struct vgatext_softc *softc)
13967c478bd9Sstevel@tonic-gate {
13977c478bd9Sstevel@tonic-gate 	int i;
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	for (i = 0; i < VGA_ATR_NUM_PLT; i++) {
14007c478bd9Sstevel@tonic-gate 		vga_set_atr(&softc->regs, i, softc->attrib_palette[i]);
14017c478bd9Sstevel@tonic-gate 	}
14027c478bd9Sstevel@tonic-gate 	for (i = 0; i < VGA8_CMAP_ENTRIES; i++) {
14037c478bd9Sstevel@tonic-gate 		vga_put_cmap(&softc->regs, i,
14047c478bd9Sstevel@tonic-gate 		    softc->colormap[i].red,
14057c478bd9Sstevel@tonic-gate 		    softc->colormap[i].green,
14067c478bd9Sstevel@tonic-gate 		    softc->colormap[i].blue);
14077c478bd9Sstevel@tonic-gate 	}
14087c478bd9Sstevel@tonic-gate }
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate /*
14117c478bd9Sstevel@tonic-gate  * search the entries of the "reg" property for one which has the desired
14127c478bd9Sstevel@tonic-gate  * combination of phys_hi bits and contains the desired address.
14137c478bd9Sstevel@tonic-gate  *
14147c478bd9Sstevel@tonic-gate  * This version searches a PCI-style "reg" property.  It was prompted by
14157c478bd9Sstevel@tonic-gate  * issues surrounding the presence or absence of an entry for the ROM:
14167c478bd9Sstevel@tonic-gate  * (a) a transition problem with PowerPC Virtual Open Firmware
14177c478bd9Sstevel@tonic-gate  * (b) uncertainty as to whether an entry will be included on a device
14187c478bd9Sstevel@tonic-gate  *     with ROM support (and so an "active" ROM base address register),
14197c478bd9Sstevel@tonic-gate  *     but no ROM actually installed.
14207c478bd9Sstevel@tonic-gate  *
14217c478bd9Sstevel@tonic-gate  * See the note below on vgatext_get_isa_reg_index for the reasons for
14227c478bd9Sstevel@tonic-gate  * returning the offset.
14237c478bd9Sstevel@tonic-gate  *
14247c478bd9Sstevel@tonic-gate  * Note that this routine may not be fully general; it is intended for the
14257c478bd9Sstevel@tonic-gate  * specific purpose of finding a couple of particular VGA reg entries and
14267c478bd9Sstevel@tonic-gate  * may not be suitable for all reg-searching purposes.
14277c478bd9Sstevel@tonic-gate  */
14287c478bd9Sstevel@tonic-gate static int
14297c478bd9Sstevel@tonic-gate vgatext_get_pci_reg_index(
14307c478bd9Sstevel@tonic-gate 	dev_info_t *const devi,
14317c478bd9Sstevel@tonic-gate 	unsigned long himask,
14327c478bd9Sstevel@tonic-gate 	unsigned long hival,
14337c478bd9Sstevel@tonic-gate 	unsigned long addr,
14347c478bd9Sstevel@tonic-gate 	off_t *offset)
14357c478bd9Sstevel@tonic-gate {
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	int			length, index;
14387c478bd9Sstevel@tonic-gate 	pci_regspec_t	*reg;
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
14417c478bd9Sstevel@tonic-gate 	    "reg", (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
14427c478bd9Sstevel@tonic-gate 		return (-1);
14437c478bd9Sstevel@tonic-gate 	}
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	for (index = 0; index < length / sizeof (pci_regspec_t); index++) {
14467c478bd9Sstevel@tonic-gate 		if ((reg[index].pci_phys_hi & himask) != hival)
14477c478bd9Sstevel@tonic-gate 			continue;
14487c478bd9Sstevel@tonic-gate 		if (reg[index].pci_size_hi != 0)
14497c478bd9Sstevel@tonic-gate 			continue;
14507c478bd9Sstevel@tonic-gate 		if (reg[index].pci_phys_mid != 0)
14517c478bd9Sstevel@tonic-gate 			continue;
14527c478bd9Sstevel@tonic-gate 		if (reg[index].pci_phys_low > addr)
14537c478bd9Sstevel@tonic-gate 			continue;
14547c478bd9Sstevel@tonic-gate 		if (reg[index].pci_phys_low + reg[index].pci_size_low <= addr)
14557c478bd9Sstevel@tonic-gate 			continue;
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 		*offset = addr - reg[index].pci_phys_low;
14587c478bd9Sstevel@tonic-gate 		kmem_free(reg, (size_t)length);
14597c478bd9Sstevel@tonic-gate 		return (index);
14607c478bd9Sstevel@tonic-gate 	}
14617c478bd9Sstevel@tonic-gate 	kmem_free(reg, (size_t)length);
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	return (-1);
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate /*
14677c478bd9Sstevel@tonic-gate  * search the entries of the "reg" property for one which has the desired
14687c478bd9Sstevel@tonic-gate  * combination of phys_hi bits and contains the desired address.
14697c478bd9Sstevel@tonic-gate  *
14707c478bd9Sstevel@tonic-gate  * This version searches a ISA-style "reg" property.  It was prompted by
14717c478bd9Sstevel@tonic-gate  * issues surrounding 8514/A support.  By IEEE 1275 compatibility conventions,
14727c478bd9Sstevel@tonic-gate  * 8514/A registers should have been added after all standard VGA registers.
14737c478bd9Sstevel@tonic-gate  * Unfortunately, the Solaris/Intel device configuration framework
14747c478bd9Sstevel@tonic-gate  * (a) lists the 8514/A registers before the video memory, and then
14757c478bd9Sstevel@tonic-gate  * (b) also sorts the entries so that I/O entries come before memory
14767c478bd9Sstevel@tonic-gate  *     entries.
14777c478bd9Sstevel@tonic-gate  *
14787c478bd9Sstevel@tonic-gate  * It returns the "reg" index and offset into that register set.
14797c478bd9Sstevel@tonic-gate  * The offset is needed because there exist (broken?) BIOSes that
14807c478bd9Sstevel@tonic-gate  * report larger ranges enclosing the standard ranges.  One reports
14817c478bd9Sstevel@tonic-gate  * 0x3bf for 0x21 instead of 0x3c0 for 0x20, for instance.  Using the
14827c478bd9Sstevel@tonic-gate  * offset adjusts for this difference in the base of the register set.
14837c478bd9Sstevel@tonic-gate  *
14847c478bd9Sstevel@tonic-gate  * Note that this routine may not be fully general; it is intended for the
14857c478bd9Sstevel@tonic-gate  * specific purpose of finding a couple of particular VGA reg entries and
14867c478bd9Sstevel@tonic-gate  * may not be suitable for all reg-searching purposes.
14877c478bd9Sstevel@tonic-gate  */
14887c478bd9Sstevel@tonic-gate static int
14897c478bd9Sstevel@tonic-gate vgatext_get_isa_reg_index(
14907c478bd9Sstevel@tonic-gate 	dev_info_t *const devi,
14917c478bd9Sstevel@tonic-gate 	unsigned long hival,
14927c478bd9Sstevel@tonic-gate 	unsigned long addr,
14937c478bd9Sstevel@tonic-gate 	off_t *offset)
14947c478bd9Sstevel@tonic-gate {
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	int		length, index;
14977c478bd9Sstevel@tonic-gate 	struct regspec	*reg;
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
15007c478bd9Sstevel@tonic-gate 	    "reg", (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
15017c478bd9Sstevel@tonic-gate 		return (-1);
15027c478bd9Sstevel@tonic-gate 	}
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	for (index = 0; index < length / sizeof (struct regspec); index++) {
15057c478bd9Sstevel@tonic-gate 		if (reg[index].regspec_bustype != hival)
15067c478bd9Sstevel@tonic-gate 			continue;
15077c478bd9Sstevel@tonic-gate 		if (reg[index].regspec_addr > addr)
15087c478bd9Sstevel@tonic-gate 			continue;
15097c478bd9Sstevel@tonic-gate 		if (reg[index].regspec_addr + reg[index].regspec_size <= addr)
15107c478bd9Sstevel@tonic-gate 			continue;
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 		*offset = addr - reg[index].regspec_addr;
15137c478bd9Sstevel@tonic-gate 		kmem_free(reg, (size_t)length);
15147c478bd9Sstevel@tonic-gate 		return (index);
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate 	kmem_free(reg, (size_t)length);
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	return (-1);
15197c478bd9Sstevel@tonic-gate }
1520