debug-stub.c 6.0 KB

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