serial_s3c24x0.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * (C) Copyright 2002
  3. * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <linux/compiler.h>
  9. #include <asm/arch/s3c24x0_cpu.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. #ifdef CONFIG_SERIAL1
  12. #define UART_NR S3C24X0_UART0
  13. #elif defined(CONFIG_SERIAL2)
  14. #define UART_NR S3C24X0_UART1
  15. #elif defined(CONFIG_SERIAL3)
  16. #define UART_NR S3C24X0_UART2
  17. #else
  18. #error "Bad: you didn't configure serial ..."
  19. #endif
  20. #include <asm/io.h>
  21. #include <serial.h>
  22. /* Multi serial device functions */
  23. #define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
  24. int s3serial##port##_init(void) \
  25. { \
  26. return serial_init_dev(port); \
  27. } \
  28. void s3serial##port##_setbrg(void) \
  29. { \
  30. serial_setbrg_dev(port); \
  31. } \
  32. int s3serial##port##_getc(void) \
  33. { \
  34. return serial_getc_dev(port); \
  35. } \
  36. int s3serial##port##_tstc(void) \
  37. { \
  38. return serial_tstc_dev(port); \
  39. } \
  40. void s3serial##port##_putc(const char c) \
  41. { \
  42. serial_putc_dev(port, c); \
  43. } \
  44. void s3serial##port##_puts(const char *s) \
  45. { \
  46. serial_puts_dev(port, s); \
  47. }
  48. #define INIT_S3C_SERIAL_STRUCTURE(port, __name) { \
  49. .name = __name, \
  50. .start = s3serial##port##_init, \
  51. .stop = NULL, \
  52. .setbrg = s3serial##port##_setbrg, \
  53. .getc = s3serial##port##_getc, \
  54. .tstc = s3serial##port##_tstc, \
  55. .putc = s3serial##port##_putc, \
  56. .puts = s3serial##port##_puts, \
  57. }
  58. #ifdef CONFIG_HWFLOW
  59. static int hwflow;
  60. #endif
  61. static void _serial_setbrg(const int dev_index)
  62. {
  63. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  64. unsigned int reg = 0;
  65. int i;
  66. /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
  67. reg = get_PCLK() / (16 * gd->baudrate) - 1;
  68. writel(reg, &uart->ubrdiv);
  69. for (i = 0; i < 100; i++)
  70. /* Delay */ ;
  71. }
  72. static inline void serial_setbrg_dev(unsigned int dev_index)
  73. {
  74. _serial_setbrg(dev_index);
  75. }
  76. /* Initialise the serial port. The settings are always 8 data bits, no parity,
  77. * 1 stop bit, no start bits.
  78. */
  79. static int serial_init_dev(const int dev_index)
  80. {
  81. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  82. #ifdef CONFIG_HWFLOW
  83. hwflow = 0; /* turned off by default */
  84. #endif
  85. /* FIFO enable, Tx/Rx FIFO clear */
  86. writel(0x07, &uart->ufcon);
  87. writel(0x0, &uart->umcon);
  88. /* Normal,No parity,1 stop,8 bit */
  89. writel(0x3, &uart->ulcon);
  90. /*
  91. * tx=level,rx=edge,disable timeout int.,enable rx error int.,
  92. * normal,interrupt or polling
  93. */
  94. writel(0x245, &uart->ucon);
  95. #ifdef CONFIG_HWFLOW
  96. writel(0x1, &uart->umcon); /* rts up */
  97. #endif
  98. /* FIXME: This is sooooooooooooooooooo ugly */
  99. #if defined(CONFIG_ARCH_GTA02_v1) || defined(CONFIG_ARCH_GTA02_v2)
  100. /* we need auto hw flow control on the gsm and gps port */
  101. if (dev_index == 0 || dev_index == 1)
  102. writel(0x10, &uart->umcon);
  103. #endif
  104. _serial_setbrg(dev_index);
  105. return (0);
  106. }
  107. /*
  108. * Read a single byte from the serial port. Returns 1 on success, 0
  109. * otherwise. When the function is succesfull, the character read is
  110. * written into its argument c.
  111. */
  112. static int _serial_getc(const int dev_index)
  113. {
  114. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  115. while (!(readl(&uart->utrstat) & 0x1))
  116. /* wait for character to arrive */ ;
  117. return readb(&uart->urxh) & 0xff;
  118. }
  119. static inline int serial_getc_dev(unsigned int dev_index)
  120. {
  121. return _serial_getc(dev_index);
  122. }
  123. #ifdef CONFIG_HWFLOW
  124. int hwflow_onoff(int on)
  125. {
  126. switch (on) {
  127. case 0:
  128. default:
  129. break; /* return current */
  130. case 1:
  131. hwflow = 1; /* turn on */
  132. break;
  133. case -1:
  134. hwflow = 0; /* turn off */
  135. break;
  136. }
  137. return hwflow;
  138. }
  139. #endif
  140. #ifdef CONFIG_MODEM_SUPPORT
  141. static int be_quiet = 0;
  142. void disable_putc(void)
  143. {
  144. be_quiet = 1;
  145. }
  146. void enable_putc(void)
  147. {
  148. be_quiet = 0;
  149. }
  150. #endif
  151. /*
  152. * Output a single byte to the serial port.
  153. */
  154. static void _serial_putc(const char c, const int dev_index)
  155. {
  156. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  157. #ifdef CONFIG_MODEM_SUPPORT
  158. if (be_quiet)
  159. return;
  160. #endif
  161. while (!(readl(&uart->utrstat) & 0x2))
  162. /* wait for room in the tx FIFO */ ;
  163. #ifdef CONFIG_HWFLOW
  164. while (hwflow && !(readl(&uart->umstat) & 0x1))
  165. /* Wait for CTS up */ ;
  166. #endif
  167. writeb(c, &uart->utxh);
  168. /* If \n, also do \r */
  169. if (c == '\n')
  170. serial_putc('\r');
  171. }
  172. static inline void serial_putc_dev(unsigned int dev_index, const char c)
  173. {
  174. _serial_putc(c, dev_index);
  175. }
  176. /*
  177. * Test whether a character is in the RX buffer
  178. */
  179. static int _serial_tstc(const int dev_index)
  180. {
  181. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  182. return readl(&uart->utrstat) & 0x1;
  183. }
  184. static inline int serial_tstc_dev(unsigned int dev_index)
  185. {
  186. return _serial_tstc(dev_index);
  187. }
  188. static void _serial_puts(const char *s, const int dev_index)
  189. {
  190. while (*s) {
  191. _serial_putc(*s++, dev_index);
  192. }
  193. }
  194. static inline void serial_puts_dev(int dev_index, const char *s)
  195. {
  196. _serial_puts(s, dev_index);
  197. }
  198. DECLARE_S3C_SERIAL_FUNCTIONS(0);
  199. struct serial_device s3c24xx_serial0_device =
  200. INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0");
  201. DECLARE_S3C_SERIAL_FUNCTIONS(1);
  202. struct serial_device s3c24xx_serial1_device =
  203. INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1");
  204. DECLARE_S3C_SERIAL_FUNCTIONS(2);
  205. struct serial_device s3c24xx_serial2_device =
  206. INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2");
  207. __weak struct serial_device *default_serial_console(void)
  208. {
  209. #if defined(CONFIG_SERIAL1)
  210. return &s3c24xx_serial0_device;
  211. #elif defined(CONFIG_SERIAL2)
  212. return &s3c24xx_serial1_device;
  213. #elif defined(CONFIG_SERIAL3)
  214. return &s3c24xx_serial2_device;
  215. #else
  216. #error "CONFIG_SERIAL? missing."
  217. #endif
  218. }
  219. void s3c24xx_serial_initialize(void)
  220. {
  221. serial_register(&s3c24xx_serial0_device);
  222. serial_register(&s3c24xx_serial1_device);
  223. serial_register(&s3c24xx_serial2_device);
  224. }