serial.c 6.7 KB

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