Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Cirrus Logic EP93xx timer support. |
| 4 | * |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 5 | * Copyright (C) 2009, 2010 Matthias Kaehlcke <matthias@kaehlcke.net> |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 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. |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <common.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 15 | #include <init.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 16 | #include <time.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 17 | #include <linux/delay.h> |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 18 | #include <linux/types.h> |
| 19 | #include <asm/arch/ep93xx.h> |
| 20 | #include <asm/io.h> |
Matthias Kaehlcke | f56e70f | 2010-02-24 00:22:00 +0100 | [diff] [blame] | 21 | #include <div64.h> |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 22 | |
| 23 | #define TIMER_CLKSEL (1 << 3) |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 24 | #define TIMER_ENABLE (1 << 7) |
| 25 | |
Matthias Kaehlcke | 64d667b | 2010-03-09 22:13:20 +0100 | [diff] [blame] | 26 | #define TIMER_FREQ 508469 /* ticks / second */ |
Matthias Kaehlcke | 8df23b7 | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 27 | #define TIMER_MAX_VAL 0xFFFFFFFF |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 28 | |
Matthias Kaehlcke | 8df23b7 | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 29 | static struct ep93xx_timer |
| 30 | { |
| 31 | unsigned long long ticks; |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 32 | unsigned long last_read; |
Matthias Kaehlcke | 8df23b7 | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 33 | } timer; |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 34 | |
Matthias Kaehlcke | 64d667b | 2010-03-09 22:13:20 +0100 | [diff] [blame] | 35 | static inline unsigned long long usecs_to_ticks(unsigned long usecs) |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 36 | { |
Matthias Kaehlcke | 64d667b | 2010-03-09 22:13:20 +0100 | [diff] [blame] | 37 | unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ; |
| 38 | do_div(ticks, 1000 * 1000); |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 39 | |
| 40 | return ticks; |
| 41 | } |
| 42 | |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 43 | static inline void read_timer(void) |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 44 | { |
Matthias Kaehlcke | 9d46656 | 2010-03-09 22:13:47 +0100 | [diff] [blame] | 45 | struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE; |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 46 | const unsigned long now = TIMER_MAX_VAL - readl(&timer_regs->timer3.value); |
| 47 | |
| 48 | if (now >= timer.last_read) |
| 49 | timer.ticks += now - timer.last_read; |
| 50 | else |
| 51 | /* an overflow occurred */ |
| 52 | timer.ticks += TIMER_MAX_VAL - timer.last_read + now; |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 53 | |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 54 | timer.last_read = now; |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* |
Matthias Kaehlcke | 957f7b5 | 2010-03-09 22:13:33 +0100 | [diff] [blame] | 58 | * Get the number of ticks (in CONFIG_SYS_HZ resolution) |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 59 | */ |
| 60 | unsigned long long get_ticks(void) |
| 61 | { |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 62 | unsigned long long sys_ticks; |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 63 | |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 64 | read_timer(); |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 65 | |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 66 | sys_ticks = timer.ticks * CONFIG_SYS_HZ; |
| 67 | do_div(sys_ticks, TIMER_FREQ); |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 68 | |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 69 | return sys_ticks; |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 72 | unsigned long get_timer(unsigned long base) |
| 73 | { |
Patrick Delaunay | 9858a60 | 2018-10-05 11:33:52 +0200 | [diff] [blame] | 74 | return get_ticks() - base; |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 77 | void __udelay(unsigned long usec) |
| 78 | { |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 79 | unsigned long long target; |
Matthias Kaehlcke | 957f7b5 | 2010-03-09 22:13:33 +0100 | [diff] [blame] | 80 | |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 81 | read_timer(); |
| 82 | |
| 83 | target = timer.ticks + usecs_to_ticks(usec); |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 84 | |
Matthias Kaehlcke | 957f7b5 | 2010-03-09 22:13:33 +0100 | [diff] [blame] | 85 | while (timer.ticks < target) |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 86 | read_timer(); |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | int timer_init(void) |
| 90 | { |
Matthias Kaehlcke | 9d46656 | 2010-03-09 22:13:47 +0100 | [diff] [blame] | 91 | struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE; |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 92 | |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 93 | /* use timer 3 with 508KHz and free running, not enabled now */ |
Matthias Kaehlcke | 9d46656 | 2010-03-09 22:13:47 +0100 | [diff] [blame] | 94 | writel(TIMER_CLKSEL, &timer_regs->timer3.control); |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 95 | |
Matthias Kaehlcke | 37911ae | 2010-03-09 22:13:56 +0100 | [diff] [blame] | 96 | /* set initial timer value */ |
Matthias Kaehlcke | 9d46656 | 2010-03-09 22:13:47 +0100 | [diff] [blame] | 97 | writel(TIMER_MAX_VAL, &timer_regs->timer3.load); |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 98 | |
Matthias Kaehlcke | 8df23b7 | 2010-02-24 00:22:09 +0100 | [diff] [blame] | 99 | /* Enable the timer */ |
| 100 | writel(TIMER_ENABLE | TIMER_CLKSEL, |
Matthias Kaehlcke | 9d46656 | 2010-03-09 22:13:47 +0100 | [diff] [blame] | 101 | &timer_regs->timer3.control); |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 102 | |
Graeme Russ | 944a7fe | 2011-07-15 02:21:14 +0000 | [diff] [blame] | 103 | /* Reset the timer */ |
| 104 | read_timer(); |
| 105 | timer.ticks = 0; |
Matthias Kaehlcke | 195dbd1 | 2010-02-01 21:29:39 +0100 | [diff] [blame] | 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * This function is derived from PowerPC code (timebase clock frequency). |
| 112 | * On ARM it returns the number of timer ticks per second. |
| 113 | */ |
| 114 | unsigned long get_tbclk(void) |
| 115 | { |
| 116 | return CONFIG_SYS_HZ; |
| 117 | } |