aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/SAMA/LLD/SDMMCv1/ch_sdmmc_tc.c
blob: 9b66742c7fce0f47a18bb9f3be318e391c45c82d (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
#include "hal.h"
#include "sama_sdmmc_lld.h"
#include "ch_sdmmc_pmc.h"
#include "ch_sdmmc_tc.h"

#if SDMMC_USE_TC == 1

/*------------------------------------------------------------------------------
 *         Global functions
 *------------------------------------------------------------------------------*/
uint32_t get_tc_id_from_addr(const Tc* addr, uint8_t channel)
{
	(void)channel;
#ifdef TC0
	if (addr == TC0)
#ifdef ID_TC0_CH0
		return ID_TC0 + channel;
#else
		return ID_TC0;
#endif
#endif

#ifdef TC1
	if (addr == TC1)
#ifdef ID_TC1_CH0
		return ID_TC1 + channel;
#else
		return ID_TC1;
#endif
#endif

#ifdef TC2
	if (addr == TC2)
#ifdef ID_TC2_CH0
		return ID_TC2 + channel;
#else
		return ID_TC2;
#endif
#endif

#ifdef TC3
	if (addr == TC3)
#ifdef ID_TC3_CH0
		return ID_TC3 + channel;
#else
		return ID_TC3;
#endif
#endif
	return ID_PERIPH_COUNT;
}
void tc_configure(Tc *tc, uint32_t channel, uint32_t mode)
{
	TcChannel *ch;

//	assert(channel < ARRAY_SIZE(tc->TC_CHANNEL));

	ch = &tc->TC_CHANNEL[channel];

	/*  Disable TC clock */
	ch->TC_CCR = TC_CCR_CLKDIS;

	/*  Disable interrupts */
	ch->TC_IDR = ch->TC_IMR;

	/*  Clear status register */
	ch->TC_SR;

	/*  Set mode */
	ch->TC_CMR = mode;
}

void tc_start(Tc *tc, uint32_t channel)
{
	TcChannel *ch;

//	assert(channel < ARRAY_SIZE(tc->TC_CHANNEL));

	ch = &tc->TC_CHANNEL[channel];

	/*  Clear status register */
	ch->TC_SR;

	ch->TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
}

void tc_stop(Tc *tc, uint32_t channel)
{
	TcChannel *ch;

//	assert(channel < ARRAY_SIZE(tc->TC_CHANNEL));

	ch = &tc->TC_CHANNEL[channel];

	ch->TC_CCR = TC_CCR_CLKDIS;
}

void tc_enable_it(Tc *tc, uint32_t channel, uint32_t mask)
{
	TcChannel *ch;

//	assert(channel < ARRAY_SIZE(tc->TC_CHANNEL));

	ch = &tc->TC_CHANNEL[channel];

	ch->TC_IER = mask;
}

void tc_disable_it(Tc *tc, uint32_t channel, uint32_t mask)
{
	TcChannel *ch;

//	assert(channel < ARRAY_SIZE(tc->TC_CHANNEL));

	ch = &tc->TC_CHANNEL[channel];

	ch->TC_IDR = mask;
}

uint32_t tc_find_best_clock_source(Tc *tc, uint8_t channel, uint32_t freq)
{
	const int tcclks[] = {
		TC_CMR_TCCLKS_TIMER_CLOCK1,
		TC_CMR_TCCLKS_TIMER_CLOCK2,
		TC_CMR_TCCLKS_TIMER_CLOCK3,
		TC_CMR_TCCLKS_TIMER_CLOCK4,
		TC_CMR_TCCLKS_TIMER_CLOCK5,
	};
	int i, best, higher;
	int best_freq, higher_freq;

	best = higher = -1;
	best_freq = higher_freq = 0;
	for (i = 0 ; i <(int) ARRAY_SIZE(tcclks) ; i++) {
		uint32_t f = tc_get_available_freq(tc, channel, tcclks[i]);
		if ( higher < 0 || (f > ((uint32_t)higher_freq) ) ) {
			higher_freq = f;
			higher = tcclks[i];
		}
		if (f > freq) {
			if (best < 0 || (f - freq) < (f - best_freq)) {
				best_freq = f;
				best = tcclks[i];
			}
		}
	}

	if (best < 0)
		best = higher;

	return best;
}

uint32_t tc_get_status(Tc *tc, uint32_t channel)
{
//	assert(channel < ARRAY_SIZE(tc->TC_CHANNEL));

	return tc->TC_CHANNEL[channel].TC_SR;
}

uint32_t tc_get_available_freq(Tc *tc, uint8_t channel, uint8_t tc_clks)
{
	uint32_t tc_id = get_tc_id_from_addr(tc, channel);

	switch (tc_clks) {
	case TC_CMR_TCCLKS_TIMER_CLOCK1:
#ifdef CONFIG_HAVE_PMC_GENERATED_CLOCKS
		if (pmc_is_gck_enabled(tc_id))
			return pmc_get_gck_clock(tc_id);
		else
			return 0;
#else
		return pmc_get_peripheral_clock(tc_id) >> 1;
#endif
	case TC_CMR_TCCLKS_TIMER_CLOCK2:
		return pmc_get_peripheral_clock(tc_id) >> 3;
	case TC_CMR_TCCLKS_TIMER_CLOCK3:
		return pmc_get_peripheral_clock(tc_id) >> 5;
	case TC_CMR_TCCLKS_TIMER_CLOCK4:
		return pmc_get_peripheral_clock(tc_id) >> 7;
	case TC_CMR_TCCLKS_TIMER_CLOCK5:
		return pmc_get_slow_clock();
	default:
		return 0;
	}
}

uint32_t tc_get_channel_freq(Tc *tc, uint32_t channel)
{
	TcChannel* ch;

//	assert(channel < ARRAY_SIZE(tc->TC_CHANNEL));

	ch = &tc->TC_CHANNEL[channel];

	return tc_get_available_freq(tc, channel, ch->TC_CMR & TC_CMR_TCCLKS_Msk);
}

void tc_set_ra_rb_rc(Tc *tc, uint32_t channel,
	uint32_t *ra, uint32_t *rb, uint32_t *rc)
{
	TcChannel* ch;

	//assert(channel < ARRAY_SIZE(tc->TC_CHANNEL));

	ch = &tc->TC_CHANNEL[channel];

//	assert(!(ra && rb) || (ch->TC_CMR & TC_CMR_WAVE));

	if (ra)
		ch->TC_RA = *ra;
	if (rb)
		ch->TC_RB = *rb;
	if (rc)
		ch->TC_RC = *rc;
}

void tc_get_ra_rb_rc(Tc *tc, uint32_t channel,
	uint32_t *ra, uint32_t *rb, uint32_t *rc)
{
	TcChannel* ch;

//	assert(channel < ARRAY_SIZE(tc->TC_CHANNEL));

	ch = &tc->TC_CHANNEL[channel];

	if (ra)
		*ra = ch->TC_RA;
	if (rb)
		*rb = ch->TC_RB;
	if (rc)
		*rc = ch->TC_RC;
}

#ifdef CONFIG_HAVE_TC_FAULT_MODE

void tc_set_fault_mode(Tc *tc, uint32_t mode)
{
	tc->TC_FMR = mode;
}

#endif /* CONFIG_HAVE_TC_FAULT_MODE */

uint32_t tc_get_cv(Tc* tc, uint32_t channel)
{
	TcChannel* ch;

//	assert(channel < ARRAY_SIZE(tc->TC_CHANNEL));

	ch = &tc->TC_CHANNEL[channel];

	return ch->TC_CV;
}

#endif