Setting up repository
[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, 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 "timer.h"
28 #include "gpio.h"
29 #include "printf.h"
30 #include "wl.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 void clock_set(enum cpu_clock_t clock_, bool on)
45 {
46         /*
47          * Word of Warning!
48          * This setting does more than just mess with the CPU Clock.
49          * So watch out, if you need _stable_ timer interrupts.
50          */
51 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
52         if (fw.phy.frequency < 3000000)
53                 set(AR9170_PWR_REG_PLL_ADDAC, 0x5163);
54         else
55                 set(AR9170_PWR_REG_PLL_ADDAC, 0x5143);
56 #else
57         set(AR9170_PWR_REG_PLL_ADDAC, 0x5163);
58 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
59
60         fw.ticks_per_usec = GET_VAL(AR9170_PWR_PLL_ADDAC_DIV,
61                 get(AR9170_PWR_REG_PLL_ADDAC));
62
63         set(AR9170_PWR_REG_CLOCK_SEL, (uint32_t) ((on ? 0x70 : 0x600) | clock_));
64
65         switch (clock_) {
66         case AHB_20_22MHZ:
67                 fw.ticks_per_usec >>= 1;
68         case AHB_40MHZ_OSC:
69         case AHB_40_44MHZ:
70                 fw.ticks_per_usec >>= 1;
71         case AHB_80_88MHZ:
72                 break;
73         }
74 }
75
76 static void timer0_isr(void)
77 {
78         wlan_timer();
79
80 #ifdef CONFIG_CARL9170FW_GPIO_INTERRUPT
81         gpio_timer();
82 #endif /* CONFIG_CARL9170FW_GPIO_INTERRUPT */
83
84 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
85         set(AR9170_GPIO_REG_PORT_DATA, get(AR9170_GPIO_REG_PORT_DATA) ^ 1);
86 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
87 }
88
89 void handle_timer(void)
90 {
91         uint32_t intr;
92
93         intr = get(AR9170_TIMER_REG_INTERRUPT);
94
95         /* ACK timer interrupt */
96         set(AR9170_TIMER_REG_INTERRUPT, intr);
97
98 #define HANDLER(intr, flag, func)                       \
99         do {                                            \
100                 if ((intr & flag) != 0) {               \
101                         intr &= ~flag;                  \
102                         func();                         \
103                 }                                       \
104         } while (0)
105
106         HANDLER(intr, BIT(0), timer0_isr);
107
108         if (intr)
109                 DBG("Unhandled Timer Event %x", (unsigned int) intr);
110
111 #undef HANDLER
112 }