summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/freescale/sdk_fman/Peripherals/FM/Rtc/fman_rtc.c
blob: acdf507e096d3e85594a26b80b4086a100cf1ec0 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright 2008-2013 Freescale Semiconductor Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Freescale Semiconductor nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 *
 * ALTERNATIVELY, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") as published by the Free Software
 * Foundation, either version 2 of that License or (at your option) any
 * later version.
 *
 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
 * 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) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "fsl_fman_rtc.h"

void fman_rtc_defconfig(struct rtc_cfg *cfg)
{
	int i;
	cfg->src_clk = DEFAULT_SRC_CLOCK;
	cfg->invert_input_clk_phase = DEFAULT_INVERT_INPUT_CLK_PHASE;
	cfg->invert_output_clk_phase = DEFAULT_INVERT_OUTPUT_CLK_PHASE;
	cfg->pulse_realign = DEFAULT_PULSE_REALIGN;
	for (i = 0; i < FMAN_RTC_MAX_NUM_OF_ALARMS; i++)
		cfg->alarm_polarity[i] = DEFAULT_ALARM_POLARITY;
	for (i = 0; i < FMAN_RTC_MAX_NUM_OF_EXT_TRIGGERS; i++)
		cfg->trigger_polarity[i] = DEFAULT_TRIGGER_POLARITY;
}

uint32_t fman_rtc_get_events(struct rtc_regs *regs)
{
	return ioread32be(&regs->tmr_tevent);
}

uint32_t fman_rtc_get_event(struct rtc_regs *regs, uint32_t ev_mask)
{
	return ioread32be(&regs->tmr_tevent) & ev_mask;
}

uint32_t fman_rtc_get_interrupt_mask(struct rtc_regs *regs)
{
	return ioread32be(&regs->tmr_temask);
}

void fman_rtc_set_interrupt_mask(struct rtc_regs *regs, uint32_t mask)
{
	iowrite32be(mask, &regs->tmr_temask);
}

void fman_rtc_ack_event(struct rtc_regs *regs, uint32_t events)
{
	iowrite32be(events, &regs->tmr_tevent);
}

uint32_t fman_rtc_check_and_clear_event(struct rtc_regs *regs)
{
	uint32_t event;

	event = ioread32be(&regs->tmr_tevent);
	event &= ioread32be(&regs->tmr_temask);

	if (event)
		iowrite32be(event, &regs->tmr_tevent);
	return event;
}

uint32_t fman_rtc_get_frequency_compensation(struct rtc_regs *regs)
{
	return ioread32be(&regs->tmr_add);
}

void fman_rtc_set_frequency_compensation(struct rtc_regs *regs, uint32_t val)
{
	iowrite32be(val, &regs->tmr_add);
}

void fman_rtc_enable_interupt(struct rtc_regs *regs, uint32_t events)
{
	fman_rtc_set_interrupt_mask(regs, fman_rtc_get_interrupt_mask(regs) | events);
}

void fman_rtc_disable_interupt(struct rtc_regs *regs, uint32_t events)
{
	fman_rtc_set_interrupt_mask(regs, fman_rtc_get_interrupt_mask(regs) & ~events);
}

void fman_rtc_set_timer_alarm_l(struct rtc_regs *regs, int index, uint32_t val)
{
	iowrite32be(val, &regs->tmr_alarm[index].tmr_alarm_l);
}

void fman_rtc_set_timer_fiper(struct rtc_regs *regs, int index, uint32_t val)
{
	iowrite32be(val, &regs->tmr_fiper[index]);
}

void fman_rtc_set_timer_alarm(struct rtc_regs *regs, int index, int64_t val)
{
	iowrite32be((uint32_t)val, &regs->tmr_alarm[index].tmr_alarm_l);
	iowrite32be((uint32_t)(val >> 32), &regs->tmr_alarm[index].tmr_alarm_h);
}

void fman_rtc_set_timer_offset(struct rtc_regs *regs, int64_t val)
{
	iowrite32be((uint32_t)val, &regs->tmr_off_l);
	iowrite32be((uint32_t)(val >> 32), &regs->tmr_off_h);
}

uint64_t fman_rtc_get_trigger_stamp(struct rtc_regs *regs, int id)
{
	uint64_t time;
	/* TMR_CNT_L must be read first to get an accurate value */
	time = (uint64_t)ioread32be(&regs->tmr_etts[id].tmr_etts_l);
	time |= ((uint64_t)ioread32be(&regs->tmr_etts[id].tmr_etts_h)
		<< 32);

	return time;
}

