serial_ns16550.c 7.3 KB

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