serial_s3c24x0.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. static void _serial_setbrg(const int dev_index)
  59. {
  60. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  61. unsigned int reg = 0;
  62. int i;
  63. /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
  64. reg = get_PCLK() / (16 * gd->baudrate) - 1;
  65. writel(reg, &uart->ubrdiv);
  66. for (i = 0; i < 100; i++)
  67. /* Delay */ ;
  68. }
  69. static inline void serial_setbrg_dev(unsigned int dev_index)
  70. {
  71. _serial_setbrg(dev_index);
  72. }
  73. /* Initialise the serial port. The settings are always 8 data bits, no parity,
  74. * 1 stop bit, no start bits.
  75. */
  76. static int serial_init_dev(const int dev_index)
  77. {
  78. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  79. /* FIFO enable, Tx/Rx FIFO clear */
  80. writel(0x07, &uart->ufcon);
  81. writel(0x0, &uart->umcon);
  82. /* Normal,No parity,1 stop,8 bit */
  83. writel(0x3, &uart->ulcon);
  84. /*
  85. * tx=level,rx=edge,disable timeout int.,enable rx error int.,
  86. * normal,interrupt or polling
  87. */
  88. writel(0x245, &uart->ucon);
  89. _serial_setbrg(dev_index);
  90. return (0);
  91. }
  92. /*
  93. * Read a single byte from the serial port. Returns 1 on success, 0
  94. * otherwise. When the function is succesfull, the character read is
  95. * written into its argument c.
  96. */
  97. static int _serial_getc(const int dev_index)
  98. {
  99. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  100. while (!(readl(&uart->utrstat) & 0x1))
  101. /* wait for character to arrive */ ;
  102. return readb(&uart->urxh) & 0xff;
  103. }
  104. static inline int serial_getc_dev(unsigned int dev_index)
  105. {
  106. return _serial_getc(dev_index);
  107. }
  108. /*
  109. * Output a single byte to the serial port.
  110. */
  111. static void _serial_putc(const char c, const int dev_index)
  112. {
  113. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  114. /* If \n, also do \r */
  115. if (c == '\n')
  116. serial_putc('\r');
  117. while (!(readl(&uart->utrstat) & 0x2))
  118. /* wait for room in the tx FIFO */ ;
  119. writeb(c, &uart->utxh);
  120. }
  121. static inline void serial_putc_dev(unsigned int dev_index, const char c)
  122. {
  123. _serial_putc(c, dev_index);
  124. }
  125. /*
  126. * Test whether a character is in the RX buffer
  127. */
  128. static int _serial_tstc(const int dev_index)
  129. {
  130. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  131. return readl(&uart->utrstat) & 0x1;
  132. }
  133. static inline int serial_tstc_dev(unsigned int dev_index)
  134. {
  135. return _serial_tstc(dev_index);
  136. }
  137. static void _serial_puts(const char *s, const int dev_index)
  138. {
  139. while (*s) {
  140. _serial_putc(*s++, dev_index);
  141. }
  142. }
  143. static inline void serial_puts_dev(int dev_index, const char *s)
  144. {
  145. _serial_puts(s, dev_index);
  146. }
  147. DECLARE_S3C_SERIAL_FUNCTIONS(0);
  148. struct serial_device s3c24xx_serial0_device =
  149. INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0");
  150. DECLARE_S3C_SERIAL_FUNCTIONS(1);
  151. struct serial_device s3c24xx_serial1_device =
  152. INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1");
  153. DECLARE_S3C_SERIAL_FUNCTIONS(2);
  154. struct serial_device s3c24xx_serial2_device =
  155. INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2");
  156. __weak struct serial_device *default_serial_console(void)
  157. {
  158. #if defined(CONFIG_SERIAL1)
  159. return &s3c24xx_serial0_device;
  160. #elif defined(CONFIG_SERIAL2)
  161. return &s3c24xx_serial1_device;
  162. #elif defined(CONFIG_SERIAL3)
  163. return &s3c24xx_serial2_device;
  164. #else
  165. #error "CONFIG_SERIAL? missing."
  166. #endif
  167. }
  168. void s3c24xx_serial_initialize(void)
  169. {
  170. serial_register(&s3c24xx_serial0_device);
  171. serial_register(&s3c24xx_serial1_device);
  172. serial_register(&s3c24xx_serial2_device);
  173. }