uint32_t fman_rtc_get_timer_ctrl(struct rtc_regs *regs)
{
	return ioread32be(&regs->tmr_ctrl);
}

void fman_rtc_set_timer_ctrl(struct rtc_regs *regs, uint32_t val)
{
	iowrite32be(val, &regs->tmr_ctrl);
}

void fman_rtc_timers_soft_reset(struct rtc_regs *regs)
{
	fman_rtc_set_timer_ctrl(regs, FMAN_RTC_TMR_CTRL_TMSR);
	udelay(10);
	fman_rtc_set_timer_ctrl(regs, 0);
}

void fman_rtc_init(struct rtc_cfg *cfg, struct rtc_regs *regs, int num_alarms,
		int num_fipers, int num_ext_triggers, bool init_freq_comp,
		uint32_t freq_compensation, uint32_t output_clock_divisor)
{
	uint32_t            tmr_ctrl;
	int			i;

	fman_rtc_timers_soft_reset(regs);

	/* Set the source clock */
	switch (cfg->src_clk) {
	case E_FMAN_RTC_SOURCE_CLOCK_SYSTEM:
		tmr_ctrl = FMAN_RTC_TMR_CTRL_CKSEL_MAC_CLK;
		break;
	case E_FMAN_RTC_SOURCE_CLOCK_OSCILATOR:
		tmr_ctrl = FMAN_RTC_TMR_CTRL_CKSEL_OSC_CLK;
		break;
	default:
		/* Use a clock from the External TMR reference clock.*/
		tmr_ctrl = FMAN_RTC_TMR_CTRL_CKSEL_EXT_CLK;
		break;
	}

	/* whatever period the user picked, the timestamp will advance in '1'
	* every time the period passed. */
	tmr_ctrl |= ((1 << FMAN_RTC_TMR_CTRL_TCLK_PERIOD_SHIFT) &
				FMAN_RTC_TMR_CTRL_TCLK_PERIOD_MASK);

	if (cfg->invert_input_clk_phase)
		tmr_ctrl |= FMAN_RTC_TMR_CTRL_CIPH;
	if (cfg->invert_output_clk_phase)
		tmr_ctrl |= FMAN_RTC_TMR_CTRL_COPH;

	for (i = 0; i < num_alarms; i++) {
		if (cfg->alarm_polarity[i] ==
			E_FMAN_RTC_ALARM_POLARITY_ACTIVE_LOW)
			tmr_ctrl |= (FMAN_RTC_TMR_CTRL_ALMP1 >> i);
	}

	for (i = 0; i < num_ext_triggers; i++)
		if (cfg->trigger_polarity[i] ==
			E_FMAN_RTC_TRIGGER_ON_FALLING_EDGE)
			tmr_ctrl |= (FMAN_RTC_TMR_CTRL_ETEP1 << i);

	if (!cfg->timer_slave_mode && cfg->bypass)
		tmr_ctrl |= FMAN_RTC_TMR_CTRL_BYP;

	fman_rtc_set_timer_ctrl(regs, tmr_ctrl);
	if (init_freq_comp)
		fman_rtc_set_frequency_compensation(regs, freq_compensation);

	/* Clear TMR_ALARM registers */
	for (i = 0; i < num_alarms; i++)
		fman_rtc_set_timer_alarm(regs, i, 0xFFFFFFFFFFFFFFFFLL);

	/* Clear TMR_TEVENT */
	fman_rtc_ack_event(regs, FMAN_RTC_TMR_TEVENT_ALL);

	/* Initialize TMR_TEMASK */
	fman_rtc_set_interrupt_mask(regs, 0);

	/* Clear TMR_FIPER registers */
	for (i = 0; i < num_fipers; i++)
		fman_rtc_set_timer_fiper(regs, i, 0xFFFFFFFF);

	/* Initialize TMR_PRSC */
	iowrite32be(output_clock_divisor, &regs->tmr_prsc);

	/* Clear TMR_OFF */
	fman_rtc_set_timer_offset(regs, 0);
}

bool fman_rtc_is_enabled(struct rtc_regs *regs)
{
	return (bool)(fman_rtc_get_timer_ctrl(regs) & FMAN_RTC_TMR_CTRL_TE);
}

