Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2006 Atmel Corporation |
| 3 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 5 | */ |
| 6 | #include <common.h> |
Haavard Skinnemoen | 9ec3b35 | 2007-08-13 17:22:31 +0200 | [diff] [blame] | 7 | #include <div64.h> |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 8 | |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 9 | #include <linux/errno.h> |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | #include <asm/processor.h> |
| 12 | #include <asm/sysreg.h> |
| 13 | |
Andreas Bießmann | 94156fa | 2010-11-04 23:15:30 +0000 | [diff] [blame] | 14 | #include <asm/arch/hardware.h> |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 15 | |
| 16 | #define HANDLER_MASK 0x00ffffff |
| 17 | #define INTLEV_SHIFT 30 |
| 18 | #define INTLEV_MASK 0x00000003 |
| 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | /* Incremented whenever COUNT reaches 0xffffffff by timer_interrupt_handler */ |
| 23 | volatile 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 | */ |
| 29 | static unsigned long tb_factor; |
| 30 | |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 31 | unsigned long get_tbclk(void) |
| 32 | { |
Simon Glass | 421b7c0 | 2012-12-13 20:49:10 +0000 | [diff] [blame] | 33 | return gd->arch.cpu_hz; |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | unsigned 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 Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 49 | unsigned 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 Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 57 | /* |
| 58 | * For short delays only. It will overflow after a few seconds. |
| 59 | */ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 60 | void __udelay(unsigned long usec) |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 61 | { |
Haavard Skinnemoen | 0edc148 | 2008-05-26 12:19:10 +0200 | [diff] [blame] | 62 | unsigned long cycles; |
| 63 | unsigned long base; |
| 64 | unsigned long now; |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 65 | |
Haavard Skinnemoen | 0edc148 | 2008-05-26 12:19:10 +0200 | [diff] [blame] | 66 | base = sysreg_read(COUNT); |
| 67 | cycles = ((usec * (get_tbclk() / 10000)) + 50) / 100; |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 68 | |
Haavard Skinnemoen | 0edc148 | 2008-05-26 12:19:10 +0200 | [diff] [blame] | 69 | do { |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 70 | now = sysreg_read(COUNT); |
Haavard Skinnemoen | 0edc148 | 2008-05-26 12:19:10 +0200 | [diff] [blame] | 71 | } while ((now - base) < cycles); |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static int set_interrupt_handler(unsigned int nr, void (*handler)(void), |
| 75 | unsigned int priority) |
| 76 | { |
Haavard Skinnemoen | abf19bf | 2006-11-20 15:53:10 +0100 | [diff] [blame] | 77 | extern void _evba(void); |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 78 | unsigned long intpr; |
| 79 | unsigned long handler_addr = (unsigned long)handler; |
| 80 | |
Haavard Skinnemoen | abf19bf | 2006-11-20 15:53:10 +0100 | [diff] [blame] | 81 | handler_addr -= (unsigned long)&_evba; |
| 82 | |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 83 | 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ßmann | 5807e79 | 2010-11-04 23:15:31 +0000 | [diff] [blame] | 89 | writel(intpr, (void *)ATMEL_BASE_INTC + 4 * nr); |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Sven Schnelle | 2e4e87b | 2011-10-04 21:53:32 +0200 | [diff] [blame] | 94 | int timer_init(void) |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 95 | { |
| 96 | extern void timer_interrupt_handler(void); |
| 97 | u64 tmp; |
| 98 | |
| 99 | sysreg_write(COUNT, 0); |
| 100 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 101 | tmp = (u64)CONFIG_SYS_HZ << 32; |
Simon Glass | 421b7c0 | 2012-12-13 20:49:10 +0000 | [diff] [blame] | 102 | tmp += gd->arch.cpu_hz / 2; |
| 103 | do_div(tmp, gd->arch.cpu_hz); |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 104 | tb_factor = (u32)tmp; |
| 105 | |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 106 | if (set_interrupt_handler(0, &timer_interrupt_handler, 3)) |
Sven Schnelle | 2e4e87b | 2011-10-04 21:53:32 +0200 | [diff] [blame] | 107 | return -EINVAL; |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 108 | |
| 109 | /* For all practical purposes, this gives us an overflow interrupt */ |
| 110 | sysreg_write(COMPARE, 0xffffffff); |
Sven Schnelle | 2e4e87b | 2011-10-04 21:53:32 +0200 | [diff] [blame] | 111 | return 0; |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 112 | } |