summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4u/io/todds1337.c
blob: 468846776930da3b87f374faf76749c2e84c68f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <sys/conf.h>
#include <sys/devops.h>
#include <sys/kmem.h>
#include <sys/open.h>
#include <sys/file.h>
#include <sys/note.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>

#include <sys/modctl.h>
#include <sys/stat.h>
#include <sys/clock.h>
#include <sys/reboot.h>
#include <sys/machsystm.h>
#include <sys/poll.h>
#include <sys/pbio.h>
#include <sys/sysmacros.h>

/* Added for prom interface */
#include <sys/promif.h>
#include <sys/promimpl.h>

#include <sys/i2c/misc/i2c_svc.h>
#include <sys/todds1337.h>

#define	DS1337_DEVICE_TYPE	"rtc"

/*
 * Driver entry routines
 */
static int todds1337_attach(dev_info_t *, ddi_attach_cmd_t);
static int todds1337_detach(dev_info_t *, ddi_detach_cmd_t);
static int todds1337_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);

/*
 * tod_ops entry routines
 */
static timestruc_t	todds1337_get(void);
static void		todds1337_set(timestruc_t);
static uint_t		todds1337_set_watchdog_timer(uint_t);
static uint_t		todds1337_clear_watchdog_timer(void);
static void		todds1337_set_power_alarm(timestruc_t);
static void		todds1337_clear_power_alarm(void);
static int		todds1337_setup_prom(void);
static void		todds1337_rele_prom(void);
static int		todds1337_prom_getdate(struct rtc_t *rtc);
static int		todds1337_prom_setdate(struct rtc_t *rtc);

/*
 * Local functions
 */
static int		todds1337_read_rtc(struct rtc_t *);
static int		todds1337_write_rtc(struct rtc_t *);

/* Anchor for soft state structure */
static void	*ds1337_statep;
static int	instance = -1;
static int	todds1337_attach_done = 0;
static kmutex_t	todds1337_rd_lock;
static kmutex_t	todds1337_alarm_lock;
static ihandle_t todds1337_ihandle = 0;

/* one second time out */
#define	I2C_CYCLIC_TIMEOUT	1000000000
uint_t i2c_cyclic_timeout = I2C_CYCLIC_TIMEOUT;
static int sync_clock_once = 1;
static struct	rtc_t	 soft_rtc;

/*
 * cp_ops structure
 */
static struct cb_ops ds1337_cbops = {
	nodev,				/* open */
	nodev,				/* close */
	nodev,				/* strategy */
	nodev,				/* print */
	nodev,				/* dump */
	nodev,				/* read */
	nodev,				/* write */
	nodev,				/* ioctl */
	nodev,				/* devmap */
	nodev,				/* mmap */
	nodev,				/* segmap */
	NULL,				/* poll */
	ddi_prop_op,			/* cb_prop_op */
	NULL,				/* streamtab */
	D_NEW | D_MP,			/* Driver compatibility flag */
	CB_REV,				/* rev */
	nodev,				/* int (*cb_aread)() */
	nodev				/* int (*cb_awrite)() */
};

/*
 * dev_ops structure
 */
static struct dev_ops ds1337_ops = {
	DEVO_REV,		/* devo_rev */
	0,			/* refcnt - reference cnt always set to 0 */
	todds1337_getinfo,	/* getinfo - Maybe requred */
	nulldev,		/* identify */
	nulldev,		/* probe */
	todds1337_attach,	/* attach */
	todds1337_detach,	/* detach */
	nodev,			/* reset */
	&ds1337_cbops,		/* cb_ops - ds1337 does not need this(?) */
	NULL,			/* bus_ops */
	NULL,			/* power */
	ddi_quiesce_not_needed,		/* quiesce */
};

static struct modldrv todds1337_modldrv = {
	&mod_driverops,		/* Type of module. This one is a driver */
	"tod driver for DS1337",	/* Name of the module. */
	&ds1337_ops,			/* Pointer to dev_ops */
};

/*
 * Module linkage structure
 */
static struct modlinkage todds1337_modlinkage = {
	MODREV_1,
	&todds1337_modldrv,
	0
};

