wdenk | 2d5b561 | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 1 | /* |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 2 | * (C) Copyright 2010 |
| 3 | * Michael Schwingen, michael@schwingen.org |
| 4 | * |
Wolfgang Denk | ba94a1b | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 5 | * (C) Copyright 2006 |
| 6 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 7 | * |
wdenk | 2d5b561 | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 8 | * (C) Copyright 2002 |
| 9 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 10 | * Marius Groeger <mgroeger@sysgo.de> |
| 11 | * |
| 12 | * (C) Copyright 2002 |
| 13 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 14 | * Alex Zuepke <azu@sysgo.de> |
| 15 | * |
| 16 | * See file CREDITS for list of people who contributed to this |
| 17 | * project. |
| 18 | * |
| 19 | * This program is free software; you can redistribute it and/or |
| 20 | * modify it under the terms of the GNU General Public License as |
| 21 | * published by the Free Software Foundation; either version 2 of |
| 22 | * the License, or (at your option) any later version. |
| 23 | * |
| 24 | * This program is distributed in the hope that it will be useful, |
| 25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | * GNU General Public License for more details. |
| 28 | * |
| 29 | * You should have received a copy of the GNU General Public License |
| 30 | * along with this program; if not, write to the Free Software |
| 31 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 32 | * MA 02111-1307 USA |
| 33 | */ |
| 34 | |
| 35 | #include <common.h> |
| 36 | #include <asm/arch/ixp425.h> |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 37 | #include <asm/io.h> |
| 38 | #include <div64.h> |
wdenk | 2d5b561 | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 39 | |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 40 | DECLARE_GLOBAL_DATA_PTR; |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 41 | |
| 42 | /* |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 43 | * The IXP42x time-stamp timer runs at 2*OSC_IN (66.666MHz when using a |
| 44 | * 33.333MHz crystal). |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 45 | */ |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 46 | static inline unsigned long long tick_to_time(unsigned long long tick) |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 47 | { |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 48 | tick *= CONFIG_SYS_HZ; |
| 49 | do_div(tick, CONFIG_IXP425_TIMER_CLK); |
| 50 | return tick; |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 53 | static inline unsigned long long time_to_tick(unsigned long long time) |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 54 | { |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 55 | time *= CONFIG_IXP425_TIMER_CLK; |
| 56 | do_div(time, CONFIG_SYS_HZ); |
| 57 | return time; |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 60 | static inline unsigned long long us_to_tick(unsigned long long us) |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 61 | { |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 62 | us = us * CONFIG_IXP425_TIMER_CLK + 999999; |
| 63 | do_div(us, 1000000); |
| 64 | return us; |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 67 | unsigned long long get_ticks(void) |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 68 | { |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 69 | ulong now = readl(IXP425_OSTS_B); |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 70 | |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 71 | if (readl(IXP425_OSST) & IXP425_OSST_TIMER_TS_PEND) { |
| 72 | /* rollover of timestamp timer register */ |
Simon Glass | b4d51db | 2012-12-13 20:48:37 +0000 | [diff] [blame^] | 73 | gd->arch.timestamp += (0xFFFFFFFF - gd->arch.lastinc) + now + 1; |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 74 | writel(IXP425_OSST_TIMER_TS_PEND, IXP425_OSST); |
| 75 | } else { |
| 76 | /* move stamp forward with absolut diff ticks */ |
Simon Glass | b4d51db | 2012-12-13 20:48:37 +0000 | [diff] [blame^] | 77 | gd->arch.timestamp += (now - gd->arch.lastinc); |
wdenk | 2d5b561 | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 78 | } |
Simon Glass | 582601d | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 79 | gd->arch.lastinc = now; |
Simon Glass | b4d51db | 2012-12-13 20:48:37 +0000 | [diff] [blame^] | 80 | return gd->arch.timestamp; |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | |
| 84 | void reset_timer_masked(void) |
| 85 | { |
| 86 | /* capture current timestamp counter */ |
Simon Glass | 582601d | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 87 | gd->arch.lastinc = readl(IXP425_OSTS_B); |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 88 | /* start "advancing" time stamp from 0 */ |
Simon Glass | b4d51db | 2012-12-13 20:48:37 +0000 | [diff] [blame^] | 89 | gd->arch.timestamp = 0; |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 92 | ulong get_timer_masked(void) |
| 93 | { |
| 94 | return tick_to_time(get_ticks()); |
| 95 | } |
| 96 | |
| 97 | ulong get_timer(ulong base) |
| 98 | { |
| 99 | return get_timer_masked() - base; |
| 100 | } |
| 101 | |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 102 | /* delay x useconds AND preserve advance timestamp value */ |
| 103 | void __udelay(unsigned long usec) |
| 104 | { |
| 105 | unsigned long long tmp; |
| 106 | |
| 107 | tmp = get_ticks() + us_to_tick(usec); |
| 108 | |
| 109 | while (get_ticks() < tmp) |
| 110 | ; |
wdenk | 2d5b561 | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 111 | } |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 112 | |
| 113 | int timer_init(void) |
| 114 | { |
Michael Schwingen | ce04bb4 | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 115 | writel(IXP425_OSST_TIMER_TS_PEND, IXP425_OSST); |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 116 | return 0; |
| 117 | } |