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