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