xref: /titanic_51/usr/src/uts/sun4u/io/todstarcat.c (revision 1c42de6d020629af774dd9e9fc81be3f3ed9398e)
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 2004 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 /*
29  * tod driver module for Starcat
30  * This module implements a soft tod since
31  * starcat has no tod part.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/sysmacros.h>
37 #include <sys/systm.h>
38 #include <sys/errno.h>
39 #include <sys/modctl.h>
40 #include <sys/autoconf.h>
41 #include <sys/debug.h>
42 #include <sys/clock.h>
43 #include <sys/cmn_err.h>
44 #include <sys/promif.h>
45 #include <sys/cpuvar.h>
46 #include <sys/sunddi.h>
47 #include <sys/iosramio.h>
48 #include <sys/domaind.h>
49 
50 #define	abs(x)	((x) < 0 ? -(x) : (x))
51 
52 #define	TODSC_SET_THRESHOLD	30
53 
54 static timestruc_t	todsc_get(void);
55 static void		todsc_set(timestruc_t);
56 static uint_t		todsc_set_watchdog_timer(uint_t);
57 static uint_t		todsc_clear_watchdog_timer(void);
58 static void		todsc_set_power_alarm(timestruc_t);
59 static void		todsc_clear_power_alarm(void);
60 static uint64_t		todsc_get_cpufrequency(void);
61 
62 /*
63  * Module linkage information for the kernel.
64  */
65 static struct modlmisc modlmisc = {
66 	&mod_miscops, "Soft tod module for Sun Fire 15000"
67 };
68 
69 static struct modlinkage modlinkage = {
70 	MODREV_1, (void *)&modlmisc, NULL
71 };
72 
73 static uint32_t heartbeat = 0;
74 
75 int
76 _init(void)
77 {
78 	if (strcmp(tod_module_name, "todstarcat") == 0) {
79 		uint32_t ssc_time32 = 0;
80 		char obp_string[40];
81 
82 		/*
83 		 * To obtain the initial start of day time, we use an
84 		 * OBP callback; this is because the iosram is not yet
85 		 * accessible from the OS at this early stage of startup.
86 		 */
87 
88 		/*
89 		 * Set the string to pass to OBP
90 		 * for now, we assume we always get a 32bit value
91 		 */
92 		(void) sprintf(obp_string, "h# %p unix-gettod",
93 			(void *) &ssc_time32);
94 
95 		prom_interpret(obp_string, 0, 0, 0, 0, 0);
96 
97 		hrestime.tv_sec = (time_t)ssc_time32;
98 
99 		/*
100 		 * A date of zero causes the root filesystem driver
101 		 * to try to set the date from the last shutdown.
102 		 */
103 
104 		/*
105 		 * Check for a zero date.
106 		 */
107 		if (ssc_time32 == 0) {
108 			cmn_err(CE_WARN, "Initial date is invalid.");
109 			cmn_err(CE_CONT, "Attempting to set the date and time "
110 				"based on the last shutdown.\n");
111 			cmn_err(CE_CONT, "Please inspect the date and time and "
112 				"correct if necessary.\n");
113 		}
114 
115 		/*
116 		 * Check that the date has not overflowed a 32-bit integer.
117 		 */
118 		if (TIMESPEC_OVERFLOW(&hrestime)) {
119 			cmn_err(CE_WARN, "Date overflow detected.");
120 			cmn_err(CE_CONT, "Attempting to set the date and time "
121 				"based on the last shutdown.\n");
122 			cmn_err(CE_CONT, "Please inspect the date and time and "
123 				"correct if necessary.\n");
124 
125 			hrestime.tv_sec = (time_t)0;
126 		}
127 
128 		tod_ops.tod_get = todsc_get;
129 		tod_ops.tod_set = todsc_set;
130 		tod_ops.tod_set_watchdog_timer = todsc_set_watchdog_timer;
131 		tod_ops.tod_clear_watchdog_timer = todsc_clear_watchdog_timer;
132 		tod_ops.tod_set_power_alarm = todsc_set_power_alarm;
133 		tod_ops.tod_clear_power_alarm = todsc_clear_power_alarm;
134 		tod_ops.tod_get_cpufrequency = todsc_get_cpufrequency;
135 
136 		/*
137 		 * Flag warning if user tried to use hardware watchdog
138 		 */
139 		if (watchdog_enable) {
140 			cmn_err(CE_WARN, "Hardware watchdog unavailable");
141 		}
142 	}
143 
144 	return (mod_install(&modlinkage));
145 }
146 
147 int
148 _fini(void)
149 {
150 	if (strcmp(tod_module_name, "todstarcat") == 0)
151 		return (EBUSY);
152 	else
153 		return (mod_remove(&modlinkage));
154 }
155 
156 int
157 _info(struct modinfo *modinfop)
158 {
159 	return (mod_info(&modlinkage, modinfop));
160 }
161 
162 
163 /*
164  * Starcat tod_get is simplified to return hrestime and to
165  * update the domain heartbeat.
166  * Must be called with tod_lock held.
167  */
168 static timestruc_t
169 todsc_get(void)
170 {
171 	ASSERT(MUTEX_HELD(&tod_lock));
172 
173 	heartbeat++;
174 	(void) iosram_wr(DOMD_MAGIC, DOMD_HEARTBEAT_OFFSET,
175 		sizeof (uint32_t), (caddr_t)&heartbeat);
176 	return (hrestime);
177 }
178 
179 /*
180  * Must be called with tod_lock held.
181  *
182  * When running NTP, tod_set is called at least once per second in order
183  * to update the hardware clock - for Starcat, we don't want to sync
184  * the non-existent hardware clock, and only want to record significant
185  * time changes on the SC (i.e. when date(1M) is run).  So, we have a
186  * threshold requirement before recording the time change.
187  */
188 /* ARGSUSED */
189 static void
190 todsc_set(timestruc_t ts)
191 {
192 	char obp_string[40];
193 
194 	ASSERT(MUTEX_HELD(&tod_lock));
195 
196 	heartbeat++;
197 	(void) iosram_wr(DOMD_MAGIC, DOMD_HEARTBEAT_OFFSET,
198 		sizeof (uint32_t), (caddr_t)&heartbeat);
199 
200 	if (abs(hrestime.tv_sec - ts.tv_sec) > TODSC_SET_THRESHOLD) {
201 		/*
202 		 * Update the SSC with the new UTC domain time
203 		 */
204 		(void) sprintf(obp_string, "h# %x unix-settod",
205 			(int)ts.tv_sec);
206 
207 		prom_interpret(obp_string, 0, 0, 0, 0, 0);
208 #ifdef DEBUG
209 		cmn_err(CE_NOTE, "todsc_set: new domain time 0x%lx\n",
210 			ts.tv_sec);
211 #endif
212 	}
213 }
214 
215 /*
216  * No watchdog function.
217  */
218 /* ARGSUSED */
219 static uint_t
220 todsc_set_watchdog_timer(uint_t timeoutval)
221 {
222 	ASSERT(MUTEX_HELD(&tod_lock));
223 	return (0);
224 }
225 
226 /*
227  * No watchdog function
228  */
229 static uint_t
230 todsc_clear_watchdog_timer(void)
231 {
232 	ASSERT(MUTEX_HELD(&tod_lock));
233 	return (0);
234 }
235 
236 /*
237  * Null function.
238  */
239 /* ARGSUSED */
240 static void
241 todsc_set_power_alarm(timestruc_t ts)
242 {
243 	ASSERT(MUTEX_HELD(&tod_lock));
244 }
245 
246 /*
247  * Null function
248  */
249 static void
250 todsc_clear_power_alarm()
251 {
252 	ASSERT(MUTEX_HELD(&tod_lock));
253 }
254 
255 /*
256  * Get clock freq from the cpunode.  This function is only called
257  * when use_stick = 0, otherwise, system_clock_freq gets used instead.
258  */
259 uint64_t
260 todsc_get_cpufrequency(void)
261 {
262 	return (cpunodes[CPU->cpu_id].clock_freq);
263 }
264