serial.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * (C) Copyright 2000
  3. * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
  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. #ifdef CFG_NS16550_SERIAL
  25. #include <ns16550.h>
  26. #ifdef CFG_NS87308
  27. #include <ns87308.h>
  28. #endif
  29. #if !defined(CONFIG_CONS_INDEX)
  30. #error "No console index specified."
  31. #elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 4)
  32. #error "Invalid console index value."
  33. #endif
  34. #if CONFIG_CONS_INDEX == 1 && !defined(CFG_NS16550_COM1)
  35. #error "Console port 1 defined but not configured."
  36. #elif CONFIG_CONS_INDEX == 2 && !defined(CFG_NS16550_COM2)
  37. #error "Console port 2 defined but not configured."
  38. #elif CONFIG_CONS_INDEX == 3 && !defined(CFG_NS16550_COM3)
  39. #error "Console port 3 defined but not configured."
  40. #elif CONFIG_CONS_INDEX == 4 && !defined(CFG_NS16550_COM4)
  41. #error "Console port 4 defined but not configured."
  42. #endif
  43. /* Note: The port number specified in the functions is 1 based.
  44. * the array is 0 based.
  45. */
  46. static NS16550_t serial_ports[4] = {
  47. #ifdef CFG_NS16550_COM1
  48. (NS16550_t)CFG_NS16550_COM1,
  49. #else
  50. NULL,
  51. #endif
  52. #ifdef CFG_NS16550_COM2
  53. (NS16550_t)CFG_NS16550_COM2,
  54. #else
  55. NULL,
  56. #endif
  57. #ifdef CFG_NS16550_COM3
  58. (NS16550_t)CFG_NS16550_COM3,
  59. #else
  60. NULL,
  61. #endif
  62. #ifdef CFG_NS16550_COM4
  63. (NS16550_t)CFG_NS16550_COM4
  64. #else
  65. NULL
  66. #endif
  67. };
  68. #define PORT serial_ports[port-1]
  69. #define CONSOLE (serial_ports[CONFIG_CONS_INDEX-1])
  70. static int calc_divisor (NS16550_t port)
  71. {
  72. DECLARE_GLOBAL_DATA_PTR;
  73. #ifdef CONFIG_OMAP1510
  74. /* If can't cleanly clock 115200 set div to 1 */
  75. if ((CFG_NS16550_CLK == 12000000) && (gd->baudrate == 115200)) {
  76. port->osc_12m_sel = OSC_12M_SEL; /* enable 6.5 * divisor */
  77. return (1); /* return 1 for base divisor */
  78. }
  79. port->osc_12m_sel = 0; /* clear if previsouly set */
  80. #endif
  81. #ifdef CONFIG_OMAP1610
  82. /* If can't cleanly clock 115200 set div to 1 */
  83. if ((CFG_NS16550_CLK == 48000000) && (gd->baudrate == 115200)) {
  84. return (26); /* return 26 for base divisor */
  85. }
  86. #endif
  87. #ifdef CONFIG_APTIX
  88. #define MODE_X_DIV 13
  89. #else
  90. #define MODE_X_DIV 16
  91. #endif
  92. return (CFG_NS16550_CLK / MODE_X_DIV / gd->baudrate);
  93. }
  94. int serial_init (void)
  95. {
  96. int clock_divisor;
  97. #ifdef CFG_NS87308
  98. initialise_ns87308();
  99. #endif
  100. #ifdef CFG_NS16550_COM1
  101. clock_divisor = calc_divisor(serial_ports[0]);
  102. NS16550_init(serial_ports[0], clock_divisor);
  103. #endif
  104. #ifdef CFG_NS16550_COM2
  105. clock_divisor = calc_divisor(serial_ports[1]);
  106. NS16550_init(serial_ports[1], clock_divisor);
  107. #endif
  108. #ifdef CFG_NS16550_COM3
  109. clock_divisor = calc_divisor(serial_ports[2]);
  110. NS16550_init(serial_ports[2], clock_divisor);
  111. #endif
  112. #ifdef CFG_NS16550_COM4
  113. clock_divisor = calc_divisor(serial_ports[3]);
  114. NS16550_init(serial_ports[3], clock_divisor);
  115. #endif
  116. return (0);
  117. }
  118. void
  119. _serial_putc(const char c,const int port)
  120. {
  121. if (c == '\n')
  122. NS16550_putc(PORT, '\r');
  123. NS16550_putc(PORT, c);
  124. }
  125. void
  126. _serial_putc_raw(const char c,const int port)
  127. {
  128. NS16550_putc(PORT, c);
  129. }
  130. void
  131. _serial_puts (const char *s,const int port)
  132. {
  133. while (*s) {
  134. _serial_putc (*s++,port);
  135. }
  136. }
  137. int
  138. _serial_getc(const int port)
  139. {
  140. return NS16550_getc(PORT);
  141. }
  142. int
  143. _serial_tstc(const int port)
  144. {
  145. return NS16550_tstc(PORT);
  146. }
  147. void
  148. _serial_setbrg (const int port)
  149. {
  150. int clock_divisor;
  151. clock_divisor = calc_divisor(PORT);
  152. NS16550_reinit(PORT, clock_divisor);
  153. }
  154. void
  155. serial_putc(const char c)
  156. {
  157. _serial_putc(c,CONFIG_CONS_INDEX);
  158. }
  159. void
  160. serial_putc_raw(const char c)
  161. {
  162. _serial_putc_raw(c,CONFIG_CONS_INDEX);
  163. }
  164. void
  165. serial_puts(const char *s)
  166. {
  167. _serial_puts(s,CONFIG_CONS_INDEX);
  168. }
  169. int
  170. serial_getc(void)
  171. {
  172. return _serial_getc(CONFIG_CONS_INDEX);
  173. }
  174. int
  175. serial_tstc(void)
  176. {
  177. return _serial_tstc(CONFIG_CONS_INDEX);
  178. }
  179. void
  180. serial_setbrg(void)
  181. {
  182. _serial_setbrg(CONFIG_CONS_INDEX);
  183. }
  184. #endif