int
_init(void)
{
	int error;

	if (strcmp(tod_module_name, "todds1337") == 0) {
		if ((error = ddi_soft_state_init(&ds1337_statep,
		    sizeof (ds1337_state_t), 0)) != DDI_SUCCESS) {
			return (error);
		}

		tod_ops.tod_get = todds1337_get;
		tod_ops.tod_set = todds1337_set;
		tod_ops.tod_set_watchdog_timer = todds1337_set_watchdog_timer;
		tod_ops.tod_clear_watchdog_timer =
		    todds1337_clear_watchdog_timer;
		tod_ops.tod_set_power_alarm = todds1337_set_power_alarm;
		tod_ops.tod_clear_power_alarm = todds1337_clear_power_alarm;
	}

	(void) todds1337_setup_prom();

	/*
	 * Install the module
	 */
	if ((error = mod_install(&todds1337_modlinkage)) != 0) {
		if (strcmp(tod_module_name, "todds1337") == 0) {
			ddi_soft_state_fini(&ds1337_statep);
		}
		todds1337_rele_prom();
		return (error);
	}
	mutex_init(&todds1337_rd_lock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&todds1337_alarm_lock, NULL, MUTEX_DEFAULT, NULL);

	return (0);
}

int
_fini(void)
{
	int error = 0;

	if (strcmp(tod_module_name, "todds1337") == 0) {
		error = EBUSY;
	} else {
		if ((error = mod_remove(&todds1337_modlinkage)) == 0) {
			mutex_destroy(&todds1337_rd_lock);
			mutex_destroy(&todds1337_alarm_lock);
			todds1337_rele_prom();
		}
	}

	return (error);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&todds1337_modlinkage, modinfop));
}

/*
 * cyclical call to get tod.
 */
static void
todds1337_cyclic(void *arg)
{

	(void) todds1337_read_rtc((struct rtc_t *)arg);

}

/*
 * register ds1337 client device with i2c services, and
 * allocate & initialize soft state structure.
 */
static int
todds1337_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
	static ds1337_state_t	*statep = NULL;
	i2c_transfer_t	*i2c_tp = NULL;
	uint8_t tempVal = (uint8_t)0;
	switch (cmd) {
	case DDI_ATTACH:
		break;
	case DDI_RESUME:
		return (DDI_SUCCESS);
	default:
		return (DDI_FAILURE);
	}

	if (instance != -1) {
		cmn_err(CE_WARN, "todds1337_attach: wrong instance");
		return (DDI_FAILURE);
	}

	instance = ddi_get_instance(dip);

	/*
	 * Allocate soft state structure
	 */
	if (ddi_soft_state_zalloc(ds1337_statep, instance) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "todds1337_attach: cannot allocate soft "
		    "state");
		instance = -1;
		return (DDI_FAILURE);
	}

	statep = ddi_get_soft_state(ds1337_statep, instance);
	if (statep == NULL) {
		cmn_err(CE_WARN, "todds1337_attach: cannot acquire soft "
		    "state");
		instance = -1;
		return (DDI_FAILURE);
	}

	statep->dip = dip;

	if (i2c_client_register(dip, &statep->ds1337_i2c_hdl) != I2C_SUCCESS) {
		ddi_soft_state_free(ds1337_statep, instance);
		cmn_err(CE_WARN, "todds1337_attach: cannot register i2c "
		    "client");
		instance = -1;
		return (DDI_FAILURE);
	}

	/* check and initialize the oscillator */

	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
	    &i2c_tp, 1, 1, I2C_SLEEP);
	i2c_tp->i2c_version = I2C_XFER_REV;
	i2c_tp->i2c_flags = I2C_WR_RD;
	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */
	i2c_tp->i2c_wlen = 1;
	i2c_tp->i2c_rlen = 1;

	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
		i2c_client_unregister(statep->ds1337_i2c_hdl);
		ddi_soft_state_free(ds1337_statep, instance);
		cmn_err(CE_WARN, "todds1337_attach: failed to read DS1337 "
		    "status register");
		instance = -1;
		return (DDI_FAILURE);
	}

	tempVal = i2c_tp->i2c_rbuf[0];

	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);

	/*
	 * Check Oscillator and initialize chip if OBP failed to do it
	 */

	if (tempVal & RTC_CTL_EOSC) {
		(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp,
		    2, 0, I2C_SLEEP);
		i2c_tp->i2c_version = I2C_XFER_REV;
		i2c_tp->i2c_flags = I2C_WR;
		i2c_tp->i2c_wbuf[0] = RTC_CTL; /* Write Control register */
		i2c_tp->i2c_wbuf[1] = (uchar_t)(RTC_CTL_RS2 | RTC_CTL_RS1 |
		    RTC_CTL_INTCN);
		if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp))
		    != I2C_SUCCESS) {
			(void) i2c_transfer_free(statep->ds1337_i2c_hdl,
			    i2c_tp);
			i2c_client_unregister(statep->ds1337_i2c_hdl);
			ddi_soft_state_free(ds1337_statep, instance);
			cmn_err(CE_WARN, "todds1337_attach: failed to write "
			    "DS1337 control register");
			instance = -1;
			return (DDI_FAILURE);
		}

		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);

		/*
		 * Now reset the OSF flag in the Status register
		 */
		(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp,
		    2, 0, I2C_SLEEP);
		i2c_tp->i2c_version = I2C_XFER_REV;
		i2c_tp->i2c_flags = I2C_WR;
		i2c_tp->i2c_wbuf[0] = RTC_STATUS;
		i2c_tp->i2c_wbuf[1] = (uchar_t)0;
		if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp))
		    != I2C_SUCCESS) {
			(void) i2c_transfer_free(statep->ds1337_i2c_hdl,
			    i2c_tp);
			i2c_client_unregister(statep->ds1337_i2c_hdl);
			ddi_soft_state_free(ds1337_statep, instance);
			cmn_err(CE_WARN, "todds1337_attach: failed to write "
			    "DS1337 status register");
			instance = -1;
			return (DDI_FAILURE);
		}

		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
	}

	/*
	 * Create a periodical handler to read TOD.
	 */
	ASSERT(statep->cycid == NULL);
	statep->cycid = ddi_periodic_add(todds1337_cyclic, &soft_rtc,
	    i2c_cyclic_timeout, DDI_IPL_1);
	statep->state = TOD_ATTACHED;
	todds1337_attach_done = 1;
	ddi_report_dev(dip);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
