xref: /onnv-gate/usr/src/uts/sun4u/snowbird/io/todds1307/todds1307.c (revision 11752:9c475fee0b48)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55107Seota  * Common Development and Distribution License (the "License").
65107Seota  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11752STrevor.Thompson@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/conf.h>
290Sstevel@tonic-gate #include <sys/devops.h>
300Sstevel@tonic-gate #include <sys/kmem.h>
310Sstevel@tonic-gate #include <sys/open.h>
320Sstevel@tonic-gate #include <sys/file.h>
330Sstevel@tonic-gate #include <sys/note.h>
340Sstevel@tonic-gate #include <sys/ddi.h>
350Sstevel@tonic-gate #include <sys/sunddi.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/modctl.h>
380Sstevel@tonic-gate #include <sys/stat.h>
390Sstevel@tonic-gate #include <sys/clock.h>
400Sstevel@tonic-gate #include <sys/reboot.h>
410Sstevel@tonic-gate #include <sys/machsystm.h>
420Sstevel@tonic-gate #include <sys/poll.h>
430Sstevel@tonic-gate #include <sys/pbio.h>
440Sstevel@tonic-gate #include <sys/sysmacros.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /* Added for prom interface */
470Sstevel@tonic-gate #include <sys/promif.h>
480Sstevel@tonic-gate #include <sys/promimpl.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <sys/i2c/misc/i2c_svc.h>
510Sstevel@tonic-gate #include <sys/todds1307.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define	I2C_DELAY	20000
540Sstevel@tonic-gate #define	DS1307_DEVICE_TYPE	"rtc"
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * Driver enrty routines
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate static int todds1307_attach(dev_info_t *, ddi_attach_cmd_t);
600Sstevel@tonic-gate static int todds1307_detach(dev_info_t *, ddi_detach_cmd_t);
610Sstevel@tonic-gate static int todds1307_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate  * tod_ops entry routines
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate static timestruc_t	todds1307_get(void);
670Sstevel@tonic-gate static void		todds1307_set(timestruc_t);
680Sstevel@tonic-gate static uint_t		todds1307_set_watchdog_timer(uint_t);
690Sstevel@tonic-gate static uint_t		todds1307_clear_watchdog_timer(void);
700Sstevel@tonic-gate static void		todds1307_set_power_alarm(timestruc_t);
710Sstevel@tonic-gate static void		todds1307_clear_power_alarm(void);
720Sstevel@tonic-gate static int todds1307_setup_prom();
730Sstevel@tonic-gate static void todds1307_rele_prom();
740Sstevel@tonic-gate static int todds1307_prom_getdate(struct rtc_t *rtc);
750Sstevel@tonic-gate static int todds1307_prom_setdate(struct rtc_t *rtc);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Local functions
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate static int	todds1307_read_rtc(struct rtc_t *);
810Sstevel@tonic-gate static int	todds1307_write_rtc(struct rtc_t *);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /* Anchor for soft state structure */
840Sstevel@tonic-gate static void	*ds1307_statep;
850Sstevel@tonic-gate static int	instance = -1;
860Sstevel@tonic-gate static int	todds1307_attach_done = 0;
870Sstevel@tonic-gate static kmutex_t	todds1307_rd_lock;
880Sstevel@tonic-gate static ihandle_t todds1307_ihandle = 0;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /* one second time out */
910Sstevel@tonic-gate #define	I2c_CYCLIC_TIMEOUT	1000000000
920Sstevel@tonic-gate uint_t i2c_cyclic_timeout = I2c_CYCLIC_TIMEOUT;
930Sstevel@tonic-gate static int sync_clock_once = 1;
940Sstevel@tonic-gate static 	struct	rtc_t	 soft_rtc;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * For debugging only
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate static unsigned char int2bcd(int num);
1000Sstevel@tonic-gate static int bcd2int(unsigned char num);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * cp_ops structure
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate static struct cb_ops ds1307_cbops = {
1060Sstevel@tonic-gate 	nodev,				/* open */
1070Sstevel@tonic-gate 	nodev,				/* close */
1080Sstevel@tonic-gate 	nodev,				/* strategy */
1090Sstevel@tonic-gate 	nodev,				/* print */
1100Sstevel@tonic-gate 	nodev,				/* dump */
1110Sstevel@tonic-gate 	nodev,				/* read */
1120Sstevel@tonic-gate 	nodev,				/* write */
1130Sstevel@tonic-gate 	nodev,				/* ioctl */
1140Sstevel@tonic-gate 	nodev,				/* devmap */
1150Sstevel@tonic-gate 	nodev,				/* mmap */
1160Sstevel@tonic-gate 	nodev,				/* segmap */
1170Sstevel@tonic-gate 	NULL,				/* poll */
1180Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
1190Sstevel@tonic-gate 	NULL,				/* streamtab */
1200Sstevel@tonic-gate 	D_NEW | D_MP,			/* Driver compatibility flag */
1210Sstevel@tonic-gate 	CB_REV,				/* rev */
1220Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
1230Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
1240Sstevel@tonic-gate };
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * dev_ops structure
1280Sstevel@tonic-gate  */
1290Sstevel@tonic-gate static struct dev_ops ds1307_ops = {
1300Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1310Sstevel@tonic-gate 	0,			/* refcnt - reference cnt always set to 0 */
1320Sstevel@tonic-gate 	todds1307_getinfo,	/* getinfo - Maybe requred */
1330Sstevel@tonic-gate 	nulldev,		/* identify */
1340Sstevel@tonic-gate 	nulldev,		/* probe */
1350Sstevel@tonic-gate 	todds1307_attach,	/* attach */
1360Sstevel@tonic-gate 	todds1307_detach,	/* detach */
1370Sstevel@tonic-gate 	nodev,			/* reset */
1380Sstevel@tonic-gate 	&ds1307_cbops,		/* cb_ops - ds1307 does not need this(?) */
1397656SSherry.Moore@Sun.COM 	NULL,			/* bus_ops */
1407656SSherry.Moore@Sun.COM 	NULL,			/* power */
1417656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
1420Sstevel@tonic-gate };
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate static struct modldrv todds1307_modldrv = {
1450Sstevel@tonic-gate 	&mod_driverops,		/* Type of module. This one is a driver */
1467656SSherry.Moore@Sun.COM 	"tod driver for DS1307 v1.12",	/* Name of the module */
1470Sstevel@tonic-gate 	&ds1307_ops,			/* Pointer to dev_ops */
1480Sstevel@tonic-gate };
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * Module linkage structure
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate static struct modlinkage todds1307_modlinkage = {
1540Sstevel@tonic-gate 	MODREV_1,
1550Sstevel@tonic-gate 	&todds1307_modldrv,
1560Sstevel@tonic-gate 	0
1570Sstevel@tonic-gate };
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate int
_init(void)1600Sstevel@tonic-gate _init(void)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	int error;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (strcmp(tod_module_name, "todds1307") == 0) {
1650Sstevel@tonic-gate 		if ((error = ddi_soft_state_init(&ds1307_statep,
1665107Seota 		    sizeof (ds1307_state_t), 0)) != DDI_SUCCESS) {
1670Sstevel@tonic-gate 			return (error);
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 		tod_ops.tod_get = todds1307_get;
1710Sstevel@tonic-gate 		tod_ops.tod_set = todds1307_set;
1720Sstevel@tonic-gate 		tod_ops.tod_set_watchdog_timer = todds1307_set_watchdog_timer;
1730Sstevel@tonic-gate 		tod_ops.tod_clear_watchdog_timer =
1745107Seota 		    todds1307_clear_watchdog_timer;
1750Sstevel@tonic-gate 		tod_ops.tod_set_power_alarm = todds1307_set_power_alarm;
1760Sstevel@tonic-gate 		tod_ops.tod_clear_power_alarm = todds1307_clear_power_alarm;
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	(void) todds1307_setup_prom();
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * Install the module
1830Sstevel@tonic-gate 	 */
1840Sstevel@tonic-gate 	if ((error = mod_install(&todds1307_modlinkage)) != 0) {
1850Sstevel@tonic-gate 		ddi_soft_state_fini(&ds1307_statep);
1860Sstevel@tonic-gate 		return (error);
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 	mutex_init(&todds1307_rd_lock, NULL, MUTEX_DEFAULT, NULL);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	return (0);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate int
_fini(void)1940Sstevel@tonic-gate _fini(void)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	int error = 0;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if (strcmp(tod_module_name, "todds1307") == 0) {
1990Sstevel@tonic-gate 		error = EBUSY;
2000Sstevel@tonic-gate 	} else {
2010Sstevel@tonic-gate 		if ((error = mod_remove(&todds1307_modlinkage)) == 0) {
2020Sstevel@tonic-gate 			ddi_soft_state_fini(&ds1307_statep);
2030Sstevel@tonic-gate 			mutex_destroy(&todds1307_rd_lock);
2040Sstevel@tonic-gate 			todds1307_rele_prom();
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	return (error);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2120Sstevel@tonic-gate _info(struct modinfo *modinfop)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	return (mod_info(&todds1307_modlinkage, modinfop));
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate  * cyclical call to get tod.
2190Sstevel@tonic-gate  */
2200Sstevel@tonic-gate static void
todds1307_cyclic(void * arg)2210Sstevel@tonic-gate todds1307_cyclic(void *arg)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 
22411311SSurya.Prakki@Sun.COM 	(void) todds1307_read_rtc((struct rtc_t *)arg);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * register ds1307 client device with i2c services, and
2300Sstevel@tonic-gate  * allocate & initialize soft state structure.
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate static int
todds1307_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)2330Sstevel@tonic-gate todds1307_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	static ds1307_state_t	*statep = NULL;
2360Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
2370Sstevel@tonic-gate 	uint8_t tempVal = (uint8_t)0;
2380Sstevel@tonic-gate 	switch (cmd) {
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 		case DDI_ATTACH:
2410Sstevel@tonic-gate 			break;
2420Sstevel@tonic-gate 		case DDI_RESUME:
2430Sstevel@tonic-gate 			return (DDI_SUCCESS);
2440Sstevel@tonic-gate 		default:
2450Sstevel@tonic-gate 			return (DDI_FAILURE);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	if (instance != -1) {
2490Sstevel@tonic-gate 		return (DDI_FAILURE);
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/*
2550Sstevel@tonic-gate 	 * Allocate soft state structure
2560Sstevel@tonic-gate 	 */
2570Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(ds1307_statep, instance) != DDI_SUCCESS) {
2580Sstevel@tonic-gate 		return (DDI_FAILURE);
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1307_statep, instance);
2620Sstevel@tonic-gate 	if (statep == NULL) {
2630Sstevel@tonic-gate 		return (DDI_FAILURE);
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	statep->dip = dip;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if (i2c_client_register(dip, &statep->ds1307_i2c_hdl) != I2C_SUCCESS) {
2690Sstevel@tonic-gate 		ddi_soft_state_free(ds1307_statep, instance);
2700Sstevel@tonic-gate 		delay(drv_usectohz(I2C_DELAY));
2710Sstevel@tonic-gate 		return (DDI_FAILURE);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	/* check and initialize the oscillator */
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1307_i2c_hdl,
2775107Seota 	    &i2c_tp, 1, 1, I2C_SLEEP);
2780Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
2790Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR_RD;
2800Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)0x00; /* Read 00h */
2810Sstevel@tonic-gate 	i2c_tp->i2c_wlen = 1;
2820Sstevel@tonic-gate 	i2c_tp->i2c_rlen = 1;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1307_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
2850Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
2860Sstevel@tonic-gate 		ddi_soft_state_free(ds1307_statep, instance);
2870Sstevel@tonic-gate 		delay(drv_usectohz(I2C_DELAY));
2880Sstevel@tonic-gate 		return (DDI_FAILURE);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	tempVal = i2c_tp->i2c_rbuf[0];
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if (tempVal & 0x80) {			 /* check Oscillator */
2960Sstevel@tonic-gate 		(void) i2c_transfer_alloc(statep->ds1307_i2c_hdl, &i2c_tp,
2975107Seota 		    2, 1, I2C_SLEEP);
2980Sstevel@tonic-gate 		i2c_tp->i2c_version = I2C_XFER_REV;
2990Sstevel@tonic-gate 		i2c_tp->i2c_flags = I2C_WR;
3000Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[0] = 0x00;
3010Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[1] =
3020Sstevel@tonic-gate 		    (uchar_t)(i2c_tp->i2c_rbuf[0]& 0x7f);
3030Sstevel@tonic-gate 		i2c_tp->i2c_wlen = 2;
3040Sstevel@tonic-gate 					/* Enable oscillator */
3050Sstevel@tonic-gate 		if ((i2c_transfer(statep->ds1307_i2c_hdl, i2c_tp))
3065107Seota 		    != I2C_SUCCESS) {
3070Sstevel@tonic-gate 			(void) i2c_transfer_free(statep->ds1307_i2c_hdl,
3085107Seota 			    i2c_tp);
3090Sstevel@tonic-gate 			ddi_soft_state_free(ds1307_statep, instance);
3100Sstevel@tonic-gate 			return (DDI_FAILURE);
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 
3155107Seota 	/*
3165107Seota 	 * Create a periodical handler to read TOD.
3175107Seota 	 */
3185107Seota 	ASSERT(statep->cycid == NULL);
3195107Seota 	statep->cycid = ddi_periodic_add(todds1307_cyclic, &soft_rtc,
3205107Seota 	    i2c_cyclic_timeout, DDI_IPL_1);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	statep->state = TOD_ATTACHED;
3230Sstevel@tonic-gate 	todds1307_attach_done = 1;
3240Sstevel@tonic-gate 	ddi_report_dev(dip);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	return (DDI_SUCCESS);
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate /*
3300Sstevel@tonic-gate  * Unregister ds1307 client device with i2c services and free
3310Sstevel@tonic-gate  * soft state structure.
3320Sstevel@tonic-gate  */
3330Sstevel@tonic-gate /*ARGSUSED*/
3340Sstevel@tonic-gate static int
todds1307_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)3350Sstevel@tonic-gate todds1307_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate 	switch (cmd) {
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	/*
3400Sstevel@tonic-gate 	 * Once attached, do not allow detach because the system constantly
3410Sstevel@tonic-gate 	 * calling todds1307_get() to get the time.  If the driver is detached
3420Sstevel@tonic-gate 	 * and the system try to get the time, the system will have memory
3430Sstevel@tonic-gate 	 * problem.
3440Sstevel@tonic-gate 	 *
3450Sstevel@tonic-gate 	 *	ds1307_state_t	*statep = NULL;
3460Sstevel@tonic-gate 	 *	case DDI_DETACH:
3470Sstevel@tonic-gate 	 *		if ((statep = ddi_get_soft_state(ds1307_statep,
3480Sstevel@tonic-gate 	 *					instance)) == NULL) {
3490Sstevel@tonic-gate 	 *			return (ENOMEM);
3500Sstevel@tonic-gate 	 *		}
3510Sstevel@tonic-gate 	 *		i2c_client_unregister(statep->ds1307_i2c_hdl);
3520Sstevel@tonic-gate 	 *		ddi_soft_state_free(ds1307_statep, instance);
3530Sstevel@tonic-gate 	 *		return (DDI_SUCCESS);
3540Sstevel@tonic-gate 	 */
3550Sstevel@tonic-gate 		case DDI_SUSPEND:
3560Sstevel@tonic-gate 			return (DDI_SUCCESS);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 		default:
3590Sstevel@tonic-gate 			return (DDI_FAILURE);
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate /* *********************** tod_ops entry points ******************** */
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate  * Read the current time from the DS1307 chip and convert to UNIX form.
3670Sstevel@tonic-gate  * Should be called with tod_clock held.
3680Sstevel@tonic-gate  */
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate static timestruc_t
todds1307_get(void)3710Sstevel@tonic-gate todds1307_get(void)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	timestruc_t	ts;
3740Sstevel@tonic-gate 	todinfo_t	tod;
3750Sstevel@tonic-gate 	struct	rtc_t	rtc;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if (sync_clock_once) {
38011311SSurya.Prakki@Sun.COM 		(void) todds1307_read_rtc(&soft_rtc);
3810Sstevel@tonic-gate 		sync_clock_once = 0;
3820Sstevel@tonic-gate 	} else {
383*11752STrevor.Thompson@Sun.COM 		tod_status_set(TOD_GET_FAILED);
3840Sstevel@tonic-gate 		return (hrestime);
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	bcopy(&soft_rtc, &rtc, sizeof (rtc));
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	/*
3900Sstevel@tonic-gate 	 * 00 - 68 = 2000 thru 2068
3910Sstevel@tonic-gate 	 * 69-99 = 1969 thru 1999
3920Sstevel@tonic-gate 	 */
3930Sstevel@tonic-gate 	tod.tod_year    = rtc.rtc_year;
3940Sstevel@tonic-gate 	if (rtc.rtc_year <= 68)
3950Sstevel@tonic-gate 		tod.tod_year += 100;
3960Sstevel@tonic-gate 	tod.tod_month	= rtc.rtc_mon;
3970Sstevel@tonic-gate 	tod.tod_day	= rtc.rtc_dom;
3980Sstevel@tonic-gate 	tod.tod_dow	= rtc.rtc_dow;
3990Sstevel@tonic-gate 	tod.tod_hour	= rtc.rtc_hrs;
4000Sstevel@tonic-gate 	tod.tod_min	= rtc.rtc_min;
4010Sstevel@tonic-gate 	tod.tod_sec	= rtc.rtc_sec;
4020Sstevel@tonic-gate 
403*11752STrevor.Thompson@Sun.COM 	/* read was successful so ensure failure flag is clear */
404*11752STrevor.Thompson@Sun.COM 	tod_status_clear(TOD_GET_FAILED);
405*11752STrevor.Thompson@Sun.COM 
4060Sstevel@tonic-gate 	ts.tv_sec = tod_to_utc(tod);
4070Sstevel@tonic-gate 	ts.tv_nsec = 0;
4080Sstevel@tonic-gate 	return (ts);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate /*
4120Sstevel@tonic-gate  * Program DS1307 with the specified time.
4130Sstevel@tonic-gate  * Must be called with tod_lock held. The TOD
4140Sstevel@tonic-gate  * chip supports date from 1969-2068 only. We must
4150Sstevel@tonic-gate  * reject requests to set date below 2000.
4160Sstevel@tonic-gate  */
4170Sstevel@tonic-gate static void
todds1307_set(timestruc_t ts)4180Sstevel@tonic-gate todds1307_set(timestruc_t ts)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate 	struct rtc_t	rtc;
4210Sstevel@tonic-gate 	todinfo_t	tod = utc_to_tod(ts.tv_sec);
4220Sstevel@tonic-gate 	int		year;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	/*
4280Sstevel@tonic-gate 	 * Year is base 1900, valid year range 1969-2068
4290Sstevel@tonic-gate 	 */
4300Sstevel@tonic-gate 	if ((tod.tod_year < 69) || (tod.tod_year > 168))
4310Sstevel@tonic-gate 		return;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	year = tod.tod_year;
4340Sstevel@tonic-gate 	if (year >= 100)
4350Sstevel@tonic-gate 		year -= 100;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	rtc.rtc_year	= int2bcd(year);
4380Sstevel@tonic-gate 	rtc.rtc_mon	= int2bcd(tod.tod_month);
4390Sstevel@tonic-gate 	rtc.rtc_dom	= int2bcd(tod.tod_day);
4400Sstevel@tonic-gate 	rtc.rtc_dow	= int2bcd(tod.tod_dow);
4410Sstevel@tonic-gate 	rtc.rtc_hrs	= int2bcd(tod.tod_hour);
4420Sstevel@tonic-gate 	rtc.rtc_min	= int2bcd(tod.tod_min);
4430Sstevel@tonic-gate 	rtc.rtc_sec	= int2bcd(tod.tod_sec);
4440Sstevel@tonic-gate 
44511311SSurya.Prakki@Sun.COM 	(void) todds1307_write_rtc(&rtc);
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate /* ARGSUSED */
4490Sstevel@tonic-gate static void
todds1307_set_power_alarm(timestruc_t ts)4500Sstevel@tonic-gate todds1307_set_power_alarm(timestruc_t ts)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate /* ARGSUSED */
4560Sstevel@tonic-gate static void
todds1307_clear_power_alarm(void)4570Sstevel@tonic-gate todds1307_clear_power_alarm(void)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /* ARGSUSED */
4630Sstevel@tonic-gate static uint_t
todds1307_set_watchdog_timer(uint_t timeoutval)4640Sstevel@tonic-gate todds1307_set_watchdog_timer(uint_t timeoutval)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4670Sstevel@tonic-gate 	return (0);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate /* ARGSUSED */
4710Sstevel@tonic-gate static uint_t
todds1307_clear_watchdog_timer(void)4720Sstevel@tonic-gate todds1307_clear_watchdog_timer(void)
4730Sstevel@tonic-gate {
4740Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
4750Sstevel@tonic-gate 	return (0);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate /* ********************** Local functions ***************************** */
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate static char tod_read[7] = {-1, -1, -1, -1, -1, -1, -1};
4810Sstevel@tonic-gate static int
todds1307_read_rtc(struct rtc_t * rtc)4820Sstevel@tonic-gate todds1307_read_rtc(struct rtc_t *rtc)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	static	ds1307_state_t	*statep = NULL;
4850Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
4860Sstevel@tonic-gate 	int i2c_cmd_status = I2C_FAILURE;
4870Sstevel@tonic-gate 	int counter = 4;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if (!todds1307_attach_done) {
4900Sstevel@tonic-gate 		return (todds1307_prom_getdate(rtc));
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1307_statep, instance);
4940Sstevel@tonic-gate 	if (statep == NULL) {
4950Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1307: ddi_get_soft_state failed");
4960Sstevel@tonic-gate 		return (DDI_FAILURE);
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	mutex_enter(&todds1307_rd_lock);
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	/*
5020Sstevel@tonic-gate 	 * Allocate 1 byte for write buffer and 7 bytes for read buffer to
5030Sstevel@tonic-gate 	 * to accomodate sec, min, hrs, dayOfWeek, dayOfMonth, year
5040Sstevel@tonic-gate 	 */
5050Sstevel@tonic-gate 	if ((i2c_transfer_alloc(statep->ds1307_i2c_hdl, &i2c_tp, 1,
5065107Seota 	    7, I2C_SLEEP)) != I2C_SUCCESS) {
5070Sstevel@tonic-gate 		mutex_exit(&todds1307_rd_lock);
5080Sstevel@tonic-gate 		return (DDI_FAILURE);
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	do {
5120Sstevel@tonic-gate 		i2c_tp->i2c_version = I2C_XFER_REV;
5130Sstevel@tonic-gate 		i2c_tp->i2c_flags = I2C_WR_RD;
5140Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[0] = (uchar_t)0x00; /* Start from reg 0x00 */
5150Sstevel@tonic-gate 		i2c_tp->i2c_wlen = 1;	/* Write one byte address */
5160Sstevel@tonic-gate 		i2c_tp->i2c_rlen = 7;	/* Read 7 regs */
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		if ((i2c_cmd_status = i2c_transfer(statep->ds1307_i2c_hdl,
5195107Seota 		    i2c_tp)) != I2C_SUCCESS) {
5200Sstevel@tonic-gate 			drv_usecwait(I2C_DELAY);
5210Sstevel@tonic-gate 			goto done;
5220Sstevel@tonic-gate 		}
5235107Seota 		/* for first read, need to get valid data */
5245107Seota 		while (tod_read[0] == -1 && counter > 0) {
5255107Seota 		/* move data to static buffer */
5260Sstevel@tonic-gate 		bcopy(i2c_tp->i2c_rbuf, tod_read, 7);
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 		/* now read again */
5290Sstevel@tonic-gate 		/* Start reading reg from 0x00 */
5300Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[0] = (uchar_t)0x00;
5310Sstevel@tonic-gate 		i2c_tp->i2c_wlen = 1;	/* Write one byte address */
5320Sstevel@tonic-gate 		i2c_tp->i2c_rlen = 7;	/* Read 7 regs */
5330Sstevel@tonic-gate 		if ((i2c_cmd_status = i2c_transfer(statep->ds1307_i2c_hdl,
5345107Seota 		    i2c_tp)) != I2C_SUCCESS) {
5355107Seota 			drv_usecwait(I2C_DELAY);
5365107Seota 			goto done;
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 		/* if they are not the same, then read again */
5390Sstevel@tonic-gate 		if (bcmp(tod_read, i2c_tp->i2c_rbuf, 7) != 0) {
5405107Seota 			tod_read[0] = -1;
5415107Seota 			counter--;
5420Sstevel@tonic-gate 		}
5435107Seota 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	} while (i2c_tp->i2c_rbuf[0] == 0x59 &&
5460Sstevel@tonic-gate 	    /* if seconds register is 0x59 (BCD), add data should match */
5470Sstevel@tonic-gate 	    bcmp(&tod_read[1], &i2c_tp->i2c_rbuf[1], 6) != 0 &&
5480Sstevel@tonic-gate 	    counter-- > 0);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	if (counter < 0)
5515107Seota 		cmn_err(CE_WARN, "i2ctod: TOD Chip failed ??");
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	/* move data to static buffer */
5540Sstevel@tonic-gate 	bcopy(i2c_tp->i2c_rbuf, tod_read, 7);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	rtc->rtc_year	= bcd2int(i2c_tp->i2c_rbuf[6]);
5580Sstevel@tonic-gate 	rtc->rtc_mon	= bcd2int(i2c_tp->i2c_rbuf[5]);
5590Sstevel@tonic-gate 	rtc->rtc_dom	= bcd2int(i2c_tp->i2c_rbuf[4]);
5600Sstevel@tonic-gate 	rtc->rtc_dow	= bcd2int(i2c_tp->i2c_rbuf[3]);
5610Sstevel@tonic-gate 	rtc->rtc_hrs	= bcd2int(i2c_tp->i2c_rbuf[2]);
5620Sstevel@tonic-gate 	rtc->rtc_min	= bcd2int(i2c_tp->i2c_rbuf[1]);
5630Sstevel@tonic-gate 	rtc->rtc_sec	= bcd2int(i2c_tp->i2c_rbuf[0]);
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate done:
5660Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	mutex_exit(&todds1307_rd_lock);
5690Sstevel@tonic-gate 	return (i2c_cmd_status);
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate static int
todds1307_write_rtc(struct rtc_t * rtc)5740Sstevel@tonic-gate todds1307_write_rtc(struct rtc_t *rtc)
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate 	ds1307_state_t	*statep = NULL;
5770Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
5780Sstevel@tonic-gate 	int i2c_cmd_status = I2C_SUCCESS;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	if (!todds1307_attach_done) {
5820Sstevel@tonic-gate 		return (todds1307_prom_setdate(rtc));
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1307_statep, instance);
5860Sstevel@tonic-gate 	if (statep == NULL) {
5870Sstevel@tonic-gate 		return (DDI_FAILURE);
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	if ((i2c_cmd_status = i2c_transfer_alloc(statep->ds1307_i2c_hdl,
5915107Seota 	    &i2c_tp, 8, 0, I2C_SLEEP)) != I2C_SUCCESS) {
5920Sstevel@tonic-gate 		return (i2c_cmd_status);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
5960Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
5970Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)0x00;
5980Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = rtc->rtc_sec;
5990Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[2] = rtc->rtc_min;
6000Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[3] = rtc->rtc_hrs;
6010Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[4] = rtc->rtc_dow;
6020Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[5] = rtc->rtc_dom;
6030Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[6] = rtc->rtc_mon;
6040Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[7] = rtc->rtc_year;
6050Sstevel@tonic-gate 	i2c_tp->i2c_wlen = 8;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	if ((i2c_cmd_status = i2c_transfer(statep->ds1307_i2c_hdl,
6085107Seota 	    i2c_tp)) != I2C_SUCCESS) {
6090Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
6100Sstevel@tonic-gate 		/* delay(drv_usectohz(I2C_DELAY)); */
6110Sstevel@tonic-gate 		drv_usecwait(I2C_DELAY);
6120Sstevel@tonic-gate 		return (i2c_cmd_status);
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	tod_read[0] = -1;  /* invalidate saved data from read routine */
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	return (i2c_cmd_status);
6200Sstevel@tonic-gate }
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /*ARGSUSED*/
6240Sstevel@tonic-gate static int
todds1307_getinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)6250Sstevel@tonic-gate todds1307_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
6260Sstevel@tonic-gate     void **result)
6270Sstevel@tonic-gate {
6280Sstevel@tonic-gate 	ds1307_state_t *softsp;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	if (instance == -1) {
6310Sstevel@tonic-gate 		return (DDI_FAILURE);
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	switch (infocmd) {
6350Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
6360Sstevel@tonic-gate 		if ((softsp = ddi_get_soft_state(ds1307_statep, instance))
6370Sstevel@tonic-gate 		    == NULL)
6380Sstevel@tonic-gate 			return (DDI_FAILURE);
6390Sstevel@tonic-gate 		*result = (void *)softsp->dip;
6400Sstevel@tonic-gate 		return (DDI_SUCCESS);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
643434Swnelson 		*result = (void *)(uintptr_t)instance;
6440Sstevel@tonic-gate 		return (DDI_SUCCESS);
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	default:
6470Sstevel@tonic-gate 		return (DDI_FAILURE);
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate  * Conversion functions
6530Sstevel@tonic-gate  */
6540Sstevel@tonic-gate static unsigned char
int2bcd(int num)6550Sstevel@tonic-gate int2bcd(int num) {
6560Sstevel@tonic-gate 	return (((num / 10) << 4)	/* tens BCD digit in high four bits */
6570Sstevel@tonic-gate 	+ (num % 10));		/* units digit goes in low four bits */
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate static int
bcd2int(unsigned char num)6610Sstevel@tonic-gate bcd2int(unsigned char num) {
6620Sstevel@tonic-gate 	return (((num >> 4) * 10)	/* 10 times high-order four bits */
6630Sstevel@tonic-gate 	+ (num & 0x0f));		/* plus low-order four bits */
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate /*
6670Sstevel@tonic-gate  * Finds the device node with device_type "rtc" and opens it to
6680Sstevel@tonic-gate  * execute the get-time method
6690Sstevel@tonic-gate  */
6700Sstevel@tonic-gate static int
todds1307_setup_prom()6710Sstevel@tonic-gate todds1307_setup_prom()
6720Sstevel@tonic-gate {
673789Sahrens 	pnode_t todnode;
6740Sstevel@tonic-gate 	char tod1307_devpath[MAXNAMELEN];
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	if ((todnode = prom_findnode_bydevtype(prom_rootnode(),
6775107Seota 	    DS1307_DEVICE_TYPE)) == OBP_NONODE)
6780Sstevel@tonic-gate 		return (DDI_FAILURE);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	/*
6810Sstevel@tonic-gate 	 * We now have the phandle of the rtc node, we need to open the
6820Sstevel@tonic-gate 	 * node and get the ihandle
6830Sstevel@tonic-gate 	 */
6840Sstevel@tonic-gate 	if (prom_phandle_to_path(todnode, tod1307_devpath,
6855107Seota 	    sizeof (tod1307_devpath)) < 0) {
6860Sstevel@tonic-gate 		cmn_err(CE_WARN, "prom_phandle_to_path failed");
6870Sstevel@tonic-gate 		return (DDI_FAILURE);
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	/*
6910Sstevel@tonic-gate 	 * Now open the node and store it's ihandle
6920Sstevel@tonic-gate 	 */
6930Sstevel@tonic-gate 	if ((todds1307_ihandle = prom_open(tod1307_devpath)) == NULL) {
6940Sstevel@tonic-gate 		cmn_err(CE_WARN, "prom_open failed");
6950Sstevel@tonic-gate 		return (DDI_FAILURE);
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	return (DDI_SUCCESS);
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate /*
7020Sstevel@tonic-gate  * Closes the prom interface
7030Sstevel@tonic-gate  */
7040Sstevel@tonic-gate static void
todds1307_rele_prom()7050Sstevel@tonic-gate todds1307_rele_prom()
7060Sstevel@tonic-gate {
7070Sstevel@tonic-gate 	(void) prom_close(todds1307_ihandle);
7080Sstevel@tonic-gate }
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate /*
7110Sstevel@tonic-gate  * Read the date using "get-time" method in rtc node
7120Sstevel@tonic-gate  * PROM returns 1969-1999 when reading 69-99 and
7130Sstevel@tonic-gate  * 2000-2068 when reading 00-68
7140Sstevel@tonic-gate  */
7150Sstevel@tonic-gate static int
todds1307_prom_getdate(struct rtc_t * rtc)7160Sstevel@tonic-gate todds1307_prom_getdate(struct rtc_t *rtc)
7170Sstevel@tonic-gate {
7180Sstevel@tonic-gate 	int year;
7190Sstevel@tonic-gate 	cell_t ci[12];
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");  /* Service name */
7220Sstevel@tonic-gate 	ci[1] = 2; /* # of arguments */
7230Sstevel@tonic-gate 	ci[2] = 7; /* # of result cells */
7240Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("get-time");
7250Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(todds1307_ihandle);
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	promif_preprom();
7280Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
7290Sstevel@tonic-gate 	promif_postprom();
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	year 		= p1275_cell2int(ci[6]);
7320Sstevel@tonic-gate 	rtc->rtc_mon	= p1275_cell2int(ci[7]);
7330Sstevel@tonic-gate 	rtc->rtc_dom	= p1275_cell2int(ci[8]);
7340Sstevel@tonic-gate 	rtc->rtc_dow	= 0;
7350Sstevel@tonic-gate 	rtc->rtc_hrs	= p1275_cell2int(ci[9]);
7360Sstevel@tonic-gate 	rtc->rtc_min	= p1275_cell2int(ci[10]);
7370Sstevel@tonic-gate 	rtc->rtc_sec	= p1275_cell2int(ci[11]);
7380Sstevel@tonic-gate 	if (year >= 2000)
7390Sstevel@tonic-gate 		year -= 2000;
7400Sstevel@tonic-gate 	else
7410Sstevel@tonic-gate 		year -= 1900;
7420Sstevel@tonic-gate 	rtc->rtc_year	= year;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	return (DDI_SUCCESS);
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate /*
7480Sstevel@tonic-gate  * Read the date using "set-time" method in rtc node
7490Sstevel@tonic-gate  * For values 00 - 68, write 2000-2068, and for 69-99,
7500Sstevel@tonic-gate  * write 1969-1999
7510Sstevel@tonic-gate  */
7520Sstevel@tonic-gate static int
todds1307_prom_setdate(struct rtc_t * rtc)7530Sstevel@tonic-gate todds1307_prom_setdate(struct rtc_t *rtc)
7540Sstevel@tonic-gate {
7550Sstevel@tonic-gate 	int year;
7560Sstevel@tonic-gate 	cell_t ci[12];
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	year = rtc->rtc_year;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	if ((year < 0) || (year > 99))
7610Sstevel@tonic-gate 		return (DDI_FAILURE);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	if (year <= 68)
7640Sstevel@tonic-gate 		year = rtc->rtc_year + 2000;
7650Sstevel@tonic-gate 	else
7660Sstevel@tonic-gate 		year = rtc->rtc_year + 1900;
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");  /* Service name */
7690Sstevel@tonic-gate 	ci[1] = 8; /* # of arguments */
7700Sstevel@tonic-gate 	ci[2] = 0; /* # of result cells */
7710Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("set-time");
7720Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(todds1307_ihandle);
7730Sstevel@tonic-gate 	ci[5] = p1275_int2cell(year);
7740Sstevel@tonic-gate 	ci[6] = p1275_int2cell(rtc->rtc_mon);
7750Sstevel@tonic-gate 	ci[7] = p1275_int2cell(rtc->rtc_dom);
7760Sstevel@tonic-gate 	ci[8] = p1275_int2cell(rtc->rtc_hrs);
7770Sstevel@tonic-gate 	ci[9] = p1275_int2cell(rtc->rtc_min);
7780Sstevel@tonic-gate 	ci[10] = p1275_int2cell(rtc->rtc_sec);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	promif_preprom();
7810Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
7820Sstevel@tonic-gate 	promif_postprom();
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	return (DDI_SUCCESS);
7850Sstevel@tonic-gate }
786