carl9170 firmware: remove noisy debug messages
[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 "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 void clock_set(enum cpu_clock_t clock_, bool on)
49 {
50         /*
51          * Word of Warning!
52          * This setting does more than just mess with the CPU Clock.
53          * So watch out, if you need _stable_ timer interrupts.
54          */
55         if (fw.phy.frequency < 3000000)
56                 set(AR9170_PWR_REG_PLL_ADDAC, 0x5163);
57         else
58                 set(AR9170_PWR_REG_PLL_ADDAC, 0x5143);
59
60         fw.ticks_per_usec = GET_VAL(AR9170_PWR_PLL_ADDAC_DIV,
61                 get(AR9170_PWR_REG_PLL_ADDAC));
62
63         set(AR9170_PWR_REG_CLOCK_SEL, (uint32_t) ((on ? 0x70 : 0x600) | clock_));
64
65         switch (clock_) {
66         case AHB_20_22MHZ:
67                 fw.ticks_per_usec >>= 1;
68         case AHB_40MHZ_OSC:
69         case AHB_40_44MHZ:
70                 fw.ticks_per_usec >>= 1;
71         case AHB_80_88MHZ:
72                 break;
73         }
74
75         timer_init(1, (fw.ticks_per_usec * 25) >> 1);
76 }
77
78 static void init(void)
79 {
80         led_init();
81
82 #ifdef CONFIG_CARL9170FW_DEBUG_UART
83         uart_init();
84 #endif /* CONFIG_CARL9170FW_DEBUG_UART */
85
86         /* 25/50/100ms timer (depends on cpu clock) */
87         timer_init(0, 50000);
88
89         /* USB init */
90         usb_init();
91
92         /* initialize DMA memory */
93         memset(&dma_mem, 0, sizeof(dma_mem));
94
95         /* fill DMA rings */
96         dma_init_descriptors();
97
98         /* clear all interrupt */
99         set(AR9170_MAC_REG_INT_CTRL, 0xffff);
100
101         orl(AR9170_MAC_REG_AFTER_PNP, 1);
102
103         /* Init watch dog control flag */
104         fw.watchdog_enable = 1;
105
106         set(AR9170_TIMER_REG_WATCH_DOG, AR9170_WATCH_DOG_TIMER);
107
108 #ifdef CONFIG_CARL9170FW_GPIO_INTERRUPT
109         fw.cached_gpio_state.gpio = get(AR9170_GPIO_REG_PORT_DATA) &
110                                     CARL9170_GPIO_MASK;
111 #endif /* CONFIG_CARL9170FW_GPIO_INTERRUPT */
112
113         /* this will get the downqueue moving. */
114         down_trigger();
115 }
116
117 static void handle_fw(void)
118 {
119         if (fw.watchdog_enable == 1)
120                 set(AR9170_TIMER_REG_WATCH_DOG, AR9170_WATCH_DOG_TIMER);
121
122         if (fw.reboot)
123                 reboot();
124 }
125
126 static void timer0_isr(void)
127 {
128         wlan_timer();
129
130 #ifdef CONFIG_CARL9170FW_GPIO_INTERRUPT
131         gpio_timer();
132 #endif /* CONFIG_CARL9170FW_GPIO_INTERRUPT */
133
134 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
135         set(AR9170_GPIO_REG_PORT_DATA, get(AR9170_GPIO_REG_PORT_DATA) ^ 1);
136 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
137 }
138
139 static void timer1_isr(void)
140 {
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         HANDLER(intr, BIT(1), timer1_isr);
163
164         if (intr)
165                 DBG("Unhandled Timer Event %x", (unsigned int) intr);
166
167 #undef HANDLER
168 }
169
170 static void __noreturn main_loop(void)
171 {
172         /* main loop */
173         while (1) {
174                 handle_fw();
175
176                 /*
177                  * Due to frame order persevation, the wlan subroutines
178                  * must be executed before handle_host_interface.
179                  */
180                 handle_wlan();
181
182                 handle_host_interface();
183
184                 handle_usb();
185
186                 handle_timer();
187
188                 fw.counter++;
189         }
190 }
191
192 /*
193  * The bootcode will work with the device driver to load the firmware
194  * onto the device's Program SRAM. The Program SRAM has a size of 16 KB
195  * and also contains the stack, which grows down from 0x204000.
196  *
197  * The Program SRAM starts at address 0x200000 on the device.
198  * The firmware entry point (0x200004) is located in boot.S.
199  * we put _start() there with the linker script carl9170.lds.
200  */
201
202 void __section(boot) start(void)
203 {
204         clock_set(AHB_40MHZ_OSC, true);
205
206         /* watchdog magic pattern check */
207         if ((get(AR9170_PWR_REG_WATCH_DOG_MAGIC) & 0xffff0000) == 0x12340000) {
208                 /* watch dog warm start */
209                 incl(AR9170_PWR_REG_WATCH_DOG_MAGIC);
210                 usb_trigger_out();
211         } else if ((get(AR9170_PWR_REG_WATCH_DOG_MAGIC) & 0xffff0000) == 0x98760000) {
212                 /* suspend/resume */
213         }
214
215         /* write the magic pattern for watch dog */
216         andl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0xFFFF);
217         orl(AR9170_PWR_REG_WATCH_DOG_MAGIC, 0x12340000);
218
219         init();
220
221 #ifdef CONFIG_CARL9170FW_DEBUG
222
223         BUG("TEST BUG");
224         BUG_ON(0x2b || !0x2b);
225         INFO("INFO MESSAGE");
226
227         /* a set of unique characters to detect transfer data corruptions */
228         DBG("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
229             " ~`!1@2#3$4%%5^6&7*8(9)0_-+={[}]|\\:;\"'<,>.?/");
230 #endif /* CONFIG_CARL9170FW_DEBUG */
231
232         /*
233          * Tell the host, that the firmware has booted and is
234          * now ready to process requests.
235          */
236         send_cmd_to_host(0, CARL9170_RSP_BOOT, 0x00, NULL);
237         main_loop();
238 }