serial.c 6.6 KB

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