carl9170 firmware: privatize firmware structs
[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, 2010 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, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #ifndef __CARL9170FW_TIMER_H
27 #define __CARL9170FW_TIMER_H
28
29 #include "config.h"
30
31 enum cpu_clock_t {
32         AHB_40MHZ_OSC   = 0,
33         AHB_20_22MHZ    = 1,
34         AHB_40_44MHZ    = 2,
35         AHB_80_88MHZ    = 3
36 };
37
38 #define AR9170_TICKS_PER_MICROSECOND    80
39
40 static inline __inline uint32_t get_clock_counter(void)
41 {
42         return (get(AR9170_TIMER_REG_CLOCK_HIGH) << 16) | get(AR9170_TIMER_REG_CLOCK_LOW);
43 }
44
45 static inline __inline bool is_after_msecs(uint32_t t0, uint32_t msecs)
46 {
47         return (get_clock_counter() - t0) / (AR9170_TICKS_PER_MICROSECOND * 1000) > msecs;
48 }
49
50 static inline __inline void delay(uint32_t msec)
51 {
52         uint32_t t1, t2, dt;
53
54         t1 = get_clock_counter();
55         while (1) {
56                 t2 = get_clock_counter();
57                 dt = (t2 - t1) / AR9170_TICKS_PER_MICROSECOND / 1000;
58                 if (dt >= msec)
59                         break;
60         }
61 }
62
63 static inline __inline void udelay(uint32_t usec)
64 {
65         uint32_t t1, t2, dt;
66
67         t1 = get_clock_counter();
68         while (1) {
69                 t2 = get_clock_counter();
70                 dt = (t2 - t1) / AR9170_TICKS_PER_MICROSECOND;
71                 if (dt >= usec)
72                         break;
73         }
74 }
75
76 static inline void clock_set(const bool on, const enum cpu_clock_t _clock)
77 {
78         /*
79          * Word of Warning!
80          * This setting does more than just mess with the CPU Clock.
81          * So watch out, if you need _stable_ timer interrupts.
82          */
83
84         set(AR9170_PWR_REG_CLOCK_SEL, (uint32_t) ((on ? 0x70 : 0x600) | _clock));
85 }
86
87 #endif /* __CARL9170FW_TIMER_H */