97a4331c285d1a9e91b0eb0376f0ebc844437d6c
[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         timer_init(1, (fw.ticks_per_usec * 25) >> 1);
81 }
82
83 static void init(void)
84 {
85         led_init();
86
87 #ifdef CONFIG_CARL9170FW_DEBUG_UART
88         uart_init();
89 #endif /* CONFIG_CARL9170FW_DEBUG_UART */
90
91         /* 25/50/100ms timer (depends on cpu clock) */
92         timer_init(0, 50000);
93
94         /* USB init */
95         usb_init();
96
97         /* initialize DMA memory */
98         memset(&dma_mem, 0, sizeof(dma_mem));
99
100         /* fill DMA rings */
101         dma_init_descriptors();
102
103         /* clear all interrupt */
104         set(AR9170_MAC_REG_INT_CTRL, 0xffff);
105
106         orl(AR9170_MAC_REG_AFTER_PNP, 1);
107
108         /* Init watch dog control flag */
109         fw.watchdog_enable = 1;
110
111         set(AR9170_TIMER_REG_WATCH_DOG, AR9170_WATCH_DOG_TIMER);
112
113 #ifdef CONFIG_CARL9170FW_GPIO_INTERRUPT
114         fw.cached_gpio_state.gpio = get(AR9170_GPIO_REG_PORT_DATA) &
115                                     CARL9170_GPIO_MASK;
116 #endif /* CONFIG_CARL9170FW_GPIO_INTERRUPT */
117
118         /* this will get the downqueue moving. */
119         down_trigger();
120 }
121
122 static void handle_fw(void)
123 {
124         if (fw.watchdog_enable == 1)
125                 set(AR9170_TIMER_REG_WATCH_DOG, AR9170_WATCH_DOG_TIMER);
126
127         if (fw.reboot)
128                 reboot();
129 }
130
131 static void timer0_isr(void)
132 {
133         wlan_timer();
134
135 #ifdef CONFIG_CARL9170FW_GPIO_INTERRUPT
136         gpio_timer();
137 #endif /* CONFIG_CARL9170FW_GPIO_INTERRUPT */
138
139 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
140         tally_update();
141 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
142
143 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
144         set(AR9170_GPIO_REG_PORT_DATA, get(AR9170_GPIO_REG_PORT_DATA) ^ 1);
145 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
146 }
147
148 static void timer1_isr(void)
149 {
150 }
151
152 static void handle_timer(void)
153 {
154         uint32_t intr;
155
156         intr = get(AR9170_TIMER_REG_INTERRUPT);
157
158         /* ACK timer interrupt */
159         set(AR9170_TIMER_REG_INTERRUPT, intr);
160
161 #define HANDLER(intr, flag, func)                       \
162         do {                                            \
163                 if ((intr & flag) != 0) {               \
164                         intr &= ~flag;                  \
165                         func();                         \
166                 }                                       \
167         } while (0)
168
169         HANDLER(intr, BIT(0), timer0_isr);
170
171         HANDLER(intr, BIT(1), timer1_isr);
172
173         if (intr)
174                 DBG("Unhandled Timer Event %x", (unsigned int) intr);
175
176 #undef HANDLER
177 }
178
179 static void __noreturn main_loop(void)
180 {
181         /* main loop */
182         while (1) {
183                 handle_fw();
184
185                 /*
186                  * Due to frame order persevation, the wlan subroutines
187                  * must be executed before handle_host_interface.
188                  */
189                 handle_wlan();
190
191                 handle_host_interface();
192
193                 handle_usb();
194
195                 handle_timer();
196
197                 fw.counter++;
198         }
199 }
200
201 /*
202  * The bootcode will work with the device driver to load the firmware
203  * onto the device's Program SRAM. The Program SRAM has a size of 16 KB
204  * and also contains the stack, which grows down from 0x204000.
205  *
206  * The Program SRAM starts at address 0x200000 on the device.
207  * The firmware entry point (0x200004) is located in boot.S.
208  * we put _start() there with the linker script carl9170.lds.
209  */
210
211 void __section(boot) start(void)
212 {
213         clock_set(AHB_40MHZ_OSC, true);
214
215         /* watchdog magic pattern check */
216         if ((get(AR9170_PWR_REG_WATCH_DOG_MAGIC) & 0xffff0000) == 0x12340000) {
217                 /* watch dog warm start */
218                 incl(AR9170_PWR_REG_WATCH_DOG_MAGIC);
219                 usb_trigger_out();
220         } else if ((get(AR9170_PWR_REG_WATCH_DOG_MAGIC) & 0xffff0000) == 0x98760000) {
221                 /* suspend/resume */
222         }
223
224         /* write the magic pattern for watch dog */
225         andl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0xFFFF);
226         orl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0x12340000);
227
228         init();
229
230 #ifdef CONFIG_CARL9170FW_DEBUG
231
232         BUG("TEST BUG");
233         BUG_ON(0x2b || !0x2b);
234         INFO("INFO MESSAGE");
235
236         /* a set of unique characters to detect transfer data corruptions */
237         DBG("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
238             " ~`!1@2#3$4%%5^6&7*8(9)0_-+={[}]|\\:;\"'<,>.?/");
239 #endif /* CONFIG_CARL9170FW_DEBUG */
240
241         /*
242          * Tell the host, that the firmware has booted and is
243          * now ready to process requests.
244          */
245         send_cmd_to_host(0, CARL9170_RSP_BOOT, 0x00, NULL);
246         main_loop();
247 }