serial.c 6.4 KB

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