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