Add a new kernel-version independent broadcom binary driver for brcm47xx (contributed...
[openwrt/svn-archive/archive.git] / package / busybox / patches / 000-upstream-hwclock.patch
1 --- a/util-linux/hwclock.c
2 +++ b/util-linux/hwclock.c
3 @@ -109,10 +109,53 @@ static void to_sys_clock(const char **pp
4
5 static void from_sys_clock(const char **pp_rtcname, int utc)
6 {
7 -#define TWEAK_USEC 200
8 - struct tm tm_time;
9 +#if 1
10 struct timeval tv;
11 + struct tm tm_time;
12 + int rtc;
13 +
14 + rtc = rtc_xopen(pp_rtcname, O_WRONLY);
15 + gettimeofday(&tv, NULL);
16 + /* Prepare tm_time */
17 + if (sizeof(time_t) == sizeof(tv.tv_sec)) {
18 + if (utc)
19 + gmtime_r((time_t*)&tv.tv_sec, &tm_time);
20 + else
21 + localtime_r((time_t*)&tv.tv_sec, &tm_time);
22 + } else {
23 + time_t t = tv.tv_sec;
24 + if (utc)
25 + gmtime_r(&t, &tm_time);
26 + else
27 + localtime_r(&t, &tm_time);
28 + }
29 +#else
30 +/* Bloated code which tries to set hw clock with better precision.
31 + * On x86, even though code does set hw clock within <1ms of exact
32 + * whole seconds, apparently hw clock (at least on some machines)
33 + * doesn't reset internal fractional seconds to 0,
34 + * making all this a pointless excercise.
35 + */
36 + /* If we see that we are N usec away from whole second,
37 + * we'll sleep for N-ADJ usecs. ADJ corrects for the fact
38 + * that CPU is not infinitely fast.
39 + * On infinitely fast CPU, next wakeup would be
40 + * on (exactly_next_whole_second - ADJ). On real CPUs,
41 + * this difference between current time and whole second
42 + * is less than ADJ (assuming system isn't heavily loaded).
43 + */
44 + /* Small value of 256us gives very precise sync for 2+ GHz CPUs.
45 + * Slower CPUs will fail to sync and will go to bigger
46 + * ADJ values. qemu-emulated armv4tl with ~100 MHz
47 + * performance ends up using ADJ ~= 4*1024 and it takes
48 + * 2+ secs (2 tries with successively larger ADJ)
49 + * to sync. Even straced one on the same qemu (very slow)
50 + * takes only 4 tries.
51 + */
52 +#define TWEAK_USEC 256
53 unsigned adj = TWEAK_USEC;
54 + struct tm tm_time;
55 + struct timeval tv;
56 int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
57
58 /* Try to catch the moment when whole second is close */
59 @@ -124,55 +167,64 @@ static void from_sys_clock(const char **
60
61 t = tv.tv_sec;
62 rem_usec = 1000000 - tv.tv_usec;
63 - if (rem_usec < 1024) {
64 - /* Less than 1ms to next second. Good enough */
65 + if (rem_usec < adj) {
66 + /* Close enough */
67 small_rem:
68 t++;
69 }
70
71 - /* Prepare tm */
72 + /* Prepare tm_time from t */
73 if (utc)
74 gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */
75 else
76 localtime_r(&t, &tm_time); /* same */
77 - tm_time.tm_isdst = 0;
78 +
79 + if (adj >= 32*1024) {
80 + break; /* 32 ms diff and still no luck?? give up trying to sync */
81 + }
82
83 /* gmtime/localtime took some time, re-get cur time */
84 gettimeofday(&tv, NULL);
85
86 - if (tv.tv_sec < t /* may happen if rem_usec was < 1024 */
87 - || (tv.tv_sec == t && tv.tv_usec < 1024)
88 + if (tv.tv_sec < t /* we are still in old second */
89 + || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */
90 ) {
91 - /* We are not too far into next second. Good. */
92 - break;
93 - }
94 - adj += 32; /* 2^(10-5) = 2^5 = 32 iterations max */
95 - if (adj >= 1024) {
96 - /* Give up trying to sync */
97 - break;
98 + break; /* good, we are in sync! */
99 }
100
101 - /* Try to sync up by sleeping */
102 rem_usec = 1000000 - tv.tv_usec;
103 - if (rem_usec < 1024) {
104 - goto small_rem; /* already close, don't sleep */
105 + if (rem_usec < adj) {
106 + t = tv.tv_sec;
107 + goto small_rem; /* already close to next sec, don't sleep */
108 }
109 - /* Need to sleep.
110 - * Note that small adj on slow processors can make us
111 - * to always overshoot tv.tv_usec < 1024 check on next
112 - * iteration. That's why adj is increased on each iteration.
113 - * This also allows it to be reused as a loop limiter.
114 - */
115 - usleep(rem_usec - adj);
116 - }
117
118 - xioctl(rtc, RTC_SET_TIME, &tm_time);
119 + /* Try to sync up by sleeping */
120 + usleep(rem_usec - adj);
121
122 - /* Debug aid to find "good" TWEAK_USEC.
123 + /* Jump to 1ms diff, then increase fast (x2): EVERY loop
124 + * takes ~1 sec, people won't like slowly converging code here!
125 + */
126 + //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec);
127 + if (adj < 512)
128 + adj = 512;
129 + /* ... and if last "overshoot" does not look insanely big,
130 + * just use it as adj increment. This makes convergence faster.
131 + */
132 + if (tv.tv_usec < adj * 8) {
133 + adj += tv.tv_usec;
134 + continue;
135 + }
136 + adj *= 2;
137 + }
138 + /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync.
139 * Look for a value which makes tv_usec close to 999999 or 0.
140 - * for 2.20GHz Intel Core 2: TWEAK_USEC ~= 200
141 + * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200
142 */
143 - //bb_error_msg("tv.tv_usec:%d adj:%d", (int)tv.tv_usec, adj);
144 + //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec);
145 +#endif
146 +
147 + tm_time.tm_isdst = 0;
148 + xioctl(rtc, RTC_SET_TIME, &tm_time);
149
150 if (ENABLE_FEATURE_CLEAN_UP)
151 close(rtc);