diff options
author | Doug Anderson <dianders@chromium.org> | 2013-11-26 19:57:19 -0500 |
---|---|---|
committer | Wim Van Sebroeck <wim@iguana.be> | 2014-01-28 15:23:20 -0500 |
commit | 178624403c04a4af315583eaf75ebb8545474544 (patch) | |
tree | 75481b9bc3cc0bcff0b4bd97e71bffe0fae8dfe1 | |
parent | 0f1dd98d896efb956d474754b5d2f0c420d40192 (diff) |
watchdog: s3c2410_wdt: Handle rounding a little better for timeout
The existing watchdog timeout worked OK but didn't deal with
rounding in an ideal way when dividing out all of its clocks.
Specifically if you had a timeout of 32 seconds and an input clock of
66666666, you'd end up setting a timeout of 31.9998 seconds and
reporting a timeout of 31 seconds.
Specifically DBG printouts showed:
s3c2410wdt_set_heartbeat: count=16666656, timeout=32, freq=520833
s3c2410wdt_set_heartbeat: timeout=32, divisor=255, count=16666656 (0000ff4f)
and the final timeout reported to the user was:
((count / divisor) * divisor) / freq
(0xff4f * 255) / 520833 = 31 (truncated from 31.9998)
the technically "correct" value is:
(0xff4f * 255) / (66666666.0 / 128) = 31.9998
By using "DIV_ROUND_UP" we can be a little more correct.
s3c2410wdt_set_heartbeat: count=16666688, timeout=32, freq=520834
s3c2410wdt_set_heartbeat: timeout=32, divisor=255, count=16666688 (0000ff50)
and the final timeout reported to the user:
(0xff50 * 255) / 520834 = 32
the technically "correct" value is:
(0xff50 * 255) / (66666666.0 / 128) = 32.0003
We'll use a DIV_ROUND_UP to solve this, generally erroring on the side
of reporting shorter values to the user and setting the watchdog to
slightly longer than requested:
* Round input frequency up to assume watchdog is counting faster.
* Round divisions by divisor up to give us extra time.
At the same time we can avoid a for loop by just doing the right math.
Signed-off-by: Doug Anderson <dianders@chromium.org>
Reviewed-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Wim Van Sebroeck <wim@iguana.be>
-rw-r--r-- | drivers/watchdog/s3c2410_wdt.c | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/drivers/watchdog/s3c2410_wdt.c b/drivers/watchdog/s3c2410_wdt.c index c1ada330e125..8beaa1799df6 100644 --- a/drivers/watchdog/s3c2410_wdt.c +++ b/drivers/watchdog/s3c2410_wdt.c | |||
@@ -188,7 +188,7 @@ static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeou | |||
188 | if (timeout < 1) | 188 | if (timeout < 1) |
189 | return -EINVAL; | 189 | return -EINVAL; |
190 | 190 | ||
191 | freq /= 128; | 191 | freq = DIV_ROUND_UP(freq, 128); |
192 | count = timeout * freq; | 192 | count = timeout * freq; |
193 | 193 | ||
194 | DBG("%s: count=%d, timeout=%d, freq=%lu\n", | 194 | DBG("%s: count=%d, timeout=%d, freq=%lu\n", |
@@ -200,21 +200,18 @@ static int s3c2410wdt_set_heartbeat(struct watchdog_device *wdd, unsigned timeou | |||
200 | */ | 200 | */ |
201 | 201 | ||
202 | if (count >= 0x10000) { | 202 | if (count >= 0x10000) { |
203 | for (divisor = 1; divisor <= 0x100; divisor++) { | 203 | divisor = DIV_ROUND_UP(count, 0xffff); |
204 | if ((count / divisor) < 0x10000) | ||
205 | break; | ||
206 | } | ||
207 | 204 | ||
208 | if ((count / divisor) >= 0x10000) { | 205 | if (divisor > 0x100) { |
209 | dev_err(wdt->dev, "timeout %d too big\n", timeout); | 206 | dev_err(wdt->dev, "timeout %d too big\n", timeout); |
210 | return -EINVAL; | 207 | return -EINVAL; |
211 | } | 208 | } |
212 | } | 209 | } |
213 | 210 | ||
214 | DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n", | 211 | DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)\n", |
215 | __func__, timeout, divisor, count, count/divisor); | 212 | __func__, timeout, divisor, count, DIV_ROUND_UP(count, divisor)); |
216 | 213 | ||
217 | count /= divisor; | 214 | count = DIV_ROUND_UP(count, divisor); |
218 | wdt->count = count; | 215 | wdt->count = count; |
219 | 216 | ||
220 | /* update the pre-scaler */ | 217 | /* update the pre-scaler */ |