serial.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. static void serial_null(void)
  32. {
  33. }
  34. #define serial_initfunc(name) \
  35. void name(void) \
  36. __attribute__((weak, alias("serial_null")));
  37. void serial_register(struct serial_device *dev)
  38. {
  39. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  40. dev->start += gd->reloc_off;
  41. dev->setbrg += gd->reloc_off;
  42. dev->getc += gd->reloc_off;
  43. dev->tstc += gd->reloc_off;
  44. dev->putc += gd->reloc_off;
  45. dev->puts += gd->reloc_off;
  46. #endif
  47. dev->next = serial_devices;
  48. serial_devices = dev;
  49. }
  50. void serial_initialize(void)
  51. {
  52. #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
  53. serial_register(&serial_smc_device);
  54. #endif
  55. #if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) || \
  56. defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
  57. serial_register(&serial_scc_device);
  58. #endif
  59. #if defined(CONFIG_SYS_NS16550_SERIAL)
  60. #if defined(CONFIG_SYS_NS16550_COM1)
  61. serial_register(&eserial1_device);
  62. #endif
  63. #if defined(CONFIG_SYS_NS16550_COM2)
  64. serial_register(&eserial2_device);
  65. #endif
  66. #if defined(CONFIG_SYS_NS16550_COM3)
  67. serial_register(&eserial3_device);
  68. #endif
  69. #if defined(CONFIG_SYS_NS16550_COM4)
  70. serial_register(&eserial4_device);
  71. #endif
  72. #endif /* CONFIG_SYS_NS16550_SERIAL */
  73. #if defined(CONFIG_FFUART)
  74. serial_register(&serial_ffuart_device);
  75. #endif
  76. #if defined(CONFIG_BTUART)
  77. serial_register(&serial_btuart_device);
  78. #endif
  79. #if defined(CONFIG_STUART)
  80. serial_register(&serial_stuart_device);
  81. #endif
  82. #if defined(CONFIG_S3C2410)
  83. serial_register(&s3c24xx_serial0_device);
  84. serial_register(&s3c24xx_serial1_device);
  85. serial_register(&s3c24xx_serial2_device);
  86. #endif
  87. #if defined(CONFIG_S5P)
  88. serial_register(&s5p_serial0_device);
  89. serial_register(&s5p_serial1_device);
  90. serial_register(&s5p_serial2_device);
  91. serial_register(&s5p_serial3_device);
  92. #endif
  93. #if defined(CONFIG_MPC512X)
  94. #if defined(CONFIG_SYS_PSC1)
  95. serial_register(&serial1_device);
  96. #endif
  97. #if defined(CONFIG_SYS_PSC3)
  98. serial_register(&serial3_device);
  99. #endif
  100. #if defined(CONFIG_SYS_PSC4)
  101. serial_register(&serial4_device);
  102. #endif
  103. #if defined(CONFIG_SYS_PSC6)
  104. serial_register(&serial6_device);
  105. #endif
  106. #endif
  107. #if defined(CONFIG_SYS_BFIN_UART)
  108. serial_register_bfin_uart();
  109. #endif
  110. #if defined(CONFIG_XILINX_UARTLITE)
  111. # ifdef XILINX_UARTLITE_BASEADDR
  112. serial_register(&uartlite_serial0_device);
  113. # endif /* XILINX_UARTLITE_BASEADDR */
  114. # ifdef XILINX_UARTLITE_BASEADDR1
  115. serial_register(&uartlite_serial1_device);
  116. # endif /* XILINX_UARTLITE_BASEADDR1 */
  117. # ifdef XILINX_UARTLITE_BASEADDR2
  118. serial_register(&uartlite_serial2_device);
  119. # endif /* XILINX_UARTLITE_BASEADDR2 */
  120. # ifdef XILINX_UARTLITE_BASEADDR3
  121. serial_register(&uartlite_serial3_device);
  122. # endif /* XILINX_UARTLITE_BASEADDR3 */
  123. #endif /* CONFIG_XILINX_UARTLITE */
  124. #if defined(CONFIG_ZYNQ_SERIAL)
  125. # ifdef CONFIG_ZYNQ_SERIAL_BASEADDR0
  126. serial_register(&uart_zynq_serial0_device);
  127. # endif
  128. # ifdef CONFIG_ZYNQ_SERIAL_BASEADDR1
  129. serial_register(&uart_zynq_serial1_device);
  130. # endif
  131. #endif
  132. serial_assign(default_serial_console()->name);
  133. }
  134. void serial_stdio_init(void)
  135. {
  136. struct stdio_dev dev;
  137. struct serial_device *s = serial_devices;
  138. while (s) {
  139. memset(&dev, 0, sizeof(dev));
  140. strcpy(dev.name, s->name);
  141. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  142. dev.start = s->start;
  143. dev.stop = s->stop;
  144. dev.putc = s->putc;
  145. dev.puts = s->puts;
  146. dev.getc = s->getc;
  147. dev.tstc = s->tstc;
  148. stdio_register(&dev);
  149. s = s->next;
  150. }
  151. }
  152. int serial_assign(const char *name)
  153. {
  154. struct serial_device *s;
  155. for (s = serial_devices; s; s = s->next) {
  156. if (strcmp(s->name, name) == 0) {
  157. serial_current = s;
  158. return 0;
  159. }
  160. }
  161. return 1;
  162. }
  163. void serial_reinit_all(void)
  164. {
  165. struct serial_device *s;
  166. for (s = serial_devices; s; s = s->next)
  167. s->start();
  168. }
  169. static struct serial_device *get_current(void)
  170. {
  171. struct serial_device *dev;
  172. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  173. dev = default_serial_console();
  174. /* We must have a console device */
  175. if (!dev)
  176. panic("Cannot find console");
  177. } else
  178. dev = serial_current;
  179. return dev;
  180. }
  181. int serial_init(void)
  182. {
  183. return get_current()->start();
  184. }
  185. void serial_setbrg(void)
  186. {
  187. get_current()->setbrg();
  188. }
  189. int serial_getc(void)
  190. {
  191. return get_current()->getc();
  192. }
  193. int serial_tstc(void)
  194. {
  195. return get_current()->tstc();
  196. }
  197. void serial_putc(const char c)
  198. {
  199. get_current()->putc(c);
  200. }
  201. void serial_puts(const char *s)
  202. {
  203. get_current()->puts(s);
  204. }
  205. #if CONFIG_POST & CONFIG_SYS_POST_UART
  206. static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
  207. /* Mark weak until post/cpu/.../uart.c migrate over */
  208. __weak
  209. int uart_post_test(int flags)
  210. {
  211. unsigned char c;
  212. int ret, saved_baud, b;
  213. struct serial_device *saved_dev, *s;
  214. bd_t *bd = gd->bd;
  215. /* Save current serial state */
  216. ret = 0;
  217. saved_dev = serial_current;
  218. saved_baud = bd->bi_baudrate;
  219. for (s = serial_devices; s; s = s->next) {
  220. /* If this driver doesn't support loop back, skip it */
  221. if (!s->loop)
  222. continue;
  223. /* Test the next device */
  224. serial_current = s;
  225. ret = serial_init();
  226. if (ret)
  227. goto done;
  228. /* Consume anything that happens to be queued */
  229. while (serial_tstc())
  230. serial_getc();
  231. /* Enable loop back */
  232. s->loop(1);
  233. /* Test every available baud rate */
  234. for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
  235. bd->bi_baudrate = bauds[b];
  236. serial_setbrg();
  237. /*
  238. * Stick to printable chars to avoid issues:
  239. * - terminal corruption
  240. * - serial program reacting to sequences and sending
  241. * back random extra data
  242. * - most serial drivers add in extra chars (like \r\n)
  243. */
  244. for (c = 0x20; c < 0x7f; ++c) {
  245. /* Send it out */
  246. serial_putc(c);
  247. /* Make sure it's the same one */
  248. ret = (c != serial_getc());
  249. if (ret) {
  250. s->loop(0);
  251. goto done;
  252. }
  253. /* Clean up the output in case it was sent */
  254. serial_putc('\b');
  255. ret = ('\b' != serial_getc());
  256. if (ret) {
  257. s->loop(0);
  258. goto done;
  259. }
  260. }
  261. }
  262. /* Disable loop back */
  263. s->loop(0);
  264. /* XXX: There is no serial_stop() !? */
  265. if (s->stop)
  266. s->stop();
  267. }
  268. done:
  269. /* Restore previous serial state */
  270. serial_current = saved_dev;
  271. bd->bi_baudrate = saved_baud;
  272. serial_reinit_all();
  273. serial_setbrg();
  274. return ret;
  275. }
  276. #endif