3 * Sascha Hauer, Pengutronix
5 * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
7 * See file CREDITS for list of people who contributed to this
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.
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.
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,
28 #include <asm/arch/imx-regs.h>
30 /* General purpose timers bitfields */
31 #define GPTCR_SWR (1<<15) /* Software reset */
32 #define GPTCR_FRR (1<<9) /* Freerun / restart */
33 #define GPTCR_CLKSOURCE_32 (0x100<<6) /* Clock source */
34 #define GPTCR_CLKSOURCE_IPG (0x001<<6) /* Clock source */
35 #define GPTCR_TEN (1) /* Timer enable */
36 #define GPTPR_VAL (66)
41 struct gpt_regs *gpt = (struct gpt_regs *)GPT1_BASE_ADDR;
43 /* setup GP Timer 1 */
44 writel(GPTCR_SWR, &gpt->ctrl);
45 for (i = 0; i < 100; i++)
46 writel(0, &gpt->ctrl); /* We have no udelay by now */
48 writel(GPTPR_VAL, &gpt->pre);
49 /* Freerun Mode, PERCLK1 input */
50 writel(readl(&gpt->ctrl) |
51 GPTCR_CLKSOURCE_IPG | GPTCR_TEN,
57 void reset_timer_masked(void)
59 struct gpt_regs *gpt = (struct gpt_regs *)GPT1_BASE_ADDR;
61 writel(0, &gpt->ctrl);
62 /* Freerun Mode, PERCLK1 input */
63 writel(GPTCR_CLKSOURCE_IPG | GPTCR_TEN,
67 inline ulong get_timer_masked(void)
70 struct gpt_regs *gpt = (struct gpt_regs *)GPT1_BASE_ADDR;
71 ulong val = readl(&gpt->counter);
76 ulong get_timer(ulong base)
80 tmp = get_timer_masked();
82 if (tmp <= (base * 1000)) {
84 tmp += (0xffffffff - base);
87 return (tmp / 1000) - base;
91 * delay x useconds AND preserve advance timstamp value
92 * GPTCNT is now supposed to tick 1 by 1 us.
94 void __udelay(unsigned long usec)
98 tmp = get_timer_masked(); /* get current timestamp */
100 /* if setting this forward will roll time stamp */
101 if ((usec + tmp + 1) < tmp) {
102 /* reset "advancing" timestamp to 0, set lastinc value */
103 reset_timer_masked();
105 /* else, set advancing stamp wake up time */
109 while (get_timer_masked() < tmp) /* loop till event */