serial.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #ifndef CONFIG_LWMON
  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_MPC5xxx)
  40. #if defined(CONFIG_UART1_CONSOLE)
  41. return &serial1_device;
  42. #else
  43. return &serial0_device;
  44. #endif
  45. #else
  46. #error No default console
  47. #endif
  48. }
  49. #endif
  50. static int serial_register (struct serial_device *dev)
  51. {
  52. dev->init += gd->reloc_off;
  53. dev->setbrg += gd->reloc_off;
  54. dev->getc += gd->reloc_off;
  55. dev->tstc += gd->reloc_off;
  56. dev->putc += gd->reloc_off;
  57. dev->puts += gd->reloc_off;
  58. dev->next = serial_devices;
  59. serial_devices = dev;
  60. return 0;
  61. }
  62. void serial_initialize (void)
  63. {
  64. #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
  65. serial_register (&serial_smc_device);
  66. #endif
  67. #if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
  68. || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
  69. serial_register (&serial_scc_device);
  70. #endif
  71. #if defined(CONFIG_405GP) || defined(CONFIG_405CR) || defined(CONFIG_440) \
  72. || defined(CONFIG_405EP) || defined(CONFIG_MPC5xxx)
  73. serial_register(&serial0_device);
  74. serial_register(&serial1_device);
  75. #endif
  76. serial_assign (default_serial_console ()->name);
  77. }
  78. void serial_devices_init (void)
  79. {
  80. device_t dev;
  81. struct serial_device *s = serial_devices;
  82. while (s) {
  83. memset (&dev, 0, sizeof (dev));
  84. strcpy (dev.name, s->name);
  85. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
  86. dev.start = s->init;
  87. dev.putc = s->putc;
  88. dev.puts = s->puts;
  89. dev.getc = s->getc;
  90. dev.tstc = s->tstc;
  91. device_register (&dev);
  92. s = s->next;
  93. }
  94. }
  95. int serial_assign (char *name)
  96. {
  97. struct serial_device *s;
  98. for (s = serial_devices; s; s = s->next) {
  99. if (strcmp (s->name, name) == 0) {
  100. serial_current = s;
  101. return 0;
  102. }
  103. }
  104. return 1;
  105. }
  106. void serial_reinit_all (void)
  107. {
  108. struct serial_device *s;
  109. for (s = serial_devices; s; s = s->next) {
  110. s->init ();
  111. }
  112. }
  113. int serial_init (void)
  114. {
  115. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  116. struct serial_device *dev = default_serial_console ();
  117. return dev->init ();
  118. }
  119. return serial_current->init ();
  120. }
  121. void serial_setbrg (void)
  122. {
  123. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  124. struct serial_device *dev = default_serial_console ();
  125. dev->setbrg ();
  126. return;
  127. }
  128. serial_current->setbrg ();
  129. }
  130. int serial_getc (void)
  131. {
  132. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  133. struct serial_device *dev = default_serial_console ();
  134. return dev->getc ();
  135. }
  136. return serial_current->getc ();
  137. }
  138. int serial_tstc (void)
  139. {
  140. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  141. struct serial_device *dev = default_serial_console ();
  142. return dev->tstc ();
  143. }
  144. return serial_current->tstc ();
  145. }
  146. void serial_putc (const char c)
  147. {
  148. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  149. struct serial_device *dev = default_serial_console ();
  150. dev->putc (c);
  151. return;
  152. }
  153. serial_current->putc (c);
  154. }
  155. void serial_puts (const char *s)
  156. {
  157. if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
  158. struct serial_device *dev = default_serial_console ();
  159. dev->puts (s);
  160. return;
  161. }
  162. serial_current->puts (s);
  163. }
  164. #endif /* CONFIG_SERIAL_MULTI */