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