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. |
Daisuke Nojiri | fcfd989 | 2015-02-04 17:23:53 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <arch/io.h> |
| 17 | #include <delay.h> |
| 18 | #include <soc/addressmap.h> |
| 19 | #include <timer.h> |
| 20 | |
| 21 | #define TIMER_GLB_TIM_CTRL_PRESC_MASK 0x0000FF00 |
| 22 | #define TIMER_GLB_TIM_CTRL_TIM_EN 0x00000001 |
| 23 | #define TIMER_GLB_TIM_CTRL_PRESC 0x0 |
| 24 | /* |
| 25 | * arm clk is 1GHz, periph_clk=arm_clk/2, tick per usec. |
| 26 | * arm clk is set by the bootrom. See util/broadcom/unauth.cfg for details. |
| 27 | */ |
| 28 | #define PERIPH_CLOCK 500 |
| 29 | #define CLOCKS_PER_USEC (PERIPH_CLOCK / \ |
| 30 | (((TIMER_GLB_TIM_CTRL_PRESC & TIMER_GLB_TIM_CTRL_PRESC_MASK) >> 8) + 1)) |
| 31 | |
| 32 | struct cygnus_timer { |
| 33 | u32 gtim_glob_low; |
| 34 | u32 gtim_glob_hi; |
| 35 | u32 gtim_glob_ctrl; |
| 36 | }; |
| 37 | |
| 38 | static struct cygnus_timer * const timer_ptr = |
| 39 | (void *)IPROC_PERIPH_GLB_TIM_REG_BASE; |
| 40 | |
| 41 | static inline uint64_t timer_raw_value(void) |
| 42 | { |
| 43 | uint64_t cur_tick; |
| 44 | uint32_t count_h; |
| 45 | uint32_t count_l; |
| 46 | |
| 47 | do { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 48 | count_h = read32(&timer_ptr->gtim_glob_hi); |
| 49 | count_l = read32(&timer_ptr->gtim_glob_low); |
| 50 | cur_tick = read32(&timer_ptr->gtim_glob_hi); |
Daisuke Nojiri | fcfd989 | 2015-02-04 17:23:53 -0800 | [diff] [blame] | 51 | } while (cur_tick != count_h); |
| 52 | |
| 53 | return (cur_tick << 32) + count_l; |
| 54 | } |
| 55 | |
| 56 | void timer_monotonic_get(struct mono_time *mt) |
| 57 | { |
| 58 | mono_time_set_usecs(mt, timer_raw_value() / CLOCKS_PER_USEC); |
| 59 | } |
| 60 | |
| 61 | void init_timer(void) |
| 62 | { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 63 | write32(&timer_ptr->gtim_glob_ctrl, TIMER_GLB_TIM_CTRL_PRESC); |
| 64 | write32(&timer_ptr->gtim_glob_low, 0); |
| 65 | write32(&timer_ptr->gtim_glob_hi, 0); |
| 66 | write32(&timer_ptr->gtim_glob_ctrl, TIMER_GLB_TIM_CTRL_TIM_EN); |
Daisuke Nojiri | fcfd989 | 2015-02-04 17:23:53 -0800 | [diff] [blame] | 67 | } |