static int
todds1337_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	/*
	 * Once attached, do not allow detach because the system constantly
	 * calling todds1337_get() to get the time.  If the driver is detached
	 * and the system try to get the time, the system will have memory
	 * problem.
	 *
	 */
	switch (cmd) {
	case DDI_SUSPEND:
		return (DDI_SUCCESS);
	default:
		return (DDI_FAILURE);
	}
}

/* *********************** tod_ops entry points ******************** */

/*
 * Read the current time from the DS1337 chip and convert to UNIX form.
 * Should be called with tod_lock held.
 */

static timestruc_t
todds1337_get(void)
{
	timestruc_t	ts;
	todinfo_t	tod;
	struct	rtc_t	rtc;

	ASSERT(MUTEX_HELD(&tod_lock));

	if (sync_clock_once) {
		(void) todds1337_read_rtc(&soft_rtc);
		sync_clock_once = 0;
	} else {
		tod_status_set(TOD_GET_FAILED);
		return (hrestime);
	}

	bcopy(&soft_rtc, &rtc, sizeof (rtc));

	/*
	 * 00 - 68 = 2000 thru 2068
	 * 69-99 = 1969 thru 1999
	 */
	tod.tod_year    = rtc.rtc_year;
	if (rtc.rtc_year <= 68)
		tod.tod_year += 100;
	tod.tod_month	= rtc.rtc_mon;
	tod.tod_day	= rtc.rtc_dom;
	tod.tod_dow	= rtc.rtc_dow;
	tod.tod_hour	= rtc.rtc_hrs;
	tod.tod_min	= rtc.rtc_min;
	tod.tod_sec	= rtc.rtc_sec;

	/* read was successful so ensure failure flag is clear */
	tod_status_clear(TOD_GET_FAILED);

	ts.tv_sec = tod_to_utc(tod);
	ts.tv_nsec = 0;
	return (ts);
}

/*
 * Program DS1337 with the specified time.
 * Must be called with tod_lock held. The TOD
 * chip supports date from 1969-2068 only. We must
 * reject requests to set date below 1969.
 */
