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