Furquan Shaikh | 7657057 | 2014-03-19 14:29:48 -0700 | [diff] [blame] | 1 | /* |
Deepa Dinamani | 74aa777 | 2014-07-14 16:26:11 -0700 | [diff] [blame] | 2 | * Copyright (c) 2011 - 2014 The Linux Foundation. All rights reserved. |
Furquan Shaikh | 7657057 | 2014-03-19 14:29:48 -0700 | [diff] [blame] | 3 | * |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in |
| 11 | * the documentation and/or other materials provided with the |
| 12 | * distribution. |
| 13 | * * Neither the name of Google, Inc. nor the names of its contributors |
| 14 | * may be used to endorse or promote products derived from this |
| 15 | * software without specific prior written permission. |
Furquan Shaikh | 7657057 | 2014-03-19 14:29:48 -0700 | [diff] [blame] | 16 | * |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 21 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 23 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 24 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 25 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 27 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
Furquan Shaikh | 7657057 | 2014-03-19 14:29:48 -0700 | [diff] [blame] | 29 | */ |
| 30 | |
Vadim Bendebury | f4b209f | 2014-04-09 19:23:04 -0700 | [diff] [blame] | 31 | #include <delay.h> |
Julius Werner | 73d1ed6 | 2014-10-20 13:20:49 -0700 | [diff] [blame] | 32 | #include <soc/iomap.h> |
| 33 | #include <soc/ipq_timer.h> |
Vadim Bendebury | f4b209f | 2014-04-09 19:23:04 -0700 | [diff] [blame] | 34 | #include <timer.h> |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 35 | |
Deepa Dinamani | 74aa777 | 2014-07-14 16:26:11 -0700 | [diff] [blame] | 36 | /* |
| 37 | * DGT runs at 25 MHz / 4, or 6.25 ticks per microsecond |
| 38 | */ |
| 39 | #define DGT_MHZ_NUM 25 |
| 40 | #define DGT_MHZ_DEN 4 |
| 41 | |
| 42 | #define TIMER_TICKS(us) ((DGT_MHZ_NUM*(us) + (DGT_MHZ_DEN - 1)) / DGT_MHZ_DEN) |
| 43 | #define TIMER_USECS(ticks) (DGT_MHZ_DEN*(ticks) / DGT_MHZ_NUM) |
| 44 | |
| 45 | /* Clock divider values for the timer. */ |
| 46 | #define DGT_CLK_DIV_1 0 |
| 47 | #define DGT_CLK_DIV_2 1 |
| 48 | #define DGT_CLK_DIV_3 2 |
| 49 | #define DGT_CLK_DIV_4 3 |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
Marc Jones | 017287a | 2014-12-29 16:52:59 -0700 | [diff] [blame] | 52 | * init_timer - initialize timer |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 53 | */ |
Vadim Bendebury | f4b209f | 2014-04-09 19:23:04 -0700 | [diff] [blame] | 54 | void init_timer(void) |
Furquan Shaikh | 7657057 | 2014-03-19 14:29:48 -0700 | [diff] [blame] | 55 | { |
Deepa Dinamani | 74aa777 | 2014-07-14 16:26:11 -0700 | [diff] [blame] | 56 | /* disable timer */ |
| 57 | writel_i(0, DGT_ENABLE); |
| 58 | |
| 59 | /* DGT uses TCXO source which is 25MHz. |
| 60 | * The timer should run at 1/4th the frequency of TCXO |
| 61 | * according to clock plan. |
| 62 | * Set clock divider to 4. |
| 63 | */ |
| 64 | writel_i(DGT_CLK_DIV_4, DGT_CLK_CTL); |
| 65 | |
| 66 | /* Enable timer */ |
| 67 | writel_i(0, DGT_CLEAR); |
| 68 | writel_i(DGT_ENABLE_EN, DGT_ENABLE); |
Furquan Shaikh | 7657057 | 2014-03-19 14:29:48 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 71 | /** |
Vadim Bendebury | f4b209f | 2014-04-09 19:23:04 -0700 | [diff] [blame] | 72 | * udelay - generates micro second delay. |
Martin Roth | 5f066b2 | 2015-01-04 16:47:39 -0700 | [diff] [blame] | 73 | * @param usec: delay duration in microseconds |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 74 | */ |
Vadim Bendebury | f4b209f | 2014-04-09 19:23:04 -0700 | [diff] [blame] | 75 | void udelay(unsigned usec) |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 76 | { |
Deepa Dinamani | 74aa777 | 2014-07-14 16:26:11 -0700 | [diff] [blame] | 77 | uint32_t now; |
| 78 | uint32_t last; |
| 79 | uint32_t ticks; |
| 80 | uint32_t curr_ticks = 0; |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 81 | |
Deepa Dinamani | 74aa777 | 2014-07-14 16:26:11 -0700 | [diff] [blame] | 82 | /* Calculate number of ticks required. */ |
| 83 | ticks = TIMER_TICKS(usec); |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 84 | |
Deepa Dinamani | 74aa777 | 2014-07-14 16:26:11 -0700 | [diff] [blame] | 85 | /* Obtain the current timer value. */ |
| 86 | last = readl_i(DGT_COUNT_VAL); |
| 87 | |
| 88 | /* Loop until the right number of ticks. */ |
| 89 | while (curr_ticks < ticks) { |
| 90 | now = readl_i(DGT_COUNT_VAL); |
| 91 | curr_ticks += now - last; |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 92 | last = now; |
Deepa Dinamani | 74aa777 | 2014-07-14 16:26:11 -0700 | [diff] [blame] | 93 | } |
Vadim Bendebury | 476f731 | 2014-04-08 18:45:46 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Vadim Bendebury | 20d3d53 | 2014-05-01 15:24:32 -0700 | [diff] [blame] | 96 | void timer_monotonic_get(struct mono_time *mt) |
| 97 | { |
Deepa Dinamani | 74aa777 | 2014-07-14 16:26:11 -0700 | [diff] [blame] | 98 | mono_time_set_usecs(mt, TIMER_USECS(readl_i(DGT_COUNT_VAL))); |
Vadim Bendebury | 20d3d53 | 2014-05-01 15:24:32 -0700 | [diff] [blame] | 99 | } |