serial.c 6.8 KB

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