gdb-io-serial.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/system.h>
  21. #include <asm/gdb-stub.h>
  22. #include <asm/exceptions.h>
  23. #include <asm/serial-regs.h>
  24. #include <asm/unit/serial.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. IVAR0 = EXCEP_IRQ_LEVEL0;
  42. set_intr_stub(EXCEP_IRQ_LEVEL0, gdbstub_io_rx_handler);
  43. XIRQxICR(GDBPORT_SERIAL_IRQ) &= ~GxICR_REQUEST;
  44. XIRQxICR(GDBPORT_SERIAL_IRQ) = GxICR_ENABLE | GxICR_LEVEL_0;
  45. tmp = XIRQxICR(GDBPORT_SERIAL_IRQ);
  46. GDBPORT_SERIAL_IER = UART_IER_RDI | UART_IER_RLSI;
  47. /* permit level 0 IRQs to take place */
  48. asm volatile(
  49. " and %0,epsw \n"
  50. " or %1,epsw \n"
  51. :
  52. : "i"(~EPSW_IM), "i"(EPSW_IE | EPSW_IM_1)
  53. );
  54. }
  55. /*
  56. * set up the GDB stub serial port baud rate timers
  57. */
  58. void gdbstub_io_set_baud(unsigned baud)
  59. {
  60. unsigned value;
  61. u8 lcr;
  62. value = 18432000 / 16 / baud;
  63. lcr = GDBPORT_SERIAL_LCR;
  64. GDBPORT_SERIAL_LCR |= UART_LCR_DLAB;
  65. GDBPORT_SERIAL_DLL = value & 0xff;
  66. GDBPORT_SERIAL_DLM = (value >> 8) & 0xff;
  67. GDBPORT_SERIAL_LCR = lcr;
  68. }
  69. /*
  70. * wait for a character to come from the debugger
  71. */
  72. int gdbstub_io_rx_char(unsigned char *_ch, int nonblock)
  73. {
  74. unsigned ix;
  75. u8 ch, st;
  76. *_ch = 0xff;
  77. if (gdbstub_rx_unget) {
  78. *_ch = gdbstub_rx_unget;
  79. gdbstub_rx_unget = 0;
  80. return 0;
  81. }
  82. try_again:
  83. /* pull chars out of the buffer */
  84. ix = gdbstub_rx_outp;
  85. barrier();
  86. if (ix == gdbstub_rx_inp) {
  87. if (nonblock)
  88. return -EAGAIN;
  89. #ifdef CONFIG_MN10300_WD_TIMER
  90. watchdog_alert_counter = 0;
  91. #endif /* CONFIG_MN10300_WD_TIMER */
  92. goto try_again;
  93. }
  94. ch = gdbstub_rx_buffer[ix++];
  95. st = gdbstub_rx_buffer[ix++];
  96. barrier();
  97. gdbstub_rx_outp = ix & 0x00000fff;
  98. if (st & UART_LSR_BI) {
  99. gdbstub_proto("### GDB Rx Break Detected ###\n");
  100. return -EINTR;
  101. } else if (st & (UART_LSR_FE | UART_LSR_OE | UART_LSR_PE)) {
  102. gdbstub_proto("### GDB Rx Error (st=%02x) ###\n", st);
  103. return -EIO;
  104. } else {
  105. gdbstub_proto("### GDB Rx %02x (st=%02x) ###\n", ch, st);
  106. *_ch = ch & 0x7f;
  107. return 0;
  108. }
  109. }
  110. /*
  111. * send a character to the debugger
  112. */
  113. void gdbstub_io_tx_char(unsigned char ch)
  114. {
  115. FLOWCTL_SET(DTR);
  116. LSR_WAIT_FOR(THRE);
  117. /* FLOWCTL_WAIT_FOR(CTS); */
  118. if (ch == 0x0a) {
  119. GDBPORT_SERIAL_TX = 0x0d;
  120. LSR_WAIT_FOR(THRE);
  121. /* FLOWCTL_WAIT_FOR(CTS); */
  122. }
  123. GDBPORT_SERIAL_TX = ch;
  124. FLOWCTL_CLEAR(DTR);
  125. }
  126. /*
  127. * send a character to the debugger
  128. */
  129. void gdbstub_io_tx_flush(void)
  130. {
  131. LSR_WAIT_FOR(TEMT);
  132. LSR_WAIT_FOR(THRE);
  133. FLOWCTL_CLEAR(DTR);
  134. }