serial.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. int serial_register (struct serial_device *dev)
  30. {
  31. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  32. dev->init += gd->reloc_off;
  33. dev->setbrg += gd->reloc_off;
  34. dev->getc += gd->reloc_off;
  35. dev->tstc += gd->reloc_off;
  36. dev->putc += gd->reloc_off;
  37. dev->puts += gd->reloc_off;
  38. #endif
  39. dev->next = serial_devices;
  40. serial_devices = dev;
  41. return 0;
  42. }
  43. void serial_initialize (void)
  44. {
  45. #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
  46. serial_register (&serial_smc_device);
  47. #endif
  48. #if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
  49. || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
  50. serial_register (&serial_scc_device);
  51. #endif
  52. #if defined(CONFIG_SYS_NS16550_SERIAL)
  53. #if defined(CONFIG_SYS_NS16550_COM1)
  54. serial_register(&eserial1_device);
  55. #endif
  56. #if defined(CONFIG_SYS_NS16550_COM2)
  57. serial_register(&eserial2_device);
  58. #endif
  59. #if defined(CONFIG_SYS_NS16550_COM3)
  60. serial_register(&eserial3_device);
  61. #endif
  62. #if defined(CONFIG_SYS_NS16550_COM4)
  63. serial_register(&eserial4_device);
  64. #endif
  65. #endif /* CONFIG_SYS_NS16550_SERIAL */
  66. #if defined (CONFIG_FFUART)
  67. serial_register(&serial_ffuart_device);
  68. #endif
  69. #if defined (CONFIG_BTUART)
  70. serial_register(&serial_btuart_device);
  71. #endif
  72. #if defined (CONFIG_STUART)
  73. serial_register(&serial_stuart_device);
  74. #endif
  75. #if defined(CONFIG_S3C2410)
  76. serial_register(&s3c24xx_serial0_device);
  77. serial_register(&s3c24xx_serial1_device);
  78. serial_register(&s3c24xx_serial2_device);
  79. #endif
  80. #if defined(CONFIG_S5P)
  81. serial_register(&s5p_serial0_device);
  82. serial_register(&s5p_serial1_device);
  83. serial_register(&s5p_serial2_device);
  84. serial_register(&s5p_serial3_device);
  85. #endif
  86. #if defined(CONFIG_MPC512X)
  87. #if defined(CONFIG_SYS_PSC1)
  88. serial_register(&serial1_device);
  89. #endif
  90. #if defined(CONFIG_SYS_PSC3)
  91. serial_register(&serial3_device);
  92. #endif
  93. #if defined(CONFIG_SYS_PSC4)
  94. serial_register(&serial4_device);
  95. #endif
  96. #if defined(CONFIG_SYS_PSC6)
  97. serial_register(&serial6_device);
  98. #endif
  99. #endif
  100. serial_assign (default_serial_console ()->name);
  101. }
  102. void serial_stdio_init (void)
  103. {
  104. struct stdio_dev dev;
  105. struct serial_device *s = serial_devices;
  106. while (s) {
  107. memset (&dev, 0, sizeof (dev));
  108. strcpy (dev.name, s->name);
  109. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  110. dev.start = s->init;
  111. dev.stop = s->uninit;
  112. dev.putc = s->putc;
  113. dev.puts = s->puts;
  114. dev.getc = s->getc;
  115. dev.tstc = s->tstc;
  116. stdio_register (&dev);
  117. s = s->next;
  118. }
  119. }
  120. int serial_assign (char *name)
  121. {
  122. struct serial_device *s;
  123. for (s = serial_devices; s; s = s->next) {
  124. if (strcmp (s->name, name) == 0) {
  125. serial_current = s;
  126. return 0;
  127. }
  128. }
  129. return 1;
  130. }
  131. void serial_reinit_all (void)
  132. {
  133. struct serial_device *s;
  134. for (s = serial_devices; s; s = s->next) {
  135. s->init ();
  136. }
  137. }
  138. int serial_init (void)
  139. {
  140. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  141. struct serial_device *dev = default_serial_console ();
  142. return dev->init ();
  143. }
  144. return serial_current->init ();
  145. }
  146. void serial_setbrg (void)
  147. {
  148. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  149. struct serial_device *dev = default_serial_console ();
  150. dev->setbrg ();
  151. return;
  152. }
  153. serial_current->setbrg ();
  154. }
  155. int serial_getc (void)
  156. {
  157. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  158. struct serial_device *dev = default_serial_console ();
  159. return dev->getc ();
  160. }
  161. return serial_current->getc ();
  162. }
  163. int serial_tstc (void)
  164. {
  165. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  166. struct serial_device *dev = default_serial_console ();
  167. return dev->tstc ();
  168. }
  169. return serial_current->tstc ();
  170. }
  171. void serial_putc (const char c)
  172. {
  173. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  174. struct serial_device *dev = default_serial_console ();
  175. dev->putc (c);
  176. return;
  177. }
  178. serial_current->putc (c);
  179. }
  180. void serial_puts (const char *s)
  181. {
  182. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  183. struct serial_device *dev = default_serial_console ();
  184. dev->puts (s);
  185. return;
  186. }
  187. serial_current->puts (s);
  188. }