xref: /onnv-gate/usr/src/uts/sun4u/io/todds1337.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/conf.h>
31*0Sstevel@tonic-gate #include <sys/devops.h>
32*0Sstevel@tonic-gate #include <sys/kmem.h>
33*0Sstevel@tonic-gate #include <sys/open.h>
34*0Sstevel@tonic-gate #include <sys/file.h>
35*0Sstevel@tonic-gate #include <sys/note.h>
36*0Sstevel@tonic-gate #include <sys/ddi.h>
37*0Sstevel@tonic-gate #include <sys/sunddi.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include <sys/modctl.h>
40*0Sstevel@tonic-gate #include <sys/stat.h>
41*0Sstevel@tonic-gate #include <sys/clock.h>
42*0Sstevel@tonic-gate #include <sys/reboot.h>
43*0Sstevel@tonic-gate #include <sys/machsystm.h>
44*0Sstevel@tonic-gate #include <sys/poll.h>
45*0Sstevel@tonic-gate #include <sys/pbio.h>
46*0Sstevel@tonic-gate #include <sys/sysmacros.h>
47*0Sstevel@tonic-gate #include <sys/cyclic.h>
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /* Added for prom interface */
50*0Sstevel@tonic-gate #include <sys/promif.h>
51*0Sstevel@tonic-gate #include <sys/promimpl.h>
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #include <sys/i2c/misc/i2c_svc.h>
54*0Sstevel@tonic-gate #include <sys/todds1337.h>
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #define	DS1337_DEVICE_TYPE	"rtc"
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate /*
59*0Sstevel@tonic-gate  * Driver entry routines
60*0Sstevel@tonic-gate  */
61*0Sstevel@tonic-gate static int todds1337_attach(dev_info_t *, ddi_attach_cmd_t);
62*0Sstevel@tonic-gate static int todds1337_detach(dev_info_t *, ddi_detach_cmd_t);
63*0Sstevel@tonic-gate static int todds1337_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /*
66*0Sstevel@tonic-gate  * tod_ops entry routines
67*0Sstevel@tonic-gate  */
68*0Sstevel@tonic-gate static timestruc_t	todds1337_get(void);
69*0Sstevel@tonic-gate static void		todds1337_set(timestruc_t);
70*0Sstevel@tonic-gate static uint_t		todds1337_set_watchdog_timer(uint_t);
71*0Sstevel@tonic-gate static uint_t		todds1337_clear_watchdog_timer(void);
72*0Sstevel@tonic-gate static void		todds1337_set_power_alarm(timestruc_t);
73*0Sstevel@tonic-gate static void		todds1337_clear_power_alarm(void);
74*0Sstevel@tonic-gate static int		todds1337_setup_prom();
75*0Sstevel@tonic-gate static void		todds1337_rele_prom();
76*0Sstevel@tonic-gate static int		todds1337_prom_getdate(struct rtc_t *rtc);
77*0Sstevel@tonic-gate static int		todds1337_prom_setdate(struct rtc_t *rtc);
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate /*
80*0Sstevel@tonic-gate  * Local functions
81*0Sstevel@tonic-gate  */
82*0Sstevel@tonic-gate static int		todds1337_read_rtc(struct rtc_t *);
83*0Sstevel@tonic-gate static int		todds1337_write_rtc(struct rtc_t *);
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate /* Anchor for soft state structure */
86*0Sstevel@tonic-gate static void	*ds1337_statep;
87*0Sstevel@tonic-gate static int	instance = -1;
88*0Sstevel@tonic-gate static int	todds1337_attach_done = 0;
89*0Sstevel@tonic-gate static kmutex_t	todds1337_rd_lock;
90*0Sstevel@tonic-gate static kmutex_t	todds1337_alarm_lock;
91*0Sstevel@tonic-gate static ihandle_t todds1337_ihandle = 0;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate /* one second time out */
94*0Sstevel@tonic-gate #define	I2C_CYCLIC_TIMEOUT	1000000000
95*0Sstevel@tonic-gate uint_t i2c_cyclic_timeout = I2C_CYCLIC_TIMEOUT;
96*0Sstevel@tonic-gate static int sync_clock_once = 1;
97*0Sstevel@tonic-gate static 	struct	rtc_t	 soft_rtc;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate /*
100*0Sstevel@tonic-gate  * cp_ops structure
101*0Sstevel@tonic-gate  */
102*0Sstevel@tonic-gate static struct cb_ops ds1337_cbops = {
103*0Sstevel@tonic-gate 	nodev,				/* open */
104*0Sstevel@tonic-gate 	nodev,				/* close */
105*0Sstevel@tonic-gate 	nodev,				/* strategy */
106*0Sstevel@tonic-gate 	nodev,				/* print */
107*0Sstevel@tonic-gate 	nodev,				/* dump */
108*0Sstevel@tonic-gate 	nodev,				/* read */
109*0Sstevel@tonic-gate 	nodev,				/* write */
110*0Sstevel@tonic-gate 	nodev,				/* ioctl */
111*0Sstevel@tonic-gate 	nodev,				/* devmap */
112*0Sstevel@tonic-gate 	nodev,				/* mmap */
113*0Sstevel@tonic-gate 	nodev,				/* segmap */
114*0Sstevel@tonic-gate 	NULL,				/* poll */
115*0Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
116*0Sstevel@tonic-gate 	NULL,				/* streamtab */
117*0Sstevel@tonic-gate 	D_NEW | D_MP,			/* Driver compatibility flag */
118*0Sstevel@tonic-gate 	CB_REV,				/* rev */
119*0Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
120*0Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
121*0Sstevel@tonic-gate };
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate /*
124*0Sstevel@tonic-gate  * dev_ops structure
125*0Sstevel@tonic-gate  */
126*0Sstevel@tonic-gate static struct dev_ops ds1337_ops = {
127*0Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
128*0Sstevel@tonic-gate 	0,			/* refcnt - reference cnt always set to 0 */
129*0Sstevel@tonic-gate 	todds1337_getinfo,	/* getinfo - Maybe requred */
130*0Sstevel@tonic-gate 	nulldev,		/* identify */
131*0Sstevel@tonic-gate 	nulldev,		/* probe */
132*0Sstevel@tonic-gate 	todds1337_attach,	/* attach */
133*0Sstevel@tonic-gate 	todds1337_detach,	/* detach */
134*0Sstevel@tonic-gate 	nodev,			/* reset */
135*0Sstevel@tonic-gate 	&ds1337_cbops,		/* cb_ops - ds1337 does not need this(?) */
136*0Sstevel@tonic-gate 	NULL
137*0Sstevel@tonic-gate };
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate static struct modldrv todds1337_modldrv = {
140*0Sstevel@tonic-gate 	&mod_driverops,		/* Type of module. This one is a driver */
141*0Sstevel@tonic-gate 	"tod driver for DS1337 %I%",   /* Name of the module. */
142*0Sstevel@tonic-gate 	&ds1337_ops,			/* Pointer to dev_ops */
143*0Sstevel@tonic-gate };
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate /*
146*0Sstevel@tonic-gate  * Module linkage structure
147*0Sstevel@tonic-gate  */
148*0Sstevel@tonic-gate static struct modlinkage todds1337_modlinkage = {
149*0Sstevel@tonic-gate 	MODREV_1,
150*0Sstevel@tonic-gate 	&todds1337_modldrv,
151*0Sstevel@tonic-gate 	0
152*0Sstevel@tonic-gate };
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate int
155*0Sstevel@tonic-gate _init(void)
156*0Sstevel@tonic-gate {
157*0Sstevel@tonic-gate 	int error;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	if (strcmp(tod_module_name, "todds1337") == 0) {
160*0Sstevel@tonic-gate 		if ((error = ddi_soft_state_init(&ds1337_statep,
161*0Sstevel@tonic-gate 			sizeof (ds1337_state_t), 0)) != DDI_SUCCESS) {
162*0Sstevel@tonic-gate 			return (error);
163*0Sstevel@tonic-gate 		}
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 		tod_ops.tod_get = todds1337_get;
166*0Sstevel@tonic-gate 		tod_ops.tod_set = todds1337_set;
167*0Sstevel@tonic-gate 		tod_ops.tod_set_watchdog_timer = todds1337_set_watchdog_timer;
168*0Sstevel@tonic-gate 		tod_ops.tod_clear_watchdog_timer =
169*0Sstevel@tonic-gate 		    todds1337_clear_watchdog_timer;
170*0Sstevel@tonic-gate 		tod_ops.tod_set_power_alarm = todds1337_set_power_alarm;
171*0Sstevel@tonic-gate 		tod_ops.tod_clear_power_alarm = todds1337_clear_power_alarm;
172*0Sstevel@tonic-gate 	}
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	(void) todds1337_setup_prom();
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	/*
177*0Sstevel@tonic-gate 	 * Install the module
178*0Sstevel@tonic-gate 	 */
179*0Sstevel@tonic-gate 	if ((error = mod_install(&todds1337_modlinkage)) != 0) {
180*0Sstevel@tonic-gate 		ddi_soft_state_fini(&ds1337_statep);
181*0Sstevel@tonic-gate 		return (error);
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 	mutex_init(&todds1337_rd_lock, NULL, MUTEX_DEFAULT, NULL);
184*0Sstevel@tonic-gate 	mutex_init(&todds1337_alarm_lock, NULL, MUTEX_DEFAULT, NULL);
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	return (0);
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate int
190*0Sstevel@tonic-gate _fini(void)
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate 	int error = 0;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	if (strcmp(tod_module_name, "todds1337") == 0) {
195*0Sstevel@tonic-gate 		error = EBUSY;
196*0Sstevel@tonic-gate 	} else {
197*0Sstevel@tonic-gate 		if ((error = mod_remove(&todds1337_modlinkage)) == 0) {
198*0Sstevel@tonic-gate 			ddi_soft_state_fini(&ds1337_statep);
199*0Sstevel@tonic-gate 			mutex_destroy(&todds1337_rd_lock);
200*0Sstevel@tonic-gate 			mutex_destroy(&todds1337_alarm_lock);
201*0Sstevel@tonic-gate 			todds1337_rele_prom();
202*0Sstevel@tonic-gate 		}
203*0Sstevel@tonic-gate 	}
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	return (error);
206*0Sstevel@tonic-gate }
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate int
209*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
210*0Sstevel@tonic-gate {
211*0Sstevel@tonic-gate 	return (mod_info(&todds1337_modlinkage, modinfop));
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate /*
215*0Sstevel@tonic-gate  * cyclical call to get tod.
216*0Sstevel@tonic-gate  */
217*0Sstevel@tonic-gate static void
218*0Sstevel@tonic-gate todds1337_cyclic(void *arg)
219*0Sstevel@tonic-gate {
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	(void) todds1337_read_rtc((struct rtc_t *)arg);
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate /*
226*0Sstevel@tonic-gate  * register ds1337 client device with i2c services, and
227*0Sstevel@tonic-gate  * allocate & initialize soft state structure.
228*0Sstevel@tonic-gate  */
229*0Sstevel@tonic-gate static int
230*0Sstevel@tonic-gate todds1337_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
231*0Sstevel@tonic-gate {
232*0Sstevel@tonic-gate 	static ds1337_state_t	*statep = NULL;
233*0Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
234*0Sstevel@tonic-gate 	uint8_t tempVal = (uint8_t)0;
235*0Sstevel@tonic-gate 	cyc_handler_t cychand;
236*0Sstevel@tonic-gate 	cyc_time_t  cyctime;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	switch (cmd) {
239*0Sstevel@tonic-gate 	case DDI_ATTACH:
240*0Sstevel@tonic-gate 		break;
241*0Sstevel@tonic-gate 	case DDI_RESUME:
242*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
243*0Sstevel@tonic-gate 	default:
244*0Sstevel@tonic-gate 		return (DDI_FAILURE);
245*0Sstevel@tonic-gate 	}
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	if (instance != -1) {
248*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_attach: wrong instance");
249*0Sstevel@tonic-gate 		return (DDI_FAILURE);
250*0Sstevel@tonic-gate 	}
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	/*
255*0Sstevel@tonic-gate 	 * Allocate soft state structure
256*0Sstevel@tonic-gate 	 */
257*0Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(ds1337_statep, instance) != DDI_SUCCESS) {
258*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_attach: cannot allocate soft "
259*0Sstevel@tonic-gate 		    "state");
260*0Sstevel@tonic-gate 		instance = -1;
261*0Sstevel@tonic-gate 		return (DDI_FAILURE);
262*0Sstevel@tonic-gate 	}
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1337_statep, instance);
265*0Sstevel@tonic-gate 	if (statep == NULL) {
266*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_attach: cannot acquire soft "
267*0Sstevel@tonic-gate 		    "state");
268*0Sstevel@tonic-gate 		instance = -1;
269*0Sstevel@tonic-gate 		return (DDI_FAILURE);
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	statep->dip = dip;
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	if (i2c_client_register(dip, &statep->ds1337_i2c_hdl) != I2C_SUCCESS) {
275*0Sstevel@tonic-gate 		ddi_soft_state_free(ds1337_statep, instance);
276*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_attach: cannot register i2c "
277*0Sstevel@tonic-gate 		    "client");
278*0Sstevel@tonic-gate 		instance = -1;
279*0Sstevel@tonic-gate 		return (DDI_FAILURE);
280*0Sstevel@tonic-gate 	}
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	/* check and initialize the oscillator */
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
285*0Sstevel@tonic-gate 	    &i2c_tp, 1, 1, I2C_SLEEP);
286*0Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
287*0Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR_RD;
288*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */
289*0Sstevel@tonic-gate 	i2c_tp->i2c_wlen = 1;
290*0Sstevel@tonic-gate 	i2c_tp->i2c_rlen = 1;
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
293*0Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
294*0Sstevel@tonic-gate 		i2c_client_unregister(statep->ds1337_i2c_hdl);
295*0Sstevel@tonic-gate 		ddi_soft_state_free(ds1337_statep, instance);
296*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_attach: failed to read DS1337 "
297*0Sstevel@tonic-gate 		    "status register");
298*0Sstevel@tonic-gate 		instance = -1;
299*0Sstevel@tonic-gate 		return (DDI_FAILURE);
300*0Sstevel@tonic-gate 	}
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	tempVal = i2c_tp->i2c_rbuf[0];
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	/*
307*0Sstevel@tonic-gate 	 * Check Oscillator and initialize chip if OBP failed to do it
308*0Sstevel@tonic-gate 	 */
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	if (tempVal & RTC_CTL_EOSC) {
311*0Sstevel@tonic-gate 		(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp,
312*0Sstevel@tonic-gate 		    2, 0, I2C_SLEEP);
313*0Sstevel@tonic-gate 		i2c_tp->i2c_version = I2C_XFER_REV;
314*0Sstevel@tonic-gate 		i2c_tp->i2c_flags = I2C_WR;
315*0Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[0] = RTC_CTL; /* Write Control register */
316*0Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[1] = (uchar_t)(RTC_CTL_RS2 | RTC_CTL_RS1 |
317*0Sstevel@tonic-gate 		    RTC_CTL_INTCN);
318*0Sstevel@tonic-gate 		if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp))
319*0Sstevel@tonic-gate 		    != I2C_SUCCESS) {
320*0Sstevel@tonic-gate 			(void) i2c_transfer_free(statep->ds1337_i2c_hdl,
321*0Sstevel@tonic-gate 			    i2c_tp);
322*0Sstevel@tonic-gate 			i2c_client_unregister(statep->ds1337_i2c_hdl);
323*0Sstevel@tonic-gate 			ddi_soft_state_free(ds1337_statep, instance);
324*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "todds1337_attach: failed to write "
325*0Sstevel@tonic-gate 			    "DS1337 control register");
326*0Sstevel@tonic-gate 			instance = -1;
327*0Sstevel@tonic-gate 			return (DDI_FAILURE);
328*0Sstevel@tonic-gate 		}
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 		/*
333*0Sstevel@tonic-gate 		 * Now reset the OSF flag in the Status register
334*0Sstevel@tonic-gate 		 */
335*0Sstevel@tonic-gate 		(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp,
336*0Sstevel@tonic-gate 		    2, 0, I2C_SLEEP);
337*0Sstevel@tonic-gate 		i2c_tp->i2c_version = I2C_XFER_REV;
338*0Sstevel@tonic-gate 		i2c_tp->i2c_flags = I2C_WR;
339*0Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[0] = RTC_STATUS;
340*0Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[1] = (uchar_t)0;
341*0Sstevel@tonic-gate 		if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp))
342*0Sstevel@tonic-gate 		    != I2C_SUCCESS) {
343*0Sstevel@tonic-gate 			(void) i2c_transfer_free(statep->ds1337_i2c_hdl,
344*0Sstevel@tonic-gate 			    i2c_tp);
345*0Sstevel@tonic-gate 			i2c_client_unregister(statep->ds1337_i2c_hdl);
346*0Sstevel@tonic-gate 			ddi_soft_state_free(ds1337_statep, instance);
347*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "todds1337_attach: failed to write "
348*0Sstevel@tonic-gate 			    "DS1337 status register");
349*0Sstevel@tonic-gate 			instance = -1;
350*0Sstevel@tonic-gate 			return (DDI_FAILURE);
351*0Sstevel@tonic-gate 		}
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
354*0Sstevel@tonic-gate 	}
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 	/* Create a cyclic handler to read TOD */
357*0Sstevel@tonic-gate 	cychand.cyh_func = todds1337_cyclic;
358*0Sstevel@tonic-gate 	cychand.cyh_arg = &soft_rtc;
359*0Sstevel@tonic-gate 	cychand.cyh_level = CY_LOW_LEVEL;
360*0Sstevel@tonic-gate 	cyctime.cyt_when = 0;
361*0Sstevel@tonic-gate 	cyctime.cyt_interval = i2c_cyclic_timeout;
362*0Sstevel@tonic-gate 	ASSERT(statep->cycid == CYCLIC_NONE);
363*0Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
364*0Sstevel@tonic-gate 	statep->cycid = cyclic_add(&cychand, &cyctime);
365*0Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
366*0Sstevel@tonic-gate 	ASSERT(statep->cycid != CYCLIC_NONE);
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	statep->state = TOD_ATTACHED;
369*0Sstevel@tonic-gate 	todds1337_attach_done = 1;
370*0Sstevel@tonic-gate 	ddi_report_dev(dip);
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
373*0Sstevel@tonic-gate }
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate /*ARGSUSED*/
376*0Sstevel@tonic-gate static int
377*0Sstevel@tonic-gate todds1337_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
378*0Sstevel@tonic-gate {
379*0Sstevel@tonic-gate 	/*
380*0Sstevel@tonic-gate 	 * Once attached, do not allow detach because the system constantly
381*0Sstevel@tonic-gate 	 * calling todds1337_get() to get the time.  If the driver is detached
382*0Sstevel@tonic-gate 	 * and the system try to get the time, the system will have memory
383*0Sstevel@tonic-gate 	 * problem.
384*0Sstevel@tonic-gate 	 *
385*0Sstevel@tonic-gate 	 */
386*0Sstevel@tonic-gate 	switch (cmd) {
387*0Sstevel@tonic-gate 	case DDI_SUSPEND:
388*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
389*0Sstevel@tonic-gate 	default:
390*0Sstevel@tonic-gate 		return (DDI_FAILURE);
391*0Sstevel@tonic-gate 	}
392*0Sstevel@tonic-gate }
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate /* *********************** tod_ops entry points ******************** */
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate /*
397*0Sstevel@tonic-gate  * Read the current time from the DS1337 chip and convert to UNIX form.
398*0Sstevel@tonic-gate  * Should be called with tod_clock held.
399*0Sstevel@tonic-gate  */
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate static timestruc_t
402*0Sstevel@tonic-gate todds1337_get(void)
403*0Sstevel@tonic-gate {
404*0Sstevel@tonic-gate 	timestruc_t	ts;
405*0Sstevel@tonic-gate 	todinfo_t	tod;
406*0Sstevel@tonic-gate 	struct	rtc_t	rtc;
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	if (sync_clock_once) {
411*0Sstevel@tonic-gate 		(void) todds1337_read_rtc(&soft_rtc);
412*0Sstevel@tonic-gate 		sync_clock_once = 0;
413*0Sstevel@tonic-gate 	} else {
414*0Sstevel@tonic-gate 		tod_fault_reset();
415*0Sstevel@tonic-gate 		return (hrestime);
416*0Sstevel@tonic-gate 	}
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 	bcopy(&soft_rtc, &rtc, sizeof (rtc));
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	/*
421*0Sstevel@tonic-gate 	 * 00 - 68 = 2000 thru 2068
422*0Sstevel@tonic-gate 	 * 69-99 = 1969 thru 1999
423*0Sstevel@tonic-gate 	 */
424*0Sstevel@tonic-gate 	tod.tod_year    = rtc.rtc_year;
425*0Sstevel@tonic-gate 	if (rtc.rtc_year <= 68)
426*0Sstevel@tonic-gate 		tod.tod_year += 100;
427*0Sstevel@tonic-gate 	tod.tod_month	= rtc.rtc_mon;
428*0Sstevel@tonic-gate 	tod.tod_day	= rtc.rtc_dom;
429*0Sstevel@tonic-gate 	tod.tod_dow	= rtc.rtc_dow;
430*0Sstevel@tonic-gate 	tod.tod_hour	= rtc.rtc_hrs;
431*0Sstevel@tonic-gate 	tod.tod_min	= rtc.rtc_min;
432*0Sstevel@tonic-gate 	tod.tod_sec	= rtc.rtc_sec;
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 	ts.tv_sec = tod_to_utc(tod);
435*0Sstevel@tonic-gate 	ts.tv_nsec = 0;
436*0Sstevel@tonic-gate 	return (ts);
437*0Sstevel@tonic-gate }
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate /*
440*0Sstevel@tonic-gate  * Program DS1337 with the specified time.
441*0Sstevel@tonic-gate  * Must be called with tod_lock held. The TOD
442*0Sstevel@tonic-gate  * chip supports date from 1969-2068 only. We must
443*0Sstevel@tonic-gate  * reject requests to set date below 1969.
444*0Sstevel@tonic-gate  */
445*0Sstevel@tonic-gate static void
446*0Sstevel@tonic-gate todds1337_set(timestruc_t ts)
447*0Sstevel@tonic-gate {
448*0Sstevel@tonic-gate 	struct rtc_t	rtc;
449*0Sstevel@tonic-gate 	todinfo_t	tod = utc_to_tod(ts.tv_sec);
450*0Sstevel@tonic-gate 	int		year;
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 	/*
456*0Sstevel@tonic-gate 	 * Year is base 1900, valid year range 1969-2068
457*0Sstevel@tonic-gate 	 */
458*0Sstevel@tonic-gate 	if ((tod.tod_year < 69) || (tod.tod_year > 168))
459*0Sstevel@tonic-gate 		return;
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	year = tod.tod_year;
462*0Sstevel@tonic-gate 	if (year >= 100)
463*0Sstevel@tonic-gate 		year -= 100;
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 	rtc.rtc_year	= (uint8_t)year;
466*0Sstevel@tonic-gate 	rtc.rtc_mon	= (uint8_t)tod.tod_month;
467*0Sstevel@tonic-gate 	rtc.rtc_dom	= (uint8_t)tod.tod_day;
468*0Sstevel@tonic-gate 	rtc.rtc_dow	= (uint8_t)tod.tod_dow;
469*0Sstevel@tonic-gate 	rtc.rtc_hrs	= (uint8_t)tod.tod_hour;
470*0Sstevel@tonic-gate 	rtc.rtc_min	= (uint8_t)tod.tod_min;
471*0Sstevel@tonic-gate 	rtc.rtc_sec	= (uint8_t)tod.tod_sec;
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	(void) todds1337_write_rtc(&rtc);
474*0Sstevel@tonic-gate }
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate /*
477*0Sstevel@tonic-gate  * Program ds1337 registers for alarm to go off at the specified time.
478*0Sstevel@tonic-gate  * Alarm #1 is used (Alarm #2 stays idle)
479*0Sstevel@tonic-gate  */
480*0Sstevel@tonic-gate /* ARGSUSED */
481*0Sstevel@tonic-gate static void
482*0Sstevel@tonic-gate todds1337_set_power_alarm(timestruc_t ts)
483*0Sstevel@tonic-gate {
484*0Sstevel@tonic-gate 	todinfo_t	tod;
485*0Sstevel@tonic-gate 	ds1337_state_t	*statep = NULL;
486*0Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
487*0Sstevel@tonic-gate 	uint8_t tmpval;
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	if (!todds1337_attach_done) {
492*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337: driver not attached");
493*0Sstevel@tonic-gate 		return;
494*0Sstevel@tonic-gate 	}
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1337_statep, instance);
497*0Sstevel@tonic-gate 	if (statep == NULL) {
498*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failed");
499*0Sstevel@tonic-gate 		return;
500*0Sstevel@tonic-gate 	}
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	tod = utc_to_tod(ts.tv_sec);
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	/*
505*0Sstevel@tonic-gate 	 * i2c_transfe() may block; to avoid locking clock() which
506*0Sstevel@tonic-gate 	 * is running in interrupt context at PIL10 we temporarely exit
507*0Sstevel@tonic-gate 	 * the tod_mutex and enter alarm lock. Time/date and alarm hardware
508*0Sstevel@tonic-gate 	 * have non-interlsecting registers, it is safe to use different locks.
509*0Sstevel@tonic-gate 	 */
510*0Sstevel@tonic-gate 	mutex_exit(&tod_lock);
511*0Sstevel@tonic-gate 	mutex_enter(&todds1337_alarm_lock);
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	/*
514*0Sstevel@tonic-gate 	 * Disable Power Alarm (A1IE)
515*0Sstevel@tonic-gate 	 */
516*0Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
517*0Sstevel@tonic-gate 	    &i2c_tp, 1, 1, I2C_SLEEP);
518*0Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
519*0Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR_RD;
520*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
523*0Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
524*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to read "
525*0Sstevel@tonic-gate 		    "DS1337 control register");
526*0Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
527*0Sstevel@tonic-gate 		mutex_enter(&tod_lock);
528*0Sstevel@tonic-gate 		return;
529*0Sstevel@tonic-gate 	}
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	tmpval = i2c_tp->i2c_rbuf[0];
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
536*0Sstevel@tonic-gate 	    &i2c_tp, 2, 0, I2C_SLEEP);
537*0Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
538*0Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
539*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */
540*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE;
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
543*0Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
544*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write "
545*0Sstevel@tonic-gate 		    "DS1337control register");
546*0Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
547*0Sstevel@tonic-gate 		mutex_enter(&tod_lock);
548*0Sstevel@tonic-gate 		return;
549*0Sstevel@tonic-gate 	}
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 	/*
555*0Sstevel@tonic-gate 	 * Write Alarm #1 registers
556*0Sstevel@tonic-gate 	 */
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
559*0Sstevel@tonic-gate 	    &i2c_tp, 5, 0, I2C_SLEEP);
560*0Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
561*0Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
562*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_ALARM_SEC; /* Alarm #1 Seconds */
563*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(tod.tod_sec);
564*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(tod.tod_min);
565*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(tod.tod_hour);
566*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(tod.tod_day);
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
569*0Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
570*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write "
571*0Sstevel@tonic-gate 		    "DS1337 alarm registers");
572*0Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
573*0Sstevel@tonic-gate 		mutex_enter(&tod_lock);
574*0Sstevel@tonic-gate 		return;
575*0Sstevel@tonic-gate 	}
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 	/*
581*0Sstevel@tonic-gate 	 * Enable Power Alarm
582*0Sstevel@tonic-gate 	 */
583*0Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
584*0Sstevel@tonic-gate 	    &i2c_tp, 2, 0, I2C_SLEEP);
585*0Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
586*0Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
587*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */
588*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = tmpval | RTC_CTL_A1IE;
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
591*0Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
592*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to enable "
593*0Sstevel@tonic-gate 		    "DS1337 alarm");
594*0Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
595*0Sstevel@tonic-gate 		mutex_enter(&tod_lock);
596*0Sstevel@tonic-gate 		return;
597*0Sstevel@tonic-gate 	}
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 	mutex_exit(&todds1337_alarm_lock);
602*0Sstevel@tonic-gate 	mutex_enter(&tod_lock);
603*0Sstevel@tonic-gate }
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate /* ARGSUSED */
606*0Sstevel@tonic-gate static void
607*0Sstevel@tonic-gate todds1337_clear_power_alarm(void)
608*0Sstevel@tonic-gate {
609*0Sstevel@tonic-gate 	ds1337_state_t	*statep = NULL;
610*0Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
611*0Sstevel@tonic-gate 	uint8_t tmpval;
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 	if (!todds1337_attach_done) {
616*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337: driver was not attached");
617*0Sstevel@tonic-gate 		return;
618*0Sstevel@tonic-gate 	}
619*0Sstevel@tonic-gate 
620*0Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1337_statep, instance);
621*0Sstevel@tonic-gate 	if (statep == NULL) {
622*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337: ddi_get_soft_state has failed");
623*0Sstevel@tonic-gate 		return;
624*0Sstevel@tonic-gate 	}
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate 	/*
627*0Sstevel@tonic-gate 	 * i2c_transfe() may block; to avoid locking clock() which
628*0Sstevel@tonic-gate 	 * is running in interrupt context at PIL10 we temporarely exit
629*0Sstevel@tonic-gate 	 * the tod_mutex and enter alarm lock. Time/date and alarm hardware
630*0Sstevel@tonic-gate 	 * have non-interlsecting registers, it is safe to use different locks.
631*0Sstevel@tonic-gate 	 */
632*0Sstevel@tonic-gate 	mutex_exit(&tod_lock);
633*0Sstevel@tonic-gate 	mutex_enter(&todds1337_alarm_lock);
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate 	/*
636*0Sstevel@tonic-gate 	 * Disable Alarm #1 Interrupt
637*0Sstevel@tonic-gate 	 */
638*0Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
639*0Sstevel@tonic-gate 	    &i2c_tp, 1, 1, I2C_SLEEP);
640*0Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
641*0Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR_RD;
642*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
645*0Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
646*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read "
647*0Sstevel@tonic-gate 		    "DS1337 control register");
648*0Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
649*0Sstevel@tonic-gate 		mutex_enter(&tod_lock);
650*0Sstevel@tonic-gate 		return;
651*0Sstevel@tonic-gate 	}
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 	tmpval = i2c_tp->i2c_rbuf[0];
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
658*0Sstevel@tonic-gate 	    &i2c_tp, 2, 0, I2C_SLEEP);
659*0Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
660*0Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
661*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */
662*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE;
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
665*0Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
666*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write "
667*0Sstevel@tonic-gate 		    "DS1337 control register");
668*0Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
669*0Sstevel@tonic-gate 		mutex_enter(&tod_lock);
670*0Sstevel@tonic-gate 		return;
671*0Sstevel@tonic-gate 	}
672*0Sstevel@tonic-gate 
673*0Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 	/*
676*0Sstevel@tonic-gate 	 * Reset Alarm #1 Flag
677*0Sstevel@tonic-gate 	 */
678*0Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
679*0Sstevel@tonic-gate 	    &i2c_tp, 1, 1, I2C_SLEEP);
680*0Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
681*0Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR_RD;
682*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
685*0Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
686*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read "
687*0Sstevel@tonic-gate 		    "DS1337 status register");
688*0Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
689*0Sstevel@tonic-gate 		mutex_enter(&tod_lock);
690*0Sstevel@tonic-gate 		return;
691*0Sstevel@tonic-gate 	}
692*0Sstevel@tonic-gate 
693*0Sstevel@tonic-gate 	tmpval = i2c_tp->i2c_rbuf[0];
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
698*0Sstevel@tonic-gate 	    &i2c_tp, 2, 0, I2C_SLEEP);
699*0Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
700*0Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
701*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Write Status register */
702*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_STATUS_A1F;
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate 	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
705*0Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
706*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write "
707*0Sstevel@tonic-gate 		    "DS1337 status register");
708*0Sstevel@tonic-gate 		mutex_exit(&todds1337_alarm_lock);
709*0Sstevel@tonic-gate 		mutex_enter(&tod_lock);
710*0Sstevel@tonic-gate 		return;
711*0Sstevel@tonic-gate 	}
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
714*0Sstevel@tonic-gate 
715*0Sstevel@tonic-gate 	mutex_exit(&todds1337_alarm_lock);
716*0Sstevel@tonic-gate 	mutex_enter(&tod_lock);
717*0Sstevel@tonic-gate }
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate /* ARGSUSED */
720*0Sstevel@tonic-gate static uint_t
721*0Sstevel@tonic-gate todds1337_set_watchdog_timer(uint_t timeoutval)
722*0Sstevel@tonic-gate {
723*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
724*0Sstevel@tonic-gate 	return (0);
725*0Sstevel@tonic-gate }
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate /* ARGSUSED */
728*0Sstevel@tonic-gate static uint_t
729*0Sstevel@tonic-gate todds1337_clear_watchdog_timer(void)
730*0Sstevel@tonic-gate {
731*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tod_lock));
732*0Sstevel@tonic-gate 	return (0);
733*0Sstevel@tonic-gate }
734*0Sstevel@tonic-gate 
735*0Sstevel@tonic-gate /* ********************** Local functions ***************************** */
736*0Sstevel@tonic-gate 
737*0Sstevel@tonic-gate static char tod_read[7] = {-1, -1, -1, -1, -1, -1, -1};
738*0Sstevel@tonic-gate static int
739*0Sstevel@tonic-gate todds1337_read_rtc(struct rtc_t *rtc)
740*0Sstevel@tonic-gate {
741*0Sstevel@tonic-gate 	static	ds1337_state_t	*statep = NULL;
742*0Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
743*0Sstevel@tonic-gate 	int i2c_cmd_status = I2C_FAILURE;
744*0Sstevel@tonic-gate 	int counter = 4;
745*0Sstevel@tonic-gate 
746*0Sstevel@tonic-gate 	if (!todds1337_attach_done) {
747*0Sstevel@tonic-gate 		return (todds1337_prom_getdate(rtc));
748*0Sstevel@tonic-gate 	}
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1337_statep, instance);
751*0Sstevel@tonic-gate 	if (statep == NULL) {
752*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failing");
753*0Sstevel@tonic-gate 		return (DDI_FAILURE);
754*0Sstevel@tonic-gate 	}
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate 	mutex_enter(&todds1337_rd_lock);
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate 	/*
759*0Sstevel@tonic-gate 	 * Allocate 1 byte for write buffer and 7 bytes for read buffer to
760*0Sstevel@tonic-gate 	 * to accomodate sec, min, hrs, dayOfWeek, dayOfMonth, year
761*0Sstevel@tonic-gate 	 */
762*0Sstevel@tonic-gate 	if ((i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp, 1,
763*0Sstevel@tonic-gate 	    7, I2C_SLEEP)) != I2C_SUCCESS) {
764*0Sstevel@tonic-gate 		mutex_exit(&todds1337_rd_lock);
765*0Sstevel@tonic-gate 		return (DDI_FAILURE);
766*0Sstevel@tonic-gate 	}
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 	do {
769*0Sstevel@tonic-gate 		i2c_tp->i2c_version = I2C_XFER_REV;
770*0Sstevel@tonic-gate 		i2c_tp->i2c_flags = I2C_WR_RD;
771*0Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC; /* Start from 0x00 */
772*0Sstevel@tonic-gate 		i2c_tp->i2c_wlen = 1;	/* Write one byte address */
773*0Sstevel@tonic-gate 		i2c_tp->i2c_rlen = 7;	/* Read 7 regs */
774*0Sstevel@tonic-gate 
775*0Sstevel@tonic-gate 		if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl,
776*0Sstevel@tonic-gate 		    i2c_tp)) != I2C_SUCCESS) {
777*0Sstevel@tonic-gate 			goto done;
778*0Sstevel@tonic-gate 		}
779*0Sstevel@tonic-gate 	    /* for first read, need to get valid data */
780*0Sstevel@tonic-gate 	    while (tod_read[0] == -1 && counter > 0) {
781*0Sstevel@tonic-gate 	    /* move data to static buffer */
782*0Sstevel@tonic-gate 		bcopy(i2c_tp->i2c_rbuf, tod_read, 7);
783*0Sstevel@tonic-gate 
784*0Sstevel@tonic-gate 		/* now read again */
785*0Sstevel@tonic-gate 		/* Start reading reg from 0x00 */
786*0Sstevel@tonic-gate 		i2c_tp->i2c_wbuf[0] = (uchar_t)0x00;
787*0Sstevel@tonic-gate 		i2c_tp->i2c_wlen = 1;	/* Write one byte address */
788*0Sstevel@tonic-gate 		i2c_tp->i2c_rlen = 7;	/* Read 7 regs */
789*0Sstevel@tonic-gate 		if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl,
790*0Sstevel@tonic-gate 		    i2c_tp)) != I2C_SUCCESS) {
791*0Sstevel@tonic-gate 		    goto done;
792*0Sstevel@tonic-gate 		}
793*0Sstevel@tonic-gate 		/* if they are not the same, then read again */
794*0Sstevel@tonic-gate 		if (bcmp(tod_read, i2c_tp->i2c_rbuf, 7) != 0) {
795*0Sstevel@tonic-gate 		    tod_read[0] = -1;
796*0Sstevel@tonic-gate 		    counter--;
797*0Sstevel@tonic-gate 		}
798*0Sstevel@tonic-gate 	    }
799*0Sstevel@tonic-gate 
800*0Sstevel@tonic-gate 	} while (i2c_tp->i2c_rbuf[0] == 0x59 &&
801*0Sstevel@tonic-gate 	    /* if seconds register is 0x59 (BCD), add data should match */
802*0Sstevel@tonic-gate 		bcmp(&tod_read[1], &i2c_tp->i2c_rbuf[1], 6) != 0 &&
803*0Sstevel@tonic-gate 		    counter-- > 0);
804*0Sstevel@tonic-gate 
805*0Sstevel@tonic-gate 	if (counter < 0)
806*0Sstevel@tonic-gate 	    cmn_err(CE_WARN, "i2ctod: TOD Chip failed ??");
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate 	/* move data to static buffer */
809*0Sstevel@tonic-gate 	bcopy(i2c_tp->i2c_rbuf, tod_read, 7);
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate 
812*0Sstevel@tonic-gate 	rtc->rtc_year	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[6]);
813*0Sstevel@tonic-gate 	rtc->rtc_mon	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[5]);
814*0Sstevel@tonic-gate 	rtc->rtc_dom	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[4]);
815*0Sstevel@tonic-gate 	rtc->rtc_dow	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[3]);
816*0Sstevel@tonic-gate 	rtc->rtc_hrs	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[2]);
817*0Sstevel@tonic-gate 	rtc->rtc_min	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[1]);
818*0Sstevel@tonic-gate 	rtc->rtc_sec	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[0]);
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate done:
821*0Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate 	mutex_exit(&todds1337_rd_lock);
824*0Sstevel@tonic-gate 	return (i2c_cmd_status);
825*0Sstevel@tonic-gate }
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 
828*0Sstevel@tonic-gate static int
829*0Sstevel@tonic-gate todds1337_write_rtc(struct rtc_t *rtc)
830*0Sstevel@tonic-gate {
831*0Sstevel@tonic-gate 	ds1337_state_t	*statep = NULL;
832*0Sstevel@tonic-gate 	i2c_transfer_t	*i2c_tp = NULL;
833*0Sstevel@tonic-gate 	int i2c_cmd_status = I2C_SUCCESS;
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate 
836*0Sstevel@tonic-gate 	if (!todds1337_attach_done) {
837*0Sstevel@tonic-gate 		return (todds1337_prom_setdate(rtc));
838*0Sstevel@tonic-gate 	}
839*0Sstevel@tonic-gate 
840*0Sstevel@tonic-gate 	statep = ddi_get_soft_state(ds1337_statep, instance);
841*0Sstevel@tonic-gate 	if (statep == NULL) {
842*0Sstevel@tonic-gate 		return (DDI_FAILURE);
843*0Sstevel@tonic-gate 	}
844*0Sstevel@tonic-gate 
845*0Sstevel@tonic-gate 	if ((i2c_cmd_status = i2c_transfer_alloc(statep->ds1337_i2c_hdl,
846*0Sstevel@tonic-gate 	    &i2c_tp, 8, 0, I2C_SLEEP)) != I2C_SUCCESS) {
847*0Sstevel@tonic-gate 		return (i2c_cmd_status);
848*0Sstevel@tonic-gate 	}
849*0Sstevel@tonic-gate 
850*0Sstevel@tonic-gate 	i2c_tp->i2c_version = I2C_XFER_REV;
851*0Sstevel@tonic-gate 	i2c_tp->i2c_flags = I2C_WR;
852*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC;
853*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(rtc->rtc_sec);
854*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(rtc->rtc_min);
855*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(rtc->rtc_hrs);
856*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(rtc->rtc_dow);
857*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[5] = BYTE_TO_BCD(rtc->rtc_dom);
858*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[6] = BYTE_TO_BCD(rtc->rtc_mon);
859*0Sstevel@tonic-gate 	i2c_tp->i2c_wbuf[7] = BYTE_TO_BCD(rtc->rtc_year);
860*0Sstevel@tonic-gate 	i2c_tp->i2c_wlen = 8;
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate 	if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl,
863*0Sstevel@tonic-gate 	    i2c_tp)) != I2C_SUCCESS) {
864*0Sstevel@tonic-gate 		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
865*0Sstevel@tonic-gate 		return (i2c_cmd_status);
866*0Sstevel@tonic-gate 	}
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate 	tod_read[0] = -1;  /* invalidate saved data from read routine */
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate 	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
871*0Sstevel@tonic-gate 
872*0Sstevel@tonic-gate 	return (i2c_cmd_status);
873*0Sstevel@tonic-gate }
874*0Sstevel@tonic-gate 
875*0Sstevel@tonic-gate 
876*0Sstevel@tonic-gate /*ARGSUSED*/
877*0Sstevel@tonic-gate static int
878*0Sstevel@tonic-gate todds1337_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
879*0Sstevel@tonic-gate     void **result)
880*0Sstevel@tonic-gate {
881*0Sstevel@tonic-gate 	ds1337_state_t *softsp;
882*0Sstevel@tonic-gate 
883*0Sstevel@tonic-gate 	if (instance == -1) {
884*0Sstevel@tonic-gate 		return (DDI_FAILURE);
885*0Sstevel@tonic-gate 	}
886*0Sstevel@tonic-gate 
887*0Sstevel@tonic-gate 	switch (infocmd) {
888*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
889*0Sstevel@tonic-gate 		if ((softsp = ddi_get_soft_state(ds1337_statep, instance)) ==
890*0Sstevel@tonic-gate 		    NULL)
891*0Sstevel@tonic-gate 			return (DDI_FAILURE);
892*0Sstevel@tonic-gate 		*result = (void *)softsp->dip;
893*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
894*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
895*0Sstevel@tonic-gate 		*result = (void *)instance;
896*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
897*0Sstevel@tonic-gate 	default:
898*0Sstevel@tonic-gate 		return (DDI_FAILURE);
899*0Sstevel@tonic-gate 	}
900*0Sstevel@tonic-gate }
901*0Sstevel@tonic-gate 
902*0Sstevel@tonic-gate /*
903*0Sstevel@tonic-gate  * Finds the device node with device_type "rtc" and opens it to
904*0Sstevel@tonic-gate  * execute the get-time method
905*0Sstevel@tonic-gate  */
906*0Sstevel@tonic-gate static int
907*0Sstevel@tonic-gate todds1337_setup_prom()
908*0Sstevel@tonic-gate {
909*0Sstevel@tonic-gate 	dnode_t todnode;
910*0Sstevel@tonic-gate 	char tod1337_devpath[MAXNAMELEN];
911*0Sstevel@tonic-gate 
912*0Sstevel@tonic-gate 	if ((todnode = prom_findnode_bydevtype(prom_rootnode(),
913*0Sstevel@tonic-gate 	    DS1337_DEVICE_TYPE)) == OBP_NONODE)
914*0Sstevel@tonic-gate 		return (DDI_FAILURE);
915*0Sstevel@tonic-gate 
916*0Sstevel@tonic-gate 	/*
917*0Sstevel@tonic-gate 	 * We now have the phandle of the rtc node, we need to open the
918*0Sstevel@tonic-gate 	 * node and get the ihandle
919*0Sstevel@tonic-gate 	 */
920*0Sstevel@tonic-gate 	if (prom_phandle_to_path(todnode, tod1337_devpath,
921*0Sstevel@tonic-gate 	    sizeof (tod1337_devpath)) < 0) {
922*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "prom_phandle_to_path failed");
923*0Sstevel@tonic-gate 		return (DDI_FAILURE);
924*0Sstevel@tonic-gate 	}
925*0Sstevel@tonic-gate 
926*0Sstevel@tonic-gate 	/*
927*0Sstevel@tonic-gate 	 * Now open the node and store it's ihandle
928*0Sstevel@tonic-gate 	 */
929*0Sstevel@tonic-gate 	if ((todds1337_ihandle = prom_open(tod1337_devpath)) == NULL) {
930*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "prom_open failed");
931*0Sstevel@tonic-gate 		return (DDI_FAILURE);
932*0Sstevel@tonic-gate 	}
933*0Sstevel@tonic-gate 
934*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
935*0Sstevel@tonic-gate }
936*0Sstevel@tonic-gate 
937*0Sstevel@tonic-gate /*
938*0Sstevel@tonic-gate  * Closes the prom interface
939*0Sstevel@tonic-gate  */
940*0Sstevel@tonic-gate static void
941*0Sstevel@tonic-gate todds1337_rele_prom()
942*0Sstevel@tonic-gate {
943*0Sstevel@tonic-gate 	(void) prom_close(todds1337_ihandle);
944*0Sstevel@tonic-gate }
945*0Sstevel@tonic-gate 
946*0Sstevel@tonic-gate /*
947*0Sstevel@tonic-gate  * Read the date using "get-time" method in rtc node
948*0Sstevel@tonic-gate  * PROM returns 1969-1999 when reading 69-99 and
949*0Sstevel@tonic-gate  * 2000-2068 when reading 00-68
950*0Sstevel@tonic-gate  */
951*0Sstevel@tonic-gate static int
952*0Sstevel@tonic-gate todds1337_prom_getdate(struct rtc_t *rtc)
953*0Sstevel@tonic-gate {
954*0Sstevel@tonic-gate 	int year;
955*0Sstevel@tonic-gate 	cell_t ci[12];
956*0Sstevel@tonic-gate 
957*0Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");  /* Service name */
958*0Sstevel@tonic-gate 	ci[1] = 2; /* # of arguments */
959*0Sstevel@tonic-gate 	ci[2] = 7; /* # of result cells */
960*0Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("get-time");
961*0Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(todds1337_ihandle);
962*0Sstevel@tonic-gate 
963*0Sstevel@tonic-gate 	promif_preprom();
964*0Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
965*0Sstevel@tonic-gate 	promif_postprom();
966*0Sstevel@tonic-gate 
967*0Sstevel@tonic-gate 	year 		= p1275_cell2int(ci[6]);
968*0Sstevel@tonic-gate 	rtc->rtc_mon	= p1275_cell2int(ci[7]);
969*0Sstevel@tonic-gate 	rtc->rtc_dom	= p1275_cell2int(ci[8]);
970*0Sstevel@tonic-gate 	rtc->rtc_dow	= 0;
971*0Sstevel@tonic-gate 	rtc->rtc_hrs	= p1275_cell2int(ci[9]);
972*0Sstevel@tonic-gate 	rtc->rtc_min	= p1275_cell2int(ci[10]);
973*0Sstevel@tonic-gate 	rtc->rtc_sec	= p1275_cell2int(ci[11]);
974*0Sstevel@tonic-gate 	if (year >= 2000)
975*0Sstevel@tonic-gate 		year -= 2000;
976*0Sstevel@tonic-gate 	else
977*0Sstevel@tonic-gate 		year -= 1900;
978*0Sstevel@tonic-gate 	rtc->rtc_year	= year;
979*0Sstevel@tonic-gate 
980*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
981*0Sstevel@tonic-gate }
982*0Sstevel@tonic-gate 
983*0Sstevel@tonic-gate /*
984*0Sstevel@tonic-gate  * Read the date using "set-time" method in rtc node
985*0Sstevel@tonic-gate  * For values 00 - 68, write 2000-2068, and for 69-99,
986*0Sstevel@tonic-gate  * write 1969-1999
987*0Sstevel@tonic-gate  */
988*0Sstevel@tonic-gate static int
989*0Sstevel@tonic-gate todds1337_prom_setdate(struct rtc_t *rtc)
990*0Sstevel@tonic-gate {
991*0Sstevel@tonic-gate 	int year;
992*0Sstevel@tonic-gate 	cell_t ci[12];
993*0Sstevel@tonic-gate 
994*0Sstevel@tonic-gate 	year = rtc->rtc_year;
995*0Sstevel@tonic-gate 
996*0Sstevel@tonic-gate 	if ((year < 0) || (year > 99))
997*0Sstevel@tonic-gate 		return (DDI_FAILURE);
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate 	if (year <= 68)
1000*0Sstevel@tonic-gate 		year = rtc->rtc_year + 2000;
1001*0Sstevel@tonic-gate 	else
1002*0Sstevel@tonic-gate 		year = rtc->rtc_year + 1900;
1003*0Sstevel@tonic-gate 
1004*0Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");  /* Service name */
1005*0Sstevel@tonic-gate 	ci[1] = 8; /* # of arguments */
1006*0Sstevel@tonic-gate 	ci[2] = 0; /* # of result cells */
1007*0Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("set-time");
1008*0Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(todds1337_ihandle);
1009*0Sstevel@tonic-gate 	ci[5] = p1275_int2cell(year);
1010*0Sstevel@tonic-gate 	ci[6] = p1275_int2cell(rtc->rtc_mon);
1011*0Sstevel@tonic-gate 	ci[7] = p1275_int2cell(rtc->rtc_dom);
1012*0Sstevel@tonic-gate 	ci[8] = p1275_int2cell(rtc->rtc_hrs);
1013*0Sstevel@tonic-gate 	ci[9] = p1275_int2cell(rtc->rtc_min);
1014*0Sstevel@tonic-gate 	ci[10] = p1275_int2cell(rtc->rtc_sec);
1015*0Sstevel@tonic-gate 
1016*0Sstevel@tonic-gate 	promif_preprom();
1017*0Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
1018*0Sstevel@tonic-gate 	promif_postprom();
1019*0Sstevel@tonic-gate 
1020*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
1021*0Sstevel@tonic-gate }
1022