Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Freescale i.MX28 timer driver |
| 3 | * |
| 4 | * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com> |
| 5 | * on behalf of DENX Software Engineering GmbH |
| 6 | * |
| 7 | * Based on code from LTIB: |
| 8 | * (C) Copyright 2009-2010 Freescale Semiconductor, Inc. |
| 9 | * |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <asm/io.h> |
| 15 | #include <asm/arch/imx-regs.h> |
| 16 | #include <asm/arch/sys_proto.h> |
| 17 | |
| 18 | /* Maximum fixed count */ |
Fadil Berisha | d608f6e | 2013-02-27 17:00:07 +0000 | [diff] [blame] | 19 | #if defined(CONFIG_MX23) |
| 20 | #define TIMER_LOAD_VAL 0xffff |
| 21 | #elif defined(CONFIG_MX28) |
| 22 | #define TIMER_LOAD_VAL 0xffffffff |
| 23 | #endif |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 24 | |
| 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 27 | #define timestamp (gd->arch.tbl) |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 28 | #define lastdec (gd->arch.lastinc) |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * This driver uses 1kHz clock source. |
| 32 | */ |
Fadil Berisha | 69d8ce0 | 2013-02-28 10:03:26 -0500 | [diff] [blame] | 33 | #define MXS_INCREMENTER_HZ 1000 |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 34 | |
| 35 | static inline unsigned long tick_to_time(unsigned long tick) |
| 36 | { |
Fadil Berisha | 69d8ce0 | 2013-02-28 10:03:26 -0500 | [diff] [blame] | 37 | return tick / (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ); |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static inline unsigned long time_to_tick(unsigned long time) |
| 41 | { |
Fadil Berisha | 69d8ce0 | 2013-02-28 10:03:26 -0500 | [diff] [blame] | 42 | return time * (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ); |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* Calculate how many ticks happen in "us" microseconds */ |
| 46 | static inline unsigned long us_to_tick(unsigned long us) |
| 47 | { |
Fadil Berisha | 69d8ce0 | 2013-02-28 10:03:26 -0500 | [diff] [blame] | 48 | return (us * MXS_INCREMENTER_HZ) / 1000000; |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | int timer_init(void) |
| 52 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 53 | struct mxs_timrot_regs *timrot_regs = |
| 54 | (struct mxs_timrot_regs *)MXS_TIMROT_BASE; |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 55 | |
| 56 | /* Reset Timers and Rotary Encoder module */ |
Otavio Salvador | cbf0bf2 | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 57 | mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg); |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 58 | |
| 59 | /* Set fixed_count to 0 */ |
Fadil Berisha | d608f6e | 2013-02-27 17:00:07 +0000 | [diff] [blame] | 60 | #if defined(CONFIG_MX23) |
| 61 | writel(0, &timrot_regs->hw_timrot_timcount0); |
| 62 | #elif defined(CONFIG_MX28) |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 63 | writel(0, &timrot_regs->hw_timrot_fixed_count0); |
Fadil Berisha | d608f6e | 2013-02-27 17:00:07 +0000 | [diff] [blame] | 64 | #endif |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 65 | |
| 66 | /* Set UPDATE bit and 1Khz frequency */ |
| 67 | writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD | |
| 68 | TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL, |
| 69 | &timrot_regs->hw_timrot_timctrl0); |
| 70 | |
| 71 | /* Set fixed_count to maximal value */ |
Fadil Berisha | d608f6e | 2013-02-27 17:00:07 +0000 | [diff] [blame] | 72 | #if defined(CONFIG_MX23) |
| 73 | writel(TIMER_LOAD_VAL - 1, &timrot_regs->hw_timrot_timcount0); |
| 74 | #elif defined(CONFIG_MX28) |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 75 | writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0); |
Fadil Berisha | d608f6e | 2013-02-27 17:00:07 +0000 | [diff] [blame] | 76 | #endif |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Marek Vasut | c142b67 | 2012-02-07 06:47:31 +0000 | [diff] [blame] | 81 | unsigned long long get_ticks(void) |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 82 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 83 | struct mxs_timrot_regs *timrot_regs = |
| 84 | (struct mxs_timrot_regs *)MXS_TIMROT_BASE; |
Fadil Berisha | d608f6e | 2013-02-27 17:00:07 +0000 | [diff] [blame] | 85 | uint32_t now; |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 86 | |
| 87 | /* Current tick value */ |
Fadil Berisha | d608f6e | 2013-02-27 17:00:07 +0000 | [diff] [blame] | 88 | #if defined(CONFIG_MX23) |
| 89 | /* Upper bits are the valid ones. */ |
| 90 | now = readl(&timrot_regs->hw_timrot_timcount0) >> |
| 91 | TIMROT_RUNNING_COUNTn_RUNNING_COUNT_OFFSET; |
| 92 | #elif defined(CONFIG_MX28) |
| 93 | now = readl(&timrot_regs->hw_timrot_running_count0); |
Wolfgang Denk | 1fc4234 | 2014-11-06 14:03:04 +0100 | [diff] [blame] | 94 | #else |
| 95 | #error "Don't know how to read timrot_regs" |
Fadil Berisha | d608f6e | 2013-02-27 17:00:07 +0000 | [diff] [blame] | 96 | #endif |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 97 | |
| 98 | if (lastdec >= now) { |
| 99 | /* |
| 100 | * normal mode (non roll) |
| 101 | * move stamp forward with absolut diff ticks |
| 102 | */ |
| 103 | timestamp += (lastdec - now); |
| 104 | } else { |
| 105 | /* we have rollover of decrementer */ |
| 106 | timestamp += (TIMER_LOAD_VAL - now) + lastdec; |
| 107 | |
| 108 | } |
| 109 | lastdec = now; |
| 110 | |
Marek Vasut | c142b67 | 2012-02-07 06:47:31 +0000 | [diff] [blame] | 111 | return timestamp; |
| 112 | } |
| 113 | |
| 114 | ulong get_timer_masked(void) |
| 115 | { |
| 116 | return tick_to_time(get_ticks()); |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Marek Vasut | c142b67 | 2012-02-07 06:47:31 +0000 | [diff] [blame] | 119 | ulong get_timer(ulong base) |
| 120 | { |
| 121 | return get_timer_masked() - base; |
| 122 | } |
| 123 | |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 124 | /* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */ |
Fadil Berisha | 69d8ce0 | 2013-02-28 10:03:26 -0500 | [diff] [blame] | 125 | #define MXS_HW_DIGCTL_MICROSECONDS 0x8001c0c0 |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 126 | |
| 127 | void __udelay(unsigned long usec) |
| 128 | { |
| 129 | uint32_t old, new, incr; |
| 130 | uint32_t counter = 0; |
| 131 | |
Fadil Berisha | 69d8ce0 | 2013-02-28 10:03:26 -0500 | [diff] [blame] | 132 | old = readl(MXS_HW_DIGCTL_MICROSECONDS); |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 133 | |
| 134 | while (counter < usec) { |
Fadil Berisha | 69d8ce0 | 2013-02-28 10:03:26 -0500 | [diff] [blame] | 135 | new = readl(MXS_HW_DIGCTL_MICROSECONDS); |
Marek Vasut | c140e98 | 2011-11-08 23:18:08 +0000 | [diff] [blame] | 136 | |
| 137 | /* Check if the timer wrapped. */ |
| 138 | if (new < old) { |
| 139 | incr = 0xffffffff - old; |
| 140 | incr += new; |
| 141 | } else { |
| 142 | incr = new - old; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Check if we are close to the maximum time and the counter |
| 147 | * would wrap if incremented. If that's the case, break out |
| 148 | * from the loop as the requested delay time passed. |
| 149 | */ |
| 150 | if (counter + incr < counter) |
| 151 | break; |
| 152 | |
| 153 | counter += incr; |
| 154 | old = new; |
| 155 | } |
| 156 | } |
Marek Vasut | c142b67 | 2012-02-07 06:47:31 +0000 | [diff] [blame] | 157 | |
| 158 | ulong get_tbclk(void) |
| 159 | { |
Fadil Berisha | 69d8ce0 | 2013-02-28 10:03:26 -0500 | [diff] [blame] | 160 | return MXS_INCREMENTER_HZ; |
Marek Vasut | c142b67 | 2012-02-07 06:47:31 +0000 | [diff] [blame] | 161 | } |