Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Cirrus Logic EP93xx timer support. |
| 3 | * |
| 4 | * Copyright (C) 2009, 2010 |
| 5 | * Matthias Kaehlcke <matthias@kaehlcke.net> |
| 6 | * |
| 7 | * Copyright (C) 2004, 2005 |
| 8 | * Cory T. Tusar, Videon Central, Inc., <ctusar@videon-central.com> |
| 9 | * |
| 10 | * Based on the original intr.c Cirrus Logic EP93xx Rev D. interrupt support, |
| 11 | * author unknown. |
| 12 | * |
| 13 | * See file CREDITS for list of people who contributed to this project. |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, but |
| 21 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 22 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 23 | * for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License along |
| 26 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 27 | * 675 Mass Ave, Cambridge, MA 02139, USA. |
| 28 | */ |
| 29 | |
| 30 | #include <common.h> |
| 31 | #include <linux/types.h> |
| 32 | #include <asm/arch/ep93xx.h> |
| 33 | #include <asm/io.h> |
Matthias Kaehlcke | d9f505e | 2010-02-24 00:22:00 +0100 | [diff] [blame] | 34 | #include <div64.h> |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 35 | |
| 36 | #define TIMER_CLKSEL (1 << 3) |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 37 | #define TIMER_ENABLE (1 << 7) |
| 38 | |
Matthias Kaehlcke | 7e67fb5 | 2010-03-09 22:13:20 +0100 | [diff] [blame^] | 39 | #define TIMER_FREQ 508469 /* ticks / second */ |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 40 | #define TIMER_MAX_VAL 0xFFFFFFFF |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 41 | |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 42 | static struct ep93xx_timer |
| 43 | { |
| 44 | unsigned long long ticks; |
| 45 | unsigned long last_update; |
| 46 | } timer; |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 47 | |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 48 | static inline unsigned long clk_to_systicks(unsigned long long clk_ticks) |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 49 | { |
Matthias Kaehlcke | d9f505e | 2010-02-24 00:22:00 +0100 | [diff] [blame] | 50 | unsigned long long sys_ticks = (clk_ticks * CONFIG_SYS_HZ); |
| 51 | do_div(sys_ticks, TIMER_FREQ); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 52 | |
Matthias Kaehlcke | d9f505e | 2010-02-24 00:22:00 +0100 | [diff] [blame] | 53 | return (unsigned long)sys_ticks; |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Matthias Kaehlcke | 7e67fb5 | 2010-03-09 22:13:20 +0100 | [diff] [blame^] | 56 | static inline unsigned long long usecs_to_ticks(unsigned long usecs) |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 57 | { |
Matthias Kaehlcke | 7e67fb5 | 2010-03-09 22:13:20 +0100 | [diff] [blame^] | 58 | unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ; |
| 59 | do_div(ticks, 1000 * 1000); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 60 | |
| 61 | return ticks; |
| 62 | } |
| 63 | |
| 64 | static inline unsigned long read_timer(void) |
| 65 | { |
| 66 | struct timer_regs *timer = (struct timer_regs *)TIMER_BASE; |
| 67 | |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 68 | return TIMER_MAX_VAL - readl(&timer->timer3.value); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
| 72 | * timer without interrupts |
| 73 | */ |
| 74 | unsigned long long get_ticks(void) |
| 75 | { |
| 76 | const unsigned long now = read_timer(); |
| 77 | |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 78 | if (now >= timer.last_update) |
| 79 | timer.ticks += now - timer.last_update; |
| 80 | else |
| 81 | /* an overflow occurred */ |
| 82 | timer.ticks += TIMER_MAX_VAL - timer.last_update + now; |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 83 | |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 84 | timer.last_update = now; |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 85 | |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 86 | return timer.ticks; |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | unsigned long get_timer_masked(void) |
| 90 | { |
| 91 | return clk_to_systicks(get_ticks()); |
| 92 | } |
| 93 | |
| 94 | unsigned long get_timer(unsigned long base) |
| 95 | { |
| 96 | return get_timer_masked() - base; |
| 97 | } |
| 98 | |
| 99 | void reset_timer_masked(void) |
| 100 | { |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 101 | timer.last_update = read_timer(); |
| 102 | timer.ticks = 0; |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void reset_timer(void) |
| 106 | { |
| 107 | reset_timer_masked(); |
| 108 | } |
| 109 | |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 110 | void __udelay(unsigned long usec) |
| 111 | { |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 112 | const unsigned long target = get_ticks() + usecs_to_ticks(usec); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 113 | |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 114 | while (get_ticks() < target) |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 115 | /* noop */; |
| 116 | } |
| 117 | |
| 118 | int timer_init(void) |
| 119 | { |
| 120 | struct timer_regs *timer = (struct timer_regs *)TIMER_BASE; |
| 121 | |
| 122 | /* use timer 3 with 508KHz and free running */ |
| 123 | writel(TIMER_CLKSEL, &timer->timer3.control); |
| 124 | |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 125 | /* set initial timer value 3 */ |
| 126 | writel(TIMER_MAX_VAL, &timer->timer3.load); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 127 | |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 128 | /* Enable the timer */ |
| 129 | writel(TIMER_ENABLE | TIMER_CLKSEL, |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 130 | &timer->timer3.control); |
| 131 | |
| 132 | reset_timer_masked(); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * This function is derived from PowerPC code (timebase clock frequency). |
| 139 | * On ARM it returns the number of timer ticks per second. |
| 140 | */ |
| 141 | unsigned long get_tbclk(void) |
| 142 | { |
| 143 | return CONFIG_SYS_HZ; |
| 144 | } |