serial.c 5.7 KB

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