blob: 1ac5b6012933b61a271a8e23c77c7c027abee543 [file] [log] [blame]
Po-Yu Chuang43a5f0d2009-11-11 17:27:30 +08001/*
2 * (C) Copyright 2009 Faraday Technology
3 * Po-Yu Chuang <ratbert@faraday-tech.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Po-Yu Chuang43a5f0d2009-11-11 17:27:30 +08006 */
7
8#include <common.h>
Po-Yu Chuang60d1ea92011-08-10 17:44:21 +00009#include <div64.h>
Po-Yu Chuang43a5f0d2009-11-11 17:27:30 +080010#include <asm/io.h>
Po-Yu Chuang8dc667c2011-02-17 19:35:23 +000011#include <faraday/ftpmu010.h>
Macpaul Lin844c05a2011-03-21 01:45:43 +000012#include <faraday/fttmr010.h>
Po-Yu Chuang43a5f0d2009-11-11 17:27:30 +080013
Po-Yu Chuang60d1ea92011-08-10 17:44:21 +000014DECLARE_GLOBAL_DATA_PTR;
Po-Yu Chuang43a5f0d2009-11-11 17:27:30 +080015
16#define TIMER_CLOCK 32768
17#define TIMER_LOAD_VAL 0xffffffff
18
Po-Yu Chuang60d1ea92011-08-10 17:44:21 +000019static inline unsigned long long tick_to_time(unsigned long long tick)
20{
21 tick *= CONFIG_SYS_HZ;
Simon Glassb3390512012-12-13 20:48:32 +000022 do_div(tick, gd->arch.timer_rate_hz);
Po-Yu Chuang60d1ea92011-08-10 17:44:21 +000023
24 return tick;
25}
26
27static inline unsigned long long usec_to_tick(unsigned long long usec)
28{
Simon Glassb3390512012-12-13 20:48:32 +000029 usec *= gd->arch.timer_rate_hz;
Po-Yu Chuang60d1ea92011-08-10 17:44:21 +000030 do_div(usec, 1000000);
31
32 return usec;
33}
34
Po-Yu Chuang43a5f0d2009-11-11 17:27:30 +080035int timer_init(void)
36{
Po-Yu Chuang60d1ea92011-08-10 17:44:21 +000037 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Po-Yu Chuang43a5f0d2009-11-11 17:27:30 +080038 unsigned int cr;
39
40 debug("%s()\n", __func__);
41
42 /* disable timers */
43 writel(0, &tmr->cr);
44
Po-Yu Chuang8dc667c2011-02-17 19:35:23 +000045 /* use 32768Hz oscillator for RTC, WDT, TIMER */
46 ftpmu010_32768osc_enable();
Po-Yu Chuang43a5f0d2009-11-11 17:27:30 +080047
48 /* setup timer */
49 writel(TIMER_LOAD_VAL, &tmr->timer3_load);
50 writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
51 writel(0, &tmr->timer3_match1);
52 writel(0, &tmr->timer3_match2);
53
54 /* we don't want timer to issue interrupts */
55 writel(FTTMR010_TM3_MATCH1 |
56 FTTMR010_TM3_MATCH2 |
57 FTTMR010_TM3_OVERFLOW,
58 &tmr->interrupt_mask);
59
60 cr = readl(&tmr->cr);
61 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
62 cr |= FTTMR010_TM3_ENABLE;
63 writel(cr, &tmr->cr);
64
Simon Glassb3390512012-12-13 20:48:32 +000065 gd->arch.timer_rate_hz = TIMER_CLOCK;
Simon Glass66ee6922012-12-13 20:48:34 +000066 gd->arch.tbu = gd->arch.tbl = 0;
Po-Yu Chuang43a5f0d2009-11-11 17:27:30 +080067
68 return 0;
69}
70
71/*
Po-Yu Chuang60d1ea92011-08-10 17:44:21 +000072 * Get the current 64 bit timer tick count
Po-Yu Chuang43a5f0d2009-11-11 17:27:30 +080073 */
74unsigned long long get_ticks(void)
75{
Po-Yu Chuang60d1ea92011-08-10 17:44:21 +000076 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
77 ulong now = TIMER_LOAD_VAL - readl(&tmr->timer3_counter);
78
79 /* increment tbu if tbl has rolled over */
Simon Glass66ee6922012-12-13 20:48:34 +000080 if (now < gd->arch.tbl)
Simon Glass8ff43b02012-12-13 20:48:33 +000081 gd->arch.tbu++;
Simon Glass66ee6922012-12-13 20:48:34 +000082 gd->arch.tbl = now;
83 return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
Po-Yu Chuang60d1ea92011-08-10 17:44:21 +000084}
85
86void __udelay(unsigned long usec)
87{
88 unsigned long long start;
89 ulong tmo;
90
91 start = get_ticks(); /* get current timestamp */
92 tmo = usec_to_tick(usec); /* convert usecs to ticks */
93 while ((get_ticks() - start) < tmo)
94 ; /* loop till time has passed */
Po-Yu Chuang43a5f0d2009-11-11 17:27:30 +080095}
96
97/*
Po-Yu Chuang60d1ea92011-08-10 17:44:21 +000098 * get_timer(base) can be used to check for timeouts or
99 * to measure elasped time relative to an event:
100 *
101 * ulong start_time = get_timer(0) sets start_time to the current
102 * time value.
103 * get_timer(start_time) returns the time elapsed since then.
104 *
105 * The time is used in CONFIG_SYS_HZ units!
106 */
107ulong get_timer(ulong base)
108{
109 return tick_to_time(get_ticks()) - base;
110}
111
112/*
113 * Return the number of timer ticks per second.
Po-Yu Chuang43a5f0d2009-11-11 17:27:30 +0800114 */
115ulong get_tbclk(void)
116{
Simon Glassb3390512012-12-13 20:48:32 +0000117 return gd->arch.timer_rate_hz;
Po-Yu Chuang43a5f0d2009-11-11 17:27:30 +0800118}