gdb-io-serial.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* 16550 serial driver for gdbstub I/O
  2. *
  3. * Copyright (C) 2007 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 Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, 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/nmi.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/gdb-stub.h>
  21. #include <asm/exceptions.h>
  22. #include <asm/serial-regs.h>
  23. #include <unit/serial.h>
  24. #include <asm/smp.h>
  25. /*
  26. * initialise the GDB stub
  27. */
  28. void gdbstub_io_init(void)
  29. {
  30. u16 tmp;
  31. /* set up the serial port */
  32. GDBPORT_SERIAL_LCR = UART_LCR_WLEN8; /* 1N8 */
  33. GDBPORT_SERIAL_FCR = (UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
  34. UART_FCR_CLEAR_XMIT);
  35. FLOWCTL_CLEAR(DTR);
  36. FLOWCTL_SET(RTS);
  37. gdbstub_io_set_baud(115200);
  38. /* we want to get serial receive interrupts */
  39. XIRQxICR(GDBPORT_SERIAL_IRQ) = 0;
  40. tmp = XIRQxICR(GDBPORT_SERIAL_IRQ);
  41. #if CONFIG_GDBSTUB_IRQ_LEVEL == 0
  42. IVAR0 = EXCEP_IRQ_LEVEL0;
  43. #elif CONFIG_GDBSTUB_IRQ_LEVEL == 1
  44. IVAR1 = EXCEP_IRQ_LEVEL1;
  45. #elif CONFIG_GDBSTUB_IRQ_LEVEL == 2
  46. IVAR2 = EXCEP_IRQ_LEVEL2;
  47. #elif CONFIG_GDBSTUB_IRQ_LEVEL == 3
  48. IVAR3 = EXCEP_IRQ_LEVEL3;
  49. #elif CONFIG_GDBSTUB_IRQ_LEVEL == 4
  50. IVAR4 = EXCEP_IRQ_LEVEL4;
  51. #elif CONFIG_GDBSTUB_IRQ_LEVEL == 5
  52. IVAR5 = EXCEP_IRQ_LEVEL5;
  53. #else
  54. #error "Unknown irq level for gdbstub."
  55. #endif
  56. set_intr_stub(NUM2EXCEP_IRQ_LEVEL(CONFIG_GDBSTUB_IRQ_LEVEL),
  57. gdbstub_io_rx_handler);
  58. XIRQxICR(GDBPORT_SERIAL_IRQ) &= ~GxICR_REQUEST;
  59. XIRQxICR(GDBPORT_SERIAL_IRQ) =
  60. GxICR_ENABLE | NUM2GxICR_LEVEL(CONFIG_GDBSTUB_IRQ_LEVEL);
  61. tmp = XIRQxICR(GDBPORT_SERIAL_IRQ);
  62. GDBPORT_SERIAL_IER = UART_IER_RDI | UART_IER_RLSI;
  63. /* permit level 0 IRQs to take place */
  64. arch_local_change_intr_mask_level(
  65. NUM2EPSW_IM(CONFIG_GDBSTUB_IRQ_LEVEL + 1));
  66. }
  67. /*
  68. * set up the GDB stub serial port baud rate timers
  69. */
  70. void gdbstub_io_set_baud(unsigned baud)
  71. {
  72. unsigned value;
  73. u8 lcr;
  74. value = 18432000 / 16 / baud;
  75. lcr = GDBPORT_SERIAL_LCR;
  76. GDBPORT_SERIAL_LCR |= UART_LCR_DLAB;
  77. GDBPORT_SERIAL_DLL = value & 0xff;
  78. GDBPORT_SERIAL_DLM = (value >> 8) & 0xff;
  79. GDBPORT_SERIAL_LCR = lcr;
  80. }
  81. /*
  82. * wait for a character to come from the debugger
  83. */
  84. int gdbstub_io_rx_char(unsigned char *_ch, int nonblock)
  85. {
  86. unsigned ix;
  87. u8 ch, st;
  88. #if defined(CONFIG_MN10300_WD_TIMER)
  89. int cpu;
  90. #endif
  91. *_ch = 0xff;
  92. if (gdbstub_rx_unget) {
  93. *_ch = gdbstub_rx_unget;
  94. gdbstub_rx_unget = 0;
  95. return 0;
  96. }
  97. try_again:
  98. /* pull chars out of the buffer */
  99. ix = gdbstub_rx_outp;
  100. barrier();
  101. if (ix == gdbstub_rx_inp) {
  102. if (nonblock)
  103. return -EAGAIN;
  104. #ifdef CONFIG_MN10300_WD_TIMER
  105. for (cpu = 0; cpu < NR_CPUS; cpu++)
  106. watchdog_alert_counter[cpu] = 0;
  107. #endif
  108. goto try_again;
  109. }
  110. ch = gdbstub_rx_buffer[ix++];
  111. st = gdbstub_rx_buffer[ix++];
  112. barrier();
  113. gdbstub_rx_outp = ix & 0x00000fff;
  114. if (st & UART_LSR_BI) {
  115. gdbstub_proto("### GDB Rx Break Detected ###\n");
  116. return -EINTR;
  117. } else if (st & (UART_LSR_FE | UART_LSR_OE | UART_LSR_PE)) {
  118. gdbstub_proto("### GDB Rx Error (st=%02x) ###\n", st);
  119. return -EIO;
  120. } else {
  121. gdbstub_proto("### GDB Rx %02x (st=%02x) ###\n", ch, st);
  122. *_ch = ch & 0x7f;
  123. return 0;
  124. }
  125. }
  126. /*
  127. * send a character to the debugger
  128. */
  129. void gdbstub_io_tx_char(unsigned char ch)
  130. {
  131. FLOWCTL_SET(DTR);
  132. LSR_WAIT_FOR(THRE);
  133. /* FLOWCTL_WAIT_FOR(CTS); */
  134. if (ch == 0x0a) {
  135. GDBPORT_SERIAL_TX = 0x0d;
  136. LSR_WAIT_FOR(THRE);
  137. /* FLOWCTL_WAIT_FOR(CTS); */
  138. }
  139. GDBPORT_SERIAL_TX = ch;
  140. FLOWCTL_CLEAR(DTR);
  141. }
  142. /*
  143. * send a character to the debugger
  144. */
  145. void gdbstub_io_tx_flush(void)
  146. {
  147. LSR_WAIT_FOR(TEMT);
  148. LSR_WAIT_FOR(THRE);
  149. FLOWCTL_CLEAR(DTR);
  150. }