Merge pull request #21 from olerem/buildfixes
[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 #if defined(PROJECT_K2)
39 #if SYSTEM_MODULE_SFLASH
40 #include "sflash_api.h"
41 #endif
42 #endif /* #if defined(PROJECT_K2) */
43
44 #if defined(SYSTEM_MODULE_DBG)
45
46 /* Function prototypes */
47 static int db_help_cmd(char *, char *, char *, char *);
48 static int db_ldr_cmd(char *, char *, char *, char *);
49 static int db_str_cmd(char *, char *, char *, char *);
50 static int db_info_cmd(char *, char *, char *, char *);
51 static int db_usb_cmd(char *, char *, char *, char *);
52 static int db_intr_cmd(char *, char *, char *, char *);
53
54 static int db_cmd_starthtc(char *cmd, char *param1, char *param2, char *param3);
55
56 static int db_wdt_cmd(char *cmd, char *param1, char *param2, char *param3);
57
58 #if defined(PROJECT_K2)
59 #if SYSTEM_MODULE_SFLASH
60 static int db_cmd_sferase(char *cmd, char *param1, char *param2, char *param3);
61 static int db_cmd_sfpg(char *cmd, char *param1, char *param2, char *param3);
62 static int db_cmd_sfru(char *cmd, char *param1, char *param2, char *param3);
63 static int db_cmd_sfrm(char *cmd, char *param1, char *param2, char *param3);
64 static int db_cmd_sfrdsr(char *cmd, char *param1, char *param2, char *param3);
65 #endif
66 #endif /* #if defined(PROJECT_K2) */
67 static int db_cmd_memcmp(char *cmd, char *param1, char *param2, char *param3);
68 static int db_cmd_memdump(char *cmd, char *param1, char *param2, char *param3);
69
70 static int db_clock_cmd(char *cmd, char *param1, char *param2, char *param3);
71
72 static uint16_t db_get_cmd_line(uint8_t ch, char *cmd_line, uint16_t *i);
73 static int db_formalize_command(char *, char *);
74 static int db_ascii_to_hex(char *, unsigned long *);
75 static int db_hex_to_ascii(unsigned long, char *);
76 static void zf_debug_task(void);
77
78 /* Console debug command table */
79 const struct DB_COMMAND_STRUCT command_table[] =
80 {
81         {"HELP",   ", List all debug commands", db_help_cmd},
82         {"?",      ", Equal to HELP comamnd", db_help_cmd},
83
84         /* Basic load/store/dump command */
85         {"LDR",    "<Hex addr>, Load word", db_ldr_cmd},
86         {"LDRH",   "<Hex addr>, Load half word", db_ldr_cmd},
87         {"LDRB",   "<Hex addr>, Load byte", db_ldr_cmd},
88         {"STR",    "<Hex addr> <Hex value>, Store word", db_str_cmd},
89         {"STRH",   "<Hex addr> <Hex value>, Store half word", db_str_cmd},
90         {"STRB",   "<Hex addr> <Hex value>, Store byte", db_str_cmd},
91         {"INFO",   ", Print debug information", db_info_cmd},
92         {"USB",   ", usb releated command", db_usb_cmd},
93         {"INTR",   ", intr releated command", db_intr_cmd},
94         {"CLOCK",    ", change the clock...", db_clock_cmd},
95         {"HTCR", "Issue HTC ready to host", db_cmd_starthtc},
96         {"WDT",   ", wdt debug command", db_wdt_cmd},
97 #if defined(PROJECT_K2)
98 #if SYSTEM_MODULE_SFLASH
99         {"SFE", ", S<Hex>/B<Hex>/C, SPI Flash chip erase", db_cmd_sferase},
100         {"SFPG", "<Hex addr> <Hex len> <Hex buf>, SPI Flash program", db_cmd_sfpg},
101         {"SFRU", "f/r <Hex addr> <Hex addr>, SPI Flash fast read/read to UART", db_cmd_sfru},
102         {"SFRM", "f/r <Hex addr> <Hex addr>, SPI Flash fast read/read to Memory 0x520000", db_cmd_sfrm},
103         {"SFRDSR", ", SPI Flash status register read", db_cmd_sfrdsr},
104 #endif
105 #endif /* #if defined(PROJECT_K2) */
106         {"MEMCMP", "<Hex addr> <Hex addr> <Hex len>, memory comparison", db_cmd_memcmp},
107         {"MEMDMP", "<Hex addr> <Hex addr>, memory dump", db_cmd_memdump},
108         {"", "", 0}
109         /* {Command, Help description, function} */
110 };
111
112 char cmd_buffer[COMMAND_BUFFER_SIZE][DB_MAX_COMMAND_LENGTH]; /* Backup previous command */
113 int cmd_buf_ptr;
114 int cmd_buf_full;
115 char raw_cmd[DB_MAX_COMMAND_LENGTH];
116 char cmd_str[DB_MAX_COMMAND_LENGTH*4];
117 int cmd_not_found;
118 uint16_t gvLen;
119 int pressed_time;
120
121 static void db_incorect_format(void)
122 {
123         A_PRINTF("Error! Incorrect format.\n\r");
124 }
125
126 static void db_unknown_command(void)
127 {
128         A_PRINTF("Error! Unknown command.\n\r");
129 }
130
131 static void db_print_dump(const char *mem1, const char *mem2)
132 {
133         unsigned int i = 0;
134         const char *tmp;
135
136         do {
137                 if (i == 0) {
138                         A_PRINTF("\n\r%06x: ", mem1);
139                         tmp = mem1;
140                 }
141
142                 A_PRINTF("%04x ", *(uint16_t *)mem1);
143
144                 mem1 += 2;
145                 i++;
146
147                 if (i == 8) {
148                         A_PRINTF(" ");
149                         do {
150                                 if (*tmp > 0x20 && *tmp < 0x7e)
151                                         A_PRINTF("%c", *tmp);
152                                 else
153                                         A_PRINTF(".");
154                                 tmp++;
155                         } while (tmp < mem1);
156                         i = 0;
157                 }
158         } while (mem1 < mem2);
159         A_PRINTF("\n\r");
160 }
161
162 static void zf_debug_init(void)
163 {
164         uint8_t ch;
165
166         /* Purge Rx FIFO */
167         while ((zm_get_char(&ch)) != 0)
168         {
169         }
170
171         cmd_buf_ptr = 0;
172         cmd_buf_full = FALSE;
173         gvLen = 0;
174         pressed_time = 0;
175 }
176
177 static void zf_debug_task(void)
178 {
179         int i;
180         uint8_t ch;
181
182         if ((zm_get_char(&ch)) == 0)
183         {
184                 return;
185         }
186
187         if (db_get_cmd_line(ch, raw_cmd, &gvLen) == 0)
188         {
189                 return;
190         }
191
192         if (db_formalize_command(raw_cmd, cmd_str))
193         {
194                 gvLen = 0;
195                 i = 0;
196
197                 cmd_not_found = TRUE;
198                 while(command_table[i].cmd_func)
199                 {
200                         if (!strcmp(command_table[i].cmd_str, cmd_str))
201                         {
202                                 cmd_not_found = FALSE;
203                                 command_table[i].cmd_func(cmd_str,
204                                                           cmd_str+DB_MAX_COMMAND_LENGTH,
205                                                           cmd_str+DB_MAX_COMMAND_LENGTH*2,
206                                                           cmd_str+DB_MAX_COMMAND_LENGTH*3);
207                                 break;
208                         }
209                         i++;
210                 }
211                 if (cmd_not_found)
212                 {
213                         A_PRINTF("Error, HELP for command list.\n\r");
214                 }
215
216         }
217
218         A_PRINTF(">");
219         return;
220 }
221
222 static uint16_t db_get_cmd_line(uint8_t ch, char *cmd_line, uint16_t *i)
223 {
224         int cmd_buf_loc;
225
226         switch (ch)
227         {
228         case '\\' : /* Last command */
229                 pressed_time++;
230                 if (pressed_time >= COMMAND_BUFFER_SIZE)
231                 {
232                         pressed_time--;
233                 }
234                 cmd_buf_loc = cmd_buf_ptr - pressed_time;
235                 if (cmd_buf_loc < 0)
236                 {
237                         if (cmd_buf_full == TRUE)
238                         {
239                                 cmd_buf_loc += COMMAND_BUFFER_SIZE;
240                         }
241                         else
242                         {
243                                 cmd_buf_loc = 0;
244                         }
245                 }
246
247                 if (A_STRLEN(cmd_buffer[cmd_buf_loc]) != 0)
248                 {
249                         A_STRCPY(cmd_line, cmd_buffer[cmd_buf_loc]);
250                         *i = A_STRLEN(cmd_buffer[cmd_buf_loc]);
251                         A_PRINTF("\r>");
252                         A_PRINTF("%s", cmd_line);
253                 }
254                 break;
255         case 13 : /* Return */
256                 pressed_time = 0;
257                 cmd_line[*i] = 0;
258                 A_PRINTF("\n\r");
259                 if (*i != 0)
260                 {
261                         //Filter duplicated string in command history
262                         if (strcmp(cmd_buffer[(cmd_buf_ptr==0)?(COMMAND_BUFFER_SIZE-1):(cmd_buf_ptr-1)], cmd_line) != 0)
263                         {
264                                 A_STRCPY(cmd_buffer[cmd_buf_ptr++], cmd_line);
265                         }
266                 }
267                 if (cmd_buf_ptr >= COMMAND_BUFFER_SIZE)
268                 {
269                         cmd_buf_ptr = 0;
270                         cmd_buf_full = TRUE;
271                 }
272                 return 1;
273         case '\b' : /* Backspace */
274                 pressed_time = 0;
275                 if (*i > 0)
276                 {
277                         *i = *i-1;
278                         A_PRINTF("\b \b");
279                 }
280                 break;
281         case 0 : //None
282                 break;
283         default :
284                 if ((ch >= ' ') && (ch <= '~'))
285                 {
286                         pressed_time = 0;
287                         if (*i < DB_MAX_COMMAND_LENGTH-2)
288                         {
289                                 if ((ch >= 0x11) && (ch <= 0x7e))
290                                 {
291                                         //if ((buf <= 'z') && (buf >= 'a'))
292                                         //{
293                                         //    buf -= 'a' - 'A';
294                                         //}
295                                         cmd_line[*i] = ch;
296                                         *i = *i + 1;
297                                         A_PRINTF("%c", ch);
298                                 }
299                         }
300                 }
301                 else
302                 {
303                         ch = 7; /* Beep */
304                         A_PRINTF("%c", ch);
305                 }
306                 break;
307         } /* end of switch */
308
309         return 0;
310
311 }
312
313 static int db_formalize_command(char *raw_str,  char *cmd_str)
314 {
315         int i = 0;
316         int j;
317         int k;
318
319
320         for (k=0; k<4; k++)
321         {
322                 /* Remove preceeding spaces */
323                 while (raw_str[i++] == ' '){}
324                 i--;
325
326                 /* Copy command string */
327                 j = 0;
328                 while(raw_str[i] && (raw_str[i] != ' '))
329                 {
330                         if (k == 0)
331                         {
332                                 if ((raw_str[i] <= 'z') && (raw_str[i] >= 'a'))
333                                 {
334                                         raw_str[i] -= 'a' - 'A';
335                                 }
336                                 cmd_str[k*DB_MAX_COMMAND_LENGTH + j++] = raw_str[i++];
337                         }
338                         else
339                         {
340                                 cmd_str[k*DB_MAX_COMMAND_LENGTH + j++] = raw_str[i++];
341                         }
342                 }
343                 cmd_str[k*DB_MAX_COMMAND_LENGTH + j] = 0;
344         }
345         return (int)A_STRLEN(cmd_str);
346 }
347
348 static int db_ascii_to_hex(char *num_str, unsigned long *hex_num)
349 {
350         int i = 0;
351
352         *hex_num = 0;
353         while (num_str[i])
354         {
355                 if ((num_str[i] >= '0') && (num_str[i] <= '9'))
356                 {
357                         *hex_num <<= 4;
358                         *hex_num += (num_str[i] - '0');
359                 }
360                 else if ((num_str[i] >= 'A') && (num_str[i] <= 'F'))
361                 {
362                         *hex_num <<= 4;
363                         *hex_num += (num_str[i] - 'A' + 10);
364                 }
365                 else if ((num_str[i] >= 'a') && (num_str[i] <= 'f'))
366                 {
367                         *hex_num <<= 4;
368                         *hex_num += (num_str[i] - 'a' + 10);
369                 }
370                 else
371                 {
372                         return -1;
373                 }
374                 i++;
375         }
376         return 0;
377 }
378
379 int db_ascii_to_int(char *num_str, unsigned long *int_num)
380 {
381         int i = 0;
382
383         *int_num = 0;
384         while (num_str[i])
385         {
386                 if ((num_str[i] >= '0') && (num_str[i] <= '9'))
387                 {
388                         *int_num *= 10;
389                         *int_num += (num_str[i] - '0');
390                 }
391                 else
392                 {
393                         return -1;
394                 }
395                 i++;
396         }
397         return 0;
398 }
399
400 static int db_hex_to_ascii(unsigned long hex_num, char *num_str)
401 {
402         int i;
403         unsigned long four_bits;
404
405         for (i=7; i>=0; i--)
406         {
407                 four_bits = (hex_num >> i*4) & 0xf;
408                 if (four_bits < 10)
409                 {
410                         num_str[7-i] = four_bits + '0';
411                 }
412                 else
413                 {
414                         num_str[7-i] = four_bits - 10 + 'A';
415                 }
416         }
417         num_str[8] = 0;
418         return 0;
419 }
420
421 int db_help_cmd(char *cmd, char *param1, char *param2, char *param3)
422 {
423         int i;
424
425         i = 0;
426
427         A_PRINTF("%s %s\n", ATH_DEBUGGER_VERSION_STR, ATH_COMMAND_LIST_STR);
428
429         while (command_table[i].cmd_func)
430         {
431                 A_PRINTF("%s\t%s\n\r", command_table[i].cmd_str,
432                                        command_table[i].help_str);
433                 i++;
434         }
435         return i;
436 }
437
438 static int db_ldr_cmd(char *cmd, char *param1, char *param2, char *param3)
439 {
440         unsigned long val;
441         unsigned long addr;
442         char val_str[20];
443         char addr_str[20];
444
445         if (db_ascii_to_hex(param1, &addr) != -1)
446         {
447                 if( addr == 0 )
448                 {
449                         A_PRINTF("Error! bad address 0x%08x.\n\r",
450                                  (unsigned long)addr);
451                         return -1;
452                 }
453                 if (strcmp(cmd, "LDR") == 0)
454                 {
455                         addr &= 0xfffffffc;
456                         //val = *(unsigned long *)addr;
457
458                         val = HAL_WORD_REG_READ(addr);
459                 }
460                 else if (strcmp(cmd, "LDRH") == 0)
461                 {
462                         addr &= 0xfffffffe;
463                         val = HAL_HALF_WORD_REG_READ(addr);
464                 }
465                 else if (strcmp(cmd, "LDRB") == 0)
466                 {
467                 }
468
469                 db_hex_to_ascii(val, val_str);
470                 db_hex_to_ascii(addr, addr_str);
471
472                 A_PRINTF("%s : %s\n\r", addr_str, val_str);
473                 return 0;
474         }
475
476         db_incorect_format();
477         return -1;
478 }
479
480 static int db_str_cmd(char *cmd, char *param1, char *param2, char *param3)
481 {
482         unsigned long val;
483         unsigned long addr;
484         char val_str[20];
485         char addr_str[20];
486
487         if ((A_STRLEN(param2) > 0) &&
488             (db_ascii_to_hex(param1, &addr) != -1) &&
489             (db_ascii_to_hex(param2, &val) != -1))
490         {
491                 if (strcmp(cmd, "STR") == 0)
492                 {
493                         addr &= 0xfffffffc;
494                         //HAL_WORD_REG_WRITE(addr, val);
495                         HAL_WORD_REG_WRITE(addr, val);
496                         //*(volatile unsigned long *)(addr & 0xfffffffc) = (unsigned long)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         HAL_WORD_REG_WRITE(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 #define USB_WORD_REG_WRITE(addr, val)           HAL_WORD_REG_WRITE(USB_CTRL_BASE_ADDRESS|(uint32_t)(addr), (val))
800 #define USB_WORD_REG_READ(addr)                 HAL_WORD_REG_READ(USB_CTRL_BASE_ADDRESS|(uint32_t)(addr))
801
802                 // disable ep3 intr
803                 USB_BYTE_REG_WRITE(0x17, USB_BYTE_REG_READ(0x17)|0xc0);
804
805                 //ZM_CBUS_FIFO_SIZE_REG = 0xf;
806                 USB_WORD_REG_WRITE(0x100, 0x0f);
807
808                 //ZM_EP3_DATA_REG = event;
809                 USB_WORD_REG_WRITE(0xF8, event);
810
811                 // tx done
812                 USB_BYTE_REG_WRITE(0xAE, USB_BYTE_REG_READ(0xAE)|0x08);
813
814                 // enable ep3 intr
815                 USB_BYTE_REG_WRITE(0x17, USB_BYTE_REG_READ(0x17)&0xbf);
816         }
817 }
818
819 #if defined(PROJECT_K2)
820 #if SYSTEM_MODULE_SFLASH
821 /* Serial Flash -> Chip Erase, Sector Erase, Block Erase */
822 static int db_cmd_sferase(char *cmd, char *param1, char *param2, char *param3)
823 {
824         unsigned long       addr;
825
826         if (strcmp(param1, "s") == 0)
827         {
828                 if (db_ascii_to_hex(param2, &addr) != -1 && addr < SPI_FLASH_MAX_SIZE)
829                 {
830                         /* Sector size is 4K (0x1000) */
831                         A_PRINTF("Sector addr : 0x%08X\n\r", addr - addr%0x1000);
832                         A_SFLASH_ERASE(ZM_SFLASH_SECTOR_ERASE, addr);
833
834                         return 0;
835                 }
836
837                 db_incorect_format();
838                 return -1;
839         }
840         else if (strcmp(param2, "b") == 0)
841         {
842                 if (db_ascii_to_hex(param2, &addr) != -1 && addr < SPI_FLASH_MAX_SIZE)
843                 {
844                         /* Sector size is 64K (0x10000) */
845                         A_PRINTF("Block addr : 0x%08X\n\r", addr - addr%0x10000);
846                         A_SFLASH_ERASE(ZM_SFLASH_BLOCK_ERASE, addr);
847
848                         return 0;
849                 }
850
851                 db_incorect_format();
852                 return -1;
853
854         }
855         else if (strcmp(param1, "c") == 0)
856         {
857                 A_SFLASH_ERASE(ZM_SFLASH_CHIP_ERASE, addr);
858
859                 A_PRINTF("\n\r");
860                 return 0;
861         }
862
863         db_unknown_command();
864         return -1;
865 }
866
867 /* Serial Flash -> Program */
868 static int db_cmd_sfpg(char *cmd, char *param1, char *param2, char *param3)
869 {
870         unsigned long       addr, len, buf;
871
872         if (db_ascii_to_hex(param1, &addr) != -1 &&
873             db_ascii_to_hex(param2, &len) != -1 &&
874             db_ascii_to_hex(param3, &buf) != -1 &&
875             ((addr+len) <= SPI_FLASH_MAX_SIZE) &&
876             addr%4 == 0 && len%4 == 0 && buf%4 == 0 &&
877             ((buf >=0x500000 && buf < 0x528000) || (buf >=0x4e0000 && buf < 0x4e6000)) )
878         {
879                 A_SFLASH_PROG(addr, len, (A_UINT8 *)buf);
880
881                 A_PRINTF("\n\r");
882                 return 0;
883         }
884
885         db_incorect_format();
886         return -1;
887 }
888
889 /* Serial Flash -> Read, Fast Read to UART */
890 static int db_cmd_sfru(char *cmd, char *param1, char *param2, char *param3)
891 {
892         A_UINT32            i;
893         unsigned long       addr1, addr2, t_addr;
894         A_UINT32            fast, val;
895
896         if (strcmp(param1, "r") == 0)
897                 fast = 0;
898         else if (strcmp(param1, "f") == 0)
899                 fast = 1;
900         else
901         {
902                 db_unknown_command();
903                 return -1;
904         }
905
906         if (db_ascii_to_hex(param2, &addr1) != -1 &&
907             db_ascii_to_hex(param3, &addr2) != -1 &&
908             addr1 < addr2 && addr1 < SPI_FLASH_MAX_SIZE &&
909             addr2 < SPI_FLASH_MAX_SIZE && addr1%4 == 0)
910         {
911                 A_PRINTF("addr    data     data     data     data     data     data     data     data\n\r");
912                 A_PRINTF("======  ======== ======== ======== ======== ======== ======== ======== ========");
913
914                 for (i = 0, t_addr = addr1; t_addr < addr2; i++, t_addr += 4)
915                 {
916                         if ((i%8) == 0)
917                                 A_PRINTF("\n\r%06X  ", t_addr);
918
919                         A_SFLASH_READ(fast, t_addr, 4, (A_UINT8 *)&val);
920                         A_PRINTF("%08X ", val);
921                 }
922
923                 A_PRINTF("\n\r");
924                 return 0;
925         }
926
927         db_incorect_format();
928         return -1;
929 }
930
931 /* Serial Flash -> Read, Fast Read to Memory */
932 static int db_cmd_sfrm(char *cmd, char *param1, char *param2, char *param3)
933 {
934         A_UINT32            i;
935         unsigned long       addr1, addr2, t_addr;
936         A_UINT32            fast;
937         A_UINT8             *buf = (A_UINT8 *)0x520000;
938
939         if (strcmp(param1, "r") == 0)
940                 fast = 0;
941         else if (strcmp(param1, "f") == 0)
942                 fast = 1;
943         else
944         {
945                 db_unknown_command();
946                 return -1;
947         }
948
949         if (db_ascii_to_hex(param2, &addr1) != -1 &&
950             db_ascii_to_hex(param3, &addr2) != -1 &&
951             addr1 < addr2 && addr1 < SPI_FLASH_MAX_SIZE &&
952             addr2 < SPI_FLASH_MAX_SIZE && addr1%4 == 0)
953         {
954                 for (i = 0, t_addr = addr1; t_addr < addr2; i++, t_addr += 4)
955                 {
956                         A_SFLASH_READ(fast, t_addr, 4, buf + i*4);
957                 }
958
959                 A_PRINTF("\n\r");
960                 return 0;
961         }
962
963         db_incorect_format();
964         return -1;
965 }
966
967 /* Serial Flash -> Read Status Register */
968 static int db_cmd_sfrdsr(char *cmd, char *param1, char *param2, char *param3)
969 {
970         A_PRINTF("0x%02X\n\r", A_SFLASH_RDSR());
971         return 0;
972 }
973 #endif
974 #endif /* #if defined(PROJECT_K2) */
975
976 /* Memory Comparison */
977 static int db_cmd_memcmp(char *cmd, char *param1, char *param2, char *param3)
978 {
979         unsigned long       addr1, addr2, len;
980         A_UINT8             *buf1, *buf2;
981
982         if (db_ascii_to_hex(param1, &addr1) != -1 &&
983             db_ascii_to_hex(param2, &addr2) != -1 &&
984             db_ascii_to_hex(param3, &len) != -1 &&
985             addr1 != addr2 && addr1%4 == 0 && addr2%4 == 0 && len%4 == 0)
986         {
987                 buf1 = (A_UINT8 *)addr1;
988                 buf2 = (A_UINT8 *)addr2;        ;
989
990                 A_PRINTF("memcmp(buf1, buf2, len) = %d\n\r", A_MEMCMP(buf1, buf2, len));
991                 return 0;
992         }
993
994         db_incorect_format();
995         return -1;
996 }
997
998 /* Memory Dump */
999 static int db_cmd_memdump(char *cmd, char *param1, char *param2, char *param3)
1000 {
1001         unsigned long       addr1, addr2;
1002
1003         if (db_ascii_to_hex(param1, &addr1) != -1 && db_ascii_to_hex(param2, &addr2) != -1 && addr1 < addr2 && addr1%4 == 0)
1004         {
1005                 db_print_dump((const char *)addr1, (const char *)addr2);
1006                 return 0;
1007         }
1008
1009         db_incorect_format();
1010         return -1;
1011 }
1012 void cmnos_dbg_module_install(struct dbg_api *apis)
1013 {
1014         apis->_dbg_init = zf_debug_init;
1015         apis->_dbg_task = zf_debug_task;
1016 }
1017
1018 #endif  /* SYSTEM_MODULE_DBG */
1019