summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/main/cfe_timer.c
blob: 2b8a0fc65b040ad7cb793bf9939581cef065b122 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*  *********************************************************************
    *  Broadcom Common Firmware Environment (CFE)
    *  
    *  Timer routines				File: cfe_timer.c
    *  
    *  This module contains routines to keep track of the system time,.
    *  Since we don't have any interrupts in the firmware, even the
    *  timer is polled.  The timer must be called often enough
    *  to prevent missing the overflow of the CP0 COUNT
    *  register, approximately 2 billion cycles (half the count)
    * 
    *  Be sure to use the POLL() macro each time you enter a loop
    *  where you are waiting for some I/O event to occur or
    *  are waiting for time to elapse.
    *
    *  It is *not* a time-of-year clock.  The timer is only used
    *  for timing I/O events.
    *
    *  Internally, time is maintained in units of "CLOCKSPERTICK",
    *  which should be about tenths of seconds.
    *  
    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
    *  
    *********************************************************************  
    *
    *  Copyright 2000,2001,2002,2003
    *  Broadcom Corporation. All rights reserved.
    *  
    *  This software is furnished under license and may be used and 
    *  copied only in accordance with the following terms and 
    *  conditions.  Subject to these conditions, you may download, 
    *  copy, install, use, modify and distribute modified or unmodified 
    *  copies of this software in source and/or binary form.  No title 
    *  or ownership is transferred hereby.
    *  
    *  1) Any source code used, modified or distributed must reproduce 
    *     and retain this copyright notice and list of conditions 
    *     as they appear in the source file.
    *  
    *  2) No right is granted to use any trade name, trademark, or 
    *     logo of Broadcom Corporation.  The "Broadcom Corporation" 
    *     name may not be used to endorse or promote products derived 
    *     from this software without the prior written permission of 
    *     Broadcom Corporation.
    *  
    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 
    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 
    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF 
    *     THE POSSIBILITY OF SUCH DAMAGE.
    ********************************************************************* */


#include "lib_types.h"
#include "lib_printf.h"

#include "cfe_timer.h"

#include "cfe.h"

#include "bsp_config.h"
#include "cpu_config.h"

/* Foxconn add start by Jenny Zhao, 07/02/2008*/
#include "boardparms.h"
#include "bcm63xx_util.h"
#include "tftpd.h"
/* Foxconn add end by Jenny Zhao, 07/02/2008*/

#ifndef CFG_CPU_SPEED
#define CFG_CPU_SPEED	500000		/* CPU speed in Hz */
#endif

#ifndef CPUCFG_CYCLESPERCPUTICK
#define CPUCFG_CYCLESPERCPUTICK 1	/* CPU clock ticks per CP0 COUNT */
#endif

/*  *********************************************************************
    *  Externs
    ********************************************************************* */

extern int32_t _getticks(void);		/* return value of CP0 COUNT */

/*  *********************************************************************
    *  Data
    ********************************************************************* */

volatile int64_t cfe_ticks;		/* current system time */

int cfe_cpu_speed = CFG_CPU_SPEED;	/* CPU speed in clocks/second */

static unsigned int cfe_clocks_per_usec;
static unsigned int cfe_clocks_per_tick;

static int32_t cfe_oldcount;		/* For keeping track of ticks */
static int32_t cfe_remticks;
static int cfe_timer_initflg = 0;

/*
 * C0_COUNT clocks per microsecond and per tick.  Some CPUs tick CP0
 * every 'n' cycles, that's what CPUCFG_CYCLESPERCPUTICK is for.  */
#define CFE_CLOCKSPERUSEC (cfe_cpu_speed/1000000/(CPUCFG_CYCLESPERCPUTICK))
#define CFE_CLOCKSPERTICK (cfe_cpu_speed/(CFE_HZ)/(CPUCFG_CYCLESPERCPUTICK)) 

/* Foxconn add start by Jenny Zhao, 07/02/2008*/
extern int ui_docommands(char *str);

extern int g_tftp_upgrade_success;
extern int nmrp_server_detected;

//extern int g_reboot;
///* Foxconn add end by Jenny Zhao, 07/02/2008*/

