xref: /onnv-gate/usr/src/uts/sun4u/io/ppm/schppm.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 /*
280Sstevel@tonic-gate  *	Schizo Power Management Driver
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  *	This driver deals with Safari bus interface and it is used
310Sstevel@tonic-gate  *	as part of the protocol to change the clock speed on Safari bus.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  *	The routine on this driver is referenced by Platform Power
340Sstevel@tonic-gate  *	Management driver of systems like Excalibur.  Driver is
350Sstevel@tonic-gate  *	loaded because of an explicit dependency defined in PPM driver.
360Sstevel@tonic-gate  *	PPM driver also attaches the driver.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/conf.h>
410Sstevel@tonic-gate #include <sys/ddi.h>
420Sstevel@tonic-gate #include <sys/sunddi.h>
430Sstevel@tonic-gate #include <sys/modctl.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * Function prototypes
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate static int spm_attach(dev_info_t *, ddi_attach_cmd_t);
500Sstevel@tonic-gate static int spm_detach(dev_info_t *, ddi_detach_cmd_t);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * Private data for schizo_pm driver
540Sstevel@tonic-gate  */
55533Sjan struct spm_soft_state {
560Sstevel@tonic-gate 	dev_info_t		*dip;
570Sstevel@tonic-gate };
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * Configuration data structures
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate static struct dev_ops spm_ops = {
630Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
640Sstevel@tonic-gate 	0,			/* refcnt */
650Sstevel@tonic-gate 	nodev,			/* getinfo */
660Sstevel@tonic-gate 	nulldev,		/* identify */
670Sstevel@tonic-gate 	nulldev,		/* probe */
680Sstevel@tonic-gate 	spm_attach,		/* attach */
690Sstevel@tonic-gate 	spm_detach,		/* detach */
700Sstevel@tonic-gate 	nodev,			/* reset */
710Sstevel@tonic-gate 	(struct cb_ops *)0,	/* cb_ops */
720Sstevel@tonic-gate 	(struct bus_ops *)0,	/* bus_ops */
73*7656SSherry.Moore@Sun.COM 	NULL,			/* power */
74*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_supported,	/* devo_quiesce */
750Sstevel@tonic-gate };
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Driver globals
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate static void *spm_state;
810Sstevel@tonic-gate static int spm_inst = -1;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static struct modldrv modldrv = {
840Sstevel@tonic-gate 	&mod_driverops,			/* Type of module = driver */
85*7656SSherry.Moore@Sun.COM 	"schizo pm driver",	/* name of module */
860Sstevel@tonic-gate 	&spm_ops,			/* driver ops */
870Sstevel@tonic-gate };
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static struct modlinkage modlinkage = {
900Sstevel@tonic-gate 	MODREV_1,
910Sstevel@tonic-gate 	(void *)&modldrv,
920Sstevel@tonic-gate 	NULL
930Sstevel@tonic-gate };
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * Schizo CSR E* bit masks
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate #define	SCHIZO_SAFARI_ECLK_32	0x20ULL
990Sstevel@tonic-gate #define	SCHIZO_SAFARI_ECLK_2	0x2ULL
1000Sstevel@tonic-gate #define	SCHIZO_SAFARI_ECLK_1	0x1ULL
1010Sstevel@tonic-gate #define	SCHIZO_SAFARI_ECLK_MASK	(SCHIZO_SAFARI_ECLK_32 |	\
1020Sstevel@tonic-gate     SCHIZO_SAFARI_ECLK_2 | SCHIZO_SAFARI_ECLK_1)
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * bit masks to set schizo clock in parallel with setting cpu clock.
1060Sstevel@tonic-gate  * Used when changing cpu speeds.
1070Sstevel@tonic-gate  *
1080Sstevel@tonic-gate  * NOTE: The order of entries must be from slowest to fastest.
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate static const uint64_t schizo_safari_masks[] = {
1110Sstevel@tonic-gate 	SCHIZO_SAFARI_ECLK_32,
1120Sstevel@tonic-gate 	SCHIZO_SAFARI_ECLK_2,
1130Sstevel@tonic-gate 	SCHIZO_SAFARI_ECLK_1
1140Sstevel@tonic-gate };
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * Normally, the address of the registers we use would be accessed from
1180Sstevel@tonic-gate  * our "official" private data.  However, since the dip is not passed
1190Sstevel@tonic-gate  * in when spm_change_speed (see below) is called, and since there is
1200Sstevel@tonic-gate  * only one unit of the spm "device", we keep it here as a static.
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate static volatile uint64_t *spm_schizo_csr;
1230Sstevel@tonic-gate ddi_acc_handle_t	 spm_schizo_handle;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate int
_init(void)1260Sstevel@tonic-gate _init(void)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	int error;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	if ((error = ddi_soft_state_init(&spm_state,
1310Sstevel@tonic-gate 	    sizeof (struct spm_soft_state), 0)) != 0)
1320Sstevel@tonic-gate 		return (error);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0)
1350Sstevel@tonic-gate 		ddi_soft_state_fini(&spm_state);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	return (error);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate int
_fini(void)1410Sstevel@tonic-gate _fini(void)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	int error;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if ((error = mod_remove(&modlinkage)) == 0)
1460Sstevel@tonic-gate 		ddi_soft_state_fini(&spm_state);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	return (error);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1520Sstevel@tonic-gate _info(struct modinfo *modinfop)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate static int
spm_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)1580Sstevel@tonic-gate spm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	int rv;
1610Sstevel@tonic-gate 	struct spm_soft_state *softsp;
1620Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	switch (cmd) {
1650Sstevel@tonic-gate 	case DDI_ATTACH:
1660Sstevel@tonic-gate 		if (spm_inst != -1) {
1670Sstevel@tonic-gate 			cmn_err(CE_WARN, "spm_attach: "
1680Sstevel@tonic-gate 			    "only one instance is allowed.");
1690Sstevel@tonic-gate 			return (DDI_FAILURE);
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 		break;
1730Sstevel@tonic-gate 	case DDI_RESUME:
1740Sstevel@tonic-gate 		return (DDI_SUCCESS);
1750Sstevel@tonic-gate 	default:
1760Sstevel@tonic-gate 		return (DDI_FAILURE);
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	spm_inst = ddi_get_instance(dip);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(spm_state, spm_inst) != DDI_SUCCESS) {
1820Sstevel@tonic-gate 		cmn_err(CE_WARN, "spm_attach: can't allocate state.");
1830Sstevel@tonic-gate 		return (DDI_FAILURE);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if ((softsp = ddi_get_soft_state(spm_state, spm_inst)) == NULL) {
1870Sstevel@tonic-gate 		cmn_err(CE_WARN, "spm_attach: can't get state.");
1880Sstevel@tonic-gate 		return (DDI_FAILURE);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	softsp->dip = dip;
1920Sstevel@tonic-gate 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
1930Sstevel@tonic-gate 	attr.devacc_attr_endian_flags  = DDI_NEVERSWAP_ACC;
1940Sstevel@tonic-gate 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/*
1970Sstevel@tonic-gate 	 * Map the Safari E* Control register.
1980Sstevel@tonic-gate 	 */
1990Sstevel@tonic-gate 	rv = ddi_regs_map_setup(dip, 0,
2000Sstevel@tonic-gate 	    (caddr_t *)&spm_schizo_csr, 0, 8, &attr, &spm_schizo_handle);
2010Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
2020Sstevel@tonic-gate 		cmn_err(CE_WARN, "spm_attach: can't map the register.");
2030Sstevel@tonic-gate 		ddi_soft_state_free(spm_state, spm_inst);
2040Sstevel@tonic-gate 		return (rv);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	ddi_report_dev(dip);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	return (DDI_SUCCESS);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate /*ARGSUSED*/
2130Sstevel@tonic-gate static int
spm_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)2140Sstevel@tonic-gate spm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	switch (cmd) {
2170Sstevel@tonic-gate 	case DDI_SUSPEND:
2180Sstevel@tonic-gate 		return (DDI_SUCCESS);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	case DDI_DETACH:
2210Sstevel@tonic-gate 		return (DDI_FAILURE);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	default:
2240Sstevel@tonic-gate 		return (DDI_FAILURE);
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * This globally visible function is the main reason this driver exists.
2300Sstevel@tonic-gate  * It will be called by a platform power management driver to write to
2310Sstevel@tonic-gate  * the schizo ASIC csr which changes schizo's clock rate.  This is a
2320Sstevel@tonic-gate  * required step when changing the clock of the cpus.
2330Sstevel@tonic-gate  *
2340Sstevel@tonic-gate  * NOTE - The caller should enter this routine sequentially.
2350Sstevel@tonic-gate  */
2360Sstevel@tonic-gate void
spm_change_schizo_speed(int lvl_index)2370Sstevel@tonic-gate spm_change_schizo_speed(int lvl_index)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	uint64_t	contents;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	ASSERT(lvl_index >= 0 && lvl_index <= 2);
2420Sstevel@tonic-gate 	contents = ddi_get64(spm_schizo_handle, (uint64_t *)spm_schizo_csr);
2430Sstevel@tonic-gate 	contents &= ~SCHIZO_SAFARI_ECLK_MASK;
2440Sstevel@tonic-gate 	contents |= schizo_safari_masks[ lvl_index ];
2450Sstevel@tonic-gate 	ddi_put64(spm_schizo_handle, (uint64_t *)spm_schizo_csr, contents);
2460Sstevel@tonic-gate }
247