serial_ns16550.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_putc_raw(const char c, const int port)
  117. {
  118. NS16550_putc(PORT, c);
  119. }
  120. static void _serial_puts(const char *s, const int port)
  121. {
  122. while (*s) {
  123. _serial_putc(*s++, port);
  124. }
  125. }
  126. static int _serial_getc(const int port)
  127. {
  128. return NS16550_getc(PORT);
  129. }
  130. static int _serial_tstc(const int port)
  131. {
  132. return NS16550_tstc(PORT);
  133. }
  134. static void _serial_setbrg(const int port)
  135. {
  136. int clock_divisor;
  137. clock_divisor = ns16550_calc_divisor(PORT, CONFIG_SYS_NS16550_CLK,
  138. gd->baudrate);
  139. NS16550_reinit(PORT, clock_divisor);
  140. }
  141. static inline void
  142. serial_putc_dev(unsigned int dev_index,const char c)
  143. {
  144. _serial_putc(c,dev_index);
  145. }
  146. static inline void
  147. serial_putc_raw_dev(unsigned int dev_index,const char c)
  148. {
  149. _serial_putc_raw(c,dev_index);
  150. }
  151. static inline void
  152. serial_puts_dev(unsigned int dev_index,const char *s)
  153. {
  154. _serial_puts(s,dev_index);
  155. }
  156. static inline int
  157. serial_getc_dev(unsigned int dev_index)
  158. {
  159. return _serial_getc(dev_index);
  160. }
  161. static inline int
  162. serial_tstc_dev(unsigned int dev_index)
  163. {
  164. return _serial_tstc(dev_index);
  165. }
  166. static inline void
  167. serial_setbrg_dev(unsigned int dev_index)
  168. {
  169. _serial_setbrg(dev_index);
  170. }
  171. #if defined(CONFIG_SYS_NS16550_COM1)
  172. DECLARE_ESERIAL_FUNCTIONS(1);
  173. struct serial_device eserial1_device =
  174. INIT_ESERIAL_STRUCTURE(1, "eserial0");
  175. #endif
  176. #if defined(CONFIG_SYS_NS16550_COM2)
  177. DECLARE_ESERIAL_FUNCTIONS(2);
  178. struct serial_device eserial2_device =
  179. INIT_ESERIAL_STRUCTURE(2, "eserial1");
  180. #endif
  181. #if defined(CONFIG_SYS_NS16550_COM3)
  182. DECLARE_ESERIAL_FUNCTIONS(3);
  183. struct serial_device eserial3_device =
  184. INIT_ESERIAL_STRUCTURE(3, "eserial2");
  185. #endif
  186. #if defined(CONFIG_SYS_NS16550_COM4)
  187. DECLARE_ESERIAL_FUNCTIONS(4);
  188. struct serial_device eserial4_device =
  189. INIT_ESERIAL_STRUCTURE(4, "eserial3");
  190. #endif
  191. #if defined(CONFIG_SYS_NS16550_COM5)
  192. DECLARE_ESERIAL_FUNCTIONS(5);
  193. struct serial_device eserial5_device =
  194. INIT_ESERIAL_STRUCTURE(5, "eserial4");
  195. #endif
  196. #if defined(CONFIG_SYS_NS16550_COM6)
  197. DECLARE_ESERIAL_FUNCTIONS(6);
  198. struct serial_device eserial6_device =
  199. INIT_ESERIAL_STRUCTURE(6, "eserial5");
  200. #endif
  201. __weak struct serial_device *default_serial_console(void)
  202. {
  203. #if CONFIG_CONS_INDEX == 1
  204. return &eserial1_device;
  205. #elif CONFIG_CONS_INDEX == 2
  206. return &eserial2_device;
  207. #elif CONFIG_CONS_INDEX == 3
  208. return &eserial3_device;
  209. #elif CONFIG_CONS_INDEX == 4
  210. return &eserial4_device;
  211. #elif CONFIG_CONS_INDEX == 5
  212. return &eserial5_device;
  213. #elif CONFIG_CONS_INDEX == 6
  214. return &eserial6_device;
  215. #else
  216. #error "Bad CONFIG_CONS_INDEX."
  217. #endif
  218. }
  219. void ns16550_serial_initialize(void)
  220. {
  221. #if defined(CONFIG_SYS_NS16550_COM1)
  222. serial_register(&eserial1_device);
  223. #endif
  224. #if defined(CONFIG_SYS_NS16550_COM2)
  225. serial_register(&eserial2_device);
  226. #endif
  227. #if defined(CONFIG_SYS_NS16550_COM3)
  228. serial_register(&eserial3_device);
  229. #endif
  230. #if defined(CONFIG_SYS_NS16550_COM4)
  231. serial_register(&eserial4_device);
  232. #endif
  233. #if defined(CONFIG_SYS_NS16550_COM5)
  234. serial_register(&eserial5_device);
  235. #endif
  236. #if defined(CONFIG_SYS_NS16550_COM6)
  237. serial_register(&eserial6_device);
  238. #endif
  239. }
  240. #endif /* !CONFIG_NS16550_MIN_FUNCTIONS */