summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4u/io/todds1287.c
blob: 64ecff8a8da19aa791c7893c48794a0bd8676866 (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
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
/*
 * 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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


/*
 *	The "todds1287" module has implementation for both tod
 *	and power button (pbio) interfaces.  This driver controls
 *	RTC & APC units of National Semiconductor's 87317 SuperI/O
 *	chip.  The tod interface accesses the RTC unit and pbio
 *	interface accesses the APC unit of SuperI/O.  Since both
 *	units are implemented in the same Logical Device, registers
 *	for both units are accessible through a common set of index
 *	address & data registers.  That is why both interfaces are
 *	implemented in a same driver.
 *
 *	The APC unit is used to implement the power button.  When the
 *	button momentarily is pressed, an interrupt is generated and
 *	at the same time a Fail-safe timer starts to run.  If the
 *	timer is not stopped in 21 seconds, the power to system is
 *	turned off.  So the first task in the interrupt handler is to
 *	reset the Fail-safe timer.  Note that OBP is not clearing
 *	the Fail-safe timer due to limitation in handling interrupts,
 *	so when OBP is running, the power button should be pressed
 *	and held for 4 seconds for the power to go off, otherwise
 *	a momentarily press will delay the power-off for 21 seconds.
 *
 *	PSARC/1999/393 describes the pbio(4I) interface.
 */

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

#include <sys/todds1287.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>

#define	ABORT_INCREMENT_DELAY	10

static timestruc_t todds_get(void);
static void todds_set(timestruc_t);
static uint_t todds_set_watchdog_timer(uint_t);
static uint_t todds_clear_watchdog_timer(void);
static void todds_set_power_alarm(timestruc_t);
static void todds_clear_power_alarm(void);
static uint64_t todds_get_cpufrequency(void);

extern uint64_t find_cpufrequency(volatile uint8_t *);

/*
 * External variables
 */
extern int	watchdog_activated;
extern uint_t	watchdog_timeout_seconds;
extern volatile uint8_t	*v_pmc_addr_reg;

/*
 * Global variables
 */
int ds1287_debug_flags;
int ds1287_caddr_warn;

/*
 * cb ops
 */
static int ds1287_open(dev_t *, int, int, cred_t *);
static int ds1287_close(dev_t, int, int, cred_t *);
static int ds1287_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
static int ds1287_chpoll(dev_t, short, int, short *, struct pollhead **);

static void read_rtc(struct rtc_t *);
static void write_rtc_time(struct rtc_t *);
static void write_rtc_alarm(struct rtc_t *);
static void select_bank(int bank);
static uint_t ds1287_intr(caddr_t);
static uint_t ds1287_softintr(caddr_t);
static void ds1287_timeout(caddr_t);
static uint_t ds1287_issue_shutdown(caddr_t);
static void ds1287_log_message(void);

static struct cb_ops ds1287_cbops = {
	ds1287_open,			/* open */
	ds1287_close,			/* close */
	nodev,				/* strategy */
	nodev,				/* print */
	nodev,				/* dump */
	nodev,				/* read */
	nodev,				/* write */
	ds1287_ioctl,			/* ioctl */
	nodev,				/* devmap */
	nodev,				/* mmap */
	nodev,				/* segmap */
	ds1287_chpoll,			/* 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
 */
static int ds1287_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int ds1287_attach(dev_info_t *, ddi_attach_cmd_t);
static int ds1287_detach(dev_info_t *, ddi_detach_cmd_t);

static struct dev_ops ds1287_ops = {
	DEVO_REV,			/* devo_rev */
	0,				/* refcnt */
	ds1287_getinfo,			/* getinfo */
	nulldev,			/* identify */
	nulldev,			/* probe */
	ds1287_attach,			/* attach */
	ds1287_detach,			/* detach */
	nodev,				/* reset */
	&ds1287_cbops,			/* cb_ops */
	(struct bus_ops *)NULL,		/* bus_ops */
	NULL,				/* power */
	ddi_quiesce_not_supported,	/* devo_quiesce */
};


static void	*ds1287_state;
static int	instance = -1;

/* Driver Tunables */
static int	ds1287_interrupt_priority = 15;
static int	ds1287_softint_priority = 2;
static hrtime_t power_button_debounce = MSEC2NSEC(10);
static hrtime_t power_button_abort_interval = 1.5 * NANOSEC;
static int	power_button_abort_presses = 3;
static int	power_button_abort_enable = 1;
static int	power_button_enable = 1;

static int	power_button_pressed = 0;
static int	power_button_cancel = 0;
static int	power_button_timeouts = 0;
static int	timeout_cancel = 0;
static int	additional_presses = 0;

static ddi_iblock_cookie_t ds1287_lo_iblock;
static ddi_iblock_cookie_t ds1287_hi_iblock;
static ddi_softintr_t	ds1287_softintr_id;
static kmutex_t ds1287_reg_mutex;	/* Protects ds1287 Registers */

static struct modldrv modldrv = {
	&mod_driverops,		/* Type of module. This one is a driver */
	"ds1287 clock driver",	/* Name of the module. */
	&ds1287_ops,		/* driver ops */
};

static struct modlinkage modlinkage = {
	MODREV_1, &modldrv, NULL
};


int
_init(void)
{
	int status;

	status = ddi_soft_state_init(&ds1287_state, sizeof (struct ds1287), 0);
	if (status != 0) {
		return (status);
	}

	if ((status = mod_install(&modlinkage)) != 0) {
		ddi_soft_state_fini(&ds1287_state);
		return (status);
	}


	ds1287_hi_iblock = (ddi_iblock_cookie_t)(uintptr_t)
	    ipltospl(ds1287_interrupt_priority);
	mutex_init(&ds1287_reg_mutex, NULL, MUTEX_DRIVER, ds1287_hi_iblock);

	mutex_enter(&ds1287_reg_mutex);
	/* Select Bank 1 */
	select_bank(1);
	DS1287_ADDR_REG = RTC_B;
	DS1287_DATA_REG = (RTC_DM | RTC_HM);
	mutex_exit(&ds1287_reg_mutex);

	tod_ops.tod_get = todds_get;
	tod_ops.tod_set = todds_set;

	/*
	 * If v_pmc_addr_reg isn't set, it's because it wasn't set in
	 * sun4u/os/fillsysinfo.c:have_pmc(). This means the real (pmc)
	 * watchdog routines (sun4u/io/pmc.c) will not be used. If the
	 * user were to set watchdog_enable in /etc/system, we'll need to
	 * use our own NOP routines.
	 */
	if (v_pmc_addr_reg == NULL) {
		tod_ops.tod_set_watchdog_timer = todds_set_watchdog_timer;
		tod_ops.tod_clear_watchdog_timer = todds_clear_watchdog_timer;
	}
	tod_ops.tod_set_power_alarm = todds_set_power_alarm;
	tod_ops.tod_clear_power_alarm = todds_clear_power_alarm;
	tod_ops.tod_get_cpufrequency = todds_get_cpufrequency;

	return (status);
}

int
_fini(void)
{
	if (strcmp(tod_module_name, "todds1287") == 0)
		return (EBUSY);

	return (mod_remove(&modlinkage));
}

/*
 * The loadable-module _info(9E) entry point
 */
int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

/*ARGSUSED*/
static int
ds1287_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
    void **result)
{
	struct ds1287 *softsp;

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

	switch (infocmd) {
	case DDI_INFO_DEVT2DEVINFO:
		if ((softsp = ddi_get_soft_state(ds1287_state, 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);
	}
}

static int
ds1287_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
	struct ds1287 *softsp;

	DPRINTF("ds1287_attach\n");
	switch (cmd) {
	case DDI_ATTACH:
		break;
	case DDI_RESUME:
		return (DDI_SUCCESS);
	default:
		return (DDI_FAILURE);
	}

	if (instance != -1) {
		cmn_err(CE_WARN, "ds1287_attach: Another instance is already "
		    "attached.");
		return (DDI_FAILURE);
	}

	instance = ddi_get_instance(dip);

	if (v_rtc_addr_reg == NULL) {
		cmn_err(CE_WARN, "ds1287_attach: v_rtc_addr_reg is NULL");
		return (DDI_FAILURE);
	}

	/*
	 * Allocate softc information.
	 */
	if (ddi_soft_state_zalloc(ds1287_state, instance) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "ds1287_attach: Failed to allocate "
		    "soft states.");
		return (DDI_FAILURE);
	}

	softsp = ddi_get_soft_state(ds1287_state, instance);
	DPRINTF("ds1287_attach: instance=%d softsp=0x%p\n", instance,
	    (void *)softsp);

	softsp->dip = dip;

	if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
	    "interrupt-priorities", (caddr_t)&ds1287_interrupt_priority,
	    sizeof (int)) != DDI_PROP_SUCCESS) {
		cmn_err(CE_WARN, "ds1287_attach: Failed to create \""
		    "interrupt-priorities\" property.");
		goto error;
	}

	/* add the softint */
	ds1287_lo_iblock = (ddi_iblock_cookie_t)(uintptr_t)
	    ipltospl(ds1287_softint_priority);

	if (ddi_add_softintr(dip, DDI_SOFTINT_FIXED, &ds1287_softintr_id,
	    &ds1287_lo_iblock, NULL, ds1287_softintr, (caddr_t)softsp) !=
	    DDI_SUCCESS) {
		cmn_err(CE_WARN, "ds1287_attach: Failed to add low interrupt.");
		goto error1;
	}

	/* add the hi interrupt */
	if (ddi_add_intr(dip, 0, NULL, (ddi_idevice_cookie_t *)
	    &ds1287_hi_iblock, ds1287_intr, NULL) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "ds1287_attach: Failed to add high "
		    "interrupt.");
		goto error2;
	}

	/*
	 * Combination of instance number and clone number 0 is used for
	 * creating the minor node.
	 */
	if (ddi_create_minor_node(dip, "power_button", S_IFCHR,
	    (instance << 8) + 0, "ddi_power_button", 0) == DDI_FAILURE) {
		cmn_err(CE_WARN, "ds1287_attach: Failed to create minor node");
		goto error3;
	}

	ddi_report_dev(dip);

	return (DDI_SUCCESS);

