xref: /titanic_51/usr/src/uts/sun4u/io/todds1337.c (revision 193974072f41a843678abf5f61979c748687e66b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
570e456aaSrameshc  * Common Development and Distribution License (the "License").
670e456aaSrameshc  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*19397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/conf.h>
297c478bd9Sstevel@tonic-gate #include <sys/devops.h>
307c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
317c478bd9Sstevel@tonic-gate #include <sys/open.h>
327c478bd9Sstevel@tonic-gate #include <sys/file.h>
337c478bd9Sstevel@tonic-gate #include <sys/note.h>
347c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
357c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/clock.h>
407c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
417c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
427c478bd9Sstevel@tonic-gate #include <sys/poll.h>
437c478bd9Sstevel@tonic-gate #include <sys/pbio.h>
447c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /* Added for prom interface */
477c478bd9Sstevel@tonic-gate #include <sys/promif.h>
487c478bd9Sstevel@tonic-gate #include <sys/promimpl.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <sys/i2c/misc/i2c_svc.h>
517c478bd9Sstevel@tonic-gate #include <sys/todds1337.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	DS1337_DEVICE_TYPE	"rtc"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Driver entry routines
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate static int todds1337_attach(dev_info_t *, ddi_attach_cmd_t);
597c478bd9Sstevel@tonic-gate static int todds1337_detach(dev_info_t *, ddi_detach_cmd_t);
607c478bd9Sstevel@tonic-gate static int todds1337_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * tod_ops entry routines
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate static timestruc_t	todds1337_get(void);
667c478bd9Sstevel@tonic-gate static void		todds1337_set(timestruc_t);
677c478bd9Sstevel@tonic-gate static uint_t		todds1337_set_watchdog_timer(uint_t);
687c478bd9Sstevel@tonic-gate static uint_t		todds1337_clear_watchdog_timer(void);
697c478bd9Sstevel@tonic-gate static void		todds1337_set_power_alarm(timestruc_t);
707c478bd9Sstevel@tonic-gate static void		todds1337_clear_power_alarm(void);
717c478bd9Sstevel@tonic-gate static int		todds1337_setup_prom();
727c478bd9Sstevel@tonic-gate static void		todds1337_rele_prom();
737c478bd9Sstevel@tonic-gate static int		todds1337_prom_getdate(struct rtc_t *rtc);
747c478bd9Sstevel@tonic-gate static int		todds1337_prom_setdate(struct rtc_t *rtc);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * Local functions
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate static int		todds1337_read_rtc(struct rtc_t *);
807c478bd9Sstevel@tonic-gate static int		todds1337_write_rtc(struct rtc_t *);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /* Anchor for soft state structure */
837c478bd9Sstevel@tonic-gate static void	*ds1337_statep;
847c478bd9Sstevel@tonic-gate static int	instance = -1;
857c478bd9Sstevel@tonic-gate static int	todds1337_attach_done = 0;
867c478bd9Sstevel@tonic-gate static kmutex_t	todds1337_rd_lock;
877c478bd9Sstevel@tonic-gate static kmutex_t	todds1337_alarm_lock;
887c478bd9Sstevel@tonic-gate static ihandle_t todds1337_ihandle = 0;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* one second time out */
917c478bd9Sstevel@tonic-gate #define	I2C_CYCLIC_TIMEOUT	1000000000
927c478bd9Sstevel@tonic-gate uint_t i2c_cyclic_timeout = I2C_CYCLIC_TIMEOUT;
937c478bd9Sstevel@tonic-gate static int sync_clock_once = 1;
947c478bd9Sstevel@tonic-gate static 	struct	rtc_t	 soft_rtc;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * cp_ops structure
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate static struct cb_ops ds1337_cbops = {
1007c478bd9Sstevel@tonic-gate 	nodev,				/* open */
1017c478bd9Sstevel@tonic-gate 	nodev,				/* close */
1027c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
1037c478bd9Sstevel@tonic-gate 	nodev,				/* print */
1047c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
1057c478bd9Sstevel@tonic-gate 	nodev,				/* read */
1067c478bd9Sstevel@tonic-gate 	nodev,				/* write */
1077c478bd9Sstevel@tonic-gate 	nodev,				/* ioctl */
1087c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
1097c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
1107c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
1117c478bd9Sstevel@tonic-gate 	NULL,				/* poll */
1127c478bd9Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
1137c478bd9Sstevel@tonic-gate 	NULL,				/* streamtab */
1147c478bd9Sstevel@tonic-gate 	D_NEW | D_MP,			/* Driver compatibility flag */
1157c478bd9Sstevel@tonic-gate 	CB_REV,				/* rev */
1167c478bd9Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
1177c478bd9Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
1187c478bd9Sstevel@tonic-gate };
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * dev_ops structure
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate static struct dev_ops ds1337_ops = {
1247c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1257c478bd9Sstevel@tonic-gate 	0,			/* refcnt - reference cnt always set to 0 */
1267c478bd9Sstevel@tonic-gate 	todds1337_getinfo,	/* getinfo - Maybe requred */
1277c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
1287c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
1297c478bd9Sstevel@tonic-gate 	todds1337_attach,	/* attach */
1307c478bd9Sstevel@tonic-gate 	todds1337_detach,	/* detach */
1317c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
1327c478bd9Sstevel@tonic-gate 	&ds1337_cbops,		/* cb_ops - ds1337 does not need this(?) */
133*19397407SSherry Moore 	NULL,			/* bus_ops */
134*19397407SSherry Moore 	NULL,			/* power */
135*19397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
1367c478bd9Sstevel@tonic-gate };
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static struct modldrv todds1337_modldrv = {
1397c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* Type of module. This one is a driver */
140*19397407SSherry Moore 	"tod driver for DS1337 1.12",   /* Name of the module. */
1417c478bd9Sstevel@tonic-gate 	&ds1337_ops,			/* Pointer to dev_ops */
1427c478bd9Sstevel@tonic-gate };
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * Module linkage structure
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate static struct modlinkage todds1337_modlinkage = {
1487c478bd9Sstevel@tonic-gate 	MODREV_1,
1497c478bd9Sstevel@tonic-gate 	&todds1337_modldrv,
1507c478bd9Sstevel@tonic-gate 	0
1517c478bd9Sstevel@tonic-gate };
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate int
1547c478bd9Sstevel@tonic-gate _init(void)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	int error;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (strcmp(tod_module_name, "todds1337") == 0) {
1597c478bd9Sstevel@tonic-gate 		if ((error = ddi_soft_state_init(&ds1337_statep,
1607c478bd9Sstevel@tonic-gate 		    sizeof (ds1337_state_t), 0)) != DDI_SUCCESS) {
1617c478bd9Sstevel@tonic-gate 			return (error);
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		tod_ops.tod_get = todds1337_get;
1657c478bd9Sstevel@tonic-gate 		tod_ops.tod_set = todds1337_set;
1667c478bd9Sstevel@tonic-gate 		tod_ops.tod_set_watchdog_timer = todds1337_set_watchdog_timer;
1677c478bd9Sstevel@tonic-gate 		tod_ops.tod_clear_watchdog_timer =
1687c478bd9Sstevel@tonic-gate 		    todds1337_clear_watchdog_timer;
1697c478bd9Sstevel@tonic-gate 		tod_ops.tod_set_power_alarm = todds1337_set_power_alarm;
1707c478bd9Sstevel@tonic-gate 		tod_ops.tod_clear_power_alarm = todds1337_clear_power_alarm;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	(void) todds1337_setup_prom();
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/*
1767c478bd9Sstevel@tonic-gate 	 * Install the module
1777c478bd9Sstevel@tonic-gate 	 */
1787c478bd9Sstevel@tonic-gate 	if ((error = mod_install(&todds1337_modlinkage)) != 0) {
17970e456aaSrameshc 		if (strcmp(tod_module_name, "todds1337") == 0) {
1807c478bd9Sstevel@tonic-gate 			ddi_soft_state_fini(&ds1337_statep);
18170e456aaSrameshc 		}
18270e456aaSrameshc 		todds1337_rele_prom();
1837c478bd9Sstevel@tonic-gate 		return (error);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 	mutex_init(&todds1337_rd_lock, NULL, MUTEX_DEFAULT, NULL);
1867c478bd9Sstevel@tonic-gate 	mutex_init(&todds1337_alarm_lock, NULL, MUTEX_DEFAULT, NULL);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	return (0);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate int
1927c478bd9Sstevel@tonic-gate _fini(void)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	int error = 0;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (strcmp(tod_module_name, "todds1337") == 0) {
1977c478bd9Sstevel@tonic-gate 		error = EBUSY;
1987c478bd9Sstevel@tonic-gate 	} else {
1997c478bd9Sstevel@tonic-gate 		if ((error = mod_remove(&todds1337_modlinkage)) == 0) {
2007c478bd9Sstevel@tonic-gate 			mutex_destroy(&todds1337_rd_lock);
2017c478bd9Sstevel@tonic-gate 			mutex_destroy(&todds1337_alarm_lock);
2027c478bd9Sstevel@tonic-gate 			todds1337_rele_prom();
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	return (error);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate int
2107c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	return (mod_info(&todds1337_modlinkage, modinfop));
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * cyclical call to get tod.
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate static void
2197c478bd9Sstevel@tonic-gate todds1337_cyclic(void *arg)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	(void) todds1337_read_rtc((struct rtc_t *)arg);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * register ds1337 client device with i2c services, and
2287c478bd9Sstevel@tonic-gate  * allocate & initialize soft state structure.
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate static int
2317c478bd9Sstevel@tonic-gate todds1337_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	static ds1337_state_t	*statep = NULL;
2347c478bd9Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
2357c478bd9Sstevel@tonic-gate 	uint8_t tempVal = (uint8_t)0;
2367c478bd9Sstevel@tonic-gate 	switch (cmd) {
2377c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
2387c478bd9Sstevel@tonic-gate 		break;
2397c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
2407c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2417c478bd9Sstevel@tonic-gate 	default:
2427c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (instance != -1) {
2467c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_attach: wrong instance");
2477c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * Allocate soft state structure
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(ds1337_statep, instance) != DDI_SUCCESS) {
2567c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_attach: cannot allocate soft "
2577c478bd9Sstevel@tonic-gate 		    "state");
2587c478bd9Sstevel@tonic-gate 		instance = -1;
2597c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1337_statep, instance);
2637c478bd9Sstevel@tonic-gate 	if (statep == NULL) {
2647c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_attach: cannot acquire soft "
2657c478bd9Sstevel@tonic-gate 		    "state");
2667c478bd9Sstevel@tonic-gate 		instance = -1;
2677c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	statep->dip = dip;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (i2c_client_register(dip, &statep->ds1337_i2c_hdl) != I2C_SUCCESS) {
2737c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(ds1337_statep, instance);
2747c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_attach: cannot register i2c "
2757c478bd9Sstevel@tonic-gate 		    "client");
2767c478bd9Sstevel@tonic-gate 		instance = -1;
2777c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	/* check and initialize the oscillator */
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
2837c478bd9Sstevel@tonic-gate 	    &i2c_tp, 1, 1, I2C_SLEEP);
2847c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
2857c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR_RD;
2867c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */
2877c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wlen = 1;
2887c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_rlen = 1;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
2917c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
2927c478bd9Sstevel@tonic-gate 		i2c_client_unregister(statep->ds1337_i2c_hdl);
2937c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(ds1337_statep, instance);
2947c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_attach: failed to read DS1337 "
2957c478bd9Sstevel@tonic-gate 		    "status register");
2967c478bd9Sstevel@tonic-gate 		instance = -1;
2977c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	tempVal = i2c_tp->i2c_rbuf[0];
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * Check Oscillator and initialize chip if OBP failed to do it
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (tempVal & RTC_CTL_EOSC) {
3097c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp,
3107c478bd9Sstevel@tonic-gate 		    2, 0, I2C_SLEEP);
3117c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_version = I2C_XFER_REV;
3127c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_flags = I2C_WR;
3137c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[0] = RTC_CTL; /* Write Control register */
3147c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[1] = (uchar_t)(RTC_CTL_RS2 | RTC_CTL_RS1 |
3157c478bd9Sstevel@tonic-gate 		    RTC_CTL_INTCN);
3167c478bd9Sstevel@tonic-gate 		if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp))
3177c478bd9Sstevel@tonic-gate 		    != I2C_SUCCESS) {
3187c478bd9Sstevel@tonic-gate 			(void) i2c_transfer_free(statep->ds1337_i2c_hdl,
3197c478bd9Sstevel@tonic-gate 			    i2c_tp);
3207c478bd9Sstevel@tonic-gate 			i2c_client_unregister(statep->ds1337_i2c_hdl);
3217c478bd9Sstevel@tonic-gate 			ddi_soft_state_free(ds1337_statep, instance);
3227c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "todds1337_attach: failed to write "
3237c478bd9Sstevel@tonic-gate 			    "DS1337 control register");
3247c478bd9Sstevel@tonic-gate 			instance = -1;
3257c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 		/*
3317c478bd9Sstevel@tonic-gate 		 * Now reset the OSF flag in the Status register
3327c478bd9Sstevel@tonic-gate 		 */
3337c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp,
3347c478bd9Sstevel@tonic-gate 		    2, 0, I2C_SLEEP);
3357c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_version = I2C_XFER_REV;
3367c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_flags = I2C_WR;
3377c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[0] = RTC_STATUS;
3387c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[1] = (uchar_t)0;
3397c478bd9Sstevel@tonic-gate 		if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp))
3407c478bd9Sstevel@tonic-gate 		    != I2C_SUCCESS) {
3417c478bd9Sstevel@tonic-gate 			(void) i2c_transfer_free(statep->ds1337_i2c_hdl,
3427c478bd9Sstevel@tonic-gate 			    i2c_tp);
3437c478bd9Sstevel@tonic-gate 			i2c_client_unregister(statep->ds1337_i2c_hdl);
3447c478bd9Sstevel@tonic-gate 			ddi_soft_state_free(ds1337_statep, instance);
3457c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "todds1337_attach: failed to write "
3467c478bd9Sstevel@tonic-gate 			    "DS1337 status register");
3477c478bd9Sstevel@tonic-gate 			instance = -1;
3487c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
354dd4eeefdSeota 	/*
355dd4eeefdSeota 	 * Create a periodical handler to read TOD.
356dd4eeefdSeota 	 */
357dd4eeefdSeota 	ASSERT(statep->cycid == NULL);
358dd4eeefdSeota 	statep->cycid = ddi_periodic_add(todds1337_cyclic, &soft_rtc,
359dd4eeefdSeota 	    i2c_cyclic_timeout, DDI_IPL_1);
3607c478bd9Sstevel@tonic-gate 	statep->state = TOD_ATTACHED;
3617c478bd9Sstevel@tonic-gate 	todds1337_attach_done = 1;
3627c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3687c478bd9Sstevel@tonic-gate static int
3697c478bd9Sstevel@tonic-gate todds1337_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	/*
3727c478bd9Sstevel@tonic-gate 	 * Once attached, do not allow detach because the system constantly
3737c478bd9Sstevel@tonic-gate 	 * calling todds1337_get() to get the time.  If the driver is detached
3747c478bd9Sstevel@tonic-gate 	 * and the system try to get the time, the system will have memory
3757c478bd9Sstevel@tonic-gate 	 * problem.
3767c478bd9Sstevel@tonic-gate 	 *
3777c478bd9Sstevel@tonic-gate 	 */
3787c478bd9Sstevel@tonic-gate 	switch (cmd) {
3797c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
3807c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3817c478bd9Sstevel@tonic-gate 	default:
3827c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate /* *********************** tod_ops entry points ******************** */
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * Read the current time from the DS1337 chip and convert to UNIX form.
3907c478bd9Sstevel@tonic-gate  * Should be called with tod_clock held.
3917c478bd9Sstevel@tonic-gate  */
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate static timestruc_t
3947c478bd9Sstevel@tonic-gate todds1337_get(void)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	timestruc_t	ts;
3977c478bd9Sstevel@tonic-gate 	todinfo_t	tod;
3987c478bd9Sstevel@tonic-gate 	struct	rtc_t	rtc;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (sync_clock_once) {
4037c478bd9Sstevel@tonic-gate 		(void) todds1337_read_rtc(&soft_rtc);
4047c478bd9Sstevel@tonic-gate 		sync_clock_once = 0;
4057c478bd9Sstevel@tonic-gate 	} else {
4067c478bd9Sstevel@tonic-gate 		tod_fault_reset();
4077c478bd9Sstevel@tonic-gate 		return (hrestime);
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	bcopy(&soft_rtc, &rtc, sizeof (rtc));
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	/*
4137c478bd9Sstevel@tonic-gate 	 * 00 - 68 = 2000 thru 2068
4147c478bd9Sstevel@tonic-gate 	 * 69-99 = 1969 thru 1999
4157c478bd9Sstevel@tonic-gate 	 */
4167c478bd9Sstevel@tonic-gate 	tod.tod_year    = rtc.rtc_year;
4177c478bd9Sstevel@tonic-gate 	if (rtc.rtc_year <= 68)
4187c478bd9Sstevel@tonic-gate 		tod.tod_year += 100;
4197c478bd9Sstevel@tonic-gate 	tod.tod_month	= rtc.rtc_mon;
4207c478bd9Sstevel@tonic-gate 	tod.tod_day	= rtc.rtc_dom;
4217c478bd9Sstevel@tonic-gate 	tod.tod_dow	= rtc.rtc_dow;
4227c478bd9Sstevel@tonic-gate 	tod.tod_hour	= rtc.rtc_hrs;
4237c478bd9Sstevel@tonic-gate 	tod.tod_min	= rtc.rtc_min;
4247c478bd9Sstevel@tonic-gate 	tod.tod_sec	= rtc.rtc_sec;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	ts.tv_sec = tod_to_utc(tod);
4277c478bd9Sstevel@tonic-gate 	ts.tv_nsec = 0;
4287c478bd9Sstevel@tonic-gate 	return (ts);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate /*
4327c478bd9Sstevel@tonic-gate  * Program DS1337 with the specified time.
4337c478bd9Sstevel@tonic-gate  * Must be called with tod_lock held. The TOD
4347c478bd9Sstevel@tonic-gate  * chip supports date from 1969-2068 only. We must
4357c478bd9Sstevel@tonic-gate  * reject requests to set date below 1969.
4367c478bd9Sstevel@tonic-gate  */
4377c478bd9Sstevel@tonic-gate static void
4387c478bd9Sstevel@tonic-gate todds1337_set(timestruc_t ts)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	struct rtc_t	rtc;
4417c478bd9Sstevel@tonic-gate 	todinfo_t	tod = utc_to_tod(ts.tv_sec);
4427c478bd9Sstevel@tonic-gate 	int		year;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	/*
4487c478bd9Sstevel@tonic-gate 	 * Year is base 1900, valid year range 1969-2068
4497c478bd9Sstevel@tonic-gate 	 */
4507c478bd9Sstevel@tonic-gate 	if ((tod.tod_year < 69) || (tod.tod_year > 168))
4517c478bd9Sstevel@tonic-gate 		return;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	year = tod.tod_year;
4547c478bd9Sstevel@tonic-gate 	if (year >= 100)
4557c478bd9Sstevel@tonic-gate 		year -= 100;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	rtc.rtc_year	= (uint8_t)year;
4587c478bd9Sstevel@tonic-gate 	rtc.rtc_mon	= (uint8_t)tod.tod_month;
4597c478bd9Sstevel@tonic-gate 	rtc.rtc_dom	= (uint8_t)tod.tod_day;
4607c478bd9Sstevel@tonic-gate 	rtc.rtc_dow	= (uint8_t)tod.tod_dow;
4617c478bd9Sstevel@tonic-gate 	rtc.rtc_hrs	= (uint8_t)tod.tod_hour;
4627c478bd9Sstevel@tonic-gate 	rtc.rtc_min	= (uint8_t)tod.tod_min;
4637c478bd9Sstevel@tonic-gate 	rtc.rtc_sec	= (uint8_t)tod.tod_sec;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	(void) todds1337_write_rtc(&rtc);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  * Program ds1337 registers for alarm to go off at the specified time.
4707c478bd9Sstevel@tonic-gate  * Alarm #1 is used (Alarm #2 stays idle)
4717c478bd9Sstevel@tonic-gate  */
4727c478bd9Sstevel@tonic-gate /* ARGSUSED */
4737c478bd9Sstevel@tonic-gate static void
4747c478bd9Sstevel@tonic-gate todds1337_set_power_alarm(timestruc_t ts)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	todinfo_t	tod;
4777c478bd9Sstevel@tonic-gate 	ds1337_state_t	*statep = NULL;
4787c478bd9Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
4797c478bd9Sstevel@tonic-gate 	uint8_t tmpval;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (!todds1337_attach_done) {
4847c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337: driver not attached");
4857c478bd9Sstevel@tonic-gate 		return;
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1337_statep, instance);
4897c478bd9Sstevel@tonic-gate 	if (statep == NULL) {
4907c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failed");
4917c478bd9Sstevel@tonic-gate 		return;
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	tod = utc_to_tod(ts.tv_sec);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/*
4977c478bd9Sstevel@tonic-gate 	 * i2c_transfe() may block; to avoid locking clock() which
4987c478bd9Sstevel@tonic-gate 	 * is running in interrupt context at PIL10 we temporarely exit
4997c478bd9Sstevel@tonic-gate 	 * the tod_mutex and enter alarm lock. Time/date and alarm hardware
5007c478bd9Sstevel@tonic-gate 	 * have non-interlsecting registers, it is safe to use different locks.
5017c478bd9Sstevel@tonic-gate 	 */
5027c478bd9Sstevel@tonic-gate 	mutex_exit(&tod_lock);
5037c478bd9Sstevel@tonic-gate 	mutex_enter(&todds1337_alarm_lock);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	/*
5067c478bd9Sstevel@tonic-gate 	 * Disable Power Alarm (A1IE)
5077c478bd9Sstevel@tonic-gate 	 */
5087c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
5097c478bd9Sstevel@tonic-gate 	    &i2c_tp, 1, 1, I2C_SLEEP);
5107c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
5117c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR_RD;
5127c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
5157c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5167c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to read "
5177c478bd9Sstevel@tonic-gate 		    "DS1337 control register");
5187c478bd9Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
5197c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
5207c478bd9Sstevel@tonic-gate 		return;
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	tmpval = i2c_tp->i2c_rbuf[0];
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
5287c478bd9Sstevel@tonic-gate 	    &i2c_tp, 2, 0, I2C_SLEEP);
5297c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
5307c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
5317c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */
5327c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
5357c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5367c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write "
5377c478bd9Sstevel@tonic-gate 		    "DS1337control register");
5387c478bd9Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
5397c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
5407c478bd9Sstevel@tonic-gate 		return;
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	/*
5477c478bd9Sstevel@tonic-gate 	 * Write Alarm #1 registers
5487c478bd9Sstevel@tonic-gate 	 */
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
5517c478bd9Sstevel@tonic-gate 	    &i2c_tp, 5, 0, I2C_SLEEP);
5527c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
5537c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
5547c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_ALARM_SEC; /* Alarm #1 Seconds */
5557c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(tod.tod_sec);
5567c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(tod.tod_min);
5577c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(tod.tod_hour);
5587c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(tod.tod_day);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
5617c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5627c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write "
5637c478bd9Sstevel@tonic-gate 		    "DS1337 alarm registers");
5647c478bd9Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
5657c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
5667c478bd9Sstevel@tonic-gate 		return;
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/*
5737c478bd9Sstevel@tonic-gate 	 * Enable Power Alarm
5747c478bd9Sstevel@tonic-gate 	 */
5757c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
5767c478bd9Sstevel@tonic-gate 	    &i2c_tp, 2, 0, I2C_SLEEP);
5777c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
5787c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
5797c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */
5807c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = tmpval | RTC_CTL_A1IE;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
5837c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5847c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to enable "
5857c478bd9Sstevel@tonic-gate 		    "DS1337 alarm");
5867c478bd9Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
5877c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
5887c478bd9Sstevel@tonic-gate 		return;
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	mutex_exit(&todds1337_alarm_lock);
5947c478bd9Sstevel@tonic-gate 	mutex_enter(&tod_lock);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate /* ARGSUSED */
5987c478bd9Sstevel@tonic-gate static void
5997c478bd9Sstevel@tonic-gate todds1337_clear_power_alarm(void)
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate 	ds1337_state_t	*statep = NULL;
6027c478bd9Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
6037c478bd9Sstevel@tonic-gate 	uint8_t tmpval;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if (!todds1337_attach_done) {
6087c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337: driver was not attached");
6097c478bd9Sstevel@tonic-gate 		return;
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1337_statep, instance);
6137c478bd9Sstevel@tonic-gate 	if (statep == NULL) {
6147c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337: ddi_get_soft_state has failed");
6157c478bd9Sstevel@tonic-gate 		return;
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	/*
6197c478bd9Sstevel@tonic-gate 	 * i2c_transfe() may block; to avoid locking clock() which
6207c478bd9Sstevel@tonic-gate 	 * is running in interrupt context at PIL10 we temporarely exit
6217c478bd9Sstevel@tonic-gate 	 * the tod_mutex and enter alarm lock. Time/date and alarm hardware
6227c478bd9Sstevel@tonic-gate 	 * have non-interlsecting registers, it is safe to use different locks.
6237c478bd9Sstevel@tonic-gate 	 */
6247c478bd9Sstevel@tonic-gate 	mutex_exit(&tod_lock);
6257c478bd9Sstevel@tonic-gate 	mutex_enter(&todds1337_alarm_lock);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	/*
6287c478bd9Sstevel@tonic-gate 	 * Disable Alarm #1 Interrupt
6297c478bd9Sstevel@tonic-gate 	 */
6307c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
6317c478bd9Sstevel@tonic-gate 	    &i2c_tp, 1, 1, I2C_SLEEP);
6327c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
6337c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR_RD;
6347c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
6377c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
6387c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read "
6397c478bd9Sstevel@tonic-gate 		    "DS1337 control register");
6407c478bd9Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
6417c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
6427c478bd9Sstevel@tonic-gate 		return;
6437c478bd9Sstevel@tonic-gate 	}
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	tmpval = i2c_tp->i2c_rbuf[0];
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
6507c478bd9Sstevel@tonic-gate 	    &i2c_tp, 2, 0, I2C_SLEEP);
6517c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
6527c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
6537c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */
6547c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
6577c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
6587c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write "
6597c478bd9Sstevel@tonic-gate 		    "DS1337 control register");
6607c478bd9Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
6617c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
6627c478bd9Sstevel@tonic-gate 		return;
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	/*
6687c478bd9Sstevel@tonic-gate 	 * Reset Alarm #1 Flag
6697c478bd9Sstevel@tonic-gate 	 */
6707c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
6717c478bd9Sstevel@tonic-gate 	    &i2c_tp, 1, 1, I2C_SLEEP);
6727c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
6737c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR_RD;
6747c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
6777c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
6787c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read "
6797c478bd9Sstevel@tonic-gate 		    "DS1337 status register");
6807c478bd9Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
6817c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
6827c478bd9Sstevel@tonic-gate 		return;
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	tmpval = i2c_tp->i2c_rbuf[0];
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
6907c478bd9Sstevel@tonic-gate 	    &i2c_tp, 2, 0, I2C_SLEEP);
6917c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
6927c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
6937c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Write Status register */
6947c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_STATUS_A1F;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
6977c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
6987c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write "
6997c478bd9Sstevel@tonic-gate 		    "DS1337 status register");
7007c478bd9Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
7017c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
7027c478bd9Sstevel@tonic-gate 		return;
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	mutex_exit(&todds1337_alarm_lock);
7087c478bd9Sstevel@tonic-gate 	mutex_enter(&tod_lock);
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate /* ARGSUSED */
7127c478bd9Sstevel@tonic-gate static uint_t
7137c478bd9Sstevel@tonic-gate todds1337_set_watchdog_timer(uint_t timeoutval)
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
7167c478bd9Sstevel@tonic-gate 	return (0);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate /* ARGSUSED */
7207c478bd9Sstevel@tonic-gate static uint_t
7217c478bd9Sstevel@tonic-gate todds1337_clear_watchdog_timer(void)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
7247c478bd9Sstevel@tonic-gate 	return (0);
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate /* ********************** Local functions ***************************** */
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate static char tod_read[7] = {-1, -1, -1, -1, -1, -1, -1};
7307c478bd9Sstevel@tonic-gate static int
7317c478bd9Sstevel@tonic-gate todds1337_read_rtc(struct rtc_t *rtc)
7327c478bd9Sstevel@tonic-gate {
7337c478bd9Sstevel@tonic-gate 	static	ds1337_state_t	*statep = NULL;
7347c478bd9Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
7357c478bd9Sstevel@tonic-gate 	int i2c_cmd_status = I2C_FAILURE;
7367c478bd9Sstevel@tonic-gate 	int counter = 4;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	if (!todds1337_attach_done) {
7397c478bd9Sstevel@tonic-gate 		return (todds1337_prom_getdate(rtc));
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1337_statep, instance);
7437c478bd9Sstevel@tonic-gate 	if (statep == NULL) {
7447c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failing");
7457c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	mutex_enter(&todds1337_rd_lock);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	/*
7517c478bd9Sstevel@tonic-gate 	 * Allocate 1 byte for write buffer and 7 bytes for read buffer to
7527c478bd9Sstevel@tonic-gate 	 * to accomodate sec, min, hrs, dayOfWeek, dayOfMonth, year
7537c478bd9Sstevel@tonic-gate 	 */
7547c478bd9Sstevel@tonic-gate 	if ((i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp, 1,
7557c478bd9Sstevel@tonic-gate 	    7, I2C_SLEEP)) != I2C_SUCCESS) {
7567c478bd9Sstevel@tonic-gate 		mutex_exit(&todds1337_rd_lock);
7577c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	do {
7617c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_version = I2C_XFER_REV;
7627c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_flags = I2C_WR_RD;
7637c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC; /* Start from 0x00 */
7647c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wlen = 1;	/* Write one byte address */
7657c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_rlen = 7;	/* Read 7 regs */
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 		if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl,
7687c478bd9Sstevel@tonic-gate 		    i2c_tp)) != I2C_SUCCESS) {
7697c478bd9Sstevel@tonic-gate 			goto done;
7707c478bd9Sstevel@tonic-gate 		}
7717c478bd9Sstevel@tonic-gate 		/* for first read, need to get valid data */
7727c478bd9Sstevel@tonic-gate 		while (tod_read[0] == -1 && counter > 0) {
7737c478bd9Sstevel@tonic-gate 		/* move data to static buffer */
7747c478bd9Sstevel@tonic-gate 		bcopy(i2c_tp->i2c_rbuf, tod_read, 7);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		/* now read again */
7777c478bd9Sstevel@tonic-gate 		/* Start reading reg from 0x00 */
7787c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[0] = (uchar_t)0x00;
7797c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wlen = 1;	/* Write one byte address */
7807c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_rlen = 7;	/* Read 7 regs */
7817c478bd9Sstevel@tonic-gate 		if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl,
7827c478bd9Sstevel@tonic-gate 		    i2c_tp)) != I2C_SUCCESS) {
7837c478bd9Sstevel@tonic-gate 			goto done;
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 		/* if they are not the same, then read again */
7867c478bd9Sstevel@tonic-gate 		if (bcmp(tod_read, i2c_tp->i2c_rbuf, 7) != 0) {
7877c478bd9Sstevel@tonic-gate 			tod_read[0] = -1;
7887c478bd9Sstevel@tonic-gate 			counter--;
7897c478bd9Sstevel@tonic-gate 		}
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	} while (i2c_tp->i2c_rbuf[0] == 0x59 &&
7937c478bd9Sstevel@tonic-gate 	    /* if seconds register is 0x59 (BCD), add data should match */
7947c478bd9Sstevel@tonic-gate 	    bcmp(&tod_read[1], &i2c_tp->i2c_rbuf[1], 6) != 0 &&
7957c478bd9Sstevel@tonic-gate 	    counter-- > 0);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if (counter < 0)
7987c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "i2ctod: TOD Chip failed ??");
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	/* move data to static buffer */
8017c478bd9Sstevel@tonic-gate 	bcopy(i2c_tp->i2c_rbuf, tod_read, 7);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	rtc->rtc_year	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[6]);
8057c478bd9Sstevel@tonic-gate 	rtc->rtc_mon	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[5]);
8067c478bd9Sstevel@tonic-gate 	rtc->rtc_dom	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[4]);
8077c478bd9Sstevel@tonic-gate 	rtc->rtc_dow	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[3]);
8087c478bd9Sstevel@tonic-gate 	rtc->rtc_hrs	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[2]);
8097c478bd9Sstevel@tonic-gate 	rtc->rtc_min	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[1]);
8107c478bd9Sstevel@tonic-gate 	rtc->rtc_sec	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[0]);
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate done:
8137c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	mutex_exit(&todds1337_rd_lock);
8167c478bd9Sstevel@tonic-gate 	return (i2c_cmd_status);
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate static int
8217c478bd9Sstevel@tonic-gate todds1337_write_rtc(struct rtc_t *rtc)
8227c478bd9Sstevel@tonic-gate {
8237c478bd9Sstevel@tonic-gate 	ds1337_state_t	*statep = NULL;
8247c478bd9Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
8257c478bd9Sstevel@tonic-gate 	int i2c_cmd_status = I2C_SUCCESS;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	if (!todds1337_attach_done) {
8297c478bd9Sstevel@tonic-gate 		return (todds1337_prom_setdate(rtc));
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1337_statep, instance);
8337c478bd9Sstevel@tonic-gate 	if (statep == NULL) {
8347c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8357c478bd9Sstevel@tonic-gate 	}
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	if ((i2c_cmd_status = i2c_transfer_alloc(statep->ds1337_i2c_hdl,
8387c478bd9Sstevel@tonic-gate 	    &i2c_tp, 8, 0, I2C_SLEEP)) != I2C_SUCCESS) {
8397c478bd9Sstevel@tonic-gate 		return (i2c_cmd_status);
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
8437c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
8447c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC;
8457c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(rtc->rtc_sec);
8467c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(rtc->rtc_min);
8477c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(rtc->rtc_hrs);
8487c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(rtc->rtc_dow);
8497c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[5] = BYTE_TO_BCD(rtc->rtc_dom);
8507c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[6] = BYTE_TO_BCD(rtc->rtc_mon);
8517c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[7] = BYTE_TO_BCD(rtc->rtc_year);
8527c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wlen = 8;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl,
8557c478bd9Sstevel@tonic-gate 	    i2c_tp)) != I2C_SUCCESS) {
8567c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
8577c478bd9Sstevel@tonic-gate 		return (i2c_cmd_status);
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	tod_read[0] = -1;  /* invalidate saved data from read routine */
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	return (i2c_cmd_status);
8657c478bd9Sstevel@tonic-gate }
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8697c478bd9Sstevel@tonic-gate static int
8707c478bd9Sstevel@tonic-gate todds1337_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
8717c478bd9Sstevel@tonic-gate     void **result)
8727c478bd9Sstevel@tonic-gate {
8737c478bd9Sstevel@tonic-gate 	ds1337_state_t *softsp;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	if (instance == -1) {
8767c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	switch (infocmd) {
8807c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
8817c478bd9Sstevel@tonic-gate 		if ((softsp = ddi_get_soft_state(ds1337_statep, instance)) ==
8827c478bd9Sstevel@tonic-gate 		    NULL)
8837c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
8847c478bd9Sstevel@tonic-gate 		*result = (void *)softsp->dip;
8857c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
8867c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
887b0b35aceSmike_s 		*result = (void *)(uintptr_t)instance;
8887c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
8897c478bd9Sstevel@tonic-gate 	default:
8907c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate /*
8957c478bd9Sstevel@tonic-gate  * Finds the device node with device_type "rtc" and opens it to
8967c478bd9Sstevel@tonic-gate  * execute the get-time method
8977c478bd9Sstevel@tonic-gate  */
8987c478bd9Sstevel@tonic-gate static int
8997c478bd9Sstevel@tonic-gate todds1337_setup_prom()
9007c478bd9Sstevel@tonic-gate {
901fa9e4066Sahrens 	pnode_t todnode;
9027c478bd9Sstevel@tonic-gate 	char tod1337_devpath[MAXNAMELEN];
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	if ((todnode = prom_findnode_bydevtype(prom_rootnode(),
9057c478bd9Sstevel@tonic-gate 	    DS1337_DEVICE_TYPE)) == OBP_NONODE)
9067c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/*
9097c478bd9Sstevel@tonic-gate 	 * We now have the phandle of the rtc node, we need to open the
9107c478bd9Sstevel@tonic-gate 	 * node and get the ihandle
9117c478bd9Sstevel@tonic-gate 	 */
9127c478bd9Sstevel@tonic-gate 	if (prom_phandle_to_path(todnode, tod1337_devpath,
9137c478bd9Sstevel@tonic-gate 	    sizeof (tod1337_devpath)) < 0) {
9147c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "prom_phandle_to_path failed");
9157c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9167c478bd9Sstevel@tonic-gate 	}
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	/*
9197c478bd9Sstevel@tonic-gate 	 * Now open the node and store it's ihandle
9207c478bd9Sstevel@tonic-gate 	 */
9217c478bd9Sstevel@tonic-gate 	if ((todds1337_ihandle = prom_open(tod1337_devpath)) == NULL) {
9227c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "prom_open failed");
9237c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate /*
9307c478bd9Sstevel@tonic-gate  * Closes the prom interface
9317c478bd9Sstevel@tonic-gate  */
9327c478bd9Sstevel@tonic-gate static void
9337c478bd9Sstevel@tonic-gate todds1337_rele_prom()
9347c478bd9Sstevel@tonic-gate {
9357c478bd9Sstevel@tonic-gate 	(void) prom_close(todds1337_ihandle);
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate /*
9397c478bd9Sstevel@tonic-gate  * Read the date using "get-time" method in rtc node
9407c478bd9Sstevel@tonic-gate  * PROM returns 1969-1999 when reading 69-99 and
9417c478bd9Sstevel@tonic-gate  * 2000-2068 when reading 00-68
9427c478bd9Sstevel@tonic-gate  */
9437c478bd9Sstevel@tonic-gate static int
9447c478bd9Sstevel@tonic-gate todds1337_prom_getdate(struct rtc_t *rtc)
9457c478bd9Sstevel@tonic-gate {
9467c478bd9Sstevel@tonic-gate 	int year;
9477c478bd9Sstevel@tonic-gate 	cell_t ci[12];
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");  /* Service name */
9507c478bd9Sstevel@tonic-gate 	ci[1] = 2; /* # of arguments */
9517c478bd9Sstevel@tonic-gate 	ci[2] = 7; /* # of result cells */
9527c478bd9Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("get-time");
9537c478bd9Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(todds1337_ihandle);
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	promif_preprom();
9567c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
9577c478bd9Sstevel@tonic-gate 	promif_postprom();
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	year 		= p1275_cell2int(ci[6]);
9607c478bd9Sstevel@tonic-gate 	rtc->rtc_mon	= p1275_cell2int(ci[7]);
9617c478bd9Sstevel@tonic-gate 	rtc->rtc_dom	= p1275_cell2int(ci[8]);
9627c478bd9Sstevel@tonic-gate 	rtc->rtc_dow	= 0;
9637c478bd9Sstevel@tonic-gate 	rtc->rtc_hrs	= p1275_cell2int(ci[9]);
9647c478bd9Sstevel@tonic-gate 	rtc->rtc_min	= p1275_cell2int(ci[10]);
9657c478bd9Sstevel@tonic-gate 	rtc->rtc_sec	= p1275_cell2int(ci[11]);
9667c478bd9Sstevel@tonic-gate 	if (year >= 2000)
9677c478bd9Sstevel@tonic-gate 		year -= 2000;
9687c478bd9Sstevel@tonic-gate 	else
9697c478bd9Sstevel@tonic-gate 		year -= 1900;
9707c478bd9Sstevel@tonic-gate 	rtc->rtc_year	= year;
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate /*
9767c478bd9Sstevel@tonic-gate  * Read the date using "set-time" method in rtc node
9777c478bd9Sstevel@tonic-gate  * For values 00 - 68, write 2000-2068, and for 69-99,
9787c478bd9Sstevel@tonic-gate  * write 1969-1999
9797c478bd9Sstevel@tonic-gate  */
9807c478bd9Sstevel@tonic-gate static int
9817c478bd9Sstevel@tonic-gate todds1337_prom_setdate(struct rtc_t *rtc)
9827c478bd9Sstevel@tonic-gate {
9837c478bd9Sstevel@tonic-gate 	int year;
9847c478bd9Sstevel@tonic-gate 	cell_t ci[12];
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	year = rtc->rtc_year;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	if ((year < 0) || (year > 99))
9897c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	if (year <= 68)
9927c478bd9Sstevel@tonic-gate 		year = rtc->rtc_year + 2000;
9937c478bd9Sstevel@tonic-gate 	else
9947c478bd9Sstevel@tonic-gate 		year = rtc->rtc_year + 1900;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");  /* Service name */
9977c478bd9Sstevel@tonic-gate 	ci[1] = 8; /* # of arguments */
9987c478bd9Sstevel@tonic-gate 	ci[2] = 0; /* # of result cells */
9997c478bd9Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("set-time");
10007c478bd9Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(todds1337_ihandle);
10017c478bd9Sstevel@tonic-gate 	ci[5] = p1275_int2cell(year);
10027c478bd9Sstevel@tonic-gate 	ci[6] = p1275_int2cell(rtc->rtc_mon);
10037c478bd9Sstevel@tonic-gate 	ci[7] = p1275_int2cell(rtc->rtc_dom);
10047c478bd9Sstevel@tonic-gate 	ci[8] = p1275_int2cell(rtc->rtc_hrs);
10057c478bd9Sstevel@tonic-gate 	ci[9] = p1275_int2cell(rtc->rtc_min);
10067c478bd9Sstevel@tonic-gate 	ci[10] = p1275_int2cell(rtc->rtc_sec);
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	promif_preprom();
10097c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
10107c478bd9Sstevel@tonic-gate 	promif_postprom();
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
10137c478bd9Sstevel@tonic-gate }
1014