serial_ns16550.c 5.8 KB

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