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