serial_ns16550.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000
  4. * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
  5. */
  6. #include <common.h>
  7. #include <clock_legacy.h>
  8. #include <ns16550.h>
  9. #include <serial.h>
  10. #include <asm/global_data.h>
  11. #include <linux/compiler.h>
  12. #if !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS)
  13. DECLARE_GLOBAL_DATA_PTR;
  14. #if !defined(CONFIG_CONS_INDEX)
  15. #elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6)
  16. #error "Invalid console index value."
  17. #endif
  18. #if CONFIG_CONS_INDEX == 1 && !defined(CFG_SYS_NS16550_COM1)
  19. #error "Console port 1 defined but not configured."
  20. #elif CONFIG_CONS_INDEX == 2 && !defined(CFG_SYS_NS16550_COM2)
  21. #error "Console port 2 defined but not configured."
  22. #elif CONFIG_CONS_INDEX == 3 && !defined(CFG_SYS_NS16550_COM3)
  23. #error "Console port 3 defined but not configured."
  24. #elif CONFIG_CONS_INDEX == 4 && !defined(CFG_SYS_NS16550_COM4)
  25. #error "Console port 4 defined but not configured."
  26. #elif CONFIG_CONS_INDEX == 5 && !defined(CFG_SYS_NS16550_COM5)
  27. #error "Console port 5 defined but not configured."
  28. #elif CONFIG_CONS_INDEX == 6 && !defined(CFG_SYS_NS16550_COM6)
  29. #error "Console port 6 defined but not configured."
  30. #endif
  31. /* Note: The port number specified in the functions is 1 based.
  32. * the array is 0 based.
  33. */
  34. static struct ns16550 *serial_ports[6] = {
  35. #ifdef CFG_SYS_NS16550_COM1
  36. (struct ns16550 *)CFG_SYS_NS16550_COM1,
  37. #else
  38. NULL,
  39. #endif
  40. #ifdef CFG_SYS_NS16550_COM2
  41. (struct ns16550 *)CFG_SYS_NS16550_COM2,
  42. #else
  43. NULL,
  44. #endif
  45. #ifdef CFG_SYS_NS16550_COM3
  46. (struct ns16550 *)CFG_SYS_NS16550_COM3,
  47. #else
  48. NULL,
  49. #endif
  50. #ifdef CFG_SYS_NS16550_COM4
  51. (struct ns16550 *)CFG_SYS_NS16550_COM4,
  52. #else
  53. NULL,
  54. #endif
  55. #ifdef CFG_SYS_NS16550_COM5
  56. (struct ns16550 *)CFG_SYS_NS16550_COM5,
  57. #else
  58. NULL,
  59. #endif
  60. #ifdef CFG_SYS_NS16550_COM6
  61. (struct ns16550 *)CFG_SYS_NS16550_COM6
  62. #else
  63. NULL
  64. #endif
  65. };
  66. #define PORT serial_ports[port-1]
  67. /* Multi serial device functions */
  68. #define DECLARE_ESERIAL_FUNCTIONS(port) \
  69. static int eserial##port##_init(void) \
  70. { \
  71. int clock_divisor; \
  72. clock_divisor = ns16550_calc_divisor(serial_ports[port-1], \
  73. CFG_SYS_NS16550_CLK, gd->baudrate); \
  74. ns16550_init(serial_ports[port - 1], clock_divisor); \
  75. return 0 ; \
  76. } \
  77. static void eserial##port##_setbrg(void) \
  78. { \
  79. serial_setbrg_dev(port); \
  80. } \
  81. static int eserial##port##_getc(void) \
  82. { \
  83. return serial_getc_dev(port); \
  84. } \
  85. static int eserial##port##_tstc(void) \
  86. { \
  87. return serial_tstc_dev(port); \
  88. } \
  89. static void eserial##port##_putc(const char c) \
  90. { \
  91. serial_putc_dev(port, c); \
  92. } \
  93. static void eserial##port##_puts(const char *s) \
  94. { \
  95. serial_puts_dev(port, s); \
  96. }
  97. /* Serial device descriptor */
  98. #define INIT_ESERIAL_STRUCTURE(port, __name) { \
  99. .name = __name, \
  100. .start = eserial##port##_init, \
  101. .stop = NULL, \
  102. .setbrg = eserial##port##_setbrg, \
  103. .getc = eserial##port##_getc, \
  104. .tstc = eserial##port##_tstc, \
  105. .putc = eserial##port##_putc, \
  106. .puts = eserial##port##_puts, \
  107. }
  108. static void _serial_putc(const char c, const int port)
  109. {
  110. if (c == '\n')
  111. ns16550_putc(PORT, '\r');
  112. ns16550_putc(PORT, c);
  113. }
  114. static void _serial_puts(const char *s, const int port)
  115. {
  116. while (*s) {
  117. _serial_putc(*s++, port);
  118. }
  119. }
  120. static int _serial_getc(const int port)
  121. {
  122. return ns16550_getc(PORT);
  123. }
  124. static int _serial_tstc(const int port)
  125. {
  126. return ns16550_tstc(PORT);
  127. }
  128. static void _serial_setbrg(const int port)
  129. {
  130. int clock_divisor;
  131. clock_divisor = ns16550_calc_divisor(PORT, CFG_SYS_NS16550_CLK,
  132. gd->baudrate);
  133. ns16550_reinit(PORT, clock_divisor);
  134. }
  135. static inline void
  136. serial_putc_dev(unsigned int dev_index,const char c)
  137. {
  138. _serial_putc(c,dev_index);
  139. }
  140. static inline void
  141. serial_puts_dev(unsigned int dev_index,const char *s)
  142. {
  143. _serial_puts(s,dev_index);
  144. }
  145. static inline int
  146. serial_getc_dev(unsigned int dev_index)
  147. {
  148. return _serial_getc(dev_index);
  149. }
  150. static inline int
  151. serial_tstc_dev(unsigned int dev_index)
  152. {
  153. return _serial_tstc(dev_index);
  154. }
  155. static inline void
  156. serial_setbrg_dev(unsigned int dev_index)
  157. {
  158. _serial_setbrg(dev_index);
  159. }
  160. #if defined(CFG_SYS_NS16550_COM1)
  161. DECLARE_ESERIAL_FUNCTIONS(1);
  162. struct serial_device eserial1_device =
  163. INIT_ESERIAL_STRUCTURE(1, "eserial0");
  164. #endif
  165. #if defined(CFG_SYS_NS16550_COM2)
  166. DECLARE_ESERIAL_FUNCTIONS(2);
  167. struct serial_device eserial2_device =
  168. INIT_ESERIAL_STRUCTURE(2, "eserial1");
  169. #endif
  170. #if defined(CFG_SYS_NS16550_COM3)
  171. DECLARE_ESERIAL_FUNCTIONS(3);
  172. struct serial_device eserial3_device =
  173. INIT_ESERIAL_STRUCTURE(3, "eserial2");
  174. #endif
  175. #if defined(CFG_SYS_NS16550_COM4)
  176. DECLARE_ESERIAL_FUNCTIONS(4);
  177. struct serial_device eserial4_device =
  178. INIT_ESERIAL_STRUCTURE(4, "eserial3");
  179. #endif
  180. #if defined(CFG_SYS_NS16550_COM5)
  181. DECLARE_ESERIAL_FUNCTIONS(5);
  182. struct serial_device eserial5_device =
  183. INIT_ESERIAL_STRUCTURE(5, "eserial4");
  184. #endif
  185. #if defined(CFG_SYS_NS16550_COM6)
  186. DECLARE_ESERIAL_FUNCTIONS(6);
  187. struct serial_device eserial6_device =
  188. INIT_ESERIAL_STRUCTURE(6, "eserial5");
  189. #endif
  190. __weak struct serial_device *default_serial_console(void)
  191. {
  192. #if CONFIG_CONS_INDEX == 1
  193. return &eserial1_device;
  194. #elif CONFIG_CONS_INDEX == 2
  195. return &eserial2_device;
  196. #elif CONFIG_CONS_INDEX == 3
  197. return &eserial3_device;
  198. #elif CONFIG_CONS_INDEX == 4
  199. return &eserial4_device;
  200. #elif CONFIG_CONS_INDEX == 5
  201. return &eserial5_device;
  202. #elif CONFIG_CONS_INDEX == 6
  203. return &eserial6_device;
  204. #else
  205. #error "Bad CONFIG_CONS_INDEX."
  206. #endif
  207. }
  208. void ns16550_serial_initialize(void)
  209. {
  210. #if defined(CFG_SYS_NS16550_COM1)
  211. serial_register(&eserial1_device);
  212. #endif
  213. #if defined(CFG_SYS_NS16550_COM2)
  214. serial_register(&eserial2_device);
  215. #endif
  216. #if defined(CFG_SYS_NS16550_COM3)
  217. serial_register(&eserial3_device);
  218. #endif
  219. #if defined(CFG_SYS_NS16550_COM4)
  220. serial_register(&eserial4_device);
  221. #endif
  222. #if defined(CFG_SYS_NS16550_COM5)
  223. serial_register(&eserial5_device);
  224. #endif
  225. #if defined(CFG_SYS_NS16550_COM6)
  226. serial_register(&eserial6_device);
  227. #endif
  228. }
  229. #endif /* !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS) */