error3:
	ddi_remove_intr(dip, 0, NULL);
error2:
	ddi_remove_softintr(ds1287_softintr_id);
error1:
	(void) ddi_prop_remove(DDI_DEV_T_NONE, dip, "interrupt-priorities");
error:
	ddi_soft_state_free(ds1287_state, instance);
	return (DDI_FAILURE);
}

/*ARGSUSED*/
static int
ds1287_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	DPRINTF("ds1287_detach\n");
	switch (cmd) {
	case DDI_DETACH:
		/*
		 * Since it needs to always handle the power button, fail
		 * to detach.
		 */
		return (DDI_FAILURE);
	case DDI_SUSPEND:
		return (DDI_SUCCESS);
	default:
		return (DDI_FAILURE);
	}
}

/*ARGSUSED1*/
static int
ds1287_open(dev_t *devp, int flags, int otyp, cred_t *credp)
{
	struct ds1287 *softsp;
	int clone;

	if (otyp != OTYP_CHR)
		return (EINVAL);

	if ((softsp = ddi_get_soft_state(ds1287_state, instance)) ==
	    NULL)
		return (ENXIO);

	mutex_enter(&softsp->ds1287_mutex);
	for (clone = 1; clone < DS1287_MAX_CLONE; clone++)
		if (!softsp->clones[clone])
			break;

	if (clone == DS1287_MAX_CLONE) {
		cmn_err(CE_WARN, "ds1287_open: No more allocation left "
		    "to clone a minor.");
		mutex_exit(&softsp->ds1287_mutex);
		return (ENXIO);
	}

	*devp = makedevice(getmajor(*devp), (instance << 8) + clone);
	softsp->clones[clone] = 1;
	mutex_exit(&softsp->ds1287_mutex);

	return (0);
}

