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> |
| 23 | |
| 24 | #include <asm/div64.h> |
| 25 | #include <asm/errno.h> |
| 26 | #include <asm/io.h> |
| 27 | #include <asm/processor.h> |
| 28 | #include <asm/sysreg.h> |
| 29 | |
| 30 | #include <asm/arch/platform.h> |
| 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 | |
| 47 | static const struct device *intc_dev; |
| 48 | |
| 49 | unsigned long get_tbclk(void) |
| 50 | { |
| 51 | return gd->cpu_hz; |
| 52 | } |
| 53 | |
| 54 | unsigned long long get_ticks(void) |
| 55 | { |
| 56 | unsigned long lo, hi_now, hi_prev; |
| 57 | |
| 58 | do { |
| 59 | hi_prev = timer_overflow; |
| 60 | lo = sysreg_read(COUNT); |
| 61 | hi_now = timer_overflow; |
| 62 | } while (hi_prev != hi_now); |
| 63 | |
| 64 | return ((unsigned long long)hi_now << 32) | lo; |
| 65 | } |
| 66 | |
| 67 | void reset_timer(void) |
| 68 | { |
| 69 | sysreg_write(COUNT, 0); |
| 70 | cpu_sync_pipeline(); /* process any pending interrupts */ |
| 71 | timer_overflow = 0; |
| 72 | } |
| 73 | |
| 74 | unsigned long get_timer(unsigned long base) |
| 75 | { |
| 76 | u64 now = get_ticks(); |
| 77 | |
| 78 | now *= tb_factor; |
| 79 | return (unsigned long)(now >> 32) - base; |
| 80 | } |
| 81 | |
| 82 | void set_timer(unsigned long t) |
| 83 | { |
| 84 | unsigned long long ticks = t; |
| 85 | unsigned long lo, hi, hi_new; |
| 86 | |
| 87 | ticks = (ticks * get_tbclk()) / CFG_HZ; |
| 88 | hi = ticks >> 32; |
| 89 | lo = ticks & 0xffffffffUL; |
| 90 | |
| 91 | do { |
| 92 | timer_overflow = hi; |
| 93 | sysreg_write(COUNT, lo); |
| 94 | hi_new = timer_overflow; |
| 95 | } while (hi_new != hi); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * For short delays only. It will overflow after a few seconds. |
| 100 | */ |
| 101 | void udelay(unsigned long usec) |
| 102 | { |
| 103 | unsigned long now, end; |
| 104 | |
| 105 | now = sysreg_read(COUNT); |
| 106 | |
| 107 | end = ((usec * (get_tbclk() / 10000)) + 50) / 100; |
| 108 | end += now; |
| 109 | |
| 110 | while (now > end) |
| 111 | now = sysreg_read(COUNT); |
| 112 | |
| 113 | while (now < end) |
| 114 | now = sysreg_read(COUNT); |
| 115 | } |
| 116 | |
| 117 | static int set_interrupt_handler(unsigned int nr, void (*handler)(void), |
| 118 | unsigned int priority) |
| 119 | { |
| 120 | unsigned long intpr; |
| 121 | unsigned long handler_addr = (unsigned long)handler; |
| 122 | |
| 123 | if ((handler_addr & HANDLER_MASK) != handler_addr |
| 124 | || (priority & INTLEV_MASK) != priority) |
| 125 | return -EINVAL; |
| 126 | |
| 127 | intpr = (handler_addr & HANDLER_MASK); |
| 128 | intpr |= (priority & INTLEV_MASK) << INTLEV_SHIFT; |
| 129 | writel(intpr, intc_dev->regs + 4 * nr); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | void timer_init(void) |
| 135 | { |
| 136 | extern void timer_interrupt_handler(void); |
| 137 | u64 tmp; |
| 138 | |
| 139 | sysreg_write(COUNT, 0); |
| 140 | |
| 141 | tmp = (u64)CFG_HZ << 32; |
| 142 | tmp += gd->cpu_hz / 2; |
| 143 | do_div(tmp, gd->cpu_hz); |
| 144 | tb_factor = (u32)tmp; |
| 145 | |
| 146 | intc_dev = get_device(DEVICE_INTC); |
| 147 | |
| 148 | if (!intc_dev |
| 149 | || set_interrupt_handler(0, &timer_interrupt_handler, 3)) |
| 150 | return; |
| 151 | |
| 152 | /* For all practical purposes, this gives us an overflow interrupt */ |
| 153 | sysreg_write(COMPARE, 0xffffffff); |
| 154 | } |