void fman_rtc_enable(struct rtc_regs *regs, bool reset_clock)
{
	uint32_t tmr_ctrl = fman_rtc_get_timer_ctrl(regs);

	/* TODO check that no timestamping MACs are working in this stage. */
	if (reset_clock) {
		fman_rtc_set_timer_ctrl(regs, (tmr_ctrl | FMAN_RTC_TMR_CTRL_TMSR));

		udelay(10);
		/* Clear TMR_OFF */
		fman_rtc_set_timer_offset(regs, 0);
	}

	fman_rtc_set_timer_ctrl(regs, (tmr_ctrl | FMAN_RTC_TMR_CTRL_TE));
}

void fman_rtc_disable(struct rtc_regs *regs)
{
	fman_rtc_set_timer_ctrl(regs, (fman_rtc_get_timer_ctrl(regs)
					& ~(FMAN_RTC_TMR_CTRL_TE)));
}

void fman_rtc_clear_periodic_pulse(struct rtc_regs *regs, int id)
{
	uint32_t tmp_reg;
	if (id == 0)
		tmp_reg = FMAN_RTC_TMR_TEVENT_PP1;
	else
		tmp_reg = FMAN_RTC_TMR_TEVENT_PP2;
	fman_rtc_disable_interupt(regs, tmp_reg);

	tmp_reg = fman_rtc_get_timer_ctrl(regs);
	if (tmp_reg & FMAN_RTC_TMR_CTRL_FS)
		fman_rtc_set_timer_ctrl(regs, tmp_reg & ~FMAN_RTC_TMR_CTRL_FS);

	fman_rtc_set_timer_fiper(regs, id, 0xFFFFFFFF);
}

void fman_rtc_clear_external_trigger(struct rtc_regs *regs, int id)
{
	uint32_t    tmpReg, tmp_ctrl;

	if (id == 0)
		tmpReg = FMAN_RTC_TMR_TEVENT_ETS1;
	else
		tmpReg = FMAN_RTC_TMR_TEVENT_ETS2;
	fman_rtc_disable_interupt(regs, tmpReg);

	if (id == 0)
		tmpReg = FMAN_RTC_TMR_CTRL_PP1L;
	else
		tmpReg = FMAN_RTC_TMR_CTRL_PP2L;
	tmp_ctrl = fman_rtc_get_timer_ctrl(regs);
	if (tmp_ctrl & tmpReg)
		fman_rtc_set_timer_ctrl(regs, tmp_ctrl & ~tmpReg);
}

void fman_rtc_set_alarm(struct rtc_regs *regs, int id, uint32_t val, bool enable)
{
	uint32_t    tmpReg;
	fman_rtc_set_timer_alarm(regs, id, val);
	if (enable) {
		if (id == 0)
			tmpReg = FMAN_RTC_TMR_TEVENT_ALM1;
		else
			tmpReg = FMAN_RTC_TMR_TEVENT_ALM2;
		fman_rtc_enable_interupt(regs, tmpReg);
	}
}

void fman_rtc_set_periodic_pulse(struct rtc_regs *regs, int id, uint32_t val,
						bool enable)
{
	uint32_t    tmpReg;
	fman_rtc_set_timer_fiper(regs, id, val);
	if (enable) {
		if (id == 0)
			tmpReg = FMAN_RTC_TMR_TEVENT_PP1;
		else
			tmpReg = FMAN_RTC_TMR_TEVENT_PP2;
		fman_rtc_enable_interupt(regs, tmpReg);
	}
}

void fman_rtc_set_ext_trigger(struct rtc_regs *regs, int id, bool enable,
						bool use_pulse_as_input)
{
	uint32_t    tmpReg;
	if (enable) {
		if (id == 0)
			tmpReg = FMAN_RTC_TMR_TEVENT_ETS1;
		else
			tmpReg = FMAN_RTC_TMR_TEVENT_ETS2;
		fman_rtc_enable_interupt(regs, tmpReg);
	}
	if (use_pulse_as_input)	{
		if (id == 0)
			tmpReg = FMAN_RTC_TMR_CTRL_PP1L;
		else
			tmpReg = FMAN_RTC_TMR_CTRL_PP2L;
		fman_rtc_set_timer_ctrl(regs, fman_rtc_get_timer_ctrl(regs) | tmpReg);
	}
}