#include <sys/types.h>
#include <sys/conf.h>
#include <sys/open.h>
#include <sys/modctl.h>
#include <sys/promif.h>
#include <sys/stat.h>
#include <sys/ddi_impldefs.h>
#include <sys/jbusppm.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
static int jbppm_attach(dev_info_t *, ddi_attach_cmd_t);
static int jbppm_detach(dev_info_t *, ddi_detach_cmd_t);
static int jbppm_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int jbppm_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
static int jbppm_close(dev_t dev, int flag, int otyp, cred_t *cred_p);
static int jbppm_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
static struct cb_ops jbppm_cbops = {
jbppm_open,
jbppm_close,
nodev,
nodev,
nodev,
nodev,
nodev,
jbppm_ioctl,
nodev,
nodev,
nodev,
nochpoll,
ddi_prop_op,
NULL,
D_MP | D_NEW,
CB_REV,
nodev,
nodev,
};
static struct dev_ops jbppm_ops = {
DEVO_REV,
0,
jbppm_getinfo,
nulldev,
nulldev,
jbppm_attach,
jbppm_detach,
nodev,
&jbppm_cbops,
NULL,
NULL,
ddi_quiesce_not_supported,
};
extern struct mod_ops mod_driverops;
static struct modldrv modldrv = {
&mod_driverops,
"JBus ppm driver",
&jbppm_ops,
};
static struct modlinkage modlinkage = {
MODREV_1,
&modldrv,
NULL
};
static void jbppm_next_speed(dev_info_t *, uint_t);
static int jbppm_start_next(dev_info_t *, int);
static void *jbppm_statep;
static kmutex_t jbppm_lock;
static const uint64_t jbus_clock_masks[] = {
JBUS_ESTAR_CNTL_32,
JBUS_ESTAR_CNTL_2,
JBUS_ESTAR_CNTL_1
};
int
_init(void)
{
int error;
if ((error = ddi_soft_state_init(&jbppm_statep,
sizeof (jbppm_unit), 0)) != DDI_SUCCESS) {
return (error);
}
mutex_init(&jbppm_lock, NULL, MUTEX_DRIVER, NULL);
if ((error = mod_install(&modlinkage)) != DDI_SUCCESS) {
mutex_destroy(&jbppm_lock);
ddi_soft_state_fini(&jbppm_statep);
return (error);
}
return (error);
}
int
_fini(void)
{
int error;
if ((error = mod_remove(&modlinkage)) == DDI_SUCCESS) {
mutex_destroy(&jbppm_lock);
ddi_soft_state_fini(&jbppm_statep);
}
return (error);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
static int
jbppm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
char *str = "jbppm_attach";
int instance;
jbppm_unit *unitp;
uint64_t data64;
ddi_device_acc_attr_t attr;
int rv = DDI_SUCCESS;
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
return (DDI_SUCCESS);
default:
cmn_err(CE_WARN, "%s: cmd %d unsupported.\n", str, cmd);
return (DDI_FAILURE);
}
instance = ddi_get_instance(dip);
rv = ddi_soft_state_zalloc(jbppm_statep, instance);
if (rv != DDI_SUCCESS) {
cmn_err(CE_WARN, "%s: failed alloc for dev(%s@%s)",
str, ddi_binding_name(dip),
ddi_get_name_addr(dip) ? ddi_get_name_addr(dip) : " ");
return (rv);
}
if ((unitp = ddi_get_soft_state(jbppm_statep, instance)) == NULL) {
rv = DDI_FAILURE;
goto doerrs;
}
rv = ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
DDI_KERNEL_IOCTL, NULL, 0);
if (rv != DDI_PROP_SUCCESS)
goto doerrs;
ddi_report_dev(dip);
unitp->dip = dip;
attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
rv = ddi_regs_map_setup(dip, 0, (caddr_t *)&unitp->devid_csr, 0, 8,
&attr, &unitp->devid_hndl);
if (rv != DDI_SUCCESS)
goto doerrs;
rv = ddi_regs_map_setup(dip, 1, (caddr_t *)&unitp->estar_csr, 0, 16,
&attr, &unitp->estar_hndl);
if (rv != DDI_SUCCESS)
goto doerrs;
unitp->j_chng_csr = (uint64_t *)((caddr_t)unitp->estar_csr +
J_CHNG_INITIATION_OFFSET);
data64 = ddi_get64(unitp->devid_hndl, (uint64_t *)unitp->devid_csr);
unitp->is_master = (data64 & MASTER_IOBRIDGE_BIT) ? 1 : 0;
unitp->lyropen = 0;
rv = ddi_create_minor_node(dip, "jbus-ppm", S_IFCHR, instance, 0, 0);
if (rv != DDI_SUCCESS)
goto doerrs;
return (rv);
doerrs:
if (unitp->devid_hndl != NULL)
ddi_regs_map_free(&unitp->devid_hndl);
if (unitp->estar_csr != NULL)
ddi_regs_map_free(&unitp->estar_hndl);
if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS |
DDI_PROP_NOTPROM, DDI_KERNEL_IOCTL))
ddi_prop_remove_all(dip);
ddi_soft_state_free(jbppm_statep, instance);
return (rv);
}
static int
jbppm_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
{
jbppm_unit *unitp;
int instance;
switch (cmd) {
case DDI_INFO_DEVT2DEVINFO:
instance = getminor((dev_t)arg);
unitp = ddi_get_soft_state(jbppm_statep, instance);
if (unitp == NULL) {
return (DDI_FAILURE);
}
*result = (void *) unitp->dip;
return (DDI_SUCCESS);
case DDI_INFO_DEVT2INSTANCE:
instance = getminor((dev_t)arg);
*result = (void *)(uintptr_t)instance;
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
static int
jbppm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
char *str = "jbppm_detach";
switch (cmd) {
case DDI_DETACH:
return (DDI_FAILURE);
case DDI_SUSPEND:
return (DDI_SUCCESS);
default:
cmn_err(CE_WARN, "%s: cmd %d unsupported", str, cmd);
return (DDI_FAILURE);
}
}
static int
jbppm_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
{
jbppm_unit *unitp;
if (drv_priv(cred_p) != DDI_SUCCESS)
return (EPERM);
if ((unitp = ddi_get_soft_state(
jbppm_statep, getminor(*dev_p))) == NULL) {
cmn_err(CE_WARN, "jbppm_open: failed to get soft state!");
return (DDI_FAILURE);
}
mutex_enter(&jbppm_lock);
if (unitp->lyropen != 0) {
mutex_exit(&jbppm_lock);
return (EBUSY);
}
unitp->lyropen++;
mutex_exit(&jbppm_lock);
return (DDI_SUCCESS);
}
static int
jbppm_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
{
jbppm_unit *unitp;
if ((unitp =
ddi_get_soft_state(jbppm_statep, getminor(dev))) == NULL)
return (DDI_FAILURE);
mutex_enter(&jbppm_lock);
unitp->lyropen = 0;
mutex_exit(&jbppm_lock);
return (DDI_SUCCESS);
}
#define JBPPMIOC ('j' << 8)
#define JBPPMIOC_ISMASTER (JBPPMIOC | 1)
#define JBPPMIOC_NEXT (JBPPMIOC | 2)
#define JBPPMIOC_GO (JBPPMIOC | 3)
static int
jbppm_ioctl(dev_t dev, int cmd, intptr_t arg, int flag,
cred_t *cred_p, int *rval_p)
{
jbppm_unit *unitp;
if (drv_priv(cred_p) != 0)
return (EPERM);
if ((unitp =
ddi_get_soft_state(jbppm_statep, getminor(dev))) == NULL)
return (EIO);
switch (cmd) {
case JBPPMIOC_ISMASTER:
if (unitp->is_master)
return (0);
else
return (-1);
case JBPPMIOC_NEXT:
jbppm_next_speed(unitp->dip, (uint_t)arg);
return (0);
case JBPPMIOC_GO:
if (!unitp->is_master)
return (EINVAL);
return (jbppm_start_next(unitp->dip, (int)arg));
default:
return (ENOTTY);
}
}
static void
jbppm_next_speed(dev_info_t *dip, uint_t lvl_index)
{
volatile uint64_t data64;
static jbppm_unit *unitp;
unitp = ddi_get_soft_state(jbppm_statep, ddi_get_instance(dip));
ASSERT(unitp);
mutex_enter(&jbppm_lock);
data64 = ddi_get64(unitp->estar_hndl, unitp->estar_csr);
data64 &= ~JBUS_ESTAR_CNTL_MASK;
data64 |= jbus_clock_masks[lvl_index];
ddi_put64(unitp->estar_hndl, (uint64_t *)unitp->estar_csr, data64);
data64 = ddi_get64(unitp->estar_hndl, unitp->estar_csr);
mutex_exit(&jbppm_lock);
}
static int
jbppm_start_next(dev_info_t *dip, int chng_delay)
{
volatile uint64_t data64;
static jbppm_unit *unitp;
unitp = ddi_get_soft_state(jbppm_statep, ddi_get_instance(dip));
ASSERT(unitp && unitp->is_master);
mutex_enter(&jbppm_lock);
do {
data64 = ddi_get64(unitp->estar_hndl, unitp->j_chng_csr);
} while ((J_CHNG_INITIATION_MASK & data64) == J_CHNG_START);
data64 &= ~J_CHNG_INITIATION_MASK;
ddi_put64(unitp->estar_hndl, (uint64_t *)unitp->j_chng_csr, data64);
data64 &= ~J_CHNG_DELAY_MASK;
data64 |= (J_CHNG_START | chng_delay);
ddi_put64(unitp->estar_hndl, (uint64_t *)unitp->j_chng_csr, data64);
do {
data64 = ddi_get64(unitp->estar_hndl, unitp->j_chng_csr);
} while ((J_CHNG_INITIATION_MASK & data64) == J_CHNG_START);
data64 &= ~J_CHNG_INITIATION_MASK;
ddi_put64(unitp->estar_hndl, (uint64_t *)unitp->j_chng_csr, data64);
(void) ddi_get64(unitp->estar_hndl, unitp->j_chng_csr);
mutex_exit(&jbppm_lock);
return (DDI_SUCCESS);
}