1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/conf.h> 31*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 33*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/devops.h> 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate /* 39*7c478bd9Sstevel@tonic-gate * Time periods, in nanoseconds 40*7c478bd9Sstevel@tonic-gate */ 41*7c478bd9Sstevel@tonic-gate #define PMUGPIO_TWO_SEC 2000000000LL 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate static dev_info_t *pmugpio_dip; 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate typedef struct pmugpio_state { 46*7c478bd9Sstevel@tonic-gate uint8_t *pmugpio_reset_reg; 47*7c478bd9Sstevel@tonic-gate ddi_acc_handle_t pmugpio_reset_reg_handle; 48*7c478bd9Sstevel@tonic-gate uint8_t *pmugpio_watchdog_reg; 49*7c478bd9Sstevel@tonic-gate ddi_acc_handle_t pmugpio_watchdog_reg_handle; 50*7c478bd9Sstevel@tonic-gate hrtime_t hw_last_pat; 51*7c478bd9Sstevel@tonic-gate } pmugpio_state_t; 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate static void *pmugpio_statep; 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate static int pmugpio_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 56*7c478bd9Sstevel@tonic-gate static int pmugpio_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 57*7c478bd9Sstevel@tonic-gate static int pmugpio_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 58*7c478bd9Sstevel@tonic-gate void **result); 59*7c478bd9Sstevel@tonic-gate static int pmugpio_map_regs(dev_info_t *, pmugpio_state_t *); 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate struct cb_ops pmugpio_cb_ops = { 62*7c478bd9Sstevel@tonic-gate nulldev, /* open */ 63*7c478bd9Sstevel@tonic-gate nulldev, /* close */ 64*7c478bd9Sstevel@tonic-gate nulldev, /* strategy */ 65*7c478bd9Sstevel@tonic-gate nulldev, /* print */ 66*7c478bd9Sstevel@tonic-gate nulldev, /* dump */ 67*7c478bd9Sstevel@tonic-gate nulldev, /* read */ 68*7c478bd9Sstevel@tonic-gate nulldev, /* write */ 69*7c478bd9Sstevel@tonic-gate nulldev, /* ioctl */ 70*7c478bd9Sstevel@tonic-gate nulldev, /* devmap */ 71*7c478bd9Sstevel@tonic-gate nulldev, /* mmap */ 72*7c478bd9Sstevel@tonic-gate nulldev, /* segmap */ 73*7c478bd9Sstevel@tonic-gate nochpoll, /* poll */ 74*7c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 75*7c478bd9Sstevel@tonic-gate NULL, /* streamtab */ 76*7c478bd9Sstevel@tonic-gate D_MP | D_NEW 77*7c478bd9Sstevel@tonic-gate }; 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate static struct dev_ops pmugpio_ops = { 80*7c478bd9Sstevel@tonic-gate DEVO_REV, /* Devo_rev */ 81*7c478bd9Sstevel@tonic-gate 0, /* Refcnt */ 82*7c478bd9Sstevel@tonic-gate pmugpio_info, /* Info */ 83*7c478bd9Sstevel@tonic-gate nulldev, /* Identify */ 84*7c478bd9Sstevel@tonic-gate nulldev, /* Probe */ 85*7c478bd9Sstevel@tonic-gate pmugpio_attach, /* Attach */ 86*7c478bd9Sstevel@tonic-gate pmugpio_detach, /* Detach */ 87*7c478bd9Sstevel@tonic-gate nodev, /* Reset */ 88*7c478bd9Sstevel@tonic-gate &pmugpio_cb_ops, /* Driver operations */ 89*7c478bd9Sstevel@tonic-gate 0, /* Bus operations */ 90*7c478bd9Sstevel@tonic-gate NULL /* Power */ 91*7c478bd9Sstevel@tonic-gate }; 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 94*7c478bd9Sstevel@tonic-gate &mod_driverops, /* This one is a driver */ 95*7c478bd9Sstevel@tonic-gate "Pmugpio Driver %I%", /* Name of the module. */ 96*7c478bd9Sstevel@tonic-gate &pmugpio_ops, /* Driver ops */ 97*7c478bd9Sstevel@tonic-gate }; 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 100*7c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modldrv, NULL 101*7c478bd9Sstevel@tonic-gate }; 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate int 104*7c478bd9Sstevel@tonic-gate _init(void) 105*7c478bd9Sstevel@tonic-gate { 106*7c478bd9Sstevel@tonic-gate int error; 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate /* Initialize the soft state structures */ 109*7c478bd9Sstevel@tonic-gate if ((error = ddi_soft_state_init(&pmugpio_statep, 110*7c478bd9Sstevel@tonic-gate sizeof (pmugpio_state_t), 1)) != 0) { 111*7c478bd9Sstevel@tonic-gate return (error); 112*7c478bd9Sstevel@tonic-gate } 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate /* Install the loadable module */ 115*7c478bd9Sstevel@tonic-gate if ((error = mod_install(&modlinkage)) != 0) { 116*7c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&pmugpio_statep); 117*7c478bd9Sstevel@tonic-gate } 118*7c478bd9Sstevel@tonic-gate return (error); 119*7c478bd9Sstevel@tonic-gate } 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate int 122*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 123*7c478bd9Sstevel@tonic-gate { 124*7c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate int 128*7c478bd9Sstevel@tonic-gate _fini(void) 129*7c478bd9Sstevel@tonic-gate { 130*7c478bd9Sstevel@tonic-gate int error; 131*7c478bd9Sstevel@tonic-gate 132*7c478bd9Sstevel@tonic-gate error = mod_remove(&modlinkage); 133*7c478bd9Sstevel@tonic-gate if (error == 0) { 134*7c478bd9Sstevel@tonic-gate /* Release per module resources */ 135*7c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&pmugpio_statep); 136*7c478bd9Sstevel@tonic-gate } 137*7c478bd9Sstevel@tonic-gate return (error); 138*7c478bd9Sstevel@tonic-gate } 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate static int 141*7c478bd9Sstevel@tonic-gate pmugpio_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 142*7c478bd9Sstevel@tonic-gate { 143*7c478bd9Sstevel@tonic-gate int instance; 144*7c478bd9Sstevel@tonic-gate pmugpio_state_t *pmugpio_ptr = NULL; 145*7c478bd9Sstevel@tonic-gate 146*7c478bd9Sstevel@tonic-gate switch (cmd) { 147*7c478bd9Sstevel@tonic-gate case DDI_ATTACH: 148*7c478bd9Sstevel@tonic-gate break; 149*7c478bd9Sstevel@tonic-gate case DDI_RESUME: 150*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 151*7c478bd9Sstevel@tonic-gate default: 152*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 153*7c478bd9Sstevel@tonic-gate } 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate /* Get the instance and create soft state */ 156*7c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 157*7c478bd9Sstevel@tonic-gate if (ddi_soft_state_zalloc(pmugpio_statep, instance) != 0) { 158*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 159*7c478bd9Sstevel@tonic-gate } 160*7c478bd9Sstevel@tonic-gate pmugpio_ptr = ddi_get_soft_state(pmugpio_statep, instance); 161*7c478bd9Sstevel@tonic-gate if (pmugpio_ptr == NULL) { 162*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 163*7c478bd9Sstevel@tonic-gate } 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate if (pmugpio_map_regs(dip, pmugpio_ptr) != DDI_SUCCESS) { 166*7c478bd9Sstevel@tonic-gate ddi_soft_state_free(pmugpio_statep, instance); 167*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate /* Display information in the banner */ 171*7c478bd9Sstevel@tonic-gate ddi_report_dev(dip); 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate /* Save the dip */ 174*7c478bd9Sstevel@tonic-gate pmugpio_dip = dip; 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 177*7c478bd9Sstevel@tonic-gate } 178*7c478bd9Sstevel@tonic-gate 179*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 180*7c478bd9Sstevel@tonic-gate static int 181*7c478bd9Sstevel@tonic-gate pmugpio_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 182*7c478bd9Sstevel@tonic-gate { 183*7c478bd9Sstevel@tonic-gate /* Pointer to soft state */ 184*7c478bd9Sstevel@tonic-gate switch (cmd) { 185*7c478bd9Sstevel@tonic-gate case DDI_SUSPEND: 186*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 187*7c478bd9Sstevel@tonic-gate default: 188*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 189*7c478bd9Sstevel@tonic-gate } 190*7c478bd9Sstevel@tonic-gate } 191*7c478bd9Sstevel@tonic-gate 192*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 193*7c478bd9Sstevel@tonic-gate static int 194*7c478bd9Sstevel@tonic-gate pmugpio_info(dev_info_t *dip, ddi_info_cmd_t infocmd, 195*7c478bd9Sstevel@tonic-gate void *arg, void **result) 196*7c478bd9Sstevel@tonic-gate { 197*7c478bd9Sstevel@tonic-gate dev_t dev; 198*7c478bd9Sstevel@tonic-gate int instance, error; 199*7c478bd9Sstevel@tonic-gate 200*7c478bd9Sstevel@tonic-gate switch (infocmd) { 201*7c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 202*7c478bd9Sstevel@tonic-gate *result = (void *)pmugpio_dip; 203*7c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 204*7c478bd9Sstevel@tonic-gate break; 205*7c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 206*7c478bd9Sstevel@tonic-gate dev = (dev_t)arg; 207*7c478bd9Sstevel@tonic-gate instance = getminor(dev); 208*7c478bd9Sstevel@tonic-gate *result = (void *)(uintptr_t)instance; 209*7c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 210*7c478bd9Sstevel@tonic-gate break; 211*7c478bd9Sstevel@tonic-gate default: 212*7c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 213*7c478bd9Sstevel@tonic-gate } 214*7c478bd9Sstevel@tonic-gate return (error); 215*7c478bd9Sstevel@tonic-gate } 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate void 218*7c478bd9Sstevel@tonic-gate pmugpio_watchdog_pat(void) 219*7c478bd9Sstevel@tonic-gate { 220*7c478bd9Sstevel@tonic-gate dev_info_t *dip = pmugpio_dip; 221*7c478bd9Sstevel@tonic-gate int instance; 222*7c478bd9Sstevel@tonic-gate pmugpio_state_t *pmugpio_ptr; 223*7c478bd9Sstevel@tonic-gate hrtime_t now; 224*7c478bd9Sstevel@tonic-gate uint8_t value; 225*7c478bd9Sstevel@tonic-gate 226*7c478bd9Sstevel@tonic-gate if (dip == NULL) { 227*7c478bd9Sstevel@tonic-gate return; 228*7c478bd9Sstevel@tonic-gate } 229*7c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 230*7c478bd9Sstevel@tonic-gate pmugpio_ptr = ddi_get_soft_state(pmugpio_statep, instance); 231*7c478bd9Sstevel@tonic-gate if (pmugpio_ptr == NULL) { 232*7c478bd9Sstevel@tonic-gate return; 233*7c478bd9Sstevel@tonic-gate } 234*7c478bd9Sstevel@tonic-gate /* 235*7c478bd9Sstevel@tonic-gate * The RMC can read interrupts either high to low OR low to high. As 236*7c478bd9Sstevel@tonic-gate * a result all that needs to happen is that when we hit the time to 237*7c478bd9Sstevel@tonic-gate * send an signal we simply need to change the state. 238*7c478bd9Sstevel@tonic-gate */ 239*7c478bd9Sstevel@tonic-gate now = gethrtime(); 240*7c478bd9Sstevel@tonic-gate if ((now - pmugpio_ptr->hw_last_pat) >= PMUGPIO_TWO_SEC) { 241*7c478bd9Sstevel@tonic-gate /* 242*7c478bd9Sstevel@tonic-gate * fetch current reg value and invert it 243*7c478bd9Sstevel@tonic-gate */ 244*7c478bd9Sstevel@tonic-gate value = (uint8_t)(0xff ^ 245*7c478bd9Sstevel@tonic-gate ddi_get8(pmugpio_ptr->pmugpio_watchdog_reg_handle, 246*7c478bd9Sstevel@tonic-gate pmugpio_ptr->pmugpio_watchdog_reg)); 247*7c478bd9Sstevel@tonic-gate 248*7c478bd9Sstevel@tonic-gate ddi_put8(pmugpio_ptr->pmugpio_watchdog_reg_handle, 249*7c478bd9Sstevel@tonic-gate pmugpio_ptr->pmugpio_watchdog_reg, value); 250*7c478bd9Sstevel@tonic-gate pmugpio_ptr->hw_last_pat = now; 251*7c478bd9Sstevel@tonic-gate } 252*7c478bd9Sstevel@tonic-gate } 253*7c478bd9Sstevel@tonic-gate 254*7c478bd9Sstevel@tonic-gate void 255*7c478bd9Sstevel@tonic-gate pmugpio_reset(void) 256*7c478bd9Sstevel@tonic-gate { 257*7c478bd9Sstevel@tonic-gate dev_info_t *dip = pmugpio_dip; 258*7c478bd9Sstevel@tonic-gate int instance; 259*7c478bd9Sstevel@tonic-gate pmugpio_state_t *pmugpio_ptr; 260*7c478bd9Sstevel@tonic-gate 261*7c478bd9Sstevel@tonic-gate if (dip == NULL) { 262*7c478bd9Sstevel@tonic-gate return; 263*7c478bd9Sstevel@tonic-gate } 264*7c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 265*7c478bd9Sstevel@tonic-gate pmugpio_ptr = ddi_get_soft_state(pmugpio_statep, instance); 266*7c478bd9Sstevel@tonic-gate if (pmugpio_ptr == NULL) { 267*7c478bd9Sstevel@tonic-gate return; 268*7c478bd9Sstevel@tonic-gate } 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate /* 271*7c478bd9Sstevel@tonic-gate * turn all bits on then off again - pmubus nexus will ensure 272*7c478bd9Sstevel@tonic-gate * that only unmasked bit is affected 273*7c478bd9Sstevel@tonic-gate */ 274*7c478bd9Sstevel@tonic-gate ddi_put8(pmugpio_ptr->pmugpio_reset_reg_handle, 275*7c478bd9Sstevel@tonic-gate pmugpio_ptr->pmugpio_reset_reg, ~0); 276*7c478bd9Sstevel@tonic-gate ddi_put8(pmugpio_ptr->pmugpio_reset_reg_handle, 277*7c478bd9Sstevel@tonic-gate pmugpio_ptr->pmugpio_reset_reg, 0); 278*7c478bd9Sstevel@tonic-gate } 279*7c478bd9Sstevel@tonic-gate 280*7c478bd9Sstevel@tonic-gate static int 281*7c478bd9Sstevel@tonic-gate pmugpio_map_regs(dev_info_t *dip, pmugpio_state_t *pmugpio_ptr) 282*7c478bd9Sstevel@tonic-gate { 283*7c478bd9Sstevel@tonic-gate ddi_device_acc_attr_t attr; 284*7c478bd9Sstevel@tonic-gate 285*7c478bd9Sstevel@tonic-gate /* The host controller will be little endian */ 286*7c478bd9Sstevel@tonic-gate attr.devacc_attr_version = DDI_DEVICE_ATTR_V0; 287*7c478bd9Sstevel@tonic-gate attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC; 288*7c478bd9Sstevel@tonic-gate attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate if (ddi_regs_map_setup(dip, 1, 291*7c478bd9Sstevel@tonic-gate (caddr_t *)&pmugpio_ptr->pmugpio_watchdog_reg, 0, 1, &attr, 292*7c478bd9Sstevel@tonic-gate &pmugpio_ptr->pmugpio_watchdog_reg_handle) != DDI_SUCCESS) { 293*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 294*7c478bd9Sstevel@tonic-gate } 295*7c478bd9Sstevel@tonic-gate if (ddi_regs_map_setup(dip, 0, 296*7c478bd9Sstevel@tonic-gate (caddr_t *)&pmugpio_ptr->pmugpio_reset_reg, 0, 1, &attr, 297*7c478bd9Sstevel@tonic-gate &pmugpio_ptr->pmugpio_reset_reg_handle) != DDI_SUCCESS) { 298*7c478bd9Sstevel@tonic-gate ddi_regs_map_free(&pmugpio_ptr->pmugpio_watchdog_reg_handle); 299*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 300*7c478bd9Sstevel@tonic-gate } 301*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 302*7c478bd9Sstevel@tonic-gate } 303