source: trunk/libs/newlib/src/libgloss/sparc_leon/asm-leon/jiffies.h @ 509

Last change on this file since 509 was 444, checked in by satin@…, 7 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 3.5 KB
Line 
1/*
2 * Copyright (c) 2011 Aeroflex Gaisler
3 *
4 * BSD license:
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25
26#ifndef _LINUX_JIFFIES_H
27#define _LINUX_JIFFIES_H
28
29#include <asm-leon/types.h>
30#include <asm-leon/clock.h>
31#include <asm-leon/linkage.h>
32
33/* Suppose we want to devide two numbers NOM and DEN: NOM/DEN, the we can
34 * improve accuracy by shifting LSH bits, hence calculating:
35 *     (NOM << LSH) / DEN
36 * This however means trouble for large NOM, because (NOM << LSH) may no
37 * longer fit in 32 bits. The following way of calculating this gives us
38 * some slack, under the following conditions:
39 *   - (NOM / DEN) fits in (32 - LSH) bits.
40 *   - (NOM % DEN) fits in (32 - LSH) bits.
41 */
42#define SH_DIV(NOM,DEN,LSH) (   ((NOM / DEN) << LSH)                    \
43                             + (((NOM % DEN) << LSH) + DEN / 2) / DEN)
44
45/* TICK_NSEC is the time between ticks in nsec assuming real ACTHZ */
46#define TICK_NSEC (SH_DIV (1000000UL * 1000, (HZ<<8), 8))
47
48/*
49 * The 64-bit value is not volatile - you MUST NOT read it
50 * without sampling the sequence number in xtime_lock.
51 */
52extern u64 jiffies_64;
53extern struct timespec xtime __attribute__ ((aligned (16)));
54#define jiffies (*((unsigned long *)(((unsigned long)(&jiffies_64))+4)))
55
56/*
57 *      These inlines deal with timer wrapping correctly. You are
58 *      strongly encouraged to use them
59 *      1. Because people otherwise forget
60 *      2. Because if the timer wrap changes in future you won't have to
61 *         alter your driver code.
62 *
63 * time_after(a,b) returns true if the time a is after time b.
64 *
65 * Do this with "<0" and ">=0" to only test the sign of the result. A
66 * good compiler would generate better code (and a really good compiler
67 * wouldn't care). Gcc is currently neither.
68 */
69#define time_after(a,b)         \
70        (typecheck(unsigned long, a) && \
71         typecheck(unsigned long, b) && \
72         ((long)(b) - (long)(a) < 0))
73#define time_before(a,b)        time_after(b,a)
74
75#define time_after_eq(a,b)      \
76        (typecheck(unsigned long, a) && \
77         typecheck(unsigned long, b) && \
78         ((long)(a) - (long)(b) >= 0))
79#define time_before_eq(a,b)     time_after_eq(b,a)
80
81/*
82 * Have the 32 bit jiffies value wrap 5 minutes after boot
83 * so jiffies wrap bugs show up earlier.
84 */
85#define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
86
87static inline void
88set_normalized_timespec (struct timespec *ts, time_t sec, long nsec)
89{
90  while (nsec > NSEC_PER_SEC)
91    {
92      nsec -= NSEC_PER_SEC;
93      ++sec;
94    }
95  while (nsec < 0)
96    {
97      nsec += NSEC_PER_SEC;
98      --sec;
99    }
100  ts->tv_sec = sec;
101  ts->tv_nsec = nsec;
102}
103
104#endif
Note: See TracBrowser for help on using the repository browser.