summaryrefslogtreecommitdiffstats
path: root/arch/mips/galileo-boards/ev64120/cntmr.c
blob: 568a88cb06607bda27e5d363117832779aaa6e93 (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
/* cntmr.c - GT counters/timers functions */

/* Copyright - Galileo technology. 9/3/2000 */

/*
DESCRIPTION
This file contains function which serves the user with a complete interface
to the GT internal counters and timers, please advise: each counter/timer unit
can function only as a counter or a timer at current time.
Counter/timer 0 is 32 bit wide.
Counters/timers 1-3 are 24 bit wide.
*/

/* includes */

#ifdef __linux__
#include <asm/galileo/evb64120A/cntmr.h>
#include <asm/galileo/evb64120A/core.h>
#else
#include "cntmr.h"
#include "core.h"
#endif

/********************************************************************
* cntTmrStart - Starts a counter/timer with given an initiate value.
*
* INPUTS:  unsigned int countNum - Selects one of the 8 counters/timers.
*          unsigned int countValue - Initial value for count down. 
*          CNT_TMR_OP_MODES opMode - Set Mode, Counter or Timer.
*
* RETURNS: false if one of the parameters is erroneous, true otherwise.
*********************************************************************/

bool cntTmrStart(CNTMR_NUM countNum, unsigned int countValue,
		 CNT_TMR_OP_MODES opMode)
{
	unsigned int command = 1;
	unsigned int value;

	if (countNum > LAST_CNTMR)
		return false;
	else {
		GT_REG_READ(TIMER_COUNTER_CONTROL, &value);
		cntTmrDisable(countNum);
		GT_REG_WRITE((TIMER_COUNTER0 + (4 * countNum)),
			     countValue);
		command = command << countNum * 2;
		value = value | command;
		command = command << 1;
		switch (opMode) {
		case TIMER:	/* The Timer/Counter bit set to logic '1' */
			value = value | command;
			break;
		case COUNTER:	/* The Timer/Counter bit set to logic '0' */
			value = value & ~command;
			break;
		default:
			return false;
		}
		GT_REG_WRITE(TIMER_COUNTER_CONTROL, value);
		return true;
	}
}

/********************************************************************
* cntTmrDisable - Disables the timer/counter operation and return its 
*                 value.
*
* INPUTS:  unsigned int countNum - Selects one of the 8 counters/timers.
* RETURNS: The counter/timer value (unsigned int), if any of the arguments are 
*          erroneous return 0.
*********************************************************************/

unsigned int cntTmrDisable(CNTMR_NUM countNum)
{
	unsigned int command = 1;
	unsigned int regValue;
	unsigned int value;

	GT_REG_READ(TIMER_COUNTER_CONTROL, &value);
	if (countNum > LAST_CNTMR)
		return 0;
	GT_REG_READ(TIMER_COUNTER0 + 4 * countNum, &regValue);
	command = command << countNum * 2;	/* Disable the timer/counter */
	value = value & ~command;
	GT_REG_WRITE(TIMER_COUNTER_CONTROL, value);
	return regValue;
}

/********************************************************************
* cntTmrRead - Reads a timer or a counter value. (This operation can be
*              perform while the counter/timer is active).
*
* RETURNS: The counter/timer value. If wrong input value, return 0.
*********************************************************************/

unsigned int cntTmrRead(CNTMR_NUM countNum)
{
	unsigned int value;
	if (countNum > LAST_CNTMR)
		return 0;
	else
		GT_REG_READ(TIMER_COUNTER0 + countNum * 4, &value);
	return value;
}

/********************************************************************
* cntTmrEnable - Set enable-bit of timer/counter.
*                Be aware: If the counter/timer is active, this function
*                          will terminate with an false.
*
* INPUTS:  unsigned int countNum - Selects one of the 8 counters/timers.
* RETURNS: false if one of the parameters is erroneous, true otherwise.
*********************************************************************/

bool cntTmrEnable(CNTMR_NUM countNum)
{
	unsigned int command = 1;
	unsigned int value;
	GT_REG_READ(TIMER_COUNTER_CONTROL, &value);
	if (countNum > LAST_CNTMR)
		return false;
	else {
		command = command << countNum * 2;
		if ((command & value) != 0)	/* ==> The counter/timer is enabled */
			return false;	/* doesn't make sense to Enable an "enabled" counter */
		value = value | command;
		GT_REG_WRITE(TIMER_COUNTER_CONTROL, value);
		return true;
	}
}

/********************************************************************
* cntTmrLoad - loading value for timer number countNum.
*              Be aware: If this function try to load value to an enabled
*                        counter/timer it terminate with false.
*
* INPUTS:  unsigned int countNum - Selects one of the 8 counters/timers.
*          unsigned int countValue - The value for load the register.
* RETURNS: false if one of the parameters is erroneous, true otherwise.
*********************************************************************/

bool cntTmrLoad(unsigned int countNum, unsigned int countValue)
{
	unsigned int command = 1;
	unsigned int value;
	GT_REG_READ(TIMER_COUNTER_CONTROL, &value);
	if (countNum > LAST_CNTMR)
		return false;
	else {
		command = command << countNum * 2;
		value = value & command;
		if (value != 0) {	/* ==> The counter/timer is enabled */
			return false;	/* can't reload value when counter/timer is enabled */
		} else {
			GT_REG_WRITE((TIMER_COUNTER0 + (4 * countNum)),
				     countValue);
			return true;
		}

	}
}

/********************************************************************
* cntTmrSetMode - Configurate the Mode of the channel to work as a counter
*                 or as a timer. (for more details on the different between
*                 those two modes is written in the Data Sheet).
*                 NOTE: This function only set the counter/timer mode and
*                 don't enable it. 
*                 Be aware: If this function try to load value to an enabled
*                           counter/timer it terminate with false.
* 
* INPUTS:  unsigned int countNum - Selects one of the 8 counters/timers.
*          CNT_TMR_OP_MODES opMode - TIMER or COUNTER mode.
* RETURNS: false if one of the parameters is erroneous true otherwise .
*********************************************************************/

bool cntTmrSetMode(CNTMR_NUM countNum, CNT_TMR_OP_MODES opMode)
{
	unsigned int command = 1;
	unsigned int value;

	GT_REG_READ(TIMER_COUNTER_CONTROL, &value);
	if (countNum > LAST_CNTMR)
		return false;
	else {
		command = command << countNum * 2;
		value = value & command;
		if (value != 0) {	/* ==> The counter/timer is enabled */
			return false;	/* can't set the Mode when counter/timer is enabled */
		} else {
			command = command << 1;
			switch (opMode) {
			case TIMER:
				value = value | command;	/* The Timer/Counter bit set to logic '1' */
				break;
			case COUNTER:
				value = value & ~command;	/*The Timer/Counter bit set to logic '0' */
				break;
			default:
				return false;
			}
			GT_REG_WRITE(TIMER_COUNTER_CONTROL, value);
			return true;
		}
	}
}