Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 1 | /* |
| 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 Skinnemoen | 9ec3b35 | 2007-08-13 17:22:31 +0200 | [diff] [blame] | 23 | #include <div64.h> |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 24 | |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 25 | #include <asm/errno.h> |
| 26 | #include <asm/io.h> |
| 27 | #include <asm/processor.h> |
| 28 | #include <asm/sysreg.h> |
| 29 | |
Andreas Bießmann | 94156fa | 2010-11-04 23:15:30 +0000 | [diff] [blame] | 30 | #include <asm/arch/hardware.h> |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 31 | |
| 32 | #define HANDLER_MASK 0x00ffffff |
| 33 | #define INTLEV_SHIFT 30 |
| 34 | #define INTLEV_MASK 0x00000003 |
| 35 | |
| 36 | DECLARE_GLOBAL_DATA_PTR; |
| 37 | |
| 38 | /* Incremented whenever COUNT reaches 0xffffffff by timer_interrupt_handler */ |
| 39 | volatile 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 | */ |
| 45 | static unsigned long tb_factor; |
| 46 | |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 47 | unsigned long get_tbclk(void) |
| 48 | { |
| 49 | return gd->cpu_hz; |
| 50 | } |
| 51 | |
| 52 | unsigned 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 | |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 65 | unsigned long get_timer(unsigned long base) |
| 66 | { |
| 67 | u64 now = get_ticks(); |
| 68 | |
| 69 | now *= tb_factor; |
| 70 | return (unsigned long)(now >> 32) - base; |
| 71 | } |
| 72 | |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 73 | /* |
| 74 | * For short delays only. It will overflow after a few seconds. |
| 75 | */ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 76 | void __udelay(unsigned long usec) |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 77 | { |
Haavard Skinnemoen | 0edc148 | 2008-05-26 12:19:10 +0200 | [diff] [blame] | 78 | unsigned long cycles; |
| 79 | unsigned long base; |
| 80 | unsigned long now; |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 81 | |
Haavard Skinnemoen | 0edc148 | 2008-05-26 12:19:10 +0200 | [diff] [blame] | 82 | base = sysreg_read(COUNT); |
| 83 | cycles = ((usec * (get_tbclk() / 10000)) + 50) / 100; |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 84 | |
Haavard Skinnemoen | 0edc148 | 2008-05-26 12:19:10 +0200 | [diff] [blame] | 85 | do { |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 86 | now = sysreg_read(COUNT); |
Haavard Skinnemoen | 0edc148 | 2008-05-26 12:19:10 +0200 | [diff] [blame] | 87 | } while ((now - base) < cycles); |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static int set_interrupt_handler(unsigned int nr, void (*handler)(void), |
| 91 | unsigned int priority) |
| 92 | { |
Haavard Skinnemoen | abf19bf | 2006-11-20 15:53:10 +0100 | [diff] [blame] | 93 | extern void _evba(void); |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 94 | unsigned long intpr; |
| 95 | unsigned long handler_addr = (unsigned long)handler; |
| 96 | |
Haavard Skinnemoen | abf19bf | 2006-11-20 15:53:10 +0100 | [diff] [blame] | 97 | handler_addr -= (unsigned long)&_evba; |
| 98 | |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 99 | if ((handler_addr & HANDLER_MASK) != handler_addr |
| 100 | || (priority & INTLEV_MASK) != priority) |
| 101 | return -EINVAL; |
| 102 | |
| 103 | intpr = (handler_addr & HANDLER_MASK); |
| 104 | intpr |= (priority & INTLEV_MASK) << INTLEV_SHIFT; |
Andreas Bießmann | 5807e79 | 2010-11-04 23:15:31 +0000 | [diff] [blame] | 105 | writel(intpr, (void *)ATMEL_BASE_INTC + 4 * nr); |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Sven Schnelle | 2e4e87b | 2011-10-04 21:53:32 +0200 | [diff] [blame] | 110 | int timer_init(void) |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 111 | { |
| 112 | extern void timer_interrupt_handler(void); |
| 113 | u64 tmp; |
| 114 | |
| 115 | sysreg_write(COUNT, 0); |
| 116 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 117 | tmp = (u64)CONFIG_SYS_HZ << 32; |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 118 | tmp += gd->cpu_hz / 2; |
| 119 | do_div(tmp, gd->cpu_hz); |
| 120 | tb_factor = (u32)tmp; |
| 121 | |
Haavard Skinnemoen | 0a2743f | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 122 | if (set_interrupt_handler(0, &timer_interrupt_handler, 3)) |
Sven Schnelle | 2e4e87b | 2011-10-04 21:53:32 +0200 | [diff] [blame] | 123 | return -EINVAL; |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 124 | |
| 125 | /* For all practical purposes, this gives us an overflow interrupt */ |
| 126 | sysreg_write(COMPARE, 0xffffffff); |
Sven Schnelle | 2e4e87b | 2011-10-04 21:53:32 +0200 | [diff] [blame] | 127 | return 0; |
Wolfgang Denk | 6470255 | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 128 | } |