blob: de202207ce8ea66f33425c3115ee6cb86c0f8c31 [file] [log] [blame]
Wolfgang Denk64702552006-10-24 14:27:35 +02001/*
2 * Copyright (C) 2004-2006 Atmel Corporation
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Denk64702552006-10-24 14:27:35 +02005 */
6#include <common.h>
Haavard Skinnemoen9ec3b352007-08-13 17:22:31 +02007#include <div64.h>
Wolfgang Denk64702552006-10-24 14:27:35 +02008
Masahiro Yamada56a931c2016-09-21 11:28:55 +09009#include <linux/errno.h>
Wolfgang Denk64702552006-10-24 14:27:35 +020010#include <asm/io.h>
11#include <asm/processor.h>
12#include <asm/sysreg.h>
13
Andreas Bießmann94156fa2010-11-04 23:15:30 +000014#include <asm/arch/hardware.h>
Wolfgang Denk64702552006-10-24 14:27:35 +020015
16#define HANDLER_MASK 0x00ffffff
17#define INTLEV_SHIFT 30
18#define INTLEV_MASK 0x00000003
19
20DECLARE_GLOBAL_DATA_PTR;
21
22/* Incremented whenever COUNT reaches 0xffffffff by timer_interrupt_handler */
23volatile unsigned long timer_overflow;
24
25/*
26 * Instead of dividing by get_tbclk(), multiply by this constant and
27 * right-shift the result by 32 bits.
28 */
29static unsigned long tb_factor;
30
Wolfgang Denk64702552006-10-24 14:27:35 +020031unsigned long get_tbclk(void)
32{
Simon Glass421b7c02012-12-13 20:49:10 +000033 return gd->arch.cpu_hz;
Wolfgang Denk64702552006-10-24 14:27:35 +020034}
35
36unsigned long long get_ticks(void)
37{
38 unsigned long lo, hi_now, hi_prev;
39
40 do {
41 hi_prev = timer_overflow;
42 lo = sysreg_read(COUNT);
43 hi_now = timer_overflow;
44 } while (hi_prev != hi_now);
45
46 return ((unsigned long long)hi_now << 32) | lo;
47}
48
Wolfgang Denk64702552006-10-24 14:27:35 +020049unsigned long get_timer(unsigned long base)
50{
51 u64 now = get_ticks();
52
53 now *= tb_factor;
54 return (unsigned long)(now >> 32) - base;
55}
56
Wolfgang Denk64702552006-10-24 14:27:35 +020057/*
58 * For short delays only. It will overflow after a few seconds.
59 */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010060void __udelay(unsigned long usec)
Wolfgang Denk64702552006-10-24 14:27:35 +020061{
Haavard Skinnemoen0edc1482008-05-26 12:19:10 +020062 unsigned long cycles;
63 unsigned long base;
64 unsigned long now;
Wolfgang Denk64702552006-10-24 14:27:35 +020065
Haavard Skinnemoen0edc1482008-05-26 12:19:10 +020066 base = sysreg_read(COUNT);
67 cycles = ((usec * (get_tbclk() / 10000)) + 50) / 100;
Wolfgang Denk64702552006-10-24 14:27:35 +020068
Haavard Skinnemoen0edc1482008-05-26 12:19:10 +020069 do {
Wolfgang Denk64702552006-10-24 14:27:35 +020070 now = sysreg_read(COUNT);
Haavard Skinnemoen0edc1482008-05-26 12:19:10 +020071 } while ((now - base) < cycles);
Wolfgang Denk64702552006-10-24 14:27:35 +020072}
73
74static int set_interrupt_handler(unsigned int nr, void (*handler)(void),
75 unsigned int priority)
76{
Haavard Skinnemoenabf19bf2006-11-20 15:53:10 +010077 extern void _evba(void);
Wolfgang Denk64702552006-10-24 14:27:35 +020078 unsigned long intpr;
79 unsigned long handler_addr = (unsigned long)handler;
80
Haavard Skinnemoenabf19bf2006-11-20 15:53:10 +010081 handler_addr -= (unsigned long)&_evba;
82
Wolfgang Denk64702552006-10-24 14:27:35 +020083 if ((handler_addr & HANDLER_MASK) != handler_addr
84 || (priority & INTLEV_MASK) != priority)
85 return -EINVAL;
86
87 intpr = (handler_addr & HANDLER_MASK);
88 intpr |= (priority & INTLEV_MASK) << INTLEV_SHIFT;
Andreas Bießmann5807e792010-11-04 23:15:31 +000089 writel(intpr, (void *)ATMEL_BASE_INTC + 4 * nr);
Wolfgang Denk64702552006-10-24 14:27:35 +020090
91 return 0;
92}
93
Sven Schnelle2e4e87b2011-10-04 21:53:32 +020094int timer_init(void)
Wolfgang Denk64702552006-10-24 14:27:35 +020095{
96 extern void timer_interrupt_handler(void);
97 u64 tmp;
98
99 sysreg_write(COUNT, 0);
100
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200101 tmp = (u64)CONFIG_SYS_HZ << 32;
Simon Glass421b7c02012-12-13 20:49:10 +0000102 tmp += gd->arch.cpu_hz / 2;
103 do_div(tmp, gd->arch.cpu_hz);
Wolfgang Denk64702552006-10-24 14:27:35 +0200104 tb_factor = (u32)tmp;
105
Haavard Skinnemoen0a2743f2006-11-19 18:06:53 +0100106 if (set_interrupt_handler(0, &timer_interrupt_handler, 3))
Sven Schnelle2e4e87b2011-10-04 21:53:32 +0200107 return -EINVAL;
Wolfgang Denk64702552006-10-24 14:27:35 +0200108
109 /* For all practical purposes, this gives us an overflow interrupt */
110 sysreg_write(COMPARE, 0xffffffff);
Sven Schnelle2e4e87b2011-10-04 21:53:32 +0200111 return 0;
Wolfgang Denk64702552006-10-24 14:27:35 +0200112}