static void
todds1337_set(timestruc_t ts)
{
	struct rtc_t	rtc;
	todinfo_t	tod = utc_to_tod(ts.tv_sec);
	int		year;


	ASSERT(MUTEX_HELD(&tod_lock));

	/*
	 * Year is base 1900, valid year range 1969-2068
	 */
	if ((tod.tod_year < 69) || (tod.tod_year > 168))
		return;

	year = tod.tod_year;
	if (year >= 100)
		year -= 100;

	rtc.rtc_year	= (uint8_t)year;
	rtc.rtc_mon	= (uint8_t)tod.tod_month;
	rtc.rtc_dom	= (uint8_t)tod.tod_day;
	rtc.rtc_dow	= (uint8_t)tod.tod_dow;
	rtc.rtc_hrs	= (uint8_t)tod.tod_hour;
	rtc.rtc_min	= (uint8_t)tod.tod_min;
	rtc.rtc_sec	= (uint8_t)tod.tod_sec;

	(void) todds1337_write_rtc(&rtc);
}

/*
 * Program ds1337 registers for alarm to go off at the specified time.
 * Alarm #1 is used (Alarm #2 stays idle)
 */
/* ARGSUSED */
static void
todds1337_set_power_alarm(timestruc_t ts)
{
	todinfo_t	tod;
	ds1337_state_t	*statep = NULL;
	i2c_transfer_t	*i2c_tp = NULL;
	uint8_t tmpval;

	ASSERT(MUTEX_HELD(&tod_lock));

	if (!todds1337_attach_done) {
		cmn_err(CE_WARN, "todds1337: driver not attached");
		return;
	}

	statep = ddi_get_soft_state(ds1337_statep, instance);
	if (statep == NULL) {
		cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failed");
		return;
	}

	tod = utc_to_tod(ts.tv_sec);

	/*
	 * i2c_transfe() may block; to avoid locking clock() which
	 * is running in interrupt context at PIL10 we temporarely exit
	 * the tod_mutex and enter alarm lock. Time/date and alarm hardware
	 * have non-interlsecting registers, it is safe to use different locks.
	 */
	mutex_exit(&tod_lock);
	mutex_enter(&todds1337_alarm_lock);

	/*
	 * Disable Power Alarm (A1IE)
	 */
	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
	    &i2c_tp, 1, 1, I2C_SLEEP);
	i2c_tp->i2c_version = I2C_XFER_REV;
	i2c_tp->i2c_flags = I2C_WR_RD;
	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */

	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
		cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to read "
		    "DS1337 control register");
		mutex_exit(&todds1337_alarm_lock);
		mutex_enter(&tod_lock);
		return;
	}

	tmpval = i2c_tp->i2c_rbuf[0];

	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);

	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
	    &i2c_tp, 2, 0, I2C_SLEEP);
	i2c_tp->i2c_version = I2C_XFER_REV;
	i2c_tp->i2c_flags = I2C_WR;
	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */
	i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE;

	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
		cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write "
		    "DS1337control register");
		mutex_exit(&todds1337_alarm_lock);
		mutex_enter(&tod_lock);
		return;
	}

	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);


	/*
	 * Write Alarm #1 registers
	 */

	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
	    &i2c_tp, 5, 0, I2C_SLEEP);
	i2c_tp->i2c_version = I2C_XFER_REV;
	i2c_tp->i2c_flags = I2C_WR;
	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_ALARM_SEC; /* Alarm #1 Seconds */
	i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(tod.tod_sec);
	i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(tod.tod_min);
	i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(tod.tod_hour);
	i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(tod.tod_day);

	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
		cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write "
		    "DS1337 alarm registers");
		mutex_exit(&todds1337_alarm_lock);
		mutex_enter(&tod_lock);
		return;
	}

	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);


	/*
	 * Enable Power Alarm
	 */
	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
	    &i2c_tp, 2, 0, I2C_SLEEP);
	i2c_tp->i2c_version = I2C_XFER_REV;
	i2c_tp->i2c_flags = I2C_WR;
	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */
	i2c_tp->i2c_wbuf[1] = tmpval | RTC_CTL_A1IE;

	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
		cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to enable "
		    "DS1337 alarm");
		mutex_exit(&todds1337_alarm_lock);
		mutex_enter(&tod_lock);
		return;
	}

	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);

	mutex_exit(&todds1337_alarm_lock);
	mutex_enter(&tod_lock);
}