/*ARGSUSED*/
static int
ds1287_close(dev_t dev, int flags, int otyp, cred_t *credp)
{
	struct ds1287 *softsp;
	int clone;

	if (otyp != OTYP_CHR)
		return (EINVAL);

	if ((softsp = ddi_get_soft_state(ds1287_state, instance)) ==
	    NULL)
		return (ENXIO);

	clone = DS1287_MINOR_TO_CLONE(getminor(dev));
	mutex_enter(&softsp->ds1287_mutex);
	if (softsp->monitor_on == clone)
		softsp->monitor_on = 0;
	softsp->clones[clone] = 0;
	mutex_exit(&softsp->ds1287_mutex);

	return (0);
}

/*ARGSUSED4*/
static int
ds1287_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
    cred_t *credp, int *rvalp)
{
	struct ds1287 *softsp;
	int clone;

	if ((softsp = ddi_get_soft_state(ds1287_state, instance)) ==
	    NULL)
		return (ENXIO);

	clone = DS1287_MINOR_TO_CLONE(getminor(dev));
	switch (cmd) {
	case PB_BEGIN_MONITOR:
		DPRINTF("ds1287_ioctl: PB_BEGIN_MONITOR is called.\n");
		mutex_enter(&softsp->ds1287_mutex);
		if (softsp->monitor_on) {
			mutex_exit(&softsp->ds1287_mutex);
			return (EBUSY);
		}
		softsp->monitor_on = clone;
		mutex_exit(&softsp->ds1287_mutex);
		return (0);

	case PB_END_MONITOR:
		DPRINTF("ds1287_ioctl: PB_END_MONITOR is called.\n");
		mutex_enter(&softsp->ds1287_mutex);

		/*
		 * If PB_END_MONITOR is called without first
		 * calling PB_BEGIN_MONITOR, an error will be
		 * returned.
		 */
		if (!softsp->monitor_on) {
			mutex_exit(&softsp->ds1287_mutex);
			return (ENXIO);
		}

		/*
		 * This clone is not monitoring the button.
		 */
		if (softsp->monitor_on != clone) {
			mutex_exit(&softsp->ds1287_mutex);
			return (EINVAL);
		}
		softsp->monitor_on = 0;
		mutex_exit(&softsp->ds1287_mutex);
		return (0);

	case PB_GET_EVENTS:
		DPRINTF("ds1287_ioctl: PB_GET_EVENTS is called.\n");
		mutex_enter(&softsp->ds1287_mutex);
		if (ddi_copyout((void *)&softsp->events, (void *)arg,
		    sizeof (int), mode) != 0) {
			mutex_exit(&softsp->ds1287_mutex);
			return (EFAULT);
		}

		/*
		 * This ioctl returned the events detected since last
		 * call.  Note that any application can get the events
		 * and clear the event register.
		 */
		softsp->events = 0;
		mutex_exit(&softsp->ds1287_mutex);
		return (0);

	/*
	 * This ioctl is used by the test suite.
	 */
	case PB_CREATE_BUTTON_EVENT:
		DPRINTF("ds1287_ioctl: PB_CREATE_BUTTON_EVENT is called.\n");
		(void) ds1287_intr(NULL);
		return (0);

	default:
		return (ENOTTY);
	}
}

