Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007 |
| 3 | * Sascha Hauer, Pengutronix |
| 4 | * |
| 5 | * (C) Copyright 2009 Freescale Semiconductor, Inc. |
| 6 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | #include <common.h> |
| 27 | #include <asm/io.h> |
Stefano Babic | c37b7f7 | 2012-02-06 12:52:36 +0100 | [diff] [blame] | 28 | #include <div64.h> |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 29 | #include <asm/arch/imx-regs.h> |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 30 | #include <asm/arch/clock.h> |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 31 | |
| 32 | /* General purpose timers registers */ |
| 33 | struct mxc_gpt { |
| 34 | unsigned int control; |
| 35 | unsigned int prescaler; |
| 36 | unsigned int status; |
| 37 | unsigned int nouse[6]; |
| 38 | unsigned int counter; |
| 39 | }; |
| 40 | |
| 41 | static struct mxc_gpt *cur_gpt = (struct mxc_gpt *)GPT1_BASE_ADDR; |
| 42 | |
| 43 | /* General purpose timers bitfields */ |
Jason Liu | 83aa8fe | 2011-11-25 00:18:01 +0000 | [diff] [blame] | 44 | #define GPTCR_SWR (1 << 15) /* Software reset */ |
| 45 | #define GPTCR_FRR (1 << 9) /* Freerun / restart */ |
| 46 | #define GPTCR_CLKSOURCE_32 (4 << 6) /* Clock source */ |
| 47 | #define GPTCR_TEN 1 /* Timer enable */ |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 48 | |
Stefano Babic | 19c53b4 | 2011-01-21 21:16:15 +0100 | [diff] [blame] | 49 | DECLARE_GLOBAL_DATA_PTR; |
| 50 | |
Stefano Babic | c37b7f7 | 2012-02-06 12:52:36 +0100 | [diff] [blame] | 51 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 52 | { |
| 53 | tick *= CONFIG_SYS_HZ; |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 54 | do_div(tick, MXC_CLK32); |
Stefano Babic | c37b7f7 | 2012-02-06 12:52:36 +0100 | [diff] [blame] | 55 | |
| 56 | return tick; |
| 57 | } |
| 58 | |
| 59 | static inline unsigned long long us_to_tick(unsigned long long usec) |
| 60 | { |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 61 | usec = usec * MXC_CLK32 + 999999; |
Stefano Babic | c37b7f7 | 2012-02-06 12:52:36 +0100 | [diff] [blame] | 62 | do_div(usec, 1000000); |
| 63 | |
| 64 | return usec; |
| 65 | } |
| 66 | |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 67 | int timer_init(void) |
| 68 | { |
| 69 | int i; |
| 70 | |
| 71 | /* setup GP Timer 1 */ |
| 72 | __raw_writel(GPTCR_SWR, &cur_gpt->control); |
| 73 | |
| 74 | /* We have no udelay by now */ |
| 75 | for (i = 0; i < 100; i++) |
| 76 | __raw_writel(0, &cur_gpt->control); |
| 77 | |
| 78 | __raw_writel(0, &cur_gpt->prescaler); /* 32Khz */ |
| 79 | |
| 80 | /* Freerun Mode, PERCLK1 input */ |
| 81 | i = __raw_readl(&cur_gpt->control); |
| 82 | __raw_writel(i | GPTCR_CLKSOURCE_32 | GPTCR_TEN, &cur_gpt->control); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 83 | |
Knut Wohlrab | d2481f2 | 2013-03-04 04:16:02 +0000 | [diff] [blame] | 84 | gd->arch.tbl = __raw_readl(&cur_gpt->counter); |
| 85 | gd->arch.tbu = 0; |
Graeme Russ | 944a7fe | 2011-07-15 02:21:14 +0000 | [diff] [blame] | 86 | |
| 87 | return 0; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Stefano Babic | c37b7f7 | 2012-02-06 12:52:36 +0100 | [diff] [blame] | 90 | unsigned long long get_ticks(void) |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 91 | { |
Stefano Babic | c37b7f7 | 2012-02-06 12:52:36 +0100 | [diff] [blame] | 92 | ulong now = __raw_readl(&cur_gpt->counter); /* current tick value */ |
| 93 | |
Knut Wohlrab | d2481f2 | 2013-03-04 04:16:02 +0000 | [diff] [blame] | 94 | /* increment tbu if tbl has rolled over */ |
| 95 | if (now < gd->arch.tbl) |
| 96 | gd->arch.tbu++; |
| 97 | gd->arch.tbl = now; |
| 98 | return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Stefano Babic | c37b7f7 | 2012-02-06 12:52:36 +0100 | [diff] [blame] | 101 | ulong get_timer_masked(void) |
| 102 | { |
| 103 | /* |
| 104 | * get_ticks() returns a long long (64 bit), it wraps in |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 105 | * 2^64 / MXC_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~ |
Stefano Babic | c37b7f7 | 2012-02-06 12:52:36 +0100 | [diff] [blame] | 106 | * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in |
| 107 | * 5 * 10^6 days - long enough. |
| 108 | */ |
| 109 | return tick_to_time(get_ticks()); |
| 110 | } |
| 111 | |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 112 | ulong get_timer(ulong base) |
| 113 | { |
| 114 | return get_timer_masked() - base; |
| 115 | } |
| 116 | |
Stefano Babic | c37b7f7 | 2012-02-06 12:52:36 +0100 | [diff] [blame] | 117 | /* delay x useconds AND preserve advance timstamp value */ |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 118 | void __udelay(unsigned long usec) |
| 119 | { |
Stefano Babic | c37b7f7 | 2012-02-06 12:52:36 +0100 | [diff] [blame] | 120 | unsigned long long tmp; |
| 121 | ulong tmo; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 122 | |
Stefano Babic | c37b7f7 | 2012-02-06 12:52:36 +0100 | [diff] [blame] | 123 | tmo = us_to_tick(usec); |
| 124 | tmp = get_ticks() + tmo; /* get current timestamp */ |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 125 | |
Stefano Babic | c37b7f7 | 2012-02-06 12:52:36 +0100 | [diff] [blame] | 126 | while (get_ticks() < tmp) /* loop till event */ |
| 127 | /*NOP*/; |
| 128 | } |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 129 | |
Stefano Babic | c37b7f7 | 2012-02-06 12:52:36 +0100 | [diff] [blame] | 130 | /* |
| 131 | * This function is derived from PowerPC code (timebase clock frequency). |
| 132 | * On ARM it returns the number of timer ticks per second. |
| 133 | */ |
| 134 | ulong get_tbclk(void) |
| 135 | { |
Benoît Thébaudeau | afac165 | 2012-09-27 10:19:58 +0000 | [diff] [blame] | 136 | return MXC_CLK32; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 137 | } |