serial.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <devices.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. #if defined(CONFIG_SERIAL_MULTI)
  28. static struct serial_device *serial_devices = NULL;
  29. static struct serial_device *serial_current = NULL;
  30. #if !defined(CONFIG_LWMON) && !defined(CONFIG_PXA27X)
  31. struct serial_device *default_serial_console (void)
  32. {
  33. #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
  34. return &serial_smc_device;
  35. #elif defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
  36. || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
  37. return &serial_scc_device;
  38. #elif defined(CONFIG_405GP) || defined(CONFIG_405CR) || defined(CONFIG_440) \
  39. || defined(CONFIG_405EP) || defined(CONFIG_405EZ) || defined(CONFIG_405EX) \
  40. || defined(CONFIG_MPC5xxx)
  41. #if defined(CONFIG_CONS_INDEX) && defined(CFG_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. #elif defined(CONFIG_UART1_CONSOLE)
  54. return &serial1_device;
  55. #else
  56. return &serial0_device;
  57. #endif
  58. #else
  59. #error No default console
  60. #endif
  61. }
  62. #endif
  63. int serial_register (struct serial_device *dev)
  64. {
  65. dev->init += gd->reloc_off;
  66. dev->setbrg += gd->reloc_off;
  67. dev->getc += gd->reloc_off;
  68. dev->tstc += gd->reloc_off;
  69. dev->putc += gd->reloc_off;
  70. dev->puts += gd->reloc_off;
  71. dev->next = serial_devices;
  72. serial_devices = dev;
  73. return 0;
  74. }
  75. void serial_initialize (void)
  76. {
  77. #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
  78. serial_register (&serial_smc_device);
  79. #endif
  80. #if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
  81. || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
  82. serial_register (&serial_scc_device);
  83. #endif
  84. #if defined(CONFIG_405GP) || defined(CONFIG_405CR) || defined(CONFIG_440) \
  85. || defined(CONFIG_405EP) || defined(CONFIG_405EZ) || defined(CONFIG_405EX) \
  86. || defined(CONFIG_MPC5xxx)
  87. serial_register(&serial0_device);
  88. serial_register(&serial1_device);
  89. #endif
  90. #if defined(CFG_NS16550_SERIAL)
  91. #if defined(CFG_NS16550_COM1)
  92. serial_register(&eserial1_device);
  93. #endif
  94. #if defined(CFG_NS16550_COM2)
  95. serial_register(&eserial2_device);
  96. #endif
  97. #if defined(CFG_NS16550_COM3)
  98. serial_register(&eserial3_device);
  99. #endif
  100. #if defined(CFG_NS16550_COM4)
  101. serial_register(&eserial4_device);
  102. #endif
  103. #endif /* CFG_NS16550_SERIAL */
  104. #if defined (CONFIG_FFUART)
  105. serial_register(&serial_ffuart_device);
  106. #endif
  107. #if defined (CONFIG_BTUART)
  108. serial_register(&serial_btuart_device);
  109. #endif
  110. #if defined (CONFIG_STUART)
  111. serial_register(&serial_stuart_device);
  112. #endif
  113. serial_assign (default_serial_console ()->name);
  114. }
  115. void serial_devices_init (void)
  116. {
  117. device_t dev;
  118. struct serial_device *s = serial_devices;
  119. while (s) {
  120. memset (&dev, 0, sizeof (dev));
  121. strcpy (dev.name, s->name);
  122. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  123. dev.start = s->init;
  124. dev.putc = s->putc;
  125. dev.puts = s->puts;
  126. dev.getc = s->getc;
  127. dev.tstc = s->tstc;
  128. device_register (&dev);
  129. s = s->next;
  130. }
  131. }
  132. int serial_assign (char *name)
  133. {
  134. struct serial_device *s;
  135. for (s = serial_devices; s; s = s->next) {
  136. if (strcmp (s->name, name) == 0) {
  137. serial_current = s;
  138. return 0;
  139. }
  140. }
  141. return 1;
  142. }
  143. void serial_reinit_all (void)
  144. {
  145. struct serial_device *s;
  146. for (s = serial_devices; s; s = s->next) {
  147. s->init ();
  148. }
  149. }
  150. int serial_init (void)
  151. {
  152. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  153. struct serial_device *dev = default_serial_console ();
  154. return dev->init ();
  155. }
  156. return serial_current->init ();
  157. }
  158. void serial_setbrg (void)
  159. {
  160. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  161. struct serial_device *dev = default_serial_console ();
  162. dev->setbrg ();
  163. return;
  164. }
  165. serial_current->setbrg ();
  166. }
  167. int serial_getc (void)
  168. {
  169. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  170. struct serial_device *dev = default_serial_console ();
  171. return dev->getc ();
  172. }
  173. return serial_current->getc ();
  174. }
  175. int serial_tstc (void)
  176. {
  177. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  178. struct serial_device *dev = default_serial_console ();
  179. return dev->tstc ();
  180. }
  181. return serial_current->tstc ();
  182. }
  183. void serial_putc (const char c)
  184. {
  185. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  186. struct serial_device *dev = default_serial_console ();
  187. dev->putc (c);
  188. return;
  189. }
  190. serial_current->putc (c);
  191. }
  192. void serial_puts (const char *s)
  193. {
  194. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  195. struct serial_device *dev = default_serial_console ();
  196. dev->puts (s);
  197. return;
  198. }
  199. serial_current->puts (s);
  200. }
  201. #endif /* CONFIG_SERIAL_MULTI */