xref: /onnv-gate/usr/src/uts/sun4u/io/ppm/jbusppm.c (revision 7656:2621e50fdf4a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
6*7656SSherry.Moore@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7656SSherry.Moore@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/conf.h>
290Sstevel@tonic-gate #include <sys/open.h>
300Sstevel@tonic-gate #include <sys/modctl.h>
310Sstevel@tonic-gate #include <sys/promif.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
340Sstevel@tonic-gate #include <sys/jbusppm.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  *	JBus Power Management Driver
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  *	jbusppm driver initiates the JBus clock speed change
420Sstevel@tonic-gate  *	as part of the protocol to adjust the clock speed on
430Sstevel@tonic-gate  *	all JBus resident devices.
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  *	jbusppm driver is loaded because of the explicit dependency
460Sstevel@tonic-gate  *	defined in PPM driver.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * Configuration Function prototypes and data structures
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate static int	jbppm_attach(dev_info_t *, ddi_attach_cmd_t);
530Sstevel@tonic-gate static int	jbppm_detach(dev_info_t *, ddi_detach_cmd_t);
540Sstevel@tonic-gate static int	jbppm_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
550Sstevel@tonic-gate static int	jbppm_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
560Sstevel@tonic-gate static int	jbppm_close(dev_t dev, int flag, int otyp, cred_t *cred_p);
570Sstevel@tonic-gate static int	jbppm_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * Configuration data structures
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate static struct cb_ops jbppm_cbops = {
630Sstevel@tonic-gate 	jbppm_open,		/* open */
640Sstevel@tonic-gate 	jbppm_close,		/* close */
650Sstevel@tonic-gate 	nodev,			/* strategy */
660Sstevel@tonic-gate 	nodev,			/* print */
670Sstevel@tonic-gate 	nodev,			/* dump */
680Sstevel@tonic-gate 	nodev,			/* read */
690Sstevel@tonic-gate 	nodev,			/* write */
700Sstevel@tonic-gate 	jbppm_ioctl,		/* ioctl */
710Sstevel@tonic-gate 	nodev,			/* devmap */
720Sstevel@tonic-gate 	nodev,			/* mmap */
730Sstevel@tonic-gate 	nodev,			/* segmap */
740Sstevel@tonic-gate 	nochpoll,		/* chpoll */
750Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
760Sstevel@tonic-gate 	NULL,			/* stream */
770Sstevel@tonic-gate 	D_MP | D_NEW,		/* flag */
780Sstevel@tonic-gate 	CB_REV,			/* rev */
790Sstevel@tonic-gate 	nodev,			/* aread */
800Sstevel@tonic-gate 	nodev,			/* awrite */
810Sstevel@tonic-gate };
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static struct dev_ops jbppm_ops = {
840Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
850Sstevel@tonic-gate 	0,			/* refcnt */
860Sstevel@tonic-gate 	jbppm_getinfo,		/* getinfo */
870Sstevel@tonic-gate 	nulldev,		/* identify */
880Sstevel@tonic-gate 	nulldev,		/* probe */
890Sstevel@tonic-gate 	jbppm_attach,		/* attach */
900Sstevel@tonic-gate 	jbppm_detach,		/* detach */
910Sstevel@tonic-gate 	nodev,			/* reset */
920Sstevel@tonic-gate 	&jbppm_cbops,		/* cb_ops */
930Sstevel@tonic-gate 	NULL,			/* bus_ops */
94*7656SSherry.Moore@Sun.COM 	NULL,			/* power */
95*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_supported,	/* devo_quiesce */
960Sstevel@tonic-gate };
970Sstevel@tonic-gate 
980Sstevel@tonic-gate extern struct mod_ops mod_driverops;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate static struct modldrv modldrv = {
1010Sstevel@tonic-gate 	&mod_driverops,
102*7656SSherry.Moore@Sun.COM 	"JBus ppm driver",
1030Sstevel@tonic-gate 	&jbppm_ops,
1040Sstevel@tonic-gate };
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static struct modlinkage modlinkage = {
1070Sstevel@tonic-gate 	MODREV_1,
1080Sstevel@tonic-gate 	&modldrv,
1090Sstevel@tonic-gate 	NULL
1100Sstevel@tonic-gate };
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * Local functions
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate static void jbppm_next_speed(dev_info_t *, uint_t);
1160Sstevel@tonic-gate static int jbppm_start_next(dev_info_t *, int);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * Driver global variables
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  * jbppm_lock synchronize the access of lyr handle to each jbppm
1220Sstevel@tonic-gate  * minor device, therefore write to tomatillo device is
1230Sstevel@tonic-gate  * sequentialized.  Lyr protocol requires pairing up lyr open
1240Sstevel@tonic-gate  * and close, so only a single reference is allowed per minor node.
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate static void	*jbppm_statep;
1270Sstevel@tonic-gate static kmutex_t  jbppm_lock;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * bit masks to scale the IO bridge clock in sync with and only with
1310Sstevel@tonic-gate  * scaling CPU clock.
1320Sstevel@tonic-gate  *
1330Sstevel@tonic-gate  * The array index indicates power level (from lowest to highest).
1340Sstevel@tonic-gate  */
1350Sstevel@tonic-gate static const uint64_t jbus_clock_masks[] = {
1360Sstevel@tonic-gate 	JBUS_ESTAR_CNTL_32,
1370Sstevel@tonic-gate 	JBUS_ESTAR_CNTL_2,
1380Sstevel@tonic-gate 	JBUS_ESTAR_CNTL_1
1390Sstevel@tonic-gate };
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate int
_init(void)1420Sstevel@tonic-gate _init(void)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	int	error;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	if ((error = ddi_soft_state_init(&jbppm_statep,
1470Sstevel@tonic-gate 	    sizeof (jbppm_unit), 0)) != DDI_SUCCESS) {
1480Sstevel@tonic-gate 		return (error);
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	mutex_init(&jbppm_lock, NULL, MUTEX_DRIVER, NULL);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != DDI_SUCCESS) {
1540Sstevel@tonic-gate 		mutex_destroy(&jbppm_lock);
1550Sstevel@tonic-gate 		ddi_soft_state_fini(&jbppm_statep);
1560Sstevel@tonic-gate 		return (error);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	return (error);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate int
_fini(void)1630Sstevel@tonic-gate _fini(void)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	int	error;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if ((error = mod_remove(&modlinkage)) == DDI_SUCCESS) {
1680Sstevel@tonic-gate 		mutex_destroy(&jbppm_lock);
1690Sstevel@tonic-gate 		ddi_soft_state_fini(&jbppm_statep);
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	return (error);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1770Sstevel@tonic-gate _info(struct modinfo *modinfop)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate  * Driver attach(9e) entry point
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate static int
jbppm_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)1880Sstevel@tonic-gate jbppm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	char	*str = "jbppm_attach";
1910Sstevel@tonic-gate 	int		instance;
1920Sstevel@tonic-gate 	jbppm_unit	*unitp;
1930Sstevel@tonic-gate 	uint64_t	data64;
1940Sstevel@tonic-gate 	ddi_device_acc_attr_t	attr;
1950Sstevel@tonic-gate 	int		rv = DDI_SUCCESS;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	switch (cmd) {
1980Sstevel@tonic-gate 	case DDI_ATTACH:
1990Sstevel@tonic-gate 		break;
2000Sstevel@tonic-gate 	case DDI_RESUME:
2010Sstevel@tonic-gate 		return (DDI_SUCCESS);
2020Sstevel@tonic-gate 	default:
2030Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: cmd %d unsupported.\n", str, cmd);
2040Sstevel@tonic-gate 		return (DDI_FAILURE);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2080Sstevel@tonic-gate 	rv = ddi_soft_state_zalloc(jbppm_statep, instance);
2090Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
2100Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: failed alloc for dev(%s@%s)",
2110Sstevel@tonic-gate 		    str, ddi_binding_name(dip),
2120Sstevel@tonic-gate 		    ddi_get_name_addr(dip) ? ddi_get_name_addr(dip) : " ");
2130Sstevel@tonic-gate 		return (rv);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if ((unitp = ddi_get_soft_state(jbppm_statep, instance)) == NULL) {
2170Sstevel@tonic-gate 		rv = DDI_FAILURE;
2180Sstevel@tonic-gate 		goto doerrs;
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/*
2220Sstevel@tonic-gate 	 * Export "ddi-kernel-ioctl" property - prepared to support
2230Sstevel@tonic-gate 	 * kernel ioctls (driver layering).
2240Sstevel@tonic-gate 	 */
2250Sstevel@tonic-gate 	rv = ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
2260Sstevel@tonic-gate 	    DDI_KERNEL_IOCTL, NULL, 0);
2270Sstevel@tonic-gate 	if (rv != DDI_PROP_SUCCESS)
2280Sstevel@tonic-gate 		goto doerrs;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	ddi_report_dev(dip);
2310Sstevel@tonic-gate 	unitp->dip = dip;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
2340Sstevel@tonic-gate 	attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
2350Sstevel@tonic-gate 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	rv = ddi_regs_map_setup(dip, 0, (caddr_t *)&unitp->devid_csr, 0, 8,
2380Sstevel@tonic-gate 	    &attr, &unitp->devid_hndl);
2390Sstevel@tonic-gate 	if (rv != DDI_SUCCESS)
2400Sstevel@tonic-gate 		goto doerrs;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	rv = ddi_regs_map_setup(dip, 1, (caddr_t *)&unitp->estar_csr, 0, 16,
2430Sstevel@tonic-gate 	    &attr, &unitp->estar_hndl);
2440Sstevel@tonic-gate 	if (rv != DDI_SUCCESS)
2450Sstevel@tonic-gate 		goto doerrs;
2460Sstevel@tonic-gate 	unitp->j_chng_csr = (uint64_t *)((caddr_t)unitp->estar_csr +
2470Sstevel@tonic-gate 	    J_CHNG_INITIATION_OFFSET);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	data64 = ddi_get64(unitp->devid_hndl, (uint64_t *)unitp->devid_csr);
2500Sstevel@tonic-gate 	unitp->is_master = (data64 & MASTER_IOBRIDGE_BIT) ? 1 : 0;
2510Sstevel@tonic-gate 	unitp->lyropen = 0;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	/*
2540Sstevel@tonic-gate 	 * create minor node for kernel_ioctl calls
2550Sstevel@tonic-gate 	 */
2560Sstevel@tonic-gate 	rv = ddi_create_minor_node(dip, "jbus-ppm", S_IFCHR, instance, 0, 0);
2570Sstevel@tonic-gate 	if (rv != DDI_SUCCESS)
2580Sstevel@tonic-gate 		goto doerrs;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	return (rv);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate doerrs:
2630Sstevel@tonic-gate 	if (unitp->devid_hndl != NULL)
2640Sstevel@tonic-gate 		ddi_regs_map_free(&unitp->devid_hndl);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if (unitp->estar_csr != NULL)
2670Sstevel@tonic-gate 		ddi_regs_map_free(&unitp->estar_hndl);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS |
2700Sstevel@tonic-gate 	    DDI_PROP_NOTPROM, DDI_KERNEL_IOCTL))
2710Sstevel@tonic-gate 		ddi_prop_remove_all(dip);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	ddi_soft_state_free(jbppm_statep, instance);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	return (rv);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate  * Driver getinfo(9e) entry routine
2810Sstevel@tonic-gate  */
2820Sstevel@tonic-gate /* ARGSUSED */
2830Sstevel@tonic-gate static int
jbppm_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)2840Sstevel@tonic-gate jbppm_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	jbppm_unit	*unitp;
2870Sstevel@tonic-gate 	int		instance;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	switch (cmd) {
2900Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2910Sstevel@tonic-gate 		instance = getminor((dev_t)arg);
2920Sstevel@tonic-gate 		unitp = ddi_get_soft_state(jbppm_statep, instance);
2930Sstevel@tonic-gate 		if (unitp == NULL) {
2940Sstevel@tonic-gate 			return (DDI_FAILURE);
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate 		*result = (void *) unitp->dip;
2970Sstevel@tonic-gate 		return (DDI_SUCCESS);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
3000Sstevel@tonic-gate 		instance = getminor((dev_t)arg);
3010Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
3020Sstevel@tonic-gate 		return (DDI_SUCCESS);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	default:
3050Sstevel@tonic-gate 		return (DDI_FAILURE);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * detach(9e)
3120Sstevel@tonic-gate  */
3130Sstevel@tonic-gate /* ARGSUSED */
3140Sstevel@tonic-gate static int
jbppm_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)3150Sstevel@tonic-gate jbppm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3160Sstevel@tonic-gate {
3170Sstevel@tonic-gate 	char *str = "jbppm_detach";
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	switch (cmd) {
3200Sstevel@tonic-gate 	case DDI_DETACH:
3210Sstevel@tonic-gate 		return (DDI_FAILURE);
3220Sstevel@tonic-gate 	case DDI_SUSPEND:
3230Sstevel@tonic-gate 		return (DDI_SUCCESS);
3240Sstevel@tonic-gate 	default:
3250Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: cmd %d unsupported", str, cmd);
3260Sstevel@tonic-gate 		return (DDI_FAILURE);
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate /* ARGSUSED */
3320Sstevel@tonic-gate static int
jbppm_open(dev_t * dev_p,int flag,int otyp,cred_t * cred_p)3330Sstevel@tonic-gate jbppm_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	jbppm_unit	*unitp;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	/* not intended to allow sysadmin level root process to open it */
3380Sstevel@tonic-gate 	if (drv_priv(cred_p) != DDI_SUCCESS)
3390Sstevel@tonic-gate 		return (EPERM);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	if ((unitp = ddi_get_soft_state(
3420Sstevel@tonic-gate 	    jbppm_statep, getminor(*dev_p))) == NULL) {
3430Sstevel@tonic-gate 		cmn_err(CE_WARN, "jbppm_open: failed to get soft state!");
3440Sstevel@tonic-gate 		return (DDI_FAILURE);
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	mutex_enter(&jbppm_lock);
3480Sstevel@tonic-gate 	if (unitp->lyropen != 0) {
3490Sstevel@tonic-gate 		mutex_exit(&jbppm_lock);
3500Sstevel@tonic-gate 		return (EBUSY);
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 	unitp->lyropen++;
3530Sstevel@tonic-gate 	mutex_exit(&jbppm_lock);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	return (DDI_SUCCESS);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate /* ARGSUSED */
3600Sstevel@tonic-gate static int
jbppm_close(dev_t dev,int flag,int otyp,cred_t * cred_p)3610Sstevel@tonic-gate jbppm_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	jbppm_unit	*unitp;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	if ((unitp =
3660Sstevel@tonic-gate 	    ddi_get_soft_state(jbppm_statep, getminor(dev))) == NULL)
3670Sstevel@tonic-gate 		return (DDI_FAILURE);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	mutex_enter(&jbppm_lock);
3700Sstevel@tonic-gate 	unitp->lyropen = 0;
3710Sstevel@tonic-gate 	mutex_exit(&jbppm_lock);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	return (DDI_SUCCESS);
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate #define	JBPPMIOC		('j' << 8)
3780Sstevel@tonic-gate #define	JBPPMIOC_ISMASTER	(JBPPMIOC | 1)	/* no 'arg' */
3790Sstevel@tonic-gate #define	JBPPMIOC_NEXT		(JBPPMIOC | 2)	/* 'arg': next speed level */
3800Sstevel@tonic-gate #define	JBPPMIOC_GO		(JBPPMIOC | 3)	/* 'arg': jbus chng_delay */
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate /* ARGSUSED3 */
3830Sstevel@tonic-gate static int
jbppm_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * cred_p,int * rval_p)3840Sstevel@tonic-gate jbppm_ioctl(dev_t dev, int cmd, intptr_t arg, int flag,
3850Sstevel@tonic-gate     cred_t *cred_p, int *rval_p)
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	jbppm_unit	*unitp;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (drv_priv(cred_p) != 0)
3900Sstevel@tonic-gate 		return (EPERM);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if ((unitp =
3930Sstevel@tonic-gate 	    ddi_get_soft_state(jbppm_statep, getminor(dev))) == NULL)
3940Sstevel@tonic-gate 		return (EIO);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	switch (cmd) {
3970Sstevel@tonic-gate 	case JBPPMIOC_ISMASTER:
3980Sstevel@tonic-gate 		if (unitp->is_master)
3990Sstevel@tonic-gate 			return (0);
4000Sstevel@tonic-gate 		else
4010Sstevel@tonic-gate 			return (-1);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	case  JBPPMIOC_NEXT:
4040Sstevel@tonic-gate 		jbppm_next_speed(unitp->dip, (uint_t)arg);
4050Sstevel@tonic-gate 		return (0);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	case  JBPPMIOC_GO:
4080Sstevel@tonic-gate 		if (!unitp->is_master)
4090Sstevel@tonic-gate 			return (EINVAL);
4100Sstevel@tonic-gate 		return (jbppm_start_next(unitp->dip, (int)arg));
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	default:
4130Sstevel@tonic-gate 		return (ENOTTY);
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate /*
4190Sstevel@tonic-gate  * jbppm_next_speed - program a new speed into IO bridge device prior to
4200Sstevel@tonic-gate  * actual speed transition.
4210Sstevel@tonic-gate  */
4220Sstevel@tonic-gate static void
jbppm_next_speed(dev_info_t * dip,uint_t lvl_index)4230Sstevel@tonic-gate jbppm_next_speed(dev_info_t *dip, uint_t lvl_index)
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate 	volatile uint64_t	data64;
4260Sstevel@tonic-gate 	static jbppm_unit	*unitp;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	unitp = ddi_get_soft_state(jbppm_statep, ddi_get_instance(dip));
4290Sstevel@tonic-gate 	ASSERT(unitp);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	mutex_enter(&jbppm_lock);
4320Sstevel@tonic-gate 	data64 = ddi_get64(unitp->estar_hndl, unitp->estar_csr);
4330Sstevel@tonic-gate 	data64 &= ~JBUS_ESTAR_CNTL_MASK;
4340Sstevel@tonic-gate 	data64 |= jbus_clock_masks[lvl_index];
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	ddi_put64(unitp->estar_hndl, (uint64_t *)unitp->estar_csr, data64);
4370Sstevel@tonic-gate 	data64 = ddi_get64(unitp->estar_hndl, unitp->estar_csr);
4380Sstevel@tonic-gate 	mutex_exit(&jbppm_lock);
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate /*
4430Sstevel@tonic-gate  * jbppm_start_next - Initiate JBus speed change on all JBus devices.
4440Sstevel@tonic-gate  * chng_delay indicates after master deassert j_chng signal the number of
4450Sstevel@tonic-gate  * jbus clock delay before all jbus device start to transit to the new
4460Sstevel@tonic-gate  * speed.
4470Sstevel@tonic-gate  * Trigger sequence:
4480Sstevel@tonic-gate  *      wait while j_chng[1:0] == 10
4490Sstevel@tonic-gate  *	write 00 to j_chng
4500Sstevel@tonic-gate  *	trigger by writing 10 to j_chng[1:0]
4510Sstevel@tonic-gate  *	wait while j_chng[1:0] == 10
4520Sstevel@tonic-gate  *	write 00 to j_chng[1:0]
4530Sstevel@tonic-gate  * Note: this sequence is not the same as Enchilada spec described, chiefly
4540Sstevel@tonic-gate  * because else where (e.g. flush E$ code) may have speed change code. If sw
4550Sstevel@tonic-gate  * wait upon j_chng[1:0] == 11 in both places, we'll have problem.  That spec
4560Sstevel@tonic-gate  * requires wait on 11 to ensure that trigger has completed. An alternative
4570Sstevel@tonic-gate  * way to ensure that is to check and wait upon 10. J_chng[1:0] stays as 10
4580Sstevel@tonic-gate  * for only a short period of time that is under HW control, unlike 11 signals
4590Sstevel@tonic-gate  * which has to be cleared by sw.
4600Sstevel@tonic-gate  */
4610Sstevel@tonic-gate /* ARGSUSED */
4620Sstevel@tonic-gate static int
jbppm_start_next(dev_info_t * dip,int chng_delay)4630Sstevel@tonic-gate jbppm_start_next(dev_info_t *dip, int chng_delay)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate 	volatile uint64_t	data64;
4660Sstevel@tonic-gate 	static jbppm_unit	*unitp;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	unitp = ddi_get_soft_state(jbppm_statep, ddi_get_instance(dip));
4690Sstevel@tonic-gate 	ASSERT(unitp && unitp->is_master);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	mutex_enter(&jbppm_lock);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	/* wait while trigger is incomplete */
4740Sstevel@tonic-gate 	do {
4750Sstevel@tonic-gate 		data64 = ddi_get64(unitp->estar_hndl, unitp->j_chng_csr);
4760Sstevel@tonic-gate 	} while ((J_CHNG_INITIATION_MASK & data64) == J_CHNG_START);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/* clear(reset) */
4790Sstevel@tonic-gate 	data64 &= ~J_CHNG_INITIATION_MASK;
4800Sstevel@tonic-gate 	ddi_put64(unitp->estar_hndl, (uint64_t *)unitp->j_chng_csr, data64);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	/* trigger */
4830Sstevel@tonic-gate 	data64 &= ~J_CHNG_DELAY_MASK;
4840Sstevel@tonic-gate 	data64 |= (J_CHNG_START | chng_delay);
4850Sstevel@tonic-gate 	ddi_put64(unitp->estar_hndl, (uint64_t *)unitp->j_chng_csr, data64);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/* wait while trigger is incomplete */
4880Sstevel@tonic-gate 	do {
4890Sstevel@tonic-gate 		data64 = ddi_get64(unitp->estar_hndl, unitp->j_chng_csr);
4900Sstevel@tonic-gate 	} while ((J_CHNG_INITIATION_MASK & data64) == J_CHNG_START);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	/* clear(reset) */
4930Sstevel@tonic-gate 	data64 &= ~J_CHNG_INITIATION_MASK;
4940Sstevel@tonic-gate 	ddi_put64(unitp->estar_hndl, (uint64_t *)unitp->j_chng_csr, data64);
4950Sstevel@tonic-gate 	(void) ddi_get64(unitp->estar_hndl, unitp->j_chng_csr);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	mutex_exit(&jbppm_lock);
4980Sstevel@tonic-gate 	return (DDI_SUCCESS);
4990Sstevel@tonic-gate }
500