serial.c 5.3 KB

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