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