1 /******************************************************************************
2 * *
3 * License Agreement *
4 * *
5 * Copyright (c) 2003 Altera Corporation, San Jose, California, USA. *
6 * All rights reserved. *
7 * *
8 * Permission is hereby granted, free of charge, to any person obtaining a *
9 * copy of this software and associated documentation files (the "Software"), *
10 * to deal in the Software without restriction, including without limitation *
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
12 * and/or sell copies of the Software, and to permit persons to whom the *
13 * Software is furnished to do so, subject to the following conditions: *
14 * *
15 * The above copyright notice and this permission notice shall be included in *
16 * all copies or substantial portions of the Software. *
17 * *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
24 * DEALINGS IN THE SOFTWARE. *
25 * *
26 * *
27 * Altera does not recommend, suggest or require that this reference design *
28 * file be used in conjunction or combination with any other product. *
29 ******************************************************************************/
30
31 #include <string.h>
32
33 #include "system.h"
34 #include "sys/alt_timestamp.h"
35
36 #include "altera_avalon_timer.h"
37 #include "altera_avalon_timer_regs.h"
38
39 #include "alt_types.h"
40
41 /*
42 * These functions are only available if a timestamp device has been selected
43 * for this system.
44 */
45
46 #if (ALT_TIMESTAMP_CLK_BASE != none_BASE)
47
48 /*
49 * The function alt_timestamp_start() can be called at application level to
50 * initialise the timestamp facility. In this case the period register is
51 * set to full scale, i.e. 0xffffffff, and then started running. Note that
52 * the period register may not be writable, depending on the hardware
53 * configuration, in which case this function does not reset the period.
54 *
55 * The timer is not run in continuous mode, so that the user can detect timer
56 * roll-over, i.e. alt_timestamp() returns 0.
57 *
58 * The return value of this function is 0 upon sucess and -1 if in timestamp
59 * device has not been registered.
60 */
61
alt_timestamp_start(void)62 int alt_timestamp_start(void)
63 {
64 void* base = altera_avalon_timer_ts_base;
65
66 if (!altera_avalon_timer_ts_freq)
67 {
68 return -1;
69 }
70 else
71 {
72 if(ALT_TIMESTAMP_COUNTER_SIZE == 64) {
73 IOWR_ALTERA_AVALON_TIMER_CONTROL (base,ALTERA_AVALON_TIMER_CONTROL_STOP_MSK);
74 IOWR_ALTERA_AVALON_TIMER_PERIOD_0 (base, 0xFFFF);
75 IOWR_ALTERA_AVALON_TIMER_PERIOD_1 (base, 0xFFFF);;
76 IOWR_ALTERA_AVALON_TIMER_PERIOD_2 (base, 0xFFFF);
77 IOWR_ALTERA_AVALON_TIMER_PERIOD_3 (base, 0xFFFF);
78 IOWR_ALTERA_AVALON_TIMER_CONTROL (base, ALTERA_AVALON_TIMER_CONTROL_START_MSK);
79 } else {
80 IOWR_ALTERA_AVALON_TIMER_CONTROL (base,ALTERA_AVALON_TIMER_CONTROL_STOP_MSK);
81 IOWR_ALTERA_AVALON_TIMER_PERIODL (base, 0xFFFF);
82 IOWR_ALTERA_AVALON_TIMER_PERIODH (base, 0xFFFF);
83 IOWR_ALTERA_AVALON_TIMER_CONTROL (base, ALTERA_AVALON_TIMER_CONTROL_START_MSK);
84 }
85 }
86 return 0;
87 }
88
89 /*
90 * alt_timestamp() returns the current timestamp count. In the event that
91 * the timer has run full period, or there is no timestamp available, this
92 * function return -1.
93 *
94 * The returned timestamp counts up from the last time the period register
95 * was reset.
96 */
97
alt_timestamp(void)98 alt_timestamp_type alt_timestamp(void)
99 {
100
101 void* base = altera_avalon_timer_ts_base;
102
103 if (!altera_avalon_timer_ts_freq)
104 {
105 #if (ALT_TIMESTAMP_COUNTER_SIZE == 64)
106 return 0xFFFFFFFFFFFFFFFFULL;
107 #else
108 return 0xFFFFFFFF;
109 #endif
110 }
111 else
112 {
113 #if (ALT_TIMESTAMP_COUNTER_SIZE == 64)
114 IOWR_ALTERA_AVALON_TIMER_SNAP_0 (base, 0);
115 alt_timestamp_type snap_0 = IORD_ALTERA_AVALON_TIMER_SNAP_0(base) & ALTERA_AVALON_TIMER_SNAP_0_MSK;
116 alt_timestamp_type snap_1 = IORD_ALTERA_AVALON_TIMER_SNAP_1(base) & ALTERA_AVALON_TIMER_SNAP_1_MSK;
117 alt_timestamp_type snap_2 = IORD_ALTERA_AVALON_TIMER_SNAP_2(base) & ALTERA_AVALON_TIMER_SNAP_2_MSK;
118 alt_timestamp_type snap_3 = IORD_ALTERA_AVALON_TIMER_SNAP_3(base) & ALTERA_AVALON_TIMER_SNAP_3_MSK;
119
120 return (0xFFFFFFFFFFFFFFFFULL - ( (snap_3 << 48) | (snap_2 << 32) | (snap_1 << 16) | (snap_0) ));
121 #else
122 IOWR_ALTERA_AVALON_TIMER_SNAPL (base, 0);
123 alt_timestamp_type lower = IORD_ALTERA_AVALON_TIMER_SNAPL(base) & ALTERA_AVALON_TIMER_SNAPL_MSK;
124 alt_timestamp_type upper = IORD_ALTERA_AVALON_TIMER_SNAPH(base) & ALTERA_AVALON_TIMER_SNAPH_MSK;
125
126 return (0xFFFFFFFF - ((upper << 16) | lower));
127 #endif
128 }
129 }
130
131 /*
132 * Return the number of timestamp ticks per second. This will be 0 if no
133 * timestamp device has been registered.
134 */
135
alt_timestamp_freq(void)136 alt_u32 alt_timestamp_freq(void)
137 {
138 return altera_avalon_timer_ts_freq;
139 }
140
141 #endif /* timestamp available */
142