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