debugport.c 9.4 KB

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