serial_ns16550.c 6.0 KB

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