/*ARGSUSED*/
static int
ds1287_chpoll(dev_t dev, short events, int anyyet,
    short *reventsp, struct pollhead **phpp)
{
	struct ds1287 *softsp;

	if ((softsp = ddi_get_soft_state(ds1287_state, instance)) == NULL)
		return (ENXIO);

	mutex_enter(&softsp->ds1287_mutex);
	*reventsp = 0;
	if (softsp->events)
		*reventsp = POLLRDNORM|POLLIN;
	else {
		if (!anyyet)
			*phpp = &softsp->pollhd;
	}
	mutex_exit(&softsp->ds1287_mutex);

	return (0);
}

static void
ds1287_log_message(void)
{
	struct ds1287 *softsp;

	if ((softsp = ddi_get_soft_state(ds1287_state, instance)) == NULL) {
		cmn_err(CE_WARN, "ds1287: Failed to get internal state!");
		return;
	}

	mutex_enter(&softsp->ds1287_mutex);
	softsp->shutdown_pending = 0;
	cmn_err(CE_WARN, "ds1287: Failed to shut down the system!");
	mutex_exit(&softsp->ds1287_mutex);
}

/*
 * To facilitate a power button abort, ds1287_intr() now posts
 * a softint (calling ds1287_softintr()) for all power button presses and
 * counts the number of button presses. An abort is issued if the desired
 * number of button presses within the given time interval.
 *
 * Two variables are used to synchronize between the high level intr;
 * the softint handler and timeout handler
 *
 * power_button_cancel  - Indicates that an abort happened and the number
 *                        of outstanding timeouts that have to be cancelled
 *
 * power_button_pressed - Indicates the number of button presses outstanding
 *                        which have not been serviced
 */
/*ARGSUSED*/
static uint_t
ds1287_intr(caddr_t ignore)
{
	hrtime_t tstamp;
	static hrtime_t o_tstamp = 0;
	static hrtime_t power_button_tstamp = 0;
	static int power_button_cnt;
	uint8_t	apcr1;

	/*
	 * Stop the Fail-safe timer that starts running
	 * after power button is pressed.  If it is not
	 * stopped in 21 seconds, system powers off.
	 */
	mutex_enter(&ds1287_reg_mutex);
	select_bank(2);
	DS1287_ADDR_REG = APC_APCR1;
	apcr1 = DS1287_DATA_REG;
	apcr1 |= APC_FSTRC;
	DS1287_DATA_REG = apcr1;
	select_bank(1);
	mutex_exit(&ds1287_reg_mutex);

	tstamp = gethrtime();

	/* need to deal with power button debounce */
	if (o_tstamp && (tstamp - o_tstamp) < power_button_debounce) {
		o_tstamp = tstamp;
		return (DDI_INTR_CLAIMED);
	}
	o_tstamp = tstamp;

	power_button_cnt++;

	mutex_enter(&ds1287_reg_mutex);
	power_button_pressed++;
	mutex_exit(&ds1287_reg_mutex);

	/*
	 * If power button abort is enabled and power button was pressed
	 * power_button_abort_presses times within power_button_abort_interval
	 * then call abort_sequence_enter();
	 */
	if (power_button_abort_enable) {
		if (power_button_abort_presses == 1 ||
		    tstamp < (power_button_tstamp +
		    power_button_abort_interval)) {
			if (power_button_cnt == power_button_abort_presses) {
				mutex_enter(&ds1287_reg_mutex);
				power_button_cancel += power_button_timeouts;
				power_button_pressed = 0;
				mutex_exit(&ds1287_reg_mutex);
				power_button_cnt = 0;
				abort_sequence_enter("Power Button Abort");
				return (DDI_INTR_CLAIMED);
			}
		} else {
			power_button_cnt = 1;
			power_button_tstamp = tstamp;
		}
	}

	if (!power_button_enable)
		return (DDI_INTR_CLAIMED);

	/* post softint to issue timeout for power button action */
	ddi_trigger_softintr(ds1287_softintr_id);

	return (DDI_INTR_CLAIMED);
}