/* ARGSUSED */
static void
todds1337_clear_power_alarm(void)
{
	ds1337_state_t	*statep = NULL;
	i2c_transfer_t	*i2c_tp = NULL;
	uint8_t tmpval;

	ASSERT(MUTEX_HELD(&tod_lock));

	if (!todds1337_attach_done) {
		cmn_err(CE_WARN, "todds1337: driver was not attached");
		return;
	}

	statep = ddi_get_soft_state(ds1337_statep, instance);
	if (statep == NULL) {
		cmn_err(CE_WARN, "todds1337: ddi_get_soft_state has failed");
		return;
	}

	/*
	 * i2c_transfe() may block; to avoid locking clock() which
	 * is running in interrupt context at PIL10 we temporarely exit
	 * the tod_mutex and enter alarm lock. Time/date and alarm hardware
	 * have non-interlsecting registers, it is safe to use different locks.
	 */
	mutex_exit(&tod_lock);
	mutex_enter(&todds1337_alarm_lock);

	/*
	 * Disable Alarm #1 Interrupt
	 */
	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
	    &i2c_tp, 1, 1, I2C_SLEEP);
	i2c_tp->i2c_version = I2C_XFER_REV;
	i2c_tp->i2c_flags = I2C_WR_RD;
	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */

	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
		cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read "
		    "DS1337 control register");
		mutex_exit(&todds1337_alarm_lock);
		mutex_enter(&tod_lock);
		return;
	}

	tmpval = i2c_tp->i2c_rbuf[0];

	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);

	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
	    &i2c_tp, 2, 0, I2C_SLEEP);
	i2c_tp->i2c_version = I2C_XFER_REV;
	i2c_tp->i2c_flags = I2C_WR;
	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */
	i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE;

	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
		cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write "
		    "DS1337 control register");
		mutex_exit(&todds1337_alarm_lock);
		mutex_enter(&tod_lock);
		return;
	}

	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);

	/*
	 * Reset Alarm #1 Flag
	 */
	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
	    &i2c_tp, 1, 1, I2C_SLEEP);
	i2c_tp->i2c_version = I2C_XFER_REV;
	i2c_tp->i2c_flags = I2C_WR_RD;
	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */

	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
		cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read "
		    "DS1337 status register");
		mutex_exit(&todds1337_alarm_lock);
		mutex_enter(&tod_lock);
		return;
	}

	tmpval = i2c_tp->i2c_rbuf[0];

	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);

	(void) i2c_transfer_alloc(statep->ds1337_i2c_hdl,
	    &i2c_tp, 2, 0, I2C_SLEEP);
	i2c_tp->i2c_version = I2C_XFER_REV;
	i2c_tp->i2c_flags = I2C_WR;
	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Write Status register */
	i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_STATUS_A1F;

	if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) {
		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
		cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write "
		    "DS1337 status register");
		mutex_exit(&todds1337_alarm_lock);
		mutex_enter(&tod_lock);
		return;
	}

	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);

	mutex_exit(&todds1337_alarm_lock);
	mutex_enter(&tod_lock);
}

/* ARGSUSED */
static uint_t
todds1337_set_watchdog_timer(uint_t timeoutval)
{
	ASSERT(MUTEX_HELD(&tod_lock));
	return (0);
}

/* ARGSUSED */
static uint_t
todds1337_clear_watchdog_timer(void)
{
	ASSERT(MUTEX_HELD(&tod_lock));
	return (0);
}

/* ********************** Local functions ***************************** */

