9c45ee9bb546d34bcf37d61a39f15c7bd2788aab
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / target / cmnos / dbg_api.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 #include "sys_cfg.h"
36 #include "athos_api.h"
37
38 #include "adf_os_io.h"
39
40 #if defined(PROJECT_K2)
41 #if SYSTEM_MODULE_SFLASH
42 #include "sflash_api.h"
43 #endif
44 #endif /* #if defined(PROJECT_K2) */
45
46 #if defined(SYSTEM_MODULE_DBG)
47
48 /* Function prototypes */
49 static int db_help_cmd(char *, char *, char *, char *);
50 static int db_ldr_cmd(char *, char *, char *, char *);
51 static int db_str_cmd(char *, char *, char *, char *);
52 static int db_info_cmd(char *, char *, char *, char *);
53 static int db_usb_cmd(char *, char *, char *, char *);
54 static int db_intr_cmd(char *, char *, char *, char *);
55
56 static int db_cmd_starthtc(char *cmd, char *param1, char *param2, char *param3);
57
58 static int db_wdt_cmd(char *cmd, char *param1, char *param2, char *param3);
59
60 #if defined(PROJECT_K2)
61 #if SYSTEM_MODULE_SFLASH
62 static int db_cmd_sferase(char *cmd, char *param1, char *param2, char *param3);
63 static int db_cmd_sfpg(char *cmd, char *param1, char *param2, char *param3);
64 static int db_cmd_sfru(char *cmd, char *param1, char *param2, char *param3);
65 static int db_cmd_sfrm(char *cmd, char *param1, char *param2, char *param3);
66 static int db_cmd_sfrdsr(char *cmd, char *param1, char *param2, char *param3);
67 #endif
68 #endif /* #if defined(PROJECT_K2) */
69 static int db_cmd_memcmp(char *cmd, char *param1, char *param2, char *param3);
70 static int db_cmd_memdump(char *cmd, char *param1, char *param2, char *param3);
71
72 static int db_clock_cmd(char *cmd, char *param1, char *param2, char *param3);
73
74 static uint16_t db_get_cmd_line(uint8_t ch, char *cmd_line, uint16_t *i);
75 static int db_formalize_command(char *, char *);
76 static int db_ascii_to_hex(char *, unsigned long *);
77 static int db_hex_to_ascii(unsigned long, char *);
78 static void zf_debug_task(void);
79
80 /* Console debug command table */
81 const struct DB_COMMAND_STRUCT command_table[] =
82 {
83         {"HELP",   ", List all debug commands", db_help_cmd},
84         {"?",      ", Equal to HELP comamnd", db_help_cmd},
85
86         /* Basic load/store/dump command */
87         {"LDR",    "<Hex addr>, Load word", db_ldr_cmd},
88         {"LDRH",   "<Hex addr>, Load half word", db_ldr_cmd},
89         {"LDRB",   "<Hex addr>, Load byte", db_ldr_cmd},
90         {"STR",    "<Hex addr> <Hex value>, Store word", db_str_cmd},
91         {"STRH",   "<Hex addr> <Hex value>, Store half word", db_str_cmd},
92         {"STRB",   "<Hex addr> <Hex value>, Store byte", db_str_cmd},
93         {"INFO",   ", Print debug information", db_info_cmd},
94         {"USB",   ", usb releated command", db_usb_cmd},
95         {"INTR",   ", intr releated command", db_intr_cmd},
96         {"CLOCK",    ", change the clock...", db_clock_cmd},
97         {"HTCR", "Issue HTC ready to host", db_cmd_starthtc},
98         {"WDT",   ", wdt debug command", db_wdt_cmd},
99 #if defined(PROJECT_K2)
100 #if SYSTEM_MODULE_SFLASH
101         {"SFE", ", S<Hex>/B<Hex>/C, SPI Flash chip erase", db_cmd_sferase},
102         {"SFPG", "<Hex addr> <Hex len> <Hex buf>, SPI Flash program", db_cmd_sfpg},
103         {"SFRU", "f/r <Hex addr> <Hex addr>, SPI Flash fast read/read to UART", db_cmd_sfru},
104         {"SFRM", "f/r <Hex addr> <Hex addr>, SPI Flash fast read/read to Memory 0x520000", db_cmd_sfrm},
105         {"SFRDSR", ", SPI Flash status register read", db_cmd_sfrdsr},
106 #endif
107 #endif /* #if defined(PROJECT_K2) */
108         {"MEMCMP", "<Hex addr> <Hex addr> <Hex len>, memory comparison", db_cmd_memcmp},
109         {"MEMDMP", "<Hex addr> <Hex addr>, memory dump", db_cmd_memdump},
110         {"", "", 0}
111         /* {Command, Help description, function} */
112 };
113
114 char cmd_buffer[COMMAND_BUFFER_SIZE][DB_MAX_COMMAND_LENGTH]; /* Backup previous command */
115 int cmd_buf_ptr;
116 int cmd_buf_full;
117 char raw_cmd[DB_MAX_COMMAND_LENGTH];
118 char cmd_str[DB_MAX_COMMAND_LENGTH*4];
119 int cmd_not_found;
120 uint16_t gvLen;
121 int pressed_time;
122
123 static void db_incorect_format(void)
124 {
125         A_PRINTF("Error! Incorrect format.\n\r");
126 }
127
128 static void db_unknown_command(void)
129 {
130         A_PRINTF("Error! Unknown command.\n\r");
131 }
132
133 static void db_print_dump(const char *mem1, const char *mem2)
134 {
135         unsigned int i = 0;
136         const char *tmp;
137
138         do {
139                 if (i == 0) {
140                         A_PRINTF("\n\r%06x: ", mem1);
141                         tmp = mem1;
142                 }
143
144                 A_PRINTF("%04x ", *(uint16_t *)mem1);
145
146                 mem1 += 2;
147                 i++;
148
149                 if (i == 8) {
150                         A_PRINTF(" ");
151                         do {
152                                 if (*tmp > 0x20 && *tmp < 0x7e)
153                                         A_PRINTF("%c", *tmp);
154                                 else
155                                         A_PRINTF(".");
156                                 tmp++;
157                         } while (tmp < mem1);
158                         i = 0;
159                 }
160         } while (mem1 < mem2);
161         A_PRINTF("\n\r");
162 }
163
164 static void zf_debug_init(void)
165 {
166         uint8_t ch;
167
168         /* Purge Rx FIFO */
169         while ((zm_get_char(&ch)) != 0)
170         {
171         }
172
173         cmd_buf_ptr = 0;
174         cmd_buf_full = FALSE;
175         gvLen = 0;
176         pressed_time = 0;
177 }
178
179 static void zf_debug_task(void)
180 {
181         int i;
182         uint8_t ch;
183
184         if ((zm_get_char(&ch)) == 0)
185         {
186                 return;
187         }
188
189         if (db_get_cmd_line(ch, raw_cmd, &gvLen) == 0)
190         {
191                 return;
192         }
193
194         if (db_formalize_command(raw_cmd, cmd_str))
195         {
196                 gvLen = 0;
197                 i = 0;
198
199                 cmd_not_found = TRUE;
200                 while(command_table[i].cmd_func)
201                 {
202                         if (!strcmp(command_table[i].cmd_str, cmd_str))
203                         {
204                                 cmd_not_found = FALSE;
205                                 command_table[i].cmd_func(cmd_str,
206                                                           cmd_str+DB_MAX_COMMAND_LENGTH,
207                                                           cmd_str+DB_MAX_COMMAND_LENGTH*2,
208                                                           cmd_str+DB_MAX_COMMAND_LENGTH*3);
209                                 break;
210                         }
211                         i++;
212                 }
213                 if (cmd_not_found)
214                 {
215                         A_PRINTF("Error, HELP for command list.\n\r");
216                 }
217
218         }
219
220         A_PRINTF(">");
221         return;
222 }
223
224 static uint16_t db_get_cmd_line(uint8_t ch, char *cmd_line, uint16_t *i)
225 {
226         int cmd_buf_loc;
227
228         switch (ch)
229         {
230         case '\\' : /* Last command */
231                 pressed_time++;
232                 if (pressed_time >= COMMAND_BUFFER_SIZE)
233                 {
234                         pressed_time--;
235                 }
236                 cmd_buf_loc = cmd_buf_ptr - pressed_time;
237                 if (cmd_buf_loc < 0)
238                 {
239                         if (cmd_buf_full == TRUE)
240                         {
241                                 cmd_buf_loc += COMMAND_BUFFER_SIZE;
242                         }
243                         else
244                         {
245                                 cmd_buf_loc = 0;
246                         }
247                 }
248
249                 if (A_STRLEN(cmd_buffer[cmd_buf_loc]) != 0)
250                 {
251                         A_STRCPY(cmd_line, cmd_buffer[cmd_buf_loc]);
252                         *i = A_STRLEN(cmd_buffer[cmd_buf_loc]);
253                         A_PRINTF("\r>");
254                         A_PRINTF("%s", cmd_line);
255                 }
256                 break;
257         case 13 : /* Return */
258                 pressed_time = 0;
259                 cmd_line[*i] = 0;
260                 A_PRINTF("\n\r");
261                 if (*i != 0)
262                 {
263                         //Filter duplicated string in command history
264                         if (strcmp(cmd_buffer[(cmd_buf_ptr==0)?(COMMAND_BUFFER_SIZE-1):(cmd_buf_ptr-1)], cmd_line) != 0)
265                         {
266                                 A_STRCPY(cmd_buffer[cmd_buf_ptr++], cmd_line);
267                         }
268                 }
269                 if (cmd_buf_ptr >= COMMAND_BUFFER_SIZE)
270                 {
271                         cmd_buf_ptr = 0;
272                         cmd_buf_full = TRUE;
273                 }
274                 return 1;
275         case '\b' : /* Backspace */
276                 pressed_time = 0;
277                 if (*i > 0)
278                 {
279                         *i = *i-1;
280                         A_PRINTF("\b \b");
281                 }
282                 break;
283         case 0 : //None
284                 break;
285         default :
286                 if ((ch >= ' ') && (ch <= '~'))
287                 {
288                         pressed_time = 0;
289                         if (*i < DB_MAX_COMMAND_LENGTH-2)
290                         {
291                                 if ((ch >= 0x11) && (ch <= 0x7e))
292                                 {
293                                         //if ((buf <= 'z') && (buf >= 'a'))
294                                         //{
295                                         //    buf -= 'a' - 'A';
296                                         //}
297                                         cmd_line[*i] = ch;
298                                         *i = *i + 1;
299                                         A_PRINTF("%c", ch);
300                                 }
301                         }
302                 }
303                 else
304                 {
305                         ch = 7; /* Beep */
306                         A_PRINTF("%c", ch);
307                 }
308                 break;
309         } /* end of switch */
310
311         return 0;
312
313 }
314
315 static int db_formalize_command(char *raw_str,  char *cmd_str)
316 {
317         int i = 0;
318         int j;
319         int k;
320
321
322         for (k=0; k<4; k++)
323         {
324                 /* Remove preceeding spaces */
325                 while (raw_str[i++] == ' '){}
326                 i--;
327
328                 /* Copy command string */
329                 j = 0;
330                 while(raw_str[i] && (raw_str[i] != ' '))
331                 {
332                         if (k == 0)
333                         {
334                                 if ((raw_str[i] <= 'z') && (raw_str[i] >= 'a'))
335                                 {
336                                         raw_str[i] -= 'a' - 'A';
337                                 }
338                                 cmd_str[k*DB_MAX_COMMAND_LENGTH + j++] = raw_str[i++];
339                         }
340                         else
341                         {
342                                 cmd_str[k*DB_MAX_COMMAND_LENGTH + j++] = raw_str[i++];
343                         }
344                 }
345                 cmd_str[k*DB_MAX_COMMAND_LENGTH + j] = 0;
346         }
347         return (int)A_STRLEN(cmd_str);
348 }
349
350 static int db_ascii_to_hex(char *num_str, unsigned long *hex_num)
351 {
352         int i = 0;
353
354         *hex_num = 0;
355         while (num_str[i])
356         {
357                 if ((num_str[i] >= '0') && (num_str[i] <= '9'))
358                 {
359                         *hex_num <<= 4;
360                         *hex_num += (num_str[i] - '0');
361                 }
362                 else if ((num_str[i] >= 'A') && (num_str[i] <= 'F'))
363                 {
364                         *hex_num <<= 4;
365                         *hex_num += (num_str[i] - 'A' + 10);
366                 }
367                 else if ((num_str[i] >= 'a') && (num_str[i] <= 'f'))
368                 {
369                         *hex_num <<= 4;
370                         *hex_num += (num_str[i] - 'a' + 10);
371                 }
372                 else
373                 {
374                         return -1;
375                 }
376                 i++;
377         }
378         return 0;
379 }
380
381 int db_ascii_to_int(char *num_str, unsigned long *int_num)
382 {
383         int i = 0;
384
385         *int_num = 0;
386         while (num_str[i])
387         {
388                 if ((num_str[i] >= '0') && (num_str[i] <= '9'))
389                 {
390                         *int_num *= 10;
391                         *int_num += (num_str[i] - '0');
392                 }
393                 else
394                 {
395                         return -1;
396                 }
397                 i++;
398         }
399         return 0;
400 }
401
402 static int db_hex_to_ascii(unsigned long hex_num, char *num_str)
403 {
404         int i;
405         unsigned long four_bits;
406
407         for (i=7; i>=0; i--)
408         {
409                 four_bits = (hex_num >> i*4) & 0xf;
410                 if (four_bits < 10)
411                 {
412                         num_str[7-i] = four_bits + '0';
413                 }
414                 else
415                 {
416                         num_str[7-i] = four_bits - 10 + 'A';
417                 }
418         }
419         num_str[8] = 0;
420         return 0;
421 }
422
423 int db_help_cmd(char *cmd, char *param1, char *param2, char *param3)
424 {
425         int i;
426
427         i = 0;
428
429         A_PRINTF("%s %s\n", ATH_DEBUGGER_VERSION_STR, ATH_COMMAND_LIST_STR);
430
431         while (command_table[i].cmd_func)
432         {
433                 A_PRINTF("%s\t%s\n\r", command_table[i].cmd_str,
434                                        command_table[i].help_str);
435                 i++;
436         }
437         return i;
438 }
439
440 static int db_ldr_cmd(char *cmd, char *param1, char *param2, char *param3)
441 {
442         unsigned long val;
443         unsigned long addr;
444         char val_str[20];
445         char addr_str[20];
446
447         if (db_ascii_to_hex(param1, &addr) != -1)
448         {
449                 if( addr == 0 )
450                 {
451                         A_PRINTF("Error! bad address 0x%08x.\n\r",
452                                  (unsigned long)addr);
453                         return -1;
454                 }
455                 if (strcmp(cmd, "LDR") == 0)
456                 {
457                         addr &= 0xfffffffc;
458                         //val = *(unsigned long *)addr;
459
460                         val = ioread32(addr);
461                 }
462                 else if (strcmp(cmd, "LDRH") == 0)
463                 {
464                         addr &= 0xfffffffe;
465                         val = HAL_HALF_WORD_REG_READ(addr);
466                 }
467                 else if (strcmp(cmd, "LDRB") == 0)
468                 {
469                 }
470
471                 db_hex_to_ascii(val, val_str);
472                 db_hex_to_ascii(addr, addr_str);
473
474                 A_PRINTF("%s : %s\n\r", addr_str, val_str);
475                 return 0;
476         }
477
478         db_incorect_format();
479         return -1;
480 }
481
482 static int db_str_cmd(char *cmd, char *param1, char *param2, char *param3)
483 {
484         unsigned long val;
485         unsigned long addr;
486         char val_str[20];
487         char addr_str[20];
488
489         if ((A_STRLEN(param2) > 0) &&
490             (db_ascii_to_hex(param1, &addr) != -1) &&
491             (db_ascii_to_hex(param2, &val) != -1))
492         {
493                 if (strcmp(cmd, "STR") == 0)
494                 {
495                         addr &= 0xfffffffc;
496                         iowrite32(addr, val);
497                 }
498
499                 else if (strcmp(cmd, "STRH") == 0)
500                 {
501                         addr &= 0xfffffffe;
502                         //*(volatile unsigned short *)(addr & 0xfffffffe) = (unsigned short)val;
503                         HAL_HALF_WORD_REG_WRITE(addr, val);
504                 }
505                 else if (strcmp(cmd, "STRB") == 0)
506                 {
507                         if( addr & 0x00f00000 )
508                                 HAL_BYTE_REG_WRITE(addr, val);
509                         else
510                                 HAL_BYTE_REG_WRITE(addr^3, val);
511                         //*(volatile unsigned char *)addr = (unsigned char)val;
512                 }
513
514                 db_hex_to_ascii(val, val_str);
515                 db_hex_to_ascii(addr, addr_str);
516
517                 A_PRINTF("%s : %s\n\r", addr_str, val_str);
518                 return 0;
519         }
520
521         db_incorect_format();
522         return -1;
523 }
524
525 LOCAL void dbg_timer_func(A_HANDLE alarm, void *data)
526 {
527         A_PRINTF("this is a timer alarm function 0x%08x\n\r", xthal_get_ccount());
528 }
529
530 uint32_t delay = 0;
531
532 static int db_intr_cmd(char *cmd, char *param1, char *param2, char *param3)
533 {
534 #if SYSTEM_MODULE_INTR
535         uint32_t pending_intrs;
536
537         if(strcmp(param1, "read") == 0 )
538         {
539                 {
540                         /* Update snapshot of pending interrupts */
541
542                         pending_intrs = A_INTR_GET_INTRPENDING();
543
544                         A_PRINTF("intr mask [0x%08x]\n\r", xthal_get_intenable());
545                         A_PRINTF("intr on [0x%08x]\n\r", pending_intrs);
546                 }
547         }
548         else if (strcmp(param1, "timer") == 0 )
549         {
550                 uint32_t data = 0;
551
552                 if (strcmp(param2, "on") == 0 )
553                 {
554                         /* TODO: this part is probably dead. */
555                         pending_intrs = A_INTR_GET_INTRENABLE()|CMNOS_IMASK_XTTIMER;
556                         A_INTR_SET_INTRENABLE(pending_intrs);
557                         A_PRINTF("- intr [0x%08x]\n\r", pending_intrs);
558                 }
559                 else if ( strcmp(param2, "off") == 0 )
560                 {
561                         pending_intrs = A_INTR_GET_INTRENABLE()&(~CMNOS_IMASK_XTTIMER);
562                         A_INTR_SET_INTRENABLE(pending_intrs);
563                         A_PRINTF("- intr [0x%08x]\n\r", pending_intrs);
564             
565                 }
566                 else if( db_ascii_to_hex(param2, &data)==0 )
567                 {
568                         if( data>=0 && data <=10 )
569                                 delay = data;
570                         else
571                                 delay = 3;
572             
573                         A_PRINTF("==>set cb to %d seconds \n\r", delay);
574                 }
575
576         }
577         else
578         {
579                 A_PRINTF("\tintr read - read the interrenable status\n\r");
580                 A_PRINTF("\tintr timer on/off/tick - timer attach on/off/ticks\n\r");
581
582         }
583
584 #endif //#if SYSTEM_MODULE_INTR
585         return 0;
586 }
587
588 static int db_usb_cmd(char *cmd, char *param1, char *param2, char *param3)
589 {
590         A_PRINTF("THIS IS USB COMMAND\n\r");
591
592         if( strcmp(param1, "que") == 0 )
593         {
594                 HIFusb_DescTraceDump();
595         }
596         else
597         {
598                 A_PRINTF("\tusb que - dump descriptor queue\n\r");
599                 A_PRINTF("\tusb fw on/off - enable/disable write fw download to ram\n\r");
600
601         }
602         return 0;
603 }
604
605 static void clk_change(uint32_t clk, uint32_t ratio, uint32_t baud)
606 {
607         uint32_t clk_sel = 0;
608
609         switch(clk){
610         case 22:
611                 clk_sel = 0;
612                 break;
613         case 88:
614                 clk_sel = 1;
615                 break;
616         case 44:
617                 clk_sel = 2;
618                 break;
619         case 117:
620                 clk_sel = 4;
621                 break;
622         case 40:
623                 clk_sel = 6;            
624                 break;
625         default:
626                 clk_sel = 6;
627                 break;
628         }
629
630         iowrite32(0x50040, (0x300|clk_sel|(ratio>>1)<<12));
631         A_UART_HWINIT((clk*1000*1000)/ratio, baud);
632
633 }
634
635 static int db_clock_cmd(char *cmd, char *param1, char *param2, char *param3)
636 {
637         uint32_t ratio = 1;
638         uint32_t baud = 19200;
639         uint32_t clk = 0;
640     
641         if( db_ascii_to_int(param1, &clk) != -1 )
642         {
643                 A_PRINTF("changing clock to %d\n", clk);
644                 clk_change(clk, ratio, baud);
645         }
646 }
647
648 static int db_info_cmd(char *cmd, char *param1, char *param2, char *param3)
649 {
650 #if 1
651
652         if(strcmp(param1, "ram") == 0 )
653         {
654                 A_ALLOCRAM_DEBUG();
655         }
656 #if 0  /* TODO: SYSTEM_MODULE_SYS_MONITOR depends on _ROM_ or _RAM_ which
657         * is dead too */
658         else if(strcmp(param1, "cpu") == 0)
659                 zfPrintCpuUtilization();
660 #endif
661         else   // defalut dump
662                 HIFusb_DescTraceDump();
663
664         return 1;
665
666 #else
667     
668         {
669                 uint32_t ccount1;
670                 uint32_t ccount2;
671
672                 uint32_t data;
673                 register uint32_t data1;
674                 if( db_ascii_to_hex(param1, &data1)==0 )
675                 {
676                         __asm__ __volatile__ (
677                                 "rsr     %0, ccount"
678                                 : "=a" (ccount1) : : "memory"
679                                 );
680                         data = *(volatile uint32_t *)(data1);
681                         __asm__ __volatile__ (
682                                 "rsr     %0, ccount"
683                                 : "=a" (ccount2) : : "memory"
684                                 );
685                         A_PRINTF("\n\rread 0x%08x (0x%08x) use %d clocks\n\r", data1, data, ccount2-ccount1);
686                 }
687
688                 __asm__ __volatile__ (
689                         "rsr     %0, ccount"
690                         : "=a" (ccount1) : : "memory"
691                         );
692                 data = *(volatile uint32_t *)(data1);
693                 __asm__ __volatile__ (
694                         "rsr     %0, ccount"
695                         : "=a" (ccount2) : : "memory"
696                         );
697                 A_PRINTF("\n\rread 0x%08x (0x%08x) use %d clocks\n\r", data1, data, ccount2-ccount1);
698
699
700                 __asm__ __volatile__ (
701                         "rsr     %0, ccount"
702                         : "=a" (ccount1) : : "memory"
703                         );
704                 data = *(volatile uint32_t *)(data2);
705                 __asm__ __volatile__ (
706                         "rsr     %0, ccount"
707                         : "=a" (ccount2) : : "memory"
708                         );
709                 A_PRINTF("read 0x%08x (0x%08x) use %d clocks\n\r", data2, data, ccount2-ccount1);
710
711
712                 __asm__ __volatile__ (
713                         "rsr     %0, ccount"
714                         : "=a" (ccount1) : : "memory"
715                         );
716                 data = *(volatile uint32_t *)(data3);
717                 __asm__ __volatile__ (
718                         "rsr     %0, ccount"
719                         : "=a" (ccount2) : : "memory"
720                         );
721                 A_PRINTF("read 0x%08x (0x%08x) use %d clocks\n\r", data3, data, ccount2-ccount1);
722
723         }
724 #endif
725         return 1;
726 }
727
728 static int db_cmd_starthtc(char *cmd, char *param1, char *param2, char *param3)
729 {
730     extern htc_handle_t htc_handle;
731     HTC_Ready(htc_handle);
732 }
733
734 static int db_wdt_cmd(char *cmd, char *param1, char *param2, char *param3)
735 {
736         if ( strcmp(param1, "rst") == 0 )
737         {
738                 A_PRINTF(" reseting...\n\n\r");
739                 A_WDT_RESET();
740         }
741         else if( strcmp(param1, "on") == 0 )
742         {
743                 A_WDT_ENABLE();
744         }
745         else if (strcmp(param1, "off") == 0 )
746         {
747                 A_WDT_DISABLE();
748         }
749         else if ( strcmp(param1, "boot") == 0 )
750         {
751                 A_PRINTF("Last BOOT is ");
752                 if (ENUM_WDT_BOOT == A_WDT_LASTBOOT() )
753                         A_PRINTF("wdt");
754                 else
755                         A_PRINTF("normal boot");
756         }
757         else if (strcmp(param1, "loop") == 0 )
758         {
759                 T_WDT_CMD wdt_cmd;
760                 uint32_t time_offset;
761                 A_PRINTF(" doing the wdt reseting...");
762
763                 if( db_ascii_to_hex(param2, &time_offset)!=0 )
764                 {
765                         if( time_offset < 0 || time_offset >0xffffffff )
766                                 time_offset = 0xffffff;
767                 }
768                 A_PRINTF(" (wdt tick: 0x%08x...\n\n\r", time_offset);
769                 wdt_cmd.cmd = WDT_TIMEOUT;
770                 wdt_cmd.timeout = time_offset;
771
772                 A_WDT_SET(wdt_cmd);
773                 while(1) ;
774         }
775         else if (strcmp(param1, "noloop") == 0 )
776         {
777                 T_WDT_CMD wdt_cmd;
778                 uint32_t time_offset;
779                 A_PRINTF(" doing the wdt reseting...");
780
781                 if( db_ascii_to_hex(param3, &time_offset)!=0 )
782                 {
783                         if( time_offset < 0 || time_offset >0xffffffff )
784                                 time_offset = 0xffffff;
785                 }
786                 A_PRINTF(" (wdt tick: 0x%08x...\n\n\r", time_offset);
787
788                 wdt_cmd.cmd = WDT_TIMEOUT;
789                 wdt_cmd.timeout = time_offset;
790
791                 A_WDT_SET(wdt_cmd);
792         }
793         else if( strcmp(param1, "event") == 0 )
794         {
795                 uint32_t event= 0x00123400;
796 #define USB_BYTE_REG_WRITE(addr, val)           HAL_BYTE_REG_WRITE(USB_CTRL_BASE_ADDRESS|(uint8_t)(addr^3), (val))
797 #define USB_BYTE_REG_READ(addr)                 HAL_BYTE_REG_READ(USB_CTRL_BASE_ADDRESS|(uint8_t)(addr^3))
798
799                 // disable ep3 intr
800                 USB_BYTE_REG_WRITE(0x17, USB_BYTE_REG_READ(0x17)|0xc0);
801
802                 //ZM_CBUS_FIFO_SIZE_REG = 0xf;
803                 iowrite32_usb(0x100, 0x0f);
804
805                 //ZM_EP3_DATA_REG = event;
806                 iowrite32_usb(0xF8, event);
807
808                 // tx done
809                 USB_BYTE_REG_WRITE(0xAE, USB_BYTE_REG_READ(0xAE)|0x08);
810
811                 // enable ep3 intr
812                 USB_BYTE_REG_WRITE(0x17, USB_BYTE_REG_READ(0x17)&0xbf);
813         }
814 }
815
816 #if defined(PROJECT_K2)
817 #if SYSTEM_MODULE_SFLASH
818 /* Serial Flash -> Chip Erase, Sector Erase, Block Erase */
819 static int db_cmd_sferase(char *cmd, char *param1, char *param2, char *param3)
820 {
821         unsigned long       addr;
822
823         if (strcmp(param1, "s") == 0)
824         {
825                 if (db_ascii_to_hex(param2, &addr) != -1 && addr < SPI_FLASH_MAX_SIZE)
826                 {
827                         /* Sector size is 4K (0x1000) */
828                         A_PRINTF("Sector addr : 0x%08X\n\r", addr - addr%0x1000);
829                         A_SFLASH_ERASE(ZM_SFLASH_SECTOR_ERASE, addr);
830
831                         return 0;
832                 }
833
834                 db_incorect_format();
835                 return -1;
836         }
837         else if (strcmp(param2, "b") == 0)
838         {
839                 if (db_ascii_to_hex(param2, &addr) != -1 && addr < SPI_FLASH_MAX_SIZE)
840                 {
841                         /* Sector size is 64K (0x10000) */
842                         A_PRINTF("Block addr : 0x%08X\n\r", addr - addr%0x10000);
843                         A_SFLASH_ERASE(ZM_SFLASH_BLOCK_ERASE, addr);
844
845                         return 0;
846                 }
847
848                 db_incorect_format();
849                 return -1;
850
851         }
852         else if (strcmp(param1, "c") == 0)
853         {
854                 A_SFLASH_ERASE(ZM_SFLASH_CHIP_ERASE, addr);
855
856                 A_PRINTF("\n\r");
857                 return 0;
858         }
859
860         db_unknown_command();
861         return -1;
862 }
863
864 /* Serial Flash -> Program */
865 static int db_cmd_sfpg(char *cmd, char *param1, char *param2, char *param3)
866 {
867         unsigned long       addr, len, buf;
868
869         if (db_ascii_to_hex(param1, &addr) != -1 &&
870             db_ascii_to_hex(param2, &len) != -1 &&
871             db_ascii_to_hex(param3, &buf) != -1 &&
872             ((addr+len) <= SPI_FLASH_MAX_SIZE) &&
873             addr%4 == 0 && len%4 == 0 && buf%4 == 0 &&
874             ((buf >=0x500000 && buf < 0x528000) || (buf >=0x4e0000 && buf < 0x4e6000)) )
875         {
876                 A_SFLASH_PROG(addr, len, (A_UINT8 *)buf);
877
878                 A_PRINTF("\n\r");
879                 return 0;
880         }
881
882         db_incorect_format();
883         return -1;
884 }
885
886 /* Serial Flash -> Read, Fast Read to UART */
887 static int db_cmd_sfru(char *cmd, char *param1, char *param2, char *param3)
888 {
889         A_UINT32            i;
890         unsigned long       addr1, addr2, t_addr;
891         A_UINT32            fast, val;
892
893         if (strcmp(param1, "r") == 0)
894                 fast = 0;
895         else if (strcmp(param1, "f") == 0)
896                 fast = 1;
897         else
898         {
899                 db_unknown_command();
900                 return -1;
901         }
902
903         if (db_ascii_to_hex(param2, &addr1) != -1 &&
904             db_ascii_to_hex(param3, &addr2) != -1 &&
905             addr1 < addr2 && addr1 < SPI_FLASH_MAX_SIZE &&
906             addr2 < SPI_FLASH_MAX_SIZE && addr1%4 == 0)
907         {
908                 A_PRINTF("addr    data     data     data     data     data     data     data     data\n\r");
909                 A_PRINTF("======  ======== ======== ======== ======== ======== ======== ======== ========");
910
911                 for (i = 0, t_addr = addr1; t_addr < addr2; i++, t_addr += 4)
912                 {
913                         if ((i%8) == 0)
914                                 A_PRINTF("\n\r%06X  ", t_addr);
915
916                         A_SFLASH_READ(fast, t_addr, 4, (A_UINT8 *)&val);
917                         A_PRINTF("%08X ", val);
918                 }
919
920                 A_PRINTF("\n\r");
921                 return 0;
922         }
923
924         db_incorect_format();
925         return -1;
926 }
927
928 /* Serial Flash -> Read, Fast Read to Memory */
929 static int db_cmd_sfrm(char *cmd, char *param1, char *param2, char *param3)
930 {
931         A_UINT32            i;
932         unsigned long       addr1, addr2, t_addr;
933         A_UINT32            fast;
934         A_UINT8             *buf = (A_UINT8 *)0x520000;
935
936         if (strcmp(param1, "r") == 0)
937                 fast = 0;
938         else if (strcmp(param1, "f") == 0)
939                 fast = 1;
940         else
941         {
942                 db_unknown_command();
943                 return -1;
944         }
945
946         if (db_ascii_to_hex(param2, &addr1) != -1 &&
947             db_ascii_to_hex(param3, &addr2) != -1 &&
948             addr1 < addr2 && addr1 < SPI_FLASH_MAX_SIZE &&
949             addr2 < SPI_FLASH_MAX_SIZE && addr1%4 == 0)
950         {
951                 for (i = 0, t_addr = addr1; t_addr < addr2; i++, t_addr += 4)
952                 {
953                         A_SFLASH_READ(fast, t_addr, 4, buf + i*4);
954                 }
955
956                 A_PRINTF("\n\r");
957                 return 0;
958         }
959
960         db_incorect_format();
961         return -1;
962 }
963
964 /* Serial Flash -> Read Status Register */
965 static int db_cmd_sfrdsr(char *cmd, char *param1, char *param2, char *param3)
966 {
967         A_PRINTF("0x%02X\n\r", A_SFLASH_RDSR());
968         return 0;
969 }
970 #endif
971 #endif /* #if defined(PROJECT_K2) */
972
973 /* Memory Comparison */
974 static int db_cmd_memcmp(char *cmd, char *param1, char *param2, char *param3)
975 {
976         unsigned long       addr1, addr2, len;
977         A_UINT8             *buf1, *buf2;
978
979         if (db_ascii_to_hex(param1, &addr1) != -1 &&
980             db_ascii_to_hex(param2, &addr2) != -1 &&
981             db_ascii_to_hex(param3, &len) != -1 &&
982             addr1 != addr2 && addr1%4 == 0 && addr2%4 == 0 && len%4 == 0)
983         {
984                 buf1 = (A_UINT8 *)addr1;
985                 buf2 = (A_UINT8 *)addr2;        ;
986
987                 A_PRINTF("memcmp(buf1, buf2, len) = %d\n\r", A_MEMCMP(buf1, buf2, len));
988                 return 0;
989         }
990
991         db_incorect_format();
992         return -1;
993 }
994
995 /* Memory Dump */
996 static int db_cmd_memdump(char *cmd, char *param1, char *param2, char *param3)
997 {
998         unsigned long       addr1, addr2;
999
1000         if (db_ascii_to_hex(param1, &addr1) != -1 && db_ascii_to_hex(param2, &addr2) != -1 && addr1 < addr2 && addr1%4 == 0)
1001         {
1002                 db_print_dump((const char *)addr1, (const char *)addr2);
1003                 return 0;
1004         }
1005
1006         db_incorect_format();
1007         return -1;
1008 }
1009 void cmnos_dbg_module_install(struct dbg_api *apis)
1010 {
1011         apis->_dbg_init = zf_debug_init;
1012         apis->_dbg_task = zf_debug_task;
1013 }
1014
1015 #endif  /* SYSTEM_MODULE_DBG */
1016