carl9170: Update based on commit 467556acea56f361a21b2a3761ca056b9da2d237 dated Nov...
[linux-libre-firmware.git] / carl9170fw / carlfw / src / timer.c
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * timer code
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-2012  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 #include "carl9170.h"
26 #include "timer.h"
27 #include "gpio.h"
28 #include "printf.h"
29 #include "wl.h"
30
31 void timer_init(const unsigned int timer, const unsigned int interval)
32 {
33         /* Set timer to periodic mode */
34         orl(AR9170_TIMER_REG_CONTROL, BIT(timer));
35
36         /* Set time interval */
37         set(AR9170_TIMER_REG_TIMER0 + (timer << 2), interval - 1);
38
39         /* Clear timer interrupt flag */
40         orl(AR9170_TIMER_REG_INTERRUPT, BIT(timer));
41 }
42
43 void clock_set(enum cpu_clock_t clock_, bool on)
44 {
45         /*
46          * Word of Warning!
47          * This setting does more than just mess with the CPU Clock.
48          * So watch out, if you need _stable_ timer interrupts.
49          */
50 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
51         if (fw.phy.frequency < 3000000)
52                 set(AR9170_PWR_REG_PLL_ADDAC, 0x5163);
53         else
54                 set(AR9170_PWR_REG_PLL_ADDAC, 0x5143);
55 #else
56         set(AR9170_PWR_REG_PLL_ADDAC, 0x5163);
57 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
58
59         fw.ticks_per_usec = GET_VAL(AR9170_PWR_PLL_ADDAC_DIV,
60                 get(AR9170_PWR_REG_PLL_ADDAC));
61
62         set(AR9170_PWR_REG_CLOCK_SEL, (uint32_t) ((on ? 0x70 : 0x600) | clock_));
63
64         switch (clock_) {
65         case AHB_20_22MHZ:
66                 fw.ticks_per_usec >>= 1;
67                 /* fall through */
68         case AHB_40MHZ_OSC:
69                 /* fall through */
70         case AHB_40_44MHZ:
71                 fw.ticks_per_usec >>= 1;
72                 /* fall through */
73         case AHB_80_88MHZ:
74                 break;
75         }
76 }
77
78 static void timer0_isr(void)
79 {
80         wlan_timer();
81
82 #ifdef CONFIG_CARL9170FW_GPIO_INTERRUPT
83         gpio_timer();
84 #endif /* CONFIG_CARL9170FW_GPIO_INTERRUPT */
85
86 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
87         set(AR9170_GPIO_REG_PORT_DATA, get(AR9170_GPIO_REG_PORT_DATA) ^ 1);
88 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
89 }
90
91 void handle_timer(void)
92 {
93         uint32_t intr;
94
95         intr = get(AR9170_TIMER_REG_INTERRUPT);
96
97         /* ACK timer interrupt */
98         set(AR9170_TIMER_REG_INTERRUPT, intr);
99
100 #define HANDLER(intr, flag, func)                       \
101         do {                                            \
102                 if ((intr & flag) != 0) {               \
103                         intr &= ~flag;                  \
104                         func();                         \
105                 }                                       \
106         } while (0)
107
108         HANDLER(intr, BIT(0), timer0_isr);
109
110         if (intr)
111                 DBG("Unhandled Timer Event %x", (unsigned int) intr);
112
113 #undef HANDLER
114 }