serial.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. 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_405GP) || defined(CONFIG_405CR) || defined(CONFIG_440) \
  38. || defined(CONFIG_405EP) || defined(CONFIG_405EZ) || defined(CONFIG_405EX) \
  39. || defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC83xx) \
  40. || defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
  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. #elif defined(CONFIG_UART1_CONSOLE)
  54. return &serial1_device;
  55. #else
  56. return &serial0_device;
  57. #endif
  58. #elif defined(CONFIG_S3C2410)
  59. #if defined(CONFIG_SERIAL1)
  60. return &s3c24xx_serial0_device;
  61. #elif defined(CONFIG_SERIAL2)
  62. return &s3c24xx_serial1_device;
  63. #elif defined(CONFIG_SERIAL3)
  64. return &s3c24xx_serial2_device;
  65. #else
  66. #error "CONFIG_SERIAL? missing."
  67. #endif
  68. #elif defined(CONFIG_OMAP3_ZOOM2)
  69. return ZOOM2_DEFAULT_SERIAL_DEVICE;
  70. #else
  71. #error No default console
  72. #endif
  73. }
  74. struct serial_device *default_serial_console(void) __attribute__((weak, alias("__default_serial_console")));
  75. #endif
  76. int serial_register (struct serial_device *dev)
  77. {
  78. dev->init += gd->reloc_off;
  79. dev->setbrg += gd->reloc_off;
  80. dev->getc += gd->reloc_off;
  81. dev->tstc += gd->reloc_off;
  82. dev->putc += gd->reloc_off;
  83. dev->puts += gd->reloc_off;
  84. dev->next = serial_devices;
  85. serial_devices = dev;
  86. return 0;
  87. }
  88. void serial_initialize (void)
  89. {
  90. #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
  91. serial_register (&serial_smc_device);
  92. #endif
  93. #if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
  94. || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
  95. serial_register (&serial_scc_device);
  96. #endif
  97. #if defined(CONFIG_405GP) || defined(CONFIG_405CR) || defined(CONFIG_440) \
  98. || defined(CONFIG_405EP) || defined(CONFIG_405EZ) || defined(CONFIG_405EX) \
  99. || defined(CONFIG_MPC5xxx)
  100. serial_register(&serial0_device);
  101. serial_register(&serial1_device);
  102. #endif
  103. #if defined(CONFIG_SYS_NS16550_SERIAL)
  104. #if defined(CONFIG_SYS_NS16550_COM1)
  105. serial_register(&eserial1_device);
  106. #endif
  107. #if defined(CONFIG_SYS_NS16550_COM2)
  108. serial_register(&eserial2_device);
  109. #endif
  110. #if defined(CONFIG_SYS_NS16550_COM3)
  111. serial_register(&eserial3_device);
  112. #endif
  113. #if defined(CONFIG_SYS_NS16550_COM4)
  114. serial_register(&eserial4_device);
  115. #endif
  116. #endif /* CONFIG_SYS_NS16550_SERIAL */
  117. #if defined (CONFIG_FFUART)
  118. serial_register(&serial_ffuart_device);
  119. #endif
  120. #if defined (CONFIG_BTUART)
  121. serial_register(&serial_btuart_device);
  122. #endif
  123. #if defined (CONFIG_STUART)
  124. serial_register(&serial_stuart_device);
  125. #endif
  126. #if defined(CONFIG_S3C2410)
  127. serial_register(&s3c24xx_serial0_device);
  128. serial_register(&s3c24xx_serial1_device);
  129. serial_register(&s3c24xx_serial2_device);
  130. #endif
  131. serial_assign (default_serial_console ()->name);
  132. }
  133. void serial_devices_init (void)
  134. {
  135. device_t dev;
  136. struct serial_device *s = serial_devices;
  137. while (s) {
  138. memset (&dev, 0, sizeof (dev));
  139. strcpy (dev.name, s->name);
  140. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  141. dev.start = s->init;
  142. dev.putc = s->putc;
  143. dev.puts = s->puts;
  144. dev.getc = s->getc;
  145. dev.tstc = s->tstc;
  146. device_register (&dev);
  147. s = s->next;
  148. }
  149. }
  150. int serial_assign (char *name)
  151. {
  152. struct serial_device *s;
  153. for (s = serial_devices; s; s = s->next) {
  154. if (strcmp (s->name, name) == 0) {
  155. serial_current = s;
  156. return 0;
  157. }
  158. }
  159. return 1;
  160. }
  161. void serial_reinit_all (void)
  162. {
  163. struct serial_device *s;
  164. for (s = serial_devices; s; s = s->next) {
  165. s->init ();
  166. }
  167. }
  168. int serial_init (void)
  169. {
  170. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  171. struct serial_device *dev = default_serial_console ();
  172. return dev->init ();
  173. }
  174. return serial_current->init ();
  175. }
  176. void serial_setbrg (void)
  177. {
  178. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  179. struct serial_device *dev = default_serial_console ();
  180. dev->setbrg ();
  181. return;
  182. }
  183. serial_current->setbrg ();
  184. }
  185. int serial_getc (void)
  186. {
  187. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  188. struct serial_device *dev = default_serial_console ();
  189. return dev->getc ();
  190. }
  191. return serial_current->getc ();
  192. }
  193. int serial_tstc (void)
  194. {
  195. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  196. struct serial_device *dev = default_serial_console ();
  197. return dev->tstc ();
  198. }
  199. return serial_current->tstc ();
  200. }
  201. void serial_putc (const char c)
  202. {
  203. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  204. struct serial_device *dev = default_serial_console ();
  205. dev->putc (c);
  206. return;
  207. }
  208. serial_current->putc (c);
  209. }
  210. void serial_puts (const char *s)
  211. {
  212. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  213. struct serial_device *dev = default_serial_console ();
  214. dev->puts (s);
  215. return;
  216. }
  217. serial_current->puts (s);
  218. }