Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007-2008 |
Stelian Pop | 5ee0c7f | 2011-11-01 00:00:39 +0100 | [diff] [blame] | 3 | * Stelian Pop <stelian@popies.net> |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 4 | * Lead Tech Design <www.leadtechdesign.com> |
| 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Reinhard Meyer | b06208c | 2010-11-07 13:26:14 +0100 | [diff] [blame] | 10 | #include <asm/io.h> |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 11 | #include <asm/arch/hardware.h> |
Stelian Pop | d4bfbc5 | 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 | 1d4a379 | 2009-04-16 21:30:48 +0200 | [diff] [blame] | 14 | #include <asm/arch/clk.h> |
Jean-Christophe PLAGNIOL-VILLARD | 1d4a379 | 2009-04-16 21:30:48 +0200 | [diff] [blame] | 15 | #include <div64.h> |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 16 | |
Reinhard Meyer | 0a1790a | 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 | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 23 | /* |
Stelian Pop | eea44aa | 2008-03-26 20:52:28 +0100 | [diff] [blame] | 24 | * We're using the AT91CAP9/SAM9 PITC in 32 bit mode, by |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 25 | * setting the 20 bit counter period to its maximum (0xfffff). |
Reinhard Meyer | 0a1790a | 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 | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 32 | */ |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 33 | |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 34 | #define TIMER_LOAD_VAL 0xfffff |
Jean-Christophe PLAGNIOL-VILLARD | 1d4a379 | 2009-04-16 21:30:48 +0200 | [diff] [blame] | 35 | |
| 36 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 37 | { |
| 38 | tick *= CONFIG_SYS_HZ; |
Simon Glass | 6ed6e03 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 39 | do_div(tick, gd->arch.timer_rate_hz); |
Jean-Christophe PLAGNIOL-VILLARD | 1d4a379 | 2009-04-16 21:30:48 +0200 | [diff] [blame] | 40 | |
| 41 | return tick; |
| 42 | } |
| 43 | |
| 44 | static inline unsigned long long usec_to_tick(unsigned long long usec) |
| 45 | { |
Simon Glass | 6ed6e03 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 46 | usec *= gd->arch.timer_rate_hz; |
Jean-Christophe PLAGNIOL-VILLARD | 1d4a379 | 2009-04-16 21:30:48 +0200 | [diff] [blame] | 47 | do_div(usec, 1000000); |
| 48 | |
| 49 | return usec; |
| 50 | } |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 51 | |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 52 | /* |
| 53 | * Use the PITC in full 32 bit incrementing mode |
| 54 | */ |
Stelian Pop | 6bf2de2 | 2008-03-26 21:52:27 +0100 | [diff] [blame] | 55 | int timer_init(void) |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 56 | { |
Reinhard Meyer | e260d0b | 2010-11-03 15:39:55 +0100 | [diff] [blame] | 57 | at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC; |
| 58 | at91_pit_t *pit = (at91_pit_t *) ATMEL_BASE_PIT; |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 59 | |
| 60 | /* Enable PITC Clock */ |
Reinhard Meyer | e260d0b | 2010-11-03 15:39:55 +0100 | [diff] [blame] | 61 | writel(1 << ATMEL_ID_SYS, &pmc->pcer); |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 62 | |
| 63 | /* Enable PITC */ |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 64 | writel(TIMER_LOAD_VAL | AT91_PIT_MR_EN , &pit->mr); |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 65 | |
Simon Glass | 6ed6e03 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 66 | gd->arch.timer_rate_hz = gd->arch.mck_rate_hz / 16; |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 67 | gd->arch.tbu = gd->arch.tbl = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 1d4a379 | 2009-04-16 21:30:48 +0200 | [diff] [blame] | 68 | |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | /* |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 73 | * Get the current 64 bit timer tick count |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 74 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 1d4a379 | 2009-04-16 21:30:48 +0200 | [diff] [blame] | 75 | unsigned long long get_ticks(void) |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 76 | { |
Reinhard Meyer | e260d0b | 2010-11-03 15:39:55 +0100 | [diff] [blame] | 77 | at91_pit_t *pit = (at91_pit_t *) ATMEL_BASE_PIT; |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 78 | |
| 79 | ulong now = readl(&pit->piir); |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 80 | |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 81 | /* increment tbu if tbl has rolled over */ |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 82 | if (now < gd->arch.tbl) |
Simon Glass | 8ca1520 | 2012-12-13 20:48:33 +0000 | [diff] [blame] | 83 | gd->arch.tbu++; |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 84 | gd->arch.tbl = now; |
| 85 | return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl; |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 88 | void __udelay(unsigned long usec) |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 89 | { |
Reinhard Meyer | 7beb915 | 2010-11-10 18:07:56 +0100 | [diff] [blame] | 90 | unsigned long long start; |
Jean-Christophe PLAGNIOL-VILLARD | 1d4a379 | 2009-04-16 21:30:48 +0200 | [diff] [blame] | 91 | ulong tmo; |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 92 | |
Reinhard Meyer | 7beb915 | 2010-11-10 18:07:56 +0100 | [diff] [blame] | 93 | start = get_ticks(); /* get current timestamp */ |
| 94 | tmo = usec_to_tick(usec); /* convert usecs to ticks */ |
| 95 | while ((get_ticks() - start) < tmo) |
| 96 | ; /* loop till time has passed */ |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 99 | /* |
Reinhard Meyer | 7beb915 | 2010-11-10 18:07:56 +0100 | [diff] [blame] | 100 | * get_timer(base) can be used to check for timeouts or |
| 101 | * to measure elasped time relative to an event: |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 102 | * |
Reinhard Meyer | 7beb915 | 2010-11-10 18:07:56 +0100 | [diff] [blame] | 103 | * ulong start_time = get_timer(0) sets start_time to the current |
| 104 | * time value. |
| 105 | * get_timer(start_time) returns the time elapsed since then. |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 106 | * |
| 107 | * The time is used in CONFIG_SYS_HZ units! |
| 108 | */ |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 109 | ulong get_timer(ulong base) |
| 110 | { |
Reinhard Meyer | 7beb915 | 2010-11-10 18:07:56 +0100 | [diff] [blame] | 111 | return tick_to_time(get_ticks()) - base; |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
Reinhard Meyer | 0a1790a | 2010-10-05 16:54:35 +0200 | [diff] [blame] | 115 | * Return the number of timer ticks per second. |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 116 | */ |
| 117 | ulong get_tbclk(void) |
| 118 | { |
Simon Glass | 6ed6e03 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 119 | return gd->arch.timer_rate_hz; |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 120 | } |