carl9170 firmware: add radar pattern generator
[carl9170fw.git] / carlfw / include / timer.h
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * Clock, Timer & Timing
5  *
6  * Copyright (c) 2000-2005 ZyDAS Technology Corporation
7  * Copyright (c) 2007-2009 Atheros Communications, Inc.
8  * Copyright    2009    Johannes Berg <johannes@sipsolutions.net>
9  * Copyright 2009-2011  Christian Lamparter <chunkeey@googlemail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; If not, see <http://www.gnu.org/licenses/>.
23  */
24
25 #ifndef __CARL9170FW_TIMER_H
26 #define __CARL9170FW_TIMER_H
27
28 #include "config.h"
29
30 enum cpu_clock_t {
31         AHB_40MHZ_OSC   = 0,
32         AHB_20_22MHZ    = 1,
33         AHB_40_44MHZ    = 2,
34         AHB_80_88MHZ    = 3
35 };
36
37 static inline __inline uint32_t get_clock_counter(void)
38 {
39         return (get(AR9170_TIMER_REG_CLOCK_HIGH) << 16) | get(AR9170_TIMER_REG_CLOCK_LOW);
40 }
41
42 /*
43  * works only up to 97 secs [44 MHz] or 107 secs for 40 MHz
44  * Also, the delay wait will be affected by 2.4GHz<->5GHz
45  * band changes.
46  */
47 static inline __inline bool is_after_msecs(const uint32_t t0, const uint32_t msecs)
48 {
49         return ((get_clock_counter() - t0) / 1000) > (msecs * fw.ticks_per_usec);
50 }
51
52 static inline __inline bool is_after_usecs(const uint32_t t0, const uint32_t usecs)
53 {
54         return ((get_clock_counter() - t0)) > (usecs * fw.ticks_per_usec);
55 }
56
57 /*
58  * Note: Be careful with [u]delay. They won't service the
59  * hardware watchdog timer. It might trigger if you
60  * wait long enough. Also they don't terminate if sec is
61  * above 97 sec [44MHz] or more than 107 sec [40MHz].
62  */
63 static inline __inline void delay(const uint32_t msec)
64 {
65         uint32_t t1, t2, dt, wt;
66
67         wt = msec * fw.ticks_per_usec;
68
69         t1 = get_clock_counter();
70         while (1) {
71                 t2 = get_clock_counter();
72                 dt = (t2 - t1) / 1000;
73                 if (dt >= wt)
74                         break;
75         }
76 }
77
78 static inline __inline void udelay(const uint32_t usec)
79 {
80         uint32_t t1, t2, dt;
81
82         t1 = get_clock_counter();
83         while (1) {
84                 t2 = get_clock_counter();
85                 dt = (t2 - t1);
86                 if (dt >= (usec * fw.ticks_per_usec))
87                         break;
88         }
89 }
90
91 void clock_set(enum cpu_clock_t _clock, bool on);
92 void handle_timer(void);
93 void timer_init(const unsigned int timer, const unsigned int interval);
94
95 #endif /* __CARL9170FW_TIMER_H */