blob: 9f08806fb127003236d9215983786f813d7b7b54 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bo Shen60f3dd32013-05-12 22:40:54 +00002/*
3 * (C) Copyright 2007-2008
4 * Stelian Pop <stelian@popies.net>
5 * Lead Tech Design <www.leadtechdesign.com>
6 *
7 * (C) Copyright 2013
8 * Bo Shen <voice.shen@atmel.com>
Bo Shen60f3dd32013-05-12 22:40:54 +00009 */
10
11#include <common.h>
12#include <asm/io.h>
13#include <asm/arch/hardware.h>
14#include <asm/arch/at91_pit.h>
Bo Shen60f3dd32013-05-12 22:40:54 +000015#include <asm/arch/clk.h>
16#include <div64.h>
17
18#if !defined(CONFIG_AT91FAMILY)
19# error You need to define CONFIG_AT91FAMILY in your board config!
20#endif
21
22DECLARE_GLOBAL_DATA_PTR;
23
24/*
25 * We're using the SAMA5D3x PITC in 32 bit mode, by
26 * setting the 20 bit counter period to its maximum (0xfffff).
27 * (See the relevant data sheets to understand that this really works)
28 *
29 * We do also mimic the typical powerpc way of incrementing
30 * two 32 bit registers called tbl and tbu.
31 *
32 * Those registers increment at 1/16 the main clock rate.
33 */
34
35#define TIMER_LOAD_VAL 0xfffff
36
Bo Shen60f3dd32013-05-12 22:40:54 +000037/*
38 * Use the PITC in full 32 bit incrementing mode
39 */
40int timer_init(void)
41{
42 at91_pit_t *pit = (at91_pit_t *)ATMEL_BASE_PIT;
43
44 /* Enable PITC Clock */
Bo Shenf2afc3b2013-11-15 11:12:32 +080045 at91_periph_clk_enable(ATMEL_ID_PIT);
Bo Shen60f3dd32013-05-12 22:40:54 +000046
47 /* Enable PITC */
48 writel(TIMER_LOAD_VAL | AT91_PIT_MR_EN , &pit->mr);
49
Bo Shen58645902014-11-10 15:24:02 +080050 gd->arch.timer_rate_hz = get_pit_clk_rate() / 16;
51
Bo Shen60f3dd32013-05-12 22:40:54 +000052 return 0;
53}
54
55/*
Bo Shen60f3dd32013-05-12 22:40:54 +000056 * Return the number of timer ticks per second.
57 */
58ulong get_tbclk(void)
59{
60 return gd->arch.timer_rate_hz;
61}