serial.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * (C) Copyright 2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <serial.h>
  25. #include <stdio_dev.h>
  26. #include <post.h>
  27. #include <linux/compiler.h>
  28. DECLARE_GLOBAL_DATA_PTR;
  29. static struct serial_device *serial_devices;
  30. static struct serial_device *serial_current;
  31. void serial_register(struct serial_device *dev)
  32. {
  33. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  34. dev->init += gd->reloc_off;
  35. dev->setbrg += gd->reloc_off;
  36. dev->getc += gd->reloc_off;
  37. dev->tstc += gd->reloc_off;
  38. dev->putc += gd->reloc_off;
  39. dev->puts += gd->reloc_off;
  40. #endif
  41. dev->next = serial_devices;
  42. serial_devices = dev;
  43. }
  44. void serial_initialize(void)
  45. {
  46. #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
  47. serial_register(&serial_smc_device);
  48. #endif
  49. #if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) || \
  50. defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
  51. serial_register(&serial_scc_device);
  52. #endif
  53. #if defined(CONFIG_SYS_NS16550_SERIAL)
  54. #if defined(CONFIG_SYS_NS16550_COM1)
  55. serial_register(&eserial1_device);
  56. #endif
  57. #if defined(CONFIG_SYS_NS16550_COM2)
  58. serial_register(&eserial2_device);
  59. #endif
  60. #if defined(CONFIG_SYS_NS16550_COM3)
  61. serial_register(&eserial3_device);
  62. #endif
  63. #if defined(CONFIG_SYS_NS16550_COM4)
  64. serial_register(&eserial4_device);
  65. #endif
  66. #endif /* CONFIG_SYS_NS16550_SERIAL */
  67. #if defined(CONFIG_FFUART)
  68. serial_register(&serial_ffuart_device);
  69. #endif
  70. #if defined(CONFIG_BTUART)
  71. serial_register(&serial_btuart_device);
  72. #endif
  73. #if defined(CONFIG_STUART)
  74. serial_register(&serial_stuart_device);
  75. #endif
  76. #if defined(CONFIG_S3C2410)
  77. serial_register(&s3c24xx_serial0_device);
  78. serial_register(&s3c24xx_serial1_device);
  79. serial_register(&s3c24xx_serial2_device);
  80. #endif
  81. #if defined(CONFIG_S5P)
  82. serial_register(&s5p_serial0_device);
  83. serial_register(&s5p_serial1_device);
  84. serial_register(&s5p_serial2_device);
  85. serial_register(&s5p_serial3_device);
  86. #endif
  87. #if defined(CONFIG_MPC512X)
  88. #if defined(CONFIG_SYS_PSC1)
  89. serial_register(&serial1_device);
  90. #endif
  91. #if defined(CONFIG_SYS_PSC3)
  92. serial_register(&serial3_device);
  93. #endif
  94. #if defined(CONFIG_SYS_PSC4)
  95. serial_register(&serial4_device);
  96. #endif
  97. #if defined(CONFIG_SYS_PSC6)
  98. serial_register(&serial6_device);
  99. #endif
  100. #endif
  101. #if defined(CONFIG_SYS_BFIN_UART)
  102. serial_register_bfin_uart();
  103. #endif
  104. #if defined(CONFIG_XILINX_UARTLITE)
  105. # ifdef XILINX_UARTLITE_BASEADDR
  106. serial_register(&uartlite_serial0_device);
  107. # endif /* XILINX_UARTLITE_BASEADDR */
  108. # ifdef XILINX_UARTLITE_BASEADDR1
  109. serial_register(&uartlite_serial1_device);
  110. # endif /* XILINX_UARTLITE_BASEADDR1 */
  111. # ifdef XILINX_UARTLITE_BASEADDR2
  112. serial_register(&uartlite_serial2_device);
  113. # endif /* XILINX_UARTLITE_BASEADDR2 */
  114. # ifdef XILINX_UARTLITE_BASEADDR3
  115. serial_register(&uartlite_serial3_device);
  116. # endif /* XILINX_UARTLITE_BASEADDR3 */
  117. #endif /* CONFIG_XILINX_UARTLITE */
  118. #if defined(CONFIG_ZYNQ_SERIAL)
  119. # ifdef CONFIG_ZYNQ_SERIAL_BASEADDR0
  120. serial_register(&uart_zynq_serial0_device);
  121. # endif
  122. # ifdef CONFIG_ZYNQ_SERIAL_BASEADDR1
  123. serial_register(&uart_zynq_serial1_device);
  124. # endif
  125. #endif
  126. serial_assign(default_serial_console()->name);
  127. }
  128. void serial_stdio_init(void)
  129. {
  130. struct stdio_dev dev;
  131. struct serial_device *s = serial_devices;
  132. while (s) {
  133. memset(&dev, 0, sizeof(dev));
  134. strcpy(dev.name, s->name);
  135. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  136. dev.start = s->init;
  137. dev.stop = s->uninit;
  138. dev.putc = s->putc;
  139. dev.puts = s->puts;
  140. dev.getc = s->getc;
  141. dev.tstc = s->tstc;
  142. stdio_register(&dev);
  143. s = s->next;
  144. }
  145. }
  146. int serial_assign(const char *name)
  147. {
  148. struct serial_device *s;
  149. for (s = serial_devices; s; s = s->next) {
  150. if (strcmp(s->name, name) == 0) {
  151. serial_current = s;
  152. return 0;
  153. }
  154. }
  155. return 1;
  156. }
  157. void serial_reinit_all(void)
  158. {
  159. struct serial_device *s;
  160. for (s = serial_devices; s; s = s->next)
  161. s->init();
  162. }
  163. static struct serial_device *get_current(void)
  164. {
  165. struct serial_device *dev;
  166. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  167. dev = default_serial_console();
  168. /* We must have a console device */
  169. if (!dev)
  170. panic("Cannot find console");
  171. } else
  172. dev = serial_current;
  173. return dev;
  174. }
  175. int serial_init(void)
  176. {
  177. return get_current()->init();
  178. }
  179. void serial_setbrg(void)
  180. {
  181. get_current()->setbrg();
  182. }
  183. int serial_getc(void)
  184. {
  185. return get_current()->getc();
  186. }
  187. int serial_tstc(void)
  188. {
  189. return get_current()->tstc();
  190. }
  191. void serial_putc(const char c)
  192. {
  193. get_current()->putc(c);
  194. }
  195. void serial_puts(const char *s)
  196. {
  197. get_current()->puts(s);
  198. }
  199. #if CONFIG_POST & CONFIG_SYS_POST_UART
  200. static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
  201. /* Mark weak until post/cpu/.../uart.c migrate over */
  202. __weak
  203. int uart_post_test(int flags)
  204. {
  205. unsigned char c;
  206. int ret, saved_baud, b;
  207. struct serial_device *saved_dev, *s;
  208. bd_t *bd = gd->bd;
  209. /* Save current serial state */
  210. ret = 0;
  211. saved_dev = serial_current;
  212. saved_baud = bd->bi_baudrate;
  213. for (s = serial_devices; s; s = s->next) {
  214. /* If this driver doesn't support loop back, skip it */
  215. if (!s->loop)
  216. continue;
  217. /* Test the next device */
  218. serial_current = s;
  219. ret = serial_init();
  220. if (ret)
  221. goto done;
  222. /* Consume anything that happens to be queued */
  223. while (serial_tstc())
  224. serial_getc();
  225. /* Enable loop back */
  226. s->loop(1);
  227. /* Test every available baud rate */
  228. for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
  229. bd->bi_baudrate = bauds[b];
  230. serial_setbrg();
  231. /*
  232. * Stick to printable chars to avoid issues:
  233. * - terminal corruption
  234. * - serial program reacting to sequences and sending
  235. * back random extra data
  236. * - most serial drivers add in extra chars (like \r\n)
  237. */
  238. for (c = 0x20; c < 0x7f; ++c) {
  239. /* Send it out */
  240. serial_putc(c);
  241. /* Make sure it's the same one */
  242. ret = (c != serial_getc());
  243. if (ret) {
  244. s->loop(0);
  245. goto done;
  246. }
  247. /* Clean up the output in case it was sent */
  248. serial_putc('\b');
  249. ret = ('\b' != serial_getc());
  250. if (ret) {
  251. s->loop(0);
  252. goto done;
  253. }
  254. }
  255. }
  256. /* Disable loop back */
  257. s->loop(0);
  258. /* XXX: There is no serial_uninit() !? */
  259. if (s->uninit)
  260. s->uninit();
  261. }
  262. done:
  263. /* Restore previous serial state */
  264. serial_current = saved_dev;
  265. bd->bi_baudrate = saved_baud;
  266. serial_reinit_all();
  267. serial_setbrg();
  268. return ret;
  269. }
  270. #endif