Daisuke Nojiri | fcfd989 | 2015-02-04 17:23:53 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2015 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <arch/io.h> |
| 21 | #include <delay.h> |
| 22 | #include <soc/addressmap.h> |
| 23 | #include <timer.h> |
| 24 | |
| 25 | #define TIMER_GLB_TIM_CTRL_PRESC_MASK 0x0000FF00 |
| 26 | #define TIMER_GLB_TIM_CTRL_TIM_EN 0x00000001 |
| 27 | #define TIMER_GLB_TIM_CTRL_PRESC 0x0 |
| 28 | /* |
| 29 | * arm clk is 1GHz, periph_clk=arm_clk/2, tick per usec. |
| 30 | * arm clk is set by the bootrom. See util/broadcom/unauth.cfg for details. |
| 31 | */ |
| 32 | #define PERIPH_CLOCK 500 |
| 33 | #define CLOCKS_PER_USEC (PERIPH_CLOCK / \ |
| 34 | (((TIMER_GLB_TIM_CTRL_PRESC & TIMER_GLB_TIM_CTRL_PRESC_MASK) >> 8) + 1)) |
| 35 | |
| 36 | struct cygnus_timer { |
| 37 | u32 gtim_glob_low; |
| 38 | u32 gtim_glob_hi; |
| 39 | u32 gtim_glob_ctrl; |
| 40 | }; |
| 41 | |
| 42 | static struct cygnus_timer * const timer_ptr = |
| 43 | (void *)IPROC_PERIPH_GLB_TIM_REG_BASE; |
| 44 | |
| 45 | static inline uint64_t timer_raw_value(void) |
| 46 | { |
| 47 | uint64_t cur_tick; |
| 48 | uint32_t count_h; |
| 49 | uint32_t count_l; |
| 50 | |
| 51 | do { |
| 52 | count_h = readl(&timer_ptr->gtim_glob_hi); |
| 53 | count_l = readl(&timer_ptr->gtim_glob_low); |
| 54 | cur_tick = readl(&timer_ptr->gtim_glob_hi); |
| 55 | } while (cur_tick != count_h); |
| 56 | |
| 57 | return (cur_tick << 32) + count_l; |
| 58 | } |
| 59 | |
| 60 | void timer_monotonic_get(struct mono_time *mt) |
| 61 | { |
| 62 | mono_time_set_usecs(mt, timer_raw_value() / CLOCKS_PER_USEC); |
| 63 | } |
| 64 | |
| 65 | void init_timer(void) |
| 66 | { |
| 67 | writel(TIMER_GLB_TIM_CTRL_PRESC, &timer_ptr->gtim_glob_ctrl); |
| 68 | writel(0, &timer_ptr->gtim_glob_low); |
| 69 | writel(0, &timer_ptr->gtim_glob_hi); |
| 70 | writel(TIMER_GLB_TIM_CTRL_TIM_EN, &timer_ptr->gtim_glob_ctrl); |
| 71 | } |