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
53728Srameshc * Common Development and Distribution License (the "License").
63728Srameshc * 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*11752STrevor.Thompson@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/conf.h>
280Sstevel@tonic-gate #include <sys/devops.h>
290Sstevel@tonic-gate #include <sys/kmem.h>
300Sstevel@tonic-gate #include <sys/open.h>
310Sstevel@tonic-gate #include <sys/file.h>
320Sstevel@tonic-gate #include <sys/note.h>
330Sstevel@tonic-gate #include <sys/ddi.h>
340Sstevel@tonic-gate #include <sys/sunddi.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include <sys/modctl.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/machsystm.h>
410Sstevel@tonic-gate #include <sys/poll.h>
420Sstevel@tonic-gate #include <sys/pbio.h>
430Sstevel@tonic-gate #include <sys/sysmacros.h>
440Sstevel@tonic-gate
450Sstevel@tonic-gate /* Added for prom interface */
460Sstevel@tonic-gate #include <sys/promif.h>
470Sstevel@tonic-gate #include <sys/promimpl.h>
480Sstevel@tonic-gate
490Sstevel@tonic-gate #include <sys/i2c/misc/i2c_svc.h>
500Sstevel@tonic-gate #include <sys/todds1337.h>
510Sstevel@tonic-gate
520Sstevel@tonic-gate #define DS1337_DEVICE_TYPE "rtc"
530Sstevel@tonic-gate
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate * Driver entry routines
560Sstevel@tonic-gate */
570Sstevel@tonic-gate static int todds1337_attach(dev_info_t *, ddi_attach_cmd_t);
580Sstevel@tonic-gate static int todds1337_detach(dev_info_t *, ddi_detach_cmd_t);
590Sstevel@tonic-gate static int todds1337_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
600Sstevel@tonic-gate
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate * tod_ops entry routines
630Sstevel@tonic-gate */
640Sstevel@tonic-gate static timestruc_t todds1337_get(void);
650Sstevel@tonic-gate static void todds1337_set(timestruc_t);
660Sstevel@tonic-gate static uint_t todds1337_set_watchdog_timer(uint_t);
670Sstevel@tonic-gate static uint_t todds1337_clear_watchdog_timer(void);
680Sstevel@tonic-gate static void todds1337_set_power_alarm(timestruc_t);
690Sstevel@tonic-gate static void todds1337_clear_power_alarm(void);
700Sstevel@tonic-gate static int todds1337_setup_prom();
710Sstevel@tonic-gate static void todds1337_rele_prom();
720Sstevel@tonic-gate static int todds1337_prom_getdate(struct rtc_t *rtc);
730Sstevel@tonic-gate static int todds1337_prom_setdate(struct rtc_t *rtc);
740Sstevel@tonic-gate
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate * Local functions
770Sstevel@tonic-gate */
780Sstevel@tonic-gate static int todds1337_read_rtc(struct rtc_t *);
790Sstevel@tonic-gate static int todds1337_write_rtc(struct rtc_t *);
800Sstevel@tonic-gate
810Sstevel@tonic-gate /* Anchor for soft state structure */
820Sstevel@tonic-gate static void *ds1337_statep;
830Sstevel@tonic-gate static int instance = -1;
840Sstevel@tonic-gate static int todds1337_attach_done = 0;
850Sstevel@tonic-gate static kmutex_t todds1337_rd_lock;
860Sstevel@tonic-gate static kmutex_t todds1337_alarm_lock;
870Sstevel@tonic-gate static ihandle_t todds1337_ihandle = 0;
880Sstevel@tonic-gate
890Sstevel@tonic-gate /* one second time out */
900Sstevel@tonic-gate #define I2C_CYCLIC_TIMEOUT 1000000000
910Sstevel@tonic-gate uint_t i2c_cyclic_timeout = I2C_CYCLIC_TIMEOUT;
920Sstevel@tonic-gate static int sync_clock_once = 1;
930Sstevel@tonic-gate static struct rtc_t soft_rtc;
940Sstevel@tonic-gate
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate * cp_ops structure
970Sstevel@tonic-gate */
980Sstevel@tonic-gate static struct cb_ops ds1337_cbops = {
990Sstevel@tonic-gate nodev, /* open */
1000Sstevel@tonic-gate nodev, /* close */
1010Sstevel@tonic-gate nodev, /* strategy */
1020Sstevel@tonic-gate nodev, /* print */
1030Sstevel@tonic-gate nodev, /* dump */
1040Sstevel@tonic-gate nodev, /* read */
1050Sstevel@tonic-gate nodev, /* write */
1060Sstevel@tonic-gate nodev, /* ioctl */
1070Sstevel@tonic-gate nodev, /* devmap */
1080Sstevel@tonic-gate nodev, /* mmap */
1090Sstevel@tonic-gate nodev, /* segmap */
1100Sstevel@tonic-gate NULL, /* poll */
1110Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
1120Sstevel@tonic-gate NULL, /* streamtab */
1130Sstevel@tonic-gate D_NEW | D_MP, /* Driver compatibility flag */
1140Sstevel@tonic-gate CB_REV, /* rev */
1150Sstevel@tonic-gate nodev, /* int (*cb_aread)() */
1160Sstevel@tonic-gate nodev /* int (*cb_awrite)() */
1170Sstevel@tonic-gate };
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate * dev_ops structure
1210Sstevel@tonic-gate */
1220Sstevel@tonic-gate static struct dev_ops ds1337_ops = {
1230Sstevel@tonic-gate DEVO_REV, /* devo_rev */
1240Sstevel@tonic-gate 0, /* refcnt - reference cnt always set to 0 */
1250Sstevel@tonic-gate todds1337_getinfo, /* getinfo - Maybe requred */
1260Sstevel@tonic-gate nulldev, /* identify */
1270Sstevel@tonic-gate nulldev, /* probe */
1280Sstevel@tonic-gate todds1337_attach, /* attach */
1290Sstevel@tonic-gate todds1337_detach, /* detach */
1300Sstevel@tonic-gate nodev, /* reset */
1310Sstevel@tonic-gate &ds1337_cbops, /* cb_ops - ds1337 does not need this(?) */
1327656SSherry.Moore@Sun.COM NULL, /* bus_ops */
1337656SSherry.Moore@Sun.COM NULL, /* power */
1347656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */
1350Sstevel@tonic-gate };
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate static struct modldrv todds1337_modldrv = {
1380Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a driver */
1397696SRichard.Bean@Sun.COM "tod driver for DS1337", /* Name of the module. */
1400Sstevel@tonic-gate &ds1337_ops, /* Pointer to dev_ops */
1410Sstevel@tonic-gate };
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate * Module linkage structure
1450Sstevel@tonic-gate */
1460Sstevel@tonic-gate static struct modlinkage todds1337_modlinkage = {
1470Sstevel@tonic-gate MODREV_1,
1480Sstevel@tonic-gate &todds1337_modldrv,
1490Sstevel@tonic-gate 0
1500Sstevel@tonic-gate };
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate int
_init(void)1530Sstevel@tonic-gate _init(void)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate int error;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate if (strcmp(tod_module_name, "todds1337") == 0) {
1580Sstevel@tonic-gate if ((error = ddi_soft_state_init(&ds1337_statep,
1595107Seota sizeof (ds1337_state_t), 0)) != DDI_SUCCESS) {
1600Sstevel@tonic-gate return (error);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate tod_ops.tod_get = todds1337_get;
1640Sstevel@tonic-gate tod_ops.tod_set = todds1337_set;
1650Sstevel@tonic-gate tod_ops.tod_set_watchdog_timer = todds1337_set_watchdog_timer;
1660Sstevel@tonic-gate tod_ops.tod_clear_watchdog_timer =
1670Sstevel@tonic-gate todds1337_clear_watchdog_timer;
1680Sstevel@tonic-gate tod_ops.tod_set_power_alarm = todds1337_set_power_alarm;
1690Sstevel@tonic-gate tod_ops.tod_clear_power_alarm = todds1337_clear_power_alarm;
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate (void) todds1337_setup_prom();
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate * Install the module
1760Sstevel@tonic-gate */
1770Sstevel@tonic-gate if ((error = mod_install(&todds1337_modlinkage)) != 0) {
1783728Srameshc if (strcmp(tod_module_name, "todds1337") == 0) {
1793728Srameshc ddi_soft_state_fini(&ds1337_statep);
1803728Srameshc }
1813728Srameshc todds1337_rele_prom();
1820Sstevel@tonic-gate return (error);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate mutex_init(&todds1337_rd_lock, NULL, MUTEX_DEFAULT, NULL);
1850Sstevel@tonic-gate mutex_init(&todds1337_alarm_lock, NULL, MUTEX_DEFAULT, NULL);
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate return (0);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate int
_fini(void)1910Sstevel@tonic-gate _fini(void)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate int error = 0;
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate if (strcmp(tod_module_name, "todds1337") == 0) {
1960Sstevel@tonic-gate error = EBUSY;
1970Sstevel@tonic-gate } else {
1980Sstevel@tonic-gate if ((error = mod_remove(&todds1337_modlinkage)) == 0) {
1990Sstevel@tonic-gate mutex_destroy(&todds1337_rd_lock);
2000Sstevel@tonic-gate mutex_destroy(&todds1337_alarm_lock);
2010Sstevel@tonic-gate todds1337_rele_prom();
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate return (error);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2090Sstevel@tonic-gate _info(struct modinfo *modinfop)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate return (mod_info(&todds1337_modlinkage, modinfop));
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate * cyclical call to get tod.
2160Sstevel@tonic-gate */
2170Sstevel@tonic-gate static void
todds1337_cyclic(void * arg)2180Sstevel@tonic-gate todds1337_cyclic(void *arg)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate (void) todds1337_read_rtc((struct rtc_t *)arg);
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * register ds1337 client device with i2c services, and
2270Sstevel@tonic-gate * allocate & initialize soft state structure.
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate static int
todds1337_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)2300Sstevel@tonic-gate todds1337_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate static ds1337_state_t *statep = NULL;
2330Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL;
2340Sstevel@tonic-gate uint8_t tempVal = (uint8_t)0;
2350Sstevel@tonic-gate switch (cmd) {
2360Sstevel@tonic-gate case DDI_ATTACH:
2370Sstevel@tonic-gate break;
2380Sstevel@tonic-gate case DDI_RESUME:
2390Sstevel@tonic-gate return (DDI_SUCCESS);
2400Sstevel@tonic-gate default:
2410Sstevel@tonic-gate return (DDI_FAILURE);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate if (instance != -1) {
2450Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: wrong instance");
2460Sstevel@tonic-gate return (DDI_FAILURE);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate instance = ddi_get_instance(dip);
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate * Allocate soft state structure
2530Sstevel@tonic-gate */
2540Sstevel@tonic-gate if (ddi_soft_state_zalloc(ds1337_statep, instance) != DDI_SUCCESS) {
2550Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: cannot allocate soft "
2560Sstevel@tonic-gate "state");
2570Sstevel@tonic-gate instance = -1;
2580Sstevel@tonic-gate return (DDI_FAILURE);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance);
2620Sstevel@tonic-gate if (statep == NULL) {
2630Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: cannot acquire soft "
2640Sstevel@tonic-gate "state");
2650Sstevel@tonic-gate instance = -1;
2660Sstevel@tonic-gate return (DDI_FAILURE);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate statep->dip = dip;
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate if (i2c_client_register(dip, &statep->ds1337_i2c_hdl) != I2C_SUCCESS) {
2720Sstevel@tonic-gate ddi_soft_state_free(ds1337_statep, instance);
2730Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: cannot register i2c "
2740Sstevel@tonic-gate "client");
2750Sstevel@tonic-gate instance = -1;
2760Sstevel@tonic-gate return (DDI_FAILURE);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate /* check and initialize the oscillator */
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
2820Sstevel@tonic-gate &i2c_tp, 1, 1, I2C_SLEEP);
2830Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV;
2840Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD;
2850Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */
2860Sstevel@tonic-gate i2c_tp->i2c_wlen = 1;
2870Sstevel@tonic-gate i2c_tp->i2c_rlen = 1;
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
2900Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
2910Sstevel@tonic-gate i2c_client_unregister(statep->ds1337_i2c_hdl);
2920Sstevel@tonic-gate ddi_soft_state_free(ds1337_statep, instance);
2930Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: failed to read DS1337 "
2940Sstevel@tonic-gate "status register");
2950Sstevel@tonic-gate instance = -1;
2960Sstevel@tonic-gate return (DDI_FAILURE);
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate tempVal = i2c_tp->i2c_rbuf[0];
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate * Check Oscillator and initialize chip if OBP failed to do it
3050Sstevel@tonic-gate */
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate if (tempVal & RTC_CTL_EOSC) {
3080Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp,
3090Sstevel@tonic-gate 2, 0, I2C_SLEEP);
3100Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV;
3110Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR;
3120Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = RTC_CTL; /* Write Control register */
3130Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = (uchar_t)(RTC_CTL_RS2 | RTC_CTL_RS1 |
3140Sstevel@tonic-gate RTC_CTL_INTCN);
3150Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp))
3160Sstevel@tonic-gate != I2C_SUCCESS) {
3170Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl,
3180Sstevel@tonic-gate i2c_tp);
3190Sstevel@tonic-gate i2c_client_unregister(statep->ds1337_i2c_hdl);
3200Sstevel@tonic-gate ddi_soft_state_free(ds1337_statep, instance);
3210Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: failed to write "
3220Sstevel@tonic-gate "DS1337 control register");
3230Sstevel@tonic-gate instance = -1;
3240Sstevel@tonic-gate return (DDI_FAILURE);
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate /*
3300Sstevel@tonic-gate * Now reset the OSF flag in the Status register
3310Sstevel@tonic-gate */
3320Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp,
3330Sstevel@tonic-gate 2, 0, I2C_SLEEP);
3340Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV;
3350Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR;
3360Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = RTC_STATUS;
3370Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = (uchar_t)0;
3380Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp))
3390Sstevel@tonic-gate != I2C_SUCCESS) {
3400Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl,
3410Sstevel@tonic-gate i2c_tp);
3420Sstevel@tonic-gate i2c_client_unregister(statep->ds1337_i2c_hdl);
3430Sstevel@tonic-gate ddi_soft_state_free(ds1337_statep, instance);
3440Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: failed to write "
3450Sstevel@tonic-gate "DS1337 status register");
3460Sstevel@tonic-gate instance = -1;
3470Sstevel@tonic-gate return (DDI_FAILURE);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate
3535107Seota /*
3545107Seota * Create a periodical handler to read TOD.
3555107Seota */
3565107Seota ASSERT(statep->cycid == NULL);
3575107Seota statep->cycid = ddi_periodic_add(todds1337_cyclic, &soft_rtc,
3585107Seota i2c_cyclic_timeout, DDI_IPL_1);
3590Sstevel@tonic-gate statep->state = TOD_ATTACHED;
3600Sstevel@tonic-gate todds1337_attach_done = 1;
3610Sstevel@tonic-gate ddi_report_dev(dip);
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate return (DDI_SUCCESS);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate /*ARGSUSED*/
3670Sstevel@tonic-gate static int
todds1337_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)3680Sstevel@tonic-gate todds1337_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate * Once attached, do not allow detach because the system constantly
3720Sstevel@tonic-gate * calling todds1337_get() to get the time. If the driver is detached
3730Sstevel@tonic-gate * and the system try to get the time, the system will have memory
3740Sstevel@tonic-gate * problem.
3750Sstevel@tonic-gate *
3760Sstevel@tonic-gate */
3770Sstevel@tonic-gate switch (cmd) {
3780Sstevel@tonic-gate case DDI_SUSPEND:
3790Sstevel@tonic-gate return (DDI_SUCCESS);
3800Sstevel@tonic-gate default:
3810Sstevel@tonic-gate return (DDI_FAILURE);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate /* *********************** tod_ops entry points ******************** */
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate /*
3880Sstevel@tonic-gate * Read the current time from the DS1337 chip and convert to UNIX form.
389*11752STrevor.Thompson@Sun.COM * Should be called with tod_lock held.
3900Sstevel@tonic-gate */
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate static timestruc_t
todds1337_get(void)3930Sstevel@tonic-gate todds1337_get(void)
3940Sstevel@tonic-gate {
3950Sstevel@tonic-gate timestruc_t ts;
3960Sstevel@tonic-gate todinfo_t tod;
3970Sstevel@tonic-gate struct rtc_t rtc;
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate if (sync_clock_once) {
4020Sstevel@tonic-gate (void) todds1337_read_rtc(&soft_rtc);
4030Sstevel@tonic-gate sync_clock_once = 0;
4040Sstevel@tonic-gate } else {
405*11752STrevor.Thompson@Sun.COM tod_status_set(TOD_GET_FAILED);
4060Sstevel@tonic-gate return (hrestime);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate bcopy(&soft_rtc, &rtc, sizeof (rtc));
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate /*
4120Sstevel@tonic-gate * 00 - 68 = 2000 thru 2068
4130Sstevel@tonic-gate * 69-99 = 1969 thru 1999
4140Sstevel@tonic-gate */
4150Sstevel@tonic-gate tod.tod_year = rtc.rtc_year;
4160Sstevel@tonic-gate if (rtc.rtc_year <= 68)
4170Sstevel@tonic-gate tod.tod_year += 100;
4180Sstevel@tonic-gate tod.tod_month = rtc.rtc_mon;
4190Sstevel@tonic-gate tod.tod_day = rtc.rtc_dom;
4200Sstevel@tonic-gate tod.tod_dow = rtc.rtc_dow;
4210Sstevel@tonic-gate tod.tod_hour = rtc.rtc_hrs;
4220Sstevel@tonic-gate tod.tod_min = rtc.rtc_min;
4230Sstevel@tonic-gate tod.tod_sec = rtc.rtc_sec;
4240Sstevel@tonic-gate
425*11752STrevor.Thompson@Sun.COM /* read was successful so ensure failure flag is clear */
426*11752STrevor.Thompson@Sun.COM tod_status_clear(TOD_GET_FAILED);
427*11752STrevor.Thompson@Sun.COM
4280Sstevel@tonic-gate ts.tv_sec = tod_to_utc(tod);
4290Sstevel@tonic-gate ts.tv_nsec = 0;
4300Sstevel@tonic-gate return (ts);
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate
4330Sstevel@tonic-gate /*
4340Sstevel@tonic-gate * Program DS1337 with the specified time.
4350Sstevel@tonic-gate * Must be called with tod_lock held. The TOD
4360Sstevel@tonic-gate * chip supports date from 1969-2068 only. We must
4370Sstevel@tonic-gate * reject requests to set date below 1969.
4380Sstevel@tonic-gate */
4390Sstevel@tonic-gate static void
todds1337_set(timestruc_t ts)4400Sstevel@tonic-gate todds1337_set(timestruc_t ts)
4410Sstevel@tonic-gate {
4420Sstevel@tonic-gate struct rtc_t rtc;
4430Sstevel@tonic-gate todinfo_t tod = utc_to_tod(ts.tv_sec);
4440Sstevel@tonic-gate int year;
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate /*
4500Sstevel@tonic-gate * Year is base 1900, valid year range 1969-2068
4510Sstevel@tonic-gate */
4520Sstevel@tonic-gate if ((tod.tod_year < 69) || (tod.tod_year > 168))
4530Sstevel@tonic-gate return;
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate year = tod.tod_year;
4560Sstevel@tonic-gate if (year >= 100)
4570Sstevel@tonic-gate year -= 100;
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate rtc.rtc_year = (uint8_t)year;
4600Sstevel@tonic-gate rtc.rtc_mon = (uint8_t)tod.tod_month;
4610Sstevel@tonic-gate rtc.rtc_dom = (uint8_t)tod.tod_day;
4620Sstevel@tonic-gate rtc.rtc_dow = (uint8_t)tod.tod_dow;
4630Sstevel@tonic-gate rtc.rtc_hrs = (uint8_t)tod.tod_hour;
4640Sstevel@tonic-gate rtc.rtc_min = (uint8_t)tod.tod_min;
4650Sstevel@tonic-gate rtc.rtc_sec = (uint8_t)tod.tod_sec;
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate (void) todds1337_write_rtc(&rtc);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate /*
4710Sstevel@tonic-gate * Program ds1337 registers for alarm to go off at the specified time.
4720Sstevel@tonic-gate * Alarm #1 is used (Alarm #2 stays idle)
4730Sstevel@tonic-gate */
4740Sstevel@tonic-gate /* ARGSUSED */
4750Sstevel@tonic-gate static void
todds1337_set_power_alarm(timestruc_t ts)4760Sstevel@tonic-gate todds1337_set_power_alarm(timestruc_t ts)
4770Sstevel@tonic-gate {
4780Sstevel@tonic-gate todinfo_t tod;
4790Sstevel@tonic-gate ds1337_state_t *statep = NULL;
4800Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL;
4810Sstevel@tonic-gate uint8_t tmpval;
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate if (!todds1337_attach_done) {
4860Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: driver not attached");
4870Sstevel@tonic-gate return;
4880Sstevel@tonic-gate }
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance);
4910Sstevel@tonic-gate if (statep == NULL) {
4920Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failed");
4930Sstevel@tonic-gate return;
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate tod = utc_to_tod(ts.tv_sec);
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate /*
4990Sstevel@tonic-gate * i2c_transfe() may block; to avoid locking clock() which
5000Sstevel@tonic-gate * is running in interrupt context at PIL10 we temporarely exit
5010Sstevel@tonic-gate * the tod_mutex and enter alarm lock. Time/date and alarm hardware
5020Sstevel@tonic-gate * have non-interlsecting registers, it is safe to use different locks.
5030Sstevel@tonic-gate */
5040Sstevel@tonic-gate mutex_exit(&tod_lock);
5050Sstevel@tonic-gate mutex_enter(&todds1337_alarm_lock);
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate * Disable Power Alarm (A1IE)
5090Sstevel@tonic-gate */
5100Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
5110Sstevel@tonic-gate &i2c_tp, 1, 1, I2C_SLEEP);
5120Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV;
5130Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD;
5140Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
5170Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5180Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to read "
5190Sstevel@tonic-gate "DS1337 control register");
5200Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock);
5210Sstevel@tonic-gate mutex_enter(&tod_lock);
5220Sstevel@tonic-gate return;
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate
5250Sstevel@tonic-gate tmpval = i2c_tp->i2c_rbuf[0];
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
5300Sstevel@tonic-gate &i2c_tp, 2, 0, I2C_SLEEP);
5310Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV;
5320Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR;
5330Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */
5340Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE;
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
5370Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5380Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write "
5390Sstevel@tonic-gate "DS1337control register");
5400Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock);
5410Sstevel@tonic-gate mutex_enter(&tod_lock);
5420Sstevel@tonic-gate return;
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate
5480Sstevel@tonic-gate /*
5490Sstevel@tonic-gate * Write Alarm #1 registers
5500Sstevel@tonic-gate */
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
5530Sstevel@tonic-gate &i2c_tp, 5, 0, I2C_SLEEP);
5540Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV;
5550Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR;
5560Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_ALARM_SEC; /* Alarm #1 Seconds */
5570Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(tod.tod_sec);
5580Sstevel@tonic-gate i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(tod.tod_min);
5590Sstevel@tonic-gate i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(tod.tod_hour);
5600Sstevel@tonic-gate i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(tod.tod_day);
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
5630Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5640Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write "
5650Sstevel@tonic-gate "DS1337 alarm registers");
5660Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock);
5670Sstevel@tonic-gate mutex_enter(&tod_lock);
5680Sstevel@tonic-gate return;
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate
5710Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate /*
5750Sstevel@tonic-gate * Enable Power Alarm
5760Sstevel@tonic-gate */
5770Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
5780Sstevel@tonic-gate &i2c_tp, 2, 0, I2C_SLEEP);
5790Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV;
5800Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR;
5810Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */
5820Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = tmpval | RTC_CTL_A1IE;
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
5850Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5860Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to enable "
5870Sstevel@tonic-gate "DS1337 alarm");
5880Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock);
5890Sstevel@tonic-gate mutex_enter(&tod_lock);
5900Sstevel@tonic-gate return;
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock);
5960Sstevel@tonic-gate mutex_enter(&tod_lock);
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate
5990Sstevel@tonic-gate /* ARGSUSED */
6000Sstevel@tonic-gate static void
todds1337_clear_power_alarm(void)6010Sstevel@tonic-gate todds1337_clear_power_alarm(void)
6020Sstevel@tonic-gate {
6030Sstevel@tonic-gate ds1337_state_t *statep = NULL;
6040Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL;
6050Sstevel@tonic-gate uint8_t tmpval;
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
6080Sstevel@tonic-gate
6090Sstevel@tonic-gate if (!todds1337_attach_done) {
6100Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: driver was not attached");
6110Sstevel@tonic-gate return;
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance);
6150Sstevel@tonic-gate if (statep == NULL) {
6160Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: ddi_get_soft_state has failed");
6170Sstevel@tonic-gate return;
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate
6200Sstevel@tonic-gate /*
6210Sstevel@tonic-gate * i2c_transfe() may block; to avoid locking clock() which
6220Sstevel@tonic-gate * is running in interrupt context at PIL10 we temporarely exit
6230Sstevel@tonic-gate * the tod_mutex and enter alarm lock. Time/date and alarm hardware
6240Sstevel@tonic-gate * have non-interlsecting registers, it is safe to use different locks.
6250Sstevel@tonic-gate */
6260Sstevel@tonic-gate mutex_exit(&tod_lock);
6270Sstevel@tonic-gate mutex_enter(&todds1337_alarm_lock);
6280Sstevel@tonic-gate
6290Sstevel@tonic-gate /*
6300Sstevel@tonic-gate * Disable Alarm #1 Interrupt
6310Sstevel@tonic-gate */
6320Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
6330Sstevel@tonic-gate &i2c_tp, 1, 1, I2C_SLEEP);
6340Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV;
6350Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD;
6360Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */
6370Sstevel@tonic-gate
6380Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
6390Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
6400Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read "
6410Sstevel@tonic-gate "DS1337 control register");
6420Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock);
6430Sstevel@tonic-gate mutex_enter(&tod_lock);
6440Sstevel@tonic-gate return;
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate tmpval = i2c_tp->i2c_rbuf[0];
6480Sstevel@tonic-gate
6490Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
6520Sstevel@tonic-gate &i2c_tp, 2, 0, I2C_SLEEP);
6530Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV;
6540Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR;
6550Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */
6560Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE;
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
6590Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
6600Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write "
6610Sstevel@tonic-gate "DS1337 control register");
6620Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock);
6630Sstevel@tonic-gate mutex_enter(&tod_lock);
6640Sstevel@tonic-gate return;
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
6680Sstevel@tonic-gate
6690Sstevel@tonic-gate /*
6700Sstevel@tonic-gate * Reset Alarm #1 Flag
6710Sstevel@tonic-gate */
6720Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
6730Sstevel@tonic-gate &i2c_tp, 1, 1, I2C_SLEEP);
6740Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV;
6750Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD;
6760Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
6790Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
6800Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read "
6810Sstevel@tonic-gate "DS1337 status register");
6820Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock);
6830Sstevel@tonic-gate mutex_enter(&tod_lock);
6840Sstevel@tonic-gate return;
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate
6870Sstevel@tonic-gate tmpval = i2c_tp->i2c_rbuf[0];
6880Sstevel@tonic-gate
6890Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
6920Sstevel@tonic-gate &i2c_tp, 2, 0, I2C_SLEEP);
6930Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV;
6940Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR;
6950Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Write Status register */
6960Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_STATUS_A1F;
6970Sstevel@tonic-gate
6980Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
6990Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
7000Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write "
7010Sstevel@tonic-gate "DS1337 status register");
7020Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock);
7030Sstevel@tonic-gate mutex_enter(&tod_lock);
7040Sstevel@tonic-gate return;
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
7080Sstevel@tonic-gate
7090Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock);
7100Sstevel@tonic-gate mutex_enter(&tod_lock);
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate
7130Sstevel@tonic-gate /* ARGSUSED */
7140Sstevel@tonic-gate static uint_t
todds1337_set_watchdog_timer(uint_t timeoutval)7150Sstevel@tonic-gate todds1337_set_watchdog_timer(uint_t timeoutval)
7160Sstevel@tonic-gate {
7170Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
7180Sstevel@tonic-gate return (0);
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate /* ARGSUSED */
7220Sstevel@tonic-gate static uint_t
todds1337_clear_watchdog_timer(void)7230Sstevel@tonic-gate todds1337_clear_watchdog_timer(void)
7240Sstevel@tonic-gate {
7250Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
7260Sstevel@tonic-gate return (0);
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate /* ********************** Local functions ***************************** */
7300Sstevel@tonic-gate
7310Sstevel@tonic-gate static char tod_read[7] = {-1, -1, -1, -1, -1, -1, -1};
7320Sstevel@tonic-gate static int
todds1337_read_rtc(struct rtc_t * rtc)7330Sstevel@tonic-gate todds1337_read_rtc(struct rtc_t *rtc)
7340Sstevel@tonic-gate {
7350Sstevel@tonic-gate static ds1337_state_t *statep = NULL;
7360Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL;
7370Sstevel@tonic-gate int i2c_cmd_status = I2C_FAILURE;
7380Sstevel@tonic-gate int counter = 4;
7390Sstevel@tonic-gate
7400Sstevel@tonic-gate if (!todds1337_attach_done) {
7410Sstevel@tonic-gate return (todds1337_prom_getdate(rtc));
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate
7440Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance);
7450Sstevel@tonic-gate if (statep == NULL) {
7460Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failing");
7470Sstevel@tonic-gate return (DDI_FAILURE);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate mutex_enter(&todds1337_rd_lock);
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate /*
7530Sstevel@tonic-gate * Allocate 1 byte for write buffer and 7 bytes for read buffer to
7540Sstevel@tonic-gate * to accomodate sec, min, hrs, dayOfWeek, dayOfMonth, year
7550Sstevel@tonic-gate */
7560Sstevel@tonic-gate if ((i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp, 1,
7570Sstevel@tonic-gate 7, I2C_SLEEP)) != I2C_SUCCESS) {
7580Sstevel@tonic-gate mutex_exit(&todds1337_rd_lock);
7590Sstevel@tonic-gate return (DDI_FAILURE);
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate do {
7630Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV;
7640Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD;
7650Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC; /* Start from 0x00 */
7660Sstevel@tonic-gate i2c_tp->i2c_wlen = 1; /* Write one byte address */
7670Sstevel@tonic-gate i2c_tp->i2c_rlen = 7; /* Read 7 regs */
7680Sstevel@tonic-gate
7690Sstevel@tonic-gate if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl,
7700Sstevel@tonic-gate i2c_tp)) != I2C_SUCCESS) {
7710Sstevel@tonic-gate goto done;
7720Sstevel@tonic-gate }
7735107Seota /* for first read, need to get valid data */
7745107Seota while (tod_read[0] == -1 && counter > 0) {
7755107Seota /* move data to static buffer */
7760Sstevel@tonic-gate bcopy(i2c_tp->i2c_rbuf, tod_read, 7);
7770Sstevel@tonic-gate
7780Sstevel@tonic-gate /* now read again */
7790Sstevel@tonic-gate /* Start reading reg from 0x00 */
7800Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)0x00;
7810Sstevel@tonic-gate i2c_tp->i2c_wlen = 1; /* Write one byte address */
7820Sstevel@tonic-gate i2c_tp->i2c_rlen = 7; /* Read 7 regs */
7830Sstevel@tonic-gate if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl,
7840Sstevel@tonic-gate i2c_tp)) != I2C_SUCCESS) {
7855107Seota goto done;
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate /* if they are not the same, then read again */
7880Sstevel@tonic-gate if (bcmp(tod_read, i2c_tp->i2c_rbuf, 7) != 0) {
7895107Seota tod_read[0] = -1;
7905107Seota counter--;
7910Sstevel@tonic-gate }
7925107Seota }
7930Sstevel@tonic-gate
7940Sstevel@tonic-gate } while (i2c_tp->i2c_rbuf[0] == 0x59 &&
7950Sstevel@tonic-gate /* if seconds register is 0x59 (BCD), add data should match */
7965107Seota bcmp(&tod_read[1], &i2c_tp->i2c_rbuf[1], 6) != 0 &&
7975107Seota counter-- > 0);
7980Sstevel@tonic-gate
7990Sstevel@tonic-gate if (counter < 0)
8005107Seota cmn_err(CE_WARN, "i2ctod: TOD Chip failed ??");
8010Sstevel@tonic-gate
8020Sstevel@tonic-gate /* move data to static buffer */
8030Sstevel@tonic-gate bcopy(i2c_tp->i2c_rbuf, tod_read, 7);
8040Sstevel@tonic-gate
8050Sstevel@tonic-gate
8060Sstevel@tonic-gate rtc->rtc_year = BCD_TO_BYTE(i2c_tp->i2c_rbuf[6]);
8070Sstevel@tonic-gate rtc->rtc_mon = BCD_TO_BYTE(i2c_tp->i2c_rbuf[5]);
8080Sstevel@tonic-gate rtc->rtc_dom = BCD_TO_BYTE(i2c_tp->i2c_rbuf[4]);
8090Sstevel@tonic-gate rtc->rtc_dow = BCD_TO_BYTE(i2c_tp->i2c_rbuf[3]);
8100Sstevel@tonic-gate rtc->rtc_hrs = BCD_TO_BYTE(i2c_tp->i2c_rbuf[2]);
8110Sstevel@tonic-gate rtc->rtc_min = BCD_TO_BYTE(i2c_tp->i2c_rbuf[1]);
8120Sstevel@tonic-gate rtc->rtc_sec = BCD_TO_BYTE(i2c_tp->i2c_rbuf[0]);
8130Sstevel@tonic-gate
8140Sstevel@tonic-gate done:
8150Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
8160Sstevel@tonic-gate
8170Sstevel@tonic-gate mutex_exit(&todds1337_rd_lock);
8180Sstevel@tonic-gate return (i2c_cmd_status);
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate
8210Sstevel@tonic-gate
8220Sstevel@tonic-gate static int
todds1337_write_rtc(struct rtc_t * rtc)8230Sstevel@tonic-gate todds1337_write_rtc(struct rtc_t *rtc)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate ds1337_state_t *statep = NULL;
8260Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL;
8270Sstevel@tonic-gate int i2c_cmd_status = I2C_SUCCESS;
8280Sstevel@tonic-gate
8290Sstevel@tonic-gate
8300Sstevel@tonic-gate if (!todds1337_attach_done) {
8310Sstevel@tonic-gate return (todds1337_prom_setdate(rtc));
8320Sstevel@tonic-gate }
8330Sstevel@tonic-gate
8340Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance);
8350Sstevel@tonic-gate if (statep == NULL) {
8360Sstevel@tonic-gate return (DDI_FAILURE);
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate
8390Sstevel@tonic-gate if ((i2c_cmd_status = i2c_transfer_alloc(statep->ds1337_i2c_hdl,
8400Sstevel@tonic-gate &i2c_tp, 8, 0, I2C_SLEEP)) != I2C_SUCCESS) {
8410Sstevel@tonic-gate return (i2c_cmd_status);
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate
8440Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV;
8450Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR;
8460Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC;
8470Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(rtc->rtc_sec);
8480Sstevel@tonic-gate i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(rtc->rtc_min);
8490Sstevel@tonic-gate i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(rtc->rtc_hrs);
8500Sstevel@tonic-gate i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(rtc->rtc_dow);
8510Sstevel@tonic-gate i2c_tp->i2c_wbuf[5] = BYTE_TO_BCD(rtc->rtc_dom);
8520Sstevel@tonic-gate i2c_tp->i2c_wbuf[6] = BYTE_TO_BCD(rtc->rtc_mon);
8530Sstevel@tonic-gate i2c_tp->i2c_wbuf[7] = BYTE_TO_BCD(rtc->rtc_year);
8540Sstevel@tonic-gate i2c_tp->i2c_wlen = 8;
8550Sstevel@tonic-gate
8560Sstevel@tonic-gate if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl,
8570Sstevel@tonic-gate i2c_tp)) != I2C_SUCCESS) {
8580Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
8590Sstevel@tonic-gate return (i2c_cmd_status);
8600Sstevel@tonic-gate }
8610Sstevel@tonic-gate
8620Sstevel@tonic-gate tod_read[0] = -1; /* invalidate saved data from read routine */
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
8650Sstevel@tonic-gate
8660Sstevel@tonic-gate return (i2c_cmd_status);
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate
8690Sstevel@tonic-gate
8700Sstevel@tonic-gate /*ARGSUSED*/
8710Sstevel@tonic-gate static int
todds1337_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)8720Sstevel@tonic-gate todds1337_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
8730Sstevel@tonic-gate void **result)
8740Sstevel@tonic-gate {
8750Sstevel@tonic-gate ds1337_state_t *softsp;
8760Sstevel@tonic-gate
8770Sstevel@tonic-gate if (instance == -1) {
8780Sstevel@tonic-gate return (DDI_FAILURE);
8790Sstevel@tonic-gate }
8800Sstevel@tonic-gate
8810Sstevel@tonic-gate switch (infocmd) {
8820Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
8830Sstevel@tonic-gate if ((softsp = ddi_get_soft_state(ds1337_statep, instance)) ==
8840Sstevel@tonic-gate NULL)
8850Sstevel@tonic-gate return (DDI_FAILURE);
8860Sstevel@tonic-gate *result = (void *)softsp->dip;
8870Sstevel@tonic-gate return (DDI_SUCCESS);
8880Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
8891004Smike_s *result = (void *)(uintptr_t)instance;
8900Sstevel@tonic-gate return (DDI_SUCCESS);
8910Sstevel@tonic-gate default:
8920Sstevel@tonic-gate return (DDI_FAILURE);
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate
8960Sstevel@tonic-gate /*
8970Sstevel@tonic-gate * Finds the device node with device_type "rtc" and opens it to
8980Sstevel@tonic-gate * execute the get-time method
8990Sstevel@tonic-gate */
9000Sstevel@tonic-gate static int
todds1337_setup_prom()9010Sstevel@tonic-gate todds1337_setup_prom()
9020Sstevel@tonic-gate {
903789Sahrens pnode_t todnode;
9040Sstevel@tonic-gate char tod1337_devpath[MAXNAMELEN];
9050Sstevel@tonic-gate
9060Sstevel@tonic-gate if ((todnode = prom_findnode_bydevtype(prom_rootnode(),
9070Sstevel@tonic-gate DS1337_DEVICE_TYPE)) == OBP_NONODE)
9080Sstevel@tonic-gate return (DDI_FAILURE);
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate /*
9110Sstevel@tonic-gate * We now have the phandle of the rtc node, we need to open the
9120Sstevel@tonic-gate * node and get the ihandle
9130Sstevel@tonic-gate */
9140Sstevel@tonic-gate if (prom_phandle_to_path(todnode, tod1337_devpath,
9150Sstevel@tonic-gate sizeof (tod1337_devpath)) < 0) {
9160Sstevel@tonic-gate cmn_err(CE_WARN, "prom_phandle_to_path failed");
9170Sstevel@tonic-gate return (DDI_FAILURE);
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate
9200Sstevel@tonic-gate /*
9210Sstevel@tonic-gate * Now open the node and store it's ihandle
9220Sstevel@tonic-gate */
9230Sstevel@tonic-gate if ((todds1337_ihandle = prom_open(tod1337_devpath)) == NULL) {
9240Sstevel@tonic-gate cmn_err(CE_WARN, "prom_open failed");
9250Sstevel@tonic-gate return (DDI_FAILURE);
9260Sstevel@tonic-gate }
9270Sstevel@tonic-gate
9280Sstevel@tonic-gate return (DDI_SUCCESS);
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate
9310Sstevel@tonic-gate /*
9320Sstevel@tonic-gate * Closes the prom interface
9330Sstevel@tonic-gate */
9340Sstevel@tonic-gate static void
todds1337_rele_prom()9350Sstevel@tonic-gate todds1337_rele_prom()
9360Sstevel@tonic-gate {
9370Sstevel@tonic-gate (void) prom_close(todds1337_ihandle);
9380Sstevel@tonic-gate }
9390Sstevel@tonic-gate
9400Sstevel@tonic-gate /*
9410Sstevel@tonic-gate * Read the date using "get-time" method in rtc node
9420Sstevel@tonic-gate * PROM returns 1969-1999 when reading 69-99 and
9430Sstevel@tonic-gate * 2000-2068 when reading 00-68
9440Sstevel@tonic-gate */
9450Sstevel@tonic-gate static int
todds1337_prom_getdate(struct rtc_t * rtc)9460Sstevel@tonic-gate todds1337_prom_getdate(struct rtc_t *rtc)
9470Sstevel@tonic-gate {
9480Sstevel@tonic-gate int year;
9490Sstevel@tonic-gate cell_t ci[12];
9500Sstevel@tonic-gate
9510Sstevel@tonic-gate ci[0] = p1275_ptr2cell("call-method"); /* Service name */
9520Sstevel@tonic-gate ci[1] = 2; /* # of arguments */
9530Sstevel@tonic-gate ci[2] = 7; /* # of result cells */
9540Sstevel@tonic-gate ci[3] = p1275_ptr2cell("get-time");
9550Sstevel@tonic-gate ci[4] = p1275_ihandle2cell(todds1337_ihandle);
9560Sstevel@tonic-gate
9570Sstevel@tonic-gate promif_preprom();
9580Sstevel@tonic-gate (void) p1275_cif_handler(&ci);
9590Sstevel@tonic-gate promif_postprom();
9600Sstevel@tonic-gate
9610Sstevel@tonic-gate year = p1275_cell2int(ci[6]);
9620Sstevel@tonic-gate rtc->rtc_mon = p1275_cell2int(ci[7]);
9630Sstevel@tonic-gate rtc->rtc_dom = p1275_cell2int(ci[8]);
9640Sstevel@tonic-gate rtc->rtc_dow = 0;
9650Sstevel@tonic-gate rtc->rtc_hrs = p1275_cell2int(ci[9]);
9660Sstevel@tonic-gate rtc->rtc_min = p1275_cell2int(ci[10]);
9670Sstevel@tonic-gate rtc->rtc_sec = p1275_cell2int(ci[11]);
9680Sstevel@tonic-gate if (year >= 2000)
9690Sstevel@tonic-gate year -= 2000;
9700Sstevel@tonic-gate else
9710Sstevel@tonic-gate year -= 1900;
9720Sstevel@tonic-gate rtc->rtc_year = year;
9730Sstevel@tonic-gate
9740Sstevel@tonic-gate return (DDI_SUCCESS);
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate
9770Sstevel@tonic-gate /*
9780Sstevel@tonic-gate * Read the date using "set-time" method in rtc node
9790Sstevel@tonic-gate * For values 00 - 68, write 2000-2068, and for 69-99,
9800Sstevel@tonic-gate * write 1969-1999
9810Sstevel@tonic-gate */
9820Sstevel@tonic-gate static int
todds1337_prom_setdate(struct rtc_t * rtc)9830Sstevel@tonic-gate todds1337_prom_setdate(struct rtc_t *rtc)
9840Sstevel@tonic-gate {
9850Sstevel@tonic-gate int year;
9860Sstevel@tonic-gate cell_t ci[12];
9870Sstevel@tonic-gate
9880Sstevel@tonic-gate year = rtc->rtc_year;
9890Sstevel@tonic-gate
9900Sstevel@tonic-gate if ((year < 0) || (year > 99))
9910Sstevel@tonic-gate return (DDI_FAILURE);
9920Sstevel@tonic-gate
9930Sstevel@tonic-gate if (year <= 68)
9940Sstevel@tonic-gate year = rtc->rtc_year + 2000;
9950Sstevel@tonic-gate else
9960Sstevel@tonic-gate year = rtc->rtc_year + 1900;
9970Sstevel@tonic-gate
9980Sstevel@tonic-gate ci[0] = p1275_ptr2cell("call-method"); /* Service name */
9990Sstevel@tonic-gate ci[1] = 8; /* # of arguments */
10000Sstevel@tonic-gate ci[2] = 0; /* # of result cells */
10010Sstevel@tonic-gate ci[3] = p1275_ptr2cell("set-time");
10020Sstevel@tonic-gate ci[4] = p1275_ihandle2cell(todds1337_ihandle);
10030Sstevel@tonic-gate ci[5] = p1275_int2cell(year);
10040Sstevel@tonic-gate ci[6] = p1275_int2cell(rtc->rtc_mon);
10050Sstevel@tonic-gate ci[7] = p1275_int2cell(rtc->rtc_dom);
10060Sstevel@tonic-gate ci[8] = p1275_int2cell(rtc->rtc_hrs);
10070Sstevel@tonic-gate ci[9] = p1275_int2cell(rtc->rtc_min);
10080Sstevel@tonic-gate ci[10] = p1275_int2cell(rtc->rtc_sec);
10090Sstevel@tonic-gate
10100Sstevel@tonic-gate promif_preprom();
10110Sstevel@tonic-gate (void) p1275_cif_handler(&ci);
10120Sstevel@tonic-gate promif_postprom();
10130Sstevel@tonic-gate
10140Sstevel@tonic-gate return (DDI_SUCCESS);
10150Sstevel@tonic-gate }
1016