blob: a8cf0e4bd7992aa9a05414ee761141f3da70301e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stelian Popfefb6c12008-01-30 21:15:54 +00002/*
3 * (C) Copyright 2007-2008
Stelian Popc9e798d2011-11-01 00:00:39 +01004 * Stelian Pop <stelian@popies.net>
Stelian Popfefb6c12008-01-30 21:15:54 +00005 * Lead Tech Design <www.leadtechdesign.com>
Stelian Popfefb6c12008-01-30 21:15:54 +00006 */
7
8#include <common.h>
Simon Glass691d7192020-05-10 11:40:02 -06009#include <init.h>
Simon Glass049f8d62019-12-28 10:44:59 -070010#include <time.h>
Simon Glass401d1c42020-10-30 21:38:53 -060011#include <asm/global_data.h>
Reinhard Meyer86592f62010-11-07 13:26:14 +010012#include <asm/io.h>
Stelian Popfefb6c12008-01-30 21:15:54 +000013#include <asm/arch/hardware.h>
Stelian Pop983c1db2008-03-26 20:52:32 +010014#include <asm/arch/at91_pit.h>
Jean-Christophe PLAGNIOL-VILLARD6ebff362009-04-16 21:30:48 +020015#include <asm/arch/clk.h>
Jean-Christophe PLAGNIOL-VILLARD6ebff362009-04-16 21:30:48 +020016#include <div64.h>
Stelian Popfefb6c12008-01-30 21:15:54 +000017
Reinhard Meyer5dca7102010-10-05 16:54:35 +020018#if !defined(CONFIG_AT91FAMILY)
19# error You need to define CONFIG_AT91FAMILY in your board config!
20#endif
21
22DECLARE_GLOBAL_DATA_PTR;
23
Stelian Popfefb6c12008-01-30 21:15:54 +000024/*
Stelian Popa8a78f22008-03-26 20:52:28 +010025 * We're using the AT91CAP9/SAM9 PITC in 32 bit mode, by
Stelian Popfefb6c12008-01-30 21:15:54 +000026 * setting the 20 bit counter period to its maximum (0xfffff).
Reinhard Meyer5dca7102010-10-05 16:54:35 +020027 * (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.
Stelian Popfefb6c12008-01-30 21:15:54 +000033 */
Stelian Popfefb6c12008-01-30 21:15:54 +000034
Reinhard Meyer5dca7102010-10-05 16:54:35 +020035#define TIMER_LOAD_VAL 0xfffff
Jean-Christophe PLAGNIOL-VILLARD6ebff362009-04-16 21:30:48 +020036
Reinhard Meyer5dca7102010-10-05 16:54:35 +020037/*
38 * Use the PITC in full 32 bit incrementing mode
39 */
Stelian Pop61106a52008-03-26 21:52:27 +010040int timer_init(void)
Stelian Popfefb6c12008-01-30 21:15:54 +000041{
Reinhard Meyer9f3fe902010-11-03 15:39:55 +010042 at91_pit_t *pit = (at91_pit_t *) ATMEL_BASE_PIT;
Reinhard Meyer5dca7102010-10-05 16:54:35 +020043
Wenyou Yangeced5a72016-02-03 10:16:49 +080044 at91_periph_clk_enable(ATMEL_ID_SYS);
Stelian Popfefb6c12008-01-30 21:15:54 +000045
46 /* Enable PITC */
Jens Scharsig0cf0b932010-02-03 22:46:58 +010047 writel(TIMER_LOAD_VAL | AT91_PIT_MR_EN , &pit->mr);
Stelian Popfefb6c12008-01-30 21:15:54 +000048
Simon Glassb3390512012-12-13 20:48:32 +000049 gd->arch.timer_rate_hz = gd->arch.mck_rate_hz / 16;
Jean-Christophe PLAGNIOL-VILLARD6ebff362009-04-16 21:30:48 +020050
Stelian Popfefb6c12008-01-30 21:15:54 +000051 return 0;
52}
53
54/*
Reinhard Meyer5dca7102010-10-05 16:54:35 +020055 * Return the number of timer ticks per second.
Stelian Popfefb6c12008-01-30 21:15:54 +000056 */
57ulong get_tbclk(void)
58{
Simon Glassb3390512012-12-13 20:48:32 +000059 return gd->arch.timer_rate_hz;
Stelian Popfefb6c12008-01-30 21:15:54 +000060}