serial_ns16550.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #include <linux/compiler.h>
  25. #include <ns16550.h>
  26. #ifdef CONFIG_NS87308
  27. #include <ns87308.h>
  28. #endif
  29. #include <serial.h>
  30. DECLARE_GLOBAL_DATA_PTR;
  31. #if !defined(CONFIG_CONS_INDEX)
  32. #elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 4)
  33. #error "Invalid console index value."
  34. #endif
  35. #if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1)
  36. #error "Console port 1 defined but not configured."
  37. #elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2)
  38. #error "Console port 2 defined but not configured."
  39. #elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3)
  40. #error "Console port 3 defined but not configured."
  41. #elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4)
  42. #error "Console port 4 defined but not configured."
  43. #endif
  44. /* Note: The port number specified in the functions is 1 based.
  45. * the array is 0 based.
  46. */
  47. static NS16550_t serial_ports[4] = {
  48. #ifdef CONFIG_SYS_NS16550_COM1
  49. (NS16550_t)CONFIG_SYS_NS16550_COM1,
  50. #else
  51. NULL,
  52. #endif
  53. #ifdef CONFIG_SYS_NS16550_COM2
  54. (NS16550_t)CONFIG_SYS_NS16550_COM2,
  55. #else
  56. NULL,
  57. #endif
  58. #ifdef CONFIG_SYS_NS16550_COM3
  59. (NS16550_t)CONFIG_SYS_NS16550_COM3,
  60. #else
  61. NULL,
  62. #endif
  63. #ifdef CONFIG_SYS_NS16550_COM4
  64. (NS16550_t)CONFIG_SYS_NS16550_COM4
  65. #else
  66. NULL
  67. #endif
  68. };
  69. #define PORT serial_ports[port-1]
  70. /* Multi serial device functions */
  71. #define DECLARE_ESERIAL_FUNCTIONS(port) \
  72. static int eserial##port##_init (void) {\
  73. int clock_divisor; \
  74. clock_divisor = calc_divisor(serial_ports[port-1]); \
  75. NS16550_init(serial_ports[port-1], clock_divisor); \
  76. return(0);}\
  77. static void eserial##port##_setbrg (void) {\
  78. serial_setbrg_dev(port);}\
  79. static int eserial##port##_getc (void) {\
  80. return serial_getc_dev(port);}\
  81. static int eserial##port##_tstc (void) {\
  82. return serial_tstc_dev(port);}\
  83. static void eserial##port##_putc (const char c) {\
  84. serial_putc_dev(port, c);}\
  85. static void eserial##port##_puts (const char *s) {\
  86. serial_puts_dev(port, s);}
  87. /* Serial device descriptor */
  88. #define INIT_ESERIAL_STRUCTURE(port, __name) { \
  89. .name = __name, \
  90. .start = eserial##port##_init, \
  91. .stop = NULL, \
  92. .setbrg = eserial##port##_setbrg, \
  93. .getc = eserial##port##_getc, \
  94. .tstc = eserial##port##_tstc, \
  95. .putc = eserial##port##_putc, \
  96. .puts = eserial##port##_puts, \
  97. }
  98. static int calc_divisor (NS16550_t port)
  99. {
  100. #ifdef CONFIG_OMAP1510
  101. /* If can't cleanly clock 115200 set div to 1 */
  102. if ((CONFIG_SYS_NS16550_CLK == 12000000) && (gd->baudrate == 115200)) {
  103. port->osc_12m_sel = OSC_12M_SEL; /* enable 6.5 * divisor */
  104. return (1); /* return 1 for base divisor */
  105. }
  106. port->osc_12m_sel = 0; /* clear if previsouly set */
  107. #endif
  108. #ifdef CONFIG_OMAP1610
  109. /* If can't cleanly clock 115200 set div to 1 */
  110. if ((CONFIG_SYS_NS16550_CLK == 48000000) && (gd->baudrate == 115200)) {
  111. return (26); /* return 26 for base divisor */
  112. }
  113. #endif
  114. #ifdef CONFIG_APTIX
  115. #define MODE_X_DIV 13
  116. #else
  117. #define MODE_X_DIV 16
  118. #endif
  119. /* Compute divisor value. Normally, we should simply return:
  120. * CONFIG_SYS_NS16550_CLK) / MODE_X_DIV / gd->baudrate
  121. * but we need to round that value by adding 0.5.
  122. * Rounding is especially important at high baud rates.
  123. */
  124. return (CONFIG_SYS_NS16550_CLK + (gd->baudrate * (MODE_X_DIV / 2))) /
  125. (MODE_X_DIV * gd->baudrate);
  126. }
  127. void
  128. _serial_putc(const char c,const int port)
  129. {
  130. if (c == '\n')
  131. NS16550_putc(PORT, '\r');
  132. NS16550_putc(PORT, c);
  133. }
  134. void
  135. _serial_putc_raw(const char c,const int port)
  136. {
  137. NS16550_putc(PORT, c);
  138. }
  139. void
  140. _serial_puts (const char *s,const int port)
  141. {
  142. while (*s) {
  143. _serial_putc (*s++,port);
  144. }
  145. }
  146. int
  147. _serial_getc(const int port)
  148. {
  149. return NS16550_getc(PORT);
  150. }
  151. int
  152. _serial_tstc(const int port)
  153. {
  154. return NS16550_tstc(PORT);
  155. }
  156. void
  157. _serial_setbrg (const int port)
  158. {
  159. int clock_divisor;
  160. clock_divisor = calc_divisor(PORT);
  161. NS16550_reinit(PORT, clock_divisor);
  162. }
  163. static inline void
  164. serial_putc_dev(unsigned int dev_index,const char c)
  165. {
  166. _serial_putc(c,dev_index);
  167. }
  168. static inline void
  169. serial_putc_raw_dev(unsigned int dev_index,const char c)
  170. {
  171. _serial_putc_raw(c,dev_index);
  172. }
  173. static inline void
  174. serial_puts_dev(unsigned int dev_index,const char *s)
  175. {
  176. _serial_puts(s,dev_index);
  177. }
  178. static inline int
  179. serial_getc_dev(unsigned int dev_index)
  180. {
  181. return _serial_getc(dev_index);
  182. }
  183. static inline int
  184. serial_tstc_dev(unsigned int dev_index)
  185. {
  186. return _serial_tstc(dev_index);
  187. }
  188. static inline void
  189. serial_setbrg_dev(unsigned int dev_index)
  190. {
  191. _serial_setbrg(dev_index);
  192. }
  193. DECLARE_ESERIAL_FUNCTIONS(1);
  194. struct serial_device eserial1_device =
  195. INIT_ESERIAL_STRUCTURE(1, "eserial0");
  196. DECLARE_ESERIAL_FUNCTIONS(2);
  197. struct serial_device eserial2_device =
  198. INIT_ESERIAL_STRUCTURE(2, "eserial1");
  199. DECLARE_ESERIAL_FUNCTIONS(3);
  200. struct serial_device eserial3_device =
  201. INIT_ESERIAL_STRUCTURE(3, "eserial2");
  202. DECLARE_ESERIAL_FUNCTIONS(4);
  203. struct serial_device eserial4_device =
  204. INIT_ESERIAL_STRUCTURE(4, "eserial3");
  205. __weak struct serial_device *default_serial_console(void)
  206. {
  207. #if CONFIG_CONS_INDEX == 1
  208. return &eserial1_device;
  209. #elif CONFIG_CONS_INDEX == 2
  210. return &eserial2_device;
  211. #elif CONFIG_CONS_INDEX == 3
  212. return &eserial3_device;
  213. #elif CONFIG_CONS_INDEX == 4
  214. return &eserial4_device;
  215. #else
  216. #error "Bad CONFIG_CONS_INDEX."
  217. #endif
  218. }
  219. void ns16550_serial_initialize(void)
  220. {
  221. #if defined(CONFIG_SYS_NS16550_COM1)
  222. serial_register(&eserial1_device);
  223. #endif
  224. #if defined(CONFIG_SYS_NS16550_COM2)
  225. serial_register(&eserial2_device);
  226. #endif
  227. #if defined(CONFIG_SYS_NS16550_COM3)
  228. serial_register(&eserial3_device);
  229. #endif
  230. #if defined(CONFIG_SYS_NS16550_COM4)
  231. serial_register(&eserial4_device);
  232. #endif
  233. }