922cf6a9638d0360b6cd87bb36ac41fdd47e8394
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / target / init / init.c
1 #if defined(_RAM_)
2
3 #include "athos_api.h"
4     
5 #if defined(PROJECT_MAGPIE)
6 #include "regdump.h"
7 #include "usb_defs.h"
8 extern  uint32_t *init_htc_handle;
9 uint8_t htc_complete_setup = 0;
10 void reset_EP4_FIFO(void);
11 #endif
12 #include "init.h"
13
14 void Magpie_init(void);
15
16
17 #if defined(PROJECT_MAGPIE)
18 extern BOOLEAN bEepromExist;
19 extern BOOLEAN bJumptoFlash;
20 #endif
21
22 static uint32_t loop_low, loop_high;
23
24 // reference idle count at the beginning
25 uint32_t idle_cnt = 0;
26
27 #if defined(PROJECT_K2)
28 // save the ROM printf function point
29 uint32_t save_cmnos_printf;
30 #endif
31
32 #define ATH_DATE_STRING     __DATE__" "__TIME__
33
34 static void idle_task();
35
36 #if defined(PROJECT_MAGPIE)
37 void fatal_exception_func()
38 {
39         // patch for execption
40         (void)_xtos_set_exception_handler(EXCCAUSE_UNALIGNED, AR6002_fatal_exception_handler_patch);
41         (void)_xtos_set_exception_handler(EXCCAUSE_LOAD_STORE_ERROR, AR6002_fatal_exception_handler_patch);
42         (void)_xtos_set_exception_handler(EXCCAUSE_ILLEGAL, AR6002_fatal_exception_handler_patch);
43         (void)_xtos_set_exception_handler(EXCCAUSE_INSTR_ERROR, AR6002_fatal_exception_handler_patch);
44         (void)_xtos_set_exception_handler(EXCCAUSE_PRIVILEGED, AR6002_fatal_exception_handler_patch);
45         (void)_xtos_set_exception_handler(EXCCAUSE_INSTR_DATA_ERROR, AR6002_fatal_exception_handler_patch);
46         (void)_xtos_set_exception_handler(EXCCAUSE_LOAD_STORE_DATA_ERROR, AR6002_fatal_exception_handler_patch);
47         (void)_xtos_set_exception_handler(EXCCAUSE_DIVIDE_BY_ZERO, AR6002_fatal_exception_handler_patch);
48 }
49 #endif
50
51 #if defined(PROJECT_MAGPIE)
52 void
53 change_magpie_clk(void)
54 {
55         volatile uint32_t i=0, rd_data;
56
57         HAL_WORD_REG_WRITE(0x00056004, 0x11);
58         rd_data = HAL_WORD_REG_READ(0x00056004) & 0x1;
59
60         /* Wait for the update bit to get cleared */
61         while (rd_data)
62                 rd_data = HAL_WORD_REG_READ(0x00056004) & 0x1;
63
64         /* Put the PLL into reset */
65         rd_data = HAL_WORD_REG_READ(0x00050010) | (1<<1);
66         HAL_WORD_REG_WRITE(0x00050010,rd_data);
67
68         /*
69          * XXX: statically set the CPU clock to 200Mhz
70          */
71         /* Setting of the PLL */
72         HAL_WORD_REG_WRITE(0x00056000, 0x325);//400 MHz
73
74         /* Pull CPU PLL out of Reset */
75         rd_data = HAL_WORD_REG_READ(0x00050010) & ~(1<<1);
76         HAL_WORD_REG_WRITE(0x00050010,rd_data);
77
78         A_DELAY_USECS(60); // wait for stable
79
80         /* CPU & AHB settings */  
81         /*
82          * AHB clk = ( CPU clk / 2 )
83          */
84         HAL_WORD_REG_WRITE(0x00056004, ((0x00001 | (1 << 16)|(1 << 8)))); // set plldiv to 2
85         rd_data = HAL_WORD_REG_READ(0x00056004) & 0x1;
86
87         while (rd_data)
88                 rd_data = HAL_WORD_REG_READ(0x00056004) & 0x1;
89
90         /* UART Setting */
91         A_UART_HWINIT((100*1000*1000), 115200);
92
93 }
94
95 void exception_reset(struct register_dump_s *dump)
96 {
97         A_PRINTF("exception_reset \n");
98
99         /* phase I dump info */
100         A_PRINTF("exception reset-phase 1\n");
101         if(_assfail_ori)
102                 _assfail_ori(dump);
103
104         /* phase II reset */
105         A_PRINTF("exception reset-phase 2\n");
106         *((volatile uint32_t*)WATCH_DOG_MAGIC_PATTERN_ADDR) = WDT_MAGIC_PATTERN;
107
108         HAL_WORD_REG_WRITE(MAGPIE_REG_RST_RESET_ADDR, 
109                            HAL_WORD_REG_READ(MAGPIE_REG_RST_RESET_ADDR)|(BIT10|BIT8|BIT7|BIT6));
110
111         HAL_WORD_REG_WRITE(MAGPIE_REG_AHB_ARB_ADDR,
112                            (HAL_WORD_REG_READ(MAGPIE_REG_AHB_ARB_ADDR)|BIT1));
113
114         HAL_WORD_REG_WRITE((USB_CTRL_BASE_ADDRESS+0x118), 0x0);
115         HAL_WORD_REG_WRITE(0x50010, HAL_WORD_REG_READ(0x50010)|BIT4);
116         A_DELAY_USECS(5);
117         HAL_WORD_REG_WRITE(0x50010, HAL_WORD_REG_READ(0x50010)&~BIT4);
118         A_DELAY_USECS(5);
119         HAL_WORD_REG_WRITE((USB_CTRL_BASE_ADDRESS+0x118), 0x1);
120
121         // set clock to bypass mode - 40Mhz from XTAL
122         HAL_WORD_REG_WRITE(MAGPIE_REG_CPU_PLL_BYPASS_ADDR, (BIT0|BIT4));
123         A_DELAY_USECS(100); // wait for stable
124         HAL_WORD_REG_WRITE(MAGPIE_REG_CPU_PLL_ADDR, (BIT16));
125
126         A_UART_HWINIT((40*1000*1000), 115200);
127
128         A_PRINTF("do TX/RX swap\n");
129
130         MAGPIE_REG_USB_RX0_SWAP_DATA = 0x1;
131         MAGPIE_REG_USB_TX0_SWAP_DATA = 0x1;
132         MAGPIE_REG_USB_RX1_SWAP_DATA = 0x1;
133         MAGPIE_REG_USB_RX2_SWAP_DATA = 0x1;
134
135         A_PRINTF("Jump to BOOT\n");
136
137         // reboot.....
138         A_USB_JUMP_BOOT();
139 }
140
141 void reset_EP4_FIFO(void)
142 {
143         int i;
144
145         // reset EP4 FIFO
146         USB_BYTE_REG_WRITE(ZM_EP4_BYTE_COUNT_HIGH_OFFSET, (USB_BYTE_REG_READ(ZM_EP4_BYTE_COUNT_HIGH_OFFSET) | BIT4));
147         for(i = 0; i < 100; i++) {}
148         USB_BYTE_REG_WRITE(ZM_EP4_BYTE_COUNT_HIGH_OFFSET, (USB_BYTE_REG_READ(ZM_EP4_BYTE_COUNT_HIGH_OFFSET) & ~BIT4)); 
149 }
150
151 LOCAL void zfGenExceptionEvent(uint32_t exccause, uint32_t pc, uint32_t badvaddr)
152 {
153         uint32_t pattern = 0x33221199;
154
155         A_PRINTF("<Exception>Tgt Drv send an event 44332211 to Host Drv\n");
156         mUSB_STATUS_IN_INT_DISABLE();
157
158         USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0x0f);
159
160         USB_WORD_REG_WRITE(ZM_EP3_DATA_OFFSET, pattern);
161         USB_WORD_REG_WRITE(ZM_EP3_DATA_OFFSET, exccause);
162         USB_WORD_REG_WRITE(ZM_EP3_DATA_OFFSET, pc);
163         USB_WORD_REG_WRITE(ZM_EP3_DATA_OFFSET, badvaddr);
164     
165         mUSB_EP3_XFER_DONE();
166 }
167
168 LOCAL void zfGenWrongEpidEvent(uint32_t epid)
169 {
170         uint32_t pattern   = 0x33221299;
171
172         A_PRINTF("<WrongEPID>Tgt Drv send an event 44332212 to Host Drv\n");
173         mUSB_STATUS_IN_INT_DISABLE();
174
175         USB_WORD_REG_WRITE(ZM_CBUS_FIFO_SIZE_OFFSET, 0x0f);
176
177         USB_WORD_REG_WRITE(ZM_EP3_DATA_OFFSET, pattern);
178         USB_WORD_REG_WRITE(ZM_EP3_DATA_OFFSET, epid);
179
180         mUSB_EP3_XFER_DONE();
181 }
182
183 void
184 AR6002_fatal_exception_handler_patch(CPU_exception_frame_t *exc_frame)
185 {
186         struct register_dump_s dump;
187         void (*reset_func)(void) = (void*)(RESET_VECTOR_ADDRESS);
188         uint32_t  exc_cause, exc_vaddr;
189         asm volatile("rsr %0,%1" : "=r" (exc_cause) : "n" (EXCCAUSE));
190         asm volatile("rsr %0,%1" : "=r" (exc_vaddr) : "n" (EXCVADDR));
191
192         dump.exc_frame              = *exc_frame; /* structure copy */
193         dump.badvaddr               = exc_vaddr;
194         dump.exc_frame.xt_exccause  = exc_cause;
195         dump.pc                     = exc_frame->xt_pc;
196         dump.assline                = 0;
197
198         zfGenExceptionEvent(dump.exc_frame.xt_exccause, dump.pc, dump.badvaddr);
199
200 #if SYSTEM_MODULE_PRINT
201         A_PRINTF("\nFatal exception (%d): \tpc=0x%x \n\r\tbadvaddr=0x%x \n\r\tdump area=0x%x\n",
202                  dump.exc_frame.xt_exccause, dump.pc, dump.badvaddr, &dump);
203         PRINT_FAILURE_STATE();
204 #else
205         A_PUTS("Fatal exception\n\r");
206 #endif
207         A_ASSFAIL(&dump);
208
209 #if defined(_ROM_)     
210         A_WDT_ENABLE();
211 #endif
212
213         while(1) ;
214 }
215
216 void 
217 HTCControlSvcProcessMsg_patch(HTC_ENDPOINT_ID EndpointID, adf_nbuf_t hdr_buf,
218                               adf_nbuf_t pBuffers, void *arg)
219 {
220         a_uint8_t *anbdata;
221         a_uint32_t anblen;
222         HTC_UNKNOWN_MSG *pMsg;
223
224         /* we assume buffers are aligned such that we can access the message
225          * parameters directly*/
226         adf_nbuf_peek_header(pBuffers, &anbdata, &anblen);
227         pMsg = (HTC_UNKNOWN_MSG *)anbdata;
228
229         if (pMsg->MessageID == HTC_MSG_SETUP_COMPLETE_ID) {
230                 htc_complete_setup = 1;
231         }
232
233         HTCControlSvcProcessMsg(EndpointID, hdr_buf, pBuffers, arg);
234 }
235
236 /* Patch callback for check the endpoint ID is correct or not */
237 void 
238 HTCMsgRecvHandler_patch(adf_nbuf_t hdr_buf, adf_nbuf_t buffer, void *context)
239 {
240         int eid;
241         a_uint8_t *anbdata;
242         a_uint32_t anblen;
243         adf_nbuf_t tmp_nbuf;
244         HTC_FRAME_HDR *pHTCHdr;
245                 
246         if (hdr_buf == ADF_NBUF_NULL) {
247                 /* HTC hdr is not in the hdr_buf */
248                 tmp_nbuf = buffer;
249         } else {
250                 tmp_nbuf = hdr_buf;
251         }
252                 
253         adf_nbuf_peek_header(tmp_nbuf, &anbdata, &anblen);        
254         pHTCHdr = (HTC_FRAME_HDR *)anbdata; 
255   
256         eid = pHTCHdr->EndpointID;
257     
258         if ((eid != 0) && (htc_complete_setup == 0)) {
259                 A_PRINTF("\nHTC Hdr EndpointID = %d, anblen = %d\n", pHTCHdr->EndpointID, anblen);
260                 A_PRINTF("HTC Hder : %2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x\n",
261                          *anbdata, *(anbdata+1), *(anbdata+2), *(anbdata+3), 
262                          *(anbdata+4), *(anbdata+5), *(anbdata+6), *(anbdata+7),
263                          *(anbdata+8), *(anbdata+9), *(anbdata+10), *(anbdata+11)); 
264                 A_PRINTF("init_htc_handle = 0x%8x\n", init_htc_handle);
265             
266                 if (pHTCHdr->EndpointID == 1) {
267                         A_PRINTF("Return WMI Command buffer\n");
268                         HTC_ReturnBuffers(init_htc_handle, 1, tmp_nbuf);
269                 } else if ((pHTCHdr->EndpointID == 5) || (pHTCHdr->EndpointID == 6)) {
270                         A_PRINTF("Return Data buffer\n");
271                         HTC_ReturnBuffers(init_htc_handle, 6, tmp_nbuf);
272                 } else {
273                 }
274         } else {
275                 if ((pHTCHdr->EndpointID < 0) || (pHTCHdr->EndpointID >= ENDPOINT_MAX)) {
276                         A_PRINTF("HTC Hdr EndpointID = %d, anblen = %d\n", pHTCHdr->EndpointID, anblen);
277                         A_PRINTF("HTC Hder : %2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x\n", 
278                                  *anbdata, *(anbdata+1), *(anbdata+2), *(anbdata+3), 
279                                  *(anbdata+4), *(anbdata+5), *(anbdata+6), *(anbdata+7));
280
281                         if (anblen > 64) {
282                                 A_PRINTF("EP1-Tx-Data with Wrong Htc Header Endpoint ID, WAR free this buffer\n");
283                                 HTC_ReturnBuffers(init_htc_handle, 6, tmp_nbuf);
284                                 A_PRINTF("EP1-Tx-Data > Free this buffer successfully\n");
285                         } else {
286                                 A_PRINTF("EP4-WMI-Cmd with Wrong Htc Header Endpoint ID, WAR free this buffer\n");
287                                 zfGenWrongEpidEvent((a_uint32_t)pHTCHdr->EndpointID);
288                                 HTC_ReturnBuffers(init_htc_handle, 1, tmp_nbuf);
289                                 A_PRINTF("EP4-WMI-Cmd > Free this buffer successfully\n");
290                         }
291                 } else
292                         HTCMsgRecvHandler( hdr_buf, buffer, context);
293         }
294 }
295 #endif
296
297 void init_mem()
298 {
299         int i = 0;
300         uint32_t *temp = (uint32_t *)ALLOCRAM_START;
301
302         /* clear bss segment */
303         for(temp = (uint32_t *)&START_BSS; temp < (uint32_t *)&END_BSS; temp++)
304                 *temp = 0;
305
306         /* clear heap segment */
307         for(i = 0; i < ((ALLOCRAM_SIZE - 4)/4); i++)
308                 temp[i] = 0;
309 }
310
311 static void idle_task()
312 {
313         if (loop_low == 0xffffffff) {
314                 loop_low = 0;
315                 loop_high++;
316         } else {
317                 loop_low++;
318         }
319         return;
320 }
321
322 void wlan_task(void)
323 {
324         loop_low=loop_high=0;
325
326         while(1) {
327 #if defined(PROJECT_MAGPIE)
328                 if (bJumptoFlash){
329                         bJumptoFlash = FALSE;
330                         break;
331                 }
332 #endif
333
334                 /* update wdt timer */
335                 A_WDT_TASK();
336
337                 /* UPDATE cticks - to be moved to idle_tsk, put here will be easier to read  */
338                 A_CLOCK_TICK();
339
340                 HIF_isr_handler(NULL);
341
342 #if MAGPIE_ENABLE_WLAN == 1
343                 wlan_pci_isr();
344 #endif
345
346                 A_TASKLET_RUN();
347                 A_TIMER_RUN();
348
349                 /* Low priority tasks */
350                 if ((loop_low & 0xf) == 0) {
351                 }
352
353                 /* Very low priority tasks */
354                 if ((loop_low & 0xfff) == 0x7) {
355                         if ((loop_low & 0x1000) == 0) {
356                                 A_DBG_TASK();
357                         } else {
358                         }
359                 }
360
361                 idle_task();
362         }
363 }
364
365 #endif /* #if defined(_RAM_) */