/*
 * Handle the softints....
 *
 * If only one softint is posted for several button presses, record
 * the number of additional presses just incase this was actually not quite
 * an Abort sequence so that we can log this event later.
 *
 * Issue a timeout with a duration being a fraction larger than
 * the specified Abort interval inorder to perform a power down if required.
 */
static uint_t
ds1287_softintr(caddr_t arg)
{
	struct ds1287 *softsp = (struct ds1287 *)arg;

	DPRINTF("ds1287_softintr\n");

	if (!power_button_abort_enable)
		return (ds1287_issue_shutdown(arg));

	mutex_enter(&ds1287_reg_mutex);
	if (!power_button_pressed) {
		mutex_exit(&ds1287_reg_mutex);
		return (DDI_INTR_CLAIMED);
	}

	/*
	 * Schedule a timeout to do the necessary
	 * work for shutdown, only one timeout for
	 * n presses if power button was pressed
	 * more than once before softint fired
	 */
	if (power_button_pressed > 1)
		additional_presses += power_button_pressed - 1;

	timeout_cancel = 0;
	power_button_pressed = 0;
	power_button_timeouts++;
	mutex_exit(&ds1287_reg_mutex);
	(void) timeout((void(*)(void *))ds1287_timeout,
	    softsp, NSEC_TO_TICK(power_button_abort_interval) +
	    ABORT_INCREMENT_DELAY);

	return (DDI_INTR_CLAIMED);
}

/*
 * Upon receiving a timeout the following is determined:
 *
 * If an  Abort sequence was issued, then we cancel all outstanding timeouts
 * and additional presses prior to the Abort sequence.
 *
 * If we had multiple timeouts issued and the abort sequence was not met,
 * then we had more than one button press to power down the machine. We
 * were probably trying to issue an abort. So log a message indicating this
 * and cancel all outstanding timeouts.
 *
 * If we had just one timeout and the abort sequence was not met then
 * we really did want to power down the machine, so call ds1287_issue_shutdown()
 * to do the work and schedule a power down
 */
static void
ds1287_timeout(caddr_t arg)
{
	static int first = 0;

	DPRINTF("ds1287_timeout\n");

	/*
	 * Abort was generated cancel all outstanding power
	 * button timeouts
	 */
	mutex_enter(&ds1287_reg_mutex);
	if (power_button_cancel) {
		power_button_cancel--;
		power_button_timeouts--;
		if (!first) {
			first++;
			additional_presses = 0;
		}
		mutex_exit(&ds1287_reg_mutex);
		return;
	}
	first = 0;

	/*
	 * We get here if the timeout(s) have fired and they were
	 * not issued prior to an abort.
	 *
	 * If we had more than one press in the interval we were
	 * probably trying to issue an abort, but didnt press the
	 * required number within the interval. Hence cancel all
	 * timeouts and do not continue towards shutdown.
	 */
	if (!timeout_cancel) {
		timeout_cancel = power_button_timeouts +
		    additional_presses;

		power_button_timeouts--;
		if (!power_button_timeouts)
			additional_presses = 0;

		if (timeout_cancel > 1) {
			mutex_exit(&ds1287_reg_mutex);
			cmn_err(CE_NOTE, "Power Button pressed "
			    "%d times, cancelling all requests",
			    timeout_cancel);
			return;
		}
		mutex_exit(&ds1287_reg_mutex);

		/* Go and do the work to request shutdown */
		(void) ds1287_issue_shutdown(arg);
		return;
	}

	power_button_timeouts--;
	if (!power_button_timeouts)
		additional_presses = 0;
	mutex_exit(&ds1287_reg_mutex);
}

