2 * Copyright (C) 2004-2006 Atmel Corporation
4 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/processor.h>
12 #include <asm/sysreg.h>
14 #include <asm/arch/hardware.h>
16 #define HANDLER_MASK 0x00ffffff
17 #define INTLEV_SHIFT 30
18 #define INTLEV_MASK 0x00000003
20 DECLARE_GLOBAL_DATA_PTR;
22 /* Incremented whenever COUNT reaches 0xffffffff by timer_interrupt_handler */
23 volatile unsigned long timer_overflow;
26 * Instead of dividing by get_tbclk(), multiply by this constant and
27 * right-shift the result by 32 bits.
29 static unsigned long tb_factor;
31 unsigned long get_tbclk(void)
33 return gd->arch.cpu_hz;
36 unsigned long long get_ticks(void)
38 unsigned long lo, hi_now, hi_prev;
41 hi_prev = timer_overflow;
42 lo = sysreg_read(COUNT);
43 hi_now = timer_overflow;
44 } while (hi_prev != hi_now);
46 return ((unsigned long long)hi_now << 32) | lo;
49 unsigned long get_timer(unsigned long base)
51 u64 now = get_ticks();
54 return (unsigned long)(now >> 32) - base;
58 * For short delays only. It will overflow after a few seconds.
60 void __udelay(unsigned long usec)
66 base = sysreg_read(COUNT);
67 cycles = ((usec * (get_tbclk() / 10000)) + 50) / 100;
70 now = sysreg_read(COUNT);
71 } while ((now - base) < cycles);
74 static int set_interrupt_handler(unsigned int nr, void (*handler)(void),
75 unsigned int priority)
77 extern void _evba(void);
79 unsigned long handler_addr = (unsigned long)handler;
81 handler_addr -= (unsigned long)&_evba;
83 if ((handler_addr & HANDLER_MASK) != handler_addr
84 || (priority & INTLEV_MASK) != priority)
87 intpr = (handler_addr & HANDLER_MASK);
88 intpr |= (priority & INTLEV_MASK) << INTLEV_SHIFT;
89 writel(intpr, (void *)ATMEL_BASE_INTC + 4 * nr);
96 extern void timer_interrupt_handler(void);
99 sysreg_write(COUNT, 0);
101 tmp = (u64)CONFIG_SYS_HZ << 32;
102 tmp += gd->arch.cpu_hz / 2;
103 do_div(tmp, gd->arch.cpu_hz);
104 tb_factor = (u32)tmp;
106 if (set_interrupt_handler(0, &timer_interrupt_handler, 3))
109 /* For all practical purposes, this gives us an overflow interrupt */
110 sysreg_write(COMPARE, 0xffffffff);