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 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 12 | * SPDX-License-Identifier: GPL-2.0+ |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <common.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <asm/arch/ep93xx.h> |
| 18 | #include <asm/io.h> |
Matthias Kaehlcke | d9f505e | 2010-02-24 00:22:00 +0100 | [diff] [blame] | 19 | #include <div64.h> |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 20 | |
| 21 | #define TIMER_CLKSEL (1 << 3) |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 22 | #define TIMER_ENABLE (1 << 7) |
| 23 | |
Matthias Kaehlcke | 7e67fb5 | 2010-03-09 22:13:20 +0100 | [diff] [blame] | 24 | #define TIMER_FREQ 508469 /* ticks / second */ |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 25 | #define TIMER_MAX_VAL 0xFFFFFFFF |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 26 | |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 27 | static struct ep93xx_timer |
| 28 | { |
| 29 | unsigned long long ticks; |
Matthias Kaehlcke | b032698 | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 30 | unsigned long last_read; |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 31 | } timer; |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 32 | |
Matthias Kaehlcke | 7e67fb5 | 2010-03-09 22:13:20 +0100 | [diff] [blame] | 33 | static inline unsigned long long usecs_to_ticks(unsigned long usecs) |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 34 | { |
Matthias Kaehlcke | 7e67fb5 | 2010-03-09 22:13:20 +0100 | [diff] [blame] | 35 | unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ; |
| 36 | do_div(ticks, 1000 * 1000); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 37 | |
| 38 | return ticks; |
| 39 | } |
| 40 | |
Matthias Kaehlcke | b032698 | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 41 | static inline void read_timer(void) |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 42 | { |
Matthias Kaehlcke | 33eef04 | 2010-03-09 22:13:47 +0100 | [diff] [blame] | 43 | struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE; |
Matthias Kaehlcke | b032698 | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 44 | const unsigned long now = TIMER_MAX_VAL - readl(&timer_regs->timer3.value); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 45 | |
Matthias Kaehlcke | b032698 | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 46 | if (now >= timer.last_read) |
| 47 | timer.ticks += now - timer.last_read; |
| 48 | else |
| 49 | /* an overflow occurred */ |
| 50 | timer.ticks += TIMER_MAX_VAL - timer.last_read + now; |
| 51 | |
| 52 | timer.last_read = now; |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
Matthias Kaehlcke | d650da2 | 2010-03-09 22:13:33 +0100 | [diff] [blame] | 56 | * Get the number of ticks (in CONFIG_SYS_HZ resolution) |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 57 | */ |
| 58 | unsigned long long get_ticks(void) |
| 59 | { |
Matthias Kaehlcke | b032698 | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 60 | unsigned long long sys_ticks; |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 61 | |
Matthias Kaehlcke | b032698 | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 62 | read_timer(); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 63 | |
Matthias Kaehlcke | b032698 | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 64 | sys_ticks = timer.ticks * CONFIG_SYS_HZ; |
| 65 | do_div(sys_ticks, TIMER_FREQ); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 66 | |
Matthias Kaehlcke | b032698 | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 67 | return sys_ticks; |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | unsigned long get_timer_masked(void) |
| 71 | { |
Matthias Kaehlcke | d650da2 | 2010-03-09 22:13:33 +0100 | [diff] [blame] | 72 | return get_ticks(); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | unsigned long get_timer(unsigned long base) |
| 76 | { |
| 77 | return get_timer_masked() - base; |
| 78 | } |
| 79 | |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 80 | void __udelay(unsigned long usec) |
| 81 | { |
Matthias Kaehlcke | b032698 | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 82 | unsigned long long target; |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 83 | |
Matthias Kaehlcke | b032698 | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 84 | read_timer(); |
| 85 | |
| 86 | target = timer.ticks + usecs_to_ticks(usec); |
Matthias Kaehlcke | d650da2 | 2010-03-09 22:13:33 +0100 | [diff] [blame] | 87 | |
| 88 | while (timer.ticks < target) |
Matthias Kaehlcke | b032698 | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 89 | read_timer(); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | int timer_init(void) |
| 93 | { |
Matthias Kaehlcke | 33eef04 | 2010-03-09 22:13:47 +0100 | [diff] [blame] | 94 | struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE; |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 95 | |
Matthias Kaehlcke | b032698 | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 96 | /* use timer 3 with 508KHz and free running, not enabled now */ |
Matthias Kaehlcke | 33eef04 | 2010-03-09 22:13:47 +0100 | [diff] [blame] | 97 | writel(TIMER_CLKSEL, &timer_regs->timer3.control); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 98 | |
Matthias Kaehlcke | b032698 | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 99 | /* set initial timer value */ |
Matthias Kaehlcke | 33eef04 | 2010-03-09 22:13:47 +0100 | [diff] [blame] | 100 | writel(TIMER_MAX_VAL, &timer_regs->timer3.load); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 101 | |
Matthias Kaehlcke | c7ad13a | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 102 | /* Enable the timer */ |
| 103 | writel(TIMER_ENABLE | TIMER_CLKSEL, |
Matthias Kaehlcke | 33eef04 | 2010-03-09 22:13:47 +0100 | [diff] [blame] | 104 | &timer_regs->timer3.control); |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 105 | |
Graeme Russ | 17659d7 | 2011-07-15 02:21:14 +0000 | [diff] [blame] | 106 | /* Reset the timer */ |
| 107 | read_timer(); |
| 108 | timer.ticks = 0; |
Matthias Kaehlcke | fcfb632 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * This function is derived from PowerPC code (timebase clock frequency). |
| 115 | * On ARM it returns the number of timer ticks per second. |
| 116 | */ |
| 117 | unsigned long get_tbclk(void) |
| 118 | { |
| 119 | return CONFIG_SYS_HZ; |
| 120 | } |