blob: 31ce6462603f7d023bee15e0a8d993f83739e56e [file] [log] [blame]
Stelian Popd1aea1c2008-01-30 21:15:54 +00001/*
2 * (C) Copyright 2007-2008
Stelian Pop5ee0c7f2011-11-01 00:00:39 +01003 * Stelian Pop <stelian@popies.net>
Stelian Popd1aea1c2008-01-30 21:15:54 +00004 * Lead Tech Design <www.leadtechdesign.com>
5 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Stelian Popd1aea1c2008-01-30 21:15:54 +00007 */
8
9#include <common.h>
Reinhard Meyerb06208c2010-11-07 13:26:14 +010010#include <asm/io.h>
Stelian Popd1aea1c2008-01-30 21:15:54 +000011#include <asm/arch/hardware.h>
Stelian Popd4bfbc52008-03-26 20:52:32 +010012#include <asm/arch/at91_pit.h>
13#include <asm/arch/at91_pmc.h>
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020014#include <asm/arch/clk.h>
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020015#include <div64.h>
Stelian Popd1aea1c2008-01-30 21:15:54 +000016
Reinhard Meyer0a1790a2010-10-05 16:54:35 +020017#if !defined(CONFIG_AT91FAMILY)
18# error You need to define CONFIG_AT91FAMILY in your board config!
19#endif
20
21DECLARE_GLOBAL_DATA_PTR;
22
Stelian Popd1aea1c2008-01-30 21:15:54 +000023/*
Stelian Popeea44aa2008-03-26 20:52:28 +010024 * We're using the AT91CAP9/SAM9 PITC in 32 bit mode, by
Stelian Popd1aea1c2008-01-30 21:15:54 +000025 * setting the 20 bit counter period to its maximum (0xfffff).
Reinhard Meyer0a1790a2010-10-05 16:54:35 +020026 * (See the relevant data sheets to understand that this really works)
27 *
28 * We do also mimic the typical powerpc way of incrementing
29 * two 32 bit registers called tbl and tbu.
30 *
31 * Those registers increment at 1/16 the main clock rate.
Stelian Popd1aea1c2008-01-30 21:15:54 +000032 */
Stelian Popd1aea1c2008-01-30 21:15:54 +000033
Reinhard Meyer0a1790a2010-10-05 16:54:35 +020034#define TIMER_LOAD_VAL 0xfffff
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020035
Reinhard Meyer0a1790a2010-10-05 16:54:35 +020036/*
37 * Use the PITC in full 32 bit incrementing mode
38 */
Stelian Pop6bf2de22008-03-26 21:52:27 +010039int timer_init(void)
Stelian Popd1aea1c2008-01-30 21:15:54 +000040{
Reinhard Meyere260d0b2010-11-03 15:39:55 +010041 at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
42 at91_pit_t *pit = (at91_pit_t *) ATMEL_BASE_PIT;
Reinhard Meyer0a1790a2010-10-05 16:54:35 +020043
44 /* Enable PITC Clock */
Reinhard Meyere260d0b2010-11-03 15:39:55 +010045 writel(1 << ATMEL_ID_SYS, &pmc->pcer);
Stelian Popd1aea1c2008-01-30 21:15:54 +000046
47 /* Enable PITC */
Jens Scharsiga4db1ca2010-02-03 22:46:58 +010048 writel(TIMER_LOAD_VAL | AT91_PIT_MR_EN , &pit->mr);
Stelian Popd1aea1c2008-01-30 21:15:54 +000049
Simon Glass6ed6e032012-12-13 20:48:32 +000050 gd->arch.timer_rate_hz = gd->arch.mck_rate_hz / 16;
Jean-Christophe PLAGNIOL-VILLARD1d4a3792009-04-16 21:30:48 +020051
Stelian Popd1aea1c2008-01-30 21:15:54 +000052 return 0;
53}
54
55/*
Reinhard Meyer0a1790a2010-10-05 16:54:35 +020056 * Return the number of timer ticks per second.
Stelian Popd1aea1c2008-01-30 21:15:54 +000057 */
58ulong get_tbclk(void)
59{
Simon Glass6ed6e032012-12-13 20:48:32 +000060 return gd->arch.timer_rate_hz;
Stelian Popd1aea1c2008-01-30 21:15:54 +000061}