a4d24b60b69bf56bb3851d17fd2a2414bcaef4d4
[carl9170fw.git] / carlfw / src / main.c
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * initialization and main() loop
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, 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 "hostif.h"
29 #include "printf.h"
30 #include "gpio.h"
31 #include "wl.h"
32 #include "rf.h"
33 #include "usb.h"
34
35 #define AR9170_WATCH_DOG_TIMER             0x100
36
37 static void timer_init(const unsigned int timer, const unsigned int interval)
38 {
39         /* Set timer to periodic mode */
40         orl(AR9170_TIMER_REG_CONTROL, BIT(timer));
41
42         /* Set time interval */
43         set(AR9170_TIMER_REG_TIMER0 + (timer << 2), interval - 1);
44
45         /* Clear timer interrupt flag */
46         orl(AR9170_TIMER_REG_INTERRUPT, BIT(timer));
47 }
48
49 void clock_set(enum cpu_clock_t clock_, bool on)
50 {
51         /*
52          * Word of Warning!
53          * This setting does more than just mess with the CPU Clock.
54          * So watch out, if you need _stable_ timer interrupts.
55          */
56 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
57         if (fw.phy.frequency < 3000000)
58                 set(AR9170_PWR_REG_PLL_ADDAC, 0x5163);
59         else
60                 set(AR9170_PWR_REG_PLL_ADDAC, 0x5143);
61 #else
62         set(AR9170_PWR_REG_PLL_ADDAC, 0x5163);
63 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
64
65         fw.ticks_per_usec = GET_VAL(AR9170_PWR_PLL_ADDAC_DIV,
66                 get(AR9170_PWR_REG_PLL_ADDAC));
67
68         set(AR9170_PWR_REG_CLOCK_SEL, (uint32_t) ((on ? 0x70 : 0x600) | clock_));
69
70         switch (clock_) {
71         case AHB_20_22MHZ:
72                 fw.ticks_per_usec >>= 1;
73         case AHB_40MHZ_OSC:
74         case AHB_40_44MHZ:
75                 fw.ticks_per_usec >>= 1;
76         case AHB_80_88MHZ:
77                 break;
78         }
79 }
80
81 static void init(void)
82 {
83         led_init();
84
85 #ifdef CONFIG_CARL9170FW_DEBUG_UART
86         uart_init();
87 #endif /* CONFIG_CARL9170FW_DEBUG_UART */
88
89         /* 25/50/100ms timer (depends on cpu clock) */
90         timer_init(0, 50000);
91
92         /* USB init */
93         usb_init();
94
95         /* initialize DMA memory */
96         memset(&dma_mem, 0, sizeof(dma_mem));
97
98         /* fill DMA rings */
99         dma_init_descriptors();
100
101         /* clear all interrupt */
102         set(AR9170_MAC_REG_INT_CTRL, 0xffff);
103
104         orl(AR9170_MAC_REG_AFTER_PNP, 1);
105
106         /* Init watch dog control flag */
107         fw.watchdog_enable = 1;
108
109         set(AR9170_TIMER_REG_WATCH_DOG, AR9170_WATCH_DOG_TIMER);
110
111 #ifdef CONFIG_CARL9170FW_GPIO_INTERRUPT
112         fw.cached_gpio_state.gpio = get(AR9170_GPIO_REG_PORT_DATA) &
113                                     CARL9170_GPIO_MASK;
114 #endif /* CONFIG_CARL9170FW_GPIO_INTERRUPT */
115
116         /* this will get the downqueue moving. */
117         down_trigger();
118 }
119
120 static void handle_fw(void)
121 {
122         if (fw.watchdog_enable == 1)
123                 set(AR9170_TIMER_REG_WATCH_DOG, AR9170_WATCH_DOG_TIMER);
124
125         if (fw.reboot)
126                 reboot();
127 }
128
129 static void timer0_isr(void)
130 {
131         wlan_timer();
132
133 #ifdef CONFIG_CARL9170FW_GPIO_INTERRUPT
134         gpio_timer();
135 #endif /* CONFIG_CARL9170FW_GPIO_INTERRUPT */
136
137 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
138         set(AR9170_GPIO_REG_PORT_DATA, get(AR9170_GPIO_REG_PORT_DATA) ^ 1);
139 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
140 }
141
142 static void handle_timer(void)
143 {
144         uint32_t intr;
145
146         intr = get(AR9170_TIMER_REG_INTERRUPT);
147
148         /* ACK timer interrupt */
149         set(AR9170_TIMER_REG_INTERRUPT, intr);
150
151 #define HANDLER(intr, flag, func)                       \
152         do {                                            \
153                 if ((intr & flag) != 0) {               \
154                         intr &= ~flag;                  \
155                         func();                         \
156                 }                                       \
157         } while (0)
158
159         HANDLER(intr, BIT(0), timer0_isr);
160
161         if (intr)
162                 DBG("Unhandled Timer Event %x", (unsigned int) intr);
163
164 #undef HANDLER
165 }
166
167 static void tally_update(void)
168 {
169         unsigned int boff, time, delta;
170
171         time = get_clock_counter();
172         if (fw.phy.state == CARL9170_PHY_ON) {
173                 delta = (time - fw.tally_clock);
174
175                 fw.tally.active += delta;
176
177                 boff = get(AR9170_MAC_REG_BACKOFF_STATUS);
178                 if (boff & AR9170_MAC_BACKOFF_TX_PE)
179                         fw.tally.tx_time += delta;
180                 if (boff & AR9170_MAC_BACKOFF_CCA)
181                         fw.tally.cca += delta;
182         }
183
184         fw.tally_clock = time;
185         fw.counter++;
186 }
187
188 static void __noreturn main_loop(void)
189 {
190         /* main loop */
191         while (1) {
192                 handle_fw();
193
194                 /*
195                  * Due to frame order persevation, the wlan subroutines
196                  * must be executed before handle_host_interface.
197                  */
198                 handle_wlan();
199
200                 handle_host_interface();
201
202                 handle_usb();
203
204                 handle_timer();
205
206                 tally_update();
207         }
208 }
209
210 /*
211  * The bootcode will work with the device driver to load the firmware
212  * onto the device's Program SRAM. The Program SRAM has a size of 16 KB
213  * and also contains the stack, which grows down from 0x204000.
214  *
215  * The Program SRAM starts at address 0x200000 on the device.
216  * The firmware entry point (0x200004) is located in boot.S.
217  * we put _start() there with the linker script carl9170.lds.
218  */
219
220 void __section(boot) start(void)
221 {
222         clock_set(AHB_40MHZ_OSC, true);
223
224         /* watchdog magic pattern check */
225         if ((get(AR9170_PWR_REG_WATCH_DOG_MAGIC) & 0xffff0000) == 0x12340000) {
226                 /* watch dog warm start */
227                 incl(AR9170_PWR_REG_WATCH_DOG_MAGIC);
228                 usb_trigger_out();
229         } else if ((get(AR9170_PWR_REG_WATCH_DOG_MAGIC) & 0xffff0000) == 0x98760000) {
230                 /* suspend/resume */
231         }
232
233         /* write the magic pattern for watch dog */
234         andl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0xFFFF);
235         orl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0x12340000);
236
237         init();
238
239 #ifdef CONFIG_CARL9170FW_DEBUG
240
241         BUG("TEST BUG");
242         BUG_ON(0x2b || !0x2b);
243         INFO("INFO MESSAGE");
244
245         /* a set of unique characters to detect transfer data corruptions */
246         DBG("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
247             " ~`!1@2#3$4%%5^6&7*8(9)0_-+={[}]|\\:;\"'<,>.?/");
248 #endif /* CONFIG_CARL9170FW_DEBUG */
249
250         /*
251          * Tell the host, that the firmware has booted and is
252          * now ready to process requests.
253          */
254         send_cmd_to_host(0, CARL9170_RSP_BOOT, 0x00, NULL);
255         main_loop();
256 }