serial.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. int i;
  98. /* FIFO enable, Tx/Rx FIFO clear */
  99. uart->UFCON = 0x07;
  100. uart->UMCON = 0x0;
  101. /* Normal,No parity,1 stop,8 bit */
  102. uart->ULCON = 0x3;
  103. /*
  104. * tx=level,rx=edge,disable timeout int.,enable rx error int.,
  105. * normal,interrupt or polling
  106. */
  107. uart->UCON = 0x245;
  108. #ifdef CONFIG_HWFLOW
  109. uart->UMCON = 0x1; /* 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. uart->UMCON = 0x10;
  116. #endif
  117. _serial_setbrg(dev_index);
  118. return (0);
  119. }
  120. #if !defined(CONFIG_SERIAL_MULTI)
  121. /* Initialise the serial port. The settings are always 8 data bits, no parity,
  122. * 1 stop bit, no start bits.
  123. */
  124. int serial_init (void)
  125. {
  126. return serial_init_dev(UART_NR);
  127. }
  128. #endif
  129. /*
  130. * Read a single byte from the serial port. Returns 1 on success, 0
  131. * otherwise. When the function is succesfull, the character read is
  132. * written into its argument c.
  133. */
  134. int _serial_getc (const int dev_index)
  135. {
  136. S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
  137. /* wait for character to arrive */
  138. while (!(uart->UTRSTAT & 0x1));
  139. return uart->URXH & 0xff;
  140. }
  141. #if defined(CONFIG_SERIAL_MULTI)
  142. static inline int serial_getc_dev(unsigned int dev_index)
  143. {
  144. return _serial_getc(dev_index);
  145. }
  146. #else
  147. int serial_getc (void)
  148. {
  149. return _serial_getc(UART_NR);
  150. }
  151. #endif
  152. #ifdef CONFIG_HWFLOW
  153. static int hwflow = 0; /* turned off by default */
  154. int hwflow_onoff(int on)
  155. {
  156. switch(on) {
  157. case 0:
  158. default:
  159. break; /* return current */
  160. case 1:
  161. hwflow = 1; /* turn on */
  162. break;
  163. case -1:
  164. hwflow = 0; /* turn off */
  165. break;
  166. }
  167. return hwflow;
  168. }
  169. #endif
  170. #ifdef CONFIG_MODEM_SUPPORT
  171. static int be_quiet = 0;
  172. void disable_putc(void)
  173. {
  174. be_quiet = 1;
  175. }
  176. void enable_putc(void)
  177. {
  178. be_quiet = 0;
  179. }
  180. #endif
  181. /*
  182. * Output a single byte to the serial port.
  183. */
  184. void _serial_putc (const char c, const int dev_index)
  185. {
  186. S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
  187. #ifdef CONFIG_MODEM_SUPPORT
  188. if (be_quiet)
  189. return;
  190. #endif
  191. /* wait for room in the tx FIFO */
  192. while (!(uart->UTRSTAT & 0x2));
  193. #ifdef CONFIG_HWFLOW
  194. /* Wait for CTS up */
  195. while(hwflow && !(uart->UMSTAT & 0x1))
  196. ;
  197. #endif
  198. uart->UTXH = c;
  199. /* If \n, also do \r */
  200. if (c == '\n')
  201. serial_putc ('\r');
  202. }
  203. #if defined(CONFIG_SERIAL_MULTI)
  204. static inline void serial_putc_dev(unsigned int dev_index, const char c)
  205. {
  206. _serial_putc(c, dev_index);
  207. }
  208. #else
  209. void serial_putc(const char c)
  210. {
  211. _serial_putc(c, UART_NR);
  212. }
  213. #endif
  214. /*
  215. * Test whether a character is in the RX buffer
  216. */
  217. int _serial_tstc(const int dev_index)
  218. {
  219. S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
  220. return uart->UTRSTAT & 0x1;
  221. }
  222. #if defined(CONFIG_SERIAL_MULTI)
  223. static inline int
  224. serial_tstc_dev(unsigned int dev_index)
  225. {
  226. return _serial_tstc(dev_index);
  227. }
  228. #else
  229. int serial_tstc(void)
  230. {
  231. return _serial_tstc(UART_NR);
  232. }
  233. #endif
  234. void _serial_puts(const char *s, const int dev_index)
  235. {
  236. while (*s) {
  237. _serial_putc (*s++, dev_index);
  238. }
  239. }
  240. #if defined(CONFIG_SERIAL_MULTI)
  241. static inline void
  242. serial_puts_dev(int dev_index, const char *s)
  243. {
  244. _serial_puts(s, dev_index);
  245. }
  246. #else
  247. void
  248. serial_puts (const char *s)
  249. {
  250. _serial_puts(s, UART_NR);
  251. }
  252. #endif
  253. #if defined(CONFIG_SERIAL_MULTI)
  254. DECLARE_S3C_SERIAL_FUNCTIONS(0);
  255. struct serial_device s3c24xx_serial0_device =
  256. INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0", "S3UART1");
  257. DECLARE_S3C_SERIAL_FUNCTIONS(1);
  258. struct serial_device s3c24xx_serial1_device =
  259. INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1", "S3UART2");
  260. DECLARE_S3C_SERIAL_FUNCTIONS(2);
  261. struct serial_device s3c24xx_serial2_device =
  262. INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2", "S3UART3");
  263. #endif /* CONFIG_SERIAL_MULTI */
  264. #endif /* defined(CONFIG_S3C2400) || defined (CONFIG_S3C2410) || defined (CONFIG_TRAB) */