/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/machsystm.h>
#include <sys/sysmacros.h>
/* Added for prom interface */
#include <sys/promimpl.h>
#include <sys/todds1337.h>
/*
* Driver entry routines
*/
/*
* tod_ops entry routines
*/
static timestruc_t todds1337_get(void);
static void todds1337_set(timestruc_t);
static uint_t todds1337_clear_watchdog_timer(void);
static void todds1337_set_power_alarm(timestruc_t);
static void todds1337_clear_power_alarm(void);
static int todds1337_setup_prom();
static void todds1337_rele_prom();
/*
* Local functions
*/
static int todds1337_read_rtc(struct rtc_t *);
static int todds1337_write_rtc(struct rtc_t *);
/* Anchor for soft state structure */
static void *ds1337_statep;
static int todds1337_attach_done = 0;
/* one second time out */
/*
* cp_ops structure
*/
nodev, /* open */
nodev, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
nodev, /* read */
nodev, /* write */
nodev, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
NULL, /* poll */
ddi_prop_op, /* cb_prop_op */
NULL, /* streamtab */
CB_REV, /* rev */
nodev, /* int (*cb_aread)() */
nodev /* int (*cb_awrite)() */
};
/*
* dev_ops structure
*/
DEVO_REV, /* devo_rev */
0, /* refcnt - reference cnt always set to 0 */
todds1337_getinfo, /* getinfo - Maybe requred */
nulldev, /* identify */
nulldev, /* probe */
todds1337_attach, /* attach */
todds1337_detach, /* detach */
nodev, /* reset */
&ds1337_cbops, /* cb_ops - ds1337 does not need this(?) */
NULL, /* bus_ops */
NULL, /* power */
ddi_quiesce_not_needed, /* quiesce */
};
&mod_driverops, /* Type of module. This one is a driver */
"tod driver for DS1337", /* Name of the module. */
&ds1337_ops, /* Pointer to dev_ops */
};
/*
* Module linkage structure
*/
0
};
int
_init(void)
{
int error;
sizeof (ds1337_state_t), 0)) != DDI_SUCCESS) {
return (error);
}
}
(void) todds1337_setup_prom();
/*
* Install the module
*/
}
return (error);
}
return (0);
}
int
_fini(void)
{
int error = 0;
} else {
}
}
return (error);
}
int
{
}
/*
* cyclical call to get tod.
*/
static void
{
}
/*
* register ds1337 client device with i2c services, and
* allocate & initialize soft state structure.
*/
static int
{
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
if (instance != -1) {
return (DDI_FAILURE);
}
/*
* Allocate soft state structure
*/
"state");
instance = -1;
return (DDI_FAILURE);
}
"state");
instance = -1;
return (DDI_FAILURE);
}
"client");
instance = -1;
return (DDI_FAILURE);
}
/* check and initialize the oscillator */
"status register");
instance = -1;
return (DDI_FAILURE);
}
/*
* Check Oscillator and initialize chip if OBP failed to do it
*/
if (tempVal & RTC_CTL_EOSC) {
2, 0, I2C_SLEEP);
!= I2C_SUCCESS) {
i2c_tp);
"DS1337 control register");
instance = -1;
return (DDI_FAILURE);
}
/*
* Now reset the OSF flag in the Status register
*/
2, 0, I2C_SLEEP);
!= I2C_SUCCESS) {
i2c_tp);
"DS1337 status register");
instance = -1;
return (DDI_FAILURE);
}
}
/*
* Create a periodical handler to read TOD.
*/
return (DDI_SUCCESS);
}
/*ARGSUSED*/
static int
{
/*
* Once attached, do not allow detach because the system constantly
* calling todds1337_get() to get the time. If the driver is detached
* and the system try to get the time, the system will have memory
* problem.
*
*/
switch (cmd) {
case DDI_SUSPEND:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/* *********************** tod_ops entry points ******************** */
/*
* Read the current time from the DS1337 chip and convert to UNIX form.
* Should be called with tod_lock held.
*/
static timestruc_t
todds1337_get(void)
{
if (sync_clock_once) {
(void) todds1337_read_rtc(&soft_rtc);
sync_clock_once = 0;
} else {
return (hrestime);
}
/*
* 00 - 68 = 2000 thru 2068
* 69-99 = 1969 thru 1999
*/
/* read was successful so ensure failure flag is clear */
return (ts);
}
/*
* Program DS1337 with the specified time.
* Must be called with tod_lock held. The TOD
* chip supports date from 1969-2068 only. We must
* reject requests to set date below 1969.
*/
static void
{
int year;
/*
* Year is base 1900, valid year range 1969-2068
*/
return;
if (year >= 100)
year -= 100;
(void) todds1337_write_rtc(&rtc);
}
/*
* Program ds1337 registers for alarm to go off at the specified time.
* Alarm #1 is used (Alarm #2 stays idle)
*/
/* ARGSUSED */
static void
{
if (!todds1337_attach_done) {
return;
}
return;
}
/*
* i2c_transfe() may block; to avoid locking clock() which
* is running in interrupt context at PIL10 we temporarely exit
* have non-interlsecting registers, it is safe to use different locks.
*/
/*
* Disable Power Alarm (A1IE)
*/
"DS1337 control register");
return;
}
"DS1337control register");
return;
}
/*
* Write Alarm #1 registers
*/
"DS1337 alarm registers");
return;
}
/*
* Enable Power Alarm
*/
"DS1337 alarm");
return;
}
}
/* ARGSUSED */
static void
{
if (!todds1337_attach_done) {
return;
}
return;
}
/*
* i2c_transfe() may block; to avoid locking clock() which
* is running in interrupt context at PIL10 we temporarely exit
* have non-interlsecting registers, it is safe to use different locks.
*/
/*
* Disable Alarm #1 Interrupt
*/
"DS1337 control register");
return;
}
"DS1337 control register");
return;
}
/*
* Reset Alarm #1 Flag
*/
"DS1337 status register");
return;
}
"DS1337 status register");
return;
}
}
/* ARGSUSED */
static uint_t
{
return (0);
}
/* ARGSUSED */
static uint_t
{
return (0);
}
/* ********************** Local functions ***************************** */
static int
{
if (!todds1337_attach_done) {
return (todds1337_prom_getdate(rtc));
}
return (DDI_FAILURE);
}
/*
* Allocate 1 byte for write buffer and 7 bytes for read buffer to
* to accomodate sec, min, hrs, dayOfWeek, dayOfMonth, year
*/
return (DDI_FAILURE);
}
do {
i2c_tp)) != I2C_SUCCESS) {
goto done;
}
/* for first read, need to get valid data */
/* move data to static buffer */
/* now read again */
/* Start reading reg from 0x00 */
i2c_tp)) != I2C_SUCCESS) {
goto done;
}
/* if they are not the same, then read again */
tod_read[0] = -1;
counter--;
}
}
/* if seconds register is 0x59 (BCD), add data should match */
counter-- > 0);
if (counter < 0)
/* move data to static buffer */
done:
return (i2c_cmd_status);
}
static int
{
if (!todds1337_attach_done) {
return (todds1337_prom_setdate(rtc));
}
return (DDI_FAILURE);
}
return (i2c_cmd_status);
}
i2c_tp)) != I2C_SUCCESS) {
return (i2c_cmd_status);
}
return (i2c_cmd_status);
}
/*ARGSUSED*/
static int
void **result)
{
if (instance == -1) {
return (DDI_FAILURE);
}
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
NULL)
return (DDI_FAILURE);
return (DDI_SUCCESS);
case DDI_INFO_DEVT2INSTANCE:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/*
* Finds the device node with device_type "rtc" and opens it to
* execute the get-time method
*/
static int
{
DS1337_DEVICE_TYPE)) == OBP_NONODE)
return (DDI_FAILURE);
/*
* We now have the phandle of the rtc node, we need to open the
* node and get the ihandle
*/
sizeof (tod1337_devpath)) < 0) {
return (DDI_FAILURE);
}
/*
* Now open the node and store it's ihandle
*/
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
/*
* Closes the prom interface
*/
static void
{
(void) prom_close(todds1337_ihandle);
}
/*
* Read the date using "get-time" method in rtc node
* PROM returns 1969-1999 when reading 69-99 and
* 2000-2068 when reading 00-68
*/
static int
{
int year;
(void) p1275_cif_handler(&ci);
if (year >= 2000)
year -= 2000;
else
year -= 1900;
return (DDI_SUCCESS);
}
/*
* Read the date using "set-time" method in rtc node
* For values 00 - 68, write 2000-2068, and for 69-99,
* write 1969-1999
*/
static int
{
int year;
return (DDI_FAILURE);
if (year <= 68)
else
(void) p1275_cif_handler(&ci);
return (DDI_SUCCESS);
}