debugport.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (C) 2003, Axis Communications AB.
  3. */
  4. #include <linux/console.h>
  5. #include <linux/init.h>
  6. #include <linux/major.h>
  7. #include <linux/delay.h>
  8. #include <linux/tty.h>
  9. #include <asm/system.h>
  10. #include <asm/io.h>
  11. #include <asm/arch/hwregs/ser_defs.h>
  12. #include <asm/arch/hwregs/dma_defs.h>
  13. #include <asm/arch/pinmux.h>
  14. #include <asm/irq.h>
  15. #include <asm/arch/hwregs/intr_vect_defs.h>
  16. struct dbg_port
  17. {
  18. unsigned char nbr;
  19. unsigned long instance;
  20. unsigned int started;
  21. unsigned long baudrate;
  22. unsigned char parity;
  23. unsigned int bits;
  24. };
  25. struct dbg_port ports[] =
  26. {
  27. {
  28. 0,
  29. regi_ser0,
  30. 0,
  31. 115200,
  32. 'N',
  33. 8
  34. },
  35. {
  36. 1,
  37. regi_ser1,
  38. 0,
  39. 115200,
  40. 'N',
  41. 8
  42. },
  43. {
  44. 2,
  45. regi_ser2,
  46. 0,
  47. 115200,
  48. 'N',
  49. 8
  50. },
  51. {
  52. 3,
  53. regi_ser3,
  54. 0,
  55. 115200,
  56. 'N',
  57. 8
  58. }
  59. };
  60. static struct dbg_port *port =
  61. #if defined(CONFIG_ETRAX_DEBUG_PORT0)
  62. &ports[0];
  63. #elif defined(CONFIG_ETRAX_DEBUG_PORT1)
  64. &ports[1];
  65. #elif defined(CONFIG_ETRAX_DEBUG_PORT2)
  66. &ports[2];
  67. #elif defined(CONFIG_ETRAX_DEBUG_PORT3)
  68. &ports[3];
  69. #else
  70. NULL;
  71. #endif
  72. #ifdef CONFIG_ETRAX_KGDB
  73. static struct dbg_port *kgdb_port =
  74. #if defined(CONFIG_ETRAX_KGDB_PORT0)
  75. &ports[0];
  76. #elif defined(CONFIG_ETRAX_KGDB_PORT1)
  77. &ports[1];
  78. #elif defined(CONFIG_ETRAX_KGDB_PORT2)
  79. &ports[2];
  80. #elif defined(CONFIG_ETRAX_KGDB_PORT3)
  81. &ports[3];
  82. #else
  83. NULL;
  84. #endif
  85. #endif
  86. #ifdef CONFIG_ETRAXFS_SIM
  87. extern void print_str( const char *str );
  88. static char buffer[1024];
  89. static char msg[] = "Debug: ";
  90. static int buffer_pos = sizeof(msg) - 1;
  91. #endif
  92. extern struct tty_driver *serial_driver;
  93. static void
  94. start_port(struct dbg_port* p)
  95. {
  96. if (!p)
  97. return;
  98. if (p->started)
  99. return;
  100. p->started = 1;
  101. if (p->nbr == 1)
  102. crisv32_pinmux_alloc_fixed(pinmux_ser1);
  103. else if (p->nbr == 2)
  104. crisv32_pinmux_alloc_fixed(pinmux_ser2);
  105. else if (p->nbr == 3)
  106. crisv32_pinmux_alloc_fixed(pinmux_ser3);
  107. /* Set up serial port registers */
  108. reg_ser_rw_tr_ctrl tr_ctrl = {0};
  109. reg_ser_rw_tr_dma_en tr_dma_en = {0};
  110. reg_ser_rw_rec_ctrl rec_ctrl = {0};
  111. reg_ser_rw_tr_baud_div tr_baud_div = {0};
  112. reg_ser_rw_rec_baud_div rec_baud_div = {0};
  113. tr_ctrl.base_freq = rec_ctrl.base_freq = regk_ser_f29_493;
  114. tr_dma_en.en = rec_ctrl.dma_mode = regk_ser_no;
  115. tr_baud_div.div = rec_baud_div.div = 29493000 / p->baudrate / 8;
  116. tr_ctrl.en = rec_ctrl.en = 1;
  117. if (p->parity == 'O')
  118. {
  119. tr_ctrl.par_en = regk_ser_yes;
  120. tr_ctrl.par = regk_ser_odd;
  121. rec_ctrl.par_en = regk_ser_yes;
  122. rec_ctrl.par = regk_ser_odd;
  123. }
  124. else if (p->parity == 'E')
  125. {
  126. tr_ctrl.par_en = regk_ser_yes;
  127. tr_ctrl.par = regk_ser_even;
  128. rec_ctrl.par_en = regk_ser_yes;
  129. rec_ctrl.par = regk_ser_odd;
  130. }
  131. if (p->bits == 7)
  132. {
  133. tr_ctrl.data_bits = regk_ser_bits7;
  134. rec_ctrl.data_bits = regk_ser_bits7;
  135. }
  136. REG_WR (ser, p->instance, rw_tr_baud_div, tr_baud_div);
  137. REG_WR (ser, p->instance, rw_rec_baud_div, rec_baud_div);
  138. REG_WR (ser, p->instance, rw_tr_dma_en, tr_dma_en);
  139. REG_WR (ser, p->instance, rw_tr_ctrl, tr_ctrl);
  140. REG_WR (ser, p->instance, rw_rec_ctrl, rec_ctrl);
  141. }
  142. /* No debug */
  143. #ifdef CONFIG_ETRAX_DEBUG_PORT_NULL
  144. static void
  145. console_write(struct console *co, const char *buf, unsigned int len)
  146. {
  147. return;
  148. }
  149. /* Target debug */
  150. #elif !defined(CONFIG_ETRAXFS_SIM)
  151. static void
  152. console_write_direct(struct console *co, const char *buf, unsigned int len)
  153. {
  154. int i;
  155. reg_ser_r_stat_din stat;
  156. reg_ser_rw_tr_dma_en tr_dma_en, old;
  157. /* Switch to manual mode */
  158. tr_dma_en = old = REG_RD (ser, port->instance, rw_tr_dma_en);
  159. if (tr_dma_en.en == regk_ser_yes) {
  160. tr_dma_en.en = regk_ser_no;
  161. REG_WR(ser, port->instance, rw_tr_dma_en, tr_dma_en);
  162. }
  163. /* Send data */
  164. for (i = 0; i < len; i++) {
  165. /* LF -> CRLF */
  166. if (buf[i] == '\n') {
  167. do {
  168. stat = REG_RD (ser, port->instance, r_stat_din);
  169. } while (!stat.tr_rdy);
  170. REG_WR_INT (ser, port->instance, rw_dout, '\r');
  171. }
  172. /* Wait until transmitter is ready and send.*/
  173. do {
  174. stat = REG_RD (ser, port->instance, r_stat_din);
  175. } while (!stat.tr_rdy);
  176. REG_WR_INT (ser, port->instance, rw_dout, buf[i]);
  177. }
  178. /* Restore mode */
  179. if (tr_dma_en.en != old.en)
  180. REG_WR(ser, port->instance, rw_tr_dma_en, old);
  181. }
  182. static void
  183. console_write(struct console *co, const char *buf, unsigned int len)
  184. {
  185. if (!port)
  186. return;
  187. console_write_direct(co, buf, len);
  188. }
  189. #else
  190. /* VCS debug */
  191. static void
  192. console_write(struct console *co, const char *buf, unsigned int len)
  193. {
  194. char* pos;
  195. pos = memchr(buf, '\n', len);
  196. if (pos) {
  197. int l = ++pos - buf;
  198. memcpy(buffer + buffer_pos, buf, l);
  199. memcpy(buffer, msg, sizeof(msg) - 1);
  200. buffer[buffer_pos + l] = '\0';
  201. print_str(buffer);
  202. buffer_pos = sizeof(msg) - 1;
  203. if (pos - buf != len) {
  204. memcpy(buffer + buffer_pos, pos, len - l);
  205. buffer_pos += len - l;
  206. }
  207. } else {
  208. memcpy(buffer + buffer_pos, buf, len);
  209. buffer_pos += len;
  210. }
  211. }
  212. #endif
  213. int raw_printk(const char *fmt, ...)
  214. {
  215. static char buf[1024];
  216. int printed_len;
  217. va_list args;
  218. va_start(args, fmt);
  219. printed_len = vsnprintf(buf, sizeof(buf), fmt, args);
  220. va_end(args);
  221. console_write(NULL, buf, strlen(buf));
  222. return printed_len;
  223. }
  224. void
  225. stupid_debug(char* buf)
  226. {
  227. console_write(NULL, buf, strlen(buf));
  228. }
  229. #ifdef CONFIG_ETRAX_KGDB
  230. /* Use polling to get a single character from the kernel debug port */
  231. int
  232. getDebugChar(void)
  233. {
  234. reg_ser_rs_status_data stat;
  235. reg_ser_rw_ack_intr ack_intr = { 0 };
  236. do {
  237. stat = REG_RD(ser, kgdb_instance, rs_status_data);
  238. } while (!stat.data_avail);
  239. /* Ack the data_avail interrupt. */
  240. ack_intr.data_avail = 1;
  241. REG_WR(ser, kgdb_instance, rw_ack_intr, ack_intr);
  242. return stat.data;
  243. }
  244. /* Use polling to put a single character to the kernel debug port */
  245. void
  246. putDebugChar(int val)
  247. {
  248. reg_ser_r_status_data stat;
  249. do {
  250. stat = REG_RD (ser, kgdb_instance, r_status_data);
  251. } while (!stat.tr_ready);
  252. REG_WR (ser, kgdb_instance, rw_data_out, REG_TYPE_CONV(reg_ser_rw_data_out, int, val));
  253. }
  254. #endif /* CONFIG_ETRAX_KGDB */
  255. static int __init
  256. console_setup(struct console *co, char *options)
  257. {
  258. char* s;
  259. if (options) {
  260. port = &ports[co->index];
  261. port->baudrate = 115200;
  262. port->parity = 'N';
  263. port->bits = 8;
  264. port->baudrate = simple_strtoul(options, NULL, 10);
  265. s = options;
  266. while(*s >= '0' && *s <= '9')
  267. s++;
  268. if (*s) port->parity = *s++;
  269. if (*s) port->bits = *s++ - '0';
  270. port->started = 0;
  271. start_port(port);
  272. }
  273. return 0;
  274. }
  275. /* This is a dummy serial device that throws away anything written to it.
  276. * This is used when no debug output is wanted.
  277. */
  278. static struct tty_driver dummy_driver;
  279. static int dummy_open(struct tty_struct *tty, struct file * filp)
  280. {
  281. return 0;
  282. }
  283. static void dummy_close(struct tty_struct *tty, struct file * filp)
  284. {
  285. }
  286. static int dummy_write(struct tty_struct * tty,
  287. const unsigned char *buf, int count)
  288. {
  289. return count;
  290. }
  291. static int
  292. dummy_write_room(struct tty_struct *tty)
  293. {
  294. return 8192;
  295. }
  296. void __init
  297. init_dummy_console(void)
  298. {
  299. memset(&dummy_driver, 0, sizeof(struct tty_driver));
  300. dummy_driver.driver_name = "serial";
  301. dummy_driver.name = "ttyS";
  302. dummy_driver.major = TTY_MAJOR;
  303. dummy_driver.minor_start = 68;
  304. dummy_driver.num = 1; /* etrax100 has 4 serial ports */
  305. dummy_driver.type = TTY_DRIVER_TYPE_SERIAL;
  306. dummy_driver.subtype = SERIAL_TYPE_NORMAL;
  307. dummy_driver.init_termios = tty_std_termios;
  308. dummy_driver.init_termios.c_cflag =
  309. B115200 | CS8 | CREAD | HUPCL | CLOCAL; /* is normally B9600 default... */
  310. dummy_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  311. dummy_driver.open = dummy_open;
  312. dummy_driver.close = dummy_close;
  313. dummy_driver.write = dummy_write;
  314. dummy_driver.write_room = dummy_write_room;
  315. if (tty_register_driver(&dummy_driver))
  316. panic("Couldn't register dummy serial driver\n");
  317. }
  318. static struct tty_driver*
  319. crisv32_console_device(struct console* co, int *index)
  320. {
  321. if (port)
  322. *index = port->nbr;
  323. return port ? serial_driver : &dummy_driver;
  324. }
  325. static struct console sercons = {
  326. name : "ttyS",
  327. write: console_write,
  328. read : NULL,
  329. device : crisv32_console_device,
  330. unblank : NULL,
  331. setup : console_setup,
  332. flags : CON_PRINTBUFFER,
  333. index : -1,
  334. cflag : 0,
  335. next : NULL
  336. };
  337. static struct console sercons0 = {
  338. name : "ttyS",
  339. write: console_write,
  340. read : NULL,
  341. device : crisv32_console_device,
  342. unblank : NULL,
  343. setup : console_setup,
  344. flags : CON_PRINTBUFFER,
  345. index : 0,
  346. cflag : 0,
  347. next : NULL
  348. };
  349. static struct console sercons1 = {
  350. name : "ttyS",
  351. write: console_write,
  352. read : NULL,
  353. device : crisv32_console_device,
  354. unblank : NULL,
  355. setup : console_setup,
  356. flags : CON_PRINTBUFFER,
  357. index : 1,
  358. cflag : 0,
  359. next : NULL
  360. };
  361. static struct console sercons2 = {
  362. name : "ttyS",
  363. write: console_write,
  364. read : NULL,
  365. device : crisv32_console_device,
  366. unblank : NULL,
  367. setup : console_setup,
  368. flags : CON_PRINTBUFFER,
  369. index : 2,
  370. cflag : 0,
  371. next : NULL
  372. };
  373. static struct console sercons3 = {
  374. name : "ttyS",
  375. write: console_write,
  376. read : NULL,
  377. device : crisv32_console_device,
  378. unblank : NULL,
  379. setup : console_setup,
  380. flags : CON_PRINTBUFFER,
  381. index : 3,
  382. cflag : 0,
  383. next : NULL
  384. };
  385. /* Register console for printk's, etc. */
  386. int __init
  387. init_etrax_debug(void)
  388. {
  389. static int first = 1;
  390. if (!first) {
  391. unregister_console(&sercons);
  392. register_console(&sercons0);
  393. register_console(&sercons1);
  394. register_console(&sercons2);
  395. register_console(&sercons3);
  396. init_dummy_console();
  397. return 0;
  398. }
  399. first = 0;
  400. register_console(&sercons);
  401. start_port(port);
  402. #ifdef CONFIG_ETRAX_KGDB
  403. start_port(kgdb_port);
  404. #endif /* CONFIG_ETRAX_KGDB */
  405. return 0;
  406. }
  407. __initcall(init_etrax_debug);