]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - cpu/arm_cortexa8/mx50/timer.c
applied patches from Freescale and Ka-Ro
[karo-tx-uboot.git] / cpu / arm_cortexa8 / mx50 / timer.c
1 /*
2  * (C) Copyright 2007
3  * Sascha Hauer, Pengutronix
4  *
5  * Copyright (C) 2010 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/arch/mx50.h>
28
29 /* General purpose timers registers */
30 #define GPTCR   __REG(GPT1_BASE_ADDR)   /* Control register */
31 #define GPTPR   __REG(GPT1_BASE_ADDR + 0x4)     /* Prescaler register */
32 #define GPTSR   __REG(GPT1_BASE_ADDR + 0x8)     /* Status register */
33 #define GPTCNT  __REG(GPT1_BASE_ADDR + 0x24)    /* Counter register */
34
35 /* General purpose timers bitfields */
36 #define GPTCR_SWR       (1<<15) /* Software reset */
37 #define GPTCR_FRR       (1<<9)  /* Freerun / restart */
38 #define GPTCR_CLKSOURCE_32   (0x100<<6) /* Clock source */
39 #define GPTCR_CLKSOURCE_IPG (0x001<<6)  /* Clock source */
40 #define GPTCR_TEN       (1)     /* Timer enable */
41 #define GPTPR_VAL       (50)
42
43 static inline void setup_gpt(void)
44 {
45         int i;
46         static int init_done;
47
48         if (init_done)
49                 return;
50
51         init_done = 1;
52
53         /* setup GP Timer 1 */
54         GPTCR = GPTCR_SWR;
55         for (i = 0; i < 100; i++)
56                 GPTCR = 0;              /* We have no udelay by now */
57         GPTPR = GPTPR_VAL;      /* 50Mhz / 50 */
58         /* Freerun Mode, PERCLK1 input */
59         GPTCR |= GPTCR_CLKSOURCE_IPG | GPTCR_TEN;
60 }
61
62 int timer_init(void)
63 {
64         setup_gpt();
65
66         return 0;
67 }
68
69 void reset_timer_masked(void)
70 {
71         GPTCR = 0;
72         /* Freerun Mode, PERCLK1 input */
73         GPTCR = GPTCR_CLKSOURCE_IPG | GPTCR_TEN;
74 }
75
76 inline ulong get_timer_masked(void)
77 {
78         ulong val = GPTCNT;
79
80         return val;
81 }
82
83 void reset_timer(void)
84 {
85         reset_timer_masked();
86 }
87
88 ulong get_timer(ulong base)
89 {
90         ulong tmp;
91
92         tmp = get_timer_masked();
93
94         if (tmp <= (base * 1000)) {
95                 /* Overflow */
96                 tmp += (0xffffffff - base);
97         }
98
99         return (tmp / 1000) - base;
100 }
101
102 void set_timer(ulong t)
103 {
104 }
105
106 /* delay x useconds AND perserve advance timstamp value */
107 /* GPTCNT is now supposed to tick 1 by 1 us. */
108 void udelay(unsigned long usec)
109 {
110         ulong tmp;
111
112         setup_gpt();
113
114         tmp = get_timer_masked();       /* get current timestamp */
115
116         /* if setting this forward will roll time stamp */
117         if ((usec + tmp + 1) < tmp) {
118                 /* reset "advancing" timestamp to 0, set lastinc value */
119                 reset_timer_masked();
120         } else {
121                 /* else, set advancing stamp wake up time */
122                 tmp += usec;
123         }
124
125         while (get_timer_masked() < tmp)        /* loop till event */
126                  /*NOP*/;
127 }