serial_ns16550.c 5.8 KB

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