Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007-2008 |
Stelian Pop | c9e798d | 2011-11-01 00:00:39 +0100 | [diff] [blame] | 3 | * Stelian Pop <stelian@popies.net> |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 4 | * Lead Tech Design <www.leadtechdesign.com> |
| 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Reinhard Meyer | 86592f6 | 2010-11-07 13:26:14 +0100 | [diff] [blame] | 10 | #include <asm/io.h> |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 11 | #include <asm/arch/hardware.h> |
Stelian Pop | 983c1db | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 12 | #include <asm/arch/at91_pit.h> |
| 13 | #include <asm/arch/at91_pmc.h> |
Jean-Christophe PLAGNIOL-VILLARD | 6ebff36 | 2009-04-16 21:30:48 +0200 | [diff] [blame] | 14 | #include <asm/arch/clk.h> |
Jean-Christophe PLAGNIOL-VILLARD | 6ebff36 | 2009-04-16 21:30:48 +0200 | [diff] [blame] | 15 | #include <div64.h> |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 16 | |
Reinhard Meyer | 5dca710 | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 17 | #if !defined(CONFIG_AT91FAMILY) |
| 18 | # error You need to define CONFIG_AT91FAMILY in your board config! |
| 19 | #endif |
| 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 23 | /* |
Stelian Pop | a8a78f2 | 2008-03-26 20:52:28 +0100 | [diff] [blame] | 24 | * We're using the AT91CAP9/SAM9 PITC in 32 bit mode, by |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 25 | * setting the 20 bit counter period to its maximum (0xfffff). |
Reinhard Meyer | 5dca710 | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 26 | * (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 Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 32 | */ |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 33 | |
Reinhard Meyer | 5dca710 | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 34 | #define TIMER_LOAD_VAL 0xfffff |
Jean-Christophe PLAGNIOL-VILLARD | 6ebff36 | 2009-04-16 21:30:48 +0200 | [diff] [blame] | 35 | |
Reinhard Meyer | 5dca710 | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 36 | /* |
| 37 | * Use the PITC in full 32 bit incrementing mode |
| 38 | */ |
Stelian Pop | 61106a5 | 2008-03-26 21:52:27 +0100 | [diff] [blame] | 39 | int timer_init(void) |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 40 | { |
Reinhard Meyer | 9f3fe90 | 2010-11-03 15:39:55 +0100 | [diff] [blame] | 41 | at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC; |
| 42 | at91_pit_t *pit = (at91_pit_t *) ATMEL_BASE_PIT; |
Reinhard Meyer | 5dca710 | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 43 | |
| 44 | /* Enable PITC Clock */ |
Reinhard Meyer | 9f3fe90 | 2010-11-03 15:39:55 +0100 | [diff] [blame] | 45 | writel(1 << ATMEL_ID_SYS, &pmc->pcer); |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 46 | |
| 47 | /* Enable PITC */ |
Jens Scharsig | 0cf0b93 | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 48 | writel(TIMER_LOAD_VAL | AT91_PIT_MR_EN , &pit->mr); |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 49 | |
Simon Glass | b339051 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 50 | gd->arch.timer_rate_hz = gd->arch.mck_rate_hz / 16; |
Jean-Christophe PLAGNIOL-VILLARD | 6ebff36 | 2009-04-16 21:30:48 +0200 | [diff] [blame] | 51 | |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | /* |
Reinhard Meyer | 5dca710 | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 56 | * Return the number of timer ticks per second. |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 57 | */ |
| 58 | ulong get_tbclk(void) |
| 59 | { |
Simon Glass | b339051 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 60 | return gd->arch.timer_rate_hz; |
Stelian Pop | fefb6c1 | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 61 | } |