serial_ns16550.c 5.8 KB

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