gdb-io-ttysm.c 6.9 KB

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