serial.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. DECLARE_GLOBAL_DATA_PTR;
  27. static struct serial_device *serial_devices = NULL;
  28. static struct serial_device *serial_current = NULL;
  29. #if !defined(CONFIG_LWMON) && !defined(CONFIG_PXA250) && !defined(CONFIG_PXA27X)
  30. struct serial_device *__default_serial_console (void)
  31. {
  32. #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
  33. return &serial_smc_device;
  34. #elif defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
  35. || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
  36. return &serial_scc_device;
  37. #elif defined(CONFIG_4xx) \
  38. || defined(CONFIG_MB86R0x) || defined(CONFIG_MPC5xxx) \
  39. || defined(CONFIG_MPC83xx) || defined(CONFIG_MPC85xx) \
  40. || defined(CONFIG_MPC86xx) || defined(CONFIG_SYS_SC520) \
  41. || defined(CONFIG_TEGRA2)
  42. #if defined(CONFIG_CONS_INDEX) && defined(CONFIG_SYS_NS16550_SERIAL)
  43. #if (CONFIG_CONS_INDEX==1)
  44. return &eserial1_device;
  45. #elif (CONFIG_CONS_INDEX==2)
  46. return &eserial2_device;
  47. #elif (CONFIG_CONS_INDEX==3)
  48. return &eserial3_device;
  49. #elif (CONFIG_CONS_INDEX==4)
  50. return &eserial4_device;
  51. #else
  52. #error "Bad CONFIG_CONS_INDEX."
  53. #endif
  54. #else
  55. return &serial0_device;
  56. #endif
  57. #elif defined(CONFIG_MPC512X)
  58. #if (CONFIG_PSC_CONSOLE == 3)
  59. return &serial3_device;
  60. #elif (CONFIG_PSC_CONSOLE == 6)
  61. return &serial6_device;
  62. #else
  63. #error "Bad CONFIG_PSC_CONSOLE."
  64. #endif
  65. #elif defined(CONFIG_S3C2410)
  66. #if defined(CONFIG_SERIAL1)
  67. return &s3c24xx_serial0_device;
  68. #elif defined(CONFIG_SERIAL2)
  69. return &s3c24xx_serial1_device;
  70. #elif defined(CONFIG_SERIAL3)
  71. return &s3c24xx_serial2_device;
  72. #else
  73. #error "CONFIG_SERIAL? missing."
  74. #endif
  75. #elif defined(CONFIG_S5P)
  76. #if defined(CONFIG_SERIAL0)
  77. return &s5p_serial0_device;
  78. #elif defined(CONFIG_SERIAL1)
  79. return &s5p_serial1_device;
  80. #elif defined(CONFIG_SERIAL2)
  81. return &s5p_serial2_device;
  82. #elif defined(CONFIG_SERIAL3)
  83. return &s5p_serial3_device;
  84. #else
  85. #error "CONFIG_SERIAL? missing."
  86. #endif
  87. #elif defined(CONFIG_OMAP3_ZOOM2)
  88. return ZOOM2_DEFAULT_SERIAL_DEVICE;
  89. #else
  90. #error No default console
  91. #endif
  92. }
  93. struct serial_device *default_serial_console(void) __attribute__((weak, alias("__default_serial_console")));
  94. #endif
  95. int serial_register (struct serial_device *dev)
  96. {
  97. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  98. dev->init += gd->reloc_off;
  99. dev->setbrg += gd->reloc_off;
  100. dev->getc += gd->reloc_off;
  101. dev->tstc += gd->reloc_off;
  102. dev->putc += gd->reloc_off;
  103. dev->puts += gd->reloc_off;
  104. #endif
  105. dev->next = serial_devices;
  106. serial_devices = dev;
  107. return 0;
  108. }
  109. void serial_initialize (void)
  110. {
  111. #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
  112. serial_register (&serial_smc_device);
  113. #endif
  114. #if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
  115. || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
  116. serial_register (&serial_scc_device);
  117. #endif
  118. #if defined(CONFIG_SYS_NS16550_SERIAL)
  119. #if defined(CONFIG_SYS_NS16550_COM1)
  120. serial_register(&eserial1_device);
  121. #endif
  122. #if defined(CONFIG_SYS_NS16550_COM2)
  123. serial_register(&eserial2_device);
  124. #endif
  125. #if defined(CONFIG_SYS_NS16550_COM3)
  126. serial_register(&eserial3_device);
  127. #endif
  128. #if defined(CONFIG_SYS_NS16550_COM4)
  129. serial_register(&eserial4_device);
  130. #endif
  131. #endif /* CONFIG_SYS_NS16550_SERIAL */
  132. #if defined (CONFIG_FFUART)
  133. serial_register(&serial_ffuart_device);
  134. #endif
  135. #if defined (CONFIG_BTUART)
  136. serial_register(&serial_btuart_device);
  137. #endif
  138. #if defined (CONFIG_STUART)
  139. serial_register(&serial_stuart_device);
  140. #endif
  141. #if defined(CONFIG_S3C2410)
  142. serial_register(&s3c24xx_serial0_device);
  143. serial_register(&s3c24xx_serial1_device);
  144. serial_register(&s3c24xx_serial2_device);
  145. #endif
  146. #if defined(CONFIG_S5P)
  147. serial_register(&s5p_serial0_device);
  148. serial_register(&s5p_serial1_device);
  149. serial_register(&s5p_serial2_device);
  150. serial_register(&s5p_serial3_device);
  151. #endif
  152. #if defined(CONFIG_MPC512X)
  153. #if defined(CONFIG_SYS_PSC1)
  154. serial_register(&serial1_device);
  155. #endif
  156. #if defined(CONFIG_SYS_PSC3)
  157. serial_register(&serial3_device);
  158. #endif
  159. #if defined(CONFIG_SYS_PSC4)
  160. serial_register(&serial4_device);
  161. #endif
  162. #if defined(CONFIG_SYS_PSC6)
  163. serial_register(&serial6_device);
  164. #endif
  165. #endif
  166. serial_assign (default_serial_console ()->name);
  167. }
  168. void serial_stdio_init (void)
  169. {
  170. struct stdio_dev dev;
  171. struct serial_device *s = serial_devices;
  172. while (s) {
  173. memset (&dev, 0, sizeof (dev));
  174. strcpy (dev.name, s->name);
  175. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  176. dev.start = s->init;
  177. dev.stop = s->uninit;
  178. dev.putc = s->putc;
  179. dev.puts = s->puts;
  180. dev.getc = s->getc;
  181. dev.tstc = s->tstc;
  182. stdio_register (&dev);
  183. s = s->next;
  184. }
  185. }
  186. int serial_assign (char *name)
  187. {
  188. struct serial_device *s;
  189. for (s = serial_devices; s; s = s->next) {
  190. if (strcmp (s->name, name) == 0) {
  191. serial_current = s;
  192. return 0;
  193. }
  194. }
  195. return 1;
  196. }
  197. void serial_reinit_all (void)
  198. {
  199. struct serial_device *s;
  200. for (s = serial_devices; s; s = s->next) {
  201. s->init ();
  202. }
  203. }
  204. int serial_init (void)
  205. {
  206. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  207. struct serial_device *dev = default_serial_console ();
  208. return dev->init ();
  209. }
  210. return serial_current->init ();
  211. }
  212. void serial_setbrg (void)
  213. {
  214. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  215. struct serial_device *dev = default_serial_console ();
  216. dev->setbrg ();
  217. return;
  218. }
  219. serial_current->setbrg ();
  220. }
  221. int serial_getc (void)
  222. {
  223. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  224. struct serial_device *dev = default_serial_console ();
  225. return dev->getc ();
  226. }
  227. return serial_current->getc ();
  228. }
  229. int serial_tstc (void)
  230. {
  231. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  232. struct serial_device *dev = default_serial_console ();
  233. return dev->tstc ();
  234. }
  235. return serial_current->tstc ();
  236. }
  237. void serial_putc (const char c)
  238. {
  239. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  240. struct serial_device *dev = default_serial_console ();
  241. dev->putc (c);
  242. return;
  243. }
  244. serial_current->putc (c);
  245. }
  246. void serial_puts (const char *s)
  247. {
  248. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  249. struct serial_device *dev = default_serial_console ();
  250. dev->puts (s);
  251. return;
  252. }
  253. serial_current->puts (s);
  254. }