carl9170 firmware: import 1.7.0
[carl9170fw.git] / carlfw / src / timer.c
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * Clock, Timer & Timing functions
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 #include "carl9170.h"
27 #include "printf.h"
28 #include "timer.h"
29 #include "wl.h"
30 #include "gpio.h"
31
32 void timer_init(const unsigned int timer, const unsigned int interval)
33 {
34         /* Set timer to periodic mode */
35         orl(AR9170_TIMER_REG_CONTROL, BIT(timer));
36
37         /* Set time interval */
38         set(AR9170_TIMER_REG_TIMER0 + (timer << 2), interval - 1);
39
40         /* Clear timer interrupt flag */
41         orl(AR9170_TIMER_REG_INTERRUPT, BIT(timer));
42 }
43
44 static void clock_calibrate(void)
45 {
46         uint32_t t0, loop = 13;
47
48         t0 = get_clock_counter();
49
50         /*
51          * TODO:
52          * Write this code in assembler, so the reading is accurate
53          * and can be used to correct the timer intervals.
54          */
55         while (((get_clock_counter() - t0) & (BIT(18)-1)) < 1000)
56                 loop += 9;      /* really rough uOP estimation */
57
58         fw.bogoclock = loop;
59 }
60
61 void clock_set(const bool on, const enum cpu_clock_t _clock)
62 {
63         /*
64          * Word of Warning!
65          * This setting does more than just mess with the CPU Clock.
66          * So watch out, if you need _stable_ timer interrupts.
67          */
68
69         set(AR9170_PWR_REG_CLOCK_SEL, (uint32_t) ((on ? 0x70 : 0x600) | _clock));
70         clock_calibrate();
71 }
72
73 static void timer0_isr(void)
74 {
75         wlan_timer();
76
77 #ifdef CONFIG_CARL9170FW_GPIO_INTERRUPT
78         gpio_timer();
79 #endif /* CONFIG_CARL9170FW_GPIO_INTERRUPT */
80
81 #ifdef CONFIG_CARL9170FW_USB_WATCHDOG
82         usb_watchdog_timer();
83 #endif /* CONFIG_CARL9170FW_USB_WATCHDOG */
84
85 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
86         set(AR9170_GPIO_REG_PORT_DATA, get(AR9170_GPIO_REG_PORT_DATA) ^ 1);
87 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
88 }
89
90 void handle_timer(void)
91 {
92         uint32_t intr;
93
94         intr = get(AR9170_TIMER_REG_INTERRUPT);
95
96         /* ACK timer interrupt */
97         set(AR9170_TIMER_REG_INTERRUPT, intr);
98
99 #define HANDLER(intr, flag, func)                       \
100         do {                                            \
101                 if ((intr & flag) != 0) {               \
102                         intr &= ~flag;                  \
103                         func();                         \
104                 }                                       \
105         } while (0)
106
107         HANDLER(intr, BIT(0), timer0_isr);
108
109         if (intr)
110                 DBG("Unhandled Timer Event %x", (unsigned int) intr);
111
112 #undef HANDLER
113 }