serial.c 6.8 KB

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