static char tod_read[7] = {-1, -1, -1, -1, -1, -1, -1};
static int
todds1337_read_rtc(struct rtc_t *rtc)
{
	static	ds1337_state_t	*statep = NULL;
	i2c_transfer_t	*i2c_tp = NULL;
	int i2c_cmd_status = I2C_FAILURE;
	int counter = 4;

	if (!todds1337_attach_done) {
		return (todds1337_prom_getdate(rtc));
	}

	statep = ddi_get_soft_state(ds1337_statep, instance);
	if (statep == NULL) {
		cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failing");
		return (DDI_FAILURE);
	}

	mutex_enter(&todds1337_rd_lock);

	/*
	 * Allocate 1 byte for write buffer and 7 bytes for read buffer to
	 * to accomodate sec, min, hrs, dayOfWeek, dayOfMonth, year
	 */
	if ((i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp, 1,
	    7, I2C_SLEEP)) != I2C_SUCCESS) {
		mutex_exit(&todds1337_rd_lock);
		return (DDI_FAILURE);
	}

	do {
		i2c_tp->i2c_version = I2C_XFER_REV;
		i2c_tp->i2c_flags = I2C_WR_RD;
		i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC; /* Start from 0x00 */
		i2c_tp->i2c_wlen = 1;	/* Write one byte address */
		i2c_tp->i2c_rlen = 7;	/* Read 7 regs */

		if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl,
		    i2c_tp)) != I2C_SUCCESS) {
			goto done;
		}
		/* for first read, need to get valid data */
		while (tod_read[0] == -1 && counter > 0) {
		/* move data to static buffer */
		bcopy(i2c_tp->i2c_rbuf, tod_read, 7);

		/* now read again */
		/* Start reading reg from 0x00 */
		i2c_tp->i2c_wbuf[0] = (uchar_t)0x00;
		i2c_tp->i2c_wlen = 1;	/* Write one byte address */
		i2c_tp->i2c_rlen = 7;	/* Read 7 regs */
		if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl,
		    i2c_tp)) != I2C_SUCCESS) {
			goto done;
		}
		/* if they are not the same, then read again */
		if (bcmp(tod_read, i2c_tp->i2c_rbuf, 7) != 0) {
			tod_read[0] = -1;
			counter--;
		}
	}

	} while (i2c_tp->i2c_rbuf[0] == 0x59 &&
	    /* if seconds register is 0x59 (BCD), add data should match */
	    bcmp(&tod_read[1], &i2c_tp->i2c_rbuf[1], 6) != 0 &&
	    counter-- > 0);

	if (counter < 0)
		cmn_err(CE_WARN, "i2ctod: TOD Chip failed ??");

	/* move data to static buffer */
	bcopy(i2c_tp->i2c_rbuf, tod_read, 7);


	rtc->rtc_year	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[6]);
	rtc->rtc_mon	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[5]);
	rtc->rtc_dom	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[4]);
	rtc->rtc_dow	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[3]);
	rtc->rtc_hrs	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[2]);
	rtc->rtc_min	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[1]);
	rtc->rtc_sec	= BCD_TO_BYTE(i2c_tp->i2c_rbuf[0]);

done:
	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);

	mutex_exit(&todds1337_rd_lock);
	return (i2c_cmd_status);
}


static int
todds1337_write_rtc(struct rtc_t *rtc)
{
	ds1337_state_t	*statep = NULL;
	i2c_transfer_t	*i2c_tp = NULL;
	int i2c_cmd_status = I2C_SUCCESS;


	if (!todds1337_attach_done) {
		return (todds1337_prom_setdate(rtc));
	}

	statep = ddi_get_soft_state(ds1337_statep, instance);
	if (statep == NULL) {
		return (DDI_FAILURE);
	}

	if ((i2c_cmd_status = i2c_transfer_alloc(statep->ds1337_i2c_hdl,
	    &i2c_tp, 8, 0, I2C_SLEEP)) != I2C_SUCCESS) {
		return (i2c_cmd_status);
	}

	i2c_tp->i2c_version = I2C_XFER_REV;
	i2c_tp->i2c_flags = I2C_WR;
	i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC;
	i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(rtc->rtc_sec);
	i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(rtc->rtc_min);
	i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(rtc->rtc_hrs);
	i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(rtc->rtc_dow);
	i2c_tp->i2c_wbuf[5] = BYTE_TO_BCD(rtc->rtc_dom);
	i2c_tp->i2c_wbuf[6] = BYTE_TO_BCD(rtc->rtc_mon);
	i2c_tp->i2c_wbuf[7] = BYTE_TO_BCD(rtc->rtc_year);
	i2c_tp->i2c_wlen = 8;

	if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl,
	    i2c_tp)) != I2C_SUCCESS) {
		(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);
		return (i2c_cmd_status);
	}

	tod_read[0] = -1;  /* invalidate saved data from read routine */

	(void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp);

	return (i2c_cmd_status);
}


