summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2011-07-06 10:25:04 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2011-07-06 10:25:04 +0200
commite6c03f01bf7da74f4d846efb3e014f053ace92bd (patch)
treef73fef831bf2bb7dcc1a27413e5857161d385d8b /common
parent6d00046884f79e53b87c1c7dfb09dc992036a396 (diff)
parented66bf27876d13b69000d1e63b2fadf6c0e0b231 (diff)
downloadbarebox-e6c03f01bf7da74f4d846efb3e014f053ace92bd.tar.gz
barebox-e6c03f01bf7da74f4d846efb3e014f053ace92bd.tar.xz
Merge branch 'next'
Diffstat (limited to 'common')
-rw-r--r--common/clock.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/common/clock.c b/common/clock.c
index 15df0abb48..79c06c8ddc 100644
--- a/common/clock.c
+++ b/common/clock.c
@@ -57,6 +57,61 @@ uint64_t get_time_ns(void)
EXPORT_SYMBOL(get_time_ns);
/**
+ * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
+ * @mult: pointer to mult variable
+ * @shift: pointer to shift variable
+ * @from: frequency to convert from
+ * @to: frequency to convert to
+ * @maxsec: guaranteed runtime conversion range in seconds
+ *
+ * The function evaluates the shift/mult pair for the scaled math
+ * operations of clocksources and clockevents.
+ *
+ * @to and @from are frequency values in HZ. For clock sources @to is
+ * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
+ * event @to is the counter frequency and @from is NSEC_PER_SEC.
+ *
+ * The @maxsec conversion range argument controls the time frame in
+ * seconds which must be covered by the runtime conversion with the
+ * calculated mult and shift factors. This guarantees that no 64bit
+ * overflow happens when the input value of the conversion is
+ * multiplied with the calculated mult factor. Larger ranges may
+ * reduce the conversion accuracy by chosing smaller mult and shift
+ * factors.
+ */
+
+void clocks_calc_mult_shift(uint32_t *mult, uint32_t *shift, uint32_t from, uint32_t to, uint32_t maxsec)
+{
+ uint64_t tmp;
+ uint32_t sft, sftacc = 32;
+
+ /*
+ * Calculate the shift factor which is limiting the conversion
+ * range:
+ */
+ tmp = ((uint64_t)maxsec * from) >> 32;
+ while (tmp) {
+ tmp >>=1;
+ sftacc--;
+ }
+
+ /*
+ * Find the conversion shift/mult pair which has the best
+ * accuracy and fits the maxsec conversion range:
+ */
+ for (sft = 32; sft > 0; sft--) {
+ tmp = (uint64_t) to << sft;
+ tmp += from / 2;
+ do_div(tmp, from);
+ if ((tmp >> sftacc) == 0)
+ break;
+ }
+ *mult = tmp;
+ *shift = sft;
+}
+
+
+/**
* clocksource_hz2mult - calculates mult from hz and shift
* @hz: Clocksource frequency in Hz
* @shift_constant: Clocksource shift factor