blob: f52f2efdc8cb2641791d08ca6ca9adc40c167179 [file] [log] [blame]
Furquan Shaikh76570572014-03-19 14:29:48 -07001/*
Deepa Dinamani74aa7772014-07-14 16:26:11 -07002 * Copyright (c) 2011 - 2014 The Linux Foundation. All rights reserved.
Furquan Shaikh76570572014-03-19 14:29:48 -07003 *
Vadim Bendebury476f7312014-04-08 18:45:46 -07004 * 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 Shaikh76570572014-03-19 14:29:48 -070016 *
Vadim Bendebury476f7312014-04-08 18:45:46 -070017 * 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 Shaikh76570572014-03-19 14:29:48 -070029 */
30
Vadim Bendeburyf4b209f2014-04-09 19:23:04 -070031#include <delay.h>
Julius Werner73d1ed62014-10-20 13:20:49 -070032#include <soc/iomap.h>
33#include <soc/ipq_timer.h>
Vadim Bendeburyf4b209f2014-04-09 19:23:04 -070034#include <timer.h>
Vadim Bendebury476f7312014-04-08 18:45:46 -070035
Deepa Dinamani74aa7772014-07-14 16:26:11 -070036/*
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 Bendebury476f7312014-04-08 18:45:46 -070050
51/**
Marc Jones017287a2014-12-29 16:52:59 -070052 * init_timer - initialize timer
Vadim Bendebury476f7312014-04-08 18:45:46 -070053 */
Vadim Bendeburyf4b209f2014-04-09 19:23:04 -070054void init_timer(void)
Furquan Shaikh76570572014-03-19 14:29:48 -070055{
Deepa Dinamani74aa7772014-07-14 16:26:11 -070056 /* 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 Shaikh76570572014-03-19 14:29:48 -070069}
70
Vadim Bendebury476f7312014-04-08 18:45:46 -070071/**
Vadim Bendeburyf4b209f2014-04-09 19:23:04 -070072 * udelay - generates micro second delay.
Martin Roth5f066b22015-01-04 16:47:39 -070073 * @param usec: delay duration in microseconds
Vadim Bendebury476f7312014-04-08 18:45:46 -070074 */
Vadim Bendeburyf4b209f2014-04-09 19:23:04 -070075void udelay(unsigned usec)
Vadim Bendebury476f7312014-04-08 18:45:46 -070076{
Deepa Dinamani74aa7772014-07-14 16:26:11 -070077 uint32_t now;
78 uint32_t last;
79 uint32_t ticks;
80 uint32_t curr_ticks = 0;
Vadim Bendebury476f7312014-04-08 18:45:46 -070081
Deepa Dinamani74aa7772014-07-14 16:26:11 -070082 /* Calculate number of ticks required. */
83 ticks = TIMER_TICKS(usec);
Vadim Bendebury476f7312014-04-08 18:45:46 -070084
Deepa Dinamani74aa7772014-07-14 16:26:11 -070085 /* 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 Bendebury476f7312014-04-08 18:45:46 -070092 last = now;
Deepa Dinamani74aa7772014-07-14 16:26:11 -070093 }
Vadim Bendebury476f7312014-04-08 18:45:46 -070094}
95
Vadim Bendebury20d3d532014-05-01 15:24:32 -070096void timer_monotonic_get(struct mono_time *mt)
97{
Deepa Dinamani74aa7772014-07-14 16:26:11 -070098 mono_time_set_usecs(mt, TIMER_USECS(readl_i(DGT_COUNT_VAL)));
Vadim Bendebury20d3d532014-05-01 15:24:32 -070099}