gdb-io-ttysm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. barrier();
  169. if (ix == gdbstub_rx_inp) {
  170. if (nonblock)
  171. return -EAGAIN;
  172. #ifdef CONFIG_MN10300_WD_TIMER
  173. watchdog_alert_counter = 0;
  174. #endif /* CONFIG_MN10300_WD_TIMER */
  175. goto try_again;
  176. }
  177. ch = gdbstub_rx_buffer[ix++];
  178. st = gdbstub_rx_buffer[ix++];
  179. barrier();
  180. gdbstub_rx_outp = ix & (PAGE_SIZE - 1);
  181. st &= SC01STR_RXF | SC01STR_RBF | SC01STR_FEF | SC01STR_PEF |
  182. SC01STR_OEF;
  183. /* deal with what we've got
  184. * - note that the UART doesn't do BREAK-detection for us
  185. */
  186. if (st & SC01STR_FEF && ch == 0) {
  187. switch (gdbstub_port->rx_brk) {
  188. case 0: gdbstub_port->rx_brk = 1; goto try_again;
  189. case 1: gdbstub_port->rx_brk = 2; goto try_again;
  190. case 2:
  191. gdbstub_port->rx_brk = 3;
  192. gdbstub_proto("### GDB MNSERIAL Rx Break Detected"
  193. " ###\n");
  194. return -EINTR;
  195. default:
  196. goto try_again;
  197. }
  198. } else if (st & SC01STR_FEF) {
  199. if (gdbstub_port->rx_brk)
  200. goto try_again;
  201. gdbstub_proto("### GDB MNSERIAL Framing Error ###\n");
  202. return -EIO;
  203. } else if (st & SC01STR_OEF) {
  204. if (gdbstub_port->rx_brk)
  205. goto try_again;
  206. gdbstub_proto("### GDB MNSERIAL Overrun Error ###\n");
  207. return -EIO;
  208. } else if (st & SC01STR_PEF) {
  209. if (gdbstub_port->rx_brk)
  210. goto try_again;
  211. gdbstub_proto("### GDB MNSERIAL Parity Error ###\n");
  212. return -EIO;
  213. } else {
  214. /* look for the tail-end char on a break run */
  215. if (gdbstub_port->rx_brk == 3) {
  216. switch (ch) {
  217. case 0xFF:
  218. case 0xFE:
  219. case 0xFC:
  220. case 0xF8:
  221. case 0xF0:
  222. case 0xE0:
  223. case 0xC0:
  224. case 0x80:
  225. case 0x00:
  226. gdbstub_port->rx_brk = 0;
  227. goto try_again;
  228. default:
  229. break;
  230. }
  231. }
  232. gdbstub_port->rx_brk = 0;
  233. gdbstub_io("### GDB Rx %02x (st=%02x) ###\n", ch, st);
  234. *_ch = ch & 0x7f;
  235. return 0;
  236. }
  237. }
  238. /*
  239. * send a character to the debugger
  240. */
  241. void gdbstub_io_tx_char(unsigned char ch)
  242. {
  243. while (*gdbstub_port->_status & SC01STR_TBF)
  244. continue;
  245. if (ch == 0x0a) {
  246. *(u8 *) gdbstub_port->_txb = 0x0d;
  247. while (*gdbstub_port->_status & SC01STR_TBF)
  248. continue;
  249. }
  250. *(u8 *) gdbstub_port->_txb = ch;
  251. }
  252. /*
  253. * flush the transmission buffers
  254. */
  255. void gdbstub_io_tx_flush(void)
  256. {
  257. while (*gdbstub_port->_status & (SC01STR_TBF | SC01STR_TXF))
  258. continue;
  259. }