9b2a09c5ec6ebb1c69ea111187e540f2f85dce20
[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 init(void)
36 {
37         led_init();
38
39 #ifdef CONFIG_CARL9170FW_DEBUG_UART
40         uart_init();
41 #endif /* CONFIG_CARL9170FW_DEBUG_UART */
42
43         /* 25/50/100ms timer (depends on cpu clock) */
44         timer_init(0, 50000);
45
46         /* USB init */
47         usb_init();
48
49         memset(&dma_mem, 0, sizeof(dma_mem));
50
51         /* fill DMA rings */
52         dma_init_descriptors();
53
54         /* clear all interrupt */
55         set(AR9170_MAC_REG_INT_CTRL, 0xffff);
56
57         orl(AR9170_MAC_REG_AFTER_PNP, 1);
58
59         /* Init watch dog control flag */
60 #ifdef CONFIG_CARL9170FW_WATCHDOG
61         fw.watchdog_enable = 1;
62
63         set(AR9170_TIMER_REG_WATCH_DOG, AR9170_WATCH_DOG_TIMER);
64 #else
65         fw.watchdog_enable = 0;
66         set(AR9170_TIMER_REG_WATCH_DOG, 0xffff);
67 #endif /* CONFIG_CARL9170FW_WATCHDOG */
68
69 #ifdef CONFIG_CARL9170FW_GPIO_INTERRUPT
70         fw.cached_gpio_state.gpio = get(AR9170_GPIO_REG_PORT_DATA) &
71                                     CARL9170_GPIO_MASK;
72 #endif /* CONFIG_CARL9170FW_GPIO_INTERRUPT */
73
74         /* this will get the downqueue moving. */
75         down_trigger();
76 }
77
78 static void handle_fw(void)
79 {
80         if (fw.watchdog_enable == 1)
81                 set(AR9170_TIMER_REG_WATCH_DOG, AR9170_WATCH_DOG_TIMER);
82
83         if (fw.reboot)
84                 reboot();
85 }
86
87 static void __attribute__((noreturn)) main_loop(void)
88 {
89         /* main loop */
90         while (1) {
91                 handle_fw();
92
93                 /*
94                  * Due to frame order persevation, the wlan subroutines
95                  * must be executed before handle_host_interface.
96                  */
97                 handle_wlan();
98
99                 handle_host_interface();
100
101                 handle_usb();
102
103                 handle_timer();
104
105                 fw.counter++;
106         }
107 }
108
109 /*
110  * The bootcode will work with the device driver to load the firmware
111  * onto the device's Program SRAM. The Program SRAM has a size of 32 KB
112  * and also contains the stack, which grows down from 0x208000.
113  *
114  * The Program SRAM starts at address 0x200000 on the device.
115  * The firmware entry point (0x200004) is located in boot.S.
116  * we put _start() there with the linker script carl9170.lds.
117  */
118
119 void __attribute__((noreturn)) start(void)
120 {
121         clock_set(true, AHB_40MHZ_OSC);
122
123         /* initialize firmware context and DMA memory */
124         memset(&fw, 0, sizeof(fw));
125
126         /* watchdog magic pattern check */
127         if ((get(AR9170_PWR_REG_WATCH_DOG_MAGIC) & 0xffff0000) == 0x12340000) {
128                 /* watch dog warm start */
129                 incl(AR9170_PWR_REG_WATCH_DOG_MAGIC);
130                 usb_trigger_out();
131         } else if ((get(AR9170_PWR_REG_WATCH_DOG_MAGIC) & 0xffff0000) == 0x98760000) {
132                 /* suspend/resume */
133         }
134
135         /* write the magic pattern for watch dog */
136         andl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0xFFFF);
137         orl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0x12340000);
138
139         init();
140
141 #ifdef CONFIG_CARL9170FW_DEBUG
142
143         BUG("TEST BUG");
144         BUG_ON(0x2b || !0x2b);
145         INFO("INFO MESSAGE");
146
147         /* a set of unique characters to detect transfer data corruptions */
148         DBG("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
149             " ~`!1@2#3$4%%5^6&7*8(9)0_-+={[}]|\\:;\"'<,>.?/");
150 #endif /* CONFIG_CARL9170FW_DEBUG */
151
152         main_loop();
153 }