xref: /titanic_44/usr/src/uts/sun4u/snowbird/io/todds1307/todds1307.c (revision 60b08185ce63023f22fd6b2ed0db8c0d119b2023)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/conf.h>
31 #include <sys/devops.h>
32 #include <sys/kmem.h>
33 #include <sys/open.h>
34 #include <sys/file.h>
35 #include <sys/note.h>
36 #include <sys/ddi.h>
37 #include <sys/sunddi.h>
38 
39 #include <sys/modctl.h>
40 #include <sys/stat.h>
41 #include <sys/clock.h>
42 #include <sys/reboot.h>
43 #include <sys/machsystm.h>
44 #include <sys/poll.h>
45 #include <sys/pbio.h>
46 #include <sys/sysmacros.h>
47 #include <sys/cyclic.h>
48 
49 /* Added for prom interface */
50 #include <sys/promif.h>
51 #include <sys/promimpl.h>
52 
53 #include <sys/i2c/misc/i2c_svc.h>
54 #include <sys/todds1307.h>
55 
56 #define	I2C_DELAY	20000
57 #define	DS1307_DEVICE_TYPE	"rtc"
58 
59 /*
60  * Driver enrty routines
61  */
62 static int todds1307_attach(dev_info_t *, ddi_attach_cmd_t);
63 static int todds1307_detach(dev_info_t *, ddi_detach_cmd_t);
64 static int todds1307_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
65 
66 /*
67  * tod_ops entry routines
68  */
69 static timestruc_t	todds1307_get(void);
70 static void		todds1307_set(timestruc_t);
71 static uint_t		todds1307_set_watchdog_timer(uint_t);
72 static uint_t		todds1307_clear_watchdog_timer(void);
73 static void		todds1307_set_power_alarm(timestruc_t);
74 static void		todds1307_clear_power_alarm(void);
75 static int todds1307_setup_prom();
76 static void todds1307_rele_prom();
77 static int todds1307_prom_getdate(struct rtc_t *rtc);
78 static int todds1307_prom_setdate(struct rtc_t *rtc);
79 
80 /*
81  * Local functions
82  */
83 static int	todds1307_read_rtc(struct rtc_t *);
84 static int	todds1307_write_rtc(struct rtc_t *);
85 
86 /* Anchor for soft state structure */
87 static void	*ds1307_statep;
88 static int	instance = -1;
89 static int	todds1307_attach_done = 0;
90 static kmutex_t	todds1307_rd_lock;
91 static ihandle_t todds1307_ihandle = 0;
92 
93 /* one second time out */
94 #define	I2c_CYCLIC_TIMEOUT	1000000000
95 uint_t i2c_cyclic_timeout = I2c_CYCLIC_TIMEOUT;
96 static int sync_clock_once = 1;
97 static 	struct	rtc_t	 soft_rtc;
98 
99 /*
100  * For debugging only
101  */
102 static unsigned char int2bcd(int num);
103 static int bcd2int(unsigned char num);
104 
105 /*
106  * cp_ops structure
107  */
108 static struct cb_ops ds1307_cbops = {
109 	nodev,				/* open */
110 	nodev,				/* close */
111 	nodev,				/* strategy */
112 	nodev,				/* print */
113 	nodev,				/* dump */
114 	nodev,				/* read */
115 	nodev,				/* write */
116 	nodev,				/* ioctl */
117 	nodev,				/* devmap */
118 	nodev,				/* mmap */
119 	nodev,				/* segmap */
120 	NULL,				/* poll */
121 	ddi_prop_op,			/* cb_prop_op */
122 	NULL,				/* streamtab */
123 	D_NEW | D_MP,			/* Driver compatibility flag */
124 	CB_REV,				/* rev */
125 	nodev,				/* int (*cb_aread)() */
126 	nodev				/* int (*cb_awrite)() */
127 };
128 
129 /*
130  * dev_ops structure
131  */
132 static struct dev_ops ds1307_ops = {
133 	DEVO_REV,		/* devo_rev */
134 	0,			/* refcnt - reference cnt always set to 0 */
135 	todds1307_getinfo,	/* getinfo - Maybe requred */
136 	nulldev,		/* identify */
137 	nulldev,		/* probe */
138 	todds1307_attach,	/* attach */
139 	todds1307_detach,	/* detach */
140 	nodev,			/* reset */
141 	&ds1307_cbops,		/* cb_ops - ds1307 does not need this(?) */
142 	NULL
143 };
144 
145 static struct modldrv todds1307_modldrv = {
146 	&mod_driverops,		/* Type of module. This one is a driver */
147 	"tod driver for DS1307 v%I%",	/* Name of the module */
148 	&ds1307_ops,			/* Pointer to dev_ops */
149 };
150 
151 /*
152  * Module linkage structure
153  */
154 static struct modlinkage todds1307_modlinkage = {
155 	MODREV_1,
156 	&todds1307_modldrv,
157 	0
158 };
159 
160 int
161 _init(void)
162 {
163 	int error;
164 
165 	if (strcmp(tod_module_name, "todds1307") == 0) {
166 		if ((error = ddi_soft_state_init(&ds1307_statep,
167 			sizeof (ds1307_state_t), 0)) != DDI_SUCCESS) {
168 			return (error);
169 		}
170 
171 		tod_ops.tod_get = todds1307_get;
172 		tod_ops.tod_set = todds1307_set;
173 		tod_ops.tod_set_watchdog_timer = todds1307_set_watchdog_timer;
174 		tod_ops.tod_clear_watchdog_timer =
175 				todds1307_clear_watchdog_timer;
176 		tod_ops.tod_set_power_alarm = todds1307_set_power_alarm;
177 		tod_ops.tod_clear_power_alarm = todds1307_clear_power_alarm;
178 	}
179 
180 	(void) todds1307_setup_prom();
181 
182 	/*
183 	 * Install the module
184 	 */
185 	if ((error = mod_install(&todds1307_modlinkage)) != 0) {
186 		ddi_soft_state_fini(&ds1307_statep);
187 		return (error);
188 	}
189 	mutex_init(&todds1307_rd_lock, NULL, MUTEX_DEFAULT, NULL);
190 
191 	return (0);
192 }
193 
194 int
195 _fini(void)
196 {
197 	int error = 0;
198 
199 	if (strcmp(tod_module_name, "todds1307") == 0) {
200 		error = EBUSY;
201 	} else {
202 		if ((error = mod_remove(&todds1307_modlinkage)) == 0) {
203 			ddi_soft_state_fini(&ds1307_statep);
204 			mutex_destroy(&todds1307_rd_lock);
205 			todds1307_rele_prom();
206 		}
207 	}
208 
209 	return (error);
210 }
211 
212 int
213 _info(struct modinfo *modinfop)
214 {
215 	return (mod_info(&todds1307_modlinkage, modinfop));
216 }
217 
218 /*
219  * cyclical call to get tod.
220  */
221 static void
222 todds1307_cyclic(void *arg)
223 {
224 
225 	todds1307_read_rtc((struct rtc_t *)arg);
226 
227 }
228 
229 /*
230  * register ds1307 client device with i2c services, and
231  * allocate & initialize soft state structure.
232  */
233 static int
234 todds1307_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
235 {
236 	static ds1307_state_t	*statep = NULL;
237 	i2c_transfer_t	*i2c_tp = NULL;
238 	uint8_t tempVal = (uint8_t)0;
239 	cyc_handler_t cychand;
240 	cyc_time_t  cyctime;
241 
242 	switch (cmd) {
243 
244 		case DDI_ATTACH:
245 			break;
246 		case DDI_RESUME:
247 			return (DDI_SUCCESS);
248 		default:
249 			return (DDI_FAILURE);
250 	}
251 
252 	if (instance != -1) {
253 		return (DDI_FAILURE);
254 	}
255 
256 	instance = ddi_get_instance(dip);
257 
258 	/*
259 	 * Allocate soft state structure
260 	 */
261 	if (ddi_soft_state_zalloc(ds1307_statep, instance) != DDI_SUCCESS) {
262 		return (DDI_FAILURE);
263 	}
264 
265 	statep = ddi_get_soft_state(ds1307_statep, instance);
266 	if (statep == NULL) {
267 		return (DDI_FAILURE);
268 	}
269 
270 	statep->dip = dip;
271 
272 	if (i2c_client_register(dip, &statep->ds1307_i2c_hdl) != I2C_SUCCESS) {
273 		ddi_soft_state_free(ds1307_statep, instance);
274 		delay(drv_usectohz(I2C_DELAY));
275 		return (DDI_FAILURE);
276 	}
277 
278 	/* check and initialize the oscillator */
279 
280 	(void) i2c_transfer_alloc(statep->ds1307_i2c_hdl,
281 					&i2c_tp, 1, 1, I2C_SLEEP);
282 	i2c_tp->i2c_version = I2C_XFER_REV;
283 	i2c_tp->i2c_flags = I2C_WR_RD;
284 	i2c_tp->i2c_wbuf[0] = (uchar_t)0x00; /* Read 00h */
285 	i2c_tp->i2c_wlen = 1;
286 	i2c_tp->i2c_rlen = 1;
287 
288 	if ((i2c_transfer(statep->ds1307_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
289 		(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
290 		ddi_soft_state_free(ds1307_statep, instance);
291 		delay(drv_usectohz(I2C_DELAY));
292 		return (DDI_FAILURE);
293 	}
294 
295 	tempVal = i2c_tp->i2c_rbuf[0];
296 
297 	(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
298 
299 	if (tempVal & 0x80) {			 /* check Oscillator */
300 		(void) i2c_transfer_alloc(statep->ds1307_i2c_hdl, &i2c_tp,
301 						2, 1, I2C_SLEEP);
302 		i2c_tp->i2c_version = I2C_XFER_REV;
303 		i2c_tp->i2c_flags = I2C_WR;
304 		i2c_tp->i2c_wbuf[0] = 0x00;
305 		i2c_tp->i2c_wbuf[1] =
306 		    (uchar_t)(i2c_tp->i2c_rbuf[0]& 0x7f);
307 		i2c_tp->i2c_wlen = 2;
308 					/* Enable oscillator */
309 		if ((i2c_transfer(statep->ds1307_i2c_hdl, i2c_tp))
310 						!= I2C_SUCCESS) {
311 			(void) i2c_transfer_free(statep->ds1307_i2c_hdl,
312 								i2c_tp);
313 			ddi_soft_state_free(ds1307_statep, instance);
314 			return (DDI_FAILURE);
315 		}
316 		(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
317 	}
318 
319 	/* Create a cyclic handler to read TOD */
320 	cychand.cyh_func = todds1307_cyclic;
321 	cychand.cyh_arg = &soft_rtc;
322 	cychand.cyh_level = CY_LOW_LEVEL;
323 	cyctime.cyt_when = 0;
324 	cyctime.cyt_interval = i2c_cyclic_timeout;
325 	ASSERT(statep->cycid == CYCLIC_NONE);
326 	mutex_enter(&cpu_lock);
327 	statep->cycid = cyclic_add(&cychand, &cyctime);
328 	mutex_exit(&cpu_lock);
329 	ASSERT(statep->cycid != CYCLIC_NONE);
330 
331 	statep->state = TOD_ATTACHED;
332 	todds1307_attach_done = 1;
333 	ddi_report_dev(dip);
334 
335 	return (DDI_SUCCESS);
336 }
337 
338 /*
339  * Unregister ds1307 client device with i2c services and free
340  * soft state structure.
341  */
342 /*ARGSUSED*/
343 static int
344 todds1307_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
345 {
346 	switch (cmd) {
347 
348 	/*
349 	 * Once attached, do not allow detach because the system constantly
350 	 * calling todds1307_get() to get the time.  If the driver is detached
351 	 * and the system try to get the time, the system will have memory
352 	 * problem.
353 	 *
354 	 *	ds1307_state_t	*statep = NULL;
355 	 *	case DDI_DETACH:
356 	 *		if ((statep = ddi_get_soft_state(ds1307_statep,
357 	 *					instance)) == NULL) {
358 	 *			return (ENOMEM);
359 	 *		}
360 	 *		i2c_client_unregister(statep->ds1307_i2c_hdl);
361 	 *		ddi_soft_state_free(ds1307_statep, instance);
362 	 *		return (DDI_SUCCESS);
363 	 */
364 		case DDI_SUSPEND:
365 			return (DDI_SUCCESS);
366 
367 		default:
368 			return (DDI_FAILURE);
369 	}
370 }
371 
372 /* *********************** tod_ops entry points ******************** */
373 
374 /*
375  * Read the current time from the DS1307 chip and convert to UNIX form.
376  * Should be called with tod_clock held.
377  */
378 
379 static timestruc_t
380 todds1307_get(void)
381 {
382 	timestruc_t	ts;
383 	todinfo_t	tod;
384 	struct	rtc_t	rtc;
385 
386 	ASSERT(MUTEX_HELD(&tod_lock));
387 
388 	if (sync_clock_once) {
389 		todds1307_read_rtc(&soft_rtc);
390 		sync_clock_once = 0;
391 	} else {
392 		tod_fault_reset();
393 		return (hrestime);
394 	}
395 
396 	bcopy(&soft_rtc, &rtc, sizeof (rtc));
397 
398 	/*
399 	 * 00 - 68 = 2000 thru 2068
400 	 * 69-99 = 1969 thru 1999
401 	 */
402 	tod.tod_year    = rtc.rtc_year;
403 	if (rtc.rtc_year <= 68)
404 		tod.tod_year += 100;
405 	tod.tod_month	= rtc.rtc_mon;
406 	tod.tod_day	= rtc.rtc_dom;
407 	tod.tod_dow	= rtc.rtc_dow;
408 	tod.tod_hour	= rtc.rtc_hrs;
409 	tod.tod_min	= rtc.rtc_min;
410 	tod.tod_sec	= rtc.rtc_sec;
411 
412 	ts.tv_sec = tod_to_utc(tod);
413 	ts.tv_nsec = 0;
414 	return (ts);
415 }
416 
417 /*
418  * Program DS1307 with the specified time.
419  * Must be called with tod_lock held. The TOD
420  * chip supports date from 1969-2068 only. We must
421  * reject requests to set date below 2000.
422  */
423 static void
424 todds1307_set(timestruc_t ts)
425 {
426 	struct rtc_t	rtc;
427 	todinfo_t	tod = utc_to_tod(ts.tv_sec);
428 	int		year;
429 
430 
431 	ASSERT(MUTEX_HELD(&tod_lock));
432 
433 	/*
434 	 * Year is base 1900, valid year range 1969-2068
435 	 */
436 	if ((tod.tod_year < 69) || (tod.tod_year > 168))
437 		return;
438 
439 	year = tod.tod_year;
440 	if (year >= 100)
441 		year -= 100;
442 
443 	rtc.rtc_year	= int2bcd(year);
444 	rtc.rtc_mon	= int2bcd(tod.tod_month);
445 	rtc.rtc_dom	= int2bcd(tod.tod_day);
446 	rtc.rtc_dow	= int2bcd(tod.tod_dow);
447 	rtc.rtc_hrs	= int2bcd(tod.tod_hour);
448 	rtc.rtc_min	= int2bcd(tod.tod_min);
449 	rtc.rtc_sec	= int2bcd(tod.tod_sec);
450 
451 	todds1307_write_rtc(&rtc);
452 }
453 
454 /* ARGSUSED */
455 static void
456 todds1307_set_power_alarm(timestruc_t ts)
457 {
458 	ASSERT(MUTEX_HELD(&tod_lock));
459 }
460 
461 /* ARGSUSED */
462 static void
463 todds1307_clear_power_alarm(void)
464 {
465 	ASSERT(MUTEX_HELD(&tod_lock));
466 }
467 
468 /* ARGSUSED */
469 static uint_t
470 todds1307_set_watchdog_timer(uint_t timeoutval)
471 {
472 	ASSERT(MUTEX_HELD(&tod_lock));
473 	return (0);
474 }
475 
476 /* ARGSUSED */
477 static uint_t
478 todds1307_clear_watchdog_timer(void)
479 {
480 	ASSERT(MUTEX_HELD(&tod_lock));
481 	return (0);
482 }
483 
484 /* ********************** Local functions ***************************** */
485 
486 static char tod_read[7] = {-1, -1, -1, -1, -1, -1, -1};
487 static int
488 todds1307_read_rtc(struct rtc_t *rtc)
489 {
490 	static	ds1307_state_t	*statep = NULL;
491 	i2c_transfer_t	*i2c_tp = NULL;
492 	int i2c_cmd_status = I2C_FAILURE;
493 	int counter = 4;
494 
495 	if (!todds1307_attach_done) {
496 		return (todds1307_prom_getdate(rtc));
497 	}
498 
499 	statep = ddi_get_soft_state(ds1307_statep, instance);
500 	if (statep == NULL) {
501 		cmn_err(CE_WARN, "todds1307: ddi_get_soft_state failed");
502 		return (DDI_FAILURE);
503 	}
504 
505 	mutex_enter(&todds1307_rd_lock);
506 
507 	/*
508 	 * Allocate 1 byte for write buffer and 7 bytes for read buffer to
509 	 * to accomodate sec, min, hrs, dayOfWeek, dayOfMonth, year
510 	 */
511 	if ((i2c_transfer_alloc(statep->ds1307_i2c_hdl, &i2c_tp, 1,
512 				7, I2C_SLEEP)) != I2C_SUCCESS) {
513 		mutex_exit(&todds1307_rd_lock);
514 		return (DDI_FAILURE);
515 	}
516 
517 	do {
518 		i2c_tp->i2c_version = I2C_XFER_REV;
519 		i2c_tp->i2c_flags = I2C_WR_RD;
520 		i2c_tp->i2c_wbuf[0] = (uchar_t)0x00; /* Start from reg 0x00 */
521 		i2c_tp->i2c_wlen = 1;	/* Write one byte address */
522 		i2c_tp->i2c_rlen = 7;	/* Read 7 regs */
523 
524 		if ((i2c_cmd_status = i2c_transfer(statep->ds1307_i2c_hdl,
525 						i2c_tp)) != I2C_SUCCESS) {
526 			drv_usecwait(I2C_DELAY);
527 			goto done;
528 		}
529 	    /* for first read, need to get valid data */
530 	    while (tod_read[0] == -1 && counter > 0) {
531 	    /* move data to static buffer */
532 		bcopy(i2c_tp->i2c_rbuf, tod_read, 7);
533 
534 		/* now read again */
535 		/* Start reading reg from 0x00 */
536 		i2c_tp->i2c_wbuf[0] = (uchar_t)0x00;
537 		i2c_tp->i2c_wlen = 1;	/* Write one byte address */
538 		i2c_tp->i2c_rlen = 7;	/* Read 7 regs */
539 		if ((i2c_cmd_status = i2c_transfer(statep->ds1307_i2c_hdl,
540 					    i2c_tp)) != I2C_SUCCESS) {
541 		    drv_usecwait(I2C_DELAY);
542 		    goto done;
543 		}
544 		/* if they are not the same, then read again */
545 		if (bcmp(tod_read, i2c_tp->i2c_rbuf, 7) != 0) {
546 		    tod_read[0] = -1;
547 		    counter--;
548 		}
549 	    }
550 
551 	} while (i2c_tp->i2c_rbuf[0] == 0x59 &&
552 	    /* if seconds register is 0x59 (BCD), add data should match */
553 	    bcmp(&tod_read[1], &i2c_tp->i2c_rbuf[1], 6) != 0 &&
554 	    counter-- > 0);
555 
556 	if (counter < 0)
557 	    cmn_err(CE_WARN, "i2ctod: TOD Chip failed ??");
558 
559 	/* move data to static buffer */
560 	bcopy(i2c_tp->i2c_rbuf, tod_read, 7);
561 
562 
563 	rtc->rtc_year	= bcd2int(i2c_tp->i2c_rbuf[6]);
564 	rtc->rtc_mon	= bcd2int(i2c_tp->i2c_rbuf[5]);
565 	rtc->rtc_dom	= bcd2int(i2c_tp->i2c_rbuf[4]);
566 	rtc->rtc_dow	= bcd2int(i2c_tp->i2c_rbuf[3]);
567 	rtc->rtc_hrs	= bcd2int(i2c_tp->i2c_rbuf[2]);
568 	rtc->rtc_min	= bcd2int(i2c_tp->i2c_rbuf[1]);
569 	rtc->rtc_sec	= bcd2int(i2c_tp->i2c_rbuf[0]);
570 
571 done:
572 	(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
573 
574 	mutex_exit(&todds1307_rd_lock);
575 	return (i2c_cmd_status);
576 }
577 
578 
579 static int
580 todds1307_write_rtc(struct rtc_t *rtc)
581 {
582 	ds1307_state_t	*statep = NULL;
583 	i2c_transfer_t	*i2c_tp = NULL;
584 	int i2c_cmd_status = I2C_SUCCESS;
585 
586 
587 	if (!todds1307_attach_done) {
588 		return (todds1307_prom_setdate(rtc));
589 	}
590 
591 	statep = ddi_get_soft_state(ds1307_statep, instance);
592 	if (statep == NULL) {
593 		return (DDI_FAILURE);
594 	}
595 
596 	if ((i2c_cmd_status = i2c_transfer_alloc(statep->ds1307_i2c_hdl,
597 				&i2c_tp, 8, 0, I2C_SLEEP)) != I2C_SUCCESS) {
598 		return (i2c_cmd_status);
599 	}
600 
601 	i2c_tp->i2c_version = I2C_XFER_REV;
602 	i2c_tp->i2c_flags = I2C_WR;
603 	i2c_tp->i2c_wbuf[0] = (uchar_t)0x00;
604 	i2c_tp->i2c_wbuf[1] = rtc->rtc_sec;
605 	i2c_tp->i2c_wbuf[2] = rtc->rtc_min;
606 	i2c_tp->i2c_wbuf[3] = rtc->rtc_hrs;
607 	i2c_tp->i2c_wbuf[4] = rtc->rtc_dow;
608 	i2c_tp->i2c_wbuf[5] = rtc->rtc_dom;
609 	i2c_tp->i2c_wbuf[6] = rtc->rtc_mon;
610 	i2c_tp->i2c_wbuf[7] = rtc->rtc_year;
611 	i2c_tp->i2c_wlen = 8;
612 
613 	if ((i2c_cmd_status = i2c_transfer(statep->ds1307_i2c_hdl,
614 					i2c_tp)) != I2C_SUCCESS) {
615 		(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
616 		/* delay(drv_usectohz(I2C_DELAY)); */
617 		drv_usecwait(I2C_DELAY);
618 		return (i2c_cmd_status);
619 	}
620 
621 	tod_read[0] = -1;  /* invalidate saved data from read routine */
622 
623 	(void) i2c_transfer_free(statep->ds1307_i2c_hdl, i2c_tp);
624 
625 	return (i2c_cmd_status);
626 }
627 
628 
629 /*ARGSUSED*/
630 static int
631 todds1307_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
632     void **result)
633 {
634 	ds1307_state_t *softsp;
635 
636 	if (instance == -1) {
637 		return (DDI_FAILURE);
638 	}
639 
640 	switch (infocmd) {
641 	case DDI_INFO_DEVT2DEVINFO:
642 		if ((softsp = ddi_get_soft_state(ds1307_statep, instance))
643 		    == NULL)
644 			return (DDI_FAILURE);
645 		*result = (void *)softsp->dip;
646 		return (DDI_SUCCESS);
647 
648 	case DDI_INFO_DEVT2INSTANCE:
649 		*result = (void *)(uintptr_t)instance;
650 		return (DDI_SUCCESS);
651 
652 	default:
653 		return (DDI_FAILURE);
654 	}
655 }
656 
657 /*
658  * Conversion functions
659  */
660 static unsigned char
661 int2bcd(int num) {
662 	return (((num / 10) << 4)	/* tens BCD digit in high four bits */
663 	+ (num % 10));		/* units digit goes in low four bits */
664 }
665 
666 static int
667 bcd2int(unsigned char num) {
668 	return (((num >> 4) * 10)	/* 10 times high-order four bits */
669 	+ (num & 0x0f));		/* plus low-order four bits */
670 }
671 
672 /*
673  * Finds the device node with device_type "rtc" and opens it to
674  * execute the get-time method
675  */
676 static int
677 todds1307_setup_prom()
678 {
679 	pnode_t todnode;
680 	char tod1307_devpath[MAXNAMELEN];
681 
682 	if ((todnode = prom_findnode_bydevtype(prom_rootnode(),
683 			DS1307_DEVICE_TYPE)) == OBP_NONODE)
684 		return (DDI_FAILURE);
685 
686 	/*
687 	 * We now have the phandle of the rtc node, we need to open the
688 	 * node and get the ihandle
689 	 */
690 	if (prom_phandle_to_path(todnode, tod1307_devpath,
691 			sizeof (tod1307_devpath)) < 0) {
692 		cmn_err(CE_WARN, "prom_phandle_to_path failed");
693 		return (DDI_FAILURE);
694 	}
695 
696 	/*
697 	 * Now open the node and store it's ihandle
698 	 */
699 	if ((todds1307_ihandle = prom_open(tod1307_devpath)) == NULL) {
700 		cmn_err(CE_WARN, "prom_open failed");
701 		return (DDI_FAILURE);
702 	}
703 
704 	return (DDI_SUCCESS);
705 }
706 
707 /*
708  * Closes the prom interface
709  */
710 static void
711 todds1307_rele_prom()
712 {
713 	(void) prom_close(todds1307_ihandle);
714 }
715 
716 /*
717  * Read the date using "get-time" method in rtc node
718  * PROM returns 1969-1999 when reading 69-99 and
719  * 2000-2068 when reading 00-68
720  */
721 static int
722 todds1307_prom_getdate(struct rtc_t *rtc)
723 {
724 	int year;
725 	cell_t ci[12];
726 
727 	ci[0] = p1275_ptr2cell("call-method");  /* Service name */
728 	ci[1] = 2; /* # of arguments */
729 	ci[2] = 7; /* # of result cells */
730 	ci[3] = p1275_ptr2cell("get-time");
731 	ci[4] = p1275_ihandle2cell(todds1307_ihandle);
732 
733 	promif_preprom();
734 	(void) p1275_cif_handler(&ci);
735 	promif_postprom();
736 
737 	year 		= p1275_cell2int(ci[6]);
738 	rtc->rtc_mon	= p1275_cell2int(ci[7]);
739 	rtc->rtc_dom	= p1275_cell2int(ci[8]);
740 	rtc->rtc_dow	= 0;
741 	rtc->rtc_hrs	= p1275_cell2int(ci[9]);
742 	rtc->rtc_min	= p1275_cell2int(ci[10]);
743 	rtc->rtc_sec	= p1275_cell2int(ci[11]);
744 	if (year >= 2000)
745 		year -= 2000;
746 	else
747 		year -= 1900;
748 	rtc->rtc_year	= year;
749 
750 	return (DDI_SUCCESS);
751 }
752 
753 /*
754  * Read the date using "set-time" method in rtc node
755  * For values 00 - 68, write 2000-2068, and for 69-99,
756  * write 1969-1999
757  */
758 static int
759 todds1307_prom_setdate(struct rtc_t *rtc)
760 {
761 	int year;
762 	cell_t ci[12];
763 
764 	year = rtc->rtc_year;
765 
766 	if ((year < 0) || (year > 99))
767 		return (DDI_FAILURE);
768 
769 	if (year <= 68)
770 		year = rtc->rtc_year + 2000;
771 	else
772 		year = rtc->rtc_year + 1900;
773 
774 	ci[0] = p1275_ptr2cell("call-method");  /* Service name */
775 	ci[1] = 8; /* # of arguments */
776 	ci[2] = 0; /* # of result cells */
777 	ci[3] = p1275_ptr2cell("set-time");
778 	ci[4] = p1275_ihandle2cell(todds1307_ihandle);
779 	ci[5] = p1275_int2cell(year);
780 	ci[6] = p1275_int2cell(rtc->rtc_mon);
781 	ci[7] = p1275_int2cell(rtc->rtc_dom);
782 	ci[8] = p1275_int2cell(rtc->rtc_hrs);
783 	ci[9] = p1275_int2cell(rtc->rtc_min);
784 	ci[10] = p1275_int2cell(rtc->rtc_sec);
785 
786 	promif_preprom();
787 	(void) p1275_cif_handler(&ci);
788 	promif_postprom();
789 
790 	return (DDI_SUCCESS);
791 }
792