gdb-io-serial.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. if (ix == gdbstub_rx_inp) {
  86. if (nonblock)
  87. return -EAGAIN;
  88. #ifdef CONFIG_MN10300_WD_TIMER
  89. watchdog_alert_counter = 0;
  90. #endif /* CONFIG_MN10300_WD_TIMER */
  91. goto try_again;
  92. }
  93. ch = gdbstub_rx_buffer[ix++];
  94. st = gdbstub_rx_buffer[ix++];
  95. gdbstub_rx_outp = ix & 0x00000fff;
  96. if (st & UART_LSR_BI) {
  97. gdbstub_proto("### GDB Rx Break Detected ###\n");
  98. return -EINTR;
  99. } else if (st & (UART_LSR_FE | UART_LSR_OE | UART_LSR_PE)) {
  100. gdbstub_proto("### GDB Rx Error (st=%02x) ###\n", st);
  101. return -EIO;
  102. } else {
  103. gdbstub_proto("### GDB Rx %02x (st=%02x) ###\n", ch, st);
  104. *_ch = ch & 0x7f;
  105. return 0;
  106. }
  107. }
  108. /*
  109. * send a character to the debugger
  110. */
  111. void gdbstub_io_tx_char(unsigned char ch)
  112. {
  113. FLOWCTL_SET(DTR);
  114. LSR_WAIT_FOR(THRE);
  115. /* FLOWCTL_WAIT_FOR(CTS); */
  116. if (ch == 0x0a) {
  117. GDBPORT_SERIAL_TX = 0x0d;
  118. LSR_WAIT_FOR(THRE);
  119. /* FLOWCTL_WAIT_FOR(CTS); */
  120. }
  121. GDBPORT_SERIAL_TX = ch;
  122. FLOWCTL_CLEAR(DTR);
  123. }
  124. /*
  125. * send a character to the debugger
  126. */
  127. void gdbstub_io_tx_flush(void)
  128. {
  129. LSR_WAIT_FOR(TEMT);
  130. LSR_WAIT_FOR(THRE);
  131. FLOWCTL_CLEAR(DTR);
  132. }