gdb-io.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* gdb-io.c: FR403 GDB stub I/O
  2. *
  3. * Copyright (C) 2003 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/mm.h>
  16. #include <linux/console.h>
  17. #include <linux/init.h>
  18. #include <linux/serial_reg.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/system.h>
  21. #include <asm/irc-regs.h>
  22. #include <asm/timer-regs.h>
  23. #include <asm/gdb-stub.h>
  24. #include "gdb-io.h"
  25. #ifdef CONFIG_GDBSTUB_UART0
  26. #define __UART(X) (*(volatile uint8_t *)(UART0_BASE + (UART_##X)))
  27. #define __UART_IRR_NMI 0xff0f0000
  28. #else /* CONFIG_GDBSTUB_UART1 */
  29. #define __UART(X) (*(volatile uint8_t *)(UART1_BASE + (UART_##X)))
  30. #define __UART_IRR_NMI 0xfff00000
  31. #endif
  32. #define LSR_WAIT_FOR(STATE) \
  33. do { \
  34. gdbstub_do_rx(); \
  35. } while (!(__UART(LSR) & UART_LSR_##STATE))
  36. #define FLOWCTL_QUERY(LINE) ({ __UART(MSR) & UART_MSR_##LINE; })
  37. #define FLOWCTL_CLEAR(LINE) do { __UART(MCR) &= ~UART_MCR_##LINE; mb(); } while (0)
  38. #define FLOWCTL_SET(LINE) do { __UART(MCR) |= UART_MCR_##LINE; mb(); } while (0)
  39. #define FLOWCTL_WAIT_FOR(LINE) \
  40. do { \
  41. gdbstub_do_rx(); \
  42. } while(!FLOWCTL_QUERY(LINE))
  43. /*****************************************************************************/
  44. /*
  45. * initialise the GDB stub
  46. * - called with PSR.ET==0, so can't incur external interrupts
  47. */
  48. void gdbstub_io_init(void)
  49. {
  50. /* set up the serial port */
  51. __UART(LCR) = UART_LCR_WLEN8; /* 1N8 */
  52. __UART(FCR) =
  53. UART_FCR_ENABLE_FIFO |
  54. UART_FCR_CLEAR_RCVR |
  55. UART_FCR_CLEAR_XMIT |
  56. UART_FCR_TRIGGER_1;
  57. FLOWCTL_CLEAR(DTR);
  58. FLOWCTL_SET(RTS);
  59. // gdbstub_set_baud(115200);
  60. /* we want to get serial receive interrupts */
  61. __UART(IER) = UART_IER_RDI | UART_IER_RLSI;
  62. mb();
  63. __set_IRR(6, __UART_IRR_NMI); /* map ERRs and UARTx to NMI */
  64. } /* end gdbstub_io_init() */
  65. /*****************************************************************************/
  66. /*
  67. * set up the GDB stub serial port baud rate timers
  68. */
  69. void gdbstub_set_baud(unsigned baud)
  70. {
  71. unsigned value, high, low;
  72. u8 lcr;
  73. /* work out the divisor to give us the nearest higher baud rate */
  74. value = __serial_clock_speed_HZ / 16 / baud;
  75. /* determine the baud rate range */
  76. high = __serial_clock_speed_HZ / 16 / value;
  77. low = __serial_clock_speed_HZ / 16 / (value + 1);
  78. /* pick the nearest bound */
  79. if (low + (high - low) / 2 > baud)
  80. value++;
  81. lcr = __UART(LCR);
  82. __UART(LCR) |= UART_LCR_DLAB;
  83. mb();
  84. __UART(DLL) = value & 0xff;
  85. __UART(DLM) = (value >> 8) & 0xff;
  86. mb();
  87. __UART(LCR) = lcr;
  88. mb();
  89. } /* end gdbstub_set_baud() */
  90. /*****************************************************************************/
  91. /*
  92. * receive characters into the receive FIFO
  93. */
  94. void gdbstub_do_rx(void)
  95. {
  96. unsigned ix, nix;
  97. ix = gdbstub_rx_inp;
  98. while (__UART(LSR) & UART_LSR_DR) {
  99. nix = (ix + 2) & 0xfff;
  100. if (nix == gdbstub_rx_outp)
  101. break;
  102. gdbstub_rx_buffer[ix++] = __UART(LSR);
  103. gdbstub_rx_buffer[ix++] = __UART(RX);
  104. ix = nix;
  105. }
  106. gdbstub_rx_inp = ix;
  107. __clr_RC(15);
  108. __clr_IRL();
  109. } /* end gdbstub_do_rx() */
  110. /*****************************************************************************/
  111. /*
  112. * wait for a character to come from the debugger
  113. */
  114. int gdbstub_rx_char(unsigned char *_ch, int nonblock)
  115. {
  116. unsigned ix;
  117. u8 ch, st;
  118. *_ch = 0xff;
  119. if (gdbstub_rx_unget) {
  120. *_ch = gdbstub_rx_unget;
  121. gdbstub_rx_unget = 0;
  122. return 0;
  123. }
  124. try_again:
  125. gdbstub_do_rx();
  126. /* pull chars out of the buffer */
  127. ix = gdbstub_rx_outp;
  128. if (ix == gdbstub_rx_inp) {
  129. if (nonblock)
  130. return -EAGAIN;
  131. //watchdog_alert_counter = 0;
  132. goto try_again;
  133. }
  134. st = gdbstub_rx_buffer[ix++];
  135. ch = gdbstub_rx_buffer[ix++];
  136. gdbstub_rx_outp = ix & 0x00000fff;
  137. if (st & UART_LSR_BI) {
  138. gdbstub_proto("### GDB Rx Break Detected ###\n");
  139. return -EINTR;
  140. }
  141. else if (st & (UART_LSR_FE|UART_LSR_OE|UART_LSR_PE)) {
  142. gdbstub_io("### GDB Rx Error (st=%02x) ###\n",st);
  143. return -EIO;
  144. }
  145. else {
  146. gdbstub_io("### GDB Rx %02x (st=%02x) ###\n",ch,st);
  147. *_ch = ch & 0x7f;
  148. return 0;
  149. }
  150. } /* end gdbstub_rx_char() */
  151. /*****************************************************************************/
  152. /*
  153. * send a character to the debugger
  154. */
  155. void gdbstub_tx_char(unsigned char ch)
  156. {
  157. FLOWCTL_SET(DTR);
  158. LSR_WAIT_FOR(THRE);
  159. // FLOWCTL_WAIT_FOR(CTS);
  160. if (ch == 0x0a) {
  161. __UART(TX) = 0x0d;
  162. mb();
  163. LSR_WAIT_FOR(THRE);
  164. // FLOWCTL_WAIT_FOR(CTS);
  165. }
  166. __UART(TX) = ch;
  167. mb();
  168. FLOWCTL_CLEAR(DTR);
  169. } /* end gdbstub_tx_char() */
  170. /*****************************************************************************/
  171. /*
  172. * send a character to the debugger
  173. */
  174. void gdbstub_tx_flush(void)
  175. {
  176. LSR_WAIT_FOR(TEMT);
  177. LSR_WAIT_FOR(THRE);
  178. FLOWCTL_CLEAR(DTR);
  179. } /* end gdbstub_tx_flush() */