blob: 160838eeeb3b1d3d12e36fb67f911b3ae1b95547 [file] [log] [blame]
Wolfgang Denk64702552006-10-24 14:27:35 +02001/*
2 * Copyright (C) 2004-2006 Atmel Corporation
3 *
4 * See file CREDITS for list of people who contributed to this
5 * project.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22#include <common.h>
Haavard Skinnemoen9ec3b352007-08-13 17:22:31 +020023#include <div64.h>
Wolfgang Denk64702552006-10-24 14:27:35 +020024
Wolfgang Denk64702552006-10-24 14:27:35 +020025#include <asm/errno.h>
26#include <asm/io.h>
27#include <asm/processor.h>
28#include <asm/sysreg.h>
29
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +010030#include <asm/arch/memory-map.h>
Wolfgang Denk64702552006-10-24 14:27:35 +020031
32#define HANDLER_MASK 0x00ffffff
33#define INTLEV_SHIFT 30
34#define INTLEV_MASK 0x00000003
35
36DECLARE_GLOBAL_DATA_PTR;
37
38/* Incremented whenever COUNT reaches 0xffffffff by timer_interrupt_handler */
39volatile unsigned long timer_overflow;
40
41/*
42 * Instead of dividing by get_tbclk(), multiply by this constant and
43 * right-shift the result by 32 bits.
44 */
45static unsigned long tb_factor;
46
Wolfgang Denk64702552006-10-24 14:27:35 +020047unsigned long get_tbclk(void)
48{
49 return gd->cpu_hz;
50}
51
52unsigned long long get_ticks(void)
53{
54 unsigned long lo, hi_now, hi_prev;
55
56 do {
57 hi_prev = timer_overflow;
58 lo = sysreg_read(COUNT);
59 hi_now = timer_overflow;
60 } while (hi_prev != hi_now);
61
62 return ((unsigned long long)hi_now << 32) | lo;
63}
64
65void reset_timer(void)
66{
67 sysreg_write(COUNT, 0);
68 cpu_sync_pipeline(); /* process any pending interrupts */
69 timer_overflow = 0;
70}
71
72unsigned long get_timer(unsigned long base)
73{
74 u64 now = get_ticks();
75
76 now *= tb_factor;
77 return (unsigned long)(now >> 32) - base;
78}
79
80void set_timer(unsigned long t)
81{
82 unsigned long long ticks = t;
83 unsigned long lo, hi, hi_new;
84
85 ticks = (ticks * get_tbclk()) / CFG_HZ;
86 hi = ticks >> 32;
87 lo = ticks & 0xffffffffUL;
88
89 do {
90 timer_overflow = hi;
91 sysreg_write(COUNT, lo);
92 hi_new = timer_overflow;
93 } while (hi_new != hi);
94}
95
96/*
97 * For short delays only. It will overflow after a few seconds.
98 */
99void udelay(unsigned long usec)
100{
Haavard Skinnemoen0edc1482008-05-26 12:19:10 +0200101 unsigned long cycles;
102 unsigned long base;
103 unsigned long now;
Wolfgang Denk64702552006-10-24 14:27:35 +0200104
Haavard Skinnemoen0edc1482008-05-26 12:19:10 +0200105 base = sysreg_read(COUNT);
106 cycles = ((usec * (get_tbclk() / 10000)) + 50) / 100;
Wolfgang Denk64702552006-10-24 14:27:35 +0200107
Haavard Skinnemoen0edc1482008-05-26 12:19:10 +0200108 do {
Wolfgang Denk64702552006-10-24 14:27:35 +0200109 now = sysreg_read(COUNT);
Haavard Skinnemoen0edc1482008-05-26 12:19:10 +0200110 } while ((now - base) < cycles);
Wolfgang Denk64702552006-10-24 14:27:35 +0200111}
112
113static int set_interrupt_handler(unsigned int nr, void (*handler)(void),
114 unsigned int priority)
115{
Haavard Skinnemoenabf19bf2006-11-20 15:53:10 +0100116 extern void _evba(void);
Wolfgang Denk64702552006-10-24 14:27:35 +0200117 unsigned long intpr;
118 unsigned long handler_addr = (unsigned long)handler;
119
Haavard Skinnemoenabf19bf2006-11-20 15:53:10 +0100120 handler_addr -= (unsigned long)&_evba;
121
Wolfgang Denk64702552006-10-24 14:27:35 +0200122 if ((handler_addr & HANDLER_MASK) != handler_addr
123 || (priority & INTLEV_MASK) != priority)
124 return -EINVAL;
125
126 intpr = (handler_addr & HANDLER_MASK);
127 intpr |= (priority & INTLEV_MASK) << INTLEV_SHIFT;
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +0100128 writel(intpr, (void *)INTC_BASE + 4 * nr);
Wolfgang Denk64702552006-10-24 14:27:35 +0200129
130 return 0;
131}
132
133void timer_init(void)
134{
135 extern void timer_interrupt_handler(void);
136 u64 tmp;
137
138 sysreg_write(COUNT, 0);
139
140 tmp = (u64)CFG_HZ << 32;
141 tmp += gd->cpu_hz / 2;
142 do_div(tmp, gd->cpu_hz);
143 tb_factor = (u32)tmp;
144
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +0100145 if (set_interrupt_handler(0, &timer_interrupt_handler, 3))
Wolfgang Denk64702552006-10-24 14:27:35 +0200146 return;
147
148 /* For all practical purposes, this gives us an overflow interrupt */
149 sysreg_write(COMPARE, 0xffffffff);
150}