static uint_t
ds1287_issue_shutdown(caddr_t arg)
{
	struct ds1287 *softsp = (struct ds1287 *)arg;

	DPRINTF("ds1287_issue_shutdown\n");

	mutex_enter(&softsp->ds1287_mutex);
	softsp->events |= PB_BUTTON_PRESS;
	if (softsp->monitor_on != 0) {
		mutex_exit(&softsp->ds1287_mutex);
		pollwakeup(&softsp->pollhd, POLLRDNORM);
		pollwakeup(&softsp->pollhd, POLLIN);
		return (DDI_INTR_CLAIMED);
	}

	if (!softsp->shutdown_pending) {
		cmn_err(CE_WARN, "Power button is pressed, powering down "
		    "the system!");
		softsp->shutdown_pending = 1;
		do_shutdown();

		/*
		 * Wait a while for "do_shutdown()" to shut down the system
		 * before logging an error message.
		 */
		(void) timeout((void(*)(void *))ds1287_log_message, NULL,
		    100 * hz);
	}
	mutex_exit(&softsp->ds1287_mutex);

	return (DDI_INTR_CLAIMED);
}

/*
 * Read the current time from the clock chip and convert to UNIX form.
 * Assumes that the year in the clock chip is valid.
 * Must be called with tod_lock held.
 */
static timestruc_t
todds_get(void)
{
	timestruc_t ts;
	todinfo_t tod;
	struct rtc_t rtc;

	ASSERT(MUTEX_HELD(&tod_lock));

	read_rtc(&rtc);
	DPRINTF("todds_get: century=%d year=%d dom=%d hrs=%d\n",
	    rtc.rtc_century, rtc.rtc_year, rtc.rtc_dom, rtc.rtc_hrs);

	/*
	 * tod_year is base 1900 so this code needs to adjust the true
	 * year retrieved from the rtc's century and year fields.
	 */
	tod.tod_year	= rtc.rtc_year + (rtc.rtc_century * 100) - 1900;
	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;

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

	/* set the hw watchdog timer if it's been activated */
	if (watchdog_activated) {
		int ret = 0;
		ret = tod_ops.tod_set_watchdog_timer(watchdog_timeout_seconds);
		if (ret == 0)
			cmn_err(CE_WARN, "ds1287: failed to set hardware "
			    "watchdog timer.");
	}

	return (ts);
}

void
read_rtc(struct rtc_t *rtc)
{
	uint8_t regb;

	/*
	 * Some SuperIO tod devices don't seem to properly initialize
	 * the CADDR register to place the Century register at bank 1
	 * address 0x48.
	 */
	mutex_enter(&ds1287_reg_mutex);

	select_bank(2);
	DS1287_ADDR_REG = RTC_CADDR;
	regb = DS1287_DATA_REG;
	if (regb != 0xc8) {
		if (!ds1287_caddr_warn) {
			ds1287_caddr_warn = 1;
			cmn_err(CE_WARN, "ds1287: century address register "
			    "incorrect (exp 0xc8, obs %x)", regb);
		}
		DS1287_DATA_REG = 0xc8;
	}

	select_bank(1);
	/*
	 * Freeze clock update
	 */
	DS1287_ADDR_REG = RTC_B;
	regb = DS1287_DATA_REG;
	DS1287_DATA_REG = (regb | RTC_SET);

	DS1287_ADDR_REG = RTC_SEC;
	rtc->rtc_sec = DS1287_DATA_REG;
	DS1287_ADDR_REG = RTC_ASEC;
	rtc->rtc_asec = DS1287_DATA_REG;
	DS1287_ADDR_REG = RTC_MIN;
	rtc->rtc_min = DS1287_DATA_REG;
	DS1287_ADDR_REG = RTC_AMIN;
	rtc->rtc_amin = DS1287_DATA_REG;
	DS1287_ADDR_REG = RTC_HRS;
	rtc->rtc_hrs = DS1287_DATA_REG;
	DS1287_ADDR_REG = RTC_AHRS;
	rtc->rtc_ahrs = DS1287_DATA_REG;
	DS1287_ADDR_REG = RTC_DOW;
	rtc->rtc_dow = DS1287_DATA_REG;
	DS1287_ADDR_REG = RTC_DOM;
	rtc->rtc_dom = DS1287_DATA_REG;
	DS1287_ADDR_REG = RTC_MON;
	rtc->rtc_mon = DS1287_DATA_REG;
	DS1287_ADDR_REG = RTC_YEAR;
	rtc->rtc_year = DS1287_DATA_REG;
	DS1287_ADDR_REG = RTC_CENTURY;
	rtc->rtc_century = DS1287_DATA_REG;

	/* Read date alarm */
	DS1287_ADDR_REG = RTC_ADOM;
	rtc->rtc_adom = DS1287_DATA_REG;
	DS1287_ADDR_REG = RTC_AMON;
	rtc->rtc_amon = DS1287_DATA_REG;

	/* Read wakeup data */
	select_bank(2);
	DS1287_ADDR_REG = APC_WDWR;
	rtc->apc_wdwr = DS1287_DATA_REG;
	DS1287_ADDR_REG = APC_WDMR;
	rtc->apc_wdmr = DS1287_DATA_REG;
	DS1287_ADDR_REG = APC_WMR;
	rtc->apc_wmr = DS1287_DATA_REG;
	DS1287_ADDR_REG = APC_WYR;
	rtc->apc_wyr = DS1287_DATA_REG;
	DS1287_ADDR_REG = APC_WCR;
	rtc->apc_wcr = DS1287_DATA_REG;

	/*
	 * Unfreeze clock update
	 */
	DS1287_ADDR_REG = RTC_B;
	DS1287_DATA_REG = regb;

	mutex_exit(&ds1287_reg_mutex);
}

