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