gdb-io-ttysm.c 6.9 KB

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