debugport.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /* Serialport functions for debugging
  2. *
  3. * Copyright (c) 2000 Axis Communications AB
  4. *
  5. * Authors: Bjorn Wesen
  6. *
  7. * Exports:
  8. * console_print_etrax(char *buf)
  9. * int getDebugChar()
  10. * putDebugChar(int)
  11. * enableDebugIRQ()
  12. * init_etrax_debug()
  13. *
  14. * $Log: debugport.c,v $
  15. * Revision 1.19 2004/10/21 07:26:16 starvik
  16. * Made it possible to specify console settings on kernel command line.
  17. *
  18. * Revision 1.18 2004/10/19 13:07:37 starvik
  19. * Merge of Linux 2.6.9
  20. *
  21. * Revision 1.17 2004/09/29 10:33:46 starvik
  22. * Resolved a dealock when printing debug from kernel.
  23. *
  24. * Revision 1.16 2004/08/24 06:12:19 starvik
  25. * Whitespace cleanup
  26. *
  27. * Revision 1.15 2004/08/16 12:37:19 starvik
  28. * Merge of Linux 2.6.8
  29. *
  30. * Revision 1.14 2004/05/17 13:11:29 starvik
  31. * Disable DMA until real serial driver is up
  32. *
  33. * Revision 1.13 2004/05/14 07:58:01 starvik
  34. * Merge of changes from 2.4
  35. *
  36. * Revision 1.12 2003/09/11 07:29:49 starvik
  37. * Merge of Linux 2.6.0-test5
  38. *
  39. * Revision 1.11 2003/07/07 09:53:36 starvik
  40. * Revert all the 2.5.74 merge changes to make the console work again
  41. *
  42. * Revision 1.9 2003/02/17 17:07:23 starvik
  43. * Solved the problem with corrupted debug output (from Linux 2.4)
  44. * * Wait until DMA, FIFO and pipe is empty before and after transmissions
  45. * * Buffer data until a FIFO flush can be triggered.
  46. *
  47. * Revision 1.8 2003/01/22 06:48:36 starvik
  48. * Fixed warnings issued by GCC 3.2.1
  49. *
  50. * Revision 1.7 2002/12/12 08:26:32 starvik
  51. * Don't use C-comments inside CVS comments
  52. *
  53. * Revision 1.6 2002/12/11 15:42:02 starvik
  54. * Extracted v10 (ETRAX 100LX) specific stuff from arch/cris/kernel/
  55. *
  56. * Revision 1.5 2002/11/20 06:58:03 starvik
  57. * Compiles with kgdb
  58. *
  59. * Revision 1.4 2002/11/19 14:35:24 starvik
  60. * Changes from linux 2.4
  61. * Changed struct initializer syntax to the currently prefered notation
  62. *
  63. * Revision 1.3 2002/11/06 09:47:03 starvik
  64. * Modified for new interrupt macros
  65. *
  66. * Revision 1.2 2002/01/21 15:21:50 bjornw
  67. * Update for kdev_t changes
  68. *
  69. * Revision 1.6 2001/04/17 13:58:39 orjanf
  70. * * Renamed CONFIG_KGDB to CONFIG_ETRAX_KGDB.
  71. *
  72. * Revision 1.5 2001/03/26 14:22:05 bjornw
  73. * Namechange of some config options
  74. *
  75. * Revision 1.4 2000/10/06 12:37:26 bjornw
  76. * Use physical addresses when talking to DMA
  77. *
  78. *
  79. */
  80. #include <linux/config.h>
  81. #include <linux/console.h>
  82. #include <linux/init.h>
  83. #include <linux/major.h>
  84. #include <linux/delay.h>
  85. #include <linux/tty.h>
  86. #include <asm/system.h>
  87. #include <asm/arch/svinto.h>
  88. #include <asm/io.h> /* Get SIMCOUT. */
  89. struct dbg_port
  90. {
  91. unsigned int index;
  92. const volatile unsigned* read;
  93. volatile char* write;
  94. volatile unsigned* xoff;
  95. volatile char* baud;
  96. volatile char* tr_ctrl;
  97. volatile char* rec_ctrl;
  98. unsigned long irq;
  99. unsigned int started;
  100. unsigned long baudrate;
  101. unsigned char parity;
  102. unsigned int bits;
  103. };
  104. struct dbg_port ports[]=
  105. {
  106. {
  107. 0,
  108. R_SERIAL0_READ,
  109. R_SERIAL0_TR_DATA,
  110. R_SERIAL0_XOFF,
  111. R_SERIAL0_BAUD,
  112. R_SERIAL0_TR_CTRL,
  113. R_SERIAL0_REC_CTRL,
  114. IO_STATE(R_IRQ_MASK1_SET, ser0_data, set)
  115. },
  116. {
  117. 1,
  118. R_SERIAL1_READ,
  119. R_SERIAL1_TR_DATA,
  120. R_SERIAL1_XOFF,
  121. R_SERIAL1_BAUD,
  122. R_SERIAL1_TR_CTRL,
  123. R_SERIAL1_REC_CTRL,
  124. IO_STATE(R_IRQ_MASK1_SET, ser1_data, set)
  125. },
  126. {
  127. 2,
  128. R_SERIAL2_READ,
  129. R_SERIAL2_TR_DATA,
  130. R_SERIAL2_XOFF,
  131. R_SERIAL2_BAUD,
  132. R_SERIAL2_TR_CTRL,
  133. R_SERIAL2_REC_CTRL,
  134. IO_STATE(R_IRQ_MASK1_SET, ser2_data, set)
  135. },
  136. {
  137. 3,
  138. R_SERIAL3_READ,
  139. R_SERIAL3_TR_DATA,
  140. R_SERIAL3_XOFF,
  141. R_SERIAL3_BAUD,
  142. R_SERIAL3_TR_CTRL,
  143. R_SERIAL3_REC_CTRL,
  144. IO_STATE(R_IRQ_MASK1_SET, ser3_data, set)
  145. }
  146. };
  147. static struct tty_driver *serial_driver;
  148. struct dbg_port* port =
  149. #if defined(CONFIG_ETRAX_DEBUG_PORT0)
  150. &ports[0];
  151. #elif defined(CONFIG_ETRAX_DEBUG_PORT1)
  152. &ports[1];
  153. #elif defined(CONFIG_ETRAX_DEBUG_PORT2)
  154. &ports[2];
  155. #elif defined(CONFIG_ETRAX_DEBUG_PORT3)
  156. &ports[3];
  157. #else
  158. NULL;
  159. #endif
  160. /* Used by serial.c to register a debug_write_function so that the normal
  161. * serial driver is used for kernel debug output
  162. */
  163. typedef int (*debugport_write_function)(int i, const char *buf, unsigned int len);
  164. debugport_write_function debug_write_function = NULL;
  165. static void
  166. start_port(void)
  167. {
  168. unsigned long rec_ctrl = 0;
  169. unsigned long tr_ctrl = 0;
  170. if (!port)
  171. return;
  172. if (port->started)
  173. return;
  174. port->started = 1;
  175. if (port->index == 0)
  176. {
  177. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
  178. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
  179. }
  180. else if (port->index == 1)
  181. {
  182. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
  183. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
  184. }
  185. else if (port->index == 2)
  186. {
  187. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
  188. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
  189. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
  190. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, par0);
  191. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, ser2, select);
  192. }
  193. else
  194. {
  195. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
  196. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
  197. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
  198. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, par1);
  199. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, ser3, select);
  200. }
  201. *R_GEN_CONFIG = genconfig_shadow;
  202. *port->xoff =
  203. IO_STATE(R_SERIAL0_XOFF, tx_stop, enable) |
  204. IO_STATE(R_SERIAL0_XOFF, auto_xoff, disable) |
  205. IO_FIELD(R_SERIAL0_XOFF, xoff_char, 0);
  206. switch (port->baudrate)
  207. {
  208. case 0:
  209. case 115200:
  210. *port->baud =
  211. IO_STATE(R_SERIAL0_BAUD, tr_baud, c115k2Hz) |
  212. IO_STATE(R_SERIAL0_BAUD, rec_baud, c115k2Hz);
  213. break;
  214. case 1200:
  215. *port->baud =
  216. IO_STATE(R_SERIAL0_BAUD, tr_baud, c1200Hz) |
  217. IO_STATE(R_SERIAL0_BAUD, rec_baud, c1200Hz);
  218. break;
  219. case 2400:
  220. *port->baud =
  221. IO_STATE(R_SERIAL0_BAUD, tr_baud, c2400Hz) |
  222. IO_STATE(R_SERIAL0_BAUD, rec_baud, c2400Hz);
  223. break;
  224. case 4800:
  225. *port->baud =
  226. IO_STATE(R_SERIAL0_BAUD, tr_baud, c4800Hz) |
  227. IO_STATE(R_SERIAL0_BAUD, rec_baud, c4800Hz);
  228. break;
  229. case 9600:
  230. *port->baud =
  231. IO_STATE(R_SERIAL0_BAUD, tr_baud, c9600Hz) |
  232. IO_STATE(R_SERIAL0_BAUD, rec_baud, c9600Hz);
  233. break;
  234. case 19200:
  235. *port->baud =
  236. IO_STATE(R_SERIAL0_BAUD, tr_baud, c19k2Hz) |
  237. IO_STATE(R_SERIAL0_BAUD, rec_baud, c19k2Hz);
  238. break;
  239. case 38400:
  240. *port->baud =
  241. IO_STATE(R_SERIAL0_BAUD, tr_baud, c38k4Hz) |
  242. IO_STATE(R_SERIAL0_BAUD, rec_baud, c38k4Hz);
  243. break;
  244. case 57600:
  245. *port->baud =
  246. IO_STATE(R_SERIAL0_BAUD, tr_baud, c57k6Hz) |
  247. IO_STATE(R_SERIAL0_BAUD, rec_baud, c57k6Hz);
  248. break;
  249. default:
  250. *port->baud =
  251. IO_STATE(R_SERIAL0_BAUD, tr_baud, c115k2Hz) |
  252. IO_STATE(R_SERIAL0_BAUD, rec_baud, c115k2Hz);
  253. break;
  254. }
  255. if (port->parity == 'E') {
  256. rec_ctrl =
  257. IO_STATE(R_SERIAL0_REC_CTRL, rec_par, even) |
  258. IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
  259. tr_ctrl =
  260. IO_STATE(R_SERIAL0_TR_CTRL, tr_par, even) |
  261. IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
  262. } else if (port->parity == 'O') {
  263. rec_ctrl =
  264. IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd) |
  265. IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
  266. tr_ctrl =
  267. IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd) |
  268. IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
  269. } else {
  270. rec_ctrl =
  271. IO_STATE(R_SERIAL0_REC_CTRL, rec_par, even) |
  272. IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, disable);
  273. tr_ctrl =
  274. IO_STATE(R_SERIAL0_TR_CTRL, tr_par, even) |
  275. IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, disable);
  276. }
  277. if (port->bits == 7)
  278. {
  279. rec_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
  280. tr_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
  281. }
  282. else
  283. {
  284. rec_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_8bit);
  285. tr_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_8bit);
  286. }
  287. *port->rec_ctrl =
  288. IO_STATE(R_SERIAL0_REC_CTRL, dma_err, stop) |
  289. IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable) |
  290. IO_STATE(R_SERIAL0_REC_CTRL, rts_, active) |
  291. IO_STATE(R_SERIAL0_REC_CTRL, sampling, middle) |
  292. IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, normal) |
  293. rec_ctrl;
  294. *port->tr_ctrl =
  295. IO_FIELD(R_SERIAL0_TR_CTRL, txd, 0) |
  296. IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable) |
  297. IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, disabled) |
  298. IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, one_bit) |
  299. IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, normal) |
  300. tr_ctrl;
  301. }
  302. static void
  303. console_write_direct(struct console *co, const char *buf, unsigned int len)
  304. {
  305. int i;
  306. unsigned long flags;
  307. local_irq_save(flags);
  308. /* Send data */
  309. for (i = 0; i < len; i++) {
  310. /* Wait until transmitter is ready and send.*/
  311. while (!(*port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
  312. ;
  313. *port->write = buf[i];
  314. }
  315. local_irq_restore(flags);
  316. }
  317. static void
  318. console_write(struct console *co, const char *buf, unsigned int len)
  319. {
  320. if (!port)
  321. return;
  322. #ifdef CONFIG_SVINTO_SIM
  323. /* no use to simulate the serial debug output */
  324. SIMCOUT(buf, len);
  325. return;
  326. #endif
  327. start_port();
  328. #ifdef CONFIG_ETRAX_KGDB
  329. /* kgdb needs to output debug info using the gdb protocol */
  330. putDebugString(buf, len);
  331. return;
  332. #endif
  333. if (debug_write_function)
  334. debug_write_function(co->index, buf, len);
  335. else
  336. console_write_direct(co, buf, len);
  337. }
  338. /* legacy function */
  339. void
  340. console_print_etrax(const char *buf)
  341. {
  342. console_write(NULL, buf, strlen(buf));
  343. }
  344. /* Use polling to get a single character FROM the debug port */
  345. int
  346. getDebugChar(void)
  347. {
  348. unsigned long readval;
  349. do {
  350. readval = *port->read;
  351. } while (!(readval & IO_MASK(R_SERIAL0_READ, data_avail)));
  352. return (readval & IO_MASK(R_SERIAL0_READ, data_in));
  353. }
  354. /* Use polling to put a single character to the debug port */
  355. void
  356. putDebugChar(int val)
  357. {
  358. while (!(*port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
  359. ;
  360. *port->write = val;
  361. }
  362. /* Enable irq for receiving chars on the debug port, used by kgdb */
  363. void
  364. enableDebugIRQ(void)
  365. {
  366. *R_IRQ_MASK1_SET = port->irq;
  367. /* use R_VECT_MASK directly, since we really bypass Linux normal
  368. * IRQ handling in kgdb anyway, we don't need to use enable_irq
  369. */
  370. *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
  371. *port->rec_ctrl = IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
  372. }
  373. static struct tty_driver*
  374. etrax_console_device(struct console* co, int *index)
  375. {
  376. return serial_driver;
  377. }
  378. static int __init
  379. console_setup(struct console *co, char *options)
  380. {
  381. char* s;
  382. if (options) {
  383. port = &ports[co->index];
  384. port->baudrate = 115200;
  385. port->parity = 'N';
  386. port->bits = 8;
  387. port->baudrate = simple_strtoul(options, NULL, 10);
  388. s = options;
  389. while(*s >= '0' && *s <= '9')
  390. s++;
  391. if (*s) port->parity = *s++;
  392. if (*s) port->bits = *s++ - '0';
  393. port->started = 0;
  394. start_port();
  395. }
  396. return 0;
  397. }
  398. static struct console sercons = {
  399. name : "ttyS",
  400. write: console_write,
  401. read : NULL,
  402. device : etrax_console_device,
  403. unblank : NULL,
  404. setup : console_setup,
  405. flags : CON_PRINTBUFFER,
  406. index : -1,
  407. cflag : 0,
  408. next : NULL
  409. };
  410. static struct console sercons0 = {
  411. name : "ttyS",
  412. write: console_write,
  413. read : NULL,
  414. device : etrax_console_device,
  415. unblank : NULL,
  416. setup : console_setup,
  417. flags : CON_PRINTBUFFER,
  418. index : 0,
  419. cflag : 0,
  420. next : NULL
  421. };
  422. static struct console sercons1 = {
  423. name : "ttyS",
  424. write: console_write,
  425. read : NULL,
  426. device : etrax_console_device,
  427. unblank : NULL,
  428. setup : console_setup,
  429. flags : CON_PRINTBUFFER,
  430. index : 1,
  431. cflag : 0,
  432. next : NULL
  433. };
  434. static struct console sercons2 = {
  435. name : "ttyS",
  436. write: console_write,
  437. read : NULL,
  438. device : etrax_console_device,
  439. unblank : NULL,
  440. setup : console_setup,
  441. flags : CON_PRINTBUFFER,
  442. index : 2,
  443. cflag : 0,
  444. next : NULL
  445. };
  446. static struct console sercons3 = {
  447. name : "ttyS",
  448. write: console_write,
  449. read : NULL,
  450. device : etrax_console_device,
  451. unblank : NULL,
  452. setup : console_setup,
  453. flags : CON_PRINTBUFFER,
  454. index : 3,
  455. cflag : 0,
  456. next : NULL
  457. };
  458. /*
  459. * Register console (for printk's etc)
  460. */
  461. int __init
  462. init_etrax_debug(void)
  463. {
  464. static int first = 1;
  465. if (!first) {
  466. if (!port) {
  467. register_console(&sercons0);
  468. register_console(&sercons1);
  469. register_console(&sercons2);
  470. register_console(&sercons3);
  471. unregister_console(&sercons);
  472. }
  473. return 0;
  474. }
  475. first = 0;
  476. if (port)
  477. register_console(&sercons);
  478. return 0;
  479. }
  480. int __init
  481. init_console(void)
  482. {
  483. serial_driver = alloc_tty_driver(1);
  484. if (!serial_driver)
  485. return -ENOMEM;
  486. return 0;
  487. }
  488. __initcall(init_etrax_debug);