serial_s3c24x0.c 6.5 KB

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