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> |
| 28 | #include <asm/arch/imx-regs.h> |
| 29 | |
| 30 | /* General purpose timers registers */ |
| 31 | struct mxc_gpt { |
| 32 | unsigned int control; |
| 33 | unsigned int prescaler; |
| 34 | unsigned int status; |
| 35 | unsigned int nouse[6]; |
| 36 | unsigned int counter; |
| 37 | }; |
| 38 | |
| 39 | static struct mxc_gpt *cur_gpt = (struct mxc_gpt *)GPT1_BASE_ADDR; |
| 40 | |
| 41 | /* General purpose timers bitfields */ |
| 42 | #define GPTCR_SWR (1<<15) /* Software reset */ |
| 43 | #define GPTCR_FRR (1<<9) /* Freerun / restart */ |
| 44 | #define GPTCR_CLKSOURCE_32 (4<<6) /* Clock source */ |
| 45 | #define GPTCR_TEN (1) /* Timer enable */ |
| 46 | |
Stefano Babic | 19c53b4 | 2011-01-21 21:16:15 +0100 | [diff] [blame] | 47 | DECLARE_GLOBAL_DATA_PTR; |
| 48 | |
| 49 | #define timestamp (gd->tbl) |
| 50 | #define lastinc (gd->lastinc) |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 51 | |
| 52 | int timer_init(void) |
| 53 | { |
| 54 | int i; |
| 55 | |
| 56 | /* setup GP Timer 1 */ |
| 57 | __raw_writel(GPTCR_SWR, &cur_gpt->control); |
| 58 | |
| 59 | /* We have no udelay by now */ |
| 60 | for (i = 0; i < 100; i++) |
| 61 | __raw_writel(0, &cur_gpt->control); |
| 62 | |
| 63 | __raw_writel(0, &cur_gpt->prescaler); /* 32Khz */ |
| 64 | |
| 65 | /* Freerun Mode, PERCLK1 input */ |
| 66 | i = __raw_readl(&cur_gpt->control); |
| 67 | __raw_writel(i | GPTCR_CLKSOURCE_32 | GPTCR_TEN, &cur_gpt->control); |
| 68 | reset_timer_masked(); |
| 69 | return 0; |
| 70 | } |
| 71 | |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 72 | void reset_timer_masked(void) |
| 73 | { |
| 74 | ulong val = __raw_readl(&cur_gpt->counter); |
Jason Liu | e7a7ed2 | 2010-10-18 11:09:26 +0800 | [diff] [blame] | 75 | lastinc = val / (CONFIG_SYS_MX5_CLK32 / CONFIG_SYS_HZ); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 76 | timestamp = 0; |
| 77 | } |
| 78 | |
| 79 | ulong get_timer_masked(void) |
| 80 | { |
| 81 | ulong val = __raw_readl(&cur_gpt->counter); |
Jason Liu | e7a7ed2 | 2010-10-18 11:09:26 +0800 | [diff] [blame] | 82 | val /= (CONFIG_SYS_MX5_CLK32 / CONFIG_SYS_HZ); |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 83 | if (val >= lastinc) |
| 84 | timestamp += (val - lastinc); |
| 85 | else |
Jason Liu | e7a7ed2 | 2010-10-18 11:09:26 +0800 | [diff] [blame] | 86 | timestamp += ((0xFFFFFFFF / (CONFIG_SYS_MX5_CLK32 / CONFIG_SYS_HZ)) |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 87 | - lastinc) + val; |
| 88 | lastinc = val; |
Li Haibo | cfdda9e | 2010-08-10 14:18:38 +0800 | [diff] [blame] | 89 | return timestamp; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | ulong get_timer(ulong base) |
| 93 | { |
| 94 | return get_timer_masked() - base; |
| 95 | } |
| 96 | |
Wolfgang Denk | d7cb355 | 2010-05-21 23:13:18 +0200 | [diff] [blame] | 97 | /* delay x useconds AND preserve advance timestamp value */ |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 98 | void __udelay(unsigned long usec) |
| 99 | { |
| 100 | unsigned long now, start, tmo; |
Jason Liu | e7a7ed2 | 2010-10-18 11:09:26 +0800 | [diff] [blame] | 101 | tmo = usec * (CONFIG_SYS_MX5_CLK32 / 1000) / 1000; |
Stefano Babic | a521a77 | 2010-01-20 18:19:32 +0100 | [diff] [blame] | 102 | |
| 103 | if (!tmo) |
| 104 | tmo = 1; |
| 105 | |
| 106 | now = start = readl(&cur_gpt->counter); |
| 107 | |
| 108 | while ((now - start) < tmo) |
| 109 | now = readl(&cur_gpt->counter); |
| 110 | |
| 111 | } |