serial_s3c24x0.c 6.6 KB

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