/*ARGSUSED*/
static int
todds1337_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
    void **result)
{
	ds1337_state_t *softsp;

	if (instance == -1) {
		return (DDI_FAILURE);
	}

	switch (infocmd) {
	case DDI_INFO_DEVT2DEVINFO:
		if ((softsp = ddi_get_soft_state(ds1337_statep, instance)) ==
		    NULL)
			return (DDI_FAILURE);
		*result = (void *)softsp->dip;
		return (DDI_SUCCESS);
	case DDI_INFO_DEVT2INSTANCE:
		*result = (void *)(uintptr_t)instance;
		return (DDI_SUCCESS);
	default:
		return (DDI_FAILURE);
	}
}

/*
 * Finds the device node with device_type "rtc" and opens it to
 * execute the get-time method
 */
static int
todds1337_setup_prom(void)
{
	pnode_t todnode;
	char tod1337_devpath[MAXNAMELEN];

	if ((todnode = prom_findnode_bydevtype(prom_rootnode(),
	    DS1337_DEVICE_TYPE)) == OBP_NONODE)
		return (DDI_FAILURE);

	/*
	 * We now have the phandle of the rtc node, we need to open the
	 * node and get the ihandle
	 */
	if (prom_phandle_to_path(todnode, tod1337_devpath,
	    sizeof (tod1337_devpath)) < 0) {
		cmn_err(CE_WARN, "prom_phandle_to_path failed");
		return (DDI_FAILURE);
	}

	/*
	 * Now open the node and store it's ihandle
	 */
	if ((todds1337_ihandle = prom_open(tod1337_devpath)) == 0) {
		cmn_err(CE_WARN, "prom_open failed");
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*
 * Closes the prom interface
 */
static void
todds1337_rele_prom(void)
{
	(void) prom_close(todds1337_ihandle);
}

/*
 * Read the date using "get-time" method in rtc node
 * PROM returns 1969-1999 when reading 69-99 and
 * 2000-2068 when reading 00-68
 */
static int
todds1337_prom_getdate(struct rtc_t *rtc)
{
	int year;
	cell_t ci[12];

	ci[0] = p1275_ptr2cell("call-method");  /* Service name */
	ci[1] = 2; /* # of arguments */
	ci[2] = 7; /* # of result cells */
	ci[3] = p1275_ptr2cell("get-time");
	ci[4] = p1275_ihandle2cell(todds1337_ihandle);

	promif_preprom();
	(void) p1275_cif_handler(&ci);
	promif_postprom();

	year		= p1275_cell2int(ci[6]);
	rtc->rtc_mon	= p1275_cell2int(ci[7]);
	rtc->rtc_dom	= p1275_cell2int(ci[8]);
	rtc->rtc_dow	= 0;
	rtc->rtc_hrs	= p1275_cell2int(ci[9]);
	rtc->rtc_min	= p1275_cell2int(ci[10]);
	rtc->rtc_sec	= p1275_cell2int(ci[11]);
	if (year >= 2000)
		year -= 2000;
	else
		year -= 1900;
	rtc->rtc_year	= year;

	return (DDI_SUCCESS);
}

/*
 * Read the date using "set-time" method in rtc node
 * For values 00 - 68, write 2000-2068, and for 69-99,
 * write 1969-1999
 */
static int
todds1337_prom_setdate(struct rtc_t *rtc)
{
	int year;
	cell_t ci[12];

	year = rtc->rtc_year;

	if ((year < 0) || (year > 99))
		return (DDI_FAILURE);

	if (year <= 68)
		year = rtc->rtc_year + 2000;
	else
		year = rtc->rtc_year + 1900;

	ci[0] = p1275_ptr2cell("call-method");  /* Service name */
	ci[1] = 8; /* # of arguments */
	ci[2] = 0; /* # of result cells */
	ci[3] = p1275_ptr2cell("set-time");
	ci[4] = p1275_ihandle2cell(todds1337_ihandle);
	ci[5] = p1275_int2cell(year);
	ci[6] = p1275_int2cell(rtc->rtc_mon);
	ci[7] = p1275_int2cell(rtc->rtc_dom);
	ci[8] = p1275_int2cell(rtc->rtc_hrs);
	ci[9] = p1275_int2cell(rtc->rtc_min);
	ci[10] = p1275_int2cell(rtc->rtc_sec);

	promif_preprom();
	(void) p1275_cif_handler(&ci);
	promif_postprom();

	return (DDI_SUCCESS);
}