xref: /titanic_51/usr/src/uts/sun4u/snowbird/io/todds1307/todds1307.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
5dd4eeefdSeota  * Common Development and Distribution License (the "License").
6dd4eeefdSeota  * 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 
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/todds1307.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	I2C_DELAY	20000
547c478bd9Sstevel@tonic-gate #define	DS1307_DEVICE_TYPE	"rtc"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * Driver enrty routines
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate static int todds1307_attach(dev_info_t *, ddi_attach_cmd_t);
607c478bd9Sstevel@tonic-gate static int todds1307_detach(dev_info_t *, ddi_detach_cmd_t);
617c478bd9Sstevel@tonic-gate static int todds1307_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * tod_ops entry routines
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate static timestruc_t	todds1307_get(void);
677c478bd9Sstevel@tonic-gate static void		todds1307_set(timestruc_t);
687c478bd9Sstevel@tonic-gate static uint_t		todds1307_set_watchdog_timer(uint_t);
697c478bd9Sstevel@tonic-gate static uint_t		todds1307_clear_watchdog_timer(void);
707c478bd9Sstevel@tonic-gate static void		todds1307_set_power_alarm(timestruc_t);
717c478bd9Sstevel@tonic-gate static void		todds1307_clear_power_alarm(void);
727c478bd9Sstevel@tonic-gate static int todds1307_setup_prom();
737c478bd9Sstevel@tonic-gate static void todds1307_rele_prom();
747c478bd9Sstevel@tonic-gate static int todds1307_prom_getdate(struct rtc_t *rtc);
757c478bd9Sstevel@tonic-gate static int todds1307_prom_setdate(struct rtc_t *rtc);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Local functions
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate static int	todds1307_read_rtc(struct rtc_t *);
817c478bd9Sstevel@tonic-gate static int	todds1307_write_rtc(struct rtc_t *);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /* Anchor for soft state structure */
847c478bd9Sstevel@tonic-gate static void	*ds1307_statep;
857c478bd9Sstevel@tonic-gate static int	instance = -1;
867c478bd9Sstevel@tonic-gate static int	todds1307_attach_done = 0;
877c478bd9Sstevel@tonic-gate static kmutex_t	todds1307_rd_lock;
887c478bd9Sstevel@tonic-gate static ihandle_t todds1307_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  * For debugging only
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate static unsigned char int2bcd(int num);
1007c478bd9Sstevel@tonic-gate static int bcd2int(unsigned char num);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * cp_ops structure
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate static struct cb_ops ds1307_cbops = {
1067c478bd9Sstevel@tonic-gate 	nodev,				/* open */
1077c478bd9Sstevel@tonic-gate 	nodev,				/* close */
1087c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
1097c478bd9Sstevel@tonic-gate 	nodev,				/* print */
1107c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
1117c478bd9Sstevel@tonic-gate 	nodev,				/* read */
1127c478bd9Sstevel@tonic-gate 	nodev,				/* write */
1137c478bd9Sstevel@tonic-gate 	nodev,				/* ioctl */
1147c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
1157c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
1167c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
1177c478bd9Sstevel@tonic-gate 	NULL,				/* poll */
1187c478bd9Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
1197c478bd9Sstevel@tonic-gate 	NULL,				/* streamtab */
1207c478bd9Sstevel@tonic-gate 	D_NEW | D_MP,			/* Driver compatibility flag */
1217c478bd9Sstevel@tonic-gate 	CB_REV,				/* rev */
1227c478bd9Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
1237c478bd9Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
1247c478bd9Sstevel@tonic-gate };
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * dev_ops structure
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate static struct dev_ops ds1307_ops = {
1307c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1317c478bd9Sstevel@tonic-gate 	0,			/* refcnt - reference cnt always set to 0 */
1327c478bd9Sstevel@tonic-gate 	todds1307_getinfo,	/* getinfo - Maybe requred */
1337c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
1347c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
1357c478bd9Sstevel@tonic-gate 	todds1307_attach,	/* attach */
1367c478bd9Sstevel@tonic-gate 	todds1307_detach,	/* detach */
1377c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
1387c478bd9Sstevel@tonic-gate 	&ds1307_cbops,		/* cb_ops - ds1307 does not need this(?) */
13919397407SSherry Moore 	NULL,			/* bus_ops */
14019397407SSherry Moore 	NULL,			/* power */
14119397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
1427c478bd9Sstevel@tonic-gate };
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static struct modldrv todds1307_modldrv = {
1457c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* Type of module. This one is a driver */
14619397407SSherry Moore 	"tod driver for DS1307 v1.12",	/* Name of the module */
1477c478bd9Sstevel@tonic-gate 	&ds1307_ops,			/* Pointer to dev_ops */
1487c478bd9Sstevel@tonic-gate };
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * Module linkage structure
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate static struct modlinkage todds1307_modlinkage = {
1547c478bd9Sstevel@tonic-gate 	MODREV_1,
1557c478bd9Sstevel@tonic-gate 	&todds1307_modldrv,
1567c478bd9Sstevel@tonic-gate 	0
1577c478bd9Sstevel@tonic-gate };
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate int
1607c478bd9Sstevel@tonic-gate _init(void)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	int error;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (strcmp(tod_module_name, "todds1307") == 0) {
1657c478bd9Sstevel@tonic-gate 		if ((error = ddi_soft_state_init(&ds1307_statep,
1667c478bd9Sstevel@tonic-gate 		    sizeof (ds1307_state_t), 0)) != DDI_SUCCESS) {
1677c478bd9Sstevel@tonic-gate 			return (error);
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 		tod_ops.tod_get = todds1307_get;
1717c478bd9Sstevel@tonic-gate 		tod_ops.tod_set = todds1307_set;
1727c478bd9Sstevel@tonic-gate 		tod_ops.tod_set_watchdog_timer = todds1307_set_watchdog_timer;
1737c478bd9Sstevel@tonic-gate 		tod_ops.tod_clear_watchdog_timer =
1747c478bd9Sstevel@tonic-gate 		    todds1307_clear_watchdog_timer;
1757c478bd9Sstevel@tonic-gate 		tod_ops.tod_set_power_alarm = todds1307_set_power_alarm;
1767c478bd9Sstevel@tonic-gate 		tod_ops.tod_clear_power_alarm = todds1307_clear_power_alarm;
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	(void) todds1307_setup_prom();
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * Install the module
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	if ((error = mod_install(&todds1307_modlinkage)) != 0) {
1857c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&ds1307_statep);
1867c478bd9Sstevel@tonic-gate 		return (error);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	mutex_init(&todds1307_rd_lock, NULL, MUTEX_DEFAULT, NULL);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	return (0);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate int
1947c478bd9Sstevel@tonic-gate _fini(void)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	int error = 0;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if (strcmp(tod_module_name, "todds1307") == 0) {
1997c478bd9Sstevel@tonic-gate 		error = EBUSY;
2007c478bd9Sstevel@tonic-gate 	} else {
2017c478bd9Sstevel@tonic-gate 		if ((error = mod_remove(&todds1307_modlinkage)) == 0) {
2027c478bd9Sstevel@tonic-gate 			ddi_soft_state_fini(&ds1307_statep);
2037c478bd9Sstevel@tonic-gate 			mutex_destroy(&todds1307_rd_lock);
2047c478bd9Sstevel@tonic-gate 			todds1307_rele_prom();
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	return (error);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate int
2127c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	return (mod_info(&todds1307_modlinkage, modinfop));
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * cyclical call to get tod.
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate static void
2217c478bd9Sstevel@tonic-gate todds1307_cyclic(void *arg)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 
22407d06da5SSurya Prakki 	(void) todds1307_read_rtc((struct rtc_t *)arg);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * register ds1307 client device with i2c services, and
2307c478bd9Sstevel@tonic-gate  * allocate & initialize soft state structure.
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate static int
2337c478bd9Sstevel@tonic-gate todds1307_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	static ds1307_state_t	*statep = NULL;
2367c478bd9Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
2377c478bd9Sstevel@tonic-gate 	uint8_t tempVal = (uint8_t)0;
2387c478bd9Sstevel@tonic-gate 	switch (cmd) {
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		case DDI_ATTACH:
2417c478bd9Sstevel@tonic-gate 			break;
2427c478bd9Sstevel@tonic-gate 		case DDI_RESUME:
2437c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
2447c478bd9Sstevel@tonic-gate 		default:
2457c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (instance != -1) {
2497c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * Allocate soft state structure
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(ds1307_statep, instance) != DDI_SUCCESS) {
2587c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1307_statep, instance);
2627c478bd9Sstevel@tonic-gate 	if (statep == NULL) {
2637c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	statep->dip = dip;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if (i2c_client_register(dip, &statep->ds1307_i2c_hdl) != I2C_SUCCESS) {
2697c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(ds1307_statep, instance);
2707c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(I2C_DELAY));
2717c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/* check and initialize the oscillator */
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1307_i2c_hdl,
2777c478bd9Sstevel@tonic-gate 	    &i2c_tp, 1, 1, I2C_SLEEP);
2787c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
2797c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR_RD;
2807c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)0x00; /* Read 00h */
2817c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wlen = 1;
2827c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_rlen = 1;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1307_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
2857c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
2867c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(ds1307_statep, instance);
2877c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(I2C_DELAY));
2887c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	tempVal = i2c_tp->i2c_rbuf[0];
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (tempVal & 0x80) {			 /* check Oscillator */
2967c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_alloc(statep->ds1307_i2c_hdl, &i2c_tp,
2977c478bd9Sstevel@tonic-gate 		    2, 1, I2C_SLEEP);
2987c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_version = I2C_XFER_REV;
2997c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_flags = I2C_WR;
3007c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[0] = 0x00;
3017c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[1] =
3027c478bd9Sstevel@tonic-gate 		    (uchar_t)(i2c_tp->i2c_rbuf[0]& 0x7f);
3037c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wlen = 2;
3047c478bd9Sstevel@tonic-gate 					/* Enable oscillator */
3057c478bd9Sstevel@tonic-gate 		if ((i2c_transfer(statep->ds1307_i2c_hdl, i2c_tp))
3067c478bd9Sstevel@tonic-gate 		    != I2C_SUCCESS) {
3077c478bd9Sstevel@tonic-gate 			(void) i2c_transfer_free(statep->ds1307_i2c_hdl,
3087c478bd9Sstevel@tonic-gate 			    i2c_tp);
3097c478bd9Sstevel@tonic-gate 			ddi_soft_state_free(ds1307_statep, instance);
3107c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
315dd4eeefdSeota 	/*
316dd4eeefdSeota 	 * Create a periodical handler to read TOD.
317dd4eeefdSeota 	 */
318dd4eeefdSeota 	ASSERT(statep->cycid == NULL);
319dd4eeefdSeota 	statep->cycid = ddi_periodic_add(todds1307_cyclic, &soft_rtc,
320dd4eeefdSeota 	    i2c_cyclic_timeout, DDI_IPL_1);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	statep->state = TOD_ATTACHED;
3237c478bd9Sstevel@tonic-gate 	todds1307_attach_done = 1;
3247c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate /*
3307c478bd9Sstevel@tonic-gate  * Unregister ds1307 client device with i2c services and free
3317c478bd9Sstevel@tonic-gate  * soft state structure.
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3347c478bd9Sstevel@tonic-gate static int
3357c478bd9Sstevel@tonic-gate todds1307_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	switch (cmd) {
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	 * Once attached, do not allow detach because the system constantly
3417c478bd9Sstevel@tonic-gate 	 * calling todds1307_get() to get the time.  If the driver is detached
3427c478bd9Sstevel@tonic-gate 	 * and the system try to get the time, the system will have memory
3437c478bd9Sstevel@tonic-gate 	 * problem.
3447c478bd9Sstevel@tonic-gate 	 *
3457c478bd9Sstevel@tonic-gate 	 *	ds1307_state_t	*statep = NULL;
3467c478bd9Sstevel@tonic-gate 	 *	case DDI_DETACH:
3477c478bd9Sstevel@tonic-gate 	 *		if ((statep = ddi_get_soft_state(ds1307_statep,
3487c478bd9Sstevel@tonic-gate 	 *					instance)) == NULL) {
3497c478bd9Sstevel@tonic-gate 	 *			return (ENOMEM);
3507c478bd9Sstevel@tonic-gate 	 *		}
3517c478bd9Sstevel@tonic-gate 	 *		i2c_client_unregister(statep->ds1307_i2c_hdl);
3527c478bd9Sstevel@tonic-gate 	 *		ddi_soft_state_free(ds1307_statep, instance);
3537c478bd9Sstevel@tonic-gate 	 *		return (DDI_SUCCESS);
3547c478bd9Sstevel@tonic-gate 	 */
3557c478bd9Sstevel@tonic-gate 		case DDI_SUSPEND:
3567c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 		default:
3597c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate /* *********************** tod_ops entry points ******************** */
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * Read the current time from the DS1307 chip and convert to UNIX form.
3677c478bd9Sstevel@tonic-gate  * Should be called with tod_clock held.
3687c478bd9Sstevel@tonic-gate  */
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate static timestruc_t
3717c478bd9Sstevel@tonic-gate todds1307_get(void)
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate 	timestruc_t	ts;
3747c478bd9Sstevel@tonic-gate 	todinfo_t	tod;
3757c478bd9Sstevel@tonic-gate 	struct	rtc_t	rtc;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (sync_clock_once) {
38007d06da5SSurya Prakki 		(void) todds1307_read_rtc(&soft_rtc);
3817c478bd9Sstevel@tonic-gate 		sync_clock_once = 0;
3827c478bd9Sstevel@tonic-gate 	} else {
383*8fc99e42STrevor Thompson 		tod_status_set(TOD_GET_FAILED);
3847c478bd9Sstevel@tonic-gate 		return (hrestime);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	bcopy(&soft_rtc, &rtc, sizeof (rtc));
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	/*
3907c478bd9Sstevel@tonic-gate 	 * 00 - 68 = 2000 thru 2068
3917c478bd9Sstevel@tonic-gate 	 * 69-99 = 1969 thru 1999
3927c478bd9Sstevel@tonic-gate 	 */
3937c478bd9Sstevel@tonic-gate 	tod.tod_year    = rtc.rtc_year;
3947c478bd9Sstevel@tonic-gate 	if (rtc.rtc_year <= 68)
3957c478bd9Sstevel@tonic-gate 		tod.tod_year += 100;
3967c478bd9Sstevel@tonic-gate 	tod.tod_month	= rtc.rtc_mon;
3977c478bd9Sstevel@tonic-gate 	tod.tod_day	= rtc.rtc_dom;
3987c478bd9Sstevel@tonic-gate 	tod.tod_dow	= rtc.rtc_dow;
3997c478bd9Sstevel@tonic-gate 	tod.tod_hour	= rtc.rtc_hrs;
4007c478bd9Sstevel@tonic-gate 	tod.tod_min	= rtc.rtc_min;
4017c478bd9Sstevel@tonic-gate 	tod.tod_sec	= rtc.rtc_sec;
4027c478bd9Sstevel@tonic-gate 
403*8fc99e42STrevor Thompson 	/* read was successful so ensure failure flag is clear */
404*8fc99e42STrevor Thompson 	tod_status_clear(TOD_GET_FAILED);
405*8fc99e42STrevor Thompson 
4067c478bd9Sstevel@tonic-gate 	ts.tv_sec = tod_to_utc(tod);
4077c478bd9Sstevel@tonic-gate 	ts.tv_nsec = 0;
4087c478bd9Sstevel@tonic-gate 	return (ts);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate  * Program DS1307 with the specified time.
4137c478bd9Sstevel@tonic-gate  * Must be called with tod_lock held. The TOD
4147c478bd9Sstevel@tonic-gate  * chip supports date from 1969-2068 only. We must
4157c478bd9Sstevel@tonic-gate  * reject requests to set date below 2000.
4167c478bd9Sstevel@tonic-gate  */
4177c478bd9Sstevel@tonic-gate static void
4187c478bd9Sstevel@tonic-gate todds1307_set(timestruc_t ts)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	struct rtc_t	rtc;
4217c478bd9Sstevel@tonic-gate 	todinfo_t	tod = utc_to_tod(ts.tv_sec);
4227c478bd9Sstevel@tonic-gate 	int		year;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	/*
4287c478bd9Sstevel@tonic-gate 	 * Year is base 1900, valid year range 1969-2068
4297c478bd9Sstevel@tonic-gate 	 */
4307c478bd9Sstevel@tonic-gate 	if ((tod.tod_year < 69) || (tod.tod_year > 168))
4317c478bd9Sstevel@tonic-gate 		return;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	year = tod.tod_year;
4347c478bd9Sstevel@tonic-gate 	if (year >= 100)
4357c478bd9Sstevel@tonic-gate 		year -= 100;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	rtc.rtc_year	= int2bcd(year);
4387c478bd9Sstevel@tonic-gate 	rtc.rtc_mon	= int2bcd(tod.tod_month);
4397c478bd9Sstevel@tonic-gate 	rtc.rtc_dom	= int2bcd(tod.tod_day);
4407c478bd9Sstevel@tonic-gate 	rtc.rtc_dow	= int2bcd(tod.tod_dow);
4417c478bd9Sstevel@tonic-gate 	rtc.rtc_hrs	= int2bcd(tod.tod_hour);
4427c478bd9Sstevel@tonic-gate 	rtc.rtc_min	= int2bcd(tod.tod_min);
4437c478bd9Sstevel@tonic-gate 	rtc.rtc_sec	= int2bcd(tod.tod_sec);
4447c478bd9Sstevel@tonic-gate 
44507d06da5SSurya Prakki 	(void) todds1307_write_rtc(&rtc);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate /* ARGSUSED */
4497c478bd9Sstevel@tonic-gate static void
4507c478bd9Sstevel@tonic-gate todds1307_set_power_alarm(timestruc_t ts)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate /* ARGSUSED */
4567c478bd9Sstevel@tonic-gate static void
4577c478bd9Sstevel@tonic-gate todds1307_clear_power_alarm(void)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate /* ARGSUSED */
4637c478bd9Sstevel@tonic-gate static uint_t
4647c478bd9Sstevel@tonic-gate todds1307_set_watchdog_timer(uint_t timeoutval)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4677c478bd9Sstevel@tonic-gate 	return (0);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate /* ARGSUSED */
4717c478bd9Sstevel@tonic-gate static uint_t
4727c478bd9Sstevel@tonic-gate todds1307_clear_watchdog_timer(void)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4757c478bd9Sstevel@tonic-gate 	return (0);
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate /* ********************** Local functions ***************************** */
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate static char tod_read[7] = {-1, -1, -1, -1, -1, -1, -1};
4817c478bd9Sstevel@tonic-gate static int
4827c478bd9Sstevel@tonic-gate todds1307_read_rtc(struct rtc_t *rtc)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	static	ds1307_state_t	*statep = NULL;
4857c478bd9Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
4867c478bd9Sstevel@tonic-gate 	int i2c_cmd_status = I2C_FAILURE;
4877c478bd9Sstevel@tonic-gate 	int counter = 4;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	if (!todds1307_attach_done) {
4907c478bd9Sstevel@tonic-gate 		return (todds1307_prom_getdate(rtc));
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1307_statep, instance);
4947c478bd9Sstevel@tonic-gate 	if (statep == NULL) {
4957c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1307: ddi_get_soft_state failed");
4967c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	mutex_enter(&todds1307_rd_lock);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	/*
5027c478bd9Sstevel@tonic-gate 	 * Allocate 1 byte for write buffer and 7 bytes for read buffer to
5037c478bd9Sstevel@tonic-gate 	 * to accomodate sec, min, hrs, dayOfWeek, dayOfMonth, year
5047c478bd9Sstevel@tonic-gate 	 */
5057c478bd9Sstevel@tonic-gate 	if ((i2c_transfer_alloc(statep->ds1307_i2c_hdl, &i2c_tp, 1,
5067c478bd9Sstevel@tonic-gate 	    7, I2C_SLEEP)) != I2C_SUCCESS) {
5077c478bd9Sstevel@tonic-gate 		mutex_exit(&todds1307_rd_lock);
5087c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	do {
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)0x00; /* Start from reg 0x00 */
5157c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wlen = 1;	/* Write one byte address */
5167c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_rlen = 7;	/* Read 7 regs */
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 		if ((i2c_cmd_status = i2c_transfer(statep->ds1307_i2c_hdl,
5197c478bd9Sstevel@tonic-gate 		    i2c_tp)) != I2C_SUCCESS) {
5207c478bd9Sstevel@tonic-gate 			drv_usecwait(I2C_DELAY);
5217c478bd9Sstevel@tonic-gate 			goto done;
5227c478bd9Sstevel@tonic-gate 		}
5237c478bd9Sstevel@tonic-gate 		/* for first read, need to get valid data */
5247c478bd9Sstevel@tonic-gate 		while (tod_read[0] == -1 && counter > 0) {
5257c478bd9Sstevel@tonic-gate 		/* move data to static buffer */
5267c478bd9Sstevel@tonic-gate 		bcopy(i2c_tp->i2c_rbuf, tod_read, 7);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 		/* now read again */
5297c478bd9Sstevel@tonic-gate 		/* Start reading reg from 0x00 */
5307c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[0] = (uchar_t)0x00;
5317c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_wlen = 1;	/* Write one byte address */
5327c478bd9Sstevel@tonic-gate 		i2c_tp->i2c_rlen = 7;	/* Read 7 regs */
5337c478bd9Sstevel@tonic-gate 		if ((i2c_cmd_status = i2c_transfer(statep->ds1307_i2c_hdl,
5347c478bd9Sstevel@tonic-gate 		    i2c_tp)) != I2C_SUCCESS) {
5357c478bd9Sstevel@tonic-gate 			drv_usecwait(I2C_DELAY);
5367c478bd9Sstevel@tonic-gate 			goto done;
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate 		/* if they are not the same, then read again */
5397c478bd9Sstevel@tonic-gate 		if (bcmp(tod_read, i2c_tp->i2c_rbuf, 7) != 0) {
5407c478bd9Sstevel@tonic-gate 			tod_read[0] = -1;
5417c478bd9Sstevel@tonic-gate 			counter--;
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	} while (i2c_tp->i2c_rbuf[0] == 0x59 &&
5467c478bd9Sstevel@tonic-gate 	    /* if seconds register is 0x59 (BCD), add data should match */
5477c478bd9Sstevel@tonic-gate 	    bcmp(&tod_read[1], &i2c_tp->i2c_rbuf[1], 6) != 0 &&
5487c478bd9Sstevel@tonic-gate 	    counter-- > 0);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (counter < 0)
5517c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "i2ctod: TOD Chip failed ??");
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	/* move data to static buffer */
5547c478bd9Sstevel@tonic-gate 	bcopy(i2c_tp->i2c_rbuf, tod_read, 7);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	rtc->rtc_year	= bcd2int(i2c_tp->i2c_rbuf[6]);
5587c478bd9Sstevel@tonic-gate 	rtc->rtc_mon	= bcd2int(i2c_tp->i2c_rbuf[5]);
5597c478bd9Sstevel@tonic-gate 	rtc->rtc_dom	= bcd2int(i2c_tp->i2c_rbuf[4]);
5607c478bd9Sstevel@tonic-gate 	rtc->rtc_dow	= bcd2int(i2c_tp->i2c_rbuf[3]);
5617c478bd9Sstevel@tonic-gate 	rtc->rtc_hrs	= bcd2int(i2c_tp->i2c_rbuf[2]);
5627c478bd9Sstevel@tonic-gate 	rtc->rtc_min	= bcd2int(i2c_tp->i2c_rbuf[1]);
5637c478bd9Sstevel@tonic-gate 	rtc->rtc_sec	= bcd2int(i2c_tp->i2c_rbuf[0]);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate done:
5667c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	mutex_exit(&todds1307_rd_lock);
5697c478bd9Sstevel@tonic-gate 	return (i2c_cmd_status);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate static int
5747c478bd9Sstevel@tonic-gate todds1307_write_rtc(struct rtc_t *rtc)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	ds1307_state_t	*statep = NULL;
5777c478bd9Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
5787c478bd9Sstevel@tonic-gate 	int i2c_cmd_status = I2C_SUCCESS;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	if (!todds1307_attach_done) {
5827c478bd9Sstevel@tonic-gate 		return (todds1307_prom_setdate(rtc));
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1307_statep, instance);
5867c478bd9Sstevel@tonic-gate 	if (statep == NULL) {
5877c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	if ((i2c_cmd_status = i2c_transfer_alloc(statep->ds1307_i2c_hdl,
5917c478bd9Sstevel@tonic-gate 	    &i2c_tp, 8, 0, I2C_SLEEP)) != I2C_SUCCESS) {
5927c478bd9Sstevel@tonic-gate 		return (i2c_cmd_status);
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
5967c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
5977c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)0x00;
5987c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = rtc->rtc_sec;
5997c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[2] = rtc->rtc_min;
6007c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[3] = rtc->rtc_hrs;
6017c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[4] = rtc->rtc_dow;
6027c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[5] = rtc->rtc_dom;
6037c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[6] = rtc->rtc_mon;
6047c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[7] = rtc->rtc_year;
6057c478bd9Sstevel@tonic-gate 	i2c_tp->i2c_wlen = 8;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if ((i2c_cmd_status = i2c_transfer(statep->ds1307_i2c_hdl,
6087c478bd9Sstevel@tonic-gate 	    i2c_tp)) != I2C_SUCCESS) {
6097c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
6107c478bd9Sstevel@tonic-gate 		/* delay(drv_usectohz(I2C_DELAY)); */
6117c478bd9Sstevel@tonic-gate 		drv_usecwait(I2C_DELAY);
6127c478bd9Sstevel@tonic-gate 		return (i2c_cmd_status);
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	tod_read[0] = -1;  /* invalidate saved data from read routine */
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	return (i2c_cmd_status);
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6247c478bd9Sstevel@tonic-gate static int
6257c478bd9Sstevel@tonic-gate todds1307_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
6267c478bd9Sstevel@tonic-gate     void **result)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate 	ds1307_state_t *softsp;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	if (instance == -1) {
6317c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	switch (infocmd) {
6357c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
6367c478bd9Sstevel@tonic-gate 		if ((softsp = ddi_get_soft_state(ds1307_statep, instance))
6377c478bd9Sstevel@tonic-gate 		    == NULL)
6387c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
6397c478bd9Sstevel@tonic-gate 		*result = (void *)softsp->dip;
6407c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
643544f04c0Swnelson 		*result = (void *)(uintptr_t)instance;
6447c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	default:
6477c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate /*
6527c478bd9Sstevel@tonic-gate  * Conversion functions
6537c478bd9Sstevel@tonic-gate  */
6547c478bd9Sstevel@tonic-gate static unsigned char
6557c478bd9Sstevel@tonic-gate int2bcd(int num) {
6567c478bd9Sstevel@tonic-gate 	return (((num / 10) << 4)	/* tens BCD digit in high four bits */
6577c478bd9Sstevel@tonic-gate 	+ (num % 10));		/* units digit goes in low four bits */
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate static int
6617c478bd9Sstevel@tonic-gate bcd2int(unsigned char num) {
6627c478bd9Sstevel@tonic-gate 	return (((num >> 4) * 10)	/* 10 times high-order four bits */
6637c478bd9Sstevel@tonic-gate 	+ (num & 0x0f));		/* plus low-order four bits */
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate  * Finds the device node with device_type "rtc" and opens it to
6687c478bd9Sstevel@tonic-gate  * execute the get-time method
6697c478bd9Sstevel@tonic-gate  */
6707c478bd9Sstevel@tonic-gate static int
6717c478bd9Sstevel@tonic-gate todds1307_setup_prom()
6727c478bd9Sstevel@tonic-gate {
673fa9e4066Sahrens 	pnode_t todnode;
6747c478bd9Sstevel@tonic-gate 	char tod1307_devpath[MAXNAMELEN];
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	if ((todnode = prom_findnode_bydevtype(prom_rootnode(),
6777c478bd9Sstevel@tonic-gate 	    DS1307_DEVICE_TYPE)) == OBP_NONODE)
6787c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	/*
6817c478bd9Sstevel@tonic-gate 	 * We now have the phandle of the rtc node, we need to open the
6827c478bd9Sstevel@tonic-gate 	 * node and get the ihandle
6837c478bd9Sstevel@tonic-gate 	 */
6847c478bd9Sstevel@tonic-gate 	if (prom_phandle_to_path(todnode, tod1307_devpath,
6857c478bd9Sstevel@tonic-gate 	    sizeof (tod1307_devpath)) < 0) {
6867c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "prom_phandle_to_path failed");
6877c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/*
6917c478bd9Sstevel@tonic-gate 	 * Now open the node and store it's ihandle
6927c478bd9Sstevel@tonic-gate 	 */
6937c478bd9Sstevel@tonic-gate 	if ((todds1307_ihandle = prom_open(tod1307_devpath)) == NULL) {
6947c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "prom_open failed");
6957c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate  * Closes the prom interface
7037c478bd9Sstevel@tonic-gate  */
7047c478bd9Sstevel@tonic-gate static void
7057c478bd9Sstevel@tonic-gate todds1307_rele_prom()
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate 	(void) prom_close(todds1307_ihandle);
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate /*
7117c478bd9Sstevel@tonic-gate  * Read the date using "get-time" method in rtc node
7127c478bd9Sstevel@tonic-gate  * PROM returns 1969-1999 when reading 69-99 and
7137c478bd9Sstevel@tonic-gate  * 2000-2068 when reading 00-68
7147c478bd9Sstevel@tonic-gate  */
7157c478bd9Sstevel@tonic-gate static int
7167c478bd9Sstevel@tonic-gate todds1307_prom_getdate(struct rtc_t *rtc)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	int year;
7197c478bd9Sstevel@tonic-gate 	cell_t ci[12];
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");  /* Service name */
7227c478bd9Sstevel@tonic-gate 	ci[1] = 2; /* # of arguments */
7237c478bd9Sstevel@tonic-gate 	ci[2] = 7; /* # of result cells */
7247c478bd9Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("get-time");
7257c478bd9Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(todds1307_ihandle);
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	promif_preprom();
7287c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
7297c478bd9Sstevel@tonic-gate 	promif_postprom();
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	year 		= p1275_cell2int(ci[6]);
7327c478bd9Sstevel@tonic-gate 	rtc->rtc_mon	= p1275_cell2int(ci[7]);
7337c478bd9Sstevel@tonic-gate 	rtc->rtc_dom	= p1275_cell2int(ci[8]);
7347c478bd9Sstevel@tonic-gate 	rtc->rtc_dow	= 0;
7357c478bd9Sstevel@tonic-gate 	rtc->rtc_hrs	= p1275_cell2int(ci[9]);
7367c478bd9Sstevel@tonic-gate 	rtc->rtc_min	= p1275_cell2int(ci[10]);
7377c478bd9Sstevel@tonic-gate 	rtc->rtc_sec	= p1275_cell2int(ci[11]);
7387c478bd9Sstevel@tonic-gate 	if (year >= 2000)
7397c478bd9Sstevel@tonic-gate 		year -= 2000;
7407c478bd9Sstevel@tonic-gate 	else
7417c478bd9Sstevel@tonic-gate 		year -= 1900;
7427c478bd9Sstevel@tonic-gate 	rtc->rtc_year	= year;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate /*
7487c478bd9Sstevel@tonic-gate  * Read the date using "set-time" method in rtc node
7497c478bd9Sstevel@tonic-gate  * For values 00 - 68, write 2000-2068, and for 69-99,
7507c478bd9Sstevel@tonic-gate  * write 1969-1999
7517c478bd9Sstevel@tonic-gate  */
7527c478bd9Sstevel@tonic-gate static int
7537c478bd9Sstevel@tonic-gate todds1307_prom_setdate(struct rtc_t *rtc)
7547c478bd9Sstevel@tonic-gate {
7557c478bd9Sstevel@tonic-gate 	int year;
7567c478bd9Sstevel@tonic-gate 	cell_t ci[12];
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	year = rtc->rtc_year;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	if ((year < 0) || (year > 99))
7617c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	if (year <= 68)
7647c478bd9Sstevel@tonic-gate 		year = rtc->rtc_year + 2000;
7657c478bd9Sstevel@tonic-gate 	else
7667c478bd9Sstevel@tonic-gate 		year = rtc->rtc_year + 1900;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");  /* Service name */
7697c478bd9Sstevel@tonic-gate 	ci[1] = 8; /* # of arguments */
7707c478bd9Sstevel@tonic-gate 	ci[2] = 0; /* # of result cells */
7717c478bd9Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("set-time");
7727c478bd9Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(todds1307_ihandle);
7737c478bd9Sstevel@tonic-gate 	ci[5] = p1275_int2cell(year);
7747c478bd9Sstevel@tonic-gate 	ci[6] = p1275_int2cell(rtc->rtc_mon);
7757c478bd9Sstevel@tonic-gate 	ci[7] = p1275_int2cell(rtc->rtc_dom);
7767c478bd9Sstevel@tonic-gate 	ci[8] = p1275_int2cell(rtc->rtc_hrs);
7777c478bd9Sstevel@tonic-gate 	ci[9] = p1275_int2cell(rtc->rtc_min);
7787c478bd9Sstevel@tonic-gate 	ci[10] = p1275_int2cell(rtc->rtc_sec);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	promif_preprom();
7817c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
7827c478bd9Sstevel@tonic-gate 	promif_postprom();
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7857c478bd9Sstevel@tonic-gate }
786