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