gdb-io-ttysm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* MN10300 On-chip 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/tty.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/unit/clock.h>
  24. #include "mn10300-serial.h"
  25. #if defined(CONFIG_GDBSTUB_ON_TTYSM0)
  26. struct mn10300_serial_port *const gdbstub_port = &mn10300_serial_port_sif0;
  27. #elif defined(CONFIG_GDBSTUB_ON_TTYSM1)
  28. struct mn10300_serial_port *const gdbstub_port = &mn10300_serial_port_sif1;
  29. #else
  30. struct mn10300_serial_port *const gdbstub_port = &mn10300_serial_port_sif2;
  31. #endif
  32. /*
  33. * initialise the GDB stub I/O routines
  34. */
  35. void __init gdbstub_io_init(void)
  36. {
  37. uint16_t scxctr;
  38. int tmp;
  39. switch (gdbstub_port->clock_src) {
  40. case MNSCx_CLOCK_SRC_IOCLK:
  41. gdbstub_port->ioclk = MN10300_IOCLK;
  42. break;
  43. #ifdef MN10300_IOBCLK
  44. case MNSCx_CLOCK_SRC_IOBCLK:
  45. gdbstub_port->ioclk = MN10300_IOBCLK;
  46. break;
  47. #endif
  48. default:
  49. BUG();
  50. }
  51. /* set up the serial port */
  52. gdbstub_io_set_baud(115200);
  53. /* we want to get serial receive interrupts */
  54. set_intr_level(gdbstub_port->rx_irq, GxICR_LEVEL_0);
  55. set_intr_level(gdbstub_port->tx_irq, GxICR_LEVEL_0);
  56. set_intr_stub(EXCEP_IRQ_LEVEL0, gdbstub_io_rx_handler);
  57. *gdbstub_port->rx_icr |= GxICR_ENABLE;
  58. tmp = *gdbstub_port->rx_icr;
  59. /* enable the device */
  60. scxctr = SC01CTR_CLN_8BIT; /* 1N8 */
  61. switch (gdbstub_port->div_timer) {
  62. case MNSCx_DIV_TIMER_16BIT:
  63. scxctr |= SC0CTR_CK_TM8UFLOW_8; /* == SC1CTR_CK_TM9UFLOW_8
  64. == SC2CTR_CK_TM10UFLOW_8 */
  65. break;
  66. case MNSCx_DIV_TIMER_8BIT:
  67. scxctr |= SC0CTR_CK_TM2UFLOW_8;
  68. break;
  69. }
  70. scxctr |= SC01CTR_TXE | SC01CTR_RXE;
  71. *gdbstub_port->_control = scxctr;
  72. tmp = *gdbstub_port->_control;
  73. /* permit level 0 IRQs only */
  74. asm volatile(
  75. " and %0,epsw \n"
  76. " or %1,epsw \n"
  77. :
  78. : "i"(~EPSW_IM), "i"(EPSW_IE|EPSW_IM_1)
  79. );
  80. }
  81. /*
  82. * set up the GDB stub serial port baud rate timers
  83. */
  84. void gdbstub_io_set_baud(unsigned baud)
  85. {
  86. const unsigned bits = 10; /* 1 [start] + 8 [data] + 0 [parity] +
  87. * 1 [stop] */
  88. unsigned long ioclk = gdbstub_port->ioclk;
  89. unsigned xdiv, tmp;
  90. uint16_t tmxbr;
  91. uint8_t tmxmd;
  92. if (!baud) {
  93. baud = 9600;
  94. } else if (baud == 134) {
  95. baud = 269; /* 134 is really 134.5 */
  96. xdiv = 2;
  97. }
  98. try_alternative:
  99. xdiv = 1;
  100. switch (gdbstub_port->div_timer) {
  101. case MNSCx_DIV_TIMER_16BIT:
  102. tmxmd = TM8MD_SRC_IOCLK;
  103. tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
  104. if (tmp > 0 && tmp <= 65535)
  105. goto timer_okay;
  106. tmxmd = TM8MD_SRC_IOCLK_8;
  107. tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
  108. if (tmp > 0 && tmp <= 65535)
  109. goto timer_okay;
  110. tmxmd = TM8MD_SRC_IOCLK_32;
  111. tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
  112. if (tmp > 0 && tmp <= 65535)
  113. goto timer_okay;
  114. break;
  115. case MNSCx_DIV_TIMER_8BIT:
  116. tmxmd = TM2MD_SRC_IOCLK;
  117. tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
  118. if (tmp > 0 && tmp <= 255)
  119. goto timer_okay;
  120. tmxmd = TM2MD_SRC_IOCLK_8;
  121. tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
  122. if (tmp > 0 && tmp <= 255)
  123. goto timer_okay;
  124. tmxmd = TM2MD_SRC_IOCLK_32;
  125. tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
  126. if (tmp > 0 && tmp <= 255)
  127. goto timer_okay;
  128. break;
  129. }
  130. /* as a last resort, if the quotient is zero, default to 9600 bps */
  131. baud = 9600;
  132. goto try_alternative;
  133. timer_okay:
  134. gdbstub_port->uart.timeout = (2 * bits * HZ) / baud;
  135. gdbstub_port->uart.timeout += HZ / 50;
  136. /* set the timer to produce the required baud rate */
  137. switch (gdbstub_port->div_timer) {
  138. case MNSCx_DIV_TIMER_16BIT:
  139. *gdbstub_port->_tmxmd = 0;
  140. *gdbstub_port->_tmxbr = tmxbr;
  141. *gdbstub_port->_tmxmd = TM8MD_INIT_COUNTER;
  142. *gdbstub_port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
  143. break;
  144. case MNSCx_DIV_TIMER_8BIT:
  145. *gdbstub_port->_tmxmd = 0;
  146. *(volatile u8 *) gdbstub_port->_tmxbr = (u8)tmxbr;
  147. *gdbstub_port->_tmxmd = TM2MD_INIT_COUNTER;
  148. *gdbstub_port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
  149. break;
  150. }
  151. }
  152. /*
  153. * wait for a character to come from the debugger
  154. */
  155. int gdbstub_io_rx_char(unsigned char *_ch, int nonblock)
  156. {
  157. unsigned ix;
  158. u8 ch, st;
  159. *_ch = 0xff;
  160. if (gdbstub_rx_unget) {
  161. *_ch = gdbstub_rx_unget;
  162. gdbstub_rx_unget = 0;
  163. return 0;
  164. }
  165. try_again:
  166. /* pull chars out of the buffer */
  167. ix = gdbstub_rx_outp;
  168. if (ix == gdbstub_rx_inp) {
  169. if (nonblock)
  170. return -EAGAIN;
  171. #ifdef CONFIG_MN10300_WD_TIMER
  172. watchdog_alert_counter = 0;
  173. #endif /* CONFIG_MN10300_WD_TIMER */
  174. goto try_again;
  175. }
  176. ch = gdbstub_rx_buffer[ix++];
  177. st = gdbstub_rx_buffer[ix++];
  178. gdbstub_rx_outp = ix & (PAGE_SIZE - 1);
  179. st &= SC01STR_RXF | SC01STR_RBF | SC01STR_FEF | SC01STR_PEF |
  180. SC01STR_OEF;
  181. /* deal with what we've got
  182. * - note that the UART doesn't do BREAK-detection for us
  183. */
  184. if (st & SC01STR_FEF && ch == 0) {
  185. switch (gdbstub_port->rx_brk) {
  186. case 0: gdbstub_port->rx_brk = 1; goto try_again;
  187. case 1: gdbstub_port->rx_brk = 2; goto try_again;
  188. case 2:
  189. gdbstub_port->rx_brk = 3;
  190. gdbstub_proto("### GDB MNSERIAL Rx Break Detected"
  191. " ###\n");
  192. return -EINTR;
  193. default:
  194. goto try_again;
  195. }
  196. } else if (st & SC01STR_FEF) {
  197. if (gdbstub_port->rx_brk)
  198. goto try_again;
  199. gdbstub_proto("### GDB MNSERIAL Framing Error ###\n");
  200. return -EIO;
  201. } else if (st & SC01STR_OEF) {
  202. if (gdbstub_port->rx_brk)
  203. goto try_again;
  204. gdbstub_proto("### GDB MNSERIAL Overrun Error ###\n");
  205. return -EIO;
  206. } else if (st & SC01STR_PEF) {
  207. if (gdbstub_port->rx_brk)
  208. goto try_again;
  209. gdbstub_proto("### GDB MNSERIAL Parity Error ###\n");
  210. return -EIO;
  211. } else {
  212. /* look for the tail-end char on a break run */
  213. if (gdbstub_port->rx_brk == 3) {
  214. switch (ch) {
  215. case 0xFF:
  216. case 0xFE:
  217. case 0xFC:
  218. case 0xF8:
  219. case 0xF0:
  220. case 0xE0:
  221. case 0xC0:
  222. case 0x80:
  223. case 0x00:
  224. gdbstub_port->rx_brk = 0;
  225. goto try_again;
  226. default:
  227. break;
  228. }
  229. }
  230. gdbstub_port->rx_brk = 0;
  231. gdbstub_io("### GDB Rx %02x (st=%02x) ###\n", ch, st);
  232. *_ch = ch & 0x7f;
  233. return 0;
  234. }
  235. }
  236. /*
  237. * send a character to the debugger
  238. */
  239. void gdbstub_io_tx_char(unsigned char ch)
  240. {
  241. while (*gdbstub_port->_status & SC01STR_TBF)
  242. continue;
  243. if (ch == 0x0a) {
  244. *(u8 *) gdbstub_port->_txb = 0x0d;
  245. while (*gdbstub_port->_status & SC01STR_TBF)
  246. continue;
  247. }
  248. *(u8 *) gdbstub_port->_txb = ch;
  249. }
  250. /*
  251. * flush the transmission buffers
  252. */
  253. void gdbstub_io_tx_flush(void)
  254. {
  255. while (*gdbstub_port->_status & (SC01STR_TBF | SC01STR_TXF))
  256. continue;
  257. }