debug-stub.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* debug-stub.c: debug-mode stub
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/string.h>
  12. #include <linux/kernel.h>
  13. #include <linux/signal.h>
  14. #include <linux/sched.h>
  15. #include <linux/init.h>
  16. #include <linux/serial_reg.h>
  17. #include <linux/start_kernel.h>
  18. #include <asm/system.h>
  19. #include <asm/serial-regs.h>
  20. #include <asm/timer-regs.h>
  21. #include <asm/irc-regs.h>
  22. #include <asm/gdb-stub.h>
  23. #include "gdb-io.h"
  24. /* CPU board CON5 */
  25. #define __UART0(X) (*(volatile uint8_t *)(UART0_BASE + (UART_##X)))
  26. #define LSR_WAIT_FOR0(STATE) \
  27. do { \
  28. } while (!(__UART0(LSR) & UART_LSR_##STATE))
  29. #define FLOWCTL_QUERY0(LINE) ({ __UART0(MSR) & UART_MSR_##LINE; })
  30. #define FLOWCTL_CLEAR0(LINE) do { __UART0(MCR) &= ~UART_MCR_##LINE; } while (0)
  31. #define FLOWCTL_SET0(LINE) do { __UART0(MCR) |= UART_MCR_##LINE; } while (0)
  32. #define FLOWCTL_WAIT_FOR0(LINE) \
  33. do { \
  34. gdbstub_do_rx(); \
  35. } while(!FLOWCTL_QUERY(LINE))
  36. struct frv_debug_status __debug_status;
  37. static void __init debug_stub_init(void);
  38. /*****************************************************************************/
  39. /*
  40. * debug mode handler stub
  41. * - we come here with the CPU in debug mode and with exceptions disabled
  42. * - handle debugging services for userspace
  43. */
  44. asmlinkage void debug_stub(void)
  45. {
  46. unsigned long hsr0;
  47. int type = 0;
  48. static u8 inited = 0;
  49. if (!inited) {
  50. debug_stub_init();
  51. type = -1;
  52. inited = 1;
  53. }
  54. hsr0 = __get_HSR(0);
  55. if (hsr0 & HSR0_ETMD)
  56. __set_HSR(0, hsr0 & ~HSR0_ETMD);
  57. /* disable single stepping */
  58. __debug_status.dcr &= ~DCR_SE;
  59. /* kernel mode can propose an exception be handled in debug mode by jumping to a special
  60. * location */
  61. if (__debug_frame->pc == (unsigned long) __break_hijack_kernel_event_breaks_here) {
  62. /* replace the debug frame with the kernel frame and discard
  63. * the top kernel context */
  64. *__debug_frame = *__frame;
  65. __frame = __debug_frame->next_frame;
  66. __debug_status.brr = (__debug_frame->tbr & TBR_TT) << 12;
  67. __debug_status.brr |= BRR_EB;
  68. }
  69. if (__debug_frame->pc == (unsigned long) __debug_bug_trap + 4) {
  70. __debug_frame->pc = __debug_frame->lr;
  71. type = __debug_frame->gr8;
  72. }
  73. #ifdef CONFIG_GDBSTUB
  74. gdbstub(type);
  75. #endif
  76. if (hsr0 & HSR0_ETMD)
  77. __set_HSR(0, __get_HSR(0) | HSR0_ETMD);
  78. } /* end debug_stub() */
  79. /*****************************************************************************/
  80. /*
  81. * debug stub initialisation
  82. */
  83. static void __init debug_stub_init(void)
  84. {
  85. __set_IRR(6, 0xff000000); /* map ERRs to NMI */
  86. __set_IITMR(1, 0x20000000); /* ERR0/1, UART0/1 IRQ detect levels */
  87. asm volatile(" movgs gr0,ibar0 \n"
  88. " movgs gr0,ibar1 \n"
  89. " movgs gr0,ibar2 \n"
  90. " movgs gr0,ibar3 \n"
  91. " movgs gr0,dbar0 \n"
  92. " movgs gr0,dbmr00 \n"
  93. " movgs gr0,dbmr01 \n"
  94. " movgs gr0,dbdr00 \n"
  95. " movgs gr0,dbdr01 \n"
  96. " movgs gr0,dbar1 \n"
  97. " movgs gr0,dbmr10 \n"
  98. " movgs gr0,dbmr11 \n"
  99. " movgs gr0,dbdr10 \n"
  100. " movgs gr0,dbdr11 \n"
  101. );
  102. /* deal with debugging stub initialisation and initial pause */
  103. if (__debug_frame->pc == (unsigned long) __debug_stub_init_break)
  104. __debug_frame->pc = (unsigned long) start_kernel;
  105. /* enable the debug events we want to trap */
  106. __debug_status.dcr = DCR_EBE;
  107. #ifdef CONFIG_GDBSTUB
  108. gdbstub_init();
  109. #endif
  110. __clr_MASK_all();
  111. __clr_MASK(15);
  112. __clr_RC(15);
  113. } /* end debug_stub_init() */
  114. /*****************************************************************************/
  115. /*
  116. * kernel "exit" trap for gdb stub
  117. */
  118. void debug_stub_exit(int status)
  119. {
  120. #ifdef CONFIG_GDBSTUB
  121. gdbstub_exit(status);
  122. #endif
  123. } /* end debug_stub_exit() */
  124. /*****************************************************************************/
  125. /*
  126. * send string to serial port
  127. */
  128. void debug_to_serial(const char *p, int n)
  129. {
  130. char ch;
  131. for (; n > 0; n--) {
  132. ch = *p++;
  133. FLOWCTL_SET0(DTR);
  134. LSR_WAIT_FOR0(THRE);
  135. // FLOWCTL_WAIT_FOR(CTS);
  136. if (ch == 0x0a) {
  137. __UART0(TX) = 0x0d;
  138. mb();
  139. LSR_WAIT_FOR0(THRE);
  140. // FLOWCTL_WAIT_FOR(CTS);
  141. }
  142. __UART0(TX) = ch;
  143. mb();
  144. FLOWCTL_CLEAR0(DTR);
  145. }
  146. } /* end debug_to_serial() */
  147. /*****************************************************************************/
  148. /*
  149. * send string to serial port
  150. */
  151. void debug_to_serial2(const char *fmt, ...)
  152. {
  153. va_list va;
  154. char buf[64];
  155. int n;
  156. va_start(va, fmt);
  157. n = vsprintf(buf, fmt, va);
  158. va_end(va);
  159. debug_to_serial(buf, n);
  160. } /* end debug_to_serial2() */
  161. /*****************************************************************************/
  162. /*
  163. * set up the ttyS0 serial port baud rate timers
  164. */
  165. void __init console_set_baud(unsigned baud)
  166. {
  167. unsigned value, high, low;
  168. u8 lcr;
  169. /* work out the divisor to give us the nearest higher baud rate */
  170. value = __serial_clock_speed_HZ / 16 / baud;
  171. /* determine the baud rate range */
  172. high = __serial_clock_speed_HZ / 16 / value;
  173. low = __serial_clock_speed_HZ / 16 / (value + 1);
  174. /* pick the nearest bound */
  175. if (low + (high - low) / 2 > baud)
  176. value++;
  177. lcr = __UART0(LCR);
  178. __UART0(LCR) |= UART_LCR_DLAB;
  179. mb();
  180. __UART0(DLL) = value & 0xff;
  181. __UART0(DLM) = (value >> 8) & 0xff;
  182. mb();
  183. __UART0(LCR) = lcr;
  184. mb();
  185. } /* end console_set_baud() */
  186. /*****************************************************************************/
  187. /*
  188. *
  189. */
  190. int __init console_get_baud(void)
  191. {
  192. unsigned value;
  193. u8 lcr;
  194. lcr = __UART0(LCR);
  195. __UART0(LCR) |= UART_LCR_DLAB;
  196. mb();
  197. value = __UART0(DLM) << 8;
  198. value |= __UART0(DLL);
  199. __UART0(LCR) = lcr;
  200. mb();
  201. return value;
  202. } /* end console_get_baud() */
  203. /*****************************************************************************/
  204. /*
  205. * display BUG() info
  206. */
  207. #ifndef CONFIG_NO_KERNEL_MSG
  208. void __debug_bug_printk(const char *file, unsigned line)
  209. {
  210. printk("kernel BUG at %s:%d!\n", file, line);
  211. } /* end __debug_bug_printk() */
  212. #endif