/*  *********************************************************************
    *  cfe_timer_task()
    *  
    *  This routine is called as part of normal device polling to 
    *  update the system time.   We read the CP0 COUNT register,
    *  add the delta into our current time, convert to ticks,
    *  and keep track of the COUNT register overflow
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */


static void cfe_timer_task(void *arg)
{
    int32_t count;	
    int32_t deltaticks;
    int32_t clockspertick;

    /* Foxconn add start by Jenny Zhao, 07/02/2008*/
    static int64_t nmrp_previous_trigger_ticks = 0;
    static int64_t tftp_previous_trigger_ticks = 0;
    static int tftp_led_state = 0;
    /* Foxconn add start by Jenny Zhao, 07/02/2008*/

    clockspertick = CFE_CLOCKSPERTICK;

    count = _getticks();

    if (count >= cfe_oldcount) {
	deltaticks    = (count - cfe_oldcount) / clockspertick;
	cfe_remticks += (count - cfe_oldcount) % clockspertick;
	}
    else {
	deltaticks    = (cfe_oldcount - count) / clockspertick;
	cfe_remticks += (cfe_oldcount - count) % clockspertick;
	}

    cfe_ticks += deltaticks + (cfe_remticks / clockspertick);
    cfe_remticks %= clockspertick;
    cfe_oldcount = count;

    /* Foxconn add start by Jenny Zhao, 07/02/2008*/
    if(g_tftp_upgrade_success==1)
    {
        if((cfe_ticks - nmrp_previous_trigger_ticks) > 5) //500 ms
        {
            nmrp_previous_trigger_ticks = cfe_ticks;
            nmrp_led_toggle();
        }
    } 

    /* Blink Power LED in tftpd mode */
    if ( (get_tftpd_state() != TFTPD_STATE_OFF) && (nmrp_server_detected==0) )
    {
        if(tftp_led_state == 0) //led is off
        {
            if((cfe_ticks - tftp_previous_trigger_ticks) > 7) //700ms, spec. is 750ms
            {
                tftp_led_state = 1;
                power_led_toggle(tftp_led_state);
                tftp_previous_trigger_ticks = cfe_ticks;
            }
        }
        else  // led is currently on
        {
            if((cfe_ticks - tftp_previous_trigger_ticks) > 3) //300ms, spec. is 250ms
            {
                tftp_led_state = 0;
                power_led_toggle(tftp_led_state);
                tftp_previous_trigger_ticks = cfe_ticks;
            }
        }
    }
    /* Foxconn add end by Jenny Zhao, 07/02/2008*/    
}


/*  *********************************************************************
    *  cfe_timer_init()
    *  
    *  Initialize the timer module.
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

void cfe_timer_init(void)
{
    cfe_clocks_per_tick = CFE_CLOCKSPERTICK;
    cfe_clocks_per_usec = CFE_CLOCKSPERUSEC;
    if (cfe_clocks_per_usec == 0)
	cfe_clocks_per_usec = 1;    /* for the simulator */

    cfe_oldcount = _getticks();		/* get current COUNT register */
    cfe_ticks = 0;

    if (!cfe_timer_initflg) {
	cfe_bg_add(cfe_timer_task,NULL); /* add task for background polling */
	cfe_timer_initflg = 1;
	}
}


/*  *********************************************************************
    *  cfe_sleep(ticks)
    *  
    *  Sleep for 'ticks' ticks.  Background tasks are processed while
    *  we wait.
    *  
    *  Input parameters: 
    *  	   ticks - number of ticks to sleep (note: *not* clocks!)
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

void cfe_sleep(int ticks)
{
    int64_t timer;

    TIMER_SET(timer,ticks);
    while (!TIMER_EXPIRED(timer)) {	
	POLL();
	}
}



/*  *********************************************************************
    *  cfe_usleep(usec)
    *  
    *  Sleep for approximately the specified number of microseconds.
    *  
    *  Input parameters: 
    *  	   usec - number of microseconds to wait
    *  	   
    *  Return value:
    *  	   nothing
    ********************************************************************* */

void cfe_usleep(int usec)
{
    uint32_t newcount;
    uint32_t now;

    /* XXX fix the wrap problem */

    now = _getticks();
    newcount = now + usec*cfe_clocks_per_usec;

    if (newcount < now)  	/* wait for wraparound */
        while (_getticks() > now)
	    ;
    

    while (_getticks() < newcount)
	;
}