xref: /onnv-gate/usr/src/uts/sun4u/io/pmc.c (revision 533:3327cb03e22c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*533Sjan  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*533Sjan  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Driver for the Power Management Controller (logical unit 8) of the
310Sstevel@tonic-gate  * PC87317 SuperI/O chip. The PMC contains the hardware watchdog timer.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/time.h>
360Sstevel@tonic-gate #include <sys/cmn_err.h>
370Sstevel@tonic-gate #include <sys/param.h>
380Sstevel@tonic-gate #include <sys/modctl.h>
390Sstevel@tonic-gate #include <sys/conf.h>
400Sstevel@tonic-gate #include <sys/stat.h>
410Sstevel@tonic-gate #include <sys/clock.h>
420Sstevel@tonic-gate #include <sys/reboot.h>
430Sstevel@tonic-gate #include <sys/ddi.h>
440Sstevel@tonic-gate #include <sys/sunddi.h>
450Sstevel@tonic-gate #include <sys/file.h>
460Sstevel@tonic-gate #include <sys/note.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #ifdef	DEBUG
490Sstevel@tonic-gate int pmc_debug_flag = 0;
500Sstevel@tonic-gate #define	DPRINTF(ARGLIST) if (pmc_debug_flag) printf ARGLIST;
510Sstevel@tonic-gate #else
520Sstevel@tonic-gate #define	DPRINTF(ARGLIST)
530Sstevel@tonic-gate #endif /* DEBUG */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /* Driver soft state structure */
560Sstevel@tonic-gate typedef struct pmc {
570Sstevel@tonic-gate 	dev_info_t		*dip;
580Sstevel@tonic-gate 	ddi_acc_handle_t	pmc_handle;
590Sstevel@tonic-gate } pmc_t;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static void *pmc_soft_state;
620Sstevel@tonic-gate static int instance = -1;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /* dev_ops and cb_ops entry point function declarations */
650Sstevel@tonic-gate static int pmc_attach(dev_info_t *, ddi_attach_cmd_t);
660Sstevel@tonic-gate static int pmc_detach(dev_info_t *, ddi_detach_cmd_t);
670Sstevel@tonic-gate static int pmc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /* hardware watchdog parameters */
700Sstevel@tonic-gate static uint_t pmc_set_watchdog_timer(uint_t);
710Sstevel@tonic-gate static uint_t pmc_clear_watchdog_timer(void);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate extern volatile uint8_t	*v_pmc_addr_reg;
740Sstevel@tonic-gate extern volatile uint8_t	*v_pmc_data_reg;
750Sstevel@tonic-gate extern int		watchdog_enable;
760Sstevel@tonic-gate extern int		watchdog_available;
770Sstevel@tonic-gate extern int		watchdog_activated;
780Sstevel@tonic-gate extern int		boothowto;
790Sstevel@tonic-gate extern uint_t		watchdog_timeout_seconds;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * Power Management Registers and values
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate #define	PMC_WDTO	0x05	/* Watchdog Time Out */
850Sstevel@tonic-gate #define	PMC_CLEAR_WDTO	0x00
860Sstevel@tonic-gate 
870Sstevel@tonic-gate struct cb_ops pmc_cb_ops = {
880Sstevel@tonic-gate 	nodev,
890Sstevel@tonic-gate 	nodev,
900Sstevel@tonic-gate 	nodev,
910Sstevel@tonic-gate 	nodev,
920Sstevel@tonic-gate 	nodev,			/* dump */
930Sstevel@tonic-gate 	nodev,
940Sstevel@tonic-gate 	nodev,
950Sstevel@tonic-gate 	nodev,
960Sstevel@tonic-gate 	nodev,			/* devmap */
970Sstevel@tonic-gate 	nodev,
980Sstevel@tonic-gate 	nodev,
990Sstevel@tonic-gate 	nochpoll,
1000Sstevel@tonic-gate 	ddi_prop_op,
1010Sstevel@tonic-gate 	NULL,			/* for STREAMS drivers */
1020Sstevel@tonic-gate 	D_NEW | D_MP,		/* driver compatibility flag */
1030Sstevel@tonic-gate 	CB_REV,
1040Sstevel@tonic-gate 	nodev,
1050Sstevel@tonic-gate 	nodev
1060Sstevel@tonic-gate };
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static struct dev_ops pmc_dev_ops = {
1090Sstevel@tonic-gate 	DEVO_REV,			/* driver build version */
1100Sstevel@tonic-gate 	0,				/* device reference count */
1110Sstevel@tonic-gate 	pmc_getinfo,
1120Sstevel@tonic-gate 	nulldev,
1130Sstevel@tonic-gate 	nulldev,			/* probe */
1140Sstevel@tonic-gate 	pmc_attach,
1150Sstevel@tonic-gate 	pmc_detach,
1160Sstevel@tonic-gate 	nulldev,			/* reset */
1170Sstevel@tonic-gate 	&pmc_cb_ops,
1180Sstevel@tonic-gate 	(struct bus_ops *)NULL,
1190Sstevel@tonic-gate 	nulldev				/* power */
1200Sstevel@tonic-gate };
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /* module configuration stuff */
1230Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1240Sstevel@tonic-gate static struct modldrv modldrv = {
1250Sstevel@tonic-gate 	&mod_driverops,
1260Sstevel@tonic-gate 	"pmc driver %I%",
1270Sstevel@tonic-gate 	&pmc_dev_ops
1280Sstevel@tonic-gate };
1290Sstevel@tonic-gate static struct modlinkage modlinkage = {
1300Sstevel@tonic-gate 	MODREV_1,
1310Sstevel@tonic-gate 	&modldrv,
1320Sstevel@tonic-gate 	0
1330Sstevel@tonic-gate };
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate int
1370Sstevel@tonic-gate _init(void)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	int e;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	e = ddi_soft_state_init(&pmc_soft_state, sizeof (pmc_t), 1);
1420Sstevel@tonic-gate 	if (e != 0) {
1430Sstevel@tonic-gate 		DPRINTF(("_init: ddi_soft_state_init failed\n"));
1440Sstevel@tonic-gate 		return (e);
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	e = mod_install(&modlinkage);
1480Sstevel@tonic-gate 	if (e != 0) {
1490Sstevel@tonic-gate 		DPRINTF(("_init: mod_install failed\n"));
1500Sstevel@tonic-gate 		ddi_soft_state_fini(&pmc_soft_state);
1510Sstevel@tonic-gate 		return (e);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	if (v_pmc_addr_reg != NULL) {
1550Sstevel@tonic-gate 		tod_ops.tod_set_watchdog_timer = pmc_set_watchdog_timer;
1560Sstevel@tonic-gate 		tod_ops.tod_clear_watchdog_timer = pmc_clear_watchdog_timer;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		/*
1590Sstevel@tonic-gate 		 * See if the user has enabled the watchdog timer, and if
1600Sstevel@tonic-gate 		 * it's available.
1610Sstevel@tonic-gate 		 */
1620Sstevel@tonic-gate 		if (watchdog_enable) {
1630Sstevel@tonic-gate 			if (!watchdog_available) {
1640Sstevel@tonic-gate 				cmn_err(CE_WARN, "pmc: Hardware watchdog "
1650Sstevel@tonic-gate 					"unavailable");
1660Sstevel@tonic-gate 			} else if (boothowto & RB_DEBUG) {
1670Sstevel@tonic-gate 				watchdog_available = 0;
1680Sstevel@tonic-gate 				cmn_err(CE_WARN, "pmc: kernel debugger "
1690Sstevel@tonic-gate 					"detected: hardware watchdog disabled");
1700Sstevel@tonic-gate 			}
1710Sstevel@tonic-gate 		}
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 	return (e);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate int
1770Sstevel@tonic-gate _fini(void)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	int e;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (v_pmc_addr_reg != NULL)
1820Sstevel@tonic-gate 		return (DDI_FAILURE);
1830Sstevel@tonic-gate 	else {
1840Sstevel@tonic-gate 		e = mod_remove(&modlinkage);
1850Sstevel@tonic-gate 		if (e != 0)
1860Sstevel@tonic-gate 			return (e);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 		ddi_soft_state_fini(&pmc_soft_state);
1890Sstevel@tonic-gate 		return (DDI_SUCCESS);
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate int
1950Sstevel@tonic-gate _info(struct modinfo *modinfop)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate static int
2010Sstevel@tonic-gate pmc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	pmc_t	*pmcp;
2060Sstevel@tonic-gate 	int	instance;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	switch (cmd) {
2090Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2100Sstevel@tonic-gate 		instance = getminor((dev_t)arg);
2110Sstevel@tonic-gate 		pmcp = (pmc_t *)ddi_get_soft_state(pmc_soft_state, instance);
2120Sstevel@tonic-gate 		if (pmcp == NULL) {
2130Sstevel@tonic-gate 			*result = (void *)NULL;
2140Sstevel@tonic-gate 			return (DDI_FAILURE);
2150Sstevel@tonic-gate 		}
2160Sstevel@tonic-gate 		*result = (void *)pmcp->dip;
2170Sstevel@tonic-gate 		return (DDI_SUCCESS);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
220*533Sjan 		*result = (void *)(uintptr_t)getminor((dev_t)arg);
2210Sstevel@tonic-gate 		return (DDI_SUCCESS);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	default:
2240Sstevel@tonic-gate 		return (DDI_FAILURE);
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate static int
2300Sstevel@tonic-gate pmc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	pmc_t	*pmcp;
2330Sstevel@tonic-gate 	uint_t	wd_timout;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	switch (cmd) {
2360Sstevel@tonic-gate 	case DDI_ATTACH:
2370Sstevel@tonic-gate 		break;
2380Sstevel@tonic-gate 	case DDI_RESUME:
2390Sstevel@tonic-gate 		if (v_pmc_addr_reg != NULL && watchdog_enable) {
2400Sstevel@tonic-gate 			int ret = 0;
2410Sstevel@tonic-gate 			wd_timout = watchdog_timeout_seconds;
2420Sstevel@tonic-gate 			mutex_enter(&tod_lock);
2430Sstevel@tonic-gate 			ret = tod_ops.tod_set_watchdog_timer(wd_timout);
2440Sstevel@tonic-gate 			mutex_exit(&tod_lock);
2450Sstevel@tonic-gate 			if (ret == 0)
2460Sstevel@tonic-gate 				return (DDI_FAILURE);
2470Sstevel@tonic-gate 		}
2480Sstevel@tonic-gate 		return (DDI_SUCCESS);
2490Sstevel@tonic-gate 	default:
2500Sstevel@tonic-gate 		return (DDI_FAILURE);
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if (instance != -1) {
2540Sstevel@tonic-gate 		DPRINTF(("pmc_attach: Another instance is already attached."));
2550Sstevel@tonic-gate 		return (DDI_FAILURE);
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pmc_soft_state, instance) != DDI_SUCCESS) {
2610Sstevel@tonic-gate 		DPRINTF(("pmc_attach: Failed to allocate soft state."));
2620Sstevel@tonic-gate 		return (DDI_FAILURE);
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	pmcp = (pmc_t *)ddi_get_soft_state(pmc_soft_state, instance);
2660Sstevel@tonic-gate 	pmcp->dip = dip;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	return (DDI_SUCCESS);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate static int
2720Sstevel@tonic-gate pmc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	pmc_t	*pmcp;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	switch (cmd) {
2790Sstevel@tonic-gate 	case DDI_DETACH:
2800Sstevel@tonic-gate 		/* allow detach if no hardware watchdog */
2810Sstevel@tonic-gate 		if (v_pmc_addr_reg == NULL || !watchdog_activated) {
2820Sstevel@tonic-gate 			pmcp = (pmc_t *)ddi_get_soft_state(pmc_soft_state,
2830Sstevel@tonic-gate 				instance);
2840Sstevel@tonic-gate 			if (pmcp == NULL)
2850Sstevel@tonic-gate 				return (ENXIO);
2860Sstevel@tonic-gate 			ddi_soft_state_free(pmc_soft_state, instance);
2870Sstevel@tonic-gate 			return (DDI_SUCCESS);
2880Sstevel@tonic-gate 		} else
2890Sstevel@tonic-gate 			return (DDI_FAILURE);
2900Sstevel@tonic-gate 	case DDI_SUSPEND:
2910Sstevel@tonic-gate 		if (v_pmc_addr_reg != NULL && watchdog_activated) {
2920Sstevel@tonic-gate 			mutex_enter(&tod_lock);
2930Sstevel@tonic-gate 			(void) tod_ops.tod_clear_watchdog_timer();
2940Sstevel@tonic-gate 			mutex_exit(&tod_lock);
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate 		return (DDI_SUCCESS);
2970Sstevel@tonic-gate 	default:
2980Sstevel@tonic-gate 		return (DDI_FAILURE);
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * Set the hardware watchdog timer; returning what we set it to.
3050Sstevel@tonic-gate  */
3060Sstevel@tonic-gate static uint_t
3070Sstevel@tonic-gate pmc_set_watchdog_timer(uint_t timeoutval)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	uint_t timeoutval_minutes;
3100Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	/* sanity checks */
3130Sstevel@tonic-gate 	if (watchdog_enable == 0 || watchdog_available == 0 ||
3140Sstevel@tonic-gate 	    timeoutval == 0)
3150Sstevel@tonic-gate 		return (0);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	/*
3180Sstevel@tonic-gate 	 * Historically the timer has been counted out in seconds.
3190Sstevel@tonic-gate 	 * The PC87317 counts the timeout in minutes. The default
3200Sstevel@tonic-gate 	 * timeout is 10 seconds; the least we can do is one minute.
3210Sstevel@tonic-gate 	 */
3220Sstevel@tonic-gate 	timeoutval_minutes = (timeoutval + 59) / 60;
3230Sstevel@tonic-gate 	if (timeoutval_minutes > UINT8_MAX)
3240Sstevel@tonic-gate 		return (0);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	*v_pmc_addr_reg = (uint8_t)PMC_WDTO;
3270Sstevel@tonic-gate 	*v_pmc_data_reg = (uint8_t)timeoutval_minutes;
3280Sstevel@tonic-gate 	watchdog_activated = 1;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/* we'll still return seconds */
3310Sstevel@tonic-gate 	return (timeoutval_minutes * 60);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate /*
3350Sstevel@tonic-gate  * Clear the hardware watchdog timer; returning what it was set to.
3360Sstevel@tonic-gate  */
3370Sstevel@tonic-gate static uint_t
3380Sstevel@tonic-gate pmc_clear_watchdog_timer(void)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	uint_t	wd_timeout;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
3430Sstevel@tonic-gate 	if (watchdog_activated == 0)
3440Sstevel@tonic-gate 		return (0);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	*v_pmc_addr_reg = (uint8_t)PMC_WDTO;
3470Sstevel@tonic-gate 	wd_timeout = (uint_t)*v_pmc_data_reg;
3480Sstevel@tonic-gate 	*v_pmc_data_reg = (uint8_t)PMC_CLEAR_WDTO;
3490Sstevel@tonic-gate 	watchdog_activated = 0;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	/* return seconds */
3520Sstevel@tonic-gate 	return (wd_timeout * 60);
3530Sstevel@tonic-gate }
354