serial.c 4.5 KB

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