driver: add dev_name inline
[barebox-mini2440.git] / common / clock.c
blobad708adbbff2b875a40f165440cf76872af3ffbd
1 /*
2 * clock.c - generic clocksource implementation
4 * This file contains the clocksource implementation from the Linux
5 * kernel originally by John Stultz
7 * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
8 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
10 * See file CREDITS for list of people who contributed to this
11 * project.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <common.h>
28 #include <asm-generic/div64.h>
29 #include <clock.h>
31 static struct clocksource *current_clock;
32 static uint64_t time_ns;
34 /**
35 * get_time_ns - get current timestamp in nanoseconds
37 uint64_t get_time_ns(void)
39 struct clocksource *cs = current_clock;
40 uint64_t cycle_now, cycle_delta;
41 uint64_t ns_offset;
43 /* read clocksource: */
44 cycle_now = cs->read() & cs->mask;
46 /* calculate the delta since the last call: */
47 cycle_delta = (cycle_now - cs->cycle_last) & cs->mask;
49 /* convert to nanoseconds: */
50 ns_offset = cyc2ns(cs, cycle_delta);
52 cs->cycle_last = cycle_now;
54 time_ns += ns_offset;
55 return time_ns;
57 EXPORT_SYMBOL(get_time_ns);
59 /**
60 * clocksource_hz2mult - calculates mult from hz and shift
61 * @hz: Clocksource frequency in Hz
62 * @shift_constant: Clocksource shift factor
64 * Helper functions that converts a hz counter
65 * frequency to a timsource multiplier, given the
66 * clocksource shift value
68 uint32_t clocksource_hz2mult(uint32_t hz, uint32_t shift_constant)
70 /* hz = cyc/(Billion ns)
71 * mult/2^shift = ns/cyc
72 * mult = ns/cyc * 2^shift
73 * mult = 1Billion/hz * 2^shift
74 * mult = 1000000000 * 2^shift / hz
75 * mult = (1000000000<<shift) / hz
77 uint64_t tmp = ((uint64_t)1000000000) << shift_constant;
79 tmp += hz/2; /* round for do_div */
80 do_div(tmp, hz);
82 return (uint32_t)tmp;
85 int is_timeout(uint64_t start_ns, uint64_t time_offset_ns)
88 if (start_ns + time_offset_ns < get_time_ns())
89 return 1;
90 else
91 return 0;
93 EXPORT_SYMBOL(is_timeout);
95 void ndelay(unsigned long nsecs)
97 uint64_t start = get_time_ns();
99 while(!is_timeout(start, nsecs));
101 EXPORT_SYMBOL(ndelay);
103 void udelay(unsigned long usecs)
105 uint64_t start = get_time_ns();
107 while(!is_timeout(start, usecs * 1000));
109 EXPORT_SYMBOL(udelay);
111 void mdelay(unsigned long msecs)
113 uint64_t start = get_time_ns();
115 while(!is_timeout(start, msecs * 1000000));
117 EXPORT_SYMBOL(mdelay);
119 int init_clock(struct clocksource *cs)
121 current_clock = cs;
122 return 0;