wdenk | bd1575f | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 1 | /* |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 2 | * (C) Copyright 2010 |
| 3 | * Michael Schwingen, michael@schwingen.org |
| 4 | * |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 5 | * (C) Copyright 2006 |
| 6 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 7 | * |
wdenk | bd1575f | 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 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 16 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | bd1575f | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <common.h> |
| 20 | #include <asm/arch/ixp425.h> |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 21 | #include <asm/io.h> |
| 22 | #include <div64.h> |
wdenk | bd1575f | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 23 | |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 24 | DECLARE_GLOBAL_DATA_PTR; |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 25 | |
| 26 | /* |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 27 | * The IXP42x time-stamp timer runs at 2*OSC_IN (66.666MHz when using a |
| 28 | * 33.333MHz crystal). |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 29 | */ |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 30 | static inline unsigned long long tick_to_time(unsigned long long tick) |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 31 | { |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 32 | tick *= CONFIG_SYS_HZ; |
| 33 | do_div(tick, CONFIG_IXP425_TIMER_CLK); |
| 34 | return tick; |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 37 | static inline unsigned long long time_to_tick(unsigned long long time) |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 38 | { |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 39 | time *= CONFIG_IXP425_TIMER_CLK; |
| 40 | do_div(time, CONFIG_SYS_HZ); |
| 41 | return time; |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 44 | static inline unsigned long long us_to_tick(unsigned long long us) |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 45 | { |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 46 | us = us * CONFIG_IXP425_TIMER_CLK + 999999; |
| 47 | do_div(us, 1000000); |
| 48 | return us; |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 51 | unsigned long long get_ticks(void) |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 52 | { |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 53 | ulong now = readl(IXP425_OSTS_B); |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 54 | |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 55 | if (readl(IXP425_OSST) & IXP425_OSST_TIMER_TS_PEND) { |
| 56 | /* rollover of timestamp timer register */ |
Simon Glass | dddf345 | 2012-12-13 20:48:37 +0000 | [diff] [blame] | 57 | gd->arch.timestamp += (0xFFFFFFFF - gd->arch.lastinc) + now + 1; |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 58 | writel(IXP425_OSST_TIMER_TS_PEND, IXP425_OSST); |
| 59 | } else { |
| 60 | /* move stamp forward with absolut diff ticks */ |
Simon Glass | dddf345 | 2012-12-13 20:48:37 +0000 | [diff] [blame] | 61 | gd->arch.timestamp += (now - gd->arch.lastinc); |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 62 | } |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 63 | gd->arch.lastinc = now; |
Simon Glass | dddf345 | 2012-12-13 20:48:37 +0000 | [diff] [blame] | 64 | return gd->arch.timestamp; |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 65 | } |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 66 | |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 67 | |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 68 | void reset_timer_masked(void) |
wdenk | 655a0f9 | 2003-10-30 21:49:38 +0000 | [diff] [blame] | 69 | { |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 70 | /* capture current timestamp counter */ |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 71 | gd->arch.lastinc = readl(IXP425_OSTS_B); |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 72 | /* start "advancing" time stamp from 0 */ |
Simon Glass | dddf345 | 2012-12-13 20:48:37 +0000 | [diff] [blame] | 73 | gd->arch.timestamp = 0; |
wdenk | 655a0f9 | 2003-10-30 21:49:38 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 76 | ulong get_timer_masked(void) |
wdenk | bd1575f | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 77 | { |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 78 | return tick_to_time(get_ticks()); |
wdenk | bd1575f | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 81 | ulong get_timer(ulong base) |
wdenk | bd1575f | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 82 | { |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 83 | return get_timer_masked() - base; |
| 84 | } |
wdenk | bd1575f | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 85 | |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 86 | /* delay x useconds AND preserve advance timestamp value */ |
| 87 | void __udelay(unsigned long usec) |
wdenk | bd1575f | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 88 | { |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 89 | unsigned long long tmp; |
| 90 | |
| 91 | tmp = get_ticks() + us_to_tick(usec); |
| 92 | |
| 93 | while (get_ticks() < tmp) |
| 94 | ; |
wdenk | bd1575f | 2003-10-14 19:43:55 +0000 | [diff] [blame] | 95 | } |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 96 | |
| 97 | int timer_init(void) |
| 98 | { |
Michael Schwingen | 7ddf55e | 2011-05-23 00:00:00 +0200 | [diff] [blame] | 99 | writel(IXP425_OSST_TIMER_TS_PEND, IXP425_OSST); |
Jean-Christophe PLAGNIOL-VILLARD | 8c9fc00 | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 100 | return 0; |
| 101 | } |