debugport.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /* Serialport functions for debugging
  2. *
  3. * Copyright (c) 2000-2007 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. */
  15. #include <linux/console.h>
  16. #include <linux/init.h>
  17. #include <linux/major.h>
  18. #include <linux/delay.h>
  19. #include <linux/tty.h>
  20. #include <asm/system.h>
  21. #include <arch/svinto.h>
  22. #include <asm/io.h> /* Get SIMCOUT. */
  23. extern void reset_watchdog(void);
  24. struct dbg_port
  25. {
  26. unsigned int index;
  27. const volatile unsigned* read;
  28. volatile char* write;
  29. volatile unsigned* xoff;
  30. volatile char* baud;
  31. volatile char* tr_ctrl;
  32. volatile char* rec_ctrl;
  33. unsigned long irq;
  34. unsigned int started;
  35. unsigned long baudrate;
  36. unsigned char parity;
  37. unsigned int bits;
  38. };
  39. struct dbg_port ports[]=
  40. {
  41. {
  42. 0,
  43. R_SERIAL0_READ,
  44. R_SERIAL0_TR_DATA,
  45. R_SERIAL0_XOFF,
  46. R_SERIAL0_BAUD,
  47. R_SERIAL0_TR_CTRL,
  48. R_SERIAL0_REC_CTRL,
  49. IO_STATE(R_IRQ_MASK1_SET, ser0_data, set),
  50. 0,
  51. 115200,
  52. 'N',
  53. 8
  54. },
  55. {
  56. 1,
  57. R_SERIAL1_READ,
  58. R_SERIAL1_TR_DATA,
  59. R_SERIAL1_XOFF,
  60. R_SERIAL1_BAUD,
  61. R_SERIAL1_TR_CTRL,
  62. R_SERIAL1_REC_CTRL,
  63. IO_STATE(R_IRQ_MASK1_SET, ser1_data, set),
  64. 0,
  65. 115200,
  66. 'N',
  67. 8
  68. },
  69. {
  70. 2,
  71. R_SERIAL2_READ,
  72. R_SERIAL2_TR_DATA,
  73. R_SERIAL2_XOFF,
  74. R_SERIAL2_BAUD,
  75. R_SERIAL2_TR_CTRL,
  76. R_SERIAL2_REC_CTRL,
  77. IO_STATE(R_IRQ_MASK1_SET, ser2_data, set),
  78. 0,
  79. 115200,
  80. 'N',
  81. 8
  82. },
  83. {
  84. 3,
  85. R_SERIAL3_READ,
  86. R_SERIAL3_TR_DATA,
  87. R_SERIAL3_XOFF,
  88. R_SERIAL3_BAUD,
  89. R_SERIAL3_TR_CTRL,
  90. R_SERIAL3_REC_CTRL,
  91. IO_STATE(R_IRQ_MASK1_SET, ser3_data, set),
  92. 0,
  93. 115200,
  94. 'N',
  95. 8
  96. }
  97. };
  98. #ifdef CONFIG_ETRAX_SERIAL
  99. extern struct tty_driver *serial_driver;
  100. #endif
  101. struct dbg_port* port =
  102. #if defined(CONFIG_ETRAX_DEBUG_PORT0)
  103. &ports[0];
  104. #elif defined(CONFIG_ETRAX_DEBUG_PORT1)
  105. &ports[1];
  106. #elif defined(CONFIG_ETRAX_DEBUG_PORT2)
  107. &ports[2];
  108. #elif defined(CONFIG_ETRAX_DEBUG_PORT3)
  109. &ports[3];
  110. #else
  111. NULL;
  112. #endif
  113. static struct dbg_port* kgdb_port =
  114. #if defined(CONFIG_ETRAX_KGDB_PORT0)
  115. &ports[0];
  116. #elif defined(CONFIG_ETRAX_KGDB_PORT1)
  117. &ports[1];
  118. #elif defined(CONFIG_ETRAX_KGDB_PORT2)
  119. &ports[2];
  120. #elif defined(CONFIG_ETRAX_KGDB_PORT3)
  121. &ports[3];
  122. #else
  123. NULL;
  124. #endif
  125. static void
  126. start_port(struct dbg_port* p)
  127. {
  128. unsigned long rec_ctrl = 0;
  129. unsigned long tr_ctrl = 0;
  130. if (!p)
  131. return;
  132. if (p->started)
  133. return;
  134. p->started = 1;
  135. if (p->index == 0)
  136. {
  137. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma6);
  138. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma6, unused);
  139. }
  140. else if (p->index == 1)
  141. {
  142. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma8);
  143. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma8, usb);
  144. }
  145. else if (p->index == 2)
  146. {
  147. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma2);
  148. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma2, par0);
  149. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma3);
  150. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma3, par0);
  151. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, ser2, select);
  152. }
  153. else
  154. {
  155. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma4);
  156. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma4, par1);
  157. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, dma5);
  158. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, dma5, par1);
  159. genconfig_shadow |= IO_STATE(R_GEN_CONFIG, ser3, select);
  160. }
  161. *R_GEN_CONFIG = genconfig_shadow;
  162. *p->xoff =
  163. IO_STATE(R_SERIAL0_XOFF, tx_stop, enable) |
  164. IO_STATE(R_SERIAL0_XOFF, auto_xoff, disable) |
  165. IO_FIELD(R_SERIAL0_XOFF, xoff_char, 0);
  166. switch (p->baudrate)
  167. {
  168. case 0:
  169. case 115200:
  170. *p->baud =
  171. IO_STATE(R_SERIAL0_BAUD, tr_baud, c115k2Hz) |
  172. IO_STATE(R_SERIAL0_BAUD, rec_baud, c115k2Hz);
  173. break;
  174. case 1200:
  175. *p->baud =
  176. IO_STATE(R_SERIAL0_BAUD, tr_baud, c1200Hz) |
  177. IO_STATE(R_SERIAL0_BAUD, rec_baud, c1200Hz);
  178. break;
  179. case 2400:
  180. *p->baud =
  181. IO_STATE(R_SERIAL0_BAUD, tr_baud, c2400Hz) |
  182. IO_STATE(R_SERIAL0_BAUD, rec_baud, c2400Hz);
  183. break;
  184. case 4800:
  185. *p->baud =
  186. IO_STATE(R_SERIAL0_BAUD, tr_baud, c4800Hz) |
  187. IO_STATE(R_SERIAL0_BAUD, rec_baud, c4800Hz);
  188. break;
  189. case 9600:
  190. *p->baud =
  191. IO_STATE(R_SERIAL0_BAUD, tr_baud, c9600Hz) |
  192. IO_STATE(R_SERIAL0_BAUD, rec_baud, c9600Hz);
  193. break;
  194. case 19200:
  195. *p->baud =
  196. IO_STATE(R_SERIAL0_BAUD, tr_baud, c19k2Hz) |
  197. IO_STATE(R_SERIAL0_BAUD, rec_baud, c19k2Hz);
  198. break;
  199. case 38400:
  200. *p->baud =
  201. IO_STATE(R_SERIAL0_BAUD, tr_baud, c38k4Hz) |
  202. IO_STATE(R_SERIAL0_BAUD, rec_baud, c38k4Hz);
  203. break;
  204. case 57600:
  205. *p->baud =
  206. IO_STATE(R_SERIAL0_BAUD, tr_baud, c57k6Hz) |
  207. IO_STATE(R_SERIAL0_BAUD, rec_baud, c57k6Hz);
  208. break;
  209. default:
  210. *p->baud =
  211. IO_STATE(R_SERIAL0_BAUD, tr_baud, c115k2Hz) |
  212. IO_STATE(R_SERIAL0_BAUD, rec_baud, c115k2Hz);
  213. break;
  214. }
  215. if (p->parity == 'E') {
  216. rec_ctrl =
  217. IO_STATE(R_SERIAL0_REC_CTRL, rec_par, even) |
  218. IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
  219. tr_ctrl =
  220. IO_STATE(R_SERIAL0_TR_CTRL, tr_par, even) |
  221. IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
  222. } else if (p->parity == 'O') {
  223. rec_ctrl =
  224. IO_STATE(R_SERIAL0_REC_CTRL, rec_par, odd) |
  225. IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, enable);
  226. tr_ctrl =
  227. IO_STATE(R_SERIAL0_TR_CTRL, tr_par, odd) |
  228. IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, enable);
  229. } else {
  230. rec_ctrl =
  231. IO_STATE(R_SERIAL0_REC_CTRL, rec_par, even) |
  232. IO_STATE(R_SERIAL0_REC_CTRL, rec_par_en, disable);
  233. tr_ctrl =
  234. IO_STATE(R_SERIAL0_TR_CTRL, tr_par, even) |
  235. IO_STATE(R_SERIAL0_TR_CTRL, tr_par_en, disable);
  236. }
  237. if (p->bits == 7)
  238. {
  239. rec_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_7bit);
  240. tr_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_7bit);
  241. }
  242. else
  243. {
  244. rec_ctrl |= IO_STATE(R_SERIAL0_REC_CTRL, rec_bitnr, rec_8bit);
  245. tr_ctrl |= IO_STATE(R_SERIAL0_TR_CTRL, tr_bitnr, tr_8bit);
  246. }
  247. *p->rec_ctrl =
  248. IO_STATE(R_SERIAL0_REC_CTRL, dma_err, stop) |
  249. IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable) |
  250. IO_STATE(R_SERIAL0_REC_CTRL, rts_, active) |
  251. IO_STATE(R_SERIAL0_REC_CTRL, sampling, middle) |
  252. IO_STATE(R_SERIAL0_REC_CTRL, rec_stick_par, normal) |
  253. rec_ctrl;
  254. *p->tr_ctrl =
  255. IO_FIELD(R_SERIAL0_TR_CTRL, txd, 0) |
  256. IO_STATE(R_SERIAL0_TR_CTRL, tr_enable, enable) |
  257. IO_STATE(R_SERIAL0_TR_CTRL, auto_cts, disabled) |
  258. IO_STATE(R_SERIAL0_TR_CTRL, stop_bits, one_bit) |
  259. IO_STATE(R_SERIAL0_TR_CTRL, tr_stick_par, normal) |
  260. tr_ctrl;
  261. }
  262. static void
  263. console_write_direct(struct console *co, const char *buf, unsigned int len)
  264. {
  265. int i;
  266. unsigned long flags;
  267. if (!port)
  268. return;
  269. local_irq_save(flags);
  270. /* Send data */
  271. for (i = 0; i < len; i++) {
  272. /* LF -> CRLF */
  273. if (buf[i] == '\n') {
  274. while (!(*port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
  275. ;
  276. *port->write = '\r';
  277. }
  278. /* Wait until transmitter is ready and send.*/
  279. while (!(*port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
  280. ;
  281. *port->write = buf[i];
  282. }
  283. /*
  284. * Feed the watchdog, otherwise it will reset the chip during boot.
  285. * The time to send an ordinary boot message line (10-90 chars)
  286. * varies between 1-8ms at 115200. What makes up for the additional
  287. * 90ms that allows the watchdog to bite?
  288. */
  289. reset_watchdog();
  290. local_irq_restore(flags);
  291. }
  292. static void
  293. console_write(struct console *co, const char *buf, unsigned int len)
  294. {
  295. if (!port)
  296. return;
  297. #ifdef CONFIG_SVINTO_SIM
  298. /* no use to simulate the serial debug output */
  299. SIMCOUT(buf, len);
  300. return;
  301. #endif
  302. console_write_direct(co, buf, len);
  303. }
  304. /* legacy function */
  305. void
  306. console_print_etrax(const char *buf)
  307. {
  308. console_write(NULL, buf, strlen(buf));
  309. }
  310. /* Use polling to get a single character FROM the debug port */
  311. int
  312. getDebugChar(void)
  313. {
  314. unsigned long readval;
  315. if (!kgdb_port)
  316. return 0;
  317. do {
  318. readval = *kgdb_port->read;
  319. } while (!(readval & IO_MASK(R_SERIAL0_READ, data_avail)));
  320. return (readval & IO_MASK(R_SERIAL0_READ, data_in));
  321. }
  322. /* Use polling to put a single character to the debug port */
  323. void
  324. putDebugChar(int val)
  325. {
  326. if (!kgdb_port)
  327. return;
  328. while (!(*kgdb_port->read & IO_MASK(R_SERIAL0_READ, tr_ready)))
  329. ;
  330. *kgdb_port->write = val;
  331. }
  332. /* Enable irq for receiving chars on the debug port, used by kgdb */
  333. void
  334. enableDebugIRQ(void)
  335. {
  336. if (!kgdb_port)
  337. return;
  338. *R_IRQ_MASK1_SET = kgdb_port->irq;
  339. /* use R_VECT_MASK directly, since we really bypass Linux normal
  340. * IRQ handling in kgdb anyway, we don't need to use enable_irq
  341. */
  342. *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, serial, set);
  343. *kgdb_port->rec_ctrl = IO_STATE(R_SERIAL0_REC_CTRL, rec_enable, enable);
  344. }
  345. static int __init
  346. console_setup(struct console *co, char *options)
  347. {
  348. char* s;
  349. if (options) {
  350. port = &ports[co->index];
  351. port->baudrate = 115200;
  352. port->parity = 'N';
  353. port->bits = 8;
  354. port->baudrate = simple_strtoul(options, NULL, 10);
  355. s = options;
  356. while(*s >= '0' && *s <= '9')
  357. s++;
  358. if (*s) port->parity = *s++;
  359. if (*s) port->bits = *s++ - '0';
  360. port->started = 0;
  361. start_port(0);
  362. }
  363. return 0;
  364. }
  365. /* This is a dummy serial device that throws away anything written to it.
  366. * This is used when no debug output is wanted.
  367. */
  368. static struct tty_driver dummy_driver;
  369. static int dummy_open(struct tty_struct *tty, struct file * filp)
  370. {
  371. return 0;
  372. }
  373. static void dummy_close(struct tty_struct *tty, struct file * filp)
  374. {
  375. }
  376. static int dummy_write(struct tty_struct * tty,
  377. const unsigned char *buf, int count)
  378. {
  379. return count;
  380. }
  381. static int dummy_write_room(struct tty_struct *tty)
  382. {
  383. return 8192;
  384. }
  385. static const struct tty_operations dummy_ops = {
  386. .open = dummy_open,
  387. .close = dummy_close,
  388. .write = dummy_write,
  389. .write_room = dummy_write_room,
  390. };
  391. void __init
  392. init_dummy_console(void)
  393. {
  394. memset(&dummy_driver, 0, sizeof(struct tty_driver));
  395. dummy_driver.driver_name = "serial";
  396. dummy_driver.name = "ttyS";
  397. dummy_driver.major = TTY_MAJOR;
  398. dummy_driver.minor_start = 68;
  399. dummy_driver.num = 1; /* etrax100 has 4 serial ports */
  400. dummy_driver.type = TTY_DRIVER_TYPE_SERIAL;
  401. dummy_driver.subtype = SERIAL_TYPE_NORMAL;
  402. dummy_driver.init_termios = tty_std_termios;
  403. /* Normally B9600 default... */
  404. dummy_driver.init_termios.c_cflag =
  405. B115200 | CS8 | CREAD | HUPCL | CLOCAL;
  406. dummy_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  407. dummy_driver.init_termios.c_ispeed = 115200;
  408. dummy_driver.init_termios.c_ospeed = 115200;
  409. dummy_driver.ops = &dummy_ops;
  410. if (tty_register_driver(&dummy_driver))
  411. panic("Couldn't register dummy serial driver\n");
  412. }
  413. static struct tty_driver*
  414. etrax_console_device(struct console* co, int *index)
  415. {
  416. if (port)
  417. *index = port->index;
  418. else
  419. *index = 0;
  420. #ifdef CONFIG_ETRAX_SERIAL
  421. return port ? serial_driver : &dummy_driver;
  422. #else
  423. return &dummy_driver;
  424. #endif
  425. }
  426. static struct console sercons = {
  427. name : "ttyS",
  428. write: console_write,
  429. read : NULL,
  430. device : etrax_console_device,
  431. unblank : NULL,
  432. setup : console_setup,
  433. flags : CON_PRINTBUFFER,
  434. index : -1,
  435. cflag : 0,
  436. next : NULL
  437. };
  438. static struct console sercons0 = {
  439. name : "ttyS",
  440. write: console_write,
  441. read : NULL,
  442. device : etrax_console_device,
  443. unblank : NULL,
  444. setup : console_setup,
  445. flags : CON_PRINTBUFFER,
  446. index : 0,
  447. cflag : 0,
  448. next : NULL
  449. };
  450. static struct console sercons1 = {
  451. name : "ttyS",
  452. write: console_write,
  453. read : NULL,
  454. device : etrax_console_device,
  455. unblank : NULL,
  456. setup : console_setup,
  457. flags : CON_PRINTBUFFER,
  458. index : 1,
  459. cflag : 0,
  460. next : NULL
  461. };
  462. static struct console sercons2 = {
  463. name : "ttyS",
  464. write: console_write,
  465. read : NULL,
  466. device : etrax_console_device,
  467. unblank : NULL,
  468. setup : console_setup,
  469. flags : CON_PRINTBUFFER,
  470. index : 2,
  471. cflag : 0,
  472. next : NULL
  473. };
  474. static struct console sercons3 = {
  475. name : "ttyS",
  476. write: console_write,
  477. read : NULL,
  478. device : etrax_console_device,
  479. unblank : NULL,
  480. setup : console_setup,
  481. flags : CON_PRINTBUFFER,
  482. index : 3,
  483. cflag : 0,
  484. next : NULL
  485. };
  486. /*
  487. * Register console (for printk's etc)
  488. */
  489. int __init
  490. init_etrax_debug(void)
  491. {
  492. static int first = 1;
  493. if (!first) {
  494. unregister_console(&sercons);
  495. register_console(&sercons0);
  496. register_console(&sercons1);
  497. register_console(&sercons2);
  498. register_console(&sercons3);
  499. init_dummy_console();
  500. return 0;
  501. }
  502. first = 0;
  503. register_console(&sercons);
  504. start_port(port);
  505. #ifdef CONFIG_ETRAX_KGDB
  506. start_port(kgdb_port);
  507. #endif
  508. return 0;
  509. }
  510. __initcall(init_etrax_debug);