serial.c 5.4 KB

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