blob: ac26f407b07af68348feb7d361836b6078d285de [file] [log] [blame]
Gabe Blackd4d29a12014-04-10 02:36:49 -07001/*
Julius Werner4fda9bd2016-05-16 15:39:12 -07002 * This file is part of the libpayload project.
3 *
4 * Copyright 2016 Google Inc.
Gabe Blackd4d29a12014-04-10 02:36:49 -07005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
Julius Werner4fda9bd2016-05-16 15:39:12 -070017 * Alternatively, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2 as published by the Free
19 * Software Foundation.
20 *
Gabe Blackd4d29a12014-04-10 02:36:49 -070021 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
Julius Werner4fda9bd2016-05-16 15:39:12 -070034#include <assert.h>
Gabe Blackd4d29a12014-04-10 02:36:49 -070035#include <libpayload.h>
Gabe Blackd4d29a12014-04-10 02:36:49 -070036
37uint64_t timer_hz(void)
38{
Julius Werner4fda9bd2016-05-16 15:39:12 -070039 /* libc/time.c currently requires all timers to be at least 1MHz. */
40 assert(CONFIG_LP_TIMER_GENERIC_HZ >= 1000000);
41 return CONFIG_LP_TIMER_GENERIC_HZ;
Gabe Blackd4d29a12014-04-10 02:36:49 -070042}
43
44uint64_t timer_raw_value(void)
45{
Julius Werner4fda9bd2016-05-16 15:39:12 -070046 uint64_t cur_tick;
47 uint32_t count_h;
48 uint32_t count_l;
49
50 if (!CONFIG_LP_TIMER_GENERIC_HIGH_REG)
51 return readl(phys_to_virt(CONFIG_LP_TIMER_GENERIC_REG));
52
53 do {
54 count_h = readl(phys_to_virt(CONFIG_LP_TIMER_GENERIC_HIGH_REG));
55 count_l = readl(phys_to_virt(CONFIG_LP_TIMER_GENERIC_REG));
Paul Kocialkowski35562d82016-12-19 19:22:39 +010056 cur_tick = readl(phys_to_virt(CONFIG_LP_TIMER_GENERIC_HIGH_REG));
Julius Werner4fda9bd2016-05-16 15:39:12 -070057 } while (cur_tick != count_h);
58
59 return (cur_tick << 32) + count_l;
Gabe Blackd4d29a12014-04-10 02:36:49 -070060}