2 * (C) Copyright 2009 Faraday Technology
3 * Po-Yu Chuang <ratbert@faraday-tech.com>
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <faraday/ftpmu010.h>
12 #include <faraday/fttmr010.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 #define TIMER_CLOCK 32768
17 #define TIMER_LOAD_VAL 0xffffffff
19 static inline unsigned long long tick_to_time(unsigned long long tick)
21 tick *= CONFIG_SYS_HZ;
22 do_div(tick, gd->arch.timer_rate_hz);
27 static inline unsigned long long usec_to_tick(unsigned long long usec)
29 usec *= gd->arch.timer_rate_hz;
30 do_div(usec, 1000000);
37 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
40 debug("%s()\n", __func__);
45 /* use 32768Hz oscillator for RTC, WDT, TIMER */
46 ftpmu010_32768osc_enable();
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);
54 /* we don't want timer to issue interrupts */
55 writel(FTTMR010_TM3_MATCH1 |
57 FTTMR010_TM3_OVERFLOW,
58 &tmr->interrupt_mask);
61 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
62 cr |= FTTMR010_TM3_ENABLE;
65 gd->arch.timer_rate_hz = TIMER_CLOCK;
66 gd->arch.tbu = gd->arch.tbl = 0;
72 * Get the current 64 bit timer tick count
74 unsigned long long get_ticks(void)
76 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
77 ulong now = TIMER_LOAD_VAL - readl(&tmr->timer3_counter);
79 /* increment tbu if tbl has rolled over */
80 if (now < gd->arch.tbl)
83 return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
86 void __udelay(unsigned long usec)
88 unsigned long long start;
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 */
98 * get_timer(base) can be used to check for timeouts or
99 * to measure elasped time relative to an event:
101 * ulong start_time = get_timer(0) sets start_time to the current
103 * get_timer(start_time) returns the time elapsed since then.
105 * The time is used in CONFIG_SYS_HZ units!
107 ulong get_timer(ulong base)
109 return tick_to_time(get_ticks()) - base;
113 * Return the number of timer ticks per second.
115 ulong get_tbclk(void)
117 return gd->arch.timer_rate_hz;