serial_ns16550.c 7.2 KB

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