fix compile warnings for gcc 6.2.0
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / target / init / app_start.c
1 /*
2  * Copyright (c) 2013 Qualcomm Atheros, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted (subject to the limitations in the
7  * disclaimer below) provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the
15  *    distribution.
16  *
17  *  * Neither the name of Qualcomm Atheros nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
22  * GRANTED BY THIS LICENSE.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
23  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include <wlan_pci.h>
37 #include <Magpie_api.h>
38 #include <rom.h>
39
40 #include "dt_defs.h"
41 #include "athos_api.h"
42
43 #include "regdump.h"
44 #include "usb_defs.h"
45
46 #include "adf_os_io.h"
47
48 #include "init.h"
49 #include "app_start.h"
50 #include <linux/compiler.h>
51
52 // @TODO: Should define the memory region later~
53 #define ALLOCRAM_START       ( ((unsigned int)&_fw_image_end) + 4)
54 #define ALLOCRAM_SIZE        ( SYS_RAM_SZIE - ( ALLOCRAM_START - SYS_D_RAM_REGION_0_BASE) - SYS_D_RAM_STACK_SIZE)
55
56 // support for more than 64 bytes on command pipe
57 extern void usb_reg_out_patch(void);
58 extern int _HIFusb_get_max_msg_len_patch(hif_handle_t handle, int pipe);
59 extern void _HIFusb_isr_handler_patch(hif_handle_t h);
60 extern BOOLEAN bSet_configuration_patch(void);
61 extern void vUSBFIFO_EP6Cfg_FS_patch(void);
62 extern void usb_status_in_patch(void);
63 extern void _fw_usbfifo_init(USB_FIFO_CONFIG *pConfig);
64 extern void zfTurnOffPower_patch(void);
65 extern void zfResetUSBFIFO_patch(void);
66 extern void _HIFusb_start_patch(hif_handle_t handle);
67 extern void hif_pci_patch_install(struct hif_api *apis);
68 extern BOOLEAN bGet_descriptor_patch(void);
69 extern BOOLEAN bStandardCommand_patch(void);
70
71 // patch for clock
72 extern void cmnos_clock_init_patch(a_uint32_t refclk);
73 extern a_uint32_t cmnos_refclk_speed_get_patch(void);
74 extern void cmnos_delay_us_patch(int us);
75 extern void cmnos_tick_patch(void);
76 extern a_uint32_t cmnos_milliseconds_patch(void);
77
78 extern BOOLEAN bJumptoFlash;
79 extern BOOLEAN bEepromExist;
80
81 void __section(boot) __noreturn __visible app_start(void)
82 {
83         uint32_t rst_status;
84         A_HOSTIF hostif;
85 #if defined(PROJECT_MAGPIE)
86         T_EEP_RET retEEP;
87 #endif
88
89         /* Zero BSS segment & dynamic memory section. */
90         init_mem();
91
92 #if defined(PROJECT_MAGPIE)
93         fatal_exception_func();
94 #endif
95
96         if( IS_FLASHBOOT() ) {
97                 athos_indirection_table_install();
98                 DBG_MODULE_INSTALL();
99                 A_CLOCK_INIT(SYSTEM_CLK);
100                 A_UART_INIT();
101                 A_PRINTF_INIT();
102                 A_DBG_INIT();
103                 A_EEP_INIT();
104                 A_TASKLET_INIT();
105                 _indir_tbl.cmnos.timer._timer_init();
106
107 #if defined(PROJECT_K2)
108                 /*
109                  * WAR: these variable is not initialized when boot from flash
110                  *      either re-enumeration or config them to default value = 0 would fix the issue
111                  */
112                 u8UsbInterfaceAlternateSetting = u8UsbConfigValue = u8UsbInterfaceValue = 0;
113 #endif
114         }
115 #ifdef ROM_VER_1_1
116         else
117                 A_EEP_INIT(); /*Required for 1_1*/
118 #endif
119
120 #if defined(PROJECT_MAGPIE)
121         retEEP = A_EEP_IS_EXIST();
122         bJumptoFlash = FALSE;
123         if ( RET_SUCCESS == retEEP ) {
124                 bEepromExist = TRUE;
125         } else {
126                 bEepromExist = FALSE;
127         }
128 #endif
129
130         hostif = A_IS_HOST_PRESENT();
131
132 #if defined(PROJECT_MAGPIE)
133         rst_status = ioread32(WATCH_DOG_MAGIC_PATTERN_ADDR);
134 #elif defined(PROJECT_K2)
135         rst_status = ioread32(MAGPIE_REG_RST_STATUS_ADDR);
136 #endif /* #if defined(PROJECT_MAGPIE) */
137
138
139         A_PRINTF(" A_WDT_INIT()\n\r");
140
141 #if defined(PROJECT_K2)
142         save_cmnos_printf = fw_cmnos_printf;
143 #endif
144
145         if( hostif == HIF_USB ) {
146 #if defined(PROJECT_K2)
147 #if MOVE_PRINT_TO_RAM
148                 save_cmnos_printf = _indir_tbl.cmnos.printf._printf;
149                 _indir_tbl.cmnos.printf._printf = fw_cmnos_printf;
150 #endif
151                 _indir_tbl.cmnos.usb._usb_fw_task = _fw_usb_fw_task;
152                 _indir_tbl.cmnos.usb._usb_reset_fifo = _fw_usb_reset_fifo;
153 #endif
154         }
155
156         if( rst_status == WDT_MAGIC_PATTERN ) {
157                 A_PRINTF(" ==>WDT reset<==\n");
158 #if defined(PROJECT_MAGPIE)
159                 reset_EP4_FIFO();
160 #endif
161                 *((volatile uint32_t*)WATCH_DOG_RESET_COUNTER_ADDR)+=1;
162         } else if (rst_status == SUS_MAGIC_PATTERN) {
163                 A_PRINTF(" ==>warm start<==\n");
164         } else
165                 A_PRINTF(" ==>cold start<==\n");
166
167 #if defined(PROJECT_MAGPIE)
168         *((volatile uint32_t*)WATCH_DOG_MAGIC_PATTERN_ADDR)=WDT_MAGIC_PATTERN;
169 #elif defined(PROJECT_K2)
170         iowrite32(MAGPIE_REG_RST_STATUS_ADDR, WDT_MAGIC_PATTERN);
171 #endif /* #if defined(PROJECT_MAGPIE) */
172
173         /* intr enable would left for firmware */
174         /* athos_interrupt_init(); */
175
176         DBG_MODULE_INSTALL();
177 #if defined(PROJECT_K2)
178         A_DBG_INIT();
179 #endif
180
181 #if defined(PROJECT_K2)
182 #if SYSTEM_MODULE_SFLASH
183         SFLASH_MODULE_INSTALL();
184         A_SFLASH_INIT();
185 #endif
186 #endif
187
188         HIF_MODULE_INSTALL();
189         HTC_MODULE_INSTALL();
190         WMI_SERVICE_MODULE_INSTALL();
191         BUF_POOL_MODULE_INSTALL();
192         VBUF_MODULE_INSTALL();
193         VDESC_MODULE_INSTALL();
194
195         //init each module, should be put together..
196         A_PRINTF("ALLOCRAM start 0x%x size %d\n", ALLOCRAM_START, ALLOCRAM_SIZE);
197         A_ALLOCRAM_INIT(ALLOCRAM_START, ALLOCRAM_SIZE);
198
199         if( hostif == HIF_USB ) {
200                 _indir_tbl.hif._get_max_msg_len = _HIFusb_get_max_msg_len_patch;
201                 _indir_tbl.cmnos.usb._usb_reg_out = usb_reg_out_patch;
202                 _indir_tbl.hif._isr_handler = _HIFusb_isr_handler_patch;
203                 _indir_tbl.cmnos.usb._usb_set_configuration = bSet_configuration_patch;
204                 _indir_tbl.cmnos.usb._usb_status_in = usb_status_in_patch;
205                 _indir_tbl.cmnos.usb._usb_get_descriptor = bGet_descriptor_patch;
206                 _indir_tbl.cmnos.usb._usb_standard_cmd = bStandardCommand_patch;
207                 _indir_tbl.usbfifo_api._init = _fw_usbfifo_init;
208
209 #if defined(PROJECT_MAGPIE)
210                 _indir_tbl.cmnos.usb._usb_power_off = zfTurnOffPower_patch;
211                 _indir_tbl.cmnos.usb._usb_reset_fifo = zfResetUSBFIFO_patch;
212                 _indir_tbl.hif._start = _HIFusb_start_patch;
213                 _indir_tbl.htc._HTC_MsgRecvHandler = HTCMsgRecvHandler_patch;
214                 _indir_tbl.htc._HTC_ControlSvcProcessMsg = HTCControlSvcProcessMsg_patch;
215 #endif
216
217                 if (!(ioread8_usb(ZM_MAIN_CTRL_OFFSET) & BIT6))
218                         vUSBFIFO_EP6Cfg_FS_patch();
219
220 #ifdef FUSION_USB_ENABLE_TX_STREAM
221                 // For K2, enable tx stream mode
222                 A_PRINTF("Enable Tx Stream mode: 0x%x\r\n",
223                         ioread32_usb(ZM_SOC_USB_MODE_CTRL_OFFSET));
224
225                 /* Patch for K2 USB STREAM mode */
226                 /* disable down stream DMA mode */
227                 io32_rmw_usb(ZM_SOC_USB_MODE_CTRL_OFFSET, BIT6, BIT0);
228 #if SYSTEM_MODULE_HP_EP5
229                 io32_set_usb(ZM_SOC_USB_MODE_CTRL_OFFSET, BIT8);
230 #endif
231
232 #if SYSTEM_MODULE_HP_EP6
233                 io32_set_usb(ZM_SOC_USB_MODE_CTRL_OFFSET, BIT9);
234 #endif
235                 /* enable down stream DMA mode */
236                 io32_set_usb(ZM_SOC_USB_MODE_CTRL_OFFSET, BIT0);
237 #endif
238
239 #ifdef FUSION_USB_ENABLE_RX_STREAM
240                 /* Patch for K2 USB STREAM mode */
241                 /* disable upstream DMA mode and enable upstream stream mode */
242                 io32_clr_usb(ZM_SOC_USB_MODE_CTRL_OFFSET, BIT1 | BIT3);
243
244                 /* K2, Set maximum IN transfer to 8K */
245                 io32_rmw_usb(ZM_SOC_USB_MODE_CTRL_OFFSET, 0x20, 0x30);
246
247                 /* enable upstream DMA mode */
248                 io32_set_usb(ZM_SOC_USB_MODE_CTRL_OFFSET, BIT1);
249
250                 /* set stream mode timeout critirea */
251                 iowrite32_usb(ZM_SOC_USB_TIME_CTRL_OFFSET, 0xa0);
252 #if defined(PROJECT_K2)
253                 /*0x10004020 is vaild in k2 but could be invaild in other chip*/
254                 if ((ioread32(0x10004020) & 0x2000) != 0) {
255                         /* disable stream mode for AR9270 */
256                         iowrite32_usb(ZM_SOC_USB_MAX_AGGREGATE_OFFSET, 0);
257                 } else {
258                         /* enable stream mode for AR9271 */
259                         iowrite32_usb(ZM_SOC_USB_MAX_AGGREGATE_OFFSET, 9);
260                 }
261 #else
262                 iowrite32_usb(ZM_SOC_USB_MAX_AGGREGATE_OFFSET, 9);
263 #endif
264 #endif
265         }
266 #if defined(PROJECT_MAGPIE) && !defined(ROM_VER_1_1)
267         else if (hostif == HIF_PCI )
268                 hif_pci_patch_install(&_indir_tbl.hif);
269 #endif
270         A_PRINTF("USB mode: 0x%x\r\n", ioread32_usb(0x100));
271
272         // patch the clock function
273         if(1) {
274                 _indir_tbl.cmnos.clock._clock_init = cmnos_clock_init_patch;
275                 _indir_tbl.cmnos.clock._refclk_speed_get = cmnos_refclk_speed_get_patch;
276                 _indir_tbl.cmnos.clock._delay_us = cmnos_delay_us_patch;
277                 _indir_tbl.cmnos.clock._clock_tick = cmnos_tick_patch;
278                 _indir_tbl.cmnos.clock._milliseconds = cmnos_milliseconds_patch;
279
280                 //default clock, setup initial variable, SYSTEM_FREQ=40
281                 A_CLOCK_INIT(SYSTEM_FREQ);
282         }
283
284         Magpie_init();
285
286 #if MAGPIE_ENABLE_WLAN == 1
287         io32_clr(MAGPIE_REG_RST_RESET_ADDR, BIT10 | BIT8 | BIT7 | BIT6);
288 #if defined(PROJECT_MAGPIE)
289         io32_set(MAGPIE_REG_AHB_ARB_ADDR, BIT1);
290 #endif
291
292         wlan_pci_module_init();
293         wlan_pci_probe();
294 #endif
295
296
297         A_PRINTF("Tgt running\n\r");
298
299 #if defined(PROJECT_MAGPIE)
300         if(1) {
301                 A_PRINTF("======= Apply MISC Assert patch\n\r");
302                 _assfail_ori =  _indir_tbl.cmnos.misc._assfail;
303                 _indir_tbl.cmnos.misc._assfail = exception_reset;
304         }
305
306         change_magpie_clk();
307 #endif
308         wlan_task(); //never return
309 }