serial_s3c24x0.c 6.2 KB

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