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
519397407SSherry Moore  * Common Development and Distribution License (the "License").
619397407SSherry 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 /*
2207d06da5SSurya Prakki  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
26*3fe80ca4SDan Cross /*
27*3fe80ca4SDan Cross  * Copyright 2023 Oxide Computer Company
28*3fe80ca4SDan Cross  */
29*3fe80ca4SDan Cross 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Platform Power Management driver for SUNW,Sun-Blade-1000
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
347c478bd9Sstevel@tonic-gate #include <sys/conf.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
387c478bd9Sstevel@tonic-gate #include <sys/ppmvar.h>
397c478bd9Sstevel@tonic-gate #include <sys/ppmio.h>
407c478bd9Sstevel@tonic-gate #include <sys/xcalppm_reg.h>
417c478bd9Sstevel@tonic-gate #include <sys/xcalppm_var.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <sys/epm.h>
447c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
457c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
467c478bd9Sstevel@tonic-gate #include <sys/cheetahregs.h>
477c478bd9Sstevel@tonic-gate #include <sys/us3_module.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Locking Considerations
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * To look at and/or modify xcppm_domain fields or elements of its list of
537c478bd9Sstevel@tonic-gate  * xcppm_dev structures the domain_lock for the affected domain must be held.
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * When the autopm framework needs to change the power of a component of a
567c478bd9Sstevel@tonic-gate  * device, it needs to hold the associated power lock (see discussion at
577c478bd9Sstevel@tonic-gate  * top of uts/common/os/sunpm.c).
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * If the framework needs to lock a dev/cmpt for a device which this ppm
607c478bd9Sstevel@tonic-gate  * has claimed, xcppm_ctlops will be called with PMR_PPM_LOCK_POWER.  Ppm
617c478bd9Sstevel@tonic-gate  * needs to be involved because, due to platform constraints, changing the
627c478bd9Sstevel@tonic-gate  * power of one device may require that other devices be changed in the same
637c478bd9Sstevel@tonic-gate  * operation.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * In some domains (e.g., cpus) the power lock must be acquired for all the
667c478bd9Sstevel@tonic-gate  * affected devices to avoid possible corruption of the power states.  The
677c478bd9Sstevel@tonic-gate  * joint change must be an atomic operation.  Ppm handles this by acquiring
687c478bd9Sstevel@tonic-gate  * the domain lock, then walking the list of affected devices and acquiring
697c478bd9Sstevel@tonic-gate  * the power lock for each of them.  To unlock, the list is traversed and
707c478bd9Sstevel@tonic-gate  * each of the power locks is freed, followed by freeing the domain lock.
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  * For other domains ppm will only be changing the power of a single device
737c478bd9Sstevel@tonic-gate  * that is known to the framework.  In these cases, the locking is done by
747c478bd9Sstevel@tonic-gate  * acquiring the domain lock and directly calling the framework routine for
757c478bd9Sstevel@tonic-gate  * getting a single power lock.
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static int	xcppm_attach(dev_info_t *, ddi_attach_cmd_t);
797c478bd9Sstevel@tonic-gate static int	xcppm_detach(dev_info_t *, ddi_detach_cmd_t);
807c478bd9Sstevel@tonic-gate static int	xcppm_ctlops(dev_info_t *, dev_info_t *,
817c478bd9Sstevel@tonic-gate 		    ddi_ctl_enum_t, void *, void *);
827c478bd9Sstevel@tonic-gate static void	xcppm_dev_init(ppm_dev_t *);
837c478bd9Sstevel@tonic-gate static void	xcppm_dev_fini(ppm_dev_t *);
847c478bd9Sstevel@tonic-gate static void	xcppm_iocset(uint8_t);
857c478bd9Sstevel@tonic-gate static uint8_t	xcppm_iocget(void);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Note: 1394 and pciupa were originally required to be LOCK_ALL domains.
897c478bd9Sstevel@tonic-gate  * However, the underlying nexus drivers aren't able to do power mgmt
907c478bd9Sstevel@tonic-gate  * (because of hw implementation issues).  The locking protocol for these
917c478bd9Sstevel@tonic-gate  * domains is changed to LOCK_ONE to simplify other code.  The domain
927c478bd9Sstevel@tonic-gate  * code itself will be removed in the future.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate static ppm_domain_t xcppm_1394 = { "domain_1394",	PPMD_LOCK_ONE };
957c478bd9Sstevel@tonic-gate static ppm_domain_t xcppm_cpu  = { "domain_cpu",	PPMD_LOCK_ALL };
967c478bd9Sstevel@tonic-gate static ppm_domain_t xcppm_fet  = { "domain_powerfet",	PPMD_LOCK_ONE };
977c478bd9Sstevel@tonic-gate static ppm_domain_t xcppm_upa  = { "domain_pciupa",	PPMD_LOCK_ONE };
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate ppm_domain_t *ppm_domains[] = {
1007c478bd9Sstevel@tonic-gate 	&xcppm_1394,
1017c478bd9Sstevel@tonic-gate 	&xcppm_cpu,
1027c478bd9Sstevel@tonic-gate 	&xcppm_fet,
1037c478bd9Sstevel@tonic-gate 	&xcppm_upa,
1047c478bd9Sstevel@tonic-gate 	NULL
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate struct ppm_funcs ppmf = {
1097c478bd9Sstevel@tonic-gate 	xcppm_dev_init,			/* dev_init */
1107c478bd9Sstevel@tonic-gate 	xcppm_dev_fini,			/* dev_fini */
1117c478bd9Sstevel@tonic-gate 	xcppm_iocset,			/* iocset */
1127c478bd9Sstevel@tonic-gate 	xcppm_iocget,			/* iocget */
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * The order of entries must be from slowest to fastest and in
1187c478bd9Sstevel@tonic-gate  * one-to-one correspondence with the cpu_level array.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate static const uint16_t bbc_estar_control_masks[] = {
1217c478bd9Sstevel@tonic-gate 	BBC_ESTAR_SLOW, BBC_ESTAR_MEDIUM, BBC_ESTAR_FAST
1227c478bd9Sstevel@tonic-gate };
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate int bbc_delay = 10;			/* microsec */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * Configuration data structures
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate static struct cb_ops xcppm_cb_ops = {
1317c478bd9Sstevel@tonic-gate 	ppm_open,		/* open */
1327c478bd9Sstevel@tonic-gate 	ppm_close,		/* close */
1337c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
1347c478bd9Sstevel@tonic-gate 	nodev,			/* print */
1357c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
1367c478bd9Sstevel@tonic-gate 	nodev,			/* read */
1377c478bd9Sstevel@tonic-gate 	nodev,			/* write */
1387c478bd9Sstevel@tonic-gate 	ppm_ioctl,		/* ioctl */
1397c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
1407c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
1417c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
1427c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
1437c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
1447c478bd9Sstevel@tonic-gate 	NULL,			/* streamtab */
1457c478bd9Sstevel@tonic-gate 	D_MP | D_NEW,		/* driver compatibility flag */
1467c478bd9Sstevel@tonic-gate 	CB_REV,			/* cb_ops revision */
1477c478bd9Sstevel@tonic-gate 	nodev,			/* async read */
1487c478bd9Sstevel@tonic-gate 	nodev			/* async write */
1497c478bd9Sstevel@tonic-gate };
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static struct bus_ops xcppm_bus_ops = {
1527c478bd9Sstevel@tonic-gate 	BUSO_REV,
1537c478bd9Sstevel@tonic-gate 	0,
1547c478bd9Sstevel@tonic-gate 	0,
1557c478bd9Sstevel@tonic-gate 	0,
1567c478bd9Sstevel@tonic-gate 	0,
1577c478bd9Sstevel@tonic-gate 	0,
1587c478bd9Sstevel@tonic-gate 	ddi_no_dma_map,
1597c478bd9Sstevel@tonic-gate 	ddi_no_dma_allochdl,
1607c478bd9Sstevel@tonic-gate 	ddi_no_dma_freehdl,
1617c478bd9Sstevel@tonic-gate 	ddi_no_dma_bindhdl,
1627c478bd9Sstevel@tonic-gate 	ddi_no_dma_unbindhdl,
1637c478bd9Sstevel@tonic-gate 	ddi_no_dma_flush,
1647c478bd9Sstevel@tonic-gate 	ddi_no_dma_win,
1657c478bd9Sstevel@tonic-gate 	ddi_no_dma_mctl,
1667c478bd9Sstevel@tonic-gate 	xcppm_ctlops,
1677c478bd9Sstevel@tonic-gate 	0,
1687c478bd9Sstevel@tonic-gate 	0,			/* (*bus_get_eventcookie)();	*/
1697c478bd9Sstevel@tonic-gate 	0,			/* (*bus_add_eventcall)();	*/
1707c478bd9Sstevel@tonic-gate 	0,			/* (*bus_remove_eventcall)();	*/
1717c478bd9Sstevel@tonic-gate 	0			/* (*bus_post_event)();		*/
1727c478bd9Sstevel@tonic-gate };
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate static struct dev_ops xcppm_ops = {
1757c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1767c478bd9Sstevel@tonic-gate 	0,			/* refcnt */
1777c478bd9Sstevel@tonic-gate 	ppm_getinfo,		/* info */
1787c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
1797c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
1807c478bd9Sstevel@tonic-gate 	xcppm_attach,		/* attach */
1817c478bd9Sstevel@tonic-gate 	xcppm_detach,		/* detach */
1827c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
1837c478bd9Sstevel@tonic-gate 	&xcppm_cb_ops,		/* driver operations */
1847c478bd9Sstevel@tonic-gate 	&xcppm_bus_ops,		/* bus operations */
1857c478bd9Sstevel@tonic-gate 	NULL,			/* power */
18619397407SSherry Moore 	ddi_quiesce_not_supported,	/* devo_quiesce */
1877c478bd9Sstevel@tonic-gate };
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1927c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* type of module - pseudo */
19319397407SSherry Moore 	"platform pm driver",
1947c478bd9Sstevel@tonic-gate 	&xcppm_ops
1957c478bd9Sstevel@tonic-gate };
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1987c478bd9Sstevel@tonic-gate 	MODREV_1,
1997c478bd9Sstevel@tonic-gate 	&modldrv,
2007c478bd9Sstevel@tonic-gate 	NULL
2017c478bd9Sstevel@tonic-gate };
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate int
_init(void)2057c478bd9Sstevel@tonic-gate _init(void)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	return (ppm_init(&modlinkage, sizeof (xcppm_unit_t), "xc"));
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate int
_fini(void)2127c478bd9Sstevel@tonic-gate _fini(void)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	return (EBUSY);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2197c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate static int
xcppm_map_all_regs(dev_info_t * dip)2267c478bd9Sstevel@tonic-gate xcppm_map_all_regs(dev_info_t *dip)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	ddi_device_acc_attr_t attr_be, attr_le;
2297c478bd9Sstevel@tonic-gate 	int rv0, rv1, rv2, rv3;
2307c478bd9Sstevel@tonic-gate 	xcppm_unit_t *unitp;
2317c478bd9Sstevel@tonic-gate 	caddr_t base_addr;
2327c478bd9Sstevel@tonic-gate 	uint8_t data8;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
2357c478bd9Sstevel@tonic-gate 	attr_be.devacc_attr_version = DDI_DEVICE_ATTR_V0;
2367c478bd9Sstevel@tonic-gate 	attr_be.devacc_attr_endian_flags  = DDI_STRUCTURE_BE_ACC;
2377c478bd9Sstevel@tonic-gate 	attr_be.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	attr_le.devacc_attr_version = DDI_DEVICE_ATTR_V0;
2407c478bd9Sstevel@tonic-gate 	attr_le.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
2417c478bd9Sstevel@tonic-gate 	attr_le.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	rv0 = ddi_regs_map_setup(dip, 0, &base_addr, 0, 0, &attr_be,
2447c478bd9Sstevel@tonic-gate 	    &unitp->hndls.bbc_estar_ctrl);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	unitp->regs.bbc_estar_ctrl = (uint16_t *)(base_addr +
2477c478bd9Sstevel@tonic-gate 	    BBC_ESTAR_CTRL_OFFSET);
2487c478bd9Sstevel@tonic-gate 	unitp->regs.bbc_assert_change = (uint32_t *)(base_addr +
2497c478bd9Sstevel@tonic-gate 	    BBC_ASSERT_CHANGE_OFFSET);
2507c478bd9Sstevel@tonic-gate 	unitp->regs.bbc_pll_settle = (uint32_t *)(base_addr +
2517c478bd9Sstevel@tonic-gate 	    BBC_PLL_SETTLE_OFFSET);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	rv1 = ddi_regs_map_setup(dip, 1,
2547c478bd9Sstevel@tonic-gate 	    (caddr_t *)&unitp->regs.rio_mode_auxio,
2557c478bd9Sstevel@tonic-gate 	    0, 0, &attr_le, &unitp->hndls.rio_mode_auxio);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	rv2 = ddi_regs_map_setup(dip, 2, &base_addr,
2587c478bd9Sstevel@tonic-gate 	    0, 0, &attr_le, &unitp->hndls.gpio_bank_select);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	unitp->regs.gpio_bank_sel_index = (uint8_t *)(base_addr +
2617c478bd9Sstevel@tonic-gate 	    GPIO_BANK_SEL_INDEX_OFFSET);
2627c478bd9Sstevel@tonic-gate 	unitp->regs.gpio_bank_sel_data = (uint8_t *)(base_addr +
2637c478bd9Sstevel@tonic-gate 	    GPIO_BANK_SEL_DATA_OFFSET);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	rv3 = ddi_regs_map_setup(dip, 3, &base_addr, 0, 0, &attr_le,
2667c478bd9Sstevel@tonic-gate 	    &unitp->hndls.gpio_data_ports);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	unitp->regs.gpio_port1_data = (uint8_t *)(base_addr +
2697c478bd9Sstevel@tonic-gate 	    GPIO_PORT1_DATA_OFFSET);
2707c478bd9Sstevel@tonic-gate 	unitp->regs.gpio_port2_data = (uint8_t *)(base_addr +
2717c478bd9Sstevel@tonic-gate 	    GPIO_PORT2_DATA_OFFSET);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (rv0 != DDI_SUCCESS || rv1 != DDI_SUCCESS ||
2747c478bd9Sstevel@tonic-gate 	    rv2 != DDI_SUCCESS || rv3 != DDI_SUCCESS) {
2757c478bd9Sstevel@tonic-gate 		if (rv0 == DDI_SUCCESS)
2767c478bd9Sstevel@tonic-gate 			ddi_regs_map_free(&unitp->hndls.bbc_estar_ctrl);
2777c478bd9Sstevel@tonic-gate 		if (rv1 == DDI_SUCCESS)
2787c478bd9Sstevel@tonic-gate 			ddi_regs_map_free(&unitp->hndls.rio_mode_auxio);
2797c478bd9Sstevel@tonic-gate 		if (rv2 == DDI_SUCCESS)
2807c478bd9Sstevel@tonic-gate 			ddi_regs_map_free(&unitp->hndls.gpio_bank_select);
2817c478bd9Sstevel@tonic-gate 		if (rv3 == DDI_SUCCESS)
2827c478bd9Sstevel@tonic-gate 			ddi_regs_map_free(&unitp->hndls.gpio_data_ports);
2837c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/*
2877c478bd9Sstevel@tonic-gate 	 * Ppm uses GPIO bits in Bank 0.  Make sure Bank 0 is selected.
2887c478bd9Sstevel@tonic-gate 	 */
2897c478bd9Sstevel@tonic-gate 	data8 = SIO_CONFIG2_INDEX;
2907c478bd9Sstevel@tonic-gate 	XCPPM_SETGET8(unitp->hndls.gpio_bank_select,
2917c478bd9Sstevel@tonic-gate 	    unitp->regs.gpio_bank_sel_index, data8);
2927c478bd9Sstevel@tonic-gate 	data8 = XCPPM_GET8(unitp->hndls.gpio_bank_select,
2937c478bd9Sstevel@tonic-gate 	    unitp->regs.gpio_bank_sel_data);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	data8 &= 0x7f;	/* Set Bit7 to zero */
2967c478bd9Sstevel@tonic-gate 	XCPPM_SETGET8(unitp->hndls.gpio_bank_select,
2977c478bd9Sstevel@tonic-gate 	    unitp->regs.gpio_bank_sel_data, data8);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate static int
xcppm_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)3047c478bd9Sstevel@tonic-gate xcppm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate #ifdef DEBUG
3077c478bd9Sstevel@tonic-gate 	char *str = "xcppm_attach";
3087c478bd9Sstevel@tonic-gate #endif
3097c478bd9Sstevel@tonic-gate 	xcppm_unit_t *unitp;
3107c478bd9Sstevel@tonic-gate 	ppm_domain_t **dompp;
3117c478bd9Sstevel@tonic-gate 	int retval;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	DPRINTF(D_ATTACH, ("%s: attach cmd %d\n", str, cmd));
3147c478bd9Sstevel@tonic-gate 	retval = DDI_SUCCESS;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	switch (cmd) {
3177c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
3187c478bd9Sstevel@tonic-gate 		if (ppm_inst != -1) {
3197c478bd9Sstevel@tonic-gate 			DPRINTF(D_ERROR,
3207c478bd9Sstevel@tonic-gate 			    ("%s: instance already attached\n", str));
3217c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 		ppm_inst = ddi_get_instance(dip);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 		/*
3267c478bd9Sstevel@tonic-gate 		 * Allocate and initialize soft state structure
3277c478bd9Sstevel@tonic-gate 		 */
3287c478bd9Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(ppm_statep, ppm_inst) != 0)
3297c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3307c478bd9Sstevel@tonic-gate 		unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
3317c478bd9Sstevel@tonic-gate 		mutex_init(&unitp->unit_lock, NULL, MUTEX_DRIVER, NULL);
3327c478bd9Sstevel@tonic-gate 		mutex_init(&unitp->creator_lock, NULL, MUTEX_DRIVER, NULL);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "ppm", S_IFCHR,
3357c478bd9Sstevel@tonic-gate 		    ppm_inst, "ddi_ppm", 0) == DDI_FAILURE) {
3367c478bd9Sstevel@tonic-gate 			ddi_soft_state_free(ppm_statep, ppm_inst);
3377c478bd9Sstevel@tonic-gate 			DPRINTF(D_ERROR,
33807d06da5SSurya Prakki 			    ("%s: Can't create minor for 0x%p\n", str,
33907d06da5SSurya Prakki 			    (void *)dip));
3407c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 		ddi_report_dev(dip);
3437c478bd9Sstevel@tonic-gate 		unitp->dip = dip;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		if (retval = ppm_create_db(dip))
3467c478bd9Sstevel@tonic-gate 			return (retval);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		/*
3497c478bd9Sstevel@tonic-gate 		 * Map all of the registers under the ppm node.
3507c478bd9Sstevel@tonic-gate 		 */
3517c478bd9Sstevel@tonic-gate 		if (xcppm_map_all_regs(dip) != DDI_SUCCESS)
3527c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		if ((retval =
3557c478bd9Sstevel@tonic-gate 		    pm_register_ppm(ppm_claim_dev, dip)) != DDI_SUCCESS) {
3567c478bd9Sstevel@tonic-gate 			DPRINTF(D_ERROR,
3577c478bd9Sstevel@tonic-gate 			    ("%s: can't register ppm handler\n", str));
3587c478bd9Sstevel@tonic-gate 			return (retval);
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		for (dompp = ppm_domains; *dompp; dompp++)
3627c478bd9Sstevel@tonic-gate 			mutex_init(&(*dompp)->lock, NULL, MUTEX_DRIVER, NULL);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		break;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
3677c478bd9Sstevel@tonic-gate 		unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
3687c478bd9Sstevel@tonic-gate 		mutex_enter(&unitp->unit_lock);
3697c478bd9Sstevel@tonic-gate 		unitp->state &= ~XCPPM_ST_SUSPENDED;
3707c478bd9Sstevel@tonic-gate 		mutex_exit(&unitp->unit_lock);
3717c478bd9Sstevel@tonic-gate 		break;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	default:
3747c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "xcppm_attach: unknown "
37507d06da5SSurya Prakki 		    "attach command %d, dip 0x%p\n", cmd, (void *)dip);
3767c478bd9Sstevel@tonic-gate 		retval = DDI_FAILURE;
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	return (retval);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate  * set the front panel LED:
3857c478bd9Sstevel@tonic-gate  * PPM_LEDON turns it on, PPM_LEDOFF turns it off.
3867c478bd9Sstevel@tonic-gate  * for GPIO register: 0x0 means led-on, 0x2 means led-off.
3877c478bd9Sstevel@tonic-gate  */
3887c478bd9Sstevel@tonic-gate static void
xcppm_set_led(int action)3897c478bd9Sstevel@tonic-gate xcppm_set_led(int action)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 	xcppm_unit_t *unitp;
3927c478bd9Sstevel@tonic-gate 	uint8_t	reg;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	ASSERT(action == PPM_LEDON || action == PPM_LEDOFF);
3957c478bd9Sstevel@tonic-gate 	DPRINTF(D_LED, ("xcppm_set_led: Turn LED %s\n",
3967c478bd9Sstevel@tonic-gate 	    (action == PPM_LEDON) ? "on" : "off"));
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
3997c478bd9Sstevel@tonic-gate 	reg = XCPPM_GET8(unitp->hndls.gpio_data_ports,
4007c478bd9Sstevel@tonic-gate 	    unitp->regs.gpio_port1_data);
4017c478bd9Sstevel@tonic-gate 	if (action == PPM_LEDON)
4027c478bd9Sstevel@tonic-gate 		reg &= ~LED;
4037c478bd9Sstevel@tonic-gate 	else
4047c478bd9Sstevel@tonic-gate 		reg |= LED;
4057c478bd9Sstevel@tonic-gate 	XCPPM_SETGET8(unitp->hndls.gpio_data_ports,
4067c478bd9Sstevel@tonic-gate 	    unitp->regs.gpio_port1_data, reg);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate static void
xcppm_blink_led(void * action)4117c478bd9Sstevel@tonic-gate xcppm_blink_led(void *action)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	xcppm_unit_t *unitp;
4147c478bd9Sstevel@tonic-gate 	int new_action;
4157c478bd9Sstevel@tonic-gate 	clock_t intvl;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
4187c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->unit_lock);
4197c478bd9Sstevel@tonic-gate 	if (unitp->led_tid == 0) {
4207c478bd9Sstevel@tonic-gate 		mutex_exit(&unitp->unit_lock);
4217c478bd9Sstevel@tonic-gate 		return;
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
424360e6f5eSmathue 	if ((int)(uintptr_t)action == PPM_LEDON) {
4257c478bd9Sstevel@tonic-gate 		new_action = PPM_LEDOFF;
4267c478bd9Sstevel@tonic-gate 		intvl = PPM_LEDOFF_INTERVAL;
4277c478bd9Sstevel@tonic-gate 	} else {
428360e6f5eSmathue 		ASSERT((int)(uintptr_t)action == PPM_LEDOFF);
4297c478bd9Sstevel@tonic-gate 		new_action = PPM_LEDON;
4307c478bd9Sstevel@tonic-gate 		intvl = PPM_LEDON_INTERVAL;
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	xcppm_set_led(new_action);
434360e6f5eSmathue 	unitp->led_tid = timeout(xcppm_blink_led, (void *)(uintptr_t)new_action,
435360e6f5eSmathue 	    intvl);
4367c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->unit_lock);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate static void
xcppm_freeze_led(void * action)4417c478bd9Sstevel@tonic-gate xcppm_freeze_led(void *action)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate 	xcppm_unit_t *unitp;
4447c478bd9Sstevel@tonic-gate 	timeout_id_t tid;
4457c478bd9Sstevel@tonic-gate 
446360e6f5eSmathue 	DPRINTF(D_LOWEST, ("xcppm_freeze_led: action %d\n",
447360e6f5eSmathue 	    (int)(uintptr_t)action));
4487c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
4497c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->unit_lock);
4507c478bd9Sstevel@tonic-gate 	tid = unitp->led_tid;
4517c478bd9Sstevel@tonic-gate 	unitp->led_tid = 0;
4527c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->unit_lock);
45307d06da5SSurya Prakki 	(void) untimeout(tid);
4547c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->unit_lock);
455360e6f5eSmathue 	xcppm_set_led((int)(uintptr_t)action);
4567c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->unit_lock);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate /* ARGSUSED */
4617c478bd9Sstevel@tonic-gate static int
xcppm_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4627c478bd9Sstevel@tonic-gate xcppm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4637c478bd9Sstevel@tonic-gate {
4647c478bd9Sstevel@tonic-gate 	xcppm_unit_t *unitp;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
4677c478bd9Sstevel@tonic-gate 	DPRINTF(D_DETACH, ("xcppm_detach: cmd %d\n", cmd));
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	switch (cmd) {
4707c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
4717c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
4747c478bd9Sstevel@tonic-gate 		mutex_enter(&unitp->unit_lock);
4757c478bd9Sstevel@tonic-gate 		unitp->state |= XCPPM_ST_SUSPENDED;
4767c478bd9Sstevel@tonic-gate 		mutex_exit(&unitp->unit_lock);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 		/*
4797c478bd9Sstevel@tonic-gate 		 * Suspend requires that timeout callouts to be canceled.
4807c478bd9Sstevel@tonic-gate 		 * Turning off the LED blinking will cancel the timeout.
4817c478bd9Sstevel@tonic-gate 		 */
4827c478bd9Sstevel@tonic-gate 		xcppm_freeze_led((void *)PPM_LEDON);
4837c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	default:
4867c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate /*
4927c478bd9Sstevel@tonic-gate  * Device we claimed has detached.  We must get rid of
4937c478bd9Sstevel@tonic-gate  * our state which was used to track this device.
4947c478bd9Sstevel@tonic-gate  */
4957c478bd9Sstevel@tonic-gate static void
xcppm_detach_ctlop(dev_info_t * dip,power_req_t * reqp)4967c478bd9Sstevel@tonic-gate xcppm_detach_ctlop(dev_info_t *dip, power_req_t *reqp)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	ppm_dev_t *ppmd;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	ppmd = PPM_GET_PRIVATE(dip);
5017c478bd9Sstevel@tonic-gate 	if (ppmd == NULL || reqp->req.ppm_config_req.result != DDI_SUCCESS)
5027c478bd9Sstevel@tonic-gate 		return;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	ppm_rem_dev(dip);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate /*
5097c478bd9Sstevel@tonic-gate  * The system is being resumed from a cpr suspend operation and this
5107c478bd9Sstevel@tonic-gate  * device's attach entry will be called shortly.  The driver will set
5117c478bd9Sstevel@tonic-gate  * the device's power to a conventional starting value, and we need to
5127c478bd9Sstevel@tonic-gate  * stay in sync and set our private copy to the same value.
5137c478bd9Sstevel@tonic-gate  */
5147c478bd9Sstevel@tonic-gate /* ARGSUSED */
5157c478bd9Sstevel@tonic-gate static void
xcppm_resume_ctlop(dev_info_t * dip,power_req_t * reqp)5167c478bd9Sstevel@tonic-gate xcppm_resume_ctlop(dev_info_t *dip, power_req_t *reqp)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	ppm_domain_t *domp;
5197c478bd9Sstevel@tonic-gate 	ppm_dev_t *ppmd;
5207c478bd9Sstevel@tonic-gate 	int powered;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	ppmd = PPM_GET_PRIVATE(dip);
5237c478bd9Sstevel@tonic-gate 	if (ppmd == NULL)
5247c478bd9Sstevel@tonic-gate 		return;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	/*
5277c478bd9Sstevel@tonic-gate 	 * Maintain correct powered count for domain which cares
5287c478bd9Sstevel@tonic-gate 	 */
5297c478bd9Sstevel@tonic-gate 	powered = 0;
5307c478bd9Sstevel@tonic-gate 	domp = ppmd->domp;
5317c478bd9Sstevel@tonic-gate 	mutex_enter(&domp->lock);
5327c478bd9Sstevel@tonic-gate 	if (domp == &xcppm_fet) {
5337c478bd9Sstevel@tonic-gate 		for (ppmd = domp->devlist; ppmd; ppmd = ppmd->next) {
5347c478bd9Sstevel@tonic-gate 			if (ppmd->dip == dip && ppmd->level)
5357c478bd9Sstevel@tonic-gate 				powered++;
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		/*
5397c478bd9Sstevel@tonic-gate 		 * If this device was powered off when the system was
5407c478bd9Sstevel@tonic-gate 		 * suspended, this resume acts like a power-on transition,
5417c478bd9Sstevel@tonic-gate 		 * so we adjust the count.
5427c478bd9Sstevel@tonic-gate 		 */
5437c478bd9Sstevel@tonic-gate 		if (powered == 0)
5447c478bd9Sstevel@tonic-gate 			domp->pwr_cnt++;
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	for (ppmd = domp->devlist; ppmd; ppmd = ppmd->next) {
5487c478bd9Sstevel@tonic-gate 		if (ppmd->dip == dip)
5497c478bd9Sstevel@tonic-gate 			ppmd->level = ppmd->rplvl = PM_LEVEL_UNKNOWN;
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 	mutex_exit(&domp->lock);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /*
5567c478bd9Sstevel@tonic-gate  * Change the power level for a component of a device.  If the change
5577c478bd9Sstevel@tonic-gate  * arg is true, we call the framework to actually change the device's
5587c478bd9Sstevel@tonic-gate  * power; otherwise, we just update our own copy of the power level.
5597c478bd9Sstevel@tonic-gate  */
5607c478bd9Sstevel@tonic-gate static int
xcppm_set_level(ppm_dev_t * ppmd,int cmpt,int level,boolean_t change)5617c478bd9Sstevel@tonic-gate xcppm_set_level(ppm_dev_t *ppmd, int cmpt, int level, boolean_t change)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate #ifdef DEBUG
5647c478bd9Sstevel@tonic-gate 	char *str = "xcppm_set_level";
5657c478bd9Sstevel@tonic-gate #endif
5667c478bd9Sstevel@tonic-gate 	int ret;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	ret = DDI_SUCCESS;
5697c478bd9Sstevel@tonic-gate 	if (change)
5707c478bd9Sstevel@tonic-gate 		ret = pm_power(ppmd->dip, cmpt, level);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	DPRINTF(D_SETLVL, ("%s: \"%s\" change=%d, old %d, new %d, ret %d\n",
5737c478bd9Sstevel@tonic-gate 	    str, ppmd->path, change, ppmd->level, level, ret));
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
5767c478bd9Sstevel@tonic-gate 		ppmd->level = level;
5777c478bd9Sstevel@tonic-gate 		ppmd->rplvl = PM_LEVEL_UNKNOWN;
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	return (ret);
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate static int
xcppm_change_power_level(ppm_dev_t * ppmd,int cmpt,int level)5857c478bd9Sstevel@tonic-gate xcppm_change_power_level(ppm_dev_t *ppmd, int cmpt, int level)
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate 	return (xcppm_set_level(ppmd, cmpt, level, B_TRUE));
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate static int
xcppm_record_level_change(ppm_dev_t * ppmd,int cmpt,int level)5927c478bd9Sstevel@tonic-gate xcppm_record_level_change(ppm_dev_t *ppmd, int cmpt, int level)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	return (xcppm_set_level(ppmd, cmpt, level, B_FALSE));
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate static uint8_t
xcppm_gpio_port2(int action,uint8_t pos)5997c478bd9Sstevel@tonic-gate xcppm_gpio_port2(int action, uint8_t pos)
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate #ifdef DEBUG
6027c478bd9Sstevel@tonic-gate 	char *str = "xcppm_gpio_port2";
6037c478bd9Sstevel@tonic-gate #endif
6047c478bd9Sstevel@tonic-gate 	xcppm_unit_t *unitp;
6057c478bd9Sstevel@tonic-gate 	uint8_t data8, buf8;
6067c478bd9Sstevel@tonic-gate 	uint8_t	ret;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
6097c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->gpio_lock);
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	data8 = buf8 = XCPPM_GET8(unitp->hndls.gpio_data_ports,
6127c478bd9Sstevel@tonic-gate 	    unitp->regs.gpio_port2_data);
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	switch (action) {
6157c478bd9Sstevel@tonic-gate 	case XCPPM_GETBIT:
6167c478bd9Sstevel@tonic-gate 		ret = data8 & pos;
6177c478bd9Sstevel@tonic-gate 		DPRINTF(D_GPIO, ("%s: READ: GPIO Bank2 value 0x%x\n",
6187c478bd9Sstevel@tonic-gate 		    str, buf8));
6197c478bd9Sstevel@tonic-gate 		break;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	case XCPPM_SETBIT:
6227c478bd9Sstevel@tonic-gate 	case XCPPM_CLRBIT:
6237c478bd9Sstevel@tonic-gate 		if (action == XCPPM_SETBIT)
6247c478bd9Sstevel@tonic-gate 			data8 |= pos;
6257c478bd9Sstevel@tonic-gate 		else
6267c478bd9Sstevel@tonic-gate 			data8 &= ~pos;
6277c478bd9Sstevel@tonic-gate 		XCPPM_SETGET8(unitp->hndls.gpio_data_ports,
6287c478bd9Sstevel@tonic-gate 		    unitp->regs.gpio_port2_data, data8);
6297c478bd9Sstevel@tonic-gate 		ret = data8 & pos;
6307c478bd9Sstevel@tonic-gate 		DPRINTF(D_GPIO, ("%s: %s: GPIO Bank2 "
6317c478bd9Sstevel@tonic-gate 		    "bit 0x%x changed from 0x%x to 0x%x\n",
6327c478bd9Sstevel@tonic-gate 		    str, (action == XCPPM_SETBIT) ? "UP" : "DOWN",
6337c478bd9Sstevel@tonic-gate 		    pos, buf8, data8));
6347c478bd9Sstevel@tonic-gate 		break;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	default:
6377c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "xcalppm: unrecognized register "
6387c478bd9Sstevel@tonic-gate 		    "IO command %d\n", action);
6397c478bd9Sstevel@tonic-gate 		break;
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->gpio_lock);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	return (ret);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate /*
6487c478bd9Sstevel@tonic-gate  * Raise the power level of a subrange of cpus.  Used when cpu driver
6497c478bd9Sstevel@tonic-gate  * failed an attempt to lower the power of a cpu (probably because
6507c478bd9Sstevel@tonic-gate  * it got busy).  Need to revert the ones we already changed.
6517c478bd9Sstevel@tonic-gate  *
6527c478bd9Sstevel@tonic-gate  * ecpup = the ppm_dev_t for the cpu which failed to lower power
6537c478bd9Sstevel@tonic-gate  * level = power level to reset prior cpus to
6547c478bd9Sstevel@tonic-gate  */
6557c478bd9Sstevel@tonic-gate static void
xcppm_revert_cpu_power(ppm_dev_t * ecpup,int level)6567c478bd9Sstevel@tonic-gate xcppm_revert_cpu_power(ppm_dev_t *ecpup, int level)
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate 	ppm_dev_t *cpup;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	for (cpup = xcppm_cpu.devlist; cpup != ecpup; cpup = cpup->next) {
6617c478bd9Sstevel@tonic-gate 		DPRINTF(D_CPU, ("xrcp: \"%s\", revert to level %d\n",
6627c478bd9Sstevel@tonic-gate 		    cpup->path, level));
6637c478bd9Sstevel@tonic-gate 		(void) xcppm_change_power_level(cpup, 0, level);
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate /*
6687c478bd9Sstevel@tonic-gate  * Switch the DC/DC converter.  Clearing the GPIO bit in SuperI/O puts
6697c478bd9Sstevel@tonic-gate  * the converter in low power mode and setting the bit puts it back in
6707c478bd9Sstevel@tonic-gate  * normal mode.
6717c478bd9Sstevel@tonic-gate  */
6727c478bd9Sstevel@tonic-gate static void
xcppm_switch_dcdc_converter(int action)6737c478bd9Sstevel@tonic-gate xcppm_switch_dcdc_converter(int action)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	int tries = XCPPM_VCL_TRIES;
6767c478bd9Sstevel@tonic-gate 	uint_t spl;
6777c478bd9Sstevel@tonic-gate 	uint64_t stick_begin, stick_end;
6787c478bd9Sstevel@tonic-gate 	uint64_t tick_begin, tick_end;
6797c478bd9Sstevel@tonic-gate 	uint64_t cur_speed_ratio, full_speed_ratio;
6807c478bd9Sstevel@tonic-gate 	static int xcppm_dcdc_lpm;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	switch (action) {
6837c478bd9Sstevel@tonic-gate 	case XCPPM_SETBIT:
6847c478bd9Sstevel@tonic-gate 		if (xcppm_dcdc_lpm) {
6857c478bd9Sstevel@tonic-gate 			DPRINTF(D_CPU, ("xcppm_switch_dcdc_converter: "
6867c478bd9Sstevel@tonic-gate 			    "switch to normal power mode.\n"));
6877c478bd9Sstevel@tonic-gate 			(void) xcppm_gpio_port2(action, HIGHPWR);
6887c478bd9Sstevel@tonic-gate 			xcppm_dcdc_lpm = 0;
6897c478bd9Sstevel@tonic-gate 		}
6907c478bd9Sstevel@tonic-gate 		break;
6917c478bd9Sstevel@tonic-gate 	case XCPPM_CLRBIT:
6927c478bd9Sstevel@tonic-gate 		/*
6937c478bd9Sstevel@tonic-gate 		 * In some fast CPU configurations, DC/DC converter was
6947c478bd9Sstevel@tonic-gate 		 * put in low power mode before CPUs made the transition
6957c478bd9Sstevel@tonic-gate 		 * to 1/32 of clock speed.  In those cases, system was
6967c478bd9Sstevel@tonic-gate 		 * shut down by hardware for protection.  To resolve that
6977c478bd9Sstevel@tonic-gate 		 * problem, we make sure CPUs have made the clock transition
6987c478bd9Sstevel@tonic-gate 		 * before the DC/DC converter has been put to low power mode.
6997c478bd9Sstevel@tonic-gate 		 */
7007c478bd9Sstevel@tonic-gate 		ASSERT(xcppm_dcdc_lpm == 0);
7017c478bd9Sstevel@tonic-gate 		kpreempt_disable();
7027c478bd9Sstevel@tonic-gate 		full_speed_ratio = cpunodes[CPU->cpu_id].clock_freq /
7037c478bd9Sstevel@tonic-gate 		    sys_tick_freq;
7047c478bd9Sstevel@tonic-gate 		while (tries) {
7057c478bd9Sstevel@tonic-gate 			spl = ddi_enter_critical();
7067c478bd9Sstevel@tonic-gate 			tick_begin = gettick_counter();
7077c478bd9Sstevel@tonic-gate 			stick_timestamp((int64_t *)&stick_begin);
7087c478bd9Sstevel@tonic-gate 			ddi_exit_critical(spl);
7097c478bd9Sstevel@tonic-gate 			drv_usecwait(XCPPM_VCL_DELAY);
7107c478bd9Sstevel@tonic-gate 			spl = ddi_enter_critical();
7117c478bd9Sstevel@tonic-gate 			tick_end = gettick_counter();
7127c478bd9Sstevel@tonic-gate 			stick_timestamp((int64_t *)&stick_end);
7137c478bd9Sstevel@tonic-gate 			ddi_exit_critical(spl);
7147c478bd9Sstevel@tonic-gate 			cur_speed_ratio = (tick_end - tick_begin) /
7157c478bd9Sstevel@tonic-gate 			    (stick_end - stick_begin);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 			/*
7187c478bd9Sstevel@tonic-gate 			 * tick/stick at current speed should at most be
7197c478bd9Sstevel@tonic-gate 			 * equal to full-speed tick/stick, adjusted with
7207c478bd9Sstevel@tonic-gate 			 * full/lowest clock speed ratio.  If not, speed
7217c478bd9Sstevel@tonic-gate 			 * transition has not happened yet.
7227c478bd9Sstevel@tonic-gate 			 */
7237c478bd9Sstevel@tonic-gate 			if (cur_speed_ratio <= ((full_speed_ratio /
7247c478bd9Sstevel@tonic-gate 			    XCPPM_VCL_DIVISOR) + 1)) {
7257c478bd9Sstevel@tonic-gate 				DPRINTF(D_CPU, ("xcppm_switch_dcdc_converter: "
7267c478bd9Sstevel@tonic-gate 				    "switch to low power mode.\n"));
7277c478bd9Sstevel@tonic-gate 				(void) xcppm_gpio_port2(action, HIGHPWR);
7287c478bd9Sstevel@tonic-gate 				xcppm_dcdc_lpm = 1;
7297c478bd9Sstevel@tonic-gate 				break;
7307c478bd9Sstevel@tonic-gate 			}
7317c478bd9Sstevel@tonic-gate 			DPRINTF(D_CPU, ("xcppm_switch_dcdc_converter: CPU "
7327c478bd9Sstevel@tonic-gate 			    "has not made transition to lowest speed yet "
7337c478bd9Sstevel@tonic-gate 			    "(%d)\n", tries));
7347c478bd9Sstevel@tonic-gate 			tries--;
7357c478bd9Sstevel@tonic-gate 		}
7367c478bd9Sstevel@tonic-gate 		kpreempt_enable();
7377c478bd9Sstevel@tonic-gate 		break;
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate static void
xcppm_rio_mode(xcppm_unit_t * unitp,int mode)7427c478bd9Sstevel@tonic-gate xcppm_rio_mode(xcppm_unit_t *unitp, int mode)
7437c478bd9Sstevel@tonic-gate {
7447c478bd9Sstevel@tonic-gate 	uint32_t data32, buf32;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->gpio_lock);
7477c478bd9Sstevel@tonic-gate 	data32 = buf32 = XCPPM_GET32(unitp->hndls.rio_mode_auxio,
7487c478bd9Sstevel@tonic-gate 	    unitp->regs.rio_mode_auxio);
7497c478bd9Sstevel@tonic-gate 	if (mode == XCPPM_SETBIT)
7507c478bd9Sstevel@tonic-gate 		data32 |= RIO_BBC_ESTAR_MODE;
7517c478bd9Sstevel@tonic-gate 	else
7527c478bd9Sstevel@tonic-gate 		data32 &= ~RIO_BBC_ESTAR_MODE;
7537c478bd9Sstevel@tonic-gate 	XCPPM_SETGET32(unitp->hndls.rio_mode_auxio,
7547c478bd9Sstevel@tonic-gate 	    unitp->regs.rio_mode_auxio, data32);
7557c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->gpio_lock);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	DPRINTF(D_CPU, ("xcppm_rio_mode: %s: change from 0x%x to 0x%x\n",
7587c478bd9Sstevel@tonic-gate 	    (mode == XCPPM_SETBIT) ? "DOWN" : "UP", buf32, data32));
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate /*
7637c478bd9Sstevel@tonic-gate  * change the power level of all cpus to the arg value;
7647c478bd9Sstevel@tonic-gate  * the caller needs to ensure that a legal transition is requested.
7657c478bd9Sstevel@tonic-gate  */
7667c478bd9Sstevel@tonic-gate static int
xcppm_change_cpu_power(int newlevel)7677c478bd9Sstevel@tonic-gate xcppm_change_cpu_power(int newlevel)
7687c478bd9Sstevel@tonic-gate {
7697c478bd9Sstevel@tonic-gate #ifdef DEBUG
7707c478bd9Sstevel@tonic-gate 	char *str = "xcppm_ccp";
7717c478bd9Sstevel@tonic-gate #endif
7727c478bd9Sstevel@tonic-gate 	int index, level, oldlevel;
7737c478bd9Sstevel@tonic-gate 	int lowest, highest;
7747c478bd9Sstevel@tonic-gate 	int undo_flag, ret;
7757c478bd9Sstevel@tonic-gate 	int speedup, incr;
7767c478bd9Sstevel@tonic-gate 	uint32_t data32;
7777c478bd9Sstevel@tonic-gate 	uint16_t data16;
7787c478bd9Sstevel@tonic-gate 	xcppm_unit_t *unitp;
7797c478bd9Sstevel@tonic-gate 	ppm_dev_t *cpup;
7807c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
7817c478bd9Sstevel@tonic-gate 	char *chstr;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
7847c478bd9Sstevel@tonic-gate 	ASSERT(unitp);
7857c478bd9Sstevel@tonic-gate 	cpup = xcppm_cpu.devlist;
7867c478bd9Sstevel@tonic-gate 	lowest = cpup->lowest;
7877c478bd9Sstevel@tonic-gate 	highest = cpup->highest;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	/*
7907c478bd9Sstevel@tonic-gate 	 * not all cpus may have transitioned to a known level by this time
7917c478bd9Sstevel@tonic-gate 	 */
7927c478bd9Sstevel@tonic-gate 	oldlevel = (cpup->level == PM_LEVEL_UNKNOWN) ? highest : cpup->level;
7937c478bd9Sstevel@tonic-gate 	dip = cpup->dip;
7947c478bd9Sstevel@tonic-gate 	ASSERT(dip);
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	DPRINTF(D_CPU, ("%s: old %d, new %d, highest %d, lowest %d\n",
7977c478bd9Sstevel@tonic-gate 	    str, oldlevel, newlevel, highest, lowest));
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	if (newlevel > oldlevel) {
8007c478bd9Sstevel@tonic-gate 		chstr = "UP";
8017c478bd9Sstevel@tonic-gate 		speedup = 1;
8027c478bd9Sstevel@tonic-gate 		incr = 1;
8037c478bd9Sstevel@tonic-gate 	} else if (newlevel < oldlevel) {
8047c478bd9Sstevel@tonic-gate 		chstr = "DOWN";
8057c478bd9Sstevel@tonic-gate 		speedup = 0;
8067c478bd9Sstevel@tonic-gate 		incr = -1;
8077c478bd9Sstevel@tonic-gate 	} else
8087c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	undo_flag = 0;
8117c478bd9Sstevel@tonic-gate 	if (speedup) {
8127c478bd9Sstevel@tonic-gate 		/*
8137c478bd9Sstevel@tonic-gate 		 * If coming up from lowest power level, set the E*
8147c478bd9Sstevel@tonic-gate 		 * mode bit in GPIO to make power supply efficient
8157c478bd9Sstevel@tonic-gate 		 * at normal power.
8167c478bd9Sstevel@tonic-gate 		 */
8177c478bd9Sstevel@tonic-gate 		if (oldlevel == cpup->lowest) {
8187c478bd9Sstevel@tonic-gate 			xcppm_switch_dcdc_converter(XCPPM_SETBIT);
8197c478bd9Sstevel@tonic-gate 			undo_flag = 1;
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 	} else {
8227c478bd9Sstevel@tonic-gate 		/*
8237c478bd9Sstevel@tonic-gate 		 * set BBC Estar mode bit in RIO AUXIO register
8247c478bd9Sstevel@tonic-gate 		 */
8257c478bd9Sstevel@tonic-gate 		if (oldlevel == highest) {
8267c478bd9Sstevel@tonic-gate 			xcppm_rio_mode(unitp, XCPPM_SETBIT);
8277c478bd9Sstevel@tonic-gate 			undo_flag = 1;
8287c478bd9Sstevel@tonic-gate 		}
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	/*
8327c478bd9Sstevel@tonic-gate 	 * this loop will execute 1x or 2x depending on
8337c478bd9Sstevel@tonic-gate 	 * number of times we need to change clock rates
8347c478bd9Sstevel@tonic-gate 	 */
8357c478bd9Sstevel@tonic-gate 	for (level = oldlevel+incr; level != newlevel+incr; level += incr) {
8367c478bd9Sstevel@tonic-gate 		for (cpup = xcppm_cpu.devlist; cpup; cpup = cpup->next) {
8377c478bd9Sstevel@tonic-gate 			if (cpup->level == level)
8387c478bd9Sstevel@tonic-gate 				continue;
8397c478bd9Sstevel@tonic-gate 			ret = xcppm_change_power_level(cpup, 0, level);
8407c478bd9Sstevel@tonic-gate 			DPRINTF(D_CPU, ("%s: \"%s\", %s to level %d, ret %d\n",
8417c478bd9Sstevel@tonic-gate 			    str, cpup->path, chstr, cpup->level, ret));
8427c478bd9Sstevel@tonic-gate 			if (ret == DDI_SUCCESS)
8437c478bd9Sstevel@tonic-gate 				continue;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 			/*
8467c478bd9Sstevel@tonic-gate 			 * if the driver was unable to lower cpu speed,
8477c478bd9Sstevel@tonic-gate 			 * the cpu probably got busy; set the previous
8487c478bd9Sstevel@tonic-gate 			 * cpus back to the original level
8497c478bd9Sstevel@tonic-gate 			 */
8507c478bd9Sstevel@tonic-gate 			if (speedup == 0)
8517c478bd9Sstevel@tonic-gate 				xcppm_revert_cpu_power(cpup, level + 1);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 			if (undo_flag) {
8547c478bd9Sstevel@tonic-gate 				if (speedup)
8557c478bd9Sstevel@tonic-gate 					xcppm_switch_dcdc_converter(
8567c478bd9Sstevel@tonic-gate 					    XCPPM_CLRBIT);
8577c478bd9Sstevel@tonic-gate 				else
8587c478bd9Sstevel@tonic-gate 					xcppm_rio_mode(unitp, XCPPM_CLRBIT);
8597c478bd9Sstevel@tonic-gate 			}
8607c478bd9Sstevel@tonic-gate 			return (ret);
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		index = level - 1;
8647c478bd9Sstevel@tonic-gate 		spm_change_schizo_speed(index);
8657c478bd9Sstevel@tonic-gate 		DPRINTF(D_CPU, ("%s: safari config reg changed\n", str));
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 		/*
8687c478bd9Sstevel@tonic-gate 		 * set the delay times for changing to this rate
8697c478bd9Sstevel@tonic-gate 		 */
8707c478bd9Sstevel@tonic-gate 		data32 = XCPPM_BBC_DELAY(index);
8717c478bd9Sstevel@tonic-gate 		XCPPM_SETGET32(unitp->hndls.bbc_estar_ctrl,
8727c478bd9Sstevel@tonic-gate 		    (caddr_t)unitp->regs.bbc_assert_change, data32);
8737c478bd9Sstevel@tonic-gate 		DPRINTF(D_CPU, ("%s: %s: Wrote E* Assert Change Time "
8747c478bd9Sstevel@tonic-gate 		    "(t1) = 0x%x\n", str, chstr, data32));
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 		data32 = XCPPM_BBC_DELAY(index);
8777c478bd9Sstevel@tonic-gate 		XCPPM_SETGET32(unitp->hndls.bbc_estar_ctrl,
8787c478bd9Sstevel@tonic-gate 		    (caddr_t)unitp->regs.bbc_pll_settle, data32);
8797c478bd9Sstevel@tonic-gate 		DPRINTF(D_CPU, ("%s: %s: Wrote E* PLL Settle Time "
8807c478bd9Sstevel@tonic-gate 		    "(t4) = 0x%x\n", str, chstr, data32));
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		data16 = bbc_estar_control_masks[index];
8837c478bd9Sstevel@tonic-gate 		XCPPM_SETGET16(unitp->hndls.bbc_estar_ctrl,
8847c478bd9Sstevel@tonic-gate 		    (caddr_t)unitp->regs.bbc_estar_ctrl, data16);
8857c478bd9Sstevel@tonic-gate 		DPRINTF(D_CPU, ("%s: %s: Wrote BCC E* Control = 0x%x\n",
8867c478bd9Sstevel@tonic-gate 		    str, chstr, data16));
8877c478bd9Sstevel@tonic-gate 	}
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	/*
8907c478bd9Sstevel@tonic-gate 	 * clear CPU Estar Mode bit in the gpio register
8917c478bd9Sstevel@tonic-gate 	 */
8927c478bd9Sstevel@tonic-gate 	if (speedup) {
8937c478bd9Sstevel@tonic-gate 		if (newlevel == highest)
8947c478bd9Sstevel@tonic-gate 			xcppm_rio_mode(unitp, XCPPM_CLRBIT);
8957c478bd9Sstevel@tonic-gate 	} else {
8967c478bd9Sstevel@tonic-gate 		if (newlevel == lowest)
8977c478bd9Sstevel@tonic-gate 			xcppm_switch_dcdc_converter(XCPPM_CLRBIT);
8987c478bd9Sstevel@tonic-gate 	}
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate /*
9057c478bd9Sstevel@tonic-gate  * Process a request to change the power level of a cpu.  If all cpus
9067c478bd9Sstevel@tonic-gate  * don't want to be at the same power yet, or if we are currently
9077c478bd9Sstevel@tonic-gate  * refusing slowdown requests due to thermal stress, just cache the
9087c478bd9Sstevel@tonic-gate  * request.  Otherwise, make the change for all cpus.
9097c478bd9Sstevel@tonic-gate  */
9107c478bd9Sstevel@tonic-gate /* ARGSUSED */
9117c478bd9Sstevel@tonic-gate static int
xcppm_manage_cpus(dev_info_t * dip,power_req_t * reqp,int * result)9127c478bd9Sstevel@tonic-gate xcppm_manage_cpus(dev_info_t *dip, power_req_t *reqp, int *result)
9137c478bd9Sstevel@tonic-gate {
9147c478bd9Sstevel@tonic-gate #ifdef DEBUG
9157c478bd9Sstevel@tonic-gate 	char *str = "xcppm_manage_cpus";
9167c478bd9Sstevel@tonic-gate #endif
9177c478bd9Sstevel@tonic-gate 	int old, new, ret, kmflag;
9187c478bd9Sstevel@tonic-gate 	ppm_dev_t *ppmd;
9197c478bd9Sstevel@tonic-gate 	pm_ppm_devlist_t *devlist = NULL, *p;
9207c478bd9Sstevel@tonic-gate 	int		do_rescan = 0;
9217c478bd9Sstevel@tonic-gate 	dev_info_t	*rescan_dip;
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	*result = DDI_SUCCESS;
9247c478bd9Sstevel@tonic-gate 	switch (reqp->request_type) {
9257c478bd9Sstevel@tonic-gate 	case PMR_PPM_SET_POWER:
9267c478bd9Sstevel@tonic-gate 		break;
9277c478bd9Sstevel@tonic-gate 	case PMR_PPM_POWER_CHANGE_NOTIFY:
9287c478bd9Sstevel@tonic-gate 		/* cpu driver can`t change cpu power level by itself */
9297c478bd9Sstevel@tonic-gate 	default:
9307c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	ppmd = PPM_GET_PRIVATE(dip);
9347c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ppmd->domp->lock));
9357c478bd9Sstevel@tonic-gate 	old = reqp->req.ppm_set_power_req.old_level;
9367c478bd9Sstevel@tonic-gate 	new = reqp->req.ppm_set_power_req.new_level;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	/*
9397c478bd9Sstevel@tonic-gate 	 * At power on, the cpus are at full speed.  There is no hardware
9407c478bd9Sstevel@tonic-gate 	 * transition needed for going from unknown to full.  However, the
9417c478bd9Sstevel@tonic-gate 	 * state of the pm framework and cpu driver needs to be adjusted.
9427c478bd9Sstevel@tonic-gate 	 */
9437c478bd9Sstevel@tonic-gate 	if (ppmd->level == PM_LEVEL_UNKNOWN && new == ppmd->highest) {
9447c478bd9Sstevel@tonic-gate 		*result = ret = xcppm_change_power_level(ppmd, 0, new);
9457c478bd9Sstevel@tonic-gate 		if (ret != DDI_SUCCESS) {
9467c478bd9Sstevel@tonic-gate 			DPRINTF(D_CPU, ("%s: Failed to change "
9477c478bd9Sstevel@tonic-gate 			    "power level to %d\n", str, new));
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 		return (ret);
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	if (new == ppmd->level) {
9537c478bd9Sstevel@tonic-gate 		DPRINTF(D_CPU, ("%s: already at power level %d\n", str, new));
9547c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	ppmd->rplvl = new;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	/*
9607c478bd9Sstevel@tonic-gate 	 * A request from lower to higher level transition is granted and
9617c478bd9Sstevel@tonic-gate 	 * made effective on both cpus. For more than two cpu platform model,
9627c478bd9Sstevel@tonic-gate 	 * the following code needs to be modified to remember the rest of
9637c478bd9Sstevel@tonic-gate 	 * the unsoliciting cpus to be rescan'ed.
9647c478bd9Sstevel@tonic-gate 	 * A request from higher to lower must be agreed by all cpus.
9657c478bd9Sstevel@tonic-gate 	 */
9667c478bd9Sstevel@tonic-gate 	for (ppmd = xcppm_cpu.devlist; ppmd; ppmd = ppmd->next) {
9677c478bd9Sstevel@tonic-gate 		if (ppmd->rplvl == new)
9687c478bd9Sstevel@tonic-gate 			continue;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 		if (new < old) {
9717c478bd9Sstevel@tonic-gate 			DPRINTF(D_SOME, ("%s: not all cpus want to go down to "
9727c478bd9Sstevel@tonic-gate 			    "level %d yet\n", str, new));
9737c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
9747c478bd9Sstevel@tonic-gate 		}
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 		/*
9777c478bd9Sstevel@tonic-gate 		 * If a single cpu requests power up, honor the request
9787c478bd9Sstevel@tonic-gate 		 * by powering up both cpus.
9797c478bd9Sstevel@tonic-gate 		 */
9807c478bd9Sstevel@tonic-gate 		if (new > old) {
9817c478bd9Sstevel@tonic-gate 			DPRINTF(D_SOME, ("%s: powering up device(%s@%s, %p) "
9827c478bd9Sstevel@tonic-gate 			    "because of request from dip(%s@%s, %p), "
9837c478bd9Sstevel@tonic-gate 			    "need pm_rescan\n", str, PM_NAME(ppmd->dip),
9847c478bd9Sstevel@tonic-gate 			    PM_ADDR(ppmd->dip), (void *)ppmd->dip,
9857c478bd9Sstevel@tonic-gate 			    PM_NAME(dip), PM_ADDR(dip), (void *)dip))
9867c478bd9Sstevel@tonic-gate 			do_rescan++;
9877c478bd9Sstevel@tonic-gate 			rescan_dip = ppmd->dip;
9887c478bd9Sstevel@tonic-gate 			break;
9897c478bd9Sstevel@tonic-gate 		}
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	ret = xcppm_change_cpu_power(new);
9937c478bd9Sstevel@tonic-gate 	*result = ret;
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
9967c478bd9Sstevel@tonic-gate 		if (reqp->req.ppm_set_power_req.canblock == PM_CANBLOCK_BLOCK)
9977c478bd9Sstevel@tonic-gate 			kmflag = KM_SLEEP;
9987c478bd9Sstevel@tonic-gate 		else
9997c478bd9Sstevel@tonic-gate 			kmflag = KM_NOSLEEP;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 		for (ppmd = xcppm_cpu.devlist; ppmd; ppmd = ppmd->next) {
10027c478bd9Sstevel@tonic-gate 			if (ppmd->dip == dip)
10037c478bd9Sstevel@tonic-gate 				continue;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 			if ((p = kmem_zalloc(sizeof (pm_ppm_devlist_t),
10067c478bd9Sstevel@tonic-gate 			    kmflag)) == NULL) {
10077c478bd9Sstevel@tonic-gate 				break;
10087c478bd9Sstevel@tonic-gate 			}
10097c478bd9Sstevel@tonic-gate 			p->ppd_who = ppmd->dip;
10107c478bd9Sstevel@tonic-gate 			p->ppd_cmpt = ppmd->cmpt;
10117c478bd9Sstevel@tonic-gate 			p->ppd_old_level = old;
10127c478bd9Sstevel@tonic-gate 			p->ppd_new_level = new;
10137c478bd9Sstevel@tonic-gate 			p->ppd_next = devlist;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 			devlist = p;
10167c478bd9Sstevel@tonic-gate 		}
10177c478bd9Sstevel@tonic-gate 		reqp->req.ppm_set_power_req.cookie = (void *) devlist;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 		if (do_rescan > 0)
10207c478bd9Sstevel@tonic-gate 			pm_rescan(rescan_dip);
10217c478bd9Sstevel@tonic-gate 	}
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	return (ret);
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate /*
10287c478bd9Sstevel@tonic-gate  * If powering off and all devices in this domain will now be off,
10297c478bd9Sstevel@tonic-gate  * shut off common power.  If powering up and no devices up yet,
10307c478bd9Sstevel@tonic-gate  * turn on common power.  Always make the requested power level
10317c478bd9Sstevel@tonic-gate  * change for the target device.
10327c478bd9Sstevel@tonic-gate  */
10337c478bd9Sstevel@tonic-gate static int
xcppm_manage_fet(dev_info_t * dip,power_req_t * reqp,int * result)10347c478bd9Sstevel@tonic-gate xcppm_manage_fet(dev_info_t *dip, power_req_t *reqp, int *result)
10357c478bd9Sstevel@tonic-gate {
10367c478bd9Sstevel@tonic-gate #ifdef DEBUG
10377c478bd9Sstevel@tonic-gate 	char *str = "xcppm_manage_fet";
10387c478bd9Sstevel@tonic-gate #endif
10397c478bd9Sstevel@tonic-gate 	int (*pwr_func)(ppm_dev_t *, int, int);
10407c478bd9Sstevel@tonic-gate 	int new, old, cmpt, incr = 0;
10417c478bd9Sstevel@tonic-gate 	ppm_dev_t *ppmd;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	ppmd = PPM_GET_PRIVATE(dip);
10447c478bd9Sstevel@tonic-gate 	DPRINTF(D_FET, ("%s: \"%s\", req %s\n", str,
10457c478bd9Sstevel@tonic-gate 	    ppmd->path, ppm_get_ctlstr(reqp->request_type, ~0)));
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	*result = DDI_SUCCESS;	/* change later for failures */
10487c478bd9Sstevel@tonic-gate 	switch (reqp->request_type) {
10497c478bd9Sstevel@tonic-gate 	case PMR_PPM_SET_POWER:
10507c478bd9Sstevel@tonic-gate 		pwr_func = xcppm_change_power_level;
10517c478bd9Sstevel@tonic-gate 		old = reqp->req.ppm_set_power_req.old_level;
10527c478bd9Sstevel@tonic-gate 		new = reqp->req.ppm_set_power_req.new_level;
10537c478bd9Sstevel@tonic-gate 		cmpt = reqp->req.ppm_set_power_req.cmpt;
10547c478bd9Sstevel@tonic-gate 		break;
10557c478bd9Sstevel@tonic-gate 	case PMR_PPM_POWER_CHANGE_NOTIFY:
10567c478bd9Sstevel@tonic-gate 		pwr_func = xcppm_record_level_change;
10577c478bd9Sstevel@tonic-gate 		old = reqp->req.ppm_notify_level_req.old_level;
10587c478bd9Sstevel@tonic-gate 		new = reqp->req.ppm_notify_level_req.new_level;
10597c478bd9Sstevel@tonic-gate 		cmpt = reqp->req.ppm_notify_level_req.cmpt;
10607c478bd9Sstevel@tonic-gate 		break;
10617c478bd9Sstevel@tonic-gate 	default:
10627c478bd9Sstevel@tonic-gate 		return (*result = DDI_FAILURE);
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	/* This is common code for SET_POWER and POWER_CHANGE_NOTIFY cases */
10677c478bd9Sstevel@tonic-gate 	DPRINTF(D_FET, ("%s: \"%s\", old %d, new %d\n",
10687c478bd9Sstevel@tonic-gate 	    str, ppmd->path, old, new));
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	ASSERT(old == ppmd->level);
10717c478bd9Sstevel@tonic-gate 	if (new == ppmd->level)
10727c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	PPM_LOCK_DOMAIN(ppmd->domp);
10757c478bd9Sstevel@tonic-gate 	/*
10767c478bd9Sstevel@tonic-gate 	 * Devices in this domain are known to have 0 (off) as their
10777c478bd9Sstevel@tonic-gate 	 * lowest power level.  We use this fact to simplify the logic.
10787c478bd9Sstevel@tonic-gate 	 */
10797c478bd9Sstevel@tonic-gate 	if (new > 0) {
10807c478bd9Sstevel@tonic-gate 		if (ppmd->domp->pwr_cnt == 0)
10817c478bd9Sstevel@tonic-gate 			(void) xcppm_gpio_port2(XCPPM_SETBIT, DRVON);
10827c478bd9Sstevel@tonic-gate 		if (old == 0) {
10837c478bd9Sstevel@tonic-gate 			ppmd->domp->pwr_cnt++;
10847c478bd9Sstevel@tonic-gate 			incr = 1;
10857c478bd9Sstevel@tonic-gate 			DPRINTF(D_FET, ("%s: UP cnt = %d\n",
10867c478bd9Sstevel@tonic-gate 			    str, ppmd->domp->pwr_cnt));
10877c478bd9Sstevel@tonic-gate 		}
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	PPM_UNLOCK_DOMAIN(ppmd->domp);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	ASSERT(ppmd->domp->pwr_cnt > 0);
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	if ((*result = (*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS) {
10957c478bd9Sstevel@tonic-gate 		DPRINTF(D_FET, ("%s: \"%s\" power change failed \n",
10967c478bd9Sstevel@tonic-gate 		    str, ppmd->path));
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	PPM_LOCK_DOMAIN(ppmd->domp);
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	/*
11027c478bd9Sstevel@tonic-gate 	 * Decr the power count in two cases:
11037c478bd9Sstevel@tonic-gate 	 *
11047c478bd9Sstevel@tonic-gate 	 *   1) request was to power device down and was successful
11057c478bd9Sstevel@tonic-gate 	 *   2) request was to power up (we pre-incremented count), but failed.
11067c478bd9Sstevel@tonic-gate 	 */
11077c478bd9Sstevel@tonic-gate 	if ((*result == DDI_SUCCESS && ppmd->level == 0) ||
11087c478bd9Sstevel@tonic-gate 	    (*result != DDI_SUCCESS && incr)) {
11097c478bd9Sstevel@tonic-gate 		ASSERT(ppmd->domp->pwr_cnt > 0);
11107c478bd9Sstevel@tonic-gate 		ppmd->domp->pwr_cnt--;
11117c478bd9Sstevel@tonic-gate 		DPRINTF(D_FET, ("%s: DN cnt = %d\n", str, ppmd->domp->pwr_cnt));
11127c478bd9Sstevel@tonic-gate 		if (ppmd->domp->pwr_cnt == 0)
11137c478bd9Sstevel@tonic-gate 			(void) xcppm_gpio_port2(XCPPM_CLRBIT, DRVON);
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	PPM_UNLOCK_DOMAIN(ppmd->domp);
11177c478bd9Sstevel@tonic-gate 	ASSERT(ppmd->domp->pwr_cnt >= 0);
11187c478bd9Sstevel@tonic-gate 	return (*result == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate /*
11237c478bd9Sstevel@tonic-gate  * Since UPA64S relies on PCI B staying at nominal 33MHz in order to
11247c478bd9Sstevel@tonic-gate  * have its interrupt pulse function properly, we ensure
11257c478bd9Sstevel@tonic-gate  * - Lowering PCI B only if UPA64S is at low power, otherwise defer
11267c478bd9Sstevel@tonic-gate  *   the action until UPA64S goes down; hence right after UPA64S goes
11277c478bd9Sstevel@tonic-gate  *   down, perform the deferred action for PCI B;
11287c478bd9Sstevel@tonic-gate  * - Always raise PCI B power prior to raising UPA64S power.
11297c478bd9Sstevel@tonic-gate  *
11307c478bd9Sstevel@tonic-gate  * Both UPA64S and PCI B devices are considered each other's dependency
11317c478bd9Sstevel@tonic-gate  * device whenever actual power transition is handled (PMR_PPM_SET_POWER).
11327c478bd9Sstevel@tonic-gate  */
11337c478bd9Sstevel@tonic-gate static int
xcppm_manage_pciupa(dev_info_t * dip,power_req_t * reqp,int * result)11347c478bd9Sstevel@tonic-gate xcppm_manage_pciupa(dev_info_t *dip, power_req_t *reqp, int *result)
11357c478bd9Sstevel@tonic-gate {
11367c478bd9Sstevel@tonic-gate #ifdef DEBUG
11377c478bd9Sstevel@tonic-gate 	char *str = "xcppm_manage_pciupa";
11387c478bd9Sstevel@tonic-gate #endif
11397c478bd9Sstevel@tonic-gate 	int (*pwr_func)(ppm_dev_t *, int, int);
11407c478bd9Sstevel@tonic-gate 	uint_t flags = 0, co_flags = 0;
11417c478bd9Sstevel@tonic-gate 	ppm_dev_t *ppmd, *codev;
11427c478bd9Sstevel@tonic-gate 	int new, cmpt, retval;
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	ppmd = PPM_GET_PRIVATE(dip);
11457c478bd9Sstevel@tonic-gate 	DPRINTF(D_PCIUPA, ("%s: \"%s\", req %s\n", str,
11467c478bd9Sstevel@tonic-gate 	    ppmd->path, ppm_get_ctlstr(reqp->request_type, ~0)));
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	*result = DDI_SUCCESS;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	switch (reqp->request_type) {
11517c478bd9Sstevel@tonic-gate 	case PMR_PPM_SET_POWER:
11527c478bd9Sstevel@tonic-gate 		pwr_func = xcppm_change_power_level;
11537c478bd9Sstevel@tonic-gate 		new = reqp->req.ppm_set_power_req.new_level;
11547c478bd9Sstevel@tonic-gate 		cmpt = reqp->req.ppm_set_power_req.cmpt;
11557c478bd9Sstevel@tonic-gate 		break;
11567c478bd9Sstevel@tonic-gate 	case PMR_PPM_POWER_CHANGE_NOTIFY:
11577c478bd9Sstevel@tonic-gate 		pwr_func = xcppm_record_level_change;
11587c478bd9Sstevel@tonic-gate 		new = reqp->req.ppm_notify_level_req.new_level;
11597c478bd9Sstevel@tonic-gate 		cmpt = reqp->req.ppm_notify_level_req.cmpt;
11607c478bd9Sstevel@tonic-gate 		break;
11617c478bd9Sstevel@tonic-gate 	default:
11627c478bd9Sstevel@tonic-gate 		*result = DDI_FAILURE;
11637c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
11647c478bd9Sstevel@tonic-gate 	}
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	/* Common code for SET_POWER and POWER_CHANGE_NOTIFY cases */
11677c478bd9Sstevel@tonic-gate 	ASSERT(ppmd);	/* since it should be locked already */
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	if (new == ppmd->level)
11707c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	DPRINTF(D_PCIUPA, ("%s: \"%s\", levels: current %d, new %d\n",
11737c478bd9Sstevel@tonic-gate 	    str, ppmd->path, ppmd->level, new));
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	/*
11767c478bd9Sstevel@tonic-gate 	 * find power-wise co-related device
11777c478bd9Sstevel@tonic-gate 	 */
11787c478bd9Sstevel@tonic-gate 	flags =  ppmd->flags;
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate #ifdef DEBUG
11817c478bd9Sstevel@tonic-gate 	if (flags & ~(XCPPMF_PCIB|XCPPMF_UPA))
1182360e6f5eSmathue 		DPRINTF(D_ERROR, ("%s: invalid ppmd->flags value 0x%x\n", str,
11837c478bd9Sstevel@tonic-gate 		    ppmd->flags));
11847c478bd9Sstevel@tonic-gate #endif
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	if (flags == XCPPMF_UPA)
11877c478bd9Sstevel@tonic-gate 		co_flags = XCPPMF_PCIB;
11887c478bd9Sstevel@tonic-gate 	else if (flags == XCPPMF_PCIB)
11897c478bd9Sstevel@tonic-gate 		co_flags = XCPPMF_UPA;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	for (codev = ppmd->domp->devlist; codev; codev = codev->next)
11927c478bd9Sstevel@tonic-gate 		if ((codev->cmpt == 0) && (codev->flags == co_flags))
11937c478bd9Sstevel@tonic-gate 			break;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	if (new > ppmd->level) {
11967c478bd9Sstevel@tonic-gate 		/*
11977c478bd9Sstevel@tonic-gate 		 * Raise power level -
11987c478bd9Sstevel@tonic-gate 		 * pre-raising: upa ensure pci is powered up.
11997c478bd9Sstevel@tonic-gate 		 */
12007c478bd9Sstevel@tonic-gate 		if ((flags == XCPPMF_UPA) && codev &&
12017c478bd9Sstevel@tonic-gate 		    (codev->level != codev->highest)) {
12027c478bd9Sstevel@tonic-gate 			if ((retval = xcppm_change_power_level(codev,
12037c478bd9Sstevel@tonic-gate 			    0, codev->highest)) != DDI_SUCCESS &&
12047c478bd9Sstevel@tonic-gate 			    codev->level != codev->highest) {
12057c478bd9Sstevel@tonic-gate 				*result = retval;
12067c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
12077c478bd9Sstevel@tonic-gate 			}
12087c478bd9Sstevel@tonic-gate 		}
12097c478bd9Sstevel@tonic-gate 		if ((retval = (*pwr_func)(ppmd, 0, new)) != DDI_SUCCESS) {
12107c478bd9Sstevel@tonic-gate 			*result = retval;
12117c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
12127c478bd9Sstevel@tonic-gate 		}
12137c478bd9Sstevel@tonic-gate 	} else if (new < ppmd->level) {
12147c478bd9Sstevel@tonic-gate 		/*
12157c478bd9Sstevel@tonic-gate 		 * Lower power level
12167c478bd9Sstevel@tonic-gate 		 *
12177c478bd9Sstevel@tonic-gate 		 * once upa is attached, pci checks upa level:
12187c478bd9Sstevel@tonic-gate 		 * if upa is at high level, defer the request and return.
12197c478bd9Sstevel@tonic-gate 		 * otherwise, set power level then check and lower pci level.
12207c478bd9Sstevel@tonic-gate 		 */
12217c478bd9Sstevel@tonic-gate 		if ((flags == XCPPMF_PCIB) && codev &&
12227c478bd9Sstevel@tonic-gate 		    (codev->level != codev->lowest)) {
12237c478bd9Sstevel@tonic-gate 			ppmd->rplvl = new;
12247c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 		if ((retval = (*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS &&
12277c478bd9Sstevel@tonic-gate 		    ppmd->level != new) {
12287c478bd9Sstevel@tonic-gate 			*result = retval;
12297c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
12307c478bd9Sstevel@tonic-gate 		}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 		if (flags == XCPPMF_UPA) {
12337c478bd9Sstevel@tonic-gate 			if (codev && (codev->rplvl != PM_LEVEL_UNKNOWN) &&
12347c478bd9Sstevel@tonic-gate 			    (codev->rplvl < codev->level)) {
12357c478bd9Sstevel@tonic-gate 				DPRINTF(D_PCIUPA, ("%s: codev \"%s\" "
12367c478bd9Sstevel@tonic-gate 				    "rplvl %d level %d\n", str, codev->path,
12377c478bd9Sstevel@tonic-gate 				    codev->rplvl, codev->level));
12387c478bd9Sstevel@tonic-gate 				if ((retval = xcppm_change_power_level(
12397c478bd9Sstevel@tonic-gate 				    codev, 0, codev->rplvl)) != DDI_SUCCESS) {
12407c478bd9Sstevel@tonic-gate 					*result = retval;
12417c478bd9Sstevel@tonic-gate 					return (DDI_FAILURE);
12427c478bd9Sstevel@tonic-gate 				}
12437c478bd9Sstevel@tonic-gate 			}
12447c478bd9Sstevel@tonic-gate 		}
12457c478bd9Sstevel@tonic-gate 	}
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
12487c478bd9Sstevel@tonic-gate }
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate /*
12527c478bd9Sstevel@tonic-gate  * When all of the children of the 1394 nexus are idle, a call will be
12537c478bd9Sstevel@tonic-gate  * made to the nexus driver's own power entry point to lower power.  Ppm
12547c478bd9Sstevel@tonic-gate  * intercepts this and kills 1394 cable power (since the driver doesn't
12557c478bd9Sstevel@tonic-gate  * have access to the required register).  Similar logic applies when
12567c478bd9Sstevel@tonic-gate  * coming up from the state where all the children were off.
12577c478bd9Sstevel@tonic-gate  */
12587c478bd9Sstevel@tonic-gate static int
xcppm_manage_1394(dev_info_t * dip,power_req_t * reqp,int * result)12597c478bd9Sstevel@tonic-gate xcppm_manage_1394(dev_info_t *dip, power_req_t *reqp, int *result)
12607c478bd9Sstevel@tonic-gate {
12617c478bd9Sstevel@tonic-gate #ifdef DEBUG
12627c478bd9Sstevel@tonic-gate 	char *str = "xcppm_manage_1394";
12637c478bd9Sstevel@tonic-gate #endif
12647c478bd9Sstevel@tonic-gate 	int (*pwr_func)(ppm_dev_t *, int, int);
12657c478bd9Sstevel@tonic-gate 	int new, old, cmpt;
12667c478bd9Sstevel@tonic-gate 	ppm_dev_t *ppmd;
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	ppmd = PPM_GET_PRIVATE(dip);
12697c478bd9Sstevel@tonic-gate 	DPRINTF(D_1394, ("%s: \"%s\", req %s\n", str,
12707c478bd9Sstevel@tonic-gate 	    ppmd->path, ppm_get_ctlstr(reqp->request_type, ~0)));
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	switch (reqp->request_type) {
12737c478bd9Sstevel@tonic-gate 	case PMR_PPM_SET_POWER:
12747c478bd9Sstevel@tonic-gate 		pwr_func = xcppm_change_power_level;
12757c478bd9Sstevel@tonic-gate 		old = reqp->req.ppm_set_power_req.old_level;
12767c478bd9Sstevel@tonic-gate 		new = reqp->req.ppm_set_power_req.new_level;
12777c478bd9Sstevel@tonic-gate 		cmpt = reqp->req.ppm_set_power_req.cmpt;
12787c478bd9Sstevel@tonic-gate 		break;
12797c478bd9Sstevel@tonic-gate 	case PMR_PPM_POWER_CHANGE_NOTIFY:
12807c478bd9Sstevel@tonic-gate 		pwr_func = xcppm_record_level_change;
12817c478bd9Sstevel@tonic-gate 		old = reqp->req.ppm_notify_level_req.old_level;
12827c478bd9Sstevel@tonic-gate 		new = reqp->req.ppm_notify_level_req.new_level;
12837c478bd9Sstevel@tonic-gate 		cmpt = reqp->req.ppm_notify_level_req.cmpt;
12847c478bd9Sstevel@tonic-gate 		break;
12857c478bd9Sstevel@tonic-gate 	default:
12867c478bd9Sstevel@tonic-gate 		return (*result = DDI_FAILURE);
12877c478bd9Sstevel@tonic-gate 	}
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	/* Common code for SET_POWER and POWER_CHANGE_NOTIFY cases */
12917c478bd9Sstevel@tonic-gate 	DPRINTF(D_1394, ("%s: dev %s@%s, old %d new %d\n", str,
12927c478bd9Sstevel@tonic-gate 	    ddi_binding_name(dip), ddi_get_name_addr(dip), old, new));
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	ASSERT(ppmd);	/* since it must already be locked */
12957c478bd9Sstevel@tonic-gate 	ASSERT(old == ppmd->level);
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	if (new == ppmd->level)
12987c478bd9Sstevel@tonic-gate 		return (*result = DDI_SUCCESS);
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	/* the reduce power case */
13017c478bd9Sstevel@tonic-gate 	if (cmpt == 0 && new < ppmd->level) {
13027c478bd9Sstevel@tonic-gate 		if ((*result =
13037c478bd9Sstevel@tonic-gate 		    (*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS) {
13047c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
13057c478bd9Sstevel@tonic-gate 		}
13067c478bd9Sstevel@tonic-gate 		if (new == ppmd->lowest)
13077c478bd9Sstevel@tonic-gate 			(void) xcppm_gpio_port2(XCPPM_CLRBIT, CPEN);
13087c478bd9Sstevel@tonic-gate 		ppmd->level = new;
13097c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
13107c478bd9Sstevel@tonic-gate 	}
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	/* the increase power case */
13137c478bd9Sstevel@tonic-gate 	if (cmpt == 0 && new > ppmd->level) {
13147c478bd9Sstevel@tonic-gate 		if (ppmd->level == ppmd->lowest) {
13157c478bd9Sstevel@tonic-gate 			(void) xcppm_gpio_port2(XCPPM_SETBIT, CPEN);
13167c478bd9Sstevel@tonic-gate 			delay(1);
13177c478bd9Sstevel@tonic-gate 		}
13187c478bd9Sstevel@tonic-gate 		/*
13197c478bd9Sstevel@tonic-gate 		 * Even if pwr_func fails we need to check current level again
13207c478bd9Sstevel@tonic-gate 		 * because it could have been changed by an intervening
13217c478bd9Sstevel@tonic-gate 		 * POWER_CHANGE_NOTIFY operation.
13227c478bd9Sstevel@tonic-gate 		 */
13237c478bd9Sstevel@tonic-gate 		if ((*result =
13247c478bd9Sstevel@tonic-gate 		    (*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS &&
13257c478bd9Sstevel@tonic-gate 		    ppmd->level == ppmd->lowest) {
13267c478bd9Sstevel@tonic-gate 			(void) xcppm_gpio_port2(XCPPM_CLRBIT, CPEN);
13277c478bd9Sstevel@tonic-gate 		} else {
13287c478bd9Sstevel@tonic-gate 			ppmd->level = new;
13297c478bd9Sstevel@tonic-gate 		}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 		return (*result == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
13327c478bd9Sstevel@tonic-gate 	}
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	/*
13357c478bd9Sstevel@tonic-gate 	 * We get here if component was non-zero.  This is not what we
13367c478bd9Sstevel@tonic-gate 	 * expect.  Let the device deal with it and just pass back the
13377c478bd9Sstevel@tonic-gate 	 * result.
13387c478bd9Sstevel@tonic-gate 	 */
13397c478bd9Sstevel@tonic-gate 	*result = xcppm_change_power_level(ppmd, cmpt, new);
13407c478bd9Sstevel@tonic-gate 	return (*result == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
13417c478bd9Sstevel@tonic-gate }
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate /*
13457c478bd9Sstevel@tonic-gate  * lock, unlock, or trylock for one power mutex
13467c478bd9Sstevel@tonic-gate  */
13477c478bd9Sstevel@tonic-gate static void
xcppm_lock_one(ppm_dev_t * ppmd,power_req_t * reqp,int * iresp)13487c478bd9Sstevel@tonic-gate xcppm_lock_one(ppm_dev_t *ppmd, power_req_t *reqp, int *iresp)
13497c478bd9Sstevel@tonic-gate {
13507c478bd9Sstevel@tonic-gate 	switch (reqp->request_type) {
13517c478bd9Sstevel@tonic-gate 	case PMR_PPM_LOCK_POWER:
1352*3fe80ca4SDan Cross 		pm_lock_power_single(ppmd->dip);
13537c478bd9Sstevel@tonic-gate 		break;
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	case PMR_PPM_UNLOCK_POWER:
1356*3fe80ca4SDan Cross 		pm_unlock_power_single(ppmd->dip);
13577c478bd9Sstevel@tonic-gate 		break;
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	case PMR_PPM_TRY_LOCK_POWER:
1360*3fe80ca4SDan Cross 		*iresp = pm_try_locking_power_single(ppmd->dip);
13617c478bd9Sstevel@tonic-gate 		break;
13627c478bd9Sstevel@tonic-gate 	}
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate /*
13677c478bd9Sstevel@tonic-gate  * lock, unlock, or trylock all devices within a domain.
13687c478bd9Sstevel@tonic-gate  */
13697c478bd9Sstevel@tonic-gate static void
xcppm_lock_all(ppm_domain_t * domp,power_req_t * reqp,int * iresp)13707c478bd9Sstevel@tonic-gate xcppm_lock_all(ppm_domain_t *domp, power_req_t *reqp, int *iresp)
13717c478bd9Sstevel@tonic-gate {
13727c478bd9Sstevel@tonic-gate 	/*
13737c478bd9Sstevel@tonic-gate 	 * To simplify the implementation we let all the devices
13747c478bd9Sstevel@tonic-gate 	 * in the domain be represented by a single device (dip).
13757c478bd9Sstevel@tonic-gate 	 * We use the first device in the domain's devlist.  This
13767c478bd9Sstevel@tonic-gate 	 * is safe because we return with the domain lock held
13777c478bd9Sstevel@tonic-gate 	 * which prevents the list from changing.
13787c478bd9Sstevel@tonic-gate 	 */
13797c478bd9Sstevel@tonic-gate 	if (reqp->request_type == PMR_PPM_LOCK_POWER) {
13807c478bd9Sstevel@tonic-gate 		if (!MUTEX_HELD(&domp->lock))
13817c478bd9Sstevel@tonic-gate 			mutex_enter(&domp->lock);
13827c478bd9Sstevel@tonic-gate 		domp->refcnt++;
13837c478bd9Sstevel@tonic-gate 		ASSERT(domp->devlist != NULL);
1384*3fe80ca4SDan Cross 		pm_lock_power_single(domp->devlist->dip);
13857c478bd9Sstevel@tonic-gate 		/* domain lock remains held */
13867c478bd9Sstevel@tonic-gate 		return;
13877c478bd9Sstevel@tonic-gate 	} else if (reqp->request_type == PMR_PPM_UNLOCK_POWER) {
13887c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&domp->lock));
13897c478bd9Sstevel@tonic-gate 		ASSERT(domp->devlist != NULL);
1390*3fe80ca4SDan Cross 		pm_unlock_power_single(domp->devlist->dip);
13917c478bd9Sstevel@tonic-gate 		if (--domp->refcnt == 0)
13927c478bd9Sstevel@tonic-gate 			mutex_exit(&domp->lock);
13937c478bd9Sstevel@tonic-gate 		return;
13947c478bd9Sstevel@tonic-gate 	}
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	ASSERT(reqp->request_type == PMR_PPM_TRY_LOCK_POWER);
13977c478bd9Sstevel@tonic-gate 	if (!MUTEX_HELD(&domp->lock))
13987c478bd9Sstevel@tonic-gate 		if (!mutex_tryenter(&domp->lock)) {
13997c478bd9Sstevel@tonic-gate 			*iresp = 0;
14007c478bd9Sstevel@tonic-gate 			return;
14017c478bd9Sstevel@tonic-gate 		}
1402*3fe80ca4SDan Cross 	*iresp = pm_try_locking_power_single(domp->devlist->dip);
14037c478bd9Sstevel@tonic-gate 	if (*iresp)
14047c478bd9Sstevel@tonic-gate 		domp->refcnt++;
14057c478bd9Sstevel@tonic-gate 	else
14067c478bd9Sstevel@tonic-gate 		mutex_exit(&domp->lock);
14077c478bd9Sstevel@tonic-gate }
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate /*
14117c478bd9Sstevel@tonic-gate  * The pm framework calls us here to manage power for a device.
14127c478bd9Sstevel@tonic-gate  * We maintain state which tells us whether we need to turn off/on
14137c478bd9Sstevel@tonic-gate  * system board power components based on the status of all the devices
14147c478bd9Sstevel@tonic-gate  * sharing a component.
14157c478bd9Sstevel@tonic-gate  *
14167c478bd9Sstevel@tonic-gate  */
14177c478bd9Sstevel@tonic-gate /* ARGSUSED */
14187c478bd9Sstevel@tonic-gate static int
xcppm_ctlops(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result)14197c478bd9Sstevel@tonic-gate xcppm_ctlops(dev_info_t *dip, dev_info_t *rdip,
14207c478bd9Sstevel@tonic-gate     ddi_ctl_enum_t ctlop, void *arg, void *result)
14217c478bd9Sstevel@tonic-gate {
14227c478bd9Sstevel@tonic-gate 	power_req_t *reqp = arg;
14237c478bd9Sstevel@tonic-gate 	xcppm_unit_t *unitp;
14247c478bd9Sstevel@tonic-gate 	ppm_domain_t *domp;
14257c478bd9Sstevel@tonic-gate 	ppm_dev_t *ppmd;
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate #ifdef DEBUG
14287c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], *ctlstr, *str = "xcppm_ctlops";
14297c478bd9Sstevel@tonic-gate 	uint_t mask = ppm_debug & (D_CTLOPS1 | D_CTLOPS2);
14307c478bd9Sstevel@tonic-gate 	if (mask && (ctlstr = ppm_get_ctlstr(reqp->request_type, mask))) {
14317c478bd9Sstevel@tonic-gate 		prom_printf("%s: \"%s\", %s\n", str,
14327c478bd9Sstevel@tonic-gate 		    ddi_pathname(rdip, path), ctlstr);
14337c478bd9Sstevel@tonic-gate 	}
14347c478bd9Sstevel@tonic-gate #endif
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	if (ctlop != DDI_CTLOPS_POWER)
14377c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	switch (reqp->request_type) {
14407c478bd9Sstevel@tonic-gate 	case PMR_PPM_UNMANAGE:
14417c478bd9Sstevel@tonic-gate 	case PMR_PPM_PRE_PROBE:
14427c478bd9Sstevel@tonic-gate 	case PMR_PPM_POST_PROBE:
14437c478bd9Sstevel@tonic-gate 	case PMR_PPM_PRE_ATTACH:
14447c478bd9Sstevel@tonic-gate 	case PMR_PPM_PRE_DETACH:
14457c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 	/*
14487c478bd9Sstevel@tonic-gate 	 * There is no hardware configuration required to be done on this
14497c478bd9Sstevel@tonic-gate 	 * platform prior to installing drivers.
14507c478bd9Sstevel@tonic-gate 	 */
14517c478bd9Sstevel@tonic-gate 	case PMR_PPM_INIT_CHILD:
14527c478bd9Sstevel@tonic-gate 	case PMR_PPM_UNINIT_CHILD:
14537c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	case PMR_PPM_ALL_LOWEST:
14567c478bd9Sstevel@tonic-gate 		DPRINTF(D_LOWEST, ("%s: all devices at lowest power = %d\n",
14577c478bd9Sstevel@tonic-gate 		    str, reqp->req.ppm_all_lowest_req.mode));
14587c478bd9Sstevel@tonic-gate 		if (reqp->req.ppm_all_lowest_req.mode == PM_ALL_LOWEST) {
14597c478bd9Sstevel@tonic-gate 			unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
14607c478bd9Sstevel@tonic-gate 			mutex_enter(&unitp->unit_lock);
14617c478bd9Sstevel@tonic-gate 			if (unitp->state & XCPPM_ST_SUSPENDED) {
14627c478bd9Sstevel@tonic-gate 				mutex_exit(&unitp->unit_lock);
14637c478bd9Sstevel@tonic-gate 				return (DDI_SUCCESS);
14647c478bd9Sstevel@tonic-gate 			}
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 			xcppm_set_led(PPM_LEDON);
14677c478bd9Sstevel@tonic-gate 			unitp->led_tid = timeout(xcppm_blink_led,
14687c478bd9Sstevel@tonic-gate 			    (void *)PPM_LEDON, PPM_LEDON_INTERVAL);
14697c478bd9Sstevel@tonic-gate 			mutex_exit(&unitp->unit_lock);
14707c478bd9Sstevel@tonic-gate 			DPRINTF(D_LOWEST, ("%s: LED blink started\n", str));
14717c478bd9Sstevel@tonic-gate 		} else {
14727c478bd9Sstevel@tonic-gate 			xcppm_freeze_led((void *)PPM_LEDON);
14737c478bd9Sstevel@tonic-gate 			DPRINTF(D_LOWEST, ("%s: LED freeze ON\n", str));
14747c478bd9Sstevel@tonic-gate 		}
14757c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 	case PMR_PPM_POST_ATTACH:
14787c478bd9Sstevel@tonic-gate 		/*
14797c478bd9Sstevel@tonic-gate 		 * After a successful attach, if we haven't already created
14807c478bd9Sstevel@tonic-gate 		 * our private data structure for this device, ppm_get_dev()
14817c478bd9Sstevel@tonic-gate 		 * will force it to be created.
14827c478bd9Sstevel@tonic-gate 		 */
14837c478bd9Sstevel@tonic-gate 		ppmd = PPM_GET_PRIVATE(rdip);
14847c478bd9Sstevel@tonic-gate 		if (reqp->req.ppm_config_req.result != DDI_SUCCESS) {
14857c478bd9Sstevel@tonic-gate 			if (ppmd)
14867c478bd9Sstevel@tonic-gate 				ppm_rem_dev(rdip);
14877c478bd9Sstevel@tonic-gate 		} else if (!ppmd) {
14887c478bd9Sstevel@tonic-gate 			domp = ppm_lookup_dev(rdip);
14897c478bd9Sstevel@tonic-gate 			ASSERT(domp);
14907c478bd9Sstevel@tonic-gate 			(void) ppm_get_dev(rdip, domp);
14917c478bd9Sstevel@tonic-gate 		}
14927c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	case PMR_PPM_POST_DETACH:
14957c478bd9Sstevel@tonic-gate 		xcppm_detach_ctlop(rdip, reqp);
14967c478bd9Sstevel@tonic-gate 		*(int *)result = DDI_SUCCESS;
14977c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	case PMR_PPM_PRE_RESUME:
15007c478bd9Sstevel@tonic-gate 		xcppm_resume_ctlop(rdip, reqp);
15017c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	case PMR_PPM_UNLOCK_POWER:
15047c478bd9Sstevel@tonic-gate 	case PMR_PPM_TRY_LOCK_POWER:
15057c478bd9Sstevel@tonic-gate 	case PMR_PPM_LOCK_POWER:
15067c478bd9Sstevel@tonic-gate 		ppmd = PPM_GET_PRIVATE(rdip);
15077c478bd9Sstevel@tonic-gate 		if (ppmd)
15087c478bd9Sstevel@tonic-gate 			domp = ppmd->domp;
15097c478bd9Sstevel@tonic-gate 		else if (reqp->request_type != PMR_PPM_UNLOCK_POWER) {
15107c478bd9Sstevel@tonic-gate 			domp = ppm_lookup_dev(rdip);
15117c478bd9Sstevel@tonic-gate 			ASSERT(domp);
15127c478bd9Sstevel@tonic-gate 			ppmd = ppm_get_dev(rdip, domp);
15137c478bd9Sstevel@tonic-gate 		}
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 		ASSERT(domp->dflags == PPMD_LOCK_ALL ||
15167c478bd9Sstevel@tonic-gate 		    domp->dflags == PPMD_LOCK_ONE);
15177c478bd9Sstevel@tonic-gate 		DPRINTF(D_LOCKS, ("xcppm_lock_%s: \"%s\", %s\n",
15187c478bd9Sstevel@tonic-gate 		    (domp->dflags == PPMD_LOCK_ALL) ? "all" : "one",
15197c478bd9Sstevel@tonic-gate 		    ppmd->path, ppm_get_ctlstr(reqp->request_type, D_LOCKS)));
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 		if (domp->dflags == PPMD_LOCK_ALL)
15227c478bd9Sstevel@tonic-gate 			xcppm_lock_all(domp, reqp, result);
15237c478bd9Sstevel@tonic-gate 		else
15247c478bd9Sstevel@tonic-gate 			xcppm_lock_one(ppmd, reqp, result);
15257c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	case PMR_PPM_POWER_LOCK_OWNER:
15287c478bd9Sstevel@tonic-gate 		ASSERT(reqp->req.ppm_power_lock_owner_req.who == rdip);
15297c478bd9Sstevel@tonic-gate 		ppmd = PPM_GET_PRIVATE(rdip);
15307c478bd9Sstevel@tonic-gate 		if (ppmd)
15317c478bd9Sstevel@tonic-gate 			domp = ppmd->domp;
15327c478bd9Sstevel@tonic-gate 		else {
15337c478bd9Sstevel@tonic-gate 			domp = ppm_lookup_dev(rdip);
15347c478bd9Sstevel@tonic-gate 			ASSERT(domp);
15357c478bd9Sstevel@tonic-gate 			ppmd = ppm_get_dev(rdip, domp);
15367c478bd9Sstevel@tonic-gate 		}
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 		/*
15397c478bd9Sstevel@tonic-gate 		 * In case of LOCK_ALL, effective owner of the power lock
15407c478bd9Sstevel@tonic-gate 		 * is the owner of the domain lock. otherwise, it is the owner
15417c478bd9Sstevel@tonic-gate 		 * of the power lock.
15427c478bd9Sstevel@tonic-gate 		 */
15437c478bd9Sstevel@tonic-gate 		if (domp->dflags & PPMD_LOCK_ALL)
15447c478bd9Sstevel@tonic-gate 			reqp->req.ppm_power_lock_owner_req.owner =
15457c478bd9Sstevel@tonic-gate 			    mutex_owner(&domp->lock);
15467c478bd9Sstevel@tonic-gate 		else {
15477c478bd9Sstevel@tonic-gate 			reqp->req.ppm_power_lock_owner_req.owner =
15487c478bd9Sstevel@tonic-gate 			    DEVI(rdip)->devi_busy_thread;
15497c478bd9Sstevel@tonic-gate 		}
15507c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	default:
15537c478bd9Sstevel@tonic-gate 		ppmd = PPM_GET_PRIVATE(rdip);
15547c478bd9Sstevel@tonic-gate 		if (ppmd == NULL) {
15557c478bd9Sstevel@tonic-gate 			domp = ppm_lookup_dev(rdip);
15567c478bd9Sstevel@tonic-gate 			ASSERT(domp);
15577c478bd9Sstevel@tonic-gate 			ppmd = ppm_get_dev(rdip, domp);
15587c478bd9Sstevel@tonic-gate 		}
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate #ifdef DEBUG
15617c478bd9Sstevel@tonic-gate 		if ((reqp->request_type == PMR_PPM_SET_POWER) &&
15627c478bd9Sstevel@tonic-gate 		    (ppm_debug & D_SETPWR)) {
15637c478bd9Sstevel@tonic-gate 			prom_printf("%s: \"%s\", PMR_PPM_SET_POWER\n",
15647c478bd9Sstevel@tonic-gate 			    str, ppmd->path);
15657c478bd9Sstevel@tonic-gate 		}
15667c478bd9Sstevel@tonic-gate #endif
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 		if (ppmd->domp == &xcppm_cpu)
15697c478bd9Sstevel@tonic-gate 			return (xcppm_manage_cpus(rdip, reqp, result));
15707c478bd9Sstevel@tonic-gate 		else if (ppmd->domp == &xcppm_fet)
15717c478bd9Sstevel@tonic-gate 			return (xcppm_manage_fet(rdip, reqp, result));
15727c478bd9Sstevel@tonic-gate 		else if (ppmd->domp == &xcppm_upa)
15737c478bd9Sstevel@tonic-gate 			return (xcppm_manage_pciupa(rdip, reqp, result));
15747c478bd9Sstevel@tonic-gate 		else {
15757c478bd9Sstevel@tonic-gate 			ASSERT(ppmd->domp == &xcppm_1394);
15767c478bd9Sstevel@tonic-gate 			return (xcppm_manage_1394(rdip, reqp, result));
15777c478bd9Sstevel@tonic-gate 		}
15787c478bd9Sstevel@tonic-gate 	}
15797c478bd9Sstevel@tonic-gate }
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate /*
15837c478bd9Sstevel@tonic-gate  * Initialize our private version of real power level
15847c478bd9Sstevel@tonic-gate  * as well as lowest and highest levels the device supports;
15857c478bd9Sstevel@tonic-gate  * see ppmf and ppm_add_dev
15867c478bd9Sstevel@tonic-gate  */
15877c478bd9Sstevel@tonic-gate static void
xcppm_dev_init(ppm_dev_t * ppmd)15887c478bd9Sstevel@tonic-gate xcppm_dev_init(ppm_dev_t *ppmd)
15897c478bd9Sstevel@tonic-gate {
15907c478bd9Sstevel@tonic-gate 	struct pm_component *dcomps;
15917c478bd9Sstevel@tonic-gate 	struct pm_comp *pm_comp;
15927c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
15937c478bd9Sstevel@tonic-gate 	int maxi;
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ppmd->domp->lock));
15967c478bd9Sstevel@tonic-gate 	ppmd->level = PM_LEVEL_UNKNOWN;
15977c478bd9Sstevel@tonic-gate 	ppmd->rplvl = PM_LEVEL_UNKNOWN;
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	dip = ppmd->dip;
16007c478bd9Sstevel@tonic-gate 	/*
16017c478bd9Sstevel@tonic-gate 	 * ppm exists to handle power-manageable devices which require
16027c478bd9Sstevel@tonic-gate 	 * special handling on the current platform.  However, a
16037c478bd9Sstevel@tonic-gate 	 * driver for such a device may choose not to support power
16047c478bd9Sstevel@tonic-gate 	 * management on a particular load/attach.  In this case we
16057c478bd9Sstevel@tonic-gate 	 * we create a structure to represent a single-component device
16067c478bd9Sstevel@tonic-gate 	 * for which "level" = PM_LEVEL_UNKNOWN and "lowest" = 0
16077c478bd9Sstevel@tonic-gate 	 * are effectively constant.
16087c478bd9Sstevel@tonic-gate 	 */
16097c478bd9Sstevel@tonic-gate 	if (PM_GET_PM_INFO(dip)) {
16107c478bd9Sstevel@tonic-gate 		dcomps = DEVI(dip)->devi_pm_components;
16117c478bd9Sstevel@tonic-gate 		pm_comp = &dcomps[ppmd->cmpt].pmc_comp;
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 		ppmd->lowest = pm_comp->pmc_lvals[0];
16147c478bd9Sstevel@tonic-gate 		ASSERT(ppmd->lowest >= 0);
16157c478bd9Sstevel@tonic-gate 		maxi = pm_comp->pmc_numlevels - 1;
16167c478bd9Sstevel@tonic-gate 		ppmd->highest = pm_comp->pmc_lvals[maxi];
16177c478bd9Sstevel@tonic-gate 	}
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	/*
16207c478bd9Sstevel@tonic-gate 	 * add any domain-specific initialization here
16217c478bd9Sstevel@tonic-gate 	 */
16227c478bd9Sstevel@tonic-gate 	if (ppmd->domp == &xcppm_fet) {
16237c478bd9Sstevel@tonic-gate 		/*
16247c478bd9Sstevel@tonic-gate 		 * when a new device is added to domain_powefet
16257c478bd9Sstevel@tonic-gate 		 * it is counted here as being powered up.
16267c478bd9Sstevel@tonic-gate 		 */
16277c478bd9Sstevel@tonic-gate 		ppmd->domp->pwr_cnt++;
16287c478bd9Sstevel@tonic-gate 		DPRINTF(D_FET, ("xcppm_dev_init: UP cnt = %d\n",
16297c478bd9Sstevel@tonic-gate 		    ppmd->domp->pwr_cnt));
16307c478bd9Sstevel@tonic-gate 	} else if (ppmd->domp == &xcppm_upa) {
16317c478bd9Sstevel@tonic-gate 		/*
16327c478bd9Sstevel@tonic-gate 		 * There may be a better way to determine the device type
16337c478bd9Sstevel@tonic-gate 		 * instead of comparing to hard coded string names.
16347c478bd9Sstevel@tonic-gate 		 */
16357c478bd9Sstevel@tonic-gate 		if (strstr(ppmd->path, "pci@8,700000"))
16367c478bd9Sstevel@tonic-gate 			ppmd->flags = XCPPMF_PCIB;
16377c478bd9Sstevel@tonic-gate 		else if (strstr(ppmd->path, "upa@8,480000"))
16387c478bd9Sstevel@tonic-gate 			ppmd->flags = XCPPMF_UPA;
16397c478bd9Sstevel@tonic-gate 	}
16407c478bd9Sstevel@tonic-gate }
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate /*
16447c478bd9Sstevel@tonic-gate  * see ppmf and ppm_rem_dev
16457c478bd9Sstevel@tonic-gate  */
16467c478bd9Sstevel@tonic-gate static void
xcppm_dev_fini(ppm_dev_t * ppmd)16477c478bd9Sstevel@tonic-gate xcppm_dev_fini(ppm_dev_t *ppmd)
16487c478bd9Sstevel@tonic-gate {
16497c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ppmd->domp->lock));
16507c478bd9Sstevel@tonic-gate 	if (ppmd->domp == &xcppm_fet) {
16517c478bd9Sstevel@tonic-gate 		if (ppmd->level != ppmd->lowest) {
16527c478bd9Sstevel@tonic-gate 			ppmd->domp->pwr_cnt--;
16537c478bd9Sstevel@tonic-gate 			DPRINTF(D_FET, ("xcppm_dev_fini: DN cnt = %d\n",
16547c478bd9Sstevel@tonic-gate 			    ppmd->domp->pwr_cnt));
16557c478bd9Sstevel@tonic-gate 		};
16567c478bd9Sstevel@tonic-gate 	}
16577c478bd9Sstevel@tonic-gate }
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate /*
16617c478bd9Sstevel@tonic-gate  * see ppmf and ppm_ioctl, PPMIOCSET
16627c478bd9Sstevel@tonic-gate  */
16637c478bd9Sstevel@tonic-gate static void
xcppm_iocset(uint8_t value)16647c478bd9Sstevel@tonic-gate xcppm_iocset(uint8_t value)
16657c478bd9Sstevel@tonic-gate {
16667c478bd9Sstevel@tonic-gate 	int action;
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	if (value == PPM_IDEV_POWER_ON)
16697c478bd9Sstevel@tonic-gate 		action = XCPPM_SETBIT;
16707c478bd9Sstevel@tonic-gate 	else if (value == PPM_IDEV_POWER_OFF)
16717c478bd9Sstevel@tonic-gate 		action = XCPPM_CLRBIT;
16727c478bd9Sstevel@tonic-gate 	(void) xcppm_gpio_port2(action, DRVON);
16737c478bd9Sstevel@tonic-gate }
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate /*
16777c478bd9Sstevel@tonic-gate  * see ppmf and ppm_ioctl, PPMIOCGET
16787c478bd9Sstevel@tonic-gate  */
16797c478bd9Sstevel@tonic-gate static uint8_t
xcppm_iocget(void)16807c478bd9Sstevel@tonic-gate xcppm_iocget(void)
16817c478bd9Sstevel@tonic-gate {
16827c478bd9Sstevel@tonic-gate 	uint8_t bit;
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	bit = xcppm_gpio_port2(XCPPM_GETBIT, DRVON);
16857c478bd9Sstevel@tonic-gate 	return ((bit == DRVON) ? PPM_IDEV_POWER_ON : PPM_IDEV_POWER_OFF);
16867c478bd9Sstevel@tonic-gate }
1687