Brian Swetland | 2500aa1 | 2009-01-01 04:33:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 5 | * Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved. |
| 6 | * |
Brian Swetland | 2500aa1 | 2009-01-01 04:33:55 -0800 | [diff] [blame] | 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
| 14 | * the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google, Inc. nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this |
| 18 | * software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 24 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 26 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 27 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 28 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 30 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #include <debug.h> |
| 35 | #include <reg.h> |
| 36 | #include <sys/types.h> |
| 37 | |
| 38 | #include <platform/timer.h> |
| 39 | #include <platform/irqs.h> |
| 40 | #include <platform/iomap.h> |
| 41 | #include <platform/interrupts.h> |
| 42 | #include <kernel/thread.h> |
| 43 | |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 44 | #if PLATFORM_MSM7X30 || PLATFORM_MSM8X60 |
Ajay Dudani | 232ce81 | 2009-12-02 00:14:11 -0800 | [diff] [blame] | 45 | |
| 46 | #define MSM_GPT_BASE (MSM_TMR_BASE + 0x4) |
| 47 | #define MSM_DGT_BASE (MSM_TMR_BASE + 0x24) |
| 48 | #define GPT_REG(off) (MSM_GPT_BASE + (off)) |
| 49 | #define DGT_REG(off) (MSM_DGT_BASE + (off)) |
| 50 | #define SPSS_TIMER_STATUS (MSM_TMR_BASE + 0x88) |
| 51 | |
| 52 | #define GPT_MATCH_VAL GPT_REG(0x0000) |
| 53 | #define GPT_COUNT_VAL GPT_REG(0x0004) |
| 54 | #define GPT_ENABLE GPT_REG(0x0008) |
| 55 | #define GPT_ENABLE_CLR_ON_MATCH_EN 2 |
| 56 | #define GPT_ENABLE_EN 1 |
| 57 | #define GPT_CLEAR GPT_REG(0x000C) |
| 58 | |
| 59 | #define DGT_MATCH_VAL DGT_REG(0x0000) |
| 60 | #define DGT_COUNT_VAL DGT_REG(0x0004) |
| 61 | #define DGT_ENABLE DGT_REG(0x0008) |
| 62 | #define DGT_ENABLE_CLR_ON_MATCH_EN 2 |
| 63 | #define DGT_ENABLE_EN 1 |
| 64 | #define DGT_CLEAR DGT_REG(0x000C) |
Chandan Uddaraju | 037b226 | 2010-01-13 15:21:27 -0800 | [diff] [blame] | 65 | #define DGT_CLK_CTL DGT_REG(0x0010) |
Ajay Dudani | 232ce81 | 2009-12-02 00:14:11 -0800 | [diff] [blame] | 66 | |
Ajay Dudani | c51bf63 | 2010-04-15 10:41:23 -0700 | [diff] [blame] | 67 | #define HW_REVISION_NUMBER 0xABC00270 |
| 68 | |
| 69 | |
Ajay Dudani | 232ce81 | 2009-12-02 00:14:11 -0800 | [diff] [blame] | 70 | #else |
Brian Swetland | 2500aa1 | 2009-01-01 04:33:55 -0800 | [diff] [blame] | 71 | #define GPT_REG(off) (MSM_GPT_BASE + (off)) |
| 72 | |
| 73 | #define GPT_MATCH_VAL GPT_REG(0x0000) |
| 74 | #define GPT_COUNT_VAL GPT_REG(0x0004) |
| 75 | #define GPT_ENABLE GPT_REG(0x0008) |
| 76 | #define GPT_ENABLE_CLR_ON_MATCH_EN 2 |
| 77 | #define GPT_ENABLE_EN 1 |
| 78 | #define GPT_CLEAR GPT_REG(0x000C) |
| 79 | |
| 80 | #define DGT_MATCH_VAL GPT_REG(0x0010) |
| 81 | #define DGT_COUNT_VAL GPT_REG(0x0014) |
| 82 | #define DGT_ENABLE GPT_REG(0x0018) |
| 83 | #define DGT_ENABLE_CLR_ON_MATCH_EN 2 |
| 84 | #define DGT_ENABLE_EN 1 |
| 85 | #define DGT_CLEAR GPT_REG(0x001C) |
| 86 | |
Brian Swetland | 0d7b1b8 | 2009-01-21 21:03:28 -0800 | [diff] [blame] | 87 | #define SPSS_TIMER_STATUS GPT_REG(0x0034) |
Ajay Dudani | 232ce81 | 2009-12-02 00:14:11 -0800 | [diff] [blame] | 88 | #endif |
| 89 | |
Chandan Uddaraju | 1679e68 | 2010-03-19 16:40:44 -0700 | [diff] [blame] | 90 | #if defined PLATFORM_QSD8K |
| 91 | #define DGT_HZ 4800000 /* Uses TCXO/4 (19.2 MHz / 4) */ |
| 92 | #elif defined PLATFORM_MSM7X30 |
David Ng | 5693676 | 2010-03-25 19:50:32 -0700 | [diff] [blame] | 93 | #if _EMMC_BOOT |
| 94 | #define DGT_HZ 19200000 /* Uses TCXO (19.2 MHz) */ |
| 95 | #else |
Chandan Uddaraju | 1679e68 | 2010-03-19 16:40:44 -0700 | [diff] [blame] | 96 | #define DGT_HZ 6144000 /* Uses LPXO/4 (24.576 MHz / 4) */ |
David Ng | 5693676 | 2010-03-25 19:50:32 -0700 | [diff] [blame] | 97 | #endif |
Chandan Uddaraju | 1679e68 | 2010-03-19 16:40:44 -0700 | [diff] [blame] | 98 | #else |
| 99 | #define DGT_HZ 19200000 /* Uses TCXO (19.2 MHz) */ |
| 100 | #endif |
| 101 | |
Brian Swetland | 2500aa1 | 2009-01-01 04:33:55 -0800 | [diff] [blame] | 102 | |
| 103 | static platform_timer_callback timer_callback; |
| 104 | static void *timer_arg; |
| 105 | static time_t timer_interval; |
| 106 | |
| 107 | static volatile uint32_t ticks; |
| 108 | |
| 109 | static enum handler_return timer_irq(void *arg) |
| 110 | { |
| 111 | ticks += timer_interval; |
| 112 | return timer_callback(timer_arg, ticks); |
| 113 | } |
| 114 | |
| 115 | status_t platform_set_periodic_timer( |
| 116 | platform_timer_callback callback, |
| 117 | void *arg, time_t interval) |
| 118 | { |
Chandan Uddaraju | 037b226 | 2010-01-13 15:21:27 -0800 | [diff] [blame] | 119 | #ifdef PLATFORM_MSM7X30 |
Subbaraman Narayanamurthy | 492c8c0 | 2010-10-27 11:41:49 -0700 | [diff] [blame] | 120 | unsigned val = 0; |
Chandan Uddaraju | 037b226 | 2010-01-13 15:21:27 -0800 | [diff] [blame] | 121 | //Check for the hardware revision |
| 122 | val = readl(HW_REVISION_NUMBER); |
Subbaraman Narayanamurthy | 492c8c0 | 2010-10-27 11:41:49 -0700 | [diff] [blame] | 123 | val = (val >> 28) & 0x0F; |
| 124 | if(val >= 1) |
Chandan Uddaraju | 037b226 | 2010-01-13 15:21:27 -0800 | [diff] [blame] | 125 | writel(1, DGT_CLK_CTL); |
| 126 | #endif |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 127 | #ifdef PLATFORM_MSM8X60 |
| 128 | writel(1, DGT_CLK_CTL); |
| 129 | #endif |
Brian Swetland | 2500aa1 | 2009-01-01 04:33:55 -0800 | [diff] [blame] | 130 | enter_critical_section(); |
| 131 | |
| 132 | timer_callback = callback; |
| 133 | timer_arg = arg; |
| 134 | timer_interval = interval; |
| 135 | |
Chandan Uddaraju | 1679e68 | 2010-03-19 16:40:44 -0700 | [diff] [blame] | 136 | writel(timer_interval * (DGT_HZ / 1000), DGT_MATCH_VAL); |
Brian Swetland | 2500aa1 | 2009-01-01 04:33:55 -0800 | [diff] [blame] | 137 | writel(0, DGT_CLEAR); |
| 138 | writel(DGT_ENABLE_EN | DGT_ENABLE_CLR_ON_MATCH_EN, DGT_ENABLE); |
| 139 | |
| 140 | register_int_handler(INT_DEBUG_TIMER_EXP, timer_irq, 0); |
| 141 | unmask_interrupt(INT_DEBUG_TIMER_EXP); |
| 142 | |
| 143 | exit_critical_section(); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | time_t current_time(void) |
| 149 | { |
| 150 | return ticks; |
| 151 | } |
| 152 | |
| 153 | void platform_init_timer(void) |
| 154 | { |
| 155 | writel(0, DGT_ENABLE); |
| 156 | } |
| 157 | |
Brian Swetland | 0d7b1b8 | 2009-01-21 21:03:28 -0800 | [diff] [blame] | 158 | static void wait_for_timer_op(void) |
| 159 | { |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 160 | #if PLATFORM_QSD8K || PLATFORM_MSM7X30 || PLATFORM_MSM8X60 |
Brian Swetland | 0d7b1b8 | 2009-01-21 21:03:28 -0800 | [diff] [blame] | 161 | while(readl(SPSS_TIMER_STATUS)) ; |
| 162 | #endif |
| 163 | } |
| 164 | |
| 165 | void platform_uninit_timer(void) |
| 166 | { |
| 167 | writel(0, DGT_ENABLE); |
| 168 | wait_for_timer_op(); |
| 169 | writel(0, DGT_CLEAR); |
| 170 | wait_for_timer_op(); |
| 171 | } |
Chandan Uddaraju | 852cd2c | 2009-12-17 14:28:28 -0800 | [diff] [blame] | 172 | |
| 173 | void mdelay(unsigned msecs) |
| 174 | { |
| 175 | msecs *= 33; |
| 176 | |
| 177 | writel(0, GPT_CLEAR); |
| 178 | writel(0, GPT_ENABLE); |
| 179 | while(readl(GPT_COUNT_VAL) != 0) ; |
| 180 | |
| 181 | writel(GPT_ENABLE_EN, GPT_ENABLE); |
| 182 | while(readl(GPT_COUNT_VAL) < msecs) ; |
| 183 | |
| 184 | writel(0, GPT_ENABLE); |
| 185 | writel(0, GPT_CLEAR); |
| 186 | } |
Chandan Uddaraju | 61e6d7c | 2010-07-20 17:57:06 -0700 | [diff] [blame] | 187 | |
| 188 | void udelay(unsigned usecs) |
| 189 | { |
| 190 | usecs = (usecs * 33 + 1000 - 33) / 1000; |
| 191 | |
| 192 | writel(0, GPT_CLEAR); |
| 193 | writel(0, GPT_ENABLE); |
| 194 | while(readl(GPT_COUNT_VAL) != 0); |
| 195 | |
| 196 | writel(GPT_ENABLE_EN, GPT_ENABLE); |
| 197 | while(readl(GPT_COUNT_VAL) < usecs); |
| 198 | |
| 199 | writel(0, GPT_ENABLE); |
| 200 | writel(0, GPT_CLEAR); |
| 201 | } |