/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* The "todds1287" module has implementation for both tod
* and power button (pbio) interfaces. This driver controls
* RTC & APC units of National Semiconductor's 87317 SuperI/O
* chip. The tod interface accesses the RTC unit and pbio
* interface accesses the APC unit of SuperI/O. Since both
* units are implemented in the same Logical Device, registers
* for both units are accessible through a common set of index
* address & data registers. That is why both interfaces are
* implemented in a same driver.
*
* The APC unit is used to implement the power button. When the
* button momentarily is pressed, an interrupt is generated and
* at the same time a Fail-safe timer starts to run. If the
* timer is not stopped in 21 seconds, the power to system is
* turned off. So the first task in the interrupt handler is to
* reset the Fail-safe timer. Note that OBP is not clearing
* the Fail-safe timer due to limitation in handling interrupts,
* so when OBP is running, the power button should be pressed
* and held for 4 seconds for the power to go off, otherwise
* a momentarily press will delay the power-off for 21 seconds.
*
* PSARC/1999/393 describes the pbio(7I) interface.
*/
#include <sys/todds1287.h>
#include <sys/machsystm.h>
static timestruc_t todds_get(void);
static void todds_set(timestruc_t);
static uint_t todds_clear_watchdog_timer(void);
static void todds_set_power_alarm(timestruc_t);
static void todds_clear_power_alarm(void);
static uint64_t todds_get_cpufrequency(void);
/*
* External variables
*/
extern int watchdog_activated;
extern uint_t watchdog_timeout_seconds;
extern volatile uint8_t *v_pmc_addr_reg;
/*
* Global variables
*/
int ds1287_debug_flags;
int ds1287_caddr_warn;
/*
* cb ops
*/
static void write_rtc_time(struct rtc_t *);
static void write_rtc_alarm(struct rtc_t *);
static void select_bank(int bank);
static void ds1287_timeout(caddr_t);
static void ds1287_log_message(void);
ds1287_open, /* open */
ds1287_close, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
nodev, /* read */
nodev, /* write */
ds1287_ioctl, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
ds1287_chpoll, /* poll */
ddi_prop_op, /* cb_prop_op */
NULL, /* streamtab */
CB_REV, /* rev */
nodev, /* int (*cb_aread)() */
nodev /* int (*cb_awrite)() */
};
/*
* dev ops
*/
DEVO_REV, /* devo_rev */
0, /* refcnt */
ds1287_getinfo, /* getinfo */
nulldev, /* identify */
nulldev, /* probe */
ds1287_attach, /* attach */
ds1287_detach, /* detach */
nodev, /* reset */
&ds1287_cbops, /* cb_ops */
NULL, /* power */
ddi_quiesce_not_supported, /* devo_quiesce */
};
static void *ds1287_state;
/* Driver Tunables */
static int power_button_pressed = 0;
static int power_button_cancel = 0;
static int power_button_timeouts = 0;
static int timeout_cancel = 0;
static int additional_presses = 0;
&mod_driverops, /* Type of module. This one is a driver */
"ds1287 clock driver", /* Name of the module. */
&ds1287_ops, /* driver ops */
};
};
int
_init(void)
{
int status;
if (status != 0) {
return (status);
}
return (status);
}
/* Select Bank 1 */
select_bank(1);
/*
* If v_pmc_addr_reg isn't set, it's because it wasn't set in
* sun4u/os/fillsysinfo.c:have_pmc(). This means the real (pmc)
* use our own NOP routines.
*/
if (v_pmc_addr_reg == NULL) {
}
return (status);
}
int
_fini(void)
{
return (EBUSY);
return (mod_remove(&modlinkage));
}
/*
* The loadable-module _info(9E) entry point
*/
int
{
}
/*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);
}
}
static int
{
DPRINTF("ds1287_attach\n");
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
if (instance != -1) {
"attached.");
return (DDI_FAILURE);
}
if (v_rtc_addr_reg == NULL) {
return (DDI_FAILURE);
}
/*
* Allocate softc information.
*/
"soft states.");
return (DDI_FAILURE);
}
(void *)softsp);
sizeof (int)) != DDI_PROP_SUCCESS) {
"interrupt-priorities\" property.");
goto error;
}
/* add the softint */
DDI_SUCCESS) {
goto error1;
}
/* add the hi interrupt */
"interrupt.");
goto error2;
}
/*
* Combination of instance number and clone number 0 is used for
* creating the minor node.
*/
goto error3;
}
return (DDI_SUCCESS);
return (DDI_FAILURE);
}
/*ARGSUSED*/
static int
{
DPRINTF("ds1287_detach\n");
switch (cmd) {
case DDI_DETACH:
/*
* Since it needs to always handle the power button, fail
* to detach.
*/
return (DDI_FAILURE);
case DDI_SUSPEND:
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/*ARGSUSED1*/
static int
{
int clone;
return (EINVAL);
NULL)
return (ENXIO);
break;
if (clone == DS1287_MAX_CLONE) {
"to clone a minor.");
return (ENXIO);
}
return (0);
}
/*ARGSUSED*/
static int
{
int clone;
return (EINVAL);
NULL)
return (ENXIO);
softsp->monitor_on = 0;
return (0);
}
/*ARGSUSED4*/
static int
{
int clone;
NULL)
return (ENXIO);
switch (cmd) {
case PB_BEGIN_MONITOR:
DPRINTF("ds1287_ioctl: PB_BEGIN_MONITOR is called.\n");
if (softsp->monitor_on) {
return (EBUSY);
}
return (0);
case PB_END_MONITOR:
DPRINTF("ds1287_ioctl: PB_END_MONITOR is called.\n");
/*
* If PB_END_MONITOR is called without first
* calling PB_BEGIN_MONITOR, an error will be
* returned.
*/
if (!softsp->monitor_on) {
return (ENXIO);
}
/*
* This clone is not monitoring the button.
*/
return (EINVAL);
}
softsp->monitor_on = 0;
return (0);
case PB_GET_EVENTS:
DPRINTF("ds1287_ioctl: PB_GET_EVENTS is called.\n");
sizeof (int), mode) != 0) {
return (EFAULT);
}
/*
* This ioctl returned the events detected since last
* call. Note that any application can get the events
* and clear the event register.
*/
return (0);
/*
* This ioctl is used by the test suite.
*/
case PB_CREATE_BUTTON_EVENT:
DPRINTF("ds1287_ioctl: PB_CREATE_BUTTON_EVENT is called.\n");
(void) ds1287_intr(NULL);
return (0);
default:
return (ENOTTY);
}
}
/*ARGSUSED*/
static int
{
return (ENXIO);
*reventsp = 0;
else {
if (!anyyet)
}
return (0);
}
static void
ds1287_log_message(void)
{
return;
}
softsp->shutdown_pending = 0;
}
/*
* To facilitate a power button abort, ds1287_intr() now posts
* a softint (calling ds1287_softintr()) for all power button presses and
* counts the number of button presses. An abort is issued if the desired
* number of button presses within the given time interval.
*
* Two variables are used to synchronize between the high level intr;
* the softint handler and timeout handler
*
* power_button_cancel - Indicates that an abort happened and the number
* of outstanding timeouts that have to be cancelled
*
* power_button_pressed - Indicates the number of button presses outstanding
* which have not been serviced
*/
/*ARGSUSED*/
static uint_t
{
static int power_button_cnt;
/*
* Stop the Fail-safe timer that starts running
* after power button is pressed. If it is not
* stopped in 21 seconds, system powers off.
*/
select_bank(2);
select_bank(1);
/* need to deal with power button debounce */
return (DDI_INTR_CLAIMED);
}
/*
* If power button abort is enabled and power button was pressed
* power_button_abort_presses times within power_button_abort_interval
* then call abort_sequence_enter();
*/
if (power_button_abort_enable) {
if (power_button_abort_presses == 1 ||
if (power_button_cnt == power_button_abort_presses) {
power_button_pressed = 0;
power_button_cnt = 0;
abort_sequence_enter("Power Button Abort");
return (DDI_INTR_CLAIMED);
}
} else {
power_button_cnt = 1;
}
}
if (!power_button_enable)
return (DDI_INTR_CLAIMED);
/* post softint to issue timeout for power button action */
return (DDI_INTR_CLAIMED);
}
/*
* Handle the softints....
*
* If only one softint is posted for several button presses, record
* the number of additional presses just incase this was actually not quite
* an Abort sequence so that we can log this event later.
*
* Issue a timeout with a duration being a fraction larger than
* the specified Abort interval inorder to perform a power down if required.
*/
static uint_t
{
DPRINTF("ds1287_softintr\n");
return (ds1287_issue_shutdown(arg));
if (!power_button_pressed) {
return (DDI_INTR_CLAIMED);
}
/*
* Schedule a timeout to do the necessary
* work for shutdown, only one timeout for
* n presses if power button was pressed
* more than once before softint fired
*/
if (power_button_pressed > 1)
timeout_cancel = 0;
power_button_pressed = 0;
(void) timeout((void(*)(void *))ds1287_timeout,
return (DDI_INTR_CLAIMED);
}
/*
* Upon receiving a timeout the following is determined:
*
* If an Abort sequence was issued, then we cancel all outstanding timeouts
* and additional presses prior to the Abort sequence.
*
* If we had multiple timeouts issued and the abort sequence was not met,
* then we had more than one button press to power down the machine. We
* were probably trying to issue an abort. So log a message indicating this
* and cancel all outstanding timeouts.
*
* If we had just one timeout and the abort sequence was not met then
* we really did want to power down the machine, so call ds1287_issue_shutdown()
* to do the work and schedule a power down
*/
static void
{
static int first = 0;
DPRINTF("ds1287_timeout\n");
/*
* Abort was generated cancel all outstanding power
* button timeouts
*/
if (power_button_cancel) {
if (!first) {
first++;
additional_presses = 0;
}
return;
}
first = 0;
/*
* We get here if the timeout(s) have fired and they were
* not issued prior to an abort.
*
* If we had more than one press in the interval we were
* probably trying to issue an abort, but didnt press the
* required number within the interval. Hence cancel all
* timeouts and do not continue towards shutdown.
*/
if (!timeout_cancel) {
if (!power_button_timeouts)
additional_presses = 0;
if (timeout_cancel > 1) {
"%d times, cancelling all requests",
return;
}
/* Go and do the work to request shutdown */
(void) ds1287_issue_shutdown(arg);
return;
}
if (!power_button_timeouts)
additional_presses = 0;
}
static uint_t
{
DPRINTF("ds1287_issue_shutdown\n");
if (softsp->monitor_on != 0) {
return (DDI_INTR_CLAIMED);
}
if (!softsp->shutdown_pending) {
"the system!");
do_shutdown();
/*
* Wait a while for "do_shutdown()" to shut down the system
* before logging an error message.
*/
100 * hz);
}
return (DDI_INTR_CLAIMED);
}
/*
* Read the current time from the clock chip and convert to UNIX form.
* Assumes that the year in the clock chip is valid.
* Must be called with tod_lock held.
*/
static timestruc_t
todds_get(void)
{
DPRINTF("todds_get: century=%d year=%d dom=%d hrs=%d\n",
/*
* tod_year is base 1900 so this code needs to adjust the true
* year retrieved from the rtc's century and year fields.
*/
/* set the hw watchdog timer if it's been activated */
if (watchdog_activated) {
int ret = 0;
if (ret == 0)
"watchdog timer.");
}
return (ts);
}
void
{
/*
* Some SuperIO tod devices don't seem to properly initialize
* the CADDR register to place the Century register at bank 1
* address 0x48.
*/
select_bank(2);
if (regb != 0xc8) {
if (!ds1287_caddr_warn) {
ds1287_caddr_warn = 1;
"incorrect (exp 0xc8, obs %x)", regb);
}
DS1287_DATA_REG = 0xc8;
}
select_bank(1);
/*
* Freeze clock update
*/
/* Read date alarm */
/* Read wakeup data */
select_bank(2);
/*
* Unfreeze clock update
*/
}
/*
* Write the specified time into the clock chip.
* Must be called with tod_lock held.
*/
static void
{
int year;
/* tod_year is base 1900 so this code needs to adjust */
DPRINTF("todds_set: century=%d year=%d dom=%d hrs=%d\n",
}
void
{
/*
* Some SuperIO tod devices don't seem to properly initialize
* the CADDR register to place the Century register at bank 1
* address 0x48.
*/
select_bank(2);
if (regb != 0xc8) {
if (!ds1287_caddr_warn) {
ds1287_caddr_warn = 1;
"incorrect (exp 0xc8, obs %x)", regb);
}
DS1287_DATA_REG = 0xc8;
}
select_bank(1);
/*
* Freeze
*/
/*
* Unfreeze
*/
}
void
{
select_bank(1);
select_bank(2);
}
/*
* program the rtc registers for alarm to go off at the specified time
*/
static void
{
/* Clear Time Match Detect */
select_bank(2);
/* Disable Time Match Enable */
/* Enable Time Match enable */
select_bank(2);
}
/*
* clear alarm interrupt
*/
static void
todds_clear_power_alarm(void)
{
/* Clear Time Match Detect */
select_bank(2);
/* Disable Time Match Enable */
}
/*
* Determine the cpu frequency by watching the TOD chip rollover twice.
* Cpu clock rate is determined by computing the ticks added (in tick register)
* during one second interval on TOD.
*/
todds_get_cpufrequency(void)
{
select_bank(1);
return (cpu_freq);
}
static void
{
int banksel;
/* Select Bank 1 */
switch (bank) {
case 0:
break;
case 1:
break;
case 2:
break;
}
}
/*ARGSUSED*/
static uint_t
{
return (0);
}
static uint_t
{
return (0);
}