serial_s3c24x0.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #if defined(CONFIG_S3C2400) || defined(CONFIG_TRAB)
  22. #include <asm/arch/s3c2400.h>
  23. #elif defined(CONFIG_S3C2410)
  24. #include <asm/arch/s3c2410.h>
  25. #endif
  26. DECLARE_GLOBAL_DATA_PTR;
  27. #ifdef CONFIG_SERIAL1
  28. #define UART_NR S3C24X0_UART0
  29. #elif defined(CONFIG_SERIAL2)
  30. # if defined(CONFIG_TRAB)
  31. # error "TRAB supports only CONFIG_SERIAL1"
  32. # endif
  33. #define UART_NR S3C24X0_UART1
  34. #elif defined(CONFIG_SERIAL3)
  35. # if defined(CONFIG_TRAB)
  36. # error "TRAB supports only CONFIG_SERIAL1"
  37. # endif
  38. #define UART_NR S3C24X0_UART2
  39. #else
  40. #error "Bad: you didn't configure serial ..."
  41. #endif
  42. #include <asm/io.h>
  43. #if defined(CONFIG_SERIAL_MULTI)
  44. #include <serial.h>
  45. /* Multi serial device functions */
  46. #define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
  47. int s3serial##port##_init(void) \
  48. { \
  49. return serial_init_dev(port); \
  50. } \
  51. void s3serial##port##_setbrg(void) \
  52. { \
  53. serial_setbrg_dev(port); \
  54. } \
  55. int s3serial##port##_getc(void) \
  56. { \
  57. return serial_getc_dev(port); \
  58. } \
  59. int s3serial##port##_tstc(void) \
  60. { \
  61. return serial_tstc_dev(port); \
  62. } \
  63. void s3serial##port##_putc(const char c) \
  64. { \
  65. serial_putc_dev(port, c); \
  66. } \
  67. void s3serial##port##_puts(const char *s) \
  68. { \
  69. serial_puts_dev(port, s); \
  70. }
  71. #define INIT_S3C_SERIAL_STRUCTURE(port, name, bus) { \
  72. name, \
  73. bus, \
  74. s3serial##port##_init, \
  75. s3serial##port##_setbrg, \
  76. s3serial##port##_getc, \
  77. s3serial##port##_tstc, \
  78. s3serial##port##_putc, \
  79. s3serial##port##_puts, \
  80. }
  81. #endif /* CONFIG_SERIAL_MULTI */
  82. #ifdef CONFIG_HWFLOW
  83. static int hwflow;
  84. #endif
  85. void _serial_setbrg(const int dev_index)
  86. {
  87. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  88. unsigned int reg = 0;
  89. int i;
  90. /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
  91. reg = get_PCLK() / (16 * gd->baudrate) - 1;
  92. writel(reg, &uart->UBRDIV);
  93. for (i = 0; i < 100; i++)
  94. /* Delay */ ;
  95. }
  96. #if defined(CONFIG_SERIAL_MULTI)
  97. static inline void serial_setbrg_dev(unsigned int dev_index)
  98. {
  99. _serial_setbrg(dev_index);
  100. }
  101. #else
  102. void serial_setbrg(void)
  103. {
  104. _serial_setbrg(UART_NR);
  105. }
  106. #endif
  107. /* Initialise the serial port. The settings are always 8 data bits, no parity,
  108. * 1 stop bit, no start bits.
  109. */
  110. static int serial_init_dev(const int dev_index)
  111. {
  112. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  113. #ifdef CONFIG_HWFLOW
  114. hwflow = 0; /* turned off by default */
  115. #endif
  116. /* FIFO enable, Tx/Rx FIFO clear */
  117. writel(0x07, &uart->UFCON);
  118. writel(0x0, &uart->UMCON);
  119. /* Normal,No parity,1 stop,8 bit */
  120. writel(0x3, &uart->ULCON);
  121. /*
  122. * tx=level,rx=edge,disable timeout int.,enable rx error int.,
  123. * normal,interrupt or polling
  124. */
  125. writel(0x245, &uart->UCON);
  126. #ifdef CONFIG_HWFLOW
  127. writel(0x1, &uart->UMCON); /* RTS up */
  128. #endif
  129. /* FIXME: This is sooooooooooooooooooo ugly */
  130. #if defined(CONFIG_ARCH_GTA02_v1) || defined(CONFIG_ARCH_GTA02_v2)
  131. /* we need auto hw flow control on the gsm and gps port */
  132. if (dev_index == 0 || dev_index == 1)
  133. writel(0x10, &uart->UMCON);
  134. #endif
  135. _serial_setbrg(dev_index);
  136. return (0);
  137. }
  138. #if !defined(CONFIG_SERIAL_MULTI)
  139. /* Initialise the serial port. The settings are always 8 data bits, no parity,
  140. * 1 stop bit, no start bits.
  141. */
  142. int serial_init(void)
  143. {
  144. return serial_init_dev(UART_NR);
  145. }
  146. #endif
  147. /*
  148. * Read a single byte from the serial port. Returns 1 on success, 0
  149. * otherwise. When the function is succesfull, the character read is
  150. * written into its argument c.
  151. */
  152. int _serial_getc(const int dev_index)
  153. {
  154. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  155. while (!(readl(&uart->UTRSTAT) & 0x1))
  156. /* wait for character to arrive */ ;
  157. return readb(&uart->URXH) & 0xff;
  158. }
  159. #if defined(CONFIG_SERIAL_MULTI)
  160. static inline int serial_getc_dev(unsigned int dev_index)
  161. {
  162. return _serial_getc(dev_index);
  163. }
  164. #else
  165. int serial_getc(void)
  166. {
  167. return _serial_getc(UART_NR);
  168. }
  169. #endif
  170. #ifdef CONFIG_HWFLOW
  171. int hwflow_onoff(int on)
  172. {
  173. switch (on) {
  174. case 0:
  175. default:
  176. break; /* return current */
  177. case 1:
  178. hwflow = 1; /* turn on */
  179. break;
  180. case -1:
  181. hwflow = 0; /* turn off */
  182. break;
  183. }
  184. return hwflow;
  185. }
  186. #endif
  187. #ifdef CONFIG_MODEM_SUPPORT
  188. static int be_quiet = 0;
  189. void disable_putc(void)
  190. {
  191. be_quiet = 1;
  192. }
  193. void enable_putc(void)
  194. {
  195. be_quiet = 0;
  196. }
  197. #endif
  198. /*
  199. * Output a single byte to the serial port.
  200. */
  201. void _serial_putc(const char c, const int dev_index)
  202. {
  203. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  204. #ifdef CONFIG_MODEM_SUPPORT
  205. if (be_quiet)
  206. return;
  207. #endif
  208. while (!(readl(&uart->UTRSTAT) & 0x2))
  209. /* wait for room in the tx FIFO */ ;
  210. #ifdef CONFIG_HWFLOW
  211. while (hwflow && !(readl(&uart->UMSTAT) & 0x1))
  212. /* Wait for CTS up */ ;
  213. #endif
  214. writeb(c, &uart->UTXH);
  215. /* If \n, also do \r */
  216. if (c == '\n')
  217. serial_putc('\r');
  218. }
  219. #if defined(CONFIG_SERIAL_MULTI)
  220. static inline void serial_putc_dev(unsigned int dev_index, const char c)
  221. {
  222. _serial_putc(c, dev_index);
  223. }
  224. #else
  225. void serial_putc(const char c)
  226. {
  227. _serial_putc(c, UART_NR);
  228. }
  229. #endif
  230. /*
  231. * Test whether a character is in the RX buffer
  232. */
  233. int _serial_tstc(const int dev_index)
  234. {
  235. struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
  236. return readl(&uart->UTRSTAT) & 0x1;
  237. }
  238. #if defined(CONFIG_SERIAL_MULTI)
  239. static inline int serial_tstc_dev(unsigned int dev_index)
  240. {
  241. return _serial_tstc(dev_index);
  242. }
  243. #else
  244. int serial_tstc(void)
  245. {
  246. return _serial_tstc(UART_NR);
  247. }
  248. #endif
  249. void _serial_puts(const char *s, const int dev_index)
  250. {
  251. while (*s) {
  252. _serial_putc(*s++, dev_index);
  253. }
  254. }
  255. #if defined(CONFIG_SERIAL_MULTI)
  256. static inline void serial_puts_dev(int dev_index, const char *s)
  257. {
  258. _serial_puts(s, dev_index);
  259. }
  260. #else
  261. void serial_puts(const char *s)
  262. {
  263. _serial_puts(s, UART_NR);
  264. }
  265. #endif
  266. #if defined(CONFIG_SERIAL_MULTI)
  267. DECLARE_S3C_SERIAL_FUNCTIONS(0);
  268. struct serial_device s3c24xx_serial0_device =
  269. INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0", "S3UART1");
  270. DECLARE_S3C_SERIAL_FUNCTIONS(1);
  271. struct serial_device s3c24xx_serial1_device =
  272. INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1", "S3UART2");
  273. DECLARE_S3C_SERIAL_FUNCTIONS(2);
  274. struct serial_device s3c24xx_serial2_device =
  275. INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2", "S3UART3");
  276. #endif /* CONFIG_SERIAL_MULTI */