/*
 * Write the specified time into the clock chip.
 * Must be called with tod_lock held.
 */
static void
todds_set(timestruc_t ts)
{
	struct rtc_t	rtc;
	todinfo_t tod = utc_to_tod(ts.tv_sec);
	int year;

	ASSERT(MUTEX_HELD(&tod_lock));

	/* tod_year is base 1900 so this code needs to adjust */
	year = 1900 + tod.tod_year;
	rtc.rtc_year	= year % 100;
	rtc.rtc_century = year / 100;
	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;
	DPRINTF("todds_set: century=%d year=%d dom=%d hrs=%d\n",
	    rtc.rtc_century, rtc.rtc_year, rtc.rtc_dom, rtc.rtc_hrs);

	write_rtc_time(&rtc);
}

void
write_rtc_time(struct rtc_t *rtc)
{
	uint8_t	regb;

	/*
	 * Some SuperIO tod devices don't seem to properly initialize
	 * the CADDR register to place the Century register at bank 1
	 * address 0x48.
	 */
	mutex_enter(&ds1287_reg_mutex);

	select_bank(2);
	DS1287_ADDR_REG = RTC_CADDR;
	regb = DS1287_DATA_REG;
	if (regb != 0xc8) {
		if (!ds1287_caddr_warn) {
			ds1287_caddr_warn = 1;
			cmn_err(CE_WARN, "ds1287: century address register "
			    "incorrect (exp 0xc8, obs %x)", regb);
		}
		DS1287_DATA_REG = 0xc8;
	}

	select_bank(1);

	/*
	 * Freeze
	 */
	DS1287_ADDR_REG = RTC_B;
	regb = DS1287_DATA_REG;

	DS1287_DATA_REG = (regb | RTC_SET);

	DS1287_ADDR_REG = RTC_SEC;
	DS1287_DATA_REG = rtc->rtc_sec;
	DS1287_ADDR_REG = RTC_MIN;
	DS1287_DATA_REG = rtc->rtc_min;
	DS1287_ADDR_REG = RTC_HRS;
	DS1287_DATA_REG = rtc->rtc_hrs;
	DS1287_ADDR_REG = RTC_DOW;
	DS1287_DATA_REG = rtc->rtc_dow;
	DS1287_ADDR_REG = RTC_DOM;
	DS1287_DATA_REG = rtc->rtc_dom;
	DS1287_ADDR_REG = RTC_MON;
	DS1287_DATA_REG = rtc->rtc_mon;
	DS1287_ADDR_REG = RTC_YEAR;
	DS1287_DATA_REG = rtc->rtc_year;
	DS1287_ADDR_REG = RTC_CENTURY;
	DS1287_DATA_REG = rtc->rtc_century;

	/*
	 * Unfreeze
	 */
	DS1287_ADDR_REG = RTC_B;
	DS1287_DATA_REG = regb;

	mutex_exit(&ds1287_reg_mutex);
}

void
write_rtc_alarm(struct rtc_t *rtc)
{
	mutex_enter(&ds1287_reg_mutex);

	select_bank(1);
	DS1287_ADDR_REG = RTC_ASEC;
	DS1287_DATA_REG = rtc->rtc_asec;
	DS1287_ADDR_REG = RTC_AMIN;
	DS1287_DATA_REG = rtc->rtc_amin;
	DS1287_ADDR_REG = RTC_AHRS;
	DS1287_DATA_REG = rtc->rtc_ahrs;
	DS1287_ADDR_REG = RTC_ADOM;
	DS1287_DATA_REG = rtc->rtc_adom;
	DS1287_ADDR_REG = RTC_AMON;
	DS1287_DATA_REG = rtc->rtc_amon;

	select_bank(2);
	DS1287_ADDR_REG = APC_WDWR;
	DS1287_DATA_REG = rtc->apc_wdwr;
	DS1287_ADDR_REG = APC_WDMR;
	DS1287_DATA_REG = rtc->apc_wdmr;
	DS1287_ADDR_REG = APC_WMR;
	DS1287_DATA_REG = rtc->apc_wmr;
	DS1287_ADDR_REG = APC_WYR;
	DS1287_DATA_REG = rtc->apc_wyr;
	DS1287_ADDR_REG = APC_WCR;
	DS1287_DATA_REG = rtc->apc_wcr;

	mutex_exit(&ds1287_reg_mutex);
}

