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