Merge branch 'master' into radar
[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 #include "radar.h"
35
36 #define AR9170_WATCH_DOG_TIMER             0x100
37
38 static void init(void)
39 {
40         led_init();
41
42 #ifdef CONFIG_CARL9170FW_DEBUG_UART
43         uart_init();
44 #endif /* CONFIG_CARL9170FW_DEBUG_UART */
45
46         /* 25/50/100ms timer (depends on cpu clock) */
47         timer_init(0, 50000);
48
49         /* USB init */
50         usb_init();
51
52         /* initialize DMA memory */
53         memset(&dma_mem, 0, sizeof(dma_mem));
54
55         /* fill DMA rings */
56         dma_init_descriptors();
57
58         /* clear all interrupt */
59         set(AR9170_MAC_REG_INT_CTRL, 0xffff);
60
61         orl(AR9170_MAC_REG_AFTER_PNP, 1);
62
63         /* Init watch dog control flag */
64         fw.watchdog_enable = 1;
65
66         set(AR9170_TIMER_REG_WATCH_DOG, AR9170_WATCH_DOG_TIMER);
67
68 #ifdef CONFIG_CARL9170FW_GPIO_INTERRUPT
69         fw.cached_gpio_state.gpio = get(AR9170_GPIO_REG_PORT_DATA) &
70                                     CARL9170_GPIO_MASK;
71 #endif /* CONFIG_CARL9170FW_GPIO_INTERRUPT */
72
73         /* this will get the downqueue moving. */
74         down_trigger();
75 }
76
77 static void handle_fw(void)
78 {
79         if (fw.watchdog_enable == 1)
80                 set(AR9170_TIMER_REG_WATCH_DOG, AR9170_WATCH_DOG_TIMER);
81
82         if (fw.reboot)
83                 reboot();
84 }
85
86 static void tally_update(void)
87 {
88         unsigned int time;
89
90         time = get_clock_counter();
91 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
92         if (fw.phy.state == CARL9170_PHY_ON) {
93                 unsigned int boff, delta;
94
95                 delta = (time - fw.tally_clock);
96
97                 fw.tally.active += delta;
98
99                 boff = get(AR9170_MAC_REG_BACKOFF_STATUS);
100                 if (boff & AR9170_MAC_BACKOFF_TX_PE)
101                         fw.tally.tx_time += delta;
102                 if (boff & AR9170_MAC_BACKOFF_CCA)
103                         fw.tally.cca += delta;
104         }
105 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
106         fw.tally_clock = time;
107         fw.counter++;
108 }
109
110 #ifdef CONFIG_CARL9170FW_RADAR
111 static void radar_pattern_generator(void)
112 {
113         if (fw.phy.state == CARL9170_PHY_ON) {
114                 if (fw.wlan.soft_radar == NO_RADAR ||
115                     fw.wlan.soft_radar >= __CARL9170FW_NUM_RADARS)
116                         return;
117
118                 const struct radar_info *radar = &radars[fw.wlan.soft_radar];
119                 if (radar->pulses >= fw.wlan.pattern_index) {
120                         fw.wlan.pattern_index = 0;
121                 }
122
123                 if (radar->pulses > fw.wlan.pattern_index) {
124                         const struct radar_info_pattern *pattern = &radar->pattern[fw.wlan.pattern_index];
125                         if (is_after_usecs(fw.wlan.radar_last, pattern->pulse_interval)) {
126                                 fw.wlan.radar_last = get_clock_counter();
127                                 set(0x1C3BC0, pattern->pulse_pattern);
128                                 set(0x1C3BBC, pattern->pulse_mode);
129                                 udelay(pattern->pulse_width);
130                                 set(0x1C3BBC, ~pattern->pulse_mode);
131                                 fw.wlan.pattern_index++;
132                         }
133                 }
134         }
135 }
136 #else
137 static void radar_pattern_generator(void)
138 {
139 }
140 #endif /* CONFIG_CARL9170FW_RADAR */
141
142 static void __noreturn main_loop(void)
143 {
144         /* main loop */
145         while (1) {
146                 handle_fw();
147
148                 /*
149                  * Due to frame order persevation, the wlan subroutines
150                  * must be executed before handle_host_interface.
151                  */
152                 handle_wlan();
153
154                 handle_host_interface();
155
156                 handle_usb();
157
158                 handle_timer();
159
160                 tally_update();
161
162                 radar_pattern_generator();
163         }
164 }
165
166 /*
167  * The bootcode will work with the device driver to load the firmware
168  * onto the device's Program SRAM. The Program SRAM has a size of 16 KB
169  * and also contains the stack, which grows down from 0x204000.
170  *
171  * The Program SRAM starts at address 0x200000 on the device.
172  * The firmware entry point (0x200004) is located in boot.S.
173  * we put _start() there with the linker script carl9170.lds.
174  */
175
176 void __section(boot) __noreturn __visible start(void)
177 {
178         clock_set(AHB_40MHZ_OSC, true);
179
180         /* watchdog magic pattern check */
181         if ((get(AR9170_PWR_REG_WATCH_DOG_MAGIC) & 0xffff0000) == 0x12340000) {
182                 /* watch dog warm start */
183                 incl(AR9170_PWR_REG_WATCH_DOG_MAGIC);
184                 usb_trigger_out();
185         } else if ((get(AR9170_PWR_REG_WATCH_DOG_MAGIC) & 0xffff0000) == 0x98760000) {
186                 /* suspend/resume */
187         }
188
189         /* write the magic pattern for watch dog */
190         andl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0xFFFF);
191         orl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0x12340000);
192
193         init();
194
195 #ifdef CONFIG_CARL9170FW_DEBUG
196
197         BUG("TEST BUG");
198         BUG_ON(0x2b || !0x2b);
199         INFO("INFO MESSAGE");
200
201         /* a set of unique characters to detect transfer data corruptions */
202         DBG("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
203             " ~`!1@2#3$4%%5^6&7*8(9)0_-+={[}]|\\:;\"'<,>.?/");
204 #endif /* CONFIG_CARL9170FW_DEBUG */
205
206         /*
207          * Tell the host, that the firmware has booted and is
208          * now ready to process requests.
209          */
210         send_cmd_to_host(0, CARL9170_RSP_BOOT, 0x00, NULL);
211         main_loop();
212 }