/*
 * program the rtc registers for alarm to go off at the specified time
 */
static void
todds_set_power_alarm(timestruc_t ts)
{
	todinfo_t	tod;
	uint8_t		apcr2;
	struct rtc_t	rtc;

	ASSERT(MUTEX_HELD(&tod_lock));
	tod = utc_to_tod(ts.tv_sec);
	mutex_enter(&ds1287_reg_mutex);

	/* Clear Time Match Detect */
	select_bank(2);
	DS1287_ADDR_REG = APC_APSR;
	apcr2 = DS1287_DATA_REG;

	/* Disable Time Match Enable */
	DS1287_ADDR_REG = APC_APCR2;
	apcr2 = DS1287_DATA_REG;
	DS1287_DATA_REG = (apcr2 & (~APC_TME));

	mutex_exit(&ds1287_reg_mutex);

	rtc.rtc_asec = (uint8_t)tod.tod_sec;
	rtc.rtc_amin = (uint8_t)tod.tod_min;
	rtc.rtc_ahrs = (uint8_t)tod.tod_hour;
	rtc.rtc_adom = (uint8_t)tod.tod_day;
	rtc.rtc_amon = (uint8_t)tod.tod_month;

	rtc.apc_wdwr = (uint8_t)tod.tod_dow;
	rtc.apc_wdmr = (uint8_t)tod.tod_day;
	rtc.apc_wmr = (uint8_t)tod.tod_month;
	rtc.apc_wyr = tod.tod_year % 100;
	rtc.apc_wcr = (tod.tod_year / 100) + 19;

	write_rtc_alarm(&rtc);

	mutex_enter(&ds1287_reg_mutex);
	/* Enable Time Match enable */
	select_bank(2);
	DS1287_ADDR_REG = APC_APCR2;
	DS1287_DATA_REG = (apcr2 | APC_TME);

	mutex_exit(&ds1287_reg_mutex);
}

/*
 * clear alarm interrupt
 */
static void
todds_clear_power_alarm(void)
{
	uint8_t	apcr2;

	ASSERT(MUTEX_HELD(&tod_lock));

	mutex_enter(&ds1287_reg_mutex);

	/* Clear Time Match Detect */
	select_bank(2);
	DS1287_ADDR_REG = APC_APSR;
	apcr2 = DS1287_DATA_REG;

	/* Disable Time Match Enable */
	DS1287_ADDR_REG = APC_APCR2;
	apcr2 = DS1287_DATA_REG;
	DS1287_DATA_REG = (apcr2 & (~APC_TME));

	mutex_exit(&ds1287_reg_mutex);
}

/*
 * Determine the cpu frequency by watching the TOD chip rollover twice.
 * Cpu clock rate is determined by computing the ticks added (in tick register)
 * during one second interval on TOD.
 */
uint64_t
todds_get_cpufrequency(void)
{
	uint64_t cpu_freq;

	ASSERT(MUTEX_HELD(&tod_lock));
	mutex_enter(&ds1287_reg_mutex);

	select_bank(1);
	DS1287_ADDR_REG = RTC_SEC;
	cpu_freq = find_cpufrequency(v_rtc_data_reg);

	mutex_exit(&ds1287_reg_mutex);
	return (cpu_freq);
}

static void
select_bank(int bank)
{
	uint8_t	rega;
	int banksel;

	/* Select Bank 1 */
	DS1287_ADDR_REG = RTC_A;
	rega = DS1287_DATA_REG;
	rega = rega & ~(RTC_DIV0 | RTC_DIV1 | RTC_DIV2);
	switch (bank) {
	case 0:
		banksel = RTC_DIV1;
		break;
	case 1:
		banksel = RTC_DIV0 | RTC_DIV1;
		break;
	case 2:
		banksel = RTC_DIV2;
		break;
	}
	rega |= banksel;
	DS1287_DATA_REG = rega;
}

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

static uint_t
todds_clear_watchdog_timer(void)
{
	ASSERT(MUTEX_HELD(&tod_lock));
	return (0);
}