serial.c 6.5 KB

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