OLD | NEW |
---|---|
(Empty) | |
1 /******************************************************************************* | |
2 ** | |
3 ** FILENAME: $Id$ | |
4 ** COPYRIGHT: Infineon Technologies | |
5 ** DESCRIPTION: I2C TPM - Compatibility layer to port linux driver to U-Boot | |
6 ** CREATION DATE: 2010/11/10 | |
7 ** LAST CHANGE: $Date: 2010-10-20 12:04:48 +0200 (Wed, 20 Oct 2010) $ | |
8 ** $Author$ | |
9 ** VERSION: $Revision: 59 $ | |
10 ** | |
11 *******************************************************************************/ | |
12 | |
13 #include <common.h> | |
14 #include "compatibility.h" | |
15 | |
16 struct device_private p = { | |
Che-Liang Chiou
2011/03/14 08:49:54
Should we add a "static" prefix here?
rongchang
2011/03/23 11:44:57
Same as compatibility.h. But this file was removed
| |
17 .driver_data = NULL, | |
18 }; | |
19 | |
20 /* init device structure */ | |
21 struct device d = { | |
Che-Liang Chiou
2011/03/14 08:49:54
Same as above.
| |
22 .parent = NULL, | |
23 .p = &p, | |
24 }; | |
25 | |
26 /* Taken from <linux/time.h> */ | |
27 #define LONG_MAX ((long)(~0UL>>1)) | |
28 #define MSEC_PER_SEC 1000L | |
29 #define USEC_PER_SEC 1000000L | |
30 #define MAX_JIFFY_OFFSET ((LONG_MAX >> 1)-1) | |
31 #define BITS_PER_LONG 32 | |
32 | |
33 /* global time counter */ | |
34 unsigned long jiffies; | |
Che-Liang Chiou
2011/03/14 08:49:54
Same as above.
| |
35 | |
36 /* | |
37 * Convert jiffies to milliseconds and back. | |
38 * | |
39 * Avoid unnecessary multiplications/divisions in the | |
40 * two most common HZ cases: | |
41 */ | |
42 inline unsigned int jiffies_to_msecs(const unsigned long j) | |
43 { | |
44 #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) | |
45 return (MSEC_PER_SEC / HZ) * j; | |
46 #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) | |
47 return (j + (HZ / MSEC_PER_SEC) - 1) / (HZ / MSEC_PER_SEC); | |
48 #else | |
49 # if BITS_PER_LONG == 32 | |
50 return (HZ_TO_MSEC_MUL32 * j) >> HZ_TO_MSEC_SHR32; | |
51 # else | |
52 return (j * HZ_TO_MSEC_NUM) / HZ_TO_MSEC_DEN; | |
53 # endif | |
54 #endif | |
55 } | |
56 | |
57 unsigned long msecs_to_jiffies(const unsigned int m) | |
58 { | |
59 /* | |
60 * Negative value, means infinite timeout: | |
61 */ | |
62 if ((int)m < 0) | |
63 return MAX_JIFFY_OFFSET; | |
64 | |
65 #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) | |
66 /* | |
67 * HZ is equal to or smaller than 1000, and 1000 is a nice | |
68 * round multiple of HZ, divide with the factor between them, | |
69 * but round upwards: | |
70 */ | |
71 return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ); | |
72 #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) | |
73 /* | |
74 * HZ is larger than 1000, and HZ is a nice round multiple of | |
75 * 1000 - simply multiply with the factor between them. | |
76 * | |
77 * But first make sure the multiplication result cannot | |
78 * overflow: | |
79 */ | |
80 if (m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) | |
81 return MAX_JIFFY_OFFSET; | |
82 | |
83 return m * (HZ / MSEC_PER_SEC); | |
84 #else | |
85 /* | |
86 * Generic case - multiply, round and divide. But first | |
87 * check that if we are doing a net multiplication, that | |
88 * we wouldn't overflow: | |
89 */ | |
90 if (HZ > MSEC_PER_SEC && m > jiffies_to_msecs(MAX_JIFFY_OFFSET)) | |
91 return MAX_JIFFY_OFFSET; | |
92 | |
93 return (MSEC_TO_HZ_MUL32 * m + MSEC_TO_HZ_ADJ32) | |
94 >> MSEC_TO_HZ_SHR32; | |
95 #endif | |
96 } | |
97 | |
98 inline unsigned int jiffies_to_usecs(const unsigned long j) | |
99 { | |
100 #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ) | |
101 return (USEC_PER_SEC / HZ) * j; | |
102 #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC) | |
103 return (j + (HZ / USEC_PER_SEC) - 1) / (HZ / USEC_PER_SEC); | |
104 #else | |
105 # if BITS_PER_LONG == 32 | |
106 return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32; | |
107 # else | |
108 return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN; | |
109 # endif | |
110 #endif | |
111 } | |
112 | |
113 unsigned long usecs_to_jiffies(const unsigned int u) | |
114 { | |
115 if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET)) | |
116 return MAX_JIFFY_OFFSET; | |
117 #if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ) | |
118 return (u + (USEC_PER_SEC / HZ) - 1) / (USEC_PER_SEC / HZ); | |
119 #elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC) | |
120 return u * (HZ / USEC_PER_SEC); | |
121 #else | |
122 return (USEC_TO_HZ_MUL32 * u + USEC_TO_HZ_ADJ32) | |
123 >> USEC_TO_HZ_SHR32; | |
124 #endif | |
125 } | |
126 | |
127 /* replacement for msleep() just delaying */ | |
128 void msleep2(unsigned int t) | |
129 { | |
130 dbg_printf("INFO: msleep() %d ms, jiffies=%ld\n", t, jiffies); | |
131 udelay(t * 1000); | |
132 jiffies += msecs_to_jiffies(t); | |
133 } | |
134 | |
OLD | NEW |