xref: /titanic_51/usr/src/uts/sun4u/io/ppm/schppm.c (revision 193974072f41a843678abf5f61979c748687e66b)
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
5*19397407SSherry Moore  * Common Development and Distribution License (the "License").
6*19397407SSherry Moore  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*19397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  *	Schizo Power Management Driver
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  *	This driver deals with Safari bus interface and it is used
317c478bd9Sstevel@tonic-gate  *	as part of the protocol to change the clock speed on Safari bus.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *	The routine on this driver is referenced by Platform Power
347c478bd9Sstevel@tonic-gate  *	Management driver of systems like Excalibur.  Driver is
357c478bd9Sstevel@tonic-gate  *	loaded because of an explicit dependency defined in PPM driver.
367c478bd9Sstevel@tonic-gate  *	PPM driver also attaches the driver.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/conf.h>
417c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
437c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * Function prototypes
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate static int spm_attach(dev_info_t *, ddi_attach_cmd_t);
507c478bd9Sstevel@tonic-gate static int spm_detach(dev_info_t *, ddi_detach_cmd_t);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * Private data for schizo_pm driver
547c478bd9Sstevel@tonic-gate  */
556c9bfa0bSjan struct spm_soft_state {
567c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Configuration data structures
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate static struct dev_ops spm_ops = {
637c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
647c478bd9Sstevel@tonic-gate 	0,			/* refcnt */
657c478bd9Sstevel@tonic-gate 	nodev,			/* getinfo */
667c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
677c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
687c478bd9Sstevel@tonic-gate 	spm_attach,		/* attach */
697c478bd9Sstevel@tonic-gate 	spm_detach,		/* detach */
707c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
717c478bd9Sstevel@tonic-gate 	(struct cb_ops *)0,	/* cb_ops */
727c478bd9Sstevel@tonic-gate 	(struct bus_ops *)0,	/* bus_ops */
73*19397407SSherry Moore 	NULL,			/* power */
74*19397407SSherry Moore 	ddi_quiesce_not_supported,	/* devo_quiesce */
757c478bd9Sstevel@tonic-gate };
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Driver globals
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate static void *spm_state;
817c478bd9Sstevel@tonic-gate static int spm_inst = -1;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
847c478bd9Sstevel@tonic-gate 	&mod_driverops,			/* Type of module = driver */
85*19397407SSherry Moore 	"schizo pm driver",	/* name of module */
867c478bd9Sstevel@tonic-gate 	&spm_ops,			/* driver ops */
877c478bd9Sstevel@tonic-gate };
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
907c478bd9Sstevel@tonic-gate 	MODREV_1,
917c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
927c478bd9Sstevel@tonic-gate 	NULL
937c478bd9Sstevel@tonic-gate };
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Schizo CSR E* bit masks
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate #define	SCHIZO_SAFARI_ECLK_32	0x20ULL
997c478bd9Sstevel@tonic-gate #define	SCHIZO_SAFARI_ECLK_2	0x2ULL
1007c478bd9Sstevel@tonic-gate #define	SCHIZO_SAFARI_ECLK_1	0x1ULL
1017c478bd9Sstevel@tonic-gate #define	SCHIZO_SAFARI_ECLK_MASK	(SCHIZO_SAFARI_ECLK_32 |	\
1027c478bd9Sstevel@tonic-gate     SCHIZO_SAFARI_ECLK_2 | SCHIZO_SAFARI_ECLK_1)
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * bit masks to set schizo clock in parallel with setting cpu clock.
1067c478bd9Sstevel@tonic-gate  * Used when changing cpu speeds.
1077c478bd9Sstevel@tonic-gate  *
1087c478bd9Sstevel@tonic-gate  * NOTE: The order of entries must be from slowest to fastest.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate static const uint64_t schizo_safari_masks[] = {
1117c478bd9Sstevel@tonic-gate 	SCHIZO_SAFARI_ECLK_32,
1127c478bd9Sstevel@tonic-gate 	SCHIZO_SAFARI_ECLK_2,
1137c478bd9Sstevel@tonic-gate 	SCHIZO_SAFARI_ECLK_1
1147c478bd9Sstevel@tonic-gate };
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Normally, the address of the registers we use would be accessed from
1187c478bd9Sstevel@tonic-gate  * our "official" private data.  However, since the dip is not passed
1197c478bd9Sstevel@tonic-gate  * in when spm_change_speed (see below) is called, and since there is
1207c478bd9Sstevel@tonic-gate  * only one unit of the spm "device", we keep it here as a static.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate static volatile uint64_t *spm_schizo_csr;
1237c478bd9Sstevel@tonic-gate ddi_acc_handle_t	 spm_schizo_handle;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate int
1267c478bd9Sstevel@tonic-gate _init(void)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	int error;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if ((error = ddi_soft_state_init(&spm_state,
1317c478bd9Sstevel@tonic-gate 	    sizeof (struct spm_soft_state), 0)) != 0)
1327c478bd9Sstevel@tonic-gate 		return (error);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0)
1357c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&spm_state);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	return (error);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate int
1417c478bd9Sstevel@tonic-gate _fini(void)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	int error;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	if ((error = mod_remove(&modlinkage)) == 0)
1467c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&spm_state);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	return (error);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate int
1527c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate static int
1587c478bd9Sstevel@tonic-gate spm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	int rv;
1617c478bd9Sstevel@tonic-gate 	struct spm_soft_state *softsp;
1627c478bd9Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	switch (cmd) {
1657c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
1667c478bd9Sstevel@tonic-gate 		if (spm_inst != -1) {
1677c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "spm_attach: "
1687c478bd9Sstevel@tonic-gate 			    "only one instance is allowed.");
1697c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		break;
1737c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
1747c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
1757c478bd9Sstevel@tonic-gate 	default:
1767c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	spm_inst = ddi_get_instance(dip);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(spm_state, spm_inst) != DDI_SUCCESS) {
1827c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "spm_attach: can't allocate state.");
1837c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if ((softsp = ddi_get_soft_state(spm_state, spm_inst)) == NULL) {
1877c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "spm_attach: can't get state.");
1887c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	softsp->dip = dip;
1927c478bd9Sstevel@tonic-gate 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
1937c478bd9Sstevel@tonic-gate 	attr.devacc_attr_endian_flags  = DDI_NEVERSWAP_ACC;
1947c478bd9Sstevel@tonic-gate 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/*
1977c478bd9Sstevel@tonic-gate 	 * Map the Safari E* Control register.
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate 	rv = ddi_regs_map_setup(dip, 0,
2007c478bd9Sstevel@tonic-gate 	    (caddr_t *)&spm_schizo_csr, 0, 8, &attr, &spm_schizo_handle);
2017c478bd9Sstevel@tonic-gate 	if (rv != DDI_SUCCESS) {
2027c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "spm_attach: can't map the register.");
2037c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(spm_state, spm_inst);
2047c478bd9Sstevel@tonic-gate 		return (rv);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2137c478bd9Sstevel@tonic-gate static int
2147c478bd9Sstevel@tonic-gate spm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	switch (cmd) {
2177c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
2187c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
2217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	default:
2247c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * This globally visible function is the main reason this driver exists.
2307c478bd9Sstevel@tonic-gate  * It will be called by a platform power management driver to write to
2317c478bd9Sstevel@tonic-gate  * the schizo ASIC csr which changes schizo's clock rate.  This is a
2327c478bd9Sstevel@tonic-gate  * required step when changing the clock of the cpus.
2337c478bd9Sstevel@tonic-gate  *
2347c478bd9Sstevel@tonic-gate  * NOTE - The caller should enter this routine sequentially.
2357c478bd9Sstevel@tonic-gate  */
2367c478bd9Sstevel@tonic-gate void
2377c478bd9Sstevel@tonic-gate spm_change_schizo_speed(int lvl_index)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	uint64_t	contents;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	ASSERT(lvl_index >= 0 && lvl_index <= 2);
2427c478bd9Sstevel@tonic-gate 	contents = ddi_get64(spm_schizo_handle, (uint64_t *)spm_schizo_csr);
2437c478bd9Sstevel@tonic-gate 	contents &= ~SCHIZO_SAFARI_ECLK_MASK;
2447c478bd9Sstevel@tonic-gate 	contents |= schizo_safari_masks[ lvl_index ];
2457c478bd9Sstevel@tonic-gate 	ddi_put64(spm_schizo_handle, (uint64_t *)spm_schizo_csr, contents);
2467c478bd9Sstevel@tonic-gate }
247